f906bfc8566884877b2ed153d6d1799512fec751
[platform/framework/native/telephony.git] / src / FTel_NetworkManagerImpl.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  * @file        FTel_NetworkManagerImpl.cpp
19  * @brief       This is the implementation file for _NetworkManagerImpl class.
20  */
21
22 #include <stdlib.h>
23 #include <telephony_network.h>
24 #include <FTelITelephonyNetworkEventListener.h>
25 #include <FTelITelephonyNetworkSettingListener.h>
26 #include <FTelNetworkInfo.h>
27 #include <FTelNetworkStatus.h>
28 #include <FTelNetworkManager.h>
29 #include <FSysSettingInfo.h>
30 #include <FBaseUtilStringUtil.h>
31 #include <FBaseSysLog.h>
32 #include "FApp_AppInfo.h"
33 #include "FTel_NetworkManagerImpl.h"
34 #include "FTel_NetworkStatusImpl.h"
35 #include "FTel_NetworkInfoImpl.h"
36 #include "FTel_NetworkManagerEvent.h"
37 #include "FTel_NetworkManagerEventArg.h"
38 #include "FTel_NetworkSettingEvent.h"
39 #include "FTel_NetworkSettingEventArg.h"
40 #include "FTel_TelephonyUtility.h"
41 #include "FTel_TelephonyIpcProxy.h"
42
43 using namespace std;
44 using namespace Tizen::System;
45 using namespace Tizen::App;
46 using namespace Tizen::Base;
47 using namespace Tizen::Base::Utility;
48 using namespace Tizen::Base::Collection;
49
50
51 namespace Tizen { namespace Telephony
52 {
53
54 _NetworkManagerImpl::_NetworkManagerImpl(void)
55         : __pConnectionHandle(null)
56     , __pNetworkManagerEvent(null)
57     , __pNetworkSettingEvent(null)
58     , __pTelephonyServiceProxy(null)
59     , __pSettingListener(null)
60         , __isCallServiceAvailable(false)
61         , __isDataServiceAvailable(false)
62         , __isRoaming(false)
63 {
64 }
65
66 _NetworkManagerImpl::~_NetworkManagerImpl(void)
67 {
68     if (__pNetworkManagerEvent != null)
69     {
70         network_info_unset_service_state_changed_cb();
71         network_info_unset_roaming_state_changed_cb();
72         connection_unset_type_changed_cb(__pConnectionHandle.get());
73     }
74 }
75
76 result
77 _NetworkManagerImpl::Construct(ITelephonyNetworkEventListener* pListener)
78 {
79     connection_h pConnHandle = null;
80     int err = connection_create(&pConnHandle);
81     SysLog(NID_TEL, "The return value of connection_create() is %d", err);
82     SysTryReturnResult(NID_TEL, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the connection handle.");
83
84     std::unique_ptr<void, _ConnectionDeleter> pConnectionHandle(pConnHandle);
85
86     if (pListener != null)
87     {
88         unique_ptr<_NetworkManagerEvent> pNetworkManagerEvent(new (std::nothrow) _NetworkManagerEvent);
89         SysTryReturnResult(NID_TEL, pNetworkManagerEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
90
91         result r = pNetworkManagerEvent->Construct();
92         SysTryReturnResult(NID_TEL,  r != E_OUT_OF_MEMORY, r, "Propagating.");
93         SysTryReturnResult(NID_TEL,  r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to construct of the _NetworkManagerEvent.");
94
95         // Register data service state changed event
96             err = connection_set_type_changed_cb(pConnectionHandle.get(),(connection_type_changed_cb)_NetworkManagerImpl::OnDataServiceStateChangedCallback, this);
97                 SysLog(NID_TEL, "The return value of connection_set_type_changed_cb() is 0x%x", err);
98         SysTryReturnResult(NID_TEL, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to register the callback function to received the data service state changed event.");
99
100         // Register call service state changed event
101                 err = network_info_set_service_state_changed_cb((network_info_service_state_changed_cb) _NetworkManagerImpl::OnCallServiceStateChangedCallback, this);
102                 SysLog(NID_TEL, "The return value of network_info_set_service_state_changed_cb() is 0x%x", err);
103                 if (err != NETWORK_INFO_ERROR_NONE)
104                 {
105                         SysLogException(NID_TEL, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to register the callback function to received the call service state changed event.");
106                 connection_unset_type_changed_cb(pConnectionHandle.get());
107
108                 return E_SYSTEM;
109                 }
110
111         // Register roaming state changed event
112             err = network_info_set_roaming_state_changed_cb((network_info_roaming_state_changed_cb)_NetworkManagerImpl::OnRoamingStateChangedCallback, this);
113                 SysLog(NID_TEL, "The return value of network_info_set_roaming_state_changed_cb() is 0x%x", err);
114                 if (err != NETWORK_INFO_ERROR_NONE)
115                 {
116                         SysLogException(NID_TEL, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to register the callback function to received the roaming state changed event.");
117                 connection_unset_type_changed_cb(pConnectionHandle.get());
118                 network_info_unset_service_state_changed_cb();
119
120                 return E_SYSTEM;
121                 }
122
123         pNetworkManagerEvent->AddListener(*pListener, true);
124
125         __pNetworkManagerEvent = move(pNetworkManagerEvent);
126
127     }
128
129     __pTelephonyServiceProxy = _TelephonyIpcProxy::GetInstance();
130
131     if (__pTelephonyServiceProxy == null)
132     {
133         SysLog(NID_TEL, "Creating an IPC instance to connect with the Connectivity service daemon has failed.");
134     }
135
136     __pConnectionHandle = move(pConnectionHandle);
137
138     __isCallServiceAvailable = IsServiceAvailable();
139     __isDataServiceAvailable = IsDataAvailable();
140     __isRoaming = IsRoaming();
141
142     return E_SUCCESS;
143 }
144
145 result
146 _NetworkManagerImpl::GetNetworkStatus(NetworkStatus& networkStatus) const
147 {
148     int err = NETWORK_INFO_ERROR_NONE;
149     bool isRoaming = false;
150     bool isDataServiceAvailable = false;
151     bool isCallServiceAvailable = false;
152     connection_cellular_state_e dataServiceState = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
153
154     SysTryReturnResult(NID_TEL, IsNetworkAvailable(), E_NETWORK_UNAVAILABLE, "The operation has failed because the device is in the offline mode.");
155
156     // Get the isRoming
157     err = network_info_is_roaming(&isRoaming);
158     SysLog(NID_TEL, "The return value of network_info_is_roaming() is 0x%x and the roming state is %s", err, isRoaming ? "True" : "False");
159
160         SysTryReturnResult(NID_TEL, err != NETWORK_INFO_ERROR_OUT_OF_SERVICE, E_SERVICE_UNAVAILABLE, "The operation failed because the device is out of the coverage area or in the emergency mode.");
161         SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the roaming status.")
162  
163    // Get the isCallServiceAvailable
164         SysTryReturnResult(NID_TEL, IsServiceAvailable(), E_SERVICE_UNAVAILABLE, "The operation failed because the device is out of the coverage area or in the emergency mode.")
165         isCallServiceAvailable = true;
166
167         // Get the isDataServiceAvailable
168     err = connection_get_cellular_state(__pConnectionHandle.get(), &dataServiceState);
169         
170     SysLog(NID_TEL, "The return value of connection_get_cellular_state() is 0x%x and the dataServiceState is %d", err, dataServiceState);
171     SysTryReturnResult(NID_TEL, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the data service status.");
172
173     if (dataServiceState == CONNECTION_CELLULAR_STATE_AVAILABLE || dataServiceState == CONNECTION_CELLULAR_STATE_CONNECTED)
174     {
175         isDataServiceAvailable = true;
176     }
177
178     _NetworkStatusImpl::GetInstance(networkStatus)->SetIsRoaming(isRoaming);
179     _NetworkStatusImpl::GetInstance(networkStatus)->SetIsCallServiceAvailable(isCallServiceAvailable);
180     _NetworkStatusImpl::GetInstance(networkStatus)->SetIsDataServiceAvailable(isDataServiceAvailable);
181
182         return E_SUCCESS;
183
184 }
185
186 result
187 _NetworkManagerImpl::GetNetworkInfo(NetworkInfo& networkInfo) const
188 {
189         result r = E_SUCCESS;
190
191     SysTryReturnResult(NID_TEL, IsNetworkAvailable(), E_NETWORK_UNAVAILABLE,
192                                       "The operation has failed because the device is in the offline mode.");
193     SysTryReturnResult(NID_TEL, IsServiceAvailable(), E_SERVICE_UNAVAILABLE,
194                                       "The operation failed because the device is out of the coverage area or in the emergency mode.");
195
196     int err = NETWORK_INFO_ERROR_NONE;
197     _ApiVersion apiVersion = _AppInfo::GetApiVersion();
198
199     // Get the cell ID
200     int cellId = -1;
201     err = network_info_get_cell_id(&cellId);
202         SysLog(NID_TEL, "The return value of network_info_get_cell_id() is %d and the cellId value is %d", err, cellId);
203     SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the cell ID.");
204
205     // Get the lac
206     int lac = -1;
207     err = network_info_get_lac(&lac);
208         SysLog(NID_TEL, "The return value of network_info_get_lac() is %d and the lac value is %d", err, lac);
209     SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the lac.");
210
211         char* pTemp = null;
212
213     // Get the operator name
214     err = network_info_get_provider_name(&pTemp);
215     SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the plmn.");
216     unique_ptr<char[],_CharDeleter> pOperatorName(pTemp);
217     String operatorName;
218     r = (StringUtil::Utf8ToString(pOperatorName.get(), operatorName));
219     SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
220         SysLog(NID_TEL, "The operatorname is %ls", operatorName.GetPointer());
221
222     // Get the mcc
223     err = network_info_get_mcc(&pTemp);
224         SysLog(NID_TEL, "The return value of network_info_get_mcc() is %d and the mcc value is %s", err, pTemp);
225     SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the mcc.");
226     unique_ptr<char[],_CharDeleter> pMcc(pTemp);
227
228     // Get the mnc
229     err = network_info_get_mnc(&pTemp);
230         SysLog(NID_TEL, "The return value of network_info_get_mnc() is %d and the mnc value is %s", err, pTemp);
231     SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the mnc.");
232     unique_ptr<char[],_CharDeleter> pMnc(pTemp);
233
234     String plmn;
235     r = plmn.Append(pMcc.get());
236     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to set plmn.");
237
238     r = plmn.Append(pMnc.get());
239         SysLog(NID_TEL, "The plmn value is %ls", plmn.GetPointer());
240     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to set plmn.");
241
242     _NetworkInfoImpl::GetInstance(networkInfo)->SetCellId(cellId);
243     _NetworkInfoImpl::GetInstance(networkInfo)->SetLac(lac);
244     _NetworkInfoImpl::GetInstance(networkInfo)->SetOperatorName(operatorName);
245     _NetworkInfoImpl::GetInstance(networkInfo)->SetMcc(atoi(pMcc.get()));
246     _NetworkInfoImpl::GetInstance(networkInfo)->SetMnc(atoi(pMnc.get()));
247
248     if (apiVersion == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
249     {
250         _NetworkInfoImpl::GetInstance(networkInfo)->SetPlmn(operatorName);
251     }
252     else
253     {
254         _NetworkInfoImpl::GetInstance(networkInfo)->SetPlmn(plmn);
255     }
256
257     return E_SUCCESS;
258 }
259
260 result
261 _NetworkManagerImpl::SetNetworkSettingListener(ITelephonyNetworkSettingListener* pListener)
262 {
263     SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
264
265     result r = __pTelephonyServiceProxy->HasSystemPrivilege();
266     SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
267
268     if (__pSettingListener != null)
269     {
270         __pNetworkSettingEvent->RemoveListener(*__pSettingListener);
271         __pSettingListener = null;
272     }
273
274     // Creates event object
275     if (__pNetworkSettingEvent == null && pListener != null)
276     {
277         std::unique_ptr<_NetworkSettingEvent> pNetworkSettingEvent(new (std::nothrow) _NetworkSettingEvent());
278
279          SysTryReturnResult(NID_TEL, pNetworkSettingEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
280
281          r = pNetworkSettingEvent->Construct();
282          SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
283
284          __pNetworkSettingEvent = move(pNetworkSettingEvent);
285     }
286
287     // Adds listener
288     if (pListener != null)
289     {
290         __pSettingListener = pListener;
291         r = __pNetworkSettingEvent->AddListener(*__pSettingListener, true);
292     }
293
294     if (r != E_SUCCESS)
295     {
296         __pNetworkSettingEvent.reset(null);
297                 SysLogException(NID_TEL, r, "[%s] Propagating.", GetErrorMessage(r));
298     }
299
300     return r;
301 }
302
303 result
304 _NetworkManagerImpl::GetNetworkSelectionMode(void)
305 {
306     SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
307
308     result r = __pTelephonyServiceProxy->GetNetworkSelectionMode(this);
309     SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
310     SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
311     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to get selection mode.");
312
313     return r;
314 }
315
316 result
317 _NetworkManagerImpl::SelectNetwork(const NetworkInfo & networkInfo)
318 {
319     SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "IPC instance has not been constructed yet.");
320
321     const _NetworkInfoImpl* pNetworkInfoImpl = _NetworkInfoImpl::GetInstance(networkInfo);
322     TelNetworkSystemType_t networkSystemType = _TelephonyUtility::ConvertNetworkType(pNetworkInfoImpl->GetNetworkType());
323
324     result r = __pTelephonyServiceProxy->SelectNetwork(this, pNetworkInfoImpl->GetPlmn(), (int)networkSystemType);
325     SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
326     SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
327     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to select network manually.");
328
329     return r;
330 }
331
332 result
333 _NetworkManagerImpl::SelectNetwork(void)
334 {
335     SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
336
337     result r = __pTelephonyServiceProxy->SelectNetwork(this);
338     SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
339     SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
340     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to select network automatically.");
341
342     return r;
343 }
344
345 result
346 _NetworkManagerImpl::SearchNetwork(void)
347 {
348     SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
349
350     result r = __pTelephonyServiceProxy->SearchNetwork(this);
351     SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
352     SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
353     SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to search network automatic.");
354
355     return r;
356 }
357
358 void
359 _NetworkManagerImpl::OnRoamingStateChangedCallback(bool isRoaming, void* pUserData)
360 {
361         SysLog(NID_TEL, "The callback has called and the roaming state is %s", isRoaming ? "True" : "False");
362     _NetworkManagerImpl* pNetworkManagerImpl = static_cast <_NetworkManagerImpl*>(pUserData);
363
364     pNetworkManagerImpl->OnTelephonyNetworkStatusChanged();
365
366     return;
367 }
368
369 void
370 _NetworkManagerImpl::OnCallServiceStateChangedCallback(network_info_service_state_e serviceState, void* pUserData)
371 {
372     SysLog(NID_TEL, "The callback has called and the service state is %d", serviceState);
373
374     _NetworkManagerImpl* pNetworkManagerImpl = static_cast <_NetworkManagerImpl*>(pUserData);
375
376     pNetworkManagerImpl->OnTelephonyNetworkStatusChanged();
377
378     return;
379 }
380
381 void
382 _NetworkManagerImpl::OnDataServiceStateChangedCallback(connection_type_e type, void* pUserData)
383 {
384     SysLog(NID_TEL, "The callback has called and the connection state is %d", type);
385
386     _NetworkManagerImpl* pNetworkManagerImpl = static_cast <_NetworkManagerImpl*>(pUserData);
387
388     pNetworkManagerImpl->OnTelephonyNetworkStatusChanged();
389
390     return;
391 }
392
393 void
394 _NetworkManagerImpl::OnTelephonyNetworkStatusChanged(void)
395 {
396         bool isCallServiceAvailable = false;
397         bool isDataServiceAvailable = false;
398         bool isRoaming = false;
399
400     NetworkStatus networkStatus;
401
402     isCallServiceAvailable = IsServiceAvailable();
403     if (isCallServiceAvailable)
404     {
405         isDataServiceAvailable = IsDataAvailable();
406     }
407     else
408     {
409         isDataServiceAvailable = false;
410     }
411     isRoaming = IsRoaming();
412
413     SysLog(NID_TEL, "The network status Call[%d]->[%d] Data[%d]->[%d] Roaming[%d]->[%d]",
414                 __isCallServiceAvailable, isCallServiceAvailable, __isDataServiceAvailable,
415                 isDataServiceAvailable, __isRoaming, isRoaming);
416
417     if ((__isCallServiceAvailable == isCallServiceAvailable) &&
418         (__isDataServiceAvailable == isDataServiceAvailable) &&
419         (__isRoaming == isRoaming))
420     {
421         return;
422     }
423
424     __isCallServiceAvailable = isCallServiceAvailable;
425     __isDataServiceAvailable = isDataServiceAvailable;
426     __isRoaming = isRoaming;
427
428     _NetworkStatusImpl::GetInstance(networkStatus)->SetIsCallServiceAvailable(isCallServiceAvailable);
429     _NetworkStatusImpl::GetInstance(networkStatus)->SetIsDataServiceAvailable(isDataServiceAvailable);
430     _NetworkStatusImpl::GetInstance(networkStatus)->SetIsRoaming(isRoaming);
431
432     _NetworkManagerEventArg* pEventArg = new (std::nothrow)_NetworkManagerEventArg(networkStatus);
433     SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
434     (void)__pNetworkManagerEvent->Fire(*pEventArg);
435 }
436
437 bool
438 _NetworkManagerImpl::IsDataAvailable(void)
439 {
440     int err = NETWORK_INFO_ERROR_NONE;
441     connection_cellular_state_e dataServiceState = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
442     bool isDataAvailable = false;
443
444     err = connection_get_cellular_state(__pConnectionHandle.get(), &dataServiceState);
445
446     SysLog(NID_TEL, "The return value of connection_get_cellular_state() is 0x%x and the dataServiceState is %d", err, dataServiceState);
447
448     if (dataServiceState == CONNECTION_CELLULAR_STATE_AVAILABLE || dataServiceState == CONNECTION_CELLULAR_STATE_CONNECTED)
449     {
450         isDataAvailable = true;
451     }
452     else
453     {
454         isDataAvailable = false;
455     }
456
457     return isDataAvailable;
458 }
459
460 bool
461 _NetworkManagerImpl::IsRoaming(void)
462 {
463     int err = NETWORK_INFO_ERROR_NONE;
464     bool isRoaming = false;
465
466     err = network_info_is_roaming(&isRoaming);
467     SysLog(NID_TEL, "The return value of network_info_is_roaming() is 0x%x and the roming state is %s", err, isRoaming ? "True" : "False");
468
469     return isRoaming;
470 }
471
472 bool
473 _NetworkManagerImpl::IsServiceAvailable(bool checkEmergency /* = false */)
474 {
475     bool isAvailable = false;
476     int err = NETWORK_INFO_ERROR_NONE;
477     network_info_service_state_e state;
478
479     err = network_info_get_service_state(&state);
480         SysLog(NID_TEL, "The return value of network_info_get_service_state() is 0x%x and the state is %d and checkEmergency is %s",
481                 err, state, checkEmergency ? "True" : "False");
482
483     if (!checkEmergency && state == NETWORK_INFO_SERVICE_STATE_IN_SERVICE)
484     {
485         isAvailable = true;
486     }
487     else if (checkEmergency && (state == NETWORK_INFO_SERVICE_STATE_IN_SERVICE ||
488                                                                             state == NETWORK_INFO_SERVICE_STATE_EMERGENCY_ONLY))
489     {
490         isAvailable = true;
491     }
492
493     return isAvailable;
494 }
495
496 bool
497 _NetworkManagerImpl::IsNetworkAvailable(void)
498 {
499     bool isFlightModeEnabled = false;
500     result r = SettingInfo::GetValue(L"http://tizen.org/setting/network.flight_mode", isFlightModeEnabled);
501         SysLog(NID_TEL, "isFlightModeEnabled is %s", isFlightModeEnabled ? "True":"False");
502         SysLog(NID_TEL, "The return value of SettingInfo::GetValue() is %s", GetErrorMessage(r));
503
504         if (r != E_SUCCESS)
505         {
506             return false;
507         }
508
509     return !isFlightModeEnabled;
510 }
511
512 //const char*
513 //_NetworkManagerImpl::GetStringOfNetworkEventType(_NetworkEventType type) const
514 //{
515 //    static const char* pStateStr[] =
516 //    {
517 //        "CallServiceAvailable",
518 //        "DataServiceAvailable ",
519 //        "Roaming"
520 //    };
521 //    return pStateStr[type];
522 //}
523
524 _NetworkManagerImpl*
525 _NetworkManagerImpl::GetInstance(NetworkManager& networkManager)
526 {
527     return networkManager.__pNetworkManagerImpl;
528 }
529
530 const _NetworkManagerImpl*
531 _NetworkManagerImpl::GetInstance(const NetworkManager& networkManager)
532 {
533     return networkManager.__pNetworkManagerImpl;
534 }
535
536
537 void
538 _NetworkManagerImpl::OnTelephonyNetworkSelectionModeReceived(bool isManual, result r)
539 {
540         SysLog(NID_TEL, "The mode is %s and the result is %s", isManual ? "Manual":"Automatic", GetErrorMessage(r));
541
542     if (__pNetworkSettingEvent != null)
543     {
544         _NetworkSettingEventArg* pEventArg = new (std::nothrow)_NetworkSettingEventArg(_NETWORK_EVENT_GET_SELECTION_MODE, isManual, r);
545         SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
546         (void)__pNetworkSettingEvent->Fire(*pEventArg);
547     }
548 }
549
550 void
551 _NetworkManagerImpl::OnTelephonyNetworkSelectionCompleted(result r)
552 {
553         SysLog(NID_TEL, "The listener has called with the result %s", GetErrorMessage(r));
554
555     if (__pNetworkSettingEvent != null)
556     {
557         _NetworkSettingEventArg* pEventArg = new (std::nothrow)_NetworkSettingEventArg(_NETWORK_EVENT_SELECT_NETWORK, r);
558         SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
559         (void)__pNetworkSettingEvent->Fire(*pEventArg);
560     }
561 }
562
563 void
564 _NetworkManagerImpl::OnTelephonyNetworkSearchCompleted(String message, result r)
565 {
566         SysLog(NID_TEL, "The listener has called with the result %s", GetErrorMessage(r));
567
568     if (__pNetworkSettingEvent != null)
569     {
570         IList* pNetworkInfoList = null;
571
572         if (r == E_SUCCESS)
573         {
574             pNetworkInfoList = ParsingMessageN(message);
575
576             if (pNetworkInfoList == null)
577             {
578                 SysLog(NID_TEL, "ParsingMessageN() has failed ");
579                 r = E_SYSTEM;
580             }
581         }
582
583         _NetworkSettingEventArg* pEventArg = new (std::nothrow)_NetworkSettingEventArg(_NETWORK_EVENT_SEARCH_COMPLETED, pNetworkInfoList, r);
584         SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
585
586         (void)__pNetworkSettingEvent->Fire(*pEventArg);
587     }
588 }
589
590 IList*
591 _NetworkManagerImpl::ParsingMessageN(const String& message)
592 {
593     result r = E_SUCCESS;
594     int currentIndex = 0;
595     String temp;
596
597     NetworkInfo* pNetworkInfo = null;
598
599     int networkInfoCount = 0;
600
601     r = message.SubString(0, 1, temp);
602         SysTryReturn(NID_TEL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));
603
604     temp.Trim();
605
606     r = Integer::Decode(temp, networkInfoCount);
607         SysTryReturn(NID_TEL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));
608
609     ArrayList* pNetworkInfoList = null;
610
611     pNetworkInfoList = new (std::nothrow) ArrayList();
612         SysTryReturn(NID_TEL, pNetworkInfoList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
613
614     r = pNetworkInfoList->Construct(networkInfoCount);
615     SysTryReturn(NID_TEL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
616
617     for (int i = 0 ; i < networkInfoCount ; i++)
618     {
619         pNetworkInfo = GenerateNetworkInfoN(message, currentIndex);
620
621         pNetworkInfoList->Add(*pNetworkInfo);
622     }
623
624     return pNetworkInfoList;
625
626 }
627
628 NetworkInfo*
629 _NetworkManagerImpl::GenerateNetworkInfoN(const String& message, int& currentIndex)
630 {
631     NetworkInfo* pNetworkInfo = null;
632     _NetworkInfoImpl* pNetworkInfoImpl = null;
633
634     int temp = 0;
635     String tempString;
636
637     pNetworkInfo = new (std::nothrow) NetworkInfo();
638     SysTryReturn(NID_TEL, pNetworkInfo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
639
640     pNetworkInfoImpl = _NetworkInfoImpl::GetInstance(*pNetworkInfo);
641
642     // PLMN
643     int mcc = -1;
644     int mnc = -1;
645     tempString = MessageWithLengthToString(message, currentIndex);
646     _TelephonyUtility::GetMccMncFromPlmn(tempString, mcc, mnc);
647     pNetworkInfoImpl->SetPlmn(tempString);
648     pNetworkInfoImpl->SetMcc(mcc);
649     pNetworkInfoImpl->SetMnc(mnc);
650     SysLog(NID_TEL, "The plmn value is %ls, the mcc value is %d and the mnc is %d",tempString.GetPointer(), mcc, mnc);
651
652     // Operator Name
653     tempString = MessageWithLengthToString(message, currentIndex);
654     pNetworkInfoImpl->SetOperatorName(tempString);
655     SysLog(NID_TEL, "The Operator Name is %ls", tempString.GetPointer());
656
657     // NetworkType
658     tempString = MessageToString(message, currentIndex);
659     Tizen::Base::Integer::Parse(tempString, temp);
660
661     _NetworkType networkType = _TelephonyUtility::ConvertNetworkType(static_cast<TelNetworkSystemType_t>(temp));
662     pNetworkInfoImpl->SetNetworkType(networkType);
663     SysLog(NID_TEL, "The NetworkType is %d", networkType);
664
665     return pNetworkInfo;
666 }
667
668
669 String
670 _NetworkManagerImpl::MessageToString(const String& message, int& currentIndex)
671 {
672     int nextIndex = 0;
673     String tempString;
674     result r = E_SUCCESS;
675
676     r = message.IndexOf(":", currentIndex, nextIndex);
677
678     if (r != E_SUCCESS)
679     {
680         SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
681     }
682
683     currentIndex = nextIndex + 1;
684
685     r = message.IndexOf(":", currentIndex, nextIndex);
686
687     if (r != E_SUCCESS)
688     {
689         SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
690     }
691
692     r = message.SubString(currentIndex, nextIndex-currentIndex, tempString);
693
694     if (r != E_SUCCESS)
695     {
696         SysLog(NID_TEL, "The result of message.SubString is %s", GetErrorMessage(r));
697     }
698
699     return tempString;
700 }
701
702 String
703 _NetworkManagerImpl::MessageWithLengthToString(const String& message, int& currentIndex)
704 {
705     // e.g. plmn or network name -> "05Olleh",
706     int nextIndex = 0;
707     String tempString("");
708     result r = E_SUCCESS;
709     int len = 0;
710
711     r = message.IndexOf(":", currentIndex, nextIndex);
712
713     if (r != E_SUCCESS)
714     {
715         SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
716     }
717
718     currentIndex = nextIndex + 1;
719
720     message.SubString(currentIndex, 2, tempString);
721     tempString.Trim();
722     currentIndex += 2;
723     Tizen::Base::Integer::Decode(tempString, static_cast<int&>(len));
724
725     if (len == 0)
726     {
727         tempString = "";
728     }
729     else
730     {
731         r = message.SubString(currentIndex, len, tempString);
732
733         if (r != E_SUCCESS)
734         {
735             SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
736         }
737     }
738
739     currentIndex += len;
740
741     return tempString;
742 }
743
744 }} // Tizen::Telephony