Fix types for 64bit
[platform/core/messaging/msg-service.git] / proxy / MsgProxyListener.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 <errno.h>
18
19 #include "MsgDebug.h"
20 #include "MsgCppTypes.h"
21 #include "MsgException.h"
22 #include "MsgUtilFunction.h"
23 #include "MsgUtilFile.h"
24 #include "MsgProxyListener.h"
25 #include "MsgGconfWrapper.h"
26
27 void MsgServerRestartCb(keynode_t *key, void* data)
28 {
29         bool bReady = false;
30         MSG_DEBUG("Message Service Running State Changed");
31         // server is currently booting and service is not available until the end of booting
32         MsgSettingGetBool(VCONFKEY_MSG_SERVER_READY, &bReady);
33         MSG_INFO("Message Service Running State Changed bReady:(%d)", bReady);
34
35         //bReady false indicates that server has restarted. Hence the proxylistener needs to be reset
36         if (bReady == false) {
37                 MSG_DEBUG("Message Service Is Restarted. Resetting ProxyListener");
38                 MsgProxyListener::instance()->resetProxyListener();
39         }
40 }
41
42 gboolean readSocket(GIOChannel *source, GIOCondition condition, gpointer data)
43 {
44         MSG_BEGIN();
45 #if 0
46         if ((G_IO_ERR & condition) || (G_IO_HUP & condition) || (G_IO_NVAL & condition))
47         {
48                 MSG_DEBUG("IO condition Error!!! [%d]", condition);
49
50                 MsgProxyListener::instance()->stop();
51                 return FALSE;
52         }
53 #endif
54         if (G_IO_ERR & condition)
55         {
56                 MSG_ERR("IO Error!!! [%d]", condition);
57
58                 MsgProxyListener::instance()->stop();
59                 return FALSE;
60         }
61
62         if (G_IO_HUP & condition)
63         {
64                 MSG_ERR("socket fd Error!!! [%d]", condition);
65
66                 MsgProxyListener::instance()->stop();
67                 return FALSE;
68         }
69
70         if (G_IO_NVAL & condition)
71         {
72                 MSG_ERR("Invaild socket Error!!! [%d]", condition);
73
74                 MsgProxyListener::instance()->stop();
75                 return FALSE;
76         }
77
78         char* buf = NULL;
79         unique_ptr<char*, void(*)(char**)> eventBuf(&buf, unique_ptr_deleter);
80         unsigned int len = 0;
81
82         int n = MsgProxyListener::instance()->readFromSocket(&buf, &len);
83
84         if (n > 0)
85         {
86                 MSG_DEBUG(">>Receiving %d bytes", n);
87                 MsgProxyListener::instance()->handleEvent((MSG_EVENT_S*)buf);
88         }
89         else if (n == 0)
90         {
91                 MSG_WARN("Server closed connection");
92                 MsgProxyListener::instance()->stop();
93                 return FALSE;
94         }
95         else // dataSize < 0
96         {
97                 MSG_DEBUG("Data is not for Listener");
98         }
99
100         MSG_END();
101
102         return TRUE;
103 }
104
105
106 /*==================================================================================================
107                                      IMPLEMENTATION OF MsgListenerThread - Member Functions
108 ==================================================================================================*/
109 MsgProxyListener* MsgProxyListener::pInstance = NULL;
110
111
112 MsgProxyListener::MsgProxyListener() : running(0)
113 {
114         sentStatusCBList.clear();
115         newMessageCBList.clear();
116         newMMSConfMessageCBList.clear();
117         newSyncMLMessageCBList.clear();
118         newLBSMessageCBList.clear();
119         openHandleSet.clear();
120         MsgSettingRegVconfCBCommon(VCONFKEY_MSG_SERVER_READY, MsgServerRestartCb);
121
122         channel = NULL;
123         eventSourceId = 0;
124 }
125
126
127 MsgProxyListener::~MsgProxyListener()
128 {
129         sentStatusCBList.clear();
130         newMessageCBList.clear();
131         newMMSConfMessageCBList.clear();
132         newSyncMLMessageCBList.clear();
133         newLBSMessageCBList.clear();
134         openHandleSet.clear();
135         MsgSettingRemoveVconfCBCommon(VCONFKEY_MSG_SERVER_READY, MsgServerRestartCb);
136 }
137
138
139 MsgProxyListener* MsgProxyListener::instance()
140 {
141         static Mutex mm;
142         MutexLocker lock(mm);
143
144         if (!pInstance) {
145                 pInstance = new MsgProxyListener();
146         }
147
148         return pInstance;
149 }
150
151
152 void MsgProxyListener::start(MsgHandle* pMsgHandle)
153 {
154         MutexLocker lock(mx);
155
156         handle_set::iterator it = openHandleSet.find(pMsgHandle);
157         if (it == openHandleSet.end())
158                 openHandleSet.insert(pMsgHandle);
159         if (running == 0)
160         {
161                 cliSock.connect(MSG_SOCKET_PATH);
162                 cv.signal(); // wake up the waiting thread
163
164                 int fd = cliSock.fd();
165
166                 MSG_DEBUG("Socket Fd : %d", fd);
167
168                 channel = g_io_channel_unix_new(fd); // initializes ref_count = 1
169
170                 eventSourceId = g_io_add_watch(channel, (GIOCondition)(G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL), &readSocket, NULL); // increments ref_count =2
171
172                 MSG_DEBUG("Call g_io_add_watch() : %d", eventSourceId);
173         }
174
175         running++;
176         MSG_DEBUG("add Listener and [%d] are running.", running);
177 }
178
179
180 void MsgProxyListener::stop()
181 {
182         MSG_BEGIN();
183
184         if (running > 1)
185         {
186                 running--;
187                 MSG_DEBUG("There are still running Listener. [%d] left.", running);
188         }
189         else if (running == 1)
190         {
191                 running--;
192
193                 g_io_channel_unref(channel); // decrements ref_count = 1
194
195                 g_source_remove(eventSourceId);
196
197                 cliSock.close();
198
199                 MSG_DEBUG("client Listener is terminated.");
200         }
201
202         MSG_END();
203 }
204
205
206 bool MsgProxyListener::regSentStatusEventCB(MsgHandle* pMsgHandle, msg_sent_status_cb pfSentStatus, void *pUserParam)
207 {
208         MutexLocker lock(mx);
209
210         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
211
212         for (; it != sentStatusCBList.end(); it++)
213         {
214                 if (it->hAddr == pMsgHandle && it->pfSentStatusCB == pfSentStatus) {
215                         MSG_DEBUG("msg_sent_status_cb() callback : [%p] is already registered!!!", pfSentStatus);
216                         return false;
217                 }
218         }
219
220         MSG_SENT_STATUS_CB_ITEM_S sentStatusCB = {pMsgHandle, pfSentStatus, pUserParam};
221
222         sentStatusCBList.push_back(sentStatusCB);
223
224         return true;
225 }
226
227
228 bool MsgProxyListener::regMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_sms_incoming_cb pfNewMessage, int port, void *pUserParam)
229 {
230         MutexLocker lock(mx);
231
232         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it = newMessageCBList.begin();
233
234         for (; it != newMessageCBList.end(); it++)
235         {
236                 if (it->hAddr == pMsgHandle && it->port == port && it->pfIncomingCB == pfNewMessage) {
237                         MSG_DEBUG("msg_sms_incoming_cb() callback : Port Number [%d] is already registered!!!", port);
238                         return false;
239                 }
240         }
241
242         MSG_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, pfNewMessage, port, pUserParam};
243
244         newMessageCBList.push_back(incomingCB);
245
246         return true;
247 }
248
249
250 bool MsgProxyListener::regMMSConfMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_mms_conf_msg_incoming_cb pfNewMMSConfMessage, const char *pAppId, void *pUserParam)
251 {
252         MutexLocker lock(mx);
253
254         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it = newMMSConfMessageCBList.begin();
255
256         for (; it != newMMSConfMessageCBList.end(); it++)
257         {
258                 if (it->hAddr == pMsgHandle && it->pfMMSConfIncomingCB == pfNewMMSConfMessage) {
259
260                         if(pAppId == NULL) {
261                                 MSG_DEBUG("msg_mms_conf_msg_incoming_cb() callback is already registered!!!");
262                                 return false;
263                         } else if(!strncmp(it->appId, pAppId, MAX_MMS_JAVA_APPID_LEN)) {
264                                 MSG_DEBUG("msg_mms_conf_msg_incoming_cb() callback : AppId [%s] is already registered!!!", pAppId);
265                                 return false;
266                         }
267                 }
268         }
269
270         MSG_MMS_CONF_INCOMING_CB_ITEM_S incomingConfCB = {pMsgHandle, pfNewMMSConfMessage, {0}, pUserParam};
271
272         if (pAppId != NULL)
273                 strncpy(incomingConfCB.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
274
275         newMMSConfMessageCBList.push_back(incomingConfCB);
276
277         return true;
278 }
279
280
281 bool MsgProxyListener::regPushMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_push_msg_incoming_cb pfNewPushMessage, const char *pAppId, void *pUserParam)
282 {
283         MutexLocker lock(mx);
284
285         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it = newPushMessageCBList.begin();
286
287         for (; it != newPushMessageCBList.end(); it++)
288         {
289                 if (it->hAddr == pMsgHandle && it->pfPushIncomingCB == pfNewPushMessage) {
290
291                         if(pAppId == NULL) {
292                                 MSG_DEBUG("msg_push_msg_incoming_cb() callback is already registered!!!");
293                                 return false;
294                         } else if(!strncmp(it->appId, pAppId, MAX_WAPPUSH_ID_LEN)) {
295                                 MSG_DEBUG("msg_push_msg_incoming_cb() callback : AppId [%s] is already registered!!!", pAppId);
296                                 return false;
297                         }
298                 }
299         }
300
301         MSG_PUSH_INCOMING_CB_ITEM_S incomingPushCB = {pMsgHandle, pfNewPushMessage, {0}, pUserParam};
302
303         if (pAppId != NULL)
304                 strncpy(incomingPushCB.appId, pAppId, MAX_WAPPUSH_ID_LEN);
305
306         newPushMessageCBList.push_back(incomingPushCB);
307
308         return true;
309 }
310
311 bool MsgProxyListener::regCBMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_cb_incoming_cb pfNewCBMessage, bool bSave, void *pUserParam)
312 {
313         MutexLocker lock(mx);
314
315         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it = newCBMessageCBList.begin();
316
317         for (; it != newCBMessageCBList.end(); it++)
318         {
319                 if (it->hAddr == pMsgHandle && it->pfCBIncomingCB == pfNewCBMessage) {
320                         MSG_DEBUG("msg_CB_incoming_cb() callback : [%p] is already registered!!!", pfNewCBMessage);
321                          it->bsave = bSave;
322                          it->userParam = pUserParam;
323                         return false;
324                 }
325         }
326
327         MSG_CB_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, pfNewCBMessage, bSave, pUserParam};
328
329         newCBMessageCBList.push_back(incomingCB);
330
331         return true;
332 }
333
334
335 bool MsgProxyListener::regReportMsgIncomingCB(MsgHandle* pMsgHandle, msg_report_msg_incoming_cb pfReportMessage, void *pUserParam)
336 {
337         MutexLocker lock(mx);
338
339         std::list<MSG_REPORT_INCOMING_CB_ITEM_S>::iterator it = reportMessageCBList.begin();
340
341         for (; it != reportMessageCBList.end(); it++)
342         {
343                 if (it->hAddr == pMsgHandle && it->pfReportMsgIncomingCB == pfReportMessage) {
344                         MSG_DEBUG("msg_report_msg_incoming_cb() callback : [%p] is already registered!!!", pfReportMessage);
345                          it->userParam = pUserParam;
346                         return false;
347                 }
348         }
349
350         MSG_REPORT_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, pfReportMessage, pUserParam};
351
352         reportMessageCBList.push_back(incomingCB);
353
354         return true;
355 }
356
357
358 bool MsgProxyListener::regSyncMLMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_syncml_msg_incoming_cb pfNewSyncMLMessage, void *pUserParam)
359 {
360         MutexLocker lock(mx);
361
362         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it = newSyncMLMessageCBList.begin();
363
364         for (; it != newSyncMLMessageCBList.end(); it++)
365         {
366                 if (it->hAddr == pMsgHandle && it->pfSyncMLIncomingCB == pfNewSyncMLMessage) {
367                         MSG_DEBUG("msg_syncml_msg_incoming_cb() callback : [%p] is already registered!!!", pfNewSyncMLMessage);
368                         return false;
369                 }
370         }
371
372         MSG_SYNCML_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, pfNewSyncMLMessage, pUserParam};
373
374         newSyncMLMessageCBList.push_back(incomingCB);
375
376         return true;
377 }
378
379
380 bool MsgProxyListener::regLBSMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_lbs_msg_incoming_cb pfNewLBSMsgIncoming, void *pUserParam)
381 {
382         MutexLocker lock(mx);
383
384         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it = newLBSMessageCBList.begin();
385
386         for (; it != newLBSMessageCBList.end(); it++)
387         {
388                 if (it->hAddr == pMsgHandle && it->pfLBSMsgIncoming == pfNewLBSMsgIncoming) {
389                         MSG_DEBUG("msg_lbs_msg_incoming_cb() callback : [%p] is already registered!!!", pfNewLBSMsgIncoming);
390                         return false;
391                 }
392         }
393
394         MSG_LBS_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, pfNewLBSMsgIncoming, pUserParam};
395
396         newLBSMessageCBList.push_back(incomingCB);
397
398         return true;
399 }
400
401
402 bool MsgProxyListener::regSyncMLMessageOperationEventCB(MsgHandle* pMsgHandle, msg_syncml_msg_operation_cb pfSyncMLMessageOperation, void *pUserParam)
403 {
404         MutexLocker lock(mx);
405
406         std::list<MSG_SYNCML_OPERATION_CB_ITEM_S>::iterator it = operationSyncMLMessageCBList.begin();
407
408         for (; it != operationSyncMLMessageCBList.end(); it++)
409         {
410                 if (it->hAddr == pMsgHandle && it->pfSyncMLOperationCB == pfSyncMLMessageOperation) {
411                         MSG_DEBUG("msg_syncml_msg_incoming_cb() callback : [%p] is already registered!!!", pfSyncMLMessageOperation);
412                         return false;
413                 }
414         }
415
416         MSG_SYNCML_OPERATION_CB_ITEM_S incomingCB = {pMsgHandle, pfSyncMLMessageOperation, pUserParam};
417
418         operationSyncMLMessageCBList.push_back(incomingCB);
419
420         return true;
421 }
422
423
424 bool MsgProxyListener::regStorageChangeEventCB(MsgHandle* pMsgHandle, msg_storage_change_cb pfStorageChangeOperation, void *pUserParam)
425 {
426         MutexLocker lock(mx);
427
428         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it = storageChangeCBList.begin();
429
430         for (; it != storageChangeCBList.end(); it++)
431         {
432                 if (it->hAddr == pMsgHandle && it->pfStorageChangeCB == pfStorageChangeOperation) {
433                         MSG_DEBUG("msg_storage_change_cb() callback : [%p] is already registered!!!", pfStorageChangeOperation);
434                         return false;
435                 }
436         }
437
438         MSG_STORAGE_CHANGE_CB_ITEM_S changeCB = {pMsgHandle, pfStorageChangeOperation, pUserParam};
439
440         storageChangeCBList.push_back(changeCB);
441
442         return true;
443 }
444
445
446 void MsgProxyListener::clearListOfClosedHandle(MsgHandle* pMsgHandle)
447 {
448         MSG_BEGIN();
449
450         MutexLocker lock(mx);
451
452         // sent status CB list
453         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
454
455         for (; it != sentStatusCBList.end(); )
456         {
457                 if (it->hAddr == pMsgHandle)
458                 {
459                         sentStatusCBList.erase(it++);
460
461                         //Stop client Listener
462                         stop();
463                 }
464                 else
465                         ++it;
466         }
467
468         // new message CB list
469         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it2 = newMessageCBList.begin();
470
471         for (; it2 != newMessageCBList.end(); )
472         {
473                 if (it2->hAddr == pMsgHandle)
474                 {
475                         newMessageCBList.erase(it2++);
476
477                         //Stop client Listener
478                         stop();
479                 }
480                 else
481                         ++it2;
482         }
483
484         // MMS conf Message CB list
485         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it3 = newMMSConfMessageCBList.begin();
486
487         for (; it3 != newMMSConfMessageCBList.end(); )
488         {
489                 if (it3->hAddr == pMsgHandle)
490                 {
491                         newMMSConfMessageCBList.erase(it3++);
492
493                         //Stop client Listener
494                         stop();
495                 }
496                 else
497                         ++it3;
498         }
499
500         // SyncML Message CB list
501         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it4 = newSyncMLMessageCBList.begin();
502
503         for (; it4 != newSyncMLMessageCBList.end(); )
504         {
505                 if (it4->hAddr == pMsgHandle)
506                 {
507                         newSyncMLMessageCBList.erase(it4++);
508
509                         //Stop client Listener
510                         stop();
511                 }
512                 else
513                         ++it4;
514         }
515
516         // LBS Message CB list
517         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it5 = newLBSMessageCBList.begin();
518
519         for (; it5 != newLBSMessageCBList.end(); )
520         {
521                 if (it5->hAddr == pMsgHandle)
522                 {
523                         newLBSMessageCBList.erase(it5++);
524
525                         //Stop client Listener
526                         stop();
527                 }
528                 else
529                         ++it5;
530         }
531
532         // Push Message CB list
533         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it6 = newPushMessageCBList.begin();
534
535         for (; it6 != newPushMessageCBList.end(); )
536         {
537                 if (it6->hAddr == pMsgHandle)
538                 {
539                         newPushMessageCBList.erase(it6++);
540
541                         //Stop client Listener
542                         stop();
543                 }
544                 else
545                         ++it6;
546
547         }
548
549         // CB Message CB list
550         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it7 = newCBMessageCBList.begin();
551
552         for (; it7 != newCBMessageCBList.end(); )
553         {
554
555                 if (it7->hAddr == pMsgHandle)
556                 {
557
558                         newCBMessageCBList.erase(it7++);
559
560                         //Stop client Listener
561                         stop();
562                 }
563                 else
564                 {
565                         ++it7;
566                 }
567         }
568
569         // Storage change Message CB list
570         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it8 = storageChangeCBList.begin();
571
572         for (; it8 != storageChangeCBList.end(); )
573         {
574                 if (it8->hAddr == pMsgHandle)
575                 {
576                         storageChangeCBList.erase(it8++);
577
578                         //Stop client Listener
579                         stop();
580                 }
581                 else
582                         ++it8;
583         }
584
585
586         // Report message incoming CB list
587         std::list<MSG_REPORT_INCOMING_CB_ITEM_S>::iterator it9 = reportMessageCBList.begin();
588         for (; it9 != reportMessageCBList.end(); )
589         {
590                 if (it9->hAddr == pMsgHandle)
591                 {
592                         reportMessageCBList.erase(it9++);
593
594                         //Stop client Listener
595                         stop();
596                 }
597                 else
598                         ++it9;
599         }
600
601
602         MSG_END();
603 }
604
605 void MsgProxyListener::handleEvent(const MSG_EVENT_S* pMsgEvent)
606 {
607         MSG_BEGIN();
608
609         if (!pMsgEvent)
610                 THROW(MsgException::INVALID_PARAM, "pMsgEvent is NULL");
611
612         if (pMsgEvent->eventType == MSG_EVENT_PLG_SENT_STATUS_CNF)
613         {
614                 unsigned int chInfo[3] = {0}; //3// reqid, status, object
615
616                 memcpy(&chInfo, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(chInfo));
617
618                 msg_struct_s status = {0,};
619                 MSG_SENT_STATUS_S statusData = {(msg_request_id_t)chInfo[0], (msg_network_status_t)chInfo[1]};
620
621                 status.type = MSG_STRUCT_SENT_STATUS_INFO;
622                 status.data = (void *)&statusData;
623
624                 mx.lock();
625
626                 MsgSentStatusCBList::iterator it = sentStatusCBList.begin();
627
628                 for( ; it != sentStatusCBList.end() ; it++)
629                 {
630                         MsgHandle* pHandle = it->hAddr;
631
632                         msg_sent_status_cb pfunc = it->pfSentStatusCB;
633
634                         void* param = it->userParam;
635
636                         pfunc((msg_handle_t)pHandle, (msg_struct_t)&status, param);
637                 }
638
639                 mx.unlock();
640         }
641         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MSG_IND )
642         {
643                 MSG_MESSAGE_INFO_S msgInfo;
644                 memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
645
646                 msgInfo.addressList = NULL;
647                 unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
648
649                 MsgDecodeMsgInfo((char *)pMsgEvent->data, &msgInfo);
650
651                 int portKey = (msgInfo.msgPort.valid)? msgInfo.msgPort.dstPort: 0;
652
653                 mx.lock();
654
655                 MsgNewMessageCBList::iterator it = newMessageCBList.begin();
656                 MsgNewMessageCBList matchList;
657
658                 for( ; it != newMessageCBList.end() ; it++)
659                 {
660                         if( portKey == it->port)
661                         {
662                                 matchList.push_back(*it);
663                         }
664                 }
665
666                 mx.unlock();
667
668                 it = matchList.begin();
669
670                 for( ; it != matchList.end(); it++ )
671                 {
672                         MsgHandle* pHandle = it->hAddr;
673
674                         MSG_MESSAGE_HIDDEN_S msgHidden = {0,};
675
676                         msgHidden.pData = NULL;
677                         msgHidden.pMmsData = NULL;
678
679                         /* Allocate memory for address list of message */
680                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
681
682                         addr_list->nCount = 0;
683                         addr_list->msg_struct_info = (msg_struct_t *)calloc(MAX_TO_ADDRESS_CNT, sizeof(MSG_ADDRESS_INFO_S *));
684                         if (addr_list->msg_struct_info == NULL)
685                                 continue;
686
687                         msg_struct_s *pTmp = NULL;
688
689                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
690                                 addr_list->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
691                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
692                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
693                                 pTmp->data = new MSG_ADDRESS_INFO_S;
694                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
695
696                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
697                         }
698
699                         msgHidden.addr_list = addr_list;
700
701                         try {
702                                 pHandle->convertMsgStruct(&msgInfo, &msgHidden);
703                         }
704                         catch (MsgException& e) {
705                                 MSG_FATAL("%s", e.what());
706                         }
707
708                         msg_struct_s msg = {0,};
709                         msg.type = MSG_STRUCT_MESSAGE_INFO;
710                         msg.data = &msgHidden;
711
712                         msg_sms_incoming_cb pfunc = it->pfIncomingCB;
713
714                         void* param = it->userParam;
715
716                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
717
718                         delete [] (char*)msgHidden.pData;
719                         if (msgHidden.pMmsData != NULL)
720                                 delete [] (char*)msgHidden.pMmsData;
721
722                         // address Memory Free
723                         if (msgHidden.addr_list!= NULL)
724                         {
725                                 for(int i=0; i<MAX_TO_ADDRESS_CNT; i++) {
726                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
727                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
728                                         addrInfo->data = NULL;
729                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
730                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
731                                 }
732
733                                 g_free(msgHidden.addr_list->msg_struct_info);
734
735                                 delete msgHidden.addr_list;
736                                 msgHidden.addr_list = NULL;
737                         }
738
739                 }
740
741         }
742         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MMS_CONF )
743         {
744                 MSG_MESSAGE_INFO_S msgInfo;
745                 memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
746
747                 msgInfo.addressList = NULL;
748                 unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
749
750                 MsgDecodeMsgInfo((char *)pMsgEvent->data, &msgInfo);
751
752
753                 MMS_RECV_DATA_S* pMmsRecvData = ( MMS_RECV_DATA_S*)msgInfo.msgData;
754
755                 char* appIdKey = (pMmsRecvData->msgAppId.valid)? pMmsRecvData->msgAppId.appId: NULL;
756
757                 mx.lock();
758
759                 MsgNewMMSConfMessageCBList::iterator it = newMMSConfMessageCBList.begin();
760                 MsgNewMMSConfMessageCBList matchList;
761
762                 for( ; it != newMMSConfMessageCBList.end() ; it++)
763                 {
764                         if(appIdKey)
765                         {
766                                 if(!strcmp(appIdKey, it->appId))
767                                         matchList.push_back(*it);
768                         }
769                         else//(appIdKey == NULL && it->appId[0] == 0)
770                         {
771                                 if(it->appId[0] == 0)
772                                         matchList.push_back(*it);
773                         }
774                 }
775
776                 mx.unlock();
777
778                 // Contents of msgData removed and replaced to retrievedFilePath for convertMsgStruct
779                 // it is moved from UpdateMessage in MmsPluginStorage.cpp
780                 char tempFileName[MSG_FILENAME_LEN_MAX+1] = {0};  // check MSG_FILENAME_LEN_MAX
781
782                 strncpy(tempFileName, pMmsRecvData->retrievedFilePath, MSG_FILENAME_LEN_MAX);
783
784                 memset(msgInfo.msgData, 0, MAX_MSG_DATA_LEN+1);
785                 memcpy(msgInfo.msgData, tempFileName + strlen(MSG_DATA_PATH), MAX_MSG_DATA_LEN);
786
787                 it = matchList.begin();
788
789                 for( ; it != matchList.end() ; it++)
790                 {
791                         MsgHandle* pHandle = it->hAddr;
792
793                         MSG_MESSAGE_HIDDEN_S msgHidden = {0,};
794
795                         msgHidden.pData = NULL;
796                         msgHidden.pMmsData = NULL;
797
798                         /* Allocate memory for address list of message */
799                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
800
801                         addr_list->nCount = 0;
802                         addr_list->msg_struct_info = (msg_struct_t *)calloc(MAX_TO_ADDRESS_CNT, sizeof(MSG_ADDRESS_INFO_S *));
803                         if (addr_list->msg_struct_info == NULL)
804                                 continue;
805
806                         msg_struct_s *pTmp = NULL;
807
808                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
809                                 addr_list->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
810                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
811                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
812                                 pTmp->data = new MSG_ADDRESS_INFO_S;
813                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
814
815                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
816                         }
817
818                         msgHidden.addr_list = addr_list;
819
820                         try {
821                                 pHandle->convertMsgStruct(&msgInfo, &msgHidden);
822                         }
823                         catch (MsgException& e) {
824                                 MSG_FATAL("%s", e.what());
825                         }
826
827                         msg_struct_s msg = {0,};
828                         msg.type = MSG_STRUCT_MESSAGE_INFO;
829                         msg.data = &msgHidden;
830
831                         msg_mms_conf_msg_incoming_cb pfunc = it->pfMMSConfIncomingCB;
832
833                         void* param = it->userParam;
834                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
835
836                         delete [] (char*)msgHidden.pData;
837                         if (msgHidden.pMmsData != NULL)
838                                 delete [] (char*)msgHidden.pMmsData;
839
840                         // address Memory Free
841                         if (msgHidden.addr_list != NULL)
842                         {
843                                 for(int i=0; i<MAX_TO_ADDRESS_CNT; i++) {
844                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
845                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
846                                         addrInfo->data = NULL;
847                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
848                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
849                                 }
850
851                                 g_free(msgHidden.addr_list->msg_struct_info);
852
853                                 delete msgHidden.addr_list;
854                                 msgHidden.addr_list = NULL;
855                         }
856
857                         // Here the retrieved message will be deleted from native storage.
858                         // as of now, msg which have appId is considered as JAVA MMS message and it will be sent and handled in JAVA app.
859                         if(appIdKey)
860                         {
861                                 MSG_DEBUG("delete received JAVA MMS message:%s from native storage",tempFileName);
862                                 pHandle->deleteMessage(msgInfo.msgId);
863                         }
864                 }
865         }
866         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND )
867         {
868                 MSG_SYNCML_MESSAGE_DATA_S* pSyncMLData = (MSG_SYNCML_MESSAGE_DATA_S *)pMsgEvent->data;
869
870                 MSG_DEBUG("msgType [%d]", pSyncMLData->syncmlType);
871
872                 mx.lock();
873
874                 MsgNewSyncMLMessageCBList::iterator it = newSyncMLMessageCBList.begin();
875
876                 for( ; it != newSyncMLMessageCBList.end() ; it++)
877                 {
878                         MsgHandle* pHandle = it->hAddr;
879
880                         msg_syncml_msg_incoming_cb pfunc = it->pfSyncMLIncomingCB;
881
882                         void* param = it->userParam;
883
884                         pfunc((msg_handle_t)pHandle, pSyncMLData->syncmlType, pSyncMLData->pushBody, pSyncMLData->pushBodyLen, pSyncMLData->wspHeader, pSyncMLData->wspHeaderLen, pSyncMLData->simIndex, param);
885                 }
886
887                 mx.unlock();
888         }
889         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_LBS_MSG_IND )
890         {
891                 MSG_LBS_MESSAGE_DATA_S* pLBSData = (MSG_LBS_MESSAGE_DATA_S *)pMsgEvent->data;
892
893                 mx.lock();
894
895                 MsgNewLBSMessageCBList::iterator it = newLBSMessageCBList.begin();
896
897                 for( ; it != newLBSMessageCBList.end() ; it++)
898                 {
899                         MsgHandle* pHandle = it->hAddr;
900
901                         msg_lbs_msg_incoming_cb pfunc = it->pfLBSMsgIncoming;
902
903                         void* param = it->userParam;
904
905                         pfunc((msg_handle_t)pHandle, pLBSData->pushHeader, pLBSData->pushBody, pLBSData->pushBodyLen, param);
906                 }
907
908                 mx.unlock();
909         }
910         else if ( pMsgEvent->eventType == MSG_EVENT_SYNCML_OPERATION )
911         {
912                 int msgId;
913                 int extId;
914
915                 memcpy(&msgId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(int));
916                 memcpy(&extId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(int)), sizeof(int));
917
918                 MSG_DEBUG("msgId [%d]", msgId);
919                 MSG_DEBUG("extId [%d]", extId);
920
921                 mx.lock();
922
923                 MsgOperationSyncMLMessageCBList::iterator it = operationSyncMLMessageCBList.begin();
924
925                 for( ; it != operationSyncMLMessageCBList.end() ; it++)
926                 {
927                         MsgHandle* pHandle = it->hAddr;
928
929                         msg_syncml_msg_operation_cb pfunc = it->pfSyncMLOperationCB;
930
931                         void* param = it->userParam;
932
933                         pfunc((msg_handle_t)pHandle, msgId, extId, param);
934                 }
935
936                 mx.unlock();
937         }
938         else if (pMsgEvent->eventType == MSG_EVENT_PLG_STORAGE_CHANGE_IND)
939         {
940                 msg_storage_change_type_t storageChangeType;
941                 msg_id_list_s msgIdList;
942                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
943
944                 // Decode event data
945                 memcpy(&storageChangeType, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(msg_storage_change_type_t));
946                 memcpy(&msgIdList.nCount, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_storage_change_type_t)), sizeof(int));
947
948                 if(msgIdList.nCount > 0)
949                         msgIdList.msgIdList = (msg_message_id_t*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_storage_change_type_t)+sizeof(int));
950                 else
951                         msgIdList.msgIdList = NULL;
952
953                 MSG_DEBUG("storageChangeType [%d], msgIdList.nCount [%d]", storageChangeType, msgIdList.nCount);
954
955                 mx.lock();
956
957                 MsgStorageChangeCBList::iterator it = storageChangeCBList.begin();
958
959                 for( ; it != storageChangeCBList.end() ; it++)
960                 {
961                         MsgHandle* pHandle = it->hAddr;
962
963                         msg_storage_change_cb pfunc = it->pfStorageChangeCB;
964
965                         void* param = it->userParam;
966
967                         pfunc((msg_handle_t)pHandle, storageChangeType, (msg_id_list_s*)&msgIdList, param);
968                 }
969
970                 mx.unlock();
971         }
972
973         else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_CB_MSG_IND)
974         {
975                 MSG_CB_MSG_S *pCbMsg = (MSG_CB_MSG_S *)pMsgEvent->data;
976
977                 mx.lock();
978
979                 MsgNewCBMessageCBList::iterator it = newCBMessageCBList.begin();
980
981                 for( ; it != newCBMessageCBList.end() ; it++)
982                 {
983                         MsgHandle* pHandle = it->hAddr;
984                         msg_struct_s msg = {0,};
985
986                         msg.type = MSG_STRUCT_CB_MSG;
987                         msg.data = pCbMsg;
988
989                         msg_cb_incoming_cb pfunc = it->pfCBIncomingCB;
990
991                         void* param = it->userParam;
992
993                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
994                 }
995
996                 mx.unlock();
997         }
998
999         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND )
1000         {
1001                 MSG_PUSH_MESSAGE_DATA_S* pPushData = (MSG_PUSH_MESSAGE_DATA_S *)pMsgEvent->data;
1002
1003                 mx.lock();
1004
1005                 MsgNewPushMessageCBList::iterator it = newPushMessageCBList.begin();
1006
1007                 for( ; it != newPushMessageCBList.end() ; it++)
1008                 {
1009                         MsgHandle* pHandle = it->hAddr;
1010
1011                         msg_push_msg_incoming_cb pfunc = it->pfPushIncomingCB;
1012
1013                         void* param = it->userParam;
1014
1015                         if(!strncmp(it->appId, pPushData->pushAppId, MAX_WAPPUSH_ID_LEN))
1016                                 pfunc((msg_handle_t)pHandle, pPushData->pushHeader, pPushData->pushBody, pPushData->pushBodyLen, param);
1017                 }
1018
1019                 mx.unlock();
1020         }
1021
1022         else if (pMsgEvent->eventType == MSG_EVENT_PLG_REPORT_MSG_INCOMING_IND)
1023         {
1024                 msg_report_type_t reportType;
1025                 msg_message_id_t msgId;
1026                 int addr_len;
1027                 char *addr_val;
1028
1029                 // Decode event data
1030                 memcpy(&reportType, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(msg_report_type_t));
1031                 memcpy(&msgId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_report_type_t)), sizeof(msg_message_id_t));
1032                 memcpy(&addr_len, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_report_type_t)+sizeof(msg_message_id_t)), sizeof(int));
1033                 addr_val = (char*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_report_type_t)+sizeof(msg_message_id_t)+sizeof(int));
1034                 addr_val[addr_len] = '\0';
1035
1036                 MSG_SEC_DEBUG("reportType [%d], msgId [%d], Address Length [%d], Address Value [%s]", reportType, msgId, addr_len, addr_val);
1037
1038                 mx.lock();
1039
1040                 MsgReportMessageCBList::iterator it = reportMessageCBList.begin();
1041
1042                 for( ; it != reportMessageCBList.end() ; it++)
1043                 {
1044                         MsgHandle* pHandle = it->hAddr;
1045
1046                         msg_report_msg_incoming_cb pfunc = it->pfReportMsgIncomingCB;
1047
1048                         void* param = it->userParam;
1049
1050                         pfunc((msg_handle_t)pHandle, reportType, msgId, addr_len, addr_val, param);
1051                 }
1052
1053                 mx.unlock();
1054         }
1055
1056         MSG_END();
1057 }
1058
1059
1060 int  MsgProxyListener::getRemoteFd()
1061 {
1062         MSG_BEGIN();
1063         //MutexLocker lock(mx);
1064
1065         int tmpFd = -1;
1066         int ret = mx.timedlock();
1067
1068         if (ret != 0) {
1069                 MSG_DEBUG("mx.timedlock fail [%d]", ret);
1070                 return tmpFd;
1071         }
1072
1073         tmpFd = cliSock.getRemoteFd();
1074
1075         MSG_DEBUG("listener fd [%d]", tmpFd);
1076
1077         if( tmpFd == -1 ) {
1078                 ret = cv.timedwait(mx.pMutex(),1);
1079         }
1080
1081         if (ret == ETIMEDOUT) {
1082                 MSG_DEBUG("get listener fd TIME-OUT");
1083                 mx.unlock();
1084                 return tmpFd;
1085         }
1086
1087         tmpFd = cliSock.getRemoteFd();
1088         mx.unlock();
1089
1090         MSG_END();
1091
1092         return tmpFd;
1093 }
1094
1095
1096 int MsgProxyListener::readFromSocket(char** buf, unsigned int* len)
1097 {
1098         return cliSock.read(buf, len);
1099 }
1100
1101 void MsgProxyListener::resetProxyListener()
1102 {
1103         MSG_BEGIN();
1104         MutexLocker lock(mx);
1105         handle_set::iterator it = openHandleSet.begin();
1106         for (; it != openHandleSet.end(); it++) {
1107                 MsgHandle *handle = (MsgHandle *)*it;
1108                 clearListOfClosedHandle(handle);
1109         }
1110         openHandleSet.clear();
1111         MSG_END();
1112 }
1113
1114 #ifdef CHECK_SENT_STATUS_CALLBACK
1115 int MsgProxyListener::getSentStatusCbCnt()
1116 {
1117         int cbCnt = 0;
1118
1119         cbCnt = sentStatusCBList.size();
1120
1121         MSG_DEBUG("registered sent status callback count : [%d]", cbCnt);
1122
1123         return cbCnt;
1124 }
1125 #endif