;
;   bdi_modul on channel 2
;
;  using : str  8 for work
;          str  9 for test if datalogger is on
;          str 10 reserve
;          str 11 reserve
;
;          1111111111122222222233333333334444444444555555555566666666667
;01234567890123456789012345678901234567890123456789012345678901234567890
;BDI     2<2dddddddddddddddddddddddddddddddddddddddddd
;           -data for rd and wr
;          --sender kennung bei intermodultransfer
;        ----eigene modulnummer
;------------filename
;
	setbaud 1,1200,8,n,1	;Modul Default
        mode_reco
        ;
        movd    "datalogger",s9,200
        movd    "notelzprint",s9,220
        movd    "noios_update",s9,240
        movd    "BDI            /l/n",s8,0
        movd    "2<2",s8,8
        movd    " READY for WORK with BDI/l/n",s8,11
        out     s8,0,c4         ;to sv-mon
        setc    0,1             ;timeout on request cnounter
        setc    1,1             ;buffer full counter
        sett    100             ;1. timeout 10 sec
reset_0:
        setc    2,5             ;counter for compare
        movd    "2<2",s8,8
        setl    10,1     ;modul ready
        wait    rx_sign,tf_sign,im_sign
        ;warte auf anforederung or timeout
        ;
        ; auforderung:
        ; set channl is busy
        setl    10,2
        case    tf_sign,test_im_sign
        ;
        ; time to get data from hcs
        ;
	sett	100		;default timeout seting
        buffst          ;space in buffer?
        jz      bdi_buff_full
        ; okay platz vorhanden
        ; send request command
        wrd     "R/n"
        ;
        ; wait akn or timeout 2 sec
        ;
        sett    20
bdi_rq1:
        wait    tf_sign,rx_sign
        case    rx_sign,bdi_rq_timeout
        rds     s8,11
        jz      bdi_rq1         ;if only <cr>
	;
        ; test if emty
        ;
        sett    100
        compd   "E/n",s8,11
        jz      reset_0
        ;
        ; test if "ERR_xxx/n"
        ;
        compd   "ERR",s8,11
        jz      bdi_rq_error
        ;
	out	s8,0,c4
        ; start old-request
        ;
        wrd     "O/n"
        ;
bdi_rq2:
        sett    20
        wait    rx_sign,tf_sign
        case    rx_sign,bdi_rq_timeout
        rds     s8,100
        jz      bdi_rq2         ;only <cr>
	comps	s8,11,s8,100,80
        jnz     bdi_comp_er
        ;
        ; store to buffer
        ;
        buffwr  s8,11
        ;
;************************************************
;
; send strings to data-logger
;
;
        compd   "datalogger",s9,200
        jnz     no_datalogger
        setc    3,2             ;2 versuche fuer printout
print_loop:
        out     s8,11,c5        ;print to datalogger
        jz      datalog_end
        compc   15,2
        jz      printer_offline
        decc    3
        jnz     print_loop
;
; printer error
;
printer_offline:
        out     s8,0,c4
        movs    s8,0,s9,0,80
        movd    " PRINTER OFFLINE_ERROR/n",s9,11
        out     s9,0,c4
;
;************************************************
; 
; send string to telfonzellen printer
;
; only if "C-reco"
;
datalog_end:
no_datalogger:
        compd   "telzprint",s9,220
        jnz     no_c_reco
        compd   "C",s8,11
        jnz     no_c_reco
        out     s8,10,c1        ;send to telefonzellen printer
        ;
no_c_reco:
        sett    30
        jmp     reset_0
        ;
;
; on compare error
;
bdi_comp_er:
        decc    2       ;anzahl compare
        jz      mes_comp_err
        wrd     "O/n"
        jmp     bdi_rq1 ; repeat
;
;
mes_comp_err:
        movd    " BDI-ERROR * ON COMPARE */n",s8,11
        out     s8,11,c4
        sett    100
        jmp     reset_0
;
;
;  timeout on request
;
bdi_rq_timeout:
        sett    100
        decc    0
        jnz     reset_0         ;mes alle 60 sec
        movd    " BDI-ERROR * TIMEOUT ON REQUEST */n",s8,11
        out     s8,11,c4
        setc    0,6
        sett    100
        jmp     reset_0
;
;
;   on request "ERR_xxx"
;
bdi_rq_error:
        out     s8,0,c4
        sett    100
        jmp     reset_0
;
;
bdi_buff_full:
        sett    100
        decc    1
        jnz     reset_0
        movd    " BDI-ERROR * BUFFER FULL */n",s8,11
        out     s8,0,c4
        setc    1,6
        sett    100
        jmp     reset_0
;
;************************************************
;
; on im_sign
;    send to channel and return ackn
;
test_im_sign:
        case    im_sign,test_rx_sign
        in      s8,10           ;get absender und data
        ;
        compd   "N ",s8,11
        jz      cnd_cmd
        compd   "SH",s8,11
        jz      sh_cmd
        ;
        ; SF CMD
        ;
        wrs     s8,11,100           ;data to channel
mes_rep:
        sett    30
        wait    rx_sign,tf_sign
        case    rx_sign,bdi_mes_timeout
        rds     s8,100
        jz      mes_rep
        compd   "ERR",s8,100
        jnz     no_mes_er
        ;
        ; send error to sv_mon
        ;
        out     s8,0,c4
        movs    s8,100,s8,11,20
        out     s8,0,c4
;
no_mes_er:
        movs    s8,100,s8,11,20
        anss    s8,10
        sett    30
        jmp     reset_0
;
bdi_mes_timeout:
        out     s8,0,c4
        movd    "ERR_TO/n",s8,11
        anss    s8,10
        out     s8,0,c4
        sett    20
        jmp     reset_0
;
sh_cmd:
        wrs     s8,11,100           ;data to channel
mes_1_rep:
        sett    30
        wait    rx_sign,tf_sign
        case    rx_sign,bdi_mes_timeout
        rds     s8,100
        jz      mes_1_rep
        compd   "ERR",s8,100
        jnz     no_mes_1_err
        ;
        ; send error to sv_mon
        ;
        out     s8,0,c4
        movs    s8,100,s8,11,20
        out     s8,0,c4
        jmp     sh_cmd_end
;
no_mes_1_err:
;
; If test "TLN is OFFHOOK" is not used delete next two lines and
;
        out     s8,10,c3        ;test ob TLN ist onhook
        jnz     no_mes_1_err
;
; enable next line
;        movs    s8,100,s8,11,20
;
sh_cmd_end:
        anss    s8,10
        sett    30
        jmp     reset_0
;
;
cnd_cmd:
        wrs     s8,11,100           ;data to channel
        compd   "0",s8,10
        jz      cnd_edv
;
; cnd from IOS
;
cnd_loop:
;        sett    10
;        wait    rx_sign,tf_sign
;        case    rx_sign,cnd1_okay
;        rds     s8,100
;        jz      cnd_loop
;        jmp     cnd_error
;
cnd1_okay:
        movd    "OK/n",s8,11
        anss    s8,10
        sett    30
        jmp     reset_0
;
cnd_error:
        movs    s8,100,s8,11,20
        anss    s8,10
        sett    30
        jmp     reset_0
;
cnd_edv:
        sett    10
        wait    rx_sign,tf_sign
        case    rx_sign,cnd2_okay
        rds     s8,100
        jz      cnd_edv
        jmp     cnd_error
;
cnd2_okay:
        compd   "ios_update",s9,240
        jnz     cnd1_okay
        movd    "2",s8,10
        out     s8,10,c1        ;send update to IOS
        jmp     cnd1_okay
        ;
;
;************************************************
;
; on rx_sign send error
;
test_rx_sign:
        case    rx_sign,sign_err
        rds     s8,11           ;get data
        out     s8,0,c4         ;reco nicht erwartet
        movd    " BDI-ERROR * RECO not expected */n",s8,11
        out     s8,0,c4
        jmp     reset_0
        ;
;
;************************************************
;
;   sign-error
;
sign_err:
        movd    " SIGNAL_ERROR/n",s8,11
        out     s8,0,c4
        jmp     reset_0
;
;
end
