Remove THROW exception in sms submit request & remove lock and wait in get remote FD.
[framework/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 #ifdef __x86_64__
119         chInfo.handleAddr = (uint64_t) this;
120 #else
121         chInfo.handleAddr = (unsigned int) this;
122 #endif
123
124         /* Allocate Memory to Command Data */
125         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_REQUEST_INFO_S) + sizeof(MSG_PROXY_INFO_S);
126
127         // In case of JAVA MMS msg, add trId
128         if (reqmsg->subType == MSG_SENDREQ_JAVA_MMS)
129                 cmdSize += sizeof(trId);
130
131         char cmdBuf[cmdSize];
132         bzero(cmdBuf, cmdSize);
133
134         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
135
136         // Set Command Parameters
137         pCmd->cmdType = MSG_CMD_SUBMIT_REQ;
138
139         // Copy Cookie
140         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
141
142         // Copy Command Data
143         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &reqInfo, sizeof(MSG_REQUEST_INFO_S));
144         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(MSG_REQUEST_INFO_S)), &chInfo, sizeof(MSG_PROXY_INFO_S));
145
146         // In case of JAVA MMS msg, add trId
147         if (reqmsg->subType == MSG_SENDREQ_JAVA_MMS)
148                 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));
149
150         // Send Command to Messaging FW
151         char* pEventData = NULL;
152         AutoPtr<char> eventBuf(&pEventData);
153
154         write((char*)pCmd, cmdSize, &pEventData);
155
156         // Get Return Data
157         MSG_EVENT_S* pEvent = (MSG_EVENT_S*) pEventData;
158
159         int* pReqId = (int*) pEvent->data;
160         pReq->reqId = *pReqId;
161         MSG_DEBUG("SENT_REQ_ID: %d", pReq->reqId);
162
163         if (pEvent->eventType != MSG_EVENT_SUBMIT_REQ)
164         {
165                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
166         }
167
168         MSG_END();
169
170         return pEvent->result;
171 }
172
173
174 msg_error_t MsgHandle::cancelReq(msg_request_id_t reqId)
175 {
176         // Allocate Memory to Command Data
177         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_request_id_t);
178
179         char cmdBuf[cmdSize];
180         bzero(cmdBuf, cmdSize);
181
182         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
183
184         // Set Command Parameters
185         pCmd->cmdType = MSG_CMD_CANCEL_REQ;
186
187         // Copy Cookie
188         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
189
190         // Copy Command Data
191         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &reqId, sizeof(msg_request_id_t));
192
193         // Send Command to Messaging FW
194         char* pEventData = NULL;
195         AutoPtr<char> eventBuf(&pEventData);
196
197
198         write((char*)pCmd, cmdSize, &pEventData);
199
200         // Get Return Data
201         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
202
203         if (pEvent->eventType != MSG_EVENT_CANCEL_REQ)
204         {
205                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
206         }
207
208         return pEvent->result;
209 }
210
211
212 msg_error_t MsgHandle::regSentStatusCallback(msg_sent_status_cb onStatusChanged, void *pUserParam)
213 {
214         if (!onStatusChanged)
215                 THROW(MsgException::INVALID_PARAM, "onStatusChanged is null");
216
217         MsgProxyListener* eventListener = MsgProxyListener::instance();
218
219         eventListener->start();
220
221         int clientFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
222
223         if (clientFd < 0)
224                 return MSG_ERR_TRANSPORT_ERROR;
225
226         if (eventListener->regSentStatusEventCB(this, onStatusChanged, pUserParam) == false) // callback was already registered, just return SUCCESS
227                 return MSG_SUCCESS;
228
229         // Allocate Memory to Command Data
230         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); // cmd type, listenerFd
231         char cmdBuf[cmdSize];
232         bzero(cmdBuf, cmdSize);
233         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
234
235         // Set Command Parameters
236         pCmd->cmdType = MSG_CMD_REG_SENT_STATUS_CB;
237
238         // Copy Cookie
239         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
240
241         int listenerFd = clientFd;
242
243         MSG_DEBUG("remote fd %d", listenerFd);
244
245         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &listenerFd, sizeof(listenerFd));
246
247         MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), listenerFd);
248
249         // Send Command to Messaging FW
250         char* pEventData = NULL;
251         AutoPtr<char> eventBuf(&pEventData);
252
253         write((char*)pCmd, cmdSize, &pEventData);
254
255         // Get Return Data
256         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
257
258         if (pEvent->eventType != MSG_EVENT_REG_SENT_STATUS_CB)
259         {
260                 THROW(MsgException::INVALID_PARAM, "Event Data Error");
261         }
262
263         return pEvent->result;
264 }
265
266
267 msg_error_t MsgHandle::regSmsMessageCallback(msg_sms_incoming_cb onMsgIncoming, unsigned short port, void *pUserParam)
268 {
269         if( (!onMsgIncoming) )
270                 THROW(MsgException::INVALID_PARAM, "Param %p", onMsgIncoming);
271
272         MsgProxyListener* eventListener = MsgProxyListener::instance();
273
274         eventListener->start();
275
276         int clientFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
277
278         if (clientFd < 0)
279                 return MSG_ERR_TRANSPORT_ERROR;
280
281         if (eventListener->regMessageIncomingEventCB(this, onMsgIncoming, port, pUserParam) == false) // callback was already registered, just return SUCCESS
282                 return MSG_SUCCESS;
283
284         // Allocate Memory to Command Data
285         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
286
287         char cmdBuf[cmdSize];
288         bzero(cmdBuf, cmdSize);
289
290         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
291
292         // Set Command Parameters
293         pCmd->cmdType = MSG_CMD_REG_INCOMING_MSG_CB;
294
295         // Copy Cookie
296         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
297
298         MSG_CMD_REG_INCOMING_MSG_CB_S cmdParam = {0};
299
300         cmdParam.listenerFd = clientFd;
301         cmdParam.msgType = MSG_SMS_TYPE;
302         cmdParam.port = port;
303
304         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
305
306         MSG_DEBUG("reg new msg [%s], fd %d, port %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, cmdParam.port);
307
308         // Send Command to Messaging FW
309         char* pEventData = NULL;
310         AutoPtr<char> eventBuf(&pEventData);
311
312
313         write((char*)pCmd, cmdSize, &pEventData);
314
315         // Get Return Data
316         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
317
318         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MSG_CB)
319         {
320                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
321         }
322
323         return pEvent->result;
324 }
325
326
327 msg_error_t MsgHandle::regMmsConfMessageCallback(msg_mms_conf_msg_incoming_cb onMMSConfMsgIncoming, const char *pAppId, void *pUserParam)
328 {
329         if( (!onMMSConfMsgIncoming) )
330                 THROW(MsgException::INVALID_PARAM, "Param %p", onMMSConfMsgIncoming);
331
332         MsgProxyListener* eventListener = MsgProxyListener::instance();
333
334         eventListener->start();
335
336         int clientFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
337
338         if (clientFd < 0)
339                 return MSG_ERR_TRANSPORT_ERROR;
340
341         if (eventListener->regMMSConfMessageIncomingEventCB(this, onMMSConfMsgIncoming, pAppId, pUserParam) == false) // callback was already registered, just return SUCCESS
342                 return MSG_SUCCESS;
343
344         // Allocate Memory to Command Data
345         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S);
346
347         char cmdBuf[cmdSize];
348         bzero(cmdBuf, cmdSize);
349
350         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
351
352         // Set Command Parameters
353         pCmd->cmdType = MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB;
354
355         // Copy Cookie
356         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
357
358         MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S cmdParam = {0};
359
360         cmdParam.listenerFd = clientFd;
361         cmdParam.msgType = MSG_MMS_TYPE;
362
363         if (pAppId)
364                 strncpy(cmdParam.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
365
366         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
367
368         MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd,  (pAppId)? cmdParam.appId:"NULL" );
369
370         // Send Command to Messaging FW
371         char* pEventData = NULL;
372         AutoPtr<char> eventBuf(&pEventData);
373
374
375         write((char*)pCmd, cmdSize, &pEventData);
376
377         // Get Return Data
378         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
379
380         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MMS_CONF_MSG_CB)
381         {
382                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
383         }
384
385         return pEvent->result;
386 }
387
388
389 msg_error_t MsgHandle::regSyncMLMessageCallback(msg_syncml_msg_incoming_cb onSyncMLMsgIncoming, void *pUserParam)
390 {
391         if( (!onSyncMLMsgIncoming) )
392                 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgIncoming);
393
394         MsgProxyListener* eventListener = MsgProxyListener::instance();
395
396         eventListener->start();
397
398         int clientFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
399
400         if (clientFd < 0)
401                 return MSG_ERR_TRANSPORT_ERROR;
402
403         if (eventListener->regSyncMLMessageIncomingEventCB(this, onSyncMLMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
404                 return MSG_SUCCESS;
405
406         // Allocate Memory to Command Data
407         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
408
409         char cmdBuf[cmdSize];
410         bzero(cmdBuf, cmdSize);
411
412         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
413
414         // Set Command Parameters
415         pCmd->cmdType = MSG_CMD_REG_INCOMING_SYNCML_MSG_CB;
416
417         // Copy Cookie
418         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
419
420         MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S cmdParam = {0};
421
422         cmdParam.listenerFd = clientFd;
423         cmdParam.msgType = MSG_SMS_TYPE;
424
425         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
426
427         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
428
429         // Send Command to Messaging FW
430         char* pEventData = NULL;
431         AutoPtr<char> eventBuf(&pEventData);
432
433         write((char*)pCmd, cmdSize, &pEventData);
434
435         // Get Return Data
436         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
437
438         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_SYNCML_MSG_CB)
439         {
440                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
441         }
442
443         return pEvent->result;
444 }
445
446
447 msg_error_t MsgHandle::regLBSMessageCallback(msg_lbs_msg_incoming_cb onLBSMsgIncoming, void *pUserParam)
448 {
449         if( (!onLBSMsgIncoming) )
450                 THROW(MsgException::INVALID_PARAM, "Param %p", onLBSMsgIncoming);
451
452         MsgProxyListener* eventListener = MsgProxyListener::instance();
453
454         eventListener->start();
455
456         int clientFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
457
458         if (clientFd < 0)
459                 return MSG_ERR_TRANSPORT_ERROR;
460
461         if (eventListener->regLBSMessageIncomingEventCB(this, onLBSMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
462                 return MSG_SUCCESS;
463
464         // Allocate Memory to Command Data
465         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
466         char cmdBuf[cmdSize];
467         bzero(cmdBuf, cmdSize);
468         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
469
470         // Set Command Parameters
471         pCmd->cmdType = MSG_CMD_REG_INCOMING_LBS_MSG_CB;
472
473         // Copy Cookie
474         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
475
476         MSG_CMD_REG_INCOMING_LBS_MSG_CB_S cmdParam = {0};
477
478         cmdParam.listenerFd = clientFd;
479         cmdParam.msgType = MSG_SMS_TYPE;
480
481         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
482
483         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
484
485         // Send Command to Messaging FW
486         char* pEventData = NULL;
487         AutoPtr<char> eventBuf(&pEventData);
488
489
490         write((char*)pCmd, cmdSize, &pEventData);
491
492         // Get Return Data
493         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
494
495         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_LBS_MSG_CB)
496         {
497                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
498         }
499
500         return pEvent->result;
501 }
502
503
504 msg_error_t MsgHandle::regSyncMLMessageOperationCallback(msg_syncml_msg_operation_cb onSyncMLMsgOperation, void *pUserParam)
505 {
506         if( (!onSyncMLMsgOperation) )
507                 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgOperation);
508
509         MsgProxyListener* eventListener = MsgProxyListener::instance();
510
511         eventListener->start();
512
513         int clientFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
514
515         if (clientFd < 0)
516                 return MSG_ERR_TRANSPORT_ERROR;
517
518         if (eventListener->regSyncMLMessageOperationEventCB(this, onSyncMLMsgOperation, pUserParam) == false) // callback was already registered, just return SUCCESS
519                 return MSG_SUCCESS;
520
521         // Allocate Memory to Command Data
522         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
523
524         char cmdBuf[cmdSize];
525         bzero(cmdBuf, cmdSize);
526
527         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
528
529         // Set Command Parameters
530         pCmd->cmdType = MSG_CMD_REG_SYNCML_MSG_OPERATION_CB;
531
532         // Copy Cookie
533         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
534
535         MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S cmdParam = {0};
536
537         cmdParam.listenerFd = clientFd;
538         cmdParam.msgType = MSG_SMS_TYPE;
539
540         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
541
542         MSG_DEBUG("register syncML msg operation callback [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
543
544         // Send Command to Messaging FW
545         char* pEventData = NULL;
546         AutoPtr<char> eventBuf(&pEventData);
547
548
549         write((char*)pCmd, cmdSize, &pEventData);
550
551         // Get Return Data
552         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
553
554         if (pEvent->eventType != MSG_EVENT_REG_SYNCML_MSG_OPERATION_CB)
555         {
556                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
557         }
558
559         return pEvent->result;
560 }
561
562 msg_error_t MsgHandle::regPushMessageCallback(msg_push_msg_incoming_cb onPushMsgIncoming, const char *pAppId, void *pUserParam)
563 {
564         if( (!onPushMsgIncoming) )
565                 THROW(MsgException::INVALID_PARAM, "Param %p", onPushMsgIncoming);
566
567         MsgProxyListener* eventListener = MsgProxyListener::instance();
568
569         eventListener->start();
570
571         int clientFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
572
573         if (clientFd < 0)
574                 return MSG_ERR_TRANSPORT_ERROR;
575
576         if (eventListener->regPushMessageIncomingEventCB(this, onPushMsgIncoming, pAppId, pUserParam) == false) // callback was already registered, just return SUCCESS
577                 return MSG_SUCCESS;
578
579         // Allocate Memory to Command Data
580         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S);
581
582         char cmdBuf[cmdSize];
583         bzero(cmdBuf, cmdSize);
584
585         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
586
587         // Set Command Parameters
588         pCmd->cmdType = MSG_CMD_REG_INCOMING_PUSH_MSG_CB;
589
590         // Copy Cookie
591         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
592
593         MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S cmdParam = {0};
594
595         cmdParam.listenerFd = clientFd;
596         cmdParam.msgType = MSG_SMS_TYPE;
597
598         if (pAppId)
599                 strncpy(cmdParam.appId, pAppId, MAX_WAPPUSH_ID_LEN);
600
601         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
602
603         MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd,  (pAppId)? cmdParam.appId:"NULL" );
604
605         // Send Command to Messaging FW
606         char* pEventData = NULL;
607         AutoPtr<char> eventBuf(&pEventData);
608
609
610         write((char*)pCmd, cmdSize, &pEventData);
611
612         // Get Return Data
613         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
614
615         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_PUSH_MSG_CB)
616         {
617                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
618         }
619
620         return pEvent->result;
621 }
622
623 msg_error_t MsgHandle::regCBMessageCallback(msg_cb_incoming_cb onCBIncoming, bool bSave, void *pUserParam)
624 {
625         if( (!onCBIncoming) )
626                 THROW(MsgException::INVALID_PARAM, "Param %p", onCBIncoming);
627
628         MsgProxyListener* eventListener = MsgProxyListener::instance();
629
630         eventListener->start();
631
632         int clientFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
633
634         if (clientFd < 0)
635                 return MSG_ERR_TRANSPORT_ERROR;
636
637         if (eventListener->regCBMessageIncomingEventCB(this, onCBIncoming, bSave, pUserParam) == false) // callback was already registered, just return SUCCESS
638                 return MSG_SUCCESS;
639
640         // Allocate Memory to Command Data
641         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_CB_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
642
643         char cmdBuf[cmdSize];
644         bzero(cmdBuf, cmdSize);
645
646         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
647
648         // Set Command Parameters
649         pCmd->cmdType = MSG_CMD_REG_INCOMING_CB_MSG_CB;
650
651         // Copy Cookie
652         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
653
654         MSG_CMD_REG_CB_INCOMING_MSG_CB_S cmdParam = {0};
655
656         cmdParam.listenerFd = clientFd; // fd that is reserved to the "listener thread" by msgfw daemon
657         cmdParam.msgType = MSG_SMS_TYPE;
658         cmdParam.bsave = bSave;
659
660         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
661
662         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
663
664         // Send Command to Messaging FW
665         char* pEventData = NULL;
666         AutoPtr<char> eventBuf(&pEventData);
667
668
669         write((char*)pCmd, cmdSize, &pEventData);
670
671         // Get Return Data
672         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
673
674         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_CB_MSG_CB)
675         {
676                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
677         }
678
679         return pEvent->result;
680 }
681
682
683 msg_error_t MsgHandle::operateSyncMLMessage(msg_message_id_t msgId)
684 {
685         if( msgId < 1)
686                 THROW(MsgException::INVALID_PARAM, "Param msgId %d", msgId);
687
688         // Allocate Memory to Command Data
689         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
690
691         char cmdBuf[cmdSize];
692         bzero(cmdBuf, cmdSize);
693         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
694
695         // Set Command Parameters
696         pCmd->cmdType = MSG_CMD_SYNCML_OPERATION;
697
698         // Copy Cookie
699         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
700
701         // Copy Command Data
702         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &msgId, sizeof(msg_message_id_t));
703
704         // Send Command to Messaging FW
705         char* pEventData = NULL;
706         AutoPtr<char> eventBuf(&pEventData);
707
708
709         write((char*)pCmd, cmdSize, &pEventData);
710
711         // Get Return Data
712         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
713
714         if (pEvent->eventType != MSG_EVENT_SYNCML_OPERATION)
715         {
716                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
717         }
718
719         return pEvent->result;
720 }
721