9af93b5315719b4b55cab6438f51609f995eaef8
[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     , __pNetworkManagerImpl(null)
45     , __pCallManagerImpl(null)
46 {
47 }
48
49 _TelephonyIpcProxy::~_TelephonyIpcProxy(void)
50 {
51         delete __pIpcClient;
52 }
53
54 result
55 _TelephonyIpcProxy::Construct(void)
56 {
57         result r = E_SUCCESS;
58
59         __pIpcClient = new (std::nothrow) _IpcClient();
60         SysTryReturnResult(NID_TEL, __pIpcClient != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
61
62         r = __pIpcClient->Construct(TELEPHONY_CONNECTIVITY_IPC_SERVER_NAME, this);
63         SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
64
65         if (r != E_SUCCESS)
66         {
67                 SysLogException(NID_TEL, r, "[%s] Propagating", GetErrorMessage(r));
68                 delete __pIpcClient;
69                 __pIpcClient = null;
70         }
71
72         return r;
73 }
74
75 _TelephonyIpcProxy*
76 _TelephonyIpcProxy::GetInstance(void)
77 {
78         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
79
80         if (__pInstance == null)
81         {
82                 ClearLastResult();
83                 pthread_once(&onceBlock, InitSingleton);
84                 result r = GetLastResult();
85                 if(r != E_SUCCESS)
86                 {
87                         onceBlock = PTHREAD_ONCE_INIT;
88                 }
89         }
90
91         return __pInstance;
92 }
93
94 void
95 _TelephonyIpcProxy::InitSingleton(void)
96 {
97         result r = E_SUCCESS;
98         static _TelephonyIpcProxy instance;
99
100         r = instance.Construct();
101         SysTryReturnVoidResult(NID_TEL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
102
103         __pInstance = &instance;
104 }
105
106 result
107 _TelephonyIpcProxy::GetImsi(Tizen::Base::String& imsi) const
108 {
109         result r = E_SUCCESS;
110         unsigned long ret = 0;
111
112         std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_getImsi(&imsi, &ret));
113         SysTryReturnResult(NID_TEL, pMessage!= null , E_OUT_OF_MEMORY, "Memory allocation failed.");
114
115         r = __pIpcClient->SendRequest(*pMessage);
116
117         SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
118         SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
119
120         SysLog(NID_TEL, "The Telephony Imsi obtained through IPC is %ls.", imsi.GetPointer());
121
122         return E_SUCCESS;
123 }
124
125 result
126 _TelephonyIpcProxy::HasSystemPrivilege(void)
127 {
128     result r = E_SUCCESS;
129     unsigned long ret = 0;
130
131     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_hasSystemPrivilege(&ret));
132     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
133
134     r = __pIpcClient->SendRequest(*pMessage);
135     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
136     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
137
138     return E_SUCCESS;
139 }
140
141 result
142 _TelephonyIpcProxy::GetNetworkSelectionMode(void)
143 {
144     result r = E_SUCCESS;
145     unsigned long ret = 0;
146
147     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_getNetworkSelectionMode(&ret));
148     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
149
150     r = __pIpcClient->SendRequest(*pMessage);
151     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
152     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
153
154     return E_SUCCESS;
155 }
156
157 result
158 _TelephonyIpcProxy::SelectNetwork(const Tizen::Base::String& plmn, const int netwokrType)
159 {
160     result r = E_SUCCESS;
161     unsigned long ret = 0;
162
163     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_selectNetworkManual(plmn, netwokrType, &ret));
164     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
165
166     r = __pIpcClient->SendRequest(*pMessage);
167     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
168     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
169
170     return E_SUCCESS;
171 }
172
173 result
174 _TelephonyIpcProxy::SelectNetwork(void)
175 {
176     result r = E_SUCCESS;
177     unsigned long ret = 0;
178
179     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_selectNetworkAutomatic(&ret));
180     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
181
182     r = __pIpcClient->SendRequest(*pMessage);
183     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
184     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
185
186     return E_SUCCESS;
187
188 }
189
190 result
191 _TelephonyIpcProxy::SearchNetwork(void)
192 {
193     result r = E_SUCCESS;
194     unsigned long ret = 0;
195
196     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_searchNetwork(&ret));
197     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
198
199     r = __pIpcClient->SendRequest(*pMessage);
200     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
201     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
202
203     return E_SUCCESS;
204 }
205
206
207 result
208 _TelephonyIpcProxy::HasCallForwardPrivilege(void)
209 {
210     result r = E_SUCCESS;
211     unsigned long ret = 0;
212
213     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_hasCallForwardPrivilege(&ret));
214     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
215
216     r = __pIpcClient->SendRequest(*pMessage);
217     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
218     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
219
220     return E_SUCCESS;
221 }
222
223 result
224 _TelephonyIpcProxy::RequestCallForward(const String& phoneNumber)
225 {
226     result r = E_SUCCESS;
227     unsigned long ret = 0;
228
229     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_requestCallForward(phoneNumber, &ret));
230     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
231
232     r = __pIpcClient->SendRequest(*pMessage);
233     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
234     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
235
236     return E_SUCCESS;
237 }
238
239 result
240 _TelephonyIpcProxy::StopCallForward(void)
241 {
242     result r = E_SUCCESS;
243     unsigned long ret = 0;
244
245     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_stopCallForward(&ret));
246     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
247
248     r = __pIpcClient->SendRequest(*pMessage);
249     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
250     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
251
252     return E_SUCCESS;
253 }
254
255 result
256 _TelephonyIpcProxy::GetCallForwardNumber(void)
257 {
258     result r = E_SUCCESS;
259     unsigned long ret = 0;
260
261     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_getCallForwardNumber(&ret));
262     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
263
264     r = __pIpcClient->SendRequest(*pMessage);
265     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
266     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
267
268     return E_SUCCESS;
269 }
270
271 void
272 _TelephonyIpcProxy::SetNetworkManagerImpl(_NetworkManagerImpl& impl)
273 {
274     __pNetworkManagerImpl = &impl;
275 }
276
277 void
278 _TelephonyIpcProxy::SetCallManagerImpl(_CallManagerImpl& impl)
279 {
280     __pCallManagerImpl = &impl;
281 }
282
283 void
284 _TelephonyIpcProxy::OnIpcResponseReceived(Tizen::Io::_IpcClient& client, const IPC::Message& message)
285 {
286     SysLog(NID_TEL, "OnIpcResponseReceived");
287
288     IPC_BEGIN_MESSAGE_MAP(_TelephonyIpcProxy, message)
289     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkSelectionModeReceived, OnNetworkSelectionModeReceived, &client)
290     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkSelectionCompleted, OnNetworkSelectionCompleted, &client)
291     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkSearchCompleted, OnNetworkSearchCompleted, &client)
292     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onCallForwardNumberReceived, OnTelephonyCallForwardNumberReceived, &client);
293     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onCallForwardResponseReceived, OnTelephonyCallForwardResponseReceived, &client);
294     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onCallForwardStopped, OnTelephonyCallForwardStopped, &client);
295     IPC_END_MESSAGE_MAP()
296 }
297
298 void
299 _TelephonyIpcProxy::OnNetworkSelectionModeReceived(bool isManual, unsigned long res)
300 {
301     SysLog(NID_TEL, "The listener has been called.");
302
303     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
304
305     __pNetworkManagerImpl->OnTelephonyNetworkSelectionModeReceived(isManual, res);
306 }
307
308 void
309 _TelephonyIpcProxy::OnNetworkSelectionCompleted(unsigned long res)
310 {
311     SysLog(NID_TEL, "The listener has been called.");
312
313     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
314
315     __pNetworkManagerImpl->OnTelephonyNetworkSelectionCompleted(res);
316 }
317
318 void
319 _TelephonyIpcProxy::OnNetworkSearchCompleted(Tizen::Base::String message, unsigned long res)
320 {
321     SysLog(NID_TEL, "The listener has been called.");
322
323     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
324
325     __pNetworkManagerImpl->OnTelephonyNetworkSearchCompleted(message, res);
326 }
327
328 void
329 _TelephonyIpcProxy::OnTelephonyCallForwardNumberReceived(Tizen::Base::String phoneNumber, unsigned long res)
330 {
331     SysLog(NID_TEL, "The listener has been called.");
332
333     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
334
335     __pCallManagerImpl->OnTelephonyCallForwardNumberReceived(phoneNumber, res);
336 }
337
338 void
339 _TelephonyIpcProxy::OnTelephonyCallForwardResponseReceived(Tizen::Base::String phoneNumber, unsigned long res)
340 {
341     SysLog(NID_TEL, "The listener has been called.");
342
343     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
344
345     __pCallManagerImpl->OnTelephonyCallForwardResponseReceived(phoneNumber, res);
346 }
347
348 void
349 _TelephonyIpcProxy::OnTelephonyCallForwardStopped(Tizen::Base::String phoneNumber, unsigned long res)
350 {
351     SysLog(NID_TEL, "The listener has been called.");
352
353     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
354
355     __pCallManagerImpl->OnTelephonyCallForwardStopped(phoneNumber, res);
356 }
357
358 }} // Tizen::Telephony