#include "tc35.h"
// 初始化
int tc35_init(char pBuf[])
{
pBuf[0] = 'A';
pBuf[1] = 'T';
pBuf[2] = 'E';
pBuf[3] = '0';
pBuf[4] = 13;
return 5;
}
// 设置短信中心地址
int setCsca(char pBuf[])
{
pBuf[0] = 'A';
pBuf[1] = 'T';
pBuf[2] = '+';
pBuf[3] = 'C';
pBuf[4] = 'S';
pBuf[5] = 'C';
pBuf[6] = 'A';
pBuf[7] = '=';
pBuf[8] = '+';
pBuf[9] = '8';
pBuf[10] = '6';
pBuf[11] = '1';
pBuf[12] = '3';
pBuf[13] = '8';
pBuf[14] = '0';
pBuf[15] = '0';
pBuf[16] = '2';
pBuf[17] = '3';
pBuf[18] = '0';
pBuf[19] = '5';
pBuf[20] = '0';
pBuf[21] = '0';
pBuf[22] = ',';
pBuf[23] = '1';
pBuf[24] = '4';
pBuf[25] = '9';
pBuf[26] = 13;
return 27;
}
//设置短消息格式
int setCmgf(char pBuf[])
{
pBuf[0] = 'A';
pBuf[1] = 'T';
pBuf[2] = '+';
pBuf[3] = 'C';
pBuf[4] = 'M';
pBuf[5] = 'G';
pBuf[6] = 'F';
pBuf[7] = '=';
pBuf[8] = '0';
pBuf[9] = 13;
return 10;
}
//删除短消息
int deleteSms(char pBuf[],short int index)
{
pBuf[0] = 'A';
pBuf[1] = 'T';
pBuf[2] = '+';
pBuf[3] = 'C';
pBuf[4] = 'M';
pBuf[5] = 'G';
pBuf[6] = 'D';
pBuf[7] = '=';
pBuf[8] = (char)((index >> 8) & 0xff + 0x30);
pBuf[9] = (char)(index & 0xff + 0x30);
pBuf[10] = 13;
return 11;
}
//接收短消息
void revSms(char pBuf[],char pOut[])
{
pBuf[0] = 'A';
pBuf[1] = 'T';
pBuf[2] = '+';
pBuf[3] = 'C';
pBuf[4] = 'M';
pBuf[5] = 'G';
pBuf[6] = 'R';
pBuf[7] = '=';
pBuf[8] = (char)((index >> 8) & 0xff + 0x30);
pBuf[9] = (char)(index & 0xff + 0x30);
pBuf[10] = 13;
return 11;
}
//发送短消息
void sendSms(char pPhone[],int phonelen,char pData[],int nLen,
int *nTXLen1,int *nTXLen2,char pOut1[],char pOut2)
{
char strHead[18] = {'0','8','9','1','6','8',
'3','1','0','8','2','0',
'0','3','0','5','F','0'};
char chrInfo[6] = {'1','1','0','0','0','B'};
int nLen_temp;
int nContent_Len;
int nTempLen;
int nOff;
int nOffset;
char chrTemp[100];
char chrTmp[100];
char pBuf[200];
char phoneTemp[20];
char nTemp[100];
char Len[1];
int i;
int n;
nOff = 0;
nOffset = 0;
for(i = 0;i < phonelen;i++)
{
chrTmp[i] = pPhone[i];
}
chrTmp[phonelen] = 'F';
phonelen += 1;
// 将电话号码按照规范的顺序作成
n = 0;
for(i = 0;i < phonelen / 2;i++)
{
phoneTemp[n++] = chrTmp[2 * i + 1];
phoneTemp[n++] = chrTmp[2 * i];
}
copy(chrTemp,0,chrInfo,0,6);
nOff = 6;
chrTemp[nOff] = '8';
nOff += 1;
chrTemp[nOff] = '1';
nOff += 1;
//设置电话号码
copy(chrTemp,nOff,phoneTemp,0,phonelen);
nOff += phonelen;
chrTemp[nOff] = '0';
nOff += 1;
chrTemp[nOff] = '0';
nOff += 1;
nContent_Len = nLen;
// 设置编码类型
chrTemp[nOff] = '0';
nOff += 1;
chrTemp[nOff] = '0';
nOff += 1;
Len[0] = nLen;
for(i = 0;i < 10;i++)
{
chrTmp[i] = 0;
}
ByteToChar(Len,chrTmp,1);
chrTemp[nOff] = 'A';
nOff += 1;
chrTemp[nOff] = 'A';
nOff += 1;
copy(chrTemp,nOff,chrTmp,0,2);
nOff += 2;
nLen_temp = nOff;
nLen_temp += nContent_Len;
//获得长度的字符数组
nTempLen = IntToChar(nLen_temp,chrTmp);
//封装长度信息
nTemp[0] = 'A';
nTemp[1] = 'T';
nTemp[2] = '+';
nTemp[3] = 'C';
nTemp[4] = 'M';
nTemp[5] = 'G';
nTemp[6] = 'S';
nTemp[7] = '=';
nOffset = 8;
//长度
for(i = 0;i < nTempLen;i++)
{
nTemp[nOffset] = chrTmp[i];
nOffset += 1;
}
nTemp[nOffset] = 13;
for(i = 0; i < nOffset;i++)
{
pOut1[i] = nTemp[i];
}
*nTXLen1 = nOffset;
//封装内容数据
copy(pOut2,0,strHead,0,18);
nOffset = 18;
copy(pOut2,nOffset,0,chrTemp,nOff);
nOffset += nOff;
Encode(pData,pBuf,nLen);
ByteToChar(pBuf,chrTmp,nLen);
copy(pOut2,nOffset,chrTmp,0,(2 * nLen));
nOffset += (2 * nLen);
pOut2[nOffset] = 26;
*nTXLen2 = nOffset;
}
//将源数组的内容拷贝到目的数组
void copy(char pDest[],int nOrg,char pOrg[],int nStart,int nLen)
{
int i;
for(i = 0;i < nLen;i++)
{
pDest[nOrg + i] = pOrg[i + nStart];
}
}
// 将字节处理成字符串
void ByteToChar(char nInPut[],char Out[],int nLen)
{
int i;
char chrTemp;
for(i = 0;i < nLen;i++)
{
// 高字节
chrTemp = (char)((nInPut[i] >> 4) & 0x0f);
if(chrTemp >= 0 && chrTemp else chrTemp += 55;
Out[i] = chrTemp;
// 低字节
chrTemp = (char)(nInPut[i] & 0x0f);
if(chrTemp >= 0 && chrTemp else chrTemp += 55;
Out[i] = chrTemp;
}
return;
}
// 数的范围为250以内
int IntToChar(int n,char Out[])
{
int i;
char chrTemp1;
char chrTemp2;
char chrTemp3;
int nLen;
chrTemp1 = n / 100;
chrTemp2 = (n - chrTemp1 * 100) / 10;
chrTemp3 = n - chrTemp1 * 100 - chrTemp2 * 10;
if(chrTemp1 != 0)
{
nLen = 3;
Out[0] = chrTemp1 + 0x30;
Out[1] = chrTemp2 + 0x30;
Out[2] = chrTemp3 + 0x30;
}
else
{
if(chrTemp2 != 0)
{
nLen = 2;
Out[0] = chrTemp2 + 0x30;
Out[1] = chrTemp3 + 0x30;
}
else
{
nLen = 1;
Out[0] = chrTemp3 + 0x30;
}
}
return nLen;
}
// 编码函数
void Encode(char in[],char out[],int nLen)
{
int nOrigin = 0;
int nCode = 0;
while(true)
{
if(nOrigin >= nLen) break;
out[nCode] = in[nOrigin];
if((nOrigin + 1) >= nLen) break;
out[nCode] |= (byte)((in[nOrigin + 1] & 0x01) out[nCode + 1] = (byte)((in[nOrigin + 1] >> 1) & 0xff);
if((nOrigin + 2) >= nLen) break;
out[nCode + 1] |= (byte)((in[nOrigin + 2] & 0x03) out[nCode + 2] = (byte)((in[nOrigin + 2] >> 2) & 0xff);
if((nOrigin + 3) >= nLen) break;
out[nCode + 2] |= (byte)((in[nOrigin + 3] & 0x07) out[nCode + 3] = (byte)((in[nOrigin + 3] >> 3) & 0xff);
if((nOrigin + 4) >= nLen) break;
out[nCode + 3] |= (byte)((in[nOrigin + 4] & 0x0f) out[nCode + 4] = (byte)((in[nOrigin + 4] >> 4) & 0xff);
if((nOrigin + 5) >= nLen) break;
out[nCode + 4] |= (byte)((in[nOrigin + 5] & 0x1f) out[nCode + 5] = (byte)((in[nOrigin + 5] >> 5) & 0xff);
if((nOrigin + 6) >= nLen) break;
out[nCode + 5] |= (byte)((in[nOrigin + 6] & 0x3f) out[nCode + 6] = (byte)((in[nOrigin + 6] >> 6) & 0xff);
if((nOrigin + 7) >= nLen) break;
out[nCode + 6] |= (byte)((in[nOrigin + 7] & 0x7f)
nCode += 7;
nOrigin += 8;
}
}
int AnalyseSms(char in[],int nLen,char chrPhone[],char chrMessage[])
{
char Phone[20];
int phone_len;
char chrTemp[200];
char chrMessage[140];
int nLen_temp;
int nTempLen;
int nOffset;
int nOff;
char chrTmp;
int n;
int nContent_Len;
byte content[140];
//去掉+CMGR:信息
nLen_temp = nLen - 6;
nOffset = 6;
copy(chrTemp,0,in,nOffset,nLen_temp);
if(nLen_temp < 20) return -1;
chrTmp = chrTemp[0];
if(chrTmp == '1')//新消息
{
chrTmp = strTemp[3];
if(chrTemp == '1')// 长度为三个字符
{
nTempLen = (strTemp[3] - 48) * 100 + (strTemp[4] - 48) * 10
+ (strTemp[4] - 48);
if(nLen_temp >= 26)
{
nLen_temp -= 26;
nOffset += 26;
copy(chrTemp,0,in,nOffset,nLen_temp);
}
else return -1;
}// 长度为两个字符
else
{
nTempLen = (strTemp[3] - 48) * 10 + (strTemp[4] - 48);
if(nLen_temp >= 25)
{
nLen_temp -= 25;
nOffset += 25;
copy(chrTemp,0,in,nOffset,nLen_temp);
}
else return -1;
}
//取得电话号码
int nPhone_Len = 0;
if(nLen_temp >= 18)
{
copy(Phone,0,chrTemp,0,18);
phone_len = (int)(Phone[1] - 48);
copy(chrPhone,0,Phone,4,14);
}
else return -1;
//获得电话号码的正确顺序
copy(Phone,0,chrPhone,0,14);
n = 0;
for(i = 0;i < 7;i++)
{
chrPhone[n++] = Phone[2 * i + 1];
chrPhone[n++] = Phone[2 * i];
}
if(nLen_temp >= 20)
{
nLen_temp -= 20;
nOffset += 20;
copy(chrTemp,0,in,nOffset,nLen_temp);
}
else return -1;
if(nLen_temp >= 16)
{
nLen_temp -= 16;
nOffset += 16;
copy(chrTemp,0,in,nOffset,nLen_temp);
}
else return -1;
// 取出内容的长度
nContent_Len = 0;
if(nLen_temp >= 2)
{
if((strTemp[1] >= 48) && (strTemp[1] nContent_Len = (strTemp[0] - 48) * 16 + (strTemp[1] - 48);
else if(strTemp[1] >= 65 && strTemp[1] nContent_Len = (strTemp[0] - 48) * 16 + (strTemp[1] - 55);
else if(strTemp[1] >= 97 && strTemp[1] nContent_Len = (strTemp[0] - 48) * 16 + (strTemp[1] - 87);
}
else return -1;
if(strTemp.length() >= 2)
{
nLen_temp -= 2;
nOffset += 2;
copy(chrTemp,0,in,nOffset,nLen_temp);
}
else return -1;
CharToByte(strTemp,content,nLen_temp);
nLen_temp /= 2;
n = Decode(content,chrMessage,nLen_temp);
}
return n;
}
int FindERROR(char in[],int nLen)
{
int nOffset,i;
nOffset = -1;if(nLen < 5) return nOffset;
for(i = 0;i < nLen;i++)
{
if((in[i] == 'R') && (in[i - 1] == 'O') && (in[i - 2] == 'R')
&& (in[i - 3] == 'R') && (in[i - 4] == 'E'))
{
nOffset = i - 4;
break;
}
}
return nOffset;
}
int FindCMGR(char in[],int nLen)
{
int nOffset,i;
nOffset = -1;
if(nLen < 5) return nOffset;
for(i = 0;i < nLen;i++)
{
if((in[i] == 'R') && (in[i - 1] == 'G') && (in[i - 2] == 'M')
&& (in[i - 3] == 'C') && (in[i - 4] == '+'))
{
nOffset = i - 4;
break;
}
}
return nOffset;
}
int FindOK(char in[],int nLen)
{
int nOffset,i;
nOffset = -1;
if(nLen < 2) return nOffset;
for(i = 0;i < nLen;i++)
{
if((in[i] == 'K') && (in[i - 1] == 'K'))
{
nOffset = i - 1;
break;
}
}
return nOffset;
}
void CharToByte(char in[],char out[],int nLen)
{
char chrHi,chrLow;
for(int i = 0;i < nLen / 2;i++)
{
chrHi = in[2 * i];
if(chrHi >= 48 && chrHi chrHi = (char)(chrHi - 48);
else if(chrHi >= 65 && chrHi chrHi = (char)(chrHi - 55);
else if(chrHi >= 97 && chrHi chrHi = (char)(chrHi - 87);
chrLow = in[2 * i + 1];
if(chrLow >= 48 && chrLow chrLow = (char)(chrLow - 48);
else if(chrLow >= 65 && chrLow chrLow = (char)(chrLow - 55);
else if(chrLow >= 97 && chrLow chrLow = (char)(chrLow - 87);
out[i] = (byte)(chrHi * 16 + chrLow);
}
return;
}
int Decode(char in[],char out[],int nLen)
{
int nLenTemp = byteStream.length;
int nOrigin = 0;
int nCode = 0;
nLenTemp = nLen * 8 / 7;
while(true)
{
if(nOrigin >= nLen) break;
out[nCode] = (char)(in[nOrigin] & 0x7f);
if((nOrigin + 1) >= nLen) break;
out[nCode + 1] = (char)((in[nOrigin + 1] & 0x3f) out[nCode + 1] += (char)((in[nOrigin] & 0x80) >> 7);
if((nOrigin + 2) >= nLen) break;
out[nCode + 2] = (char)((in[nOrigin + 2] & 0x1f) out[nCode + 2] += (char)((in[nOrigin + 1] & 0xc0) >> 6);
if((nOrigin + 3) >= nLen) break;
out[nCode + 3] = (char)((in[nOrigin + 3] & 0x0f) out[nCode + 3] += (char)((in[nOrigin + 2] & 0xe0) >> 5);
if((nOrigin + 4) >= nLen) break;
out[nCode + 4] = (char)((in[nOrigin + 4] & 0x07) out[nCode + 4] += (char)((in[nOrigin + 3] & 0xf0) >> 4);
if((nOrigin + 5) >= nLen) break;
out[nCode + 5] = (char)((in[nOrigin + 5] & 0x03) out[nCode + 5] += (char)((in[nOrigin + 4] & 0xf8) >> 3);
if((nOrigin + 6) >= nLen) break;
out[nCode + 6] = (char)((in[nOrigin + 6] & 0x01) out[nCode + 6] += (char)((in[nOrigin + 5] & 0xfc) >> 2);
out[nCode + 7] = (char)((in[nOrigin + 6] & 0xfe) >> 1);
nCode += 8;
nOrigin += 7;
}
return nLenTemp;
}