blob: 9ac3f91552568125eec52e0728509822a1e1566d [file] [log] [blame]
/******************************************************************************
*(C) Copyright 2021 ASR MicroElectronics Ltd.
* All Rights Reserved
******************************************************************************/
/*--------------------------------------------------------------------------------------------------------------------
* -------------------------------------------------------------------------------------------------------------------
*
* Filename: ecall_sms.c
*
* Description: ecall_sms
*
* History:
* July 22, 2022 - ZhiXu Creation of file
*
* Notes:
*
******************************************************************************/
#ifndef NO_AUDIO
#include <stdlib.h>
#include <string.h>
#include "ecall_daemon.h"
#include "ecall_sms.h"
#define ECALLSmsFree(p) \
{ \
if (p) { \
free(p); \
p = NULL; \
} \
}
extern char testNumber[];
extern char reconfigureNumber[];
extern char smsMsdNumber[];
unsigned short int sendCount = 0;
/*
Name:CRC-16 CCITT
Poly:0x1021 x^16+x^12+x^5+1
Init:0xFFFF
Revert: false
XorOut:0x0000
Check:0x29B1(<<123456789>>)
*/
const unsigned short CRC16Table[256]={
0x0000,0x1021,0x2042,0x3063,0x4084,0x50A5,0x60C6,0x70E7,
0x8108,0x9129,0xA14A,0xB16B,0xC18C,0xD1AD,0xE1CE,0xF1EF,
0x1231,0x0210,0x3273,0x2252,0x52B5,0x4294,0x72F7,0x62D6,
0x9339,0x8318,0xB37B,0xA35A,0xD3BD,0xC39C,0xF3FF,0xE3DE,
0x2462,0x3443,0x0420,0x1401,0x64E6,0x74C7,0x44A4,0x5485,
0xA56A,0xB54B,0x8528,0x9509,0xE5EE,0xF5CF,0xC5AC,0xD58D,
0x3653,0x2672,0x1611,0x0630,0x76D7,0x66F6,0x5695,0x46B4,
0xB75B,0xA77A,0x9719,0x8738,0xF7DF,0xE7FE,0xD79D,0xC7BC,
0x48C4,0x58E5,0x6886,0x78A7,0x0840,0x1861,0x2802,0x3823,
0xC9CC,0xD9ED,0xE98E,0xF9AF,0x8948,0x9969,0xA90A,0xB92B,
0x5AF5,0x4AD4,0x7AB7,0x6A96,0x1A71,0x0A50,0x3A33,0x2A12,
0xDBFD,0xCBDC,0xFBBF,0xEB9E,0x9B79,0x8B58,0xBB3B,0xAB1A,
0x6CA6,0x7C87,0x4CE4,0x5CC5,0x2C22,0x3C03,0x0C60,0x1C41,
0xEDAE,0xFD8F,0xCDEc,0xDDCD,0xAD2A,0xBD0B,0x8D68,0x9D49,
0x7E97,0x6EB6,0x5ED5,0x4EF4,0x3E13,0x2E32,0x1E51,0x0E70,
0xFF9F,0xEFBE,0xDFDD,0xCFFC,0xBF1B,0xAF3A,0x9F59,0x8F78,
0x9188,0x81A9,0xB1CA,0xA1EB,0xD10C,0xC12D,0xF14E,0xE16F,
0x1080,0x00A1,0x30C2,0x20E3,0x5004,0x4025,0x7046,0x6067,
0x83B9,0x9398,0xA3FB,0xB3DA,0xC33D,0xD31C,0xE37F,0xF35E,
0x02B1,0x1290,0x22F3,0x32D2,0x4235,0x5214,0x6277,0x7256,
0xB5EA,0xA5CB,0x95A8,0x8589,0xF56E,0xE54F,0xD52C,0xC50D,
0x34E2,0x24C3,0x14A0,0x0481,0x7466,0x6447,0x5424,0x4405,
0xA7DB,0xB7FA,0x8799,0x97B8,0xE75F,0xF77E,0xC71D,0xD73C,
0x26D3,0x36F2,0x0691,0x16B0,0x6657,0x7676,0x4615,0x5634,
0xD94C,0xC96D,0xF90E,0xE92F,0x99C8,0x89E9,0xB98A,0xA9AB,
0x5844,0x4865,0x7806,0x6827,0x18C0,0x08E1,0x3882,0x28A3,
0xCB7D,0xDB5C,0xEB3F,0xFB1E,0x8BF9,0x9BD8,0xABBB,0xBB9A,
0x4A75,0x5A54,0x6A37,0x7A16,0x0AF1,0x1AD0,0x2AB3,0x3A92,
0xFD2E,0xED0F,0xDD6C,0xCD4D,0xBDAA,0xAD8B,0x9DE8,0x8DC9,
0x7C26,0x6C07,0x5C64,0x4C45,0x3CA2,0x2C83,0x1CE0,0x0CC1,
0xEF1F,0xFF3E,0xCF5D,0xDF7C,0xAF9B,0xBFBA,0x8FD9,0x9FF8,
0x6E17,0x7E36,0x4E55,0x5E74,0x2E93,0x3EB2,0x0ED1,0x1EF0
};
unsigned short ecall_sms_CRC16(unsigned char *payload, unsigned short len)
{
unsigned short crc = 0xFFFF;
while(len--)
crc = (crc << 8) ^ CRC16Table[(crc >> 8) ^ *payload++];
return crc;
}
/*
Name :CRC-8
Poly:0x31 x^8+x^5+x^4+1
Init :0xFF
Revert: false
XorOut: 0x00
Check:0xF7(<<123456789>>)
*/
const unsigned char CRC8Table[256]={
0x00,0x31,0x62,0x53,0xC4,0xF5,0xA6,0x97,
0xB9,0x88,0xDB,0xEA,0x7D,0x4C,0x1F,0x2E,
0x43,0x72,0x21,0x10,0x87,0xB6,0xE5,0xD4,
0xFA,0xCB,0x98,0xA9,0x3E,0x0F,0x5C,0x6D,
0x86,0xB7,0xE4,0xD5,0x42,0x73,0x20,0x11,
0x3F,0x0E,0x5D,0x6C,0xFB,0xCA,0x99,0xA8,
0xC5,0xF4,0xA7,0x96,0x01,0x30,0x63,0x52,
0x7C,0x4D,0x1E,0x2F,0xB8,0x89,0xDA,0xEB,
0x3D,0x0C,0x5F,0x6E,0xF9,0xC8,0x9B,0xAA,
0x84,0xB5,0xE6,0xD7,0x40,0x71,0x22,0x13,
0x7E,0x4F,0x1C,0x2D,0xBA,0x8B,0xD8,0xE9,
0xC7,0xF6,0xA5,0x94,0x03,0x32,0x61,0x50,
0xBB,0x8A,0xD9,0xE8,0x7F,0x4E,0x1D,0x2C,
0x02,0x33,0x60,0x51,0xC6,0xF7,0xA4,0x95,
0xF8,0xC9,0x9A,0xAB,0x3C,0x0D,0x5E,0x6F,
0x41,0x70,0x23,0x12,0x85,0xB4,0xE7,0xD6,
0x7A,0x4B,0x18,0x29,0xBE,0x8F,0xDC,0xED,
0xC3,0xF2,0xA1,0x90,0x07,0x36,0x65,0x54,
0x39,0x08,0x5B,0x6A,0xFD,0xCC,0x9F,0xAE,
0x80,0xB1,0xE2,0xD3,0x44,0x75,0x26,0x17,
0xFC,0xCD,0x9E,0xAF,0x38,0x09,0x5A,0x6B,
0x45,0x74,0x27,0x16,0x81,0xB0,0xE3,0xD2,
0xBF,0x8E,0xDD,0xEC,0x7B,0x4A,0x19,0x28,
0x06,0x37,0x64,0x55,0xC2,0xF3,0xA0,0x91,
0x47,0x76,0x25,0x14,0x83,0xB2,0xE1,0xD0,
0xFE,0xCF,0x9C,0xAD,0x3A,0x0B,0x58,0x69,
0x04,0x35,0x66,0x57,0xC0,0xF1,0xA2,0x93,
0xBD,0x8C,0xDF,0xEE,0x79,0x48,0x1B,0x2A,
0xC1,0xF0,0xA3,0x92,0x05,0x34,0x67,0x56,
0x78,0x49,0x1A,0x2B,0xBC,0x8D,0xDE,0xEF,
0x82,0xB3,0xE0,0xD1,0x46,0x77,0x24,0x15,
0x3B,0x0A,0x59,0x68,0xFF,0xCE,0x9D,0xAC
};
unsigned char ecall_sms_CRC8(unsigned char *payload, unsigned char len)
{
unsigned char crc = 0xFF;
while(len--)
crc = CRC8Table[crc ^ *payload++];
return crc;
}
static char* itoa(int value, char* string, int radix)
{
int rt = 0;
if(string == NULL)
return NULL;
if(radix<=0||radix>30)
return NULL;
if(radix==10)
rt=snprintf(string,radix,"%d",value);
else if(radix==16)
rt = snprintf(string,radix,"%02X",value);
if(rt>radix)
return NULL;
string[rt]=0;
return string;
}
void ecall_sms_parse_cmd_cd(char *cmdCd)
{
int ret = 0;
int ccd = 0;
int index = 0;
char *tmp = NULL;
if (cmdCd)
{
tmp = cmdCd;
//ECALL_Log(ecall_sms_parse_cmd_cd0, "%s: recv cmdCd=%s.", __FUNCTION__, cmdCd);
}
else
{
ECALL_Log("%s: recv cmdCd is NULL.", __FUNCTION__);
return;
}
index += 3; //skip to ccd
ccd = tmp[index] + tmp[index + 1] * 256;
ECALL_Log("%s: recv ccd=%lx.\n", __FUNCTION__, ccd);
if (ccd == 0x0113) //EGTS_ECALL_MSD_REQ
{
//trigger ivs send msd via sms
//ret = ecall_sms_send_cfg_msd();
//ECALL_Log("%s: ecall_sms_send_cfg_msd ret=%d.", __FUNCTION__, ret);
ret = ecall_daemon_ubus_send_ecallCmd(8, 20);
ECALL_Log("%s: ecall daemon set ecall data ret[%d].", __FUNCTION__, ret);
}
else if (ccd == 0x0116) //EGTS_ECALL_DEREGISTRATION
{
ecall_sms_deReg();
}
else if (ccd == 0x0112) //EGTS_ECALL_REQ
{
int ecallReqFlag = 0;
index += 2; //skip to data
ecallReqFlag = tmp[index];
ECALL_Log("%s: ecallReqFlag=%d.", __FUNCTION__, ecallReqFlag);
if (ecallReqFlag == 0) //manual call
{
ret = ecall_daemon_ubus_send_ecallCmd(8, 2);
ECALL_Log("%s: ecall daemon set ecall type=2 ret[%d].", __FUNCTION__, ret);
//ecall_start_manual_ecall();
}
else if (ecallReqFlag == 1) //aotomatic call
{
ret = ecall_daemon_ubus_send_ecallCmd(8, 3);
ECALL_Log("%s: ecall daemon set ecall type=3 ret[%d].", __FUNCTION__, ret);
//ecall_start_auto_ecall();
}
else
{
ECALL_Log("%s: ecallReqFlag is invalid.", __FUNCTION__);
return;
}
}
else
{
ECALL_Log("%s: recv ccd is invalid\n", __FUNCTION__);
return;
}
return;
}
void ecall_sms_parse_cmd_srd(char *cmdSrd)
{
int ct = 0;
int index = 0;
char *tmp = NULL;
char cid[5] = {0};
int feFlag = 0;
int offset = 0;
if (cmdSrd)
{
tmp = cmdSrd;
ECALL_Log("%s: recv cmdSrd=%s.", __FUNCTION__, cmdSrd);
}
else
{
ECALL_Log("%s: recv cmdSrd is NULL.", __FUNCTION__);
return;
}
ct = tmp[index];
ECALL_Log("%s: recv ct=%d.\n", __FUNCTION__, ct);
if (((ct >> 4) & 0x0F) == 5)
{
index += 1; //skip to cid
memcpy(cid, tmp + 1, 4);
index += 8; //skip to sid
feFlag = (tmp[index]);
ECALL_Log("%s: recv feFlag=%d.\n", __FUNCTION__, feFlag);
index += 1;
if ((feFlag & 0x0f) == 1)
{
offset += 1;
}
if (((feFlag >> 4) & 0x0f) == 1)
{
int acl = 0;
index += offset;
acl = (tmp[index]);
offset += acl + 1;
ECALL_Log("%s: recv acl=%d.\n", __FUNCTION__, acl);
}
index += offset; //skip tp CD
if (tmp)
{
ECALL_Log("%s: recv cmdCd=%s.", __FUNCTION__, tmp);
ecall_sms_parse_cmd_cd(tmp + index);
}
}
return;
}
void ecall_sms_parse_SRD(char *srd, int sst)
{
int srt = 0;
int index = 0;
char *tmp = NULL;
if (srd)
{
tmp = srd;
ECALL_Log("%s: recv srd=%s.", __FUNCTION__, srd);
}
else
{
ECALL_Log("%s: recv srd is NULL.", __FUNCTION__);
return;
}
srt = (tmp[index]);
ECALL_Log("%s: recv srt=%lx.\n", __FUNCTION__, srt);
if (sst == 4)
{
if (srt == 0x33)
{
sleep(2); //sleep 2s for russia eCall Lab psap sync status, e.g ERA 33467 case 6.1.2
ecall_sms_send_cmd_confirmed(); //received a cmmand data, first send a confirmed sms to psap
index += 3; //skip to srd
ecall_sms_parse_cmd_srd(tmp + index);
}
}
return;
}
unsigned short ecall_sms_ntohs(unsigned short num)
{
return ( ( (num & 0x00FF) << 8) | ( (num & 0xFF00) >> 8) );
}
void ecall_sms_parse_RD(char *rd)
{
int rl = 0;
int rfl = 0;
int sst = 0;
int rst = 0;
int cnt = 0;
int index = 0;
char *tmp = NULL;
char *srd = NULL;
if (rd)
{
tmp = rd;
ECALL_Log("%s: recv rd=%s.", __FUNCTION__, rd);
}
else
{
ECALL_Log("%s: recv rd is NULL.", __FUNCTION__);
return;
}
rl = tmp[0] + tmp[1] * 256;//ecall_sms_ntohs(atoi(rlHex));
ECALL_Log("%s: rl=%d.\n", __FUNCTION__, rl);
index += 4; //skip to rfl
rfl = (tmp[index]);
ECALL_Log("%s: rfl=%d.\n", __FUNCTION__, rfl);
if (rfl > 0)
{
if ((rfl & 0x1) == 0x1) cnt ++;
if ((rfl & 0x2) == 0x2) cnt ++;
if ((rfl & 0x4) == 0x4) cnt ++;
ECALL_Log("%s: cnt=%d.", __FUNCTION__, cnt);
}
index += 1 + 4 * cnt; //skip to sst
sst = (tmp[index]);
index += 1; //skip to rst
rst = (tmp[index]);
ECALL_Log("%s: sst=%d, rst=%d.", __FUNCTION__, sst, rst);
index += 1; //skip to srd
if (tmp)
{
srd = tmp + index;
ecall_sms_parse_SRD(srd, sst);
}
return;
}
void ecall_sms_hex2str(int len, char *psrc, char *pdest)
{
char tmp[32] = {0};
int i = 0;
tmp[0] = '\0';
for (i = 0; i < len; i++) {
sprintf(tmp, "%02X", psrc[i] & 0xFF);
strcat(pdest, tmp);
ECALL_Log("%s: psrc[%d]: 0x%X, tmp %s, pdest %s\n", __FUNCTION__, i, psrc[i], tmp, pdest);
}
//strcat(pdest, '\0');
return;
}
void ecall_sms_parse_SFRD(char *sfrd)
{
int prv = 0;
int pt = 0;
int hl = 0;
int index = 0;
char *tmp = NULL;
char *rd = NULL;
if (sfrd)
{
tmp = sfrd;
rd = sfrd;
ECALL_Log("%s: recv sfrd=%s.", __FUNCTION__, sfrd);
}
else
{
return;
}
prv = (tmp[index]);
ECALL_Log("%s: sfrd prv=%d.", __FUNCTION__, prv);
if ((prv & 0x01) != 1) return;
index += 3;//tmp += 3 * 2; //skip to HL
hl = (tmp[index]);
index += 6;//tmp += 6 * 2; //skip to pt
pt = ecall_sms_char2int(tmp[index]);
ECALL_Log("%s: sfrd hl=%d, pt=%d.", __FUNCTION__, hl, pt);
if (pt != 1) return;
index += hl;
rd += hl; //skip to rd
if (rd)
{
ECALL_Log("%s: rd=%s.", __FUNCTION__, rd);
ecall_sms_parse_RD(rd);
}
return;
}
void ecall_sms_parse_src_number(char *pdu, int srcNumLen, char *srcNumOut)
{
int idx = 0;
int addrLen = 0;
char *data = NULL;
char *tmp = NULL;
if (pdu) data = pdu;
addrLen = srcNumLen;
tmp = srcNumOut;
ECALL_Log("%s: recv pdu=%s, srcNumLen=%d.", __FUNCTION__, pdu, srcNumLen);
for ( idx = 0; idx < addrLen; idx++ )
{
if (data[idx] == 'f' || data[idx] == 'F')
continue;
if ( (idx & 0x01) == 0 )
{
tmp[ idx + 1 ] = data[ idx ];
}
else
{
tmp[ idx - 1 ] = data[ idx ];
}
}
ECALL_Log("%s: convert src number:%s.", __FUNCTION__, srcNumOut);
return;
}
int ecall_sms_char2int(char c)
{
int ret = 0;
char arr[3] = {0};
if (c != '\0')
{
snprintf(arr, sizeof(arr), "%02x", c);
ret = atoi(arr);
}
return ret;
}
void ecall_sms_parse_pdu(char *smsPdu)
{
int index = 0;
int scaLen = 0;
int OAlen = 0;
int udl = 0;
char srcNum[32] = {0};
char *ud = NULL;
char *tmp = NULL;
char srcNumOut[16] = {0};
if (smsPdu)
tmp = smsPdu;
else
return;
scaLen = tmp[index];
ECALL_Log("%s: SCA Len=%d.", __FUNCTION__, scaLen);
index += scaLen + 1; //tmp += (scaLen + 1) * 2; //skip SCA
index += 1;//tmp += 2; //skip pduType
OAlen = tmp[index];
ECALL_Log("%s: OA Len=%d.", __FUNCTION__, OAlen);
index += 2;//tmp += 4; //skip OAlen and type
if (OAlen < 32)
{
if (OAlen % 2 != 0)
OAlen += 2;
ecall_sms_hex2str(OAlen/2, tmp+index, srcNum);
ECALL_Log("%s: srcNum=%s.", __FUNCTION__, srcNum);
ecall_sms_parse_src_number(srcNum, OAlen, srcNumOut);
ECALL_Log("%s: srcNumOut=%s.", __FUNCTION__, srcNumOut);
index += OAlen/2;
}
else
{
return;
}
index += 9;//tmp += 18; //skip PID, DCS and SCTS
udl = tmp[index]; //atoi(shortData);
ECALL_Log("%s: udl=%d.", __FUNCTION__, udl);
if (udl < 11) return; //11 is sfrd header must length.
index += 1; //tmp += 2; //skip udl
if (tmp)
{
ud = tmp + index;
if (ud)
{
ECALL_Log("%s: ud=%s.", __FUNCTION__, ud);
ecall_sms_parse_SFRD(ud);
}
}
return;
}
char *ecall_sms_pack_numberPdu(char *smsNumber)
{
char *strOut = NULL;
char *data = NULL;
char numPdu[64] = {0};
int numLen = 0;
int index = 0;
int mallocSize = 0;
int i = 0;
if (smsNumber) data = smsNumber;
numLen = strlen(data);
ECALL_Log("%s: index=%d, numLen=%d, data=%s", __FUNCTION__, index, numLen, data);
#if 0
if (data[index] == '+')
{
strncpy(numPdu, "91", strlen("91"));
index += 2;
numLen--;
data++;
}
#endif
//ECALL_Log(ecall_sms_pack_numberPdu1, "%s: index=%d, numLen=%d, data=%s", __FUNCTION__, index, numLen, data);
for (i = 0; i < numLen; i++)
{
if ((i & 0x01) == 0)
{
numPdu[i + 1] = data[ i ];
}
else
{
numPdu[i - 1] = data[ i ];
}
//ECALL_Log(ecall_sms_pack_numberPdu2, "%s: index=%d, i=%d, numPdu=%s", __FUNCTION__, index, i, numPdu);
}
//ECALL_Log(ecall_sms_pack_numberPdu3, "%s: index=%d, i=%d, numPdu=%s", __FUNCTION__, index, i, numPdu);
if ((numLen & 0x01) == 1)
{
numPdu[i - 1] = 'F';
}
ECALL_Log("%s: index=%d, numLen=%d, numPdu=%s", __FUNCTION__, index, numLen, numPdu);
mallocSize = strlen(numPdu) + 1;
if (mallocSize > 1)
{
strOut = malloc(mallocSize);
if (strOut)
{
memset(strOut, 0, mallocSize);
strncpy(strOut, numPdu, strlen(numPdu));
return strOut;
}
else
{
return NULL;
}
}
else
{
return NULL;
}
}
char *ecall_sms_itoa_uint8(unsigned char data)
{
char itoaStr[5] = {0};
char *strOut = NULL;
memset(itoaStr, 0x00, 5);
itoa(data, itoaStr, 16);
if (strlen(itoaStr) == 0)
{
return NULL;
}
else
{
strOut = malloc(strlen(itoaStr)+1);
if (strOut)
{
memset(strOut, 0, strlen(itoaStr)+1);
strncpy(strOut, itoaStr, strlen(itoaStr));
}
}
ECALL_Log("%s: strOut=%s", __FUNCTION__, strOut);
return strOut;
}
char *ecall_sms_itoa_uint16(unsigned short data)
{
char itoaStr[5] = {0};
char tmp[16] = {0};
char *strOut = NULL;
int index = 0, i = 0;
memset(itoaStr, 0x00, 5);
memset(tmp, 0x00, 16);
itoa(data & 0x00FF, itoaStr, 16);
if (strlen(itoaStr) > 0)
{
for (i = 0; i < (int)strlen(itoaStr); i++)
{
tmp[index++] = itoaStr[i];
}
}
memset(itoaStr, 0x00, 5);
itoa((data>>8) & 0x00FF, itoaStr, 16);
if (strlen(itoaStr) > 0)
{
for (i = 0; i < (int)strlen(itoaStr); i++)
{
tmp[index++] = itoaStr[i];
}
}
if (strlen(tmp) == 0)
{
return NULL;
}
else
{
strOut = malloc(strlen(tmp)+1);
if (strOut)
{
memset(strOut, 0, strlen(tmp)+1);
//memcpy(strOut, "0000", strlen("0000"));
strncpy(strOut, tmp, strlen(tmp));
}
}
ECALL_Log("%s: strOut=%s", __FUNCTION__, strOut);
return strOut;
}
char * ecall_sms_pack_pduHeader(char *smsNumber, unsigned char udl)
{
char *strOut = NULL;
char *numPdu = NULL;
char *tmp = NULL;
char pduHeader[128] = {0};
int numLen = 0;
int len = 0;
int smsNumType = 0;
numLen = strlen(smsNumber);
if (smsNumber[0] == '+')
{
smsNumber++;
numLen--;
smsNumType = 1;
}
numPdu = ecall_sms_pack_numberPdu(smsNumber);
if (numPdu)
{
len += snprintf(pduHeader+len, sizeof(pduHeader)-len, "1100");
tmp = ecall_sms_itoa_uint8((unsigned char)numLen);
len += snprintf(pduHeader+len, sizeof(pduHeader)-len, "%s", tmp);
ECALLSmsFree(tmp);
if (smsNumType == 1)
{
len += snprintf(pduHeader+len, sizeof(pduHeader)-len, "91");
}
else
{
len += snprintf(pduHeader+len, sizeof(pduHeader)-len, "81");
}
len += snprintf(pduHeader+len, sizeof(pduHeader)-len, "%s", numPdu);
ECALLSmsFree(numPdu);
len += snprintf(pduHeader+len, sizeof(pduHeader)-len, "0004B0");
tmp = ecall_sms_itoa_uint8(udl);
len += snprintf(pduHeader+len, sizeof(pduHeader)-len, "%s", tmp);
ECALLSmsFree(tmp);
if (strlen(pduHeader) > 0)
{
strOut = malloc(strlen(pduHeader) + 1);
if (strOut)
{
memset(strOut, 0, strlen(pduHeader) + 1);
strncpy(strOut, pduHeader, strlen(pduHeader));
}
}
ECALL_Log("%s: strOut=%s", __FUNCTION__, strOut);
return strOut;
}
else
{
return NULL;
}
}
unsigned short ecall_sms_get_sfrcs(unsigned char *msdSMS)
{
unsigned char *tmp = NULL;
unsigned short SFRCS = 0;
unsigned char len = 0;
unsigned char msdSMSHex[200] = {0};
if (msdSMS)
{
tmp = msdSMS;
len = strlen((const char *)msdSMS);
ECALL_Log("%s: len=%d, msdSMS=%s", __FUNCTION__, len, msdSMS);
}
else
{
return 0;
}
ecall_daemon_str2hex(len, (char *)tmp, (char *)msdSMSHex, sizeof(msdSMSHex));
#if 0
int i;
for (i = 0; i < len/2 + 1; i++)
{
ECALL_Log(ecall_sms_get_sfrcs0, "%s: msdSMSHex[%d]=%lx", __FUNCTION__, i, msdSMSHex[i]);
}
#endif
SFRCS = ecall_sms_CRC16(msdSMSHex + 11, len/2-11);
ECALL_Log("%s: SFRCS=%lx", __FUNCTION__, SFRCS);
return SFRCS;
}
unsigned char ecall_sms_get_hcs(unsigned char *msdSMS)
{
unsigned char *tmp = NULL;
unsigned char HCS = 0;
unsigned char len = 0;
unsigned char msdSMSHex[64] = {0};
if (msdSMS)
{
tmp = msdSMS;
len = strlen((const char *)msdSMS);
ECALL_Log("%s: len=%d, msdSMS=%s", __FUNCTION__, len, msdSMS);
}
else
{
return 0;
}
ecall_daemon_str2hex(len, (char *)tmp, (char *)msdSMSHex, sizeof(msdSMSHex));
#if 0
int i;
for (i = 0; i < len/2 + 1; i++)
{
ECALL_Log(ecall_sms_get_hcs0, "%s: msdSMSHex[%d]=%lx", __FUNCTION__, i, msdSMSHex[i]);
}
#endif
HCS = ecall_sms_CRC8(msdSMSHex, len/2);
ECALL_Log("%s: HCS=%lx", __FUNCTION__, HCS);
return HCS;
}
unsigned char * ecall_sms_msdSMS(char *msd, int msdLen)
{
unsigned short int FDL = 0;
unsigned short int PID = 0;
unsigned char HCS = 0;
unsigned short int RL = 0;
unsigned short int RN = 0;
unsigned char RFL = 0;
unsigned char SRT = 0;
unsigned short int SRL = 0;
unsigned short int SFRCS = 0;
int msdSMSLen = 0;
int len =0, pduHeadLen = 0;
char *p = NULL;
char *pduHeader = NULL;
unsigned char tmp[256] = {0};
unsigned char *smsDataCpy = NULL;
unsigned char *msdSMS = NULL;
smsDataCpy = tmp;
sendCount++;
ECALL_Log("%s: sendCount=%d, msdLen=%d.", __FUNCTION__, sendCount, msdLen);
/*pack Transport Layer Protocol Header*/
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "0100000B00");
FDL = 7 + 3 + 1 + msdLen/2;
p = ecall_sms_itoa_uint16(FDL);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
PID = sendCount;
p = ecall_sms_itoa_uint16(PID);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "01");
HCS = ecall_sms_get_hcs(tmp);
//tmp[index++] = HCS;
ECALL_Log("%s: HCS=%lx.", __FUNCTION__, HCS);
p = ecall_sms_itoa_uint8(HCS);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
RL = 3 + 1 + msdLen/2;
p = ecall_sms_itoa_uint16(RL);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
RN = sendCount;
p = ecall_sms_itoa_uint16(RN);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
RFL = 0x80;
p = ecall_sms_itoa_uint8(RFL);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "0A0A");
SRT = 40;
p = ecall_sms_itoa_uint8(SRT);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
SRL = 1 + msdLen/2;
p = ecall_sms_itoa_uint16(SRL);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "01");
ECALL_Log("%s: len=%d.", __FUNCTION__, len);
if (msd)
{
//memcpy(tmp + len, msd, msdLen);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", msd);
SFRCS = ecall_sms_get_sfrcs(tmp);
ECALL_Log("%s: SFRCS=%lx.", __FUNCTION__, SFRCS);
p = ecall_sms_itoa_uint16(SFRCS);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
ECALL_Log("%s: len=%d, tmp=%s.", __FUNCTION__, len, tmp);
pduHeader = ecall_sms_pack_pduHeader(smsMsdNumber, len/2);
if (pduHeader)
{
pduHeadLen = strlen(pduHeader);
ECALL_Log("%s: pduHeadLen=%d, pduHeader=%s", __FUNCTION__, pduHeadLen, pduHeader);
}
else
{
return NULL;
}
msdSMSLen = pduHeadLen + len + 1;
ECALL_Log("%s: msdSMSLen=%d, tmp=%s.", __FUNCTION__, msdSMSLen, tmp);
//char smsStr[1024] = {0};
//ecall_sms_hex2str(msdSMSLen, (char *)tmp, smsStr);
//ECALL_Log(ecall_sms_msdSMS6, "%s: smsStr=%s.", __FUNCTION__, smsStr);
msdSMS = malloc(msdSMSLen);
if (msdSMS)
{
memset(msdSMS, 0x00, msdSMSLen);
memcpy(msdSMS, pduHeader, pduHeadLen);
strcat((char *)msdSMS, (const char *)smsDataCpy);
int i = 0;
for (i = 0; i < msdSMSLen; i++)
{
ECALL_Log("%s: msdSMS[%d]=%lx.", __FUNCTION__, i, msdSMS[i]);
}
ECALLSmsFree(pduHeader);
return msdSMS;
}
}
return NULL;
}
void ecall_sms_send_cmd_confirmed(void)
{
unsigned char HCS = 0;
unsigned short int SFRCS = 0;
int len =0, pduHeadLen = 0;
char *p = NULL;
char *pduHeader = NULL;
unsigned char tmp[256] = {0};
int SMSUdLen = 0;
unsigned char *SMSUd = NULL;
/*pack Transport Layer Protocol Header*/
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "0100000B001400010001");
HCS = ecall_sms_get_hcs(tmp);
ECALL_Log("%s: HCS=%lx.", __FUNCTION__, HCS);
p = ecall_sms_itoa_uint8(HCS);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
/*pack Services Layer protocol body*/
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "0d000100800404"); //RL RN RFL SST RST
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "330a00"); //SRT SRL
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "10010000000000000000"); //CT+CCT CID SID ACFE+CHSE
SFRCS = ecall_sms_get_sfrcs(tmp);
ECALL_Log("%s: SFRCS=%lx.", __FUNCTION__, SFRCS);
p = ecall_sms_itoa_uint16(SFRCS);
len += snprintf((char *)tmp+len, sizeof(tmp)-len, "%s", p);
ECALLSmsFree(p);
/*pack SMS pdu header data*/
ECALL_Log("%s: len=%d, tmp=%s.", __FUNCTION__, len, tmp);
pduHeader = ecall_sms_pack_pduHeader(smsMsdNumber, len/2);
if (pduHeader)
{
pduHeadLen = strlen(pduHeader);
ECALL_Log("%s: pduHeadLen=%d, pduHeader=%s", __FUNCTION__, pduHeadLen, pduHeader);
}
else
{
return;
}
SMSUdLen = pduHeadLen + len + 1;
ECALL_Log("%s: SMSUdLen=%d, tmp=%s.", __FUNCTION__, SMSUdLen, tmp);
SMSUd = malloc(SMSUdLen);
if (SMSUd)
{
memset(SMSUd, 0x00, SMSUdLen);
memcpy(SMSUd, pduHeader, pduHeadLen);
strcat((char *)SMSUd, (const char *)tmp);
int i = 0;
for (i = 0; i < SMSUdLen; i++)
{
ECALL_Log("%s: SMSUd[%d]=%lx.", __FUNCTION__, i, SMSUd[i]);
}
ecall_ubus_sms_send(smsMsdNumber, (char *)SMSUd);
}
ECALLSmsFree(pduHeader);
ECALLSmsFree(SMSUd);
return;
}
#endif