Merge from 2.2
[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 <FBaseRtMutexGuard.h>
29 #include <FBaseColArrayList.h>
30 #include <FTelNetworkStatus.h>
31 #include <FIo_IpcClient.h>
32 #include <FTel_ConnectivityIpcMessages.h>
33 #include "FTel_TelephonyIpcProxy.h"
34 #include "FTel_CallManagerImpl.h"
35 #include "FTel_NetworkManagerEvent.h"
36 #include "FTel_NetworkManagerEventArg.h"
37 #include "FTel_NetworkManagerImpl.h"
38 #include "FTel_NetworkStatusImpl.h"
39
40 using namespace std;
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Runtime;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::Io;
45
46 namespace Tizen { namespace Telephony
47 {
48
49 _TelephonyIpcProxy* _TelephonyIpcProxy::__pInstance = null;
50
51 _TelephonyIpcProxy::_TelephonyIpcProxy(void)
52     : __pIpcClient(null)
53         , __pLock(null)
54         , __pNetworkEventList(null)
55         , __networkEventCount(0)
56     , __pNetworkManagerImplForGet(null)
57         , __pNetworkManagerImplForSelect(null)
58         , __pNetworkManagerImplForSearch(null)
59     , __pCallManagerImplForGet(null)
60         , __pCallManagerImplForStart(null)
61         , __pCallManagerImplForStop(null)
62 {
63 }
64
65 _TelephonyIpcProxy::~_TelephonyIpcProxy(void)
66 {
67         if ((__networkEventCount > 0) &&(__pIpcClient != null))
68     {
69             result r = E_SUCCESS;
70             unsigned long ret = 0;
71
72             std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_removeNetworkStatusEventListener(&ret));
73             SysTryReturnVoidResult(NID_TEL, pMessage != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
74
75             r = __pIpcClient->SendRequest(*pMessage);
76             SysLog(NID_TEL, "RemoveNetworkStatusEventListener[%s][%s]", GetErrorMessage(r), GetErrorMessage(ret));
77     }
78 }
79
80 result
81 _TelephonyIpcProxy::Construct(void)
82 {
83         result r = E_SUCCESS;
84
85     unique_ptr<ArrayList> pNetworkEventList(new (std::nothrow) ArrayList());
86     SysTryReturnResult(NID_TEL, pNetworkEventList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
87
88     r = pNetworkEventList->Construct();
89     SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
90
91     unique_ptr<Mutex> pLock(new (std::nothrow) Mutex());
92     SysTryReturnResult(NID_TEL, pLock != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
93
94     r = pLock->Create();
95     SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
96
97     unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient());
98         SysTryReturnResult(NID_TEL, pIpcClient != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
99
100         r = pIpcClient->Construct(TELEPHONY_CONNECTIVITY_IPC_SERVER_NAME, this);
101     SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
102
103     __pIpcClient = move(pIpcClient);
104     __pLock = move(pLock);
105     __pNetworkEventList = move(pNetworkEventList);
106     __networkEventCount = 0;
107
108         return r;
109 }
110
111 _TelephonyIpcProxy*
112 _TelephonyIpcProxy::GetInstance(void)
113 {
114         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
115
116         if (__pInstance == null)
117         {
118                 ClearLastResult();
119                 pthread_once(&onceBlock, InitSingleton);
120                 result r = GetLastResult();
121                 if(r != E_SUCCESS)
122                 {
123                         onceBlock = PTHREAD_ONCE_INIT;
124                 }
125         }
126
127         return __pInstance;
128 }
129
130 void
131 _TelephonyIpcProxy::InitSingleton(void)
132 {
133     result r = E_SUCCESS;
134
135     unique_ptr<_TelephonyIpcProxy> pInstance(new (std::nothrow) _TelephonyIpcProxy());
136         SysTryReturnVoidResult(NID_TEL, pInstance != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
137
138         r = pInstance->Construct();
139         SysTryReturnVoidResult(NID_TEL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
140
141         ClearLastResult();
142
143         __pInstance = pInstance.release();
144 }
145
146 result
147 _TelephonyIpcProxy::GetImsi(Tizen::Base::String& imsi) const
148 {
149         result r = E_SUCCESS;
150         unsigned long ret = 0;
151
152         std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_getImsi(&imsi, &ret));
153         SysTryReturnResult(NID_TEL, pMessage!= null , E_OUT_OF_MEMORY, "Memory allocation failed.");
154
155         r = __pIpcClient->SendRequest(*pMessage);
156
157         SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
158         SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
159
160         SysSecureLog(NID_TEL, "The Telephony Imsi obtained through IPC is %ls.", imsi.GetPointer());
161
162         return E_SUCCESS;
163 }
164
165 result
166 _TelephonyIpcProxy::HasSystemPrivilege(void)
167 {
168     result r = E_SUCCESS;
169     unsigned long ret = 0;
170
171     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_hasSystemPrivilege(&ret));
172     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
173
174     r = __pIpcClient->SendRequest(*pMessage);
175     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
176     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
177
178     return E_SUCCESS;
179 }
180
181 result
182 _TelephonyIpcProxy::GetNetworkSelectionMode(_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_getNetworkSelectionMode(&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     __pNetworkManagerImplForGet = pNetworkManagerImpl;
195
196     return E_SUCCESS;
197 }
198
199 result
200 _TelephonyIpcProxy::SelectNetwork(_NetworkManagerImpl* pNetworkManagerImpl, const Tizen::Base::String& plmn, const int netwokrType)
201 {
202     result r = E_SUCCESS;
203     unsigned long ret = 0;
204
205     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_selectNetworkManual(plmn, netwokrType, &ret));
206     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
207
208     r = __pIpcClient->SendRequest(*pMessage);
209     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
210     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
211
212     __pNetworkManagerImplForSelect = pNetworkManagerImpl;
213
214     return E_SUCCESS;
215 }
216
217 result
218 _TelephonyIpcProxy::SelectNetwork(_NetworkManagerImpl* pNetworkManagerImpl)
219 {
220     result r = E_SUCCESS;
221     unsigned long ret = 0;
222
223     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_selectNetworkAutomatic(&ret));
224     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
225
226     r = __pIpcClient->SendRequest(*pMessage);
227     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
228     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
229
230     __pNetworkManagerImplForSelect = pNetworkManagerImpl;
231
232     return E_SUCCESS;
233
234 }
235
236 result
237 _TelephonyIpcProxy::SearchNetwork(_NetworkManagerImpl* pNetworkManagerImpl)
238 {
239     result r = E_SUCCESS;
240     unsigned long ret = 0;
241
242     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_searchNetwork(&ret));
243     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
244
245     r = __pIpcClient->SendRequest(*pMessage);
246     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
247     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
248
249     __pNetworkManagerImplForSearch = pNetworkManagerImpl;
250
251     return E_SUCCESS;
252 }
253
254
255 result
256 _TelephonyIpcProxy::HasCallForwardPrivilege(void)
257 {
258     result r = E_SUCCESS;
259     unsigned long ret = 0;
260
261     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_hasCallForwardPrivilege(&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 result
272 _TelephonyIpcProxy::RequestCallForward(_CallManagerImpl* pCallManagerImpl, const String& phoneNumber)
273 {
274     result r = E_SUCCESS;
275     unsigned long ret = 0;
276
277     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_requestCallForward(phoneNumber, &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     __pCallManagerImplForStart = pCallManagerImpl;
285
286     return E_SUCCESS;
287 }
288
289 result
290 _TelephonyIpcProxy::StopCallForward(_CallManagerImpl* pCallManagerImpl)
291 {
292     result r = E_SUCCESS;
293     unsigned long ret = 0;
294
295     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_stopCallForward(&ret));
296     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
297
298     r = __pIpcClient->SendRequest(*pMessage);
299     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
300     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
301
302     __pCallManagerImplForStop = pCallManagerImpl;
303
304     return E_SUCCESS;
305 }
306
307 result
308 _TelephonyIpcProxy::GetCallForwardNumber(_CallManagerImpl* pCallManagerImpl)
309 {
310     result r = E_SUCCESS;
311     unsigned long ret = 0;
312
313     std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_getCallForwardNumber(&ret));
314     SysTryReturnResult(NID_TEL, pMessage != null , E_OUT_OF_MEMORY, "Memory allocation failed.");
315
316     r = __pIpcClient->SendRequest(*pMessage);
317     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
318     SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
319
320     __pCallManagerImplForGet = pCallManagerImpl;
321
322     return E_SUCCESS;
323 }
324
325 result
326 _TelephonyIpcProxy::AddNetworkEvent(_NetworkManagerEvent* pEvent)
327 {
328         result r = E_SUCCESS;
329
330     SysTryReturnResult(NID_TEL, pEvent != null, E_INVALID_ARG, "Event is null.");
331
332     MutexGuard locked(*__pLock);
333
334         r = __pNetworkEventList->Add(*pEvent);
335     SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
336
337         if (__networkEventCount == 0)
338         {
339                 SysLog(NID_TEL, "Set network event callback.");
340
341             unsigned long ret = 0;
342
343             std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_addNetworkStatusEventListener(&ret));
344             r = __pIpcClient->SendRequest(*pMessage);
345             SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to send a request.");
346             SysTryReturnResult(NID_TEL, ret == E_SUCCESS, ret, "Propagating.");
347         }
348
349     __networkEventCount++;
350
351         return r;
352 }
353
354 void
355 _TelephonyIpcProxy::RemoveNetworkEvent(_NetworkManagerEvent* pEvent)
356 {
357         SysTryReturnVoidResult(NID_TEL, pEvent != null, E_INVALID_ARG, "[E_INVALID_ARG] Event is null.");
358
359         MutexGuard locked(*__pLock);
360
361         __pNetworkEventList->Remove(*pEvent);
362
363     __networkEventCount--;
364
365         if (__networkEventCount == 0)
366         {
367                 SysLog(NID_TEL, "Unset network event callback.");
368
369                 result r = E_SUCCESS;
370             unsigned long ret = 0;
371
372             std::unique_ptr<IPC::Message> pMessage( new (std::nothrow) ConnectivityTelephonyServiceMsg_removeNetworkStatusEventListener(&ret));
373             SysTryReturnVoidResult(NID_TEL, pMessage != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
374
375             r = __pIpcClient->SendRequest(*pMessage);
376             SysLog(NID_TEL, "RemoveNetworkStatusEventListener[%s][%s]", GetErrorMessage(r), GetErrorMessage(ret));
377         }
378 }
379
380 void
381 _TelephonyIpcProxy::OnIpcResponseReceived(Tizen::Io::_IpcClient& client, const IPC::Message& message)
382 {
383     SysLog(NID_TEL, "OnIpcResponseReceived");
384
385     IPC_BEGIN_MESSAGE_MAP(_TelephonyIpcProxy, message)
386     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkSelectionModeReceived, OnNetworkSelectionModeReceived, &client)
387     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkSelectionCompleted, OnNetworkSelectionCompleted, &client)
388     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkSearchCompleted, OnNetworkSearchCompleted, &client)
389     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onCallForwardNumberReceived, OnTelephonyCallForwardNumberReceived, &client);
390     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onCallForwardResponseReceived, OnTelephonyCallForwardResponseReceived, &client);
391     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onCallForwardStopped, OnTelephonyCallForwardStopped, &client);
392     IPC_MESSAGE_HANDLER(ConnectivityTelephonyServiceMsg_onNetworkStatusChanged, OnNetworkStatusChanged, &client);
393     IPC_END_MESSAGE_MAP()
394 }
395
396 void
397 _TelephonyIpcProxy::OnNetworkSelectionModeReceived(bool isManual, unsigned long res)
398 {
399     SysLog(NID_TEL, "The listener has been called.");
400
401     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
402
403     __pNetworkManagerImplForGet->OnTelephonyNetworkSelectionModeReceived(isManual, res);
404 }
405
406 void
407 _TelephonyIpcProxy::OnNetworkSelectionCompleted(unsigned long res)
408 {
409     SysLog(NID_TEL, "The listener has been called.");
410
411     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
412
413     __pNetworkManagerImplForSelect->OnTelephonyNetworkSelectionCompleted(res);
414 }
415
416 void
417 _TelephonyIpcProxy::OnNetworkSearchCompleted(Tizen::Base::String message, unsigned long res)
418 {
419     SysLog(NID_TEL, "The listener has been called.");
420
421     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
422
423     __pNetworkManagerImplForSearch->OnTelephonyNetworkSearchCompleted(message, res);
424 }
425
426 void
427 _TelephonyIpcProxy::OnTelephonyCallForwardNumberReceived(Tizen::Base::String phoneNumber, unsigned long res)
428 {
429     SysLog(NID_TEL, "The listener has been called.");
430
431     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
432
433     __pCallManagerImplForGet->OnTelephonyCallForwardNumberReceived(phoneNumber, res);
434 }
435
436 void
437 _TelephonyIpcProxy::OnTelephonyCallForwardResponseReceived(Tizen::Base::String phoneNumber, unsigned long res)
438 {
439     SysLog(NID_TEL, "The listener has been called.");
440
441     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
442
443     __pCallManagerImplForStart->OnTelephonyCallForwardResponseReceived(phoneNumber, res);
444 }
445
446 void
447 _TelephonyIpcProxy::OnTelephonyCallForwardStopped(Tizen::Base::String phoneNumber, unsigned long res)
448 {
449     SysLog(NID_TEL, "The listener has been called.");
450
451     SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
452
453     __pCallManagerImplForStop->OnTelephonyCallForwardStopped(phoneNumber, res);
454 }
455
456 void
457 _TelephonyIpcProxy::OnNetworkStatusChanged(bool isCallServiceAvailable, bool isDataServiceAvailable, bool isRoaming)
458 {
459     SysLog(NID_TEL, "Network status is changed. [%d][%d][%d]", isCallServiceAvailable, isDataServiceAvailable, isRoaming);
460
461     NetworkStatus networkStatus;
462         _NetworkManagerEvent* pEvent = null;
463         _NetworkManagerEventArg* pEventArg = null;
464
465         _NetworkStatusImpl::GetInstance(networkStatus)->SetIsCallServiceAvailable(isCallServiceAvailable);
466         _NetworkStatusImpl::GetInstance(networkStatus)->SetIsDataServiceAvailable(isDataServiceAvailable);
467         _NetworkStatusImpl::GetInstance(networkStatus)->SetIsRoaming(isRoaming);
468
469         unique_ptr<IEnumerator> pEnum(__pNetworkEventList->GetEnumeratorN());
470         if (pEnum != null)
471         {
472                 while (pEnum->MoveNext() == E_SUCCESS)
473                 {
474                         pEvent = dynamic_cast<_NetworkManagerEvent*>(pEnum->GetCurrent());
475                         if (pEvent != null)
476                         {
477                                 pEventArg = new (std::nothrow)_NetworkManagerEventArg(networkStatus);
478                                 if (pEventArg != null)
479                                 {
480                                         pEvent->FireAsync(*pEventArg);
481                                 }
482                         }
483                 }
484         }
485 }
486
487 }} // Tizen::Telephony