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