ef5fd7eca5908163e5a8b6d789cbf337bbd826e9
[apps/native/position-finder-server.git] / src / connection_manager.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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  *
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
12  *
13  * http://floralicense.org/license/
14  *
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.
20  */
21  /*
22  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
23  *
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>
29  *
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
33  *
34  * http://floralicense.org/license/
35  *
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.
41  */
42
43 #include <net_connection.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "log.h"
48
49 struct conn_mgr_s {
50         connection_h connection;
51         connection_type_e net_state;
52         connection_wifi_state_e wifi_state;
53         char *ip_addr;
54 };
55
56 struct conn_mgr_s conn_mgr = { 0, };
57
58 static const char *__connection_error_to_string(connection_error_e error)
59 {
60         switch (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";
93         default:
94                 return "CONNECTION_ERROR_UNKNOWN";
95         }
96 }
97
98 static void __conn_mgr_ip_changed_cb(const char* ipv4_address,
99                 const char* ipv6_address, void* user_data)
100 {
101         _D("ip changed from[%s] to[%s]", conn_mgr.ip_addr, ipv4_address);
102
103         if (conn_mgr.ip_addr) {
104                 free(conn_mgr.ip_addr);
105                 conn_mgr.ip_addr = strdup(ipv4_address);
106         }
107         return;
108 }
109
110 static void __conn_mgr_connection_changed_cb(connection_type_e type, void* user_data)
111 {
112         int ret = CONNECTION_ERROR_NONE;
113         _D("connection changed from[%d] to[%d]", conn_mgr.net_state, type);
114
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));
121                 else
122                         _D("net_state[%d] - wifi_state[%d]",
123                                 conn_mgr.net_state, conn_mgr.wifi_state);
124         }
125
126         return;
127 }
128
129 int connection_manager_get_ip(const char **ip)
130 {
131         int ret = CONNECTION_ERROR_NONE;
132
133         retv_if(conn_mgr.connection == NULL, -1);
134         retv_if(ip == NULL, -1);
135
136         if (conn_mgr.ip_addr) {
137                 *ip = conn_mgr.ip_addr;
138                 return 0;
139         }
140
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;
146                 }
147                 return -1;
148         }
149
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));
155                 return -1;
156         }
157
158         return 0;
159 }
160
161 int connection_manager_init(void)
162 {
163         int ret = CONNECTION_ERROR_NONE;
164         if (conn_mgr.connection) {
165                 _W("connection manager is already initialized");
166                 return 0;
167         }
168
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));
173                 return -1;
174         }
175
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));
180
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));
187         }
188
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));
194
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));
199
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));
205
206         _D("net_state[%d], wifi_state[%d], ip address[%s]",
207                 conn_mgr.net_state, conn_mgr.wifi_state, conn_mgr.ip_addr);
208
209         return 0;
210 }
211
212 int connection_manager_fini(void)
213 {
214         if (conn_mgr.connection) {
215                 int ret = 0;
216                 ret = connection_destroy(conn_mgr.connection);
217                 _D("connection_destroy - [%s]", __connection_error_to_string(ret));
218                 conn_mgr.connection = NULL;
219         }
220
221         if (conn_mgr.ip_addr) {
222                 free(conn_mgr.ip_addr);
223                 conn_mgr.ip_addr = NULL;
224         }
225         return 0;
226 }