int getsms(int sim,char* pdu) /* reads a SMS from the SIM-memory 1-10 */
{ /* returns number of SIM memory if successful */
/* on digicom the return value can be != sim */
char command[500];
char answer[1024];
char* position;
char* beginning;
char* end;
char tmp[32];
int i;
if (strcmp(mode,"digicom")==0) // Digicom reports date+time only with AT+CMGL
{
writelogfile(LOG_INFO,"Trying to get stored message");
sprintf(command,"AT+CMGL=\"ALL\"\r");
put_command(command,answer,sizeof(answer),200,0);
/* search for beginning of the answer */
for (i=1; i<11; i++)
{
sprintf(tmp,"+CMGL: %i",i);
position=strstr(answer,tmp);
if (position)
{
writelogfile(LOG_INFO,"Found a message at memory %i",i);
sim=i;
break;
}
}
}
else
{
writelogfile(LOG_INFO,"Trying to get stored message %i",sim);
sprintf(command,"AT+CMGR=%i\r",sim);
put_command(command,answer,sizeof(answer),50,0);
/* search for beginning of the answer */
position=strstr(answer,"+CMGR:");
}
if (position==0) /* no SMS received, because modem did not answer with +CMGR or +CMGL */
return 0;
beginning=position+7;
if (strstr(answer,",,0\r")) /* No SMS, because Modem answered with +CMGR: 0,,0 */
return 0;
/* After that we have the PDU or ASCII string */
end=strstr(beginning,"\r");
if ((end==0) || ((end-beginning)<4))
return 0;
end=strstr(end+1,"\r");
if ((end==0) || ((end-beginning)<4))
return 0;
/* Now we have the end of the PDU or ASCII string */
*end=0;
strcpy(pdu,beginning);
return sim;
}