char KeyWait(int col, int row) { // Clear keyboard and keywait get_console(1); unsigned t = 0; while(!HasKey()) { if (read_ticks() > t + 1000) { DisplayBattery(col, row); t = read_ticks(); } SVC_WAIT(50); } return GetKey(); }
/****************************************************************************** * Funcao: sndSDLC * * envia bytes para o modem SDLC * * * * Retornos: POS_SUCESS - transmissao sem erros * * POS_ERRO - falha transmissao * * POS_CANCEL - timeout trasmissao * ******************************************************************************/ STAT sndSDLC ( char * inBuf, int sz ) { char xbuff[32]; ulong tempoCOM; int st; tempoCOM = COM_TIMEOUT * 1000; /* tempo em milisegundos */ tempoCOM += get_milli_sec (); /* hora de vencimento do timeout */ flushMDM (); /* elimina buffer interno de recebimento antes do envio */ while (sz > 0 && tempoCOM >= get_milli_sec ()) { for (st = 0; get_port_status (hComModem, xbuff) != 0 && st < 50; st++) { SVC_WAIT ( 20 ); } st = sz; /* tamanho do fragmento */ if (write (hComModem, inBuf, st) > 0) { /* OK. transferencia feita */ inBuf += st; sz -= st; } else /* FALHA. Houve algum problema */ { return POS_ERRO; } } if ( sz > 0 ) /* saida por time out */ return POS_CANCEL; tmoRespSDLC = read_ticks () + 60000l; /* aciona time-out de P2 */ return POS_SUCESS; }
/****************************************************************************** * Funcao: discaSDLC * * "Inicia" discagem SDLC * * * * Retornos: POS_ERRO * * POS_SUCESS * ******************************************************************************/ STAT discaSDLC ( void ) { int idx; char dial_string[28]; char mdm_buff[42]; memset (dial_string, 0, sizeof(dial_string)); memset (mdm_buff, 0, sizeof(mdm_buff)); for (idx = 0; idx < sizeof(parmSDLC.fone); idx++) { if (parmSDLC.fone[idx] < '0' || parmSDLC.fone[idx] > '9') parmSDLC.fone[idx] = 0; } if (parmSDLC.pabx[0] != 0) /* se tem pabx copia a string */ { strcat (dial_string, parmSDLC.pabx); strcat (dial_string, ","); } strcat (dial_string, parmSDLC.fone); /* numero do telefone */ strcpy (&SDLC_CMD[AT_DISCA_SDLC][4], dial_string); debugH ("dial string=[%s]\n", &SDLC_CMD[AT_DISCA_SDLC][0]); if (ATCmdSDLC ( AT_DISCA_SDLC, 0) != POS_SUCESS) /* envia comando de discagem */ return POS_ERRO; stsSDLC = SDLC_DIALLING; tmoDiscSDLC = read_ticks () + TMO_DISC_SDLC; /* inicia timeout de discagem */ debugH ("DISCANDO SDLC\n"); return POS_SUCESS; }
unsigned long Platform_read_ticks_ms(void) { return read_ticks(); }
/****************************************************************************** * Funcao: recvSDLC * * obtem bytes recebidos pelo modem SDLC * * * * Retornos: POS_SUCESS - transmissao sem erros * * POS_ERRO - falha transmissao * * POS_CANCEL - timeout trasmissao * ******************************************************************************/ STAT recvSDLC ( char * outBuf, int * sz ) { // Devido ao teste SDLC, funcao de recepcao esta em sua forma mais simples sem considerar tamanho de resposta ou protocolos... char respBuf[1024]; int ct = 0; if (!outBuf && !sz) return POS_ERRO; while (TRUE) { get_port_status ( hComModem, respBuf ); /* status da portadora */ if ((respBuf[3] & 0x08) == 0) /* Portadora NAO presente! caiu conexao*/ { debugH ("[recvSDLC]PORTADORA NAO PRESENTE\n"); return POS_CANCEL; } /* Verifica se ha dados no buffer fisico da COM. */ SVC_WAIT (20); get_port_status (hComModem, (char *)respBuf ); if (respBuf[0] == 0) /* NAO ha novos dados */ { if (read_ticks () < tmoRespSDLC) { continue; } else { debugH ("[recvSDLC]OCORREU TIMEOUT P2\n"); return POS_ERRO; /* excedeu time-out */ } } else break; /* chegou dados */ } /* Ha dados. Transfere para o buffer sincrono. */ *sz = 0; SVC_WAIT (1000); while ((ct = read (hComModem, &outBuf[*sz], 1024)) > 0) /* desconsiderado tratamento de fragmentos */ *sz += ct; /* desconsiderado tratamento de lixo */ if (*sz > 0) { debugH ("[recvSDLC]chegou [%d]bytes\n", *sz); return POS_SUCESS; } if (ct < 0) { debugH ("[recvSDLC]falha recepcao\n", *sz); return POS_CANCEL; } return POS_ERRO; }
/****************************************************************************** * Funcao: chk_sdlc * * "verifica status da conexao SDLC * * * * Retornos: POS_ERRO * * POS_SUCESS * ******************************************************************************/ STAT chk_sdlc ( void ) { struct Opn_Blk Com3ob; int status; int ct; char mdm_buff[42]; STAT ret; get_port_status (hComModem, mdm_buff); if ((mdm_buff[3] & DCD_ON) == 0) { ret = ATCmdSDLC (-1, 2); /* ainda nao conectado, obtem resposta */ debugH ("chk_sdlc-ret[0x%0.4X]\n", ret); if (ret == SDLC_ATCMD_TMO || ret == SDLC_CONNECTED) { if (read_ticks () < tmoDiscSDLC) return POS_SUCESS; /* discando... */ else /* time-out discagem */ { stsSDLC = SDLC_NO_ANSWER; return POS_ERRO; } } stsSDLC = ret; debugH ("[chk_sdlc]ERRO_DISC:stsSDLC=0x%0.4X\n", stsSDLC); return POS_ERRO; /* falha */ } debugH ("SDLC_CONECTADO\n"); /* portadora presente, trata outros sinais */ ct = 5; do { mdm_buff[0] = DTR_RTS_ON; status = set_serial_lines (hComModem, mdm_buff); SVC_WAIT (100); } while (status != 0 && --ct > 0); ct = 5; do { status = get_port_status (hComModem, mdm_buff); SVC_WAIT (20); } while ((mdm_buff[3] & CTS_ON) == 0 && --ct > 0); Com3ob.rate = Rt_19200; Com3ob.format = Fmt_SDLC | Fmt_DTR | Fmt_RTS; Com3ob.protocol = P_sdlc_mode; Com3ob.trailer.sdlc_parms.address = 0x30; Com3ob.trailer.sdlc_parms.option = P_sdlc_sec; ct = 5; do { status = set_opn_blk (hComModem, &Com3ob); SVC_WAIT (50); } while (status != 0 && --ct > 0); ct = 5; do { mdm_buff[0] = DTR_RTS_ON; status = set_serial_lines (hComModem, mdm_buff); SVC_WAIT (100); } while (status != 0 && --ct > 0); if (status == 0) status = 5; stsSDLC = SDLC_CONNECTED; debugH ("SDLC_SINCRONIZADO\n"); return ( POS_SUCESS ); /* conexao OK */ }
STAT ATCmdSDLC ( int ind , int timeOpt ) { char cmdBuf[64]; char respBuf[64]; char aux[2]; int ct; int nv = 0, idx = 0; ulong timeOut; if (ind < -1 && ind >= MAX_CMD_SDLC) return SDLC_ERRO_MDM; if ( ind != -1 ) { /* envia comando AT para o modem */ flushMDM (); sprintf (cmdBuf, "%s\r", &SDLC_CMD[ind][0]); write (hComModem, cmdBuf, strlen (cmdBuf)); debugH ("cmdEnvMDM:%s\n", cmdBuf); if (ind == AT_DISCA_SDLC) return POS_SUCESS; } /* prepara time-out de resposta */ if (ind == -1) { if (timeOpt) timeOut = (read_ticks ()) + ((ulong)timeOpt * 1000); else return SDLC_ERRO_MDM; } else { timeOut = (read_ticks ()) + ((ulong)SDLC_T[ind] * 1000); } /* aguarda resposta pelo tempo especificado */ while (TRUE) { if ((ct = read (hComModem, aux, 1)) > 0) { if (*aux == '\n' && nv == 0) nv = 1; /* indica inicio da resposta */ if (*aux == '\r' && nv == 1) /* final resposta */ break; if (nv == 1 && idx < (sizeof (respBuf))) /* cabe no buffer */ { /* verifica se é um caracter valido imprimivel */ if (*aux < 0x20 || *aux > 0x7E ) continue; /* descarta caracter */ respBuf[idx] = *aux; idx++; /* copia caracter para buffer de resp*/ } } if ((ct < 0)) /* erro recepcao ou timeout... bye bye... */ { debugH ("ERRO RECPCAO AT MDM\n"); return SDLC_ERRO_MDM; } if (read_ticks () > timeOut) { debugH ("ERRO RECPCAO timeout\n"); return SDLC_ATCMD_TMO; } } respBuf[idx] = 0; if (ind == -1) { debugH ("comandoAT->[%s]\n", &SDLC_CMD[AT_DISCA_SDLC][0]); debugH ("msgEsp[%s],tam[%d]\n", &SDLC_R[AT_DISCA_SDLC][0], strlen (&SDLC_R[AT_DISCA_SDLC][0])); } else { debugH ("comandoAT->[%s]\n", &SDLC_CMD[ind][0]); debugH ("msgEsp[%s], ind[%d], tam[%d]\n", &SDLC_R[ind][0], ind, strlen (&SDLC_R[ind][0])); } debugH ("respCMDAT<-[%s]\n", respBuf); /* verifica se eh uma resposta positiva */ if (ind == -1) /* discagem */ { if (memcmp (&SDLC_R[AT_DISCA_SDLC][0], respBuf, strlen (&SDLC_R[AT_DISCA_SDLC][0])) == 0) { debugH ("RESP OK\n"); return SDLC_CONNECTED; } } else { if (memcmp (&SDLC_R[ind][0], respBuf, strlen (&SDLC_R[ind][0])) == 0) { debugH ("RESP OK\n"); return POS_SUCESS; } } /* procura resposta negativa na tabela, se nao encontrar, generico nele */ for (ct = 0; SDLC_E[ct][0] != 0; ct++) { if (memcmp (&SDLC_E[ct][0], respBuf, strlen (&SDLC_E[ct][0])) == 0) { switch (ct) { case 1 : return SDLC_NO_CARRIER; case 2 : return SDLC_NO_DIALTONE; case 3 : return SDLC_BUSY_DETECT; case 4 : return SDLC_NO_ANSWER; case 0 : default : return SDLC_ERRO_MDM; } } } return SDLC_ERRO_MDM; }