?? 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 + -