merge with master
[framework/osp/net.git] / src / FNet_NetAccountInfoImpl.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                FNet_NetAccountInfoImpl.cpp
19  * @brief               This is the implementation for the %_NetAccountInfoImpl class.
20  */
21
22 #include <net_connection.h>
23 #include <FBaseString.h>
24 #include <FNetNetTypes.h>
25 #include <FNetNetEndPoint.h>
26 #include <FNetNetAccountInfo.h>
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include "FNet_NetTypes.h"
30 #include "FNet_NetAccountInfoImpl.h"
31 #include "FNet_NetAccountDatabase.h"
32
33 using namespace std;
34 using namespace Tizen::Base;
35 using namespace Tizen::Net::Wifi;
36
37 namespace Tizen { namespace Net {
38
39 static const int _MAX_ACCESS_POINT_NAME_LENGTH = 64;
40
41 _NetAccountInfoImpl::_NetAccountInfoImpl(void)
42         : __accountId(INVALID_HANDLE)
43         , __accountName()
44         , __accessPointName()
45         , __authType(NET_NAPAUTH_NONE)
46         , __authId()
47         , __authPassword()
48         , __homeUrl()
49         , __bearerType(NET_BEARER_NONE)
50         , __protocolType(NET_PROTO_TYPE_NONE)
51         , __localAddressScheme(NET_ADDRESS_SCHEME_NONE)
52         , __dnsAddressScheme(NET_ADDRESS_SCHEME_NONE)
53         , __pLocalAddress(null)
54         , __pPrimaryDnsAddress(null)
55         , __pSecondaryDnsAddress(null)
56         , __pProxyAddress(null)
57         , __isReadOnly(true)
58 {
59 }
60
61 _NetAccountInfoImpl::~_NetAccountInfoImpl(void)
62 {
63 }
64
65 result
66 _NetAccountInfoImpl::Construct(const _NetAccountInfoImpl& netAccountInfo)
67 {
68         result r = E_SUCCESS;
69         unique_ptr<IpAddress> pLocalAddress;
70         unique_ptr<IpAddress> pPrimaryDnsAddress;
71         unique_ptr<IpAddress> pSecondaryDnsAddress;
72         unique_ptr<NetEndPoint> pProxyAddress;
73
74         SysAssertf(__bearerType == NET_BEARER_NONE,
75                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
76
77         SysTryReturnResult(NID_NET, netAccountInfo.__bearerType != NET_BEARER_NONE, E_INVALID_ARG,
78                         "Invalid argument is used. Network bearer type is invalid.");
79
80         if (netAccountInfo.__pLocalAddress != null)
81         {
82                 pLocalAddress.reset(netAccountInfo.__pLocalAddress->CloneN());
83                 SysTryReturnResult(NID_NET, pLocalAddress != null, E_OUT_OF_MEMORY,     "Memory allocation failed.");
84         }
85
86         if (netAccountInfo.__pPrimaryDnsAddress != null)
87         {
88                 pPrimaryDnsAddress.reset(netAccountInfo.__pPrimaryDnsAddress->CloneN());
89                 SysTryReturnResult(NID_NET, pPrimaryDnsAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
90         }
91
92         if (netAccountInfo.__pSecondaryDnsAddress != null)
93         {
94                 pSecondaryDnsAddress.reset(netAccountInfo.__pSecondaryDnsAddress->CloneN());
95                 SysTryReturnResult(NID_NET, pSecondaryDnsAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
96         }
97
98         if (netAccountInfo.__pProxyAddress != null)
99         {
100                 pProxyAddress.reset(new (std::nothrow) NetEndPoint(*netAccountInfo.__pProxyAddress));
101                 SysTryReturnResult(NID_NET, pProxyAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
102         }
103
104         __accountId = netAccountInfo.__accountId;
105         __accountName = netAccountInfo.__accountName;
106         __accessPointName = netAccountInfo.__accessPointName;
107         __authType = netAccountInfo.__authType;
108         __authId = netAccountInfo.__authId;
109         __authPassword = netAccountInfo.__authPassword;
110         __homeUrl = netAccountInfo.__homeUrl;
111         __bearerType = netAccountInfo.__bearerType;
112         __protocolType = netAccountInfo.__protocolType;
113         __localAddressScheme = netAccountInfo.__localAddressScheme;
114         __dnsAddressScheme = netAccountInfo.__dnsAddressScheme;
115         __pLocalAddress = move(pLocalAddress);
116         __pPrimaryDnsAddress = move(pPrimaryDnsAddress);
117         __pSecondaryDnsAddress = move(pSecondaryDnsAddress);
118         __pProxyAddress = move(pProxyAddress);
119          __isReadOnly = netAccountInfo.__isReadOnly;
120
121         return r;
122 }
123
124 result
125 _NetAccountInfoImpl::Construct(void)
126 {
127         SysAssertf(__bearerType == NET_BEARER_NONE,
128                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
129
130         __bearerType = NET_BEARER_PS;
131
132         return E_SUCCESS;
133 }
134
135 result
136 _NetAccountInfoImpl::Construct(NetAccountInfo* pAccountInfo, void* pProfileHandle)
137 {
138         result r = E_SUCCESS;
139         NetAccountId accountId = INVALID_HANDLE;
140         String accountName;
141         String accessPointName;
142         NetNapAuthType  authType = NET_NAPAUTH_NONE;
143         String authId;
144         String authPassword;
145         String homeUrl;
146         NetBearerType bearerType;
147         NetProtocolType protocolType;
148         NetAddressScheme localAddressScheme;
149         NetAddressScheme dnsAddressScheme;
150         std::unique_ptr<IpAddress> pLocalAddress;
151         std::unique_ptr<IpAddress> pPrimaryDnsAddress;
152         std::unique_ptr<IpAddress> pSecondaryDnsAddress;
153         std::unique_ptr<NetEndPoint> pProxyAddress;
154         bool isReadOnly = true;
155         int ret = CONNECTION_ERROR_NONE;
156         connection_profile_h profileHandle = static_cast<connection_profile_h>(pProfileHandle);
157
158         SysAssertf(__bearerType == NET_BEARER_NONE,
159                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
160
161         SysTryReturnResult(NID_NET, pAccountInfo != null, E_INVALID_ARG, "Invalid argument is used. The account info is null.");
162         SysTryReturnResult(NID_NET, profileHandle != null, E_INVALID_ARG, "Invalid argument is used. The profile handle is null.");
163
164         connection_profile_type_e profileType = CONNECTION_PROFILE_TYPE_CELLULAR;
165         ret = connection_profile_get_type(profileHandle, &profileType);
166         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
167                         "A system error has been occurred. The return value from connection_profile_get_type() is %d", ret);
168
169         if (profileType == CONNECTION_PROFILE_TYPE_CELLULAR)
170         {
171                 String profileName;
172                 char* pProfileName = null;
173                 char* pApn = null;
174                 connection_cellular_service_type_e serviceType = CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN;
175                 connection_cellular_auth_type_e netAuthType = CONNECTION_CELLULAR_AUTH_TYPE_NONE;
176                 char* pAuthId = null;
177                 char* pAuthPassword = null;
178                 char* pHomeUrl = null;
179
180                 ret = connection_profile_get_id(profileHandle, &pProfileName);
181                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
182                                 "A system error has been occurred. The return value from connection_profile_get_id() is %d", ret);
183
184                 profileName = String(pProfileName);
185                 free(pProfileName);
186
187                 r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
188                 SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
189                 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
190                                 "A system error has been occurred. Failed to get the account Id from DB.");
191
192                 r = _NetAccountDatabase::GetAccountName(accountId, accountName);
193                 SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
194                 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
195                                 "A system error has been occurred. Failed to get the account name from DB.");
196
197                 connection_profile_get_cellular_service_type(profileHandle, &serviceType);
198                 if ((serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_MMS) || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS))
199                 {
200                         bearerType = NET_BEARER_MMS;
201                         isReadOnly  = true;
202                 }
203                 else if ((serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET) || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET))
204                 {
205                         bearerType = NET_BEARER_PS;
206                         isReadOnly  = true;
207                 }
208                 else
209                 {
210                         bearerType = NET_BEARER_PS;
211                         isReadOnly  = false;
212                 }
213
214                 ret = connection_profile_get_cellular_apn(profileHandle, &pApn);
215                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
216                                 "A system error has been occurred. The return value from connection_profile_get_cellular_apn() is %d", ret);
217
218                 accessPointName = String(pApn);
219                 free(pApn);
220
221                 ret = connection_profile_get_cellular_auth_info(profileHandle, &netAuthType, &pAuthId, &pAuthPassword);
222                 if (netAuthType == CONNECTION_CELLULAR_AUTH_TYPE_PAP)
223                 {
224                         authType = NET_NAPAUTH_PAP;
225                 }
226                 else if (netAuthType == CONNECTION_CELLULAR_AUTH_TYPE_CHAP)
227                 {
228                         authType = NET_NAPAUTH_CHAP;
229                 }
230                 else
231                 {
232                         authType = NET_NAPAUTH_NONE;
233                 }
234
235                 if (pAuthId != null)
236                 {
237                         authId = String(pAuthId);
238                         free(pAuthId);
239                 }
240
241                 if (pAuthPassword != null)
242                 {
243                         authPassword = String(pAuthPassword);
244                         free(pAuthPassword);
245                 }
246
247                 ret = connection_profile_get_cellular_home_url(profileHandle, &pHomeUrl);
248                 if ((ret == CONNECTION_ERROR_NONE) && (pHomeUrl != null))
249                 {
250                         homeUrl = String(pHomeUrl);
251                         free(pHomeUrl);
252                 }
253         }
254         else if (profileType == CONNECTION_PROFILE_TYPE_WIFI)
255         {
256                 accountId = _DEFAULT_WIFI_ACCOUNT_ID;
257                 bearerType = NET_BEARER_WIFI;
258         }
259         else
260         {
261                 SysLogException(NID_NET, E_INVALID_ARG, "[%s] Invalid argument is used. The profile info is invalid.", GetErrorMessage(E_INVALID_ARG));
262                 return E_INVALID_ARG;
263         }
264
265         protocolType = NET_PROTO_TYPE_IPV4;
266
267         connection_ip_config_type_e ipConfigType = CONNECTION_IP_CONFIG_TYPE_NONE;
268         ret = connection_profile_get_ip_config_type(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, &ipConfigType);
269         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
270                         "A system error has been occurred. The return value from connection_profile_get_ip_config_type() is %d", ret);
271         if ((ipConfigType == CONNECTION_IP_CONFIG_TYPE_STATIC) || (ipConfigType == CONNECTION_IP_CONFIG_TYPE_FIXED))
272         {
273                 localAddressScheme = NET_ADDRESS_SCHEME_STATIC;
274                 dnsAddressScheme = NET_ADDRESS_SCHEME_STATIC;
275         }
276         else
277         {
278                 localAddressScheme = NET_ADDRESS_SCHEME_DYNAMIC;
279                 dnsAddressScheme = NET_ADDRESS_SCHEME_DYNAMIC;
280         }
281
282         char* pIpAddr = null;
283         String ipAddr;
284
285         ret = connection_profile_get_ip_address(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, &pIpAddr);
286         if ((ret == CONNECTION_ERROR_NONE) && (pIpAddr != null))
287         {
288                 ipAddr = String(pIpAddr);
289                 pLocalAddress.reset(new (std::nothrow) Ip4Address(ipAddr));
290                 free(pIpAddr);
291                 pIpAddr = null;
292                 SysTryReturnResult(NID_NET, pLocalAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
293         }
294
295         ret = connection_profile_get_dns_address(profileHandle, 1, CONNECTION_ADDRESS_FAMILY_IPV4, &pIpAddr);
296         if ((ret == CONNECTION_ERROR_NONE) && (pIpAddr != null))
297         {
298                 ipAddr = String(pIpAddr);
299                 pPrimaryDnsAddress.reset(new (std::nothrow) Ip4Address(ipAddr));
300                 free(pIpAddr);
301                 pIpAddr = null;
302                 SysTryReturnResult(NID_NET, pPrimaryDnsAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
303         }
304
305         ret = connection_profile_get_dns_address(profileHandle, 2, CONNECTION_ADDRESS_FAMILY_IPV4, &pIpAddr);
306         if ((ret == CONNECTION_ERROR_NONE) && (pIpAddr != null))
307         {
308                 ipAddr = String(pIpAddr);
309                 pSecondaryDnsAddress.reset(new (std::nothrow) Ip4Address(ipAddr));
310                 free(pIpAddr);
311                 pIpAddr = null;
312                 SysTryReturnResult(NID_NET, pSecondaryDnsAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
313         }
314
315         char* pProxyAddr = null;
316         String proxyAddr;
317         int delimeterIndex = 0;
318
319         ret = connection_profile_get_proxy_address(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, &pProxyAddr);
320         if ((ret == CONNECTION_ERROR_NONE) && (pProxyAddr != null))
321         {
322                 proxyAddr = String(pProxyAddr);
323                 free(pProxyAddr);
324
325                 r = proxyAddr.IndexOf(L":", 0, delimeterIndex);
326                 if (r == E_SUCCESS)
327                 {
328                         String  proxyIpAddr;
329                         String proxyPort;
330                         int port = 0;
331
332                         r = proxyAddr.SubString(0, delimeterIndex, proxyIpAddr);
333                         r = proxyAddr.SubString(delimeterIndex+1, proxyPort);
334
335                         Ip4Address address(proxyIpAddr);
336                         Integer::Parse(proxyPort, port);
337
338                         pProxyAddress.reset(new (std::nothrow) NetEndPoint(address, port));
339                         SysTryReturnResult(NID_NET, pProxyAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
340                 }
341         }
342
343         __accountId = accountId;
344         __accountName = accountName;
345         __accessPointName = accessPointName;
346         __authType = authType;
347         __authId = authId;
348         __authPassword = authPassword;
349         __homeUrl = homeUrl;
350         __bearerType = bearerType;
351         __protocolType = protocolType;
352         __localAddressScheme = localAddressScheme;
353         __dnsAddressScheme = dnsAddressScheme;
354         __pLocalAddress = move(pLocalAddress);
355         __pPrimaryDnsAddress = move(pPrimaryDnsAddress);
356         __pSecondaryDnsAddress = move(pSecondaryDnsAddress);
357         __pProxyAddress = move(pProxyAddress);
358         __isReadOnly = isReadOnly;
359
360         pAccountInfo->__pNetAccountInfoImpl = this;
361
362         return E_SUCCESS;
363 }
364
365 NetAccountId
366 _NetAccountInfoImpl::GetAccountId(void) const
367 {
368         ClearLastResult();
369
370         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
371
372         return __accountId;
373 }
374
375 result
376 _NetAccountInfoImpl::SetAccountId(NetAccountId accountId)
377 {
378         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
379
380         __accountId = accountId;
381
382         return E_SUCCESS;
383 }
384
385 String
386 _NetAccountInfoImpl::GetAccountName(void) const
387 {
388         ClearLastResult();
389
390         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
391
392         return __accountName;
393 }
394
395 result
396 _NetAccountInfoImpl::SetAccountName(const String& accountName)
397 {
398         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
399
400         __accountName = accountName;
401
402         if (__accountName.GetLength() > _MAX_ACCOUNT_NAME_LENGTH)
403         {
404                 SysLog(NID_NET, "AccountName's length[%d] is over [%d], so truncate it.",
405                                 __accountName.GetLength(), _MAX_ACCOUNT_NAME_LENGTH);
406
407                 __accountName.SetLength(_MAX_ACCOUNT_NAME_LENGTH);
408         }
409
410         return E_SUCCESS;
411 }
412
413 NetProtocolType
414 _NetAccountInfoImpl::GetProtocolType(void) const
415 {
416         ClearLastResult();
417
418         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
419
420         return __protocolType;
421 }
422
423 result
424 _NetAccountInfoImpl::SetProtocolType(NetProtocolType netProtocolType)
425 {
426         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
427
428         __protocolType = netProtocolType;
429
430         return E_SUCCESS;
431 }
432
433 String
434 _NetAccountInfoImpl::GetAccessPointName(void) const
435 {
436         ClearLastResult();
437
438         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
439
440         return __accessPointName;
441 }
442
443 result
444 _NetAccountInfoImpl::SetAccessPointName(const String& accessPointName)
445 {
446         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
447
448         __accessPointName = accessPointName;
449
450         if (__accessPointName.GetLength() > _MAX_ACCESS_POINT_NAME_LENGTH)
451         {
452                 SysLog(NID_NET, "AccessPointName's length[%d] is over [%d], so truncate it.",
453                                 __accessPointName.GetLength(), _MAX_ACCESS_POINT_NAME_LENGTH);
454
455                 __accessPointName.SetLength(_MAX_ACCESS_POINT_NAME_LENGTH);
456         }
457
458         return E_SUCCESS;
459 }
460
461 NetAddressScheme
462 _NetAccountInfoImpl::GetLocalAddressScheme(void) const
463 {
464         ClearLastResult();
465
466         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
467
468         return __localAddressScheme;
469 }
470
471 const IpAddress*
472 _NetAccountInfoImpl::GetLocalAddress(void) const
473 {
474         ClearLastResult();
475
476         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
477
478         SysTryReturn(NID_NET, __localAddressScheme == NET_ADDRESS_SCHEME_STATIC, null, E_INVALID_OPERATION,
479                         "[%s] The operation is not allowed on dynamic address scheme instance.", GetErrorMessage(E_INVALID_OPERATION));
480
481         return __pLocalAddress.get();
482 }
483
484 result
485 _NetAccountInfoImpl::SetLocalAddress(NetAddressScheme localAddrScheme, const IpAddress* pLocalAddress)
486 {
487         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
488
489         std::unique_ptr<IpAddress> pTempAddress;
490
491         if (localAddrScheme == NET_ADDRESS_SCHEME_STATIC)
492         {
493                 SysLog(NID_NET, "Local address scheme is static.");
494
495                 SysTryReturnResult(NID_NET, pLocalAddress != null, E_INVALID_ARG,
496                                 "Invalid argument is used. Address scheme is static but local address is null.");
497
498                 pTempAddress.reset(pLocalAddress->CloneN());
499                 SysTryReturnResult(NID_NET, pTempAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
500         }
501         else if (localAddrScheme == NET_ADDRESS_SCHEME_DYNAMIC)
502         {
503                 SysLog(NID_NET, "Local address scheme is dynamic.");
504         }
505         else
506         {
507                 SysLogException(NID_NET, E_INVALID_ARG, "[%s] Invalid argument is used. The address scheme is invalid.", GetErrorMessage(E_INVALID_ARG));
508
509                 return E_INVALID_ARG;
510         }
511
512         __localAddressScheme = localAddrScheme;
513         __pLocalAddress.reset(pTempAddress.release());
514
515         return E_SUCCESS;
516 }
517
518 NetAddressScheme
519 _NetAccountInfoImpl::GetDnsAddressScheme(void) const
520 {
521         ClearLastResult();
522
523         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
524
525         return __dnsAddressScheme;
526 }
527
528 const IpAddress*
529 _NetAccountInfoImpl::GetPrimaryDnsAddress(void) const
530 {
531         ClearLastResult();
532
533         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
534
535         SysTryReturn(NID_NET, __dnsAddressScheme == NET_ADDRESS_SCHEME_STATIC, null, E_INVALID_OPERATION,
536                         "[%s] The operation is not allowed on dynamic address scheme instance.", GetErrorMessage(E_INVALID_OPERATION));
537
538         return __pPrimaryDnsAddress.get();
539 }
540
541 const IpAddress*
542 _NetAccountInfoImpl::GetSecondaryDnsAddress(void) const
543 {
544         ClearLastResult();
545
546         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
547
548         SysTryReturn(NID_NET, __dnsAddressScheme == NET_ADDRESS_SCHEME_STATIC, null, E_INVALID_OPERATION,
549                         "[%s] The operation is not allowed on dynamic address scheme instance.", GetErrorMessage(E_INVALID_OPERATION));
550
551         return __pSecondaryDnsAddress.get();
552 }
553
554 result
555 _NetAccountInfoImpl::SetDnsAddress(NetAddressScheme dnsAddressScheme, const IpAddress* pPrimaryDnsAddress,
556                                                                    const IpAddress* pSecondaryDnsAddress)
557 {
558         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
559
560         std::unique_ptr<IpAddress> pTempPrimaryDnsAddress;
561         std::unique_ptr<IpAddress> pTempSecondaryDnsAddress;
562
563         if (dnsAddressScheme == NET_ADDRESS_SCHEME_STATIC)
564         {
565                 SysLog(NID_NET, "DNS address scheme is static.");
566
567                 SysTryReturnResult(NID_NET, pPrimaryDnsAddress != null, E_INVALID_ARG,
568                                 "Invalid argument is used. Address scheme is static but primary dns address is null.");
569
570                 pTempPrimaryDnsAddress.reset(pPrimaryDnsAddress->CloneN());
571                 SysTryReturnResult(NID_NET, pTempPrimaryDnsAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
572
573                 if (pSecondaryDnsAddress != null)
574                 {
575                         pTempSecondaryDnsAddress.reset(pSecondaryDnsAddress->CloneN());
576                         SysTryReturnResult(NID_NET, pTempSecondaryDnsAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
577                 }
578         }
579         else if (dnsAddressScheme == NET_ADDRESS_SCHEME_DYNAMIC)
580         {
581                 SysLog(NID_NET, "DNS address scheme is dynamic.");
582         }
583         else
584         {
585                 SysLogException(NID_NET, E_INVALID_ARG, "[%s] Invalid argument is used. The address scheme is invalid.", GetErrorMessage(E_INVALID_ARG));
586
587                 return E_INVALID_ARG;
588         }
589
590         __dnsAddressScheme = dnsAddressScheme;
591         __pPrimaryDnsAddress.reset(pTempPrimaryDnsAddress.release());
592         __pSecondaryDnsAddress.reset(pTempSecondaryDnsAddress.release());
593
594         return E_SUCCESS;
595 }
596
597 const NetEndPoint*
598 _NetAccountInfoImpl::GetProxyAddress(void) const
599 {
600         ClearLastResult();
601
602         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
603
604         return __pProxyAddress.get();
605 }
606
607 result
608 _NetAccountInfoImpl::SetProxyAddress(const NetEndPoint* pProxyEndPoint)
609 {
610         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
611
612         std::unique_ptr<NetEndPoint> pProxyAddress;
613
614         if (pProxyEndPoint != null)
615         {
616                 pProxyAddress.reset(new (std::nothrow) NetEndPoint(*pProxyEndPoint));
617                 SysTryReturnResult(NID_NET, pProxyAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
618         }
619
620         __pProxyAddress.reset(pProxyAddress.release());
621
622         return E_SUCCESS;
623 }
624
625 result
626 _NetAccountInfoImpl::GetAuthenticationInfo(NetNapAuthType& authenticationType, String& id, String& password) const
627 {
628         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
629
630         authenticationType = __authType;
631         id = __authId;
632         password = __authPassword;
633
634         return E_SUCCESS;
635 }
636
637 result
638 _NetAccountInfoImpl::SetAuthenticationInfo(NetNapAuthType authenticationType, const String& id, const String& password)
639 {
640         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
641
642         __authType = authenticationType;
643         __authId = id;
644         __authPassword = password;
645
646         return E_SUCCESS;
647 }
648
649 NetBearerType
650 _NetAccountInfoImpl::GetBearerType(void) const
651 {
652         ClearLastResult();
653
654         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
655
656         return __bearerType;
657 }
658
659 result
660 _NetAccountInfoImpl::SetBearerType(NetBearerType bearerType)
661 {
662         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
663
664         __bearerType = bearerType;
665
666         return E_SUCCESS;
667 }
668
669 String
670 _NetAccountInfoImpl::GetHomeUrl(void) const
671 {
672         return __homeUrl;
673 }
674
675 void
676 _NetAccountInfoImpl::SetHomeUrl(const String& homeUrl)
677 {
678         __homeUrl = homeUrl;
679 }
680
681 int
682 _NetAccountInfoImpl::GetMaximumLengthOfId(void) const
683 {
684         return _MAX_AUTH_ID_LENGTH;
685 }
686
687 int
688 _NetAccountInfoImpl::GetMaximumLengthOfPassword(void) const
689 {
690         return _MAX_AUTH_PASSWORD_LENGTH;
691 }
692
693 int
694 _NetAccountInfoImpl::GetMaximumLengthOfAccountName(void) const
695 {
696         return _MAX_ACCOUNT_NAME_LENGTH;
697 }
698
699 bool
700 _NetAccountInfoImpl::IsReadOnly(void) const
701 {
702         ClearLastResult();
703
704         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
705
706         return __isReadOnly;
707 }
708
709 result
710 _NetAccountInfoImpl::ConvertToProfileInfo(void* pProfileHandle) const
711 {
712         result r = E_SUCCESS;
713         connection_profile_h profileHandle = static_cast<connection_profile_h>(pProfileHandle);
714         int ret = CONNECTION_ERROR_NONE;
715         connection_cellular_auth_type_e authType = CONNECTION_CELLULAR_AUTH_TYPE_NONE;
716         unique_ptr<char[]> pApn;
717         unique_ptr<char[]> pAuthId;
718         unique_ptr<char[]> pAuthPassword;
719         unique_ptr<char[]> pHomeUrl;
720         unique_ptr<char[]> pLocalAddr;
721         unique_ptr<char[]> pPrimaryDnsAddr;
722         unique_ptr<char[]> pSecondaryDnsAddr;
723         unique_ptr<char[]> pProxyAddr;
724         String proxyAddr;
725
726         SysTryReturnResult(NID_NET, pProfileHandle != null, E_INVALID_ARG, "Profile handle is null.");
727         SysTryReturnResult(NID_NET, ((__bearerType == NET_BEARER_PS) || (__bearerType == NET_BEARER_MMS)), E_INVALID_ARG,
728                         "Invalid argument is used. BearerType is NOT PS. bearerType=%d", __bearerType);
729
730         ret = connection_profile_set_cellular_service_type(profileHandle, CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION);
731         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
732                         "A system error has been occurred. The return value from connection_profile_set_cellular_service_type() is %d", ret);
733
734         if (!__accessPointName.IsEmpty())
735         {
736                 pApn.reset(_StringConverter::CopyToCharArrayN(__accessPointName));
737                 SysTryReturnResult(NID_NET, pApn != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
738
739                 ret = connection_profile_set_cellular_apn(profileHandle, pApn.get());
740                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
741                                 "A system error has been occurred. The return value from connection_profile_set_cellular_apn() is %d", ret);
742         }
743
744         if (__authType == NET_NAPAUTH_PAP)
745         {
746                 authType = CONNECTION_CELLULAR_AUTH_TYPE_PAP;
747         }
748         else if (__authType == NET_NAPAUTH_CHAP)
749         {
750                 authType = CONNECTION_CELLULAR_AUTH_TYPE_CHAP;
751         }
752         else
753         {
754                 authType = CONNECTION_CELLULAR_AUTH_TYPE_NONE;
755         }
756
757         if (!__authId.IsEmpty())
758         {
759                 pAuthId.reset(_StringConverter::CopyToCharArrayN(__authId));
760                 SysTryReturnResult(NID_NET, pAuthId != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
761         }
762
763         if (!__authPassword.IsEmpty())
764         {
765                 pAuthPassword.reset(_StringConverter::CopyToCharArrayN(__authPassword));
766                 SysTryReturnResult(NID_NET, pAuthPassword != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
767         }
768
769         if ((pAuthId != null) && (pAuthPassword != null))
770         {
771                 ret = connection_profile_set_cellular_auth_info(profileHandle, authType, pAuthId.get(), pAuthPassword.get());
772                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
773                                 "A system error has been occurred. The return value from connection_profile_set_cellular_auth_info() is %d", ret);
774         }
775
776         if (!__homeUrl.IsEmpty())
777         {
778                 pHomeUrl.reset(_StringConverter::CopyToCharArrayN(__homeUrl));
779                 SysTryReturnResult(NID_NET, pHomeUrl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
780
781                 ret = connection_profile_set_cellular_home_url(profileHandle, pHomeUrl.get());
782                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
783                                 "A system error has been occurred. The return value from connection_profile_set_cellular_home_url() is %d", ret);
784         }
785
786         if (__localAddressScheme == NET_ADDRESS_SCHEME_STATIC)
787         {
788                 SysTryReturnResult(NID_NET, __pLocalAddress != null, E_INVALID_ARG, "Invalid argument is used. Local Addr Scheme is static, but local addr is null.");
789
790                 ret = connection_profile_set_ip_config_type(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, CONNECTION_IP_CONFIG_TYPE_STATIC);
791                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
792                                 "A system error has been occurred. The return value from connection_profile_set_ip_config_type() is %d", ret);
793
794                 pLocalAddr.reset(_StringConverter::CopyToCharArrayN(__pLocalAddress->ToString()));
795                 SysTryReturnResult(NID_NET, pLocalAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
796
797                 ret = connection_profile_set_ip_address(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, pLocalAddr.get());
798                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
799                                 "A system error has been occurred. The return value from connection_profile_set_ip_address() is %d", ret);
800         }
801         else
802         {
803                 ret = connection_profile_set_ip_config_type(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, CONNECTION_IP_CONFIG_TYPE_DYNAMIC);
804                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
805                                 "A system error has been occurred. The return value from connection_profile_set_ip_config_type() is %d", ret);
806         }
807
808         if (__pPrimaryDnsAddress != null)
809         {
810                 pPrimaryDnsAddr.reset(_StringConverter::CopyToCharArrayN(__pPrimaryDnsAddress->ToString()));
811                 SysTryReturnResult(NID_NET, pPrimaryDnsAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
812
813                 ret = connection_profile_set_dns_address(profileHandle, 1, CONNECTION_ADDRESS_FAMILY_IPV4, pPrimaryDnsAddr.get());
814                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
815                                 "A system error has been occurred. The return value from connection_profile_set_dns_address() is %d", ret);
816         }
817
818         if (__pSecondaryDnsAddress != null)
819         {
820                 pSecondaryDnsAddr.reset(_StringConverter::CopyToCharArrayN(__pSecondaryDnsAddress->ToString()));
821                 SysTryReturnResult(NID_NET, pSecondaryDnsAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
822
823                 ret = connection_profile_set_dns_address(profileHandle, 2, CONNECTION_ADDRESS_FAMILY_IPV4, pSecondaryDnsAddr.get());
824                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
825                                 "A system error has been occurred. The return value from connection_profile_set_dns_address() is %d", ret);
826         }
827
828         if (__pProxyAddress != null)
829         {
830                 proxyAddr = __pProxyAddress->GetNetEndPoint();
831                 if (!proxyAddr.IsEmpty())
832                 {
833                         ret = connection_profile_set_proxy_type(profileHandle, CONNECTION_PROXY_TYPE_MANUAL);
834                         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
835                                         "A system error has been occurred. The return value from connection_profile_set_proxy_type() is %d", ret);
836
837                         pProxyAddr.reset(_StringConverter::CopyToCharArrayN(proxyAddr));
838                         SysTryReturnResult(NID_NET, pProxyAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
839
840                         ret = connection_profile_set_proxy_address(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, pProxyAddr.get());
841                         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
842                                         "A system error has been occurred. The return value from connection_profile_set_proxy_address() is %d", ret);
843                 }
844         }
845
846         return r;
847 }
848
849 bool
850 _NetAccountInfoImpl::Equals(const Object& obj) const
851 {
852         const _NetAccountInfoImpl* pRhs = dynamic_cast <const _NetAccountInfoImpl*>(&obj);
853
854         if (pRhs == null)
855         {
856                 return false;
857         }
858
859         if (__accountId != pRhs->__accountId ||
860                 __accountName != pRhs->__accountName ||
861                 __accessPointName != pRhs->__accessPointName ||
862                 __authType != pRhs->__authType ||
863                 __authId != pRhs->__authId ||
864                 __authPassword != pRhs->__authPassword ||
865                 __homeUrl != pRhs->__homeUrl ||
866                 __bearerType != pRhs->__bearerType ||
867                 __protocolType != pRhs->__protocolType ||
868                 __localAddressScheme != pRhs->__localAddressScheme ||
869                 __dnsAddressScheme != pRhs->__dnsAddressScheme ||
870                 __isReadOnly != pRhs->__isReadOnly )
871         {
872                 return false;
873         }
874
875         if (__pLocalAddress != null && pRhs->__pLocalAddress != null)
876         {
877                 if (!__pLocalAddress->Equals(*pRhs->__pLocalAddress))
878                 {
879                         return false;
880                 }
881         }
882         else if (__pLocalAddress != null || pRhs->__pLocalAddress != null)
883         {
884                 return false;
885         }
886
887         if (__pPrimaryDnsAddress != null && pRhs->__pPrimaryDnsAddress != null)
888         {
889                 if (!__pPrimaryDnsAddress->Equals(*pRhs->__pPrimaryDnsAddress))
890                 {
891                         return false;
892                 }
893         }
894         else if (__pPrimaryDnsAddress != null || pRhs->__pPrimaryDnsAddress != null)
895         {
896                 return false;
897         }
898
899         if (__pSecondaryDnsAddress != null && pRhs->__pSecondaryDnsAddress != null)
900         {
901                 if (!__pSecondaryDnsAddress->Equals(*pRhs->__pSecondaryDnsAddress))
902                 {
903                         return false;
904                 }
905         }
906         else if (__pSecondaryDnsAddress != null || pRhs->__pSecondaryDnsAddress != null)
907         {
908                 return false;
909         }
910
911         if (__pProxyAddress != null && pRhs->__pProxyAddress != null)
912         {
913                 if (!__pProxyAddress->Equals(*pRhs->__pProxyAddress))
914                 {
915                         return false;
916                 }
917         }
918         else if (__pProxyAddress != null || pRhs->__pProxyAddress != null)
919         {
920                 return false;
921         }
922
923         return true;
924 }
925
926 int
927 _NetAccountInfoImpl::GetHashCode(void) const
928 {
929         int hashCode = _HASH_CODE_INITIAL_VALUE;
930
931         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __accountId;
932         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __accountName.GetHashCode();
933         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __accessPointName.GetHashCode();
934         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __authType;
935         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __authId.GetHashCode();
936         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __authPassword.GetHashCode();
937         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __homeUrl.GetHashCode();
938         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __bearerType;
939         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __protocolType;
940         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __localAddressScheme;
941         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __dnsAddressScheme;
942
943         if (__pLocalAddress != null)
944         {
945                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pLocalAddress->GetHashCode();
946         }
947         else
948         {
949                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
950         }
951
952         if (__pPrimaryDnsAddress != null)
953         {
954                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pPrimaryDnsAddress->GetHashCode();
955         }
956         else
957         {
958                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
959         }
960
961         if (__pSecondaryDnsAddress != null)
962         {
963                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pSecondaryDnsAddress->GetHashCode();
964         }
965         else
966         {
967                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
968         }
969
970         if (__pProxyAddress != null)
971         {
972                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pProxyAddress->GetHashCode();
973         }
974         else
975         {
976                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
977         }
978
979         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isReadOnly ? 0 : 1);
980
981         return hashCode;
982 }
983
984 NetAccountInfo*
985 _NetAccountInfoImpl::CreateNetAccountInfoN(void* pProfileHandle)
986 {
987         result r = E_SUCCESS;
988
989         std::unique_ptr<NetAccountInfo> pNetAccountInfo(new (std::nothrow) NetAccountInfo());
990         SysTryReturn(NID_NET, pNetAccountInfo != null, null, E_OUT_OF_MEMORY,
991                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
992
993         unique_ptr<_NetAccountInfoImpl> pNetAccountInfoImpl(new (std::nothrow) _NetAccountInfoImpl());
994         SysTryReturn(NID_NET, pNetAccountInfoImpl != null, null, E_OUT_OF_MEMORY,
995                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
996
997         r = pNetAccountInfoImpl->Construct(pNetAccountInfo.get(), pProfileHandle);
998         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
999
1000         pNetAccountInfoImpl.release();
1001
1002         return pNetAccountInfo.release();
1003 }
1004
1005 _NetAccountInfoImpl*
1006 _NetAccountInfoImpl::GetInstance(NetAccountInfo& netAccountInfo)
1007 {
1008         return netAccountInfo.__pNetAccountInfoImpl;
1009 }
1010
1011 const _NetAccountInfoImpl*
1012 _NetAccountInfoImpl::GetInstance(const NetAccountInfo& netAccountInfo)
1013 {
1014         return netAccountInfo.__pNetAccountInfoImpl;
1015 }
1016
1017 } } // Tizen::Net