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