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.
21 * @file CallbackMgr.cpp
22 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
27 #include "CallbackMgr.h"
28 #include <dpl/log/log.h>
29 #include "MsgServiceHandleMgr.h"
30 #include <dpl/singleton_safe_impl.h>
32 IMPLEMENT_SAFE_SINGLETON(TizenApis::Platform::Messaging::CallbackMgr)
34 #include "ISendingObserver.h"
37 #include <msg_transport.h>
45 CallbackMgr::CallbackMgr()
48 // register callback once per process
49 if (NULL == MsgGetCommonHandle()) {
50 LogError("Unable to open handle");
52 if (msg_reg_sent_status_callback(MsgGetCommonHandle(), sendCallback,
53 static_cast<void*>(this)) !=
55 LogError("callback registration error");
57 LogDebug("callback registred succesfully");
62 CallbackMgr::~CallbackMgr()
67 msg_error_t CallbackMgr::registerAndSend(SendingFunction sendingFn,
68 // const msg_message_t& message,
69 const msg_struct_t& message,
70 ISendingObserver* observer)
72 // LogDebug("trying to send message, msgId=" << msg_get_message_id(message));
75 // MSG_SENDINGOPT_S pSendOpt = { false, false, false };
76 // MSG_REQUEST_S req = {};
79 // req.sendOpt = pSendOpt;
82 msg_struct_t req = NULL;
83 msg_struct_t sendOpt = NULL;
85 req = msg_create_struct(MSG_STRUCT_REQUEST_INFO);
86 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
87 msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
89 msg_set_struct_handle(req, MSG_REQUEST_MESSAGE_HND, message);
90 msg_set_struct_handle(req, MSG_REQUEST_SENDOPT_HND, sendOpt);
92 DPL::Mutex::ScopedLock lock(&m_mutex);
93 // msg_error_t err = (*sendingFn)(MsgGetCommonHandle(), &req);
94 msg_error_t err = (*sendingFn)(MsgGetCommonHandle(), req);
96 // LogDebug("req.reqId is : " << req.reqId);
98 // if (err == MSG_SUCCESS) {
100 // m_sendRequests[req.reqId] = observer;
101 // observer->setSendigRequestId(req.reqId);
102 // observer->setRecipient(msg_get_ith_address(message, 0));
106 if (err == MSG_SUCCESS)
110 msg_struct_list_s *addr_list = NULL;
111 err = msg_get_list_handle(message, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
112 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
113 msg_get_str_value(addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
115 msg_get_int_value(req, MSG_REQUEST_REQUESTID_INT, &tempInt);
116 m_sendRequests[tempInt] = observer;
117 observer->setSendigRequestId(tempInt);
118 observer->setRecipient(strNumber);
124 void CallbackMgr::unregister(int reqId)
126 LogDebug(" reqId is : " << reqId);
127 DPL::Mutex::ScopedLock lock(&m_mutex);
129 SendReqMap::iterator it = m_sendRequests.find(reqId);
130 if (it == m_sendRequests.end()) {
131 LogWarning("No matching request found");
134 LogInfo("Matching send request found!");
135 m_sendRequests.erase(it);
138 //void CallbackMgr::sendCallback(MSG_HANDLE_T handle,
139 // MSG_SENT_STATUS_S *sent_status,
140 void CallbackMgr::sendCallback(msg_handle_t handle,
141 msg_struct_t sent_status,
145 // "callback received. Req id = " << sent_status->reqId <<
146 // " Status = " << (int)sent_status->status);
147 CallbackMgr* instance = static_cast<CallbackMgr*>(user_param);
149 LogError("Empty user data!");
153 instance->call(sent_status);
156 //void CallbackMgr::call(MSG_SENT_STATUS_S *sent_status)
157 void CallbackMgr::call(msg_struct_t sent_status)
160 int status = MSG_NETWORK_NOT_SEND;
162 msg_get_int_value(sent_status, MSG_SENT_STATUS_REQUESTID_INT, &reqId);
163 msg_get_int_value(sent_status, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status);
165 DPL::Mutex::ScopedLock lock(&m_mutex);
167 // SendReqMap::iterator it = m_sendRequests.find(sent_status->reqId);
168 if(MSG_NETWORK_SEND_SUCCESS != status
169 && MSG_NETWORK_SEND_FAIL != status
170 && MSG_NETWORK_SEND_TIMEOUT != status)
175 SendReqMap::iterator it = m_sendRequests.find(reqId);
176 if (it == m_sendRequests.end()) {
177 LogWarning("No matching request found");
180 LogInfo("Matching send request found!");
182 it->second->sendingCallback(sent_status);
183 m_sendRequests.erase(it);