blob: effa401057005871254faaf82fad0f660b13f2d1 [file] [log] [blame]
Benny Prijono834aee32006-02-19 01:38:06 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono834aee32006-02-19 01:38:06 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Benny Prijono4f9f64e2006-02-27 00:00:30 +000020#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000021#include <pjsua-lib/pjsua_internal.h>
Benny Prijono834aee32006-02-19 01:38:06 +000022
Benny Prijono834aee32006-02-19 01:38:06 +000023
24#define THIS_FILE "pjsua_pres.c"
25
Benny Prijonoa17496a2007-10-31 10:20:31 +000026
Benny Prijono73bb7232009-10-20 13:56:26 +000027static void subscribe_buddy_presence(pjsua_buddy_id buddy_id);
28static void unsubscribe_buddy_presence(pjsua_buddy_id buddy_id);
Benny Prijonoa17496a2007-10-31 10:20:31 +000029
30
31/*
32 * Find buddy.
33 */
Benny Prijono73bb7232009-10-20 13:56:26 +000034static pjsua_buddy_id find_buddy(const pjsip_uri *uri)
Benny Prijonoa17496a2007-10-31 10:20:31 +000035{
36 const pjsip_sip_uri *sip_uri;
37 unsigned i;
38
Benny Prijonof0f8fd12007-11-10 12:05:59 +000039 uri = (const pjsip_uri*) pjsip_uri_get_uri((pjsip_uri*)uri);
Benny Prijonoa17496a2007-10-31 10:20:31 +000040
41 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
42 return PJSUA_INVALID_ID;
43
44 sip_uri = (const pjsip_sip_uri*) uri;
45
46 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
47 const pjsua_buddy *b = &pjsua_var.buddy[i];
48
49 if (!pjsua_buddy_is_valid(i))
50 continue;
51
52 if (pj_stricmp(&sip_uri->user, &b->name)==0 &&
53 pj_stricmp(&sip_uri->host, &b->host)==0 &&
54 (sip_uri->port==(int)b->port || (sip_uri->port==0 && b->port==5060)))
55 {
56 /* Match */
57 return i;
58 }
59 }
60
61 return PJSUA_INVALID_ID;
62}
Benny Prijono7a5f5102007-05-29 00:33:09 +000063
Benny Prijono73bb7232009-10-20 13:56:26 +000064#define LOCK_DIALOG 1
65#define LOCK_PJSUA 2
66#define LOCK_ALL (LOCK_DIALOG | LOCK_PJSUA)
67
68/* Buddy lock object */
69struct buddy_lock
70{
71 pjsua_buddy *buddy;
72 pjsip_dialog *dlg;
73 pj_uint8_t flag;
74};
75
76/* Acquire lock to the specified buddy_id */
77pj_status_t lock_buddy(const char *title,
78 pjsua_buddy_id buddy_id,
79 struct buddy_lock *lck,
80 unsigned _unused_)
81{
82 enum { MAX_RETRY=50 };
83 pj_bool_t has_pjsua_lock = PJ_FALSE;
84 unsigned retry;
85
86 PJ_UNUSED_ARG(_unused_);
87
88 pj_bzero(lck, sizeof(*lck));
89
90 for (retry=0; retry<MAX_RETRY; ++retry) {
91
92 if (PJSUA_TRY_LOCK() != PJ_SUCCESS) {
93 pj_thread_sleep(retry/10);
94 continue;
95 }
96
97 has_pjsua_lock = PJ_TRUE;
98 lck->flag = LOCK_PJSUA;
99 lck->buddy = &pjsua_var.buddy[buddy_id];
100
101 if (lck->buddy->dlg == NULL)
102 return PJ_SUCCESS;
103
104 if (pjsip_dlg_try_inc_lock(lck->buddy->dlg) != PJ_SUCCESS) {
105 lck->flag = 0;
106 lck->buddy = NULL;
107 has_pjsua_lock = PJ_FALSE;
108 PJSUA_UNLOCK();
109 pj_thread_sleep(retry/10);
110 continue;
111 }
112
113 lck->dlg = lck->buddy->dlg;
114 lck->flag = LOCK_DIALOG;
115 PJSUA_UNLOCK();
116
117 break;
118 }
119
120 if (lck->flag == 0) {
121 if (has_pjsua_lock == PJ_FALSE)
122 PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex "
123 "(possibly system has deadlocked) in %s",
124 title));
125 else
126 PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex "
127 "(possibly system has deadlocked) in %s",
128 title));
129 return PJ_ETIMEDOUT;
130 }
131
132 return PJ_SUCCESS;
133}
134
135/* Release buddy lock */
136static void unlock_buddy(struct buddy_lock *lck)
137{
138 if (lck->flag & LOCK_DIALOG)
139 pjsip_dlg_dec_lock(lck->dlg);
140
141 if (lck->flag & LOCK_PJSUA)
142 PJSUA_UNLOCK();
143}
144
Benny Prijono834aee32006-02-19 01:38:06 +0000145
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000146/*
147 * Get total number of buddies.
148 */
149PJ_DEF(unsigned) pjsua_get_buddy_count(void)
150{
151 return pjsua_var.buddy_cnt;
152}
Benny Prijono834aee32006-02-19 01:38:06 +0000153
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000154
155/*
Benny Prijono705e7842008-07-21 18:12:51 +0000156 * Find buddy.
157 */
158PJ_DEF(pjsua_buddy_id) pjsua_buddy_find(const pj_str_t *uri_str)
159{
160 pj_str_t input;
161 pj_pool_t *pool;
162 pjsip_uri *uri;
163 pjsua_buddy_id buddy_id;
164
165 pool = pjsua_pool_create("buddyfind", 512, 512);
166 pj_strdup_with_null(pool, &input, uri_str);
167
168 uri = pjsip_parse_uri(pool, input.ptr, input.slen, 0);
169 if (!uri)
170 buddy_id = PJSUA_INVALID_ID;
Benny Prijono73bb7232009-10-20 13:56:26 +0000171 else {
172 PJSUA_LOCK();
173 buddy_id = find_buddy(uri);
174 PJSUA_UNLOCK();
175 }
Benny Prijono705e7842008-07-21 18:12:51 +0000176
177 pj_pool_release(pool);
178
179 return buddy_id;
180}
181
182
183/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000184 * Check if buddy ID is valid.
185 */
186PJ_DEF(pj_bool_t) pjsua_buddy_is_valid(pjsua_buddy_id buddy_id)
187{
Benny Prijonoa1e69682007-05-11 15:14:34 +0000188 return buddy_id>=0 && buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy) &&
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000189 pjsua_var.buddy[buddy_id].uri.slen != 0;
190}
191
192
193/*
194 * Enum buddy IDs.
195 */
196PJ_DEF(pj_status_t) pjsua_enum_buddies( pjsua_buddy_id ids[],
197 unsigned *count)
198{
199 unsigned i, c;
200
201 PJ_ASSERT_RETURN(ids && count, PJ_EINVAL);
202
203 PJSUA_LOCK();
204
205 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
206 if (!pjsua_var.buddy[i].uri.slen)
207 continue;
208 ids[c] = i;
209 ++c;
210 }
211
212 *count = c;
213
214 PJSUA_UNLOCK();
215
216 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000217}
218
219
220/*
221 * Get detailed buddy info.
222 */
223PJ_DEF(pj_status_t) pjsua_buddy_get_info( pjsua_buddy_id buddy_id,
224 pjsua_buddy_info *info)
225{
Benny Prijono20da7992008-12-18 16:48:43 +0000226 unsigned total=0;
Benny Prijono73bb7232009-10-20 13:56:26 +0000227 struct buddy_lock lck;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000228 pjsua_buddy *buddy;
Benny Prijono73bb7232009-10-20 13:56:26 +0000229 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000230
Benny Prijono73bb7232009-10-20 13:56:26 +0000231 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000232
Benny Prijonoac623b32006-07-03 15:19:31 +0000233 pj_bzero(info, sizeof(pjsua_buddy_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000234
Benny Prijono73bb7232009-10-20 13:56:26 +0000235 status = lock_buddy("pjsua_buddy_get_info()", buddy_id, &lck, 0);
236 if (status != PJ_SUCCESS)
237 return status;
238
239 buddy = lck.buddy;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000240 info->id = buddy->index;
241 if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
Benny Prijono73bb7232009-10-20 13:56:26 +0000242 unlock_buddy(&lck);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000243 return PJ_SUCCESS;
244 }
245
246 /* uri */
247 info->uri.ptr = info->buf_ + total;
248 pj_strncpy(&info->uri, &buddy->uri, sizeof(info->buf_)-total);
249 total += info->uri.slen;
250
251 /* contact */
252 info->contact.ptr = info->buf_ + total;
253 pj_strncpy(&info->contact, &buddy->contact, sizeof(info->buf_)-total);
254 total += info->contact.slen;
Benny Prijono97276602007-06-23 01:07:08 +0000255
Benny Prijono28add7e2009-06-15 16:03:40 +0000256 /* Presence status */
257 pj_memcpy(&info->pres_status, &buddy->status, sizeof(pjsip_pres_status));
258
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000259 /* status and status text */
260 if (buddy->sub == NULL || buddy->status.info_cnt==0) {
261 info->status = PJSUA_BUDDY_STATUS_UNKNOWN;
262 info->status_text = pj_str("?");
263 } else if (pjsua_var.buddy[buddy_id].status.info[0].basic_open) {
264 info->status = PJSUA_BUDDY_STATUS_ONLINE;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000265
266 /* copy RPID information */
267 info->rpid = buddy->status.info[0].rpid;
268
269 if (info->rpid.note.slen)
270 info->status_text = info->rpid.note;
271 else
272 info->status_text = pj_str("Online");
273
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000274 } else {
275 info->status = PJSUA_BUDDY_STATUS_OFFLINE;
Benny Prijono73bb7232009-10-20 13:56:26 +0000276 info->rpid = buddy->status.info[0].rpid;
277
278 if (info->rpid.note.slen)
279 info->status_text = info->rpid.note;
280 else
281 info->status_text = pj_str("Offline");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000282 }
283
284 /* monitor pres */
285 info->monitor_pres = buddy->monitor;
286
Benny Prijono63fba012008-07-17 14:19:10 +0000287 /* subscription state and termination reason */
Benny Prijono73bb7232009-10-20 13:56:26 +0000288 info->sub_term_code = buddy->term_code;
Benny Prijono63fba012008-07-17 14:19:10 +0000289 if (buddy->sub) {
290 info->sub_state = pjsip_evsub_get_state(buddy->sub);
Benny Prijonod06d8c52009-06-30 13:53:47 +0000291 info->sub_state_name = pjsip_evsub_get_state_name(buddy->sub);
Benny Prijono63fba012008-07-17 14:19:10 +0000292 if (info->sub_state == PJSIP_EVSUB_STATE_TERMINATED &&
293 total < sizeof(info->buf_))
294 {
295 info->sub_term_reason.ptr = info->buf_ + total;
296 pj_strncpy(&info->sub_term_reason,
297 pjsip_evsub_get_termination_reason(buddy->sub),
298 sizeof(info->buf_) - total);
299 total += info->sub_term_reason.slen;
300 } else {
301 info->sub_term_reason = pj_str("");
302 }
303 } else if (total < sizeof(info->buf_)) {
Benny Prijonod06d8c52009-06-30 13:53:47 +0000304 info->sub_state_name = "NULL";
Benny Prijono63fba012008-07-17 14:19:10 +0000305 info->sub_term_reason.ptr = info->buf_ + total;
306 pj_strncpy(&info->sub_term_reason, &buddy->term_reason,
307 sizeof(info->buf_) - total);
308 total += info->sub_term_reason.slen;
309 } else {
Benny Prijonod06d8c52009-06-30 13:53:47 +0000310 info->sub_state_name = "NULL";
Benny Prijono63fba012008-07-17 14:19:10 +0000311 info->sub_term_reason = pj_str("");
312 }
313
Benny Prijono73bb7232009-10-20 13:56:26 +0000314 unlock_buddy(&lck);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000315 return PJ_SUCCESS;
316}
317
Benny Prijono705e7842008-07-21 18:12:51 +0000318/*
319 * Set the user data associated with the buddy object.
320 */
321PJ_DEF(pj_status_t) pjsua_buddy_set_user_data( pjsua_buddy_id buddy_id,
322 void *user_data)
323{
Benny Prijono73bb7232009-10-20 13:56:26 +0000324 struct buddy_lock lck;
325 pj_status_t status;
Benny Prijono705e7842008-07-21 18:12:51 +0000326
Benny Prijono73bb7232009-10-20 13:56:26 +0000327 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
328
329 status = lock_buddy("pjsua_buddy_set_user_data()", buddy_id, &lck, 0);
330 if (status != PJ_SUCCESS)
331 return status;
Benny Prijono705e7842008-07-21 18:12:51 +0000332
333 pjsua_var.buddy[buddy_id].user_data = user_data;
334
Benny Prijono73bb7232009-10-20 13:56:26 +0000335 unlock_buddy(&lck);
Benny Prijono705e7842008-07-21 18:12:51 +0000336
337 return PJ_SUCCESS;
338}
339
340
341/*
342 * Get the user data associated with the budy object.
343 */
344PJ_DEF(void*) pjsua_buddy_get_user_data(pjsua_buddy_id buddy_id)
345{
Benny Prijono73bb7232009-10-20 13:56:26 +0000346 struct buddy_lock lck;
347 pj_status_t status;
Benny Prijono705e7842008-07-21 18:12:51 +0000348 void *user_data;
349
Benny Prijono73bb7232009-10-20 13:56:26 +0000350 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), NULL);
Benny Prijono705e7842008-07-21 18:12:51 +0000351
Benny Prijono73bb7232009-10-20 13:56:26 +0000352 status = lock_buddy("pjsua_buddy_get_user_data()", buddy_id, &lck, 0);
353 if (status != PJ_SUCCESS)
354 return NULL;
Benny Prijono705e7842008-07-21 18:12:51 +0000355
356 user_data = pjsua_var.buddy[buddy_id].user_data;
357
Benny Prijono73bb7232009-10-20 13:56:26 +0000358 unlock_buddy(&lck);
Benny Prijono705e7842008-07-21 18:12:51 +0000359
360 return user_data;
361}
362
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363
364/*
365 * Reset buddy descriptor.
366 */
367static void reset_buddy(pjsua_buddy_id id)
368{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000369 pj_pool_t *pool = pjsua_var.buddy[id].pool;
Benny Prijonoac623b32006-07-03 15:19:31 +0000370 pj_bzero(&pjsua_var.buddy[id], sizeof(pjsua_var.buddy[id]));
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000371 pjsua_var.buddy[id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000372 pjsua_var.buddy[id].index = id;
373}
374
375
376/*
377 * Add new buddy.
378 */
379PJ_DEF(pj_status_t) pjsua_buddy_add( const pjsua_buddy_config *cfg,
380 pjsua_buddy_id *p_buddy_id)
381{
382 pjsip_name_addr *url;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000383 pjsua_buddy *buddy;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000384 pjsip_sip_uri *sip_uri;
385 int index;
386 pj_str_t tmp;
387
388 PJ_ASSERT_RETURN(pjsua_var.buddy_cnt <=
389 PJ_ARRAY_SIZE(pjsua_var.buddy),
390 PJ_ETOOMANY);
391
392 PJSUA_LOCK();
393
394 /* Find empty slot */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000395 for (index=0; index<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++index) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000396 if (pjsua_var.buddy[index].uri.slen == 0)
397 break;
398 }
399
400 /* Expect to find an empty slot */
401 if (index == PJ_ARRAY_SIZE(pjsua_var.buddy)) {
402 PJSUA_UNLOCK();
403 /* This shouldn't happen */
404 pj_assert(!"index < PJ_ARRAY_SIZE(pjsua_var.buddy)");
405 return PJ_ETOOMANY;
406 }
407
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000408 buddy = &pjsua_var.buddy[index];
409
410 /* Create pool for this buddy */
411 if (buddy->pool) {
412 pj_pool_reset(buddy->pool);
413 } else {
414 char name[PJ_MAX_OBJ_NAME];
415 pj_ansi_snprintf(name, sizeof(name), "buddy%03d", index);
416 buddy->pool = pjsua_pool_create(name, 512, 256);
417 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000418
Benny Prijono63fba012008-07-17 14:19:10 +0000419 /* Init buffers for presence subscription status */
420 buddy->term_reason.ptr = (char*)
421 pj_pool_alloc(buddy->pool,
422 PJSUA_BUDDY_SUB_TERM_REASON_LEN);
423
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000424 /* Get name and display name for buddy */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000425 pj_strdup_with_null(buddy->pool, &tmp, &cfg->uri);
426 url = (pjsip_name_addr*)pjsip_parse_uri(buddy->pool, tmp.ptr, tmp.slen,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000427 PJSIP_PARSE_URI_AS_NAMEADDR);
428
429 if (url == NULL) {
430 pjsua_perror(THIS_FILE, "Unable to add buddy", PJSIP_EINVALIDURI);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000431 pj_pool_release(buddy->pool);
432 buddy->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000433 PJSUA_UNLOCK();
434 return PJSIP_EINVALIDURI;
435 }
436
Benny Prijonofc493592007-02-18 20:56:32 +0000437 /* Only support SIP schemes */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000438 if (!PJSIP_URI_SCHEME_IS_SIP(url) && !PJSIP_URI_SCHEME_IS_SIPS(url)) {
439 pj_pool_release(buddy->pool);
440 buddy->pool = NULL;
441 PJSUA_UNLOCK();
Benny Prijonofc493592007-02-18 20:56:32 +0000442 return PJSIP_EINVALIDSCHEME;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000443 }
Benny Prijonofc493592007-02-18 20:56:32 +0000444
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000445 /* Reset buddy, to make sure everything is cleared with default
446 * values
447 */
448 reset_buddy(index);
449
450 /* Save URI */
451 pjsua_var.buddy[index].uri = tmp;
452
Benny Prijono9c1528f2007-02-10 19:22:25 +0000453 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(url->uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000454 pjsua_var.buddy[index].name = sip_uri->user;
455 pjsua_var.buddy[index].display = url->display;
456 pjsua_var.buddy[index].host = sip_uri->host;
457 pjsua_var.buddy[index].port = sip_uri->port;
458 pjsua_var.buddy[index].monitor = cfg->subscribe;
459 if (pjsua_var.buddy[index].port == 0)
460 pjsua_var.buddy[index].port = 5060;
461
Benny Prijono705e7842008-07-21 18:12:51 +0000462 /* Save user data */
463 pjsua_var.buddy[index].user_data = (void*)cfg->user_data;
464
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000465 if (p_buddy_id)
466 *p_buddy_id = index;
467
468 pjsua_var.buddy_cnt++;
469
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000470 PJSUA_UNLOCK();
471
Benny Prijonof9c40c32007-06-28 07:20:26 +0000472 pjsua_buddy_subscribe_pres(index, cfg->subscribe);
473
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000474 return PJ_SUCCESS;
475}
476
477
478/*
479 * Delete buddy.
480 */
481PJ_DEF(pj_status_t) pjsua_buddy_del(pjsua_buddy_id buddy_id)
482{
Benny Prijono73bb7232009-10-20 13:56:26 +0000483 struct buddy_lock lck;
484 pj_status_t status;
485
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000486 PJ_ASSERT_RETURN(buddy_id>=0 &&
487 buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy),
488 PJ_EINVAL);
489
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000490 if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000491 return PJ_SUCCESS;
492 }
493
Benny Prijono73bb7232009-10-20 13:56:26 +0000494 status = lock_buddy("pjsua_buddy_del()", buddy_id, &lck, 0);
495 if (status != PJ_SUCCESS)
496 return status;
497
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498 /* Unsubscribe presence */
499 pjsua_buddy_subscribe_pres(buddy_id, PJ_FALSE);
500
Benny Prijonoa5776cb2009-04-14 15:11:23 +0000501 /* Not interested with further events for this buddy */
502 if (pjsua_var.buddy[buddy_id].sub) {
503 pjsip_evsub_set_mod_data(pjsua_var.buddy[buddy_id].sub,
504 pjsua_var.mod.id, NULL);
505 }
506
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000507 /* Remove buddy */
508 pjsua_var.buddy[buddy_id].uri.slen = 0;
509 pjsua_var.buddy_cnt--;
510
511 /* Reset buddy struct */
512 reset_buddy(buddy_id);
513
Benny Prijono73bb7232009-10-20 13:56:26 +0000514 unlock_buddy(&lck);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000515 return PJ_SUCCESS;
516}
517
518
519/*
520 * Enable/disable buddy's presence monitoring.
521 */
522PJ_DEF(pj_status_t) pjsua_buddy_subscribe_pres( pjsua_buddy_id buddy_id,
523 pj_bool_t subscribe)
524{
Benny Prijono73bb7232009-10-20 13:56:26 +0000525 struct buddy_lock lck;
526 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000527
Benny Prijono73bb7232009-10-20 13:56:26 +0000528 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000529
Benny Prijono73bb7232009-10-20 13:56:26 +0000530 status = lock_buddy("pjsua_buddy_subscribe_pres()", buddy_id, &lck, 0);
531 if (status != PJ_SUCCESS)
532 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000533
Benny Prijono73bb7232009-10-20 13:56:26 +0000534 lck.buddy->monitor = subscribe;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000535
Benny Prijono73bb7232009-10-20 13:56:26 +0000536 pjsua_buddy_update_pres(buddy_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537
Benny Prijono73bb7232009-10-20 13:56:26 +0000538 unlock_buddy(&lck);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000539 return PJ_SUCCESS;
540}
541
542
543/*
Benny Prijono10861432007-10-31 10:54:53 +0000544 * Update buddy's presence.
545 */
546PJ_DEF(pj_status_t) pjsua_buddy_update_pres(pjsua_buddy_id buddy_id)
547{
Benny Prijono73bb7232009-10-20 13:56:26 +0000548 struct buddy_lock lck;
549 pj_status_t status;
Benny Prijono10861432007-10-31 10:54:53 +0000550
Benny Prijono73bb7232009-10-20 13:56:26 +0000551 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
Benny Prijono10861432007-10-31 10:54:53 +0000552
Benny Prijono73bb7232009-10-20 13:56:26 +0000553 status = lock_buddy("pjsua_buddy_update_pres()", buddy_id, &lck, 0);
554 if (status != PJ_SUCCESS)
555 return status;
Benny Prijono10861432007-10-31 10:54:53 +0000556
Benny Prijono73bb7232009-10-20 13:56:26 +0000557 /* Is this an unsubscribe request? */
558 if (!lck.buddy->monitor) {
559 unsubscribe_buddy_presence(buddy_id);
560 unlock_buddy(&lck);
561 return PJ_SUCCESS;
Benny Prijono10861432007-10-31 10:54:53 +0000562 }
563
564 /* Ignore if presence is already active for the buddy */
Benny Prijono73bb7232009-10-20 13:56:26 +0000565 if (lck.buddy->sub) {
566 unlock_buddy(&lck);
Benny Prijono10861432007-10-31 10:54:53 +0000567 return PJ_SUCCESS;
568 }
569
570 /* Initiate presence subscription */
571 subscribe_buddy_presence(buddy_id);
572
Benny Prijono73bb7232009-10-20 13:56:26 +0000573 unlock_buddy(&lck);
Benny Prijono10861432007-10-31 10:54:53 +0000574
575 return PJ_SUCCESS;
576}
577
578
579/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000580 * Dump presence subscriptions to log file.
581 */
582PJ_DEF(void) pjsua_pres_dump(pj_bool_t verbose)
583{
584 unsigned acc_id;
585 unsigned i;
586
587
588 PJSUA_LOCK();
589
590 /*
591 * When no detail is required, just dump number of server and client
592 * subscriptions.
593 */
594 if (verbose == PJ_FALSE) {
595
596 int count = 0;
597
598 for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
599
600 if (!pjsua_var.acc[acc_id].valid)
601 continue;
602
603 if (!pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
604 struct pjsua_srv_pres *uapres;
605
606 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
607 while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
608 ++count;
609 uapres = uapres->next;
610 }
611 }
612 }
613
614 PJ_LOG(3,(THIS_FILE, "Number of server/UAS subscriptions: %d",
615 count));
616
617 count = 0;
618
619 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
620 if (pjsua_var.buddy[i].uri.slen == 0)
621 continue;
622 if (pjsua_var.buddy[i].sub) {
623 ++count;
624 }
625 }
626
627 PJ_LOG(3,(THIS_FILE, "Number of client/UAC subscriptions: %d",
628 count));
629 PJSUA_UNLOCK();
630 return;
631 }
632
633
634 /*
635 * Dumping all server (UAS) subscriptions
636 */
637 PJ_LOG(3,(THIS_FILE, "Dumping pjsua server subscriptions:"));
638
639 for (acc_id=0; acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
640
641 if (!pjsua_var.acc[acc_id].valid)
642 continue;
643
644 PJ_LOG(3,(THIS_FILE, " %.*s",
645 (int)pjsua_var.acc[acc_id].cfg.id.slen,
646 pjsua_var.acc[acc_id].cfg.id.ptr));
647
648 if (pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
649
650 PJ_LOG(3,(THIS_FILE, " - none - "));
651
652 } else {
653 struct pjsua_srv_pres *uapres;
654
655 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
656 while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
657
658 PJ_LOG(3,(THIS_FILE, " %10s %s",
659 pjsip_evsub_get_state_name(uapres->sub),
660 uapres->remote));
661
662 uapres = uapres->next;
663 }
664 }
665 }
666
667 /*
668 * Dumping all client (UAC) subscriptions
669 */
670 PJ_LOG(3,(THIS_FILE, "Dumping pjsua client subscriptions:"));
671
672 if (pjsua_var.buddy_cnt == 0) {
673
674 PJ_LOG(3,(THIS_FILE, " - no buddy list - "));
675
676 } else {
677 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
678
679 if (pjsua_var.buddy[i].uri.slen == 0)
680 continue;
681
682 if (pjsua_var.buddy[i].sub) {
683 PJ_LOG(3,(THIS_FILE, " %10s %.*s",
684 pjsip_evsub_get_state_name(pjsua_var.buddy[i].sub),
685 (int)pjsua_var.buddy[i].uri.slen,
686 pjsua_var.buddy[i].uri.ptr));
687 } else {
688 PJ_LOG(3,(THIS_FILE, " %10s %.*s",
689 "(null)",
690 (int)pjsua_var.buddy[i].uri.slen,
691 pjsua_var.buddy[i].uri.ptr));
692 }
693 }
694 }
695
696 PJSUA_UNLOCK();
697}
698
699
700/***************************************************************************
701 * Server subscription.
Benny Prijono834aee32006-02-19 01:38:06 +0000702 */
703
704/* Proto */
705static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata);
706
707/* The module instance. */
708static pjsip_module mod_pjsua_pres =
709{
710 NULL, NULL, /* prev, next. */
711 { "mod-pjsua-pres", 14 }, /* Name. */
712 -1, /* Id */
713 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
Benny Prijono834aee32006-02-19 01:38:06 +0000714 NULL, /* load() */
715 NULL, /* start() */
716 NULL, /* stop() */
717 NULL, /* unload() */
718 &pres_on_rx_request, /* on_rx_request() */
719 NULL, /* on_rx_response() */
720 NULL, /* on_tx_request. */
721 NULL, /* on_tx_response() */
722 NULL, /* on_tsx_state() */
723
724};
725
726
727/* Callback called when *server* subscription state has changed. */
728static void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event)
729{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000730 pjsua_srv_pres *uapres;
Benny Prijono834aee32006-02-19 01:38:06 +0000731
732 PJ_UNUSED_ARG(event);
733
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000734 PJSUA_LOCK();
735
Benny Prijonoa1e69682007-05-11 15:14:34 +0000736 uapres = (pjsua_srv_pres*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijono834aee32006-02-19 01:38:06 +0000737 if (uapres) {
Benny Prijono63fba012008-07-17 14:19:10 +0000738 pjsip_evsub_state state;
739
Benny Prijonoba736c42008-07-10 20:45:03 +0000740 PJ_LOG(4,(THIS_FILE, "Server subscription to %s is %s",
Benny Prijono834aee32006-02-19 01:38:06 +0000741 uapres->remote, pjsip_evsub_get_state_name(sub)));
742
Benny Prijono63fba012008-07-17 14:19:10 +0000743 state = pjsip_evsub_get_state(sub);
744
745 if (pjsua_var.ua_cfg.cb.on_srv_subscribe_state) {
746 pj_str_t from;
747
748 from = uapres->dlg->remote.info_str;
749 (*pjsua_var.ua_cfg.cb.on_srv_subscribe_state)(uapres->acc_id,
750 uapres, &from,
751 state, event);
752 }
753
754 if (state == PJSIP_EVSUB_STATE_TERMINATED) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000755 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +0000756 pj_list_erase(uapres);
757 }
758 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000759
760 PJSUA_UNLOCK();
Benny Prijono834aee32006-02-19 01:38:06 +0000761}
762
763/* This is called when request is received.
764 * We need to check for incoming SUBSCRIBE request.
765 */
766static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata)
767{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000768 int acc_id;
Benny Prijono6f979412006-06-15 12:25:46 +0000769 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000770 pj_str_t contact;
Benny Prijono834aee32006-02-19 01:38:06 +0000771 pjsip_method *req_method = &rdata->msg_info.msg->line.req.method;
772 pjsua_srv_pres *uapres;
773 pjsip_evsub *sub;
774 pjsip_evsub_user pres_cb;
Benny Prijono834aee32006-02-19 01:38:06 +0000775 pjsip_dialog *dlg;
Benny Prijono63fba012008-07-17 14:19:10 +0000776 pjsip_status_code st_code;
777 pj_str_t reason;
Benny Prijonoc61cc042007-06-27 13:01:59 +0000778 pjsip_expires_hdr *expires_hdr;
Benny Prijono63fba012008-07-17 14:19:10 +0000779 pjsua_msg_data msg_data;
Benny Prijono834aee32006-02-19 01:38:06 +0000780 pj_status_t status;
781
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000782 if (pjsip_method_cmp(req_method, pjsip_get_subscribe_method()) != 0)
Benny Prijono834aee32006-02-19 01:38:06 +0000783 return PJ_FALSE;
784
785 /* Incoming SUBSCRIBE: */
786
Benny Prijono384dab42009-10-14 01:58:04 +0000787 /* Don't want to accept the request if shutdown is in progress */
788 if (pjsua_var.thread_quit_flag) {
789 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
790 PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
791 NULL, NULL);
792 return PJ_TRUE;
793 }
794
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000795 PJSUA_LOCK();
796
Benny Prijonoa91a0032006-02-26 21:23:45 +0000797 /* Find which account for the incoming request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000798 acc_id = pjsua_acc_find_for_incoming(rdata);
Benny Prijono6f979412006-06-15 12:25:46 +0000799 acc = &pjsua_var.acc[acc_id];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000800
Benny Prijono6f979412006-06-15 12:25:46 +0000801 PJ_LOG(4,(THIS_FILE, "Creating server subscription, using account %d",
802 acc_id));
803
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000804 /* Create suitable Contact header */
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000805 if (acc->contact.slen) {
806 contact = acc->contact;
807 } else {
808 status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact,
809 acc_id, rdata);
810 if (status != PJ_SUCCESS) {
811 pjsua_perror(THIS_FILE, "Unable to generate Contact header",
812 status);
813 PJSUA_UNLOCK();
Benny Prijonoa330d452008-08-05 20:14:39 +0000814 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
815 NULL, NULL);
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000816 return PJ_TRUE;
817 }
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000818 }
819
Benny Prijono834aee32006-02-19 01:38:06 +0000820 /* Create UAS dialog: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000821 status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000822 &contact, &dlg);
Benny Prijono834aee32006-02-19 01:38:06 +0000823 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000824 pjsua_perror(THIS_FILE,
825 "Unable to create UAS dialog for subscription",
826 status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000827 PJSUA_UNLOCK();
Benny Prijonoa330d452008-08-05 20:14:39 +0000828 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
829 NULL, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000830 return PJ_TRUE;
Benny Prijono834aee32006-02-19 01:38:06 +0000831 }
832
Benny Prijono48ab2b72007-11-08 09:24:30 +0000833 /* Set credentials and preference. */
Benny Prijono6f979412006-06-15 12:25:46 +0000834 pjsip_auth_clt_set_credentials(&dlg->auth_sess, acc->cred_cnt, acc->cred);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000835 pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref);
Benny Prijono6f979412006-06-15 12:25:46 +0000836
Benny Prijono834aee32006-02-19 01:38:06 +0000837 /* Init callback: */
Benny Prijonoac623b32006-07-03 15:19:31 +0000838 pj_bzero(&pres_cb, sizeof(pres_cb));
Benny Prijono834aee32006-02-19 01:38:06 +0000839 pres_cb.on_evsub_state = &pres_evsub_on_srv_state;
840
841 /* Create server presence subscription: */
842 status = pjsip_pres_create_uas( dlg, &pres_cb, rdata, &sub);
843 if (status != PJ_SUCCESS) {
Benny Prijonodbd9d4b2008-09-11 10:25:51 +0000844 int code = PJSIP_ERRNO_TO_SIP_STATUS(status);
845 pjsip_tx_data *tdata;
846
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000847 pjsua_perror(THIS_FILE, "Unable to create server subscription",
848 status);
Benny Prijonodbd9d4b2008-09-11 10:25:51 +0000849
850 if (code==599 || code > 699 || code < 300) {
851 code = 400;
852 }
853
854 status = pjsip_dlg_create_response(dlg, rdata, code, NULL, &tdata);
855 if (status == PJ_SUCCESS) {
856 status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata),
857 tdata);
858 }
859
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000860 PJSUA_UNLOCK();
861 return PJ_TRUE;
Benny Prijono834aee32006-02-19 01:38:06 +0000862 }
863
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000864 /* If account is locked to specific transport, then lock dialog
865 * to this transport too.
866 */
867 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
868 pjsip_tpselector tp_sel;
869
870 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
871 pjsip_dlg_set_transport(dlg, &tp_sel);
872 }
873
Benny Prijono834aee32006-02-19 01:38:06 +0000874 /* Attach our data to the subscription: */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000875 uapres = PJ_POOL_ALLOC_T(dlg->pool, pjsua_srv_pres);
Benny Prijono834aee32006-02-19 01:38:06 +0000876 uapres->sub = sub;
Benny Prijonoa1e69682007-05-11 15:14:34 +0000877 uapres->remote = (char*) pj_pool_alloc(dlg->pool, PJSIP_MAX_URL_SIZE);
Benny Prijono63fba012008-07-17 14:19:10 +0000878 uapres->acc_id = acc_id;
879 uapres->dlg = dlg;
Benny Prijono834aee32006-02-19 01:38:06 +0000880 status = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, dlg->remote.info->uri,
881 uapres->remote, PJSIP_MAX_URL_SIZE);
882 if (status < 1)
883 pj_ansi_strcpy(uapres->remote, "<-- url is too long-->");
884 else
885 uapres->remote[status] = '\0';
886
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000887 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, uapres);
Benny Prijono834aee32006-02-19 01:38:06 +0000888
889 /* Add server subscription to the list: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000890 pj_list_push_back(&pjsua_var.acc[acc_id].pres_srv_list, uapres);
Benny Prijono834aee32006-02-19 01:38:06 +0000891
892
Benny Prijono63fba012008-07-17 14:19:10 +0000893 /* Capture the value of Expires header. */
894 expires_hdr = (pjsip_expires_hdr*)
895 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES,
896 NULL);
897 if (expires_hdr)
898 uapres->expires = expires_hdr->ivalue;
899 else
900 uapres->expires = -1;
901
Nanang Izzuddin82f7a412008-12-17 11:36:22 +0000902 st_code = (pjsip_status_code)200;
Benny Prijono63fba012008-07-17 14:19:10 +0000903 reason = pj_str("OK");
904 pjsua_msg_data_init(&msg_data);
905
906 /* Notify application callback, if any */
907 if (pjsua_var.ua_cfg.cb.on_incoming_subscribe) {
908 pjsua_buddy_id buddy_id;
909
Benny Prijono73bb7232009-10-20 13:56:26 +0000910 buddy_id = find_buddy(rdata->msg_info.from->uri);
Benny Prijono63fba012008-07-17 14:19:10 +0000911
912 (*pjsua_var.ua_cfg.cb.on_incoming_subscribe)(acc_id, uapres, buddy_id,
913 &dlg->remote.info_str,
914 rdata, &st_code, &reason,
915 &msg_data);
916 }
917
918 /* Handle rejection case */
919 if (st_code >= 300) {
920 pjsip_tx_data *tdata;
921
922 /* Create response */
923 status = pjsip_dlg_create_response(dlg, rdata, st_code,
924 &reason, &tdata);
925 if (status != PJ_SUCCESS) {
926 pjsua_perror(THIS_FILE, "Error creating response", status);
927 pj_list_erase(uapres);
928 pjsip_pres_terminate(sub, PJ_FALSE);
929 PJSUA_UNLOCK();
930 return PJ_FALSE;
931 }
932
933 /* Add header list, if any */
934 pjsua_process_msg_data(tdata, &msg_data);
935
936 /* Send the response */
937 status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata),
938 tdata);
939 if (status != PJ_SUCCESS) {
940 pjsua_perror(THIS_FILE, "Error sending response", status);
941 /* This is not fatal */
942 }
943
944 /* Terminate presence subscription */
945 pj_list_erase(uapres);
946 pjsip_pres_terminate(sub, PJ_FALSE);
947 PJSUA_UNLOCK();
948 return PJ_TRUE;
949 }
950
951 /* Create and send 2xx response to the SUBSCRIBE request: */
952 status = pjsip_pres_accept(sub, rdata, st_code, &msg_data.hdr_list);
Benny Prijono834aee32006-02-19 01:38:06 +0000953 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000954 pjsua_perror(THIS_FILE, "Unable to accept presence subscription",
955 status);
Benny Prijono834aee32006-02-19 01:38:06 +0000956 pj_list_erase(uapres);
Benny Prijono1c2bf462006-03-05 11:54:02 +0000957 pjsip_pres_terminate(sub, PJ_FALSE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000958 PJSUA_UNLOCK();
Benny Prijono834aee32006-02-19 01:38:06 +0000959 return PJ_FALSE;
960 }
961
Benny Prijono63fba012008-07-17 14:19:10 +0000962 /* If code is 200, send NOTIFY now */
963 if (st_code == 200) {
964 pjsua_pres_notify(acc_id, uapres, PJSIP_EVSUB_STATE_ACTIVE,
965 NULL, NULL, PJ_TRUE, &msg_data);
966 }
967
968 /* Done: */
969
970 PJSUA_UNLOCK();
971
972 return PJ_TRUE;
973}
974
975
976/*
977 * Send NOTIFY.
978 */
979PJ_DEF(pj_status_t) pjsua_pres_notify( pjsua_acc_id acc_id,
980 pjsua_srv_pres *srv_pres,
981 pjsip_evsub_state ev_state,
982 const pj_str_t *state_str,
983 const pj_str_t *reason,
984 pj_bool_t with_body,
985 const pjsua_msg_data *msg_data)
986{
987 pjsua_acc *acc;
988 pjsip_pres_status pres_status;
989 pjsua_buddy_id buddy_id;
990 pjsip_tx_data *tdata;
991 pj_status_t status;
992
993 /* Check parameters */
994 PJ_ASSERT_RETURN(acc_id!=-1 && srv_pres, PJ_EINVAL);
995
996 /* Check that account ID is valid */
997 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
998 PJ_EINVAL);
999 /* Check that account is valid */
1000 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1001
1002 PJSUA_LOCK();
1003
1004 acc = &pjsua_var.acc[acc_id];
1005
1006 /* Check that the server presence subscription is still valid */
1007 if (pj_list_find_node(&acc->pres_srv_list, srv_pres) == NULL) {
1008 /* Subscription has been terminated */
1009 PJSUA_UNLOCK();
1010 return PJ_EINVALIDOP;
1011 }
Benny Prijono834aee32006-02-19 01:38:06 +00001012
1013 /* Set our online status: */
Benny Prijonoac623b32006-07-03 15:19:31 +00001014 pj_bzero(&pres_status, sizeof(pres_status));
Benny Prijono834aee32006-02-19 01:38:06 +00001015 pres_status.info_cnt = 1;
Benny Prijono63fba012008-07-17 14:19:10 +00001016 pres_status.info[0].basic_open = acc->online_status;
1017 pres_status.info[0].id = acc->cfg.pidf_tuple_id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001018 //Both pjsua_var.local_uri and pjsua_var.contact_uri are enclosed in "<" and ">"
Benny Prijono834aee32006-02-19 01:38:06 +00001019 //causing XML parsing to fail.
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001020 //pres_status.info[0].contact = pjsua_var.local_uri;
Benny Prijono7f6ee022008-07-31 08:32:46 +00001021 /* add RPID information */
1022 pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
1023 sizeof(pjrpid_element));
Benny Prijono834aee32006-02-19 01:38:06 +00001024
Benny Prijono63fba012008-07-17 14:19:10 +00001025 pjsip_pres_set_status(srv_pres->sub, &pres_status);
Benny Prijono834aee32006-02-19 01:38:06 +00001026
Benny Prijonoc61cc042007-06-27 13:01:59 +00001027 /* Check expires value. If it's zero, send our presense state but
1028 * set subscription state to TERMINATED.
1029 */
Benny Prijono63fba012008-07-17 14:19:10 +00001030 if (srv_pres->expires == 0)
Benny Prijonoc61cc042007-06-27 13:01:59 +00001031 ev_state = PJSIP_EVSUB_STATE_TERMINATED;
Benny Prijonoc61cc042007-06-27 13:01:59 +00001032
Benny Prijono63fba012008-07-17 14:19:10 +00001033 /* Create and send the NOTIFY to active subscription: */
1034 status = pjsip_pres_notify(srv_pres->sub, ev_state, state_str,
1035 reason, &tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +00001036 if (status == PJ_SUCCESS) {
Benny Prijono63fba012008-07-17 14:19:10 +00001037 /* Force removal of message body if msg_body==FALSE */
1038 if (!with_body) {
1039 tdata->msg->body = NULL;
1040 }
1041 pjsua_process_msg_data(tdata, msg_data);
1042 status = pjsip_pres_send_request( srv_pres->sub, tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +00001043 }
Benny Prijono834aee32006-02-19 01:38:06 +00001044
1045 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001046 pjsua_perror(THIS_FILE, "Unable to create/send NOTIFY",
1047 status);
Benny Prijono63fba012008-07-17 14:19:10 +00001048 pj_list_erase(srv_pres);
1049 pjsip_pres_terminate(srv_pres->sub, PJ_FALSE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001050 PJSUA_UNLOCK();
Benny Prijono63fba012008-07-17 14:19:10 +00001051 return status;
Benny Prijono834aee32006-02-19 01:38:06 +00001052 }
1053
1054
Benny Prijonoa17496a2007-10-31 10:20:31 +00001055 /* Subscribe to buddy's presence if we're not subscribed */
Benny Prijono73bb7232009-10-20 13:56:26 +00001056 buddy_id = find_buddy(srv_pres->dlg->remote.info->uri);
Benny Prijonoa17496a2007-10-31 10:20:31 +00001057 if (buddy_id != PJSUA_INVALID_ID) {
1058 pjsua_buddy *b = &pjsua_var.buddy[buddy_id];
1059 if (b->monitor && b->sub == NULL) {
1060 PJ_LOG(4,(THIS_FILE, "Received SUBSCRIBE from buddy %d, "
1061 "activating outgoing subscription", buddy_id));
1062 subscribe_buddy_presence(buddy_id);
1063 }
1064 }
1065
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001066 PJSUA_UNLOCK();
1067
Benny Prijono63fba012008-07-17 14:19:10 +00001068 return PJ_SUCCESS;
Benny Prijono834aee32006-02-19 01:38:06 +00001069}
1070
1071
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001072/*
1073 * Client presence publication callback.
1074 */
1075static void publish_cb(struct pjsip_publishc_cbparam *param)
1076{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001077 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001078
1079 if (param->code/100 != 2 || param->status != PJ_SUCCESS) {
Benny Prijono53984d12009-04-28 22:19:49 +00001080
1081 pjsip_publishc_destroy(param->pubc);
1082 acc->publish_sess = NULL;
1083
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001084 if (param->status != PJ_SUCCESS) {
1085 char errmsg[PJ_ERR_MSG_SIZE];
1086
1087 pj_strerror(param->status, errmsg, sizeof(errmsg));
1088 PJ_LOG(1,(THIS_FILE,
1089 "Client publication (PUBLISH) failed, status=%d, msg=%s",
1090 param->status, errmsg));
Benny Prijono53984d12009-04-28 22:19:49 +00001091 } else if (param->code == 412) {
1092 /* 412 (Conditional Request Failed)
1093 * The PUBLISH refresh has failed, retry with new one.
1094 */
1095 pjsua_pres_init_publish_acc(acc->index);
1096
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001097 } else {
1098 PJ_LOG(1,(THIS_FILE,
1099 "Client publication (PUBLISH) failed (%d/%.*s)",
1100 param->code, (int)param->reason.slen,
1101 param->reason.ptr));
1102 }
1103
Benny Prijono53984d12009-04-28 22:19:49 +00001104 } else {
Benny Prijono534a9ba2009-10-13 14:01:59 +00001105 if (param->expiration < 1) {
Benny Prijono53984d12009-04-28 22:19:49 +00001106 /* Could happen if server "forgot" to include Expires header
1107 * in the response. We will not renew, so destroy the pubc.
1108 */
1109 pjsip_publishc_destroy(param->pubc);
1110 acc->publish_sess = NULL;
1111 }
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001112 }
1113}
1114
1115
1116/*
1117 * Send PUBLISH request.
1118 */
1119static pj_status_t send_publish(int acc_id, pj_bool_t active)
1120{
1121 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1122 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1123 pjsip_pres_status pres_status;
1124 pjsip_tx_data *tdata;
1125 pj_status_t status;
1126
1127
1128 /* Create PUBLISH request */
1129 if (active) {
Benny Prijono8c6e8842007-02-24 15:33:54 +00001130 char *bpos;
1131 pj_str_t entity;
1132
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001133 status = pjsip_publishc_publish(acc->publish_sess, PJ_TRUE, &tdata);
1134 if (status != PJ_SUCCESS) {
1135 pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
1136 goto on_error;
1137 }
1138
1139 /* Set our online status: */
1140 pj_bzero(&pres_status, sizeof(pres_status));
1141 pres_status.info_cnt = 1;
1142 pres_status.info[0].basic_open = acc->online_status;
Benny Prijonofe04fb52007-08-24 08:28:52 +00001143 pres_status.info[0].id = acc->cfg.pidf_tuple_id;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001144 /* .. including RPID information */
1145 pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
1146 sizeof(pjrpid_element));
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001147
Benny Prijono8c6e8842007-02-24 15:33:54 +00001148 /* Be careful not to send PIDF with presence entity ID containing
1149 * "<" character.
1150 */
1151 if ((bpos=pj_strchr(&acc_cfg->id, '<')) != NULL) {
1152 char *epos = pj_strchr(&acc_cfg->id, '>');
1153 if (epos - bpos < 2) {
1154 pj_assert(!"Unexpected invalid URI");
1155 status = PJSIP_EINVALIDURI;
1156 goto on_error;
1157 }
1158 entity.ptr = bpos+1;
1159 entity.slen = epos - bpos - 1;
1160 } else {
1161 entity = acc_cfg->id;
1162 }
1163
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001164 /* Create and add PIDF message body */
1165 status = pjsip_pres_create_pidf(tdata->pool, &pres_status,
Benny Prijono8c6e8842007-02-24 15:33:54 +00001166 &entity, &tdata->msg->body);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001167 if (status != PJ_SUCCESS) {
1168 pjsua_perror(THIS_FILE, "Error creating PIDF for PUBLISH request",
1169 status);
1170 pjsip_tx_data_dec_ref(tdata);
1171 goto on_error;
1172 }
1173 } else {
1174 status = pjsip_publishc_unpublish(acc->publish_sess, &tdata);
1175 if (status != PJ_SUCCESS) {
1176 pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
1177 goto on_error;
1178 }
1179 }
1180
1181 /* Add headers etc */
1182 pjsua_process_msg_data(tdata, NULL);
1183
1184 /* Send the PUBLISH request */
1185 status = pjsip_publishc_send(acc->publish_sess, tdata);
Benny Prijonofe50c9e2009-10-12 07:44:14 +00001186 if (status == PJ_EPENDING) {
1187 PJ_LOG(3,(THIS_FILE, "Previous request is in progress, "
1188 "PUBLISH request is queued"));
1189 } else if (status != PJ_SUCCESS) {
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001190 pjsua_perror(THIS_FILE, "Error sending PUBLISH request", status);
1191 goto on_error;
1192 }
1193
1194 acc->publish_state = acc->online_status;
1195 return PJ_SUCCESS;
1196
1197on_error:
Benny Prijono29438152007-06-28 02:47:32 +00001198 if (acc->publish_sess) {
1199 pjsip_publishc_destroy(acc->publish_sess);
1200 acc->publish_sess = NULL;
1201 }
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001202 return status;
1203}
1204
1205
1206/* Create client publish session */
Benny Prijono8b6834f2007-02-24 13:29:22 +00001207pj_status_t pjsua_pres_init_publish_acc(int acc_id)
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001208{
1209 const pj_str_t STR_PRESENCE = { "presence", 8 };
1210 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1211 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1212 pj_status_t status;
1213
1214 /* Create and init client publication session */
1215 if (acc_cfg->publish_enabled) {
1216
1217 /* Create client publication */
Benny Prijonofe50c9e2009-10-12 07:44:14 +00001218 status = pjsip_publishc_create(pjsua_var.endpt, &acc_cfg->publish_opt,
1219 acc, &publish_cb,
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001220 &acc->publish_sess);
1221 if (status != PJ_SUCCESS) {
1222 acc->publish_sess = NULL;
1223 return status;
1224 }
1225
1226 /* Initialize client publication */
1227 status = pjsip_publishc_init(acc->publish_sess, &STR_PRESENCE,
1228 &acc_cfg->id, &acc_cfg->id,
1229 &acc_cfg->id,
Benny Prijono53984d12009-04-28 22:19:49 +00001230 PJSUA_PUBLISH_EXPIRATION);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001231 if (status != PJ_SUCCESS) {
1232 acc->publish_sess = NULL;
1233 return status;
1234 }
1235
Benny Prijono703b7d72007-03-20 09:13:24 +00001236 /* Add credential for authentication */
Benny Prijono29438152007-06-28 02:47:32 +00001237 if (acc->cred_cnt) {
1238 pjsip_publishc_set_credentials(acc->publish_sess, acc->cred_cnt,
1239 acc->cred);
1240 }
Benny Prijono703b7d72007-03-20 09:13:24 +00001241
1242 /* Set route-set */
1243 pjsip_publishc_set_route_set(acc->publish_sess, &acc->route_set);
1244
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001245 /* Send initial PUBLISH request */
1246 if (acc->online_status != 0) {
1247 status = send_publish(acc_id, PJ_TRUE);
1248 if (status != PJ_SUCCESS)
1249 return status;
1250 }
1251
1252 } else {
1253 acc->publish_sess = NULL;
1254 }
1255
1256 return PJ_SUCCESS;
1257}
1258
1259
1260/* Init presence for account */
1261pj_status_t pjsua_pres_init_acc(int acc_id)
1262{
1263 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1264
1265 /* Init presence subscription */
1266 pj_list_init(&acc->pres_srv_list);
1267
Benny Prijono8b6834f2007-02-24 13:29:22 +00001268 return PJ_SUCCESS;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001269}
1270
1271
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001272/* Terminate server subscription for the account */
1273void pjsua_pres_delete_acc(int acc_id)
Benny Prijono834aee32006-02-19 01:38:06 +00001274{
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001275 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1276 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
Benny Prijono834aee32006-02-19 01:38:06 +00001277 pjsua_srv_pres *uapres;
1278
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001279 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
Benny Prijono834aee32006-02-19 01:38:06 +00001280
Benny Prijono922933b2007-01-21 16:23:56 +00001281 /* Notify all subscribers that we're no longer available */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001282 while (uapres != &acc->pres_srv_list) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001283
1284 pjsip_pres_status pres_status;
1285 pj_str_t reason = { "noresource", 10 };
Benny Prijono5516f912008-05-05 12:06:08 +00001286 pjsua_srv_pres *next;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001287 pjsip_tx_data *tdata;
1288
Benny Prijono5516f912008-05-05 12:06:08 +00001289 next = uapres->next;
1290
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001291 pjsip_pres_get_status(uapres->sub, &pres_status);
1292
1293 pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status;
1294 pjsip_pres_set_status(uapres->sub, &pres_status);
1295
1296 if (pjsip_pres_notify(uapres->sub,
1297 PJSIP_EVSUB_STATE_TERMINATED, NULL,
1298 &reason, &tdata)==PJ_SUCCESS)
1299 {
1300 pjsip_pres_send_request(uapres->sub, tdata);
1301 }
1302
Benny Prijono5516f912008-05-05 12:06:08 +00001303 uapres = next;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001304 }
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001305
Benny Prijono922933b2007-01-21 16:23:56 +00001306 /* Clear server presence subscription list because account might be reused
1307 * later. */
1308 pj_list_init(&acc->pres_srv_list);
1309
1310 /* Terminate presence publication, if any */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001311 if (acc->publish_sess) {
1312 acc->online_status = PJ_FALSE;
1313 send_publish(acc_id, PJ_FALSE);
Benny Prijono534a9ba2009-10-13 14:01:59 +00001314 /* By ticket #364, don't destroy the session yet (let the callback
1315 destroy it)
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001316 if (acc->publish_sess) {
1317 pjsip_publishc_destroy(acc->publish_sess);
1318 acc->publish_sess = NULL;
1319 }
Benny Prijono534a9ba2009-10-13 14:01:59 +00001320 */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001321 acc_cfg->publish_enabled = PJ_FALSE;
1322 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001323}
1324
1325
Benny Prijono4461c7d2007-08-25 13:36:15 +00001326/* Update server subscription (e.g. when our online status has changed) */
1327void pjsua_pres_update_acc(int acc_id, pj_bool_t force)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001328{
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001329 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1330 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001331 pjsua_srv_pres *uapres;
1332
1333 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
1334
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001335 while (uapres != &acc->pres_srv_list) {
Benny Prijono834aee32006-02-19 01:38:06 +00001336
1337 pjsip_pres_status pres_status;
1338 pjsip_tx_data *tdata;
1339
1340 pjsip_pres_get_status(uapres->sub, &pres_status);
Benny Prijono232759b2008-09-08 12:46:29 +00001341
1342 /* Only send NOTIFY once subscription is active. Some subscriptions
1343 * may still be in NULL (when app is adding a new buddy while in the
1344 * on_incoming_subscribe() callback) or PENDING (when user approval is
1345 * being requested) state and we don't send NOTIFY to these subs until
1346 * the user accepted the request.
1347 */
1348 if (pjsip_evsub_get_state(uapres->sub)==PJSIP_EVSUB_STATE_ACTIVE &&
1349 (force || pres_status.info[0].basic_open != acc->online_status))
1350 {
Benny Prijono4461c7d2007-08-25 13:36:15 +00001351
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001352 pres_status.info[0].basic_open = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001353 pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
1354 sizeof(pjrpid_element));
1355
Benny Prijono834aee32006-02-19 01:38:06 +00001356 pjsip_pres_set_status(uapres->sub, &pres_status);
1357
Benny Prijono21b9ad92006-08-15 13:11:22 +00001358 if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) {
1359 pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001360 pjsip_pres_send_request(uapres->sub, tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +00001361 }
Benny Prijono834aee32006-02-19 01:38:06 +00001362 }
1363
1364 uapres = uapres->next;
1365 }
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001366
Benny Prijono8b6834f2007-02-24 13:29:22 +00001367 /* Send PUBLISH if required. We only do this when we have a PUBLISH
1368 * session. If we don't have a PUBLISH session, then it could be
1369 * that we're waiting until registration has completed before we
1370 * send the first PUBLISH.
1371 */
1372 if (acc_cfg->publish_enabled && acc->publish_sess) {
Benny Prijono4461c7d2007-08-25 13:36:15 +00001373 if (force || acc->publish_state != acc->online_status) {
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001374 send_publish(acc_id, PJ_TRUE);
1375 }
1376 }
Benny Prijono834aee32006-02-19 01:38:06 +00001377}
1378
1379
1380
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001381/***************************************************************************
1382 * Client subscription.
Benny Prijono834aee32006-02-19 01:38:06 +00001383 */
1384
Benny Prijono73bb7232009-10-20 13:56:26 +00001385static void buddy_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry)
1386{
1387 pjsua_buddy *buddy = (pjsua_buddy*)entry->user_data;
1388
1389 PJ_UNUSED_ARG(th);
1390
1391 entry->id = PJ_FALSE;
1392 pjsua_buddy_update_pres(buddy->index);
1393}
1394
1395/* Reschedule subscription refresh timer or terminate the subscription
1396 * refresh timer for the specified buddy.
1397 */
1398static void buddy_resubscribe(pjsua_buddy *buddy, pj_bool_t resched,
1399 unsigned msec_interval)
1400{
1401 if (buddy->timer.id) {
1402 pjsua_cancel_timer(&buddy->timer);
1403 buddy->timer.id = PJ_FALSE;
1404 }
1405
1406 if (resched) {
1407 pj_time_val delay;
1408
Benny Prijono12c01a92009-10-21 02:37:52 +00001409 PJ_LOG(4,(THIS_FILE,
1410 "Resubscribing buddy id %u in %u ms (reason: %.*s)",
1411 buddy->index, msec_interval,
1412 (int)buddy->term_reason.slen,
1413 buddy->term_reason.ptr));
1414
Benny Prijono73bb7232009-10-20 13:56:26 +00001415 pj_timer_entry_init(&buddy->timer, 0, buddy, &buddy_timer_cb);
1416 delay.sec = 0;
1417 delay.msec = msec_interval;
1418 pj_time_val_normalize(&delay);
1419
1420 if (pjsua_schedule_timer(&buddy->timer, &delay)==PJ_SUCCESS)
1421 buddy->timer.id = PJ_TRUE;
1422 }
1423}
1424
Benny Prijono834aee32006-02-19 01:38:06 +00001425/* Callback called when *client* subscription state has changed. */
1426static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
1427{
1428 pjsua_buddy *buddy;
1429
1430 PJ_UNUSED_ARG(event);
1431
Benny Prijono73bb7232009-10-20 13:56:26 +00001432 /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1433 * a dialog attached to it, lock_buddy() will use the dialog
1434 * lock, which we are currently holding!
1435 */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001436 buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijono834aee32006-02-19 01:38:06 +00001437 if (buddy) {
Benny Prijonoba736c42008-07-10 20:45:03 +00001438 PJ_LOG(4,(THIS_FILE,
Benny Prijono9fc735d2006-05-28 14:58:12 +00001439 "Presence subscription to %.*s is %s",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001440 (int)pjsua_var.buddy[buddy->index].uri.slen,
1441 pjsua_var.buddy[buddy->index].uri.ptr,
Benny Prijono834aee32006-02-19 01:38:06 +00001442 pjsip_evsub_get_state_name(sub)));
1443
1444 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
Benny Prijono73bb7232009-10-20 13:56:26 +00001445 int resub_delay = -1;
1446
Benny Prijono63fba012008-07-17 14:19:10 +00001447 if (buddy->term_reason.ptr == NULL) {
1448 buddy->term_reason.ptr = (char*)
1449 pj_pool_alloc(buddy->pool,
1450 PJSUA_BUDDY_SUB_TERM_REASON_LEN);
1451 }
1452 pj_strncpy(&buddy->term_reason,
1453 pjsip_evsub_get_termination_reason(sub),
1454 PJSUA_BUDDY_SUB_TERM_REASON_LEN);
Benny Prijono73bb7232009-10-20 13:56:26 +00001455
1456 buddy->term_code = 200;
1457
1458 /* Determine whether to resubscribe automatically */
Benny Prijonod3d18c32009-11-10 10:54:11 +00001459 if (event && event->type==PJSIP_EVENT_TSX_STATE) {
Benny Prijono73bb7232009-10-20 13:56:26 +00001460 const pjsip_transaction *tsx = event->body.tsx_state.tsx;
1461 if (pjsip_method_cmp(&tsx->method,
1462 &pjsip_subscribe_method)==0)
1463 {
1464 buddy->term_code = tsx->status_code;
1465 switch (tsx->status_code) {
1466 case PJSIP_SC_CALL_TSX_DOES_NOT_EXIST:
1467 /* 481: we refreshed too late? resubscribe
1468 * immediately.
1469 */
Benny Prijono6ab05322009-10-21 03:03:06 +00001470 /* But this must only happen when the 481 is received
1471 * on subscription refresh request. We MUST NOT try to
1472 * resubscribe automatically if the 481 is received
1473 * on the initial SUBSCRIBE (if server returns this
1474 * response for some reason).
1475 */
1476 if (buddy->dlg->remote.contact)
1477 resub_delay = 500;
Benny Prijono73bb7232009-10-20 13:56:26 +00001478 break;
1479 }
1480 } else if (pjsip_method_cmp(&tsx->method,
1481 &pjsip_notify_method)==0)
1482 {
1483 if (pj_stricmp2(&buddy->term_reason, "deactivated")==0 ||
1484 pj_stricmp2(&buddy->term_reason, "timeout")==0) {
1485 /* deactivated: The subscription has been terminated,
1486 * but the subscriber SHOULD retry immediately with
1487 * a new subscription.
1488 */
1489 /* timeout: The subscription has been terminated
1490 * because it was not refreshed before it expired.
1491 * Clients MAY re-subscribe immediately. The
1492 * "retry-after" parameter has no semantics for
1493 * "timeout".
1494 */
1495 resub_delay = 500;
1496 }
1497 else if (pj_stricmp2(&buddy->term_reason, "probation")==0||
1498 pj_stricmp2(&buddy->term_reason, "giveup")==0) {
1499 /* probation: The subscription has been terminated,
1500 * but the client SHOULD retry at some later time.
1501 * If a "retry-after" parameter is also present, the
1502 * client SHOULD wait at least the number of seconds
1503 * specified by that parameter before attempting to re-
1504 * subscribe.
1505 */
1506 /* giveup: The subscription has been terminated because
1507 * the notifier could not obtain authorization in a
1508 * timely fashion. If a "retry-after" parameter is
1509 * also present, the client SHOULD wait at least the
1510 * number of seconds specified by that parameter before
1511 * attempting to re-subscribe; otherwise, the client
1512 * MAY retry immediately, but will likely get put back
1513 * into pending state.
1514 */
1515 const pjsip_sub_state_hdr *sub_hdr;
1516 pj_str_t sub_state = { "Subscription-State", 18 };
1517 const pjsip_msg *msg;
1518
1519 msg = event->body.tsx_state.src.rdata->msg_info.msg;
1520 sub_hdr = (const pjsip_sub_state_hdr*)
1521 pjsip_msg_find_hdr_by_name(msg, &sub_state,
1522 NULL);
1523 if (sub_hdr && sub_hdr->retry_after > 0)
1524 resub_delay = sub_hdr->retry_after * 1000;
1525 }
1526
1527 }
1528 }
1529
1530 /* For other cases of subscription termination, if resubscribe
1531 * timer is not set, schedule with default expiration (plus minus
1532 * some random value, to avoid sending SUBSCRIBEs all at once)
1533 */
1534 if (resub_delay == -1) {
1535 pj_assert(PJSUA_PRES_TIMER >= 3);
1536 resub_delay = PJSUA_PRES_TIMER*1000 - 2500 + (pj_rand()%5000);
1537 }
1538
1539 buddy_resubscribe(buddy, PJ_TRUE, resub_delay);
Benny Prijono6ab05322009-10-21 03:03:06 +00001540
Benny Prijono63fba012008-07-17 14:19:10 +00001541 } else {
Benny Prijono6ab05322009-10-21 03:03:06 +00001542 /* This will clear the last termination code/reason */
Benny Prijono73bb7232009-10-20 13:56:26 +00001543 buddy->term_code = 0;
Benny Prijono63fba012008-07-17 14:19:10 +00001544 buddy->term_reason.slen = 0;
Benny Prijono834aee32006-02-19 01:38:06 +00001545 }
Benny Prijono9fc735d2006-05-28 14:58:12 +00001546
1547 /* Call callback */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001548 if (pjsua_var.ua_cfg.cb.on_buddy_state)
1549 (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index);
Benny Prijono63fba012008-07-17 14:19:10 +00001550
1551 /* Clear subscription */
1552 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1553 buddy->sub = NULL;
1554 buddy->status.info_cnt = 0;
Benny Prijono73bb7232009-10-20 13:56:26 +00001555 buddy->dlg = NULL;
Benny Prijono63fba012008-07-17 14:19:10 +00001556 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
1557 }
Benny Prijono834aee32006-02-19 01:38:06 +00001558 }
1559}
1560
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001561
1562/* Callback when transaction state has changed. */
1563static void pjsua_evsub_on_tsx_state(pjsip_evsub *sub,
1564 pjsip_transaction *tsx,
1565 pjsip_event *event)
1566{
1567 pjsua_buddy *buddy;
1568 pjsip_contact_hdr *contact_hdr;
1569
Benny Prijono73bb7232009-10-20 13:56:26 +00001570 /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1571 * a dialog attached to it, lock_buddy() will use the dialog
1572 * lock, which we are currently holding!
1573 */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001574 buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001575 if (!buddy) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001576 return;
1577 }
1578
1579 /* We only use this to update buddy's Contact, when it's not
1580 * set.
1581 */
1582 if (buddy->contact.slen != 0) {
1583 /* Contact already set */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001584 return;
1585 }
1586
1587 /* Only care about 2xx response to outgoing SUBSCRIBE */
1588 if (tsx->status_code/100 != 2 ||
1589 tsx->role != PJSIP_UAC_ROLE ||
1590 event->type != PJSIP_EVENT_RX_MSG ||
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001591 pjsip_method_cmp(&tsx->method, pjsip_get_subscribe_method())!=0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001592 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001593 return;
1594 }
1595
1596 /* Find contact header. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001597 contact_hdr = (pjsip_contact_hdr*)
1598 pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001599 PJSIP_H_CONTACT, NULL);
1600 if (!contact_hdr) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001601 return;
1602 }
1603
Benny Prijonoa1e69682007-05-11 15:14:34 +00001604 buddy->contact.ptr = (char*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001605 pj_pool_alloc(buddy->pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001606 buddy->contact.slen = pjsip_uri_print( PJSIP_URI_IN_CONTACT_HDR,
1607 contact_hdr->uri,
1608 buddy->contact.ptr,
1609 PJSIP_MAX_URL_SIZE);
1610 if (buddy->contact.slen < 0)
1611 buddy->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001612}
1613
1614
Benny Prijono834aee32006-02-19 01:38:06 +00001615/* Callback called when we receive NOTIFY */
1616static void pjsua_evsub_on_rx_notify(pjsip_evsub *sub,
1617 pjsip_rx_data *rdata,
1618 int *p_st_code,
1619 pj_str_t **p_st_text,
1620 pjsip_hdr *res_hdr,
1621 pjsip_msg_body **p_body)
1622{
1623 pjsua_buddy *buddy;
1624
Benny Prijono73bb7232009-10-20 13:56:26 +00001625 /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1626 * a dialog attached to it, lock_buddy() will use the dialog
1627 * lock, which we are currently holding!
1628 */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001629 buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijono834aee32006-02-19 01:38:06 +00001630 if (buddy) {
1631 /* Update our info. */
1632 pjsip_pres_get_status(sub, &buddy->status);
Benny Prijono834aee32006-02-19 01:38:06 +00001633 }
1634
1635 /* The default is to send 200 response to NOTIFY.
1636 * Just leave it there..
1637 */
1638 PJ_UNUSED_ARG(rdata);
1639 PJ_UNUSED_ARG(p_st_code);
1640 PJ_UNUSED_ARG(p_st_text);
1641 PJ_UNUSED_ARG(res_hdr);
1642 PJ_UNUSED_ARG(p_body);
1643}
1644
1645
1646/* Event subscription callback. */
1647static pjsip_evsub_user pres_callback =
1648{
1649 &pjsua_evsub_on_state,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001650 &pjsua_evsub_on_tsx_state,
Benny Prijono834aee32006-02-19 01:38:06 +00001651
1652 NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless
1653 * we want to authenticate
1654 */
1655
1656 &pjsua_evsub_on_rx_notify,
1657
1658 NULL, /* on_client_refresh: Use default behaviour, which is to
1659 * refresh client subscription. */
1660
1661 NULL, /* on_server_timeout: Use default behaviour, which is to send
1662 * NOTIFY to terminate.
1663 */
1664};
1665
1666
1667/* It does what it says.. */
Benny Prijono73bb7232009-10-20 13:56:26 +00001668static void subscribe_buddy_presence(pjsua_buddy_id buddy_id)
Benny Prijono834aee32006-02-19 01:38:06 +00001669{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001670 pj_pool_t *tmp_pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001671 pjsua_buddy *buddy;
1672 int acc_id;
1673 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001674 pj_str_t contact;
Benny Prijono834aee32006-02-19 01:38:06 +00001675 pjsip_tx_data *tdata;
1676 pj_status_t status;
1677
Benny Prijono73bb7232009-10-20 13:56:26 +00001678 buddy = &pjsua_var.buddy[buddy_id];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001679 acc_id = pjsua_acc_find_for_outgoing(&buddy->uri);
Benny Prijono8b1889b2006-06-06 18:40:40 +00001680
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001681 acc = &pjsua_var.acc[acc_id];
Benny Prijonoa91a0032006-02-26 21:23:45 +00001682
Benny Prijonob4a17c92006-07-10 14:40:21 +00001683 PJ_LOG(4,(THIS_FILE, "Using account %d for buddy %d subscription",
Benny Prijono73bb7232009-10-20 13:56:26 +00001684 acc_id, buddy_id));
Benny Prijonob4a17c92006-07-10 14:40:21 +00001685
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001686 /* Generate suitable Contact header unless one is already set in
1687 * the account
1688 */
1689 if (acc->contact.slen) {
1690 contact = acc->contact;
1691 } else {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001692 tmp_pool = pjsua_pool_create("tmpbuddy", 512, 256);
1693
1694 status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001695 acc_id, &buddy->uri);
1696 if (status != PJ_SUCCESS) {
1697 pjsua_perror(THIS_FILE, "Unable to generate Contact header",
1698 status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001699 pj_pool_release(tmp_pool);
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001700 return;
1701 }
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001702 }
1703
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001704 /* Create UAC dialog */
Benny Prijono834aee32006-02-19 01:38:06 +00001705 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001706 &acc->cfg.id,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001707 &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001708 &buddy->uri,
Benny Prijonof9c40c32007-06-28 07:20:26 +00001709 NULL, &buddy->dlg);
Benny Prijono834aee32006-02-19 01:38:06 +00001710 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001711 pjsua_perror(THIS_FILE, "Unable to create dialog",
1712 status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001713 if (tmp_pool) pj_pool_release(tmp_pool);
Benny Prijono834aee32006-02-19 01:38:06 +00001714 return;
1715 }
1716
Benny Prijono0bd5eb92009-04-14 11:10:31 +00001717 /* Increment the dialog's lock otherwise when presence session creation
1718 * fails the dialog will be destroyed prematurely.
1719 */
1720 pjsip_dlg_inc_lock(buddy->dlg);
1721
Benny Prijonof9c40c32007-06-28 07:20:26 +00001722 status = pjsip_pres_create_uac( buddy->dlg, &pres_callback,
Benny Prijonodc752ca2006-09-22 16:55:42 +00001723 PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub);
1724 if (status != PJ_SUCCESS) {
Benny Prijono73bb7232009-10-20 13:56:26 +00001725 buddy->sub = NULL;
Benny Prijonodc752ca2006-09-22 16:55:42 +00001726 pjsua_perror(THIS_FILE, "Unable to create presence client",
1727 status);
Benny Prijono0bd5eb92009-04-14 11:10:31 +00001728 /* This should destroy the dialog since there's no session
1729 * referencing it
1730 */
1731 pjsip_dlg_dec_lock(buddy->dlg);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001732 if (tmp_pool) pj_pool_release(tmp_pool);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001733 return;
1734 }
1735
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001736 /* If account is locked to specific transport, then lock dialog
1737 * to this transport too.
1738 */
1739 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1740 pjsip_tpselector tp_sel;
1741
1742 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
Benny Prijonof9c40c32007-06-28 07:20:26 +00001743 pjsip_dlg_set_transport(buddy->dlg, &tp_sel);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001744 }
1745
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001746 /* Set route-set */
1747 if (!pj_list_empty(&acc->route_set)) {
Benny Prijonof9c40c32007-06-28 07:20:26 +00001748 pjsip_dlg_set_route_set(buddy->dlg, &acc->route_set);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001749 }
1750
1751 /* Set credentials */
1752 if (acc->cred_cnt) {
Benny Prijonof9c40c32007-06-28 07:20:26 +00001753 pjsip_auth_clt_set_credentials( &buddy->dlg->auth_sess,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001754 acc->cred_cnt, acc->cred);
Benny Prijonodc39fe82006-05-26 12:17:46 +00001755 }
Benny Prijono1d8d6082006-04-29 12:38:25 +00001756
Benny Prijono48ab2b72007-11-08 09:24:30 +00001757 /* Set authentication preference */
1758 pjsip_auth_clt_set_prefs(&buddy->dlg->auth_sess, &acc->cfg.auth_pref);
1759
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001760 pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy);
Benny Prijono834aee32006-02-19 01:38:06 +00001761
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001762 status = pjsip_pres_initiate(buddy->sub, -1, &tdata);
Benny Prijono834aee32006-02-19 01:38:06 +00001763 if (status != PJ_SUCCESS) {
Benny Prijono0bd5eb92009-04-14 11:10:31 +00001764 pjsip_dlg_dec_lock(buddy->dlg);
Benny Prijonoa6992c52007-06-05 22:58:32 +00001765 if (buddy->sub) {
1766 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1767 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001768 buddy->sub = NULL;
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001769 pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE",
1770 status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001771 if (tmp_pool) pj_pool_release(tmp_pool);
Benny Prijono834aee32006-02-19 01:38:06 +00001772 return;
1773 }
1774
Benny Prijono21b9ad92006-08-15 13:11:22 +00001775 pjsua_process_msg_data(tdata, NULL);
1776
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001777 status = pjsip_pres_send_request(buddy->sub, tdata);
Benny Prijono834aee32006-02-19 01:38:06 +00001778 if (status != PJ_SUCCESS) {
Benny Prijono0bd5eb92009-04-14 11:10:31 +00001779 pjsip_dlg_dec_lock(buddy->dlg);
Benny Prijonoa6992c52007-06-05 22:58:32 +00001780 if (buddy->sub) {
1781 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1782 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001783 buddy->sub = NULL;
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001784 pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE",
1785 status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001786 if (tmp_pool) pj_pool_release(tmp_pool);
Benny Prijono834aee32006-02-19 01:38:06 +00001787 return;
1788 }
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001789
Benny Prijono0bd5eb92009-04-14 11:10:31 +00001790 pjsip_dlg_dec_lock(buddy->dlg);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001791 if (tmp_pool) pj_pool_release(tmp_pool);
Benny Prijono834aee32006-02-19 01:38:06 +00001792}
1793
1794
1795/* It does what it says... */
Benny Prijono73bb7232009-10-20 13:56:26 +00001796static void unsubscribe_buddy_presence(pjsua_buddy_id buddy_id)
Benny Prijono834aee32006-02-19 01:38:06 +00001797{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001798 pjsua_buddy *buddy;
Benny Prijono834aee32006-02-19 01:38:06 +00001799 pjsip_tx_data *tdata;
1800 pj_status_t status;
1801
Benny Prijono73bb7232009-10-20 13:56:26 +00001802 buddy = &pjsua_var.buddy[buddy_id];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001803
1804 if (buddy->sub == NULL)
Benny Prijono834aee32006-02-19 01:38:06 +00001805 return;
1806
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001807 if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) {
Benny Prijono73bb7232009-10-20 13:56:26 +00001808 buddy->sub = NULL;
Benny Prijono834aee32006-02-19 01:38:06 +00001809 return;
1810 }
1811
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001812 status = pjsip_pres_initiate( buddy->sub, 0, &tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +00001813 if (status == PJ_SUCCESS) {
1814 pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001815 status = pjsip_pres_send_request( buddy->sub, tdata );
Benny Prijono21b9ad92006-08-15 13:11:22 +00001816 }
Benny Prijono834aee32006-02-19 01:38:06 +00001817
Benny Prijono48da92e2007-05-16 08:56:30 +00001818 if (status != PJ_SUCCESS && buddy->sub) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001819 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1820 buddy->sub = NULL;
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001821 pjsua_perror(THIS_FILE, "Unable to unsubscribe presence",
1822 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001823 }
1824}
1825
Benny Prijono834aee32006-02-19 01:38:06 +00001826/* It does what it says.. */
Benny Prijono73bb7232009-10-20 13:56:26 +00001827static pj_status_t refresh_client_subscriptions(void)
Benny Prijono834aee32006-02-19 01:38:06 +00001828{
Benny Prijono9fc735d2006-05-28 14:58:12 +00001829 unsigned i;
Benny Prijono73bb7232009-10-20 13:56:26 +00001830 pj_status_t status;
Benny Prijonof9c40c32007-06-28 07:20:26 +00001831
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001832 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
Benny Prijono73bb7232009-10-20 13:56:26 +00001833 struct buddy_lock lck;
Benny Prijono834aee32006-02-19 01:38:06 +00001834
Benny Prijono73bb7232009-10-20 13:56:26 +00001835 if (!pjsua_buddy_is_valid(i))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001836 continue;
1837
Benny Prijono73bb7232009-10-20 13:56:26 +00001838 status = lock_buddy("refresh_client_subscriptions()", i, &lck, 0);
1839 if (status != PJ_SUCCESS)
1840 return status;
1841
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001842 if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) {
Benny Prijono834aee32006-02-19 01:38:06 +00001843 subscribe_buddy_presence(i);
1844
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001845 } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) {
Benny Prijono834aee32006-02-19 01:38:06 +00001846 unsubscribe_buddy_presence(i);
1847
1848 }
Benny Prijono73bb7232009-10-20 13:56:26 +00001849
1850 unlock_buddy(&lck);
Benny Prijono834aee32006-02-19 01:38:06 +00001851 }
Benny Prijonof9c40c32007-06-28 07:20:26 +00001852
Benny Prijono73bb7232009-10-20 13:56:26 +00001853 return PJ_SUCCESS;
Benny Prijono834aee32006-02-19 01:38:06 +00001854}
1855
Benny Prijono4dd961b2009-10-26 11:21:37 +00001856/***************************************************************************
1857 * MWI
1858 */
1859/* Callback called when *client* subscription state has changed. */
1860static void mwi_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
1861{
1862 pjsua_acc *acc;
1863
1864 PJ_UNUSED_ARG(event);
1865
1866 /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1867 * a dialog attached to it, lock_buddy() will use the dialog
1868 * lock, which we are currently holding!
1869 */
1870 acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1871 if (!acc)
1872 return;
1873
1874 PJ_LOG(4,(THIS_FILE,
1875 "MWI subscription for %.*s is %s",
1876 (int)acc->cfg.id.slen, acc->cfg.id.ptr,
1877 pjsip_evsub_get_state_name(sub)));
1878
1879 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1880 /* Clear subscription */
1881 acc->mwi_dlg = NULL;
1882 acc->mwi_sub = NULL;
1883 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
1884
1885 }
1886}
1887
1888/* Callback called when we receive NOTIFY */
1889static void mwi_evsub_on_rx_notify(pjsip_evsub *sub,
1890 pjsip_rx_data *rdata,
1891 int *p_st_code,
1892 pj_str_t **p_st_text,
1893 pjsip_hdr *res_hdr,
1894 pjsip_msg_body **p_body)
1895{
1896 pjsua_mwi_info mwi_info;
1897 pjsua_acc *acc;
1898
1899 PJ_UNUSED_ARG(p_st_code);
1900 PJ_UNUSED_ARG(p_st_text);
1901 PJ_UNUSED_ARG(res_hdr);
1902 PJ_UNUSED_ARG(p_body);
1903
1904 acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1905 if (!acc)
1906 return;
1907
1908 /* Construct mwi_info */
1909 pj_bzero(&mwi_info, sizeof(mwi_info));
1910 mwi_info.evsub = sub;
1911 mwi_info.rdata = rdata;
1912
1913 /* Call callback */
1914 if (pjsua_var.ua_cfg.cb.on_mwi_info) {
1915 (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc->index, &mwi_info);
1916 }
1917}
1918
1919
1920/* Event subscription callback. */
1921static pjsip_evsub_user mwi_cb =
1922{
1923 &mwi_evsub_on_state,
1924 NULL, /* on_tsx_state: not interested */
1925 NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless
1926 * we want to authenticate
1927 */
1928
1929 &mwi_evsub_on_rx_notify,
1930
1931 NULL, /* on_client_refresh: Use default behaviour, which is to
1932 * refresh client subscription. */
1933
1934 NULL, /* on_server_timeout: Use default behaviour, which is to send
1935 * NOTIFY to terminate.
1936 */
1937};
1938
1939void pjsua_start_mwi(pjsua_acc *acc)
1940{
1941 pj_pool_t *tmp_pool = NULL;
1942 pj_str_t contact;
1943 pjsip_tx_data *tdata;
1944 pj_status_t status;
1945
1946 if (!acc->cfg.mwi_enabled) {
1947 if (acc->mwi_sub) {
1948 /* Terminate MWI subscription */
1949 pjsip_tx_data *tdata;
1950 pjsip_evsub *sub = acc->mwi_sub;
1951
1952 /* Detach sub from this account */
1953 acc->mwi_sub = NULL;
1954 acc->mwi_dlg = NULL;
1955 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
1956
1957 /* Unsubscribe */
1958 status = pjsip_mwi_initiate(acc->mwi_sub, 0, &tdata);
1959 if (status == PJ_SUCCESS) {
1960 status = pjsip_mwi_send_request(acc->mwi_sub, tdata);
1961 }
1962 }
1963 return;
1964 }
1965
1966 if (acc->mwi_sub) {
1967 /* Subscription is already active */
1968 return;
1969
1970 }
1971
1972 /* Generate suitable Contact header unless one is already set in
1973 * the account
1974 */
1975 if (acc->contact.slen) {
1976 contact = acc->contact;
1977 } else {
1978 tmp_pool = pjsua_pool_create("tmpmwi", 512, 256);
1979 status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
1980 acc->index, &acc->cfg.id);
1981 if (status != PJ_SUCCESS) {
1982 pjsua_perror(THIS_FILE, "Unable to generate Contact header",
1983 status);
1984 pj_pool_release(tmp_pool);
1985 return;
1986 }
1987 }
1988
1989 /* Create UAC dialog */
1990 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
1991 &acc->cfg.id,
1992 &contact,
1993 &acc->cfg.id,
1994 NULL, &acc->mwi_dlg);
1995 if (status != PJ_SUCCESS) {
1996 pjsua_perror(THIS_FILE, "Unable to create dialog", status);
1997 if (tmp_pool) pj_pool_release(tmp_pool);
1998 return;
1999 }
2000
2001 /* Increment the dialog's lock otherwise when presence session creation
2002 * fails the dialog will be destroyed prematurely.
2003 */
2004 pjsip_dlg_inc_lock(acc->mwi_dlg);
2005
2006 /* Create UAC subscription */
2007 status = pjsip_mwi_create_uac(acc->mwi_dlg, &mwi_cb,
2008 PJSIP_EVSUB_NO_EVENT_ID, &acc->mwi_sub);
2009 if (status != PJ_SUCCESS) {
2010 pjsua_perror(THIS_FILE, "Error creating MWI subscription", status);
2011 if (tmp_pool) pj_pool_release(tmp_pool);
2012 pjsip_dlg_dec_lock(acc->mwi_dlg);
2013 return;
2014 }
2015
2016 /* If account is locked to specific transport, then lock dialog
2017 * to this transport too.
2018 */
2019 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
2020 pjsip_tpselector tp_sel;
2021
2022 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2023 pjsip_dlg_set_transport(acc->mwi_dlg, &tp_sel);
2024 }
2025
2026 /* Set route-set */
2027 if (!pj_list_empty(&acc->route_set)) {
2028 pjsip_dlg_set_route_set(acc->mwi_dlg, &acc->route_set);
2029 }
2030
2031 /* Set credentials */
2032 if (acc->cred_cnt) {
2033 pjsip_auth_clt_set_credentials( &acc->mwi_dlg->auth_sess,
2034 acc->cred_cnt, acc->cred);
2035 }
2036
2037 /* Set authentication preference */
2038 pjsip_auth_clt_set_prefs(&acc->mwi_dlg->auth_sess, &acc->cfg.auth_pref);
2039
2040 pjsip_evsub_set_mod_data(acc->mwi_sub, pjsua_var.mod.id, acc);
2041
2042 status = pjsip_mwi_initiate(acc->mwi_sub, -1, &tdata);
2043 if (status != PJ_SUCCESS) {
2044 pjsip_dlg_dec_lock(acc->mwi_dlg);
2045 if (acc->mwi_sub) {
2046 pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE);
2047 }
2048 acc->mwi_sub = NULL;
2049 acc->mwi_dlg = NULL;
2050 pjsua_perror(THIS_FILE, "Unable to create initial MWI SUBSCRIBE",
2051 status);
2052 if (tmp_pool) pj_pool_release(tmp_pool);
2053 return;
2054 }
2055
2056 pjsua_process_msg_data(tdata, NULL);
2057
2058 status = pjsip_pres_send_request(acc->mwi_sub, tdata);
2059 if (status != PJ_SUCCESS) {
2060 pjsip_dlg_dec_lock(acc->mwi_dlg);
2061 if (acc->mwi_sub) {
2062 pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE);
2063 }
2064 acc->mwi_sub = NULL;
2065 acc->mwi_dlg = NULL;
2066 pjsua_perror(THIS_FILE, "Unable to send initial MWI SUBSCRIBE",
2067 status);
2068 if (tmp_pool) pj_pool_release(tmp_pool);
2069 return;
2070 }
2071
2072 pjsip_dlg_dec_lock(acc->mwi_dlg);
2073 if (tmp_pool) pj_pool_release(tmp_pool);
2074
2075}
2076
2077
Benny Prijonofe1bd342009-11-20 23:33:07 +00002078/***************************************************************************
2079 * Unsolicited MWI
2080 */
2081static pj_bool_t unsolicited_mwi_on_rx_request(pjsip_rx_data *rdata)
2082{
2083 pjsip_msg *msg = rdata->msg_info.msg;
2084 pj_str_t EVENT_HDR = { "Event", 5 };
2085 pj_str_t MWI = { "message-summary", 15 };
2086 pjsip_event_hdr *eh;
2087
2088 if (pjsip_method_cmp(&msg->line.req.method, &pjsip_notify_method)!=0) {
2089 /* Only interested with NOTIFY request */
2090 return PJ_FALSE;
2091 }
2092
2093 eh = (pjsip_event_hdr*) pjsip_msg_find_hdr_by_name(msg, &EVENT_HDR, NULL);
2094 if (!eh) {
2095 /* Something wrong with the request, it has no Event hdr */
2096 return PJ_FALSE;
2097 }
2098
2099 if (pj_stricmp(&eh->event_type, &MWI) != 0) {
2100 /* Not MWI event */
2101 return PJ_FALSE;
2102 }
2103
2104 /* Got unsolicited MWI request, respond with 200/OK first */
2105 pjsip_endpt_respond(pjsua_get_pjsip_endpt(), NULL, rdata, 200, NULL,
2106 NULL, NULL, NULL);
2107
2108
2109 /* Call callback */
2110 if (pjsua_var.ua_cfg.cb.on_mwi_info) {
2111 pjsua_acc_id acc_id;
2112 pjsua_mwi_info mwi_info;
2113
2114 acc_id = pjsua_acc_find_for_incoming(rdata);
2115
2116 pj_bzero(&mwi_info, sizeof(mwi_info));
2117 mwi_info.rdata = rdata;
2118
2119 (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc_id, &mwi_info);
2120 }
2121
2122
2123 return PJ_TRUE;
2124}
2125
2126/* The module instance. */
2127static pjsip_module pjsua_unsolicited_mwi_mod =
2128{
2129 NULL, NULL, /* prev, next. */
2130 { "mod-unsolicited-mwi", 19 }, /* Name. */
2131 -1, /* Id */
2132 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
2133 NULL, /* load() */
2134 NULL, /* start() */
2135 NULL, /* stop() */
2136 NULL, /* unload() */
2137 &unsolicited_mwi_on_rx_request, /* on_rx_request() */
2138 NULL, /* on_rx_response() */
2139 NULL, /* on_tx_request. */
2140 NULL, /* on_tx_response() */
2141 NULL, /* on_tsx_state() */
2142};
2143
2144static pj_status_t enable_unsolicited_mwi(void)
2145{
2146 pj_status_t status;
2147
2148 status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(),
2149 &pjsua_unsolicited_mwi_mod);
2150 if (status != PJ_SUCCESS)
2151 pjsua_perror(THIS_FILE, "Error registering unsolicited MWI module",
2152 status);
2153
2154 return status;
2155}
2156
2157
2158
Benny Prijono4dd961b2009-10-26 11:21:37 +00002159/***************************************************************************/
2160
Benny Prijono7a5f5102007-05-29 00:33:09 +00002161/* Timer callback to re-create client subscription */
2162static void pres_timer_cb(pj_timer_heap_t *th,
2163 pj_timer_entry *entry)
2164{
Benny Prijono53984d12009-04-28 22:19:49 +00002165 unsigned i;
Benny Prijono7a5f5102007-05-29 00:33:09 +00002166 pj_time_val delay = { PJSUA_PRES_TIMER, 0 };
2167
Benny Prijono4dd961b2009-10-26 11:21:37 +00002168 entry->id = PJ_FALSE;
2169
2170 /* Retry failed PUBLISH and MWI SUBSCRIBE requests */
Benny Prijono53984d12009-04-28 22:19:49 +00002171 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2172 pjsua_acc *acc = &pjsua_var.acc[i];
Benny Prijono4dd961b2009-10-26 11:21:37 +00002173
2174 /* Retry PUBLISH */
Benny Prijono53984d12009-04-28 22:19:49 +00002175 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
2176 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono53984d12009-04-28 22:19:49 +00002177
Benny Prijono4dd961b2009-10-26 11:21:37 +00002178 /* Re-subscribe MWI subscription if it's terminated prematurely */
2179 if (acc->cfg.mwi_enabled && !acc->mwi_sub)
2180 pjsua_start_mwi(acc);
2181 }
Benny Prijono73bb7232009-10-20 13:56:26 +00002182
2183 /* #937: No need to do bulk client refresh, as buddies have their
2184 * own individual timer now.
2185 */
2186 //refresh_client_subscriptions();
Benny Prijono7a5f5102007-05-29 00:33:09 +00002187
2188 pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, &delay);
2189 entry->id = PJ_TRUE;
2190
Benny Prijonof9c40c32007-06-28 07:20:26 +00002191 PJ_UNUSED_ARG(th);
Benny Prijono7a5f5102007-05-29 00:33:09 +00002192}
2193
Benny Prijono834aee32006-02-19 01:38:06 +00002194
2195/*
2196 * Init presence
2197 */
2198pj_status_t pjsua_pres_init()
2199{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002200 unsigned i;
Benny Prijono834aee32006-02-19 01:38:06 +00002201 pj_status_t status;
2202
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002203 status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres);
Benny Prijono834aee32006-02-19 01:38:06 +00002204 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +00002205 pjsua_perror(THIS_FILE, "Unable to register pjsua presence module",
2206 status);
Benny Prijono834aee32006-02-19 01:38:06 +00002207 }
2208
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002209 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
2210 reset_buddy(i);
2211 }
2212
Benny Prijono834aee32006-02-19 01:38:06 +00002213 return status;
2214}
2215
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002216
Benny Prijono834aee32006-02-19 01:38:06 +00002217/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002218 * Start presence subsystem.
Benny Prijono9fc735d2006-05-28 14:58:12 +00002219 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002220pj_status_t pjsua_pres_start(void)
Benny Prijono9fc735d2006-05-28 14:58:12 +00002221{
Benny Prijono7a5f5102007-05-29 00:33:09 +00002222 /* Start presence timer to re-subscribe to buddy's presence when
2223 * subscription has failed.
2224 */
2225 if (pjsua_var.pres_timer.id == PJ_FALSE) {
2226 pj_time_val pres_interval = {PJSUA_PRES_TIMER, 0};
2227
2228 pjsua_var.pres_timer.cb = &pres_timer_cb;
2229 pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.pres_timer,
2230 &pres_interval);
Benny Prijono97276602007-06-23 01:07:08 +00002231 pjsua_var.pres_timer.id = PJ_TRUE;
Benny Prijono7a5f5102007-05-29 00:33:09 +00002232 }
2233
Benny Prijonofe1bd342009-11-20 23:33:07 +00002234 if (pjsua_var.ua_cfg.enable_unsolicited_mwi) {
2235 pj_status_t status = enable_unsolicited_mwi();
2236 if (status != PJ_SUCCESS)
2237 return status;
2238 }
2239
Benny Prijono9fc735d2006-05-28 14:58:12 +00002240 return PJ_SUCCESS;
2241}
2242
2243
2244/*
Benny Prijono834aee32006-02-19 01:38:06 +00002245 * Shutdown presence.
2246 */
2247void pjsua_pres_shutdown(void)
2248{
Benny Prijono9fc735d2006-05-28 14:58:12 +00002249 unsigned i;
Benny Prijono834aee32006-02-19 01:38:06 +00002250
Benny Prijono384dab42009-10-14 01:58:04 +00002251 PJ_LOG(4,(THIS_FILE, "Shutting down presence.."));
2252
Benny Prijono7a5f5102007-05-29 00:33:09 +00002253 if (pjsua_var.pres_timer.id != 0) {
2254 pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.pres_timer);
2255 pjsua_var.pres_timer.id = PJ_FALSE;
2256 }
2257
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002258 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2259 if (!pjsua_var.acc[i].valid)
2260 continue;
2261 pjsua_pres_delete_acc(i);
Benny Prijonoa91a0032006-02-26 21:23:45 +00002262 }
2263
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002264 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
2265 pjsua_var.buddy[i].monitor = 0;
Benny Prijono834aee32006-02-19 01:38:06 +00002266 }
Benny Prijonoa91a0032006-02-26 21:23:45 +00002267
Benny Prijono73bb7232009-10-20 13:56:26 +00002268 refresh_client_subscriptions();
2269
2270 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2271 if (pjsua_var.acc[i].valid)
2272 pjsua_pres_update_acc(i, PJ_FALSE);
2273 }
Benny Prijono834aee32006-02-19 01:38:06 +00002274}