📄 xsmsfrm.c
字号:
/************************************************************
* recv SMS: provide raw SMS data to SMS protocol interface,
* raw SMS data includes source number, date,
* message body and other informations.
* send SMS: Message data is provided by SMS protocol, then
* call SMS send AT command.
*************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "sms.h"
#include "smshandle.h"
#include "dataformat.h"
#include "ttyfun.h"
#include "errordef.h"
#include "typedef.h"
#ifdef _LINUX_
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#else
#include <windows.h>
#endif
uchar xSmsbuf[2048];
uchar inSmsbuf[2048];
uchar rawSmsbuf[1024];
int rawSmslen = 0;
int newMsgIndex; //for siemens new message index
uchar EG_ucSmsType;
uchar EG_ucSmsCode;
/* CDMA */
//fidelix
int fidelix_init(int fd, uchar ucSmsType, uchar ucSmsMode);
int fidelix_send(int fd, const uchar *pSms, int len, const char *pDest, uchar ucSmsType, uchar ucSmsCode);
int fidelix_recv(int fd, uchar *pSms, int *len, char *pFrom, uchar ucSmsType, uchar ucSmsCode);
int fidelix_ifnew(int fd, uchar ucSmsType, uchar ucSmsCode);
int fidelix_parse(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
int fidelix_delall(int fd);
int fidelix_parse_text(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
int fidelix_parse_pdu(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
int fidelix_delone(int fd);
int fidelix_getsmscnt(int fd);
//bellwave
int bellwave_init(int fd, uchar ucSmsType, uchar ucSmsMode);
int bellwave_send(int fd, const uchar *pSms, int len, const char *pDest, uchar ucSmsType, uchar ucSmsCode);
int bellwave_recv(int fd, uchar *pSms, int *len, char *pFrom, uchar ucSmsType, uchar ucSmsCode);
int bellwave_ifnew(int fd, uchar ucSmsType, uchar ucSmsCode);
int bellwave_parse(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
int bellwave_delall(int fd);
int bellwave_delone(int fd);
int bellwave_getsmscnt(int fd);
/* GPRS */
int gprs_init(int fd, uchar ucSmsType, uchar ucSmsMode);
int gprs_send(int fd, const uchar *pSms, int len, const char *pDest, uchar ucSmsType, uchar ucSmsCode);
int gprs_recv(int fd, uchar *pSms, int *len, uchar ucSmsType, uchar ucSmsCode);
int gprs_ifnew(int fd, uchar ucSmsType, uchar ucSmsCode);
int gprs_parse(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
int gprs_delall(int fd);
int gprs_parse_pdu(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
int gprs_parse_text(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
//siemens
int siemens_init(int fd, uchar ucSmsType, uchar ucSmsMode);
int siemens_send(int fd, const uchar *pSms, int len, const char *pDest, uchar ucSmsType, uchar ucSmsCode);
int siemens_recv(int fd, uchar *pSms, int *len, char *pFrom, uchar ucSmsType, uchar ucSmsCode);
int siemens_ifnew(int fd, uchar ucSmsType, uchar ucSmsCode);
int siemens_parse(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
int siemens_delall(int fd);
//simcom
int simcom_init(int fd, uchar ucSmsType, uchar ucSmsMode);
int simcom_send(int fd, const uchar *pSms, int len, const char *pDest, uchar ucSmsType, uchar ucSmsCode);
int simcom_recv(int fd, uchar *pSms, int *len, char *pFrom, uchar ucSmsType, uchar ucSmsCode);
int simcom_ifnew(int fd, uchar ucSmsType, uchar ucSmsCode);
int simcom_parse(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
int simcom_delall(int fd);
int simcom_parse_text(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
//huawei
int huawei_init(int fd, uchar ucSmsType, uchar ucSmsMode);
int huawei_send(int fd, const uchar *pSms, int len, const char *pDest, uchar ucSmsType, uchar ucSmsCode);
int huawei_recv(int fd, uchar *pSms, int *len, char *pFrom, uchar ucSmsType, uchar ucSmsCode);
int huawei_ifnew(int fd, uchar ucSmsType, uchar ucSmsCode);
int huawei_parse(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
int huawei_delall(int fd);
int huawei_parse_text(const uchar *pRawSms, int rawlen, uchar *pMsg, int *msgLen, char *pFrom);
/**********************************************
* extern variales
**********************************************/
//extern int fd; //tty file descriptor
extern int giSmsRefBufCnt;
extern SMS_REF *smsref_head;
extern SMS_REF *smsref_tail;
//gprs SMS reference buffer
extern int initsmsrefbuf(void);
extern int clearsmsrefbuf(void);
extern SMS_REF* allocsmsrefbuf(void);
extern int freesmsrefbuf(SMS_REF *ptSmsRef);
extern int add_queue(SMS_REF *ptSmsRef);
extern SMS_REF *del_queue(void);
//debug information
extern char debugType;
extern void WMMP_TRACE(char type, const char *format, ...);
int tofdpdu(uchar *dest, const uchar *src, int len)
{
int i = 0;
char temp;
for(i = 0; i < len; i++)
{
temp = (src[i]&0xF0)>>4;
if (temp <= 0x09 && temp >= 0x00)
{
dest[i*2] = temp + 0x30;
}
else if (temp <= 0x0F && temp >= 0x0A)
{
dest[i*2] = temp + 0x37;
}
temp = src[i]&0x0F;
if (temp <= 0x09 && temp >= 0x00)
{
dest[i*2 + 1] = temp + 0x30;
}
else if (temp <= 0x0F && temp >= 0x0A)
{
dest[i*2 + 1] = temp + 0x37;
}
}
return 0;
}
int tofdbin(uchar *dest, const uchar *src, int len)
{
int i = 0, ret = 0;
uchar leftbyte = 0x00, rightbyte = 0x00, newbyte = 0x00;
uchar temp = 0x00;
if((len%2) != 0)
{
WMMP_TRACE(debugType, "\r\nError: source data length");
ret = -1;
goto tofdbin_fail;
}
for(i = 0; i < len/2; i++)
{
leftbyte = src[i*2];
rightbyte = src[i*2 + 1];
if(leftbyte >= '0' && leftbyte <= '9')
{
leftbyte -= 0x30;
}
else if(leftbyte >= 'A' && leftbyte <= 'F')
{
leftbyte -= 0x37;
}
else if(leftbyte >= 'a' && leftbyte <= 'f')
{
leftbyte -= 0x57;
}
else
{
WMMP_TRACE(debugType, "Error: source data value");
ret = -2;
goto tofdbin_fail;
}
if(rightbyte >= '0' && rightbyte <= '9')
{
rightbyte -= 0x30;
}
else if(rightbyte >= 'A' && rightbyte <= 'F')
{
rightbyte -= 0x37;
}
else if(rightbyte >= 'a' && rightbyte <= 'f')
{
rightbyte -= 0x57;
}
else
{
WMMP_TRACE(debugType, "Error: source data value");
ret = -2;
goto tofdbin_fail;
}
leftbyte &= 0x0F;
leftbyte <<= 4;
rightbyte &= 0x0F;
dest[i] = leftbyte | rightbyte;
}
return 0;
tofdbin_fail:
return ret;
}
//13328657218 ->
//00310033003300320038003600350037003200310038
int toucsnum(uchar *dest, const uchar *src, int len)
{
int i = 0, ret = 0;
char temp;
for(i = 0; i < len; i++)
{
temp = src[i];
if (temp <= 0x39 && temp >= 0x30)
{
dest[i*4] = 0x30;
dest[i*4 + 1] = 0x30;
temp = (src[i]&0xF0)>>4;
if (temp <= 0x09 && temp >= 0x00)
{
dest[i*4 + 2] = temp + 0x30;
}
else
{
WMMP_TRACE(debugType, "\r\nError: phone number");
ret = -1;
goto toucsnum_fail;
}
temp = src[i]&0x0F;
if (temp <= 0x09 && temp >= 0x00)
{
dest[i*4 + 3] = temp + 0x30;
}
else
{
WMMP_TRACE(debugType, "\r\nError: phone number");
ret = -1;
goto toucsnum_fail;
}
}
else
{
WMMP_TRACE(debugType, "\r\nError: phone number");
ret = -1;
goto toucsnum_fail;
}
}
return 0;
toucsnum_fail:
return ret;
}
//00310033003300320038003600350037003200310038 ->
//13328657218
int toasciinum(uchar *dest, const uchar *src, int len)
{
int i = 0, ret = 0;
uchar leftbyte = 0x00, rightbyte = 0x00, newbyte = 0x00;
uchar temp = 0x00;
if((len%4) != 0)
{
WMMP_TRACE(debugType, "\r\nError: source data length");
ret = -1;
goto toasciinum_fail;
}
for(i = 0; i < len/4; i++)
{
leftbyte = src[i*4 + 2];
rightbyte = src[i*4 + 3];
if(leftbyte >= '0' && leftbyte <= '9')
{
leftbyte -= 0x30;
}
else
{
WMMP_TRACE(debugType, "Error: usc phone number");
ret = -2;
goto toasciinum_fail;
}
if(rightbyte >= '0' && rightbyte <= '9')
{
rightbyte -= 0x30;
}
else
{
WMMP_TRACE(debugType, "Error: usc phone number");
ret = -2;
goto toasciinum_fail;
}
leftbyte &= 0x0F;
leftbyte <<= 4;
rightbyte &= 0x0F;
dest[i] = leftbyte | rightbyte;
}
return 0;
toasciinum_fail:
return ret;
}
//sms type and sms code means nothing for fidelix module
int fidelix_init(int fd, uchar ucSmsType, uchar ucSmsCode)
{
int ret = 0;
int i = 0;
memset(xSmsbuf, 0, sizeof(xSmsbuf));
memcpy(xSmsbuf, "ATE0\x0D", strlen("ATE0\x0D"));
cleartty(fd);//for clear tty send and recv buffer
for(i = 0; i < 5; i++)
{
WMMP_TRACE(debugType, "\r\nWrite: %s, Length: %d\r\n", xSmsbuf, strlen(xSmsbuf));
ret = writetty(fd, strlen(xSmsbuf), 2, xSmsbuf);
if(ret == 0)
{
ret = wait_for_OK(fd, "OK", 2);
if(ret == 0)
{
// WMMP_TRACE(debugType, "\n\rERROR: ATE0 does not return OK");
break;
}
}
}
if(i == 5 && ret != 0)
{
WMMP_TRACE(debugType, "\n\rERROR: ATE0 does not return OK");
ret = -SMS_ERR_AT_NOTOK;
goto fidelix_init_fail;
}
return 0;
fidelix_init_fail:
return ret;
}
//text mode:
//english and chinese sms data should not be converted, directly passed to pSms of fidelix_send function
//pdu mode:
//only supporting 8-bit coding, and the data should have been converted by application
int fidelix_send(int fd, const uchar *pSms, int len, const char *pDest, uchar ucSmsType, uchar ucSmsCode)
{
int ret = 0, msglen = 0;
// uchar szTemp[512];
/* if coding is 8-bit, the convertion is performed by application
memset(szTemp, 0, sizeof(szTemp));
ret = tofdpdu(szTemp, pSms, len);
if(ret < 0)
{
WMMP_TRACE(debugType, "\n\rError: Convert to fidelix pdu format");
return -SMS_ERR_CONV_DATA;
}
*/
memset(xSmsbuf, 0, sizeof(xSmsbuf));
if (ucSmsType == SMS_TEXT)
{//In GB coding, is there 0x00 value??? Be careful!!!
sprintf(xSmsbuf, "AT$FXTXSM=\"\",\"%s\",\"%s\"\x0D", pDest, pSms);
msglen = strlen(xSmsbuf);
}
else if (ucSmsType == SMS_PDU)
{
if (ucSmsCode == SMS_ENCODE_7BIT || ucSmsCode == SMS_ENCODE_UCS16)
{
ret = -SMS_ERR_SMS_CODE;
goto fidelix_send_fail;
}
sprintf(xSmsbuf, "AT$FXTPDU=\"\",\"%s\",\"%s\"\x0D", pDest, pSms);
msglen = strlen(xSmsbuf);
}
else
{
ret = -SMS_ERR_SMS_TYPE;
goto fidelix_send_fail;
}
cleartty(fd); //for clear tty recv and send buffer
//fidelix send pdu data
WMMP_TRACE(debugType, "\r\nWrite: %s, Length: %d\r\n", xSmsbuf, strlen(xSmsbuf));
ret = writetty(fd, msglen, 2, xSmsbuf);
if(ret < 0)
{
WMMP_TRACE(debugType, "\n\rError: Write tty");
ret = -SMS_ERR_SEND_ATCMD;
goto fidelix_send_fail;
}
ret = wait_for_OK(fd, "$FXSMS:OK", 10);
if(ret < 0)
{
WMMP_TRACE(debugType, "\n\rError: Fidelix does not receive OK after sending SMS");
ret = -SMS_ERR_SEND_SMS;
goto fidelix_send_fail;
}
return 0;
fidelix_send_fail:
return ret;
}
//AT$FXRLRM=index,1 //receive PDU data
//useful in case the message is binary format data(PDU format data)
//AT$FXRRRM output format:
//$FXRRRM:0,8,"0204060800AABBCC",but the source phone number does not exist
//$FXRLRM:0,15960095351,06/11/08,13:19:26,5,*NEW*030201000D
int fidelix_recv(int fd, uchar *pSms, int *len, char *pFrom, uchar ucSmsType, uchar ucSmsCode)
{
int ret = 0;
int msglen = 0, uiRetLen = 0;
char *p1 = NULL, *p2 = NULL, *p3 = NULL;
int newcnt = 0;
if(pSms == NULL || len == NULL)
{
WMMP_TRACE(debugType, "\n\rError: Null params");
ret = -SMS_ERR_PARAMS;
goto fidelix_recv_fail;
}
ret = fidelix_ifnew(fd, ucSmsType, ucSmsCode);
if(ret <= 0)
{
WMMP_TRACE(debugType, "\r\nFidelix failed to check new SMS");
ret = -SMS_ERR_NONEWSMS;
goto fidelix_recv_fail;
}
newcnt = ret;
cleartty(fd); //for clear tty recv buffer
//AT command
memset(xSmsbuf, 0, sizeof(xSmsbuf));
if (ucSmsType == SMS_TEXT)
{
sprintf(xSmsbuf, "AT$FXRDRM=%d\x0D", newcnt -1);
}
else if (ucSmsType == SMS_PDU)
{
if (ucSmsCode == SMS_ENCODE_7BIT || ucSmsCode == SMS_ENCODE_UCS16)
{
ret = -SMS_ERR_SMS_CODE;
goto fidelix_recv_fail;
}
sprintf(xSmsbuf, "AT$FXRLRM=%d,1\x0D", newcnt -1);
}
else
{
ret = -SMS_ERR_SMS_TYPE;
goto fidelix_recv_fail;
}
WMMP_TRACE(debugType, "\r\nWrite: %s, Length: %d\r\n", xSmsbuf, strlen(xSmsbuf));
ret = writetty(fd, strlen(xSmsbuf), 2, xSmsbuf);
if(ret == 0)
{
memset(rawSmsbuf, 0, sizeof(rawSmsbuf));
memset(inSmsbuf, 0, sizeof(inSmsbuf));
ret = readtty(fd, sizeof(inSmsbuf), 5, inSmsbuf, &uiRetLen);
if(ret == 0 && uiRetLen > 0)
{//notion: 0x00 in received message
if (ucSmsType == SMS_TEXT)
{
p1 = strstr(inSmsbuf, "$FXRDRM:");
}
else if (ucSmsType == SMS_PDU)
{
p1 = strstr(inSmsbuf, "$FXRLRM:");
}
p2 = strstr(inSmsbuf, "*NEW*");
if(p1 && p2)
{
p3 = strstr(inSmsbuf, "\x0D\x0AOK\x0D\x0A");
rawSmslen = p3 - p1;
memcpy(rawSmsbuf, p1, rawSmslen);//copy total SMS, including 0x0D and 0x0A
// *len = msglen;
// return 0;
}
else
{
WMMP_TRACE(debugType, "Error: Not new SMS");
ret = -SMS_ERR_NONEWSMS;
goto fidelix_recv_fail;
}
}
else
{
WMMP_TRACE(debugType, "Error: Read tty");
ret = -SMS_ERR_READ_TTY;
goto fidelix_recv_fail;
}
}
if (ucSmsType == SMS_PDU)
{
ret = fidelix_parse_pdu(rawSmsbuf, rawSmslen, pSms, len, pFrom);
}
else if (ucSmsType == SMS_TEXT)
{
ret = fidelix_parse_text(rawSmsbuf, rawSmslen, pSms, len, pFrom);
}
if(ret < 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -