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