www.pudn.com > GPRS__moder.rar > wsmsdef.cpp, change:2004-12-17,size:12362b


/***************************************************************************** 
 *    GSM SMS Routines 
 * 
 *    Writen by Menghongwen@ 
 * 
 *   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='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>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='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> 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> (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>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); 
} 
//---------------------------------------------------------------------------