summaryrefslogtreecommitdiff
path: root/plugin/mms_plugin/MmsPluginConnManWrapper.cpp
blob: 5dac38c1581325d0dc750895e57a1427d43e0abc (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
* Copyright 2012-2013  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 <errno.h>
#include "MmsPluginConnManWrapper.h"
#include "MmsPluginDebug.h"
#include "MmsPluginHttp.h"
#include <glib.h>
#include "net_connection.h"

#define MMS_CONTEXT_INVOKE_WAIT_TIME	30
#define MMS_CONNECTION_API_WAIT_TIME	50

#define MMS_FREE(obj)\
	if (obj){\
		free(obj);\
		obj = NULL;\
	}


static Mutex g_mx;
static CndVar g_cv;
static connection_h connection = NULL;

void __connection_profile_print(connection_profile_h profile)
{
	int ret;
	char *profile_id = NULL;
	char *profile_name = NULL;
	char *interface_name = NULL;
	char *ip_address = NULL;
	char *subnet_mask = NULL;
	char *gateway_address = NULL;
	char *dns_address = NULL;
	char *proxy_address = NULL;
	char *apn = NULL;
	char *user_name = NULL;
	char *password = NULL;
	char *home_url = NULL;
	bool is_roaming;

	connection_profile_type_e profile_type;
	connection_profile_state_e profile_state;
	connection_ip_config_type_e ip_type = CONNECTION_IP_CONFIG_TYPE_NONE;
	connection_proxy_type_e proxy_type;
	connection_cellular_service_type_e service_type = CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN;
	connection_cellular_auth_type_e auth_type = CONNECTION_CELLULAR_AUTH_TYPE_NONE;

	MSG_DEBUG("**************************************************************************************************");
	ret = connection_profile_get_id(profile, &profile_id);
	MSG_DEBUG("Profile Id = [%s]", profile_id);

	ret = connection_profile_get_name(profile, &profile_name);
	MSG_DEBUG("Profile Name = [%s]", profile_name);

	ret = connection_profile_get_type(profile, &profile_type);

	if (profile_type == CONNECTION_PROFILE_TYPE_CELLULAR) {
		MSG_DEBUG("Profile Type = [CELLULAR]");
	} else if (profile_type == CONNECTION_PROFILE_TYPE_WIFI) {
		MSG_DEBUG("Profile Type = [WIFI]");
	} else if (profile_type == CONNECTION_PROFILE_TYPE_ETHERNET) {
		MSG_DEBUG("Profile Type = [ETHERNET]");
	} else if (profile_type == CONNECTION_PROFILE_TYPE_BT) {
		MSG_DEBUG("Profile Type = [BT]");
	} else {
		MSG_DEBUG("Profile Type = Unknown [%d]", profile_type);
	}

	ret = connection_profile_get_network_interface_name(profile, &interface_name);
	MSG_DEBUG("Profile Interface Name = [%s]", interface_name);

	ret = connection_profile_get_state(profile, &profile_state);
	if (profile_state == CONNECTION_PROFILE_STATE_DISCONNECTED) {
		MSG_DEBUG("Profile State = [DISCONNECTED]");
	} else if (profile_state == CONNECTION_PROFILE_STATE_ASSOCIATION) {
		MSG_DEBUG("Profile State = [ASSOCIATION]");
	} else if (profile_state == CONNECTION_PROFILE_STATE_CONFIGURATION) {
		MSG_DEBUG("Profile State = [CONFIGURATION]");
	} else if (profile_state == CONNECTION_PROFILE_STATE_CONNECTED) {
		MSG_DEBUG("Profile State = [CONNECTED]");
	} else {
		MSG_DEBUG("Profile State = Unknown [%d]", profile_state);
	}

	ret = connection_profile_get_ip_config_type(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_type);
	MSG_DEBUG("Profile Ip Config Type = [%d]", ip_type);

	ret = connection_profile_get_ip_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_address);
	MSG_DEBUG("Profile Ip Address = [%s]", ip_address);

	ret = connection_profile_get_subnet_mask(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &subnet_mask);
	MSG_DEBUG("Profile Subnet Mask = [%s]", subnet_mask);

	ret = connection_profile_get_gateway_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &gateway_address);
	MSG_DEBUG("Profile Gateway Address = [%s]", gateway_address);

	ret = connection_profile_get_dns_address(profile, 1, CONNECTION_ADDRESS_FAMILY_IPV4, &dns_address);
	MSG_DEBUG("Profile Dns Address = [%s]", dns_address);

	ret = connection_profile_get_proxy_type(profile, &proxy_type);
	MSG_DEBUG("Profile Proxy Type = [%d]", proxy_type);

	ret = connection_profile_get_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy_address);
	MSG_DEBUG("Profile Proxy Address = [%s]", proxy_address);

	ret = connection_profile_get_cellular_service_type(profile, &service_type);
	if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET) {
		MSG_DEBUG("Profile Service Type = [INTERNET]");
	} else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_MMS) {
		MSG_DEBUG("Profile Service Type = [MMS]");
	} else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET) {
		MSG_DEBUG("Profile Service Type = [PREPAID_INTERNET]");
	} else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS) {
		MSG_DEBUG("Profile Service Type = [PREPAID_MMS]");
	} else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING) {
		MSG_DEBUG("Profile Service Type = [TETHERING]");
	} else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION) {
		MSG_DEBUG("Profile Service Type = [APPLICATION]");
	} else {
		MSG_DEBUG("Profile Service Type = [Unknown][%d]", service_type);
	}

	ret = connection_profile_get_cellular_apn(profile, &apn);
	MSG_DEBUG("Profile Apn = [%s]", apn);

	ret = connection_profile_get_cellular_auth_info(profile, &auth_type, &user_name, &password);
	MSG_DEBUG("Profile Auth Type = [%d]", &auth_type);
	MSG_DEBUG("Profile Auth Name = [%s]", &user_name);
	MSG_DEBUG("Profile Auth Passward = [%s]", &password);

	ret = connection_profile_get_cellular_home_url(profile, &home_url);
	MSG_DEBUG("Profile Home Url = [%s]", home_url);

	ret = connection_profile_is_cellular_roaming(profile, &is_roaming);
	MSG_DEBUG("Profile Roaming = [%d]", is_roaming);
	MSG_DEBUG("**************************************************************************************************");

	MMS_FREE(profile_id);
	MMS_FREE(profile_name);
	MMS_FREE(interface_name);
	MMS_FREE(ip_address);
	MMS_FREE(subnet_mask);
	MMS_FREE(gateway_address);
	MMS_FREE(dns_address);
	MMS_FREE(proxy_address);
	MMS_FREE(apn);
	MMS_FREE(user_name);
	MMS_FREE(password);
	MMS_FREE(home_url);
}

static void __connection_type_changed_cb(connection_type_e type, void* user_data)
{
	MSG_DEBUG("Type changed callback, connection type : %d", type);
}

static void __connection_ip_changed_cb(const char* ipv4_address, const char* ipv6_address, void* user_data)
{
	MSG_DEBUG("IP changed callback, IPv4 address : %s, IPv6 address : %s",
			ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
}

static void __connection_proxy_changed_cb(const char* ipv4_address, const char* ipv6_address, void* user_data)
{
	MSG_DEBUG("Proxy changed callback, IPv4 address : %s, IPv6 address : %s",
			ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
}

static void __connection_profile_opened_cb(connection_error_e result, void* user_data)
{
	if (result == CONNECTION_ERROR_NONE || result == CONNECTION_ERROR_ALREADY_EXISTS)
		MSG_DEBUG("Connection open Succeeded [%d]", result);
	else
		MSG_DEBUG("Connection open Failed, err : %d", result);

	MmsPluginCmAgent *cmAgent = MmsPluginCmAgent::instance();

	cmAgent->open_callback(result, user_data);
}

static void __connection_profile_closed_cb(connection_error_e result, void* user_data)
{
	if (result ==  CONNECTION_ERROR_NONE)
		MSG_DEBUG("Connection close Succeeded");
	else
		MSG_DEBUG("Connection close Failed, err : %d", result);

	MmsPluginCmAgent *cmAgent = MmsPluginCmAgent::instance();

	cmAgent->close_callback(result, user_data);
}

static gboolean __connection_create(void *pVoid)
{
	MSG_BEGIN();

	bool ret = false;
	bool *ret_val = (bool *)pVoid;

	if (connection) {
		MSG_DEBUG("connection already exist");
		ret = true;
	} else {
		int err = connection_create(&connection);

		if (CONNECTION_ERROR_NONE == err) {
			connection_set_type_changed_cb(connection, __connection_type_changed_cb, NULL);
			connection_set_ip_address_changed_cb(connection, __connection_ip_changed_cb, NULL);
			connection_set_proxy_address_changed_cb(connection, __connection_proxy_changed_cb, NULL);
			ret = true;
			MSG_DEBUG("Client registration success [%p] ", connection);
		} else {
			MSG_DEBUG("Client registration failed %d", err);
		}
	}

	if (ret_val) {
		*ret_val = ret;
	}

	MSG_END();
	return FALSE;
}

static gboolean __connection_destroy(void *pVoid)
{
	MSG_BEGIN();

	int rv;
	int netOpenResult = NET_ERR_NONE;

	if (connection != NULL) {
		rv = connection_destroy(connection);
		connection = NULL;
		MSG_DEBUG("connection destory !!");
	} else {
		MSG_DEBUG("Cannot connection destroy : Handle is NULL");
		rv = CONNECTION_ERROR_INVALID_OPERATION;
	}

	MSG_END();
	return FALSE;
}

static gboolean __connection_profile_open(void *pVoid)
{
	MSG_BEGIN();

	int netOpenResult = NET_ERR_NONE;
	int *ret_val = (int *)pVoid;

	connection_profile_h profile;
	int err;

	err = connection_get_default_cellular_service_profile(connection, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profile);

	if (err != CONNECTION_ERROR_NONE) {
		MSG_DEBUG("connection_get_default_cellular_service_profile Failed!! [%d]", err);
		netOpenResult = NET_ERR_UNKNOWN;
	} else {

		if (connection_open_profile(connection, profile, __connection_profile_opened_cb, NULL) != CONNECTION_ERROR_NONE) {
			MSG_DEBUG("Connection open Failed!!");
			netOpenResult = NET_ERR_UNKNOWN;
		}
	}

	connection_profile_destroy(profile);

	if (ret_val) {
		*ret_val = netOpenResult;
	}

	MSG_END();

	return FALSE;
}

static gboolean __connection_profile_close(void *pVoid)
{
	MSG_BEGIN();

	int netOpenResult = NET_ERR_NONE;

	int *ret_val = (int *)pVoid;

	connection_profile_h profile;
	int err;

	err = connection_get_default_cellular_service_profile(connection, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profile);

	if (err  != CONNECTION_ERROR_NONE) {
		MSG_DEBUG("connection_get_default_cellular_service_profile Failed!! [%d]", err);
		netOpenResult = NET_ERR_UNKNOWN;
	} else {

		if (connection_close_profile(connection, profile, __connection_profile_closed_cb, NULL) != CONNECTION_ERROR_NONE) {
			MSG_DEBUG("Connection close Failed!!");
			netOpenResult = NET_ERR_UNKNOWN;
		}

	}

	connection_profile_destroy(profile);

	if (ret_val) {
		*ret_val = netOpenResult;
	}

	MSG_END();

	return FALSE;
}


void context_invoke_end_cb(gpointer data)
{
	g_mx.lock();

	MSG_DEBUG("@@ SIGNAL @@");

	g_cv.signal();

	g_mx.unlock();
}

/*
 * Network api should run at g_main_loop to receive callback
 * */
void context_invoke(GSourceFunc func, void *ret)
{
	MSG_BEGIN();

	int time_ret = 0;

	g_mx.lock();

	g_main_context_invoke_full(NULL, G_PRIORITY_HIGH, func, ret,  context_invoke_end_cb);

	MSG_DEBUG("@@ WAIT @@");

	time_ret = g_cv.timedwait(g_mx.pMutex(), MMS_CONTEXT_INVOKE_WAIT_TIME);
	g_mx.unlock();

	if (time_ret == ETIMEDOUT) {
		MSG_DEBUG("@@ WAKE by timeout@@");
	} else {
		MSG_DEBUG("@@ WAKE by signal@@");
	}

	MSG_END();
}

MmsPluginCmAgent *MmsPluginCmAgent::pInstance = NULL;

MmsPluginCmAgent *MmsPluginCmAgent::instance()
{
	if (!pInstance)
		pInstance = new MmsPluginCmAgent();

	return pInstance;
}

MmsPluginCmAgent::MmsPluginCmAgent()
{
	MSG_BEGIN();

	isCmOpened = false;

	isCmRegistered = false;

	home_url = NULL;
	interface_name = NULL;
	proxy_address = NULL;
	MSG_END();
}

MmsPluginCmAgent::~MmsPluginCmAgent()
{
	MMS_FREE(home_url);
	MMS_FREE(interface_name);
	MMS_FREE(proxy_address);
}

bool MmsPluginCmAgent::open()
{
	MSG_BEGIN();

	int netOpenResult = NET_ERR_NONE;

	lock();

	if (isCmOpened == false) {

		isCmRegistered = false;

		context_invoke(__connection_create, &isCmRegistered);

		if (isCmRegistered == true) {

			MSG_DEBUG("net_open_connection for MMS");

			context_invoke(__connection_profile_open, &netOpenResult);

			if (netOpenResult == NET_ERR_NONE) {

				MSG_DEBUG("## WAITING UNTIL Network Connection Open. ##");

				int time_ret = 0;

				time_ret = cv.timedwait(mx.pMutex(), MMS_CONNECTION_API_WAIT_TIME); // isCmOpened will changed by processCBdatas

				MSG_DEBUG("## WAKE ##");

				if (time_ret == ETIMEDOUT) {
					MSG_DEBUG("Network Connection Open Time Out.");
				}

				if(!isCmOpened) {
					MSG_DEBUG("Network Connection Open Failed");
				}

			} else { //error
				MSG_FATAL("Error!! net_open_connection_with_profile() failed. [%d]", netOpenResult);
			}

			if (isCmOpened == false) {
				context_invoke( __connection_destroy, NULL);
			}

		} else {
			MSG_FATAL("## Failed network callback registration ##");
		}
	} else {
		MSG_DEBUG("Network is already opened.");
	}

	unlock();
	MSG_END();
	return isCmOpened;
}


void MmsPluginCmAgent::close()
{
	MSG_BEGIN();

	lock();

	if (isCmOpened) {
		int netOpenResult = NET_ERR_NONE;

		context_invoke(__connection_profile_close, &netOpenResult);

		if (netOpenResult == NET_ERR_NONE) {

			MSG_DEBUG("## WAITING UNTIL Network Connection Close. ##");

			int time_ret = 0;

			time_ret = cv.timedwait(mx.pMutex(), MMS_CONNECTION_API_WAIT_TIME);

			MSG_DEBUG("## WAKE ##");

			if (time_ret == ETIMEDOUT) {
				MSG_DEBUG("Network Connection Close Timed Out.");
			}

		} else {
			MSG_DEBUG("Error!! net_close_connection() failed");
		}

		isCmOpened = false;
	} else {
		MSG_DEBUG ("Network Connection is not opened.");
	}

	if (isCmRegistered == true) {
		context_invoke(__connection_destroy, NULL);
		isCmRegistered = false;
	}

	unlock();

	MSG_END();
}

void MmsPluginCmAgent::open_callback(connection_error_e result, void* user_data)
{
	lock();

	connection_profile_h profile;
	connection_cellular_state_e state;
	int err;

	if (result == CONNECTION_ERROR_NONE || result == CONNECTION_ERROR_ALREADY_EXISTS) {

		err = connection_get_cellular_state(connection, &state);

		MSG_DEBUG("connection_get_cellular_state ret [%d], state [%d]", err, state);

		err = connection_get_default_cellular_service_profile(connection, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profile);
		if (err != CONNECTION_ERROR_NONE) {
			MSG_DEBUG("connection_get_default_cellular_service_profile Failed!! [%d]", err);
		}

		if (profile) {
			isCmOpened = true;

			MMS_FREE(this->home_url);
			MMS_FREE(this->interface_name);
			MMS_FREE(this->proxy_address);

			err = connection_profile_get_cellular_home_url(profile, &this->home_url);
			err = connection_profile_get_network_interface_name(profile, &this->interface_name);
			err = connection_profile_get_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &this->proxy_address);

			__connection_profile_print(profile);

			connection_profile_destroy(profile);
		}

	} else {
		MSG_DEBUG("connection open profile Failed!! [%d]", result);
	}

	MSG_DEBUG("## SIGNAL ##");
	signal();

	unlock();
}

void MmsPluginCmAgent::close_callback(connection_error_e result, void* user_data)
{
	lock();

	MMS_FREE(this->home_url);
	MMS_FREE(this->interface_name);
	MMS_FREE(this->proxy_address);

	isCmOpened = false;
	MSG_DEBUG("## SIGNAL ##");
	signal();

	unlock();
}

bool MmsPluginCmAgent::getInterfaceName(const char **deviceName)
{
	if (!isCmOpened)
		return false;

	if (deviceName == NULL)
		return false;

	*deviceName = interface_name;

	return true;
}

bool MmsPluginCmAgent::getHomeUrl(const char **homeURL)
{
	if (!isCmOpened)
		return false;

	if (homeURL == NULL)
		return false;

	*homeURL = home_url;

	return true;
}


bool MmsPluginCmAgent::getProxyAddr(const char **proxyAddr)
{
	if (!isCmOpened)
		return false;

	if (proxyAddr == NULL)
		return false;

	*proxyAddr = proxy_address;

	return true;
}