Remove THROW exception in sms submit request & remove lock and wait in get remote FD.
[framework/messaging/msg-service.git] / proxy / MsgProxyListener.cpp
1 /*
2 * Copyright 2012-2013  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://floralicense.org
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                 if(MsgSettingSetBool(CB_SAVE, bSave) != MSG_SUCCESS)
563                         MSG_DEBUG("MsgSettingSetBool FAIL: CB_SAVE");
564
565         // Storage change Message CB list
566         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it8 = storageChangeCBList.begin();
567
568         for (; it8 != storageChangeCBList.end(); it8++)
569         {
570                 if (it8->hAddr == pMsgHandle)
571                 {
572                         storageChangeCBList.erase(it8);
573                         it8 = storageChangeCBList.begin();
574
575                         //Stop client Listener
576                         stop();
577                 }
578         }
579
580         MSG_END();
581 }
582
583 void MsgProxyListener::handleEvent(const MSG_EVENT_S* pMsgEvent)
584 {
585         MSG_BEGIN();
586
587         if (!pMsgEvent)
588                 THROW(MsgException::INVALID_PARAM, "pMsgEvent is NULL");
589
590         if (pMsgEvent->eventType == MSG_EVENT_PLG_SENT_STATUS_CNF)
591         {
592                 unsigned int chInfo[3] = {0}; //3// reqid, status, object
593
594                 memcpy(&chInfo, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(chInfo));
595
596                 msg_struct_s status = {0,};
597                 MSG_SENT_STATUS_S statusData = {(msg_request_id_t)chInfo[0], (msg_network_status_t)chInfo[1]};
598
599                 status.type = MSG_STRUCT_SENT_STATUS_INFO;
600                 status.data = (void *)&statusData;
601
602                 mx.lock();
603
604                 MsgSentStatusCBList::iterator it = sentStatusCBList.begin();
605
606                 for( ; it != sentStatusCBList.end() ; it++)
607                 {
608                         MsgHandle* pHandle = it->hAddr;
609
610                         msg_sent_status_cb pfunc = it->pfSentStatusCB;
611
612                         void* param = it->userParam;
613
614                         pfunc((msg_handle_t)pHandle, (msg_struct_t)&status, param);
615                 }
616
617                 mx.unlock();
618         }
619         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MSG_IND )
620         {
621                 MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)pMsgEvent->data;
622                 int portKey = (pMsgInfo->msgPort.valid)? pMsgInfo->msgPort.dstPort: 0;
623
624                 mx.lock();
625
626                 MsgNewMessageCBList::iterator it = newMessageCBList.begin();
627                 MsgNewMessageCBList matchList;
628
629                 for( ; it != newMessageCBList.end() ; it++)
630                 {
631                         if( portKey == it->port)
632                         {
633                                 matchList.push_back(*it);
634                         }
635                 }
636
637                 mx.unlock();
638
639                 it = matchList.begin();
640
641                 for( ; it != matchList.end(); it++ )
642                 {
643                         MsgHandle* pHandle = it->hAddr;
644
645                         MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)pMsgEvent->data;
646                         MSG_MESSAGE_HIDDEN_S msgHidden = {0,};
647
648                         msgHidden.pData = NULL;
649                         msgHidden.pMmsData = NULL;
650
651                         /* Allocate memory for address list of message */
652                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
653
654                         addr_list->nCount = 0;
655                         addr_list->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_ADDRESS_INFO_S *)*MAX_TO_ADDRESS_CNT];
656
657                         msg_struct_s *pTmp = NULL;
658
659                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
660                                 addr_list->msg_struct_info[i] = (msg_struct_t)new char[sizeof(msg_struct_s)];
661                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
662                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
663                                 pTmp->data = new MSG_ADDRESS_INFO_S;
664                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
665
666                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
667                         }
668
669                         msgHidden.addr_list = addr_list;
670
671                         pHandle->convertMsgStruct(pMsgInfo, &msgHidden);
672
673                         msg_struct_s msg = {0,};
674                         msg.type = MSG_STRUCT_MESSAGE_INFO;
675                         msg.data = &msgHidden;
676
677                         msg_sms_incoming_cb pfunc = it->pfIncomingCB;
678
679                         void* param = it->userParam;
680
681                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
682
683                         delete [] (char*)msgHidden.pData;
684                         if (msgHidden.pMmsData != NULL)
685                                 delete [] (char*)msgHidden.pMmsData;
686
687                         // address Memory Free
688                         if (msgHidden.addr_list!= NULL)
689                         {
690                                 for(int i=0; i<MAX_TO_ADDRESS_CNT; i++) {
691                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
692                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
693                                         addrInfo->data = NULL;
694                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
695                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
696                                 }
697
698                                 delete [] msgHidden.addr_list->msg_struct_info;
699
700                                 delete msgHidden.addr_list;
701                                 msgHidden.addr_list = NULL;
702                         }
703
704                 }
705
706         }
707         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MMS_CONF )
708         {
709                 MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)pMsgEvent->data;
710                 MMS_RECV_DATA_S* pMmsRecvData = ( MMS_RECV_DATA_S*)pMsgInfo->msgData;
711
712                 char* appIdKey = (pMmsRecvData->msgAppId.valid)? pMmsRecvData->msgAppId.appId: NULL;
713
714                 mx.lock();
715
716                 MsgNewMMSConfMessageCBList::iterator it = newMMSConfMessageCBList.begin();
717                 MsgNewMMSConfMessageCBList matchList;
718
719                 for( ; it != newMMSConfMessageCBList.end() ; it++)
720                 {
721                         if(appIdKey)
722                         {
723                                 if(!strcmp(appIdKey, it->appId))
724                                         matchList.push_back(*it);
725                         }
726                         else//(appIdKey == NULL && it->appId[0] == 0)
727                         {
728                                 if(it->appId[0] == 0)
729                                         matchList.push_back(*it);
730                         }
731                 }
732
733                 mx.unlock();
734
735                 // Contents of msgData removed and replaced to retrievedFilePath for convertMsgStruct
736                 // it is moved from UpdateMessage in MmsPluginStorage.cpp
737                 char tempFileName[MSG_FILENAME_LEN_MAX+1] = {0};  // check MSG_FILENAME_LEN_MAX
738
739                 strncpy(tempFileName, pMmsRecvData->retrievedFilePath, MSG_FILENAME_LEN_MAX);
740
741                 memset(pMsgInfo->msgData, 0, MAX_MSG_DATA_LEN+1);
742                 memcpy(pMsgInfo->msgData, tempFileName + strlen(MSG_DATA_PATH), MAX_MSG_DATA_LEN);
743
744                 it = matchList.begin();
745
746                 for( ; it != matchList.end() ; it++)
747                 {
748                         MsgHandle* pHandle = it->hAddr;
749
750                         MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)pMsgEvent->data;
751                         MSG_MESSAGE_HIDDEN_S msgHidden = {0,};
752
753                         msgHidden.pData = NULL;
754                         msgHidden.pMmsData = NULL;
755
756                         /* Allocate memory for address list of message */
757                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
758
759                         addr_list->nCount = 0;
760                         addr_list->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_ADDRESS_INFO_S *)*MAX_TO_ADDRESS_CNT];
761
762                         msg_struct_s *pTmp = NULL;
763
764                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
765                                 addr_list->msg_struct_info[i] = (msg_struct_t)new char[sizeof(msg_struct_s)];
766                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
767                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
768                                 pTmp->data = new MSG_ADDRESS_INFO_S;
769                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
770
771                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
772                         }
773
774                         msgHidden.addr_list = addr_list;
775
776                         pHandle->convertMsgStruct(pMsgInfo, &msgHidden);
777
778                         msg_struct_s msg = {0,};
779                         msg.type = MSG_STRUCT_MESSAGE_INFO;
780                         msg.data = &msgHidden;
781
782                         msg_mms_conf_msg_incoming_cb pfunc = it->pfMMSConfIncomingCB;
783
784                         void* param = it->userParam;
785                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
786
787                         delete [] (char*)msgHidden.pData;
788                         if (msgHidden.pMmsData != NULL)
789                                 delete [] (char*)msgHidden.pMmsData;
790
791                         // address Memory Free
792                         if (msgHidden.addr_list!= NULL)
793                         {
794                                 for(int i=0; i<MAX_TO_ADDRESS_CNT; i++) {
795                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
796                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
797                                         addrInfo->data = NULL;
798                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
799                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
800                                 }
801
802                                 delete [] msgHidden.addr_list->msg_struct_info;
803
804                                 delete msgHidden.addr_list;
805                                 msgHidden.addr_list = NULL;
806                         }
807
808                         // Here the retrieved message will be deleted from native storage.
809                         // as of now, msg which have appId is considered as JAVA MMS message and it will be sent and handled in JAVA app.
810                         if(appIdKey)
811                         {
812                                 MSG_DEBUG("delete received JAVA MMS message:%s from native storage",tempFileName);
813                                 pHandle->deleteMessage(pMsgInfo->msgId);
814                         }
815                 }
816         }
817         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND )
818         {
819                 MSG_SYNCML_MESSAGE_DATA_S* pSyncMLData = (MSG_SYNCML_MESSAGE_DATA_S *)pMsgEvent->data;
820
821                 MSG_DEBUG("msgType [%d]", pSyncMLData->syncmlType);
822
823                 mx.lock();
824
825                 MsgNewSyncMLMessageCBList::iterator it = newSyncMLMessageCBList.begin();
826
827                 for( ; it != newSyncMLMessageCBList.end() ; it++)
828                 {
829                         MsgHandle* pHandle = it->hAddr;
830
831                         msg_syncml_msg_incoming_cb pfunc = it->pfSyncMLIncomingCB;
832
833                         void* param = it->userParam;
834
835                         pfunc((msg_handle_t)pHandle, pSyncMLData->syncmlType, pSyncMLData->pushBody, pSyncMLData->pushBodyLen, pSyncMLData->wspHeader, pSyncMLData->wspHeaderLen, param);
836                 }
837
838                 mx.unlock();
839         }
840         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_LBS_MSG_IND )
841         {
842                 MSG_LBS_MESSAGE_DATA_S* pLBSData = (MSG_LBS_MESSAGE_DATA_S *)pMsgEvent->data;
843
844                 mx.lock();
845
846                 MsgNewLBSMessageCBList::iterator it = newLBSMessageCBList.begin();
847
848                 for( ; it != newLBSMessageCBList.end() ; it++)
849                 {
850                         MsgHandle* pHandle = it->hAddr;
851
852                         msg_lbs_msg_incoming_cb pfunc = it->pfLBSMsgIncoming;
853
854                         void* param = it->userParam;
855
856                         pfunc((msg_handle_t)pHandle, pLBSData->pushHeader, pLBSData->pushBody, pLBSData->pushBodyLen, param);
857                 }
858
859                 mx.unlock();
860         }
861         else if ( pMsgEvent->eventType == MSG_EVENT_SYNCML_OPERATION )
862         {
863                 int msgId;
864                 int extId;
865
866                 memcpy(&msgId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(int));
867                 memcpy(&extId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(int)), sizeof(int));
868
869                 MSG_DEBUG("msgId [%d]", msgId);
870                 MSG_DEBUG("extId [%d]", extId);
871
872                 mx.lock();
873
874                 MsgOperationSyncMLMessageCBList::iterator it = operationSyncMLMessageCBList.begin();
875
876                 for( ; it != operationSyncMLMessageCBList.end() ; it++)
877                 {
878                         MsgHandle* pHandle = it->hAddr;
879
880                         msg_syncml_msg_operation_cb pfunc = it->pfSyncMLOperationCB;
881
882                         void* param = it->userParam;
883
884                         pfunc((msg_handle_t)pHandle, msgId, extId, param);
885                 }
886
887                 mx.unlock();
888         }
889         else if (pMsgEvent->eventType == MSG_EVENT_PLG_STORAGE_CHANGE_IND)
890         {
891                 msg_storage_change_type_t storageChangeType;
892                 msg_id_list_s msgIdList;
893                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
894
895                 // Decode event data
896                 memcpy(&storageChangeType, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(msg_storage_change_type_t));
897                 memcpy(&msgIdList.nCount, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_storage_change_type_t)), sizeof(int));
898
899                 if(msgIdList.nCount > 0)
900                         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));
901                 else
902                         msgIdList.msgIdList = NULL;
903
904                 MSG_DEBUG("storageChangeType [%d], msgIdList.nCount [%d]", storageChangeType, msgIdList.nCount);
905
906                 mx.lock();
907
908                 MsgStorageChangeCBList::iterator it = storageChangeCBList.begin();
909
910                 for( ; it != storageChangeCBList.end() ; it++)
911                 {
912                         MsgHandle* pHandle = it->hAddr;
913
914                         msg_storage_change_cb pfunc = it->pfStorageChangeCB;
915
916                         void* param = it->userParam;
917
918                         pfunc((msg_handle_t)pHandle, storageChangeType, (msg_id_list_s*)&msgIdList, param);
919                 }
920
921                 mx.unlock();
922         }
923
924         else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_CB_MSG_IND)
925         {
926                 MSG_CB_MSG_S *pCbMsg = (MSG_CB_MSG_S *)pMsgEvent->data;
927
928                 mx.lock();
929
930                 MsgNewCBMessageCBList::iterator it = newCBMessageCBList.begin();
931
932                 for( ; it != newCBMessageCBList.end() ; it++)
933                 {
934                         MsgHandle* pHandle = it->hAddr;
935                         msg_struct_s msg = {0,};
936
937                         msg.type = MSG_STRUCT_CB_MSG;
938                         msg.data = pCbMsg;
939
940                         msg_cb_incoming_cb pfunc = it->pfCBIncomingCB;
941
942                         void* param = it->userParam;
943
944                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
945                 }
946
947                 mx.unlock();
948         }
949
950         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND )
951         {
952                 MSG_PUSH_MESSAGE_DATA_S* pPushData = (MSG_PUSH_MESSAGE_DATA_S *)pMsgEvent->data;
953
954                 mx.lock();
955
956                 MsgNewPushMessageCBList::iterator it = newPushMessageCBList.begin();
957
958                 for( ; it != newPushMessageCBList.end() ; it++)
959                 {
960                         MsgHandle* pHandle = it->hAddr;
961
962                         msg_push_msg_incoming_cb pfunc = it->pfPushIncomingCB;
963
964                         void* param = it->userParam;
965
966                         pfunc((msg_handle_t)pHandle, pPushData->pushHeader, pPushData->pushBody, pPushData->pushBodyLen, param);
967                 }
968
969                 mx.unlock();
970         }
971
972         MSG_END();
973 }
974
975
976 int  MsgProxyListener::getRemoteFd()
977 {
978         int tmpFd = cliSock.getRemoteFd();
979
980         MSG_DEBUG("listener fd [%d]", tmpFd);
981
982         return tmpFd;
983 }
984
985
986 int MsgProxyListener::readFromSocket(char** buf, unsigned int* len)
987 {
988         return cliSock.read(buf, len);
989 }
990
991 #ifdef CHECK_SENT_STATUS_CALLBACK
992 int MsgProxyListener::getSentStatusCbCnt()
993 {
994         int cbCnt = 0;
995
996         cbCnt = sentStatusCBList.size();
997
998         MSG_DEBUG("registered sent status callback count : [%d]", cbCnt);
999
1000         return cbCnt;
1001 }
1002 #endif