tester.c 37.3 KB
Newer Older
1
 /*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 tester - liblinphone test suite
 Copyright (C) 2013  Belledonne Communications SARL

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
20
#include <stdlib.h>
21
#include "linphone/core.h"
22 23
#include "linphone/logging.h"
#include "logging-private.h"
24
#include "liblinphone_tester.h"
25
#include <bctoolbox/tester.h>
Benjamin REIS's avatar
Benjamin REIS committed
26
#include "tester_utils.h"
27

28 29 30
#if _WIN32
#define unlink _unlink
#endif
31

Erwan Croze's avatar
Erwan Croze committed
32
#ifdef __ANDROID__
Erwan Croze's avatar
Erwan Croze committed
33
extern jobject system_context;
Erwan Croze's avatar
Erwan Croze committed
34 35 36
#else
void *system_context=0;
#endif
37

38
static int liblinphone_tester_keep_accounts_flag = 0;
39 40 41
static bool_t liblinphone_tester_keep_record_files = FALSE;
static bool_t liblinphone_tester_leak_detector_disabled = FALSE;
bool_t liblinphone_tester_keep_uuid = FALSE;
42
bool_t liblinphone_tester_tls_support_disabled = FALSE;
43
int manager_count = 0;
44
int leaked_objects_count = 0;
45 46
const MSAudioDiffParams audio_cmp_params = {10,2000};

47 48 49
const char* test_domain="sipopen.example.org";
const char* auth_domain="sip.example.org";
const char* test_username="liblinphone_tester";
50
const char* test_sha_username="liblinphone_sha_tester";
51 52 53
const char* test_password="secret";
const char* test_route="sip2.linphone.org";
const char *userhostsfile = "tester_hosts";
54 55
bool_t liblinphonetester_ipv6 = TRUE;
bool_t liblinphonetester_show_account_manager_logs = FALSE;
56 57 58
int liblinphonetester_transport_timeout = 9000; /*milliseconds. it is set to such low value to workaround a problem with our Freebox v6 when connecting to Ipv6 addresses.
			It was found that the freebox sometimes block SYN-ACK packets, which prevents connection to be succesful.
			Thanks to the timeout, it will fallback to IPv4*/
59

60
const char *liblinphone_tester_mire_id="Mire: Mire (synthetic moving picture)";
61
const char *liblinphone_tester_static_image_id="StaticImage: Static picture";
62

63
static void network_reachable(LinphoneCore *lc, bool_t reachable) {
64 65 66 67 68 69 70 71
	stats* counters;
	ms_message("Network reachable [%s]",reachable?"TRUE":"FALSE");
	counters = get_stats(lc);
	if (reachable)
		counters->number_of_NetworkReachableTrue++;
	else
		counters->number_of_NetworkReachableFalse++;
}
72 73 74 75 76 77 78 79 80 81 82 83
void liblinphone_tester_clock_start(MSTimeSpec *start){
	ms_get_cur_time(start);
}

bool_t liblinphone_tester_clock_elapsed(const MSTimeSpec *start, int value_ms){
	MSTimeSpec current;
	ms_get_cur_time(&current);
	if ((((current.tv_sec-start->tv_sec)*1000LL) + ((current.tv_nsec-start->tv_nsec)/1000000LL))>=value_ms)
		return TRUE;
	return FALSE;
}

84

85
LinphoneAddress * create_linphone_address(const char * domain) {
86 87 88 89
	return create_linphone_address_for_algo(domain,NULL);
}

LinphoneAddress * create_linphone_address_for_algo(const char * domain, const char* username) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103
	LinphoneAddress *addr = linphone_address_new(NULL);
	if (!BC_ASSERT_PTR_NOT_NULL(addr)) return NULL;
	/* For clients who support different algorithms, their usernames must be differnet for having diffrent forms of password */
	if (username) linphone_address_set_username(addr,username);
	else linphone_address_set_username(addr,test_username);
	if (username) BC_ASSERT_STRING_EQUAL(username, linphone_address_get_username(addr));
	else BC_ASSERT_STRING_EQUAL(test_username, linphone_address_get_username(addr));
	if (!domain) domain = test_route;
	linphone_address_set_domain(addr,domain);
	BC_ASSERT_STRING_EQUAL(domain, linphone_address_get_domain(addr));
	linphone_address_set_display_name(addr, NULL);
	linphone_address_set_display_name(addr, "Mr Tester");
	BC_ASSERT_STRING_EQUAL("Mr Tester", linphone_address_get_display_name(addr));
	return addr;
104 105
}

106
static void auth_info_requested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) {
107
	stats* counters;
108
	ms_message("Auth info requested  for user id [%s] at realm [%s]\n", username, realm);
109 110 111 112 113
	counters = get_stats(lc);
	counters->number_of_auth_info_requested++;
}

void reset_counters( stats* counters) {
114
	if (counters->last_received_chat_message) linphone_chat_message_unref(counters->last_received_chat_message);
115
	if (counters->last_received_info_message) linphone_info_message_unref(counters->last_received_info_message);
116 117
	if (counters->dtmf_list_received) bctbx_free(counters->dtmf_list_received);

118 119 120
	memset(counters,0,sizeof(stats));
}

121 122 123
LinphoneCore *configure_lc_from(LinphoneCoreCbs *cbs, const char *path, const char *file, void *user_data) {
	LinphoneCore *lc;
	LinphoneConfig *config = NULL;
124 125 126 127 128 129
	char *filepath         = NULL;
	char *ringpath         = NULL;
	char *ringbackpath     = NULL;
	char *rootcapath       = NULL;
	char *dnsuserhostspath = NULL;
	char *nowebcampath     = NULL;
130

131 132
	if (!path)
		path = ".";
133 134

	if (file){
135 136
		filepath = bctbx_strdup_printf("%s/%s", path, file);
		if (bctbx_file_exist(filepath) != 0) {
137 138
			ms_fatal("Could not find file %s in path %s, did you configured resources directory correctly?", file, path);
		}
139
		config = lp_config_new_with_factory(NULL, filepath);
140 141
	}

142 143 144 145 146
	// setup dynamic-path assets
	ringpath         = ms_strdup_printf("%s/sounds/oldphone.wav",path);
	ringbackpath     = ms_strdup_printf("%s/sounds/ringback.wav", path);
	nowebcampath     = ms_strdup_printf("%s/images/nowebcamCIF.jpg", path);
	rootcapath       = ms_strdup_printf("%s/certificates/cn/cafile.pem", path);
147
	dnsuserhostspath = userhostsfile[0]=='/' ? ms_strdup(userhostsfile) : ms_strdup_printf("%s/%s", path, userhostsfile);
148

149
	if (config) {
150 151 152
		lp_config_set_string(config, "sound", "remote_ring", ringbackpath);
		lp_config_set_string(config, "sound", "local_ring" , ringpath);
		lp_config_set_string(config, "sip",   "root_ca"    , rootcapath);
Erwan Croze's avatar
Erwan Croze committed
153
		lc = linphone_factory_create_core_with_config_3(linphone_factory_get(), config, system_context);
154
	} else {
Erwan Croze's avatar
Erwan Croze committed
155
		lc = linphone_factory_create_core_3(linphone_factory_get(), NULL, (filepath && (filepath[0] != '\0')) ? filepath : NULL, system_context);
156 157 158 159
		linphone_core_set_ring(lc, ringpath);
		linphone_core_set_ringback(lc, ringbackpath);
		linphone_core_set_root_ca(lc,rootcapath);
	}
160 161 162
	linphone_core_set_user_data(lc, user_data);
	if (cbs)
		linphone_core_add_callbacks(lc, cbs);
163

164
	linphone_core_enable_ipv6(lc, liblinphonetester_ipv6);
165
	linphone_core_set_sip_transport_timeout(lc, liblinphonetester_transport_timeout);
166

167 168
	sal_enable_test_features(linphone_core_get_sal(lc),TRUE);
	sal_set_dns_user_hosts_file(linphone_core_get_sal(lc), dnsuserhostspath);
169
#ifdef VIDEO_ENABLED
170
	linphone_core_set_static_picture(lc,nowebcampath);
171
#endif
172 173 174 175 176 177 178

	ms_free(ringpath);
	ms_free(ringbackpath);
	ms_free(nowebcampath);
	ms_free(rootcapath);
	ms_free(dnsuserhostspath);

179 180
	if (filepath)
		bctbx_free(filepath);
181

182 183
	if (config)
		linphone_config_unref(config);
184

185 186 187 188 189
	return lc;
}


bool_t wait_for_until(LinphoneCore* lc_1, LinphoneCore* lc_2,int* counter,int value,int timout) {
190
	bctbx_list_t* lcs=NULL;
191 192
	bool_t result;
	if (lc_1)
193
		lcs=bctbx_list_append(lcs,lc_1);
194
	if (lc_2)
195
		lcs=bctbx_list_append(lcs,lc_2);
196
	result=wait_for_list(lcs,counter,value,timout);
197
	bctbx_list_free(lcs);
198 199 200 201
	return result;
}

bool_t wait_for(LinphoneCore* lc_1, LinphoneCore* lc_2,int* counter,int value) {
202
	return wait_for_until(lc_1, lc_2,counter,value,10000);
203 204
}

205 206
bool_t wait_for_list(bctbx_list_t* lcs,int* counter,int value,int timeout_ms) {
	bctbx_list_t* iterator;
207
	MSTimeSpec start;
208

209 210 211 212 213
	liblinphone_tester_clock_start(&start);
	while ((counter==NULL || *counter<value) && !liblinphone_tester_clock_elapsed(&start,timeout_ms)) {
		for (iterator=lcs;iterator!=NULL;iterator=iterator->next) {
			linphone_core_iterate((LinphoneCore*)(iterator->data));
		}
214
#ifdef LINPHONE_WINDOWS_DESKTOP
215 216 217 218 219 220 221 222
		{
			MSG msg;
			while (PeekMessage(&msg, NULL, 0, 0,1)){
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
#endif
223
		ms_usleep(20000);
224 225 226 227 228
	}
	if(counter && *counter<value) return FALSE;
	else return TRUE;
}

229 230 231 232
bool_t wait_for_stun_resolution(LinphoneCoreManager *m) {
	MSTimeSpec start;
	int timeout_ms = 10000;
	liblinphone_tester_clock_start(&start);
Ghislain MARY's avatar
Ghislain MARY committed
233
	while (linphone_core_get_stun_server_addrinfo(m->lc) == NULL && !liblinphone_tester_clock_elapsed(&start,timeout_ms)) {
234 235 236
		linphone_core_iterate(m->lc);
		ms_usleep(20000);
	}
Ghislain MARY's avatar
Ghislain MARY committed
237
	return linphone_core_get_stun_server_addrinfo(m->lc) != NULL;
238 239
}

240
static void set_codec_enable(LinphoneCore* lc,const char* type,int rate,bool_t enable) {
241 242
	bctbx_list_t* codecs=bctbx_list_copy(linphone_core_get_audio_codecs(lc));
	bctbx_list_t* codecs_it;
243 244
	PayloadType* pt;
	for (codecs_it=codecs;codecs_it!=NULL;codecs_it=codecs_it->next) {
245
		linphone_core_enable_payload_type(lc,(PayloadType*)codecs_it->data,0);
246
	}
247
	if ((pt = linphone_core_find_payload_type(lc,type,rate,1))) {
248 249
		linphone_core_enable_payload_type(lc,pt, enable);
	}
250
	bctbx_list_free(codecs);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
}

static void enable_codec(LinphoneCore* lc,const char* type,int rate) {
	set_codec_enable(lc,type,rate,TRUE);
}
stats * get_stats(LinphoneCore *lc){
	LinphoneCoreManager *manager=(LinphoneCoreManager *)linphone_core_get_user_data(lc);
	return &manager->stat;
}

LinphoneCoreManager *get_manager(LinphoneCore *lc){
	LinphoneCoreManager *manager=(LinphoneCoreManager *)linphone_core_get_user_data(lc);
	return manager;
}

266
bool_t transport_supported(LinphoneTransportType transport) {
267 268 269 270 271 272 273 274 275
	if ((transport == LinphoneTransportDtls || transport == LinphoneTransportTls) && liblinphone_tester_tls_support_disabled == TRUE) {
		return FALSE;
	} else {
		Sal *sal = sal_init(NULL);
		bool_t supported = sal_transport_available(sal,(SalTransport)transport);
		if (!supported) ms_message("TLS transport not supported, falling back to TCP if possible otherwise skipping test.");
		sal_uninit(sal);
		return  supported;
	}
276 277
}

278
void linphone_core_manager_configure (LinphoneCoreManager *mgr) {
279
	LinphoneImNotifPolicy *im_notif_policy;
jehan's avatar
jehan committed
280
	char *hellopath = bc_tester_res("sounds/hello8000.wav");
281

282
	mgr->lc = configure_lc_from(mgr->cbs, bc_tester_get_resource_dir_prefix(), mgr->rc_path, mgr);
283
	linphone_core_manager_check_accounts(mgr);
284
	im_notif_policy = linphone_core_get_im_notif_policy(mgr->lc);
285 286 287 288 289 290
	if (im_notif_policy != NULL) {
		/* The IM notification policy can be NULL at this point in case of remote provisioning. */
		linphone_im_notif_policy_clear(im_notif_policy);
		linphone_im_notif_policy_set_send_is_composing(im_notif_policy, TRUE);
		linphone_im_notif_policy_set_recv_is_composing(im_notif_policy, TRUE);
	}
291

Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
292
#if TARGET_OS_IPHONE
293 294
	linphone_core_set_ringer_device( mgr->lc, "AQ: Audio Queue Device");
	linphone_core_set_ringback(mgr->lc, NULL);
295 296
#elif __QNX__
	linphone_core_set_playback_device(mgr->lc, "QSA: voice");
297 298
#endif

299 300 301 302
#ifdef VIDEO_ENABLED
	{
		MSWebCam *cam;

303
		cam = ms_web_cam_manager_get_cam(ms_factory_get_web_cam_manager(linphone_core_get_ms_factory(mgr->lc)), "Mire: Mire (synthetic moving picture)");
304 305

		if (cam == NULL) {
Simon Morlat's avatar
Simon Morlat committed
306 307 308
			MSWebCamDesc *desc = ms_mire_webcam_desc_get();
			if (desc){
				cam=ms_web_cam_new(desc);
309
				ms_web_cam_manager_add_cam(ms_factory_get_web_cam_manager(linphone_core_get_ms_factory(mgr->lc)), cam);
Simon Morlat's avatar
Simon Morlat committed
310
			}
311 312 313 314
		}
	}
#endif

jehan's avatar
jehan committed
315

316 317
	linphone_core_set_play_file(mgr->lc,hellopath); /*is also used when in pause*/
	ms_free(hellopath);
318

319
	if( manager_count >= 2){
320
		char *recordpath = ms_strdup_printf("%s/record_for_lc_%p.wav",bc_tester_get_writable_dir_prefix(),mgr->lc);
321
		ms_message("Manager for '%s' using files", mgr->rc_path ? mgr->rc_path : "--");
322
		linphone_core_set_use_files(mgr->lc, TRUE);
323 324
		linphone_core_set_record_file(mgr->lc,recordpath);
		ms_free(recordpath);
325
	}
326

327
	linphone_core_set_user_certificates_path(mgr->lc,bc_tester_get_writable_dir_prefix());
328
	/*for now, we need the periodical updates facility to compute bandwidth measurements correctly during tests*/
329
	linphone_core_enable_send_call_stats_periodical_updates(mgr->lc, TRUE);
330

331
	LinphoneConfig *config = linphone_core_get_config(mgr->lc);
332 333 334
	linphone_config_set_string(config, "storage", "backend", "sqlite3");
	linphone_config_set_string(config, "storage", "uri", mgr->database_path);
}
335

336
static void generate_random_database_path (LinphoneCoreManager *mgr) {
337 338 339 340 341 342 343
	char random_id[32];
	belle_sip_random_token(random_id, sizeof random_id);
	char *database_path_format = bctbx_strdup_printf("linphone_%s.db", random_id);
	mgr->database_path = bc_tester_file(database_path_format);
	bctbx_free(database_path_format);
}

344 345 346 347 348 349 350 351 352 353
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic push
#endif
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
void linphone_core_manager_init(LinphoneCoreManager *mgr, const char* rc_file, const char* phone_alias) {
	mgr->number_of_bcunit_error_at_creation =  bc_get_number_of_failures();
354 355 356 357 358 359 360 361 362 363 364 365 366
	mgr->cbs = linphone_factory_create_core_cbs(linphone_factory_get());
	linphone_core_cbs_set_registration_state_changed(mgr->cbs, registration_state_changed);
	linphone_core_cbs_set_auth_info_requested(mgr->cbs, auth_info_requested);
	linphone_core_cbs_set_call_state_changed(mgr->cbs, call_state_changed);
	linphone_core_cbs_set_message_received(mgr->cbs, message_received);
	linphone_core_cbs_set_is_composing_received(mgr->cbs, is_composing_received);
	linphone_core_cbs_set_new_subscription_requested(mgr->cbs, new_subscription_requested);
	linphone_core_cbs_set_notify_presence_received(mgr->cbs, notify_presence_received);
	linphone_core_cbs_set_notify_presence_received_for_uri_or_tel(mgr->cbs, notify_presence_received_for_uri_or_tel);
	linphone_core_cbs_set_transfer_state_changed(mgr->cbs, linphone_transfer_state_changed);
	linphone_core_cbs_set_info_received(mgr->cbs, info_message_received);
	linphone_core_cbs_set_subscription_state_changed(mgr->cbs, linphone_subscription_state_change);
	linphone_core_cbs_set_notify_received(mgr->cbs, linphone_notify_received);
367
	linphone_core_cbs_set_subscribe_received(mgr->cbs, linphone_subscribe_received);
368 369 370 371 372 373
	linphone_core_cbs_set_publish_state_changed(mgr->cbs, linphone_publish_state_changed);
	linphone_core_cbs_set_configuring_status(mgr->cbs, linphone_configuration_status);
	linphone_core_cbs_set_call_encryption_changed(mgr->cbs, linphone_call_encryption_changed);
	linphone_core_cbs_set_network_reachable(mgr->cbs, network_reachable);
	linphone_core_cbs_set_dtmf_received(mgr->cbs, dtmf_received);
	linphone_core_cbs_set_call_stats_updated(mgr->cbs, call_stats_updated);
374 375 376 377

	mgr->phone_alias = phone_alias ? ms_strdup(phone_alias) : NULL;

	reset_counters(&mgr->stat);
378
	if (rc_file) mgr->rc_path = ms_strdup_printf("rcfiles/%s", rc_file);
379 380 381

	manager_count++;

382
	generate_random_database_path(mgr);
383
	linphone_core_manager_configure(mgr);
384
}
385 386 387
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic pop
#endif
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
388

389
void linphone_core_manager_start(LinphoneCoreManager *mgr, bool_t check_for_proxies) {
390 391
	LinphoneProxyConfig* proxy;
	int proxy_count;
392

393 394
	linphone_core_start(mgr->lc);

395
	/*BC_ASSERT_EQUAL(bctbx_list_size(linphone_core_get_proxy_config_list(lc)),proxy_count, int, "%d");*/
396
	if (check_for_proxies){ /**/
397
		proxy_count=(int)bctbx_list_size(linphone_core_get_proxy_config_list(mgr->lc));
398
	}else{
399
		proxy_count=0;
400 401 402
		/*this is to prevent registration to go on*/
		linphone_core_set_network_reachable(mgr->lc, FALSE);
	}
403

404 405 406
	if (proxy_count){
#define REGISTER_TIMEOUT 20 /* seconds */
		int success = wait_for_until(mgr->lc,NULL,&mgr->stat.number_of_LinphoneRegistrationOk,
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
407
									proxy_count,(REGISTER_TIMEOUT * 1000 * proxy_count));
408 409 410 411
		if( !success ){
			ms_error("Did not register after %d seconds for %d proxies", REGISTER_TIMEOUT, proxy_count);
		}
	}
412
	BC_ASSERT_EQUAL(mgr->stat.number_of_LinphoneRegistrationOk,proxy_count, int, "%d");
413 414
	enable_codec(mgr->lc,"PCMU",8000);

415
	proxy = linphone_core_get_default_proxy_config(mgr->lc);
416
	if (proxy) {
417
		if (mgr->identity){
Simon Morlat's avatar
Simon Morlat committed
418
			linphone_address_unref(mgr->identity);
419
		}
420
		mgr->identity = linphone_address_clone(linphone_proxy_config_get_identity_address(proxy));
421 422
		linphone_address_clean(mgr->identity);
	}
423

424
	linphone_core_manager_wait_for_stun_resolution(mgr);
425 426 427 428
	if (!check_for_proxies){
		/*now that stun server resolution is done, we can start registering*/
		linphone_core_set_network_reachable(mgr->lc, TRUE);
	}
429

430 431
}

432
LinphoneCoreManager* linphone_core_manager_create2(const char* rc_file, const char* phone_alias) {
433
	LinphoneCoreManager *manager = ms_new0(LinphoneCoreManager, 1);
434
	linphone_core_manager_init(manager, rc_file, phone_alias);
435 436 437 438 439 440 441
	return manager;
}

LinphoneCoreManager* linphone_core_manager_create(const char* rc_file) {
	return linphone_core_manager_create2(rc_file, NULL);
}

442 443 444
LinphoneCoreManager* linphone_core_manager_new4(const char* rc_file, int check_for_proxies, const char* phone_alias, const char* contact_params, int expires) {
	/* This function is for testing purposes. */
	LinphoneCoreManager *manager = ms_new0(LinphoneCoreManager, 1);
445

446 447 448 449 450 451 452
	linphone_core_manager_init(manager, rc_file, phone_alias);
	linphone_proxy_config_set_contact_parameters(linphone_core_get_default_proxy_config(manager->lc), contact_params);
	linphone_proxy_config_set_expires(linphone_core_get_default_proxy_config(manager->lc), expires);
	linphone_core_manager_start(manager, check_for_proxies);
	return manager;
}

453
LinphoneCoreManager* linphone_core_manager_new3(const char* rc_file, bool_t check_for_proxies, const char* phone_alias) {
454
	LinphoneCoreManager *manager = linphone_core_manager_create2(rc_file, phone_alias);
455
	linphone_core_manager_start(manager, check_for_proxies);
456 457 458
	return manager;
}

459
LinphoneCoreManager* linphone_core_manager_new2(const char* rc_file, bool_t check_for_proxies) {
460
	return linphone_core_manager_new3(rc_file, check_for_proxies, NULL);
461 462
}

463 464 465 466 467
LinphoneCoreManager* linphone_core_manager_new( const char* rc_file) {
	return linphone_core_manager_new2(rc_file, TRUE);
}


468 469
void linphone_core_manager_stop(LinphoneCoreManager *mgr){
	if (mgr->lc) {
470
		const char *record_file = linphone_core_get_record_file(mgr->lc);
471
		if (!liblinphone_tester_keep_record_files && record_file && ortp_file_exist(record_file)==0) {
472 473 474 475 476 477 478
			if ((bc_get_number_of_failures() - mgr->number_of_bcunit_error_at_creation)>0) {
				ms_error("Test has failed, keeping recorded file [%s]", record_file);
			}
			else {
				unlink(record_file);
			}
		}
479
		linphone_core_unref(mgr->lc);
480
		mgr->lc = NULL;
481 482 483
	}
}

484 485
void linphone_core_manager_reinit(LinphoneCoreManager *mgr) {
	char *uuid = NULL;
486
	if (mgr->lc) {
487 488
		if (lp_config_get_string(linphone_core_get_config(mgr->lc), "misc", "uuid", NULL))
			uuid = bctbx_strdup(lp_config_get_string(linphone_core_get_config(mgr->lc), "misc", "uuid", NULL));
489
		linphone_core_unref(mgr->lc);
490
	}
491 492
	linphone_core_manager_configure(mgr);
	reset_counters(&mgr->stat);
493 494
	// Make sure gruu is preserved
	lp_config_set_string(linphone_core_get_config(mgr->lc), "misc", "uuid", uuid);
495
	if (uuid)
496
		bctbx_free(uuid);
497 498 499
}

void linphone_core_manager_restart(LinphoneCoreManager *mgr, bool_t check_for_proxies) {
500
	linphone_core_manager_reinit(mgr);
501 502 503
	linphone_core_manager_start(mgr, check_for_proxies);
}

504
void linphone_core_manager_uninit(LinphoneCoreManager *mgr) {
505
	int old_log_level = linphone_core_get_log_level_mask();
506
	linphone_core_set_log_level(ORTP_ERROR);
507 508 509
	if (mgr->phone_alias) {
		ms_free(mgr->phone_alias);
	}
510
	if (mgr->identity) {
Simon Morlat's avatar
Simon Morlat committed
511
		linphone_address_unref(mgr->identity);
512
	}
513
	if (mgr->rc_path)
Ghislain MARY's avatar
Ghislain MARY committed
514
		bctbx_free(mgr->rc_path);
515 516 517 518 519
	if (mgr->database_path) {
		unlink(mgr->database_path);
		bctbx_free(mgr->database_path);
	}

520 521
	if (mgr->cbs)
		linphone_core_cbs_unref(mgr->cbs);
522

523 524
	reset_counters(&mgr->stat);

525
	manager_count--;
526
	linphone_core_set_log_level_mask(old_log_level);
527
}
528

529 530 531 532 533 534 535 536 537 538
void linphone_core_manager_wait_for_stun_resolution(LinphoneCoreManager *mgr) {
	LinphoneNatPolicy *nat_policy = linphone_core_get_nat_policy(mgr->lc);
	if ((nat_policy != NULL) && (linphone_nat_policy_get_stun_server(nat_policy) != NULL) &&
		(linphone_nat_policy_stun_enabled(nat_policy) || linphone_nat_policy_turn_enabled(nat_policy)) &&
		(linphone_nat_policy_ice_enabled(nat_policy))) {
		/*before we go, ensure that the stun server is resolved, otherwise all ice related test will fail*/
		BC_ASSERT_TRUE(wait_for_stun_resolution(mgr));
	}
}

539
void linphone_core_manager_destroy(LinphoneCoreManager* mgr) {
jehan's avatar
jehan committed
540
	if (mgr->lc && !linphone_core_is_network_reachable(mgr->lc)) {
541 542 543 544
		int previousNbRegistrationOk = mgr->stat.number_of_LinphoneRegistrationOk;
		linphone_core_set_network_reachable(mgr->lc, TRUE);
		wait_for_until(mgr->lc, NULL, &mgr->stat.number_of_LinphoneRegistrationOk, previousNbRegistrationOk + 1, 2000);
	}
545
	linphone_core_manager_stop(mgr);
546
	linphone_core_manager_uninit(mgr);
547 548 549
	ms_free(mgr);
}

550 551 552 553 554 555
void linphone_core_manager_delete_chat_room (LinphoneCoreManager *mgr, LinphoneChatRoom *cr, bctbx_list_t *coresList) {
	stats mgrStats = mgr->stat;
	linphone_core_delete_chat_room(mgr->lc, cr);
	BC_ASSERT_TRUE(wait_for_list(coresList, &mgr->stat.number_of_LinphoneChatRoomStateDeleted, mgrStats.number_of_LinphoneChatRoomStateDeleted + 1, 10000));
}

556
int liblinphone_tester_ipv6_available(void){
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	if (liblinphonetester_ipv6) {
		struct addrinfo *ai=bctbx_ip_address_to_addrinfo(AF_INET6,SOCK_STREAM,"2a01:e00::2",53);
		if (ai){
			struct sockaddr_storage ss;
			struct addrinfo src;
			socklen_t slen=sizeof(ss);
			char localip[128];
			int port=0;
			belle_sip_get_src_addr_for(ai->ai_addr,(socklen_t)ai->ai_addrlen,(struct sockaddr*) &ss,&slen,4444);
			src.ai_addr=(struct sockaddr*) &ss;
			src.ai_addrlen=slen;
			bctbx_addrinfo_to_ip_address(&src,localip, sizeof(localip),&port);
			freeaddrinfo(ai);
			return strcmp(localip,"::1")!=0;
		}
572 573 574 575
	}
	return FALSE;
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
int liblinphone_tester_ipv4_available(void){
	struct addrinfo *ai=bctbx_ip_address_to_addrinfo(AF_INET,SOCK_STREAM,"212.27.40.240",53);
	if (ai){
		struct sockaddr_storage ss;
		struct addrinfo src;
		socklen_t slen=sizeof(ss);
		char localip[128];
		int port=0;
		belle_sip_get_src_addr_for(ai->ai_addr,(socklen_t)ai->ai_addrlen,(struct sockaddr*) &ss,&slen,4444);
		src.ai_addr=(struct sockaddr*) &ss;
		src.ai_addrlen=slen;
		bctbx_addrinfo_to_ip_address(&src,localip, sizeof(localip),&port);
		freeaddrinfo(ai);
		return strcmp(localip,"127.0.0.1")!=0;
	}
	return FALSE;
}


595 596 597 598
void liblinphone_tester_keep_accounts( int keep ){
	liblinphone_tester_keep_accounts_flag = keep;
}

599 600 601 602
void liblinphone_tester_keep_recorded_files(int keep){
	liblinphone_tester_keep_record_files = keep;
}

603 604 605 606
void liblinphone_tester_disable_leak_detector(int disabled){
	liblinphone_tester_leak_detector_disabled = disabled;
}

607 608 609
void liblinphone_tester_clear_accounts(void){
	account_manager_destroy();
}
610 611 612 613

void liblinphone_tester_add_suites() {
	bc_tester_add_suite(&setup_test_suite);
	bc_tester_add_suite(&register_test_suite);
614
	bc_tester_add_suite(&tunnel_test_suite);
615 616
	bc_tester_add_suite(&offeranswer_test_suite);
	bc_tester_add_suite(&call_test_suite);
617 618 619
	#ifdef VIDEO_ENABLED
		bc_tester_add_suite(&call_video_test_suite);
	#endif // ifdef VIDEO_ENABLED
620
	bc_tester_add_suite(&audio_bypass_suite);
621 622 623
	bc_tester_add_suite(&multi_call_test_suite);
	bc_tester_add_suite(&message_test_suite);
	bc_tester_add_suite(&presence_test_suite);
624
	bc_tester_add_suite(&presence_server_test_suite);
Erwan Croze's avatar
Erwan Croze committed
625
	bc_tester_add_suite(&account_creator_test_suite);
626 627
	bc_tester_add_suite(&stun_test_suite);
	bc_tester_add_suite(&event_test_suite);
628
	bc_tester_add_suite(&conference_event_test_suite);
629
	bc_tester_add_suite(&contents_test_suite);
630 631 632 633 634 635
	bc_tester_add_suite(&flexisip_test_suite);
	bc_tester_add_suite(&remote_provisioning_test_suite);
	bc_tester_add_suite(&quality_reporting_test_suite);
	bc_tester_add_suite(&log_collection_test_suite);
	bc_tester_add_suite(&player_test_suite);
	bc_tester_add_suite(&dtmf_test_suite);
Ronan's avatar
Ronan committed
636
	bc_tester_add_suite(&cpim_test_suite);
637
	bc_tester_add_suite(&multipart_test_suite);
638
	bc_tester_add_suite(&clonable_object_test_suite);
Ronan's avatar
Ronan committed
639
	bc_tester_add_suite(&main_db_test_suite);
640
	bc_tester_add_suite(&property_container_test_suite);
641 642 643
	#ifdef VIDEO_ENABLED
		bc_tester_add_suite(&video_test_suite);
	#endif // ifdef VIDEO_ENABLED
Ghislain MARY's avatar
Ghislain MARY committed
644
	bc_tester_add_suite(&multicast_call_test_suite);
645
	bc_tester_add_suite(&proxy_config_test_suite);
jehan's avatar
jehan committed
646 647 648
#if HAVE_SIPP
	bc_tester_add_suite(&complex_sip_call_test_suite);
#endif
649
#ifdef VCARD_ENABLED
650
	bc_tester_add_suite(&vcard_test_suite);
651
#endif
652
	bc_tester_add_suite(&group_chat_test_suite);
653
}
654

655
static int linphone_core_manager_get_max_audio_bw_base(const int array[],int array_size) {
656 657 658 659 660 661 662
	int i,result=0;
	for (i=0; i<array_size; i++) {
		result = MAX(result,array[i]);
	}
	return result;
}

663 664 665 666 667 668 669 670
static int linphone_core_manager_get_mean_audio_bw_base(const int array[],int array_size) {
	int i,result=0;
	for (i=0; i<array_size; i++) {
		result += array[i];
	}
	return result/array_size;
}

671 672 673 674 675 676 677 678
int linphone_core_manager_get_max_audio_down_bw(const LinphoneCoreManager *mgr) {
	return linphone_core_manager_get_max_audio_bw_base(mgr->stat.audio_download_bandwidth
			, sizeof(mgr->stat.audio_download_bandwidth)/sizeof(int));
}
int linphone_core_manager_get_max_audio_up_bw(const LinphoneCoreManager *mgr) {
	return linphone_core_manager_get_max_audio_bw_base(mgr->stat.audio_upload_bandwidth
			, sizeof(mgr->stat.audio_upload_bandwidth)/sizeof(int));
}
679

680 681 682 683 684 685 686 687 688
int linphone_core_manager_get_mean_audio_down_bw(const LinphoneCoreManager *mgr) {
	return linphone_core_manager_get_mean_audio_bw_base(mgr->stat.audio_download_bandwidth
			, sizeof(mgr->stat.audio_download_bandwidth)/sizeof(int));
}
int linphone_core_manager_get_mean_audio_up_bw(const LinphoneCoreManager *mgr) {
	return linphone_core_manager_get_mean_audio_bw_base(mgr->stat.audio_upload_bandwidth
			, sizeof(mgr->stat.audio_upload_bandwidth)/sizeof(int));
}

689
void liblinphone_tester_before_each(void) {
690 691 692 693
	if (!liblinphone_tester_leak_detector_disabled){
		belle_sip_object_enable_leak_detector(TRUE);
		leaked_objects_count = belle_sip_object_get_object_count();
	}
694 695
}

696 697
static char* all_leaks_buffer = NULL;

698
void liblinphone_tester_after_each(void) {
Simon Morlat's avatar
Simon Morlat committed
699
	linphone_factory_clean();
700 701 702
	if (!liblinphone_tester_leak_detector_disabled){
		int leaked_objects = belle_sip_object_get_object_count() - leaked_objects_count;
		if (leaked_objects > 0) {
703
			char* format = ms_strdup_printf("%d object%s leaked in suite [%s] test [%s], please fix that!",
704
											leaked_objects, leaked_objects>1?"s were":" was",
705
											bc_tester_current_suite_name(), bc_tester_current_test_name());
706
			belle_sip_object_dump_active_objects();
707
			belle_sip_object_flush_active_objects();
708
			bc_tester_printf(ORTP_MESSAGE, format);
709 710 711
			ms_error("%s", format);

			all_leaks_buffer = ms_strcat_printf(all_leaks_buffer, "\n%s", format);
Ghislain MARY's avatar
Ghislain MARY committed
712
			ms_free(format);
713
		}
714

715 716 717 718 719 720 721 722 723 724 725
		// prevent any future leaks
		{
			const char **tags = bc_tester_current_test_tags();
			int leaks_expected =
				(tags && ((tags[0] && !strcmp(tags[0], "LeaksMemory")) || (tags[1] && !strcmp(tags[1], "LeaksMemory"))));
			// if the test is NOT marked as leaking memory and it actually is, we should make it fail
			if (!leaks_expected && leaked_objects > 0) {
				BC_FAIL("This test is leaking memory!");
				// and reciprocally
			} else if (leaks_expected && leaked_objects == 0) {
				BC_FAIL("This test is not leaking anymore, please remove LeaksMemory tag!");
726
			}
727
		}
728 729
	}

730
	if (manager_count != 0) {
731
		ms_fatal("%d Linphone core managers are still alive!", manager_count);
732 733
	}
}
734

735 736 737
void liblinphone_tester_uninit(void) {
	// show all leaks that happened during the test
	if (all_leaks_buffer) {
738
		bc_tester_printf(ORTP_MESSAGE, all_leaks_buffer);
739
		ms_free(all_leaks_buffer);
740
		all_leaks_buffer = NULL;
741 742
	}
	bc_tester_uninit();
743
	bctbx_uninit_logger();
744
}
745

746
static void check_ice_from_rtp(LinphoneCall *c1, LinphoneCall *c2, LinphoneStreamType stream_type) {
747
	MediaStream *ms;
748
	LinphoneCallStats *stats;
749 750
	switch (stream_type) {
	case LinphoneStreamTypeAudio:
751
		ms=linphone_call_get_stream(c1, LinphoneStreamTypeAudio);
752
		break;
753
	case LinphoneStreamTypeVideo:
754
		ms=linphone_call_get_stream(c1, LinphoneStreamTypeVideo);
755 756
		break;
	case LinphoneStreamTypeText:
757
		ms=linphone_call_get_stream(c1, LinphoneStreamTypeText);
758 759 760 761 762
		break;
	default:
		ms_error("Unknown stream type [%s]",  linphone_stream_type_to_string(stream_type));
		BC_ASSERT_FALSE(stream_type >= LinphoneStreamTypeUnknown);
		return;
763
	}
764

765
	stats = linphone_call_get_audio_stats(c1);
766
	if (linphone_call_stats_get_ice_state(stats) == LinphoneIceStateHostConnection && media_stream_started(ms)) {
767 768 769 770 771 772
		struct sockaddr_storage remaddr;
		socklen_t remaddrlen = sizeof(remaddr);
		char ip[NI_MAXHOST] = { 0 };
		int port = 0;
		SalMediaDescription *result_desc;
		char *expected_addr = NULL;
773
		AudioStream *astream;
774 775 776

		const LinphoneCallParams *cp1 = linphone_call_get_current_params(c1);
		const LinphoneCallParams *cp2 = linphone_call_get_current_params(c2);
777
		if (linphone_call_params_get_update_call_when_ice_completed(cp1) && linphone_call_params_get_update_call_when_ice_completed(cp2)) {
778
			memset(&remaddr, 0, remaddrlen);
779
			result_desc = sal_call_get_final_media_description(linphone_call_get_op_as_sal_op(c2));
780 781
			expected_addr = result_desc->streams[0].rtp_addr;
			if (expected_addr[0] == '\0') expected_addr = result_desc->addr;
782 783 784
			astream = (AudioStream *)linphone_call_get_stream(c1, LinphoneStreamTypeAudio);
			if ((strchr(expected_addr, ':') == NULL) && (astream->ms.sessions.rtp_session->rtp.gs.rem_addr.ss_family == AF_INET6)) {
				bctbx_sockaddr_ipv6_to_ipv4((struct sockaddr *)&astream->ms.sessions.rtp_session->rtp.gs.rem_addr, (struct sockaddr *)&remaddr, &remaddrlen);
785
			} else {
786
				memcpy(&remaddr, &astream->ms.sessions.rtp_session->rtp.gs.rem_addr, astream->ms.sessions.rtp_session->rtp.gs.rem_addrlen);
787 788 789 790 791
			}
			bctbx_sockaddr_to_ip_address((struct sockaddr *)&remaddr, remaddrlen, ip, sizeof(ip), &port);

			BC_ASSERT_STRING_EQUAL(ip, expected_addr);
		}
792
	}
793
	linphone_call_stats_unref(stats);
794
}
795

796 797
bool_t check_ice(LinphoneCoreManager* caller, LinphoneCoreManager* callee, LinphoneIceState state) {
	LinphoneCall *c1,*c2;
798
	bool_t global_success = TRUE;
799 800 801
	bool_t audio_success=FALSE;
	bool_t video_success=FALSE;
	bool_t text_success=FALSE;
802
	bool_t audio_enabled, video_enabled, realtime_text_enabled;
803
	MSTimeSpec ts;
804

805 806
	c1=linphone_core_get_current_call(caller->lc);
	c2=linphone_core_get_current_call(callee->lc);
807

808 809 810 811 812
	BC_ASSERT_PTR_NOT_NULL(c1);
	BC_ASSERT_PTR_NOT_NULL(c2);
	if (!c1 || !c2) return FALSE;
	linphone_call_ref(c1);
	linphone_call_ref(c2);
813

814 815
	BC_ASSERT_EQUAL(linphone_call_params_video_enabled(linphone_call_get_current_params(c1)),linphone_call_params_video_enabled(linphone_call_get_current_params(c2)), int, "%d");
	BC_ASSERT_EQUAL(linphone_call_params_realtime_text_enabled(linphone_call_get_current_params(c1)),linphone_call_params_realtime_text_enabled(linphone_call_get_current_params(c2)), int, "%d");
816
	audio_enabled=linphone_call_params_audio_enabled(linphone_call_get_current_params(c1));
817 818
	video_enabled=linphone_call_params_video_enabled(linphone_call_get_current_params(c1));
	realtime_text_enabled=linphone_call_params_realtime_text_enabled(linphone_call_get_current_params(c1));
819 820
	if (audio_enabled) {
		liblinphone_tester_clock_start(&ts);
Ghislain MARY's avatar
Ghislain MARY committed
821 822 823
		LinphoneCallStats *stats1 = NULL;
		LinphoneCallStats *stats2 = NULL;
		do {
824
			if ((c1 != NULL) && (c2 != NULL)) {
Ghislain MARY's avatar
Ghislain MARY committed
825 826
				stats1 = linphone_call_get_audio_stats(c1);
				stats2 = linphone_call_get_audio_stats(c2);
827 828
				if (linphone_call_stats_get_ice_state(stats1)==state &&
					linphone_call_stats_get_ice_state(stats2)==state){
829 830 831 832 833 834 835
					audio_success=TRUE;
					check_ice_from_rtp(c1,c2,LinphoneStreamTypeAudio);
					check_ice_from_rtp(c2,c1,LinphoneStreamTypeAudio);
					break;
				}
				linphone_core_iterate(caller->lc);
				linphone_core_iterate(callee->lc);
Ghislain MARY's avatar
Ghislain MARY committed
836 837 838
				linphone_call_stats_unref(stats1);
				linphone_call_stats_unref(stats2);
				stats1 = stats2 = NULL;
839
			}
840
			ms_usleep(20000);
Ghislain MARY's avatar
Ghislain MARY committed
841 842 843 844 845
		} while (!liblinphone_tester_clock_elapsed(&ts,10000));
		if (stats1)
			linphone_call_stats_unref(stats1);
		if (stats2)
			linphone_call_stats_unref(stats2);
846
	}
847

848 849
	if (video_enabled){
		liblinphone_tester_clock_start(&ts);
850 851 852
		LinphoneCallStats *stats1 = NULL;
		LinphoneCallStats *stats2 = NULL;
		do {
853
			if ((c1 != NULL) && (c2 != NULL)) {
854 855
				stats1 = linphone_call_get_video_stats(c1);
				stats2 = linphone_call_get_video_stats(c2);
856 857
				if (linphone_call_stats_get_ice_state(stats1)==state &&
					linphone_call_stats_get_ice_state(stats2)==state){
858
					video_success=TRUE;
859 860
					check_ice_from_rtp(c1,c2,LinphoneStreamTypeVideo);
					check_ice_from_rtp(c2,c1,LinphoneStreamTypeVideo);
861 862 863 864
					break;
				}
				linphone_core_iterate(caller->lc);
				linphone_core_iterate(callee->lc);
865 866
				linphone_call_stats_unref(stats1);
				linphone_call_stats_unref(stats2);
jehan's avatar
jehan committed
867
				stats1 = stats2 = NULL;
868 869
			}
			ms_usleep(20000);
870 871 872 873 874
		} while (!liblinphone_tester_clock_elapsed(&ts,10000));
		if (stats1)
			linphone_call_stats_unref(stats1);
		if (stats2)
			linphone_call_stats_unref(stats2);
875
	}
876

877 878
	if (realtime_text_enabled){
		liblinphone_tester_clock_start(&ts);
879 880 881
		LinphoneCallStats *stats1 = NULL;
		LinphoneCallStats *stats2 = NULL;
		do {
882
			if ((c1 != NULL) && (c2 != NULL)) {
883 884
				stats1 = linphone_call_get_text_stats(c1);
				stats2 = linphone_call_get_text_stats(c2);
885 886
				if (linphone_call_stats_get_ice_state(stats1)==state &&
					linphone_call_stats_get_ice_state(stats2)==state){
887
					text_success=TRUE;
888 889
					check_ice_from_rtp(c1,c2,LinphoneStreamTypeText);
					check_ice_from_rtp(c2,c1,LinphoneStreamTypeText);
890 891 892 893
					break;
				}
				linphone_core_iterate(caller->lc);
				linphone_core_iterate(callee->lc);
894 895
				linphone_call_stats_unref(stats1);
				linphone_call_stats_unref(stats2);
jehan's avatar
jehan committed
896
				stats1 = stats2 = NULL;
897 898
			}
			ms_usleep(20000);
899 900 901 902 903
		} while (!liblinphone_tester_clock_elapsed(&ts,10000));
		if (stats1)
			linphone_call_stats_unref(stats1);
		if (stats2)
			linphone_call_stats_unref(stats2);
904
	}
905

906 907 908 909 910 911 912 913 914 915 916
	/*make sure encryption mode are preserved*/
	if (c1) {
		const LinphoneCallParams* call_param = linphone_call_get_current_params(c1);
		BC_ASSERT_EQUAL(linphone_call_params_get_media_encryption(call_param),linphone_core_get_media_encryption(caller->lc), int, "%d");
	}
	if (c2) {
		const LinphoneCallParams* call_param = linphone_call_get_current_params(c2);
		BC_ASSERT_EQUAL(linphone_call_params_get_media_encryption(call_param),linphone_core_get_media_encryption(callee->lc), int, "%d");
	}
	linphone_call_unref(c1);
	linphone_call_unref(c2);
917 918 919 920
	if (audio_enabled) global_success = global_success && audio_success;
	if (video_enabled) global_success = global_success && video_success;
	if (realtime_text_enabled) global_success = global_success && text_success;
	return global_success;
921 922
}

923
static void linphone_conference_server_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *msg) {
924 925
	LinphoneCoreCbs *cbs = linphone_core_get_current_callbacks(lc);
	LinphoneConferenceServer *conf_srv = (LinphoneConferenceServer *)linphone_core_cbs_get_user_data(cbs);
926

927 928
	switch(cstate) {
		case LinphoneCallIncomingReceived:
929
			linphone_call_accept(call);
930
			break;
931

932 933 934 935
		case LinphoneCallStreamsRunning:
			if(linphone_call_get_conference(call) == NULL) {
				linphone_core_add_to_conference(lc, call);
				linphone_core_leave_conference(lc);
936
				if(conf_srv->first_call == NULL) conf_srv->first_call = call;
937 938
			}
			break;
939

940 941
		case LinphoneCallEnd:
			if(call == conf_srv->first_call) {
942 943 944
				if(linphone_core_get_conference(lc)) {
					linphone_core_terminate_conference(lc);
				}
945 946 947
				conf_srv->first_call = NULL;
			}
			break;
948

949 950 951 952
		default: break;
	}
}

953
static void linphone_conference_server_refer_received(LinphoneCore *core, const char *refer_to) {
954 955 956 957
	char method[20];
	LinphoneAddress *refer_to_addr = linphone_address_new(refer_to);
	char *uri;
	LinphoneCall *call;
958

959 960
	if(refer_to_addr == NULL) return;
	strncpy(method, linphone_address_get_method_param(refer_to_addr), sizeof(method));
961
	method[sizeof(method) - 1] = '\0';
962 963 964 965
	if(strcmp(method, "BYE") == 0) {
		linphone_address_clean(refer_to_addr);
		uri = linphone_address_as_string_uri_only(refer_to_addr);
		call = linphone_core_find_call_from_uri(core, uri);
966
		if(call) linphone_call_terminate(call);
967 968
		ms_free(uri);
	}
Simon Morlat's avatar
Simon Morlat committed
969
	linphone_address_unref(refer_to_addr);
970
}
971

972 973 974 975 976 977
static void linphone_conference_server_registration_state_changed(
	LinphoneCore *core,
	LinphoneProxyConfig *cfg,
	LinphoneRegistrationState cstate,
	const char *message
) {
978 979
	LinphoneCoreCbs *cbs = linphone_core_get_current_callbacks(core);
	LinphoneConferenceServer *m = (LinphoneConferenceServer *)linphone_core_cbs_get_user_data(cbs);
980 981 982 983 984 985
	if(cfg == linphone_core_get_default_proxy_config(core)) {
		m->reg_state = cstate;
	}
}

LinphoneConferenceServer* linphone_conference_server_new(const char *rc_file, bool_t do_registration) {
986 987
	LinphoneConferenceServer *conf_srv = (LinphoneConferenceServer *)ms_new0(LinphoneConferenceServer, 1);
	LinphoneCoreManager *lm = (LinphoneCoreManager *)conf_srv;
jehan's avatar
jehan committed
988
	LinphoneProxyConfig *proxy;
989 990 991 992 993
	conf_srv->cbs = linphone_factory_create_core_cbs(linphone_factory_get());
	linphone_core_cbs_set_call_state_changed(conf_srv->cbs, linphone_conference_server_call_state_changed);
	linphone_core_cbs_set_refer_received(conf_srv->cbs, linphone_conference_server_refer_received);
	linphone_core_cbs_set_registration_state_changed(conf_srv->cbs, linphone_conference_server_registration_state_changed);
	linphone_core_cbs_set_user_data(conf_srv->cbs, conf_srv);
994
	conf_srv->reg_state = LinphoneRegistrationNone;
995
	linphone_core_manager_init(lm, rc_file,NULL);
jehan's avatar
jehan committed
996 997 998 999 1000 1001
	if (!do_registration) {
		proxy = linphone_core_get_default_proxy_config(lm->lc);
		linphone_proxy_config_edit(proxy);
		linphone_proxy_config_enable_register(proxy,FALSE);
		linphone_proxy_config_done(proxy);
	}
1002
	linphone_core_add_callbacks(lm->lc, conf_srv->cbs);
1003
	linphone_core_manager_start(lm, do_registration);
1004 1005
	return conf_srv;
}
1006

1007
void linphone_conference_server_destroy(LinphoneConferenceServer *conf_srv) {
1008
	linphone_core_cbs_unref(conf_srv->cbs);
jehan's avatar
jehan committed
1009
	linphone_core_manager_destroy((LinphoneCoreManager *)conf_srv);
1010
}