Update change log and spec for wrt-plugins-tizen_0.2.73
[profile/ivi/wrt-plugins-tizen.git] / src / platform / 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  *
20  *
21  * @file       CallbackMgr.cpp
22  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
23  * @version    0.1
24  * @brief
25  */
26
27 #include "CallbackMgr.h"
28 #include <dpl/log/log.h>
29 #include "MsgServiceHandleMgr.h"
30 #include <dpl/singleton_safe_impl.h>
31
32 IMPLEMENT_SAFE_SINGLETON(TizenApis::Platform::Messaging::CallbackMgr)
33
34 #include "ISendingObserver.h"
35
36 extern "C" {
37 #include <msg_transport.h>
38 #include <msg.h>
39 }
40
41 namespace TizenApis {
42 namespace Platform {
43 namespace Messaging {
44
45 CallbackMgr::CallbackMgr()
46 {
47     LogInfo("enter");
48     // register callback once per process
49     if (NULL == MsgGetCommonHandle()) {
50         LogError("Unable to open handle");
51     } else {
52         if (msg_reg_sent_status_callback(MsgGetCommonHandle(), sendCallback,
53                                          static_cast<void*>(this)) !=
54             MSG_SUCCESS) {
55             LogError("callback registration error");
56         } else {
57             LogDebug("callback registred succesfully");
58         }
59     }
60 }
61
62 CallbackMgr::~CallbackMgr()
63 {
64     LogInfo("enter");
65 }
66
67 msg_error_t CallbackMgr::registerAndSend(SendingFunction sendingFn,
68 //        const msg_message_t& message,
69         const msg_struct_t& message,
70         ISendingObserver* observer)
71 {
72 //    LogDebug("trying to send message, msgId=" << msg_get_message_id(message));
73
74   
75 //    MSG_SENDINGOPT_S pSendOpt = { false, false, false };
76 //    MSG_REQUEST_S req = {};
77 //    req.reqId = 0;
78 //    req.msg = message;
79 //    req.sendOpt = pSendOpt;
80
81         int tempInt = 0;
82         msg_struct_t req = NULL;
83         msg_struct_t sendOpt = NULL;
84
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);
88         
89         msg_set_struct_handle(req, MSG_REQUEST_MESSAGE_HND, message);
90         msg_set_struct_handle(req, MSG_REQUEST_SENDOPT_HND, sendOpt);
91
92         DPL::Mutex::ScopedLock lock(&m_mutex);
93 //    msg_error_t err = (*sendingFn)(MsgGetCommonHandle(), &req);
94         msg_error_t err = (*sendingFn)(MsgGetCommonHandle(), req);
95
96 //    LogDebug("req.reqId is : " << req.reqId);
97         
98 //    if (err == MSG_SUCCESS) {
99 //        if (observer) {
100 //            m_sendRequests[req.reqId] = observer;
101 //            observer->setSendigRequestId(req.reqId);
102 //            observer->setRecipient(msg_get_ith_address(message, 0));
103 //        }
104 //    }
105
106         if (err == MSG_SUCCESS)
107         {
108                 if (observer)
109                 {
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);
114
115                         msg_get_int_value(req, MSG_REQUEST_REQUESTID_INT, &tempInt);
116                         m_sendRequests[tempInt] = observer;
117                         observer->setSendigRequestId(tempInt);
118                         observer->setRecipient(strNumber);
119                 }
120         }
121     return err;
122 }
123
124 void CallbackMgr::unregister(int reqId)
125 {
126          LogDebug(" reqId is : " << reqId);
127     DPL::Mutex::ScopedLock lock(&m_mutex);
128
129     SendReqMap::iterator it = m_sendRequests.find(reqId);
130     if (it == m_sendRequests.end()) {
131         LogWarning("No matching request found");
132         return;
133     }
134     LogInfo("Matching send request found!");
135     m_sendRequests.erase(it);
136 }
137
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,
142         void *user_param)
143 {
144 //    LogInfo(
145 //        "callback received. Req id = " << sent_status->reqId <<
146 //        " Status = " << (int)sent_status->status);
147     CallbackMgr* instance = static_cast<CallbackMgr*>(user_param);
148     if (!instance) {
149         LogError("Empty user data!");
150         return;
151     }
152
153     instance->call(sent_status);
154 }
155
156 //void CallbackMgr::call(MSG_SENT_STATUS_S *sent_status)
157 void CallbackMgr::call(msg_struct_t sent_status)
158 {
159         int reqId = 0;
160         int status = MSG_NETWORK_NOT_SEND;
161
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);
164
165         DPL::Mutex::ScopedLock lock(&m_mutex);
166
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)
171         {
172                 return;
173         }
174
175         SendReqMap::iterator it = m_sendRequests.find(reqId);
176         if (it == m_sendRequests.end()) {
177                 LogWarning("No matching request found");
178                 return;
179         }
180         LogInfo("Matching send request found!");
181                 // call it
182         it->second->sendingCallback(sent_status);
183         m_sendRequests.erase(it);
184
185 }
186 }
187 }
188 }