2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <Commons/Exception.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include "OnNetworkBearerSelectionStateChanged.h"
22 #include "NetworkBearerSelection.h"
24 #include <arpa/inet.h>
27 using namespace WrtDeviceApis::CommonsJavaScript;
28 using namespace WrtDeviceApis::Commons;
32 namespace NetworkBearerSelection {
34 static void connection_state_changed_callback(connection_profile_state_e state, void* user_data)
37 if(user_data != NULL) {
38 LoggerD("Callback registration Succeeded");
39 if (state == CONNECTION_PROFILE_STATE_ASSOCIATION) {
40 LoggerD("association state");
44 NewtorkBearerSelectionPendingEvent *pendingEvent = (NewtorkBearerSelectionPendingEvent *)user_data;
45 NetworkBearerSelection *networkBearerSelection = (NetworkBearerSelection *)pendingEvent->getThisObject();
46 EventNetworkBearerSelectionPtr event = pendingEvent->getEvent();
48 if (state == CONNECTION_PROFILE_STATE_DISCONNECTED) {
49 networkBearerSelection->makeRequestCallback(event, CONNECTION_STATE_DISCONNECTED);
53 } else if (state == CONNECTION_PROFILE_STATE_CONNECTED) {
54 networkBearerSelection->makeRequestCallback(event, CONNECTION_STATE_CONNECTED);
61 static void connection_opened_callback(connection_error_e result, void* user_data)
64 if (result == CONNECTION_ERROR_NONE) {
65 LoggerD("Connection open Succeeded");
66 if(user_data != NULL) {
67 NewtorkBearerSelectionPendingEvent *pendingEvent = (NewtorkBearerSelectionPendingEvent *)user_data;
68 NetworkBearerSelection *networkBearerSelection = (NetworkBearerSelection *)pendingEvent->getThisObject();
69 EventNetworkBearerSelectionPtr event = pendingEvent->getEvent();
71 networkBearerSelection->registStateChangeListener(event);
80 static void connection_closed_callback(connection_error_e result, void* user_data)
82 LoggerD("result : " << result);
83 if (result == CONNECTION_ERROR_NONE) {
84 LoggerD("Connection close Succeeded");
85 if (user_data != NULL) {
86 NewtorkBearerReleasePendingEvent *pendingEvent = (NewtorkBearerReleasePendingEvent *)user_data;
87 NetworkBearerSelection *networkBearerSelection = (NetworkBearerSelection *)pendingEvent->getThisObject();
88 EventNetworkBearerReleasePtr event = pendingEvent->getEvent();
90 networkBearerSelection->deregistStateChangeListener(event);
96 } else if (result == CONNECTION_ERROR_OPERATION_FAILED) {
97 LoggerD("Connection close Failed");
98 if (user_data != NULL) {
99 NewtorkBearerReleasePendingEvent *pendingEvent = (NewtorkBearerReleasePendingEvent *)user_data;
100 NetworkBearerSelection *networkBearerSelection = (NetworkBearerSelection *)pendingEvent->getThisObject();
101 EventNetworkBearerReleasePtr event = pendingEvent->getEvent();
103 networkBearerSelection->makeReleaseCallback(event);
112 static void connection_closed_callback2(connection_error_e result, void* user_data)
115 if (result == CONNECTION_ERROR_NONE) {
116 LoggerD("Connection close Succeeded");
121 static void connection_removed_callback(connection_error_e result, void* user_data)
124 if (user_data != NULL) {
125 if (result == CONNECTION_ERROR_NONE) {
126 LoggerD("Connection close Succeeded");
127 NewtorkBearerSelectionPendingEvent *pendingEvent = (NewtorkBearerSelectionPendingEvent *)user_data;
128 NetworkBearerSelection *networkBearerSelection = (NetworkBearerSelection *)pendingEvent->getThisObject();
129 EventNetworkBearerSelectionPtr event = pendingEvent->getEvent();
131 networkBearerSelection->removeStateChangeListener(event);
141 NetworkBearerSelection::NetworkBearerSelection() : m_connectionHandle(NULL),
142 m_profileHandle(NULL),
143 m_connectionState(NETWORK_UNKNOWN)
145 int ret = connection_create(&m_connectionHandle);
147 if (CONNECTION_ERROR_NONE == ret) {
148 LoggerD("Client registration success");
150 LoggerD("Client registration failed");
151 m_connectionHandle = NULL;
155 NetworkBearerSelection::~NetworkBearerSelection()
157 if(m_connectionHandle != NULL) {
158 LoggerD("Client deregistration success");
159 connection_destroy(m_connectionHandle);
161 LoggerD("Client deregistration failed");
165 void NetworkBearerSelection::requestRouteToHost(const EventNetworkBearerSelectionPtr &event)
167 EventRequestReceiver<EventNetworkBearerSelection>::PostRequest(event);
170 void NetworkBearerSelection::releaseRouteToHost(const EventNetworkBearerReleasePtr &event)
172 EventRequestReceiver<EventNetworkBearerRelease>::PostRequest(event);
175 void NetworkBearerSelection::OnRequestReceived(const EventNetworkBearerSelectionPtr &event)
177 LoggerD("m_connectionState : " << m_connectionState);
179 if (m_connectionState == NETWORK_CONNECTED) {
180 reLaunchConnection(event);
184 connection_profile_iterator_h profileIter;
186 int ret = connection_get_profile_iterator(m_connectionHandle, CONNECTION_ITERATOR_TYPE_REGISTERED, &profileIter);
187 if (ret != CONNECTION_ERROR_NONE) {
188 LoggerD("Fail to get profile iterator");
189 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
192 while (connection_profile_iterator_has_next(profileIter)) {
193 if (connection_profile_iterator_next(profileIter, &m_profileHandle) != CONNECTION_ERROR_NONE) {
194 LoggerD("Fail to get profile handle");
195 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
200 NewtorkBearerSelectionPendingEvent *pendingEvent = new NewtorkBearerSelectionPendingEvent((void *)this, event);
202 if (connection_open_profile(m_connectionHandle, m_profileHandle, connection_opened_callback, pendingEvent) != CONNECTION_ERROR_NONE) {
203 LoggerD("Connection open Failed");
204 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
210 void NetworkBearerSelection::OnRequestReceived(const EventNetworkBearerReleasePtr &event)
213 if (event->getDomainName().compare(m_domainName) == 0) {
214 if (!m_profileHandle) {
215 event->setExceptionCode(ExceptionCodes::AlreadyInUseException);
219 event->switchToManualAnswer();
221 if (connection_profile_unset_state_changed_cb(m_profileHandle) != CONNECTION_ERROR_NONE) {
222 LoggerD("unset callback is failed");
226 NewtorkBearerReleasePendingEvent *pendingEvent = new NewtorkBearerReleasePendingEvent((void *)this, event);
228 if (connection_close_profile(m_connectionHandle, m_profileHandle, connection_closed_callback, pendingEvent) != CONNECTION_ERROR_NONE) {
229 LoggerD("connection close failed");
232 EventRequestReceiver<EventNetworkBearerRelease>::ManualAnswer(event);
236 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
240 void NetworkBearerSelection::reLaunchConnection(const EventNetworkBearerSelectionPtr &event)
244 if (connection_profile_unset_state_changed_cb(m_profileHandle) != CONNECTION_ERROR_NONE) {
245 LoggerD("unset callback is failed");
246 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
250 NewtorkBearerSelectionPendingEvent *pendingEvent = new NewtorkBearerSelectionPendingEvent((void *)this, event);
252 if (connection_close_profile(m_connectionHandle, m_profileHandle, connection_removed_callback, pendingEvent) != CONNECTION_ERROR_NONE) {
253 LoggerD("connection close failed");
254 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
260 void NetworkBearerSelection::removeStateChangeListener(const EventNetworkBearerSelectionPtr &event)
264 m_profileHandle = NULL;
265 connection_profile_iterator_h profileIter;
267 int ret = connection_get_profile_iterator(m_connectionHandle, CONNECTION_ITERATOR_TYPE_REGISTERED, &profileIter);
268 if (ret != CONNECTION_ERROR_NONE) {
269 LoggerD("Fail to get profile iterator");
270 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
273 while (connection_profile_iterator_has_next(profileIter)) {
274 if (connection_profile_iterator_next(profileIter, &m_profileHandle) != CONNECTION_ERROR_NONE) {
275 LoggerD("Fail to get profile handle");
276 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
281 NewtorkBearerSelectionPendingEvent *pendingEvent = new NewtorkBearerSelectionPendingEvent((void *)this, event);
283 if (connection_open_profile(m_connectionHandle, m_profileHandle, connection_opened_callback, pendingEvent) != CONNECTION_ERROR_NONE) {
284 LoggerD("Connection open Failed");
285 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
291 void NetworkBearerSelection::registStateChangeListener(const EventNetworkBearerSelectionPtr &event)
294 char *interfaceName = NULL;
295 char *hostAddr = NULL;
296 struct hostent *host_entry;
298 if (connection_profile_get_network_interface_name(m_profileHandle, &interfaceName) != CONNECTION_ERROR_NONE) {
299 LoggerD("Fail to get interface name!");
300 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
303 LoggerD("Interface name : " << interfaceName);
306 host_entry = gethostbyname(event->getDomainName().c_str());
309 LoggerD("gethostbyname is failed");
310 makeRequestCallback(event, CONNECTION_STATE_INVALID_VALUES_ERROR);
312 if (connection_close_profile(m_connectionHandle, m_profileHandle, connection_closed_callback2, NULL) != CONNECTION_ERROR_NONE) {
313 LoggerD("connection close failed");
314 makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR);
316 m_profileHandle = NULL;
320 hostAddr = inet_ntoa( *(struct in_addr*)host_entry->h_addr_list[0]);
321 LoggerD("hostAddr : " << hostAddr);
323 NewtorkBearerSelectionPendingEvent *pendingEvent = new NewtorkBearerSelectionPendingEvent((void *)this, event);
324 if (connection_profile_set_state_changed_cb(m_profileHandle, connection_state_changed_callback, pendingEvent) != CONNECTION_ERROR_NONE) {
325 LoggerE("Callback register is failed.");
329 connection_add_route(m_connectionHandle, interfaceName, hostAddr);
333 void NetworkBearerSelection::deregistStateChangeListener(const EventNetworkBearerReleasePtr &event)
336 m_profileHandle = NULL;
337 m_connectionState = NETWORK_DISCONNECTED;
338 EventRequestReceiver<EventNetworkBearerRelease>::ManualAnswer(event);
341 void NetworkBearerSelection::makeRequestCallback(const EventNetworkBearerSelectionPtr &event, connectionStateType state)
343 LoggerD("state : " << state);
344 OnNetworkBearerSelectionStateChangedEmitterPtr emitter = event->getEmitter();
345 OnNetworkBearerSelectionStateChangedPtr listener(new OnNetworkBearerSelectionStateChanged());
347 m_domainName = event->getDomainName();
348 listener->setConnectionStateType(state);
350 if (state == CONNECTION_STATE_INVALID_VALUES_ERROR) {
351 m_connectionState = NETWORK_CONNECTION_FAILED;
352 listener->setExceptionCode(ExceptionCodes::InvalidArgumentException);
353 } else if (state == CONNECTION_STATE_PLATFORM_ERROR) {
354 m_connectionState = NETWORK_CONNECTION_FAILED;
355 listener->setExceptionCode(ExceptionCodes::PlatformException);
356 } else if (state == CONNECTION_STATE_DISCONNECTED) {
357 m_connectionState = NETWORK_DISCONNECTED;
358 if (connection_profile_unset_state_changed_cb(m_profileHandle) != CONNECTION_ERROR_NONE) {
359 LoggerD("unset callback is failed");
361 m_profileHandle = NULL;
362 } else if (state == CONNECTION_STATE_CONNECTED) {
363 m_connectionState = NETWORK_CONNECTED;
365 emitter->emit(listener);
368 void NetworkBearerSelection::makeReleaseCallback(const EventNetworkBearerReleasePtr &event)
370 event->setExceptionCode(ExceptionCodes::AlreadyInUseException);
373 bool NetworkBearerSelection::checkProfileHandle()
375 if(m_profileHandle == NULL) {
381 bool NetworkBearerSelection::checkCellularNetworkEnable()
383 connection_cellular_state_e cellularState;
385 if (connection_get_cellular_state(m_connectionHandle, &cellularState) != CONNECTION_ERROR_NONE) {
386 LoggerD("Fail to get Cellular state");
390 LoggerD("celluar network state is " << cellularState);
392 if (cellularState == CONNECTION_CELLULAR_STATE_AVAILABLE || cellularState == CONNECTION_CELLULAR_STATE_CONNECTED) {