📄 sms.c
字号:
#include "uart.h"
#include "sms.h"
#include "string.h"
#include "sfr_r811.h"
#include "int.h"
//extern const char accii[];
#pragma ROM tabhex
static const char tabhex[]="0123456789ABCDEF";
int gsmInvertNumbers(char* pSrc, char* pDst, int nSrcLength)
{
int nDstLength,i;
char ch;
nDstLength = nSrcLength;
for(i=0; i<nSrcLength;i+=2)
{
ch = *pSrc++;
*pDst++ = *pSrc++;
*pDst++ = ch;
}
if(nSrcLength & 1)
{
*(pDst-2) = 'F';
nDstLength++;
}
*pDst = '\0';
return nDstLength;
}
int gsmSerializeNumbers(char* pSrc, char* pDst, int nSrcLength)
{
int nDstLength,i;
char ch;
nDstLength = nSrcLength;
for(i=0; i<nSrcLength;i+=2)
{
ch = *pSrc++;
*pDst++ = *pSrc++;
*pDst++ = ch;
}
if(*(pDst-1) == 'F')
{
pDst--;
nDstLength--;
}
*pDst = '\0';
return nDstLength;
}
/**************************************************************
// byte data transform to string in hex
// {0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01} --> "C8329BFD0E01"
// pSrc: point to source data in hex
// pDst: point to dest string
// nSrcLength: source data length inbyte
// retur:dest string length;
**************************************************************/
int gsmBytes2String(unsigned char* pSrc, char* pDst, int nSrcLength)
{ int i;
for(i=0; i<nSrcLength; i++)
{
*pDst++ = tabhex[*pSrc >> 4];
*pDst++ = tabhex[*pSrc & 0x0f];
pSrc++;
}
*pDst = '\0';
return (nSrcLength*2);
}
/**************************************************************
//string transform to byte in hex
// example:"C8329BFD0E01" --> {0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01}
// pSrc: point to source string
// pDst: point to dest byte array
// nSrcLength: source string length
// retur:dest data length in byte;
**************************************************************/
int gsmString2Bytes(char* pSrc, unsigned char* pDst, int nSrcLength)
{ int i;
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 gsmEncode7bit(char* pSrc, unsigned char* pDst, int nSrcLength)
{
int nSrc;
int nDst;
int nChar;
unsigned char nLeft;
nSrc = 0;
nDst = 0;
while(nSrc<nSrcLength)
{
nChar = nSrc & 7;
if(nChar == 0)
{nLeft = *pSrc;
}
else
{
*pDst = (*pSrc << (8-nChar)) | nLeft;
nLeft = *pSrc >> nChar;
pDst++;
nDst++;
}
pSrc++;
nSrc++;
}
return(nDst);
}
int gsm7bitLength(char* pSrc, int nSrcLength)
{
int nSrc;
int nDst;
int nChar;
unsigned char nLeft;
nSrc = 0;
nDst = 0;
while(nSrc<nSrcLength)
{
nChar = nSrc & 7;
if(nChar == 0)
{nLeft = *pSrc;
}
else
{
//*pDst = (*pSrc << (8-nChar)) | nLeft;
nLeft = *pSrc >> nChar;
//pDst++;
nDst++;
}
pSrc++;
nSrc++;
}
return(nDst);
}
int gsmDecode7bit(unsigned char* pSrc, char* pDst, int nSrcLength)
{
int nSrc;
int nDst;
int nByte;
unsigned char nLeft;
nSrc = 0;
nDst = 0;
nByte = 0;
nLeft = 0;
while(nSrc<nSrcLength)
{
*pDst = ((*pSrc << nByte) | nLeft) & 0x7f;
nLeft = *pSrc >> (7-nByte);
pDst++;
nDst++;
nByte++;
if(nByte == 7)
{
*pDst = nLeft;
pDst++;
nDst++;
nByte = 0;
nLeft = 0;
}
pSrc++;
nSrc++;
}
*pDst = '\0';
return nDst;
}
int gsmBcd2Unistr(char *pSrc,char* pDst,int nSrcLength)
{ int i;
for(i=0; i<nSrcLength; i++)
{
*pDst++='0';
*pDst++='0';
*pDst++='3';
*pDst++=tabhex[*pSrc>>4];
*pDst++='0';
*pDst++='0';
*pDst++='3';
*pDst++=tabhex[*pSrc & 0x0f];
pSrc++;
}
*pDst = '\0';
return(nSrcLength*4);
}
int gsmEncodePdu(SM_PARAM* pSrc, char* pDst)
{
int nLength,i;
int nDstLength;
unsigned char buf[100];
//no smsc
nLength = strlen(pSrc->SCA);
if(nLength!=0)
{
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);
}
else
{
buf[0]=0;
nDstLength = gsmBytes2String(buf, pDst, 1);
}
nLength = strlen(pSrc->TPA); // TP-DA length
buf[0] = 0x11; // (TP-MTI=01),TP-VP(TP-VPF=10)
buf[1] = 0; // TP-MR=0
buf[2] = (char)nLength; // TP-DA length
buf[3] = 0x91; // international format
nDstLength += gsmBytes2String(buf, &pDst[nDstLength], 4);
nDstLength += gsmInvertNumbers(pSrc->TPA, &pDst[nDstLength], nLength);
// TPDU
nLength = strlen(pSrc->TP_UD); // TP_UD LENGTH
buf[0] = pSrc->TP_PID; // (TP-PID)
buf[1] = pSrc->TP_DCS; // (TP-DCS)
buf[2] = 0;
if(pSrc->TP_DCS == GSM_7BIT)
{
buf[3] = nLength; //
nLength = gsmEncode7bit(pSrc->TP_UD, &buf[4], nLength+1) + 4; // TP-DAP 2 PDU
nDstLength += gsmBytes2String(buf, &pDst[nDstLength], nLength); // PDU
}
else if(pSrc->TP_DCS == GSM_UCS2)
{
buf[3] = nLength/2;
nDstLength+=gsmBytes2String(buf, &pDst[nDstLength],4);
for(i=0;i<nLength;i++)
{
pDst[nDstLength+i]=pSrc->TP_UD[i];
}
nDstLength+=nLength;
}
else
{
//8-bit
buf[3] = nLength;
for(i=0;i<nLength;i++)
{
buf[4+i]=pSrc->TP_UD[i];
}
nLength = buf[3] + 4; // nLength
nDstLength += gsmBytes2String(buf, &pDst[nDstLength], nLength); // PDU
}
pDst[nDstLength]='\0';
return(nDstLength);//byte lenth
}
int gsmDecodePdu(char* pSrc, SM_PARAM* pDst)
{
int nDstLength,i;
unsigned char tmp,tmp1;
//smsc addr
gsmString2Bytes(pSrc, &tmp, 2);
tmp = (tmp - 1) * 2;
pSrc += 4;
gsmSerializeNumbers(pSrc, pDst->SCA, tmp);
pSrc += tmp;
//toa information
gsmString2Bytes(pSrc, &tmp, 2);
pSrc += 2;
gsmString2Bytes(pSrc,&tmp1,2);
if(tmp & 0x84)
{
if(tmp1!=0x91)
{
strncpy(pDst->TPA,"86",2);
}
gsmString2Bytes(pSrc, &tmp, 2);
if(tmp & 1) tmp += 1;
pSrc += 4;
if(tmp1!=0x91)
gsmSerializeNumbers(pSrc, &pDst->TPA[2], tmp);
else
gsmSerializeNumbers(pSrc, pDst->TPA, tmp);
pSrc += tmp;
}
//tpdu information
gsmString2Bytes(pSrc, (unsigned char*)&pDst->TP_PID, 2);
pSrc += 2;
gsmString2Bytes(pSrc, (unsigned char*)&pDst->TP_DCS, 2);
pSrc += 2;
gsmSerializeNumbers(pSrc, pDst->TP_SCTS, 14);
pSrc += 14;
gsmString2Bytes(pSrc, &tmp, 2);
pSrc += 2;
if(pDst->TP_DCS == GSM_7BIT)
{
// 7-bit
nDstLength = gsmString2Bytes(pSrc, pSrc, tmp & 7 ? (int)tmp * 7 / 4 + 2 : (int)tmp * 7 / 4);
gsmDecode7bit(pSrc, pDst->TP_UD, nDstLength);
nDstLength = tmp;
}
else if(pDst->TP_DCS == GSM_UCS2)
{
// UCS2
for(i=0;i<tmp/2;i++)
{
pSrc+=2;
gsmString2Bytes(pSrc,&(pDst->TP_UD[i]),2);
pSrc+=2;
}
nDstLength=tmp/2;
pDst->TP_UD[nDstLength]='\0';
}
else
{
// 8-bit
for(i=0;i<tmp*2;i++)
{
pDst->TP_UD[i]=pSrc[i];
}
nDstLength=tmp*2;
pDst->TP_UD[nDstLength]='\0';
}
return(nDstLength);
}
/**************************************************************
//read message from gsm modle,index is set in pMsg->index
// ui, uart0/1
// pSrc ,Msg buffer pointer in SM_PARAM
//timout,if GSM has no ack,seth timeout number :(ms)
// return:0/1 reading failed /reading successful
**************************************************************/
char gsmSendMessage(char ui,SM_PARAM* pSrc,unsigned int timout)
{
int nPduLength,i;
unsigned char nSmscLength;
int nLength;
char tpch[6],result;
result=0;
nPduLength = gsmPduLength(pSrc)/2;
//send com
int2str(nPduLength,tpch,3);
strncpy(txbuf,"AT+CMGS=\0",9);
strcat(txbuf,tpch);
strcat(txbuf,"\r\0");
sendATdata(ui,txbuf,"\r\n> ","ERROR\r\n",timout);
//send pdu
nPduLength=gsmEncodePdu(pSrc,txbuf);
tpch[0]=0x1a;
tpch[1]='\0';
strcat(txbuf, tpch);
result=sendATdata(ui,txbuf,"\r\nOK\r\n","ERROR\r\n",timout);
return(result);
}
/**************************************************************
//read message from gsm modle,index is set in pMsg->index
// ui, uart0/1
// pMsg ,Msg buffer pointer in SM_PARAM
//timout,if GSM has no ack,seth timeout number :(ms)
// return:0/1 reading failed /reading successful
**************************************************************/
char gsmReadMessage(char ui,SM_PARAM* pMsg,char stat,unsigned int timout)
{
int nLength,tmp;
char* ptr;
int nMsg;
char err;
char tpch[6];
clr_rxbuf(ui);
if(stat<10)int2str((int)stat,tpch,1);
else if(stat<100)int2str((int)stat,tpch,2);
nMsg = 0;
err=0;
if(ui==0)ptr = u0rxbuf;
else if(ui==1)ptr = u1rxbuf;
strncpy(txbuf,"AT+CMGL=",8);
strcat(txbuf,tpch);
strcat(txbuf,"\r");
send_n_byte(ui,txbuf,strlen(txbuf));
while(wait_str(ui,"\r\nOK\r\n")==0)
{
if(wait_str(ui,"EEROR")!=0)
{
err=1;
flag.bit.gsmerror=1;
break;
}
if(timycount[ui]>=timout)
{
err=1;
flag.bit.gsmerror=1;
break;
}
}
if(err==0)//
{
if((ptr=strstr(ptr, "+CMGL:")) != 0)
{
ptr += 6;
pMsg->index=*ptr;
ptr = strstr(ptr, "\r\n");
ptr += 2;
gsmDecodePdu(ptr, pMsg);
nMsg++;
}
}
return(nMsg);
}
char gsmReadMsg(char ui,SM_PARAM* pMsg,char *idx,unsigned int timout)
{
int nLength,tmp;
char* ptr;
int nMsg;
char err;
nMsg = 0;
err=0;
if(ui==0)ptr = u0rxbuf;
else if(ui==1)ptr = u1rxbuf;
strncpy(txbuf,"AT+CMGR=",8);
strcat(txbuf,idx);
strcat(txbuf,"\r");
send_n_byte(ui,txbuf,strlen(txbuf));
while(wait_str(ui,"\r\nOK\r\n")==0)
{
if(wait_str(ui,"EEROR")!=0)
{
err=1;
flag.bit.gsmerror=1;
break;
}
if(timycount[ui]>=timout)
{
err=1;
flag.bit.gsmerror=1;
break;
}
}
if(err==0)//
{
if((ptr=strstr(ptr, "+CMGR:")) != 0)
{
ptr += 6;
//pMsg->index=*ptr;
ptr = strstr(ptr, "\r\n");
ptr += 2;
if(strncmp(ptr,"\r\n",2)!=0)
{
gsmDecodePdu(ptr, pMsg);
nMsg++;
}
}
}
clr_rxbuf(ui);
return(nMsg);
}
/**************************************************************
//read message from gsm modle,index is set in pMsg->index
// ui, uart0/1
// index ,message index
//timout,if GSM has no ack,seth timeout number :(ms)
// return:0/1 reading failed /reading successful
**************************************************************/
char gsmDeleteMessage(char ui,char index,unsigned int timout)
{ char tpch[6];
int nLength;
char result;
result=0;
clr_txbuf();
if(index<10)int2str((int)index,tpch,1);
else if(index<100)int2str((int)index,tpch,2);
strncpy(txbuf,"AT+CMGD=",8);
strcat(txbuf,tpch);
strcat(txbuf,"\r");
if(sendATdata(ui,txbuf,"OK\r\n","ERROR\r\r",timout)<=0)result=0;
else result=1;
return(result);
}
char gsmDeleteMsg(char ui,char *idx,unsigned int timout)
{ // char tpch[6];
int nLength;
char result;
result=0;
strncpy(txbuf,"AT+CMGD=",8);
strcat(txbuf,idx);
strcat(txbuf,"\r");
if(sendATdata(ui,txbuf,"OK\r\n","ERROR\r\r",timout)<=0)result=0;
else result=1;
return(result);
}
int gsmPduLength(SM_PARAM* pSrc)
{
int nLength;
int nDstLength;
char buf[5];
//no smsc
nLength = strlen(pSrc->TPA); // TP-DA length
if(nLength & 1)nLength++;
nDstLength =8;
nDstLength += nLength;
// TPDU
nLength = strlen(pSrc->TP_UD); // TP_UD LENGTH
if(pSrc->TP_DCS == GSM_7BIT)
{
//
nLength = gsm7bitLength(pSrc->TP_UD, nLength+1) + 4; // TP-DAP 2 PDU
nDstLength += nLength*2; // PDU
}
else if(pSrc->TP_DCS == GSM_UCS2)
{
nDstLength+=8;
nDstLength+=nLength;
}
else
{
//8-bit
nLength+=4; // nLength
nDstLength +=nLength*2;
}
return(nDstLength);//byte lenth
}
int gsmBcd2Str(char *pSrc,char* pDst,int nSrcLength) //bcd length
{ int i;
for(i=0; i<nSrcLength; i++)
{
*pDst++=tabhex[*pSrc>>4];
*pDst++=tabhex[*pSrc & 0x0f];
pSrc++;
}
*pDst = '\0';
return(nSrcLength*2); //pDst length
}
int gsmStr2Bcd(char *pSrc,char* pDst,int nSrcLength) //str length
{ int i;
char tp;
for(i=0; i<nSrcLength/2; i++)
{ if(*pSrc>='0' && *pSrc<='9')
{
*pDst=(*pSrc-'0')<<4;
*pSrc++;
*pDst|=(*pSrc-'0');
pSrc++;
pDst++;
}
else {
nSrcLength=0;
break;
}
}
*pDst = '\0';
return(nSrcLength/2); //pDst length
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -