2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Contact: Jin Yoon <jinny.yoon@samsung.com>
5 * Geunsun Lee <gs86.lee@samsung.com>
6 * Eunyoung Lee <ey928.lee@samsung.com>
7 * Junkyu Han <junkyu.han@samsung.com>
9 * Licensed under the Flora License, Version 1.1 (the License);
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://floralicense.org/license/
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an AS IS BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
22 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
24 * Contact: Jin Yoon <jinny.yoon@samsung.com>
25 * Geunsun Lee <gs86.lee@samsung.com>
26 * Eunyoung Lee <ey928.lee@samsung.com>
27 * Junkyu Han <junkyu.han@samsung.com>
28 * Jeonghoon Park <jh1979.park@samsung.com>
30 * Licensed under the Flora License, Version 1.1 (the License);
31 * you may not use this file except in compliance with the License.
32 * You may obtain a copy of the License at
34 * http://floralicense.org/license/
36 * Unless required by applicable law or agreed to in writing, software
37 * distributed under the License is distributed on an AS IS BASIS,
38 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 * See the License for the specific language governing permissions and
40 * limitations under the License.
43 #include <net_connection.h>
50 connection_h connection;
51 connection_type_e net_state;
52 connection_wifi_state_e wifi_state;
56 struct conn_mgr_s conn_mgr = { 0, };
58 static const char *__connection_error_to_string(connection_error_e error)
61 case CONNECTION_ERROR_NONE:
62 return "CONNECTION_ERROR_NONE";
63 case CONNECTION_ERROR_INVALID_PARAMETER:
64 return "CONNECTION_ERROR_INVALID_PARAMETER";
65 case CONNECTION_ERROR_OUT_OF_MEMORY:
66 return "CONNECTION_ERROR_OUT_OF_MEMORY";
67 case CONNECTION_ERROR_INVALID_OPERATION:
68 return "CONNECTION_ERROR_INVALID_OPERATION";
69 case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
70 return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED";
71 case CONNECTION_ERROR_OPERATION_FAILED:
72 return "CONNECTION_ERROR_OPERATION_FAILED";
73 case CONNECTION_ERROR_ITERATOR_END:
74 return "CONNECTION_ERROR_ITERATOR_END";
75 case CONNECTION_ERROR_NO_CONNECTION:
76 return "CONNECTION_ERROR_NO_CONNECTION";
77 case CONNECTION_ERROR_NOW_IN_PROGRESS:
78 return "CONNECTION_ERROR_NOW_IN_PROGRESS";
79 case CONNECTION_ERROR_ALREADY_EXISTS:
80 return "CONNECTION_ERROR_ALREADY_EXISTS";
81 case CONNECTION_ERROR_OPERATION_ABORTED:
82 return "CONNECTION_ERROR_OPERATION_ABORTED";
83 case CONNECTION_ERROR_DHCP_FAILED:
84 return "CONNECTION_ERROR_DHCP_FAILED";
85 case CONNECTION_ERROR_INVALID_KEY:
86 return "CONNECTION_ERROR_INVALID_KEY";
87 case CONNECTION_ERROR_NO_REPLY:
88 return "CONNECTION_ERROR_NO_REPLY";
89 case CONNECTION_ERROR_PERMISSION_DENIED:
90 return "CONNECTION_ERROR_PERMISSION_DENIED";
91 case CONNECTION_ERROR_NOT_SUPPORTED:
92 return "CONNECTION_ERROR_NOT_SUPPORTED";
94 return "CONNECTION_ERROR_UNKNOWN";
98 static void __conn_mgr_ip_changed_cb(const char* ipv4_address,
99 const char* ipv6_address, void* user_data)
101 _D("ip changed from[%s] to[%s]", conn_mgr.ip_addr, ipv4_address);
103 if (conn_mgr.ip_addr) {
104 free(conn_mgr.ip_addr);
105 conn_mgr.ip_addr = strdup(ipv4_address);
110 static void __conn_mgr_connection_changed_cb(connection_type_e type, void* user_data)
112 int ret = CONNECTION_ERROR_NONE;
113 _D("connection changed from[%d] to[%d]", conn_mgr.net_state, type);
115 conn_mgr.net_state = type;
116 if (conn_mgr.net_state != CONNECTION_TYPE_WIFI) {
117 ret = connection_get_wifi_state(conn_mgr.connection, &conn_mgr.wifi_state);
118 if (CONNECTION_ERROR_NONE != ret)
119 _E("fail connection_get_wifi_state - [%s]",
120 __connection_error_to_string(ret));
122 _D("net_state[%d] - wifi_state[%d]",
123 conn_mgr.net_state, conn_mgr.wifi_state);
129 int connection_manager_get_ip(const char **ip)
131 int ret = CONNECTION_ERROR_NONE;
133 retv_if(conn_mgr.connection == NULL, -1);
134 retv_if(ip == NULL, -1);
136 if (conn_mgr.ip_addr) {
137 *ip = conn_mgr.ip_addr;
141 if (conn_mgr.net_state == CONNECTION_TYPE_DISCONNECTED) {
142 _W("disconnected now");
143 if (conn_mgr.ip_addr) {
144 free(conn_mgr.ip_addr);
145 conn_mgr.ip_addr = NULL;
150 ret = connection_get_ip_address(conn_mgr.connection,
151 CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr);
152 if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL)) {
153 _E("fail to connection_get_ip_address() - [%s]",
154 __connection_error_to_string(ret));
161 int connection_manager_init(void)
163 int ret = CONNECTION_ERROR_NONE;
164 if (conn_mgr.connection) {
165 _W("connection manager is already initialized");
169 ret = connection_create(&conn_mgr.connection);
170 if (CONNECTION_ERROR_NONE != ret) {
171 _E("fail to create connection - [%s]",
172 __connection_error_to_string(ret));
176 ret = connection_get_type(conn_mgr.connection, &conn_mgr.net_state);
177 if (CONNECTION_ERROR_NONE != ret)
178 _E("fail connection_get_type - [%s]",
179 __connection_error_to_string(ret));
181 if (conn_mgr.net_state != CONNECTION_TYPE_DISCONNECTED) {
182 ret = connection_get_ip_address(conn_mgr.connection,
183 CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr);
184 if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL))
185 _E("fail to connection_get_ip_address() - [%s]",
186 __connection_error_to_string(ret));
189 ret = connection_set_type_changed_cb(conn_mgr.connection,
190 __conn_mgr_connection_changed_cb, NULL);
191 if (CONNECTION_ERROR_NONE != ret)
192 _E("fail connection_set_type_changed_cb - [%s]",
193 __connection_error_to_string(ret));
195 ret = connection_get_wifi_state(conn_mgr.connection, &conn_mgr.wifi_state);
196 if (CONNECTION_ERROR_NONE != ret)
197 _E("fail connection_get_wifi_state - [%s]",
198 __connection_error_to_string(ret));
200 ret = connection_set_ip_address_changed_cb(conn_mgr.connection,
201 __conn_mgr_ip_changed_cb, NULL);
202 if (CONNECTION_ERROR_NONE != ret)
203 _E("fail toconnection_set_ip_address_changed_cb - [%s]",
204 __connection_error_to_string(ret));
206 _D("net_state[%d], wifi_state[%d], ip address[%s]",
207 conn_mgr.net_state, conn_mgr.wifi_state, conn_mgr.ip_addr);
212 int connection_manager_fini(void)
214 if (conn_mgr.connection) {
216 ret = connection_destroy(conn_mgr.connection);
217 _D("connection_destroy - [%s]", __connection_error_to_string(ret));
218 conn_mgr.connection = NULL;
221 if (conn_mgr.ip_addr) {
222 free(conn_mgr.ip_addr);
223 conn_mgr.ip_addr = NULL;