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