2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file CallbackMgr.cpp
20 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
25 #include "CallbackMgr.h"
26 #include <dpl/log/log.h>
27 #include <dpl/singleton_impl.h>
28 #include "ISendingObserver.h"
29 #include "MsgServiceHandleMgr.h"
32 #include <MapiTransport.h>
33 #include <MapiControl.h>
34 #include <MapiMessage.h>
37 IMPLEMENT_SINGLETON(WrtDeviceApis::Messaging::CallbackMgr)
39 namespace WrtDeviceApis {
42 CallbackMgr::CallbackMgr()
45 // register callback once per process
46 if (NULL == MsgGetCommonHandle()) {
47 LogError("Unable to open handle");
49 if (msg_reg_sent_status_callback(MsgGetCommonHandle(), sendCallback,
50 static_cast<void*>(this)) !=
52 LogError("callback registration error");
54 LogDebug("callback registred succesfully");
59 CallbackMgr::~CallbackMgr()
64 MSG_ERROR_T CallbackMgr::registerAndSend(SendingFunction sendingFn,
65 const msg_message_t& message,
66 ISendingObserver* observer)
68 LogDebug("trying to send message, msgId=" << msg_get_message_id(message));
70 MSG_SENDINGOPT_S pSendOpt;
71 memset(&pSendOpt, 0, sizeof(MSG_SENDINGOPT_S));
73 memset(&req, 0, sizeof(MSG_REQUEST_S));
76 req.sendOpt = pSendOpt;
78 DPL::Mutex::ScopedLock lock(&m_mutex);
79 MSG_ERROR_T err = (*sendingFn)(MsgGetCommonHandle(), &req);
80 if (err == MSG_SUCCESS) {
82 m_sendRequests[req.reqId] = observer;
83 observer->setSendigRequestId(req.reqId);
84 observer->setRecipient(msg_get_ith_address(message, 0));
90 void CallbackMgr::unregister(int reqId)
92 DPL::Mutex::ScopedLock lock(&m_mutex);
94 SendReqMap::iterator it = m_sendRequests.find(reqId);
95 if (it == m_sendRequests.end()) {
96 LogWarning("No matching request found");
99 LogInfo("Matching send request found!");
100 m_sendRequests.erase(it);
103 void CallbackMgr::sendCallback(MSG_HANDLE_T /*handle*/,
104 MSG_SENT_STATUS_S *sent_status,
108 "callback received. Req id = " << sent_status->reqId <<
109 " Status = " << (int)sent_status->status);
110 CallbackMgr* instance = static_cast<CallbackMgr*>(user_param);
112 LogError("Empty user data!");
116 instance->call(sent_status);
119 void CallbackMgr::call(MSG_SENT_STATUS_S *sent_status)
121 DPL::Mutex::ScopedLock lock(&m_mutex);
123 SendReqMap::iterator it = m_sendRequests.find(sent_status->reqId);
124 if (it == m_sendRequests.end()) {
125 LogWarning("No matching request found");
128 LogInfo("Matching send request found!");
131 it->second->sendingCallback(sent_status);
133 m_sendRequests.erase(it);