Modify flora license version.
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginCbMsgHandler.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 <time.h>
18
19 #include "MsgDebug.h"
20 #include "MsgCppTypes.h"
21 #include "MsgException.h"
22 #include "MsgGconfWrapper.h"
23 #include "MsgUtilFile.h"
24 #include "SmsPluginUDCodec.h"
25 #include "SmsPluginStorage.h"
26 #include "SmsPluginEventHandler.h"
27 #include "SmsPluginCbMsgHandler.h"
28
29
30 /*==================================================================================================
31                                      IMPLEMENTATION OF SmsPluginCbMsgHandler - Member Functions
32 ==================================================================================================*/
33 SmsPluginCbMsgHandler* SmsPluginCbMsgHandler::pInstance = NULL;
34
35
36 SmsPluginCbMsgHandler::SmsPluginCbMsgHandler()
37 {
38         pageList.clear();
39 }
40
41
42 SmsPluginCbMsgHandler::~SmsPluginCbMsgHandler()
43 {
44
45 }
46
47
48 SmsPluginCbMsgHandler* SmsPluginCbMsgHandler::instance()
49 {
50         if (!pInstance)
51                 pInstance = new SmsPluginCbMsgHandler();
52
53         return pInstance;
54 }
55
56
57 void SmsPluginCbMsgHandler::handleCbMsg(TelSmsCbMsg_t *pCbMsg)
58 {
59         MSG_BEGIN();
60
61         SMS_CB_NETWORK_TYPE_T type = pCbMsg->CbMsgType;
62
63         SMS_CBMSG_PAGE_S CbMsgPage = {0};
64
65         switch (type)
66         {
67                 case SMS_CB_NETWORK_TYPE_2G_GSM :
68                         Decode2gCbMsg(pCbMsg, &CbMsgPage);
69                 break;
70
71                 case SMS_CB_NETWORK_TYPE_3G_UMTS :
72                         Decode3gCbMsg(pCbMsg, &CbMsgPage);
73                 break;
74         }
75
76         // Check CB Msg Options
77         bool bJavaMsg = false;
78
79         if (!checkCbOpt(CbMsgPage, &bJavaMsg))
80         {
81                 MSG_DEBUG("The CB Msg is not supported by option.");
82                 return;
83         }
84
85         if (bJavaMsg == true)
86         {
87                 MSG_DEBUG("JAVA CB Msg.");
88                 CbMsgPage.cbMsgType = SMS_CBMSG_TYPE_JAVACBS;
89         }
90
91         // Check CB Pages
92         unsigned char pageCnt = checkCbPage(CbMsgPage);
93
94         if (pageCnt == CbMsgPage.pageHeader.totalPages)
95         {
96                 MSG_DEBUG("RECEIVED LAST MSG : %d", pageCnt);
97
98                 SMS_CBMSG_S cbMsg = {};
99                 MSG_MESSAGE_INFO_S msgInfo = {};
100
101                 // Make CB Msg Structure
102                 MakeCbMsg(CbMsgPage, &cbMsg);
103
104                 // Convert to MSG_MESSAGE_INFO_S
105                 convertCbMsgToMsginfo(&cbMsg, &msgInfo);
106
107                 // Add CB Msg into DB
108                 msg_error_t err = MSG_SUCCESS;
109
110                 err = SmsPluginStorage::instance()->addMessage(&msgInfo);
111
112                 if (err == MSG_SUCCESS)
113                 {
114                         MSG_CB_MSG_S cbOutMsg = {0, };
115
116                         cbOutMsg.type = MSG_CB_SMS;
117                         cbOutMsg.receivedTime = cbMsg.recvTime;
118                         cbOutMsg.serialNum = encodeCbSerialNum (CbMsgPage.pageHeader.serialNum);
119                         cbOutMsg.messageId = cbMsg.msgId;
120                         cbOutMsg.dcs = CbMsgPage.pageHeader.dcs.rawData;
121                         memset (cbOutMsg.cbText, 0x00, sizeof(cbOutMsg.cbText));
122
123                         cbOutMsg.cbTextLen= convertTextToUtf8((unsigned char*)cbOutMsg.cbText, sizeof(cbOutMsg.cbText), &cbMsg);
124                         memset(cbOutMsg.language_type, 0x00, sizeof(cbOutMsg.language_type));
125                         memcpy(cbOutMsg.language_type, CbMsgPage.pageHeader.dcs.iso639Lang, 3);
126                         err = SmsPluginEventHandler::instance()->callbackCBMsgIncoming(&cbOutMsg);
127                         if (err != MSG_SUCCESS)
128                         {
129                                 MSG_DEBUG("callbackMsgIncoming() Error !! [%d]", err);
130                         }
131                 }
132                 else
133                 {
134                         MSG_DEBUG("addMessage() Error !! [%d]", err);
135                 }
136
137                 // Remove From List
138                 removeFromPageList(CbMsgPage);
139         }
140         MSG_END();
141 }
142
143
144 void SmsPluginCbMsgHandler::handleEtwsMsg(TelSmsEtwsMsg_t *pEtwsMsg)
145 {
146         MSG_BEGIN();
147         msg_error_t err = MSG_SUCCESS;
148         SMS_ETWS_NETWORK_TYPE_T type = pEtwsMsg->EtwsMsgType;
149         //MSG_MESSAGE_INFO_S msgInfo = {};
150         SMS_ETWS_PRIMARY_S              etwsPn = {0, };
151         MSG_CB_MSG_S                    cbOutMsg = {0, };
152
153         if(type != TAPI_NETTEXT_ETWS_PRIMARY)
154         {
155                 MSG_DEBUG("The Etws Msg is not supported");
156                 return;
157         }
158         DecodeEtwsMsg(pEtwsMsg, &etwsPn);
159         //convertEtwsMsgToMsginfo(CbMsgPage, &msgInfo);
160
161         cbOutMsg.type = MSG_ETWS_SMS;
162         cbOutMsg.receivedTime = etwsPn.recvTime;
163         cbOutMsg.serialNum = encodeCbSerialNum (etwsPn.serialNum);
164         cbOutMsg.messageId = etwsPn.msgId;
165         cbOutMsg.etwsWarningType = etwsPn.warningType;
166         memcpy (cbOutMsg.etwsWarningSecurityInfo, etwsPn.warningSecurityInfo, sizeof(cbOutMsg.etwsWarningSecurityInfo));
167
168         err = SmsPluginEventHandler::instance()->callbackCBMsgIncoming(&cbOutMsg);
169         if (err != MSG_SUCCESS)
170         {
171                 MSG_DEBUG("callbackMsgIncoming() Error !! [%d]", err);
172         }
173
174         MSG_END();
175 }
176
177
178 void SmsPluginCbMsgHandler::Decode2gCbMsg(TelSmsCbMsg_t *pCbMsg, SMS_CBMSG_PAGE_S *pCbPage)
179 {
180         if (pCbMsg->Length > MAX_CBMSG_PAGE_SIZE)
181                 THROW(MsgException::SMS_PLG_ERROR, "CB Msg Size is over MAX [%d]", pCbMsg->Length);
182
183         unsigned char cbData[pCbMsg->Length+1];
184
185         memset(cbData, 0x00, sizeof(cbData));
186         memcpy(cbData, pCbMsg->szMsgData, pCbMsg->Length);
187         cbData[pCbMsg->Length] = '\0';
188
189         pCbPage->cbMsgType = SMS_CBMSG_TYPE_CBS;
190
191         // Serial Number
192         pCbPage->pageHeader.serialNum.geoScope = (cbData[0] & 0xC0) >> 6;
193
194         pCbPage->pageHeader.serialNum.msgCode = (cbData[0] & 0x3F) << 4;
195         pCbPage->pageHeader.serialNum.msgCode |= (cbData[1] & 0xF0) >> 4;
196
197         pCbPage->pageHeader.serialNum.updateNum = cbData[1] & 0x0F;
198
199 MSG_DEBUG("geoScope : [%d], msgCode : [%d], updateNum : [%d]", pCbPage->pageHeader.serialNum.geoScope, pCbPage->pageHeader.serialNum.msgCode, pCbPage->pageHeader.serialNum.updateNum);
200
201         pCbPage->pageHeader.msgId = (cbData[2] << 8) | cbData[3];
202
203 MSG_DEBUG("MSG ID : [%d]", pCbPage->pageHeader.msgId);
204
205         // DCS
206         decodeCbMsgDCS(cbData[4], (unsigned char*)cbData[6], &(pCbPage->pageHeader.dcs));
207
208         // Page Parameter
209         pCbPage->pageHeader.totalPages = cbData[5] & 0x0F;
210         pCbPage->pageHeader.page = (cbData[5] & 0xF0) >> 4;
211
212         if (pCbPage->pageHeader.totalPages > MAX_CBMSG_PAGE_NUM)
213                 THROW(MsgException::SMS_PLG_ERROR, "CB Page Count is over MAX[%d]", pCbPage->pageHeader.totalPages);
214
215 MSG_DEBUG("Total Page : [%d], Page : [%d]", pCbPage->pageHeader.totalPages, pCbPage->pageHeader.page);
216
217         // Convert Language Type
218         convertLangType(pCbPage->pageHeader.dcs.langType, &(pCbPage->pageHeader.langType));
219
220 MSG_DEBUG("In Language Type : [%d], Out Language Type : [%d]", pCbPage->pageHeader.dcs.langType, pCbPage->pageHeader.langType);
221 MSG_DEBUG("iso639Lang : [%s]", pCbPage->pageHeader.dcs.iso639Lang);
222         // Get Receive Time
223         pCbPage->pageHeader.recvTime = getRecvTime();
224
225         // Decode CB Data
226         int dataLen = pCbMsg->Length - 6;
227
228         switch (pCbPage->pageHeader.dcs.codingScheme)
229         {
230                 case SMS_CHARSET_7BIT :
231                 {
232                         MSG_DEBUG("GSM 7 BIT");
233
234                         dataLen = (dataLen*8) / 7;
235
236                         SmsPluginUDCodec udCodec;
237                         char pageData[MAX_CBMSG_PAGE_SIZE+1];
238                         int unpackLen = udCodec.unpack7bitChar(&cbData[6], dataLen, 0, pageData);
239
240                         if(pCbPage->pageHeader.dcs.iso639Lang[0])
241                         {
242                                 unpackLen = unpackLen - 3;
243                                 memcpy(pCbPage->pageData, &pageData[3], unpackLen);
244                         } else {
245                                 memcpy(pCbPage->pageData, &pageData, unpackLen);
246                         }
247
248                         MSG_DEBUG("unpackLen : [%d]", unpackLen);
249
250                         pCbPage->pageLength = unpackLen;
251                         pCbPage->pageData[unpackLen] = '\0';
252                 }
253                 break;
254
255                 case SMS_CHARSET_8BIT :
256                 case SMS_CHARSET_UCS2 :
257                 {
258                         MSG_DEBUG("UCS2");
259
260                         if(pCbPage->pageHeader.dcs.iso639Lang[0])
261                         {
262                                 memcpy(pCbPage->pageData, &cbData[8], dataLen - 2);
263                                 pCbPage->pageLength = dataLen - 2;
264                         } else {
265                                 memcpy(pCbPage->pageData, &cbData[6], dataLen);
266                                 pCbPage->pageLength = dataLen;
267                         }
268                 }
269                 break;
270         }
271
272 MSG_DEBUG("Page Length : [%d], Page Data : [%s]", pCbPage->pageLength, pCbPage->pageData);
273 }
274
275
276 void SmsPluginCbMsgHandler::DecodeEtwsMsg(TelSmsEtwsMsg_t *pEtwsMsg, SMS_ETWS_PRIMARY_S *pEtwsPn)
277 {
278         if ( !pEtwsMsg || !pEtwsPn )
279                 return;
280
281         if (pEtwsMsg->Length > MAX_ETWS_SIZE)
282                 THROW(MsgException::SMS_PLG_ERROR, "ETWS Msg Size is over MAX [%d]", pEtwsMsg->Length);
283
284         unsigned char EtwsData[pEtwsMsg->Length+1];
285
286         memset(EtwsData, 0x00, sizeof(EtwsData));
287         memcpy(EtwsData, pEtwsMsg->szMsgData, pEtwsMsg->Length);
288         EtwsData[pEtwsMsg->Length] = '\0';
289
290         // received time
291         pEtwsPn->recvTime = getRecvTime();
292
293         // Serial Number
294         pEtwsPn->serialNum.geoScope = (EtwsData[0] & 0xC0) >> 6;
295         pEtwsPn->serialNum.msgCode = (EtwsData[0] & 0x3F) << 4;
296         pEtwsPn->serialNum.msgCode |= (EtwsData[1] & 0xF0) >> 4;
297         pEtwsPn->serialNum.updateNum = EtwsData[1] & 0x0F;
298
299         MSG_DEBUG("geoScope : [%d], msgCode : [%d], updateNum : [%d]", pEtwsPn->serialNum.geoScope, pEtwsPn->serialNum.msgCode, pEtwsPn->serialNum.updateNum);
300
301         // Message Identifier
302         pEtwsPn->msgId = (EtwsData[2] << 8) | EtwsData[3];
303         MSG_DEBUG("MSG ID : [%d]", pEtwsPn->msgId);
304
305         // warning type
306         pEtwsPn->warningType = (EtwsData[4] << 8) | EtwsData[5];
307         MSG_DEBUG("warningType : [0x%04x]", pEtwsPn->msgId);
308
309         // warning security information
310         memcpy(pEtwsPn->warningSecurityInfo, &EtwsData[6], sizeof(pEtwsPn->warningSecurityInfo));       // 50bytes
311         for (unsigned int i = 0; i < sizeof(pEtwsPn->warningSecurityInfo); i++)
312         {
313                 MSG_DEBUG("warning secu info [%02x]", pEtwsPn->warningSecurityInfo[i] );
314         }
315 }
316
317 void SmsPluginCbMsgHandler::Decode3gCbMsg(TelSmsCbMsg_t *pCbMsg, SMS_CBMSG_PAGE_S *pCbPage)
318 {
319         if (pCbMsg->Length > (MAX_CBMSG_PAGE_SIZE*MAX_CBMSG_PAGE_NUM))
320                 THROW(MsgException::SMS_PLG_ERROR, "CB Msg Size is over MAX [%d]", pCbMsg->Length);
321
322         char cbData[(MAX_CBMSG_PAGE_SIZE*MAX_CBMSG_PAGE_NUM)+1];
323
324         memset(cbData, 0x00, sizeof(cbData));
325         memcpy(cbData, pCbMsg->szMsgData, sizeof(pCbMsg->szMsgData));
326         cbData[pCbMsg->Length] = '\0';
327
328         pCbPage->cbMsgType = (SMS_CBMSG_TYPE_T)cbData[0];
329
330         pCbPage->pageHeader.msgId = (cbData[1] << 8) | cbData[2];
331
332 MSG_DEBUG("MSG ID : [%d]", pCbPage->pageHeader.msgId);
333
334         // Serial Number
335         pCbPage->pageHeader.serialNum.geoScope = (cbData[3] & 0xC0) >> 6;
336
337         pCbPage->pageHeader.serialNum.msgCode = (cbData[3] & 0x3F) << 4;
338         pCbPage->pageHeader.serialNum.msgCode |= (cbData[4] & 0xF0) >> 4;
339
340         pCbPage->pageHeader.serialNum.updateNum = cbData[4] & 0x0F;
341
342 MSG_DEBUG("geoScope : [%d], msgCode : [%d], updateNum : [%d]", pCbPage->pageHeader.serialNum.geoScope, pCbPage->pageHeader.serialNum.msgCode, pCbPage->pageHeader.serialNum.updateNum);
343
344         // DCS
345         decodeCbMsgDCS(cbData[5], (unsigned char*)cbData[6], &(pCbPage->pageHeader.dcs));
346
347         // Convert Language Type
348         convertLangType(pCbPage->pageHeader.dcs.langType, &(pCbPage->pageHeader.langType));
349
350 MSG_DEBUG("In Language Type : [%d], Out Language Type : [%d]", pCbPage->pageHeader.dcs.langType, pCbPage->pageHeader.langType);
351
352         // Get Receive Time
353         pCbPage->pageHeader.recvTime = getRecvTime();
354
355 #if 0
356         // Decode CB Data
357         int dataLen = pCbMsg->Length - 6;
358
359         switch (pCbPage->pageHeader.dcs.codingScheme)
360         {
361                 case SMS_CHARSET_7BIT :
362                 {
363                         dataLen = (dataLen*8) / 7;
364
365                         if (pCbPage->pageLength > MAX_CBMSG_PAGE_SIZE)
366                                 THROW(MsgException::SMS_PLG_ERROR, "CB Msg Size is over MAX [%d]", pCbPage->pageLength);
367
368                         SmsPluginUDCodec udCodec;
369                         int unpackLen = udCodec.unpack7bitChar(&cbData[6], dataLen, 0, pCbPage->pageData);
370                         pCbPage->pageData[unpackLen] = '\0';
371
372                         pCbPage->pageLength = unpackLen;
373                 }
374                 break;
375
376                 case SMS_CHARSET_8BIT :
377                 case SMS_CHARSET_UCS2 :
378                 {
379                         pCbPage->pageLength = dataLen;
380
381                         memcpy(pCbPage->pageData, &cbData[6], pCbPage->pageLength);
382                         pCbPage->pageData[pCbPage->pageLength] = '\0';
383                 }
384                 break;
385         }
386
387 MSG_DEBUG("Page Length : [%d], Page Data : [%s]", pCbPage->pageLength, pCbPage->pageData);
388 #endif
389 }
390
391 unsigned short SmsPluginCbMsgHandler::encodeCbSerialNum ( SMS_CBMSG_SERIAL_NUM_S snFields )
392 {
393         unsigned short serialNum = 0;
394
395         serialNum = ((snFields.geoScope & 0x03) << 14) | ((snFields.msgCode&0x03FF) << 4) | (snFields.updateNum&0x0F);
396         MSG_DEBUG ("serialNum (%x), geo(%x), mc(%x), un(%x)\n", serialNum, snFields.geoScope, snFields.msgCode, snFields.updateNum);
397
398         return serialNum;
399 }
400
401 bool SmsPluginCbMsgHandler::checkCbOpt(SMS_CBMSG_PAGE_S CbPage, bool *pJavaMsg)
402 {
403         bool bReceive = false;
404         MsgSettingGetBool(CB_RECEIVE, &bReceive);
405
406         // Receive CB Msg = FALSE
407         if (!bReceive)
408         {
409                 MSG_DEBUG("RECEIVE CB = FALSE");
410                 return false;
411         }
412
413         char keyName[128];
414
415         // check Language
416         memset(keyName, 0x00, sizeof(keyName));
417         sprintf(keyName, "%s/%d", CB_LANGUAGE, MSG_CBLANG_TYPE_ALL);
418
419         bool bAllLang = false;
420         MsgSettingGetBool(keyName, &bAllLang);
421
422         if (!bAllLang)
423         {
424                 MSG_DEBUG("ALL LANGUAGE = FALSE");
425
426                 memset(keyName, 0x00, sizeof(keyName));
427                 sprintf(keyName, "%s/%d", CB_LANGUAGE, CbPage.pageHeader.langType);
428
429                 bool bLang = false;
430
431                 MsgSettingGetBool(keyName, &bLang);
432
433                 if (!bLang || CbPage.pageHeader.langType == MSG_CBLANG_TYPE_MAX)
434                 {
435                         MSG_DEBUG("LANGUAGE [%d] = FALSE", CbPage.pageHeader.langType);
436                         return false;
437                 }
438         }
439
440         int MsgIdCnt = MsgSettingGetInt(CB_CHANNEL_COUNT);
441
442         bool bActivate = false;
443         int MsgId_from = 0, MsgId_to = 0;
444
445         for (int i = 0; i < MsgIdCnt; i++)
446         {
447                 memset(keyName, 0x00, sizeof(keyName));
448                 sprintf(keyName, "%s/%d", CB_CHANNEL_ACTIVATE, i);
449
450                 MsgSettingGetBool(keyName, &bActivate);
451
452                 memset(keyName, 0x00, sizeof(keyName));
453                 sprintf(keyName, "%s/%d", CB_CHANNEL_ID_FROM, i);
454
455                 MsgId_from = MsgSettingGetInt(keyName);
456
457                 memset(keyName, 0x00, sizeof(keyName));
458                 sprintf(keyName, "%s/%d", CB_CHANNEL_ID_TO, i);
459
460                 MsgId_to = MsgSettingGetInt(keyName);
461
462                 if (bActivate == true && CbPage.pageHeader.msgId >= MsgId_from && CbPage.pageHeader.msgId <= MsgId_to)
463                 {
464                         MSG_DEBUG("FIND CHANNEL = [%d]", CbPage.pageHeader.msgId);
465                         return true;
466                 }
467         }
468
469         return false;
470 }
471
472
473 unsigned char SmsPluginCbMsgHandler::checkCbPage(SMS_CBMSG_PAGE_S CbPage)
474 {
475         unsigned char currPageCnt = 0;
476
477         bool bFind = false;
478
479         if (CbPage.pageHeader.totalPages > 1)
480         {
481                 for (unsigned int i = 0; i < pageList.size(); i++)
482                 {
483                         if (pageList[i].geoScope == CbPage.pageHeader.serialNum.geoScope && pageList[i].msgCode == CbPage.pageHeader.serialNum.msgCode)
484                         {
485                                 MSG_DEBUG("geoScope [%d], msgCode [%d]", pageList[i].geoScope, pageList[i].msgCode);
486
487                                 if (pageList[i].msgId == CbPage.pageHeader.msgId)
488                                 {
489                                         int updateNum = CbPage.pageHeader.serialNum.updateNum - pageList[i].updateNum;
490
491                                         if (updateNum > 0) // New Message Content
492                                         {
493                                                 break;
494                                         }
495                                         else if (updateNum == 0) // Same Message Content
496                                         {
497                                                 if (pageList[i].data.count(CbPage.pageHeader.page) != 0)
498                                                 {
499                                                         MSG_DEBUG("The Page Number already exists [%d]", CbPage.pageHeader.page);
500                                                         return 0;
501                                                 }
502
503                                                 pair<unsigned char, string> newData(CbPage.pageHeader.page, CbPage.pageData);
504                                                 pageList[i].data.insert(newData);
505
506                                                 MSG_DEBUG("PAGE DATA : %s", CbPage.pageData);
507                                                 MSG_DEBUG("PAIR DATA [%d] : %s", newData.first, newData.second.c_str());
508
509                                                 pageList[i].pageCnt++;
510                                                 pageList[i].totalSize += CbPage.pageLength;
511
512                                                 currPageCnt = pageList[i].pageCnt;
513
514                                                 bFind = true;
515
516                                                 break;
517                                         }
518                                         else // Old Message Content
519                                         {
520                                                 return 0;
521                                         }
522                                 }
523                         }
524                 }
525         }
526
527         if (bFind == false || CbPage.pageHeader.totalPages == 1)
528         {
529                 addToPageLiat(CbPage);
530                 return 1;
531         }
532
533         return currPageCnt;
534 }
535
536
537 void SmsPluginCbMsgHandler::MakeCbMsg(SMS_CBMSG_PAGE_S CbPage, SMS_CBMSG_S *pCbMsg)
538 {
539         pCbMsg->cbMsgType = CbPage.cbMsgType;
540         pCbMsg->msgId = CbPage.pageHeader.msgId;
541         pCbMsg->classType = CbPage.pageHeader.dcs.classType;
542         pCbMsg->codingScheme = CbPage.pageHeader.dcs.codingScheme;
543         pCbMsg->recvTime = CbPage.pageHeader.recvTime;
544
545         cbPageMap::iterator it;
546         string tmpStr ("");
547
548         for (unsigned int i = 0; i < pageList.size(); i++)
549         {
550                 if (pageList[i].geoScope == CbPage.pageHeader.serialNum.geoScope && pageList[i].msgCode == CbPage.pageHeader.serialNum.msgCode)
551                 {
552                         MSG_DEBUG("geoScope [%d], msgCode [%d]", pageList[i].geoScope, pageList[i].msgCode);
553
554                         if (pageList[i].msgId == CbPage.pageHeader.msgId)
555                         {
556                                 for (it = pageList[i].data.begin(); it != pageList[i].data.end(); it++)
557                                 {
558                                         tmpStr += it->second;
559                                 }
560                         }
561                 }
562         }
563
564         pCbMsg->msgLength = tmpStr.size();
565
566         memcpy(pCbMsg->msgData, tmpStr.c_str(), tmpStr.size());
567         pCbMsg->msgData[tmpStr.size()] = '\0';
568
569         MSG_DEBUG("SIZE : [%d] TOTAL MSG : %s", tmpStr.size(), tmpStr.c_str());
570 }
571
572
573 void SmsPluginCbMsgHandler::convertCbMsgToMsginfo(SMS_CBMSG_S *pCbMsg, MSG_MESSAGE_INFO_S *pMsgInfo)
574 {
575         pMsgInfo->msgId = (msg_message_id_t)pCbMsg->msgId;
576
577         pMsgInfo->folderId = MSG_CBMSGBOX_ID;
578
579         // Convert Type values
580         pMsgInfo->msgType.mainType = MSG_SMS_TYPE;
581
582         if (pCbMsg->cbMsgType == SMS_CBMSG_TYPE_CBS)
583                 pMsgInfo->msgType.subType = MSG_CB_SMS;
584         else if (pCbMsg->cbMsgType == SMS_CBMSG_TYPE_JAVACBS)
585                 pMsgInfo->msgType.subType = MSG_JAVACB_SMS;
586
587         switch(pCbMsg->classType)
588         {
589                 case SMS_MSG_CLASS_0:
590                         pMsgInfo->msgType.classType = MSG_CLASS_0;
591                         break;
592                 case SMS_MSG_CLASS_1:
593                         pMsgInfo->msgType.classType = MSG_CLASS_1;
594                         break;
595                 case SMS_MSG_CLASS_2:
596                         pMsgInfo->msgType.classType = MSG_CLASS_2;
597                         break;
598                 case SMS_MSG_CLASS_3:
599                         pMsgInfo->msgType.classType = MSG_CLASS_3;
600                         break;
601                 default:
602                         pMsgInfo->msgType.classType = MSG_CLASS_NONE;
603                         break;
604         }
605
606         pMsgInfo->storageId = MSG_STORAGE_PHONE;
607         pMsgInfo->networkStatus = MSG_NETWORK_RECEIVED;
608         pMsgInfo->bRead = false;
609         pMsgInfo->bProtected = false;
610         pMsgInfo->priority = MSG_MESSAGE_PRIORITY_NORMAL;
611         pMsgInfo->direction = MSG_DIRECTION_TYPE_MT;
612
613         // Temporary
614         pMsgInfo->nAddressCnt = 1;
615
616         pMsgInfo->addressList[0].addressType = MSG_ADDRESS_TYPE_UNKNOWN;
617         pMsgInfo->addressList[0].recipientType = MSG_RECIPIENTS_TYPE_UNKNOWN;
618
619         getDisplayName(pCbMsg->msgId, pMsgInfo->addressList[0].addressVal);
620         MSG_DEBUG("%s", pMsgInfo->addressList[0].addressVal);
621
622         pMsgInfo->msgPort.valid = false;
623         pMsgInfo->msgPort.dstPort = 0;
624         pMsgInfo->msgPort.srcPort = 0;
625
626         pMsgInfo->displayTime = pCbMsg->recvTime;
627         MSG_DEBUG("recvTime is %s", ctime(&pMsgInfo->displayTime));
628
629         int bufSize = pCbMsg->msgLength*2;
630
631         char tmpBuf[bufSize];
632         memset(tmpBuf, 0x00, sizeof(tmpBuf));
633
634         MSG_DEBUG("LENGTH %d CB MSG %s", pCbMsg->msgLength, pCbMsg->msgData);
635
636         // Convert Data values
637         pMsgInfo->dataSize = convertTextToUtf8((unsigned char*)tmpBuf, bufSize, pCbMsg);
638
639         if (pMsgInfo->dataSize > MAX_MSG_TEXT_LEN)
640         {
641                 pMsgInfo->bTextSms = false;
642
643                 // Save Message Data into File
644                 char fileName[MSG_FILENAME_LEN_MAX+1];
645                 memset(fileName, 0x00, sizeof(fileName));
646
647                 if (MsgCreateFileName(fileName) == false)
648                         THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
649
650                 MSG_DEBUG("Save Message Data into file : size[%d] name[%s]\n", pMsgInfo->dataSize, fileName);
651                 if (MsgWriteIpcFile(fileName, tmpBuf, pMsgInfo->dataSize) == false)
652                         THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
653
654                 strncpy(pMsgInfo->msgData, fileName, MAX_MSG_DATA_LEN);
655         }
656         else
657         {
658                 pMsgInfo->bTextSms = true;
659
660                 memset(pMsgInfo->msgText, 0x00, sizeof(pMsgInfo->msgText));
661                 memcpy(pMsgInfo->msgText, tmpBuf, pMsgInfo->dataSize);
662         }
663 }
664
665
666 void SmsPluginCbMsgHandler::convertEtwsMsgToMsginfo(SMS_CBMSG_PAGE_S EtwsMsg, MSG_MESSAGE_INFO_S *pMsgInfo)
667 {
668         pMsgInfo->msgId = (msg_message_id_t)EtwsMsg.pageHeader.msgId;
669
670         pMsgInfo->folderId = MSG_CBMSGBOX_ID;
671
672         // Convert Type values
673         pMsgInfo->msgType.mainType = MSG_SMS_TYPE;
674
675         if (EtwsMsg.cbMsgType == SMS_CBMSG_TYPE_ETWS)
676                 pMsgInfo->msgType.subType = MSG_ETWS_SMS;
677
678         pMsgInfo->storageId = MSG_STORAGE_PHONE;
679         pMsgInfo->networkStatus = MSG_NETWORK_RECEIVED;
680         pMsgInfo->bRead = false;
681         pMsgInfo->bProtected = false;
682         pMsgInfo->priority = MSG_MESSAGE_PRIORITY_NORMAL;
683         pMsgInfo->direction = MSG_DIRECTION_TYPE_MT;
684
685         // Temporary
686         pMsgInfo->nAddressCnt = 1;
687
688         pMsgInfo->addressList[0].addressType = MSG_ADDRESS_TYPE_UNKNOWN;
689         pMsgInfo->addressList[0].recipientType = MSG_RECIPIENTS_TYPE_UNKNOWN;
690
691         getDisplayName(EtwsMsg.pageHeader.msgId, pMsgInfo->addressList[0].addressVal);
692         MSG_DEBUG("%s", pMsgInfo->addressList[0].addressVal);
693
694         pMsgInfo->msgPort.valid = false;
695         pMsgInfo->msgPort.dstPort = 0;
696         pMsgInfo->msgPort.srcPort = 0;
697
698         pMsgInfo->displayTime = EtwsMsg.pageHeader.recvTime;
699         MSG_DEBUG("recvTime is %s", ctime(&pMsgInfo->displayTime));
700         MSG_DEBUG("LENGTH %d", EtwsMsg.pageLength);
701         pMsgInfo->bTextSms = true;
702         pMsgInfo->dataSize = EtwsMsg.pageLength;
703         memset(pMsgInfo->msgData, 0x00, sizeof(pMsgInfo->msgData));
704         memcpy(pMsgInfo->msgData, EtwsMsg.pageData, pMsgInfo->dataSize);
705 }
706
707 int SmsPluginCbMsgHandler::convertTextToUtf8 (unsigned char* outBuf, int outBufSize, SMS_CBMSG_S* pCbMsg)
708 {
709         int     convertedTextSize = 0;
710         MSG_LANG_INFO_S langInfo = {0,};
711
712         if (!outBuf || !pCbMsg)
713         {
714                 MSG_DEBUG ("invalid param.\n");
715                 return 0;
716         }
717
718         langInfo.bSingleShift = false;
719         langInfo.bLockingShift = false;
720
721
722         // Convert Data values
723         if (pCbMsg->codingScheme == SMS_CHARSET_7BIT)
724                 convertedTextSize = textCvt.convertGSM7bitToUTF8(outBuf, outBufSize, (unsigned char*)pCbMsg->msgData, pCbMsg->msgLength, &langInfo);
725         else if (pCbMsg->codingScheme == SMS_CHARSET_UCS2)
726                 convertedTextSize = textCvt.convertUCS2ToUTF8(outBuf, outBufSize, (unsigned char*)pCbMsg->msgData, pCbMsg->msgLength);
727
728         return convertedTextSize;
729 }
730
731 void SmsPluginCbMsgHandler::addToPageLiat(SMS_CBMSG_PAGE_S CbPage)
732 {
733         CB_PAGE_INFO_S tmpInfo;
734
735         tmpInfo.geoScope = CbPage.pageHeader.serialNum.geoScope;
736         tmpInfo.msgCode = CbPage.pageHeader.serialNum.msgCode;
737         tmpInfo.updateNum = CbPage.pageHeader.serialNum.updateNum;
738         tmpInfo.msgId = CbPage.pageHeader.msgId;
739         tmpInfo.totalPages = CbPage.pageHeader.totalPages;
740
741         tmpInfo.pageCnt = 1;
742         tmpInfo.totalSize = CbPage.pageLength;
743
744         pair<unsigned char, string> newData(CbPage.pageHeader.page, CbPage.pageData);
745         tmpInfo.data.insert(newData);
746
747         MSG_DEBUG("MSG DATA : %s", CbPage.pageData);
748         MSG_DEBUG("PAIR DATA [%d] : %s", newData.first, newData.second.c_str());
749
750         pageList.push_back(tmpInfo);
751 }
752
753
754 void SmsPluginCbMsgHandler::removeFromPageList(SMS_CBMSG_PAGE_S CbPage)
755 {
756         unsigned int index;
757
758         for (index = 0; index < pageList.size(); index++)
759         {
760                 if (pageList[index].geoScope == CbPage.pageHeader.serialNum.geoScope && pageList[index].msgCode == CbPage.pageHeader.serialNum.msgCode)
761                 {
762                         MSG_DEBUG("geoScope [%d], msgCode [%d]", pageList[index].geoScope, pageList[index].msgCode);
763
764                         if (pageList[index].msgId == CbPage.pageHeader.msgId) break;
765                 }
766         }
767
768         MSG_DEBUG("remove index [%d]", index);
769
770         pageList.erase(pageList.begin()+index);
771 }
772
773
774 void SmsPluginCbMsgHandler::decodeCbMsgDCS(unsigned char dcsData, const unsigned char *pMsgData, SMS_CBMSG_DCS_S* pDcs)
775 {
776         pDcs->codingGroup = SMS_CBMSG_CODGRP_GENERAL_DCS;
777         pDcs->classType = SMS_MSG_CLASS_NONE;
778         pDcs->bCompressed = false;
779         pDcs->codingScheme = SMS_CHARSET_7BIT;
780         pDcs->langType = SMS_CBMSG_LANG_UNSPECIFIED;
781         memset(pDcs->iso639Lang, 0x00, sizeof(pDcs->iso639Lang));
782         pDcs->bUDH = false;
783         pDcs->rawData = dcsData;
784
785         unsigned char codingGrp = (dcsData & 0xF0) >> 4;
786
787         switch (codingGrp)
788         {
789                 case 0x00 :
790                 case 0x02 :
791                 case 0x03 :
792                 {
793                         pDcs->codingGroup = SMS_CBMSG_CODGRP_GENERAL_DCS;
794                         pDcs->langType = (SMS_CBMSG_LANG_TYPE_T)dcsData;
795                 }
796                 break;
797
798                 case 0x01 :
799                 {
800                         if (dcsData == 0x10 || dcsData == 0x11)
801                         {
802                                 pDcs->codingGroup = SMS_CBMSG_CODGRP_GENERAL_DCS;
803                                 pDcs->codingScheme = (dcsData & 0x01) ? SMS_CHARSET_UCS2 : SMS_CHARSET_7BIT;
804                                 pDcs->langType = SMS_CBMSG_LANG_ISO639;
805
806                                 if (pMsgData != NULL)
807                                 {
808                                         pDcs->iso639Lang[0] = pMsgData[0] & 0x7F;
809                                         pDcs->iso639Lang[1] = (pMsgData[0] & 0x80) >> 7;
810                                         pDcs->iso639Lang[1] |= (pMsgData[1] & 0x3F) << 1;
811                                         pDcs->iso639Lang[2]  = 0x13; /* CR char in GSM 7-bit Alphabet */
812                                 }
813                                 else
814                                 {
815                                         /* Default it to English if pMsgData is NULL */
816                                         pDcs->iso639Lang[0] = 0x45;  /* E */
817                                         pDcs->iso639Lang[1] = 0x4E;  /* N */
818                                         pDcs->iso639Lang[2] = 0x13;  /* CR */
819                                 }
820                         }
821                 }
822                 break;
823
824                 case 0x04 :
825                 case 0x05 :
826                 case 0x06 :
827                 case 0x07 :
828                 {
829                         pDcs->codingGroup = SMS_CBMSG_CODGRP_GENERAL_DCS;
830
831                         pDcs->bCompressed = (dcsData & 0x20) ? true : false;
832
833                         if (dcsData & 0x10)
834                                 pDcs->classType = (SMS_MSG_CLASS_T)(dcsData & 0x03);
835
836                         pDcs->codingScheme = (SMS_CODING_SCHEME_T)(dcsData & 0x0C);
837                 }
838                 break;
839
840                 case 0x09 :
841                 {
842                         pDcs->bUDH = true;
843                 }
844                 break;
845
846                 case 0x14 :
847                 {
848                         pDcs->codingGroup = SMS_CBMSG_CODGRP_WAP;
849                 }
850                 break;
851
852                 case 0x15 :
853                 {
854                         pDcs->codingGroup = SMS_CBMSG_CODGRP_CLASS_CODING;
855                         pDcs->codingScheme = (dcsData & 0x04) ? SMS_CHARSET_UCS2 : SMS_CHARSET_7BIT;
856                         pDcs->classType = (MSG_CLASS_TYPE_T)(dcsData & 0x03);
857                 }
858                 break;
859         }
860 }
861
862
863 void SmsPluginCbMsgHandler::convertLangType(SMS_CBMSG_LANG_TYPE_T InType , MSG_CB_LANGUAGE_TYPE_T *pOutType)
864 {
865         switch (InType)
866         {
867                 case SMS_CBMSG_LANG_GERMAN :
868                         *pOutType = MSG_CBLANG_TYPE_GER;
869                 break;
870
871                 case SMS_CBMSG_LANG_ENGLISH :
872                         *pOutType = MSG_CBLANG_TYPE_ENG;
873                 break;
874
875                 case SMS_CBMSG_LANG_ITALIAN :
876                         *pOutType = MSG_CBLANG_TYPE_ITA;
877                 break;
878
879                 case SMS_CBMSG_LANG_FRENCH :
880                         *pOutType = MSG_CBLANG_TYPE_FRE;
881                 break;
882
883                 case SMS_CBMSG_LANG_SPANISH :
884                         *pOutType = MSG_CBLANG_TYPE_SPA;
885                 break;
886
887                 case SMS_CBMSG_LANG_DUTCH :
888                         *pOutType = MSG_CBLANG_TYPE_NED;
889                 break;
890
891                 case SMS_CBMSG_LANG_SWEDISH :
892                         *pOutType = MSG_CBLANG_TYPE_SWE;
893                 break;
894
895                 case SMS_CBMSG_LANG_PORTUGUESE :
896                         *pOutType = MSG_CBLANG_TYPE_POR;
897                 break;
898
899                 case SMS_CBMSG_LANG_TURKISH :
900                         *pOutType = MSG_CBLANG_TYPE_TUR;
901                 break;
902
903                 default :
904                         *pOutType = MSG_CBLANG_TYPE_MAX;
905                 break;
906         }
907 }
908
909
910 unsigned long SmsPluginCbMsgHandler::getRecvTime()
911 {
912         time_t recvTime;
913
914         recvTime = time(NULL);
915
916         return (unsigned long)recvTime;
917 }
918
919
920 void SmsPluginCbMsgHandler::getDisplayName(unsigned short       MsgId, char *pDisplayName)
921 {
922         int MsgIdCnt = MsgSettingGetInt(CB_CHANNEL_COUNT);
923
924         char from[128];
925         char to[128];
926
927         for (int i = 0; i < MsgIdCnt; i++)
928         {
929                 memset(from, 0x00, sizeof(from));
930                 sprintf(from, "%s/%d", CB_CHANNEL_ID_FROM, i);
931
932                 memset(to, 0x00, sizeof(to));
933                 sprintf(to, "%s/%d", CB_CHANNEL_ID_TO, i);
934
935                 if (MsgId >= MsgSettingGetInt(from) && MsgId <= MsgSettingGetInt(to))
936                 {
937                         MSG_DEBUG("FIND MSG ID = [%d]", MsgId);
938 #if 0
939                         memset(keyName, 0x00, sizeof(keyName));
940                         sprintf(keyName, "%s/%d", CB_CHANNEL_NAME, i);
941
942                         memset(strTmp, 0x00, sizeof(strTmp));
943
944                         channelName = MsgSettingGetString(keyName);
945
946                         strncpy(strTmp, channelName, CB_CHANNEL_NAME_MAX);
947
948                         if (channelName) {
949                                 free(channelName);
950                                 channelName = NULL;
951                         }
952
953                         if (strlen(strTmp) > 0)
954                                 sprintf(pDisplayName, "[%s]", strTmp);
955                         else
956                                 sprintf(pDisplayName, "[%d]", MsgId);
957 #else
958                         sprintf(pDisplayName, "[%d]", MsgId);
959 #endif
960
961                         return;
962                 }
963         }
964
965         sprintf(pDisplayName, "[%d]", MsgId);
966 }
967