示例#1
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;
}
/* 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));
}