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 | |
Benny Prijono | 011e3f2 | 2009-12-10 05:16:23 +0000 | [diff] [blame] | 511 | /* Clear timer */ |
| 512 | if (pjsua_var.buddy[buddy_id].timer.id) { |
| 513 | pjsua_cancel_timer(&pjsua_var.buddy[buddy_id].timer); |
| 514 | pjsua_var.buddy[buddy_id].timer.id = PJ_FALSE; |
| 515 | } |
| 516 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 517 | /* Reset buddy struct */ |
| 518 | reset_buddy(buddy_id); |
| 519 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 520 | unlock_buddy(&lck); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 521 | return PJ_SUCCESS; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | /* |
| 526 | * Enable/disable buddy's presence monitoring. |
| 527 | */ |
| 528 | PJ_DEF(pj_status_t) pjsua_buddy_subscribe_pres( pjsua_buddy_id buddy_id, |
| 529 | pj_bool_t subscribe) |
| 530 | { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 531 | struct buddy_lock lck; |
| 532 | pj_status_t 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 | PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL); |
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 | status = lock_buddy("pjsua_buddy_subscribe_pres()", buddy_id, &lck, 0); |
| 537 | if (status != PJ_SUCCESS) |
| 538 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 539 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 540 | lck.buddy->monitor = subscribe; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 541 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 542 | pjsua_buddy_update_pres(buddy_id); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 543 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 544 | unlock_buddy(&lck); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 545 | return PJ_SUCCESS; |
| 546 | } |
| 547 | |
| 548 | |
| 549 | /* |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 550 | * Update buddy's presence. |
| 551 | */ |
| 552 | PJ_DEF(pj_status_t) pjsua_buddy_update_pres(pjsua_buddy_id buddy_id) |
| 553 | { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 554 | struct buddy_lock lck; |
| 555 | pj_status_t 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 | PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL); |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 558 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 559 | status = lock_buddy("pjsua_buddy_update_pres()", buddy_id, &lck, 0); |
| 560 | if (status != PJ_SUCCESS) |
| 561 | return status; |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 562 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 563 | /* Is this an unsubscribe request? */ |
| 564 | if (!lck.buddy->monitor) { |
| 565 | unsubscribe_buddy_presence(buddy_id); |
| 566 | unlock_buddy(&lck); |
| 567 | return PJ_SUCCESS; |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* Ignore if presence is already active for the buddy */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 571 | if (lck.buddy->sub) { |
| 572 | unlock_buddy(&lck); |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 573 | return PJ_SUCCESS; |
| 574 | } |
| 575 | |
| 576 | /* Initiate presence subscription */ |
| 577 | subscribe_buddy_presence(buddy_id); |
| 578 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 579 | unlock_buddy(&lck); |
Benny Prijono | 1086143 | 2007-10-31 10:54:53 +0000 | [diff] [blame] | 580 | |
| 581 | return PJ_SUCCESS; |
| 582 | } |
| 583 | |
| 584 | |
| 585 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 586 | * Dump presence subscriptions to log file. |
| 587 | */ |
| 588 | PJ_DEF(void) pjsua_pres_dump(pj_bool_t verbose) |
| 589 | { |
| 590 | unsigned acc_id; |
| 591 | unsigned i; |
| 592 | |
| 593 | |
| 594 | PJSUA_LOCK(); |
| 595 | |
| 596 | /* |
| 597 | * When no detail is required, just dump number of server and client |
| 598 | * subscriptions. |
| 599 | */ |
| 600 | if (verbose == PJ_FALSE) { |
| 601 | |
| 602 | int count = 0; |
| 603 | |
| 604 | for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) { |
| 605 | |
| 606 | if (!pjsua_var.acc[acc_id].valid) |
| 607 | continue; |
| 608 | |
| 609 | if (!pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) { |
| 610 | struct pjsua_srv_pres *uapres; |
| 611 | |
| 612 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
| 613 | while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) { |
| 614 | ++count; |
| 615 | uapres = uapres->next; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | PJ_LOG(3,(THIS_FILE, "Number of server/UAS subscriptions: %d", |
| 621 | count)); |
| 622 | |
| 623 | count = 0; |
| 624 | |
| 625 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 626 | if (pjsua_var.buddy[i].uri.slen == 0) |
| 627 | continue; |
| 628 | if (pjsua_var.buddy[i].sub) { |
| 629 | ++count; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | PJ_LOG(3,(THIS_FILE, "Number of client/UAC subscriptions: %d", |
| 634 | count)); |
| 635 | PJSUA_UNLOCK(); |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | |
| 640 | /* |
| 641 | * Dumping all server (UAS) subscriptions |
| 642 | */ |
| 643 | PJ_LOG(3,(THIS_FILE, "Dumping pjsua server subscriptions:")); |
| 644 | |
| 645 | for (acc_id=0; acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) { |
| 646 | |
| 647 | if (!pjsua_var.acc[acc_id].valid) |
| 648 | continue; |
| 649 | |
| 650 | PJ_LOG(3,(THIS_FILE, " %.*s", |
| 651 | (int)pjsua_var.acc[acc_id].cfg.id.slen, |
| 652 | pjsua_var.acc[acc_id].cfg.id.ptr)); |
| 653 | |
| 654 | if (pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) { |
| 655 | |
| 656 | PJ_LOG(3,(THIS_FILE, " - none - ")); |
| 657 | |
| 658 | } else { |
| 659 | struct pjsua_srv_pres *uapres; |
| 660 | |
| 661 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
| 662 | while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) { |
| 663 | |
| 664 | PJ_LOG(3,(THIS_FILE, " %10s %s", |
| 665 | pjsip_evsub_get_state_name(uapres->sub), |
| 666 | uapres->remote)); |
| 667 | |
| 668 | uapres = uapres->next; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Dumping all client (UAC) subscriptions |
| 675 | */ |
| 676 | PJ_LOG(3,(THIS_FILE, "Dumping pjsua client subscriptions:")); |
| 677 | |
| 678 | if (pjsua_var.buddy_cnt == 0) { |
| 679 | |
| 680 | PJ_LOG(3,(THIS_FILE, " - no buddy list - ")); |
| 681 | |
| 682 | } else { |
| 683 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 684 | |
| 685 | if (pjsua_var.buddy[i].uri.slen == 0) |
| 686 | continue; |
| 687 | |
| 688 | if (pjsua_var.buddy[i].sub) { |
| 689 | PJ_LOG(3,(THIS_FILE, " %10s %.*s", |
| 690 | pjsip_evsub_get_state_name(pjsua_var.buddy[i].sub), |
| 691 | (int)pjsua_var.buddy[i].uri.slen, |
| 692 | pjsua_var.buddy[i].uri.ptr)); |
| 693 | } else { |
| 694 | PJ_LOG(3,(THIS_FILE, " %10s %.*s", |
| 695 | "(null)", |
| 696 | (int)pjsua_var.buddy[i].uri.slen, |
| 697 | pjsua_var.buddy[i].uri.ptr)); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | PJSUA_UNLOCK(); |
| 703 | } |
| 704 | |
| 705 | |
| 706 | /*************************************************************************** |
| 707 | * Server subscription. |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 708 | */ |
| 709 | |
| 710 | /* Proto */ |
| 711 | static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata); |
| 712 | |
| 713 | /* The module instance. */ |
| 714 | static pjsip_module mod_pjsua_pres = |
| 715 | { |
| 716 | NULL, NULL, /* prev, next. */ |
| 717 | { "mod-pjsua-pres", 14 }, /* Name. */ |
| 718 | -1, /* Id */ |
| 719 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 720 | NULL, /* load() */ |
| 721 | NULL, /* start() */ |
| 722 | NULL, /* stop() */ |
| 723 | NULL, /* unload() */ |
| 724 | &pres_on_rx_request, /* on_rx_request() */ |
| 725 | NULL, /* on_rx_response() */ |
| 726 | NULL, /* on_tx_request. */ |
| 727 | NULL, /* on_tx_response() */ |
| 728 | NULL, /* on_tsx_state() */ |
| 729 | |
| 730 | }; |
| 731 | |
| 732 | |
| 733 | /* Callback called when *server* subscription state has changed. */ |
| 734 | static void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event) |
| 735 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 736 | pjsua_srv_pres *uapres; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 737 | |
| 738 | PJ_UNUSED_ARG(event); |
| 739 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 740 | PJSUA_LOCK(); |
| 741 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 742 | 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] | 743 | if (uapres) { |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 744 | pjsip_evsub_state state; |
| 745 | |
Benny Prijono | ba736c4 | 2008-07-10 20:45:03 +0000 | [diff] [blame] | 746 | PJ_LOG(4,(THIS_FILE, "Server subscription to %s is %s", |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 747 | uapres->remote, pjsip_evsub_get_state_name(sub))); |
| 748 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 749 | state = pjsip_evsub_get_state(sub); |
| 750 | |
| 751 | if (pjsua_var.ua_cfg.cb.on_srv_subscribe_state) { |
| 752 | pj_str_t from; |
| 753 | |
| 754 | from = uapres->dlg->remote.info_str; |
| 755 | (*pjsua_var.ua_cfg.cb.on_srv_subscribe_state)(uapres->acc_id, |
| 756 | uapres, &from, |
| 757 | state, event); |
| 758 | } |
| 759 | |
| 760 | if (state == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 761 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 762 | pj_list_erase(uapres); |
| 763 | } |
| 764 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 765 | |
| 766 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* This is called when request is received. |
| 770 | * We need to check for incoming SUBSCRIBE request. |
| 771 | */ |
| 772 | static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata) |
| 773 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 774 | int acc_id; |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 775 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 776 | pj_str_t contact; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 777 | pjsip_method *req_method = &rdata->msg_info.msg->line.req.method; |
| 778 | pjsua_srv_pres *uapres; |
| 779 | pjsip_evsub *sub; |
| 780 | pjsip_evsub_user pres_cb; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 781 | pjsip_dialog *dlg; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 782 | pjsip_status_code st_code; |
| 783 | pj_str_t reason; |
Benny Prijono | c61cc04 | 2007-06-27 13:01:59 +0000 | [diff] [blame] | 784 | pjsip_expires_hdr *expires_hdr; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 785 | pjsua_msg_data msg_data; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 786 | pj_status_t status; |
| 787 | |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 788 | if (pjsip_method_cmp(req_method, pjsip_get_subscribe_method()) != 0) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 789 | return PJ_FALSE; |
| 790 | |
| 791 | /* Incoming SUBSCRIBE: */ |
| 792 | |
Benny Prijono | 384dab4 | 2009-10-14 01:58:04 +0000 | [diff] [blame] | 793 | /* Don't want to accept the request if shutdown is in progress */ |
| 794 | if (pjsua_var.thread_quit_flag) { |
| 795 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
| 796 | PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL, |
| 797 | NULL, NULL); |
| 798 | return PJ_TRUE; |
| 799 | } |
| 800 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 801 | PJSUA_LOCK(); |
| 802 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 803 | /* Find which account for the incoming request. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 804 | acc_id = pjsua_acc_find_for_incoming(rdata); |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 805 | acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 806 | |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 807 | PJ_LOG(4,(THIS_FILE, "Creating server subscription, using account %d", |
| 808 | acc_id)); |
| 809 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 810 | /* Create suitable Contact header */ |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 811 | if (acc->contact.slen) { |
| 812 | contact = acc->contact; |
| 813 | } else { |
| 814 | status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact, |
| 815 | acc_id, rdata); |
| 816 | if (status != PJ_SUCCESS) { |
| 817 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", |
| 818 | status); |
| 819 | PJSUA_UNLOCK(); |
Benny Prijono | a330d45 | 2008-08-05 20:14:39 +0000 | [diff] [blame] | 820 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL, |
| 821 | NULL, NULL); |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 822 | return PJ_TRUE; |
| 823 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 826 | /* Create UAS dialog: */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 827 | status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 828 | &contact, &dlg); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 829 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 830 | pjsua_perror(THIS_FILE, |
| 831 | "Unable to create UAS dialog for subscription", |
| 832 | status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 833 | PJSUA_UNLOCK(); |
Benny Prijono | a330d45 | 2008-08-05 20:14:39 +0000 | [diff] [blame] | 834 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL, |
| 835 | NULL, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 836 | return PJ_TRUE; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 839 | /* Set credentials and preference. */ |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 840 | 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] | 841 | pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref); |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 842 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 843 | /* Init callback: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 844 | pj_bzero(&pres_cb, sizeof(pres_cb)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 845 | pres_cb.on_evsub_state = &pres_evsub_on_srv_state; |
| 846 | |
| 847 | /* Create server presence subscription: */ |
| 848 | status = pjsip_pres_create_uas( dlg, &pres_cb, rdata, &sub); |
| 849 | if (status != PJ_SUCCESS) { |
Benny Prijono | dbd9d4b | 2008-09-11 10:25:51 +0000 | [diff] [blame] | 850 | int code = PJSIP_ERRNO_TO_SIP_STATUS(status); |
| 851 | pjsip_tx_data *tdata; |
| 852 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 853 | pjsua_perror(THIS_FILE, "Unable to create server subscription", |
| 854 | status); |
Benny Prijono | dbd9d4b | 2008-09-11 10:25:51 +0000 | [diff] [blame] | 855 | |
| 856 | if (code==599 || code > 699 || code < 300) { |
| 857 | code = 400; |
| 858 | } |
| 859 | |
| 860 | status = pjsip_dlg_create_response(dlg, rdata, code, NULL, &tdata); |
| 861 | if (status == PJ_SUCCESS) { |
| 862 | status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), |
| 863 | tdata); |
| 864 | } |
| 865 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 866 | PJSUA_UNLOCK(); |
| 867 | return PJ_TRUE; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 870 | /* If account is locked to specific transport, then lock dialog |
| 871 | * to this transport too. |
| 872 | */ |
| 873 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 874 | pjsip_tpselector tp_sel; |
| 875 | |
| 876 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
| 877 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 878 | } |
| 879 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 880 | /* Attach our data to the subscription: */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 881 | uapres = PJ_POOL_ALLOC_T(dlg->pool, pjsua_srv_pres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 882 | uapres->sub = sub; |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 883 | uapres->remote = (char*) pj_pool_alloc(dlg->pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 884 | uapres->acc_id = acc_id; |
| 885 | uapres->dlg = dlg; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 886 | status = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, dlg->remote.info->uri, |
| 887 | uapres->remote, PJSIP_MAX_URL_SIZE); |
| 888 | if (status < 1) |
| 889 | pj_ansi_strcpy(uapres->remote, "<-- url is too long-->"); |
| 890 | else |
| 891 | uapres->remote[status] = '\0'; |
| 892 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 893 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, uapres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 894 | |
| 895 | /* Add server subscription to the list: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 896 | 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] | 897 | |
| 898 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 899 | /* Capture the value of Expires header. */ |
| 900 | expires_hdr = (pjsip_expires_hdr*) |
| 901 | pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, |
| 902 | NULL); |
| 903 | if (expires_hdr) |
| 904 | uapres->expires = expires_hdr->ivalue; |
| 905 | else |
| 906 | uapres->expires = -1; |
| 907 | |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 908 | st_code = (pjsip_status_code)200; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 909 | reason = pj_str("OK"); |
| 910 | pjsua_msg_data_init(&msg_data); |
| 911 | |
| 912 | /* Notify application callback, if any */ |
| 913 | if (pjsua_var.ua_cfg.cb.on_incoming_subscribe) { |
| 914 | pjsua_buddy_id buddy_id; |
| 915 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 916 | buddy_id = find_buddy(rdata->msg_info.from->uri); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 917 | |
| 918 | (*pjsua_var.ua_cfg.cb.on_incoming_subscribe)(acc_id, uapres, buddy_id, |
| 919 | &dlg->remote.info_str, |
| 920 | rdata, &st_code, &reason, |
| 921 | &msg_data); |
| 922 | } |
| 923 | |
| 924 | /* Handle rejection case */ |
| 925 | if (st_code >= 300) { |
| 926 | pjsip_tx_data *tdata; |
| 927 | |
| 928 | /* Create response */ |
| 929 | status = pjsip_dlg_create_response(dlg, rdata, st_code, |
| 930 | &reason, &tdata); |
| 931 | if (status != PJ_SUCCESS) { |
| 932 | pjsua_perror(THIS_FILE, "Error creating response", status); |
| 933 | pj_list_erase(uapres); |
| 934 | pjsip_pres_terminate(sub, PJ_FALSE); |
| 935 | PJSUA_UNLOCK(); |
| 936 | return PJ_FALSE; |
| 937 | } |
| 938 | |
| 939 | /* Add header list, if any */ |
| 940 | pjsua_process_msg_data(tdata, &msg_data); |
| 941 | |
| 942 | /* Send the response */ |
| 943 | status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), |
| 944 | tdata); |
| 945 | if (status != PJ_SUCCESS) { |
| 946 | pjsua_perror(THIS_FILE, "Error sending response", status); |
| 947 | /* This is not fatal */ |
| 948 | } |
| 949 | |
| 950 | /* Terminate presence subscription */ |
| 951 | pj_list_erase(uapres); |
| 952 | pjsip_pres_terminate(sub, PJ_FALSE); |
| 953 | PJSUA_UNLOCK(); |
| 954 | return PJ_TRUE; |
| 955 | } |
| 956 | |
| 957 | /* Create and send 2xx response to the SUBSCRIBE request: */ |
| 958 | status = pjsip_pres_accept(sub, rdata, st_code, &msg_data.hdr_list); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 959 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 960 | pjsua_perror(THIS_FILE, "Unable to accept presence subscription", |
| 961 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 962 | pj_list_erase(uapres); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 963 | pjsip_pres_terminate(sub, PJ_FALSE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 964 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 965 | return PJ_FALSE; |
| 966 | } |
| 967 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 968 | /* If code is 200, send NOTIFY now */ |
| 969 | if (st_code == 200) { |
| 970 | pjsua_pres_notify(acc_id, uapres, PJSIP_EVSUB_STATE_ACTIVE, |
| 971 | NULL, NULL, PJ_TRUE, &msg_data); |
| 972 | } |
| 973 | |
| 974 | /* Done: */ |
| 975 | |
| 976 | PJSUA_UNLOCK(); |
| 977 | |
| 978 | return PJ_TRUE; |
| 979 | } |
| 980 | |
| 981 | |
| 982 | /* |
| 983 | * Send NOTIFY. |
| 984 | */ |
| 985 | PJ_DEF(pj_status_t) pjsua_pres_notify( pjsua_acc_id acc_id, |
| 986 | pjsua_srv_pres *srv_pres, |
| 987 | pjsip_evsub_state ev_state, |
| 988 | const pj_str_t *state_str, |
| 989 | const pj_str_t *reason, |
| 990 | pj_bool_t with_body, |
| 991 | const pjsua_msg_data *msg_data) |
| 992 | { |
| 993 | pjsua_acc *acc; |
| 994 | pjsip_pres_status pres_status; |
| 995 | pjsua_buddy_id buddy_id; |
| 996 | pjsip_tx_data *tdata; |
| 997 | pj_status_t status; |
| 998 | |
| 999 | /* Check parameters */ |
| 1000 | PJ_ASSERT_RETURN(acc_id!=-1 && srv_pres, PJ_EINVAL); |
| 1001 | |
| 1002 | /* Check that account ID is valid */ |
| 1003 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 1004 | PJ_EINVAL); |
| 1005 | /* Check that account is valid */ |
| 1006 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 1007 | |
| 1008 | PJSUA_LOCK(); |
| 1009 | |
| 1010 | acc = &pjsua_var.acc[acc_id]; |
| 1011 | |
| 1012 | /* Check that the server presence subscription is still valid */ |
| 1013 | if (pj_list_find_node(&acc->pres_srv_list, srv_pres) == NULL) { |
| 1014 | /* Subscription has been terminated */ |
| 1015 | PJSUA_UNLOCK(); |
| 1016 | return PJ_EINVALIDOP; |
| 1017 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1018 | |
| 1019 | /* Set our online status: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1020 | pj_bzero(&pres_status, sizeof(pres_status)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1021 | pres_status.info_cnt = 1; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1022 | pres_status.info[0].basic_open = acc->online_status; |
| 1023 | pres_status.info[0].id = acc->cfg.pidf_tuple_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1024 | //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] | 1025 | //causing XML parsing to fail. |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1026 | //pres_status.info[0].contact = pjsua_var.local_uri; |
Benny Prijono | 7f6ee02 | 2008-07-31 08:32:46 +0000 | [diff] [blame] | 1027 | /* add RPID information */ |
| 1028 | pj_memcpy(&pres_status.info[0].rpid, &acc->rpid, |
| 1029 | sizeof(pjrpid_element)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1030 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1031 | pjsip_pres_set_status(srv_pres->sub, &pres_status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1032 | |
Benny Prijono | c61cc04 | 2007-06-27 13:01:59 +0000 | [diff] [blame] | 1033 | /* Check expires value. If it's zero, send our presense state but |
| 1034 | * set subscription state to TERMINATED. |
| 1035 | */ |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1036 | if (srv_pres->expires == 0) |
Benny Prijono | c61cc04 | 2007-06-27 13:01:59 +0000 | [diff] [blame] | 1037 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
Benny Prijono | c61cc04 | 2007-06-27 13:01:59 +0000 | [diff] [blame] | 1038 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1039 | /* Create and send the NOTIFY to active subscription: */ |
| 1040 | status = pjsip_pres_notify(srv_pres->sub, ev_state, state_str, |
| 1041 | reason, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1042 | if (status == PJ_SUCCESS) { |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1043 | /* Force removal of message body if msg_body==FALSE */ |
| 1044 | if (!with_body) { |
| 1045 | tdata->msg->body = NULL; |
| 1046 | } |
| 1047 | pjsua_process_msg_data(tdata, msg_data); |
| 1048 | status = pjsip_pres_send_request( srv_pres->sub, tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1049 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1050 | |
| 1051 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1052 | pjsua_perror(THIS_FILE, "Unable to create/send NOTIFY", |
| 1053 | status); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1054 | pj_list_erase(srv_pres); |
| 1055 | pjsip_pres_terminate(srv_pres->sub, PJ_FALSE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1056 | PJSUA_UNLOCK(); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1057 | return status; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | |
Benny Prijono | a17496a | 2007-10-31 10:20:31 +0000 | [diff] [blame] | 1061 | /* Subscribe to buddy's presence if we're not subscribed */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1062 | buddy_id = find_buddy(srv_pres->dlg->remote.info->uri); |
Benny Prijono | a17496a | 2007-10-31 10:20:31 +0000 | [diff] [blame] | 1063 | if (buddy_id != PJSUA_INVALID_ID) { |
| 1064 | pjsua_buddy *b = &pjsua_var.buddy[buddy_id]; |
| 1065 | if (b->monitor && b->sub == NULL) { |
| 1066 | PJ_LOG(4,(THIS_FILE, "Received SUBSCRIBE from buddy %d, " |
| 1067 | "activating outgoing subscription", buddy_id)); |
| 1068 | subscribe_buddy_presence(buddy_id); |
| 1069 | } |
| 1070 | } |
| 1071 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1072 | PJSUA_UNLOCK(); |
| 1073 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1074 | return PJ_SUCCESS; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1078 | /* |
| 1079 | * Client presence publication callback. |
| 1080 | */ |
| 1081 | static void publish_cb(struct pjsip_publishc_cbparam *param) |
| 1082 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1083 | pjsua_acc *acc = (pjsua_acc*) param->token; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1084 | |
| 1085 | if (param->code/100 != 2 || param->status != PJ_SUCCESS) { |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1086 | |
| 1087 | pjsip_publishc_destroy(param->pubc); |
| 1088 | acc->publish_sess = NULL; |
| 1089 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1090 | if (param->status != PJ_SUCCESS) { |
| 1091 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 1092 | |
| 1093 | pj_strerror(param->status, errmsg, sizeof(errmsg)); |
| 1094 | PJ_LOG(1,(THIS_FILE, |
| 1095 | "Client publication (PUBLISH) failed, status=%d, msg=%s", |
| 1096 | param->status, errmsg)); |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1097 | } else if (param->code == 412) { |
| 1098 | /* 412 (Conditional Request Failed) |
| 1099 | * The PUBLISH refresh has failed, retry with new one. |
| 1100 | */ |
| 1101 | pjsua_pres_init_publish_acc(acc->index); |
| 1102 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1103 | } else { |
| 1104 | PJ_LOG(1,(THIS_FILE, |
| 1105 | "Client publication (PUBLISH) failed (%d/%.*s)", |
| 1106 | param->code, (int)param->reason.slen, |
| 1107 | param->reason.ptr)); |
| 1108 | } |
| 1109 | |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1110 | } else { |
Benny Prijono | 534a9ba | 2009-10-13 14:01:59 +0000 | [diff] [blame] | 1111 | if (param->expiration < 1) { |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1112 | /* Could happen if server "forgot" to include Expires header |
| 1113 | * in the response. We will not renew, so destroy the pubc. |
| 1114 | */ |
| 1115 | pjsip_publishc_destroy(param->pubc); |
| 1116 | acc->publish_sess = NULL; |
| 1117 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | |
| 1122 | /* |
| 1123 | * Send PUBLISH request. |
| 1124 | */ |
| 1125 | static pj_status_t send_publish(int acc_id, pj_bool_t active) |
| 1126 | { |
| 1127 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
| 1128 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1129 | pjsip_pres_status pres_status; |
| 1130 | pjsip_tx_data *tdata; |
| 1131 | pj_status_t status; |
| 1132 | |
| 1133 | |
| 1134 | /* Create PUBLISH request */ |
| 1135 | if (active) { |
Benny Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 1136 | char *bpos; |
| 1137 | pj_str_t entity; |
| 1138 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1139 | status = pjsip_publishc_publish(acc->publish_sess, PJ_TRUE, &tdata); |
| 1140 | if (status != PJ_SUCCESS) { |
| 1141 | pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status); |
| 1142 | goto on_error; |
| 1143 | } |
| 1144 | |
| 1145 | /* Set our online status: */ |
| 1146 | pj_bzero(&pres_status, sizeof(pres_status)); |
| 1147 | pres_status.info_cnt = 1; |
| 1148 | pres_status.info[0].basic_open = acc->online_status; |
Benny Prijono | fe04fb5 | 2007-08-24 08:28:52 +0000 | [diff] [blame] | 1149 | pres_status.info[0].id = acc->cfg.pidf_tuple_id; |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1150 | /* .. including RPID information */ |
| 1151 | pj_memcpy(&pres_status.info[0].rpid, &acc->rpid, |
| 1152 | sizeof(pjrpid_element)); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1153 | |
Benny Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 1154 | /* Be careful not to send PIDF with presence entity ID containing |
| 1155 | * "<" character. |
| 1156 | */ |
| 1157 | if ((bpos=pj_strchr(&acc_cfg->id, '<')) != NULL) { |
| 1158 | char *epos = pj_strchr(&acc_cfg->id, '>'); |
| 1159 | if (epos - bpos < 2) { |
| 1160 | pj_assert(!"Unexpected invalid URI"); |
| 1161 | status = PJSIP_EINVALIDURI; |
| 1162 | goto on_error; |
| 1163 | } |
| 1164 | entity.ptr = bpos+1; |
| 1165 | entity.slen = epos - bpos - 1; |
| 1166 | } else { |
| 1167 | entity = acc_cfg->id; |
| 1168 | } |
| 1169 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1170 | /* Create and add PIDF message body */ |
| 1171 | status = pjsip_pres_create_pidf(tdata->pool, &pres_status, |
Benny Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 1172 | &entity, &tdata->msg->body); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1173 | if (status != PJ_SUCCESS) { |
| 1174 | pjsua_perror(THIS_FILE, "Error creating PIDF for PUBLISH request", |
| 1175 | status); |
| 1176 | pjsip_tx_data_dec_ref(tdata); |
| 1177 | goto on_error; |
| 1178 | } |
| 1179 | } else { |
| 1180 | status = pjsip_publishc_unpublish(acc->publish_sess, &tdata); |
| 1181 | if (status != PJ_SUCCESS) { |
| 1182 | pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status); |
| 1183 | goto on_error; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | /* Add headers etc */ |
| 1188 | pjsua_process_msg_data(tdata, NULL); |
| 1189 | |
| 1190 | /* Send the PUBLISH request */ |
| 1191 | status = pjsip_publishc_send(acc->publish_sess, tdata); |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 1192 | if (status == PJ_EPENDING) { |
| 1193 | PJ_LOG(3,(THIS_FILE, "Previous request is in progress, " |
| 1194 | "PUBLISH request is queued")); |
| 1195 | } else if (status != PJ_SUCCESS) { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1196 | pjsua_perror(THIS_FILE, "Error sending PUBLISH request", status); |
| 1197 | goto on_error; |
| 1198 | } |
| 1199 | |
| 1200 | acc->publish_state = acc->online_status; |
| 1201 | return PJ_SUCCESS; |
| 1202 | |
| 1203 | on_error: |
Benny Prijono | 2943815 | 2007-06-28 02:47:32 +0000 | [diff] [blame] | 1204 | if (acc->publish_sess) { |
| 1205 | pjsip_publishc_destroy(acc->publish_sess); |
| 1206 | acc->publish_sess = NULL; |
| 1207 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1208 | return status; |
| 1209 | } |
| 1210 | |
| 1211 | |
| 1212 | /* Create client publish session */ |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 1213 | pj_status_t pjsua_pres_init_publish_acc(int acc_id) |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1214 | { |
| 1215 | const pj_str_t STR_PRESENCE = { "presence", 8 }; |
| 1216 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
| 1217 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1218 | pj_status_t status; |
| 1219 | |
| 1220 | /* Create and init client publication session */ |
| 1221 | if (acc_cfg->publish_enabled) { |
| 1222 | |
| 1223 | /* Create client publication */ |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 1224 | status = pjsip_publishc_create(pjsua_var.endpt, &acc_cfg->publish_opt, |
| 1225 | acc, &publish_cb, |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1226 | &acc->publish_sess); |
| 1227 | if (status != PJ_SUCCESS) { |
| 1228 | acc->publish_sess = NULL; |
| 1229 | return status; |
| 1230 | } |
| 1231 | |
| 1232 | /* Initialize client publication */ |
| 1233 | status = pjsip_publishc_init(acc->publish_sess, &STR_PRESENCE, |
| 1234 | &acc_cfg->id, &acc_cfg->id, |
| 1235 | &acc_cfg->id, |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 1236 | PJSUA_PUBLISH_EXPIRATION); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1237 | if (status != PJ_SUCCESS) { |
| 1238 | acc->publish_sess = NULL; |
| 1239 | return status; |
| 1240 | } |
| 1241 | |
Benny Prijono | 703b7d7 | 2007-03-20 09:13:24 +0000 | [diff] [blame] | 1242 | /* Add credential for authentication */ |
Benny Prijono | 2943815 | 2007-06-28 02:47:32 +0000 | [diff] [blame] | 1243 | if (acc->cred_cnt) { |
| 1244 | pjsip_publishc_set_credentials(acc->publish_sess, acc->cred_cnt, |
| 1245 | acc->cred); |
| 1246 | } |
Benny Prijono | 703b7d7 | 2007-03-20 09:13:24 +0000 | [diff] [blame] | 1247 | |
| 1248 | /* Set route-set */ |
| 1249 | pjsip_publishc_set_route_set(acc->publish_sess, &acc->route_set); |
| 1250 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1251 | /* Send initial PUBLISH request */ |
| 1252 | if (acc->online_status != 0) { |
| 1253 | status = send_publish(acc_id, PJ_TRUE); |
| 1254 | if (status != PJ_SUCCESS) |
| 1255 | return status; |
| 1256 | } |
| 1257 | |
| 1258 | } else { |
| 1259 | acc->publish_sess = NULL; |
| 1260 | } |
| 1261 | |
| 1262 | return PJ_SUCCESS; |
| 1263 | } |
| 1264 | |
| 1265 | |
| 1266 | /* Init presence for account */ |
| 1267 | pj_status_t pjsua_pres_init_acc(int acc_id) |
| 1268 | { |
| 1269 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1270 | |
| 1271 | /* Init presence subscription */ |
| 1272 | pj_list_init(&acc->pres_srv_list); |
| 1273 | |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 1274 | return PJ_SUCCESS; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | |
Benny Prijono | 166d502 | 2010-02-10 14:24:48 +0000 | [diff] [blame] | 1278 | /* Unpublish presence publication */ |
| 1279 | void pjsua_pres_unpublish(pjsua_acc *acc) |
| 1280 | { |
| 1281 | if (acc->publish_sess) { |
| 1282 | pjsua_acc_config *acc_cfg = &acc->cfg; |
| 1283 | |
| 1284 | acc->online_status = PJ_FALSE; |
| 1285 | send_publish(acc->index, PJ_FALSE); |
| 1286 | /* By ticket #364, don't destroy the session yet (let the callback |
| 1287 | destroy it) |
| 1288 | if (acc->publish_sess) { |
| 1289 | pjsip_publishc_destroy(acc->publish_sess); |
| 1290 | acc->publish_sess = NULL; |
| 1291 | } |
| 1292 | */ |
| 1293 | acc_cfg->publish_enabled = PJ_FALSE; |
| 1294 | } |
| 1295 | } |
| 1296 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1297 | /* Terminate server subscription for the account */ |
| 1298 | void pjsua_pres_delete_acc(int acc_id) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1299 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1300 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1301 | pjsua_srv_pres *uapres; |
| 1302 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1303 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1304 | |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 1305 | /* Notify all subscribers that we're no longer available */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1306 | while (uapres != &acc->pres_srv_list) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1307 | |
| 1308 | pjsip_pres_status pres_status; |
| 1309 | pj_str_t reason = { "noresource", 10 }; |
Benny Prijono | 5516f91 | 2008-05-05 12:06:08 +0000 | [diff] [blame] | 1310 | pjsua_srv_pres *next; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1311 | pjsip_tx_data *tdata; |
| 1312 | |
Benny Prijono | 5516f91 | 2008-05-05 12:06:08 +0000 | [diff] [blame] | 1313 | next = uapres->next; |
| 1314 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1315 | pjsip_pres_get_status(uapres->sub, &pres_status); |
| 1316 | |
| 1317 | pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status; |
| 1318 | pjsip_pres_set_status(uapres->sub, &pres_status); |
| 1319 | |
| 1320 | if (pjsip_pres_notify(uapres->sub, |
| 1321 | PJSIP_EVSUB_STATE_TERMINATED, NULL, |
| 1322 | &reason, &tdata)==PJ_SUCCESS) |
| 1323 | { |
| 1324 | pjsip_pres_send_request(uapres->sub, tdata); |
| 1325 | } |
| 1326 | |
Benny Prijono | 5516f91 | 2008-05-05 12:06:08 +0000 | [diff] [blame] | 1327 | uapres = next; |
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 | |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 1330 | /* Clear server presence subscription list because account might be reused |
| 1331 | * later. */ |
| 1332 | pj_list_init(&acc->pres_srv_list); |
| 1333 | |
| 1334 | /* Terminate presence publication, if any */ |
Benny Prijono | 166d502 | 2010-02-10 14:24:48 +0000 | [diff] [blame] | 1335 | pjsua_pres_unpublish(acc); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1339 | /* Update server subscription (e.g. when our online status has changed) */ |
| 1340 | void pjsua_pres_update_acc(int acc_id, pj_bool_t force) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1341 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1342 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1343 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1344 | pjsua_srv_pres *uapres; |
| 1345 | |
| 1346 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
| 1347 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1348 | while (uapres != &acc->pres_srv_list) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1349 | |
| 1350 | pjsip_pres_status pres_status; |
| 1351 | pjsip_tx_data *tdata; |
| 1352 | |
| 1353 | pjsip_pres_get_status(uapres->sub, &pres_status); |
Benny Prijono | 232759b | 2008-09-08 12:46:29 +0000 | [diff] [blame] | 1354 | |
| 1355 | /* Only send NOTIFY once subscription is active. Some subscriptions |
| 1356 | * may still be in NULL (when app is adding a new buddy while in the |
| 1357 | * on_incoming_subscribe() callback) or PENDING (when user approval is |
| 1358 | * being requested) state and we don't send NOTIFY to these subs until |
| 1359 | * the user accepted the request. |
| 1360 | */ |
| 1361 | if (pjsip_evsub_get_state(uapres->sub)==PJSIP_EVSUB_STATE_ACTIVE && |
| 1362 | (force || pres_status.info[0].basic_open != acc->online_status)) |
| 1363 | { |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1364 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1365 | pres_status.info[0].basic_open = acc->online_status; |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1366 | pj_memcpy(&pres_status.info[0].rpid, &acc->rpid, |
| 1367 | sizeof(pjrpid_element)); |
| 1368 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1369 | pjsip_pres_set_status(uapres->sub, &pres_status); |
| 1370 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1371 | if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) { |
| 1372 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1373 | pjsip_pres_send_request(uapres->sub, tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1374 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | uapres = uapres->next; |
| 1378 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1379 | |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 1380 | /* Send PUBLISH if required. We only do this when we have a PUBLISH |
| 1381 | * session. If we don't have a PUBLISH session, then it could be |
| 1382 | * that we're waiting until registration has completed before we |
| 1383 | * send the first PUBLISH. |
| 1384 | */ |
| 1385 | if (acc_cfg->publish_enabled && acc->publish_sess) { |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1386 | if (force || acc->publish_state != acc->online_status) { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 1387 | send_publish(acc_id, PJ_TRUE); |
| 1388 | } |
| 1389 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | |
| 1393 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1394 | /*************************************************************************** |
| 1395 | * Client subscription. |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1396 | */ |
| 1397 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1398 | static void buddy_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry) |
| 1399 | { |
| 1400 | pjsua_buddy *buddy = (pjsua_buddy*)entry->user_data; |
| 1401 | |
| 1402 | PJ_UNUSED_ARG(th); |
| 1403 | |
| 1404 | entry->id = PJ_FALSE; |
| 1405 | pjsua_buddy_update_pres(buddy->index); |
| 1406 | } |
| 1407 | |
| 1408 | /* Reschedule subscription refresh timer or terminate the subscription |
| 1409 | * refresh timer for the specified buddy. |
| 1410 | */ |
| 1411 | static void buddy_resubscribe(pjsua_buddy *buddy, pj_bool_t resched, |
| 1412 | unsigned msec_interval) |
| 1413 | { |
| 1414 | if (buddy->timer.id) { |
| 1415 | pjsua_cancel_timer(&buddy->timer); |
| 1416 | buddy->timer.id = PJ_FALSE; |
| 1417 | } |
| 1418 | |
| 1419 | if (resched) { |
| 1420 | pj_time_val delay; |
| 1421 | |
Benny Prijono | 12c01a9 | 2009-10-21 02:37:52 +0000 | [diff] [blame] | 1422 | PJ_LOG(4,(THIS_FILE, |
| 1423 | "Resubscribing buddy id %u in %u ms (reason: %.*s)", |
| 1424 | buddy->index, msec_interval, |
| 1425 | (int)buddy->term_reason.slen, |
| 1426 | buddy->term_reason.ptr)); |
| 1427 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1428 | pj_timer_entry_init(&buddy->timer, 0, buddy, &buddy_timer_cb); |
| 1429 | delay.sec = 0; |
| 1430 | delay.msec = msec_interval; |
| 1431 | pj_time_val_normalize(&delay); |
| 1432 | |
| 1433 | if (pjsua_schedule_timer(&buddy->timer, &delay)==PJ_SUCCESS) |
| 1434 | buddy->timer.id = PJ_TRUE; |
| 1435 | } |
| 1436 | } |
| 1437 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1438 | /* Callback called when *client* subscription state has changed. */ |
| 1439 | static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event) |
| 1440 | { |
| 1441 | pjsua_buddy *buddy; |
| 1442 | |
| 1443 | PJ_UNUSED_ARG(event); |
| 1444 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1445 | /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has |
| 1446 | * a dialog attached to it, lock_buddy() will use the dialog |
| 1447 | * lock, which we are currently holding! |
| 1448 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1449 | 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] | 1450 | if (buddy) { |
Benny Prijono | ba736c4 | 2008-07-10 20:45:03 +0000 | [diff] [blame] | 1451 | PJ_LOG(4,(THIS_FILE, |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1452 | "Presence subscription to %.*s is %s", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1453 | (int)pjsua_var.buddy[buddy->index].uri.slen, |
| 1454 | pjsua_var.buddy[buddy->index].uri.ptr, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1455 | pjsip_evsub_get_state_name(sub))); |
| 1456 | |
| 1457 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1458 | int resub_delay = -1; |
| 1459 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1460 | if (buddy->term_reason.ptr == NULL) { |
| 1461 | buddy->term_reason.ptr = (char*) |
| 1462 | pj_pool_alloc(buddy->pool, |
| 1463 | PJSUA_BUDDY_SUB_TERM_REASON_LEN); |
| 1464 | } |
| 1465 | pj_strncpy(&buddy->term_reason, |
| 1466 | pjsip_evsub_get_termination_reason(sub), |
| 1467 | PJSUA_BUDDY_SUB_TERM_REASON_LEN); |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1468 | |
| 1469 | buddy->term_code = 200; |
| 1470 | |
| 1471 | /* Determine whether to resubscribe automatically */ |
Benny Prijono | d3d18c3 | 2009-11-10 10:54:11 +0000 | [diff] [blame] | 1472 | if (event && event->type==PJSIP_EVENT_TSX_STATE) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1473 | const pjsip_transaction *tsx = event->body.tsx_state.tsx; |
| 1474 | if (pjsip_method_cmp(&tsx->method, |
| 1475 | &pjsip_subscribe_method)==0) |
| 1476 | { |
| 1477 | buddy->term_code = tsx->status_code; |
| 1478 | switch (tsx->status_code) { |
| 1479 | case PJSIP_SC_CALL_TSX_DOES_NOT_EXIST: |
| 1480 | /* 481: we refreshed too late? resubscribe |
| 1481 | * immediately. |
| 1482 | */ |
Benny Prijono | 6ab0532 | 2009-10-21 03:03:06 +0000 | [diff] [blame] | 1483 | /* But this must only happen when the 481 is received |
| 1484 | * on subscription refresh request. We MUST NOT try to |
| 1485 | * resubscribe automatically if the 481 is received |
| 1486 | * on the initial SUBSCRIBE (if server returns this |
| 1487 | * response for some reason). |
| 1488 | */ |
| 1489 | if (buddy->dlg->remote.contact) |
| 1490 | resub_delay = 500; |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1491 | break; |
| 1492 | } |
| 1493 | } else if (pjsip_method_cmp(&tsx->method, |
| 1494 | &pjsip_notify_method)==0) |
| 1495 | { |
| 1496 | if (pj_stricmp2(&buddy->term_reason, "deactivated")==0 || |
| 1497 | pj_stricmp2(&buddy->term_reason, "timeout")==0) { |
| 1498 | /* deactivated: The subscription has been terminated, |
| 1499 | * but the subscriber SHOULD retry immediately with |
| 1500 | * a new subscription. |
| 1501 | */ |
| 1502 | /* timeout: The subscription has been terminated |
| 1503 | * because it was not refreshed before it expired. |
| 1504 | * Clients MAY re-subscribe immediately. The |
| 1505 | * "retry-after" parameter has no semantics for |
| 1506 | * "timeout". |
| 1507 | */ |
| 1508 | resub_delay = 500; |
| 1509 | } |
| 1510 | else if (pj_stricmp2(&buddy->term_reason, "probation")==0|| |
| 1511 | pj_stricmp2(&buddy->term_reason, "giveup")==0) { |
| 1512 | /* probation: The subscription has been terminated, |
| 1513 | * but the client SHOULD retry at some later time. |
| 1514 | * If a "retry-after" parameter is also present, the |
| 1515 | * client SHOULD wait at least the number of seconds |
| 1516 | * specified by that parameter before attempting to re- |
| 1517 | * subscribe. |
| 1518 | */ |
| 1519 | /* giveup: The subscription has been terminated because |
| 1520 | * the notifier could not obtain authorization in a |
| 1521 | * timely fashion. If a "retry-after" parameter is |
| 1522 | * also present, the client SHOULD wait at least the |
| 1523 | * number of seconds specified by that parameter before |
| 1524 | * attempting to re-subscribe; otherwise, the client |
| 1525 | * MAY retry immediately, but will likely get put back |
| 1526 | * into pending state. |
| 1527 | */ |
| 1528 | const pjsip_sub_state_hdr *sub_hdr; |
| 1529 | pj_str_t sub_state = { "Subscription-State", 18 }; |
| 1530 | const pjsip_msg *msg; |
| 1531 | |
| 1532 | msg = event->body.tsx_state.src.rdata->msg_info.msg; |
| 1533 | sub_hdr = (const pjsip_sub_state_hdr*) |
| 1534 | pjsip_msg_find_hdr_by_name(msg, &sub_state, |
| 1535 | NULL); |
| 1536 | if (sub_hdr && sub_hdr->retry_after > 0) |
| 1537 | resub_delay = sub_hdr->retry_after * 1000; |
| 1538 | } |
| 1539 | |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | /* For other cases of subscription termination, if resubscribe |
| 1544 | * timer is not set, schedule with default expiration (plus minus |
| 1545 | * some random value, to avoid sending SUBSCRIBEs all at once) |
| 1546 | */ |
| 1547 | if (resub_delay == -1) { |
| 1548 | pj_assert(PJSUA_PRES_TIMER >= 3); |
| 1549 | resub_delay = PJSUA_PRES_TIMER*1000 - 2500 + (pj_rand()%5000); |
| 1550 | } |
| 1551 | |
| 1552 | buddy_resubscribe(buddy, PJ_TRUE, resub_delay); |
Benny Prijono | 6ab0532 | 2009-10-21 03:03:06 +0000 | [diff] [blame] | 1553 | |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1554 | } else { |
Benny Prijono | 6ab0532 | 2009-10-21 03:03:06 +0000 | [diff] [blame] | 1555 | /* This will clear the last termination code/reason */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1556 | buddy->term_code = 0; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1557 | buddy->term_reason.slen = 0; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1558 | } |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1559 | |
| 1560 | /* Call callback */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1561 | if (pjsua_var.ua_cfg.cb.on_buddy_state) |
| 1562 | (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index); |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1563 | |
| 1564 | /* Clear subscription */ |
| 1565 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 1566 | buddy->sub = NULL; |
| 1567 | buddy->status.info_cnt = 0; |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1568 | buddy->dlg = NULL; |
Benny Prijono | 63fba01 | 2008-07-17 14:19:10 +0000 | [diff] [blame] | 1569 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 1570 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1571 | } |
| 1572 | } |
| 1573 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1574 | |
| 1575 | /* Callback when transaction state has changed. */ |
| 1576 | static void pjsua_evsub_on_tsx_state(pjsip_evsub *sub, |
| 1577 | pjsip_transaction *tsx, |
| 1578 | pjsip_event *event) |
| 1579 | { |
| 1580 | pjsua_buddy *buddy; |
| 1581 | pjsip_contact_hdr *contact_hdr; |
| 1582 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1583 | /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has |
| 1584 | * a dialog attached to it, lock_buddy() will use the dialog |
| 1585 | * lock, which we are currently holding! |
| 1586 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1587 | 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] | 1588 | if (!buddy) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1589 | return; |
| 1590 | } |
| 1591 | |
| 1592 | /* We only use this to update buddy's Contact, when it's not |
| 1593 | * set. |
| 1594 | */ |
| 1595 | if (buddy->contact.slen != 0) { |
| 1596 | /* Contact already set */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1597 | return; |
| 1598 | } |
| 1599 | |
| 1600 | /* Only care about 2xx response to outgoing SUBSCRIBE */ |
| 1601 | if (tsx->status_code/100 != 2 || |
| 1602 | tsx->role != PJSIP_UAC_ROLE || |
| 1603 | event->type != PJSIP_EVENT_RX_MSG || |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 1604 | pjsip_method_cmp(&tsx->method, pjsip_get_subscribe_method())!=0) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1605 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1606 | return; |
| 1607 | } |
| 1608 | |
| 1609 | /* Find contact header. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1610 | contact_hdr = (pjsip_contact_hdr*) |
| 1611 | pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1612 | PJSIP_H_CONTACT, NULL); |
Benny Prijono | 8b33bba | 2010-06-02 03:03:43 +0000 | [diff] [blame] | 1613 | if (!contact_hdr || !contact_hdr->uri) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1614 | return; |
| 1615 | } |
| 1616 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1617 | buddy->contact.ptr = (char*) |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1618 | pj_pool_alloc(buddy->pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1619 | buddy->contact.slen = pjsip_uri_print( PJSIP_URI_IN_CONTACT_HDR, |
| 1620 | contact_hdr->uri, |
| 1621 | buddy->contact.ptr, |
| 1622 | PJSIP_MAX_URL_SIZE); |
| 1623 | if (buddy->contact.slen < 0) |
| 1624 | buddy->contact.slen = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1628 | /* Callback called when we receive NOTIFY */ |
| 1629 | static void pjsua_evsub_on_rx_notify(pjsip_evsub *sub, |
| 1630 | pjsip_rx_data *rdata, |
| 1631 | int *p_st_code, |
| 1632 | pj_str_t **p_st_text, |
| 1633 | pjsip_hdr *res_hdr, |
| 1634 | pjsip_msg_body **p_body) |
| 1635 | { |
| 1636 | pjsua_buddy *buddy; |
| 1637 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1638 | /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has |
| 1639 | * a dialog attached to it, lock_buddy() will use the dialog |
| 1640 | * lock, which we are currently holding! |
| 1641 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1642 | 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] | 1643 | if (buddy) { |
| 1644 | /* Update our info. */ |
| 1645 | pjsip_pres_get_status(sub, &buddy->status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /* The default is to send 200 response to NOTIFY. |
| 1649 | * Just leave it there.. |
| 1650 | */ |
| 1651 | PJ_UNUSED_ARG(rdata); |
| 1652 | PJ_UNUSED_ARG(p_st_code); |
| 1653 | PJ_UNUSED_ARG(p_st_text); |
| 1654 | PJ_UNUSED_ARG(res_hdr); |
| 1655 | PJ_UNUSED_ARG(p_body); |
| 1656 | } |
| 1657 | |
| 1658 | |
| 1659 | /* Event subscription callback. */ |
| 1660 | static pjsip_evsub_user pres_callback = |
| 1661 | { |
| 1662 | &pjsua_evsub_on_state, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1663 | &pjsua_evsub_on_tsx_state, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1664 | |
| 1665 | NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless |
| 1666 | * we want to authenticate |
| 1667 | */ |
| 1668 | |
| 1669 | &pjsua_evsub_on_rx_notify, |
| 1670 | |
| 1671 | NULL, /* on_client_refresh: Use default behaviour, which is to |
| 1672 | * refresh client subscription. */ |
| 1673 | |
| 1674 | NULL, /* on_server_timeout: Use default behaviour, which is to send |
| 1675 | * NOTIFY to terminate. |
| 1676 | */ |
| 1677 | }; |
| 1678 | |
| 1679 | |
| 1680 | /* It does what it says.. */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1681 | static void subscribe_buddy_presence(pjsua_buddy_id buddy_id) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1682 | { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1683 | pj_pool_t *tmp_pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1684 | pjsua_buddy *buddy; |
| 1685 | int acc_id; |
| 1686 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1687 | pj_str_t contact; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1688 | pjsip_tx_data *tdata; |
| 1689 | pj_status_t status; |
| 1690 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1691 | buddy = &pjsua_var.buddy[buddy_id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1692 | acc_id = pjsua_acc_find_for_outgoing(&buddy->uri); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1693 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1694 | acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1695 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1696 | PJ_LOG(4,(THIS_FILE, "Using account %d for buddy %d subscription", |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1697 | acc_id, buddy_id)); |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1698 | |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1699 | /* Generate suitable Contact header unless one is already set in |
| 1700 | * the account |
| 1701 | */ |
| 1702 | if (acc->contact.slen) { |
| 1703 | contact = acc->contact; |
| 1704 | } else { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1705 | tmp_pool = pjsua_pool_create("tmpbuddy", 512, 256); |
| 1706 | |
| 1707 | status = pjsua_acc_create_uac_contact(tmp_pool, &contact, |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1708 | acc_id, &buddy->uri); |
| 1709 | if (status != PJ_SUCCESS) { |
| 1710 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", |
| 1711 | status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1712 | pj_pool_release(tmp_pool); |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1713 | return; |
| 1714 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1717 | /* Create UAC dialog */ |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1718 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1719 | &acc->cfg.id, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1720 | &contact, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1721 | &buddy->uri, |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1722 | NULL, &buddy->dlg); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1723 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1724 | pjsua_perror(THIS_FILE, "Unable to create dialog", |
| 1725 | status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1726 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1727 | return; |
| 1728 | } |
| 1729 | |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 1730 | /* Increment the dialog's lock otherwise when presence session creation |
| 1731 | * fails the dialog will be destroyed prematurely. |
| 1732 | */ |
| 1733 | pjsip_dlg_inc_lock(buddy->dlg); |
| 1734 | |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1735 | status = pjsip_pres_create_uac( buddy->dlg, &pres_callback, |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1736 | PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub); |
| 1737 | if (status != PJ_SUCCESS) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1738 | buddy->sub = NULL; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1739 | pjsua_perror(THIS_FILE, "Unable to create presence client", |
| 1740 | status); |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 1741 | /* This should destroy the dialog since there's no session |
| 1742 | * referencing it |
| 1743 | */ |
Benny Prijono | 011e3f2 | 2009-12-10 05:16:23 +0000 | [diff] [blame] | 1744 | if (buddy->dlg) pjsip_dlg_dec_lock(buddy->dlg); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1745 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1746 | return; |
| 1747 | } |
| 1748 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1749 | /* If account is locked to specific transport, then lock dialog |
| 1750 | * to this transport too. |
| 1751 | */ |
| 1752 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 1753 | pjsip_tpselector tp_sel; |
| 1754 | |
| 1755 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1756 | pjsip_dlg_set_transport(buddy->dlg, &tp_sel); |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1759 | /* Set route-set */ |
| 1760 | if (!pj_list_empty(&acc->route_set)) { |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1761 | pjsip_dlg_set_route_set(buddy->dlg, &acc->route_set); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | /* Set credentials */ |
| 1765 | if (acc->cred_cnt) { |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1766 | pjsip_auth_clt_set_credentials( &buddy->dlg->auth_sess, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1767 | acc->cred_cnt, acc->cred); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1768 | } |
Benny Prijono | 1d8d608 | 2006-04-29 12:38:25 +0000 | [diff] [blame] | 1769 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 1770 | /* Set authentication preference */ |
| 1771 | pjsip_auth_clt_set_prefs(&buddy->dlg->auth_sess, &acc->cfg.auth_pref); |
| 1772 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1773 | pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1774 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1775 | status = pjsip_pres_initiate(buddy->sub, -1, &tdata); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1776 | if (status != PJ_SUCCESS) { |
Benny Prijono | 011e3f2 | 2009-12-10 05:16:23 +0000 | [diff] [blame] | 1777 | if (buddy->dlg) pjsip_dlg_dec_lock(buddy->dlg); |
Benny Prijono | a6992c5 | 2007-06-05 22:58:32 +0000 | [diff] [blame] | 1778 | if (buddy->sub) { |
| 1779 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1780 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1781 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1782 | pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE", |
| 1783 | status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1784 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1785 | return; |
| 1786 | } |
| 1787 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1788 | pjsua_process_msg_data(tdata, NULL); |
| 1789 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1790 | status = pjsip_pres_send_request(buddy->sub, tdata); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1791 | if (status != PJ_SUCCESS) { |
Benny Prijono | 011e3f2 | 2009-12-10 05:16:23 +0000 | [diff] [blame] | 1792 | if (buddy->dlg) pjsip_dlg_dec_lock(buddy->dlg); |
Benny Prijono | a6992c5 | 2007-06-05 22:58:32 +0000 | [diff] [blame] | 1793 | if (buddy->sub) { |
| 1794 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1795 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1796 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1797 | pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE", |
| 1798 | status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1799 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1800 | return; |
| 1801 | } |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1802 | |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 1803 | pjsip_dlg_dec_lock(buddy->dlg); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1804 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | |
| 1808 | /* It does what it says... */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1809 | static void unsubscribe_buddy_presence(pjsua_buddy_id buddy_id) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1810 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1811 | pjsua_buddy *buddy; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1812 | pjsip_tx_data *tdata; |
| 1813 | pj_status_t status; |
| 1814 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1815 | buddy = &pjsua_var.buddy[buddy_id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1816 | |
| 1817 | if (buddy->sub == NULL) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1818 | return; |
| 1819 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1820 | if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1821 | buddy->sub = NULL; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1822 | return; |
| 1823 | } |
| 1824 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1825 | status = pjsip_pres_initiate( buddy->sub, 0, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1826 | if (status == PJ_SUCCESS) { |
| 1827 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1828 | status = pjsip_pres_send_request( buddy->sub, tdata ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1829 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1830 | |
Benny Prijono | 48da92e | 2007-05-16 08:56:30 +0000 | [diff] [blame] | 1831 | if (status != PJ_SUCCESS && buddy->sub) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1832 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1833 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1834 | pjsua_perror(THIS_FILE, "Unable to unsubscribe presence", |
| 1835 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1839 | /* It does what it says.. */ |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1840 | static pj_status_t refresh_client_subscriptions(void) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1841 | { |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1842 | unsigned i; |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1843 | pj_status_t status; |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1844 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1845 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1846 | struct buddy_lock lck; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1847 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1848 | if (!pjsua_buddy_is_valid(i)) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1849 | continue; |
| 1850 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1851 | status = lock_buddy("refresh_client_subscriptions()", i, &lck, 0); |
| 1852 | if (status != PJ_SUCCESS) |
| 1853 | return status; |
| 1854 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1855 | if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1856 | subscribe_buddy_presence(i); |
| 1857 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1858 | } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1859 | unsubscribe_buddy_presence(i); |
| 1860 | |
| 1861 | } |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1862 | |
| 1863 | unlock_buddy(&lck); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1864 | } |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 1865 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 1866 | return PJ_SUCCESS; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 1869 | /*************************************************************************** |
| 1870 | * MWI |
| 1871 | */ |
| 1872 | /* Callback called when *client* subscription state has changed. */ |
| 1873 | static void mwi_evsub_on_state( pjsip_evsub *sub, pjsip_event *event) |
| 1874 | { |
| 1875 | pjsua_acc *acc; |
| 1876 | |
| 1877 | PJ_UNUSED_ARG(event); |
| 1878 | |
| 1879 | /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has |
| 1880 | * a dialog attached to it, lock_buddy() will use the dialog |
| 1881 | * lock, which we are currently holding! |
| 1882 | */ |
| 1883 | acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
| 1884 | if (!acc) |
| 1885 | return; |
| 1886 | |
| 1887 | PJ_LOG(4,(THIS_FILE, |
| 1888 | "MWI subscription for %.*s is %s", |
| 1889 | (int)acc->cfg.id.slen, acc->cfg.id.ptr, |
| 1890 | pjsip_evsub_get_state_name(sub))); |
| 1891 | |
| 1892 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 1893 | /* Clear subscription */ |
| 1894 | acc->mwi_dlg = NULL; |
| 1895 | acc->mwi_sub = NULL; |
| 1896 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 1897 | |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | /* Callback called when we receive NOTIFY */ |
| 1902 | static void mwi_evsub_on_rx_notify(pjsip_evsub *sub, |
| 1903 | pjsip_rx_data *rdata, |
| 1904 | int *p_st_code, |
| 1905 | pj_str_t **p_st_text, |
| 1906 | pjsip_hdr *res_hdr, |
| 1907 | pjsip_msg_body **p_body) |
| 1908 | { |
| 1909 | pjsua_mwi_info mwi_info; |
| 1910 | pjsua_acc *acc; |
| 1911 | |
| 1912 | PJ_UNUSED_ARG(p_st_code); |
| 1913 | PJ_UNUSED_ARG(p_st_text); |
| 1914 | PJ_UNUSED_ARG(res_hdr); |
| 1915 | PJ_UNUSED_ARG(p_body); |
| 1916 | |
| 1917 | acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
| 1918 | if (!acc) |
| 1919 | return; |
| 1920 | |
| 1921 | /* Construct mwi_info */ |
| 1922 | pj_bzero(&mwi_info, sizeof(mwi_info)); |
| 1923 | mwi_info.evsub = sub; |
| 1924 | mwi_info.rdata = rdata; |
| 1925 | |
| 1926 | /* Call callback */ |
| 1927 | if (pjsua_var.ua_cfg.cb.on_mwi_info) { |
| 1928 | (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc->index, &mwi_info); |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | |
| 1933 | /* Event subscription callback. */ |
| 1934 | static pjsip_evsub_user mwi_cb = |
| 1935 | { |
| 1936 | &mwi_evsub_on_state, |
| 1937 | NULL, /* on_tsx_state: not interested */ |
| 1938 | NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless |
| 1939 | * we want to authenticate |
| 1940 | */ |
| 1941 | |
| 1942 | &mwi_evsub_on_rx_notify, |
| 1943 | |
| 1944 | NULL, /* on_client_refresh: Use default behaviour, which is to |
| 1945 | * refresh client subscription. */ |
| 1946 | |
| 1947 | NULL, /* on_server_timeout: Use default behaviour, which is to send |
| 1948 | * NOTIFY to terminate. |
| 1949 | */ |
| 1950 | }; |
| 1951 | |
| 1952 | void pjsua_start_mwi(pjsua_acc *acc) |
| 1953 | { |
| 1954 | pj_pool_t *tmp_pool = NULL; |
| 1955 | pj_str_t contact; |
| 1956 | pjsip_tx_data *tdata; |
| 1957 | pj_status_t status; |
| 1958 | |
| 1959 | if (!acc->cfg.mwi_enabled) { |
| 1960 | if (acc->mwi_sub) { |
| 1961 | /* Terminate MWI subscription */ |
| 1962 | pjsip_tx_data *tdata; |
| 1963 | pjsip_evsub *sub = acc->mwi_sub; |
| 1964 | |
| 1965 | /* Detach sub from this account */ |
| 1966 | acc->mwi_sub = NULL; |
| 1967 | acc->mwi_dlg = NULL; |
| 1968 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 1969 | |
| 1970 | /* Unsubscribe */ |
| 1971 | status = pjsip_mwi_initiate(acc->mwi_sub, 0, &tdata); |
| 1972 | if (status == PJ_SUCCESS) { |
| 1973 | status = pjsip_mwi_send_request(acc->mwi_sub, tdata); |
| 1974 | } |
| 1975 | } |
| 1976 | return; |
| 1977 | } |
| 1978 | |
| 1979 | if (acc->mwi_sub) { |
| 1980 | /* Subscription is already active */ |
| 1981 | return; |
| 1982 | |
| 1983 | } |
| 1984 | |
| 1985 | /* Generate suitable Contact header unless one is already set in |
| 1986 | * the account |
| 1987 | */ |
| 1988 | if (acc->contact.slen) { |
| 1989 | contact = acc->contact; |
| 1990 | } else { |
| 1991 | tmp_pool = pjsua_pool_create("tmpmwi", 512, 256); |
| 1992 | status = pjsua_acc_create_uac_contact(tmp_pool, &contact, |
| 1993 | acc->index, &acc->cfg.id); |
| 1994 | if (status != PJ_SUCCESS) { |
| 1995 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", |
| 1996 | status); |
| 1997 | pj_pool_release(tmp_pool); |
| 1998 | return; |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | /* Create UAC dialog */ |
| 2003 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
| 2004 | &acc->cfg.id, |
| 2005 | &contact, |
| 2006 | &acc->cfg.id, |
| 2007 | NULL, &acc->mwi_dlg); |
| 2008 | if (status != PJ_SUCCESS) { |
| 2009 | pjsua_perror(THIS_FILE, "Unable to create dialog", status); |
| 2010 | if (tmp_pool) pj_pool_release(tmp_pool); |
| 2011 | return; |
| 2012 | } |
| 2013 | |
| 2014 | /* Increment the dialog's lock otherwise when presence session creation |
| 2015 | * fails the dialog will be destroyed prematurely. |
| 2016 | */ |
| 2017 | pjsip_dlg_inc_lock(acc->mwi_dlg); |
| 2018 | |
| 2019 | /* Create UAC subscription */ |
| 2020 | status = pjsip_mwi_create_uac(acc->mwi_dlg, &mwi_cb, |
| 2021 | PJSIP_EVSUB_NO_EVENT_ID, &acc->mwi_sub); |
| 2022 | if (status != PJ_SUCCESS) { |
| 2023 | pjsua_perror(THIS_FILE, "Error creating MWI subscription", status); |
| 2024 | if (tmp_pool) pj_pool_release(tmp_pool); |
Benny Prijono | 011e3f2 | 2009-12-10 05:16:23 +0000 | [diff] [blame] | 2025 | if (acc->mwi_dlg) pjsip_dlg_dec_lock(acc->mwi_dlg); |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 2026 | return; |
| 2027 | } |
| 2028 | |
| 2029 | /* If account is locked to specific transport, then lock dialog |
| 2030 | * to this transport too. |
| 2031 | */ |
| 2032 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 2033 | pjsip_tpselector tp_sel; |
| 2034 | |
| 2035 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
| 2036 | pjsip_dlg_set_transport(acc->mwi_dlg, &tp_sel); |
| 2037 | } |
| 2038 | |
| 2039 | /* Set route-set */ |
| 2040 | if (!pj_list_empty(&acc->route_set)) { |
| 2041 | pjsip_dlg_set_route_set(acc->mwi_dlg, &acc->route_set); |
| 2042 | } |
| 2043 | |
| 2044 | /* Set credentials */ |
| 2045 | if (acc->cred_cnt) { |
| 2046 | pjsip_auth_clt_set_credentials( &acc->mwi_dlg->auth_sess, |
| 2047 | acc->cred_cnt, acc->cred); |
| 2048 | } |
| 2049 | |
| 2050 | /* Set authentication preference */ |
| 2051 | pjsip_auth_clt_set_prefs(&acc->mwi_dlg->auth_sess, &acc->cfg.auth_pref); |
| 2052 | |
| 2053 | pjsip_evsub_set_mod_data(acc->mwi_sub, pjsua_var.mod.id, acc); |
| 2054 | |
| 2055 | status = pjsip_mwi_initiate(acc->mwi_sub, -1, &tdata); |
| 2056 | if (status != PJ_SUCCESS) { |
Benny Prijono | 011e3f2 | 2009-12-10 05:16:23 +0000 | [diff] [blame] | 2057 | if (acc->mwi_dlg) pjsip_dlg_dec_lock(acc->mwi_dlg); |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 2058 | if (acc->mwi_sub) { |
| 2059 | pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE); |
| 2060 | } |
| 2061 | acc->mwi_sub = NULL; |
| 2062 | acc->mwi_dlg = NULL; |
| 2063 | pjsua_perror(THIS_FILE, "Unable to create initial MWI SUBSCRIBE", |
| 2064 | status); |
| 2065 | if (tmp_pool) pj_pool_release(tmp_pool); |
| 2066 | return; |
| 2067 | } |
| 2068 | |
| 2069 | pjsua_process_msg_data(tdata, NULL); |
| 2070 | |
| 2071 | status = pjsip_pres_send_request(acc->mwi_sub, tdata); |
| 2072 | if (status != PJ_SUCCESS) { |
Benny Prijono | 011e3f2 | 2009-12-10 05:16:23 +0000 | [diff] [blame] | 2073 | if (acc->mwi_dlg) pjsip_dlg_dec_lock(acc->mwi_dlg); |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 2074 | if (acc->mwi_sub) { |
| 2075 | pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE); |
| 2076 | } |
| 2077 | acc->mwi_sub = NULL; |
| 2078 | acc->mwi_dlg = NULL; |
| 2079 | pjsua_perror(THIS_FILE, "Unable to send initial MWI SUBSCRIBE", |
| 2080 | status); |
| 2081 | if (tmp_pool) pj_pool_release(tmp_pool); |
| 2082 | return; |
| 2083 | } |
| 2084 | |
| 2085 | pjsip_dlg_dec_lock(acc->mwi_dlg); |
| 2086 | if (tmp_pool) pj_pool_release(tmp_pool); |
| 2087 | |
| 2088 | } |
| 2089 | |
| 2090 | |
Benny Prijono | fe1bd34 | 2009-11-20 23:33:07 +0000 | [diff] [blame] | 2091 | /*************************************************************************** |
| 2092 | * Unsolicited MWI |
| 2093 | */ |
| 2094 | static pj_bool_t unsolicited_mwi_on_rx_request(pjsip_rx_data *rdata) |
| 2095 | { |
| 2096 | pjsip_msg *msg = rdata->msg_info.msg; |
| 2097 | pj_str_t EVENT_HDR = { "Event", 5 }; |
| 2098 | pj_str_t MWI = { "message-summary", 15 }; |
| 2099 | pjsip_event_hdr *eh; |
| 2100 | |
| 2101 | if (pjsip_method_cmp(&msg->line.req.method, &pjsip_notify_method)!=0) { |
| 2102 | /* Only interested with NOTIFY request */ |
| 2103 | return PJ_FALSE; |
| 2104 | } |
| 2105 | |
| 2106 | eh = (pjsip_event_hdr*) pjsip_msg_find_hdr_by_name(msg, &EVENT_HDR, NULL); |
| 2107 | if (!eh) { |
| 2108 | /* Something wrong with the request, it has no Event hdr */ |
| 2109 | return PJ_FALSE; |
| 2110 | } |
| 2111 | |
| 2112 | if (pj_stricmp(&eh->event_type, &MWI) != 0) { |
| 2113 | /* Not MWI event */ |
| 2114 | return PJ_FALSE; |
| 2115 | } |
| 2116 | |
| 2117 | /* Got unsolicited MWI request, respond with 200/OK first */ |
| 2118 | pjsip_endpt_respond(pjsua_get_pjsip_endpt(), NULL, rdata, 200, NULL, |
| 2119 | NULL, NULL, NULL); |
| 2120 | |
| 2121 | |
| 2122 | /* Call callback */ |
| 2123 | if (pjsua_var.ua_cfg.cb.on_mwi_info) { |
| 2124 | pjsua_acc_id acc_id; |
| 2125 | pjsua_mwi_info mwi_info; |
| 2126 | |
| 2127 | acc_id = pjsua_acc_find_for_incoming(rdata); |
| 2128 | |
| 2129 | pj_bzero(&mwi_info, sizeof(mwi_info)); |
| 2130 | mwi_info.rdata = rdata; |
| 2131 | |
| 2132 | (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc_id, &mwi_info); |
| 2133 | } |
| 2134 | |
| 2135 | |
| 2136 | return PJ_TRUE; |
| 2137 | } |
| 2138 | |
| 2139 | /* The module instance. */ |
| 2140 | static pjsip_module pjsua_unsolicited_mwi_mod = |
| 2141 | { |
| 2142 | NULL, NULL, /* prev, next. */ |
| 2143 | { "mod-unsolicited-mwi", 19 }, /* Name. */ |
| 2144 | -1, /* Id */ |
| 2145 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 2146 | NULL, /* load() */ |
| 2147 | NULL, /* start() */ |
| 2148 | NULL, /* stop() */ |
| 2149 | NULL, /* unload() */ |
| 2150 | &unsolicited_mwi_on_rx_request, /* on_rx_request() */ |
| 2151 | NULL, /* on_rx_response() */ |
| 2152 | NULL, /* on_tx_request. */ |
| 2153 | NULL, /* on_tx_response() */ |
| 2154 | NULL, /* on_tsx_state() */ |
| 2155 | }; |
| 2156 | |
| 2157 | static pj_status_t enable_unsolicited_mwi(void) |
| 2158 | { |
| 2159 | pj_status_t status; |
| 2160 | |
| 2161 | status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(), |
| 2162 | &pjsua_unsolicited_mwi_mod); |
| 2163 | if (status != PJ_SUCCESS) |
| 2164 | pjsua_perror(THIS_FILE, "Error registering unsolicited MWI module", |
| 2165 | status); |
| 2166 | |
| 2167 | return status; |
| 2168 | } |
| 2169 | |
| 2170 | |
| 2171 | |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 2172 | /***************************************************************************/ |
| 2173 | |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 2174 | /* Timer callback to re-create client subscription */ |
| 2175 | static void pres_timer_cb(pj_timer_heap_t *th, |
| 2176 | pj_timer_entry *entry) |
| 2177 | { |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 2178 | unsigned i; |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 2179 | pj_time_val delay = { PJSUA_PRES_TIMER, 0 }; |
| 2180 | |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 2181 | entry->id = PJ_FALSE; |
| 2182 | |
| 2183 | /* Retry failed PUBLISH and MWI SUBSCRIBE requests */ |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 2184 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2185 | pjsua_acc *acc = &pjsua_var.acc[i]; |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 2186 | |
| 2187 | /* Retry PUBLISH */ |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 2188 | if (acc->cfg.publish_enabled && acc->publish_sess==NULL) |
| 2189 | pjsua_pres_init_publish_acc(acc->index); |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 2190 | |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 2191 | /* Re-subscribe MWI subscription if it's terminated prematurely */ |
| 2192 | if (acc->cfg.mwi_enabled && !acc->mwi_sub) |
| 2193 | pjsua_start_mwi(acc); |
| 2194 | } |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 2195 | |
| 2196 | /* #937: No need to do bulk client refresh, as buddies have their |
| 2197 | * own individual timer now. |
| 2198 | */ |
| 2199 | //refresh_client_subscriptions(); |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 2200 | |
| 2201 | pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, &delay); |
| 2202 | entry->id = PJ_TRUE; |
| 2203 | |
Benny Prijono | f9c40c3 | 2007-06-28 07:20:26 +0000 | [diff] [blame] | 2204 | PJ_UNUSED_ARG(th); |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2207 | |
| 2208 | /* |
| 2209 | * Init presence |
| 2210 | */ |
| 2211 | pj_status_t pjsua_pres_init() |
| 2212 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2213 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2214 | pj_status_t status; |
| 2215 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2216 | status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2217 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 2218 | pjsua_perror(THIS_FILE, "Unable to register pjsua presence module", |
| 2219 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2222 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 2223 | reset_buddy(i); |
| 2224 | } |
| 2225 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2226 | return status; |
| 2227 | } |
| 2228 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2229 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2230 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2231 | * Start presence subsystem. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2232 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2233 | pj_status_t pjsua_pres_start(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2234 | { |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 2235 | /* Start presence timer to re-subscribe to buddy's presence when |
| 2236 | * subscription has failed. |
| 2237 | */ |
| 2238 | if (pjsua_var.pres_timer.id == PJ_FALSE) { |
| 2239 | pj_time_val pres_interval = {PJSUA_PRES_TIMER, 0}; |
| 2240 | |
| 2241 | pjsua_var.pres_timer.cb = &pres_timer_cb; |
| 2242 | pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.pres_timer, |
| 2243 | &pres_interval); |
Benny Prijono | 9727660 | 2007-06-23 01:07:08 +0000 | [diff] [blame] | 2244 | pjsua_var.pres_timer.id = PJ_TRUE; |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
Benny Prijono | fe1bd34 | 2009-11-20 23:33:07 +0000 | [diff] [blame] | 2247 | if (pjsua_var.ua_cfg.enable_unsolicited_mwi) { |
| 2248 | pj_status_t status = enable_unsolicited_mwi(); |
| 2249 | if (status != PJ_SUCCESS) |
| 2250 | return status; |
| 2251 | } |
| 2252 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2253 | return PJ_SUCCESS; |
| 2254 | } |
| 2255 | |
| 2256 | |
| 2257 | /* |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2258 | * Shutdown presence. |
| 2259 | */ |
| 2260 | void pjsua_pres_shutdown(void) |
| 2261 | { |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2262 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2263 | |
Benny Prijono | 384dab4 | 2009-10-14 01:58:04 +0000 | [diff] [blame] | 2264 | PJ_LOG(4,(THIS_FILE, "Shutting down presence..")); |
| 2265 | |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 2266 | if (pjsua_var.pres_timer.id != 0) { |
| 2267 | pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.pres_timer); |
| 2268 | pjsua_var.pres_timer.id = PJ_FALSE; |
| 2269 | } |
| 2270 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2271 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2272 | if (!pjsua_var.acc[i].valid) |
| 2273 | continue; |
| 2274 | pjsua_pres_delete_acc(i); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2277 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 2278 | pjsua_var.buddy[i].monitor = 0; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2279 | } |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2280 | |
Benny Prijono | 73bb723 | 2009-10-20 13:56:26 +0000 | [diff] [blame] | 2281 | refresh_client_subscriptions(); |
| 2282 | |
| 2283 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2284 | if (pjsua_var.acc[i].valid) |
| 2285 | pjsua_pres_update_acc(i, PJ_FALSE); |
| 2286 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 2287 | } |