#pragma once
#include "ril.h"
#include "SMS_CodeDecode.h"
class SMS_RIL
{
public:
SMS_RIL();
HRESULT SmsSendMessage(TCHAR *tcharNumber,TCHAR *tcharInformation);
HRIL m_hRil; //ril实例句柄
CList<CString,CString&> MsgList;
protected:
void Initialize();
static void CALLBACK ResultCallback(DWORD dwCode, HRESULT hrCmdID, const void* lpData, DWORD cbData, DWORD dwParam);
static void CALLBACK NotifyCallback(DWORD dwCode, const void* lpData, DWORD cbData, DWORD dwParam);
private:
bool GetDefaultSubmitMsgCfg(RILMESSAGE &MsgItem);
int WildChar2BYTE(BYTE *MsgToBeBuild,TCHAR *WideCharStr,const int WideMsgLen );
int BuildWildChar(TCHAR *WideCharStr,BYTE *MsgToBeBuild,const int MsgLen );
int SetDestValue(TCHAR *DestVal,TCHAR *temp);
SMS_CodeDecode m_SMS_CodeDecode;
DWORD convertContext2UNICODE(TCHAR *pszOutUnicodeContext,BYTE *pszInrgbMsg,DWORD dwAlphabet,int cchMsgLength, BOOL isCbMsg= FALSE);
};
//////////////////////////////////////////////////////////////////////////
// File name: SMS_RIL.cpp
// Author: houjiong
// Date: 2008-1-3
// Description: 封装RIL的短消息功能
//
// Update:
// Date Name Description
// ============= ============== ======================================
// 2008-1-3 houjiong Create
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SMS_RIL.h"
#include "SMS_CodeDecode.h"
//HRIL SMS_RIL::m_hRil = NULL;
//CList<CString,CString&> SMS_RIL::MsgList=NULL;
SMS_RIL::SMS_RIL()
{
Initialize();
}
void SMS_RIL::Initialize()
{
HRESULT hr = RIL_Initialize(1, ResultCallback, NotifyCallback,RIL_NCLASS_MESSAGE /* RIL_NCLASS_ALL*/, (LPARAM)this, &m_hRil);
if(FAILED(hr))
{
// MessageBox(NULL, _T(">>>>> Calling RIL_Initialize() Fail/r/n"), _T("Caption"), MB_OK);
return ;
}
}
void CALLBACK SMS_RIL::ResultCallback(DWORD dwCode, HRESULT hrCmdID, const void* lpData, DWORD cbData, DWORD dwParam)
{
DWORD *dwNum;
switch (dwCode)
{
case RIL_RESULT_OK:
dwNum = (DWORD*)lpData;
break;
case RIL_RESULT_NOCARRIER:
case RIL_RESULT_NODIALTONE:
case RIL_RESULT_ERROR:
case RIL_RESULT_BUSY:
break;
}
}
void CALLBACK SMS_RIL::NotifyCallback(DWORD dwCode, const void* lpData, DWORD cbData, DWORD dwParam)
{
//RILMESSAGE *pMessage = (RILMESSAGE *)lpData;
switch (dwCode)
{
case RIL_NOTIFY_CONNECT:
case RIL_NOTIFY_DISCONNECT:
case RIL_NOTIFY_RING:
break;
case RIL_NOTIFY_MESSAGE: //通知消息
RILMESSAGE *NewMsg= (RILMESSAGE *)lpData;
SMS_RIL *This = (SMS_RIL*)dwParam;
//RIL_WriteMsg(This->m_hRil, NewMsg, RIL_MSGSTATUS_STOUNSENT);
//This->MsgList.InsertAfter(,"houjiong!")
//This->MsgList.AddTail(CString("XYZ"));
//This->MsgList.AddTail(
//pos = This->MsgList.InsertAfter(pos, CString("ABC"));
//pos = This->MsgList.InsertAfter(pos, CString("123"));
/*CString message;
WildChar2BYTE(pSimMsg->msgOutSubmit.rgbMsg,WideCharStr,0);
NewMsg->msgInDeliver.rgbMsg
NewMsg->msgInDeliver.raOrigAddress.wszAddress;
This->MsgList.AddHead( CString(_T("world")));*/
CString strMessage;
TCHAR *WideCharStr= new TCHAR();
//This->BuildWildChar(WideCharStr,NewMsg->msgInDeliver.rgbMsg,NewMsg->msgInDeliver.cchMsgLength );
//memcpy((void*)&WideCharStr[0],NewMsg->msgInDeliver.rgbMsg,NewMsg->msgInDeliver.cchMsgLength);
This->convertContext2UNICODE(WideCharStr,NewMsg->msgInDeliver.rgbMsg,NewMsg->msgInDeliver.rmdDataCoding.dwAlphabet,NewMsg->msgInDeliver.cchMsgLength);
strMessage=(CString)WideCharStr;
This->MsgList.AddHead( strMessage);
break;
}
}
HRESULT SMS_RIL::SmsSendMessage(TCHAR *tcharNumber,TCHAR *tcharInformation)
{
RILMESSAGE *pSimMsg= new RILMESSAGE;
GetDefaultSubmitMsgCfg(*pSimMsg);
//通常用Unicode编码
pSimMsg->msgOutSubmit.rmdDataCoding.dwAlphabet = RIL_DCSALPHABET_UCS2;
TCHAR *WideCharStr=NULL;
//WideCharStr=_T("对方号码");
WideCharStr=tcharInformation;
//int Msglen = WildChar2BYTE(pSimMsg->msgOutSubmit.rgbMsg,WideCharStr,0);
int Msglen = m_SMS_CodeDecode.WildChar2BYTE(pSimMsg->msgOutSubmit.rgbMsg,WideCharStr,0);
pSimMsg->msgOutSubmit.cchMsgLength = Msglen;
WideCharStr=tcharNumber;
lstrcpy( pSimMsg->msgOutSubmit.raDestAddress.wszAddress, WideCharStr );//发送地址
HRESULT lr = RIL_SendMsg(m_hRil, pSimMsg, 0);
return lr;
}
//////////////////////////////////////////////////////////////////////////
// Function name: GetDefaultSubmitMsgCfg
// Author: houjiong
// Date: 2008-1-4
// Description: Get Default SubmitMsg Config
//
// Input: arg0 : RILMESSAGE: RILMESSAGE
// Return value: bool
// Notes:
// Update:
// Date Name Description
// ============= ============== ======================================
// 2008-1-3 houjiong Create
//////////////////////////////////////////////////////////////////////////
bool SMS_RIL::GetDefaultSubmitMsgCfg(RILMESSAGE &MsgItem)
{
memset((void*)&MsgItem, 0x00, sizeof(RILMESSAGE));
MsgItem.cbSize=sizeof(RILMESSAGE);
MsgItem.dwParams = RIL_PARAM_M_SVCCTRADDRESS|RIL_PARAM_M_TYPE|RIL_PARAM_M_DESTADDRESS|RIL_PARAM_M_PROTOCOLID|RIL_PARAM_M_MSGLENGTH|RIL_PARAM_M_FLAGS|RIL_PARAM_M_VPFORMAT|RIL_PARAM_M_DATACODING;
MsgItem.dwParams |= RIL_PARAM_M_VP|RIL_PARAM_M_MSG;
MsgItem.dwType = RIL_MSGTYPE_OUT_SUBMIT;
MsgItem.dwFlags =RIL_MSGFLAG_STATUSREPORTRETURNED;// RIL_MSGFLAG_STATUSREPORTRETURNED;//标识回复路径等信息
MsgItem.msgOutSubmit.dwVPFormat = RIL_MSGVP_RELATIVE; //0x00000001;
MsgItem.msgOutSubmit.dwProtocolID = RIL_MSGPROTOCOL_SMETOSME;//设置协议ID PID设置 0x0001
//信息中心
MsgItem.raSvcCtrAddress.cbSize=sizeof(MsgItem.raSvcCtrAddress);
MsgItem.raSvcCtrAddress.dwParams = RIL_PARAM_A_ALL;
MsgItem.raSvcCtrAddress.dwType = RIL_ADDRTYPE_INTERNATIONAL;
MsgItem.raSvcCtrAddress.dwNumPlan = RIL_NUMPLAN_TELEPHONE;
lstrcpy( MsgItem.raSvcCtrAddress.wszAddress, _T("8613800280500") );
//设置数据编码方案DCS
MsgItem.msgOutSubmit.rmdDataCoding.cbSize = sizeof(RILMSGDCS);
MsgItem.msgOutSubmit.rmdDataCoding.dwParams = RIL_PARAM_MDCS_TYPE|RIL_PARAM_MDCS_ALPHABET;
MsgItem.msgOutSubmit.rmdDataCoding.dwType = RIL_DCSTYPE_GENERAL;
MsgItem.msgOutSubmit.rmdDataCoding.dwFlags = 0x00000000;
MsgItem.msgOutSubmit.rmdDataCoding.dwMsgClass = 0x00000000;
MsgItem.msgOutSubmit.rmdDataCoding.dwAlphabet = RIL_DCSALPHABET_UCS2;
MsgItem.msgOutSubmit.rmdDataCoding.dwIndication = 0x00000000;
MsgItem.msgOutSubmit.rmdDataCoding.dwLanguage = 0x00000000;
//设置有效时间设置ValidatePeriod, 直接根据当前保存在pSmsList中值来设置
SYSTEMTIME curTime;
GetLocalTime(&curTime);
MsgItem.msgOutSubmit.stVP.wYear = curTime.wYear;
MsgItem.msgOutSubmit.stVP.wMonth = curTime.wMonth;
MsgItem.msgOutSubmit.stVP.wDayOfWeek = curTime.wDayOfWeek;
MsgItem.msgOutSubmit.stVP.wDay = curTime.wDay;
MsgItem.msgOutSubmit.stVP.wHour = curTime.wHour;
MsgItem.msgOutSubmit.stVP.wMinute = curTime.wMinute;
MsgItem.msgOutSubmit.stVP.wSecond = curTime.wSecond;
MsgItem.msgOutSubmit.stVP.wMilliseconds = curTime.wMilliseconds;
//dest address
MsgItem.msgOutSubmit.raDestAddress.cbSize = sizeof(RILADDRESS);
MsgItem.msgOutSubmit.raDestAddress.dwParams = RIL_PARAM_A_ADDRESS|RIL_PARAM_A_TYPE|RIL_PARAM_A_NUMPLAN;//AWI_PARAM_A_ALL
MsgItem.msgOutSubmit.raDestAddress.dwType = RIL_ADDRTYPE_UNKNOWN;
MsgItem.msgOutSubmit.raDestAddress.dwNumPlan = RIL_NUMPLAN_TELEPHONE;
MsgItem.msgOutSubmit.raDestAddress.wszAddress[0] = 0x0000;
//lstrcpy( MsgItem.msgOutSubmit.raDestAddress.wszAddress, _T("8615928041614") );//发送地址
return TRUE;
}
//////////////////////////////////////////////////////////////////////////
// Function name: WildChar2BYTE
// Author: houjiong
// Date: 2008-1-3
// Description: 把我们认识的Unicode编码转换为UCS2编码,用来发送短消息
//
// Input: arg0 :BYTE *MsgToBeBuild
// arg1 :TCHAR *WideCharStr
// arg2 :const int WideMsgLen
// Return value: int
// Notes: 把UNICODE编码转换为UCS2编码,返回长度
// Update:
// Date Name Description
// ============= ============== ======================================
// 2008-1-3 houjiong Create
//////////////////////////////////////////////////////////////////////////
int SMS_RIL::WildChar2BYTE(BYTE *MsgToBeBuild,TCHAR *WideCharStr,const int WideMsgLen )
{
int j=0;
while(*WideCharStr!=0x0000)
{
BYTE temp=0x00;
j++;
temp = (*WideCharStr&0xFF00)>>8;
*MsgToBeBuild = temp;
MsgToBeBuild++;
temp = (*WideCharStr&0x00FF);
*MsgToBeBuild = temp;
MsgToBeBuild++;
WideCharStr++;
}
*MsgToBeBuild=0x00;
return j;
}
int SMS_RIL::BuildWildChar(TCHAR *WideCharStr,BYTE *MsgToBeBuild,const int MsgLen )
{
int i,j;
for(i = 0, j = 0; i < MsgLen; i +=2, j ++)
{
TCHAR temp=0x0000;
#if 1
{
memcpy(&temp,&MsgToBeBuild[i],1);
/*if((0x20==temp))
{
WideCharStr=WideCharStr+SetDestValue(WideCharStr,(TCHAR*)&temp);
temp=0x0000;
}
*/
temp=temp<<8;
memcpy(&temp,&MsgToBeBuild[i+1],1);
WideCharStr=WideCharStr+SetDestValue(WideCharStr,(TCHAR*)&temp);
/*
if((0x000D==temp)||(0x000A==temp))
{
temp=0x000D;
memcpy(WideCharStr,&temp,sizeof(TCHAR));
WideCharStr++;
temp=0x000A;
memcpy(WideCharStr,&temp,sizeof(TCHAR));
}
else
{
memcpy(WideCharStr,&temp,sizeof(TCHAR));
}
*/
}
#else
*WideCharStr=(MsgToBeBuild[i]<<8)|MsgToBeBuild[i+1];
#endif
//WideCharStr++;
//swscanf(&temp,_T("%x"),&WideCharStr[j]);
//MsgToBeBuild=MsgToBeBuild+2;
}
*WideCharStr=0x00;
return j;
}
int SMS_RIL::SetDestValue(TCHAR *DestVal,TCHAR *temp)
{
TCHAR add=0x0000;
int Count=1;
// if((0x000D==*temp)||(0x000A==*temp))
// {
// add=0x000D;
// memcpy(DestVal,&add,sizeof(TCHAR));
// DestVal++;
// add=0x000A;
// memcpy(DestVal,&add,sizeof(TCHAR));
// Count=2;
// }
if((0x000D==*temp)||(0x000A==*temp))
{
add = 0x000A;
memcpy(DestVal,&add,sizeof(TCHAR));
}
else
{
memcpy(DestVal,temp,sizeof(TCHAR));
}
return Count;
}
//////////////////////////////////////////////////////////////////////////
// Function name: convertContext2UNICODE
// Author: Huanglb
// Date: 2004-4-1
// Description: 转换信息内容
//
// Input:
// arg0 :pszOutUnicodeContext,转换结果:Unicode串
// arg1 :pszInrgbMsg,提供要进行转换的内容地址
// arg2 :dwAlphabet,编码方式
// arg3 :cchMsgLength,原信息长度
// Return value:DWORD
// Notes:
// Update:
// Date Name Description
// ============= ============== ======================================
// 2004-4-1 huanglb Create
//////////////////////////////////////////////////////////////////////////
DWORD SMS_RIL::convertContext2UNICODE(TCHAR *pszOutUnicodeContext,BYTE *pszInrgbMsg,DWORD dwAlphabet,int cchMsgLength, BOOL isCbMsg)
{
int WildchLen=0;
switch(dwAlphabet)
{
case RIL_DCSALPHABET_DEFAULT:// (0x00000001) // @constdefine TBD
{
//先将7Bit编码转换成GSM0338编码;
WildchLen=m_SMS_CodeDecode.UnpackSMS(&pszOutUnicodeContext[0], pszInrgbMsg,cchMsgLength, isCbMsg);
m_SMS_CodeDecode.Gsm0338toUNICODE(&pszOutUnicodeContext[0], WildchLen);
}
break;
case RIL_DCSALPHABET_8BIT:// (0x00000002) // @constdefine TBD
{
//每8位一个字符
WildchLen=m_SMS_CodeDecode.Convert8Bit2UNICODE(&pszOutUnicodeContext[0],pszInrgbMsg,cchMsgLength);
}
break;
case RIL_DCSALPHABET_UCS2://Unicode 编码
{
WildchLen=m_SMS_CodeDecode.BuildWildChar(&pszOutUnicodeContext[0],pszInrgbMsg,cchMsgLength*sizeof(TCHAR));
}
break;
default:
memcpy((void*)&pszOutUnicodeContext[0],pszInrgbMsg,cchMsgLength);
break;
}
return WildchLen;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hou_jiong/archive/2008/03/11/2167722.aspx