📄 hwmobile.cpp
字号:
// HwMobile.cpp: implementation of the CHwMobile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DllHwMobile.h"
#include "HwMobile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#ifndef _2Lines
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHwMobile::CHwMobile(LOG_FUNC ExternLog)
: m_nInitOK ( TRUE )
, m_bSoundPrompt ( TRUE )
, m_nBaudRate ( 0 )
, m_nComNO ( 0 )
, CHwCOM ( ExternLog )
, m_Proc_SaveSMSCFromGSM ( NULL )
{
Log = ExternLog;
ASSERT ( Log );
DbgLog ( L_DEBUG, "CHwMobile 构造完成" );
#ifndef _PUBLISH
{ //d
char* pSrc = "0891683108705505F0440D91683141430121F10008502121224435008C050003040201525B624D62118A664E868A6600630061006C006C0067006C006500200031002E002000330033589E5F376027752862364F46767C73FE589E5F37602775286236902398108A02529F80FD4E5F4E0D80FD8D777528002E00208A66554F4E00500B516C53F8768492B7552E902357FA672C7684529F80FD4E5F4E0D80FD6F14793A002E00205982";
int nHandleBytes = strlen(pSrc);
SM_PARAM Dst = {0};
gsmDecodePdu ( pSrc, nHandleBytes, &Dst );
BYTE buf[] = {0xa9, 0x6f, 0x0};
BYTE buf1[] = ",";
//u 个别字符先翻译,如“﹐”
CString csRes = GBAndBig5Convert ( "签了﹐拿了钱" );
csRes = GBAndBig5Convert ( "遲緩衝動機會議論點數據實務農藥方針織畫報廢棄權勢力氣質變遷" );
} //d
#endif
}
CHwMobile::~CHwMobile()
{
// 恢复短信通知方式:
if ( m_nInitOK && m_handleCOM && m_handleCOM != INVALID_HANDLE_VALUE )
{
Write("AT+CNMI=0,0\r" );
}
m_UIntAry_NeedDelSMS.RemoveAll ();
}
//
// PDU编码,用于编制、发送短消息
// 输入: pSrc - 源PDU参数指针
// 输出: szDst - 目标PDU串指针
// 返回: 目标PDU串长度
//
int CHwMobile::gsmEncodePdu(const SM_PARAM *pSrc, char *szDst, int nDstSize )
{
int nLength=0; // 内部用的串长度
int nDstLength=0; // 目标PDU串长度
unsigned char buf[1024]={0}; // 内部用的缓冲区
// strcpy ( (char*)pSrc->TPA, "13510517717" );
// strcpy ( (char*)pSrc->SCA, "8613800755500" );
// SMSC 地址信息段
nLength = strlen(pSrc->SCA); // SMSC地址字符串的长度
buf[0] = (char)((nLength & 1) == 0 ? nLength : nLength + 1) / 2 + 1; // SMSC地址信息长度
buf[1] = 0x91; // 0x0 : 本地格式; 0x91 : 用国际格式号码,就是:国家代码+手机号码
nDstLength = gsmBytes2String(buf, szDst, 2, nDstSize); // 转换2个字节到目标PDU串
// 转换SMSC号码到目标PDU串
nDstLength += gsmInvertNumbers(pSrc->SCA, &szDst[nDstLength], nLength,nDstSize-nDstLength);
// TPDU段基本参数、目标地址等
nLength = strlen(pSrc->TPA); // TP-DA地址字符串的长度
buf[0] = 0x11; // 是发送短信(TP-MTI=01),TP-VP用相对格式(TP-VPF=10)
buf[1] = 0; // TP-MR=0
buf[2] = (char)nLength; // 目标地址数字个数(TP-DA地址字符串真实长度)
buf[3] = pSrc->bInternationalMobileNO ? 0x91 : 0x0; // 0x0 : 本地格式; 0x91 : 用国际格式号码,就是:国家代码+手机号码
nDstLength += gsmBytes2String(buf, &szDst[nDstLength], 4, nDstSize-nDstLength ); // 转换4个字节到目标PDU串
nDstLength += gsmInvertNumbers(pSrc->TPA, &szDst[nDstLength], nLength,nDstSize-nDstLength); // 转换TP-DA到目标PDU串
// TPDU段协议标识、编码方式、用户信息等
nLength = strlen(pSrc->TP_UD_Byte); // 用户信息字符串的长度
buf[0] = pSrc->TP_PID; // 协议标识(TP-PID)
buf[1] = pSrc->TP_DCS; // 用户信息编码方式(TP-DCS)
buf[2] = 0xFF; // 有效期(TP-VP)为 : 0x00~0xff (5分钟~最长)
if(pSrc->TP_DCS == GSM_7BIT)
{
// 7-bit编码方式
buf[3] = nLength; // 编码前长度
nLength = gsmEncode7bit(pSrc->TP_UD_Byte, &buf[4], nLength+1, sizeof(buf)-4) + 4; // 转换TP-DA到目标PDU串
}
else if(pSrc->TP_DCS == GSM_UCS2)
{
// UCS2编码方式
buf[3] = gsmEncodeUcs2(pSrc->TP_UD_Byte, &buf[4], nLength, sizeof(buf)-4, (WCHAR*)pSrc->TP_UD_Wide); // 转换TP-DA到目标PDU串
nLength = buf[3] + 4; // nLength等于该段数据长度
}
else
{
// 8-bit编码方式
buf[3] = gsmEncode8bit(pSrc->TP_UD_Byte, &buf[4], nLength, sizeof(buf)-4); // 转换TP-DA到目标PDU串
nLength = buf[3] + 4; // nLength等于该段数据长度
}
nDstLength += gsmBytes2String(buf, &szDst[nDstLength], nLength,nDstSize-nDstLength); // 转换该段数据到目标PDU串
// 返回目标字符串长度
return nDstLength;
}
//
// PDU解码,用于接收、阅读短消息
// 输入: pSrc - 源PDU串指针
// 输出: pDst - 目标PDU参数指针
// 返回: >= 0 用户信息串长度; -1 数据长度不够;-2 非法的数据
//
int CHwMobile::gsmDecodePdu ( char* pSrc, IN OUT int &nHandleBytes, SM_PARAM *pDst )
{
ASSERT ( pSrc && nHandleBytes>0 && pDst );
const int nSrcLength = nHandleBytes;
nHandleBytes = 0;
int nDstLength=0; // 目标PDU串长度
unsigned int tmp=0; // 内部用的临时字节变量
unsigned char buf[1024] = {0}; // 内部用的缓冲区
// SMSC地址信息段
gsmString2Bytes(pSrc+nHandleBytes, (unsigned char *)&tmp, 2, sizeof(tmp) ); // 取长度
tmp = (tmp - 1) * 2; // SMSC号码串长度
nHandleBytes += 4; // 指针后移,忽略了SMSC地址格式
if ( nHandleBytes > nSrcLength )
return -1;
if ( tmp >= sizeof ( pDst->SCA ) )
return -2;
gsmSerializeNumbers ( pSrc+nHandleBytes, pDst->SCA, tmp, sizeof(pDst->SCA) ); // 转换SMSC号码到目标PDU串
nHandleBytes += tmp; // 指针后移
if ( nHandleBytes > nSrcLength )
return -1;
// TPDU段基本参数
gsmString2Bytes(pSrc+nHandleBytes, (unsigned char *)&tmp, 2, sizeof(tmp) ); // 取基本参数
nHandleBytes += 2; // 指针后移
if ( nHandleBytes > nSrcLength )
return -1;
// 取回复号码
gsmString2Bytes(pSrc+nHandleBytes, (unsigned char *)&tmp, 2, sizeof(tmp) ); // 取长度
if(tmp & 1) tmp += 1; // 调整奇偶性
nHandleBytes += 4; // 指针后移,忽略了回复地址(TP-RA)格式
if ( nHandleBytes > nSrcLength )
return -1;
// 取TP-RA号码
if ( tmp >= sizeof ( pDst->TPA ) )
return -2;
gsmSerializeNumbers(pSrc+nHandleBytes, pDst->TPA, tmp, sizeof(pDst->TPA) );
nHandleBytes += tmp; // 指针后移
if ( nHandleBytes > nSrcLength )
return -1;
// TPDU段协议标识、编码方式、用户信息等
gsmString2Bytes(pSrc+nHandleBytes, (unsigned char*)&pDst->TP_PID, 2, sizeof(pDst->TP_PID)); // 取协议标识(TP-PID)
nHandleBytes += 2; // 指针后移
if ( nHandleBytes > nSrcLength )
return -1;
gsmString2Bytes(pSrc+nHandleBytes, (unsigned char*)&pDst->TP_DCS, 2, sizeof(pDst->TP_DCS)); // 取编码方式(TP-DCS)
nHandleBytes += 2; // 指针后移
if ( nHandleBytes > nSrcLength )
return -1;
gsmSerializeNumbers(pSrc+nHandleBytes, pDst->TP_SCTS, 14, sizeof(pDst->TP_SCTS)); // 服务时间戳字符串(TP_SCTS)
nHandleBytes += 14; // 指针后移
if ( nHandleBytes > nSrcLength )
return -1;
gsmString2Bytes ( pSrc+nHandleBytes, (unsigned char *)&tmp, 2, sizeof(tmp) ); // 用户信息长度(TP-UDL)
nHandleBytes += 2; // 指针后移
if ( nHandleBytes > nSrcLength )
return -1;
int nLength = 0;
if(pDst->TP_DCS == GSM_7BIT)
{
// 7-bit解码
nLength = (tmp & 7) ? ((int)tmp * 7 / 4 + 2) : ((int)tmp * 7 / 4);
if ( nHandleBytes + nLength > nSrcLength )
return -1;
nDstLength = gsmString2Bytes(pSrc+nHandleBytes, buf, nLength, sizeof(buf) ); // 格式转换
gsmDecode7bit(buf, pDst->TP_UD_Byte, nDstLength, sizeof(pDst->TP_UD_Byte) ); // 转换到TP-DU
nDstLength = tmp;
}
else if(pDst->TP_DCS == GSM_UCS2)
{
nLength = tmp * 2;
if ( nHandleBytes + nLength > nSrcLength )
return -1;
// UCS2解码
nDstLength = gsmString2Bytes(pSrc+nHandleBytes, buf, nLength, sizeof(buf) ); // 格式转换
nDstLength = gsmDecodeUcs2(buf, pDst->TP_UD_Byte, nDstLength, sizeof(pDst->TP_UD_Byte),
pDst->TP_UD_Wide, LENGTH(pDst->TP_UD_Wide) ); // 转换到TP-DU
}
else
{
nLength = tmp * 2;
if ( nHandleBytes + nLength > nSrcLength )
return -1;
// 8-bit解码
nDstLength = gsmString2Bytes(pSrc+nHandleBytes, buf, nLength, sizeof(buf) ); // 格式转换
nDstLength = gsmDecode8bit(buf, pDst->TP_UD_Byte, nDstLength, sizeof(pDst->TP_UD_Byte) ); // 转换到TP-DU
}
nHandleBytes += nLength; // 指针后移
// 返回目标字符串长度
return nDstLength;
}
//
// 字节数据转换为可打印字符串
// 如:{0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01} --> "C8329BFD0E01"
// 输入: pSrc - 源数据指针
// nSrcLength - 源数据长度
// 输出: szDst - 目标字符串指针
// 返回: 目标字符串长度
//
int CHwMobile::gsmBytes2String(const unsigned char *pSrc, char *szDst, int nSrcLength, int nDstSize)
{
const char tab[]="0123456789ABCDEF"; // 0x0-0xf的字符查找表
char *pDstTail = (char *)(szDst + nDstSize);
for (int i = 0; i < nSrcLength; i++)
{
*szDst++ = tab[*pSrc >> 4]; // 输出高4位
if ( (char*)szDst >= pDstTail ) break;
*szDst++ = tab[*pSrc & 0x0f]; // 输出低4位
if ( (char*)szDst >= pDstTail ) break;
pSrc++;
}
// 输出字符串加个结束符
*szDst = '\0';
// 返回目标字符串长度
return (nSrcLength * 2);
}
//
// 正常顺序的字符串转换为两两颠倒的字符串,若长度为奇数,补'F'凑成偶数
// 如:"8613851872468" --> "683158812764F8"
// 输入: pSrc - 源字符串指针
// nSrcLength - 源字符串长度
// 输出: szDst - 目标字符串指针
// 返回: 目标字符串长度
//
int CHwMobile::gsmInvertNumbers(const char *pSrc, char *szDst, int nSrcLength, int nDstSize)
{
int nDstLength=0; // 目标字符串长度
char ch; // 用于保存一个字符
char *pDstTail = (char *)(szDst + nDstSize);
// 复制串长度
nDstLength = nSrcLength;
// 两两颠倒
for(int i=0; i<nSrcLength;i+=2)
{
ch = *pSrc++; // 保存先出现的字符
*szDst++ = *pSrc++; // 复制后出现的字符
if ( (char*)szDst >= pDstTail ) break;
*szDst++ = ch; // 复制先出现的字符
if ( (char*)szDst >= pDstTail ) break;
}
// 源串长度是奇数吗?
if(nSrcLength & 1)
{
*(szDst-2) = 'F'; // 补'F'
nDstLength++; // 目标串长度加1
}
// 输出字符串加个结束符
*szDst = '\0';
// 返回目标字符串长度
return nDstLength;
}
//
// 两两颠倒的字符串转换为正常顺序的字符串
// 如:"683158812764F8" --> "8613851872468"
// 输入: pSrc - 源字符串指针
// nSrcLength - 源字符串长度
// 输出: szDst - 目标字符串指针
// 返回: 目标字符串长度
//
int CHwMobile::gsmSerializeNumbers(const char *pSrc, char *szDst, int nSrcLength, int nDstSize)
{
int nDstLength=0; // 目标字符串长度
char ch; // 用于保存一个字符
char *pDstTail = (char *)(szDst + nDstSize);
// 复制串长度
nDstLength = nSrcLength;
// 两两颠倒
for(int i=0; i<nSrcLength;i+=2)
{
ch = *pSrc++; // 保存先出现的字符
*szDst++ = *pSrc++; // 复制后出现的字符
if ( (char*)szDst >= pDstTail ) break;
*szDst++ = ch; // 复制先出现的字符
if ( (char*)szDst >= pDstTail ) break;
}
// 最后的字符是'F'吗?
if(*(szDst-1) == 'F')
{
szDst--;
nDstLength--; // 目标字符串长度减1
}
// 输出字符串加个结束符
*szDst = '\0';
// 返回目标字符串长度
return nDstLength;
}
//
// 7bit编码
// 输入: pSrc - 源字符串指针
// nSrcLength - 源字符串长度
// 输出: szDst - 目标编码串指针
// 返回: 目标编码串长度
//
int CHwMobile::gsmEncode7bit(const char *pSrc, unsigned char *szDst, int nSrcLength, int nDstSize)
{
int nSrc=0; // 源字符串的计数值
int nDst=0; // 目标编码串的计数值
int nChar=0; // 当前正在处理的组内字符字节的序号,范围是0-7
unsigned char nLeft=0; // 上一字节残余的数据
char *pDstTail = (char *)(szDst + nDstSize);
// 计数值初始化
nSrc = 0;
nDst = 0;
// 将源串每8个字节分为一组,压缩成7个字节
// 循环该处理过程,直至源串被处理完
// 如果分组不到8字节,也能正确处理
while (nSrc < nSrcLength)
{
// 取源字符串的计数值的最低3位
nChar = nSrc & 7;
// 处理源串的每个字节
if(nChar == 0)
{
// 组内第一个字节,只是保存起来,待处理下一个字节时使用
nLeft = *pSrc;
}
else
{
// 组内其它字节,将其右边部分与残余数据相加,得到一个目标编码字节
*szDst = (*pSrc << (8-nChar)) | nLeft;
// 将该字节剩下的左边部分,作为残余数据保存起来
nLeft = *pSrc >> nChar;
// 修改目标串的指针和计数值
szDst++;
if ( (char*)szDst >= pDstTail ) break;
nDst++;
}
// 修改源串的指针和计数值
pSrc++;
nSrc++;
}
// 返回目标串长度
return nDst;
}
// 7bit解码
// 输入: pSrc - 源编码串指针
// nSrcLength - 源编码串长度
// 输出: szDst - 目标字符串指针
// 返回: 目标字符串长度
int CHwMobile::gsmDecode7bit(const unsigned char* pSrc, char* szDst, int nSrcLength, int nDstSize)
{
int nSrc=0; // 源字符串的计数值
int nDst=0; // 目标解码串的计数值
int nByte=0; // 当前正在处理的组内字节的序号,范围是0-6
unsigned char nLeft=0; // 上一字节残余的数据
char *pDstTail = (char *)(szDst + nDstSize);
// 计数值初始化
nSrc = 0;
nDst = 0;
// 组内字节序号和残余数据初始化
nByte = 0;
nLeft = 0;
// 将源数据每7个字节分为一组,解压缩成8个字节
// 循环该处理过程,直至源数据被处理完
// 如果分组不到7字节,也能正确处理
while(nSrc<nSrcLength)
{
// 将源字节右边部分与残余数据相加,去掉最高位,得到一个目标解码字节
*szDst = ((*pSrc << nByte) | nLeft) & 0x7f;
// 将该字节剩下的左边部分,作为残余数据保存起来
nLeft = *pSrc >> (7-nByte);
// 修改目标串的指针和计数值
szDst++;
if ( (char*)szDst >= pDstTail ) break;
nDst++;
// 修改字节计数值
nByte++;
// 到了一组的最后一个字节
if(nByte == 7)
{
// 额外得到一个目标解码字节
*szDst = nLeft;
// 修改目标串的指针和计数值
szDst++;
if ( (char*)szDst >= pDstTail ) break;
nDst++;
// 组内字节序号和残余数据初始化
nByte = 0;
nLeft = 0;
}
// 修改源串的指针和计数值
pSrc++;
nSrc++;
}
// 输出字符串加个结束符
*szDst = '\0';
// 返回目标串长度
return nDst;
}
//
// UCS2编码
// 输入: pSrc - 源字符串指针
// nSrcLength - 源字符串长度
// 输出: szDst - 目标编码串指针
// 返回: 目标编码串长度
//
int CHwMobile::gsmEncodeUcs2(const char *pSrcByte, unsigned char *szDst, int nSrcLength, int nDstSize, WCHAR *wzSrc/*=NULL*/ )
{
int nDstLength=0; // UNICODE宽字符数目
WCHAR wchar[256] = {0}; // UNICODE串缓冲区
char *pDstTail = (char *)(szDst + nDstSize);
// 字符串-->UNICODE串
if ( wzSrc )
nDstLength = wcslen ( wzSrc );
if ( nDstLength <= 0 )
{
nDstLength = MultiByteToWideChar(CP_ACP, 0, pSrcByte, nSrcLength, wchar, 256);
wzSrc = wchar;
}
// 高低字节对调,输出
for(int i=0; i<nDstLength; i++)
{
*szDst++ = wzSrc[i] >> 8; // 先输出高位字节
if ( (char*)szDst >= pDstTail ) break;
*szDst++ = wzSrc[i] & 0xff; // 后输出低位字节
if ( (char*)szDst >= pDstTail ) break;
}
// 返回目标编码串长度
return nDstLength * 2;
}
//
// 计算字符个数
//
int CountCharNumber ( char *szStr, char cCalc )
{
ASSERT ( szStr );
int nCount = 0;
for ( int i=0; szStr[i]!='\0'; i++ )
{
if ( szStr[i] == cCalc )
nCount ++;
}
return nCount;
}
// UCS2解码
// 输入: pSrc - 源编码串指针
// nSrcLength - 源编码串长度
// 输出: szDst - 目标字符串指针
// 返回: 目标字符串长度
int CHwMobile::gsmDecodeUcs2(
const unsigned char* pSrc,
char* szDst,
int nSrcLength,
int nDstSize,
WCHAR* wzSMSContent_Wide/*=NULL*/,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -