int checkmodem(struct modem *mdm) { char answer[500]; /* Checking if modem needs PIN */ put_command(mdm,"AT+CPIN?\r",9,answer,sizeof(answer),50,0); if (!strstr(answer,"+CPIN: READY")) { LOG(L_WARN,"WARNING:sms_checkmodem: modem wants the PIN again!\n"); goto reinit; } if (mdm->mode!=MODE_DIGICOM) { put_command(mdm,"AT+CREG?\r",9,answer,sizeof(answer),100,0); if (!strchr(answer,'1') ) { LOG(L_WARN,"WARNING:sms_checkmodem: Modem is not registered to the" " network\n"); goto reinit; } } return 1; reinit: LOG(L_WARN,"WARNING:sms_checkmodem: re -init the modem!!\n"); initmodem(mdm,cds_report_func); return -1; }
/* on digicom the return value can be != sim */ static int fetchsms(struct modem *mdm, int sim, char* pdu) { char command[16]; char answer[512]; char* position; char* beginning; char* end; int foo,err; int clen; // Digicom reports date+time only with AT+CMGL if (mdm->mode==MODE_DIGICOM) { put_command(mdm,"AT+CMGL=\"ALL\"\r",14,answer, sizeof(answer),200,0); /* search for beginning of the answer */ position=strstr(answer,"+CMGL: "); if (position) { end=position+7; while (*end<'9' && *end>'0') end++; if (end==position+7) { foo = str2s(position+7,end-position-7,&err); if (!err) { LM_DBG("found a message at memory %i\n",foo); sim=foo; } position = 0; } position = 0; } } else { LM_DBG("trying to get stored message %i\n",sim); clen=sprintf(command,"AT+CMGR=%i\r",sim); put_command(mdm,command,clen,answer,sizeof(answer),50,0); /* search for beginning of the answer */ position=strstr(answer,"+CMGR:"); } /* keine SMS empfangen, weil Modem nicht mit +CMGR oder +CMGL geantwortet hat */ if (position==0) return 0; beginning=position+7; /* keine SMS, weil Modem mit +CMGR: 0,,0 geantwortet hat */ if (strstr(answer,",,0\r")) return 0; /* After that we have the PDU or ASCII string */ for( end=beginning ; *end && *end!='\r' ; end++ ); if ( !*end || end-beginning<4) return 0; for( end=end+1 ; *end && *end!='\r' ; end++ ); if ( !*end || end-beginning<4) return 0; /* Now we have the end of the PDU or ASCII string */ *end=0; strcpy(pdu,beginning); return sim; }
void print_list(t_select sl) { int k; put_command("cl", 0); while (sl.lc != NULL) { if (sl.enable & SEARCH) print_search(sl.lc, sl.fd_tty); else print_name(sl.lc, sl.fd_tty); k = 0; if (sl.lc->next && (sl.lc->next->row == sl.lc->row)) { while (k < sl.sp[sl.lc->col] - sl.lc->len) { ft_putstr_fd(" ", sl.fd_tty); k++; } print_col(sl.fd_tty); } else ft_putstr_fd("\n", sl.fd_tty); sl.lc = sl.lc->next; } }
/* deletes the selected sms from the sim card */ static void deletesms(struct modem *mdm, int sim) { char command[32]; char answer[128]; int clen; LM_DBG("deleting message %i !\n",sim); clen = sprintf(command,"AT+CMGD=%i\r",sim); put_command(mdm, command, clen, answer, sizeof(answer), 50, 0); }
int checkmodem() { char answer[500]; int status = 0; put_command("AT+CREG?\r", 9, answer, sizeof(answer), 100, 0); if(!strchr(answer,'1')) { info("Modem is not registered to the network"); status=initmodem(); } return status; }
int initmodem() { int retries = 0; int success = 0; char answer[100]; do { retries++; put_command("AT+CREG?\r", 9, answer, sizeof(answer), 100, 0); if(strchr(answer,'1')) { info("Modem is registered to the network"); success = 1; } else if (strchr(answer,'2')) { info("Modem seems to try to reach the network!"); retries--; sleep(2); } else if(strchr(answer,'5')) { info("Modem is registered to a roaming partner network"); success = 1; } else if(strstr(answer,"ERROR")) { info("Ignoring modem does not support +CREG command"); success = 1; } else { info("initmodem: Waiting 2 secs before retrying"); sleep(2); } } while((success==0) && (retries<20)); if(success == 0) goto error; put_command(MODEM_INIT, strlen(MODEM_INIT), answer,sizeof(answer), 100, 0); if(strstr(answer,"ERROR")) { err("Failed to initalize"); err("cmd [%s] returned ERROR", MODEM_INIT); goto error; } return 0; error: return -1; }
int setsmsc(struct modem *mdm, char *smsc) { char command[100]; char answer[50]; int clen; if (smsc && smsc[0]) { clen=sprintf(command,"AT+CSCA=\"+%s\"\r",smsc); put_command(mdm,command,clen,answer,sizeof(answer),50,0); } return 0; }
// checks the size of the SIM memory int check_memory(struct modem *mdm, int flag) { char answer[500]; char* posi; int laenge; int err,foo; int j, out; for(out=0,j=0;!out && j<10; j++) { if (put_command(mdm,"AT+CPMS?\r",9,answer,sizeof(answer),50,0) && (posi=strstr(answer,"+CPMS:"))!=0 ) { // Modem supports CPMS command. Read memory size if ( (posi=strchr(posi,','))!=0 ) { posi++; if ( (laenge=strcspn(posi,",\r"))!=0 ) { if (flag==USED_MEM ) { foo = str2s(posi,laenge,&err); if (err) { LM_ERR("failed to convert into integer used_memory" " from CPMS response\n"); } else { return foo; } } posi+=laenge+1; if ( (laenge=strcspn(posi,",\r"))!=0 ) { foo = str2s(posi,laenge,&err); if (err) { LM_ERR("failed to convert into integer max_memory" " from CPMS response\n"); } else { return foo; } } } } /* if(strstr) */ } /* if(put_command) */ /* if we are here -> some error happened */ if (checkmodem(mdm)!=0) { LM_WARN("something happened with the modem -> was re-init -> let's retry\n"); } else { LM_ERR("modem seems to be ok, but we had an error? I give up!\n"); out = 1; } } /* for */ if (out==0) LM_ERR("modem does not respond after 10 retries, give up!\n"); return -1; }
int check_window_size(t_select sl) { int i; int line_len; i = 0; line_len = 0; while (i < sl.max_col) { line_len += sl.sp[i] + 3; i++; } if (line_len > sl.col_len) { put_command("cl", 0); printf("window size too small.\n"); return (0); } print_list(sl); return (1); }
int initmodem(struct modem *mdm, cds_report cds_report_f) { char command[100]; char answer[100]; int retries=0; int success=0; int clen=0; int n; LOG(L_INFO,"INFO:initmodem: init modem %s on %s.\n",mdm->name,mdm->device); if (mdm->pin[0]) { /* Checking if modem needs PIN */ put_command(mdm,"AT+CPIN?\r",9,answer,sizeof(answer),50,0); if (strstr(answer,"+CPIN: SIM PIN")) { LOG(L_INFO,"INFO:initmodem: Modem needs PIN, entering PIN...\n"); clen=sprintf(command,"AT+CPIN=\"%s\"\r",mdm->pin); put_command(mdm,command,clen,answer,sizeof(answer),100,0); put_command(mdm,"AT+CPIN?\r",9,answer,sizeof(answer),50,0); if (!strstr(answer,"+CPIN: READY")) { if (strstr(answer,"+CPIN: SIM PIN")) { LOG(L_ERR,"ERROR:initmodem: Modem did not accept" " this PIN\n"); goto error; } else if (strstr(answer,"+CPIN: SIM PUK")) { LOG(L_ERR,"ERROR:initmodem: YourPIN is locked!" " Unlock it manually!\n"); goto error; } else { goto error; } } LOG(L_INFO,"INFO:initmodem: PIN Ready!\n"); sleep(5); } } if (mdm->mode==MODE_DIGICOM) success=1; else { LOG(L_INFO,"INFO:initmodem: Checking if Modem is registered to" " the network\n"); success=0; retries=0; do { retries++; put_command(mdm,"AT+CREG?\r",9,answer,sizeof(answer),100,0); if (strchr(answer,'1') ) { LOG(L_INFO,"INFO:initmodem: Modem is registered to the" " network\n"); success=1; } else if (strchr(answer,'2')) { // added by bogdan LOG(L_WARN,"WARNING:initmodem: Modems seems to try to " "reach the network! Let's wait a little bit\n"); retries--; sleep(2); } else if (strchr(answer,'5')) { // added by Thomas Stoeckel LOG(L_INFO,"INFO:initmodem: Modem is registered to a" " roaming partner network\n"); success=1; } else if (strstr(answer,"ERROR")) { LOG(L_WARN,"WARNING:initmodem: Ignoring that modem does" " not support +CREG command.\n"); success=1; } else { LOG(L_NOTICE,"NOTICE:initmodem: Waiting 2 sec. before" " retrying\n"); sleep(2); } }while ((success==0)&&(retries<20)); } if (success==0) { LOG(L_ERR,"ERROR:initmodem: Modem is not registered to the network\n"); goto error; } for( n=0 ; n<2+2*(sms_report_type==CDS_REPORT) ; n++) { /* build the command */ switch (n) { case 0: strcpy(command,"AT+CMGF=0\r"); command[8]+=(mdm->mode==MODE_ASCII || mdm->mode==MODE_DIGICOM); clen = 10; break; case 1: strcpy(command,"AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0\r"); clen = 33; break; case 2: strcpy(command,"AT+CSMP=49,167,0,241\r"); clen = 21; break; case 3: strcpy(command,"AT+CNMI=1,1,0,1,0\r"); clen = 18; break; } /* send it to modem */ retries=0; success=0; do { retries++; /*querying the modem*/ put_command(mdm,command,clen,answer,sizeof(answer),100,0); /*dealing with the answer*/ if (strstr(answer,"ERROR")) { LOG(L_NOTICE,"NOTICE:initmodem: Waiting 1 sec. before to" " retrying\n"); sleep(1); } else success=1; }while ((success==0)&&(retries<3)); /* have we succeeded? */ if (success==0) { LOG(L_ERR,"ERROR:initmodem: cmd [%.*s] returned ERROR\n", clen-1,command); goto error; } } /* end for */ if ( sms_report_type==CDS_REPORT && !cds_report_f) { LOG(L_ERR,"ERROR:initmodem:no CDS_REPORT function given\n"); goto error; } cds_report_func = cds_report_f; if (mdm->smsc[0]) { LOG(L_INFO,"INFO:initmodem: Changing SMSC to \"%s\"\n",mdm->smsc); setsmsc(mdm,mdm->smsc); } return 0; error: return -1; }
/* send sms */ int putsms( struct sms_msg *sms_messg, struct modem *mdm) { char command[500]; char command2[500]; char answer[500]; char pdu[500]; int clen,clen2; int retries; int err_code; int pdu_len; int sms_id; pdu_len = make_pdu(sms_messg, mdm, pdu); if (mdm->mode==MODE_OLD) clen = sprintf(command,"AT+CMGS=%i\r",pdu_len/2); else if (mdm->mode==MODE_ASCII) clen = sprintf(command,"AT+CMGS=\"+%.*s\"\r",sms_messg->to.len, sms_messg->to.s); else clen = sprintf(command,"AT+CMGS=%i\r",pdu_len/2-1); if (mdm->mode==MODE_ASCII) clen2=sprintf(command2,"%.*s\x1A",sms_messg->text.len, sms_messg->text.s); else clen2=sprintf(command2,"%.*s\x1A",pdu_len,pdu); sms_id = 0; for(err_code=0,retries=0;err_code<2 && retries<mdm->retry; retries++) { if (put_command(mdm,command,clen,answer,sizeof(answer),50,"\r\n> ") && put_command(mdm,command2,clen2,answer,sizeof(answer),1000,0) && strstr(answer,"OK") ) { /* no error during sending and the modem said OK */ err_code = 2; /* if reports were request, we have to fetch the sms id from the modem reply to keep trace of the status reports */ if (sms_report_type!=NO_REPORT) { sms_id = fetch_sms_id(answer); if (sms_id==-1) err_code = 1; } } else { /* we have an error */ if (checkmodem(mdm)==-1) { err_code = 0; LOG(L_WARN,"WARNING: putsms: resending last sms! \n"); } else if (err_code==0) { LOG(L_WARN,"WARNING: putsms :possible corrupted sms." " Let's try again!\n"); err_code = 1; }else { LOG(L_ERR,"ERROR: We have a FUBAR sms!! drop it!\n"); err_code = 3; } } } if (err_code==0) LOG(L_WARN,"WARNNING: something spuky is going on with the modem!" " Re-inited and re-tried for %d times without success!\n", mdm->retry); return (err_code==0?-2:(err_code==2?sms_id:-1)); }