?? smsprs.cpp
字號:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Module Name:
smsprs.cpp
Abstract:
Code to convert an incoming SMS message from GSM format to an
RILMESSAGE struct.
Notes:
--*/
#include "precomp.h"
//
// Command types
//
static const DWORD g_rgdwCommandTypes[] =
{
0x00, // RIL_MSGCMDTYPE_STATUSREQ
0x01, // RIL_MSGCMDTYPE_CANCELSTATUSREQ
0x02, // RIL_MSGCMDTYPE_DELETEMESSAGE
0x03, // RIL_MSGCMDTYPE_ENABLESTATUSREQ
};
#define NUM_COMMANDTYPES (sizeof(g_rgdwCommandTypes) / sizeof(DWORD))
//
// Geographical Scope
//
static const DWORD g_rgdwGeographicalScopes[] =
{
RIL_GEOSCOPE_CELL_IMMEDIATE,
RIL_GEOSCOPE_PLMN,
RIL_GEOSCOPE_LOCATIONAREA,
RIL_GEOSCOPE_CELL,
};
#define NUM_GEOGRAPHICALSCOPES (sizeof(g_rgdwGeographicalScopes) / sizeof(DWORD))
//
// DCS Languages
//
static const DWORD g_rgdwDCSLanguagesGroup1[] =
{
RIL_DCSLANG_GERMAN,
RIL_DCSLANG_ENGLISH,
RIL_DCSLANG_ITALIAN,
RIL_DCSLANG_FRENCH,
RIL_DCSLANG_SPANISH,
RIL_DCSLANG_DUTCH,
RIL_DCSLANG_SWEDISH,
RIL_DCSLANG_DANISH,
RIL_DCSLANG_PORTUGUESE,
RIL_DCSLANG_FINNISH,
RIL_DCSLANG_NORWEGIAN,
RIL_DCSLANG_GREEK,
RIL_DCSLANG_TURKISH,
RIL_DCSLANG_HUNGARIAN,
RIL_DCSLANG_POLISH,
RIL_DCSLANG_UNKNOWN,
};
static const DWORD g_rgdwDCSLanguagesGroup2[] =
{
RIL_DCSLANG_CZECH,
RIL_DCSLANG_ARABIC,
RIL_DCSLANG_RUSSIAN,
RIL_DCSLANG_ICELANDIC,
RIL_DCSLANG_UNKNOWN,
};
//
// Constants and macros for parsing Cell Broadcast messages
//
const UINT CELLBROADCAST_HEADER_LENGTH = 6;
#define MESSAGECODE_FROM_SERIALNUMBER(sn) ((sn >> 4) & 0x3ff)
#define GEOSCOPE_FROM_SERIALNUMBER(sn) g_rgdwGeographicalScopes[((sn >> 14) & 0x3)]
#define UPDATENUMBER_FROM_SERIALNUMBER(sn) (sn & 0xf)
#define PAGENUMBER(b) ((b >> 4) & 0xf)
#define TOTALPAGES(b) (b & 0xf)
//
// Table used to map semi-byte values to BCD characters
//
static const WCHAR g_rgwchSemiByteToBCDMap[16] = { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7', L'8', L'9', L'*', L'#', L'a', L'b', L'c', L' ' };
//
// Convert a semi-byte into a BCD value
//
static WCHAR SemiByteToBCD(const BYTE bByte, const BOOL fHigh)
{
FUNCTION_TRACE(SemiByteToBCD);
// If a semi-octect is a non-integer value != 0x0f, then the follow mapppings hold.
// If a semi-octect prior to the last octect contains 0x0f, we skip that octet and continue on.
// GSM 03.40 section 9.1.2.3
BYTE bSemiByte = (fHigh ? (bByte & 0xf0) >> 4 : bByte & 0x0f);
DEBUGCHK(0x10 > bSemiByte);
return g_rgwchSemiByteToBCDMap[bSemiByte];
}
//
// Fill in RILADDRESS structure from BYTE array
// see GSM 03.40 section 9.1.2.5 and GSM 04.11 8.2.5.2
//
static BOOL ParseMsgAddress(const BYTE* const pbIn, const BOOL fBeforePDU, RILADDRESS& rraAddress, UINT& rcbParsed, BOOL fGSM3dot40)
{
FUNCTION_TRACE(ParseMsgAddress);
DEBUGCHK(NULL != pbIn);
UINT i;
BYTE bType;
BYTE bNumPlan = pbIn[1] & 0x0f;
const BYTE* pbWalk;
LPWSTR pwchWalk;
UINT cchUsed;
UINT cbAddress;
WCHAR wchBCD;
BOOL fRet = FALSE;
// Initalize returned values
rcbParsed = 0;
(void)memset(&rraAddress, 0x00, sizeof(RILADDRESS));
rraAddress.cbSize = sizeof(RILADDRESS);
// If address length is 0, just get out. This shouldn't happen, but we've seen it.
if (*pbIn==0)
{
DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : ParseMsgAddress : Address length is 0?\r\n")));
if (fGSM3dot40)
{
// GSM 03.40 section 9.2.2.1 indicates that there will always be a MINIMUM of 2 octets for the TP-OA field
// Some SMS messages we've seen in the wild have a TP-OA Address-Length of 0, Type-of-Address of International
// and 0 bytes of data. Setting rcbParsed to 2 below (rather than 1) is necessary to correctly parse these
// messages.
rcbParsed = 2;
}
else
{
// GSM 04.11 8.2.5.2 is ambiguous about whether a zero length address will include an address type and be
// two bytes, or will just have a length and be one byte. However we have seen at least one case in which
// an address did not have a type, just a length. There are no known instances yet when it has both a type
// and a length, so for now we are assuming it will always be just one byte.
rcbParsed = 1;
}
return TRUE;
}
// Determine the address type
bType = (pbIn[1] & 0x70) >> 4;
for (i = 0; i < NUM_ADDRTYPES; i++) {
if (g_rgbAddrTypes[i] == bType) {
rraAddress.dwType = i;
rraAddress.dwParams |= RIL_PARAM_A_TYPE;
break;
}
}
DEBUGCHK(0 != (rraAddress.dwParams & RIL_PARAM_A_TYPE));
if ((RIL_ADDRTYPE_UNKNOWN == rraAddress.dwType) ||
(RIL_ADDRTYPE_INTERNATIONAL == rraAddress.dwType) ||
(RIL_ADDRTYPE_NATIONAL == rraAddress.dwType)) {
// Determine the numbering plan
for (i = 0; i < NUM_NUMPLANS; i++) {
if (g_rgbNumPlans[i] == bNumPlan) {
rraAddress.dwNumPlan = i;
rraAddress.dwParams |= RIL_PARAM_A_NUMPLAN;
break;
}
}
// If the number plan is not one that is listed in 03.40,
// then it is a reserved value, which we are supposed to
// treat as 'Unknown'.
if (RIL_PARAM_A_NUMPLAN != (rraAddress.dwParams & RIL_PARAM_A_NUMPLAN))
{
rraAddress.dwNumPlan = RIL_NUMPLAN_UNKNOWN;
rraAddress.dwParams |= RIL_PARAM_A_NUMPLAN;
DEBUGMSG(ZONE_INFO, (TEXT("RILDrv : i : ParseMsgAddress : Parsed reserved numbering plan: 0x0%x\r\n"), bNumPlan));
}
}
if (rraAddress.dwType == RIL_ADDRTYPE_ALPHANUM) {
// It seems that alphanumeric address length is the number of semi-octets as specified in GSM 03.40 section 9.1.2.5
// so we need to determine the number of bytes from semi-octets (essentially nibbles).
//there are two semi-octets per byte, but we need the ceiling since a straggling remainder nibble takes up a whole byte.
// so + 1, then divide by two...
cbAddress = (*pbIn + 1) / 2;
if (!(ConvertToUnicode(ENCODING_GSMDEFAULT, (LPCSTR) (pbIn + 2), cbAddress, rraAddress.wszAddress,
MAXLENGTH_ADDRESS-1, cchUsed))) {
goto Error;
}
} else {
if (!fBeforePDU) {
// Calculate size of semi-octet-encoded address, per GSM 03.40 section 9.1.2.3
cbAddress = *pbIn / 2 + *pbIn % 2;
} else {
cbAddress = *pbIn - 1;
}
pbWalk = pbIn + 2;
pwchWalk = rraAddress.wszAddress;
for (i = 0; i < cbAddress; i++) {
wchBCD = SemiByteToBCD(*pbWalk, FALSE);
if (L' ' != wchBCD) {
*pwchWalk++ = wchBCD;
}
wchBCD = SemiByteToBCD(*pbWalk, TRUE);
if (L' ' != wchBCD) {
*pwchWalk++ = wchBCD;
}
pbWalk++;
}
// NULL-terminate the address string
*pwchWalk = L'\0';
}
rraAddress.dwParams |= RIL_PARAM_A_ADDRESS;
rcbParsed = cbAddress + 2;
fRet = TRUE;
Error:
return fRet;
}
//
// Fill in SYSTEMTIME structure from BYTE array
// see GSM 03.40 section 9.2.3.11
//
static BOOL ParseMsgTimeStamp(const BYTE* const pbIn, SYSTEMTIME& rstTime, UINT& rcbParsed)
{
FUNCTION_TRACE(ParseMsgTimeStamp);
DEBUGCHK(NULL != pbIn);
UINT nYear;
const BYTE* pbWalk = pbIn;
UINT nOffset;
BOOL fOffsetNegative;
FILETIME_ULARGEINT ftuliTime; memset(&ftuliTime,0,sizeof(ftuliTime)); // zero struct
ULARGE_INTEGER uliOffset; memset(&uliOffset,0,sizeof(uliOffset)); // zero struct
BOOL fRet = FALSE;
rcbParsed = 0;
(void)memset(&rstTime, 0x00, sizeof(SYSTEMTIME));
// Parse year
// We don't expect to see any messages with a timestamp prior to 1991.
// This should give plenty of buffer for abnormally early timestamps while still leaving
// working functionality until 2090.
nYear = (*pbWalk & 0x0f) * 10 + ((*pbWalk & 0xf0) >> 4);
if (nYear > 90) {
rstTime.wYear = 1900 + nYear;
} else {
rstTime.wYear = 2000 + nYear;
}
pbWalk++;
// Parse month
rstTime.wMonth = (*pbWalk & 0x0f) * 10 + ((*pbWalk & 0xf0) >> 4);
pbWalk++;
// Parse day
rstTime.wDay = (*pbWalk & 0x0f) * 10 + ((*pbWalk & 0xf0) >> 4);
pbWalk++;
// Parse hours
rstTime.wHour = (*pbWalk & 0x0f) * 10 + ((*pbWalk & 0xf0) >> 4);
pbWalk++;
// Parse minutes
rstTime.wMinute = (*pbWalk & 0x0f) * 10 + ((*pbWalk & 0xf0) >> 4);
pbWalk++;
// Parse seconds
rstTime.wSecond = (*pbWalk & 0x0f) * 10 + ((*pbWalk & 0xf0) >> 4);
pbWalk++;
#if defined(TRUST_TIMEZONE_FIELD)
/*
The following code to adjust rstTime according to the time-zone information
from the input is unused because most shipping GSM phones seem to ignore
that field. Specifically, all SMSCs seem to fill in the TP-SCTS field based
on their *local* time and then set the offset from GMT to *0*. Unless the
SMSC is actually based in the GMT+0 time zone, this behavior is incorrect.
Because our devices know which time zone they're in, if they use the time
zone information provided, then they will calculate a time that is adjusted
for the device's offset from the SMSC's (incorrectly reported) offset and
will obtain a resulting time that is incorrect. This leads to SMS messages
appearing in Inbox with incorrect time stamps and ends up confusing users.
*/
// Parse timezone offset (LocalTime - GMT in in quarters of an hour)
// See GSM 03.40 ver5.8.1 ch9.2.3.11
nOffset = ((*pbWalk & 0x07) * 10 + ((*pbWalk & 0xf0) >> 4)) * 15;
fOffsetNegative = (*pbWalk & 0x08);
#else // defined(TRUST_TIMEZONE_FIELD)
/*
Until SMSCs start getting properly configured, the best option seems to be
to effectively ignore the time zone information (as other GSM phones do).
However, in order for us to ignore the time zone field, we actually need
to fill in the SYSTEMTIME structure with the UTC representation of the local
time we just parsed. That means offsetting it by the current time zone bias
of the device.
*/
{
TIME_ZONE_INFORMATION tzi;
LONG lBias = 0;
const DWORD dwGetTimeZoneInformationResult = GetTimeZoneInformation(&tzi);
if(TIME_ZONE_ID_STANDARD == dwGetTimeZoneInformationResult) {
lBias = tzi.Bias+tzi.StandardBias;
} else if(TIME_ZONE_ID_DAYLIGHT == dwGetTimeZoneInformationResult) {
lBias = tzi.Bias+tzi.DaylightBias;
} else if(TIME_ZONE_ID_UNKNOWN == dwGetTimeZoneInformationResult) {
lBias = tzi.Bias;
} else { // Would like to check for error with TIME_ZONE_ID_INVALID, but it's not defined by our OS headers
DEBUGCHK(!"Should never reach here");
}
fOffsetNegative = (lBias < 0);
nOffset = ((fOffsetNegative) ? (-lBias) : (lBias));
// Now that we've used fOffsetNegative to set nOffset=abs(lBias), we need
// to flip fOffsetNegative because we want to calculate the UTC value that
// represents the same temporal time as the local clock time we just
// extracted.
fOffsetNegative = !fOffsetNegative;
}
#endif // defined(TRUST_TIMEZONE_FIELD)
pbWalk++;
// Convert our local time to filetime
if (!SystemTimeToFileTime(&rstTime, &ftuliTime.ft)) {
goto Error;
}
// Convert timezone offset to filetime
uliOffset.QuadPart = nOffset * (__int64)600000000;
// Apply the timezone offset
// (NOTE: the offset is LocalTime - GMT, so we need to *add* it to the LocalTime if the offset is negative,
// and *subtract* it if the offset is positive)
if (fOffsetNegative) {
ftuliTime.uli.QuadPart += uliOffset.QuadPart;
} else {
ftuliTime.uli.QuadPart -= uliOffset.QuadPart;
}
// Convert the updated filetime back to the SYSTEMTIME structure
if (!FileTimeToSystemTime(&ftuliTime.ft, &rstTime)) {
goto Error;
}
rcbParsed = pbWalk - pbIn;
fRet = TRUE;
Error:
return fRet;
}
//
// Set the ProtocolID of an Incoming SMS Message
// see GSM 03.40 section 9.2.3.9
//
static BOOL ParseMsgProtocolID(const BYTE* const pbIn, DWORD& rdwProtocolID, UINT& rcbParsed)
{
FUNCTION_TRACE(ParseMsgProtocolID);
DEBUGCHK(NULL != pbIn);
UINT i;
BOOL fRet = FALSE;
rcbParsed = 0;
if ((0 == (*pbIn & 0xe0)) || (0x80 == (*pbIn & 0xc0))) { // Reserved or unsupported values should be treated as 0x00
rdwProtocolID = RIL_MSGPROTOCOL_SMETOSME;
fRet = TRUE;
} else {
for (i = 0; i < NUM_PROTOCOLIDS; i++) {
if (g_rgdwProtocolIDs[i] == *pbIn) {
rdwProtocolID = i;
fRet = TRUE;
break;
}
}
if (!fRet)
{
rdwProtocolID = RIL_MSGPROTOCOL_UNKNOWN;
fRet = TRUE;
}
}
if (fRet) {
rcbParsed = 1;
}
return fRet;
}
//
// Set Data Coding Scheme of Incoming SMS Message
// see GSM 03.38
//
static BOOL ParseMsgDCS(const BYTE* const pbIn, RILMSGDCS& rrmdDCS, UINT& rcbParsed)
{
FUNCTION_TRACE(ParseMsgDCS);
DEBUGCHK(NULL != pbIn);
BYTE bDCS = *pbIn;
BOOL fRet = FALSE;
rcbParsed = 0;
(void)memset(&rrmdDCS, 0x00, sizeof(RILMSGDCS));
rrmdDCS.cbSize = sizeof(RILMSGDCS);
switch (bDCS & 0xf0)
{
case 0x00:
case 0x10:
case 0x20:
case 0x30:
rrmdDCS.dwType = RIL_DCSTYPE_GENERAL;
rrmdDCS.dwParams |= RIL_PARAM_MDCS_TYPE;
if (bDCS & 0x20) {
rrmdDCS.dwFlags |= RIL_DCSFLAG_COMPRESSED;
rrmdDCS.dwParams |= RIL_PARAM_MDCS_FLAGS;
}
if (bDCS & 0x10) {
switch (bDCS & 0x03)
{
case 0x00:
rrmdDCS.dwMsgClass = RIL_DCSMSGCLASS_0;
break;
case 0x01:
rrmdDCS.dwMsgClass = RIL_DCSMSGCLASS_1;
break;
case 0x02:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -