Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNet_NetIpcProxy.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 /**
19  * @file    FTel_NetIpcProxy.cpp
20  * @brief   This is the implementation file for the %_NetIpcProxy class.
21  *
22  * This file contains the implementation of the %_NetIpcProxy class.
23  */
24
25
26 #include <pthread.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseString.h>
29 #include <FNetNetTypes.h>
30 #include <FIo_IpcClient.h>
31 #include <FNet_ConnectivityIpcMessages.h>
32 #include "FNet_NetAccountDatabase.h"
33 #include "FNet_NetIpcProxy.h"
34 #include "FNet_NetAccountInfoImpl.h"
35
36 using namespace std;
37 using namespace Tizen::Base;
38 using namespace Tizen::Io;
39
40 namespace Tizen { namespace Net
41 {
42
43 _NetIpcProxy* _NetIpcProxy::__pInstance = null;
44
45 _NetIpcProxy::_NetIpcProxy(void)
46         : __pIpcClient(null)
47 {
48 }
49
50 _NetIpcProxy::~_NetIpcProxy(void)
51 {
52 }
53
54 result
55 _NetIpcProxy::Construct(void)
56 {
57         result r = E_SUCCESS;
58
59         unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient());
60         SysTryReturnResult(NID_NET, pIpcClient != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
61
62         r = pIpcClient->Construct(NET_CONNECTIVITY_IPC_SERVER_NAME, null);
63         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
64
65         __pIpcClient = move(pIpcClient);
66
67         return r;
68 }
69
70 _NetIpcProxy*
71 _NetIpcProxy::GetInstance(void)
72 {
73         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
74
75         if (__pInstance == null)
76         {
77                 ClearLastResult();
78                 pthread_once(&onceBlock, InitSingleton);
79                 result r = GetLastResult();
80                 if (r != E_SUCCESS)
81                 {
82                         onceBlock = PTHREAD_ONCE_INIT;
83                 }
84         }
85
86         return __pInstance;
87 }
88
89 void
90 _NetIpcProxy::InitSingleton(void)
91 {
92     result r = E_SUCCESS;
93     static _NetIpcProxy instance;
94
95     r = instance.Construct();
96         SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
97
98         __pInstance = &instance;
99 }
100
101 result
102 _NetIpcProxy::UpdateSystemNetAccount(const String& profileName, const NetAccountInfo& netAccountInfo, NetBearerType bearerType)
103 {
104         result r = E_SUCCESS;
105         unsigned long ret = 0;
106
107         unique_ptr<IPC::Message> pMessage(new (std::nothrow) ConnectivityNetServiceMsg_updateSystemNetAccount(profileName, netAccountInfo, bearerType, &ret));
108         SysTryReturnResult(NID_NET, pMessage != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
109
110         r = __pIpcClient->SendRequest(*pMessage);
111         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
112         SysTryReturnResult(NID_NET, ret == E_SUCCESS, ret, "Propagating.");
113
114         return r;
115 }
116 result
117 _NetIpcProxy::GetAppNetAccountId(const String& netProfileName, NetAccountId& netAccountId) const
118 {
119         SysLog(NID_NET, "GetAppNetAccountId() has been called with profileName:%ls", netProfileName.GetPointer());
120
121         result r = E_SUCCESS;
122         int accountId = INVALID_HANDLE;
123         unsigned long ret = 0;
124
125         unique_ptr<IPC::Message> pMessage(new (std::nothrow) ConnectivityNetServiceMsg_getAppNetAccountId(netProfileName, &accountId, &ret));
126         SysTryReturnResult(NID_NET, pMessage != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
127
128         r = __pIpcClient->SendRequest(*pMessage);
129         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
130         SysTryReturnResult(NID_NET, ret == E_SUCCESS, ret, "Propagating.");
131         netAccountId = static_cast<NetAccountId>(accountId);
132
133         SysLog(NID_NET, "GetAppNetAccountId() has done with accountId:%d", netAccountId);
134
135         return r;
136 }
137
138 result
139 _NetIpcProxy::SetNetAccountId(NetAccountId netAccountId, NetAccountId& netAccountId2)
140 {
141         SysLog(NID_NET, "SetNetAccountId() has been called with accountId:%d", netAccountId);
142
143         result r = E_SUCCESS;
144         int accountId = INVALID_HANDLE;
145         unsigned long ret = 0;
146
147         unique_ptr<IPC::Message> pMessage(new (std::nothrow) ConnectivityNetServiceMsg_setNetAccountId(netAccountId, &accountId, &ret));
148         SysTryReturnResult(NID_NET, pMessage != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
149
150         r = __pIpcClient->SendRequest(*pMessage);
151         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
152         SysTryReturnResult(NID_NET, ret == E_SUCCESS, ret, "Propagating.");
153
154         netAccountId2 = static_cast<NetAccountId>(accountId);
155
156         SysLog(NID_NET, "SetNetAccountId() has done with accountId:%d", netAccountId2);
157
158         return r;
159 }
160
161 result
162 _NetIpcProxy::ResetNetStatistics(NetBearerType bearerType, NetStatisticsInfoType statType)
163 {
164         result r = E_SUCCESS;
165         unsigned long ret = 0;
166
167         SysLog(NID_NET, "ResetNetStatistics() has been called with bearer:%d stat:%d", bearerType, statType);
168
169         unique_ptr<IPC::Message> pMessage(new (std::nothrow) ConnectivityNetServiceMsg_resetNetStatistics(bearerType, statType, &ret));
170         SysTryReturnResult(NID_NET, pMessage != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
171
172         r = __pIpcClient->SendRequest(*pMessage);
173         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
174         SysTryReturnResult(NID_NET, ret == E_SUCCESS, ret, "Propagating.");
175
176         return r;
177 }
178
179 result
180 _NetIpcProxy::ResetAllNetStatistics(NetBearerType bearerType)
181 {
182         result r = E_SUCCESS;
183         unsigned long ret = 0;
184
185         SysLog(NID_NET, "ResetAllNetStatistics() has been called with bearer:%d", bearerType);
186
187         unique_ptr<IPC::Message> pMessage(new (std::nothrow) ConnectivityNetServiceMsg_resetAllNetStatistics(bearerType, &ret));
188         SysTryReturnResult(NID_NET, pMessage != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
189
190         r = __pIpcClient->SendRequest(*pMessage);
191         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
192         SysTryReturnResult(NID_NET, ret == E_SUCCESS, ret, "Propagating.");
193
194         return r;
195 }
196
197 }} // Tizen::Net