merge with master
[platform/core/messaging/msg-service.git] / proxy / MsgHandleTransport.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 <time.h>
18 #include <errno.h>
19
20 #include "MsgDebug.h"
21 #include "MsgCppTypes.h"
22 #include "MsgException.h"
23 #include "MsgProxyListener.h"
24 #include "MsgHandle.h"
25
26
27 #define MAX_ADDRESS_LEN                         21 // including '+'
28
29 /*==================================================================================================
30                                      IMPLEMENTATION OF MsgHandle - Transport Member Functions
31 ==================================================================================================*/
32 msg_error_t MsgHandle::submitReq(MSG_REQUEST_S* pReq)
33 {
34         MSG_BEGIN();
35
36         if (pReq == NULL)
37                 THROW(MsgException::INVALID_PARAM, "pReq is NULL");
38
39 #ifdef CHECK_SENT_STATUS_CALLBACK
40         if (MsgProxyListener::instance()->getSentStatusCbCnt() <= 0)
41                 THROW(MsgException::SENT_STATUS_ERROR,"Register sent status callback");
42 #endif
43
44         MSG_REQUEST_INFO_S reqInfo = {0};
45         char trId[MMS_TR_ID_LEN+1] = {0};
46
47         msg_struct_s *msg_s = (msg_struct_s *)pReq->msg;
48
49         MSG_MESSAGE_HIDDEN_S *reqmsg = (MSG_MESSAGE_HIDDEN_S*)msg_s->data;
50
51         if (reqmsg->subType != MSG_SENDREQ_JAVA_MMS) {
52                 // In case MMS read report, get address value later.
53                 if(reqmsg->subType != MSG_READREPLY_MMS) {
54                         if ((reqmsg->addr_list->nCount == 0) || (reqmsg->addr_list->nCount > MAX_TO_ADDRESS_CNT)) {
55                                 MSG_DEBUG("Recipient address count error [%d]", reqmsg->addr_list->nCount );
56                                 return MSG_ERR_INVALID_MESSAGE;
57                         }
58                 }
59
60                 /* Begin: Setting default values for submit request */
61         //      pReq->msg.msgId = 0;    // Set Request ID: internal use
62         //      pReq->msg.folderId = MSG_OUTBOX_ID;     // Set Folder ID
63                 if (reqmsg->subType == MSG_RETRIEVE_MMS) {
64                         reqmsg->networkStatus = MSG_NETWORK_RETRIEVING;
65                 } else {
66                         reqmsg->networkStatus = MSG_NETWORK_SENDING;
67                 }
68
69                 reqmsg->bRead = false;
70                 reqmsg->bProtected = false;
71                 reqmsg->priority = MSG_MESSAGE_PRIORITY_NORMAL;
72                 reqmsg->direction = MSG_DIRECTION_TYPE_MO;
73                 reqmsg->storageId = MSG_STORAGE_PHONE;
74
75                 time_t curTime = time(NULL);
76
77                 if (curTime < 0)
78                         THROW(MsgException::INVALID_RESULT, "time error : %s", strerror(errno));
79
80                 reqmsg->displayTime = curTime;
81                 /* End : Setting default values for submit request */
82         } else {
83                 //in case of JAVA MMS msg, parse mms transaction id from pMmsData
84                 reqmsg->networkStatus = MSG_NETWORK_SENDING;
85                 strncpy(trId, (char*)reqmsg->pMmsData+3,MMS_TR_ID_LEN);
86                 MSG_DEBUG("JavaMMS transaction Id:%s ",trId);
87         }
88
89         // Convert MSG_MESSAGE_S to MSG_MESSAGE_INFO_S
90         convertMsgStruct(reqmsg, &(reqInfo.msgInfo));
91
92         /* Check address validation */
93         if (reqInfo.msgInfo.msgType.mainType == MSG_SMS_TYPE) {
94                 for(int i=0; i<reqmsg->addr_list->nCount; i++) {
95                                 if (reqInfo.msgInfo.addressList[i].addressVal[0] == '+' && strlen(reqInfo.msgInfo.addressList[i].addressVal)>MAX_ADDRESS_LEN) {
96                                         return MSG_ERR_INVALID_PARAMETER;
97                                 } else if (strlen(reqInfo.msgInfo.addressList[i].addressVal)>(MAX_ADDRESS_LEN-1)) {
98                                         return MSG_ERR_INVALID_PARAMETER;
99                                 }
100                 }
101         }
102
103         MSG_MESSAGE_TYPE_S msgType = {0,};
104
105         msgType.mainType = reqmsg->mainType;
106         msgType.subType = reqmsg->subType;
107         msgType.classType = reqmsg->classType;
108
109         convertSendOptStruct((const MSG_SENDINGOPT_S *)pReq->sendOpt, &(reqInfo.sendOptInfo), msgType);
110
111         reqInfo.reqId = 0;
112
113         /* Register proxy info used for receiving sent status */
114         MSG_PROXY_INFO_S chInfo = {0};
115
116         chInfo.listenerFd = MsgProxyListener::instance()->getRemoteFd();
117
118         chInfo.handleAddr = (unsigned int) this;
119
120         /* Allocate Memory to Command Data */
121         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_REQUEST_INFO_S) + sizeof(MSG_PROXY_INFO_S);
122
123         // In case of JAVA MMS msg, add trId
124         if (reqmsg->subType == MSG_SENDREQ_JAVA_MMS)
125                 cmdSize += sizeof(trId);
126
127         char cmdBuf[cmdSize];
128         bzero(cmdBuf, cmdSize);
129
130         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
131
132         // Set Command Parameters
133         pCmd->cmdType = MSG_CMD_SUBMIT_REQ;
134
135         // Copy Cookie
136         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
137
138         // Copy Command Data
139         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &reqInfo, sizeof(MSG_REQUEST_INFO_S));
140         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(MSG_REQUEST_INFO_S)), &chInfo, sizeof(MSG_PROXY_INFO_S));
141
142         // In case of JAVA MMS msg, add trId
143         if (reqmsg->subType == MSG_SENDREQ_JAVA_MMS)
144                 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(MSG_REQUEST_INFO_S)+sizeof(MSG_PROXY_INFO_S)), &trId, sizeof(trId));
145
146         // Send Command to Messaging FW
147         char* pEventData = NULL;
148         AutoPtr<char> eventBuf(&pEventData);
149
150         write((char*)pCmd, cmdSize, &pEventData);
151
152         // Get Return Data
153         MSG_EVENT_S* pEvent = (MSG_EVENT_S*) pEventData;
154
155         int* pReqId = (int*) pEvent->data;
156         pReq->reqId = *pReqId;
157         MSG_DEBUG("SENT_REQ_ID: %d", pReq->reqId);
158
159         if (pEvent->eventType != MSG_EVENT_SUBMIT_REQ)
160         {
161                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
162         }
163
164         MSG_END();
165
166         return pEvent->result;
167 }
168
169
170 msg_error_t MsgHandle::cancelReq(msg_request_id_t reqId)
171 {
172         // Allocate Memory to Command Data
173         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_request_id_t);
174
175         char cmdBuf[cmdSize];
176         bzero(cmdBuf, cmdSize);
177
178         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
179
180         // Set Command Parameters
181         pCmd->cmdType = MSG_CMD_CANCEL_REQ;
182
183         // Copy Cookie
184         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
185
186         // Copy Command Data
187         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &reqId, sizeof(msg_request_id_t));
188
189         // Send Command to Messaging FW
190         char* pEventData = NULL;
191         AutoPtr<char> eventBuf(&pEventData);
192
193
194         write((char*)pCmd, cmdSize, &pEventData);
195
196         // Get Return Data
197         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
198
199         if (pEvent->eventType != MSG_EVENT_CANCEL_REQ)
200         {
201                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
202         }
203
204         return pEvent->result;
205 }
206
207
208 msg_error_t MsgHandle::regSentStatusCallback(msg_sent_status_cb onStatusChanged, void *pUserParam)
209 {
210         if (!onStatusChanged)
211                 THROW(MsgException::INVALID_PARAM, "onStatusChanged is null");
212
213         MsgProxyListener* eventListener = MsgProxyListener::instance();
214
215         eventListener->start();
216
217         if (eventListener->regSentStatusEventCB(this, onStatusChanged, pUserParam) == false) // callback was already registered, just return SUCCESS
218                 return MSG_SUCCESS;
219
220         // Allocate Memory to Command Data
221         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); // cmd type, listenerFd
222         char cmdBuf[cmdSize];
223         bzero(cmdBuf, cmdSize);
224         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
225
226         // Set Command Parameters
227         pCmd->cmdType = MSG_CMD_REG_SENT_STATUS_CB;
228
229         // Copy Cookie
230         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
231
232         int listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
233
234         MSG_DEBUG("remote fd %d", listenerFd);
235
236         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &listenerFd, sizeof(listenerFd));
237
238         MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), listenerFd);
239
240         // Send Command to Messaging FW
241         char* pEventData = NULL;
242         AutoPtr<char> eventBuf(&pEventData);
243
244         write((char*)pCmd, cmdSize, &pEventData);
245
246         // Get Return Data
247         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
248
249         if (pEvent->eventType != MSG_EVENT_REG_SENT_STATUS_CB)
250         {
251                 THROW(MsgException::INVALID_PARAM, "Event Data Error");
252         }
253
254         return pEvent->result;
255 }
256
257
258 msg_error_t MsgHandle::regSmsMessageCallback(msg_sms_incoming_cb onMsgIncoming, unsigned short port, void *pUserParam)
259 {
260         if( (!onMsgIncoming) )
261                 THROW(MsgException::INVALID_PARAM, "Param %p", onMsgIncoming);
262
263         MsgProxyListener* eventListener = MsgProxyListener::instance();
264
265         eventListener->start();
266
267         if (eventListener->regMessageIncomingEventCB(this, onMsgIncoming, port, pUserParam) == false) // callback was already registered, just return SUCCESS
268                 return MSG_SUCCESS;
269
270         // Allocate Memory to Command Data
271         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
272
273         char cmdBuf[cmdSize];
274         bzero(cmdBuf, cmdSize);
275
276         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
277
278         // Set Command Parameters
279         pCmd->cmdType = MSG_CMD_REG_INCOMING_MSG_CB;
280
281         // Copy Cookie
282         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
283
284         MSG_CMD_REG_INCOMING_MSG_CB_S cmdParam = {0};
285
286         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
287         cmdParam.msgType = MSG_SMS_TYPE;
288         cmdParam.port = port;
289
290         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
291
292         MSG_DEBUG("reg new msg [%s], fd %d, port %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, cmdParam.port);
293
294         // Send Command to Messaging FW
295         char* pEventData = NULL;
296         AutoPtr<char> eventBuf(&pEventData);
297
298
299         write((char*)pCmd, cmdSize, &pEventData);
300
301         // Get Return Data
302         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
303
304         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MSG_CB)
305         {
306                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
307         }
308
309         return pEvent->result;
310 }
311
312
313 msg_error_t MsgHandle::regMmsConfMessageCallback(msg_mms_conf_msg_incoming_cb onMMSConfMsgIncoming, const char *pAppId, void *pUserParam)
314 {
315         if( (!onMMSConfMsgIncoming) )
316                 THROW(MsgException::INVALID_PARAM, "Param %p", onMMSConfMsgIncoming);
317
318         MsgProxyListener* eventListener = MsgProxyListener::instance();
319
320         eventListener->start();
321
322         if (eventListener->regMMSConfMessageIncomingEventCB(this, onMMSConfMsgIncoming, pAppId, pUserParam) == false) // callback was already registered, just return SUCCESS
323                 return MSG_SUCCESS;
324
325         // Allocate Memory to Command Data
326         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S);
327
328         char cmdBuf[cmdSize];
329         bzero(cmdBuf, cmdSize);
330
331         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
332
333         // Set Command Parameters
334         pCmd->cmdType = MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB;
335
336         // Copy Cookie
337         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
338
339         MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S cmdParam = {0};
340
341         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
342         cmdParam.msgType = MSG_MMS_TYPE;
343
344         if (pAppId)
345                 strncpy(cmdParam.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
346
347         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
348
349         MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd,  (pAppId)? cmdParam.appId:"NULL" );
350
351         // Send Command to Messaging FW
352         char* pEventData = NULL;
353         AutoPtr<char> eventBuf(&pEventData);
354
355
356         write((char*)pCmd, cmdSize, &pEventData);
357
358         // Get Return Data
359         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
360
361         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MMS_CONF_MSG_CB)
362         {
363                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
364         }
365
366         return pEvent->result;
367 }
368
369
370 msg_error_t MsgHandle::regSyncMLMessageCallback(msg_syncml_msg_incoming_cb onSyncMLMsgIncoming, void *pUserParam)
371 {
372         if( (!onSyncMLMsgIncoming) )
373                 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgIncoming);
374
375         MsgProxyListener* eventListener = MsgProxyListener::instance();
376
377         eventListener->start();
378
379         if (eventListener->regSyncMLMessageIncomingEventCB(this, onSyncMLMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
380                 return MSG_SUCCESS;
381
382         // Allocate Memory to Command Data
383         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
384
385         char cmdBuf[cmdSize];
386         bzero(cmdBuf, cmdSize);
387
388         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
389
390         // Set Command Parameters
391         pCmd->cmdType = MSG_CMD_REG_INCOMING_SYNCML_MSG_CB;
392
393         // Copy Cookie
394         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
395
396         MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S cmdParam = {0};
397
398         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
399         cmdParam.msgType = MSG_SMS_TYPE;
400
401         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
402
403         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
404
405         // Send Command to Messaging FW
406         char* pEventData = NULL;
407         AutoPtr<char> eventBuf(&pEventData);
408
409         write((char*)pCmd, cmdSize, &pEventData);
410
411         // Get Return Data
412         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
413
414         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_SYNCML_MSG_CB)
415         {
416                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
417         }
418
419         return pEvent->result;
420 }
421
422
423 msg_error_t MsgHandle::regLBSMessageCallback(msg_lbs_msg_incoming_cb onLBSMsgIncoming, void *pUserParam)
424 {
425         if( (!onLBSMsgIncoming) )
426                 THROW(MsgException::INVALID_PARAM, "Param %p", onLBSMsgIncoming);
427
428         MsgProxyListener* eventListener = MsgProxyListener::instance();
429
430         eventListener->start();
431
432         if (eventListener->regLBSMessageIncomingEventCB(this, onLBSMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
433                 return MSG_SUCCESS;
434
435         // Allocate Memory to Command Data
436         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
437         char cmdBuf[cmdSize];
438         bzero(cmdBuf, cmdSize);
439         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
440
441         // Set Command Parameters
442         pCmd->cmdType = MSG_CMD_REG_INCOMING_LBS_MSG_CB;
443
444         // Copy Cookie
445         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
446
447         MSG_CMD_REG_INCOMING_LBS_MSG_CB_S cmdParam = {0};
448
449         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
450         cmdParam.msgType = MSG_SMS_TYPE;
451
452         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
453
454         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
455
456         // Send Command to Messaging FW
457         char* pEventData = NULL;
458         AutoPtr<char> eventBuf(&pEventData);
459
460
461         write((char*)pCmd, cmdSize, &pEventData);
462
463         // Get Return Data
464         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
465
466         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_LBS_MSG_CB)
467         {
468                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
469         }
470
471         return pEvent->result;
472 }
473
474
475 msg_error_t MsgHandle::regSyncMLMessageOperationCallback(msg_syncml_msg_operation_cb onSyncMLMsgOperation, void *pUserParam)
476 {
477         if( (!onSyncMLMsgOperation) )
478                 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgOperation);
479
480         MsgProxyListener* eventListener = MsgProxyListener::instance();
481
482         eventListener->start();
483
484         if (eventListener->regSyncMLMessageOperationEventCB(this, onSyncMLMsgOperation, pUserParam) == false) // callback was already registered, just return SUCCESS
485                 return MSG_SUCCESS;
486
487         // Allocate Memory to Command Data
488         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
489
490         char cmdBuf[cmdSize];
491         bzero(cmdBuf, cmdSize);
492
493         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
494
495         // Set Command Parameters
496         pCmd->cmdType = MSG_CMD_REG_SYNCML_MSG_OPERATION_CB;
497
498         // Copy Cookie
499         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
500
501         MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S cmdParam = {0};
502
503         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
504         cmdParam.msgType = MSG_SMS_TYPE;
505
506         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
507
508         MSG_DEBUG("register syncML msg operation callback [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
509
510         // Send Command to Messaging FW
511         char* pEventData = NULL;
512         AutoPtr<char> eventBuf(&pEventData);
513
514
515         write((char*)pCmd, cmdSize, &pEventData);
516
517         // Get Return Data
518         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
519
520         if (pEvent->eventType != MSG_EVENT_REG_SYNCML_MSG_OPERATION_CB)
521         {
522                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
523         }
524
525         return pEvent->result;
526 }
527
528 msg_error_t MsgHandle::regPushMessageCallback(msg_push_msg_incoming_cb onPushMsgIncoming, const char *pAppId, void *pUserParam)
529 {
530         if( (!onPushMsgIncoming) )
531                 THROW(MsgException::INVALID_PARAM, "Param %p", onPushMsgIncoming);
532
533         MsgProxyListener* eventListener = MsgProxyListener::instance();
534
535         eventListener->start();
536
537         if (eventListener->regPushMessageIncomingEventCB(this, onPushMsgIncoming, pAppId, pUserParam) == false) // callback was already registered, just return SUCCESS
538                 return MSG_SUCCESS;
539
540         // Allocate Memory to Command Data
541         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S);
542
543         char cmdBuf[cmdSize];
544         bzero(cmdBuf, cmdSize);
545
546         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
547
548         // Set Command Parameters
549         pCmd->cmdType = MSG_CMD_REG_INCOMING_PUSH_MSG_CB;
550
551         // Copy Cookie
552         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
553
554         MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S cmdParam = {0};
555
556         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
557         cmdParam.msgType = MSG_SMS_TYPE;
558
559         if (pAppId)
560                 strncpy(cmdParam.appId, pAppId, MAX_WAPPUSH_ID_LEN);
561
562         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
563
564         MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd,  (pAppId)? cmdParam.appId:"NULL" );
565
566         // Send Command to Messaging FW
567         char* pEventData = NULL;
568         AutoPtr<char> eventBuf(&pEventData);
569
570
571         write((char*)pCmd, cmdSize, &pEventData);
572
573         // Get Return Data
574         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
575
576         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_PUSH_MSG_CB)
577         {
578                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
579         }
580
581         return pEvent->result;
582 }
583
584 msg_error_t MsgHandle::regCBMessageCallback(msg_cb_incoming_cb onCBIncoming, bool bSave, void *pUserParam)
585 {
586         if( (!onCBIncoming) )
587                 THROW(MsgException::INVALID_PARAM, "Param %p", onCBIncoming);
588
589         MsgProxyListener* eventListener = MsgProxyListener::instance();
590
591         eventListener->start();
592
593         if (eventListener->regCBMessageIncomingEventCB(this, onCBIncoming, bSave, pUserParam) == false) // callback was already registered, just return SUCCESS
594                 return MSG_SUCCESS;
595
596         // Allocate Memory to Command Data
597         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_CB_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
598
599         char cmdBuf[cmdSize];
600         bzero(cmdBuf, cmdSize);
601
602         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
603
604         // Set Command Parameters
605         pCmd->cmdType = MSG_CMD_REG_INCOMING_CB_MSG_CB;
606
607         // Copy Cookie
608         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
609
610         MSG_CMD_REG_CB_INCOMING_MSG_CB_S cmdParam = {0};
611
612         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
613         cmdParam.msgType = MSG_SMS_TYPE;
614         cmdParam.bsave = bSave;
615
616         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
617
618         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
619
620         // Send Command to Messaging FW
621         char* pEventData = NULL;
622         AutoPtr<char> eventBuf(&pEventData);
623
624
625         write((char*)pCmd, cmdSize, &pEventData);
626
627         // Get Return Data
628         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
629
630         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_CB_MSG_CB)
631         {
632                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
633         }
634
635         return pEvent->result;
636 }
637
638
639 msg_error_t MsgHandle::operateSyncMLMessage(msg_message_id_t msgId)
640 {
641         if( msgId < 1)
642                 THROW(MsgException::INVALID_PARAM, "Param msgId %d", msgId);
643
644         // Allocate Memory to Command Data
645         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
646
647         char cmdBuf[cmdSize];
648         bzero(cmdBuf, cmdSize);
649         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
650
651         // Set Command Parameters
652         pCmd->cmdType = MSG_CMD_SYNCML_OPERATION;
653
654         // Copy Cookie
655         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
656
657         // Copy Command Data
658         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &msgId, sizeof(msg_message_id_t));
659
660         // Send Command to Messaging FW
661         char* pEventData = NULL;
662         AutoPtr<char> eventBuf(&pEventData);
663
664
665         write((char*)pCmd, cmdSize, &pEventData);
666
667         // Get Return Data
668         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
669
670         if (pEvent->eventType != MSG_EVENT_SYNCML_OPERATION)
671         {
672                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
673         }
674
675         return pEvent->result;
676 }
677