📄 wsmsdef.cpp
字号:
/*****************************************************************************
* GSM SMS Routines
*
* Writen by Menghongwen@<menghongwen@smmail.cn>
*
* Dec., 2004 copyright YINHUA Technology, all right reserved
\****************************************************************************/
#include "wsmsdef.h"
const char szCommandHeader[4]="AT+";
const char scComandAssign = 0x3d;
const char scCommandEnd = 0x0d;
const char scCommandSend =0x1a;
const char * __GSMRESPOK = "OK";
const char * __GSMRESPERR= "ERROR";
//---------------------------------------------------------------------------
char * bcd2asc(char *hexstr, char *ascstr,short length )
{
int h,a;
unsigned char uc;
ascstr[0] = 0x0;
if ( length < 1 ) return(hexstr);
h = length-1;
a = length+length-1;
ascstr[a+1]='\0';
while(h>=0) {
uc = hexstr[h]&0x0f;
ascstr[a--] = uc + ((uc>9)?('A'-10):'0');
uc = (hexstr[h--]&0xf0)>>4;
ascstr[a--] = uc + ((uc>9)?('A'-10):'0');
}
return ascstr;
}
//---------------------------------------------------------------------------
char * asc2bcd(char *ascstr, char *bcdstr,short bcdlen)
{
unsigned char hi,lo;
int i,n;
for(i=n=0; n<bcdlen;) {
hi = toupper(ascstr[i++]);
lo = toupper(ascstr[i++]);
bcdstr[n++] = (((hi>='A')?(hi-'A'+10):(hi-'0'))<<4)|((lo>='A')?(lo-'A'+10):(lo-'0'));
}
return(bcdstr);
}
//---------------------------------------------------------------------------
char * bcd2ascb(char *hexstr, char *ascstr,short length )
{
int h,a;
unsigned char uc;
ascstr[0] = 0x0;
if ( length < 1 ) return(hexstr);
for(h=a=0; h<length; h++) {
uc = (hexstr[h]&0xf0)>>4;
ascstr[a++] = uc + ((uc>9)?('A'-10):'0');
uc = hexstr[h]&0x0f;
ascstr[a++] = uc + ((uc>9)?('A'-10):'0');
ascstr[a++] = 0x20;
}
ascstr[a-1] = 0;
return ascstr;
}
//---------------------------------------------------------------------------
char * asc2bcdInv(char *ascstr, char *bcdstr,short bcdlen)
{
unsigned char hi,lo;
int i,n;
for(i=n=0; n<bcdlen;) {
lo = toupper(ascstr[i++]);
hi = toupper(ascstr[i++]);
bcdstr[n++] = (((hi>='A')?(hi-'A'+10):(hi-'0'))<<4)|((lo>='A')?(lo-'A'+10):(lo-'0'));
}
return(bcdstr);
}
//---------------------------------------------------------------------------
void trimspace(char *string )
{
char *s,*d;
d = string;
s = string;
for(;*s!=0x0;) {
if (*s == 0x20){ s++; continue; }
*d++ = *s++;
}
*d = 0x0;
}
//---------------------------------------------------------------------------
short ascii2AddressString(char *pAsc, char *pRc)
{
char szSrc[64],tc;
short i,len;
strcpy(szSrc,pAsc);
len = strlen(pAsc);
if (len%2) {
szSrc[len]='F';
len++;
szSrc[len]=0x0;
}
for(i=0; i<len;i+=2) {
tc = szSrc[i];
szSrc[i] = szSrc[i+1];
szSrc[i+1] = tc;
}
len /=2;
asc2bcd(szSrc,pRc,len);
return len;
}
//---------------------------------------------------------------------------
BYTE encode2UCS2(AnsiString astr, BYTE *pDst)
{
int nDstLength;
WCHAR wchar[128];
nDstLength = ::MultiByteToWideChar(CP_ACP, 0, astr.c_str(), astr.Length(), wchar, 128);
for(int i=0; i<nDstLength; i++) {
*pDst++ = wchar[i] >> 8;
*pDst++ = wchar[i] & 0xff;
}
return nDstLength + nDstLength;
}
//---------------------------------------------------------------------------
BYTE decodeUcs2(BYTE * pSrc, BYTE * pDst, BYTE nSrcLength)
{
int nDstLength;
WCHAR wchar[128];
for(int i=0; i<nSrcLength/2; i++) {
wchar[i] = *pSrc++ << 8;
wchar[i] |= *pSrc++;
}
nDstLength = ::WideCharToMultiByte(CP_ACP, 0, wchar, nSrcLength/2, pDst, 160, NULL, NULL);
pDst[nDstLength] = '\0';
return nDstLength;
}
//---------------------------------------------------------------------------
BYTE decode7bit(BYTE * pSrc, char* pDst, BYTE nSrcLength)
{
int nSrc; // 源字符串的计数值
int nDst; // 目标解码串的计数值
int nByte; // 当前正在处理的组内字节的序号,范围是0-6
unsigned char nLeft; // 上一字节残余的数据
// 计数值初始化
nSrc = 0;
nDst = 0;
// 组内字节序号和残余数据初始化
nByte = 0;
nLeft = 0;
// 将源数据每7个字节分为一组,解压缩成8个字节
// 循环该处理过程,直至源数据被处理完
// 如果分组不到7字节,也能正确处理
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;
}
//---------------------------------------------------------------------------
BYTE decode8bit(BYTE * pSrc, char* pDst, BYTE nSrcLength)
{
// 简单复制
memcpy(pDst, pSrc, nSrcLength);
// 输出字符串加个结束符
*pDst = '\0';
return nSrcLength;
}
//---------------------------------------------------------------------------
TGSMAddress::TGSMAddress()
{
settype(0x91);
uclen = 0;
ucoctets = 0;
}
//---------------------------------------------------------------------------
TGSMAddress::TGSMAddress(char *psmsc)
{
settype(0x91);
setvalue(psmsc);
}
//---------------------------------------------------------------------------
void TGSMAddress::setvalue(char *psmsc)
{
if ( psmsc != NULL ) {
ucoctets = strlen(psmsc);
if(ucoctets<16) strcpy(ascii,psmsc);
uclen = ascii2AddressString(psmsc,value);
}
}
//---------------------------------------------------------------------------
short TGSMAddress::loadstring(char *pstream, bool bdao)
{
short tl,i;
char sztempasc[64], sztemphex[64];
sztempasc[0] = pstream[0];
sztempasc[1] = pstream[1];
asc2bcd(sztempasc,sztemphex,1);
tl = sztemphex[0];
if(bdao) {
if(tl%2) tl++;
i = tl>>1;
tl=i+1;
}
if ( tl == 0x0 ) {
uclen = 0;
ucoctets = 0;
return 2;
}
memcpy(sztempasc,&pstream[2],tl+tl);
asc2bcd(sztempasc,sztemphex,tl);
settype(sztemphex[0]);
uclen = tl-1;
memcpy(value,&sztemphex[1],uclen);
value[uclen] = 0x0;
for(i=0; i<uclen; i++ ) {
ascii[i+i] = (value[i]&0x0f) + (unsigned char)0x30;
ascii[i+i+1] = ((unsigned char)(value[i]& 0xf0))>>4;
ascii[i+i+1] += 0x30;
}
ucoctets = i+i;
if (ascii[ucoctets-1] == 0x3F ) ucoctets--;
ascii[ucoctets] = 0x0;
return (tl+tl+2);
}
//---------------------------------------------------------------------------
TGSMAddress & TGSMAddress::operator= (TGSMAddress& a)
{
uclen = a.uclen;
ucoctets =a.ucoctets;
type.value = a.type.value;
memcpy(value,a.value,16);
memcpy(ascii,a.ascii,16);
}
//---------------------------------------------------------------------------
TSMSPDUS::TSMSPDUS()
{
sca = 0;
tpda = 0;
foctet.bitmap.tpmti = 1; // submit
foctet.bitmap.tprd = 0; // enable duplicate
foctet.bitmap.tpvpf = 2; // vaildty period: relatice format
foctet.bitmap.tpspr = 0; // no status report request
foctet.bitmap.tpudhi= 0; // on header information in content
foctet.bitmap.tprp = 0; // no reply path specified
tppid.bitmap.b76 = 0; // defined by b50
tppid.bitmap.b5 = 0; // NO Networking, peer to peer
tppid.bitmap.b40 = 0; // if bit5 then implicit
tpdcs.bitmap.gdatacode = 0;
tpdcs.bitmap.compress = 0;
tpdcs.bitmap.hascalss = 0;
tpdcs.bitmap.alphabet = 2; // ucs2
tpdcs.bitmap.calss = 0;
tpvp = 0x0;
}
//---------------------------------------------------------------------------
BYTE TSMSPDUS::length()
{
BYTE count=0x08;
count += tpda->valuelen();
count +=tpudl;
return count;
}
//---------------------------------------------------------------------------
TGSMCommand::TGSMCommand()
{
usSend = 0;
}
//---------------------------------------------------------------------------
bool TGSMCommand::opencomm(char *pPort)
{
return hComm.OpenComm(pPort);
}
//---------------------------------------------------------------------------
bool TGSMCommand::closecomm()
{
return hComm.CloseComm();
}
//---------------------------------------------------------------------------
char *TGSMCommand::sendAsString()
{
return szSend;
}
//---------------------------------------------------------------------------
char *TGSMCommand::recvAsString()
{
return szRecv;
}
//---------------------------------------------------------------------------
void TGSMCommand::atcommand(char *pname, bool bAssign)
{
usSend = strlen(pname);
memcpy(szSend,szCommandHeader,3);
strcpy(&szSend[3], pname);
usSend += 3;
if (bAssign) {
szSend[usSend] = scComandAssign;
usSend ++;
}
}
//---------------------------------------------------------------------------
void TGSMCommand::cleardata()
{
usSend = 0;
}
//---------------------------------------------------------------------------
void TGSMCommand::appendstr(char *pStr)
{
short len=strlen(pStr);
memcpy(&szSend[usSend],pStr,len);
usSend += len;
}
//---------------------------------------------------------------------------
void TGSMCommand::append(BYTE *pData, BYTE dl)
{
memcpy(&szSend[usSend],pData,dl);
usSend += dl;
}
//---------------------------------------------------------------------------
void TGSMCommand::append(BYTE c)
{
szSend[usSend++] = c;
}
//---------------------------------------------------------------------------
void TGSMCommand::appendcr()
{
szSend[usSend++] = scCommandEnd;
}
//---------------------------------------------------------------------------
void TGSMCommand::appendcz()
{
szSend[usSend++] = scCommandSend;
}
//---------------------------------------------------------------------------
void TGSMCommand::toAscii()
{
bcd2asc(szSend,szString,usSend);
usSend+=usSend;
memcpy(szSend,szString,usSend);
}
//---------------------------------------------------------------------------
bool TGSMCommand::send(short msSecond)
{
rc = hComm.WriteComm(szSend, usSend);
if ( rc!=usSend) return false;
// DEBUG !!!!!!!!!!!!!!!!!!!!!!
memset(szRecv,0x0,1024);
// DEBUG !!!!!!!!!!!!!!!!!!!!!!
if(msSecond) Sleep(msSecond);
usRecv = hComm.ReadComm(szRecv, __GSMCOMMINBUFFSIZE);
if ( usRecv < 0 ) return false;
szRecv[usRecv] = 0x0;
return true;
}
//---------------------------------------------------------------------------
bool TGSMCommand::querychar(char qc)
{
if (strchr(szRecv,qc)!=NULL ) return true;
return false;
}
//---------------------------------------------------------------------------
bool TGSMCommand::querystring(const char *pStr)
{
if ( strstr(szRecv,pStr) != NULL ) return true;
return false;
}
//---------------------------------------------------------------------------
short TGSMCommand::queryShortValue(char *pName)
{
short distance,i=0;
char *p = strstr(szRecv,pName);
if(p==NULL) return 0;
p += strlen(pName);
distance = usRecv - (unsigned short)(p-szRecv);
do {
szString[i++] = *p++;
if ( *p == 0x0d ) break;
} while(--distance);
szString[i] = 0x0;
return StrToInt(szString);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -