ebddb2e7f6fc8d79b7c593e106ce83e94e9e710c
[platform/framework/native/telephony.git] / src / FTel_TelephonyIpcProxy.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file    FTel_TelephonyIpcProxy.cpp
20  * @brief   This is the implementation file for the %_TelephonyIpcProxy class.
21  *
22  * This file contains the implementation of the %_TelephonyIpcProxy class.
23  */
24
25 #include <pthread.h>
26 #include <unique_ptr.h>
27 #include <FBaseSysLog.h>
28 #include <FIo_IpcClient.h>
29 #include <FTel_ConnectivityIpcMessages.h>
30 #include "FTel_TelephonyIpcProxy.h"
31 #include "FTel_CallManagerImpl.h"
32 #include "FTel_NetworkManagerImpl.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Io;
36
37 namespace Tizen { namespace Telephony
38 {
39
40 _TelephonyIpcProxy* _TelephonyIpcProxy::__pInstance = null;
41
42 _TelephonyIpcProxy::_TelephonyIpcProxy(void)
43         : __pIpcClient(null)
44     , __pNetworkManagerImplForGet(null)
45         , __pNetworkManagerImplForSelect(null)
46         , __pNetworkManagerImplForSearch(null)
47     , __pCallManagerImplForGet(null)
48         , __pCallManagerImplForStart(null)
49         , __pCallManagerImplForStop(null)
50 {
51 }
52
53 _TelephonyIpcProxy::~_TelephonyIpcProxy(void)
54 {
55         delete __pIpcClient;
56 }
57
58 result
59 _TelephonyIpcProxy::Construct(void)
60 {
61         result r = E_SUCCESS;
62
63         __pIpcClient = new (std::nothrow) _IpcClient();
64         SysTryReturnResult(NID_TEL, __pIpcClient != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
65
66         r = __pIpcClient->Construct(TELEPHONY_CONNECTIVITY_IPC_SERVER_NAME, this);
67         SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
68
69         if (r != E_SUCCESS)
70         {
71                 SysLogException(NID_TEL, r, "[%s] Propagating", GetErrorMessage(r));
72                 delete __pIpcClient;
73                 __pIpcClient = null;
74         }
75
76         return r;
77 }
78
79 _TelephonyIpcProxy*
80 _TelephonyIpcProxy::GetInstance(void)
81 {
82         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
83
84         if (__pInstance == null)
85         {
86                 ClearLastResult();
87                 pthread_once(&onceBlock, InitSingleton);
88                 result r = GetLastResult();
89                 if(r != E_SUCCESS)
90                 {
91                         onceBlock = PTHREAD_ONCE_INIT;
92                 }
93         }
94
95         return __pInstance;
96 }
97
98 void
99 _TelephonyIpcProxy::InitSingleton(void)
100 {
101         result r = E_SUCCESS;
102         static _TelephonyIpcProxy instance;
103
104         r = instance.Construct();
105         SysTryReturnVoidResult(NID_TEL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
106
107         __pInstance = &instance;
108 }
109
110 result
111 _TelephonyIpcProxy::GetImsi(Tizen::Base::String& imsi) const
112 {
113         result r = E_SUCCESS;
114         unsigned long ret = 0;
115
116         std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_getImsi(&imsi, &ret));
117         SysTryReturnResult(NID_TEL, pMessage!= null , E_OUT_OF_MEMORY, "Memory allocation failed.");
118
119         r = __pIpcClient->SendRequest(*pMessage);
120
121         SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
122         SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
123
124         SysSecureLog(NID_TEL, "The Telephony Imsi obtained through IPC is %ls.", imsi.GetPointer());
125
126         return E_SUCCESS;
127 }
128
129 result
130 _TelephonyIpcProxy::HasSystemPrivilege(void)
131 {
132     result r = E_SUCCESS;
133     unsigned long ret = 0;
134
135     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_hasSystemPrivilege(&ret));
136     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
137
138     r = __pIpcClient->SendRequest(*pMessage);
139     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
140     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
141
142     return E_SUCCESS;
143 }
144
145 result
146 _TelephonyIpcProxy::GetNetworkSelectionMode(_NetworkManagerImpl* pNetworkManagerImpl)
147 {
148     result r = E_SUCCESS;
149     unsigned long ret = 0;
150
151     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_getNetworkSelectionMode(&ret));
152     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
153
154     r = __pIpcClient->SendRequest(*pMessage);
155     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
156     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
157
158     __pNetworkManagerImplForGet = pNetworkManagerImpl;
159
160     return E_SUCCESS;
161 }
162
163 result
164 _TelephonyIpcProxy::SelectNetwork(_NetworkManagerImpl* pNetworkManagerImpl, const Tizen::Base::String& plmn, const int netwokrType)
165 {
166     result r = E_SUCCESS;
167     unsigned long ret = 0;
168
169     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_selectNetworkManual(plmn, netwokrType, &ret));
170     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
171
172     r = __pIpcClient->SendRequest(*pMessage);
173     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
174     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
175
176     __pNetworkManagerImplForSelect = pNetworkManagerImpl;
177
178     return E_SUCCESS;
179 }
180
181 result
182 _TelephonyIpcProxy::SelectNetwork(_NetworkManagerImpl* pNetworkManagerImpl)
183 {
184     result r = E_SUCCESS;
185     unsigned long ret = 0;
186
187     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_selectNetworkAutomatic(&ret));
188     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
189
190     r = __pIpcClient->SendRequest(*pMessage);
191     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
192     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
193
194     __pNetworkManagerImplForSelect = pNetworkManagerImpl;
195
196     return E_SUCCESS;
197
198 }
199
200 result
201 _TelephonyIpcProxy::SearchNetwork(_NetworkManagerImpl* pNetworkManagerImpl)
202 {
203     result r = E_SUCCESS;
204     unsigned long ret = 0;
205
206     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_searchNetwork(&ret));
207     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
208
209     r = __pIpcClient->SendRequest(*pMessage);
210     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
211     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
212
213     __pNetworkManagerImplForSearch = pNetworkManagerImpl;
214
215     return E_SUCCESS;
216 }
217
218
219 result
220 _TelephonyIpcProxy::HasCallForwardPrivilege(void)
221 {
222     result r = E_SUCCESS;
223     unsigned long ret = 0;
224
225     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_hasCallForwardPrivilege(&ret));
226     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
227
228     r = __pIpcClient->SendRequest(*pMessage);
229     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
230     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
231
232     return E_SUCCESS;
233 }
234
235 result
236 _TelephonyIpcProxy::RequestCallForward(_CallManagerImpl* pCallManagerImpl, const String& phoneNumber)
237 {
238     result r = E_SUCCESS;
239     unsigned long ret = 0;
240
241     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_requestCallForward(phoneNumber, &ret));
242     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
243
244     r = __pIpcClient->SendRequest(*pMessage);
245     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
246     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
247
248     __pCallManagerImplForStart = pCallManagerImpl;
249
250     return E_SUCCESS;
251 }
252
253 result
254 _TelephonyIpcProxy::StopCallForward(_CallManagerImpl* pCallManagerImpl)
255 {
256     result r = E_SUCCESS;
257     unsigned long ret = 0;
258
259     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_stopCallForward(&ret));
260     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
261
262     r = __pIpcClient->SendRequest(*pMessage);
263     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
264     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
265
266     __pCallManagerImplForStop = pCallManagerImpl;
267
268     return E_SUCCESS;
269 }
270
271 result
272 _TelephonyIpcProxy::GetCallForwardNumber(_CallManagerImpl* pCallManagerImpl)
273 {
274     result r = E_SUCCESS;
275     unsigned long ret = 0;
276
277     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_getCallForwardNumber(&ret));
278     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
279
280     r = __pIpcClient->SendRequest(*pMessage);
281     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
282     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
283
284     __pCallManagerImplForGet = pCallManagerImpl;
285
286     return E_SUCCESS;
287 }
288
289 void
290 _TelephonyIpcProxy::OnIpcResponseReceived(Tizen::Io::_IpcClient& client, const IPC::Message& message)
291 {
292     SysLog(NID_TEL, "OnIpcResponseReceived");
293
294     IPC_BEGIN_MESSAGE_MAP(_TelephonyIpcProxy, message)
295     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkSelectionModeReceived, OnNetworkSelectionModeReceived, &client)
296     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkSelectionCompleted, OnNetworkSelectionCompleted, &client)
297     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkSearchCompleted, OnNetworkSearchCompleted, &client)
298     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onCallForwardNumberReceived, OnTelephonyCallForwardNumberReceived, &client);
299     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onCallForwardResponseReceived, OnTelephonyCallForwardResponseReceived, &client);
300     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onCallForwardStopped, OnTelephonyCallForwardStopped, &client);
301     IPC_END_MESSAGE_MAP()
302 }
303
304 void
305 _TelephonyIpcProxy::OnNetworkSelectionModeReceived(bool isManual, unsigned long res)
306 {
307     SysLog(NID_TEL, "The listener has been called.");
308
309     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
310
311     __pNetworkManagerImplForGet->OnTelephonyNetworkSelectionModeReceived(isManual, res);
312 }
313
314 void
315 _TelephonyIpcProxy::OnNetworkSelectionCompleted(unsigned long res)
316 {
317     SysLog(NID_TEL, "The listener has been called.");
318
319     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
320
321     __pNetworkManagerImplForSelect->OnTelephonyNetworkSelectionCompleted(res);
322 }
323
324 void
325 _TelephonyIpcProxy::OnNetworkSearchCompleted(Tizen::Base::String message, unsigned long res)
326 {
327     SysLog(NID_TEL, "The listener has been called.");
328
329     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
330
331     __pNetworkManagerImplForSearch->OnTelephonyNetworkSearchCompleted(message, res);
332 }
333
334 void
335 _TelephonyIpcProxy::OnTelephonyCallForwardNumberReceived(Tizen::Base::String phoneNumber, unsigned long res)
336 {
337     SysLog(NID_TEL, "The listener has been called.");
338
339     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
340
341     __pCallManagerImplForGet->OnTelephonyCallForwardNumberReceived(phoneNumber, res);
342 }
343
344 void
345 _TelephonyIpcProxy::OnTelephonyCallForwardResponseReceived(Tizen::Base::String phoneNumber, unsigned long res)
346 {
347     SysLog(NID_TEL, "The listener has been called.");
348
349     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
350
351     __pCallManagerImplForStart->OnTelephonyCallForwardResponseReceived(phoneNumber, res);
352 }
353
354 void
355 _TelephonyIpcProxy::OnTelephonyCallForwardStopped(Tizen::Base::String phoneNumber, unsigned long res)
356 {
357     SysLog(NID_TEL, "The listener has been called.");
358
359     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
360
361     __pCallManagerImplForStop->OnTelephonyCallForwardStopped(phoneNumber, res);
362 }
363
364 }} // Tizen::Telephony