Modify flora license version.
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginConcatHandler.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://floralicense.org/license/
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "MsgDebug.h"
18 #include "MsgException.h"
19 #include "MsgCppTypes.h"
20 #include "MsgUtilFile.h"
21 #include "SmsPluginStorage.h"
22 #include "SmsPluginTransport.h"
23 #include "SmsPluginEventHandler.h"
24 #include "SmsPluginWapPushHandler.h"
25 #include "SmsPluginConcatHandler.h"
26
27 /*==================================================================================================
28                                      IMPLEMENTATION OF SmsPluginConcatHandler - Member Functions
29 ==================================================================================================*/
30 SmsPluginConcatHandler* SmsPluginConcatHandler::pInstance = NULL;
31
32
33 SmsPluginConcatHandler::SmsPluginConcatHandler()
34 {
35         concatList.clear();
36 }
37
38
39 SmsPluginConcatHandler::~SmsPluginConcatHandler()
40 {
41         concatList.clear();
42 }
43
44
45 SmsPluginConcatHandler* SmsPluginConcatHandler::instance()
46 {
47         if (!pInstance)
48                 pInstance = new SmsPluginConcatHandler();
49
50         return pInstance;
51 }
52
53
54 bool SmsPluginConcatHandler::IsConcatMsg(SMS_USERDATA_S *pUserData)
55 {
56         MSG_BEGIN();
57
58         MSG_DEBUG("headerCnt [%d]", pUserData->headerCnt);
59
60         for (int i = 0; i < pUserData->headerCnt; i++) {
61                 /**  Handler Concatenated Message */
62                 if (pUserData->header[i].udhType == SMS_UDH_CONCAT_8BIT) {
63                         return true;
64                 } else if (pUserData->header[i].udhType == SMS_UDH_CONCAT_16BIT) {
65                         return true;
66                 }
67         }
68
69         MSG_END();
70
71         return false;
72 }
73
74
75 void SmsPluginConcatHandler::handleConcatMsg(SMS_TPDU_S *pTpdu)
76 {
77         MSG_BEGIN();
78
79         msg_error_t err = MSG_SUCCESS;
80         bool noneConcatTypeHeader = true;
81
82         if (pTpdu->tpduType != SMS_TPDU_DELIVER) {
83                 MSG_DEBUG("The TPDU type is not deliver [%d]", pTpdu->tpduType);
84                 return;
85         }
86
87         SMS_CONCAT_MSG_S msg = {0};
88
89         for (int i = 0; i < pTpdu->data.deliver.userData.headerCnt; i++) {
90                 if (pTpdu->data.deliver.userData.header[i].udhType == SMS_UDH_CONCAT_8BIT) {
91                         msg.msgRef = (unsigned short)pTpdu->data.deliver.userData.header[i].udh.concat8bit.msgRef;
92                         msg.totalSeg = pTpdu->data.deliver.userData.header[i].udh.concat8bit.totalSeg;
93                         msg.seqNum = pTpdu->data.deliver.userData.header[i].udh.concat8bit.seqNum;
94
95                         memcpy(&(msg.timeStamp.time.absolute), &(pTpdu->data.deliver.timeStamp.time.absolute), sizeof(SMS_TIME_ABS_S));
96                         memcpy(&(msg.originAddress), &(pTpdu->data.deliver.originAddress), sizeof(SMS_ADDRESS_S));
97                         memcpy(&(msg.dcs), &(pTpdu->data.deliver.dcs), sizeof(SMS_DCS_S));
98
99 #if 0
100                         if (msg.totalSeg > MAX_SEGMENT_NUM) {
101                                 MSG_DEBUG("Total Segment Count is over Maximum [%d]", msg.totalSeg);
102                                 return;
103                         }
104 #endif
105                         /**  check noneConcatTypeHeader */
106                         noneConcatTypeHeader = false;
107
108                         break;
109                 } else if (pTpdu->data.deliver.userData.header[i].udhType == SMS_UDH_CONCAT_16BIT) {
110                         msg.msgRef = (unsigned short)pTpdu->data.deliver.userData.header[i].udh.concat16bit.msgRef;
111                         msg.totalSeg = pTpdu->data.deliver.userData.header[i].udh.concat16bit.totalSeg;
112                         msg.seqNum = pTpdu->data.deliver.userData.header[i].udh.concat16bit.seqNum;
113
114                         memcpy(&(msg.timeStamp.time.absolute), &(pTpdu->data.deliver.timeStamp.time.absolute), sizeof(SMS_TIME_ABS_S));
115                         memcpy(&(msg.originAddress), &(pTpdu->data.deliver.originAddress), sizeof(SMS_ADDRESS_S));
116                         memcpy(&(msg.dcs), &(pTpdu->data.deliver.dcs), sizeof(SMS_DCS_S));
117 #if 0
118                         if (msg.totalSeg > MAX_SEGMENT_NUM) {
119                                 MSG_DEBUG("Total Segment Count is over Maximum [%d]", msg.totalSeg);
120                                 return;
121                         }
122 #endif
123
124                         /**  check noneConcatTypeHeader */
125                         noneConcatTypeHeader = false;
126
127                         break;
128                 }
129         }
130
131         unsigned char segCnt = checkConcatMsg(&msg, &(pTpdu->data.deliver.userData));
132
133         MSG_DEBUG("segCnt [%d]", segCnt);
134         MSG_DEBUG("msg.totalSeg [%d]", msg.totalSeg);
135
136         if ((segCnt == msg.totalSeg) || noneConcatTypeHeader) {
137                 MSG_DEBUG("RECEIVED LAST CONCAT : %d", segCnt);
138
139                 int dataSize = 0;
140                 char* pUserData = NULL;
141                 AutoPtr<char> dataBuf(&pUserData);
142
143                 MSG_MESSAGE_INFO_S msgInfo = {0};
144
145                 dataSize = makeConcatUserData(msg.msgRef, &pUserData);
146
147                 if (dataSize > 0) {
148                         if (SmsPluginWapPushHandler::instance()->IsWapPushMsg(&(pTpdu->data.deliver.userData)) == true) {
149                                 SmsPluginWapPushHandler::instance()->copyDeliverData(&(pTpdu->data.deliver));
150                                 SmsPluginWapPushHandler::instance()->handleWapPushMsg(pUserData, dataSize);
151                         } else {
152                                 convertConcatToMsginfo(&(pTpdu->data.deliver), pUserData, dataSize, &msgInfo);
153
154                                 if (msgInfo.msgPort.valid == false) {
155                                         /** Add Concat Msg into DB */
156                                         err = SmsPluginStorage::instance()->addMessage(&msgInfo);
157                                 }
158
159                                 if (err == MSG_SUCCESS) {
160                                         /** Callback */
161                                         err = SmsPluginEventHandler::instance()->callbackMsgIncoming(&msgInfo);
162
163                                         if (err != MSG_SUCCESS) {
164                                                 MSG_DEBUG("callbackMsgIncoming() Error !! [%d]", err);
165                                         }
166                                 } else {
167                                         MSG_DEBUG("addMessage() Error !! [%d]", err);
168                                 }
169                         }
170                 }
171
172                 removeFromConcatList(msg.msgRef);
173         }
174
175         /** Send Deliver Report */
176         SmsPluginTransport::instance()->sendDeliverReport(err);
177
178         MSG_END();
179 }
180
181 #ifdef CONCAT_SIM_MSG_OPERATION
182 void SmsPluginConcatHandler::handleConcatMsg(SMS_TPDU_S *pTpdu, msg_sim_id_t SimMsgId, bool bRead)
183 {
184         MSG_BEGIN();
185
186         if (pTpdu->tpduType != SMS_TPDU_DELIVER)
187         {
188                 MSG_DEBUG("The TPDU type is not deliver [%d]", pTpdu->tpduType);
189                 return;
190         }
191
192         SMS_CONCAT_MSG_S msg;
193         memset(&msg, 0x00, sizeof(SMS_CONCAT_MSG_S));
194
195         for (int i = 0; i < pTpdu->data.deliver.userData.headerCnt; i++)
196         {
197                 if (pTpdu->data.deliver.userData.header[i].udhType == SMS_UDH_CONCAT_8BIT)
198                 {
199                         msg.msgRef = (unsigned short)pTpdu->data.deliver.userData.header[i].udh.concat8bit.msgRef;
200                         msg.totalSeg = pTpdu->data.deliver.userData.header[i].udh.concat8bit.totalSeg;
201                         msg.seqNum = pTpdu->data.deliver.userData.header[i].udh.concat8bit.seqNum;
202
203                         memcpy(&(msg.timeStamp.time.absolute), &(pTpdu->data.deliver.timeStamp.time.absolute), sizeof(SMS_TIME_ABS_S));
204                         memcpy(&(msg.originAddress), &(pTpdu->data.deliver.originAddress), sizeof(SMS_ADDRESS_S));
205                         memcpy(&(msg.dcs), &(pTpdu->data.deliver.dcs), sizeof(SMS_DCS_S));
206
207                         msg.bRead = bRead;
208
209                         if (msg.totalSeg > MAX_SEGMENT_NUM)
210                         {
211                                 MSG_DEBUG("Total Segment Count is over Maximum [%d]", msg.totalSeg);
212                                 return;
213                         }
214
215                         break;
216                 }
217                 else if (pTpdu->data.deliver.userData.header[i].udhType == SMS_UDH_CONCAT_16BIT)
218                 {
219                         msg.msgRef = (unsigned short)pTpdu->data.deliver.userData.header[i].udh.concat16bit.msgRef;
220                         msg.totalSeg = pTpdu->data.deliver.userData.header[i].udh.concat16bit.totalSeg;
221                         msg.seqNum = pTpdu->data.deliver.userData.header[i].udh.concat16bit.seqNum;
222
223                         memcpy(&(msg.timeStamp.time.absolute), &(pTpdu->data.deliver.timeStamp.time.absolute), sizeof(SMS_TIME_ABS_S));
224                         memcpy(&(msg.originAddress), &(pTpdu->data.deliver.originAddress), sizeof(SMS_ADDRESS_S));
225                         memcpy(&(msg.dcs), &(pTpdu->data.deliver.dcs), sizeof(SMS_DCS_S));
226
227                         msg.bRead = bRead;
228
229                         if (msg.totalSeg > MAX_SEGMENT_NUM)
230                         {
231                                 MSG_DEBUG("Total Segment Count is over Maximum [%d]", msg.totalSeg);
232                                 return;
233                         }
234
235                         break;
236                 }
237         }
238
239         unsigned char segCnt = checkConcatMsg(&msg, &(pTpdu->data.deliver.userData));
240
241         addToSimIdList(msg.msgRef, SimMsgId);
242
243         if (segCnt == msg.totalSeg)
244         {
245                 MSG_DEBUG("RECEIVED LAST CONCAT : %d", segCnt);
246
247                 int dataSize = 0;
248                 char* pUserData = NULL;
249                 AutoPtr<char> dataBuf(&pUserData);
250
251                 MSG_MESSAGE_INFO_S msgInfo = {0};
252
253                 dataSize = makeConcatUserData(msg.msgRef, &pUserData);
254
255                 if (dataSize >= 0)
256                 {
257                         MSG_DEBUG("TOTAL DATA : %s", pUserData);
258
259                         convertSimMsgToMsginfo(&msg, pUserData, dataSize, &msgInfo);
260
261                         // set Sim Message ID
262                         msgInfo.msgId = SimMsgId;
263
264                         // set read status
265                         msgInfo.bRead = bRead;
266
267                         /// Print MSG_MESSAGE_INFO_S
268                         MSG_DEBUG("############# Convert  tpdu values to Message Info values ####################");
269
270                         MSG_DEBUG("msgInfo.msgId : %d", msgInfo.msgId);
271                         MSG_DEBUG("msgInfo.nAddressCnt : %d", msgInfo.nAddressCnt);
272                         MSG_DEBUG("msgInfo.addressList[0].addressType : %d", msgInfo.addressList[0].addressType);
273                         MSG_DEBUG("msgInfo.addressList[0].addressVal : %s", msgInfo.addressList[0].addressVal);
274                         MSG_DEBUG("msgInfo.priority : %d", msgInfo.priority);
275                         MSG_DEBUG("msgInfo.bProtected : %d", msgInfo.bProtected);
276                         MSG_DEBUG("msgInfo.bRead : %d", msgInfo.bRead);
277                         MSG_DEBUG("msgInfo.bTextSms : %d", msgInfo.bTextSms);
278                         MSG_DEBUG("msgInfo.direction : %d", msgInfo.direction);
279                         MSG_DEBUG("msgInfo.msgType.mainType : %d", msgInfo.msgType.mainType);
280                         MSG_DEBUG("msgInfo.msgType.subType : %d", msgInfo.msgType.subType);
281                         MSG_DEBUG("msgInfo.msgType.classType : %d", msgInfo.msgType.classType);
282                         MSG_DEBUG("msgInfo.displayTime : %s", ctime(&msgInfo.displayTime));
283                         MSG_DEBUG("msgInfo.dataSize : %d", msgInfo.dataSize);
284
285                         if (msgInfo.bTextSms == true)
286                                 MSG_DEBUG("msgInfo.msgText : %s", msgInfo.msgText);
287                         else
288                                 MSG_DEBUG("msgInfo.msgData : %s", msgInfo.msgData);
289
290                         MSG_DEBUG("###############################################################");
291
292                         // Remove from List
293                         removeFromConcatList(msg.msgRef);
294                         removeFromSimIdList(msg.msgRef);
295
296                         //add msgInfo to msg list
297                         SmsPluginStorage::instance()->addSimMsgToList(&msgInfo, true);
298
299                         // Callback to MSG FW
300                         SmsPluginEventHandler::instance()->callbackGetSimMsg();
301                 }
302         }
303         else
304         {
305                 //add index count to msg list
306                 SmsPluginStorage::instance()->addSimMsgToList(NULL, false);
307
308                 // Callback to MSG FW
309                 SmsPluginEventHandler::instance()->callbackGetSimMsg();
310         }
311
312         MSG_END();
313 }
314
315
316 void SmsPluginConcatHandler::handleBrokenMsg()
317 {
318         if (concatList.size() <= 0 || simIdList.size() <= 0)
319         {
320                 MSG_DEBUG("No Broken Concatenated Message");
321                 return;
322         }
323
324         do
325         {
326                 int index = 0, dataSize = 0;
327                 char* pUserData = NULL;
328                 AutoPtr<char> dataBuf(&pUserData);
329
330                 MSG_MESSAGE_INFO_S msgInfo = {0};
331
332                 dataSize = makeConcatUserData(concatList[index].msgRef, &pUserData);
333
334                 if (dataSize > 0)
335                 {
336                         MSG_DEBUG("TOTAL DATA : %s", pUserData);
337
338                         SMS_CONCAT_MSG_S msg;
339                         memset(&msg, 0x00, sizeof(SMS_CONCAT_MSG_S));
340
341                         msg.msgRef = concatList[index].msgRef;
342                         msg.totalSeg = concatList[index].totalSeg;
343
344                         memcpy(&(msg.timeStamp.time.absolute), &(concatList[index].timeStamp.time.absolute), sizeof(SMS_TIME_ABS_S));
345                         memcpy(&(msg.originAddress), &(concatList[index].originAddress), sizeof(SMS_ADDRESS_S));
346                         memcpy(&(msg.dcs), &(concatList[index].dcs), sizeof(SMS_DCS_S));
347
348                         convertSimMsgToMsginfo(&msg, pUserData, dataSize, &msgInfo);
349
350                         // set Sim Message ID
351                         msgInfo.msgId = 0;
352
353                         // set read status
354                         msgInfo.bRead = concatList[index].bRead;
355
356                         /// Print MSG_MESSAGE_INFO_S
357                         MSG_DEBUG("############# Convert  tpdu values to Message Info values ####################");
358                         MSG_DEBUG("msgInfo.msgId : %d", msgInfo.msgId);
359                         MSG_DEBUG("msgInfo.nAddressCnt : %d", msgInfo.nAddressCnt);
360                         MSG_DEBUG("msgInfo.addressList[0].addressType : %d", msgInfo.addressList[0].addressType);
361                         MSG_DEBUG("msgInfo.addressList[0].addressVal : %s", msgInfo.addressList[0].addressVal);
362                         MSG_DEBUG("msgInfo.priority : %d", msgInfo.priority);
363                         MSG_DEBUG("msgInfo.bProtected : %d", msgInfo.bProtected);
364                         MSG_DEBUG("msgInfo.bRead : %d", msgInfo.bRead);
365                         MSG_DEBUG("msgInfo.bTextSms : %d", msgInfo.bTextSms);
366                         MSG_DEBUG("msgInfo.direction : %d", msgInfo.direction);
367                         MSG_DEBUG("msgInfo.msgType.mainType : %d", msgInfo.msgType.mainType);
368                         MSG_DEBUG("msgInfo.msgType.subType : %d", msgInfo.msgType.subType);
369                         MSG_DEBUG("msgInfo.msgType.classType : %d", msgInfo.msgType.classType);
370                         MSG_DEBUG("msgInfo.displayTime : %s", ctime(&msgInfo.displayTime));
371                         MSG_DEBUG("msgInfo.dataSize : %d", msgInfo.dataSize);
372                         if (msgInfo.bTextSms == true)
373                                 MSG_DEBUG("msgInfo.msgText : %s", msgInfo.msgText);
374                         else
375                         MSG_DEBUG("msgInfo.msgData : %s", msgInfo.msgData);
376                         MSG_DEBUG("###############################################################");
377
378                         //add msgInfo to msg list
379                         SmsPluginStorage::instance()->addSimMsgToList(&msgInfo, true);
380                 }
381
382                 removeFromConcatList(concatList[index].msgRef);
383                 removeFromSimIdList(concatList[index].msgRef);
384         }while (concatList.size() > 0);
385 }
386 #endif
387
388
389 unsigned char SmsPluginConcatHandler::checkConcatMsg(SMS_CONCAT_MSG_S *pConcatMsg, SMS_USERDATA_S *pUserData)
390 {
391         if (pConcatMsg == NULL || pUserData == NULL) {
392                 MSG_DEBUG("In Parameter is NULL");
393                 return 0;
394         }
395
396         unsigned char currSegCnt = 0;
397
398         bool bFind = false;
399
400         for (unsigned int i = 0; i < concatList.size(); i++) {
401                 if (concatList[i].msgRef == pConcatMsg->msgRef) {
402                         if (concatList[i].data.count(pConcatMsg->seqNum) != 0) {
403                                 MSG_DEBUG("The Sequence Number already exists [%d]", pConcatMsg->seqNum);
404                                 return 0;
405                         }
406
407                         CONCAT_DATA_S concatData = {0};
408
409                         memcpy(concatData.data, pUserData->data, pUserData->length);
410                         concatData.length = pUserData->length;
411
412                         pair<unsigned char, CONCAT_DATA_S> newData(pConcatMsg->seqNum, concatData);
413                         concatList[i].data.insert(newData);
414
415                         MSG_DEBUG("MSG DATA : %s", pUserData->data);
416                         MSG_DEBUG("PAIR DATA [%d] : %s", newData.first, newData.second.data);
417
418                         concatList[i].segCnt++;
419                         concatList[i].totalSize += pUserData->length;
420
421                         currSegCnt = concatList[i].segCnt;
422
423                         bFind = true;
424
425                         break;
426                 }
427         }
428
429         /** New Concat Msg */
430         if (bFind == false) {
431                 SMS_CONCAT_INFO_S tmpInfo;
432
433                 tmpInfo.msgRef = pConcatMsg->msgRef;
434                 tmpInfo.totalSeg = pConcatMsg->totalSeg;
435                 tmpInfo.segCnt = 1;
436
437                 memcpy(&(tmpInfo.timeStamp.time.absolute), &(pConcatMsg->timeStamp.time.absolute), sizeof(SMS_TIME_ABS_S));
438                 memcpy(&(tmpInfo.originAddress), &(pConcatMsg->originAddress), sizeof(SMS_ADDRESS_S));
439                 memcpy(&(tmpInfo.dcs), &(pConcatMsg->dcs), sizeof(SMS_DCS_S));
440
441                 tmpInfo.totalSize = pUserData->length;
442
443                 CONCAT_DATA_S concatData = {0};
444
445                 memcpy(concatData.data, pUserData->data, pUserData->length);
446                 concatData.length = pUserData->length;
447
448                 pair<unsigned char, CONCAT_DATA_S> newData(pConcatMsg->seqNum, concatData);
449                 tmpInfo.data.insert(newData);
450
451                 MSG_DEBUG("MSG DATA : %s", pUserData->data);
452                 MSG_DEBUG("PAIR DATA [%d] : %s", newData.first, newData.second.data);
453
454                 concatList.push_back(tmpInfo);
455
456                 currSegCnt = tmpInfo.segCnt;
457         }
458
459         return currSegCnt;
460 }
461
462
463 int SmsPluginConcatHandler::makeConcatUserData(unsigned short MsgRef, char **ppTotalData)
464 {
465         concatDataMap::iterator it;
466
467         int totalSize = 0, offset = 0;
468
469         for (unsigned int i = 0; i < concatList.size(); i++) {
470                 if (concatList[i].msgRef == MsgRef) {
471                         totalSize = concatList[i].totalSize;
472
473                         if (totalSize <= 0) {
474                                 MSG_DEBUG("Size Error : totalSize <= 0");
475                                 return 0;
476                         }
477
478                         MSG_DEBUG("totalSize [%d]", totalSize);
479
480                         *ppTotalData = new char[totalSize];
481
482                         for (it = concatList[i].data.begin(); it != concatList[i].data.end(); it++) {
483                                 memcpy(*ppTotalData+offset, it->second.data, it->second.length);
484                                 offset += it->second.length;
485                         }
486                 }
487         }
488
489         return totalSize;
490 }
491
492
493 void SmsPluginConcatHandler::convertConcatToMsginfo(const SMS_DELIVER_S *pTpdu, const char *pUserData, int DataSize, MSG_MESSAGE_INFO_S *pMsgInfo)
494 {
495         /** Convert Type  values */
496         pMsgInfo->msgType.mainType = MSG_SMS_TYPE;
497         pMsgInfo->msgType.subType = MSG_NORMAL_SMS;
498
499         /** set folder id */
500         pMsgInfo->folderId = MSG_INBOX_ID;
501
502         /** set storage id */
503         pMsgInfo->storageId = MSG_STORAGE_PHONE;
504
505         switch(pTpdu->dcs.msgClass)
506         {
507                 case SMS_MSG_CLASS_0:
508                         pMsgInfo->msgType.classType = MSG_CLASS_0;
509                         break;
510                 case SMS_MSG_CLASS_1:
511                         pMsgInfo->msgType.classType = MSG_CLASS_1;
512                         break;
513                 case SMS_MSG_CLASS_2:
514                         pMsgInfo->msgType.classType = MSG_CLASS_2;
515                         break;
516                 case SMS_MSG_CLASS_3:
517                         pMsgInfo->msgType.classType = MSG_CLASS_3;
518                         break;
519                 default:
520                         pMsgInfo->msgType.classType = MSG_CLASS_NONE;
521                         break;
522         }
523
524         pMsgInfo->networkStatus = MSG_NETWORK_RECEIVED;
525         pMsgInfo->bRead = false;
526         pMsgInfo->bProtected = false;
527         pMsgInfo->priority = MSG_MESSAGE_PRIORITY_NORMAL;
528         pMsgInfo->direction = MSG_DIRECTION_TYPE_MT;
529
530
531         time_t rawtime = time(NULL);
532
533 /*** Comment below lines to save local UTC time..... (it could be used later.)
534
535         if (pTpdu->timeStamp.format == SMS_TIME_ABSOLUTE) {
536
537                 MSG_DEBUG("year : %d", pTpdu->timeStamp.time.absolute.year);
538                 MSG_DEBUG("month : %d", pTpdu->timeStamp.time.absolute.month);
539                 MSG_DEBUG("day : %d", pTpdu->timeStamp.time.absolute.day);
540                 MSG_DEBUG("hour : %d", pTpdu->timeStamp.time.absolute.hour);
541                 MSG_DEBUG("minute : %d", pTpdu->timeStamp.time.absolute.minute);
542                 MSG_DEBUG("second : %d", pTpdu->timeStamp.time.absolute.second);
543                 MSG_DEBUG("timezone : %d", pTpdu->timeStamp.time.absolute.timeZone);
544
545                 char displayTime[32];
546                 struct tm * timeTM;
547
548                 struct tm timeinfo;
549                 memset(&timeinfo, 0x00, sizeof(tm));
550
551                 timeinfo.tm_year = (pTpdu->timeStamp.time.absolute.year + 100);
552                 timeinfo.tm_mon = (pTpdu->timeStamp.time.absolute.month - 1);
553                 timeinfo.tm_mday = pTpdu->timeStamp.time.absolute.day;
554                 timeinfo.tm_hour = pTpdu->timeStamp.time.absolute.hour;
555                 timeinfo.tm_min = pTpdu->timeStamp.time.absolute.minute;
556                 timeinfo.tm_sec = pTpdu->timeStamp.time.absolute.second;
557                 timeinfo.tm_isdst = 0;
558
559                 rawtime = mktime(&timeinfo);
560
561                 MSG_DEBUG("tzname[0] [%s]", tzname[0]);
562                 MSG_DEBUG("tzname[1] [%s]", tzname[1]);
563                 MSG_DEBUG("timezone [%d]", timezone);
564                 MSG_DEBUG("daylight [%d]", daylight);
565
566                 memset(displayTime, 0x00, sizeof(displayTime));
567                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", &timeinfo);
568                 MSG_DEBUG("displayTime [%s]", displayTime);
569
570                 rawtime -= (pTpdu->timeStamp.time.absolute.timeZone * (3600/4));
571
572                 timeTM = localtime(&rawtime);
573                 memset(displayTime, 0x00, sizeof(displayTime));
574                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
575                 MSG_DEBUG("displayTime [%s]", displayTime);
576
577                 rawtime -= timezone;
578
579                 timeTM = localtime(&rawtime);
580                 memset(displayTime, 0x00, sizeof(displayTime));
581                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
582                 MSG_DEBUG("displayTime [%s]", displayTime);
583         }
584
585 ***/
586
587         pMsgInfo->displayTime = rawtime;
588
589         /** Convert Address values */
590         pMsgInfo->nAddressCnt = 1;
591         pMsgInfo->addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
592         strncpy(pMsgInfo->addressList[0].addressVal, pTpdu->originAddress.address, MAX_ADDRESS_VAL_LEN);
593
594         pMsgInfo->msgPort.valid = false;
595         pMsgInfo->msgPort.dstPort = 0;
596         pMsgInfo->msgPort.srcPort = 0;
597
598         for (int i = 0; i < pTpdu->userData.headerCnt; i++) {
599                 /** Convert UDH values - Port Number */
600                 if (pTpdu->userData.header[i].udhType == SMS_UDH_APP_PORT_8BIT) {
601                         pMsgInfo->msgPort.valid = true;
602                         pMsgInfo->msgPort.dstPort = pTpdu->userData.header[i].udh.appPort8bit.destPort;
603                         pMsgInfo->msgPort.srcPort = pTpdu->userData.header[i].udh.appPort8bit.originPort;
604                 } else if (pTpdu->userData.header[i].udhType == SMS_UDH_APP_PORT_16BIT) {
605                         pMsgInfo->msgPort.valid = true;
606                         pMsgInfo->msgPort.dstPort = pTpdu->userData.header[i].udh.appPort16bit.destPort;
607                         pMsgInfo->msgPort.srcPort = pTpdu->userData.header[i].udh.appPort16bit.originPort;
608                 }
609         }
610
611         //int bufSize = (MAX_MSG_DATA_LEN*MAX_SEGMENT_NUM) + 1;
612         int bufSize = (DataSize*4) + 1; // For UTF8
613
614         char tmpBuf[bufSize];
615         memset(tmpBuf, 0x00, sizeof(tmpBuf));
616
617         /** Convert Data values */
618         if (pTpdu->dcs.codingScheme == SMS_CHARSET_7BIT) {
619                 MSG_LANG_INFO_S langInfo = {0,};
620
621                 langInfo.bSingleShift = false;
622                 langInfo.bLockingShift = false;
623
624                 pMsgInfo->encodeType = MSG_ENCODE_GSM7BIT;
625                 pMsgInfo->dataSize = textCvt.convertGSM7bitToUTF8((unsigned char*)tmpBuf, bufSize, (unsigned char*)pUserData, DataSize, &langInfo);
626         } else if (pTpdu->dcs.codingScheme == SMS_CHARSET_8BIT) {
627                 pMsgInfo->encodeType = MSG_ENCODE_8BIT;
628                 memcpy(tmpBuf, pUserData, DataSize);
629                 pMsgInfo->dataSize = DataSize;
630         } else if (pTpdu->dcs.codingScheme == SMS_CHARSET_UCS2) {
631                 pMsgInfo->encodeType = MSG_ENCODE_UCS2;
632                 pMsgInfo->dataSize = textCvt.convertUCS2ToUTF8((unsigned char*)tmpBuf, bufSize, (unsigned char*)pUserData, DataSize);
633         }
634
635         MSG_DEBUG("Data Size [%d]", pMsgInfo->dataSize);
636         MSG_DEBUG("Data [%s]", tmpBuf);
637
638 #ifdef MSG_FW_FOR_DEBUG
639 printf("\n");
640
641 for (int i = 0; i < pMsgInfo->dataSize; i++)
642 {
643         printf("[%02x]", tmpBuf[i]);
644 }
645
646 printf("\n");
647 #endif
648
649         if (pMsgInfo->dataSize > MAX_MSG_TEXT_LEN) {
650                 pMsgInfo->bTextSms = false;
651
652                 /** Save Message Data into File */
653                 char fileName[MSG_FILENAME_LEN_MAX+1];
654                 memset(fileName, 0x00, sizeof(fileName));
655
656                 if (MsgCreateFileName(fileName) == false)
657                         THROW(MsgException::FILE_ERROR, "########  MsgCreateFileName Fail !!! #######");
658
659                 MSG_DEBUG("Save Message Data into file : size[%d] name[%s]\n", pMsgInfo->dataSize, fileName);
660                 if (MsgWriteIpcFile(fileName, tmpBuf, pMsgInfo->dataSize) == false)
661                         THROW(MsgException::FILE_ERROR, "########  MsgWriteIpcFile Fail !!! #######");
662
663                 strncpy(pMsgInfo->msgData, fileName, MAX_MSG_DATA_LEN);
664         } else {
665                 pMsgInfo->bTextSms = true;
666
667                 memset(pMsgInfo->msgText, 0x00, sizeof(pMsgInfo->msgText));
668                 memcpy(pMsgInfo->msgText, tmpBuf, pMsgInfo->dataSize);
669         }
670 }
671
672
673 #ifdef CONCAT_SIM_MSG_OPERATION
674 void SmsPluginConcatHandler::convertSimMsgToMsginfo(const SMS_CONCAT_MSG_S *pConcatMsg, const char *pUserData, int DataSize, MSG_MESSAGE_INFO_S *pMsgInfo)
675 {
676         // Convert Type  values
677         pMsgInfo->msgType.mainType = MSG_SMS_TYPE;
678         pMsgInfo->msgType.subType = MSG_CONCAT_SIM_SMS;
679
680         // set folder id (temporary)
681         pMsgInfo->folderId = MSG_INBOX_ID;
682
683         pMsgInfo->storageId = MSG_STORAGE_SIM;
684
685         switch (pConcatMsg->dcs.msgClass)
686         {
687                 case SMS_MSG_CLASS_0:
688                         pMsgInfo->msgType.classType = MSG_CLASS_0;
689                         break;
690                 case SMS_MSG_CLASS_1:
691                         pMsgInfo->msgType.classType = MSG_CLASS_1;
692                         break;
693                 case SMS_MSG_CLASS_2:
694                         pMsgInfo->msgType.classType = MSG_CLASS_2;
695                         break;
696                 case SMS_MSG_CLASS_3:
697                         pMsgInfo->msgType.classType = MSG_CLASS_3;
698                         break;
699                 default:
700                         pMsgInfo->msgType.classType = MSG_CLASS_NONE;
701         }
702
703         pMsgInfo->networkStatus = MSG_NETWORK_RECEIVED;
704         pMsgInfo->bRead = false;
705         pMsgInfo->bProtected = false;
706         pMsgInfo->priority = MSG_MESSAGE_PRIORITY_NORMAL;
707         pMsgInfo->direction = MSG_DIRECTION_TYPE_MT;
708
709         time_t rawtime = time(NULL);
710
711 /*** Comment below lines to save local UTC time..... (it could be used later.)
712
713         if (pTpdu->timeStamp.format == SMS_TIME_ABSOLUTE) {
714
715                 MSG_DEBUG("year : %d", pTpdu->timeStamp.time.absolute.year);
716                 MSG_DEBUG("month : %d", pTpdu->timeStamp.time.absolute.month);
717                 MSG_DEBUG("day : %d", pTpdu->timeStamp.time.absolute.day);
718                 MSG_DEBUG("hour : %d", pTpdu->timeStamp.time.absolute.hour);
719                 MSG_DEBUG("minute : %d", pTpdu->timeStamp.time.absolute.minute);
720                 MSG_DEBUG("second : %d", pTpdu->timeStamp.time.absolute.second);
721                 MSG_DEBUG("timezone : %d", pTpdu->timeStamp.time.absolute.timeZone);
722
723                 char displayTime[32];
724                 struct tm * timeTM;
725
726                 struct tm timeinfo;
727                 memset(&timeinfo, 0x00, sizeof(tm));
728
729                 timeinfo.tm_year = (pTpdu->timeStamp.time.absolute.year + 100);
730                 timeinfo.tm_mon = (pTpdu->timeStamp.time.absolute.month - 1);
731                 timeinfo.tm_mday = pTpdu->timeStamp.time.absolute.day;
732                 timeinfo.tm_hour = pTpdu->timeStamp.time.absolute.hour;
733                 timeinfo.tm_min = pTpdu->timeStamp.time.absolute.minute;
734                 timeinfo.tm_sec = pTpdu->timeStamp.time.absolute.second;
735                 timeinfo.tm_isdst = 0;
736
737                 rawtime = mktime(&timeinfo);
738
739                 MSG_DEBUG("tzname[0] [%s]", tzname[0]);
740                 MSG_DEBUG("tzname[1] [%s]", tzname[1]);
741                 MSG_DEBUG("timezone [%d]", timezone);
742                 MSG_DEBUG("daylight [%d]", daylight);
743
744                 memset(displayTime, 0x00, sizeof(displayTime));
745                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", &timeinfo);
746                 MSG_DEBUG("displayTime [%s]", displayTime);
747                 rawtime -= (pTpdu->timeStamp.time.absolute.timeZone * (3600/4));
748
749                 timeTM = localtime(&rawtime);
750                 memset(displayTime, 0x00, sizeof(displayTime));
751                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
752                 MSG_DEBUG("displayTime [%s]", displayTime);
753
754                 rawtime -= timezone;
755
756                 timeTM = localtime(&rawtime);
757                 memset(displayTime, 0x00, sizeof(displayTime));
758                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
759                 MSG_DEBUG("displayTime [%s]", displayTime);
760         }
761
762 ***/
763
764         pMsgInfo->displayTime = rawtime;
765
766         // Convert Address values
767         pMsgInfo->nAddressCnt = 1;
768         pMsgInfo->addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
769         strncpy(pMsgInfo->addressList[0].addressVal, pConcatMsg->originAddress.address, MAX_ADDRESS_VAL_LEN);
770
771         pMsgInfo->msgPort.valid = false;
772         pMsgInfo->msgPort.dstPort = 0;
773         pMsgInfo->msgPort.srcPort = 0;
774
775         // Insert SMS_CONCAT_SIM_MSG_S into File
776         SMS_CONCAT_SIM_MSG_S concatSimMsg = {0};
777
778         for (unsigned int i = 0; i < simIdList.size(); i++)
779         {
780                 if (simIdList[i].msgRef == pConcatMsg->msgRef)
781                 {
782                         MSG_DEBUG("Get SIM ID [%d] - List Index [%d]", simIdList[i].simId, concatSimMsg.simIdCnt);
783
784                         concatSimMsg.simIdList[concatSimMsg.simIdCnt] = simIdList[i].simId;
785                         concatSimMsg.simIdCnt++;
786                 }
787         }
788
789         int bufSize = (MAX_MSG_DATA_LEN*MAX_SEGMENT_NUM) + 1;
790
791         char tmpBuf[bufSize];
792         memset(tmpBuf, 0x00, sizeof(tmpBuf));
793
794         // Convert Data values
795         if (pConcatMsg->dcs.codingScheme == SMS_CHARSET_7BIT)
796         {
797                 SMS_LANG_INFO_S langInfo = {0};
798
799                 langInfo.bSingleShift = false;
800                 langInfo.bLockingShift = false;
801
802                 pMsgInfo->encodeType = MSG_ENCODE_GSM7BIT;
803                 pMsgInfo->dataSize = SmsPluginTextConvert::instance()->convertGSM7bitToUTF8((unsigned char*)tmpBuf, bufSize, (unsigned char*)pUserData, DataSize, &langInfo);
804         }
805         else if (pConcatMsg->dcs.codingScheme == SMS_CHARSET_8BIT)
806         {
807                 pMsgInfo->encodeType = MSG_ENCODE_8BIT;
808                 memcpy(tmpBuf, pUserData, DataSize);
809                 pMsgInfo->dataSize = DataSize;
810         }
811         else if (pConcatMsg->dcs.codingScheme == SMS_CHARSET_UCS2)
812         {
813                 pMsgInfo->encodeType = MSG_ENCODE_UCS2;
814                 pMsgInfo->dataSize = SmsPluginTextConvert::instance()->convertUCS2ToUTF8((unsigned char*)tmpBuf, bufSize, (unsigned char*)pUserData, DataSize);
815         }
816
817         MSG_DEBUG("Data Size [%d]", pMsgInfo->dataSize);
818
819         pMsgInfo->bTextSms = false;
820
821         if (pMsgInfo->dataSize > 0)
822                 memcpy(concatSimMsg.msgData, tmpBuf, pMsgInfo->dataSize);
823
824         // Save Message Data into File
825         char fileName[MAX_COMMON_INFO_SIZE+1];
826         memset(fileName, 0x00, sizeof(fileName));
827
828         if (MsgCreateFileName(fileName) == false)
829                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
830
831         if (MsgWriteIpcFile(fileName, (char*)(&concatSimMsg), sizeof(SMS_CONCAT_SIM_MSG_S)) == false)
832                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
833
834         memset(pMsgInfo->msgData, 0x00, sizeof(pMsgInfo->msgData));
835         strncpy(pMsgInfo->msgData, fileName, MAX_MSG_DATA_LEN);
836
837         MSG_DEBUG("Save Message Data into file : size[%d] name[%s]", pMsgInfo->dataSize, fileName);
838 }
839 #endif
840
841
842 void SmsPluginConcatHandler::removeFromConcatList(unsigned short MsgRef)
843 {
844         for (int index = concatList.size(); index >= 0 ; index--) {
845                 if (concatList[index].msgRef == MsgRef) {
846                         MSG_DEBUG("remove concatlist of the index [%d]", index);
847                         concatList.erase(concatList.begin()+index);
848                         break;
849                 }
850         }
851 }
852
853 #ifdef CONCAT_SIM_MSG_OPERATION
854 void SmsPluginConcatHandler::addToSimIdList(unsigned short MsgRef, msg_sim_id_t SimMsgId)
855 {
856         SMS_SIM_ID_S simIdStruct;
857
858         simIdStruct.msgRef = MsgRef;
859         simIdStruct.simId = SimMsgId;
860
861         simIdList.push_back(simIdStruct);
862 }
863
864
865 void SmsPluginConcatHandler::removeFromSimIdList(unsigned short MsgRef)
866 {
867         for (int index = simIdList.size()-1; index >= 0 ; index--)
868         {
869                 if (simIdList[index].msgRef == MsgRef)
870                 {
871                         MSG_DEBUG("remove simIdList of the index [%d]", index);
872
873                         simIdList.erase(simIdList.begin()+index);
874                 }
875         }
876 }
877 #endif