2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file NetService.cpp
20 * @brief This is the implementation file for the %NetService class.
23 #include <net_connection.h>
24 #include <FBaseSysLog.h>
25 #include <FBaseString.h>
26 #include <FNetNetAccountManager.h>
27 #include <FNetNetConnectionInfo.h>
28 #include <FApp_AppManagerImpl.h>
29 #include <FBase_StringConverter.h>
30 #include <FNet_NetTypes.h>
31 #include <FNet_NetAccountInfoImpl.h>
32 #include "ConnectivityUtility.h"
33 #include "NetService.h"
34 #include "NetAccountDatabase.h"
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Net;
42 NetService* NetService::__pNetService = null;
46 NetService::GetInstance(void)
50 if (__pNetService == null)
52 unique_ptr<NetService> pInstance(new (std::nothrow) NetService());
53 SysTryReturn(NID_NET, pInstance != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
55 r = pInstance->Construct();
56 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
58 __pNetService = pInstance.release();
65 NetService::NetService(void)
66 : __pConnectionHandle(null)
67 , __internetAccountId(INVALID_HANDLE)
68 , __mmsAccountId(INVALID_HANDLE)
70 __internetProfileName.Clear();
71 __mmsProfileName.Clear();
74 NetService::~NetService(void)
76 if (__pConnectionHandle != null)
78 connection_destroy(__pConnectionHandle);
79 __pConnectionHandle = null;
84 NetService::Construct(void)
87 int ret = CONNECTION_ERROR_NONE;
89 r = NetAccountDatabase::InitializeRepository();
90 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
94 ret = connection_create(&__pConnectionHandle);
95 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to create a connection handle.[%d]", ret);
101 NetService::GetAppNetAccountId(const String& profileName, int& netAccountId)
103 result r = E_SUCCESS;
105 if (profileName.Equals(L"Internet", false))
107 netAccountId = GetInternetAccountId();
109 else if (profileName.Equals(L"Mms", false))
111 netAccountId = GetMmsAccountId();
115 netAccountId = INVALID_HANDLE;
123 NetService::SetNetAccountId(int netAccountId, int& netAccountId2)
125 result r = E_SUCCESS;
127 netAccountId2 = netAccountId;
133 NetService::UpdateSystemNetAccount(const NetAccountInfo& netAccountInfo)
135 result r = E_SUCCESS;
137 int ret = CONNECTION_ERROR_NONE;
138 connection_h connectionHandle = null;
139 connection_profile_h profileHandle = null;
143 r = NetAccountDatabase::GetProfileName(netAccountInfo.GetAccountId(), profileName);
144 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
146 ret = connection_create(&connectionHandle);
147 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
148 "A system error has been occurred. The return value from connection_create() is %d", ret);
150 profileHandle = ConnectivityUtility::GetPsProfileHandleN(profileName);
151 SysTryReturnResult(NID_NET, profileHandle != null, E_INVALID_ACCOUNT,
152 "Invalid network account. The current value of account id is %d", netAccountInfo.GetAccountId());
154 r = ConnectivityUtility::UpdateProfileInfo(&netAccountInfo, profileHandle, false);
157 SysLogException(NID_NET, r, "[%s] Propagating.", GetErrorMessage(r));
159 connection_profile_destroy(profileHandle);
160 connection_destroy(connectionHandle);
165 ret = connection_update_profile((void*)connectionHandle, profileHandle);
167 connection_profile_destroy(profileHandle);
168 connection_destroy(connectionHandle);
170 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
171 "A system error has been occurred. The return value from connection_update_profile() is %d", ret);
173 r = NetAccountDatabase::UpdateAccountName(netAccountInfo.GetAccountId(), netAccountInfo.GetAccountName());
174 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
180 NetService::ResetNetStatistics(int bearerType, int statType)
182 int err = CONNECTION_ERROR_NONE;
184 SysTryReturnResult(NID_NET, (bearerType == NET_BEARER_WIFI || bearerType == NET_BEARER_PS),
185 E_INVALID_ARG, "Invalid bearer type. [%d]", bearerType);
187 //Reset net statistics info based on type
191 case NET_STAT_RCVD_SIZE:
192 if (bearerType == NET_BEARER_PS)
194 err = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
198 err = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
203 case NET_STAT_SENT_SIZE:
204 if (bearerType == NET_BEARER_PS)
206 err = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
210 err = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
215 case NET_STAT_TOTAL_RCVD_SIZE:
216 if (bearerType == NET_BEARER_PS)
218 err = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
222 err = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
227 case NET_STAT_TOTAL_SENT_SIZE:
228 if (bearerType == NET_BEARER_PS)
230 err = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
234 err = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
240 SysTryReturnResult(NID_NET, false, E_INVALID_ARG, "Invalid argument is used.");
245 SysTryReturnResult(NID_NET, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Operation failed. [err:%d]", err);
251 NetService::ResetAllNetStatistics(int bearerType)
253 int err = CONNECTION_ERROR_NONE;
255 SysTryReturnResult(NID_NET, (bearerType == NET_BEARER_WIFI || bearerType == NET_BEARER_PS),
256 E_INVALID_ARG, "Invalid bearer type. [%d]", bearerType);
258 //we support only PS and WIFI
259 if (bearerType == NET_BEARER_PS)
261 err = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
262 SysTryReturnResult(NID_NET, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Operation failed. [err:%d]", err);
264 err = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
265 SysTryReturnResult(NID_NET, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Operation failed. [err:%d]", err);
267 err = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
268 SysTryReturnResult(NID_NET, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Operation failed. [err:%d]", err);
270 err = connection_reset_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
271 SysTryReturnResult(NID_NET, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Operation failed. [err:%d]", err);
276 err = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA);
277 SysTryReturnResult(NID_NET, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Operation failed. [err:%d]", err);
279 err = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA);
280 SysTryReturnResult(NID_NET, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Operation failed. [err:%d]", err);
282 err = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA);
283 SysTryReturnResult(NID_NET, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Operation failed. [err:%d]", err);
285 err = connection_reset_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA);
286 SysTryReturnResult(NID_NET, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Operation failed. [err:%d]", err);
293 NetService::GetNetConnectionInfo(int netAccountId, NetConnectionInfo* pConnectionInfo)
295 result r = E_SUCCESS;
297 connection_profile_h profileHandle = null;
299 r = NetAccountDatabase::GetProfileName(netAccountId, profileName);
300 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ACCOUNT, "Invalid network account. accountId:%d", netAccountId);
302 profileHandle = ConnectivityUtility::GetPsProfileHandleN(profileName);
303 SysTryReturnResult(NID_NET, profileHandle != null, E_INVALID_ACCOUNT, "Invalid network account. accountId:%d", netAccountId);
305 ConnectivityUtility::UpdateNetConnectionInfo(pConnectionInfo, profileHandle, false);
306 connection_profile_destroy(profileHandle);
312 NetService::GetAllNetConnectionInfo(ArrayList* pList)
314 NetConnectionInfo* pInfo = null;
315 int ret = CONNECTION_ERROR_NONE;
316 connection_profile_iterator_h iterator = null;
317 connection_profile_h profileHandle = null;
319 ret = connection_get_profile_iterator(__pConnectionHandle, CONNECTION_ITERATOR_TYPE_CONNECTED, &iterator);
320 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
321 "A system error has been occurred. The return value from connection_get_profile_iterator() is %d", ret);
322 unique_ptr<void, _ProfileIteratorDeleter> pIterator(iterator);
324 ret = connection_profile_iterator_next(iterator, &profileHandle);
325 while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
327 pInfo = new (std::nothrow) NetConnectionInfo();
328 SysTryReturnResult(NID_NET, pInfo != null, E_OUT_OF_MEMORY, "Insufficient memory.");
330 ConnectivityUtility::UpdateNetConnectionInfo(pInfo, profileHandle, false);
333 profileHandle = null;
334 ret = connection_profile_iterator_next(iterator, &profileHandle);
337 SysLog(NID_NET, "GetAllNetConnectionInfo() has been succeeded with count:%d", pList->GetCount());
343 NetService::CreateNetAccount(const String& packageId, const NetAccountInfo& netAccountInfo)
345 result r = E_SUCCESS;
346 NetAccountId accountId = INVALID_HANDLE;
348 bool isCreated = false;
349 int ret = CONNECTION_ERROR_NONE;
350 connection_h connectionHandle = __pConnectionHandle;
351 connection_profile_h profileHandle = null;
353 SysTryReturn(NID_NET, !netAccountInfo.GetAccountName().IsEmpty(),
354 INVALID_HANDLE, E_INVALID_ARG,
355 "[%s] Invalid argument is used. accountName is an empty string.", GetErrorMessage(E_INVALID_ARG));
357 unique_ptr<IList, AllElementsDeleter> pProfileNamesBefore(NetService::GetAppProfileNamesN());
358 SysTryReturn(NID_NET, pProfileNamesBefore != null, INVALID_HANDLE, E_OUT_OF_MEMORY,
359 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
361 unique_ptr<char[]> pAccountName(_StringConverter::CopyToCharArrayN(netAccountInfo.GetAccountName()));
362 SysTryReturn(NID_NET, pAccountName != null, INVALID_HANDLE, E_OUT_OF_MEMORY,
363 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
365 ret = connection_profile_create(CONNECTION_PROFILE_TYPE_CELLULAR, pAccountName.get(), &profileHandle);
366 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
367 "[%s] A system error has been occurred. The return value from connection_profile_create() is %d",
368 GetErrorMessage(E_SYSTEM), ret);
370 unique_ptr<void, _ProfileDeleter> pProfileHandle(profileHandle);
371 r = ConnectivityUtility::UpdateProfileInfo(&netAccountInfo, profileHandle, true);
372 SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r));
374 ret = connection_add_profile(connectionHandle, profileHandle);
375 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
376 "[%s] A system error has been occurred. The return value from connection_add_profile() is %d",
377 GetErrorMessage(E_SYSTEM), ret);
379 unique_ptr<IList, AllElementsDeleter> pProfileNamesAfter(NetService::GetAppProfileNamesN());
380 SysTryReturn(NID_NET, pProfileNamesAfter != null, INVALID_HANDLE, E_OUT_OF_MEMORY,
381 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
383 unique_ptr<IEnumerator> pEnum(pProfileNamesAfter->GetEnumeratorN());
384 SysTryReturn(NID_NET, pEnum != null, INVALID_HANDLE, E_OUT_OF_MEMORY,
385 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
387 while (pEnum->MoveNext() == E_SUCCESS)
389 String* pProfileName = null;
391 pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
392 if ((pProfileName != null) && (!pProfileName->IsEmpty()))
394 if (!pProfileNamesBefore->Contains(*pProfileName))
396 SysLog(NID_NET, "ProfileName[%ls] is not found before, so it is a new profile name.", pProfileName->GetPointer());
398 profileName = *pProfileName;
405 SysTryReturn(NID_NET, isCreated, INVALID_HANDLE, E_SYSTEM,
406 "[%s] A system error has been occurred. New account is not found.", GetErrorMessage(E_SYSTEM));
407 SysTryReturn(NID_NET, !profileName.IsEmpty(), INVALID_HANDLE, E_SYSTEM,
408 "[%s] A system error has been occurred. The profileName of new account is an empty string.", GetErrorMessage(E_SYSTEM));
410 r = NetAccountDatabase::AddAccount(netAccountInfo.GetAccountName(), profileName, packageId, accountId);
411 SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r));
415 SysSecureLog( NID_NET, "CreateNetAccount() has been succeeded with accountId:%d, accountName:%ls, profileName:%ls",
416 netAccountInfo.GetAccountId(), netAccountInfo.GetAccountName().GetPointer(), profileName.GetPointer());
422 NetService::DeleteNetAccount(const String& packageId, NetAccountId netAccountId)
424 result r = E_SUCCESS;
426 bool isReadOnly = true;
427 int ret = CONNECTION_ERROR_NONE;
428 connection_h connectionHandle = __pConnectionHandle;
430 SysLog(NID_NET, "DeleteNetAccount() has been called with accountId:%d", netAccountId);
432 SysTryReturnResult(NID_NET, netAccountId > 0, E_INVALID_ACCOUNT,
433 "Invalid network account. accountId=%d", netAccountId);
435 isReadOnly = NetAccountDatabase::IsReadOnly(packageId, netAccountId);
437 SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
438 SysTryReturnResult(NID_NET, !isReadOnly, E_INVALID_OPERATION, "Not allowed on the specified network account.");
440 r = NetAccountDatabase::GetProfileName(netAccountId, profileName);
441 SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
442 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ACCOUNT, "Invalid network account. accountId=%d", netAccountId);
444 unique_ptr<void, _ProfileDeleter> pProfileHandle(ConnectivityUtility::GetPsProfileHandleN(profileName));
445 SysTryReturnResult(NID_NET, pProfileHandle != null, E_INVALID_ACCOUNT,
446 "Invalid network account. accountId=%d", netAccountId);
448 ret = connection_remove_profile(connectionHandle, pProfileHandle.get());
449 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
450 "A system error has been occurred. The return value from connection_remove_profile() is %d", ret);
452 r = NetAccountDatabase::RemoveAccountByAccountId(netAccountId);
453 SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
454 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
455 "A system error has been occurred. Failed to remove the account.");
461 NetService::UpdateNetAccount(const String& packageId, const NetAccountInfo& netAccountInfo)
463 result r = E_SUCCESS;
465 bool isReadOnly = true;
466 const _NetAccountInfoImpl* pInfoImpl = _NetAccountInfoImpl::GetInstance(netAccountInfo);
467 int ret = CONNECTION_ERROR_NONE;
468 connection_h connectionHandle = __pConnectionHandle;
470 SysTryReturnResult(NID_NET, pInfoImpl != null, E_INVALID_ARG,
471 "Invalid argument is used. Network account info is invalid.");
472 SysTryReturnResult(NID_NET, netAccountInfo.GetAccountId() > 0, E_INVALID_ARG,
473 "Invalid argument is used. accountId=%d", netAccountInfo.GetAccountId());
474 SysTryReturnResult(NID_NET, !netAccountInfo.GetAccountName().IsEmpty(), E_INVALID_ARG,
475 "Invalid argument is used. accountName is an empty string.");
477 isReadOnly = NetAccountDatabase::IsReadOnly(packageId, netAccountInfo.GetAccountId());
479 SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
480 SysTryReturnResult(NID_NET, !isReadOnly, E_INVALID_OPERATION,
481 "Not allowed on the specified network account.");
483 r = NetAccountDatabase::GetProfileName(netAccountInfo.GetAccountId(), profileName);
484 SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
485 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ARG,
486 "Invalid argument is used. accountId=%d.", netAccountInfo.GetAccountId());
488 unique_ptr<void, _ProfileDeleter> pProfileHandle(ConnectivityUtility::GetPsProfileHandleN(profileName));
489 SysTryReturnResult(NID_NET, pProfileHandle != null, E_INVALID_ACCOUNT,
490 "Invalid network account. accountId=%d", netAccountInfo.GetAccountId());
492 r = ConnectivityUtility::UpdateProfileInfo(&netAccountInfo, pProfileHandle.get(), true);
493 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
495 ret = connection_update_profile(connectionHandle, pProfileHandle.get());
496 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
497 "A system error has been occurred. The return value from connection_update_profile() is %d",
498 GetErrorMessage(E_SYSTEM), ret);
500 r = NetAccountDatabase::UpdateAccountName(netAccountInfo.GetAccountId(), netAccountInfo.GetAccountName());
501 SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
502 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
503 "A system error has been occurred. Failed to update accountName.");
509 NetService::GetNetAccountInfo(const String& packageId, NetAccountId netAccountId, NetAccountInfo* pNetAccountInfo) const
511 result r = E_SUCCESS;
512 connection_profile_h profileHandle = null;
513 unique_ptr<void, _ProfileDeleter> pProfileHandle;
515 SysTryReturnResult(NID_NET, netAccountId > 0, E_INVALID_ACCOUNT, "Invalid network account. accountId=%d.", netAccountId);
516 SysTryReturnResult(NID_NET, netAccountId != _DEFAULT_WIFI_DIRECT_ACCOUNT_ID, E_INVALID_ACCOUNT, "Wifi direct account information is not found.");
517 SysTryReturnResult(NID_NET, netAccountId != _DEFAULT_USB_ACCOUNT_ID, E_INVALID_ACCOUNT, "USB account information is not found.");
519 _NetAccountInfoImpl* pImpl = _NetAccountInfoImpl::GetInstance(*pNetAccountInfo);
522 r = pNetAccountInfo->Construct();
523 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
525 pImpl = _NetAccountInfoImpl::GetInstance(*pNetAccountInfo);
526 SysTryReturnResult(NID_NET, pImpl != null, E_SYSTEM, "Failed to construct NetAccountInfo.");
531 r = NetAccountDatabase::GetProfileName(netAccountId, profileName);
532 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ACCOUNT, "Ps account is NOT found for accountId[%d].", netAccountId);
534 profileHandle = ConnectivityUtility::GetPsProfileHandleN(profileName);
535 SysTryReturnResult(NID_NET, profileHandle != null, E_INVALID_ACCOUNT, "Ps account is NOT found for accountId[%d].", netAccountId);
536 pProfileHandle.reset(profileHandle);
538 pImpl->SetReadOnly(NetAccountDatabase::IsReadOnly(packageId, netAccountId));
540 r = ConnectivityUtility::UpdateNetAccountInfo(pNetAccountInfo, profileHandle);
546 NetService::GetNetAccountIds(ArrayListT<NetAccountId>* pList)
548 UpdateNetAccountDb();
552 return NetAccountDatabase::GetAccountIds(pList);
556 NetService::GetNetAccountNames(ArrayList* pList)
558 UpdateNetAccountDb();
562 return NetAccountDatabase::GetAccountNames(pList);
566 NetService::GetNetAccountId(const String& netAccountName)
570 result r = E_SUCCESS;
571 NetAccountId accountId = INVALID_HANDLE;
573 r = NetAccountDatabase::GetAccountIdByAccountName(netAccountName, accountId);
574 SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, E_INVALID_ARG,
575 "[%s] Invalid argument is used. AccountName[%ls] is not found.",
576 GetErrorMessage(E_INVALID_ARG), netAccountName.GetPointer());
578 SysSecureLog(NID_NET, "GetNetAccountId() has been succeeded with accountId:%d", accountId);
584 NetService::GetNetAccountId(NetBearerType netBearerType)
588 NetAccountId accountId = INVALID_HANDLE;
590 if (netBearerType == NET_BEARER_PS)
592 accountId = GetInternetAccountId();
594 else if (netBearerType == NET_BEARER_MMS)
596 accountId = GetMmsAccountId();
598 else if (netBearerType == NET_BEARER_WIFI)
600 accountId = _DEFAULT_WIFI_ACCOUNT_ID;
602 else if (netBearerType == NET_BEARER_WIFI_DIRECT)
604 accountId = _DEFAULT_WIFI_DIRECT_ACCOUNT_ID;
606 else if (netBearerType == NET_BEARER_USB)
608 accountId = _DEFAULT_USB_ACCOUNT_ID;
612 SysLogException(NID_NET, E_INVALID_ARG,
613 "[%s] Invalid argument is used. bearerType=%d", GetErrorMessage(E_INVALID_ARG), netBearerType);
620 NetService::GetAppProfileNamesN(void)
624 result r = E_SUCCESS;
625 int ret = CONNECTION_ERROR_NONE;
626 connection_h connectionHandle = null;
627 connection_profile_iterator_h iterator = null;
628 connection_profile_h profileHandle = null;
629 connection_profile_type_e profileType = CONNECTION_PROFILE_TYPE_WIFI;
631 ret = connection_create(&connectionHandle);
632 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
633 "[%s] A system error has been occurred. The return value from connection_create() is %d",
634 GetErrorMessage(E_SYSTEM), ret);
635 unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
637 unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
638 SysTryReturn(NID_NET, pList != null, null, E_OUT_OF_MEMORY,
639 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
641 r = pList->Construct();
642 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
644 ret = connection_get_profile_iterator(connectionHandle, CONNECTION_ITERATOR_TYPE_REGISTERED, &iterator);
645 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
646 "[%s] A system error has been occurred. The return value from connection_get_profile_iterator() is %d",
647 GetErrorMessage(E_SYSTEM), ret);
648 unique_ptr<void, _ProfileIteratorDeleter> pIterator(iterator);
650 ret = connection_profile_iterator_next(iterator, &profileHandle);
651 while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
653 char* pProfileName = null;
655 ret = connection_profile_get_type(profileHandle, &profileType);
656 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
657 "[%s] A system error has been occurred. The return value from connection_profile_get_type() is %d",
658 GetErrorMessage(E_SYSTEM), ret);
660 if (profileType == CONNECTION_PROFILE_TYPE_CELLULAR)
662 connection_cellular_service_type_e serviceType = CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN;
664 ret = connection_profile_get_cellular_service_type(profileHandle, &serviceType);
665 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
666 "[%s] A system error has been occurred. The return value from connection_profile_get_cellular_service_type() is %d",
667 GetErrorMessage(E_SYSTEM), ret);
669 if (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION)
671 ret = connection_profile_get_id(profileHandle, &pProfileName);
672 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
673 "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d",
674 GetErrorMessage(E_SYSTEM), ret);
676 unique_ptr<String> pProfileNameString(new (std::nothrow) String(pProfileName));
678 SysTryReturn(NID_NET, pProfileNameString != null, null, E_OUT_OF_MEMORY,
679 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
681 r = pList->Add(*pProfileNameString);
682 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
683 pProfileNameString.release();
687 // SysLog(NID_NET, "Ignore non-app profile.");
692 // SysLog(NID_NET, "Ignore non-cellular profile.");
695 profileHandle = null;
696 ret = connection_profile_iterator_next(iterator, &profileHandle);
699 SysLog(NID_NET, "GetAppProfileNamesN() has been succeeded with profiles count:%d", pList->GetCount());
701 return pList.release();
705 NetService::GetAllProfileNamesN(void)
709 result r = E_SUCCESS;
710 int ret = CONNECTION_ERROR_NONE;
711 connection_h connectionHandle = null;
712 connection_profile_iterator_h iterator = null;
713 connection_profile_h profileHandle = null;
714 connection_profile_type_e profileType = CONNECTION_PROFILE_TYPE_WIFI;
716 ret = connection_create(&connectionHandle);
717 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
718 "[%s] A system error has been occurred. The return value from connection_create() is %d",
719 GetErrorMessage(E_SYSTEM), ret);
720 unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
722 unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
723 SysTryReturn(NID_NET, pList != null, null, E_OUT_OF_MEMORY,
724 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
726 r = pList->Construct();
727 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
729 ret = connection_get_profile_iterator(connectionHandle, CONNECTION_ITERATOR_TYPE_REGISTERED, &iterator);
730 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
731 "[%s] A system error has been occurred. The return value from connection_get_profile_iterator() is %d",
732 GetErrorMessage(E_SYSTEM), ret);
733 unique_ptr<void, _ProfileIteratorDeleter> pIterator(iterator);
735 ret = connection_profile_iterator_next(iterator, &profileHandle);
736 while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
738 ret = connection_profile_get_type(profileHandle, &profileType);
739 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
740 "[%s] A system error has been occurred. The return value from connection_profile_get_type() is %d",
741 GetErrorMessage(E_SYSTEM), ret);
743 if (profileType == CONNECTION_PROFILE_TYPE_CELLULAR)
745 connection_cellular_service_type_e serviceType = CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN;
747 ret = connection_profile_get_cellular_service_type(profileHandle, &serviceType);
748 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
749 "[%s] A system error has been occurred. The return value from connection_profile_get_cellular_service_type() is %d",
750 GetErrorMessage(E_SYSTEM), ret);
752 if ((serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET)
753 || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
754 || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_MMS)
755 || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS)
756 || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION))
758 char* pProfileName = null;
759 char* pProfileDisplayName = null;
761 String profileDisplayName;
763 ret = connection_profile_get_id(profileHandle, &pProfileName);
764 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
765 "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d",
766 GetErrorMessage(E_SYSTEM), ret);
767 profileName = String(pProfileName);
770 ret = connection_profile_get_name(profileHandle, &pProfileDisplayName);
771 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
772 "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d",
773 GetErrorMessage(E_SYSTEM), ret);
774 profileDisplayName = String(pProfileDisplayName);
775 free(pProfileDisplayName);
777 unique_ptr<String> pNetProfileName(new (std::nothrow) String(profileName));
779 SysLog(NID_NET, "Cellular profile is found: [%d][%ls][%ls].", serviceType, profileDisplayName.GetPointer(), profileName.GetPointer());
781 r = pList->Add(*pNetProfileName);
782 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
783 pNetProfileName.release();
787 // SysLog(NID_NET, "Ignore unknown profile.");
792 // SysLog(NID_NET, "Ignore non-cellular profile.");
795 profileHandle = null;
796 ret = connection_profile_iterator_next(iterator, &profileHandle);
799 SysLog(NID_NET, "GetAllProfileNamesN() has been succeeded with profiles count:%d", pList->GetCount());
801 return pList.release();
805 NetService::GetInternetAccountId(void)
807 result r = E_SUCCESS;
808 NetAccountId accountId = INVALID_HANDLE;
810 int ret = CONNECTION_ERROR_NONE;
811 connection_h connectionHandle = null;
812 connection_profile_h profileHandle = null;
814 ret = connection_create(&connectionHandle);
815 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
816 "[%s] A system error has been occurred. The return value from connection_create() is %d",
817 GetErrorMessage(E_SYSTEM), ret);
818 unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
820 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET, &profileHandle);
821 if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null))
823 SysLog(NID_NET, "Internet profile is not found, so search a prepaid-Internet profile.");
825 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET, &profileHandle);
827 SysTryReturn(NID_NET, profileHandle != null, INVALID_HANDLE, E_INVALID_ACCOUNT,
828 "[%s] Internet(incl. prepaid) profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
829 unique_ptr<void, _ProfileDeleter> pProfileHandle(profileHandle);
831 char* pProfileName = null;
832 char* pProfileDisplayName = null;
834 String profileDisplayName;
836 ret = connection_profile_get_id(profileHandle, &pProfileName);
837 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
838 "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d",
839 GetErrorMessage(E_SYSTEM), ret);
841 profileName = String(pProfileName);
844 ret = connection_profile_get_name(profileHandle, &pProfileDisplayName);
845 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
846 "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d",
847 GetErrorMessage(E_SYSTEM), ret);
849 profileDisplayName = String(pProfileDisplayName);
850 free(pProfileDisplayName);
852 SysLog(NID_NET, "Internet profile is found. [%ls][%ls]", profileDisplayName.GetPointer(), profileName.GetPointer());
854 if (!__internetProfileName.IsEmpty())
856 if (!profileName.Equals(__internetProfileName, false))
858 // Internet profile is changed.
859 SysLog(NID_NET, "Internet profile is changed. [%ls]->[%ls]", __internetProfileName.GetPointer(), profileName.GetPointer());
861 UpdateNetAccountDb();
862 r = NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
864 __internetProfileName = profileName;
865 __internetAccountId = accountId;
869 accountId = __internetAccountId;
874 SysLog(NID_NET, "Initialize Internet profile.");
876 UpdateNetAccountDb();
877 r = NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
879 __internetProfileName = profileName;
880 __internetAccountId = accountId;
889 NetService::GetMmsAccountId(void)
891 result r = E_SUCCESS;
892 NetAccountId accountId = INVALID_HANDLE;
894 int ret = CONNECTION_ERROR_NONE;
895 connection_h connectionHandle = null;
896 connection_profile_h profileHandle = null;
898 ret = connection_create(&connectionHandle);
899 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
900 "[%s] A system error has been occurred. The return value from connection_create() is %d",
901 GetErrorMessage(E_SYSTEM), ret);
902 unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
904 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profileHandle);
905 if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null))
907 SysLog(NID_NET, "MMS profile is not found, so search a prepaid-MMS profile.");
909 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS, &profileHandle);
911 SysTryReturn(NID_NET, profileHandle != null, INVALID_HANDLE, E_INVALID_ACCOUNT,
912 "[%s] MMS(incl. prepaid) profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
913 unique_ptr<void, _ProfileDeleter> pProfileHandle(profileHandle);
915 char* pProfileName = null;
916 char* pProfileDisplayName = null;
918 String profileDisplayName;
920 ret = connection_profile_get_id(profileHandle, &pProfileName);
921 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
922 "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d",
923 GetErrorMessage(E_SYSTEM), ret);
925 profileName = String(pProfileName);
928 ret = connection_profile_get_name(profileHandle, &pProfileDisplayName);
929 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
930 "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d",
931 GetErrorMessage(E_SYSTEM), ret);
933 profileDisplayName = String(pProfileDisplayName);
934 free(pProfileDisplayName);
936 SysLog(NID_NET, "MMS profile is found. [%ls][%ls]", profileDisplayName.GetPointer(), profileName.GetPointer());
938 if (!__mmsProfileName.IsEmpty())
940 if (!profileName.Equals(__mmsProfileName, false))
942 // MMS profile is changed.
943 SysLog(NID_NET, "MMS profile is changed. [%ls]->[%ls]", __mmsProfileName.GetPointer(), profileName.GetPointer());
945 UpdateNetAccountDb();
946 r = NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
948 __mmsProfileName = profileName;
949 __mmsAccountId = accountId;
953 accountId = __mmsAccountId;
958 SysLog(NID_NET, "Initialize MMS profile.");
960 UpdateNetAccountDb();
961 r = NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
963 __mmsProfileName = profileName;
964 __mmsAccountId = accountId;
973 NetService::UpdateNetAccountDb(void)
975 result r = E_SUCCESS;
976 NetAccountId accountId = INVALID_HANDLE;
977 unique_ptr<IList, AllElementsDeleter> pAccountList(NetAccountDatabase::GetProfileNamesN());
978 unique_ptr<IList, AllElementsDeleter> pProfileList(NetService::GetAllProfileNamesN());
980 if (pProfileList != null)
982 if (pProfileList->GetCount() > 0)
984 SysLog(NID_NET, "[%d] profiles are found.", pProfileList->GetCount());
986 unique_ptr<IEnumerator> pEnum(pProfileList->GetEnumeratorN());
989 String* pProfileName = null;
990 while (pEnum->MoveNext() == E_SUCCESS)
992 pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
993 if (pProfileName != null)
995 if (pAccountList->Contains(*pProfileName))
997 SysLog(NID_NET, "ProfileName[%ls] is already on DB.", pProfileName->GetPointer());
998 pAccountList->Remove(*pProfileName, true);
1002 SysLog(NID_NET, "ProfileName[%ls] is not found on DB, so add it.", pProfileName->GetPointer());
1003 r = NetAccountDatabase::AddAccount(*pProfileName, *pProfileName, String(SYSTEM_PACKAGE_NAME), accountId);
1011 if (pAccountList != null)
1013 if (pAccountList->GetCount() > 0)
1015 SysLog(NID_NET, "[%d] zombie accounts are on DB.", pAccountList->GetCount());
1017 unique_ptr<IEnumerator> pEnum(pAccountList->GetEnumeratorN());
1020 String* pProfileName = null;
1021 while (pEnum->MoveNext() == E_SUCCESS)
1023 pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
1024 if (pProfileName != null)
1026 r = NetAccountDatabase::RemoveAccountByProfileName(*pProfileName);
1035 SysLog(NID_NET, "Network account database is updated.");