summaryrefslogtreecommitdiff
path: root/src/connection_manager.c
blob: 2ed6d1be3bc82a173e0ce9e8b76fd584e8ce4e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Flora License, Version 1.1 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <net_connection.h>
#include <stdlib.h>
#include <string.h>

#include "log.h"
#include "connection_manager.h"

struct conn_mgr_s {
	connection_h connection;
	connection_type_e net_state;
	connection_wifi_state_e wifi_state;
	char *ip_addr;
	connection_state_changed_cb state_cb;
	void *state_cb_data;
};

struct conn_mgr_s conn_mgr = {
	NULL,
	CONNECTION_TYPE_DISCONNECTED,
	CONNECTION_WIFI_STATE_DEACTIVATED,
	NULL,
	NULL,
	NULL
};

static const char *__connection_error_to_string(connection_error_e error)
{
	switch (error) {
	case CONNECTION_ERROR_NONE:
		return "CONNECTION_ERROR_NONE";
	case CONNECTION_ERROR_INVALID_PARAMETER:
		return "CONNECTION_ERROR_INVALID_PARAMETER";
	case CONNECTION_ERROR_OUT_OF_MEMORY:
		return "CONNECTION_ERROR_OUT_OF_MEMORY";
	case CONNECTION_ERROR_INVALID_OPERATION:
		return "CONNECTION_ERROR_INVALID_OPERATION";
	case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
		return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED";
	case CONNECTION_ERROR_OPERATION_FAILED:
		return "CONNECTION_ERROR_OPERATION_FAILED";
	case CONNECTION_ERROR_ITERATOR_END:
		return "CONNECTION_ERROR_ITERATOR_END";
	case CONNECTION_ERROR_NO_CONNECTION:
		return "CONNECTION_ERROR_NO_CONNECTION";
	case CONNECTION_ERROR_NOW_IN_PROGRESS:
		return "CONNECTION_ERROR_NOW_IN_PROGRESS";
	case CONNECTION_ERROR_ALREADY_EXISTS:
		return "CONNECTION_ERROR_ALREADY_EXISTS";
	case CONNECTION_ERROR_OPERATION_ABORTED:
		return "CONNECTION_ERROR_OPERATION_ABORTED";
	case CONNECTION_ERROR_DHCP_FAILED:
		return "CONNECTION_ERROR_DHCP_FAILED";
	case CONNECTION_ERROR_INVALID_KEY:
		return "CONNECTION_ERROR_INVALID_KEY";
	case CONNECTION_ERROR_NO_REPLY:
		return "CONNECTION_ERROR_NO_REPLY";
	case CONNECTION_ERROR_PERMISSION_DENIED:
		return "CONNECTION_ERROR_PERMISSION_DENIED";
	case CONNECTION_ERROR_NOT_SUPPORTED:
		return "CONNECTION_ERROR_NOT_SUPPORTED";
	default:
		return "CONNECTION_ERROR_UNKNOWN";
	}
}

static void __conn_mgr_connection_changed_cb(connection_type_e type,
	 void* user_data)
{
	int ret = CONNECTION_ERROR_NONE;
	connection_state_e state = CONNECTION_STATE_DISCONNECTED;
	_D("connection changed from[%d] to[%d]", conn_mgr.net_state, type);

	conn_mgr.net_state = type;

	ret = connection_get_wifi_state(conn_mgr.connection,
		&conn_mgr.wifi_state);

	if (CONNECTION_ERROR_NONE != ret)
		_E("failed to connection_get_wifi_state - [%s]",
			__connection_error_to_string(ret));

	free(conn_mgr.ip_addr);
	conn_mgr.ip_addr = NULL;

	if (conn_mgr.net_state != CONNECTION_TYPE_DISCONNECTED) {
		state = CONNECTION_STATE_CONNECTED;
		ret = connection_get_ip_address(conn_mgr.connection,
				CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr);

		if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL))
			_E("failed to connection_get_ip_address() - [%s]",
				__connection_error_to_string(ret));
	}

	if (conn_mgr.state_cb)
		conn_mgr.state_cb(state, conn_mgr.ip_addr, conn_mgr.state_cb_data);

	return;
}

int connection_manager_get_ip(const char **ip)
{
	int ret = CONNECTION_ERROR_NONE;

	retv_if(conn_mgr.connection == NULL, -1);
	retv_if(ip == NULL, -1);

	if (conn_mgr.ip_addr) {
		*ip = conn_mgr.ip_addr;
		return 0;
	}

	if (conn_mgr.net_state == CONNECTION_TYPE_DISCONNECTED) {
		_W("disconnected now");

		free(conn_mgr.ip_addr);
		conn_mgr.ip_addr = NULL;

		return -1;
	}

	ret = connection_get_ip_address(conn_mgr.connection,
			CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr);

	if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL)) {
		_E("failed to connection_get_ip_address() - [%s]",
			__connection_error_to_string(ret));
		return -1;
	}

	*ip = conn_mgr.ip_addr;

	return 0;
}

int connection_manager_init(void)
{
	int ret = CONNECTION_ERROR_NONE;
	if (conn_mgr.connection) {
		_W("connection manager is already initialized");
		return 0;
	}

	ret = connection_create(&conn_mgr.connection);
	if (CONNECTION_ERROR_NONE != ret) {
		_E("failed to create connection - [%s]",
			__connection_error_to_string(ret));
		return -1;
	}

	ret = connection_get_type(conn_mgr.connection, &conn_mgr.net_state);
	if (CONNECTION_ERROR_NONE != ret) {
		_E("failed to connection_get_type - [%s]",
			__connection_error_to_string(ret));
	}

	if (conn_mgr.net_state != CONNECTION_TYPE_DISCONNECTED) {
		ret = connection_get_ip_address(conn_mgr.connection,
			CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr);
		if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL))
			_E("failed to connection_get_ip_address() - [%s]",
				__connection_error_to_string(ret));
	}

	ret = connection_get_wifi_state(conn_mgr.connection, &conn_mgr.wifi_state);
	if (CONNECTION_ERROR_NONE != ret)
		_E("failed to connection_get_wifi_state - [%s]",
			__connection_error_to_string(ret));

	_D("net_state[%d], wifi_state[%d], ip address[%s]",
		conn_mgr.net_state, conn_mgr.wifi_state, conn_mgr.ip_addr);

	ret = connection_set_type_changed_cb(conn_mgr.connection,
			__conn_mgr_connection_changed_cb, &conn_mgr);
	if (CONNECTION_ERROR_NONE != ret)
		_E("failed to connection_set_type_changed_cb - [%s]",
			__connection_error_to_string(ret));

	return 0;
}

int connection_manager_fini(void)
{
	if (conn_mgr.connection) {
		int ret = 0;
		ret = connection_destroy(conn_mgr.connection);
		_D("connection_destroy - [%s]", __connection_error_to_string(ret));
	}

	conn_mgr.net_state = CONNECTION_TYPE_DISCONNECTED;
	conn_mgr.wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED;

	if (conn_mgr.ip_addr) {
		free(conn_mgr.ip_addr);
		conn_mgr.ip_addr = NULL;
	}

	conn_mgr.state_cb = NULL;
	conn_mgr.state_cb_data = NULL;

	return 0;
}

int connection_manager_set_state_changed_cb(
	connection_state_changed_cb state_cb, void *user_data)
{
	conn_mgr.state_cb = state_cb;

	if (state_cb) {
		connection_state_e state = CONNECTION_STATE_DISCONNECTED;

		conn_mgr.state_cb_data = user_data;
		if (conn_mgr.net_state != CONNECTION_TYPE_DISCONNECTED)
			state = CONNECTION_STATE_CONNECTED;

		conn_mgr.state_cb(state, conn_mgr.ip_addr, conn_mgr.state_cb_data);
	} else
		conn_mgr.state_cb_data = NULL;

	return 0;
}