linphonecore.c 268 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3
/*
linphone
Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
Simon Morlat's avatar
Simon Morlat committed
4
Copyright (C) 2010  Belledonne Communications SARL
aymeric's avatar
aymeric committed
5 6 7 8 9 10 11 12 13 14 15 16 17

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, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
aymeric's avatar
aymeric committed
19 20
*/

21
#include "linphone/api/c-content.h"
22
#include "linphone/core_utils.h"
23
#include "linphone/core.h"
24
#include "linphone/logging.h"
25
#include "linphone/lpconfig.h"
26 27
#include "linphone/sipsetup.h"

aymeric's avatar
aymeric committed
28
#include "private.h"
29
#include "logging-private.h"
30 31 32
#include "quality_reporting.h"
#include "lime.h"
#include "conference_private.h"
33
#include "logger/logger.h"
34

35
#ifdef SQLITE_STORAGE_ENABLED
36
#include "sqlite3_bctbx_vfs.h"
37 38
#endif

39
#include <math.h>
40 41
#include <sys/types.h>
#include <sys/stat.h>
42
#include <ortp/telephonyevents.h>
43
#include <mediastreamer2/zrtp.h>
johan's avatar
johan committed
44
#include <mediastreamer2/dtls_srtp.h>
45 46 47
#include "bctoolbox/defs.h"
#include "bctoolbox/regex.h"
#include "belr/grammarbuilder.h"
48

49
#include "mediastreamer2/dtmfgen.h"
aymeric's avatar
aymeric committed
50
#include "mediastreamer2/mediastream.h"
51
#include "mediastreamer2/msequalizer.h"
52 53
#include "mediastreamer2/mseventqueue.h"
#include "mediastreamer2/msfactory.h"
54
#include "mediastreamer2/msjpegwriter.h"
55 56
#include "mediastreamer2/msogl.h"
#include "mediastreamer2/msvolume.h"
57
#include "mediastreamer2/msqrcodereader.h"
58

59
#include "bctoolbox/charconv.h"
60

Ronan's avatar
Ronan committed
61
#include "chat/chat-room/client-group-chat-room-p.h"
62
#include "chat/chat-room/client-group-to-basic-chat-room.h"
63
#include "chat/chat-room/server-group-chat-room-p.h"
64
#include "conference/handlers/local-conference-list-event-handler.h"
65
#include "conference/handlers/remote-conference-event-handler.h"
66
#include "conference/handlers/remote-conference-list-event-handler.h"
67 68
#include "content/content-manager.h"
#include "content/content-type.h"
69
#include "core/core-p.h"
aymeric's avatar
aymeric committed
70

71 72
// For migration purpose.
#include "address/address-p.h"
73
#include "c-wrapper/c-wrapper.h"
74

75

76
#ifdef INET6
77
#ifndef _WIN32
78
#include <netdb.h>
aymeric's avatar
aymeric committed
79 80 81
#endif
#endif

82
#ifdef HAVE_CONFIG_H
83
#include "config.h"
84
#include "gitversion.h"
85 86
#endif

87 88 89
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
90

91
#include "c-wrapper/c-wrapper.h"
92 93 94
#include "call/call-p.h"
#include "conference/params/media-session-params-p.h"

95
#ifdef HAVE_ZLIB
96
#define COMPRESSED_LOG_COLLECTION_EXTENSION "gz"
97
#ifdef _WIN32
98 99
#include <fcntl.h>
#include <io.h>
100
#ifndef fileno
101
#define fileno _fileno
102
#endif
103
#define unlink _unlink
104 105 106 107 108
#define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
#define SET_BINARY_MODE(file)
#endif
#include <zlib.h>
109
#else
110
#define COMPRESSED_LOG_COLLECTION_EXTENSION "txt"
111
#endif
112 113 114 115
#define LOG_COLLECTION_DEFAULT_PATH "."
#define LOG_COLLECTION_DEFAULT_PREFIX "linphone"
#define LOG_COLLECTION_DEFAULT_MAX_FILE_SIZE (10 * 1024 * 1024)

116

smorlat's avatar
smorlat committed
117
/*#define UNSTANDART_GSM_11K 1*/
aymeric's avatar
aymeric committed
118

119 120 121 122 123 124 125
static const char *liblinphone_version=
#ifdef LIBLINPHONE_GIT_VERSION
	LIBLINPHONE_GIT_VERSION
#else
	LIBLINPHONE_VERSION
#endif
;
126 127 128 129 130 131 132

inline OrtpLogLevel operator|=(OrtpLogLevel a, OrtpLogLevel b) {
	int ia = static_cast<int>(a);
	int ib = static_cast<int>(b);
	return static_cast<OrtpLogLevel>(ia |= ib);
}

133 134
static OrtpLogFunc liblinphone_user_log_func = bctbx_logv_out; /*by default, user log handler = stdout*/
static OrtpLogFunc liblinphone_current_log_func = NULL; /*can be either logcolection or user_log*/
135
static LinphoneLogCollectionState liblinphone_log_collection_state = LinphoneLogCollectionDisabled;
136
static char * liblinphone_log_collection_path = NULL;
137
static char * liblinphone_log_collection_prefix = NULL;
138
static size_t liblinphone_log_collection_max_file_size = LOG_COLLECTION_DEFAULT_MAX_FILE_SIZE;
139
static ortp_mutex_t liblinphone_log_collection_mutex;
140 141
static FILE * liblinphone_log_collection_file = NULL;
static size_t liblinphone_log_collection_file_size = 0;
142
static bool_t liblinphone_serialize_logs = FALSE;
143
static void set_network_reachable(LinphoneCore* lc,bool_t isReachable, time_t curtime);
144 145
static void set_sip_network_reachable(LinphoneCore* lc,bool_t isReachable, time_t curtime);
static void set_media_network_reachable(LinphoneCore* lc,bool_t isReachable);
Simon Morlat's avatar
Simon Morlat committed
146
static void linphone_core_run_hooks(LinphoneCore *lc);
147
static void linphone_core_zrtp_cache_close(LinphoneCore *lc);
148
void linphone_core_zrtp_cache_db_init(LinphoneCore *lc, const char *fileName);
aymeric's avatar
aymeric committed
149

150 151
#include "enum.h"
#include "contact_providers_priv.h"
152

153
const char *linphone_core_get_nat_address_resolved(LinphoneCore *lc);
aymeric's avatar
aymeric committed
154 155
static void toggle_video_preview(LinphoneCore *lc, bool_t val);

156

aymeric's avatar
aymeric committed
157
/* relative path where is stored local ring*/
158 159
#define LOCAL_RING_WAV "oldphone-mono.wav"
#define LOCAL_RING_MKV "notes_of_the_optimistic.mkv"
aymeric's avatar
aymeric committed
160
/* same for remote ring (ringback)*/
161
#define REMOTE_RING_WAV "ringback.wav"
162

163 164
#define HOLD_MUSIC_WAV "toy-mono.wav"
#define HOLD_MUSIC_MKV "dont_wait_too_long.mkv"
aymeric's avatar
aymeric committed
165

166 167
using namespace std;

168 169
using namespace LinphonePrivate;

170
extern Sal::Callbacks linphone_sal_callbacks;
aymeric's avatar
aymeric committed
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

static void _linphone_core_cbs_uninit(LinphoneCoreCbs *cbs);

typedef belle_sip_object_t_vptr_t LinphoneCoreCbs_vptr_t;
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneCoreCbs);
BELLE_SIP_INSTANCIATE_VPTR(LinphoneCoreCbs, belle_sip_object_t,
	_linphone_core_cbs_uninit, // destroy
	NULL, // clone
	NULL, // Marshall
	FALSE
);

LinphoneCoreCbs *_linphone_core_cbs_new(void) {
	LinphoneCoreCbs *obj = belle_sip_object_new(LinphoneCoreCbs);
	obj->vtable = ms_new0(LinphoneCoreVTable, 1);
	obj->autorelease = TRUE;
	return obj;
}

static void _linphone_core_cbs_uninit(LinphoneCoreCbs *cbs) {
	if (cbs->autorelease) ms_free(cbs->vtable);
}

void _linphone_core_cbs_set_v_table(LinphoneCoreCbs *cbs, LinphoneCoreVTable *vtable, bool_t autorelease) {
	ms_free(cbs->vtable);
	cbs->vtable = vtable;
	cbs->autorelease = autorelease;
}

LinphoneCoreCbs *linphone_core_cbs_ref(LinphoneCoreCbs *cbs) {
	return (LinphoneCoreCbs *)belle_sip_object_ref(cbs);
}

void linphone_core_cbs_unref(LinphoneCoreCbs *cbs) {
	belle_sip_object_unref(cbs);
}

void linphone_core_cbs_set_user_data(LinphoneCoreCbs *cbs, void *user_data) {
	cbs->vtable->user_data = user_data;
}

Ghislain MARY's avatar
Ghislain MARY committed
213
void *linphone_core_cbs_get_user_data(const LinphoneCoreCbs *cbs) {
214 215 216 217 218 219 220
	return cbs->vtable->user_data;
}

LinphoneCoreCbs *linphone_core_get_current_callbacks(const LinphoneCore *lc) {
	return lc->current_cbs;
}

221 222 223 224
LinphoneCoreCbsGlobalStateChangedCb linphone_core_cbs_get_global_state_changed(LinphoneCoreCbs *cbs) {
	return cbs->vtable->global_state_changed;
}

225
void linphone_core_cbs_set_global_state_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsGlobalStateChangedCb cb) {
226 227 228
	cbs->vtable->global_state_changed = cb;
}

229 230 231 232
LinphoneCoreCbsRegistrationStateChangedCb linphone_core_cbs_get_registration_state_changed(LinphoneCoreCbs *cbs) {
	return cbs->vtable->registration_state_changed;
}

233 234 235 236
void linphone_core_cbs_set_registration_state_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsRegistrationStateChangedCb cb) {
	cbs->vtable->registration_state_changed = cb;
}

237 238 239 240
LinphoneCoreCbsCallStateChangedCb linphone_core_cbs_get_call_state_changed(LinphoneCoreCbs *cbs) {
	return cbs->vtable->call_state_changed;
}

241 242 243 244
void linphone_core_cbs_set_call_state_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsCallStateChangedCb cb) {
	cbs->vtable->call_state_changed = cb;
}

245 246 247 248
LinphoneCoreCbsNotifyPresenceReceivedCb linphone_core_cbs_get_notify_presence_received(LinphoneCoreCbs *cbs) {
	return cbs->vtable->notify_presence_received;
}

249 250 251 252
void linphone_core_cbs_set_notify_presence_received(LinphoneCoreCbs *cbs, LinphoneCoreCbsNotifyPresenceReceivedCb cb) {
	cbs->vtable->notify_presence_received = cb;
}

253 254 255 256
LinphoneCoreCbsNotifyPresenceReceivedForUriOrTelCb linphone_core_cbs_get_notify_presence_received_for_uri_or_tel(LinphoneCoreCbs *cbs) {
	return cbs->vtable->notify_presence_received_for_uri_or_tel;
}

257 258 259 260
void linphone_core_cbs_set_notify_presence_received_for_uri_or_tel(LinphoneCoreCbs *cbs, LinphoneCoreCbsNotifyPresenceReceivedForUriOrTelCb cb) {
	cbs->vtable->notify_presence_received_for_uri_or_tel = cb;
}

261 262 263 264
LinphoneCoreCbsNewSubscriptionRequestedCb linphone_core_cbs_get_new_subscription_requested(LinphoneCoreCbs *cbs) {
	return cbs->vtable->new_subscription_requested;
}

265 266 267 268
void linphone_core_cbs_set_new_subscription_requested(LinphoneCoreCbs *cbs, LinphoneCoreCbsNewSubscriptionRequestedCb cb) {
	cbs->vtable->new_subscription_requested = cb;
}

269 270 271 272
LinphoneCoreCbsAuthenticationRequestedCb linphone_core_cbs_get_authentication_requested(LinphoneCoreCbs *cbs) {
	return cbs->vtable->authentication_requested;
}

273 274 275 276
void linphone_core_cbs_set_authentication_requested(LinphoneCoreCbs *cbs, LinphoneCoreCbsAuthenticationRequestedCb cb) {
	cbs->vtable->authentication_requested = cb;
}

277 278 279 280
LinphoneCoreCbsCallLogUpdatedCb linphone_core_cbs_get_call_log_updated(LinphoneCoreCbs *cbs) {
	return cbs->vtable->call_log_updated;
}

281 282 283 284
void linphone_core_cbs_set_call_log_updated(LinphoneCoreCbs *cbs, LinphoneCoreCbsCallLogUpdatedCb cb) {
	cbs->vtable->call_log_updated = cb;
}

285 286 287 288
LinphoneCoreCbsMessageReceivedCb linphone_core_cbs_get_message_received(LinphoneCoreCbs *cbs) {
	return cbs->vtable->message_received;
}

289 290 291 292
void linphone_core_cbs_set_message_received(LinphoneCoreCbs *cbs, LinphoneCoreCbsMessageReceivedCb cb) {
	cbs->vtable->message_received = cb;
}

293 294 295 296
LinphoneCoreCbsMessageReceivedUnableDecryptCb linphone_core_cbs_get_message_received_unable_decrypt(LinphoneCoreCbs *cbs) {
	return cbs->vtable->message_received_unable_decrypt;
}

297 298 299 300
void linphone_core_cbs_set_message_received_unable_decrypt(LinphoneCoreCbs *cbs, LinphoneCoreCbsMessageReceivedUnableDecryptCb cb) {
	cbs->vtable->message_received_unable_decrypt = cb;
}

301 302 303 304
LinphoneCoreCbsIsComposingReceivedCb linphone_core_cbs_get_is_composing_received(LinphoneCoreCbs *cbs) {
	return cbs->vtable->is_composing_received;
}

305 306 307 308
void linphone_core_cbs_set_is_composing_received(LinphoneCoreCbs *cbs, LinphoneCoreCbsIsComposingReceivedCb cb) {
	cbs->vtable->is_composing_received = cb;
}

309 310 311 312
LinphoneCoreCbsDtmfReceivedCb linphone_core_cbs_get_dtmf_received(LinphoneCoreCbs *cbs) {
	return cbs->vtable->dtmf_received;
}

313 314 315 316
void linphone_core_cbs_set_dtmf_received(LinphoneCoreCbs *cbs, LinphoneCoreCbsDtmfReceivedCb cb) {
	cbs->vtable->dtmf_received = cb;
}

317 318 319 320
LinphoneCoreCbsReferReceivedCb linphone_core_cbs_get_refer_received(LinphoneCoreCbs *cbs) {
	return cbs->vtable->refer_received;
}

321 322 323 324
void linphone_core_cbs_set_refer_received(LinphoneCoreCbs *cbs, LinphoneCoreCbsReferReceivedCb cb) {
	cbs->vtable->refer_received = cb;
}

325 326 327 328
LinphoneCoreCbsCallEncryptionChangedCb linphone_core_cbs_get_call_encryption_changed(LinphoneCoreCbs *cbs) {
	return cbs->vtable->call_encryption_changed;
}

329 330 331 332
void linphone_core_cbs_set_call_encryption_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsCallEncryptionChangedCb cb) {
	cbs->vtable->call_encryption_changed = cb;
}

333 334 335 336
LinphoneCoreCbsTransferStateChangedCb linphone_core_cbs_get_transfer_state_changed(LinphoneCoreCbs *cbs) {
	return cbs->vtable->transfer_state_changed;
}

337 338 339 340
void linphone_core_cbs_set_transfer_state_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsTransferStateChangedCb cb) {
	cbs->vtable->transfer_state_changed = cb;
}

341 342 343 344
LinphoneCoreCbsBuddyInfoUpdatedCb linphone_core_cbs_get_buddy_info_updated(LinphoneCoreCbs *cbs) {
	return cbs->vtable->buddy_info_updated;
}

345 346 347 348
void linphone_core_cbs_set_buddy_info_updated(LinphoneCoreCbs *cbs, LinphoneCoreCbsBuddyInfoUpdatedCb cb) {
	cbs->vtable->buddy_info_updated = cb;
}

349 350 351 352
LinphoneCoreCbsCallStatsUpdatedCb linphone_core_cbs_get_call_stats_updated(LinphoneCoreCbs *cbs) {
	return cbs->vtable->call_stats_updated;
}

353 354 355 356
void linphone_core_cbs_set_call_stats_updated(LinphoneCoreCbs *cbs, LinphoneCoreCbsCallStatsUpdatedCb cb) {
	cbs->vtable->call_stats_updated = cb;
}

357 358 359 360
LinphoneCoreCbsInfoReceivedCb linphone_core_cbs_get_info_received(LinphoneCoreCbs *cbs) {
	return cbs->vtable->info_received;
}

361 362 363 364
void linphone_core_cbs_set_info_received(LinphoneCoreCbs *cbs, LinphoneCoreCbsInfoReceivedCb cb) {
	cbs->vtable->info_received = cb;
}

365 366 367 368
LinphoneCoreCbsSubscriptionStateChangedCb linphone_core_cbs_get_subscription_state_changed(LinphoneCoreCbs *cbs) {
	return cbs->vtable->subscription_state_changed;
}

369 370 371 372
void linphone_core_cbs_set_subscription_state_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsSubscriptionStateChangedCb cb) {
	cbs->vtable->subscription_state_changed = cb;
}

373 374 375 376
LinphoneCoreCbsNotifyReceivedCb linphone_core_cbs_get_notify_received(LinphoneCoreCbs *cbs) {
	return cbs->vtable->notify_received;
}

377 378 379 380
void linphone_core_cbs_set_notify_received(LinphoneCoreCbs *cbs, LinphoneCoreCbsNotifyReceivedCb cb) {
	cbs->vtable->notify_received = cb;
}

381 382 383 384 385 386 387 388
LinphoneCoreCbsSubscribeReceivedCb linphone_core_cbs_get_subscribe_received(LinphoneCoreCbs *cbs) {
	return cbs->vtable->subscribe_received;
}

void linphone_core_cbs_set_subscribe_received(LinphoneCoreCbs *cbs, LinphoneCoreCbsSubscribeReceivedCb cb) {
	cbs->vtable->subscribe_received = cb;
}

389 390 391 392
LinphoneCoreCbsPublishStateChangedCb linphone_core_cbs_get_rpublish_state_changed(LinphoneCoreCbs *cbs) {
	return cbs->vtable->publish_state_changed;
}

393 394 395 396
void linphone_core_cbs_set_publish_state_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsPublishStateChangedCb cb) {
	cbs->vtable->publish_state_changed = cb;
}

397 398 399 400
LinphoneCoreCbsConfiguringStatusCb linphone_core_cbs_get_configuring_status(LinphoneCoreCbs *cbs) {
	return cbs->vtable->configuring_status;
}

401 402 403 404
void linphone_core_cbs_set_configuring_status(LinphoneCoreCbs *cbs, LinphoneCoreCbsConfiguringStatusCb cb) {
	cbs->vtable->configuring_status = cb;
}

405 406 407 408
LinphoneCoreCbsNetworkReachableCb linphone_core_cbs_get_network_reachable(LinphoneCoreCbs *cbs) {
	return cbs->vtable->network_reachable;
}

409 410 411 412
void linphone_core_cbs_set_network_reachable(LinphoneCoreCbs *cbs, LinphoneCoreCbsNetworkReachableCb cb) {
	cbs->vtable->network_reachable = cb;
}

413 414 415 416
LinphoneCoreCbsLogCollectionUploadStateChangedCb linphone_core_cbs_log_collection_upload_state_changed(LinphoneCoreCbs *cbs) {
	return cbs->vtable->log_collection_upload_state_changed;
}

417 418 419 420
void linphone_core_cbs_set_log_collection_upload_state_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsLogCollectionUploadStateChangedCb cb) {
	cbs->vtable->log_collection_upload_state_changed = cb;
}

421 422 423 424
LinphoneCoreCbsLogCollectionUploadProgressIndicationCb linphone_core_cbs_get_rlog_collection_upload_progress_indication(LinphoneCoreCbs *cbs) {
	return cbs->vtable->log_collection_upload_progress_indication;
}

425 426 427 428
void linphone_core_cbs_set_log_collection_upload_progress_indication(LinphoneCoreCbs *cbs, LinphoneCoreCbsLogCollectionUploadProgressIndicationCb cb) {
	cbs->vtable->log_collection_upload_progress_indication = cb;
}

429 430 431 432
LinphoneCoreCbsFriendListCreatedCb linphone_core_cbs_get_friend_list_created(LinphoneCoreCbs *cbs) {
	return cbs->vtable->friend_list_created;
}

433 434 435 436
void linphone_core_cbs_set_friend_list_created(LinphoneCoreCbs *cbs, LinphoneCoreCbsFriendListCreatedCb cb) {
	cbs->vtable->friend_list_created = cb;
}

437 438 439 440
LinphoneCoreCbsFriendListRemovedCb linphone_core_cbs_get_friend_list_removed(LinphoneCoreCbs *cbs) {
	return cbs->vtable->friend_list_removed;
}

441 442 443 444
void linphone_core_cbs_set_friend_list_removed(LinphoneCoreCbs *cbs, LinphoneCoreCbsFriendListRemovedCb cb) {
	cbs->vtable->friend_list_removed = cb;
}

445 446 447 448 449 450 451 452
LinphoneCoreCbsCallCreatedCb linphone_core_cbs_get_call_created(LinphoneCoreCbs *cbs) {
	return cbs->vtable->call_created;
}

void linphone_core_cbs_set_call_created(LinphoneCoreCbs *cbs, LinphoneCoreCbsCallCreatedCb cb) {
	cbs->vtable->call_created = cb;
}

Ghislain MARY's avatar
Ghislain MARY committed
453 454 455 456 457 458 459 460
LinphoneCoreCbsVersionUpdateCheckResultReceivedCb linphone_core_cbs_get_version_update_check_result_received(LinphoneCoreCbs *cbs) {
	return cbs->vtable->version_update_check_result_received;
}

void linphone_core_cbs_set_version_update_check_result_received(LinphoneCoreCbs *cbs, LinphoneCoreCbsVersionUpdateCheckResultReceivedCb cb) {
	cbs->vtable->version_update_check_result_received = cb;
}

461 462
LinphoneCoreCbsChatRoomStateChangedCb linphone_core_cbs_get_chat_room_state_changed (LinphoneCoreCbs *cbs) {
	return cbs->vtable->chat_room_state_changed;
463 464
}

465 466
void linphone_core_cbs_set_chat_room_state_changed (LinphoneCoreCbs *cbs, LinphoneCoreCbsChatRoomStateChangedCb cb) {
	cbs->vtable->chat_room_state_changed = cb;
467 468
}

469 470
LinphoneCoreCbsQrcodeFoundCb linphone_core_cbs_get_qrcode_found(LinphoneCoreCbs *cbs) {
	return cbs->vtable->qrcode_found;
471 472
}

473 474
void linphone_core_cbs_set_qrcode_found(LinphoneCoreCbs *cbs, LinphoneCoreCbsQrcodeFoundCb cb) {
	cbs->vtable->qrcode_found = cb;
475 476
}

477 478 479 480 481 482 483 484 485 486 487 488
void linphone_core_cbs_set_ec_calibration_result(LinphoneCoreCbs *cbs, LinphoneCoreCbsEcCalibrationResultCb cb) {
	cbs->vtable->ec_calibration_result = cb;
}

void linphone_core_cbs_set_ec_calibration_audio_init(LinphoneCoreCbs *cbs, LinphoneCoreCbsEcCalibrationAudioInitCb cb) {
	cbs->vtable->ec_calibration_audio_init = cb;
}

void linphone_core_cbs_set_ec_calibration_audio_uninit(LinphoneCoreCbs *cbs, LinphoneCoreCbsEcCalibrationAudioUninitCb cb) {
	cbs->vtable->ec_calibration_audio_uninit = cb;
}

Ghislain MARY's avatar
Ghislain MARY committed
489

490
void lc_callback_obj_init(LCCallbackObj *obj,LinphoneCoreCbFunc func,void* ud) {
491 492
	obj->_func=func;
	obj->_user_data=ud;
aymeric's avatar
aymeric committed
493 494 495 496 497 498 499
}

int lc_callback_obj_invoke(LCCallbackObj *obj, LinphoneCore *lc){
	if (obj->_func!=NULL) obj->_func(lc,obj->_user_data);
	return 0;
}

500 501
int linphone_core_get_current_call_duration(const LinphoneCore *lc){
	LinphoneCall *call=linphone_core_get_current_call((LinphoneCore *)lc);
502 503
	if (call)  return linphone_call_get_duration(call);
	return -1;
504 505 506 507
}

const LinphoneAddress *linphone_core_get_current_call_remote_address(struct _LinphoneCore *lc){
	LinphoneCall *call=linphone_core_get_current_call(lc);
Simon Morlat's avatar
Simon Morlat committed
508
	if (call==NULL) return NULL;
509
	return linphone_call_get_remote_address(call);
smorlat's avatar
smorlat committed
510 511
}

512
static void linphone_core_log_collection_handler(const char *domain, OrtpLogLevel level, const char *fmt, va_list args);
jehan's avatar
jehan committed
513

514
void _linphone_core_set_log_handler(OrtpLogFunc logfunc) {
515 516
	liblinphone_user_log_func = logfunc;
	if (liblinphone_current_log_func == linphone_core_log_collection_handler) {
517
		ms_message("There is already a log collection handler, keep it");
518
	} else {
519
		bctbx_set_log_handler(liblinphone_current_log_func=liblinphone_user_log_func);
520
	}
521 522
}

523 524 525 526
void linphone_core_set_log_handler(OrtpLogFunc logfunc){
	_linphone_core_set_log_handler(logfunc);
}

527 528
void linphone_core_set_log_file(FILE *file) {
	if (file == NULL) file = stdout;
529
	_linphone_core_set_log_handler(NULL);
530
	bctbx_set_log_file(file); /*gather everythings*/
531 532 533
}

void linphone_core_set_log_level(OrtpLogLevel loglevel) {
534 535
	LinphoneLoggingService *log_service = linphone_logging_service_get();
	linphone_logging_service_set_log_level(log_service, _bctbx_log_level_to_linphone_log_level(loglevel));
536 537
}

538

539 540 541
void linphone_core_set_log_level_mask(unsigned int mask) {
	LinphoneLoggingService *log_service = linphone_logging_service_get();
	linphone_logging_service_set_log_level_mask(log_service, _bctbx_log_mask_to_linphone_log_mask(mask));
542
}
543

544
unsigned int linphone_core_get_log_level_mask(void) {
545 546
	LinphoneLoggingService *log_service = linphone_logging_service_get();
	return linphone_logging_service_get_log_level_mask(log_service);
547
}
548

549 550 551 552 553 554 555 556 557 558 559 560 561
static int _open_log_collection_file_with_idx(int idx) {
	struct stat statbuf;
	char *log_filename;

	log_filename = ortp_strdup_printf("%s/%s%d.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
		idx);
	liblinphone_log_collection_file = fopen(log_filename, "a");
	ortp_free(log_filename);
	if (liblinphone_log_collection_file == NULL) return -1;

	fstat(fileno(liblinphone_log_collection_file), &statbuf);
562
	if ((size_t)statbuf.st_size > liblinphone_log_collection_max_file_size) {
563 564 565 566
		fclose(liblinphone_log_collection_file);
		return -1;
	}

Benjamin REIS's avatar
Benjamin REIS committed
567
	liblinphone_log_collection_file_size = (size_t)statbuf.st_size;
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 601 602 603
	return 0;
}

static void _rotate_log_collection_files(void) {
	char *log_filename1;
	char *log_filename2;

	log_filename1 = ortp_strdup_printf("%s/%s1.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
	log_filename2 = ortp_strdup_printf("%s/%s2.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
	unlink(log_filename1);
	rename(log_filename2, log_filename1);
	ortp_free(log_filename1);
	ortp_free(log_filename2);
}

static void _open_log_collection_file(void) {
	if (_open_log_collection_file_with_idx(1) < 0) {
		if (_open_log_collection_file_with_idx(2) < 0) {
			_rotate_log_collection_files();
			_open_log_collection_file_with_idx(2);
		}
	}
}

static void _close_log_collection_file(void) {
	if (liblinphone_log_collection_file) {
		fclose(liblinphone_log_collection_file);
		liblinphone_log_collection_file = NULL;
		liblinphone_log_collection_file_size = 0;
	}
}

604
static void linphone_core_log_collection_handler(const char *domain, OrtpLogLevel level, const char *fmt, va_list args) {
605 606 607
	const char *lname="undef";
	char *msg;
	struct timeval tp;
608
	struct tm *lt;
609
	time_t tt;
610
	int ret;
611

612
	if (liblinphone_user_log_func != NULL && liblinphone_user_log_func != linphone_core_log_collection_handler) {
613
#ifndef _WIN32
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
614 615
		va_list args_copy;
		va_copy(args_copy, args);
616
		liblinphone_user_log_func(domain, level, fmt, args_copy);
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
617
		va_end(args_copy);
618 619 620
#else
		/* This works on 32 bits, luckily. */
		/* TODO: va_copy is available in Visual Studio 2013. */
621
		liblinphone_user_log_func(domain, level, fmt, args);
622
#endif
623 624 625 626 627
	}

	ortp_gettimeofday(&tp, NULL);
	tt = (time_t)tp.tv_sec;
	lt = localtime((const time_t*)&tt);
628 629 630 631 632 633 634 635 636 637 638 639 640

	if ((level & ORTP_DEBUG) != 0) {
		lname = "DEBUG";
	} else if ((level & ORTP_MESSAGE) != 0) {
		lname = "MESSAGE";
	} else if ((level & ORTP_WARNING) != 0) {
		lname = "WARNING";
	} else if ((level & ORTP_ERROR) != 0) {
		lname = "ERROR";
	} else if ((level & ORTP_FATAL) != 0) {
		lname = "FATAL";
	} else {
		ortp_fatal("Bad level !");
641 642 643
	}
	msg = ortp_strdup_vprintf(fmt, args);

644 645 646 647 648 649 650
	if (liblinphone_log_collection_file == NULL) {
		ortp_mutex_lock(&liblinphone_log_collection_mutex);
		_open_log_collection_file();
		ortp_mutex_unlock(&liblinphone_log_collection_mutex);
	}
	if (liblinphone_log_collection_file) {
		ortp_mutex_lock(&liblinphone_log_collection_mutex);
651 652
		ret = fprintf(liblinphone_log_collection_file,"%i-%.2i-%.2i %.2i:%.2i:%.2i:%.3i [%s] %s %s\n",
			1900 + lt->tm_year, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, (int)(tp.tv_usec / 1000), domain, lname, msg);
653 654
		fflush(liblinphone_log_collection_file);
		if (ret > 0) {
Benjamin REIS's avatar
Benjamin REIS committed
655
			liblinphone_log_collection_file_size += (size_t)ret;
656 657 658 659
			if (liblinphone_log_collection_file_size > liblinphone_log_collection_max_file_size) {
				_close_log_collection_file();
				_open_log_collection_file();
			}
660
		}
661
		ortp_mutex_unlock(&liblinphone_log_collection_mutex);
662 663 664 665 666
	}

	ortp_free(msg);
}

667 668 669 670 671 672 673
const char * linphone_core_get_log_collection_path(void) {
	if (liblinphone_log_collection_path != NULL) {
		return liblinphone_log_collection_path;
	}
	return LOG_COLLECTION_DEFAULT_PATH;
}

674
void linphone_core_set_log_collection_path(const char *path) {
675 676 677 678 679 680 681
	if (liblinphone_log_collection_path != NULL) {
		ms_free(liblinphone_log_collection_path);
		liblinphone_log_collection_path = NULL;
	}
	if (path != NULL) {
		liblinphone_log_collection_path = ms_strdup(path);
	}
682 683
}

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
const char * linphone_core_get_log_collection_prefix(void) {
	if (liblinphone_log_collection_prefix != NULL) {
		return liblinphone_log_collection_prefix;
	}
	return LOG_COLLECTION_DEFAULT_PREFIX;
}

void linphone_core_set_log_collection_prefix(const char *prefix) {
	if (liblinphone_log_collection_prefix != NULL) {
		ms_free(liblinphone_log_collection_prefix);
		liblinphone_log_collection_prefix = NULL;
	}
	if (prefix != NULL) {
		liblinphone_log_collection_prefix = ms_strdup(prefix);
	}
}

701
size_t linphone_core_get_log_collection_max_file_size(void) {
702 703 704
	return liblinphone_log_collection_max_file_size;
}

705
void linphone_core_set_log_collection_max_file_size(size_t size) {
706 707 708
	liblinphone_log_collection_max_file_size = size;
}

709 710 711 712 713 714 715 716
const char *linphone_core_get_log_collection_upload_server_url(LinphoneCore *core) {
	return lp_config_get_string(core->config, "misc", "log_collection_upload_server_url", NULL);
}

void linphone_core_set_log_collection_upload_server_url(LinphoneCore *core, const char *server_url) {
	lp_config_set_string(core->config, "misc", "log_collection_upload_server_url", server_url);
}

717 718 719 720
LinphoneLogCollectionState linphone_core_log_collection_enabled(void) {
	return liblinphone_log_collection_state;
}

721
void linphone_core_enable_log_collection(LinphoneLogCollectionState state) {
722 723
	if (liblinphone_log_collection_state == state) return;

724 725
	liblinphone_log_collection_state = state;
	if (state != LinphoneLogCollectionDisabled) {
726
		ortp_mutex_init(&liblinphone_log_collection_mutex, NULL);
727
		if (state == LinphoneLogCollectionEnabledWithoutPreviousLogHandler) {
728
			liblinphone_user_log_func = NULL; /*remove user log handler*/
729
		}
730
		bctbx_set_log_handler(liblinphone_current_log_func = linphone_core_log_collection_handler);
731
	} else {
732
		bctbx_set_log_handler(liblinphone_user_log_func); /*restaure */
733 734 735
	}
}

jehan's avatar
jehan committed
736
static void clean_log_collection_upload_context(LinphoneCore *lc) {
737 738 739 740
	char *filename = ms_strdup_printf("%s/%s_log.%s",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
		COMPRESSED_LOG_COLLECTION_EXTENSION);
741
	unlink(filename);
742
	ms_free(filename);
jehan's avatar
jehan committed
743
	if (lc && lc->log_collection_upload_information) {
744
		linphone_content_unref(lc->log_collection_upload_information);
jehan's avatar
jehan committed
745 746
		lc->log_collection_upload_information=NULL;
	}
747 748
}

749 750 751 752
static void process_io_error_upload_log_collection(void *data, const belle_sip_io_error_event_t *event) {
	LinphoneCore *core = (LinphoneCore *)data;
	ms_error("I/O Error during log collection upload to %s", linphone_core_get_log_collection_upload_server_url(core));
	linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateNotDelivered, "I/O Error");
jehan's avatar
jehan committed
753
	clean_log_collection_upload_context(core);
754 755 756 757 758 759
}

static void process_auth_requested_upload_log_collection(void *data, belle_sip_auth_event_t *event) {
	LinphoneCore *core = (LinphoneCore *)data;
	ms_error("Error during log collection upload: auth requested to connect %s", linphone_core_get_log_collection_upload_server_url(core));
	linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateNotDelivered, "Auth requested");
jehan's avatar
jehan committed
760
	clean_log_collection_upload_context(core);
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
}

/**
 * Callback called when posting a log collection file to server (following rcs5.1 recommendation)
 * @param[in] bh The body handler
 * @param[in] msg The belle sip message
 * @param[in] data The user data associated with the handler, contains the LinphoneCore object
 * @param[in] offset The current position in the input buffer
 * @param[in] buffer The ouput buffer where to copy the data to be uploaded
 * @param[in,out] size The size in byte of the data requested, as output it will contain the effective copied size
 */
static int log_collection_upload_on_send_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t *msg, void *data, size_t offset, uint8_t *buffer, size_t *size) {
	LinphoneCore *core = (LinphoneCore *)data;

	/* If we've not reach the end of file yet, fill the buffer with more data */
776
	if (offset < linphone_content_get_size(core->log_collection_upload_information)) {
777 778 779 780
		char *log_filename = ms_strdup_printf("%s/%s_log.%s",
			liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
			liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
			COMPRESSED_LOG_COLLECTION_EXTENSION);
781 782 783
#ifdef HAVE_ZLIB
		FILE *log_file = fopen(log_filename, "rb");
#else
784
		FILE *log_file = fopen(log_filename, "r");
785
#endif
786
		if (fseek(log_file, (long)offset, SEEK_SET)) {
jehan's avatar
jehan committed
787 788 789 790 791
			ms_error("Cannot seek file [%s] at position [%lu] errno [%s]",log_filename,(unsigned long)offset,strerror(errno));

		} else {
			*size = fread(buffer, 1, *size, log_file);
		}
792
		fclose(log_file);
793
		ms_free(log_filename);
jehan's avatar
jehan committed
794 795 796 797
		return BELLE_SIP_CONTINUE;
	} else {
		*size=0;
		return BELLE_SIP_STOP;
798 799 800 801 802 803 804 805 806
	}
}

/**
 * Callback called during upload of a log collection to server.
 * It is just forwarding the call and some parameters to the vtable defined callback.
 */
static void log_collection_upload_on_progress(belle_sip_body_handler_t *bh, belle_sip_message_t *msg, void *data, size_t offset, size_t total) {
	LinphoneCore *core = (LinphoneCore *)data;
807
	linphone_core_notify_log_collection_upload_progress_indication(core, offset, total);
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
}

/**
 * Callback function called when we have a response from server during the upload of the log collection to the server (rcs5.1 recommandation)
 * Note: The first post is empty and the server shall reply a 204 (No content) message, this will trigger a new post request to the server
 * to upload the file. The server response to this second post is processed by this same function
 *
 * @param[in] data The user-defined pointer associated with the request, it contains the LinphoneCore object
 * @param[in] event The response from server
 */
static void process_response_from_post_file_log_collection(void *data, const belle_http_response_event_t *event) {
	LinphoneCore *core = (LinphoneCore *)data;

	/* Check the answer code */
	if (event->response) {
		int code = belle_http_response_get_status_code(event->response);
		if (code == 204) { /* This is the reply to the first post to the server - an empty file */
			/* Start uploading the file */
			belle_http_request_listener_callbacks_t cbs = { 0 };
			belle_http_request_listener_t *l;
			belle_generic_uri_t *uri;
			belle_http_request_t *req;
			belle_sip_multipart_body_handler_t *bh;
			char* ua;
			char *first_part_header;
			belle_sip_user_body_handler_t *first_part_bh;

			linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateInProgress, NULL);

			/* Temporary storage for the Content-disposition header value */
838
			first_part_header = belle_sip_strdup_printf("form-data; name=\"File\"; filename=\"%s\"", linphone_content_get_name(core->log_collection_upload_information));
839 840

			/* Create a user body handler to take care of the file and add the content disposition and content-type headers */
841
			first_part_bh = belle_sip_user_body_handler_new(linphone_content_get_size(core->log_collection_upload_information), NULL, NULL, NULL, log_collection_upload_on_send_body, NULL, core);
842 843
			belle_sip_body_handler_add_header((belle_sip_body_handler_t *)first_part_bh, belle_sip_header_create("Content-disposition", first_part_header));
			belle_sip_free(first_part_header);
844 845
			belle_sip_body_handler_add_header((belle_sip_body_handler_t *)first_part_bh,
				(belle_sip_header_t *)belle_sip_header_content_type_create(linphone_content_get_type(core->log_collection_upload_information), linphone_content_get_subtype(core->log_collection_upload_information)));
846 847

			/* Insert it in a multipart body handler which will manage the boundaries of multipart message */
848
			bh = belle_sip_multipart_body_handler_new(log_collection_upload_on_progress, core, (belle_sip_body_handler_t *)first_part_bh, NULL);
849
			ua = ms_strdup_printf("%s/%s", linphone_core_get_user_agent(core), linphone_core_get_version());
850
			uri = belle_generic_uri_parse(linphone_core_get_log_collection_upload_server_url(core));
851
			req = belle_http_request_create("POST", uri, belle_sip_header_create("User-Agent", ua), NULL);
852 853 854 855 856 857
			ms_free(ua);
			belle_sip_message_set_body_handler(BELLE_SIP_MESSAGE(req), BELLE_SIP_BODY_HANDLER(bh));
			cbs.process_response = process_response_from_post_file_log_collection;
			cbs.process_io_error = process_io_error_upload_log_collection;
			cbs.process_auth_requested = process_auth_requested_upload_log_collection;
			l = belle_http_request_listener_create_from_callbacks(&cbs, core);
858
			belle_sip_object_data_set(BELLE_SIP_OBJECT(req), "http_request_listener", l, belle_sip_object_unref); // Ensure the listener object is destroyed when the request is destroyed
859
			belle_http_provider_send_request(core->http_provider, req, l);
860
		} else if (code == 200) { /* The file has been uploaded correctly, get the server reply */
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
			xmlDocPtr xmlMessageBody;
			xmlNodePtr cur;
			xmlChar *file_url = NULL;
			const char *body = belle_sip_message_get_body((belle_sip_message_t *)event->response);
			xmlMessageBody = xmlParseDoc((const xmlChar *)body);
			cur = xmlDocGetRootElement(xmlMessageBody);
			if (cur != NULL) {
				cur = cur->xmlChildrenNode;
				while (cur != NULL) {
					if (!xmlStrcmp(cur->name, (const xmlChar *)"file-info")) { /* we found a file info node, check it has a type="file" attribute */
						xmlChar *typeAttribute = xmlGetProp(cur, (const xmlChar *)"type");
						if (!xmlStrcmp(typeAttribute, (const xmlChar *)"file")) { /* this is the node we are looking for */
							cur = cur->xmlChildrenNode; /* now loop on the content of the file-info node */
							while (cur != NULL) {
								if (!xmlStrcmp(cur->name, (const xmlChar *)"data")) {
876
									file_url = xmlGetProp(cur, (const xmlChar *)"url");
877 878 879 880 881 882 883 884 885 886 887 888 889
								}
								cur=cur->next;
							}
							xmlFree(typeAttribute);
							break;
						}
						xmlFree(typeAttribute);
					}
					cur = cur->next;
				}
			}
			if (file_url != NULL) {
				linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateDelivered, (const char *)file_url);
890
				xmlFree(file_url);
891
			}
892
			xmlFreeDoc(xmlMessageBody);
jehan's avatar
jehan committed
893
			clean_log_collection_upload_context(core);
894 895 896 897
		} else {
			ms_error("Unexpected HTTP response code %i during log collection upload to %s", code, linphone_core_get_log_collection_upload_server_url(core));
			linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateNotDelivered, "Unexpected HTTP response");
			clean_log_collection_upload_context(core);
898 899 900 901
		}
	}
}

902
#ifdef HAVE_ZLIB
903 904 905
#define COMPRESS_FILE_PTR gzFile
#define COMPRESS_OPEN gzopen
#define COMPRESS_CLOSE gzclose
906
#else
907 908 909 910
#define COMPRESS_FILE_PTR FILE*
#define COMPRESS_OPEN fopen
#define COMPRESS_CLOSE fclose
#endif
911 912 913 914

/**
 * If zlib is not available the two log files are simply concatenated.
 */
915
static int compress_file(FILE *input_file, COMPRESS_FILE_PTR output_file) {
916
	char buffer[131072]; /* 128kB */
917
	size_t bytes;
Sylvain Berfini's avatar
Sylvain Berfini committed
918
	size_t total_bytes = 0;
919

920
	while ((bytes = fread(buffer, 1, sizeof(buffer), input_file)) > 0) {
921
#ifdef HAVE_ZLIB
922 923
		int res = gzwrite(output_file, buffer, (unsigned int)bytes);
		if (res < 0) return 0;
Sylvain Berfini's avatar
Sylvain Berfini committed
924
		total_bytes += (size_t)res;
925
#else
Sylvain Berfini's avatar
Sylvain Berfini committed
926
		total_bytes += fwrite(buffer, 1, bytes, output_file);
927
#endif
928
	}
929
	return (int)total_bytes;
930 931
}

932
static int prepare_log_collection_file_to_upload(const char *filename) {
933 934 935
	char *input_filename = NULL;
	char *output_filename = NULL;
	FILE *input_file = NULL;
936 937
	COMPRESS_FILE_PTR output_file = NULL;
	int ret = 0;
938

939
	ortp_mutex_lock(&liblinphone_log_collection_mutex);
940 941
	output_filename = ms_strdup_printf("%s/%s",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, filename);
942
	output_file = COMPRESS_OPEN(output_filename, "wb");
943
	if (output_file == NULL) goto error;
944 945 946
	input_filename = ms_strdup_printf("%s/%s1.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
947
	input_file = fopen(input_filename, "rb");
948 949
	if (input_file == NULL) goto error;
	ret = compress_file(input_file, output_file);
Sylvain Berfini's avatar
Sylvain Berfini committed
950
	if (ret <= 0) goto error;
951
	fclose(input_file);
952
	ms_free(input_filename);
953 954 955
	input_filename = ms_strdup_printf("%s/%s2.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
956
	input_file = fopen(input_filename, "rb");
957 958
	if (input_file != NULL) {
		ret = compress_file(input_file, output_file);
Sylvain Berfini's avatar
Sylvain Berfini committed
959
		if (ret <= 0) goto error;
960
	}
961

962 963
error:
	if (input_file != NULL) fclose(input_file);
964
	if (output_file != NULL) COMPRESS_CLOSE(output_file);
965 966
	if (input_filename != NULL) ms_free(input_filename);
	if (output_filename != NULL) ms_free(output_filename);
967
	ortp_mutex_unlock(&liblinphone_log_collection_mutex);
968 969 970 971 972
	return ret;
}

static size_t get_size_of_file_to_upload(const char *filename) {
	struct stat statbuf;
973 974
	char *output_filename = ms_strdup_printf("%s/%s",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH, filename);
975 976 977
	FILE *output_file = fopen(output_filename, "rb");
	fstat(fileno(output_file), &statbuf);
	fclose(output_file);
978
	ms_free(output_filename);
Benjamin REIS's avatar
Benjamin REIS committed
979
	return (size_t)statbuf.st_size;
980 981
}

982
void linphone_core_upload_log_collection(LinphoneCore *core) {
983
	if ((core->log_collection_upload_information == NULL) && (linphone_core_get_log_collection_upload_server_url(core) != NULL) && (liblinphone_log_collection_state != LinphoneLogCollectionDisabled)) {
984 985 986 987 988
		/* open a transaction with the server and send an empty request(RCS5.1 section 3.5.4.8.3.1) */
		belle_http_request_listener_callbacks_t cbs = { 0 };
		belle_http_request_listener_t *l;
		belle_generic_uri_t *uri;
		belle_http_request_t *req;
989
		char *name;
990

991
		core->log_collection_upload_information = linphone_core_create_content(core);
992
#ifdef HAVE_ZLIB
993 994
		linphone_content_set_type(core->log_collection_upload_information, "application");
		linphone_content_set_subtype(core->log_collection_upload_information, "gzip");
995
#else
996 997
		linphone_content_set_type(core->log_collection_upload_information, "text");
		linphone_content_set_subtype(core->log_collection_upload_information,"plain");
998
#endif
999
		name = ms_strdup_printf("%s_log.%s",
1000 1001
			liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
			COMPRESSED_LOG_COLLECTION_EXTENSION);
1002
		linphone_content_set_name(core->log_collection_upload_information, name);
Sylvain Berfini's avatar
Sylvain Berfini committed
1003
		if (prepare_log_collection_file_to_upload(name) <= 0) {
1004
			linphone_content_unref(core->log_collection_upload_information);
Sylvain Berfini's avatar
Sylvain Berfini committed
1005
			core->log_collection_upload_information = NULL;
1006
			ms_error("prepare_log_collection_file_to_upload(): error.");
1007
			linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateNotDelivered, "Error while preparing log collection upload");
1008
			return;
Sylvain Berfini's avatar
Sylvain Berfini committed
1009
		}
1010
		linphone_content_set_size(core->log_collection_upload_information, get_size_of_file_to_upload(name));
1011 1012 1013 1014 1015 1016
		uri = belle_generic_uri_parse(linphone_core_get_log_collection_upload_server_url(core));
		req = belle_http_request_create("POST", uri, NULL, NULL, NULL);
		cbs.process_response = process_response_from_post_file_log_collection;
		cbs.process_io_error = process_io_error_upload_log_collection;
		cbs.process_auth_requested = process_auth_requested_upload_log_collection;
		l = belle_http_request_listener_create_from_callbacks(&cbs, core);
1017
		belle_sip_object_data_set(BELLE_SIP_OBJECT(req), "http_request_listener", l, belle_sip_object_unref); // Ensure the listener object is destroyed when the request is destroyed
1018
		belle_http_provider_send_request(core->http_provider, req, l);
1019
		ms_free(name);
1020
	} else {
1021
		const char *msg = NULL;
1022 1023
		ms_warning("Could not upload log collection: log_collection_upload_information=%p, server_url=%s, log_collection_state=%d",
			core->log_collection_upload_information, linphone_core_get_log_collection_upload_server_url(core), liblinphone_log_collection_state);
1024 1025 1026 1027 1028 1029 1030 1031
		if (core->log_collection_upload_information != NULL) {
			msg = "Log collection upload already in progress";
		} else if (linphone_core_get_log_collection_upload_server_url(core) == NULL) {
			msg = "Log collection upload server not set";
		} else if (liblinphone_log_collection_state == LinphoneLogCollectionDisabled) {
			msg = "Log collection is disabled";
		}
		linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateNotDelivered, msg);
1032 1033 1034
	}
}

1035
char * linphone_core_compress_log_collection(void) {
1036 1037 1038 1039 1040
	char *filename = NULL;
	if (liblinphone_log_collection_state == LinphoneLogCollectionDisabled) return NULL;
	filename = ms_strdup_printf("%s_log.%s",
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
		COMPRESSED_LOG_COLLECTION_EXTENSION);
1041
	if (prepare_log_collection_file_to_upload(filename) <= 0) {
1042 1043 1044 1045 1046 1047 1048 1049
		ms_free(filename);
		return NULL;
	}
	ms_free(filename);
	return ms_strdup_printf("%s/%s_log.%s",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX,
		COMPRESSED_LOG_COLLECTION_EXTENSION);
1050 1051
}

1052
void linphone_core_reset_log_collection(void) {
1053 1054
	char *filename;
	ortp_mutex_lock(&liblinphone_log_collection_mutex);
1055
	_close_log_collection_file();
jehan's avatar
jehan committed
1056
	clean_log_collection_upload_context(NULL);
1057 1058 1059
	filename = ms_strdup_printf("%s/%s1.log",
			liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
			liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
1060 1061
	unlink(filename);
	ms_free(filename);
1062 1063 1064
	filename = ms_strdup_printf("%s/%s2.log",
		liblinphone_log_collection_path ? liblinphone_log_collection_path : LOG_COLLECTION_DEFAULT_PATH,
		liblinphone_log_collection_prefix ? liblinphone_log_collection_prefix : LOG_COLLECTION_DEFAULT_PREFIX);
1065 1066
	unlink(filename);
	ms_free(filename);
1067 1068
	liblinphone_log_collection_file = NULL;
	liblinphone_log_collection_file_size = 0;
1069
	liblinphone_log_collection_max_file_size = LOG_COLLECTION_DEFAULT_MAX_FILE_SIZE; /*also reset size*/
1070 1071 1072
	ortp_mutex_unlock(&liblinphone_log_collection_mutex);
}

aymeric's avatar
aymeric committed
1073 1074
void linphone_core_enable_logs(FILE *file){
	if (file==NULL) file=stdout;
Simon Morlat's avatar
Simon Morlat committed
1075
	linphone_core_set_log_file(file);
1076
	linphone_core_set_log_level(ORTP_MESSAGE);
aymeric's avatar
aymeric committed
1077 1078 1079
}

void linphone_core_enable_logs_with_cb(OrtpLogFunc logfunc){
1080
	_linphone_core_set_log_handler(logfunc);
1081
	linphone_core_set_log_level(ORTP_MESSAGE);
aymeric's avatar
aymeric committed
1082 1083
}

1084
void linphone_core_disable_logs(void){
1085
	linphone_core_set_log_level(ORTP_ERROR);
aymeric's avatar
aymeric committed
1086 1087
}

1088 1089 1090 1091
void linphone_core_serialize_logs(void) {
	liblinphone_serialize_logs = TRUE;
}

aymeric's avatar
aymeric committed
1092

1093
static void net_config_read(LinphoneCore *lc) {
aymeric's avatar
aymeric committed
1094 1095 1096
	int tmp;
	const char *tmpstr;
	LpConfig *config=lc->config;
1097 1098 1099 1100
	const char *nat_policy_ref;

	nat_policy_ref = lp_config_get_string(lc->config, "net", "nat_policy_ref", NULL);
	if (nat_policy_ref != NULL) {
1101 1102 1103
		LinphoneNatPolicy *nat_policy = linphone_core_create_nat_policy_from_config(lc, nat_policy_ref);
		linphone_core_set_nat_policy(lc, nat_policy);
		linphone_nat_policy_unref(nat_policy);
1104
	}
1105 1106 1107 1108
	if (lc->nat_policy == NULL){
		/*this will create a default nat policy according to deprecated config keys, or an empty nat policy otherwise*/
		linphone_core_set_firewall_policy(lc, linphone_core_get_firewall_policy(lc));
	}
aymeric's avatar
aymeric committed
1109

1110
	lc->net_conf.nat_address_ip = NULL;
aymeric's avatar
aymeric committed
1111 1112 1113 1114
	tmp=lp_config_get_int(config,"net","download_bw",0);
	linphone_core_set_download_bandwidth(lc,tmp);
	tmp=lp_config_get_int(config,"net","upload_bw",0);
	linphone_core_set_upload_bandwidth(lc,tmp);
1115 1116
	tmp=lp_config_get_int(config, "net", "expected_bw", 0);
	linphone_core_set_expected_bandwidth(lc, tmp);
1117

aymeric's avatar
aymeric committed
1118 1119 1120 1121
	tmpstr=lp_config_get_string(lc->config,"net","nat_address",NULL);
	if (tmpstr!=NULL && (strlen(tmpstr)<1)) tmpstr=NULL;
	linphone_core_set_nat_address(lc,tmpstr);
	tmp=lp_config_get_int(lc->config,"net","nat_sdp_only",0);
1122
	lc->net_conf.nat_sdp_only=!!tmp;
1123
	tmp=lp_config_get_int(lc->config,"net","mtu",1300);
aymeric's avatar
aymeric committed
1124
	linphone_core_set_mtu(lc,tmp);
1125 1126 1127 1128 1129
	tmp=lp_config_get_int(lc->config,"net","download_ptime",-1);
	if (tmp !=-1 && linphone_core_get_download_ptime(lc) !=0) {
		/*legacy parameter*/
		linphone_core_set_download_ptime(lc,tmp);
	}
1130
	tmp = lp_config_get_int(lc->config, "net", "dns_srv_enabled", 1);
1131
	linphone_core_enable_dns_srv(lc, !!tmp);
1132
	tmp = lp_config_get_int(lc->config, "net", "dns_search_enabled", 1);
1133
	linphone_core_enable_dns_search(lc, !!tmp);
aymeric's avatar
aymeric committed
1134 1135
}

1136 1137 1138
static void build_sound_devices_table(LinphoneCore *lc){
	const char **devices;
	const char **old;
1139
	size_t ndev;
1140
	int i;
1141 1142
	const bctbx_list_t *elem=ms_snd_card_manager_get_list(ms_factory_get_snd_card_manager(lc->factory));
	ndev=bctbx_list_size(elem);
1143
	devices=reinterpret_cast<const char **>(ms_malloc((ndev+1)*sizeof(const char *)));
1144 1145 1146 1147 1148 1149
	for (i=0;elem!=NULL;elem=elem->next,i++){
		devices[i]=ms_snd_card_get_string_id((MSSndCard *)elem->data);
	}
	devices[ndev]=NULL;
	old=lc->sound_conf.cards;
	lc->sound_conf.cards=devices;
1150
	if (old!=NULL) ms_free((void *)old);
1151
}
aymeric's avatar
aymeric committed
1152

1153 1154 1155 1156
static char *get_default_local_ring(LinphoneCore * lc) {
	LinphoneFactory *factory = linphone_factory_get();
	if (linphone_core_file_format_supported(lc, "mkv")) {
		return bctbx_strdup_printf("%s/%s", linphone_factory_get_ring_resources_dir(factory), LOCAL_RING_MKV);
1157
	}
1158
	return bctbx_strdup_printf("%s/%s", linphone_factory_get_ring_resources_dir(factory), LOCAL_RING_WAV);
1159 1160
}

1161 1162 1163 1164
static char *get_default_onhold_music(LinphoneCore * lc) {
	LinphoneFactory *factory = linphone_factory_get();
	if (linphone_core_file_format_supported(lc, "mkv")) {
		return bctbx_strdup_printf("%s/%s", linphone_factory_get_sound_resources_dir(factory), HOLD_MUSIC_MKV);
1165
	}
1166
	return bctbx_strdup_printf("%s/%s", linphone_factory_get_sound_resources_dir(factory), HOLD_MUSIC_WAV);
1167 1168
}

1169
static void sound_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
1170
{
1171
	int tmp;
1172
	char *default_remote_ring;
aymeric's avatar
aymeric committed
1173 1174
	const char *tmpbuf;
	const char *devid;
1175 1176
	LinphoneFactory *factory = linphone_factory_get();

1177
#ifdef __linux
aymeric's avatar
aymeric committed
1178 1179 1180
	/*alsadev let the user use custom alsa device within linphone*/
	devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
	if (devid){
1181 1182 1183
		MSSndCard* card;
		const char* delim=",";
		size_t l=strlen(devid);
1184
		char* d=reinterpret_cast<char *>(malloc(l+1));
1185 1186 1187 1188 1189 1190
		char* i;
		memcpy(d,devid,l+1);
		for (l=0,i=strpbrk(d+l,delim);i;i=strpbrk(d+l,delim)){
			char s=*i;
			*i='\0';
			card=ms_alsa_card_new_custom(d+l,d+l);
1191
			ms_snd_card_manager_add_card(ms_factory_get_snd_card_manager(lc->factory),card);
1192
			*i=s;
1193
			l=(size_t)(i-d)+1;
1194 1195 1196
		}
		if(d[l]!='\0') {
			card=ms_alsa_card_new_custom(d+l,d+l);
1197
			ms_snd_card_manager_add_card(ms_factory_get_snd_card_manager(lc->factory),card);
1198 1199
		}
		free(d);
aymeric's avatar
aymeric committed
1200
	}
1201
	tmp=lp_config_get_int(lc->config,"sound","alsa_forced_rate",-1);
1202
	if (tmp>0) ms_alsa_card_set_forced_sample_rate(tmp);
aymeric's avatar
aymeric committed
1203 1204
#endif
	/* retrieve all sound devices */
1205 1206
	build_sound_devices_table(lc);

aymeric's avatar
aymeric committed
1207 1208
	devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
	linphone_core_set_playback_device(lc,devid);
1209

aymeric's avatar
aymeric committed
1210 1211
	devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
	linphone_core_set_ringer_device(lc,devid);
1212

aymeric's avatar
aymeric committed
1213 1214
	devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
	linphone_core_set_capture_device(lc,devid);
1215

aymeric's avatar
aymeric committed
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
/*
	tmp=lp_config_get_int(lc->config,"sound","play_lev",80);
	linphone_core_set_play_level(lc,tmp);
	tmp=lp_config_get_int(lc->config,"sound","ring_lev",80);
	linphone_core_set_ring_level(lc,tmp);
	tmp=lp_config_get_int(lc->config,"sound","rec_lev",80);
	linphone_core_set_rec_level(lc,tmp);
	tmpbuf=lp_config_get_string(lc->config,"sound","source","m");
	linphone_core_set_sound_source(lc,tmpbuf[0]);
*/
1226

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
	tmpbuf = lp_config_get_string(lc->config, "sound", "local_ring", NULL);
	if (tmpbuf) {
		if (bctbx_file_exist(tmpbuf) == 0) {
			linphone_core_set_ring(lc, tmpbuf);
		} else {
			ms_warning("'%s' ring file does not exist", tmpbuf);
		}
	} else {
		char *default_local_ring = get_default_local_ring(lc);
		linphone_core_set_ring(lc, default_local_ring);
		bctbx_free(default_local_ring);
aymeric's avatar
aymeric committed
1238
	}
1239

1240 1241 1242 1243 1244
	default_remote_ring = bctbx_strdup_printf("%s/%s", linphone_factory_get_sound_resources_dir(factory), REMOTE_RING_WAV);
	tmpbuf = default_remote_ring;
	tmpbuf = lp_config_get_string(lc->config, "sound", "remote_ring", tmpbuf);
	if (bctbx_file_exist(tmpbuf) == -1){
		tmpbuf = default_remote_ring;
aymeric's avatar
aymeric committed
1245
	}
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
	if (strstr(tmpbuf, ".wav") == NULL) {
		/* It currently uses old sound files, so replace them */
		tmpbuf = default_remote_ring;
	}
	linphone_core_set_ringback(lc, tmpbuf);
	bctbx_free(default_remote_ring);

	tmpbuf = lp_config_get_string(lc->config, "sound", "hold_music", NULL);
	if (tmpbuf) {
		if (bctbx_file_exist(tmpbuf) == 0) {
			linphone_core_set_play_file(lc, tmpbuf);
		} else {
			ms_warning("'%s' on-hold music file does not exist", tmpbuf);
		}
	} else {
		char *default_onhold_music = get_default_onhold_music(lc);
		linphone_core_set_play_file(lc, default_onhold_music);
		bctbx_free(default_onhold_music);
aymeric's avatar
aymeric committed
1264
	}
1265

aymeric's avatar
aymeric committed
1266
	lc->sound_conf.latency=0;
Benjamin REIS's avatar
Benjamin REIS committed
1267
#if !TARGET_OS_IPHONE
Simon Morlat's avatar
Simon Morlat committed
1268
	tmp=TRUE;
1269
#else
Simon Morlat's avatar
Simon Morlat committed
1270
	tmp=FALSE; /* on iOS we have builtin echo cancellation.*/
1271
#endif
Simon Morlat's avatar
Simon Morlat committed
1272
	tmp=lp_config_get_int(lc->config,"sound","echocancellation",tmp