Add Camera Module
[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  *          Jeonghoon Park <jh1979.park@samsung.com>
9  *
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
13  *
14  * http://floralicense.org/license/
15  *
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.
21  */
22
23 #include <net_connection.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "log.h"
28
29 struct conn_mgr_s {
30         connection_h connection;
31         connection_type_e net_state;
32         connection_wifi_state_e wifi_state;
33         char *ip_addr;
34 };
35
36 struct conn_mgr_s conn_mgr = { 0, };
37
38 static const char *__connection_error_to_string(connection_error_e error)
39 {
40         switch (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";
73         default:
74                 return "CONNECTION_ERROR_UNKNOWN";
75         }
76 }
77
78 static void __conn_mgr_ip_changed_cb(const char* ipv4_address,
79                 const char* ipv6_address, void* user_data)
80 {
81         _D("ip changed from[%s] to[%s]", conn_mgr.ip_addr, ipv4_address);
82
83         if (conn_mgr.ip_addr) {
84                 free(conn_mgr.ip_addr);
85                 conn_mgr.ip_addr = strdup(ipv4_address);
86         }
87         return;
88 }
89
90 static void __conn_mgr_connection_changed_cb(connection_type_e type, void* user_data)
91 {
92         int ret = CONNECTION_ERROR_NONE;
93         _D("connection changed from[%d] to[%d]", conn_mgr.net_state, type);
94
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));
101                 else
102                         _D("net_state[%d] - wifi_state[%d]",
103                                 conn_mgr.net_state, conn_mgr.wifi_state);
104         }
105
106         return;
107 }
108
109 int connection_manager_get_ip(const char **ip)
110 {
111         int ret = CONNECTION_ERROR_NONE;
112
113         retv_if(conn_mgr.connection == NULL, -1);
114         retv_if(ip == NULL, -1);
115
116         if (conn_mgr.ip_addr) {
117                 *ip = conn_mgr.ip_addr;
118                 return 0;
119         }
120
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;
126                 }
127                 return -1;
128         }
129
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));
135                 return -1;
136         }
137
138         return 0;
139 }
140
141 int connection_manager_init(void)
142 {
143         int ret = CONNECTION_ERROR_NONE;
144         if (conn_mgr.connection) {
145                 _W("connection manager is already initialized");
146                 return 0;
147         }
148
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));
153                 return -1;
154         }
155
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));
160
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));
167         }
168
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));
174
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));
179
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));
185
186         _D("net_state[%d], wifi_state[%d], ip address[%s]",
187                 conn_mgr.net_state, conn_mgr.wifi_state, conn_mgr.ip_addr);
188
189         return 0;
190 }
191
192 int connection_manager_fini(void)
193 {
194         if (conn_mgr.connection) {
195                 int ret = 0;
196                 ret = connection_destroy(conn_mgr.connection);
197                 _D("connection_destroy - [%s]", __connection_error_to_string(ret));
198                 conn_mgr.connection = NULL;
199         }
200
201         if (conn_mgr.ip_addr) {
202                 free(conn_mgr.ip_addr);
203                 conn_mgr.ip_addr = NULL;
204         }
205         return 0;
206 }