summaryrefslogtreecommitdiff
path: root/src/connection_manager.c
blob: 0bd59f679c157819fd02c4724eb733b2e19b7465 (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
 /*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *
 * Contact: Jin Yoon <jinny.yoon@samsung.com>
 *          Geunsun Lee <gs86.lee@samsung.com>
 *          Eunyoung Lee <ey928.lee@samsung.com>
 *          Junkyu Han <junkyu.han@samsung.com>
 *          Jeonghoon Park <jh1979.park@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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"

struct conn_mgr_s {
	connection_h connection;
	connection_type_e net_state;
	connection_wifi_state_e wifi_state;
	char *ip_addr;
};

struct conn_mgr_s conn_mgr = { 0, };

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_ip_changed_cb(const char* ipv4_address,
		const char* ipv6_address, void* user_data)
{
	_D("ip changed from[%s] to[%s]", conn_mgr.ip_addr, ipv4_address);

	if (conn_mgr.ip_addr) {
		free(conn_mgr.ip_addr);
		conn_mgr.ip_addr = strdup(ipv4_address);
	}
	return;
}

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

	conn_mgr.net_state = type;
	if (conn_mgr.net_state != CONNECTION_TYPE_WIFI) {
		ret = connection_get_wifi_state(conn_mgr.connection, &conn_mgr.wifi_state);
		if (CONNECTION_ERROR_NONE != ret)
			_E("fail connection_get_wifi_state - [%s]",
				__connection_error_to_string(ret));
		else
			_D("net_state[%d] - wifi_state[%d]",
				conn_mgr.net_state, conn_mgr.wifi_state);
	}

	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");
		if (conn_mgr.ip_addr) {
			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("fail to connection_get_ip_address() - [%s]",
			__connection_error_to_string(ret));
		return -1;
	}

	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("fail 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("fail 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("fail to connection_get_ip_address() - [%s]",
				__connection_error_to_string(ret));
	}

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

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

	ret = connection_set_ip_address_changed_cb(conn_mgr.connection,
			__conn_mgr_ip_changed_cb, NULL);
	if (CONNECTION_ERROR_NONE != ret)
		_E("fail toconnection_set_ip_address_changed_cb - [%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);

	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.connection = NULL;
	}

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