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>
8 * Jeonghoon Park <jh1979.park@samsung.com>
10 * Licensed under the Flora License, Version 1.1 (the License);
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://floralicense.org/license/
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an AS IS BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
23 #include <net_connection.h>
30 connection_h connection;
31 connection_type_e net_state;
32 connection_wifi_state_e wifi_state;
36 struct conn_mgr_s conn_mgr = { 0, };
38 static const char *__connection_error_to_string(connection_error_e error)
41 case CONNECTION_ERROR_NONE:
42 return "CONNECTION_ERROR_NONE";
43 case CONNECTION_ERROR_INVALID_PARAMETER:
44 return "CONNECTION_ERROR_INVALID_PARAMETER";
45 case CONNECTION_ERROR_OUT_OF_MEMORY:
46 return "CONNECTION_ERROR_OUT_OF_MEMORY";
47 case CONNECTION_ERROR_INVALID_OPERATION:
48 return "CONNECTION_ERROR_INVALID_OPERATION";
49 case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
50 return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED";
51 case CONNECTION_ERROR_OPERATION_FAILED:
52 return "CONNECTION_ERROR_OPERATION_FAILED";
53 case CONNECTION_ERROR_ITERATOR_END:
54 return "CONNECTION_ERROR_ITERATOR_END";
55 case CONNECTION_ERROR_NO_CONNECTION:
56 return "CONNECTION_ERROR_NO_CONNECTION";
57 case CONNECTION_ERROR_NOW_IN_PROGRESS:
58 return "CONNECTION_ERROR_NOW_IN_PROGRESS";
59 case CONNECTION_ERROR_ALREADY_EXISTS:
60 return "CONNECTION_ERROR_ALREADY_EXISTS";
61 case CONNECTION_ERROR_OPERATION_ABORTED:
62 return "CONNECTION_ERROR_OPERATION_ABORTED";
63 case CONNECTION_ERROR_DHCP_FAILED:
64 return "CONNECTION_ERROR_DHCP_FAILED";
65 case CONNECTION_ERROR_INVALID_KEY:
66 return "CONNECTION_ERROR_INVALID_KEY";
67 case CONNECTION_ERROR_NO_REPLY:
68 return "CONNECTION_ERROR_NO_REPLY";
69 case CONNECTION_ERROR_PERMISSION_DENIED:
70 return "CONNECTION_ERROR_PERMISSION_DENIED";
71 case CONNECTION_ERROR_NOT_SUPPORTED:
72 return "CONNECTION_ERROR_NOT_SUPPORTED";
74 return "CONNECTION_ERROR_UNKNOWN";
78 static void __conn_mgr_ip_changed_cb(const char* ipv4_address,
79 const char* ipv6_address, void* user_data)
81 _D("ip changed from[%s] to[%s]", conn_mgr.ip_addr, ipv4_address);
83 if (conn_mgr.ip_addr) {
84 free(conn_mgr.ip_addr);
85 conn_mgr.ip_addr = strdup(ipv4_address);
90 static void __conn_mgr_connection_changed_cb(connection_type_e type, void* user_data)
92 int ret = CONNECTION_ERROR_NONE;
93 _D("connection changed from[%d] to[%d]", conn_mgr.net_state, type);
95 conn_mgr.net_state = type;
96 if (conn_mgr.net_state != CONNECTION_TYPE_WIFI) {
97 ret = connection_get_wifi_state(conn_mgr.connection, &conn_mgr.wifi_state);
98 if (CONNECTION_ERROR_NONE != ret)
99 _E("fail connection_get_wifi_state - [%s]",
100 __connection_error_to_string(ret));
102 _D("net_state[%d] - wifi_state[%d]",
103 conn_mgr.net_state, conn_mgr.wifi_state);
109 int connection_manager_get_ip(const char **ip)
111 int ret = CONNECTION_ERROR_NONE;
113 retv_if(conn_mgr.connection == NULL, -1);
114 retv_if(ip == NULL, -1);
116 if (conn_mgr.ip_addr) {
117 *ip = conn_mgr.ip_addr;
121 if (conn_mgr.net_state == CONNECTION_TYPE_DISCONNECTED) {
122 _W("disconnected now");
123 if (conn_mgr.ip_addr) {
124 free(conn_mgr.ip_addr);
125 conn_mgr.ip_addr = NULL;
130 ret = connection_get_ip_address(conn_mgr.connection,
131 CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr);
132 if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL)) {
133 _E("fail to connection_get_ip_address() - [%s]",
134 __connection_error_to_string(ret));
141 int connection_manager_init(void)
143 int ret = CONNECTION_ERROR_NONE;
144 if (conn_mgr.connection) {
145 _W("connection manager is already initialized");
149 ret = connection_create(&conn_mgr.connection);
150 if (CONNECTION_ERROR_NONE != ret) {
151 _E("fail to create connection - [%s]",
152 __connection_error_to_string(ret));
156 ret = connection_get_type(conn_mgr.connection, &conn_mgr.net_state);
157 if (CONNECTION_ERROR_NONE != ret)
158 _E("fail connection_get_type - [%s]",
159 __connection_error_to_string(ret));
161 if (conn_mgr.net_state != CONNECTION_TYPE_DISCONNECTED) {
162 ret = connection_get_ip_address(conn_mgr.connection,
163 CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr);
164 if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL))
165 _E("fail to connection_get_ip_address() - [%s]",
166 __connection_error_to_string(ret));
169 ret = connection_set_type_changed_cb(conn_mgr.connection,
170 __conn_mgr_connection_changed_cb, NULL);
171 if (CONNECTION_ERROR_NONE != ret)
172 _E("fail connection_set_type_changed_cb - [%s]",
173 __connection_error_to_string(ret));
175 ret = connection_get_wifi_state(conn_mgr.connection, &conn_mgr.wifi_state);
176 if (CONNECTION_ERROR_NONE != ret)
177 _E("fail connection_get_wifi_state - [%s]",
178 __connection_error_to_string(ret));
180 ret = connection_set_ip_address_changed_cb(conn_mgr.connection,
181 __conn_mgr_ip_changed_cb, NULL);
182 if (CONNECTION_ERROR_NONE != ret)
183 _E("fail toconnection_set_ip_address_changed_cb - [%s]",
184 __connection_error_to_string(ret));
186 _D("net_state[%d], wifi_state[%d], ip address[%s]",
187 conn_mgr.net_state, conn_mgr.wifi_state, conn_mgr.ip_addr);
192 int connection_manager_fini(void)
194 if (conn_mgr.connection) {
196 ret = connection_destroy(conn_mgr.connection);
197 _D("connection_destroy - [%s]", __connection_error_to_string(ret));
198 conn_mgr.connection = NULL;
201 if (conn_mgr.ip_addr) {
202 free(conn_mgr.ip_addr);
203 conn_mgr.ip_addr = NULL;