📄 sms.c
字号:
/*
* Modify Records:
* 2007-01-25 In confirmttyspeed function, the variable trytime should be
* sizeof(autospeed)/sizeof(int) - 1.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include "sms.h"
#include "smshandle.h"
#include "ttyfun.h"
#include "xsmsfrm.h"
#include "dataformat.h"
#include "smsref.h"
#include "typedef.h"
#include "errordef.h"
#ifdef _LINUX_
#include<sys/types.h>
#include<sys/time.h>
#include<sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<syslog.h>
#include <utmp.h>
#include <unistd.h>
#else
#include <windows.h>
#endif
//receive Raw SMS data
uchar szRawSms[1024];
int iRawSmsLen;
char buffer[3000]; //be care of not overflow
char debugType = DEBUG_NONE;//DEGUG_PRINT;
/*interface out to WMMP */
int opensms(const char* ttyname, SmsHandle* hSmsHanle); //return tty file descriptor, default
int initsms(SmsHandle hSmsHandle);
int sendsms(SmsHandle hSmsHandle, const uchar *pSms_content, int len, const char *pDest);
int recvsms(SmsHandle hSmsHandle, uchar *pSms_content, int *len, char *pFrom);
int ifnewsms(SmsHandle hSmsHandle);
int closesms(SmsHandle* hSmsHandle);
int testcsq(SmsHandle hSmsHandle, uchar *csq, uchar *berr);
/* Set and get message coding
7-bit Engish message
8-bit binary message
UCS-16 Chinese or English message
*/
int setsmsconfig(SmsHandle hSmsHandle, const SMS_CONFIG* pSmsConfig);
int getsmsconfig(SmsHandle hSmsHandle, SMS_CONFIG* pSmsConfig);
/************************************************************************/
/* internal functions */
/************************************************************************/
int getmoduletype(int fd, char* ptype);
int confirmttyspeed(int fd, int* speed);
int defaultsmsconfig(SMS_CONFIG* pSmsConfig);
int initsmsfunc(uchar acModType, SMS_FUNC* pSmsFunc);
int setsmsc(int fd, uchar ucModType, const char* smsc);
void WMMP_TRACE(char type, const char *format, ...);
int setwmmpdebugtype(char debType);
/*******************************************
* extern variables
*******************************************/
/************************************************************************/
/* Definition of Interfaces */
/************************************************************************/
int opensms(const char* ttyname, SmsHandle* hSmsHanle)
{
int ret;
int openRet;
if (ttyname == NULL || hSmsHanle == NULL)
{
WMMP_TRACE(debugType, "\r\nError: opensms params.");
ret = -SMS_ERR_PARAMS;
goto opensms_fail;
}
ret = findnode(ttyname);
if (ret == -SMS_ERR_DEV_USED)
{
WMMP_TRACE(debugType, "\r\nError: %s has been opened.", ttyname);
goto opensms_fail;
}
ret = allocsmshandle(hSmsHanle);
if (ret != SMS_SUCCESS)
{
WMMP_TRACE(debugType, "\r\nError: allocate a new smshandle.");
goto opensms_fail;
}
//initialize smshandle
memset(*hSmsHanle, 0, sizeof(SMS_NODE));
memcpy((*hSmsHanle)->ttyname, ttyname, strlen(ttyname));
//open tty device
openRet = opentty(ttyname);
if (openRet < 0)
{
WMMP_TRACE(debugType, "\r\nError: open tty device.");
goto opensms_fail;
}
(*hSmsHanle)->ttyfd = openRet;
return SMS_SUCCESS;
opensms_fail:
if (openRet != SMS_SUCCESS)
{
delsmshandle(hSmsHanle);
*hSmsHanle = 0;
ret = openRet;
}
return ret;
}
int initsms(SmsHandle hSmsHandle)
{
int ret = 0;
int ttySpeed;
char acModType = UNKNOWN_MODULE;
uchar acSmsType = 0x00;
uchar acSmsCode = 0x00;
int ttyfd = -1;
//1.check SmsHandle
ret = searchsmshandle(&hSmsHandle);
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: unknow SmsHandle");
goto initsms_fail;
}
//2.set the SMS_CONFIG with defaults values
ret = defaultsmsconfig(&(hSmsHandle->SmsConf));
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: set sms configure with defaults");
goto initsms_fail;
}
//3.initialize tty device--115200BPS, 8 databits, 1 stopbits, NOPARITY
//4.send AT\x0D continuously five times,
//if there is no OK return, it may be CDMA module, 230400BPS
//5.send AT\x0D continuously five times again,
//if still not OK return, then initialization error!
//6.check the type of wireless module
ret = confirmttyspeed(hSmsHandle->ttyfd, &ttySpeed);
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: confirm tty speed.");
goto initsms_fail;
}
hSmsHandle->SmsConf.baudrate = ttySpeed;
//7.get module type
ret = getmoduletype(hSmsHandle->ttyfd, &acModType);
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: get wireless module info.");
goto initsms_fail;
}
else if (acModType == UNKNOWN_MODULE)
{
WMMP_TRACE(debugType, "\r\nError: unknown module");
goto initsms_fail;
}
if ( (acModType & 0xF0) == 0x00 ) //GPRS wireless module
{
//GPRS need
//initialize SMS reference buffer
ret = initsmsrefbuf();
if(ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: Fail to initialize SMS reference buffer");
goto initsms_fail;
}
}
//8.set the SMS_FUNC structure.
ret = initsmsfunc(acModType, &(hSmsHandle->SmsFunc));
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: initialize sms functions");
goto initsms_fail;
}
//sleep
#ifdef _LINUX_
sleep(1);
#else
Sleep(1000);
#endif
//9.call specific initialization functions of wireless module.
ttyfd = hSmsHandle->ttyfd;
acSmsType = hSmsHandle->SmsConf.smstype;
acSmsCode = hSmsHandle->SmsConf.smscode;
ret = sms_init(ttyfd, acSmsType, acSmsCode);
if(ret < 0)
{
WMMP_TRACE(debugType, "\r\nFailed to initialize module");
goto initsms_out1;
}
//delete all messages in module
ret = sms_delall(ttyfd);
if(ret < 0)
{
WMMP_TRACE(debugType, "\r\nFailed to delete all messages in module");
// goto initsms_out1;
}
else
{
WMMP_TRACE(debugType, "\r\nSucc to delete all messages in module");
}
return 0;
initsms_out1:
// closetty(hSmsHandle->ttyfd);
initsms_fail:
return ret;
}
/************************************************************************
* Send short message
* pSms_content: PDU message should be converted with 7-bit, 8-bit
* and ucs16-bit coding, and
* text message is unnecessary to convert data format.
/************************************************************************/
int sendsms(SmsHandle hSmsHandle, const uchar *pSms_content, int len, const char *pDest)
{
int ret = 0;
int ttyfd = -1;
uchar ucModType;
uchar ucSmsType = 0x00;
uchar ucSmsCode = 0x00;
uchar ucPduData[1024];
const uchar* pSms;
int iPduDataLen;
int iSmsLen;
PDU pdu;
if (pSms_content == NULL || pDest == NULL)
{
WMMP_TRACE(debugType, "\r\nError: sendsms pointer params");
ret = -SMS_ERR_PARAMS;
goto sendsms_fail;
}
ret = searchsmshandle(&hSmsHandle);
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: Invalid SmsHandle");
goto sendsms_fail;
}
ttyfd = hSmsHandle->ttyfd;
ucModType = hSmsHandle->SmsFunc.acModuleType;
ucSmsType = hSmsHandle->SmsConf.smstype;
ucSmsCode = hSmsHandle->SmsConf.smscode;
if ( ( ucModType & 0xF0 ) == 0x00)
{//GPRS module
if (ucSmsType == SMS_PDU)
{
//package PDU format data
//Example: 00 11000D91 683106869031F5 0000A7 0431D98C06
memset(&pdu, 0, sizeof(PDU));
memcpy(&pdu, &(hSmsHandle->SmsConf.pdu), sizeof(PDU));
memcpy(pdu.tp_daddr, pDest, strlen(pDest));
memset(ucPduData, 0, sizeof(ucPduData));
iPduDataLen = 0;
//notion: In PDU 7-bit, data length is original sent data length.
//8-bit or ucs2, its length is the encoded bytes.
ret = packpdudata(pSms_content, len, &pdu, ucPduData, &iPduDataLen);
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: package PDU data");
goto sendsms_fail;
}
pSms = ucPduData;
iSmsLen = iPduDataLen;
}
else
{
pSms = pSms_content;
iSmsLen = len;
}
}
else
{//CDMA module
pSms = pSms_content;
iSmsLen = len;
}
ret = sms_send(ttyfd, pSms, iSmsLen, pDest, ucSmsType, ucSmsCode);
if(ret < 0)
{
WMMP_TRACE(debugType, "\r\nFailed to send SMS");
goto sendsms_fail;
}
return 0;
sendsms_fail:
return ret;
}
/************************************************************************
* Function: receive short message
* pSms_content: The data is not converted for GPRS message, especially PDU.
* For CDMA, data have been converted.
/************************************************************************/
int recvsms(SmsHandle hSmsHandle, uchar *pSms_content, int *len, char *pFrom)
{
int ret = 0;
int ttyfd = -1;
uchar ucSmsType = 0x00;
uchar ucSmsCode = 0x00;
if (pSms_content == NULL || len == NULL || pFrom == NULL)
{
WMMP_TRACE(debugType, "\r\nError: recvsms pointer params");
ret = -SMS_ERR_PARAMS;
goto recvsms_fail;
}
ret = searchsmshandle(&hSmsHandle);
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: Invalid SmsHandle");
goto recvsms_fail;
}
ttyfd = hSmsHandle->ttyfd;
ucSmsType = hSmsHandle->SmsConf.smstype;
ucSmsCode = hSmsHandle->SmsConf.smscode;
ret = sms_recv(ttyfd, pSms_content, len, pFrom, ucSmsType, ucSmsCode);
if(ret < 0)
{
WMMP_TRACE(debugType, "\r\nFailed to receive SMS");
goto recvsms_fail;
}
return 0;
recvsms_fail:
return ret;
}
int ifnewsms(SmsHandle hSmsHandle)
{
int ret = 0;
int ttyfd = -1;
uchar ucSmsType = 0x00;
uchar ucSmsCode = 0x00;
ret = searchsmshandle(&hSmsHandle);
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: ifnewsms invalid SmsHandle");
goto ifnew_fail;
}
ttyfd = hSmsHandle->ttyfd;
ucSmsType = hSmsHandle->SmsConf.smstype;
ucSmsCode = hSmsHandle->SmsConf.smscode;
ret = sms_ifnew(ttyfd, ucSmsType, ucSmsCode);
if(ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: Check if new SMS is coming\r\n");
goto ifnew_fail;
}
WMMP_TRACE(debugType, "\r\n%d new sms is coming", ret);
return ret;
ifnew_fail:
return ret;
}
int closesms(SmsHandle* hSmsHandle)
{
int ret = 0;
ret = searchsmshandle(hSmsHandle);
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: closesms invalid SmsHandle");
goto closesms_fail;
}
//close tty device
ret = closetty((*hSmsHandle)->ttyfd);
if(ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: close tty device");
goto closesms_fail;
}
//free SmsHandle
ret = delsmshandle(hSmsHandle);
if (ret < 0)
{
WMMP_TRACE(debugType, "\r\nError: delete SmsHandle");
}
return 0;
closesms_fail:
return ret;
}
int testcsq(SmsHandle hSmsHandle, uchar* csq, uchar *berr)
{
int ret = 0;
int atcmdLen = 0;
int iLeftLen = 0;
int iRetLen = 0;
int ttyfd;
uchar szBuf[50];
uchar szRecvBuf[100];
uchar *p1 = NULL, *p2 = NULL;
uchar acModType;
if (csq == NULL)
{
WMMP_TRACE(debugType, "\r\nError: testcsq invalid params");
ret = -SMS_ERR_PARAMS;
goto testcsq_fail;
}
acModType = hSmsHandle->SmsFunc.acModuleType;
ttyfd = hSmsHandle->ttyfd;
memset(szBuf, 0x00, sizeof(szBuf));
switch(acModType)
{
case GPRS_SIMCOM:
case GPRS_SIEMENS:
case GPRS_HUAWEI:
memcpy(szBuf, "AT+CSQ\x0D", strlen("AT+CSQ\x0D"));
break;
case CDMA_ANYDATA:
case CDMA_BELLWAVE:
case CDMA_FIDELIX:
case CDMA_ATEL:
memcpy(szBuf, "AT+CSQ?\x0D", strlen("AT+CSQ?\x0D"));
break;
default:
WMMP_TRACE(debugType, "Error: Unknown module type\r\n");
break;
}
cleartty(ttyfd); //clear tty read and write buffer
atcmdLen = strlen(szBuf);
ret = writetty(ttyfd, atcmdLen, 2, szBuf);
if(ret < 0)
{
WMMP_TRACE(debugType, "Error: Send AT+CSQ\r\n");
goto testcsq_fail;
}
memset(szRecvBuf, 0x00, sizeof(szRecvBuf));
iLeftLen = sizeof(szRecvBuf);
ret = readtty(ttyfd, iLeftLen, 3, szRecvBuf, &iRetLen);
if(ret < 0)
{
WMMP_TRACE(debugType, "Error: read tty\r\n");
goto testcsq_fail;
}
p1 = strstr(szRecvBuf, "+CSQ:");
if(p1 == NULL)
{
WMMP_TRACE(debugType, "Error: Recieve not +CSQ string\r\n");
ret = -SMS_ERR_AT_RESP_ERROR;
goto testcsq_fail;
}
p1 += 6;
memset(szBuf, 0x00, sizeof(szBuf));
p2 = szBuf;
while(*p1 != ',')
{
if((*p1 >= '0') && (*p1 <= '9'))
{
*p2++ = *p1++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -