#include
#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[],char phone[])
{
int offset;
int i;
pBuf[0] = 'A';
pBuf[1] = 'T';
pBuf[2] = '+';
pBuf[3] = 'C';
pBuf[4] = 'S';
pBuf[5] = 'C';
pBuf[6] = 'A';
pBuf[7] = '=';
pBuf[8] = '"';
offset = 9;
for(i = 0;i < 14;i++)
{
pBuf[offset + i] = phone[i];
}
offset += 14;
pBuf[8] = '"';
offset += 1;
pBuf[offset] = ',';
offset += 1;
pBuf[offset] = '1';
offset += 1;
pBuf[offset] = '4';
offset += 1;
pBuf[offset] = '9';
offset += 1;
pBuf[offset] = 13;
offset += 1;
return offset;
}
//设置短消息格式
int setCmgf(char pBuf[],int format)
{
pBuf[0] = 'A';
pBuf[1] = 'T';
pBuf[2] = '+';
pBuf[3] = 'C';
pBuf[4] = 'M';
pBuf[5] = 'G';
pBuf[6] = 'F';
pBuf[7] = '=';
pBuf[8] = (char)('0' + format);
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] = '=';
if(index >= 10)
{
pBuf[8] = (char)((index >> 8) & 0xff + 0x30);
pBuf[9] = (char)(index & 0xff + 0x30);
pBuf[10] = 13;
return 11;
}
else
{
pBuf[8] = (char)(index & 0xff + 0x30);
pBuf[9] = 13;
return 10;
}
}
//接收短消息
int revSms(char pBuf[],int index)
{
pBuf[0] = 'A';
pBuf[1] = 'T';
pBuf[2] = '+';
pBuf[3] = 'C';
pBuf[4] = 'M';
pBuf[5] = 'G';
pBuf[6] = 'R';
pBuf[7] = '=';
if(index >= 10)
{
pBuf[8] = (char)((index >> 8) & 0xff + 0x30);
pBuf[9] = (char)(index & 0xff + 0x30);
pBuf[10] = 13;
return 11;
}
else
{
pBuf[8] = (char)(index & 0xff + 0x30);
pBuf[9] = 13;
return 10;
}
}
//发送短消息
void packSms_pdu(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 += nLen;
//获得长度的字符数组
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;
nOffset += 1;
for(i = 0; i < nOffset;i++)
{
pOut1[i] = nTemp[i];
}
*nTXLen1 = nOffset;
//封装内容数据
copy(pOut2,0,strHead,0,18);
nOffset = 18;
copy(pOut2,nOffset,chrTemp,0,nOff);
nOffset += nOff;
Encode(pData,pBuf,nLen);
ByteToChar(pBuf,chrTmp,nLen);
copy(pOut2,nOffset,chrTmp,0,(2 * nLen));
nOffset += (2 * nLen);
pOut2[nOffset] = 26;
nOffset += 1;
*nTXLen2 = nOffset;
}
void packSms_text(char pPhone[],int phonelen,char pData[],int nLen,
int *nTXLen1,int *nTXLen2,char pOut1[],char pOut2[])
{
int nOffset;
char nTemp[100];
int i;
nOffset = 0;
nTemp[0] = 'A';
nTemp[1] = 'T';
nTemp[2] = '+';
nTemp[3] = 'C';
nTemp[4] = 'M';
nTemp[5] = 'G';
nTemp[6] = 'S';
nTemp[7] = '=';
nTemp[8] = '"';
//处理电话号码
nOffset = 9;
for(i = 0;i < phonelen;i++)
{
nTemp[nOffset + i] = pPhone[i];
}
nOffset += nLen;
nTemp[nOffset] = '"';
nOffset += 1;
nTemp[nOffset] = 13;
nOffset += 1;
for(i = 0; i < nOffset;i++)
{
pOut1[i] = nTemp[i];
}
*nTXLen1 = nOffset;
//处理发送数据
for(i = 0; i < nLen;i++)
{
pOut2[i] = pData[i];
}
pOut2[nLen] = 26;
*nTXLen2 = nLen + 1;
}
int getSendResponse(char in[],int len)
{
int res = 0;
int i;
char chrTemp = 0;
if(len >= 2)
{
//搜索 > 符号
for(i = 0;i < len;i++)
{
chrTemp = in[i];
if(chrTemp == 62)
{
if(i < len)
{
if(in[i + 1] == 32)
{
//搜索到 > 符号
res = 1;
break;
}
}
else
{
//搜索到 > 符号
res = 1;
break;
}
}
}
}
return res;
}
int isSuccessSend(char in[],int len)
{
int res = 0;
int i;
for(i = 0;i < len;i++)
{
if(in[i] == 'O')
{
if(i < len -1)
{
if(in[i + 1] == 'K')
{
//发送成功
res = 1;
break;
}
}
}
if(in[i] == 'E')
{
if(i < len - 5)
{
if((in[i + 1] == 'R') && (in[i + 2] == 'R')
&& (in[i + 3] == 'O') && (in[i + 4] == 'R'))
{
//发送失败
res = 2;
break;
}
}
}
}
return res;
}
//将源数组的内容拷贝到目的数组
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[])
{
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(1)
{
if(nOrigin >= nLen) break;
out[nCode] = in[nOrigin];
if((nOrigin + 1) >= nLen) break;
out[nCode] |= (char)((in[nOrigin + 1] & 0x01) out[nCode + 1] = (char)((in[nOrigin + 1] >> 1) & 0xff);
if((nOrigin + 2) >= nLen) break;
out[nCode + 1] |= (char)((in[nOrigin + 2] & 0x03) out[nCode + 2] = (char)((in[nOrigin + 2] >> 2) & 0xff);
if((nOrigin + 3) >= nLen) break;
out[nCode + 2] |= (char)((in[nOrigin + 3] & 0x07) out[nCode + 3] = (char)((in[nOrigin + 3] >> 3) & 0xff);
if((nOrigin + 4) >= nLen) break;
out[nCode + 3] |= (char)((in[nOrigin + 4] & 0x0f) out[nCode + 4] = (char)((in[nOrigin + 4] >> 4) & 0xff);
if((nOrigin + 5) >= nLen) break;
out[nCode + 4] |= (char)((in[nOrigin + 5] & 0x1f) out[nCode + 5] = (char)((in[nOrigin + 5] >> 5) & 0xff);
if((nOrigin + 6) >= nLen) break;
out[nCode + 5] |= (char)((in[nOrigin + 6] & 0x3f) out[nCode + 6] = (char)((in[nOrigin + 6] >> 6) & 0xff);
if((nOrigin + 7) >= nLen) break;
out[nCode + 6] |= (char)((in[nOrigin + 7] & 0x7f)
nCode += 7;
nOrigin += 8;
}
}
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] = (char)(chrHi * 16 + chrLow);
}
return;
}
int Decode(char in[],char out[],int nLen)
{
int nOrigin = 0;
int nCode = 0;
int nLenTemp = nLen * 8 / 7;
while(1)
{
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;
}