16039f34e5b7225b1a404243e4566e9d7228eedf
[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         if (eventListener->regSentStatusEventCB(this, onStatusChanged, pUserParam) == false) // callback was already registered, just return SUCCESS
222                 return MSG_SUCCESS;
223
224         // Allocate Memory to Command Data
225         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); // cmd type, listenerFd
226         char cmdBuf[cmdSize];
227         bzero(cmdBuf, cmdSize);
228         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
229
230         // Set Command Parameters
231         pCmd->cmdType = MSG_CMD_REG_SENT_STATUS_CB;
232
233         // Copy Cookie
234         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
235
236         int listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
237
238         MSG_DEBUG("remote fd %d", listenerFd);
239
240         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &listenerFd, sizeof(listenerFd));
241
242         MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), listenerFd);
243
244         // Send Command to Messaging FW
245         char* pEventData = NULL;
246         AutoPtr<char> eventBuf(&pEventData);
247
248         write((char*)pCmd, cmdSize, &pEventData);
249
250         // Get Return Data
251         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
252
253         if (pEvent->eventType != MSG_EVENT_REG_SENT_STATUS_CB)
254         {
255                 THROW(MsgException::INVALID_PARAM, "Event Data Error");
256         }
257
258         return pEvent->result;
259 }
260
261
262 msg_error_t MsgHandle::regSmsMessageCallback(msg_sms_incoming_cb onMsgIncoming, unsigned short port, void *pUserParam)
263 {
264         if( (!onMsgIncoming) )
265                 THROW(MsgException::INVALID_PARAM, "Param %p", onMsgIncoming);
266
267         MsgProxyListener* eventListener = MsgProxyListener::instance();
268
269         eventListener->start();
270
271         if (eventListener->regMessageIncomingEventCB(this, onMsgIncoming, port, pUserParam) == false) // callback was already registered, just return SUCCESS
272                 return MSG_SUCCESS;
273
274         // Allocate Memory to Command Data
275         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
276
277         char cmdBuf[cmdSize];
278         bzero(cmdBuf, cmdSize);
279
280         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
281
282         // Set Command Parameters
283         pCmd->cmdType = MSG_CMD_REG_INCOMING_MSG_CB;
284
285         // Copy Cookie
286         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
287
288         MSG_CMD_REG_INCOMING_MSG_CB_S cmdParam = {0};
289
290         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
291         cmdParam.msgType = MSG_SMS_TYPE;
292         cmdParam.port = port;
293
294         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
295
296         MSG_DEBUG("reg new msg [%s], fd %d, port %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, cmdParam.port);
297
298         // Send Command to Messaging FW
299         char* pEventData = NULL;
300         AutoPtr<char> eventBuf(&pEventData);
301
302
303         write((char*)pCmd, cmdSize, &pEventData);
304
305         // Get Return Data
306         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
307
308         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MSG_CB)
309         {
310                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
311         }
312
313         return pEvent->result;
314 }
315
316
317 msg_error_t MsgHandle::regMmsConfMessageCallback(msg_mms_conf_msg_incoming_cb onMMSConfMsgIncoming, const char *pAppId, void *pUserParam)
318 {
319         if( (!onMMSConfMsgIncoming) )
320                 THROW(MsgException::INVALID_PARAM, "Param %p", onMMSConfMsgIncoming);
321
322         MsgProxyListener* eventListener = MsgProxyListener::instance();
323
324         eventListener->start();
325
326         if (eventListener->regMMSConfMessageIncomingEventCB(this, onMMSConfMsgIncoming, pAppId, pUserParam) == false) // callback was already registered, just return SUCCESS
327                 return MSG_SUCCESS;
328
329         // Allocate Memory to Command Data
330         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S);
331
332         char cmdBuf[cmdSize];
333         bzero(cmdBuf, cmdSize);
334
335         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
336
337         // Set Command Parameters
338         pCmd->cmdType = MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB;
339
340         // Copy Cookie
341         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
342
343         MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S cmdParam = {0};
344
345         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
346         cmdParam.msgType = MSG_MMS_TYPE;
347
348         if (pAppId)
349                 strncpy(cmdParam.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
350
351         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
352
353         MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd,  (pAppId)? cmdParam.appId:"NULL" );
354
355         // Send Command to Messaging FW
356         char* pEventData = NULL;
357         AutoPtr<char> eventBuf(&pEventData);
358
359
360         write((char*)pCmd, cmdSize, &pEventData);
361
362         // Get Return Data
363         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
364
365         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MMS_CONF_MSG_CB)
366         {
367                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
368         }
369
370         return pEvent->result;
371 }
372
373
374 msg_error_t MsgHandle::regSyncMLMessageCallback(msg_syncml_msg_incoming_cb onSyncMLMsgIncoming, void *pUserParam)
375 {
376         if( (!onSyncMLMsgIncoming) )
377                 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgIncoming);
378
379         MsgProxyListener* eventListener = MsgProxyListener::instance();
380
381         eventListener->start();
382
383         if (eventListener->regSyncMLMessageIncomingEventCB(this, onSyncMLMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
384                 return MSG_SUCCESS;
385
386         // Allocate Memory to Command Data
387         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
388
389         char cmdBuf[cmdSize];
390         bzero(cmdBuf, cmdSize);
391
392         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
393
394         // Set Command Parameters
395         pCmd->cmdType = MSG_CMD_REG_INCOMING_SYNCML_MSG_CB;
396
397         // Copy Cookie
398         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
399
400         MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S cmdParam = {0};
401
402         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
403         cmdParam.msgType = MSG_SMS_TYPE;
404
405         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
406
407         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
408
409         // Send Command to Messaging FW
410         char* pEventData = NULL;
411         AutoPtr<char> eventBuf(&pEventData);
412
413         write((char*)pCmd, cmdSize, &pEventData);
414
415         // Get Return Data
416         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
417
418         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_SYNCML_MSG_CB)
419         {
420                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
421         }
422
423         return pEvent->result;
424 }
425
426
427 msg_error_t MsgHandle::regLBSMessageCallback(msg_lbs_msg_incoming_cb onLBSMsgIncoming, void *pUserParam)
428 {
429         if( (!onLBSMsgIncoming) )
430                 THROW(MsgException::INVALID_PARAM, "Param %p", onLBSMsgIncoming);
431
432         MsgProxyListener* eventListener = MsgProxyListener::instance();
433
434         eventListener->start();
435
436         if (eventListener->regLBSMessageIncomingEventCB(this, onLBSMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
437                 return MSG_SUCCESS;
438
439         // Allocate Memory to Command Data
440         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
441         char cmdBuf[cmdSize];
442         bzero(cmdBuf, cmdSize);
443         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
444
445         // Set Command Parameters
446         pCmd->cmdType = MSG_CMD_REG_INCOMING_LBS_MSG_CB;
447
448         // Copy Cookie
449         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
450
451         MSG_CMD_REG_INCOMING_LBS_MSG_CB_S cmdParam = {0};
452
453         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
454         cmdParam.msgType = MSG_SMS_TYPE;
455
456         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
457
458         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
459
460         // Send Command to Messaging FW
461         char* pEventData = NULL;
462         AutoPtr<char> eventBuf(&pEventData);
463
464
465         write((char*)pCmd, cmdSize, &pEventData);
466
467         // Get Return Data
468         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
469
470         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_LBS_MSG_CB)
471         {
472                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
473         }
474
475         return pEvent->result;
476 }
477
478
479 msg_error_t MsgHandle::regSyncMLMessageOperationCallback(msg_syncml_msg_operation_cb onSyncMLMsgOperation, void *pUserParam)
480 {
481         if( (!onSyncMLMsgOperation) )
482                 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgOperation);
483
484         MsgProxyListener* eventListener = MsgProxyListener::instance();
485
486         eventListener->start();
487
488         if (eventListener->regSyncMLMessageOperationEventCB(this, onSyncMLMsgOperation, pUserParam) == false) // callback was already registered, just return SUCCESS
489                 return MSG_SUCCESS;
490
491         // Allocate Memory to Command Data
492         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
493
494         char cmdBuf[cmdSize];
495         bzero(cmdBuf, cmdSize);
496
497         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
498
499         // Set Command Parameters
500         pCmd->cmdType = MSG_CMD_REG_SYNCML_MSG_OPERATION_CB;
501
502         // Copy Cookie
503         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
504
505         MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S cmdParam = {0};
506
507         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
508         cmdParam.msgType = MSG_SMS_TYPE;
509
510         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
511
512         MSG_DEBUG("register syncML msg operation callback [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
513
514         // Send Command to Messaging FW
515         char* pEventData = NULL;
516         AutoPtr<char> eventBuf(&pEventData);
517
518
519         write((char*)pCmd, cmdSize, &pEventData);
520
521         // Get Return Data
522         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
523
524         if (pEvent->eventType != MSG_EVENT_REG_SYNCML_MSG_OPERATION_CB)
525         {
526                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
527         }
528
529         return pEvent->result;
530 }
531
532 msg_error_t MsgHandle::regPushMessageCallback(msg_push_msg_incoming_cb onPushMsgIncoming, const char *pAppId, void *pUserParam)
533 {
534         if( (!onPushMsgIncoming) )
535                 THROW(MsgException::INVALID_PARAM, "Param %p", onPushMsgIncoming);
536
537         MsgProxyListener* eventListener = MsgProxyListener::instance();
538
539         eventListener->start();
540
541         if (eventListener->regPushMessageIncomingEventCB(this, onPushMsgIncoming, pAppId, pUserParam) == false) // callback was already registered, just return SUCCESS
542                 return MSG_SUCCESS;
543
544         // Allocate Memory to Command Data
545         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S);
546
547         char cmdBuf[cmdSize];
548         bzero(cmdBuf, cmdSize);
549
550         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
551
552         // Set Command Parameters
553         pCmd->cmdType = MSG_CMD_REG_INCOMING_PUSH_MSG_CB;
554
555         // Copy Cookie
556         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
557
558         MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S cmdParam = {0};
559
560         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
561         cmdParam.msgType = MSG_SMS_TYPE;
562
563         if (pAppId)
564                 strncpy(cmdParam.appId, pAppId, MAX_WAPPUSH_ID_LEN);
565
566         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
567
568         MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd,  (pAppId)? cmdParam.appId:"NULL" );
569
570         // Send Command to Messaging FW
571         char* pEventData = NULL;
572         AutoPtr<char> eventBuf(&pEventData);
573
574
575         write((char*)pCmd, cmdSize, &pEventData);
576
577         // Get Return Data
578         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
579
580         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_PUSH_MSG_CB)
581         {
582                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
583         }
584
585         return pEvent->result;
586 }
587
588 msg_error_t MsgHandle::regCBMessageCallback(msg_cb_incoming_cb onCBIncoming, bool bSave, void *pUserParam)
589 {
590         if( (!onCBIncoming) )
591                 THROW(MsgException::INVALID_PARAM, "Param %p", onCBIncoming);
592
593         MsgProxyListener* eventListener = MsgProxyListener::instance();
594
595         eventListener->start();
596
597         if (eventListener->regCBMessageIncomingEventCB(this, onCBIncoming, bSave, pUserParam) == false) // callback was already registered, just return SUCCESS
598                 return MSG_SUCCESS;
599
600         // Allocate Memory to Command Data
601         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_CB_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
602
603         char cmdBuf[cmdSize];
604         bzero(cmdBuf, cmdSize);
605
606         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
607
608         // Set Command Parameters
609         pCmd->cmdType = MSG_CMD_REG_INCOMING_CB_MSG_CB;
610
611         // Copy Cookie
612         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
613
614         MSG_CMD_REG_CB_INCOMING_MSG_CB_S cmdParam = {0};
615
616         cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
617         cmdParam.msgType = MSG_SMS_TYPE;
618         cmdParam.bsave = bSave;
619
620         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
621
622         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
623
624         // Send Command to Messaging FW
625         char* pEventData = NULL;
626         AutoPtr<char> eventBuf(&pEventData);
627
628
629         write((char*)pCmd, cmdSize, &pEventData);
630
631         // Get Return Data
632         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
633
634         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_CB_MSG_CB)
635         {
636                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
637         }
638
639         return pEvent->result;
640 }
641
642
643 msg_error_t MsgHandle::operateSyncMLMessage(msg_message_id_t msgId)
644 {
645         if( msgId < 1)
646                 THROW(MsgException::INVALID_PARAM, "Param msgId %d", msgId);
647
648         // Allocate Memory to Command Data
649         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
650
651         char cmdBuf[cmdSize];
652         bzero(cmdBuf, cmdSize);
653         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
654
655         // Set Command Parameters
656         pCmd->cmdType = MSG_CMD_SYNCML_OPERATION;
657
658         // Copy Cookie
659         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
660
661         // Copy Command Data
662         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &msgId, sizeof(msg_message_id_t));
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_SYNCML_OPERATION)
675         {
676                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
677         }
678
679         return pEvent->result;
680 }
681