📄 sms.c
字号:
#include "smssend.h"
#include "com.h"
int gsmBytes2String(const unsigned char* pSrc, char* pDst, int nSrcLength);
int gsmString2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength);
int gsmEncode8bit(const char* pSrc, unsigned char* pDst, int nSrcLength);
int gsmEncodeUcs2(const char* pSrc, unsigned char* pDst, int nSrcLength);
int gsmInvertNumbers(const char* pSrc, char* pDst, int nSrcLength);
int gsmSerializeNumbers(const char* pSrc, char* pDst, int nSrcLength);
int gsmEncodePdu(const SM_PARAM* pSrc, char* pDst);
static SM_PARAM *sp = NULL;
int gsmString2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
int i = 0;
for (i = 0; i < nSrcLength; i += 2)
{
if ((*pSrc >= '0') && (*pSrc <= '9'))
{
*pDst = (*pSrc - '0') << 4;
}
else
{
*pDst = (*pSrc - 'A' + 10) << 4;
}
pSrc++;
if ((*pSrc>='0') && (*pSrc<='9'))
{
*pDst |= *pSrc - '0';
}
else
{
*pDst |= *pSrc - 'A' + 10;
}
pSrc++;
pDst++;
}
return (nSrcLength / 2);
}
int gsmBytes2String(const unsigned char* pSrc, char* pDst, int nSrcLength)
{
const char tab[]="0123456789ABCDEF";
int i=0;
for( i=0; i < nSrcLength; i++)
{
*pDst++ = tab[*pSrc >> 4];
*pDst++ = tab[*pSrc & 0x0f];
pSrc++;
}
*pDst = '\0';
return (nSrcLength * 2);
}
int gsmEncode8bit(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
// simple copy
memcpy(pDst, pSrc, nSrcLength);
return nSrcLength;
}
int gsmEncodeUcs2(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
int nDstLength;
wchar_t wchar[128];
int i=0;
nDstLength = mbstowcs(wchar,pSrc,nSrcLength); //For Linux
for(i=0; i<nDstLength; i++)
{
*pDst++ = (unsigned char) wchar[i] >> 8 &0xFF;
*pDst++ = (unsigned char) wchar[i] & 0xff;
}
*pDst='\0';
return nDstLength * 2;
}
int gsmInvertNumbers(const char* pSrc, char* pDst, int nSrcLength)
{
int nDstLength;
char ch;
int i=0;
char *p=pDst;
nDstLength = nSrcLength;
for(i=0; i<nSrcLength;i+=2)
{
ch = *pSrc++;
*pDst++ = *pSrc++;
*pDst++ = ch;
}
*pDst='\0';
if(nSrcLength%2 != 0)
{
*(pDst-2)='F';
nDstLength++;
}
return nDstLength;
}
int gsmEncodePdu(const SM_PARAM* pSrc, char* pDst)
{
int nLength;
int nDstLength=0;
unsigned char buf[256];
nLength = strlen(pSrc->SCA);
buf[0] = (char)((nLength & 1) == 0 ? nLength : nLength + 1) / 2 + 1;
buf[1] = 0x91;
nDstLength = gsmBytes2String(buf, pDst, 2);
nDstLength += gsmInvertNumbers(pSrc->SCA, &pDst[nDstLength], nLength);
nLength = strlen(pSrc->TPA);
buf[0] = 0x11;
buf[1] = 0;
buf[2] = (char)nLength;
if(buf[2] = 0x0B)
buf[3] = 0x81;
else buf[3] = 0x91;
nDstLength += gsmBytes2String(buf, &pDst[nDstLength], 4);
nDstLength += gsmInvertNumbers(pSrc->TPA, &pDst[nDstLength], nLength);
nLength = strlen(pSrc->TP_UD);
buf[0] = pSrc->TP_PID;
buf[1] = pSrc->TP_DCS;
buf[2] = 0xAA;
if(pSrc->TP_DCS == GSM_UCS2)
{
buf[3] = gsmEncodeUcs2(pSrc->TP_UD, &buf[4], nLength);
nLength = buf[3] + 4;
}else
{
// 8-bit
buf[3] = gsmEncode8bit(pSrc->TP_UD, &buf[4], nLength);
nLength = buf[3] + 4;
}
nDstLength += gsmBytes2String(buf, &pDst[nDstLength], nLength);
return nDstLength;
}
int gsmSendMessage()
{
int nPduLength;
unsigned char nSmscLength;
int nLength;
char cmd[16];
char pdu[512];
char ans[128];
printf("SCA is %s\n",sp->SCA);
nPduLength = gsmEncodePdu(sp, pdu);
printf("PDU is [%s]\n",pdu);
strcat(pdu, "\x01a");
gsmString2Bytes(pdu, &nSmscLength, 2);
nSmscLength++;
sprintf(cmd, "AT+CMGS=%d\r", nPduLength / 2 - nSmscLength);
writeComm(cmd, strlen(cmd));
nLength = readComm(ans, 128);
if(nLength == 4 && strncmp(ans, "\r\n> ", 4) == 0)
{
writeComm(pdu, strlen(pdu));
}else{strcat(sp->TP_UD,"Send Failed!");}
savelog("/var/local/sms_warning.log",sp->TPA,sp->TP_UD);
free(sp);
closeComm();
return 0;
}
int smsinit(const char* smsc,const char* phone,const char* msg)
{
if (sp!= NULL)
{
free(sp);
sp = NULL;
}
sp = (SM_PARAM *)malloc(sizeof(SM_PARAM));
memset(sp->TP_SCTS,0,16);
strcpy(sp->SCA,smsc);
strcpy(sp->TP_UD,msg);
strcpy(sp->TPA,phone);
sp->TP_PID = 0;
sp->TP_DCS = GSM_UCS2;
sp->index = (short)0;
printf("Ready to Init!\n");
int fd;
if((fd=openComm("/dev/ttyUSB0")) < 0)
{
printf("can Not Open /dev/ttyUSB0!\n");
return -1;
}
char ans[128];
writeComm("AT\r", 3);
readComm(ans, 128);
if (strstr(ans, "OK") == NULL)
return -1; //return FALSE
// ECHO OFF
writeComm("ATE0\r", 5);
readComm(ans, 128);
// PDU
writeComm("AT+CMGF=0\r", 10);
readComm(ans, 128);
printf("Complete Init!\n");
return 0;
//return TRUE
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -