//*********************************************************
// Modul 0: Interface to the Medicall 800 Zettler
//
// 22.01.2009 All alarm messages from the serial channel
//      are read. The modul assume that the message are comming
//      from the opto interface form automation belgum.
//      The moduls calculates a new alarm number and send the
//      alarm message to the modul 1.
//
//      The alarm number calculation:
//      "alarm number input" + "7000"
//
//*********************************************************
//  COMMON AFS DEFINES
//
#define csra    0Xfc81
#define csrb    0Xfc89
#define csrc    0Xfc91
#define csrd    0xfc99
#define baud_300        0X44
#define baud_1200       0X66
#define baud_2400       0X88
#define baud_4800       0X99
#define baud_9600       0Xbb
//
#define dbs_search_str  s15,0
#define dbs_search      s15
//
#define str_dtmf        s15
#define _dtmf_pos       50
#define first_digit_wait        60
#define inter_digit             40
//
#define dbs_para        s15,100
#define dbs_pointer     s15,104
//
#define dbs_flag        s15,149
//
#define fdt_str         s15,150
#define dt_str          s15,180
#define lfdt_str        21
#define dt_str_year     s15,180
#define dt_str_month    s15,182
#define dt_str_day      s15,184
#define dt_str_weekday  s15,186
#define dt_str_hour     s15,188
#define dt_str_minute   s15,190
//
#define calc_erg_flag   s15,204
#define calc_p1         s15,210
#define calc_p2         s15,220
#define calc_erg        s15,230
#define calc_erg6       s15,232
#define calc_erg5       s15,233
#define calc_erg4       s15,234
#define calc_erg3       s15,235
#define calc_erg2       s15,236
#define calc_rest       s15,240
#define calc_rest7      s15,241
#define calc_rest6      s15,242
#define calc_rest5      s15,243
#define calc_rest4      s15,244
#define calc_rest3      s15,245
#define calc_rest2      s15,246
#define calc_rest1      s15,247
//
#define dbs_str         s14
#define dbs_str_0       s14,0
#define frame2_number   s14,240
#define error_led_mem   s14,210
#define error_led_m0    s14,210
#define led_error_m0    15
#define error_led_m1    s14,211
#define led_error_m1    14
#define error_led_m2    s14,212
#define led_error_m2    13
#define error_led_m3    s14,213
#define led_error_m3    12
#define date_send       s14,250
#define para_date       "@D/n"
//*********************************************************
// Buffer 0 definitionen
//***********************************************
#define buffer_m0               0
#define buffer_notify           buffer_m0
//************************************************
//*********************************************************
//*********************************************************
// Buffer 1 definitionen
//***********************************************
#define buffer_m1               1
//************************************************
//*********************************************************
//*********************************************************
// Buffer 2 definitionen
//***********************************************
#define buffer_m2               2
//************************************************
//*********************************************************
//*********************************************************
// Buffer 3 definitionen
//***********************************************
#define buffer_m3               3
//************************************************
//***********************************************
// Stringkonstant
// Modul 0
//
#define mess_startup1   " Alcatel 4400 (DHS 3) DIGITAL Interface V2.1/n"
#define mess_startup2   " * MED800.DW0 * BY HOERHAGER JOSEF AOHE       03//09/n"
#define mess_startup3   " Ready for working !/l/n"
//
#define mess_send_ack           " Sending *ACK*/n"
#define mess_send_nak           " Sending *NAK*/n"
#define mess_timeout            " AHL-Timeout nothing received within 5 min/n"
//
#define mess_error_to           " ERR_TO/n"     //for signalling a timeout
//
#define mess_test_opto_interface        "RX220A???00000"
#define mess1_test_opto_interface       "RX220E???7000   "
//***********************************************
// Values
//
#define ahl_timeout             3      // units 10sec
#define number_of_repeat        1       //
#define timeout_repeat          40      //
#define timeout_answer          30      // waiting for reply-mess (m3)
#define how_much                "80 "
//
#define test_contact            6       // relais on led 6
//
//***********************************************
// Counters
//
#define counter_timeout         1
#define repeat_counter          12
#define watch_cnt               2
//
// ack = 6
// nak = 21
// eot = 4
// enq = 5
//*********************************************************
// Resultcodes
//
#define result_ack      compc   14,6    //ein ack ?
#define result_nak      compc   14,21   //ein nak ?
#define reco_ok         compc   15,0    //rx_opus300 okay?
//
//*********************************************************
// AHL-STATES
//
#define ahl_idle_state          " AHL-State IDLE/n"
#define ahl_send_state          " AHL-State TRANSMIT/n"
#define ahl_receive_state       " AHL-State RECEIVE/n"
#define ahl_send_wait_state     " AHL-State Transmit WAIT/n"
//
//*********************************************************
// Strings
//
#define modul_str               s0
//
#define ahl_in_str              s10
#define ahl_out_str             s11
#define ahl_wstr                s11
//*********************************************************
// modul defines
//
#define instr                   s0
#define wstr                    s0
//
//*********************************************************
// Moduls
//
#define modul_notify            c1
#define modul_own               c0
#define led_error_own           led_error_m0
//
//*******************************************************************
//*******************************************************************
//*******************************************************************
// DBS 4 for handling the alarms
//
#define _al_board_adr   0       //hardware adresse of board (00...99)
#define _al_number      3       //alarm number on the board (00...99)

#define _on_off_level   6       //alarm on on level 1, or 0, (0|1)

#define _al_state       8       //state of the alarm (0,...9,) init to 0,

#define repeat_to1      10      //number of repeating the alarm (00,...99,)

#define repeat_to2      13      //number of repeating the alarm (00,...99,)

#define retrys          17      //the aktual number of repeating (00,...99,)
                                //init to 00,
#define _al_type        19      //type of alarm A|M alarm or message
//***********************************************
// if type of alarm = M
//
#define _message        20
#define destination     20      //destination type T (terminal)
                                //                 G (group)
#define call_type       21      //calling type N (normal)
                                //             U (urgent)
#define set_number      22      //set or group called number 10 digit
#define ack_request     32      //ack request N = no Y = ack request
#define info_mess       33      //message to subset up to 128 bytes
                                // for this application limit to
                                // 36 char.
#define end_of_message  70      //
#define lmessage        50
//***********************************************
// if type of alarm = A
//
#define _alarm          20
#define alarm_number    20      //alarm number (0000...9999)
#define alarm_mess      21      //message to the alarm_device
                                // for this application limit to
                                // 44 char
#define lalarm          50
//
//*********************************************************
// init the modul
//*********************************************************
//  AFS - RESET LABEL:
//
        setbaud 1,9600,7,e,1
        mode_opus300
        movd    para_date,date_send
        sett    10
        wait    tf_sign
//
//
// send signon mess
//
        movd    mess_startup1,wstr,100
        out     wstr,100,c4
        movd    mess_startup2,wstr,100
        out     wstr,100,c4
        movd    mess_startup3,wstr,100
        out     wstr,100,c4
error_main:
        mode_opus300
        call    init_ahl
        setc    watch_cnt,3
        sendsig   ans_sign,c1           // send sign to notification handler
        jmp     main
//***********************************************
// MAIN
main:
        setl    8,1
main_loop:
        jmp     main_skip1
//***********************************************/
main_skip1:
        setl    8,1
        sett    100                             // units 10 sec
        wait    im_sign,tf_sign,rx_sign,ans_sign,cntl_sign
        setl    8,2
        case    tf_sign,test_rx_state
        decc    counter_timeout                 // ahl timeout --
        jnz     main_loop
//
        jmp     main_loop       // if observation is used comment out
                                // this line
//
//***********************************************
//
test_rx_state:
        case    rx_sign,test_cntl_state
        call    opus_rxstate
        jmp     main_loop
//
mess_error:
        movd    "  M0: Error on Input-message/n",modul_str,0
        out     modul_str,0,c4
        out     date_send,c4
        jmp     main_loop
//
test_cntl_state:
        case    cntl_sign,test_ans_state
        call    opus_cntlstate
        jmp     main_loop
//
test_ans_state:
        case    ans_sign,test_im_state      //no message signal
        readb   wstr,100,buffer_m1
        jz      main
//
        sendsig ans_sign,modul_own      // send sign to notification handler
        compd   mess1_test_opto_interface,wstr,100
        jz      reset_to_link
        compd   mess_test_opto_interface,wstr,100
        jnz     skip_opto_test
reset_to_link:
        setc    watch_cnt,2
        movd    "0",error_led_m0
        setl    led_error_own,0
        jmp     main_loop

skip_opto_test:
        compd   "RX00",wstr,100
        jnz     test_for_mx
//
// input in wstr,100:
//            1
//  012345678901234567890
// "RX220EfffaaaaaaaaP~~~~~~~~~~~~~~~~text..../n"
//
//        movs    wstr,109,calc_p1,8
//        movd    "7000 ",calc_p2
//        acall   add
//        compd   "0E",wstr,104
//        jnz     test_0a
//        movs    calc_erg,wstr,109,8
//        jmp     skip_calc
test_0a:
//        movd	"        ",wstr,109	// clear old number
//        movs    calc_erg4,wstr,109,4
//
skip_calc:
        writeb  wstr,100,buffer_notify
        sendsig ans_sign,modul_notify
//*******************************************************************
// send acknowledge message
//
//"RX220E???112345678   "
// 0000000000111111111
// 0123456789012345678
//
//        movd    "K",wstr,105
//        movd    "ON/n",wstr,109
//        out     wstr,100,c4
//        movs    wstr,100,ahl_out_str,0,150
//        opuswrs ahl_out_str,4,150       //sende STX data BCC BCC ETX
//        out     date_send,c4
//        out     wstr,100,c4
//
        movd    "T",wstr,105
        movd    "O/n",wstr,109
        out     wstr,100,c4
        movs    wstr,100,ahl_out_str,0,150
        opuswrs ahl_out_str,4,150       //sende STX data BCC BCC ETX
        out     date_send,c4
//        out     wstr,100,c4
//
//"RX220E???112345678   "
// 0000000000111111111
// 0123456789012345678
//
/* copy 1
        movd    "        ",calc_p1
        movs    wstr,109,calc_p1,3
        movd    "2000 ",calc_p2
        acall   add
        movs    calc_erg4,wstr,109,4
//
        writeb  wstr,100,buffer_notify
        sendsig ans_sign,modul_notify
        out     wstr,100,c4
//
end of copy 1
*/
//
//"RX220E???112345678   "
// 0000000000111111111
// 0123456789012345678
//
/* copy 2
//
        movd    "3000 ",calc_p2
        acall   add
        movs    calc_erg4,wstr,109,4
//
        writeb  wstr,100,buffer_notify
        sendsig ans_sign,modul_notify
        out     wstr,100,c4
//
end of copy 1
*/
//        call    opus_txstate
        jmp     main_loop
//
//*******************************************************************
//
test_for_mx:
        compd   "MX",wstr,100
        jnz     test_for_next
        out     wstr,100,c4
        movd    "TX22",ahl_out_str,0
        call    opus_txstate
        jmp     main
//
//      wrong message
//
test_for_next:
        out     wstr,100,c4
        movd    "  M2: WRONG Message from the buffer/n",wstr,100
        out     wstr,100,c4
        jmp     main
//
//
//*********************************************************
//
test_im_state:
        case    im_sign,main_loop      //no message signal
        in      wstr,100
        compd   "stop",wstr,100
        jz      wait_start1
        writeb  modul_str,10,buffer_notify
        sendsig ans_sign,modul_notify
        anss    wstr,100,c4
        jmp     main
//
wait_start1:
        ansd    "MODE STOP/n"

wait_start:
        wait    im_sign
        case    im_sign,wait_start
        in      wstr,100
        anss    wstr,100
        compd   "start",wstr,100
        jnz     wait_start
        movd    "MODE RUN/n",wstr,100
        out     wstr,100,c4
        jmp     main

//*********************************************************
//*********************************************************
//*********************************************************
//*********************************************************
// ALCATEL HOTEL/HOSPITAL LINK "AHL"
// PABX: 4400 DHS3
//       NOTIFICATION-SERVER
// Revision: V 1.0
//
// setting up stx ... etx parameter
//
init_ahl:
        setp    0x7f00,0x03
	setp	0x7f01,0x00
        setp    0x7f03,0x03     ;select crc-calculation Notification Server
//
        mode_opus300
//
        movd    "AHL-RX_TX0          /n",ahl_wstr,200
        movd    "AHL-RX_M0           /n",ahl_in_str,0
        movd    "AHL-TX_M0           /n",ahl_out_str,0
        movd    ahl_idle_state,ahl_wstr,211
        out     ahl_wstr,200,c4
        setc    counter_timeout,ahl_timeout     // set timeout units 10sec
        ret
//
//*********************************************************
// Transmitt a message
//
        mode_opus300                    //reset rx-buffer
//
opus_txstate:
        movd    ahl_send_state,ahl_wstr,211
        out     ahl_wstr,200,c4
//
        out     ahl_out_str,0,c4
        setc    repeat_counter,number_of_repeat
                //number of retry on nak's
l_data_stx:
        clear   cntl_sign               //
        opuswrs ahl_out_str,14,150       //sende STX data BCC BCC ETX
        jz      wait_ackn_data          //warte auf reaktion.
//
//***********************************************
//
        mode_opus300                    //reset rx-buffer
        movd    " TIMEOUT ERROR ON WRITE TO CHANNEL AHL (xoff)!/n",ahl_wstr,211
        out     ahl_wstr,200,c4
        decc    repeat_counter
        jnz     l_data_stx
opus_err_timeout:
mess_max_retry:
        movd    " Max retry reached!/n",ahl_wstr,211
        out     ahl_wstr,200,c4
        movd    mess_error_to,ahl_wstr,211
        out     ahl_wstr,200,c4
        jmp     nz_ret
//
wait_ackn_data:
        sett    timeout_repeat          //warte 3 sec
        wait    cntl_sign,tf_sign,rx_sign
        case    rx_sign,do_cntl_stx
        call    opus_rxstate
        jmp     wait_ackn_data
//
do_cntl_stx:
        case    cntl_sign,to_data_stx
        compc   14,6                    //ein ack ?
        jnz     data_nak
        movd    "RECEIVED *ACK*/n",ahl_wstr,211
        out     ahl_wstr,200,c4
        jmp     z_ret
//
data_nak:
        compc   14,21                   //ein nak ?
        jnz     data_wrong
        movd    " ERROR ON DATA-SEND: RECEIVED *NAK*!/n",ahl_wstr,211
        out     ahl_wstr,200,c4
        decc    repeat_counter          //nochmal senden ?
        jnz     l_data_stx              // ja
        jmp     mess_max_retry
//
data_wrong:
        movd    " ERROR ON DATA-SEND: RECEIVED *   *!/n",ahl_wstr,211
        movc    14,ahl_wstr,242        //dezimal ausgabe input char
        out     ahl_wstr,200,c4
        decc    repeat_counter          //nochmal senden ?
        jnz     l_data_stx
        jmp     mess_max_retry
//
to_data_stx:
        movd    " ERROR ON DATA-SEND: WAIT ACKN. TIMEOUT!/n",ahl_wstr,211
        out     ahl_wstr,200,c4
        decc    repeat_counter          //nochmal senden ?
        jnz     l_data_stx
        jmp     mess_max_retry
        //
//**********************************************************
//*********************************************************
// cntl_state
//
opus_cntlstate:
        movd    " ERROR CNTL-SIGN:    RECEIVED *   *!/n",ahl_wstr,211
        movc    14,ahl_wstr,242        //dezimal ausgabe input char
        out     ahl_wstr,200,c4
        ret
//
//*********************************************************
// subroutinen
//
opus_rxstate:
        movd    "RX00",ahl_in_str,10       //source of message
        movd    ahl_receive_state,ahl_wstr,211
        out     ahl_wstr,200,c4
        opusrds ahl_in_str,14
        jz      nz_ret
        compc   15,0                    // receive ok?
        jnz     error_rxopus
//
// send message and data to c4
//
        movd    mess_send_ack,ahl_wstr,211
        out     ahl_in_str,0,c4
        out     ahl_wstr,200,c4
//
        writeb  ahl_in_str,10,buffer_m1
        sendsig ans_sign,modul_own
//        wrchar  ack
        jmp     z_ret
//
error_rxopus:
        movd    mess_send_nak,ahl_wstr,211
        out     ahl_in_str,0,c4
        out     ahl_wstr,200,c4
//
        wrchar  nak
        jmp     nz_ret
//
//*********************************************************
//*********************************************************
//RETURN Z
//    using counter 0 for generall working
z_ret:
        setc    0,255
        incc    0
	ret
//
nz_ret:	
        setc    0,0
        incc    0
	ret
//
//*********************************************************

//
end

