change version to 1.2.2.0
[platform/framework/native/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 is over [%d], so truncate it.", _MAX_ACCOUNT_NAME_LENGTH);
405
406                 __accountName.SetLength(_MAX_ACCOUNT_NAME_LENGTH);
407         }
408
409         return E_SUCCESS;
410 }
411
412 NetProtocolType
413 _NetAccountInfoImpl::GetProtocolType(void) const
414 {
415         ClearLastResult();
416
417         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
418
419         return __protocolType;
420 }
421
422 result
423 _NetAccountInfoImpl::SetProtocolType(NetProtocolType netProtocolType)
424 {
425         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
426
427         __protocolType = netProtocolType;
428
429         return E_SUCCESS;
430 }
431
432 String
433 _NetAccountInfoImpl::GetAccessPointName(void) const
434 {
435         ClearLastResult();
436
437         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
438
439         return __accessPointName;
440 }
441
442 result
443 _NetAccountInfoImpl::SetAccessPointName(const String& accessPointName)
444 {
445         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
446
447         __accessPointName = accessPointName;
448
449         if (__accessPointName.GetLength() > _MAX_ACCESS_POINT_NAME_LENGTH)
450         {
451                 SysLog(NID_NET, "AccessPointName's length[%d] is over [%d], so truncate it.",
452                                 __accessPointName.GetLength(), _MAX_ACCESS_POINT_NAME_LENGTH);
453
454                 __accessPointName.SetLength(_MAX_ACCESS_POINT_NAME_LENGTH);
455         }
456
457         return E_SUCCESS;
458 }
459
460 NetAddressScheme
461 _NetAccountInfoImpl::GetLocalAddressScheme(void) const
462 {
463         ClearLastResult();
464
465         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
466
467         return __localAddressScheme;
468 }
469
470 const IpAddress*
471 _NetAccountInfoImpl::GetLocalAddress(void) const
472 {
473         ClearLastResult();
474
475         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
476
477         SysTryReturn(NID_NET, __localAddressScheme == NET_ADDRESS_SCHEME_STATIC, null, E_INVALID_OPERATION,
478                         "[%s] The operation is not allowed on dynamic address scheme instance.", GetErrorMessage(E_INVALID_OPERATION));
479
480         return __pLocalAddress.get();
481 }
482
483 result
484 _NetAccountInfoImpl::SetLocalAddress(NetAddressScheme localAddrScheme, const IpAddress* pLocalAddress)
485 {
486         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
487
488         std::unique_ptr<IpAddress> pTempAddress;
489
490         if (localAddrScheme == NET_ADDRESS_SCHEME_STATIC)
491         {
492                 SysLog(NID_NET, "Local address scheme is static.");
493
494                 SysTryReturnResult(NID_NET, pLocalAddress != null, E_INVALID_ARG,
495                                 "Invalid argument is used. Address scheme is static but local address is null.");
496
497                 pTempAddress.reset(pLocalAddress->CloneN());
498                 SysTryReturnResult(NID_NET, pTempAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
499         }
500         else if (localAddrScheme == NET_ADDRESS_SCHEME_DYNAMIC)
501         {
502                 SysLog(NID_NET, "Local address scheme is dynamic.");
503         }
504         else
505         {
506                 SysLogException(NID_NET, E_INVALID_ARG, "[%s] Invalid argument is used. The address scheme is invalid.", GetErrorMessage(E_INVALID_ARG));
507
508                 return E_INVALID_ARG;
509         }
510
511         __localAddressScheme = localAddrScheme;
512         __pLocalAddress.reset(pTempAddress.release());
513
514         return E_SUCCESS;
515 }
516
517 NetAddressScheme
518 _NetAccountInfoImpl::GetDnsAddressScheme(void) const
519 {
520         ClearLastResult();
521
522         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
523
524         return __dnsAddressScheme;
525 }
526
527 const IpAddress*
528 _NetAccountInfoImpl::GetPrimaryDnsAddress(void) const
529 {
530         ClearLastResult();
531
532         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
533
534         SysTryReturn(NID_NET, __dnsAddressScheme == NET_ADDRESS_SCHEME_STATIC, null, E_INVALID_OPERATION,
535                         "[%s] The operation is not allowed on dynamic address scheme instance.", GetErrorMessage(E_INVALID_OPERATION));
536
537         return __pPrimaryDnsAddress.get();
538 }
539
540 const IpAddress*
541 _NetAccountInfoImpl::GetSecondaryDnsAddress(void) const
542 {
543         ClearLastResult();
544
545         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
546
547         SysTryReturn(NID_NET, __dnsAddressScheme == NET_ADDRESS_SCHEME_STATIC, null, E_INVALID_OPERATION,
548                         "[%s] The operation is not allowed on dynamic address scheme instance.", GetErrorMessage(E_INVALID_OPERATION));
549
550         return __pSecondaryDnsAddress.get();
551 }
552
553 result
554 _NetAccountInfoImpl::SetDnsAddress(NetAddressScheme dnsAddressScheme, const IpAddress* pPrimaryDnsAddress,
555                                                                    const IpAddress* pSecondaryDnsAddress)
556 {
557         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
558
559         std::unique_ptr<IpAddress> pTempPrimaryDnsAddress;
560         std::unique_ptr<IpAddress> pTempSecondaryDnsAddress;
561
562         if (dnsAddressScheme == NET_ADDRESS_SCHEME_STATIC)
563         {
564                 SysLog(NID_NET, "DNS address scheme is static.");
565
566                 SysTryReturnResult(NID_NET, pPrimaryDnsAddress != null, E_INVALID_ARG,
567                                 "Invalid argument is used. Address scheme is static but primary dns address is null.");
568
569                 pTempPrimaryDnsAddress.reset(pPrimaryDnsAddress->CloneN());
570                 SysTryReturnResult(NID_NET, pTempPrimaryDnsAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
571
572                 if (pSecondaryDnsAddress != null)
573                 {
574                         pTempSecondaryDnsAddress.reset(pSecondaryDnsAddress->CloneN());
575                         SysTryReturnResult(NID_NET, pTempSecondaryDnsAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
576                 }
577         }
578         else if (dnsAddressScheme == NET_ADDRESS_SCHEME_DYNAMIC)
579         {
580                 SysLog(NID_NET, "DNS address scheme is dynamic.");
581         }
582         else
583         {
584                 SysLogException(NID_NET, E_INVALID_ARG, "[%s] Invalid argument is used. The address scheme is invalid.", GetErrorMessage(E_INVALID_ARG));
585
586                 return E_INVALID_ARG;
587         }
588
589         __dnsAddressScheme = dnsAddressScheme;
590         __pPrimaryDnsAddress.reset(pTempPrimaryDnsAddress.release());
591         __pSecondaryDnsAddress.reset(pTempSecondaryDnsAddress.release());
592
593         return E_SUCCESS;
594 }
595
596 const NetEndPoint*
597 _NetAccountInfoImpl::GetProxyAddress(void) const
598 {
599         ClearLastResult();
600
601         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
602
603         return __pProxyAddress.get();
604 }
605
606 result
607 _NetAccountInfoImpl::SetProxyAddress(const NetEndPoint* pProxyEndPoint)
608 {
609         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
610
611         std::unique_ptr<NetEndPoint> pProxyAddress;
612
613         if (pProxyEndPoint != null)
614         {
615                 pProxyAddress.reset(new (std::nothrow) NetEndPoint(*pProxyEndPoint));
616                 SysTryReturnResult(NID_NET, pProxyAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
617         }
618
619         __pProxyAddress.reset(pProxyAddress.release());
620
621         return E_SUCCESS;
622 }
623
624 result
625 _NetAccountInfoImpl::GetAuthenticationInfo(NetNapAuthType& authenticationType, String& id, String& password) const
626 {
627         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
628
629         authenticationType = __authType;
630         id = __authId;
631         password = __authPassword;
632
633         return E_SUCCESS;
634 }
635
636 result
637 _NetAccountInfoImpl::SetAuthenticationInfo(NetNapAuthType authenticationType, const String& id, const String& password)
638 {
639         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
640
641         __authType = authenticationType;
642         __authId = id;
643         __authPassword = password;
644
645         return E_SUCCESS;
646 }
647
648 NetBearerType
649 _NetAccountInfoImpl::GetBearerType(void) const
650 {
651         ClearLastResult();
652
653         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
654
655         return __bearerType;
656 }
657
658 result
659 _NetAccountInfoImpl::SetBearerType(NetBearerType bearerType)
660 {
661         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
662
663         __bearerType = bearerType;
664
665         return E_SUCCESS;
666 }
667
668 String
669 _NetAccountInfoImpl::GetHomeUrl(void) const
670 {
671         return __homeUrl;
672 }
673
674 void
675 _NetAccountInfoImpl::SetHomeUrl(const String& homeUrl)
676 {
677         __homeUrl = homeUrl;
678 }
679
680 int
681 _NetAccountInfoImpl::GetMaximumLengthOfId(void) const
682 {
683         return _MAX_AUTH_ID_LENGTH;
684 }
685
686 int
687 _NetAccountInfoImpl::GetMaximumLengthOfPassword(void) const
688 {
689         return _MAX_AUTH_PASSWORD_LENGTH;
690 }
691
692 int
693 _NetAccountInfoImpl::GetMaximumLengthOfAccountName(void) const
694 {
695         return _MAX_ACCOUNT_NAME_LENGTH;
696 }
697
698 bool
699 _NetAccountInfoImpl::IsReadOnly(void) const
700 {
701         ClearLastResult();
702
703         SysAssertf(__bearerType != NET_BEARER_NONE, "Not yet constructed. Construct() should be called before use.");
704
705         return __isReadOnly;
706 }
707
708 result
709 _NetAccountInfoImpl::ConvertToProfileInfo(void* pProfileHandle) const
710 {
711         result r = E_SUCCESS;
712         connection_profile_h profileHandle = static_cast<connection_profile_h>(pProfileHandle);
713         int ret = CONNECTION_ERROR_NONE;
714         connection_cellular_auth_type_e authType = CONNECTION_CELLULAR_AUTH_TYPE_NONE;
715         connection_ip_config_type_e ipConfigType = CONNECTION_IP_CONFIG_TYPE_NONE;
716
717         unique_ptr<char[]> pApn;
718         unique_ptr<char[]> pAuthId;
719         unique_ptr<char[]> pAuthPassword;
720         unique_ptr<char[]> pHomeUrl;
721         unique_ptr<char[]> pLocalAddr;
722         unique_ptr<char[]> pPrimaryDnsAddr;
723         unique_ptr<char[]> pSecondaryDnsAddr;
724         unique_ptr<char[]> pProxyAddr;
725         String proxyAddr;
726
727         SysTryReturnResult(NID_NET, pProfileHandle != null, E_INVALID_ARG, "Profile handle is null.");
728         SysTryReturnResult(NID_NET, ((__bearerType == NET_BEARER_PS) || (__bearerType == NET_BEARER_MMS)), E_INVALID_ARG,
729                         "Invalid argument is used. BearerType is NOT PS. bearerType=%d", __bearerType);
730
731         ret = connection_profile_set_cellular_service_type(profileHandle, CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION);
732         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
733                         "A system error has been occurred. The return value from connection_profile_set_cellular_service_type() is %d", ret);
734
735         if (!__accessPointName.IsEmpty())
736         {
737                 pApn.reset(_StringConverter::CopyToCharArrayN(__accessPointName));
738                 SysTryReturnResult(NID_NET, pApn != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
739
740                 ret = connection_profile_set_cellular_apn(profileHandle, pApn.get());
741                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
742                                 "A system error has been occurred. The return value from connection_profile_set_cellular_apn() is %d", ret);
743         }
744
745         if (__authType == NET_NAPAUTH_PAP)
746         {
747                 authType = CONNECTION_CELLULAR_AUTH_TYPE_PAP;
748         }
749         else if (__authType == NET_NAPAUTH_CHAP)
750         {
751                 authType = CONNECTION_CELLULAR_AUTH_TYPE_CHAP;
752         }
753         else
754         {
755                 authType = CONNECTION_CELLULAR_AUTH_TYPE_NONE;
756         }
757
758         if (!__authId.IsEmpty())
759         {
760                 pAuthId.reset(_StringConverter::CopyToCharArrayN(__authId));
761                 SysTryReturnResult(NID_NET, pAuthId != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
762         }
763
764         if (!__authPassword.IsEmpty())
765         {
766                 pAuthPassword.reset(_StringConverter::CopyToCharArrayN(__authPassword));
767                 SysTryReturnResult(NID_NET, pAuthPassword != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
768         }
769
770         if ((pAuthId != null) && (pAuthPassword != null))
771         {
772                 ret = connection_profile_set_cellular_auth_info(profileHandle, authType, pAuthId.get(), pAuthPassword.get());
773         }
774         else
775         {
776                 ret = connection_profile_set_cellular_auth_info(profileHandle, authType, " ", " ");
777         }
778         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
779                         "A system error has been occurred. The return value from connection_profile_set_cellular_auth_info() is %d", ret);
780
781         if (!__homeUrl.IsEmpty())
782         {
783                 pHomeUrl.reset(_StringConverter::CopyToCharArrayN(__homeUrl));
784                 SysTryReturnResult(NID_NET, pHomeUrl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
785
786                 ret = connection_profile_set_cellular_home_url(profileHandle, pHomeUrl.get());
787         }
788         else
789         {
790                 ret = connection_profile_set_cellular_home_url(profileHandle, " ");
791         }
792         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
793                         "A system error has been occurred. The return value from connection_profile_set_cellular_home_url() is %d", ret);
794
795         ret = connection_profile_get_ip_config_type(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, &ipConfigType);
796
797         if (ipConfigType != CONNECTION_IP_CONFIG_TYPE_FIXED)
798         {
799                 if (__localAddressScheme == NET_ADDRESS_SCHEME_STATIC)
800                 {
801                         SysTryReturnResult(NID_NET, __pLocalAddress != null, E_INVALID_ARG, "Invalid argument is used. Local Addr Scheme is static, but local addr is null.");
802
803                         ret = connection_profile_set_ip_config_type(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, CONNECTION_IP_CONFIG_TYPE_STATIC);
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                         pLocalAddr.reset(_StringConverter::CopyToCharArrayN(__pLocalAddress->ToString()));
808                         SysTryReturnResult(NID_NET, pLocalAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
809
810                         ret = connection_profile_set_ip_address(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, pLocalAddr.get());
811                         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
812                                         "A system error has been occurred. The return value from connection_profile_set_ip_address() is %d", ret);
813                 }
814                 else
815                 {
816                         ret = connection_profile_set_ip_config_type(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, CONNECTION_IP_CONFIG_TYPE_DYNAMIC);
817                         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
818                                         "A system error has been occurred. The return value from connection_profile_set_ip_config_type() is %d", ret);
819                 }
820
821                 if (__pPrimaryDnsAddress != null)
822                 {
823                         pPrimaryDnsAddr.reset(_StringConverter::CopyToCharArrayN(__pPrimaryDnsAddress->ToString()));
824                         SysTryReturnResult(NID_NET, pPrimaryDnsAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
825
826                         ret = connection_profile_set_dns_address(profileHandle, 1, CONNECTION_ADDRESS_FAMILY_IPV4, pPrimaryDnsAddr.get());
827                         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
828                                         "A system error has been occurred. The return value from connection_profile_set_dns_address() is %d", ret);
829                 }
830
831                 if (__pSecondaryDnsAddress != null)
832                 {
833                         pSecondaryDnsAddr.reset(_StringConverter::CopyToCharArrayN(__pSecondaryDnsAddress->ToString()));
834                         SysTryReturnResult(NID_NET, pSecondaryDnsAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
835
836                         ret = connection_profile_set_dns_address(profileHandle, 2, CONNECTION_ADDRESS_FAMILY_IPV4, pSecondaryDnsAddr.get());
837                         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
838                                         "A system error has been occurred. The return value from connection_profile_set_dns_address() is %d", ret);
839                 }
840         }
841
842         if (__pProxyAddress != null)
843         {
844                 proxyAddr = __pProxyAddress->GetNetEndPoint();
845                 if (!proxyAddr.IsEmpty())
846                 {
847                         ret = connection_profile_set_proxy_type(profileHandle, CONNECTION_PROXY_TYPE_MANUAL);
848                         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
849                                         "A system error has been occurred. The return value from connection_profile_set_proxy_type() is %d", ret);
850
851                         pProxyAddr.reset(_StringConverter::CopyToCharArrayN(proxyAddr));
852                         SysTryReturnResult(NID_NET, pProxyAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
853
854                         ret = connection_profile_set_proxy_address(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, pProxyAddr.get());
855                 }
856                 else
857                 {
858                         ret = connection_profile_set_proxy_address(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, null);
859                 }
860         }
861         else
862         {
863                 ret = connection_profile_set_proxy_address(profileHandle, CONNECTION_ADDRESS_FAMILY_IPV4, null);
864         }
865         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
866                         "A system error has been occurred. The return value from connection_profile_set_proxy_address() is %d", ret);
867
868         return r;
869 }
870
871 bool
872 _NetAccountInfoImpl::Equals(const Object& obj) const
873 {
874         const _NetAccountInfoImpl* pRhs = dynamic_cast <const _NetAccountInfoImpl*>(&obj);
875
876         if (pRhs == null)
877         {
878                 return false;
879         }
880
881         if (__accountId != pRhs->__accountId ||
882                 __accountName != pRhs->__accountName ||
883                 __accessPointName != pRhs->__accessPointName ||
884                 __authType != pRhs->__authType ||
885                 __authId != pRhs->__authId ||
886                 __authPassword != pRhs->__authPassword ||
887                 __homeUrl != pRhs->__homeUrl ||
888                 __bearerType != pRhs->__bearerType ||
889                 __protocolType != pRhs->__protocolType ||
890                 __localAddressScheme != pRhs->__localAddressScheme ||
891                 __dnsAddressScheme != pRhs->__dnsAddressScheme ||
892                 __isReadOnly != pRhs->__isReadOnly )
893         {
894                 return false;
895         }
896
897         if (__pLocalAddress != null && pRhs->__pLocalAddress != null)
898         {
899                 if (!__pLocalAddress->Equals(*pRhs->__pLocalAddress))
900                 {
901                         return false;
902                 }
903         }
904         else if (__pLocalAddress != null || pRhs->__pLocalAddress != null)
905         {
906                 return false;
907         }
908
909         if (__pPrimaryDnsAddress != null && pRhs->__pPrimaryDnsAddress != null)
910         {
911                 if (!__pPrimaryDnsAddress->Equals(*pRhs->__pPrimaryDnsAddress))
912                 {
913                         return false;
914                 }
915         }
916         else if (__pPrimaryDnsAddress != null || pRhs->__pPrimaryDnsAddress != null)
917         {
918                 return false;
919         }
920
921         if (__pSecondaryDnsAddress != null && pRhs->__pSecondaryDnsAddress != null)
922         {
923                 if (!__pSecondaryDnsAddress->Equals(*pRhs->__pSecondaryDnsAddress))
924                 {
925                         return false;
926                 }
927         }
928         else if (__pSecondaryDnsAddress != null || pRhs->__pSecondaryDnsAddress != null)
929         {
930                 return false;
931         }
932
933         if (__pProxyAddress != null && pRhs->__pProxyAddress != null)
934         {
935                 if (!__pProxyAddress->Equals(*pRhs->__pProxyAddress))
936                 {
937                         return false;
938                 }
939         }
940         else if (__pProxyAddress != null || pRhs->__pProxyAddress != null)
941         {
942                 return false;
943         }
944
945         return true;
946 }
947
948 int
949 _NetAccountInfoImpl::GetHashCode(void) const
950 {
951         int hashCode = _HASH_CODE_INITIAL_VALUE;
952
953         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __accountId;
954         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __accountName.GetHashCode();
955         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __accessPointName.GetHashCode();
956         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __authType;
957         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __authId.GetHashCode();
958         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __authPassword.GetHashCode();
959         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __homeUrl.GetHashCode();
960         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __bearerType;
961         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __protocolType;
962         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __localAddressScheme;
963         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __dnsAddressScheme;
964
965         if (__pLocalAddress != null)
966         {
967                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pLocalAddress->GetHashCode();
968         }
969         else
970         {
971                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
972         }
973
974         if (__pPrimaryDnsAddress != null)
975         {
976                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pPrimaryDnsAddress->GetHashCode();
977         }
978         else
979         {
980                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
981         }
982
983         if (__pSecondaryDnsAddress != null)
984         {
985                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pSecondaryDnsAddress->GetHashCode();
986         }
987         else
988         {
989                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
990         }
991
992         if (__pProxyAddress != null)
993         {
994                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pProxyAddress->GetHashCode();
995         }
996         else
997         {
998                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
999         }
1000
1001         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isReadOnly ? 0 : 1);
1002
1003         return hashCode;
1004 }
1005
1006 NetAccountInfo*
1007 _NetAccountInfoImpl::CreateNetAccountInfoN(void* pProfileHandle)
1008 {
1009         result r = E_SUCCESS;
1010
1011         std::unique_ptr<NetAccountInfo> pNetAccountInfo(new (std::nothrow) NetAccountInfo());
1012         SysTryReturn(NID_NET, pNetAccountInfo != null, null, E_OUT_OF_MEMORY,
1013                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1014
1015         unique_ptr<_NetAccountInfoImpl> pNetAccountInfoImpl(new (std::nothrow) _NetAccountInfoImpl());
1016         SysTryReturn(NID_NET, pNetAccountInfoImpl != null, null, E_OUT_OF_MEMORY,
1017                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1018
1019         r = pNetAccountInfoImpl->Construct(pNetAccountInfo.get(), pProfileHandle);
1020         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1021
1022         pNetAccountInfoImpl.release();
1023
1024         return pNetAccountInfo.release();
1025 }
1026
1027 _NetAccountInfoImpl*
1028 _NetAccountInfoImpl::GetInstance(NetAccountInfo& netAccountInfo)
1029 {
1030         return netAccountInfo.__pNetAccountInfoImpl;
1031 }
1032
1033 const _NetAccountInfoImpl*
1034 _NetAccountInfoImpl::GetInstance(const NetAccountInfo& netAccountInfo)
1035 {
1036         return netAccountInfo.__pNetAccountInfoImpl;
1037 }
1038
1039 } } // Tizen::Net