dlog print error fix
[platform/core/messaging/msg-service.git] / proxy / MsgHandleTransport.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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 "MsgUtilFile.h"
24 #include "MsgUtilFunction.h"
25 #include "MsgProxyListener.h"
26 #include "MsgHandle.h"
27
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         reqInfo.msgInfo.addressList = NULL;
48         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&reqInfo.msgInfo.addressList, unique_ptr_deleter);
49
50         msg_struct_s *msg_s = (msg_struct_s *)pReq->msg;
51
52         MSG_MESSAGE_HIDDEN_S *reqmsg = (MSG_MESSAGE_HIDDEN_S*)msg_s->data;
53
54         if (reqmsg->simIndex <= 0) {
55                 MSG_DEBUG("Wrong SIM Index [%d]", reqmsg->simIndex);
56                 return MSG_ERR_INVALID_PARAMETER;
57         }
58
59         if (reqmsg->subType != MSG_SENDREQ_JAVA_MMS) {
60                 /* In case MMS read report, get address value later. */
61                 if (reqmsg->subType != MSG_READREPLY_MMS) {
62                         if (reqmsg->addr_list && (reqmsg->addr_list->nCount > 0) && (reqmsg->addr_list->nCount <= MAX_TO_ADDRESS_CNT)) {
63                                 MSG_DEBUG("Recipient address count [%d]", reqmsg->addr_list->nCount);
64                         } else if (g_list_length(reqmsg->addressList) > 0) {
65                                 MSG_DEBUG("Recipient address count [%d]", g_list_length(reqmsg->addressList) );
66                         } else {
67                                 MSG_DEBUG("Address count is invalid.");
68                                 return MSG_ERR_INVALID_MESSAGE;
69                         }
70                 }
71
72                 /* Begin: Setting default values for submit request */
73                 /* Set Request ID: internal use */
74                 /* pReq->msg.msgId = 0; */
75                 /* Set Folder ID */
76                 /* pReq->msg.folderId = MSG_OUTBOX_ID; */
77                 if (reqmsg->subType == MSG_RETRIEVE_MMS) {
78                         reqmsg->networkStatus = MSG_NETWORK_RETRIEVING;
79                 } else {
80                         reqmsg->networkStatus = MSG_NETWORK_SENDING;
81                 }
82
83                 reqmsg->bRead = false;
84                 /* reqmsg->bProtected = false; */
85                 reqmsg->priority = MSG_MESSAGE_PRIORITY_NORMAL;
86                 reqmsg->direction = MSG_DIRECTION_TYPE_MO;
87                 reqmsg->storageId = MSG_STORAGE_PHONE;
88
89                 time_t curTime = time(NULL);
90
91                 if (curTime < 0)
92                         THROW(MsgException::INVALID_RESULT, "time error : %s", g_strerror(errno));
93
94                 reqmsg->displayTime = curTime;
95                 /* End : Setting default values for submit request */
96         } else {
97                 /* in case of JAVA MMS msg, parse mms transaction id from pMmsData */
98                 reqmsg->networkStatus = MSG_NETWORK_SENDING;
99                 strncpy(trId, (char*)reqmsg->pMmsData+3, MMS_TR_ID_LEN);
100                 MSG_SEC_DEBUG("JavaMMS transaction Id:%s", trId);
101         }
102
103         /* Convert MSG_MESSAGE_S to MSG_MESSAGE_INFO_S */
104         convertMsgStruct(reqmsg, &(reqInfo.msgInfo));
105
106         MSG_MESSAGE_TYPE_S msgType = {0, };
107
108         msgType.mainType = reqmsg->mainType;
109         msgType.subType = reqmsg->subType;
110         msgType.classType = reqmsg->classType;
111
112         msg_struct_s *send_opt_s = (msg_struct_s *)pReq->sendOpt;
113         MSG_SENDINGOPT_S *send_opt = (MSG_SENDINGOPT_S *)send_opt_s->data;
114
115         convertSendOptStruct((const MSG_SENDINGOPT_S *)send_opt, &(reqInfo.sendOptInfo), msgType);
116
117         reqInfo.reqId = 0;
118
119         /* Register proxy info used for receiving sent status */
120         MSG_PROXY_INFO_S chInfo = {0};
121
122         chInfo.listenerFd = MsgProxyListener::instance()->getRemoteFd();
123
124         chInfo.handleAddr = (unsigned long) this;
125
126         /* Allocate Memory to Command Data */
127         char* encodedData = NULL;
128         unique_ptr<char*, void(*)(char**)> buf(&encodedData, unique_ptr_deleter);
129         int dataSize = MsgEncodeMsgInfo(&reqInfo.msgInfo, &reqInfo.sendOptInfo, &encodedData);
130
131         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_request_id_t) + dataSize + sizeof(MSG_PROXY_INFO_S);
132
133         /* In case of JAVA MMS msg, add trId */
134         if (reqmsg->subType == MSG_SENDREQ_JAVA_MMS)
135                 cmdSize += sizeof(trId);
136
137         char cmdBuf[cmdSize];
138         bzero(cmdBuf, cmdSize);
139
140         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
141
142         /* Set Command Parameters */
143         pCmd->cmdType = MSG_CMD_SUBMIT_REQ;
144
145         /* Copy Cookie */
146         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
147
148         /* Copy Command Data */
149         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &reqInfo.reqId, sizeof(msg_request_id_t));
150         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_request_id_t)), &chInfo, sizeof(MSG_PROXY_INFO_S));
151         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_request_id_t)+sizeof(MSG_PROXY_INFO_S)), encodedData, dataSize);
152
153         /* In case of JAVA MMS msg, add trId */
154         if (reqmsg->subType == MSG_SENDREQ_JAVA_MMS)
155                 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_request_id_t)+sizeof(MSG_PROXY_INFO_S)+dataSize), &trId, sizeof(trId));
156
157         /* Send Command to Messaging FW */
158         char* pEventData = NULL;
159         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
160
161         write((char*)pCmd, cmdSize, &pEventData);
162
163         /* Delete Temp File */
164         if (reqInfo.msgInfo.bTextSms == false) {
165                 MsgDeleteFile(reqInfo.msgInfo.msgData); /* ipc */
166         }
167
168         /* Get Return Data */
169         MSG_EVENT_S* pEvent = (MSG_EVENT_S*) pEventData;
170
171         int ret[3] = {0, };
172
173         memcpy(&ret, (void*)pEvent->data, sizeof(ret));
174
175         pReq->reqId = ret[0];
176         reqmsg->msgId = (msg_message_id_t)ret[1];
177         reqmsg->threadId = (msg_thread_id_t)ret[2];
178         MSG_DEBUG("SENT_REQ_ID: %d", pReq->reqId);
179         MSG_DEBUG("MSG_ID: %d", reqmsg->msgId);
180         MSG_DEBUG("THREAD_ID: %d", reqmsg->threadId);
181
182         if (pEvent->eventType != MSG_EVENT_SUBMIT_REQ) {
183                 THROW(MsgException::INVALID_RESULT, "Event Data Error:%d", pEvent->eventType);
184         }
185
186         MSG_END();
187
188         return pEvent->result;
189 }
190
191
192 msg_error_t MsgHandle::regSentStatusCallback(msg_sent_status_cb onStatusChanged, void *pUserParam)
193 {
194         if (!onStatusChanged)
195                 THROW(MsgException::INVALID_PARAM, "onStatusChanged is null");
196
197         MsgProxyListener* eventListener = MsgProxyListener::instance();
198
199         eventListener->start(this);
200
201         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
202
203         if (remoteFd == -1) {
204                 eventListener->stop();
205                 return MSG_ERR_INVALID_MSGHANDLE;
206         }
207
208         if (eventListener->regSentStatusEventCB(this, remoteFd, onStatusChanged, pUserParam) == false) {
209                 eventListener->stop();
210                 return MSG_ERR_INVALID_PARAMETER;
211         }
212
213         /* Allocate Memory to Command Data */
214         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); /* cmd type, listenerFd */
215         char cmdBuf[cmdSize];
216         bzero(cmdBuf, cmdSize);
217         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
218
219         /* Set Command Parameters */
220         pCmd->cmdType = MSG_CMD_REG_SENT_STATUS_CB;
221
222         /* Copy Cookie */
223         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
224
225         MSG_DEBUG("remote fd %d", remoteFd);
226
227         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &remoteFd, sizeof(remoteFd));
228
229         MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), remoteFd);
230
231         /* Send Command to Messaging FW */
232         char* pEventData = NULL;
233         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
234
235         write((char*)pCmd, cmdSize, &pEventData);
236
237         /* Get Return Data */
238         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
239
240         if (pEvent->eventType != MSG_EVENT_REG_SENT_STATUS_CB) {
241                 THROW(MsgException::INVALID_PARAM, "Event Data Error");
242         }
243
244         return pEvent->result;
245 }
246
247
248 msg_error_t MsgHandle::regSmsMessageCallback(msg_sms_incoming_cb onMsgIncoming, unsigned short port, void *pUserParam)
249 {
250         if (!onMsgIncoming)
251                 THROW(MsgException::INVALID_PARAM, "Param %p", onMsgIncoming);
252
253         MsgProxyListener* eventListener = MsgProxyListener::instance();
254
255         eventListener->start(this);
256
257         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
258
259         if (remoteFd == -1) {
260                 eventListener->stop();
261                 return MSG_ERR_INVALID_MSGHANDLE;
262         }
263
264         if (eventListener->regMessageIncomingEventCB(this, remoteFd, onMsgIncoming, port, pUserParam) == false) {
265                 eventListener->stop();
266                 return MSG_ERR_INVALID_PARAMETER;
267         }
268
269         /* Allocate Memory to Command Data */
270         /* sizeof(int) + sizeof; */
271         /* cmd type, listener fd */
272         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MSG_CB_S);
273
274         char cmdBuf[cmdSize];
275         bzero(cmdBuf, cmdSize);
276
277         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
278
279         /* Set Command Parameters */
280         pCmd->cmdType = MSG_CMD_REG_INCOMING_MSG_CB;
281
282         /* Copy Cookie */
283         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
284
285         MSG_CMD_REG_INCOMING_MSG_CB_S cmdParam = {0};
286
287         cmdParam.listenerFd = remoteFd;
288         cmdParam.msgType = MSG_SMS_TYPE;
289         cmdParam.port = port;
290
291         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
292
293         MSG_DEBUG("reg new msg [%s], fd %d, port %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, cmdParam.port);
294
295         /* Send Command to Messaging FW */
296         char* pEventData = NULL;
297         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
298
299
300         write((char*)pCmd, cmdSize, &pEventData);
301
302         /* Get Return Data */
303         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
304
305         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MSG_CB) {
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(this);
321
322         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
323
324         if (remoteFd == -1) {
325                 eventListener->stop();
326                 return MSG_ERR_INVALID_MSGHANDLE;
327         }
328
329         if (eventListener->regMMSConfMessageIncomingEventCB(this, remoteFd, onMMSConfMsgIncoming, pAppId, pUserParam) == false) {
330                 eventListener->stop();
331                 return MSG_ERR_INVALID_PARAMETER;
332         }
333
334         /* Allocate Memory to Command Data */
335         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S);
336
337         char cmdBuf[cmdSize];
338         bzero(cmdBuf, cmdSize);
339
340         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
341
342         /* Set Command Parameters */
343         pCmd->cmdType = MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB;
344
345         /* Copy Cookie */
346         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
347
348         MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S cmdParam = {0};
349
350         cmdParam.listenerFd = remoteFd;
351         cmdParam.msgType = MSG_MMS_TYPE;
352
353         if (pAppId)
354                 strncpy(cmdParam.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
355
356         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
357
358         MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, (pAppId)? cmdParam.appId:"NULL");
359
360         /* Send Command to Messaging FW */
361         char* pEventData = NULL;
362         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
363
364
365         write((char*)pCmd, cmdSize, &pEventData);
366
367         /* Get Return Data */
368         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
369
370         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MMS_CONF_MSG_CB) {
371                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
372         }
373
374         return pEvent->result;
375 }
376
377
378 msg_error_t MsgHandle::regSyncMLMessageCallback(msg_syncml_msg_incoming_cb onSyncMLMsgIncoming, void *pUserParam)
379 {
380         if (!onSyncMLMsgIncoming)
381                 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgIncoming);
382
383         MsgProxyListener* eventListener = MsgProxyListener::instance();
384
385         eventListener->start(this);
386
387         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
388
389         if (remoteFd == -1) {
390                 eventListener->stop();
391                 return MSG_ERR_INVALID_MSGHANDLE;
392         }
393
394         if (eventListener->regSyncMLMessageIncomingEventCB(this, remoteFd, onSyncMLMsgIncoming, pUserParam) == false) {
395                 eventListener->stop();
396                 return MSG_ERR_INVALID_PARAMETER;
397         }
398
399         /* Allocate Memory to Command Data */
400         /* sizeof(int) + sizeof; */
401         /* cmd type, listener fd */
402         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S);
403
404         char cmdBuf[cmdSize];
405         bzero(cmdBuf, cmdSize);
406
407         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
408
409         /* Set Command Parameters */
410         pCmd->cmdType = MSG_CMD_REG_INCOMING_SYNCML_MSG_CB;
411
412         /* Copy Cookie */
413         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
414
415         MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S cmdParam = {0};
416
417         cmdParam.listenerFd = remoteFd;
418         cmdParam.msgType = MSG_SMS_TYPE;
419
420         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
421
422         MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
423
424         /* Send Command to Messaging FW */
425         char* pEventData = NULL;
426         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
427
428         write((char*)pCmd, cmdSize, &pEventData);
429
430         /* Get Return Data */
431         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
432
433         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_SYNCML_MSG_CB) {
434                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
435         }
436
437         return pEvent->result;
438 }
439
440
441 msg_error_t MsgHandle::regLBSMessageCallback(msg_lbs_msg_incoming_cb onLBSMsgIncoming, void *pUserParam)
442 {
443         if (!onLBSMsgIncoming)
444                 THROW(MsgException::INVALID_PARAM, "Param %p", onLBSMsgIncoming);
445
446         MsgProxyListener* eventListener = MsgProxyListener::instance();
447
448         eventListener->start(this);
449
450         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
451
452         if (remoteFd == -1) {
453                 eventListener->stop();
454                 return MSG_ERR_INVALID_MSGHANDLE;
455         }
456
457         if (eventListener->regLBSMessageIncomingEventCB(this, remoteFd, onLBSMsgIncoming, pUserParam) == false) {
458                 eventListener->stop();
459                 return MSG_ERR_INVALID_PARAMETER;
460         }
461
462         /* Allocate Memory to Command Data */
463         /* sizeof(int) + sizeof; */
464         /* cmd type, listener fd */
465         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S);
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 = remoteFd;
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         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
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                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
497         }
498
499         return pEvent->result;
500 }
501
502
503 msg_error_t MsgHandle::regSyncMLMessageOperationCallback(msg_syncml_msg_operation_cb onSyncMLMsgOperation, void *pUserParam)
504 {
505         if (!onSyncMLMsgOperation)
506                 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgOperation);
507
508         MsgProxyListener* eventListener = MsgProxyListener::instance();
509
510         eventListener->start(this);
511
512         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
513
514         if (remoteFd == -1) {
515                 eventListener->stop();
516                 return MSG_ERR_INVALID_MSGHANDLE;
517         }
518
519         if (eventListener->regSyncMLMessageOperationEventCB(this, remoteFd, onSyncMLMsgOperation, pUserParam) == false) {
520                 eventListener->stop();
521                 return MSG_ERR_INVALID_PARAMETER;
522         }
523
524         /* Allocate Memory to Command Data */
525         /* sizeof(int) + sizeof; */
526         /* cmd type, listener fd */
527         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S);
528
529         char cmdBuf[cmdSize];
530         bzero(cmdBuf, cmdSize);
531
532         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
533
534         /* Set Command Parameters */
535         pCmd->cmdType = MSG_CMD_REG_SYNCML_MSG_OPERATION_CB;
536
537         /* Copy Cookie */
538         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
539
540         MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S cmdParam = {0};
541
542         cmdParam.listenerFd = remoteFd;
543         cmdParam.msgType = MSG_SMS_TYPE;
544
545         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
546
547         MSG_DEBUG("register syncML msg operation callback [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
548
549         /* Send Command to Messaging FW */
550         char* pEventData = NULL;
551         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
552
553
554         write((char*)pCmd, cmdSize, &pEventData);
555
556         /* Get Return Data */
557         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
558
559         if (pEvent->eventType != MSG_EVENT_REG_SYNCML_MSG_OPERATION_CB) {
560                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
561         }
562
563         return pEvent->result;
564 }
565
566
567 msg_error_t MsgHandle::regPushMessageCallback(msg_push_msg_incoming_cb onPushMsgIncoming, const char *pAppId, void *pUserParam)
568 {
569         if (!onPushMsgIncoming)
570                 THROW(MsgException::INVALID_PARAM, "Param %p", onPushMsgIncoming);
571
572         MsgProxyListener* eventListener = MsgProxyListener::instance();
573
574         eventListener->start(this);
575
576         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
577
578         if (remoteFd == -1) {
579                 eventListener->stop();
580                 return MSG_ERR_INVALID_MSGHANDLE;
581         }
582
583         if (eventListener->regPushMessageIncomingEventCB(this, remoteFd, onPushMsgIncoming, pAppId, pUserParam) == false) {
584                 eventListener->stop();
585                 return MSG_ERR_INVALID_PARAMETER;
586         }
587
588         /* Allocate Memory to Command Data */
589         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S);
590
591         char cmdBuf[cmdSize];
592         bzero(cmdBuf, cmdSize);
593
594         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
595
596         /* Set Command Parameters */
597         pCmd->cmdType = MSG_CMD_REG_INCOMING_PUSH_MSG_CB;
598
599         /* Copy Cookie */
600         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
601
602         MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S cmdParam = {0};
603
604         cmdParam.listenerFd = remoteFd;
605         cmdParam.msgType = MSG_SMS_TYPE;
606
607         if (pAppId)
608                 strncpy(cmdParam.appId, pAppId, MAX_WAPPUSH_ID_LEN);
609
610         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
611
612         MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, (pAppId)? cmdParam.appId:"NULL");
613
614         /* Send Command to Messaging FW */
615         char* pEventData = NULL;
616         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
617
618
619         write((char*)pCmd, cmdSize, &pEventData);
620
621         /* Get Return Data */
622         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
623
624         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_PUSH_MSG_CB) {
625                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
626         }
627
628         return pEvent->result;
629 }
630
631
632 msg_error_t MsgHandle::regCBMessageCallback(msg_cb_incoming_cb onCBIncoming, bool bSave, void *pUserParam)
633 {
634         if (!onCBIncoming)
635                 THROW(MsgException::INVALID_PARAM, "Param %p", onCBIncoming);
636
637         MsgProxyListener* eventListener = MsgProxyListener::instance();
638
639         eventListener->start(this);
640
641         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
642
643         if (remoteFd == -1) {
644                 eventListener->stop();
645                 return MSG_ERR_INVALID_MSGHANDLE;
646         }
647
648         if (eventListener->regCBMessageIncomingEventCB(this, remoteFd, onCBIncoming, bSave, pUserParam) == false) {
649                 eventListener->stop();
650                 return MSG_ERR_INVALID_PARAMETER;
651         }
652
653         /* Allocate Memory to Command Data */
654         /* sizeof(int) + sizeof; */
655         /* cmd type, listener fd */
656
657         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_CB_MSG_CB_S);
658
659         char cmdBuf[cmdSize];
660         bzero(cmdBuf, cmdSize);
661
662         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
663
664         /* Set Command Parameters */
665         pCmd->cmdType = MSG_CMD_REG_INCOMING_CB_MSG_CB;
666
667         /* Copy Cookie */
668         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
669
670         MSG_CMD_REG_CB_INCOMING_MSG_CB_S cmdParam = {0};
671
672         cmdParam.listenerFd = remoteFd;
673         cmdParam.msgType = MSG_SMS_TYPE;
674         cmdParam.bsave = bSave;
675
676         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
677
678         MSG_DEBUG("reg new msg [%s], fd: %d, bSave: %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, cmdParam.bsave);
679
680         /* Send Command to Messaging FW */
681         char* pEventData = NULL;
682         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
683
684
685         write((char*)pCmd, cmdSize, &pEventData);
686
687         /* Get Return Data */
688         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
689
690         if (pEvent->eventType != MSG_EVENT_REG_INCOMING_CB_MSG_CB) {
691                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
692         }
693
694         return pEvent->result;
695 }
696
697
698 msg_error_t MsgHandle::regReportMessageCallback(msg_report_msg_incoming_cb onReportMsgCB, void *pUserParam)
699 {
700         if (!onReportMsgCB)
701                 THROW(MsgException::INVALID_PARAM, "onReportMsgCB is null");
702
703         MsgProxyListener* eventListener = MsgProxyListener::instance();
704
705         eventListener->start(this);
706
707         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
708
709         if (remoteFd == -1) {
710                 eventListener->stop();
711                 return MSG_ERR_INVALID_MSGHANDLE;
712         }
713
714         if (eventListener->regReportMsgIncomingCB(this, remoteFd, onReportMsgCB, pUserParam) == false) {
715                 eventListener->stop();
716                 return MSG_ERR_INVALID_PARAMETER;
717         }
718
719         /* Allocate Memory to Command Data */
720         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); /* cmd type, listenerFd */
721         char cmdBuf[cmdSize];
722         bzero(cmdBuf, cmdSize);
723         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
724
725         /* Set Command Parameters */
726         pCmd->cmdType = MSG_CMD_REG_REPORT_MSG_INCOMING_CB;
727
728         /* Copy Cookie */
729         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
730
731         MSG_DEBUG("remote fd %d", remoteFd);
732
733         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &remoteFd, sizeof(remoteFd));
734
735         MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), remoteFd);
736
737         /* Send Command to Messaging FW */
738         char* pEventData = NULL;
739         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
740
741         write((char*)pCmd, cmdSize, &pEventData);
742
743         /* Get Return Data */
744         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
745
746         if (pEvent->eventType != MSG_EVENT_REG_REPORT_MSG_INCOMING_CB) {
747                 THROW(MsgException::INVALID_PARAM, "Event Data Error");
748         }
749
750         return pEvent->result;
751 }
752
753
754 msg_error_t MsgHandle::operateSyncMLMessage(msg_message_id_t msgId)
755 {
756         if (msgId < 1)
757                 THROW(MsgException::INVALID_PARAM, "Param msgId %d", msgId);
758
759         /* Allocate Memory to Command Data */
760         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
761
762         char cmdBuf[cmdSize];
763         bzero(cmdBuf, cmdSize);
764         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
765
766         /* Set Command Parameters */
767         pCmd->cmdType = MSG_CMD_SYNCML_OPERATION;
768
769         /* Copy Cookie */
770         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
771
772         /* Copy Command Data */
773         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &msgId, sizeof(msg_message_id_t));
774
775         /* Send Command to Messaging FW */
776         char* pEventData = NULL;
777         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
778
779
780         write((char*)pCmd, cmdSize, &pEventData);
781
782         /* Get Return Data */
783         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
784
785         if (pEvent->eventType != MSG_EVENT_SYNCML_OPERATION) {
786                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
787         }
788
789         return pEvent->result;
790 }