87f338281c1ab04b83772e02d4605905e6507000
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Messaging / CallbackMgr.cpp
1 /*
2  * Copyright (c) 2011 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  *
18  *
19  * @file       CallbackMgr.cpp
20  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24
25 #include "CallbackMgr.h"
26 #include <dpl/log/log.h>
27 #include <dpl/singleton_impl.h>
28 #include "ISendingObserver.h"
29 #include "MsgServiceHandleMgr.h"
30
31 extern "C" {
32 #include <MapiTransport.h>
33 #include <MapiControl.h>
34 #include <MapiMessage.h>
35 }
36
37 IMPLEMENT_SINGLETON(WrtDeviceApis::Messaging::CallbackMgr)
38
39 namespace WrtDeviceApis {
40 namespace Messaging {
41
42 CallbackMgr::CallbackMgr()
43 {
44     LogInfo("enter");
45     // register callback once per process
46     if (NULL == MsgGetCommonHandle()) {
47         LogError("Unable to open handle");
48     } else {
49         if (msg_reg_sent_status_callback(MsgGetCommonHandle(), sendCallback,
50                                          static_cast<void*>(this)) !=
51             MSG_SUCCESS) {
52             LogError("callback registration error");
53         } else {
54             LogDebug("callback registred succesfully");
55         }
56     }
57 }
58
59 CallbackMgr::~CallbackMgr()
60 {
61     LogInfo("enter");
62 }
63
64 MSG_ERROR_T CallbackMgr::registerAndSend(SendingFunction sendingFn,
65         const msg_message_t& message,
66         ISendingObserver* observer)
67 {
68     LogDebug("trying to send message, msgId=" << msg_get_message_id(message));
69
70     MSG_SENDINGOPT_S pSendOpt;
71     memset(&pSendOpt, 0, sizeof(MSG_SENDINGOPT_S));
72     MSG_REQUEST_S req;
73     memset(&req, 0, sizeof(MSG_REQUEST_S));
74     req.reqId = 0;
75     req.msg = message;
76     req.sendOpt = pSendOpt;
77
78     DPL::Mutex::ScopedLock lock(&m_mutex);
79     MSG_ERROR_T err = (*sendingFn)(MsgGetCommonHandle(), &req);
80     if (err == MSG_SUCCESS) {
81         if (observer) {
82             m_sendRequests[req.reqId] = observer;
83             observer->setSendigRequestId(req.reqId);
84             observer->setRecipient(msg_get_ith_address(message, 0));
85         }
86     }
87     return err;
88 }
89
90 void CallbackMgr::unregister(int reqId)
91 {
92     DPL::Mutex::ScopedLock lock(&m_mutex);
93
94     SendReqMap::iterator it = m_sendRequests.find(reqId);
95     if (it == m_sendRequests.end()) {
96         LogWarning("No matching request found");
97         return;
98     }
99     LogInfo("Matching send request found!");
100     m_sendRequests.erase(it);
101 }
102
103 void CallbackMgr::sendCallback(MSG_HANDLE_T /*handle*/,
104                                MSG_SENT_STATUS_S *sent_status,
105                                void *user_param)
106 {
107     LogInfo(
108         "callback received. Req id = " << sent_status->reqId <<
109         " Status = " << (int)sent_status->status);
110     CallbackMgr* instance = static_cast<CallbackMgr*>(user_param);
111     if (!instance) {
112         LogError("Empty user data!");
113         return;
114     }
115
116     instance->call(sent_status);
117 }
118
119 void CallbackMgr::call(MSG_SENT_STATUS_S *sent_status)
120 {
121     DPL::Mutex::ScopedLock lock(&m_mutex);
122
123     SendReqMap::iterator it = m_sendRequests.find(sent_status->reqId);
124     if (it == m_sendRequests.end()) {
125         LogWarning("No matching request found");
126         return;
127     }
128     LogInfo("Matching send request found!");
129
130     // call it
131     it->second->sendingCallback(sent_status);
132
133     m_sendRequests.erase(it);
134 }
135 }
136 }
137