Modify flora license version.
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginEventHandler.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 #include<stdio.h>
19 #include<stdlib.h>
20 #include <errno.h>
21
22 #include "MsgDebug.h"
23 #include "MsgUtilFile.h"
24 #include "MsgGconfWrapper.h"
25 #include "SmsPluginTransport.h"
26 #include "SmsPluginSimMsg.h"
27 #include "SmsPluginStorage.h"
28 #include "SmsPluginSetting.h"
29 #include "SmsPluginConcatHandler.h"
30 #include "SmsPluginEventHandler.h"
31
32
33 /*==================================================================================================
34                                      IMPLEMENTATION OF SmsPluginEventHandler - Member Functions
35 ==================================================================================================*/
36 SmsPluginEventHandler* SmsPluginEventHandler::pInstance = NULL;
37
38
39 SmsPluginEventHandler::SmsPluginEventHandler()
40 {
41         /**  Initialize global parameters */
42         memset(&listener, 0x00, sizeof(MSG_PLUGIN_LISTENER_S));
43
44         pSimCnt = NULL;
45         devStatus = false;
46 }
47
48
49 SmsPluginEventHandler::~SmsPluginEventHandler()
50 {
51
52 }
53
54
55 SmsPluginEventHandler* SmsPluginEventHandler::instance()
56 {
57         if (!pInstance)
58                 pInstance = new SmsPluginEventHandler();
59
60         return pInstance;
61 }
62
63
64 void SmsPluginEventHandler::registerListener(MSG_PLUGIN_LISTENER_S *pListener)
65 {
66         listener = *pListener;
67 }
68
69
70 void SmsPluginEventHandler::handleSentStatus(msg_network_status_t NetStatus)
71 {
72         MSG_DEBUG("NetStatus[%d]", NetStatus);
73
74         SmsPluginTransport::instance()->setNetStatus(NetStatus);
75
76         if (sentInfo.bLast == true || NetStatus != MSG_NETWORK_SEND_SUCCESS) {
77                 /** Update Msg Status */
78                 if (sentInfo.reqInfo.msgInfo.msgPort.valid == false){
79                         SmsPluginStorage::instance()->updateSentMsg(&(sentInfo.reqInfo.msgInfo), NetStatus);
80                         sentInfo.reqInfo.msgInfo.networkStatus = NetStatus;
81                         callbackStorageChange(MSG_STORAGE_CHANGE_UPDATE, &(sentInfo.reqInfo.msgInfo));
82                 }
83
84                 MSG_DEBUG("sentInfo.reqInfo.sendOptInfo.bSetting [%d]", sentInfo.reqInfo.sendOptInfo.bSetting);
85                 MSG_DEBUG("sentInfo.reqInfo.sendOptInfo.bKeepCopy [%d]", sentInfo.reqInfo.sendOptInfo.bKeepCopy);
86                 /** Check sending options */
87                 if (sentInfo.reqInfo.sendOptInfo.bSetting && !sentInfo.reqInfo.sendOptInfo.bKeepCopy && NetStatus == MSG_NETWORK_SEND_SUCCESS) {
88                         SmsPluginStorage::instance()->deleteSmsMessage(sentInfo.reqInfo.msgInfo.msgId);
89                         callbackStorageChange(MSG_STORAGE_CHANGE_DELETE, &(sentInfo.reqInfo.msgInfo));
90                 }
91
92                 /** Callback to MSG FW */
93                 MSG_SENT_STATUS_S msgStatus;
94
95                 msgStatus.reqId = sentInfo.reqInfo.reqId;
96                 msgStatus.status = NetStatus;
97
98                 MSG_DEBUG("sentStatus.reqId : %d", msgStatus.reqId);
99                 MSG_DEBUG("sentStatus.status : %d", msgStatus.status);
100
101                 listener.pfSentStatusCb(&msgStatus);
102         }
103 }
104
105
106 void SmsPluginEventHandler::handleMsgIncoming(SMS_TPDU_S *pTpdu)
107 {
108         /** Make MSG_MESSAGE_INFO_S */
109         MSG_MESSAGE_INFO_S msgInfo;
110
111         /** initialize msgInfo */
112         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
113
114         /** check unsupported message */
115         if (pTpdu->tpduType == SMS_TPDU_DELIVER) {
116                 if (pTpdu->data.deliver.dcs.codingScheme == SMS_CHARSET_8BIT && pTpdu->data.deliver.pid == 0x11) {
117                         MSG_DEBUG("Unsupported message!!");
118                         SmsPluginTransport::instance()->sendDeliverReport(MSG_SUCCESS);
119                         return;
120                 }
121         }
122
123         /** check discard & indicator inactive message **/
124         if (pTpdu->data.deliver.dcs.bMWI == true &&
125                         pTpdu->data.deliver.dcs.bIndActive == false &&
126                         pTpdu->data.deliver.dcs.codingGroup == SMS_GROUP_DISCARD) {
127                 MSG_DEBUG("Discard and no-indication message!!");
128                 SmsPluginTransport::instance()->sendDeliverReport(MSG_SUCCESS);
129                 return;
130         }
131
132         /** convert to msgInfo */
133         convertTpduToMsginfo(pTpdu, &msgInfo);
134
135         /** Short Message Type 0 - Just Send Deliver Report */
136         if (msgInfo.msgType.subType == MSG_TYPE0_SMS) {
137                 SmsPluginTransport::instance()->sendDeliverReport(MSG_SUCCESS);
138                 return;
139         }
140
141         /** Print MSG_MESSAGE_INFO_S */
142         MSG_DEBUG("############# Convert  tpdu values to Message Info values ####################");
143         MSG_DEBUG("msgInfo.nAddressCnt : %d", msgInfo.nAddressCnt);
144         MSG_DEBUG("msgInfo.addressList[0].addressType : %d", msgInfo.addressList[0].addressType);
145         MSG_DEBUG("msgInfo.addressList[0].addressVal : %s", msgInfo.addressList[0].addressVal);
146         MSG_DEBUG("msgInfo.priority : %d", msgInfo.priority);
147         MSG_DEBUG("msgInfo.bProtected : %d", msgInfo.bProtected);
148         MSG_DEBUG("msgInfo.bRead : %d", msgInfo.bRead);
149         MSG_DEBUG("msgInfo.bTextSms : %d", msgInfo.bTextSms);
150         MSG_DEBUG("msgInfo.direction : %d", msgInfo.direction);
151         MSG_DEBUG("msgInfo.msgType.mainType : %d", msgInfo.msgType.mainType);
152         MSG_DEBUG("msgInfo.msgType.subType : %d", msgInfo.msgType.subType);
153         MSG_DEBUG("msgInfo.msgType.classType : %d", msgInfo.msgType.classType);
154         MSG_DEBUG("msgInfo.displayTime : %s", ctime(&msgInfo.displayTime));
155         MSG_DEBUG("msgInfo.msgPort.valid : %d", msgInfo.msgPort.valid);
156         MSG_DEBUG("msgInfo.encodeType : %d", msgInfo.encodeType);
157         MSG_DEBUG("msgInfo.dataSize : %d", msgInfo.dataSize);
158
159         if (msgInfo.bTextSms == true) {
160                 MSG_DEBUG("msgInfo.msgText : %s", msgInfo.msgText);
161         } else {
162                 MSG_DEBUG("msgInfo.msgData : %s", msgInfo.msgData);
163         }
164
165         MSG_DEBUG("###############################################################");
166
167         msg_error_t err = MSG_SUCCESS;
168
169         if (msgInfo.msgType.subType == MSG_STATUS_REPORT_SMS) {
170                 /** Status Report Message */
171                 err = listener.pfMsgIncomingCb(&msgInfo);
172
173                 /** Handling of Fail Case ?? */
174                 SmsPluginTransport::instance()->sendDeliverReport(MSG_SUCCESS);
175         } else { /** SMS Deliver */
176                 /** Class 2 Msg */
177                 if (msgInfo.msgType.classType == MSG_CLASS_2) {
178                         if (msgInfo.bTextSms == false) { /** Concat Msg cannot be saved in SIM */
179                                 msgInfo.msgType.classType = MSG_CLASS_NONE;
180                                 msgInfo.storageId = MSG_STORAGE_PHONE;
181                         }
182                 }
183
184                 if (msgInfo.msgPort.valid == false) {
185                         err = SmsPluginStorage::instance()->addMessage(&msgInfo);
186                 }
187
188                 /** Callback to MSG FW */
189                 if (msgInfo.msgType.classType != MSG_CLASS_2) {
190                         if (err == MSG_SUCCESS) {
191                                 MSG_DEBUG("callback to msg fw");
192                                 err = listener.pfMsgIncomingCb(&msgInfo);
193                         } else {
194                                 if (msgInfo.msgType.classType == MSG_CLASS_0) {
195                                         MSG_DEBUG("callback for class0 message to msg fw");
196                                         if (listener.pfMsgIncomingCb(&msgInfo) != MSG_SUCCESS)
197                                                 MSG_DEBUG("listener.pfMsgIncomingCb is failed!");
198                                 }
199                         }
200
201                         /** Send Deliver Report */
202                         if (msgInfo.msgType.classType == MSG_CLASS_0)
203                                 SmsPluginTransport::instance()->sendClass0DeliverReport(err);
204                         else
205                                 SmsPluginTransport::instance()->sendDeliverReport(err);
206                 }
207
208                 // Tizen Validation System
209                 char *msisdn = NULL;
210                 msisdn = MsgSettingGetString(MSG_SIM_MSISDN);
211
212                 MSG_SMS_VLD_INFO("%d, SMS Receive, %s->%s, %s",  msgInfo.msgId, \
213                                                                                                                                 msgInfo.addressList[0].addressVal, \
214                                                                                                                                 (msisdn == NULL)?"ME":msisdn, \
215                                                                                                                                 (err == MSG_SUCCESS)?"Success":"Fail");
216
217                 MSG_SMS_VLD_TXT("%d, [%s]", msgInfo.msgId, msgInfo.msgText);
218         }
219 }
220
221 void SmsPluginEventHandler::handlePushMsgIncoming(char* pPushHeader, char* pPushBody, int pushBodyLen, char *application_id, char *content_type)
222 {
223         MSG_PUSH_MESSAGE_DATA_S pushData;
224
225         memset(&pushData, 0x00, sizeof(MSG_PUSH_MESSAGE_DATA_S));
226
227         /** set PUSH data */
228         memcpy(&pushData.pushHeader, pPushHeader, strlen(pPushHeader));
229
230         pushData.pushBodyLen = pushBodyLen;
231         memcpy(pushData.pushBody, pPushBody, pushBodyLen);
232
233         memcpy(pushData.pushAppId, application_id, MAX_WAPPUSH_ID_LEN);
234         memcpy(pushData.pushContentType, content_type, MAX_WAPPUSH_CONTENT_TYPE_LEN);
235
236         /** Callback to MSG FW */
237         listener.pfPushMsgIncomingCb(&pushData);
238 }
239
240
241 void SmsPluginEventHandler::handleSyncMLMsgIncoming(msg_syncml_message_type_t msgType, char* pPushBody, int PushBodyLen, char* pWspHeader, int WspHeaderLen)
242 {
243         MSG_SYNCML_MESSAGE_DATA_S syncMLData;
244
245         memset(&syncMLData, 0x00, sizeof(MSG_SYNCML_MESSAGE_DATA_S));
246
247         /** set syncML data */
248         syncMLData.syncmlType = msgType;
249
250         syncMLData.pushBodyLen = PushBodyLen;
251         memcpy(syncMLData.pushBody, pPushBody, PushBodyLen);
252
253         syncMLData.wspHeaderLen= WspHeaderLen;
254         memcpy(syncMLData.wspHeader, pWspHeader, WspHeaderLen);
255
256         /** Callback to MSG FW */
257         listener.pfSyncMLMsgIncomingCb(&syncMLData);
258 }
259
260
261 void SmsPluginEventHandler::handleLBSMsgIncoming(char* pPushHeader, char* pPushBody, int pushBodyLen)
262 {
263         MSG_LBS_MESSAGE_DATA_S lBSData;
264
265         memset(&lBSData, 0x00, sizeof(MSG_LBS_MESSAGE_DATA_S));
266
267         /** set LBA data */
268         memcpy(&lBSData.pushHeader, pPushHeader, strlen(pPushHeader));
269
270         lBSData.pushBodyLen = pushBodyLen;
271         memcpy(lBSData.pushBody, pPushBody, pushBodyLen);
272
273         /** Callback to MSG FW */
274         listener.pfLBSMsgIncomingCb(&lBSData);
275 }
276
277
278 msg_error_t SmsPluginEventHandler::callbackMsgIncoming(MSG_MESSAGE_INFO_S *pMsgInfo)
279 {
280         MSG_BEGIN();
281
282         msg_error_t err = MSG_SUCCESS;
283
284         /** Callback to MSG FW */
285         err = listener.pfMsgIncomingCb(pMsgInfo);
286
287         MSG_END();
288
289         return err;
290 }
291
292 msg_error_t SmsPluginEventHandler::callbackCBMsgIncoming(MSG_CB_MSG_S *pCbMsg)
293 {
294         MSG_BEGIN();
295
296         msg_error_t err = MSG_SUCCESS;
297
298         /** Callback to MSG FW */
299         err = listener.pfCBMsgIncomingCb(pCbMsg);
300
301         MSG_END();
302
303         return err;
304 }
305
306
307 msg_error_t SmsPluginEventHandler::callbackInitSimBySat()
308 {
309         /** Callback to MSG FW */
310         return listener.pfInitSimBySatCb();
311 }
312
313
314 msg_error_t SmsPluginEventHandler::callbackStorageChange(msg_storage_change_type_t storageChangeType, MSG_MESSAGE_INFO_S *pMsgInfo)
315 {
316         /** Callback to MSG FW */
317         listener.pfStorageChangeCb(storageChangeType, pMsgInfo);
318
319         return MSG_SUCCESS;
320 }
321
322
323 void SmsPluginEventHandler::convertTpduToMsginfo(SMS_TPDU_S *pTpdu, MSG_MESSAGE_INFO_S *msgInfo)
324 {
325         memset(msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
326
327         switch(pTpdu->tpduType)
328         {
329                 case SMS_TPDU_SUBMIT :
330                         convertSubmitTpduToMsginfo(&pTpdu->data.submit, msgInfo);
331                         break;
332                 case SMS_TPDU_DELIVER :
333                         convertDeliverTpduToMsginfo(&pTpdu->data.deliver, msgInfo);
334                         break;
335                 case SMS_TPDU_STATUS_REP :
336                         convertStatusRepTpduToMsginfo(&pTpdu->data.statusRep, msgInfo);
337                         break;
338         }
339 }
340
341
342 void SmsPluginEventHandler::convertSubmitTpduToMsginfo(const SMS_SUBMIT_S *pTpdu, MSG_MESSAGE_INFO_S *msgInfo)
343 {
344         int addressListCnt = 0;
345
346         /** Convert Type  values */
347         msgInfo->msgType.mainType = MSG_SMS_TYPE;
348         msgInfo->msgType.subType = convertMsgSubType(pTpdu->pid);
349
350         /** set folder id (temporary) */
351         msgInfo->folderId = MSG_OUTBOX_ID;
352
353         switch(pTpdu->dcs.msgClass)
354         {
355                 case SMS_MSG_CLASS_0:
356                         msgInfo->msgType.classType = MSG_CLASS_0;
357                         break;
358                 case SMS_MSG_CLASS_1:
359                         msgInfo->msgType.classType = MSG_CLASS_1;
360                         break;
361                 case SMS_MSG_CLASS_2:
362                         msgInfo->msgType.classType = MSG_CLASS_2;
363                         break;
364                 case SMS_MSG_CLASS_3:
365                         msgInfo->msgType.classType = MSG_CLASS_3;
366                         break;
367                 default:
368                         msgInfo->msgType.classType = MSG_CLASS_NONE;
369         }
370
371         msgInfo->networkStatus = MSG_NETWORK_SEND_SUCCESS;
372         msgInfo->bRead = false;
373         msgInfo->bProtected = false;
374         msgInfo->priority = MSG_MESSAGE_PRIORITY_NORMAL;
375         msgInfo->direction = MSG_DIRECTION_TYPE_MT;
376         msgInfo->bTextSms = true;
377
378         memset(msgInfo->subject, 0x00, MAX_SUBJECT_LEN+1);
379
380         /** What kind of time has to be saved?? (temporary store time) */
381
382         time_t curTime;
383         localtime(&curTime);
384
385         msgInfo->displayTime = curTime;
386
387         /** Convert Address values */
388         msgInfo->nAddressCnt = 1;
389         msgInfo->addressList[addressListCnt].addressType = MSG_ADDRESS_TYPE_PLMN;
390         strncpy(msgInfo->addressList[addressListCnt].addressVal, pTpdu->destAddress.address, MAX_ADDRESS_VAL_LEN);
391
392         /**exception operation for none userdata */
393         if (pTpdu->userData.length == 0) {
394                 sprintf(msgInfo->msgText, "[Broken Message]");
395                 msgInfo->dataSize = strlen(msgInfo->msgText);
396                 return;
397         }
398
399         /** Convert Data values */
400         if (pTpdu->dcs.codingScheme == SMS_CHARSET_7BIT) {
401                 MSG_LANG_INFO_S langInfo = {0,};
402
403                 langInfo.bSingleShift = false;
404                 langInfo.bLockingShift = false;
405
406                 msgInfo->dataSize = textCvt.convertGSM7bitToUTF8((unsigned char*)msgInfo->msgText, MAX_MSG_TEXT_LEN, (unsigned char*)pTpdu->userData.data, pTpdu->userData.length, &langInfo);
407         } else if (pTpdu->dcs.codingScheme == SMS_CHARSET_UCS2) {
408                 msgInfo->dataSize = textCvt.convertUCS2ToUTF8((unsigned char*)msgInfo->msgText, MAX_MSG_TEXT_LEN, (unsigned char*)pTpdu->userData.data, pTpdu->userData.length);
409         }
410 }
411
412
413 void SmsPluginEventHandler::convertDeliverTpduToMsginfo(const SMS_DELIVER_S *pTpdu, MSG_MESSAGE_INFO_S *msgInfo)
414 {
415         MSG_BEGIN();
416
417         /** Convert Type  values */
418         msgInfo->msgType.mainType = MSG_SMS_TYPE;
419         msgInfo->msgType.subType = convertMsgSubType(pTpdu->pid);
420
421         /** set folder id (temporary) */
422         msgInfo->folderId = MSG_INBOX_ID;
423
424         msgInfo->storageId = MSG_STORAGE_PHONE;
425
426         switch(pTpdu->dcs.msgClass)
427         {
428                 case SMS_MSG_CLASS_0:
429                         msgInfo->msgType.classType = MSG_CLASS_0;
430                         break;
431                 case SMS_MSG_CLASS_1:
432                         msgInfo->msgType.classType = MSG_CLASS_1;
433                         break;
434                 case SMS_MSG_CLASS_2:
435                         msgInfo->msgType.classType = MSG_CLASS_2;
436                         msgInfo->storageId = MSG_STORAGE_SIM;
437                         break;
438                 case SMS_MSG_CLASS_3:
439                         msgInfo->msgType.classType = MSG_CLASS_3;
440                         break;
441                 default:
442                         msgInfo->msgType.classType = MSG_CLASS_NONE;
443                         break;
444         }
445
446         if (pTpdu->dcs.bMWI) {
447                 msgInfo->msgType.subType = (pTpdu->dcs.indType + MSG_MWI_VOICE_SMS);
448         }
449
450         msgInfo->networkStatus = MSG_NETWORK_RECEIVED;
451         msgInfo->bRead = false;
452         msgInfo->bProtected = false;
453         msgInfo->priority = MSG_MESSAGE_PRIORITY_NORMAL;
454         msgInfo->direction = MSG_DIRECTION_TYPE_MT;
455         msgInfo->bTextSms = true;
456
457         memset(msgInfo->subject, 0x00, MAX_SUBJECT_LEN+1);
458
459         time_t rawtime = time(NULL);
460
461 /*** Comment below lines to save local UTC time..... (it could be used later.)
462
463         if (pTpdu->timeStamp.format == SMS_TIME_ABSOLUTE) {
464
465                 MSG_DEBUG("year : %d", pTpdu->timeStamp.time.absolute.year);
466                 MSG_DEBUG("month : %d", pTpdu->timeStamp.time.absolute.month);
467                 MSG_DEBUG("day : %d", pTpdu->timeStamp.time.absolute.day);
468                 MSG_DEBUG("hour : %d", pTpdu->timeStamp.time.absolute.hour);
469                 MSG_DEBUG("minute : %d", pTpdu->timeStamp.time.absolute.minute);
470                 MSG_DEBUG("second : %d", pTpdu->timeStamp.time.absolute.second);
471                 MSG_DEBUG("timezone : %d", pTpdu->timeStamp.time.absolute.timeZone);
472
473                 char displayTime[32];
474                 struct tm * timeTM;
475
476                 struct tm timeinfo;
477                 memset(&timeinfo, 0x00, sizeof(tm));
478
479                 timeinfo.tm_year = (pTpdu->timeStamp.time.absolute.year + 100);
480                 timeinfo.tm_mon = (pTpdu->timeStamp.time.absolute.month - 1);
481                 timeinfo.tm_mday = pTpdu->timeStamp.time.absolute.day;
482                 timeinfo.tm_hour = pTpdu->timeStamp.time.absolute.hour;
483                 timeinfo.tm_min = pTpdu->timeStamp.time.absolute.minute;
484                 timeinfo.tm_sec = pTpdu->timeStamp.time.absolute.second;
485                 timeinfo.tm_isdst = 0;
486
487                 rawtime = mktime(&timeinfo);
488
489                 MSG_DEBUG("tzname[0] [%s]", tzname[0]);
490                 MSG_DEBUG("tzname[1] [%s]", tzname[1]);
491                 MSG_DEBUG("timezone [%d]", timezone);
492                 MSG_DEBUG("daylight [%d]", daylight);
493
494                 memset(displayTime, 0x00, sizeof(displayTime));
495                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", &timeinfo);
496                 MSG_DEBUG("displayTime [%s]", displayTime);
497
498                 rawtime -= (pTpdu->timeStamp.time.absolute.timeZone * (3600/4));
499
500                 timeTM = localtime(&rawtime);
501                 memset(displayTime, 0x00, sizeof(displayTime));
502                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
503                 MSG_DEBUG("displayTime [%s]", displayTime);
504
505                 rawtime -= timezone;
506
507                 timeTM = localtime(&rawtime);
508                 memset(displayTime, 0x00, sizeof(displayTime));
509                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
510                 MSG_DEBUG("displayTime [%s]", displayTime);
511         }
512
513 ***/
514
515         msgInfo->displayTime = rawtime;
516
517         /** Convert Address values */
518         msgInfo->nAddressCnt = 1;
519         msgInfo->addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
520         strncpy(msgInfo->addressList[0].addressVal, pTpdu->originAddress.address, MAX_ADDRESS_VAL_LEN);
521
522         msgInfo->msgPort.valid = false;
523         msgInfo->msgPort.dstPort = 0;
524         msgInfo->msgPort.srcPort = 0;
525
526         for (int i = 0; i < pTpdu->userData.headerCnt; i++) {
527                 /** Convert UDH values - Port Number */
528                 if (pTpdu->userData.header[i].udhType == SMS_UDH_APP_PORT_8BIT) {
529                         msgInfo->msgPort.valid = true;
530                         msgInfo->msgPort.dstPort = pTpdu->userData.header[i].udh.appPort8bit.destPort;
531                         msgInfo->msgPort.srcPort = pTpdu->userData.header[i].udh.appPort8bit.originPort;
532                 } else if (pTpdu->userData.header[i].udhType == SMS_UDH_APP_PORT_16BIT) {
533                         msgInfo->msgPort.valid = true;
534                         msgInfo->msgPort.dstPort = pTpdu->userData.header[i].udh.appPort16bit.destPort;
535                         msgInfo->msgPort.srcPort = pTpdu->userData.header[i].udh.appPort16bit.originPort;
536                 } else if (pTpdu->userData.header[i].udhType == SMS_UDH_SPECIAL_SMS) {
537                         msgInfo->msgType.subType = (pTpdu->userData.header[i].udh.specialInd.msgInd+MSG_MWI_VOICE_SMS);
538                         msgInfo->bStore = pTpdu->userData.header[i].udh.specialInd.bStore;
539
540                         if (pTpdu->dcs.codingGroup == SMS_GROUP_DISCARD)
541                                 msgInfo->bStore = false;
542
543                         MSG_DEBUG("Message waiting number : [%d]", pTpdu->userData.header[i].udh.specialInd.waitMsgNum);
544                         SmsPluginSetting::instance()->setMwiInfo(msgInfo->msgType.subType, pTpdu->userData.header[i].udh.specialInd.waitMsgNum);
545
546                         if (pTpdu->userData.length == 0) {
547                                 switch (msgInfo->msgType.subType) {
548                                 case MSG_MWI_VOICE_SMS :
549                                         sprintf(msgInfo->msgText, "%d new voice message", pTpdu->userData.header[i].udh.specialInd.waitMsgNum);
550                                         break;
551                                 case MSG_MWI_FAX_SMS :
552                                         sprintf(msgInfo->msgText, "%d new fax message", pTpdu->userData.header[i].udh.specialInd.waitMsgNum);
553                                         break;
554                                 case MSG_MWI_EMAIL_SMS :
555                                         sprintf(msgInfo->msgText, "%d new email message", pTpdu->userData.header[i].udh.specialInd.waitMsgNum);
556                                         break;
557                                 default :
558                                         sprintf(msgInfo->msgText, "%d new special message", pTpdu->userData.header[i].udh.specialInd.waitMsgNum);
559                                         break;
560                                 }
561                                 msgInfo->dataSize = strlen(msgInfo->msgText);
562                                 return;
563                         }
564                 } else if (pTpdu->userData.header[i].udhType == SMS_UDH_ALTERNATE_REPLY_ADDRESS) {
565                         strncpy(msgInfo->addressList[0].addressVal, pTpdu->userData.header[i].udh.alternateAddress.address, MAX_ADDRESS_VAL_LEN);
566                 }
567         }
568
569         /**length 0 - no user data - msg should be received */
570         if (pTpdu->userData.length <= 0) {
571                 memset(msgInfo->msgText, 0x00, sizeof(msgInfo->msgText));
572                 msgInfo->dataSize = 0;
573
574                 switch(pTpdu->dcs.codingScheme)
575                 {
576                         case SMS_CHARSET_7BIT:
577                                 msgInfo->encodeType = MSG_ENCODE_GSM7BIT;
578                                 break;
579                         case SMS_CHARSET_8BIT:
580                                 msgInfo->encodeType = MSG_ENCODE_8BIT;
581                                 break;
582                         case SMS_CHARSET_UCS2:
583                                 msgInfo->encodeType = MSG_ENCODE_UCS2;
584                                 break;
585                         default:
586                                 msgInfo->encodeType = MSG_ENCODE_8BIT;
587                                 break;
588                 }
589
590                 return;
591         } else if (pTpdu->userData.length > MAX_MSG_TEXT_LEN) {
592                 sprintf(msgInfo->msgText, "[Broken Message]");
593                 msgInfo->dataSize = strlen(msgInfo->msgData);
594                 return;
595         }
596
597         /** Convert Data values */
598         if (pTpdu->dcs.codingScheme == SMS_CHARSET_7BIT) {
599                 MSG_LANG_INFO_S langInfo = {0,};
600
601                 langInfo.bSingleShift = false;
602                 langInfo.bLockingShift = false;
603
604                 for (int i = 0; i < pTpdu->userData.headerCnt; i++)     {
605                         if (pTpdu->userData.header[i].udhType == SMS_UDH_SINGLE_SHIFT) {
606                                 langInfo.bSingleShift = true;
607                                 langInfo.singleLang = pTpdu->userData.header[i].udh.singleShift.langId;
608                         } else if (pTpdu->userData.header[i].udhType == SMS_UDH_LOCKING_SHIFT) {
609                                 langInfo.bLockingShift = true;
610                                 langInfo.lockingLang = pTpdu->userData.header[i].udh.lockingShift.langId;
611                         }
612                 }
613
614                 msgInfo->encodeType = MSG_ENCODE_GSM7BIT;
615                 msgInfo->dataSize = textCvt.convertGSM7bitToUTF8((unsigned char*)msgInfo->msgText, MAX_MSG_TEXT_LEN, (unsigned char*)pTpdu->userData.data, pTpdu->userData.length, &langInfo);
616         } else if (pTpdu->dcs.codingScheme == SMS_CHARSET_8BIT) {
617                 msgInfo->encodeType = MSG_ENCODE_8BIT;
618                 memcpy(msgInfo->msgText, pTpdu->userData.data, sizeof(pTpdu->userData.data));
619                 msgInfo->dataSize = pTpdu->userData.length;
620         } else if (pTpdu->dcs.codingScheme == SMS_CHARSET_UCS2) {
621                 msgInfo->encodeType = MSG_ENCODE_UCS2;
622                 msgInfo->dataSize = textCvt.convertUCS2ToUTF8((unsigned char*)msgInfo->msgText, MAX_MSG_TEXT_LEN, (unsigned char*)pTpdu->userData.data, pTpdu->userData.length);
623         } else if (pTpdu->dcs.codingScheme == SMS_CHARSET_EUCKR) {
624                 msgInfo->encodeType = MSG_ENCODE_8BIT;
625                 msgInfo->dataSize = textCvt.convertEUCKRToUTF8((unsigned char*)msgInfo->msgText, MAX_MSG_TEXT_LEN, (unsigned char*)pTpdu->userData.data, pTpdu->userData.length);
626                 return;
627         }
628
629         MSG_END();
630 }
631
632
633 void SmsPluginEventHandler::convertStatusRepTpduToMsginfo(const SMS_STATUS_REPORT_S *pTpdu, MSG_MESSAGE_INFO_S *msgInfo)
634 {
635         /** Convert Type  values */
636         msgInfo->msgType.mainType = MSG_SMS_TYPE;
637         msgInfo->msgType.subType = MSG_STATUS_REPORT_SMS;
638
639         /** set folder id */
640         msgInfo->folderId = MSG_INBOX_ID;
641
642         /** set storage id */
643         msgInfo->storageId = MSG_STORAGE_PHONE;
644
645         switch(pTpdu->dcs.msgClass)
646         {
647                 case SMS_MSG_CLASS_0:
648                         msgInfo->msgType.classType = MSG_CLASS_0;
649                         break;
650                 case SMS_MSG_CLASS_1:
651                         msgInfo->msgType.classType = MSG_CLASS_1;
652                         break;
653                 case SMS_MSG_CLASS_2:
654                         msgInfo->msgType.classType = MSG_CLASS_2;
655                         break;
656                 case SMS_MSG_CLASS_3:
657                         msgInfo->msgType.classType = MSG_CLASS_3;
658                         break;
659                 default:
660                         msgInfo->msgType.classType = MSG_CLASS_NONE;
661                         break;
662         }
663
664         MSG_DEBUG("delivery status : [%d]", pTpdu->status);
665
666         if (pTpdu->status == SMS_STATUS_RECEIVE_SUCCESS) {
667                 msgInfo->networkStatus = MSG_NETWORK_DELIVER_SUCCESS;
668         } else {
669                 msgInfo->networkStatus = MSG_NETWORK_DELIVER_FAIL;
670         }
671
672         msgInfo->bRead = false;
673         msgInfo->bProtected = false;
674         msgInfo->priority = MSG_MESSAGE_PRIORITY_NORMAL;
675         msgInfo->direction = MSG_DIRECTION_TYPE_MT;
676         msgInfo->bTextSms = true;
677
678         memset(msgInfo->subject, 0x00, MAX_SUBJECT_LEN+1);
679
680         time_t rawtime = time(NULL);
681
682 /*** Comment below lines to save local UTC time..... (it could be used later.)
683
684         if (pTpdu->timeStamp.format == SMS_TIME_ABSOLUTE) {
685
686                 MSG_DEBUG("year : %d", pTpdu->timeStamp.time.absolute.year);
687                 MSG_DEBUG("month : %d", pTpdu->timeStamp.time.absolute.month);
688                 MSG_DEBUG("day : %d", pTpdu->timeStamp.time.absolute.day);
689                 MSG_DEBUG("hour : %d", pTpdu->timeStamp.time.absolute.hour);
690                 MSG_DEBUG("minute : %d", pTpdu->timeStamp.time.absolute.minute);
691                 MSG_DEBUG("second : %d", pTpdu->timeStamp.time.absolute.second);
692                 MSG_DEBUG("timezone : %d", pTpdu->timeStamp.time.absolute.timeZone);
693
694                 char displayTime[32];
695                 struct tm * timeTM;
696
697                 struct tm timeinfo;
698                 memset(&timeinfo, 0x00, sizeof(tm));
699
700                 timeinfo.tm_year = (pTpdu->timeStamp.time.absolute.year + 100);
701                 timeinfo.tm_mon = (pTpdu->timeStamp.time.absolute.month - 1);
702                 timeinfo.tm_mday = pTpdu->timeStamp.time.absolute.day;
703                 timeinfo.tm_hour = pTpdu->timeStamp.time.absolute.hour;
704                 timeinfo.tm_min = pTpdu->timeStamp.time.absolute.minute;
705                 timeinfo.tm_sec = pTpdu->timeStamp.time.absolute.second;
706                 timeinfo.tm_isdst = 0;
707
708                 rawtime = mktime(&timeinfo);
709
710                 MSG_DEBUG("tzname[0] [%s]", tzname[0]);
711                 MSG_DEBUG("tzname[1] [%s]", tzname[1]);
712                 MSG_DEBUG("timezone [%d]", timezone);
713                 MSG_DEBUG("daylight [%d]", daylight);
714
715                 memset(displayTime, 0x00, sizeof(displayTime));
716                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", &timeinfo);
717                 MSG_DEBUG("displayTime [%s]", displayTime);
718
719                 rawtime -= (pTpdu->timeStamp.time.absolute.timeZone * (3600/4));
720
721                 timeTM = localtime(&rawtime);
722                 memset(displayTime, 0x00, sizeof(displayTime));
723                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
724                 MSG_DEBUG("displayTime [%s]", displayTime);
725
726                 rawtime -= timezone;
727
728                 timeTM = localtime(&rawtime);
729                 memset(displayTime, 0x00, sizeof(displayTime));
730                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
731                 MSG_DEBUG("displayTime [%s]", displayTime);
732         }
733
734 ***/
735
736         msgInfo->displayTime = rawtime;
737
738         /** Convert Address values */
739         msgInfo->nAddressCnt = 1;
740         msgInfo->addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
741         strncpy(msgInfo->addressList[0].addressVal, pTpdu->recipAddress.address, MAX_ADDRESS_VAL_LEN);
742
743         msgInfo->msgPort.valid = false;
744         msgInfo->msgPort.dstPort = 0;
745         msgInfo->msgPort.srcPort = 0;
746
747         for (int i = 0; i < pTpdu->userData.headerCnt; i++) {
748                 /** Convert UDH values - Port Number */
749                 if (pTpdu->userData.header[i].udhType == SMS_UDH_APP_PORT_8BIT) {
750                         msgInfo->msgPort.valid = true;
751                         msgInfo->msgPort.dstPort = pTpdu->userData.header[i].udh.appPort8bit.destPort;
752                         msgInfo->msgPort.srcPort = pTpdu->userData.header[i].udh.appPort8bit.originPort;
753                 } else if (pTpdu->userData.header[i].udhType == SMS_UDH_APP_PORT_16BIT) {
754                         msgInfo->msgPort.valid = true;
755                         msgInfo->msgPort.dstPort = pTpdu->userData.header[i].udh.appPort16bit.destPort;
756                         msgInfo->msgPort.srcPort = pTpdu->userData.header[i].udh.appPort16bit.originPort;
757                 }
758         }
759
760         memset(msgInfo->msgText, 0x00, sizeof(msgInfo->msgText));
761         msgInfo->dataSize = 0;
762
763         if (pTpdu->status <= SMS_STATUS_SMSC_SPECIFIC_LAST) {
764                 strncpy(msgInfo->msgText, "Message delivered.", MAX_MSG_TEXT_LEN);
765                 msgInfo->dataSize = strlen(msgInfo->msgText);
766         } else if (pTpdu->status == SMS_STATUS_TEMP_SERVICE_REJECTED) {
767                 strncpy(msgInfo->msgText, "Message delivery rejected.", MAX_MSG_TEXT_LEN);
768                 msgInfo->dataSize = strlen(msgInfo->msgText);
769         } else if (pTpdu->status == SMS_STATUS_PERM_MSG_VAL_PERIOD_EXPIRED) {
770                 strncpy(msgInfo->msgText, "Message delivery expired.", MAX_MSG_TEXT_LEN);
771                 msgInfo->dataSize = strlen(msgInfo->msgText);
772         } else {
773                 strncpy(msgInfo->msgText, "Message delivery failed.", MAX_MSG_TEXT_LEN);
774                 msgInfo->dataSize = strlen(msgInfo->msgText);
775         }
776 }
777
778
779 MSG_SUB_TYPE_T SmsPluginEventHandler::convertMsgSubType(SMS_PID_T pid)
780 {
781         switch (pid)
782         {
783                 case SMS_PID_TYPE0 :
784                         return MSG_TYPE0_SMS;
785                 case SMS_PID_REPLACE_TYPE1 :
786                         return MSG_REPLACE_TYPE1_SMS;
787                 case SMS_PID_REPLACE_TYPE2 :
788                         return MSG_REPLACE_TYPE2_SMS;
789                 case SMS_PID_REPLACE_TYPE3 :
790                         return MSG_REPLACE_TYPE3_SMS;
791                 case SMS_PID_REPLACE_TYPE4 :
792                         return MSG_REPLACE_TYPE4_SMS;
793                 case SMS_PID_REPLACE_TYPE5 :
794                         return MSG_REPLACE_TYPE5_SMS;
795                 case SMS_PID_REPLACE_TYPE6 :
796                         return MSG_REPLACE_TYPE6_SMS;
797                 case SMS_PID_REPLACE_TYPE7 :
798                         return MSG_REPLACE_TYPE7_SMS;
799                 case SMS_PID_RETURN_CALL :
800                         return MSG_MWI_OTHER_SMS;
801                 default :
802                         return MSG_NORMAL_SMS;
803         }
804
805 }
806
807
808 void SmsPluginEventHandler::SetSentInfo(SMS_SENT_INFO_S *pSentInfo)
809 {
810         memset(&sentInfo, 0x00, sizeof(SMS_SENT_INFO_S));
811         memcpy(&sentInfo, pSentInfo, sizeof(SMS_SENT_INFO_S));
812
813         MSG_DEBUG("sentInfo.reqId : %d", sentInfo.reqInfo.reqId);
814         MSG_DEBUG("sentInfo.bLast : %d", sentInfo.bLast);
815 }
816
817
818 void SmsPluginEventHandler::setDeviceStatus()
819 {
820         mx.lock();
821         devStatus = true;
822         cv.signal();
823         mx.unlock();
824 }
825
826
827 bool SmsPluginEventHandler::getDeviceStatus()
828 {
829         int ret = 0;
830
831         mx.lock();
832
833         ret = cv.timedwait(mx.pMutex(), 16);
834
835         if (ret == ETIMEDOUT) {
836                 MSG_DEBUG("WARNING: DEVICE STATUS TIME-OUT");
837                 devStatus = false;
838         }
839         mx.unlock();
840         return devStatus;
841 }