summaryrefslogtreecommitdiff
path: root/src/connection.c
blob: 4ab566fa6edac26c6c0262dd71986cd621d4771c (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * 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 <net_connection_private.h>
#include <vconf/vconf.h>
#include <dlog.h>
#include <string.h>
#include <stdio.h>

#define TIZEN_N_CONNECTION "CAPI_NETWORK_CONNECTION"

static void __connection_cb_net_config_change_cb(keynode_t *node, void *user_data);

// API to convert error codes back and forth
static int __convert_error_code(int dnet_error_code)
{
	switch(dnet_error_code)
	{
		case CONNECTION_ERROR_NONE:
			return CONNECTION_ERROR_NONE;
		case CONNECTION_ERROR_INVALID_PARAMETER:
			return CONNECTION_ERROR_INVALID_PARAMETER;
		default:
			return CONNECTION_ERROR_INVALID_PARAMETER;
	}
}

static int __connection_set_callbacks(connection_h handle, void *callback, void *user_data)
{
	if(handle!=NULL)
	{
		connection_handle_s *local_handle = (connection_handle_s *)handle;
		local_handle->user_data = user_data;
		local_handle->callback = callback;
        if(callback)
        {
            // This single vconf key will notify 
            // network status, ip and proxy changes.
		    vconf_notify_key_changed(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 
						__connection_cb_net_config_change_cb,
						local_handle);
        }
        else
        {
		    vconf_ignore_key_changed(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 
						__connection_cb_net_config_change_cb);
        }
		return (CONNECTION_ERROR_NONE);
	}
	else
	{
		return (CONNECTION_ERROR_INVALID_PARAMETER);
	}
}

int connection_set_cb(connection_h handle, connection_cb callback, void *user_data)
{
	int retval = CONNECTION_ERROR_NONE;

	retval = __connection_set_callbacks(handle, callback, user_data);

	return __convert_error_code(retval);
}

int connection_unset_cb(connection_h handle)
{
	int retval = CONNECTION_ERROR_NONE;

	retval = __connection_set_callbacks(handle, NULL, NULL);

	return __convert_error_code(retval);
}

static void __connection_cb_net_config_change_cb(keynode_t *node, void *user_data)
{
	LOGI(TIZEN_N_CONNECTION,"Net Status Indication\n");
	if((user_data!=NULL))
	{
		connection_handle_s *temp = user_data;
		if(temp->callback!=NULL)
		{
            if(!strcmp(vconf_keynode_get_name(node), VCONFKEY_NETWORK_STATUS))
                temp->callback(CONNECTION_NETWORK_STATUS, temp->user_data);
            if(!strcmp(vconf_keynode_get_name(node), VCONFKEY_NETWORK_IP))
                temp->callback(CONNECTION_IP_ADDRESS, temp->user_data);
            if(!strcmp(vconf_keynode_get_name(node), VCONFKEY_NETWORK_PROXY))
                temp->callback(CONNECTION_PROXY_ADDRESS, temp->user_data);
		}
	}
}

int connection_create(connection_h *handle)
{
	if(handle==NULL)
	{
		LOGI(TIZEN_N_CONNECTION,"Wrong Parameter Passed\n");
		return CONNECTION_ERROR_INVALID_PARAMETER;
	}
	*handle = calloc(1, sizeof(connection_handle_s));
	if(*handle!=NULL)
	{
		LOGI(TIZEN_N_CONNECTION,"New Handle Created %p\n", *handle);
	}
	else
	{
		return CONNECTION_ERROR_OUT_OF_MEMORY;
	}
	return (CONNECTION_ERROR_NONE);
}

int connection_destroy(connection_h handle)
{
	if(handle==NULL)
	{
		LOGI(TIZEN_N_CONNECTION,"Wrong Parameter Passed\n");
		return CONNECTION_ERROR_INVALID_PARAMETER;
	}

	LOGI(TIZEN_N_CONNECTION,"Destroy Handle : %p\n", handle);
	free(handle);

	return CONNECTION_ERROR_NONE;
}

int connection_get_network_status(connection_network_type_e network_type,
				connection_network_status_e* network_status)
{

	if (network_status==NULL ||
	    network_type > CONNECTION_WIFI_TYPE ||
	    network_type < CONNECTION_DEFAULT_TYPE)
	{
		return CONNECTION_ERROR_INVALID_PARAMETER;
	}

	int status = 0;

	if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status))
	{
		LOGI(TIZEN_N_CONNECTION,"First Step Failure = %d\n", status);
		return CONNECTION_ERROR_INVALID_OPERATION;
	}
	LOGI(TIZEN_N_CONNECTION,"Connected Network = %d\n", status);

	if (network_type==CONNECTION_DEFAULT_TYPE)
	{
		switch(status)
		{
			case VCONFKEY_NETWORK_CELLULAR:
			case VCONFKEY_NETWORK_WIFI:
				*network_status = CONNECTION_STATUS_AVAILABLE;
				break;
			default :
				*network_status = CONNECTION_STATUS_UNAVAILABLE;
				break;
		}

		return CONNECTION_ERROR_NONE;
	}

	if (network_type == CONNECTION_MOBILE_TYPE)
	{
		if (!vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE,&status))
		{
			LOGI(TIZEN_N_CONNECTION,"Mobile = %d\n", status);
			if (status==VCONFKEY_NETWORK_CELLULAR_ON)
			{
				*network_status = CONNECTION_STATUS_AVAILABLE;
			}
			else
			{
				*network_status = CONNECTION_STATUS_UNAVAILABLE;
			}
			return CONNECTION_ERROR_NONE;
		}
		else
		{
			*network_status = CONNECTION_STATUS_UNKNOWN;
			LOGI(TIZEN_N_CONNECTION,"3G Failed = %d\n", status);
			return CONNECTION_ERROR_INVALID_OPERATION;
		}
	}

	if (network_type == CONNECTION_WIFI_TYPE)
	{
		if (!vconf_get_int(VCONFKEY_NETWORK_WIFI_STATE,&status))
		{
			LOGI(TIZEN_N_CONNECTION,"WiFi = %d\n", status);
			if (status==VCONFKEY_NETWORK_WIFI_CONNECTED)
			{
				*network_status = CONNECTION_STATUS_AVAILABLE;
			}
			else
			{
				*network_status = CONNECTION_STATUS_UNAVAILABLE;
			}
			return CONNECTION_ERROR_NONE;
		}
		else
		{
			*network_status = CONNECTION_STATUS_UNKNOWN;
			LOGI(TIZEN_N_CONNECTION,"WiFi Failed = %d\n", status);
			return CONNECTION_ERROR_INVALID_OPERATION;
		}
	}
	return CONNECTION_ERROR_INVALID_PARAMETER;
}

bool connection_is_connected(void)
{
	int network_status = 0;
	if (!vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status))
	{
		return (network_status > VCONFKEY_NETWORK_OFF) ? true : false;
	}
	else
		return false;
}

int connection_get_ip_address(connection_h handle, char **ip_address)
{
	if(handle)
	{
		*ip_address = vconf_get_str(VCONFKEY_NETWORK_IP);
		LOGI(TIZEN_N_CONNECTION,"IP Address %s\n", *ip_address);
		return CONNECTION_ERROR_NONE;
	}
	return (CONNECTION_ERROR_INVALID_PARAMETER);
}

int connection_get_proxy(connection_h handle, char **proxy)
{
	if(handle)
	{
		*proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
		LOGI(TIZEN_N_CONNECTION,"Proxy Address %s\n", *proxy);
		return (CONNECTION_ERROR_NONE);
	}
	return (CONNECTION_ERROR_INVALID_PARAMETER);
}

static int __fill_call_statistic(connection_h handle, stat_request_e member, int *value)
{
	if(handle && value)
	{
        switch(member)
        {
            case LAST_DATACALL_DURATION:
		        *value = 0;
                break;
            case LAST_SENT_DATA_SIZE:
                if (vconf_get_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT, value))
                {
                    LOGI(TIZEN_N_CONNECTION,"Cannot Get LAST_SENT_DATA_SIZE = %d\n", *value);
		            *value = 0;
                    return CONNECTION_ERROR_INVALID_OPERATION;
                }
                LOGI(TIZEN_N_CONNECTION,"LAST_SENT_DATA_SIZE:%d bytes\n", *value);

                break;
            case LAST_RECEIVED_DATA_SIZE:
                if (vconf_get_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV, value))
                {
                    LOGI(TIZEN_N_CONNECTION,"Cannot Get LAST_RECEIVED_DATA_SIZE: = %d\n", *value);
		            *value = 0;
                    return CONNECTION_ERROR_INVALID_OPERATION;
                }
                LOGI(TIZEN_N_CONNECTION,"LAST_RECEIVED_DATA_SIZE:%d bytes\n", *value);
                break;
            case TOTAL_DATACALL_DURATION:
		        *value = 0;
                break;
            case TOTAL_SENT_DATA_SIZE:
                if (vconf_get_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT, value))
                {
                    LOGI(TIZEN_N_CONNECTION,"Cannot Get TOTAL_SENT_DATA_SIZE: = %d\n", *value);
		            *value = 0;
                    return CONNECTION_ERROR_INVALID_OPERATION;
                }
                LOGI(TIZEN_N_CONNECTION,"TOTAL_SENT_DATA_SIZE:%d bytes\n", *value);
                break;
            case TOTAL_RECEIVED_DATA_SIZE:
                if (vconf_get_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV, value))
                {
                    LOGI(TIZEN_N_CONNECTION,"Cannot Get TOTAL_RECEIVED_DATA_SIZE: = %d\n", *value);
		            *value = 0;
                    return CONNECTION_ERROR_INVALID_OPERATION;
                }
                LOGI(TIZEN_N_CONNECTION,"TOTAL_RECEIVED_DATA_SIZE:%d bytes\n", *value);
                break;
            case LAST_WIFI_DATACALL_DURATION:
		        *value = 0;
                break;
            case LAST_WIFI_SENT_DATA_SIZE:
                if (vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, value))
                {
                    LOGI(TIZEN_N_CONNECTION,"Cannot Get LAST_WIFI_SENT_DATA_SIZE: = %d\n", *value);
		            *value = 0;
                    return CONNECTION_ERROR_INVALID_OPERATION;
                }
                LOGI(TIZEN_N_CONNECTION,"LAST_WIFI_SENT_DATA_SIZE:%d bytes\n", *value);
                break;
            case LAST_WIFI_RECEIVED_DATA_SIZE:
                if (vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, value))
                {
                    LOGI(TIZEN_N_CONNECTION,"Cannot Get LAST_WIFI_RECEIVED_DATA_SIZE: = %d\n", *value);
		            *value = 0;
                    return CONNECTION_ERROR_INVALID_OPERATION;
                }
                LOGI(TIZEN_N_CONNECTION,"LAST_WIFI_RECEIVED_DATA_SIZE:%d bytes\n", *value);
                break;
            case TOTAL_WIFI_DATACALL_DURATION:
		        *value = 0;
                break;
            case TOTAL_WIFI_SENT_DATA_SIZE:
                if (vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, value))
                {
                    LOGI(TIZEN_N_CONNECTION,"Cannot Get TOTAL_WIFI_SENT_DATA_SIZE: = %d\n", *value);
		            *value = 0;
                    return CONNECTION_ERROR_INVALID_OPERATION;
                }
                LOGI(TIZEN_N_CONNECTION,"TOTAL_WIFI_SENT_DATA_SIZE:%d bytes\n", *value);
                break;
            case TOTAL_WIFI_RECEIVED_DATA_SIZE:
                if (vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, value))
                {
                    LOGI(TIZEN_N_CONNECTION,"Cannot Get TOTAL_WIFI_RECEIVED_DATA_SIZE: = %d\n", *value);
		            *value = 0;
                    return CONNECTION_ERROR_INVALID_OPERATION;
                }
                LOGI(TIZEN_N_CONNECTION,"TOTAL_WIFI_RECEIVED_DATA_SIZE:%d bytes\n", *value);
                break;
        }
		return (CONNECTION_ERROR_NONE);
	}
	return (CONNECTION_ERROR_INVALID_PARAMETER);
}

int connection_get_last_datacall_duration(connection_h handle, int *value)
{
	return __fill_call_statistic(handle, LAST_DATACALL_DURATION, value);
}

int connection_get_last_received_data_size(connection_h handle, int *value)
{
	return __fill_call_statistic(handle, LAST_RECEIVED_DATA_SIZE, value);
}

int connection_get_last_sent_data_size(connection_h handle, int *value)
{
	return __fill_call_statistic(handle, LAST_SENT_DATA_SIZE, value);
}

int connection_get_total_datacall_duration(connection_h handle, int *value)
{
	return __fill_call_statistic(handle, TOTAL_DATACALL_DURATION, value);
}

int connection_get_total_received_data_size (connection_h handle, int *value)
{
	return __fill_call_statistic(handle, TOTAL_RECEIVED_DATA_SIZE, value);
}

int connection_get_total_sent_data_size (connection_h handle, int *value)
{
	return __fill_call_statistic(handle, TOTAL_SENT_DATA_SIZE, value);
}

int connection_get_wifi_last_datacall_duration(connection_h handle, int *value)
{
	return __fill_call_statistic(handle, LAST_WIFI_DATACALL_DURATION, value);
}

int connection_get_wifi_last_received_data_size(connection_h handle, int *value)
{
	return __fill_call_statistic(handle, LAST_WIFI_RECEIVED_DATA_SIZE, value);
}

int connection_get_wifi_last_sent_data_size(connection_h handle, int *value)
{
	return __fill_call_statistic(handle, LAST_WIFI_SENT_DATA_SIZE, value);
}

int connection_get_wifi_total_datacall_duration(connection_h handle, int *value)
{
	return __fill_call_statistic(handle, TOTAL_WIFI_DATACALL_DURATION, value);
}

int connection_get_wifi_total_received_data_size (connection_h handle, int *value)
{
	return __fill_call_statistic(handle, TOTAL_WIFI_RECEIVED_DATA_SIZE, value);
}

int connection_get_wifi_total_sent_data_size (connection_h handle, int *value)
{
	return __fill_call_statistic(handle, TOTAL_WIFI_SENT_DATA_SIZE, value);
}