Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 5 | * |
| 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 Prijono | 4f9f64e | 2006-02-27 00:00:30 +0000 | [diff] [blame] | 20 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 21 | #include <pjsua-lib/pjsua_internal.h> |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 22 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 23 | |
| 24 | #define THIS_FILE "pjsua_pres.c" |
| 25 | |
Benny Prijono | a17496a | 2007-10-31 10:20:31 +0000 | [diff] [blame] | 26 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 27 | static void subscribe_buddy_presence(pjsua_buddy_id buddy_id); |
| 28 | static void unsubscribe_buddy_presence(pjsua_buddy_id buddy_id); |
Benny Prijono | a17496a | 2007-10-31 10:20:31 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | /* |
| 32 | * Find buddy. |
| 33 | */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 34 | static pjsua_buddy_id find_buddy(const pjsip_uri *uri) |
Benny Prijono | a17496a | 2007-10-31 10:20:31 +0000 | [diff] [blame] | 35 | { |
| 36 | const pjsip_sip_uri *sip_uri; |
| 37 | unsigned i; |
| 38 | |
Benny Prijono | f0f8fd1 | 2007-11-10 12:05:59 +0000 | [diff] [blame] | 39 | uri = (const pjsip_uri*) pjsip_uri_get_uri((pjsip_uri*)uri); |
Benny Prijono | a17496a | 2007-10-31 10:20:31 +0000 | [diff] [blame] | 40 | |
| 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 Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 63 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 64 | #define LOCK_DIALOG 1 |
| 65 | #define LOCK_PJSUA 2 |
| 66 | #define LOCK_ALL (LOCK_DIALOG | LOCK_PJSUA) |
| 67 | |
| 68 | /* Buddy lock object */ |
| 69 | struct 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 */ |
| 77 | pj_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 */ |
| 136 | static 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 145 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 146 | /* |
| 147 | * Get total number of buddies. |
| 148 | */ |
| 149 | PJ_DEF(unsigned) pjsua_get_buddy_count(void) |
| 150 | { |
| 151 | return pjsua_var.buddy_cnt; |
| 152 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 153 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 156 | * Find buddy. |
| 157 | */ |
| 158 | PJ_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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 171 | else { |
| 172 | PJSUA_LOCK(); |
| 173 | buddy_id = find_buddy(uri); |
| 174 | PJSUA_UNLOCK(); |
| 175 | } |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 176 | |
| 177 | pj_pool_release(pool); |
| 178 | |
| 179 | return buddy_id; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 184 | * Check if buddy ID is valid. |
| 185 | */ |
| 186 | PJ_DEF(pj_bool_t) pjsua_buddy_is_valid(pjsua_buddy_id buddy_id) |
| 187 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 188 | return buddy_id>=0 && buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy) && |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 189 | pjsua_var.buddy[buddy_id].uri.slen != 0; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /* |
| 194 | * Enum buddy IDs. |
| 195 | */ |
| 196 | PJ_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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
| 220 | /* |
| 221 | * Get detailed buddy info. |
| 222 | */ |
| 223 | PJ_DEF(pj_status_t) pjsua_buddy_get_info( pjsua_buddy_id buddy_id, |
| 224 | pjsua_buddy_info *info) |
| 225 | { |
Benny Prijono | 20da799 | 2008-12-18 16:48:43 +0000 | [diff] [blame] | 226 | unsigned total=0; |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 227 | struct buddy_lock lck; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 228 | pjsua_buddy *buddy; |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 229 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 230 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 231 | PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 232 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 233 | pj_bzero(info, sizeof(pjsua_buddy_info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 234 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 235 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 240 | info->id = buddy->index; |
| 241 | if (pjsua_var.buddy[buddy_id].uri.slen == 0) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 242 | unlock_buddy(&lck); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 243 | 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 Prijono | 9727660 | 2007-06-23 01:07:08 +0000 | [diff] [blame] | 255 | |
Benny Prijono | 28add7e | 2009-06-15 16:03:40 +0000 | [diff] [blame] | 256 | /* Presence status */ |
| 257 | pj_memcpy(&info->pres_status, &buddy->status, sizeof(pjsip_pres_status)); |
| 258 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 259 | /* 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 Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 265 | |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 274 | } else { |
| 275 | info->status = PJSUA_BUDDY_STATUS_OFFLINE; |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 276 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* monitor pres */ |
| 285 | info->monitor_pres = buddy->monitor; |
| 286 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 287 | /* subscription state and termination reason */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 288 | info->sub_term_code = buddy->term_code; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 289 | if (buddy->sub) { |
| 290 | info->sub_state = pjsip_evsub_get_state(buddy->sub); |
Benny Prijono | d06d8c5 | 2009-06-30 13:53:47 +0000 | [diff] [blame] | 291 | info->sub_state_name = pjsip_evsub_get_state_name(buddy->sub); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 292 | 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 Prijono | d06d8c5 | 2009-06-30 13:53:47 +0000 | [diff] [blame] | 304 | info->sub_state_name = "NULL"; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 305 | 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 Prijono | d06d8c5 | 2009-06-30 13:53:47 +0000 | [diff] [blame] | 310 | info->sub_state_name = "NULL"; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 311 | info->sub_term_reason = pj_str(""); |
| 312 | } |
| 313 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 314 | unlock_buddy(&lck); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 315 | return PJ_SUCCESS; |
| 316 | } |
| 317 | |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 318 | /* |
| 319 | * Set the user data associated with the buddy object. |
| 320 | */ |
| 321 | PJ_DEF(pj_status_t) pjsua_buddy_set_user_data( pjsua_buddy_id buddy_id, |
| 322 | void *user_data) |
| 323 | { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 324 | struct buddy_lock lck; |
| 325 | pj_status_t status; |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 326 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 327 | 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 Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 332 | |
| 333 | pjsua_var.buddy[buddy_id].user_data = user_data; |
| 334 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 335 | unlock_buddy(&lck); |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 336 | |
| 337 | return PJ_SUCCESS; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | /* |
| 342 | * Get the user data associated with the budy object. |
| 343 | */ |
| 344 | PJ_DEF(void*) pjsua_buddy_get_user_data(pjsua_buddy_id buddy_id) |
| 345 | { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 346 | struct buddy_lock lck; |
| 347 | pj_status_t status; |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 348 | void *user_data; |
| 349 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 350 | PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), NULL); |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 351 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 352 | status = lock_buddy("pjsua_buddy_get_user_data()", buddy_id, &lck, 0); |
| 353 | if (status != PJ_SUCCESS) |
| 354 | return NULL; |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 355 | |
| 356 | user_data = pjsua_var.buddy[buddy_id].user_data; |
| 357 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 358 | unlock_buddy(&lck); |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 359 | |
| 360 | return user_data; |
| 361 | } |
| 362 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | * Reset buddy descriptor. |
| 366 | */ |
| 367 | static void reset_buddy(pjsua_buddy_id id) |
| 368 | { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 369 | pj_pool_t *pool = pjsua_var.buddy[id].pool; |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 370 | pj_bzero(&pjsua_var.buddy[id], sizeof(pjsua_var.buddy[id])); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 371 | pjsua_var.buddy[id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 372 | pjsua_var.buddy[id].index = id; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /* |
| 377 | * Add new buddy. |
| 378 | */ |
| 379 | PJ_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 Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 383 | pjsua_buddy *buddy; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 384 | 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 Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 395 | for (index=0; index<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++index) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 396 | 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 Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 408 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 418 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 419 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 424 | /* Get name and display name for buddy */ |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 425 | pj_strdup_with_null(buddy->pool, &tmp, &cfg->uri); |
| 426 | url = (pjsip_name_addr*)pjsip_parse_uri(buddy->pool, tmp.ptr, tmp.slen, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 427 | PJSIP_PARSE_URI_AS_NAMEADDR); |
| 428 | |
| 429 | if (url == NULL) { |
| 430 | pjsua_perror(THIS_FILE, "Unable to add buddy", PJSIP_EINVALIDURI); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 431 | pj_pool_release(buddy->pool); |
| 432 | buddy->pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 433 | PJSUA_UNLOCK(); |
| 434 | return PJSIP_EINVALIDURI; |
| 435 | } |
| 436 | |
Benny Prijono | fc49359 | 2007-02-18 20:56:32 +0000 | [diff] [blame] | 437 | /* Only support SIP schemes */ |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 438 | 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 Prijono | fc49359 | 2007-02-18 20:56:32 +0000 | [diff] [blame] | 442 | return PJSIP_EINVALIDSCHEME; |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 443 | } |
Benny Prijono | fc49359 | 2007-02-18 20:56:32 +0000 | [diff] [blame] | 444 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 445 | /* 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 Prijono | 9c1528f | 2007-02-10 19:22:25 +0000 | [diff] [blame] | 453 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(url->uri); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 454 | 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 Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 462 | /* Save user data */ |
| 463 | pjsua_var.buddy[index].user_data = (void*)cfg->user_data; |
| 464 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 465 | if (p_buddy_id) |
| 466 | *p_buddy_id = index; |
| 467 | |
| 468 | pjsua_var.buddy_cnt++; |
| 469 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 470 | PJSUA_UNLOCK(); |
| 471 | |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 472 | pjsua_buddy_subscribe_pres(index, cfg->subscribe); |
| 473 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 474 | return PJ_SUCCESS; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /* |
| 479 | * Delete buddy. |
| 480 | */ |
| 481 | PJ_DEF(pj_status_t) pjsua_buddy_del(pjsua_buddy_id buddy_id) |
| 482 | { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 483 | struct buddy_lock lck; |
| 484 | pj_status_t status; |
| 485 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 486 | PJ_ASSERT_RETURN(buddy_id>=0 && |
| 487 | buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy), |
| 488 | PJ_EINVAL); |
| 489 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 490 | if (pjsua_var.buddy[buddy_id].uri.slen == 0) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 491 | return PJ_SUCCESS; |
| 492 | } |
| 493 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 494 | status = lock_buddy("pjsua_buddy_del()", buddy_id, &lck, 0); |
| 495 | if (status != PJ_SUCCESS) |
| 496 | return status; |
| 497 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 498 | /* Unsubscribe presence */ |
| 499 | pjsua_buddy_subscribe_pres(buddy_id, PJ_FALSE); |
| 500 | |
Benny Prijono | a5776cb | 2009-04-14 15:11:23 +0000 | [diff] [blame] | 501 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 507 | /* 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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 514 | unlock_buddy(&lck); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 515 | return PJ_SUCCESS; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | /* |
| 520 | * Enable/disable buddy's presence monitoring. |
| 521 | */ |
| 522 | PJ_DEF(pj_status_t) pjsua_buddy_subscribe_pres( pjsua_buddy_id buddy_id, |
| 523 | pj_bool_t subscribe) |
| 524 | { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 525 | struct buddy_lock lck; |
| 526 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 527 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 528 | PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 529 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 530 | status = lock_buddy("pjsua_buddy_subscribe_pres()", buddy_id, &lck, 0); |
| 531 | if (status != PJ_SUCCESS) |
| 532 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 533 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 534 | lck.buddy->monitor = subscribe; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 535 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 536 | pjsua_buddy_update_pres(buddy_id); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 537 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 538 | unlock_buddy(&lck); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 539 | return PJ_SUCCESS; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /* |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 544 | * Update buddy's presence. |
| 545 | */ |
| 546 | PJ_DEF(pj_status_t) pjsua_buddy_update_pres(pjsua_buddy_id buddy_id) |
| 547 | { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 548 | struct buddy_lock lck; |
| 549 | pj_status_t status; |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 550 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 551 | PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL); |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 552 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 553 | status = lock_buddy("pjsua_buddy_update_pres()", buddy_id, &lck, 0); |
| 554 | if (status != PJ_SUCCESS) |
| 555 | return status; |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 556 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 557 | /* 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 Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* Ignore if presence is already active for the buddy */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 565 | if (lck.buddy->sub) { |
| 566 | unlock_buddy(&lck); |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 567 | return PJ_SUCCESS; |
| 568 | } |
| 569 | |
| 570 | /* Initiate presence subscription */ |
| 571 | subscribe_buddy_presence(buddy_id); |
| 572 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 573 | unlock_buddy(&lck); |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 574 | |
| 575 | return PJ_SUCCESS; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 580 | * Dump presence subscriptions to log file. |
| 581 | */ |
| 582 | PJ_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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 702 | */ |
| 703 | |
| 704 | /* Proto */ |
| 705 | static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata); |
| 706 | |
| 707 | /* The module instance. */ |
| 708 | static 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 714 | 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. */ |
| 728 | static void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event) |
| 729 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 730 | pjsua_srv_pres *uapres; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 731 | |
| 732 | PJ_UNUSED_ARG(event); |
| 733 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 734 | PJSUA_LOCK(); |
| 735 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 736 | uapres = (pjsua_srv_pres*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 737 | if (uapres) { |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 738 | pjsip_evsub_state state; |
| 739 | |
Benny Prijono | ba736c4 | 2008-07-10 20:45:03 +0000 | [diff] [blame] | 740 | PJ_LOG(4,(THIS_FILE, "Server subscription to %s is %s", |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 741 | uapres->remote, pjsip_evsub_get_state_name(sub))); |
| 742 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 743 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 755 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 756 | pj_list_erase(uapres); |
| 757 | } |
| 758 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 759 | |
| 760 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /* This is called when request is received. |
| 764 | * We need to check for incoming SUBSCRIBE request. |
| 765 | */ |
| 766 | static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata) |
| 767 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 768 | int acc_id; |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 769 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 770 | pj_str_t contact; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 771 | 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 775 | pjsip_dialog *dlg; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 776 | pjsip_status_code st_code; |
| 777 | pj_str_t reason; |
Benny Prijono | c61cc04 | 2007-06-27 13:01:59 +0000 | [diff] [blame] | 778 | pjsip_expires_hdr *expires_hdr; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 779 | pjsua_msg_data msg_data; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 780 | pj_status_t status; |
| 781 | |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 782 | if (pjsip_method_cmp(req_method, pjsip_get_subscribe_method()) != 0) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 783 | return PJ_FALSE; |
| 784 | |
| 785 | /* Incoming SUBSCRIBE: */ |
| 786 | |
Benny Prijono | 384dab4 | 2009-10-14 01:58:04 +0000 | [diff] [blame] | 787 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 795 | PJSUA_LOCK(); |
| 796 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 797 | /* Find which account for the incoming request. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 798 | acc_id = pjsua_acc_find_for_incoming(rdata); |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 799 | acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 800 | |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 801 | PJ_LOG(4,(THIS_FILE, "Creating server subscription, using account %d", |
| 802 | acc_id)); |
| 803 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 804 | /* Create suitable Contact header */ |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 805 | 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 Prijono | a330d45 | 2008-08-05 20:14:39 +0000 | [diff] [blame] | 814 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL, |
| 815 | NULL, NULL); |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 816 | return PJ_TRUE; |
| 817 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 820 | /* Create UAS dialog: */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 821 | status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 822 | &contact, &dlg); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 823 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 824 | pjsua_perror(THIS_FILE, |
| 825 | "Unable to create UAS dialog for subscription", |
| 826 | status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 827 | PJSUA_UNLOCK(); |
Benny Prijono | a330d45 | 2008-08-05 20:14:39 +0000 | [diff] [blame] | 828 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL, |
| 829 | NULL, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 830 | return PJ_TRUE; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 833 | /* Set credentials and preference. */ |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 834 | pjsip_auth_clt_set_credentials(&dlg->auth_sess, acc->cred_cnt, acc->cred); |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 835 | pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref); |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 836 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 837 | /* Init callback: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 838 | pj_bzero(&pres_cb, sizeof(pres_cb)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 839 | 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 Prijono | dbd9d4b | 2008-09-11 10:25:51 +0000 | [diff] [blame] | 844 | int code = PJSIP_ERRNO_TO_SIP_STATUS(status); |
| 845 | pjsip_tx_data *tdata; |
| 846 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 847 | pjsua_perror(THIS_FILE, "Unable to create server subscription", |
| 848 | status); |
Benny Prijono | dbd9d4b | 2008-09-11 10:25:51 +0000 | [diff] [blame] | 849 | |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 860 | PJSUA_UNLOCK(); |
| 861 | return PJ_TRUE; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 864 | /* 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 874 | /* Attach our data to the subscription: */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 875 | uapres = PJ_POOL_ALLOC_T(dlg->pool, pjsua_srv_pres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 876 | uapres->sub = sub; |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 877 | uapres->remote = (char*) pj_pool_alloc(dlg->pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 878 | uapres->acc_id = acc_id; |
| 879 | uapres->dlg = dlg; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 880 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 887 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, uapres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 888 | |
| 889 | /* Add server subscription to the list: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 890 | pj_list_push_back(&pjsua_var.acc[acc_id].pres_srv_list, uapres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 891 | |
| 892 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 893 | /* 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 Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 902 | st_code = (pjsip_status_code)200; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 903 | 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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 910 | buddy_id = find_buddy(rdata->msg_info.from->uri); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 911 | |
| 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 953 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 954 | pjsua_perror(THIS_FILE, "Unable to accept presence subscription", |
| 955 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 956 | pj_list_erase(uapres); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 957 | pjsip_pres_terminate(sub, PJ_FALSE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 958 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 959 | return PJ_FALSE; |
| 960 | } |
| 961 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 962 | /* 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 | */ |
| 979 | PJ_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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1012 | |
| 1013 | /* Set our online status: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1014 | pj_bzero(&pres_status, sizeof(pres_status)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1015 | pres_status.info_cnt = 1; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1016 | pres_status.info[0].basic_open = acc->online_status; |
| 1017 | pres_status.info[0].id = acc->cfg.pidf_tuple_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1018 | //Both pjsua_var.local_uri and pjsua_var.contact_uri are enclosed in "<" and ">" |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1019 | //causing XML parsing to fail. |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1020 | //pres_status.info[0].contact = pjsua_var.local_uri; |
Benny Prijono | 7f6ee02 | 2008-07-31 08:32:46 +0000 | [diff] [blame] | 1021 | /* add RPID information */ |
| 1022 | pj_memcpy(&pres_status.info[0].rpid, &acc->rpid, |
| 1023 | sizeof(pjrpid_element)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1024 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1025 | pjsip_pres_set_status(srv_pres->sub, &pres_status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1026 | |
Benny Prijono | c61cc04 | 2007-06-27 13:01:59 +0000 | [diff] [blame] | 1027 | /* Check expires value. If it's zero, send our presense state but |
| 1028 | * set subscription state to TERMINATED. |
| 1029 | */ |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1030 | if (srv_pres->expires == 0) |
Benny Prijono | c61cc04 | 2007-06-27 13:01:59 +0000 | [diff] [blame] | 1031 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
Benny Prijono | c61cc04 | 2007-06-27 13:01:59 +0000 | [diff] [blame] | 1032 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1033 | /* Create and send the NOTIFY to active subscription: */ |
| 1034 | status = pjsip_pres_notify(srv_pres->sub, ev_state, state_str, |
| 1035 | reason, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1036 | if (status == PJ_SUCCESS) { |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1037 | /* 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 Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1043 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1044 | |
| 1045 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1046 | pjsua_perror(THIS_FILE, "Unable to create/send NOTIFY", |
| 1047 | status); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1048 | pj_list_erase(srv_pres); |
| 1049 | pjsip_pres_terminate(srv_pres->sub, PJ_FALSE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1050 | PJSUA_UNLOCK(); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1051 | return status; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | |
Benny Prijono | a17496a | 2007-10-31 10:20:31 +0000 | [diff] [blame] | 1055 | /* Subscribe to buddy's presence if we're not subscribed */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1056 | buddy_id = find_buddy(srv_pres->dlg->remote.info->uri); |
Benny Prijono | a17496a | 2007-10-31 10:20:31 +0000 | [diff] [blame] | 1057 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1066 | PJSUA_UNLOCK(); |
| 1067 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1068 | return PJ_SUCCESS; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1072 | /* |
| 1073 | * Client presence publication callback. |
| 1074 | */ |
| 1075 | static void publish_cb(struct pjsip_publishc_cbparam *param) |
| 1076 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1077 | pjsua_acc *acc = (pjsua_acc*) param->token; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1078 | |
| 1079 | if (param->code/100 != 2 || param->status != PJ_SUCCESS) { |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1080 | |
| 1081 | pjsip_publishc_destroy(param->pubc); |
| 1082 | acc->publish_sess = NULL; |
| 1083 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1084 | 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 Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1091 | } 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 Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1097 | } 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 Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1104 | } else { |
Benny Prijono | 534a9ba | 2009-10-13 14:01:59 +0000 | [diff] [blame] | 1105 | if (param->expiration < 1) { |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1106 | /* 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 Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | /* |
| 1117 | * Send PUBLISH request. |
| 1118 | */ |
| 1119 | static 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 Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 1130 | char *bpos; |
| 1131 | pj_str_t entity; |
| 1132 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1133 | 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 Prijono | fe04fb5 | 2007-08-24 08:28:52 +0000 | [diff] [blame] | 1143 | pres_status.info[0].id = acc->cfg.pidf_tuple_id; |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1144 | /* .. including RPID information */ |
| 1145 | pj_memcpy(&pres_status.info[0].rpid, &acc->rpid, |
| 1146 | sizeof(pjrpid_element)); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1147 | |
Benny Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 1148 | /* 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 Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1164 | /* Create and add PIDF message body */ |
| 1165 | status = pjsip_pres_create_pidf(tdata->pool, &pres_status, |
Benny Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 1166 | &entity, &tdata->msg->body); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1167 | 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 Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 1186 | 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 Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1190 | 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 | |
| 1197 | on_error: |
Benny Prijono | 2943815 | 2007-06-28 02:47:32 +0000 | [diff] [blame] | 1198 | if (acc->publish_sess) { |
| 1199 | pjsip_publishc_destroy(acc->publish_sess); |
| 1200 | acc->publish_sess = NULL; |
| 1201 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1202 | return status; |
| 1203 | } |
| 1204 | |
| 1205 | |
| 1206 | /* Create client publish session */ |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 1207 | pj_status_t pjsua_pres_init_publish_acc(int acc_id) |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1208 | { |
| 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 Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 1218 | status = pjsip_publishc_create(pjsua_var.endpt, &acc_cfg->publish_opt, |
| 1219 | acc, &publish_cb, |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1220 | &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 Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1230 | PJSUA_PUBLISH_EXPIRATION); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1231 | if (status != PJ_SUCCESS) { |
| 1232 | acc->publish_sess = NULL; |
| 1233 | return status; |
| 1234 | } |
| 1235 | |
Benny Prijono | 703b7d7 | 2007-03-20 09:13:24 +0000 | [diff] [blame] | 1236 | /* Add credential for authentication */ |
Benny Prijono | 2943815 | 2007-06-28 02:47:32 +0000 | [diff] [blame] | 1237 | if (acc->cred_cnt) { |
| 1238 | pjsip_publishc_set_credentials(acc->publish_sess, acc->cred_cnt, |
| 1239 | acc->cred); |
| 1240 | } |
Benny Prijono | 703b7d7 | 2007-03-20 09:13:24 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* Set route-set */ |
| 1243 | pjsip_publishc_set_route_set(acc->publish_sess, &acc->route_set); |
| 1244 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1245 | /* 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 */ |
| 1261 | pj_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 Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 1268 | return PJ_SUCCESS; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1272 | /* Terminate server subscription for the account */ |
| 1273 | void pjsua_pres_delete_acc(int acc_id) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1274 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1275 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1276 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1277 | pjsua_srv_pres *uapres; |
| 1278 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1279 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1280 | |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 1281 | /* Notify all subscribers that we're no longer available */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1282 | while (uapres != &acc->pres_srv_list) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1283 | |
| 1284 | pjsip_pres_status pres_status; |
| 1285 | pj_str_t reason = { "noresource", 10 }; |
Benny Prijono | 5516f91 | 2008-05-05 12:06:08 +0000 | [diff] [blame] | 1286 | pjsua_srv_pres *next; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1287 | pjsip_tx_data *tdata; |
| 1288 | |
Benny Prijono | 5516f91 | 2008-05-05 12:06:08 +0000 | [diff] [blame] | 1289 | next = uapres->next; |
| 1290 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1291 | 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 Prijono | 5516f91 | 2008-05-05 12:06:08 +0000 | [diff] [blame] | 1303 | uapres = next; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1304 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1305 | |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 1306 | /* 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 Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1311 | if (acc->publish_sess) { |
| 1312 | acc->online_status = PJ_FALSE; |
| 1313 | send_publish(acc_id, PJ_FALSE); |
Benny Prijono | 534a9ba | 2009-10-13 14:01:59 +0000 | [diff] [blame] | 1314 | /* By ticket #364, don't destroy the session yet (let the callback |
| 1315 | destroy it) |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1316 | if (acc->publish_sess) { |
| 1317 | pjsip_publishc_destroy(acc->publish_sess); |
| 1318 | acc->publish_sess = NULL; |
| 1319 | } |
Benny Prijono | 534a9ba | 2009-10-13 14:01:59 +0000 | [diff] [blame] | 1320 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1321 | acc_cfg->publish_enabled = PJ_FALSE; |
| 1322 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1326 | /* Update server subscription (e.g. when our online status has changed) */ |
| 1327 | void pjsua_pres_update_acc(int acc_id, pj_bool_t force) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1328 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1329 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1330 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1331 | pjsua_srv_pres *uapres; |
| 1332 | |
| 1333 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
| 1334 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1335 | while (uapres != &acc->pres_srv_list) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1336 | |
| 1337 | pjsip_pres_status pres_status; |
| 1338 | pjsip_tx_data *tdata; |
| 1339 | |
| 1340 | pjsip_pres_get_status(uapres->sub, &pres_status); |
Benny Prijono | 232759b | 2008-09-08 12:46:29 +0000 | [diff] [blame] | 1341 | |
| 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 Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1351 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1352 | pres_status.info[0].basic_open = acc->online_status; |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1353 | pj_memcpy(&pres_status.info[0].rpid, &acc->rpid, |
| 1354 | sizeof(pjrpid_element)); |
| 1355 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1356 | pjsip_pres_set_status(uapres->sub, &pres_status); |
| 1357 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1358 | if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) { |
| 1359 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1360 | pjsip_pres_send_request(uapres->sub, tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1361 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | uapres = uapres->next; |
| 1365 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1366 | |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 1367 | /* 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 Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1373 | if (force || acc->publish_state != acc->online_status) { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1374 | send_publish(acc_id, PJ_TRUE); |
| 1375 | } |
| 1376 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | |
| 1380 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1381 | /*************************************************************************** |
| 1382 | * Client subscription. |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1383 | */ |
| 1384 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1385 | static 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 | */ |
| 1398 | static 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 Prijono | 12c01a9 | 2009-10-21 02:37:52 +0000 | [diff] [blame] | 1409 | 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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1415 | 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1425 | /* Callback called when *client* subscription state has changed. */ |
| 1426 | static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event) |
| 1427 | { |
| 1428 | pjsua_buddy *buddy; |
| 1429 | |
| 1430 | PJ_UNUSED_ARG(event); |
| 1431 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1432 | /* 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 Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1436 | buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1437 | if (buddy) { |
Benny Prijono | ba736c4 | 2008-07-10 20:45:03 +0000 | [diff] [blame] | 1438 | PJ_LOG(4,(THIS_FILE, |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1439 | "Presence subscription to %.*s is %s", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1440 | (int)pjsua_var.buddy[buddy->index].uri.slen, |
| 1441 | pjsua_var.buddy[buddy->index].uri.ptr, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1442 | pjsip_evsub_get_state_name(sub))); |
| 1443 | |
| 1444 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1445 | int resub_delay = -1; |
| 1446 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1447 | 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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1455 | |
| 1456 | buddy->term_code = 200; |
| 1457 | |
| 1458 | /* Determine whether to resubscribe automatically */ |
| 1459 | if (event->type==PJSIP_EVENT_TSX_STATE) { |
| 1460 | 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 Prijono | 6ab0532 | 2009-10-21 03:03:06 +0000 | [diff] [blame^] | 1470 | /* 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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1478 | 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 Prijono | 6ab0532 | 2009-10-21 03:03:06 +0000 | [diff] [blame^] | 1540 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1541 | } else { |
Benny Prijono | 6ab0532 | 2009-10-21 03:03:06 +0000 | [diff] [blame^] | 1542 | /* This will clear the last termination code/reason */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1543 | buddy->term_code = 0; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1544 | buddy->term_reason.slen = 0; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1545 | } |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1546 | |
| 1547 | /* Call callback */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1548 | if (pjsua_var.ua_cfg.cb.on_buddy_state) |
| 1549 | (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1550 | |
| 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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1555 | buddy->dlg = NULL; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1556 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 1557 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1561 | |
| 1562 | /* Callback when transaction state has changed. */ |
| 1563 | static 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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1570 | /* 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 Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1574 | buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1575 | if (!buddy) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1576 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1584 | 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 Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 1591 | pjsip_method_cmp(&tsx->method, pjsip_get_subscribe_method())!=0) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1592 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1593 | return; |
| 1594 | } |
| 1595 | |
| 1596 | /* Find contact header. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1597 | contact_hdr = (pjsip_contact_hdr*) |
| 1598 | pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1599 | PJSIP_H_CONTACT, NULL); |
| 1600 | if (!contact_hdr) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1601 | return; |
| 1602 | } |
| 1603 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1604 | buddy->contact.ptr = (char*) |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1605 | pj_pool_alloc(buddy->pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1606 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1615 | /* Callback called when we receive NOTIFY */ |
| 1616 | static 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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1625 | /* 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 Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1629 | buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1630 | if (buddy) { |
| 1631 | /* Update our info. */ |
| 1632 | pjsip_pres_get_status(sub, &buddy->status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1633 | } |
| 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. */ |
| 1647 | static pjsip_evsub_user pres_callback = |
| 1648 | { |
| 1649 | &pjsua_evsub_on_state, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1650 | &pjsua_evsub_on_tsx_state, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1651 | |
| 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 Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1668 | static void subscribe_buddy_presence(pjsua_buddy_id buddy_id) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1669 | { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1670 | pj_pool_t *tmp_pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1671 | pjsua_buddy *buddy; |
| 1672 | int acc_id; |
| 1673 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1674 | pj_str_t contact; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1675 | pjsip_tx_data *tdata; |
| 1676 | pj_status_t status; |
| 1677 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1678 | buddy = &pjsua_var.buddy[buddy_id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1679 | acc_id = pjsua_acc_find_for_outgoing(&buddy->uri); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1680 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1681 | acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1682 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1683 | PJ_LOG(4,(THIS_FILE, "Using account %d for buddy %d subscription", |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1684 | acc_id, buddy_id)); |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1685 | |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1686 | /* 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 Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1692 | tmp_pool = pjsua_pool_create("tmpbuddy", 512, 256); |
| 1693 | |
| 1694 | status = pjsua_acc_create_uac_contact(tmp_pool, &contact, |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1695 | acc_id, &buddy->uri); |
| 1696 | if (status != PJ_SUCCESS) { |
| 1697 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", |
| 1698 | status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1699 | pj_pool_release(tmp_pool); |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1700 | return; |
| 1701 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1704 | /* Create UAC dialog */ |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1705 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1706 | &acc->cfg.id, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1707 | &contact, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1708 | &buddy->uri, |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1709 | NULL, &buddy->dlg); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1710 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1711 | pjsua_perror(THIS_FILE, "Unable to create dialog", |
| 1712 | status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1713 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1714 | return; |
| 1715 | } |
| 1716 | |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 1717 | /* 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 Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1722 | status = pjsip_pres_create_uac( buddy->dlg, &pres_callback, |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1723 | PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub); |
| 1724 | if (status != PJ_SUCCESS) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1725 | buddy->sub = NULL; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1726 | pjsua_perror(THIS_FILE, "Unable to create presence client", |
| 1727 | status); |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 1728 | /* This should destroy the dialog since there's no session |
| 1729 | * referencing it |
| 1730 | */ |
| 1731 | pjsip_dlg_dec_lock(buddy->dlg); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1732 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1733 | return; |
| 1734 | } |
| 1735 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1736 | /* 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 Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1743 | pjsip_dlg_set_transport(buddy->dlg, &tp_sel); |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1746 | /* Set route-set */ |
| 1747 | if (!pj_list_empty(&acc->route_set)) { |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1748 | pjsip_dlg_set_route_set(buddy->dlg, &acc->route_set); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /* Set credentials */ |
| 1752 | if (acc->cred_cnt) { |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1753 | pjsip_auth_clt_set_credentials( &buddy->dlg->auth_sess, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1754 | acc->cred_cnt, acc->cred); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1755 | } |
Benny Prijono | 1d8d608 | 2006-04-29 12:38:25 +0000 | [diff] [blame] | 1756 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 1757 | /* Set authentication preference */ |
| 1758 | pjsip_auth_clt_set_prefs(&buddy->dlg->auth_sess, &acc->cfg.auth_pref); |
| 1759 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1760 | pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1761 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1762 | status = pjsip_pres_initiate(buddy->sub, -1, &tdata); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1763 | if (status != PJ_SUCCESS) { |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 1764 | pjsip_dlg_dec_lock(buddy->dlg); |
Benny Prijono | a6992c5 | 2007-06-05 22:58:32 +0000 | [diff] [blame] | 1765 | if (buddy->sub) { |
| 1766 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1767 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1768 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1769 | pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE", |
| 1770 | status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1771 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1772 | return; |
| 1773 | } |
| 1774 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1775 | pjsua_process_msg_data(tdata, NULL); |
| 1776 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1777 | status = pjsip_pres_send_request(buddy->sub, tdata); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1778 | if (status != PJ_SUCCESS) { |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 1779 | pjsip_dlg_dec_lock(buddy->dlg); |
Benny Prijono | a6992c5 | 2007-06-05 22:58:32 +0000 | [diff] [blame] | 1780 | if (buddy->sub) { |
| 1781 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1782 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1783 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1784 | pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE", |
| 1785 | status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1786 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1787 | return; |
| 1788 | } |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1789 | |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 1790 | pjsip_dlg_dec_lock(buddy->dlg); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1791 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | /* It does what it says... */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1796 | static void unsubscribe_buddy_presence(pjsua_buddy_id buddy_id) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1797 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1798 | pjsua_buddy *buddy; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1799 | pjsip_tx_data *tdata; |
| 1800 | pj_status_t status; |
| 1801 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1802 | buddy = &pjsua_var.buddy[buddy_id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1803 | |
| 1804 | if (buddy->sub == NULL) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1805 | return; |
| 1806 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1807 | if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1808 | buddy->sub = NULL; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1809 | return; |
| 1810 | } |
| 1811 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1812 | status = pjsip_pres_initiate( buddy->sub, 0, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1813 | if (status == PJ_SUCCESS) { |
| 1814 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1815 | status = pjsip_pres_send_request( buddy->sub, tdata ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1816 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1817 | |
Benny Prijono | 48da92e | 2007-05-16 08:56:30 +0000 | [diff] [blame] | 1818 | if (status != PJ_SUCCESS && buddy->sub) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1819 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1820 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1821 | pjsua_perror(THIS_FILE, "Unable to unsubscribe presence", |
| 1822 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1826 | /* It does what it says.. */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1827 | static pj_status_t refresh_client_subscriptions(void) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1828 | { |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1829 | unsigned i; |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1830 | pj_status_t status; |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1831 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1832 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1833 | struct buddy_lock lck; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1834 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1835 | if (!pjsua_buddy_is_valid(i)) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1836 | continue; |
| 1837 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1838 | status = lock_buddy("refresh_client_subscriptions()", i, &lck, 0); |
| 1839 | if (status != PJ_SUCCESS) |
| 1840 | return status; |
| 1841 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1842 | if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1843 | subscribe_buddy_presence(i); |
| 1844 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1845 | } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1846 | unsubscribe_buddy_presence(i); |
| 1847 | |
| 1848 | } |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1849 | |
| 1850 | unlock_buddy(&lck); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1851 | } |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1852 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1853 | return PJ_SUCCESS; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1856 | /* Timer callback to re-create client subscription */ |
| 1857 | static void pres_timer_cb(pj_timer_heap_t *th, |
| 1858 | pj_timer_entry *entry) |
| 1859 | { |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1860 | unsigned i; |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1861 | pj_time_val delay = { PJSUA_PRES_TIMER, 0 }; |
| 1862 | |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1863 | /* Retry failed PUBLISH requests */ |
| 1864 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1865 | pjsua_acc *acc = &pjsua_var.acc[i]; |
| 1866 | if (acc->cfg.publish_enabled && acc->publish_sess==NULL) |
| 1867 | pjsua_pres_init_publish_acc(acc->index); |
| 1868 | } |
| 1869 | |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1870 | entry->id = PJ_FALSE; |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1871 | |
| 1872 | /* #937: No need to do bulk client refresh, as buddies have their |
| 1873 | * own individual timer now. |
| 1874 | */ |
| 1875 | //refresh_client_subscriptions(); |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1876 | |
| 1877 | pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, &delay); |
| 1878 | entry->id = PJ_TRUE; |
| 1879 | |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1880 | PJ_UNUSED_ARG(th); |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1883 | |
| 1884 | /* |
| 1885 | * Init presence |
| 1886 | */ |
| 1887 | pj_status_t pjsua_pres_init() |
| 1888 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1889 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1890 | pj_status_t status; |
| 1891 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1892 | status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1893 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1894 | pjsua_perror(THIS_FILE, "Unable to register pjsua presence module", |
| 1895 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1898 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 1899 | reset_buddy(i); |
| 1900 | } |
| 1901 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1902 | return status; |
| 1903 | } |
| 1904 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1905 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1906 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1907 | * Start presence subsystem. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1908 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1909 | pj_status_t pjsua_pres_start(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1910 | { |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1911 | /* Start presence timer to re-subscribe to buddy's presence when |
| 1912 | * subscription has failed. |
| 1913 | */ |
| 1914 | if (pjsua_var.pres_timer.id == PJ_FALSE) { |
| 1915 | pj_time_val pres_interval = {PJSUA_PRES_TIMER, 0}; |
| 1916 | |
| 1917 | pjsua_var.pres_timer.cb = &pres_timer_cb; |
| 1918 | pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.pres_timer, |
| 1919 | &pres_interval); |
Benny Prijono | 9727660 | 2007-06-23 01:07:08 +0000 | [diff] [blame] | 1920 | pjsua_var.pres_timer.id = PJ_TRUE; |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1923 | return PJ_SUCCESS; |
| 1924 | } |
| 1925 | |
| 1926 | |
| 1927 | /* |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1928 | * Shutdown presence. |
| 1929 | */ |
| 1930 | void pjsua_pres_shutdown(void) |
| 1931 | { |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1932 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1933 | |
Benny Prijono | 384dab4 | 2009-10-14 01:58:04 +0000 | [diff] [blame] | 1934 | PJ_LOG(4,(THIS_FILE, "Shutting down presence..")); |
| 1935 | |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1936 | if (pjsua_var.pres_timer.id != 0) { |
| 1937 | pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.pres_timer); |
| 1938 | pjsua_var.pres_timer.id = PJ_FALSE; |
| 1939 | } |
| 1940 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1941 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1942 | if (!pjsua_var.acc[i].valid) |
| 1943 | continue; |
| 1944 | pjsua_pres_delete_acc(i); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1947 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 1948 | pjsua_var.buddy[i].monitor = 0; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1949 | } |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1950 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1951 | refresh_client_subscriptions(); |
| 1952 | |
| 1953 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1954 | if (pjsua_var.acc[i].valid) |
| 1955 | pjsua_pres_update_acc(i, PJ_FALSE); |
| 1956 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1957 | } |