event.c 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
linphone
Copyright (C) 2000 - 2010 Simon MORLAT (simon.morlat@linphone.org)

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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "private.h"
21
#include "lpconfig.h"
22 23 24 25 26 27 28 29 30 31 32 33


LinphoneSubscriptionState linphone_subscription_state_from_sal(SalSubscribeStatus ss){
	switch(ss){
		case SalSubscribeNone: return LinphoneSubscriptionNone;
		case SalSubscribePending: return LinphoneSubscriptionPending;
		case SalSubscribeTerminated: return LinphoneSubscriptionTerminated;
		case SalSubscribeActive: return LinphoneSubscriptionActive;
	}
	return LinphoneSubscriptionNone;
}

34 35 36 37
const char *linphone_subscription_state_to_string(LinphoneSubscriptionState state){
	switch(state){
		case LinphoneSubscriptionNone: return "LinphoneSubscriptionNone";
		case LinphoneSubscriptionIncomingReceived: return "LinphoneSubscriptionIncomingReceived";
38
		case LinphoneSubscriptionOutgoingInit: return "LinphoneSubscriptionOutoingInit";
39 40 41 42
		case LinphoneSubscriptionPending: return "LinphoneSubscriptionPending";
		case LinphoneSubscriptionActive: return "LinphoneSubscriptionActive";
		case LinphoneSubscriptionTerminated: return "LinphoneSubscriptionTerminated";
		case LinphoneSubscriptionError: return "LinphoneSubscriptionError";
43
		case LinphoneSubscriptionExpiring: return "LinphoneSubscriptionExpiring";
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	}
	return NULL;
}

LINPHONE_PUBLIC const char *linphone_publish_state_to_string(LinphonePublishState state){
	switch(state){
		case LinphonePublishNone: return "LinphonePublishNone";
		case LinphonePublishProgress: return "LinphonePublishProgress";
		case LinphonePublishOk: return "LinphonePublishOk";
		case LinphonePublishError: return "LinphonePublishError";
		case LinphonePublishCleared: return "LinphonePublishCleared";
		case LinphonePublishExpiring: return "LinphonePublishExpiring";
	}
	return NULL;
}

60
static LinphoneEvent * linphone_event_new_base(LinphoneCore *lc, LinphoneSubscriptionDir dir, const char *name, SalOp *op){
61 62 63
	LinphoneEvent *lev=ms_new0(LinphoneEvent,1);
	lev->lc=lc;
	lev->dir=dir;
64 65
	lev->op=op;
	lev->refcnt=1;
Simon Morlat's avatar
Simon Morlat committed
66
	lev->name=ms_strdup(name);
67 68 69 70
	sal_op_set_user_pointer(lev->op,lev);
	return lev;
}

71
LinphoneEvent *linphone_event_new(LinphoneCore *lc, LinphoneSubscriptionDir dir, const char *name, int expires){
72
	LinphoneEvent *lev=linphone_event_new_base(lc, dir, name, sal_op_new(lc->sal));
73
	lev->expires=expires;
74 75 76
	return lev;
}

77
static LinphoneEvent *linphone_event_new_with_op_base(LinphoneCore *lc, SalOp *op, LinphoneSubscriptionDir dir, const char *name, bool_t is_out_of_dialog){
78
	LinphoneEvent *lev=linphone_event_new_base(lc, dir, name, op);
79
	lev->is_out_of_dialog_op=is_out_of_dialog;
80 81
	return lev;
}
Simon Morlat's avatar
Simon Morlat committed
82

83 84 85
LinphoneEvent *linphone_event_new_with_op(LinphoneCore *lc, SalOp *op, LinphoneSubscriptionDir dir, const char *name) {
	return linphone_event_new_with_op_base(lc,op,dir,name,FALSE);
}
Simon Morlat's avatar
Simon Morlat committed
86

87 88 89
LinphoneEvent *linphone_event_new_with_out_of_dialog_op(LinphoneCore *lc, SalOp *op, LinphoneSubscriptionDir dir, const char *name) {
	return linphone_event_new_with_op_base(lc,op,dir,name,TRUE);
}
Simon Morlat's avatar
Simon Morlat committed
90

91 92 93 94 95 96 97 98
void linphone_event_set_internal(LinphoneEvent *lev, bool_t internal) {
	lev->internal = internal;
}

bool_t linphone_event_is_internal(LinphoneEvent *lev) {
	return lev->internal;
}

99
void linphone_event_set_state(LinphoneEvent *lev, LinphoneSubscriptionState state){
100 101 102
	if (lev->subscription_state!=state){
		ms_message("LinphoneEvent [%p] moving to subscription state %s",lev,linphone_subscription_state_to_string(state));
		lev->subscription_state=state;
103
		linphone_core_notify_subscription_state_changed(lev->lc,lev,state);
104
		if (state==LinphoneSubscriptionTerminated){
105 106
			linphone_event_unref(lev);
		}
107 108 109
	}
}

110 111 112 113
void linphone_event_set_publish_state(LinphoneEvent *lev, LinphonePublishState state){
	if (lev->publish_state!=state){
		ms_message("LinphoneEvent [%p] moving to publish state %s",lev,linphone_publish_state_to_string(state));
		lev->publish_state=state;
114
		linphone_core_notify_publish_state_changed(lev->lc,lev,state);
Simon Morlat's avatar
Simon Morlat committed
115 116
		switch(state){
			case LinphonePublishCleared:
117 118
				if (lev->expires!=-1)
					linphone_event_unref(lev);
Simon Morlat's avatar
Simon Morlat committed
119 120 121 122 123 124 125 126 127 128 129
				break;
			case LinphonePublishOk:
			case LinphonePublishError:
				if (lev->expires==-1)
					linphone_event_unref(lev);
				break;
			case LinphonePublishNone:
			case LinphonePublishProgress:
			case LinphonePublishExpiring:
				/*nothing special to do*/
				break;
130
		}
131

132 133 134 135 136 137 138
	}
}

LinphonePublishState linphone_event_get_publish_state(const LinphoneEvent *lev){
	return lev->publish_state;
}

139 140
const LinphoneErrorInfo *linphone_event_get_error_info(const LinphoneEvent *lev){
	return linphone_error_info_from_sal_op(lev->op);
141 142 143
}

LinphoneReason linphone_event_get_reason(const LinphoneEvent *lev){
144
	return linphone_error_info_get_reason(linphone_event_get_error_info(lev));
145 146
}

147 148
LinphoneEvent *linphone_core_create_subscribe(LinphoneCore *lc, const LinphoneAddress *resource, const char *event, int expires){
	LinphoneEvent *lev=linphone_event_new(lc, LinphoneSubscriptionOutgoing, event, expires);
149
	linphone_configure_op(lc,lev->op,resource,NULL,TRUE);
150
	sal_op_set_manual_refresher_mode(lev->op,!lp_config_get_int(lc->config,"sip","refresh_generic_subscribe",1));
151 152 153
	return lev;
}

154 155 156 157 158
LinphoneEvent *linphone_core_subscribe(LinphoneCore *lc, const LinphoneAddress *resource, const char *event, int expires, const LinphoneContent *body){
	LinphoneEvent *lev=linphone_core_create_subscribe(lc,resource,event,expires);
	linphone_event_send_subscribe(lev,body);
	return lev;
}
159

160 161

int linphone_event_send_subscribe(LinphoneEvent *lev, const LinphoneContent *body){
162
	SalBodyHandler *body_handler;
163
	int err;
164

165 166 167 168
	if (lev->dir!=LinphoneSubscriptionOutgoing){
		ms_error("linphone_event_send_subscribe(): cannot send or update something that is not an outgoing subscription.");
		return -1;
	}
169 170 171
	switch (lev->subscription_state){
		case LinphoneSubscriptionIncomingReceived:
		case LinphoneSubscriptionTerminated:
172 173
		case LinphoneSubscriptionOutgoingInit:
			ms_error("linphone_event_send_subscribe(): cannot update subscription while in state [%s]", linphone_subscription_state_to_string(lev->subscription_state));
174 175
			return -1;
		break;
176
		case LinphoneSubscriptionNone:
177 178 179 180 181 182
		case LinphoneSubscriptionActive:
		case LinphoneSubscriptionExpiring:
		case LinphoneSubscriptionError:
		case LinphoneSubscriptionPending:
			/*those states are ok*/
		break;
183
	}
184

185 186 187 188
	if (lev->send_custom_headers){
		sal_op_set_sent_custom_header(lev->op,lev->send_custom_headers);
		lev->send_custom_headers=NULL;
	}else sal_op_set_sent_custom_header(lev->op,NULL);
189

190 191
	body_handler = sal_body_handler_from_content(body);
	err=sal_subscribe(lev->op,NULL,NULL,lev->name,lev->expires,body_handler);
192 193 194
	if (err==0){
		if (lev->subscription_state==LinphoneSubscriptionNone)
			linphone_event_set_state(lev,LinphoneSubscriptionOutgoingInit);
195
	}
196 197 198 199 200
	return err;
}

int linphone_event_update_subscribe(LinphoneEvent *lev, const LinphoneContent *body){
	return linphone_event_send_subscribe(lev,body);
201 202
}

203 204 205 206
int linphone_event_refresh_subscribe(LinphoneEvent *lev) {
	return sal_subscribe_refresh(lev->op);
}

207 208
int linphone_event_accept_subscription(LinphoneEvent *lev){
	int err;
209
	if (lev->subscription_state!=LinphoneSubscriptionIncomingReceived){
210 211 212 213 214 215 216 217 218 219 220
		ms_error("linphone_event_accept_subscription(): cannot accept subscription if subscription wasn't just received.");
		return -1;
	}
	err=sal_subscribe_accept(lev->op);
	if (err==0){
		linphone_event_set_state(lev,LinphoneSubscriptionActive);
	}
	return err;
}

int linphone_event_deny_subscription(LinphoneEvent *lev, LinphoneReason reason){
221
	int err;
222
	if (lev->subscription_state!=LinphoneSubscriptionIncomingReceived){
223 224 225
		ms_error("linphone_event_deny_subscription(): cannot deny subscription if subscription wasn't just received.");
		return -1;
	}
226 227 228
	err=sal_subscribe_decline(lev->op,linphone_reason_to_sal(reason));
	linphone_event_set_state(lev,LinphoneSubscriptionTerminated);
	return err;
229 230 231
}

int linphone_event_notify(LinphoneEvent *lev, const LinphoneContent *body){
232
	SalBodyHandler *body_handler;
233
	if (lev->subscription_state!=LinphoneSubscriptionActive){
234 235 236 237 238 239 240
		ms_error("linphone_event_notify(): cannot notify if subscription is not active.");
		return -1;
	}
	if (lev->dir!=LinphoneSubscriptionIncoming){
		ms_error("linphone_event_notify(): cannot notify if not an incoming subscription.");
		return -1;
	}
241 242
	body_handler = sal_body_handler_from_content(body);
	return sal_notify(lev->op, body_handler);
243 244
}

245 246
LinphoneEvent *linphone_core_create_publish(LinphoneCore *lc, const LinphoneAddress *resource, const char *event, int expires){
	LinphoneEvent *lev=linphone_event_new(lc,LinphoneSubscriptionInvalidDir, event,expires);
247
	linphone_configure_op(lc,lev->op,resource,NULL,lp_config_get_int(lc->config,"sip","publish_msg_with_contact",0));
248
	sal_op_set_manual_refresher_mode(lev->op,!lp_config_get_int(lc->config,"sip","refresh_generic_publish",1));
249 250 251
	return lev;
}

252
static int _linphone_event_send_publish(LinphoneEvent *lev, const LinphoneContent *body, bool_t notify_err){
253
	SalBodyHandler *body_handler;
254
	int err;
255

256
	if (lev->dir!=LinphoneSubscriptionInvalidDir){
257 258 259
		ms_error("linphone_event_update_publish(): this is not a PUBLISH event.");
		return -1;
	}
260 261 262 263
	if (lev->send_custom_headers){
		sal_op_set_sent_custom_header(lev->op,lev->send_custom_headers);
		lev->send_custom_headers=NULL;
	}else sal_op_set_sent_custom_header(lev->op,NULL);
264 265
	body_handler = sal_body_handler_from_content(body);
	err=sal_publish(lev->op,NULL,NULL,lev->name,lev->expires,body_handler);
266 267
	if (err==0){
		linphone_event_set_publish_state(lev,LinphonePublishProgress);
268
	}else if (notify_err){
269 270 271
		linphone_event_set_publish_state(lev,LinphonePublishError);
	}
	return err;
272 273
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
LinphoneEvent *linphone_core_publish(LinphoneCore *lc, const LinphoneAddress *resource, const char *event, int expires, const LinphoneContent *body){
	int err;
	LinphoneEvent *lev=linphone_core_create_publish(lc,resource,event,expires);
	err=_linphone_event_send_publish(lev,body,FALSE);
	if (err==-1){
		linphone_event_unref(lev);
		lev=NULL;
	}
	return lev;
}


int linphone_event_send_publish(LinphoneEvent *lev, const LinphoneContent *body){
	return _linphone_event_send_publish(lev,body,TRUE);
}

int linphone_event_update_publish(LinphoneEvent* lev, const LinphoneContent* body ) {
	return linphone_event_send_publish(lev,body);
}

294
void linphone_event_set_user_data(LinphoneEvent *ev, void *up){
295 296 297
	ev->userdata=up;
}

298
void *linphone_event_get_user_data(const LinphoneEvent *ev){
299 300 301
	return ev->userdata;
}

302 303 304 305 306 307 308 309 310 311
void linphone_event_add_custom_header(LinphoneEvent *ev, const char *name, const char *value){
	ev->send_custom_headers=sal_custom_header_append(ev->send_custom_headers, name, value);
}

const char* linphone_event_get_custom_header(LinphoneEvent* ev, const char* name){
	const SalCustomHeader *ch=sal_op_get_recv_custom_header(ev->op);
	return sal_custom_header_find(ch,name);
}


312
void linphone_event_terminate(LinphoneEvent *lev){
313
	lev->terminating=TRUE;
314 315 316 317 318
	if (lev->dir==LinphoneSubscriptionIncoming){
		sal_notify_close(lev->op);
	}else if (lev->dir==LinphoneSubscriptionOutgoing){
		sal_unsubscribe(lev->op);
	}
319

320
	if (lev->publish_state!=LinphonePublishNone){
321
		if (lev->publish_state==LinphonePublishOk && lev->expires!=-1){
322
			sal_publish(lev->op,NULL,NULL,NULL,0,NULL);
323 324
		}else sal_op_stop_refreshing(lev->op);
		linphone_event_set_publish_state(lev,LinphonePublishCleared);
325 326
		return;
	}
327

328
	if (lev->subscription_state!=LinphoneSubscriptionNone){
329
		linphone_event_set_state(lev,LinphoneSubscriptionTerminated);
330
		return;
331 332 333
	}
}

334 335 336 337 338 339 340

LinphoneEvent *linphone_event_ref(LinphoneEvent *lev){
	lev->refcnt++;
	return lev;
}

static void linphone_event_destroy(LinphoneEvent *lev){
341 342
	if (lev->op)
		sal_op_release(lev->op);
343
	ms_free(lev->name);
344 345 346
	ms_free(lev);
}

347 348 349 350 351
void linphone_event_unref(LinphoneEvent *lev){
	lev->refcnt--;
	if (lev->refcnt==0) linphone_event_destroy(lev);
}

Simon Morlat's avatar
Simon Morlat committed
352
LinphoneSubscriptionDir linphone_event_get_subscription_dir(LinphoneEvent *lev){
353 354
	return lev->dir;
}
355 356

LinphoneSubscriptionState linphone_event_get_subscription_state(const LinphoneEvent *lev){
357
	return lev->subscription_state;
358 359 360 361 362 363
}

const char *linphone_event_get_name(const LinphoneEvent *lev){
	return lev->name;
}

364
const LinphoneAddress *linphone_event_get_from(const LinphoneEvent *lev){
365 366 367 368 369
	if (lev->is_out_of_dialog_op){
		return (LinphoneAddress*)sal_op_get_to_address(lev->op);
	}else{
		return (LinphoneAddress*)sal_op_get_from_address(lev->op);
	}
370 371 372
}

const LinphoneAddress *linphone_event_get_resource(const LinphoneEvent *lev){
373 374 375 376 377
	if (lev->is_out_of_dialog_op){
		return (LinphoneAddress*)sal_op_get_from_address(lev->op);
	}else{
		return (LinphoneAddress*)sal_op_get_to_address(lev->op);
	}
378 379
}

380 381 382 383
LinphoneCore *linphone_event_get_core(const LinphoneEvent *lev){
	return lev->lc;
}