Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
Benny Prijono | 4f9f64e | 2006-02-27 00:00:30 +0000 | [diff] [blame] | 19 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 20 | #include <pjsua-lib/pjsua_internal.h> |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 21 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 22 | |
| 23 | #define THIS_FILE "pjsua_pres.c" |
| 24 | |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 25 | #ifndef PJSUA_PRES_TIMER |
| 26 | # define PJSUA_PRES_TIMER 120 |
| 27 | #endif |
| 28 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 29 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 30 | /* |
| 31 | * Get total number of buddies. |
| 32 | */ |
| 33 | PJ_DEF(unsigned) pjsua_get_buddy_count(void) |
| 34 | { |
| 35 | return pjsua_var.buddy_cnt; |
| 36 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 37 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Check if buddy ID is valid. |
| 41 | */ |
| 42 | PJ_DEF(pj_bool_t) pjsua_buddy_is_valid(pjsua_buddy_id buddy_id) |
| 43 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 44 | 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] | 45 | pjsua_var.buddy[buddy_id].uri.slen != 0; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | * Enum buddy IDs. |
| 51 | */ |
| 52 | PJ_DEF(pj_status_t) pjsua_enum_buddies( pjsua_buddy_id ids[], |
| 53 | unsigned *count) |
| 54 | { |
| 55 | unsigned i, c; |
| 56 | |
| 57 | PJ_ASSERT_RETURN(ids && count, PJ_EINVAL); |
| 58 | |
| 59 | PJSUA_LOCK(); |
| 60 | |
| 61 | for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 62 | if (!pjsua_var.buddy[i].uri.slen) |
| 63 | continue; |
| 64 | ids[c] = i; |
| 65 | ++c; |
| 66 | } |
| 67 | |
| 68 | *count = c; |
| 69 | |
| 70 | PJSUA_UNLOCK(); |
| 71 | |
| 72 | return PJ_SUCCESS; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /* |
| 78 | * Get detailed buddy info. |
| 79 | */ |
| 80 | PJ_DEF(pj_status_t) pjsua_buddy_get_info( pjsua_buddy_id buddy_id, |
| 81 | pjsua_buddy_info *info) |
| 82 | { |
| 83 | int total=0; |
| 84 | pjsua_buddy *buddy; |
| 85 | |
| 86 | PJ_ASSERT_RETURN(buddy_id>=0 && |
| 87 | buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy), |
| 88 | PJ_EINVAL); |
| 89 | |
| 90 | PJSUA_LOCK(); |
| 91 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 92 | pj_bzero(info, sizeof(pjsua_buddy_info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 93 | |
| 94 | buddy = &pjsua_var.buddy[buddy_id]; |
| 95 | info->id = buddy->index; |
| 96 | if (pjsua_var.buddy[buddy_id].uri.slen == 0) { |
| 97 | PJSUA_UNLOCK(); |
| 98 | return PJ_SUCCESS; |
| 99 | } |
| 100 | |
| 101 | /* uri */ |
| 102 | info->uri.ptr = info->buf_ + total; |
| 103 | pj_strncpy(&info->uri, &buddy->uri, sizeof(info->buf_)-total); |
| 104 | total += info->uri.slen; |
| 105 | |
| 106 | /* contact */ |
| 107 | info->contact.ptr = info->buf_ + total; |
| 108 | pj_strncpy(&info->contact, &buddy->contact, sizeof(info->buf_)-total); |
| 109 | total += info->contact.slen; |
| 110 | |
| 111 | /* status and status text */ |
| 112 | if (buddy->sub == NULL || buddy->status.info_cnt==0) { |
| 113 | info->status = PJSUA_BUDDY_STATUS_UNKNOWN; |
| 114 | info->status_text = pj_str("?"); |
| 115 | } else if (pjsua_var.buddy[buddy_id].status.info[0].basic_open) { |
| 116 | info->status = PJSUA_BUDDY_STATUS_ONLINE; |
| 117 | info->status_text = pj_str("Online"); |
| 118 | } else { |
| 119 | info->status = PJSUA_BUDDY_STATUS_OFFLINE; |
| 120 | info->status_text = pj_str("Offline"); |
| 121 | } |
| 122 | |
| 123 | /* monitor pres */ |
| 124 | info->monitor_pres = buddy->monitor; |
| 125 | |
| 126 | PJSUA_UNLOCK(); |
| 127 | return PJ_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* |
| 132 | * Reset buddy descriptor. |
| 133 | */ |
| 134 | static void reset_buddy(pjsua_buddy_id id) |
| 135 | { |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 136 | pj_bzero(&pjsua_var.buddy[id], sizeof(pjsua_var.buddy[id])); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 137 | pjsua_var.buddy[id].index = id; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * Add new buddy. |
| 143 | */ |
| 144 | PJ_DEF(pj_status_t) pjsua_buddy_add( const pjsua_buddy_config *cfg, |
| 145 | pjsua_buddy_id *p_buddy_id) |
| 146 | { |
| 147 | pjsip_name_addr *url; |
| 148 | pjsip_sip_uri *sip_uri; |
| 149 | int index; |
| 150 | pj_str_t tmp; |
| 151 | |
| 152 | PJ_ASSERT_RETURN(pjsua_var.buddy_cnt <= |
| 153 | PJ_ARRAY_SIZE(pjsua_var.buddy), |
| 154 | PJ_ETOOMANY); |
| 155 | |
| 156 | PJSUA_LOCK(); |
| 157 | |
| 158 | /* Find empty slot */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 159 | for (index=0; index<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++index) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 160 | if (pjsua_var.buddy[index].uri.slen == 0) |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | /* Expect to find an empty slot */ |
| 165 | if (index == PJ_ARRAY_SIZE(pjsua_var.buddy)) { |
| 166 | PJSUA_UNLOCK(); |
| 167 | /* This shouldn't happen */ |
| 168 | pj_assert(!"index < PJ_ARRAY_SIZE(pjsua_var.buddy)"); |
| 169 | return PJ_ETOOMANY; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /* Get name and display name for buddy */ |
| 174 | pj_strdup_with_null(pjsua_var.pool, &tmp, &cfg->uri); |
| 175 | url = (pjsip_name_addr*)pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, |
| 176 | PJSIP_PARSE_URI_AS_NAMEADDR); |
| 177 | |
| 178 | if (url == NULL) { |
| 179 | pjsua_perror(THIS_FILE, "Unable to add buddy", PJSIP_EINVALIDURI); |
| 180 | PJSUA_UNLOCK(); |
| 181 | return PJSIP_EINVALIDURI; |
| 182 | } |
| 183 | |
Benny Prijono | fc49359 | 2007-02-18 20:56:32 +0000 | [diff] [blame] | 184 | /* Only support SIP schemes */ |
| 185 | if (!PJSIP_URI_SCHEME_IS_SIP(url) && !PJSIP_URI_SCHEME_IS_SIPS(url)) |
| 186 | return PJSIP_EINVALIDSCHEME; |
| 187 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 188 | /* Reset buddy, to make sure everything is cleared with default |
| 189 | * values |
| 190 | */ |
| 191 | reset_buddy(index); |
| 192 | |
| 193 | /* Save URI */ |
| 194 | pjsua_var.buddy[index].uri = tmp; |
| 195 | |
Benny Prijono | 9c1528f | 2007-02-10 19:22:25 +0000 | [diff] [blame] | 196 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(url->uri); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 197 | pjsua_var.buddy[index].name = sip_uri->user; |
| 198 | pjsua_var.buddy[index].display = url->display; |
| 199 | pjsua_var.buddy[index].host = sip_uri->host; |
| 200 | pjsua_var.buddy[index].port = sip_uri->port; |
| 201 | pjsua_var.buddy[index].monitor = cfg->subscribe; |
| 202 | if (pjsua_var.buddy[index].port == 0) |
| 203 | pjsua_var.buddy[index].port = 5060; |
| 204 | |
| 205 | if (p_buddy_id) |
| 206 | *p_buddy_id = index; |
| 207 | |
| 208 | pjsua_var.buddy_cnt++; |
| 209 | |
| 210 | pjsua_buddy_subscribe_pres(index, cfg->subscribe); |
| 211 | |
| 212 | PJSUA_UNLOCK(); |
| 213 | |
| 214 | return PJ_SUCCESS; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /* |
| 219 | * Delete buddy. |
| 220 | */ |
| 221 | PJ_DEF(pj_status_t) pjsua_buddy_del(pjsua_buddy_id buddy_id) |
| 222 | { |
| 223 | PJ_ASSERT_RETURN(buddy_id>=0 && |
| 224 | buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy), |
| 225 | PJ_EINVAL); |
| 226 | |
| 227 | PJSUA_LOCK(); |
| 228 | |
| 229 | if (pjsua_var.buddy[buddy_id].uri.slen == 0) { |
| 230 | PJSUA_UNLOCK(); |
| 231 | return PJ_SUCCESS; |
| 232 | } |
| 233 | |
| 234 | /* Unsubscribe presence */ |
| 235 | pjsua_buddy_subscribe_pres(buddy_id, PJ_FALSE); |
| 236 | |
| 237 | /* Remove buddy */ |
| 238 | pjsua_var.buddy[buddy_id].uri.slen = 0; |
| 239 | pjsua_var.buddy_cnt--; |
| 240 | |
| 241 | /* Reset buddy struct */ |
| 242 | reset_buddy(buddy_id); |
| 243 | |
| 244 | PJSUA_UNLOCK(); |
| 245 | return PJ_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /* |
| 250 | * Enable/disable buddy's presence monitoring. |
| 251 | */ |
| 252 | PJ_DEF(pj_status_t) pjsua_buddy_subscribe_pres( pjsua_buddy_id buddy_id, |
| 253 | pj_bool_t subscribe) |
| 254 | { |
| 255 | pjsua_buddy *buddy; |
| 256 | |
| 257 | PJ_ASSERT_RETURN(buddy_id>=0 && |
| 258 | buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy), |
| 259 | PJ_EINVAL); |
| 260 | |
| 261 | PJSUA_LOCK(); |
| 262 | |
| 263 | buddy = &pjsua_var.buddy[buddy_id]; |
| 264 | buddy->monitor = subscribe; |
| 265 | pjsua_pres_refresh(); |
| 266 | |
| 267 | PJSUA_UNLOCK(); |
| 268 | |
| 269 | return PJ_SUCCESS; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /* |
| 274 | * Dump presence subscriptions to log file. |
| 275 | */ |
| 276 | PJ_DEF(void) pjsua_pres_dump(pj_bool_t verbose) |
| 277 | { |
| 278 | unsigned acc_id; |
| 279 | unsigned i; |
| 280 | |
| 281 | |
| 282 | PJSUA_LOCK(); |
| 283 | |
| 284 | /* |
| 285 | * When no detail is required, just dump number of server and client |
| 286 | * subscriptions. |
| 287 | */ |
| 288 | if (verbose == PJ_FALSE) { |
| 289 | |
| 290 | int count = 0; |
| 291 | |
| 292 | for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) { |
| 293 | |
| 294 | if (!pjsua_var.acc[acc_id].valid) |
| 295 | continue; |
| 296 | |
| 297 | if (!pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) { |
| 298 | struct pjsua_srv_pres *uapres; |
| 299 | |
| 300 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
| 301 | while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) { |
| 302 | ++count; |
| 303 | uapres = uapres->next; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | PJ_LOG(3,(THIS_FILE, "Number of server/UAS subscriptions: %d", |
| 309 | count)); |
| 310 | |
| 311 | count = 0; |
| 312 | |
| 313 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 314 | if (pjsua_var.buddy[i].uri.slen == 0) |
| 315 | continue; |
| 316 | if (pjsua_var.buddy[i].sub) { |
| 317 | ++count; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | PJ_LOG(3,(THIS_FILE, "Number of client/UAC subscriptions: %d", |
| 322 | count)); |
| 323 | PJSUA_UNLOCK(); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /* |
| 329 | * Dumping all server (UAS) subscriptions |
| 330 | */ |
| 331 | PJ_LOG(3,(THIS_FILE, "Dumping pjsua server subscriptions:")); |
| 332 | |
| 333 | for (acc_id=0; acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) { |
| 334 | |
| 335 | if (!pjsua_var.acc[acc_id].valid) |
| 336 | continue; |
| 337 | |
| 338 | PJ_LOG(3,(THIS_FILE, " %.*s", |
| 339 | (int)pjsua_var.acc[acc_id].cfg.id.slen, |
| 340 | pjsua_var.acc[acc_id].cfg.id.ptr)); |
| 341 | |
| 342 | if (pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) { |
| 343 | |
| 344 | PJ_LOG(3,(THIS_FILE, " - none - ")); |
| 345 | |
| 346 | } else { |
| 347 | struct pjsua_srv_pres *uapres; |
| 348 | |
| 349 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
| 350 | while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) { |
| 351 | |
| 352 | PJ_LOG(3,(THIS_FILE, " %10s %s", |
| 353 | pjsip_evsub_get_state_name(uapres->sub), |
| 354 | uapres->remote)); |
| 355 | |
| 356 | uapres = uapres->next; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Dumping all client (UAC) subscriptions |
| 363 | */ |
| 364 | PJ_LOG(3,(THIS_FILE, "Dumping pjsua client subscriptions:")); |
| 365 | |
| 366 | if (pjsua_var.buddy_cnt == 0) { |
| 367 | |
| 368 | PJ_LOG(3,(THIS_FILE, " - no buddy list - ")); |
| 369 | |
| 370 | } else { |
| 371 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 372 | |
| 373 | if (pjsua_var.buddy[i].uri.slen == 0) |
| 374 | continue; |
| 375 | |
| 376 | if (pjsua_var.buddy[i].sub) { |
| 377 | PJ_LOG(3,(THIS_FILE, " %10s %.*s", |
| 378 | pjsip_evsub_get_state_name(pjsua_var.buddy[i].sub), |
| 379 | (int)pjsua_var.buddy[i].uri.slen, |
| 380 | pjsua_var.buddy[i].uri.ptr)); |
| 381 | } else { |
| 382 | PJ_LOG(3,(THIS_FILE, " %10s %.*s", |
| 383 | "(null)", |
| 384 | (int)pjsua_var.buddy[i].uri.slen, |
| 385 | pjsua_var.buddy[i].uri.ptr)); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | PJSUA_UNLOCK(); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | /*************************************************************************** |
| 395 | * Server subscription. |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 396 | */ |
| 397 | |
| 398 | /* Proto */ |
| 399 | static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata); |
| 400 | |
| 401 | /* The module instance. */ |
| 402 | static pjsip_module mod_pjsua_pres = |
| 403 | { |
| 404 | NULL, NULL, /* prev, next. */ |
| 405 | { "mod-pjsua-pres", 14 }, /* Name. */ |
| 406 | -1, /* Id */ |
| 407 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 408 | NULL, /* load() */ |
| 409 | NULL, /* start() */ |
| 410 | NULL, /* stop() */ |
| 411 | NULL, /* unload() */ |
| 412 | &pres_on_rx_request, /* on_rx_request() */ |
| 413 | NULL, /* on_rx_response() */ |
| 414 | NULL, /* on_tx_request. */ |
| 415 | NULL, /* on_tx_response() */ |
| 416 | NULL, /* on_tsx_state() */ |
| 417 | |
| 418 | }; |
| 419 | |
| 420 | |
| 421 | /* Callback called when *server* subscription state has changed. */ |
| 422 | static void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event) |
| 423 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 424 | pjsua_srv_pres *uapres; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 425 | |
| 426 | PJ_UNUSED_ARG(event); |
| 427 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 428 | PJSUA_LOCK(); |
| 429 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 430 | 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] | 431 | if (uapres) { |
| 432 | PJ_LOG(3,(THIS_FILE, "Server subscription to %s is %s", |
| 433 | uapres->remote, pjsip_evsub_get_state_name(sub))); |
| 434 | |
| 435 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 436 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 437 | pj_list_erase(uapres); |
| 438 | } |
| 439 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 440 | |
| 441 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* This is called when request is received. |
| 445 | * We need to check for incoming SUBSCRIBE request. |
| 446 | */ |
| 447 | static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata) |
| 448 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 449 | int acc_id; |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 450 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 451 | pj_str_t contact; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 452 | pjsip_method *req_method = &rdata->msg_info.msg->line.req.method; |
| 453 | pjsua_srv_pres *uapres; |
| 454 | pjsip_evsub *sub; |
| 455 | pjsip_evsub_user pres_cb; |
| 456 | pjsip_tx_data *tdata; |
| 457 | pjsip_pres_status pres_status; |
| 458 | pjsip_dialog *dlg; |
| 459 | pj_status_t status; |
| 460 | |
| 461 | if (pjsip_method_cmp(req_method, &pjsip_subscribe_method) != 0) |
| 462 | return PJ_FALSE; |
| 463 | |
| 464 | /* Incoming SUBSCRIBE: */ |
| 465 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 466 | PJSUA_LOCK(); |
| 467 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 468 | /* Find which account for the incoming request. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 469 | acc_id = pjsua_acc_find_for_incoming(rdata); |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 470 | acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 471 | |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 472 | PJ_LOG(4,(THIS_FILE, "Creating server subscription, using account %d", |
| 473 | acc_id)); |
| 474 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 475 | /* Create suitable Contact header */ |
| 476 | status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact, |
| 477 | acc_id, rdata); |
| 478 | if (status != PJ_SUCCESS) { |
| 479 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); |
| 480 | PJSUA_UNLOCK(); |
| 481 | return PJ_TRUE; |
| 482 | } |
| 483 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 484 | /* Create UAS dialog: */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 485 | status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 486 | &contact, &dlg); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 487 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 488 | pjsua_perror(THIS_FILE, |
| 489 | "Unable to create UAS dialog for subscription", |
| 490 | status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 491 | PJSUA_UNLOCK(); |
| 492 | return PJ_TRUE; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 495 | /* Set credentials. */ |
| 496 | pjsip_auth_clt_set_credentials(&dlg->auth_sess, acc->cred_cnt, acc->cred); |
| 497 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 498 | /* Init callback: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 499 | pj_bzero(&pres_cb, sizeof(pres_cb)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 500 | pres_cb.on_evsub_state = &pres_evsub_on_srv_state; |
| 501 | |
| 502 | /* Create server presence subscription: */ |
| 503 | status = pjsip_pres_create_uas( dlg, &pres_cb, rdata, &sub); |
| 504 | if (status != PJ_SUCCESS) { |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 505 | pjsip_dlg_terminate(dlg); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 506 | pjsua_perror(THIS_FILE, "Unable to create server subscription", |
| 507 | status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 508 | PJSUA_UNLOCK(); |
| 509 | return PJ_TRUE; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 512 | /* If account is locked to specific transport, then lock dialog |
| 513 | * to this transport too. |
| 514 | */ |
| 515 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 516 | pjsip_tpselector tp_sel; |
| 517 | |
| 518 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
| 519 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 520 | } |
| 521 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 522 | /* Attach our data to the subscription: */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 523 | uapres = PJ_POOL_ALLOC_T(dlg->pool, pjsua_srv_pres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 524 | uapres->sub = sub; |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 525 | uapres->remote = (char*) pj_pool_alloc(dlg->pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 526 | status = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, dlg->remote.info->uri, |
| 527 | uapres->remote, PJSIP_MAX_URL_SIZE); |
| 528 | if (status < 1) |
| 529 | pj_ansi_strcpy(uapres->remote, "<-- url is too long-->"); |
| 530 | else |
| 531 | uapres->remote[status] = '\0'; |
| 532 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 533 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, uapres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 534 | |
| 535 | /* Add server subscription to the list: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 536 | 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] | 537 | |
| 538 | |
| 539 | /* Create and send 200 (OK) to the SUBSCRIBE request: */ |
| 540 | status = pjsip_pres_accept(sub, rdata, 200, NULL); |
| 541 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 542 | pjsua_perror(THIS_FILE, "Unable to accept presence subscription", |
| 543 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 544 | pj_list_erase(uapres); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 545 | pjsip_pres_terminate(sub, PJ_FALSE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 546 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 547 | return PJ_FALSE; |
| 548 | } |
| 549 | |
| 550 | |
| 551 | /* Set our online status: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 552 | pj_bzero(&pres_status, sizeof(pres_status)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 553 | pres_status.info_cnt = 1; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 554 | pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status; |
| 555 | //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] | 556 | //causing XML parsing to fail. |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 557 | //pres_status.info[0].contact = pjsua_var.local_uri; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 558 | |
| 559 | pjsip_pres_set_status(sub, &pres_status); |
| 560 | |
| 561 | /* Create and send the first NOTIFY to active subscription: */ |
| 562 | status = pjsip_pres_notify( sub, PJSIP_EVSUB_STATE_ACTIVE, NULL, |
| 563 | NULL, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 564 | if (status == PJ_SUCCESS) { |
| 565 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 566 | status = pjsip_pres_send_request( sub, tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 567 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 568 | |
| 569 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 570 | pjsua_perror(THIS_FILE, "Unable to create/send NOTIFY", |
| 571 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 572 | pj_list_erase(uapres); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 573 | pjsip_pres_terminate(sub, PJ_FALSE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 574 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 575 | return PJ_FALSE; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | /* Done: */ |
| 580 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 581 | PJSUA_UNLOCK(); |
| 582 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 583 | return PJ_TRUE; |
| 584 | } |
| 585 | |
| 586 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 587 | /* |
| 588 | * Client presence publication callback. |
| 589 | */ |
| 590 | static void publish_cb(struct pjsip_publishc_cbparam *param) |
| 591 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 592 | pjsua_acc *acc = (pjsua_acc*) param->token; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 593 | |
| 594 | if (param->code/100 != 2 || param->status != PJ_SUCCESS) { |
| 595 | if (param->status != PJ_SUCCESS) { |
| 596 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 597 | |
| 598 | pj_strerror(param->status, errmsg, sizeof(errmsg)); |
| 599 | PJ_LOG(1,(THIS_FILE, |
| 600 | "Client publication (PUBLISH) failed, status=%d, msg=%s", |
| 601 | param->status, errmsg)); |
| 602 | } else { |
| 603 | PJ_LOG(1,(THIS_FILE, |
| 604 | "Client publication (PUBLISH) failed (%d/%.*s)", |
| 605 | param->code, (int)param->reason.slen, |
| 606 | param->reason.ptr)); |
| 607 | } |
| 608 | |
| 609 | pjsip_publishc_destroy(param->pubc); |
| 610 | acc->publish_sess = NULL; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | |
| 615 | /* |
| 616 | * Send PUBLISH request. |
| 617 | */ |
| 618 | static pj_status_t send_publish(int acc_id, pj_bool_t active) |
| 619 | { |
| 620 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
| 621 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 622 | pjsip_pres_status pres_status; |
| 623 | pjsip_tx_data *tdata; |
| 624 | pj_status_t status; |
| 625 | |
| 626 | |
| 627 | /* Create PUBLISH request */ |
| 628 | if (active) { |
Benny Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 629 | char *bpos; |
| 630 | pj_str_t entity; |
| 631 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 632 | status = pjsip_publishc_publish(acc->publish_sess, PJ_TRUE, &tdata); |
| 633 | if (status != PJ_SUCCESS) { |
| 634 | pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status); |
| 635 | goto on_error; |
| 636 | } |
| 637 | |
| 638 | /* Set our online status: */ |
| 639 | pj_bzero(&pres_status, sizeof(pres_status)); |
| 640 | pres_status.info_cnt = 1; |
| 641 | pres_status.info[0].basic_open = acc->online_status; |
| 642 | |
Benny Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 643 | /* Be careful not to send PIDF with presence entity ID containing |
| 644 | * "<" character. |
| 645 | */ |
| 646 | if ((bpos=pj_strchr(&acc_cfg->id, '<')) != NULL) { |
| 647 | char *epos = pj_strchr(&acc_cfg->id, '>'); |
| 648 | if (epos - bpos < 2) { |
| 649 | pj_assert(!"Unexpected invalid URI"); |
| 650 | status = PJSIP_EINVALIDURI; |
| 651 | goto on_error; |
| 652 | } |
| 653 | entity.ptr = bpos+1; |
| 654 | entity.slen = epos - bpos - 1; |
| 655 | } else { |
| 656 | entity = acc_cfg->id; |
| 657 | } |
| 658 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 659 | /* Create and add PIDF message body */ |
| 660 | status = pjsip_pres_create_pidf(tdata->pool, &pres_status, |
Benny Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 661 | &entity, &tdata->msg->body); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 662 | if (status != PJ_SUCCESS) { |
| 663 | pjsua_perror(THIS_FILE, "Error creating PIDF for PUBLISH request", |
| 664 | status); |
| 665 | pjsip_tx_data_dec_ref(tdata); |
| 666 | goto on_error; |
| 667 | } |
| 668 | } else { |
| 669 | status = pjsip_publishc_unpublish(acc->publish_sess, &tdata); |
| 670 | if (status != PJ_SUCCESS) { |
| 671 | pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status); |
| 672 | goto on_error; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | /* Add headers etc */ |
| 677 | pjsua_process_msg_data(tdata, NULL); |
| 678 | |
| 679 | /* Send the PUBLISH request */ |
| 680 | status = pjsip_publishc_send(acc->publish_sess, tdata); |
| 681 | if (status != PJ_SUCCESS) { |
| 682 | pjsua_perror(THIS_FILE, "Error sending PUBLISH request", status); |
| 683 | goto on_error; |
| 684 | } |
| 685 | |
| 686 | acc->publish_state = acc->online_status; |
| 687 | return PJ_SUCCESS; |
| 688 | |
| 689 | on_error: |
| 690 | pjsip_publishc_destroy(acc->publish_sess); |
| 691 | acc->publish_sess = NULL; |
| 692 | return status; |
| 693 | } |
| 694 | |
| 695 | |
| 696 | /* Create client publish session */ |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 697 | pj_status_t pjsua_pres_init_publish_acc(int acc_id) |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 698 | { |
| 699 | const pj_str_t STR_PRESENCE = { "presence", 8 }; |
| 700 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
| 701 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 702 | pj_status_t status; |
| 703 | |
| 704 | /* Create and init client publication session */ |
| 705 | if (acc_cfg->publish_enabled) { |
| 706 | |
| 707 | /* Create client publication */ |
| 708 | status = pjsip_publishc_create(pjsua_var.endpt, 0, acc, &publish_cb, |
| 709 | &acc->publish_sess); |
| 710 | if (status != PJ_SUCCESS) { |
| 711 | acc->publish_sess = NULL; |
| 712 | return status; |
| 713 | } |
| 714 | |
| 715 | /* Initialize client publication */ |
| 716 | status = pjsip_publishc_init(acc->publish_sess, &STR_PRESENCE, |
| 717 | &acc_cfg->id, &acc_cfg->id, |
| 718 | &acc_cfg->id, |
Benny Prijono | 703b7d7 | 2007-03-20 09:13:24 +0000 | [diff] [blame] | 719 | 60); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 720 | if (status != PJ_SUCCESS) { |
| 721 | acc->publish_sess = NULL; |
| 722 | return status; |
| 723 | } |
| 724 | |
Benny Prijono | 703b7d7 | 2007-03-20 09:13:24 +0000 | [diff] [blame] | 725 | /* Add credential for authentication */ |
| 726 | pjsip_publishc_set_credentials(acc->publish_sess, acc->cred_cnt, |
| 727 | acc->cred); |
| 728 | |
| 729 | /* Set route-set */ |
| 730 | pjsip_publishc_set_route_set(acc->publish_sess, &acc->route_set); |
| 731 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 732 | /* Send initial PUBLISH request */ |
| 733 | if (acc->online_status != 0) { |
| 734 | status = send_publish(acc_id, PJ_TRUE); |
| 735 | if (status != PJ_SUCCESS) |
| 736 | return status; |
| 737 | } |
| 738 | |
| 739 | } else { |
| 740 | acc->publish_sess = NULL; |
| 741 | } |
| 742 | |
| 743 | return PJ_SUCCESS; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | /* Init presence for account */ |
| 748 | pj_status_t pjsua_pres_init_acc(int acc_id) |
| 749 | { |
| 750 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 751 | |
| 752 | /* Init presence subscription */ |
| 753 | pj_list_init(&acc->pres_srv_list); |
| 754 | |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 755 | return PJ_SUCCESS; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 759 | /* Terminate server subscription for the account */ |
| 760 | void pjsua_pres_delete_acc(int acc_id) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 761 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 762 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 763 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 764 | pjsua_srv_pres *uapres; |
| 765 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 766 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 767 | |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 768 | /* Notify all subscribers that we're no longer available */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 769 | while (uapres != &acc->pres_srv_list) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 770 | |
| 771 | pjsip_pres_status pres_status; |
| 772 | pj_str_t reason = { "noresource", 10 }; |
| 773 | pjsip_tx_data *tdata; |
| 774 | |
| 775 | pjsip_pres_get_status(uapres->sub, &pres_status); |
| 776 | |
| 777 | pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status; |
| 778 | pjsip_pres_set_status(uapres->sub, &pres_status); |
| 779 | |
| 780 | if (pjsip_pres_notify(uapres->sub, |
| 781 | PJSIP_EVSUB_STATE_TERMINATED, NULL, |
| 782 | &reason, &tdata)==PJ_SUCCESS) |
| 783 | { |
| 784 | pjsip_pres_send_request(uapres->sub, tdata); |
| 785 | } |
| 786 | |
| 787 | uapres = uapres->next; |
| 788 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 789 | |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 790 | /* Clear server presence subscription list because account might be reused |
| 791 | * later. */ |
| 792 | pj_list_init(&acc->pres_srv_list); |
| 793 | |
| 794 | /* Terminate presence publication, if any */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 795 | if (acc->publish_sess) { |
| 796 | acc->online_status = PJ_FALSE; |
| 797 | send_publish(acc_id, PJ_FALSE); |
| 798 | if (acc->publish_sess) { |
| 799 | pjsip_publishc_destroy(acc->publish_sess); |
| 800 | acc->publish_sess = NULL; |
| 801 | } |
| 802 | acc_cfg->publish_enabled = PJ_FALSE; |
| 803 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | |
| 807 | /* Refresh subscription (e.g. when our online status has changed) */ |
| 808 | static void refresh_server_subscription(int acc_id) |
| 809 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 810 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 811 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 812 | pjsua_srv_pres *uapres; |
| 813 | |
| 814 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
| 815 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 816 | while (uapres != &acc->pres_srv_list) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 817 | |
| 818 | pjsip_pres_status pres_status; |
| 819 | pjsip_tx_data *tdata; |
| 820 | |
| 821 | pjsip_pres_get_status(uapres->sub, &pres_status); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 822 | if (pres_status.info[0].basic_open != acc->online_status) { |
| 823 | pres_status.info[0].basic_open = acc->online_status; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 824 | pjsip_pres_set_status(uapres->sub, &pres_status); |
| 825 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 826 | if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) { |
| 827 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 828 | pjsip_pres_send_request(uapres->sub, tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 829 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | uapres = uapres->next; |
| 833 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 834 | |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 835 | /* Send PUBLISH if required. We only do this when we have a PUBLISH |
| 836 | * session. If we don't have a PUBLISH session, then it could be |
| 837 | * that we're waiting until registration has completed before we |
| 838 | * send the first PUBLISH. |
| 839 | */ |
| 840 | if (acc_cfg->publish_enabled && acc->publish_sess) { |
| 841 | if (acc->publish_state != acc->online_status) { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 842 | send_publish(acc_id, PJ_TRUE); |
| 843 | } |
| 844 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | |
| 848 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 849 | /*************************************************************************** |
| 850 | * Client subscription. |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 851 | */ |
| 852 | |
| 853 | /* Callback called when *client* subscription state has changed. */ |
| 854 | static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event) |
| 855 | { |
| 856 | pjsua_buddy *buddy; |
| 857 | |
| 858 | PJ_UNUSED_ARG(event); |
| 859 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 860 | PJSUA_LOCK(); |
| 861 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 862 | 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] | 863 | if (buddy) { |
| 864 | PJ_LOG(3,(THIS_FILE, |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 865 | "Presence subscription to %.*s is %s", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 866 | (int)pjsua_var.buddy[buddy->index].uri.slen, |
| 867 | pjsua_var.buddy[buddy->index].uri.ptr, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 868 | pjsip_evsub_get_state_name(sub))); |
| 869 | |
| 870 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 871 | buddy->sub = NULL; |
| 872 | buddy->status.info_cnt = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 873 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 874 | } |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 875 | |
| 876 | /* Call callback */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 877 | if (pjsua_var.ua_cfg.cb.on_buddy_state) |
| 878 | (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 879 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 880 | |
| 881 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 884 | |
| 885 | /* Callback when transaction state has changed. */ |
| 886 | static void pjsua_evsub_on_tsx_state(pjsip_evsub *sub, |
| 887 | pjsip_transaction *tsx, |
| 888 | pjsip_event *event) |
| 889 | { |
| 890 | pjsua_buddy *buddy; |
| 891 | pjsip_contact_hdr *contact_hdr; |
| 892 | |
| 893 | PJSUA_LOCK(); |
| 894 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 895 | 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] | 896 | if (!buddy) { |
| 897 | PJSUA_UNLOCK(); |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | /* We only use this to update buddy's Contact, when it's not |
| 902 | * set. |
| 903 | */ |
| 904 | if (buddy->contact.slen != 0) { |
| 905 | /* Contact already set */ |
| 906 | PJSUA_UNLOCK(); |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | /* Only care about 2xx response to outgoing SUBSCRIBE */ |
| 911 | if (tsx->status_code/100 != 2 || |
| 912 | tsx->role != PJSIP_UAC_ROLE || |
| 913 | event->type != PJSIP_EVENT_RX_MSG || |
| 914 | pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method)!=0) |
| 915 | { |
| 916 | PJSUA_UNLOCK(); |
| 917 | return; |
| 918 | } |
| 919 | |
| 920 | /* Find contact header. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 921 | contact_hdr = (pjsip_contact_hdr*) |
| 922 | pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 923 | PJSIP_H_CONTACT, NULL); |
| 924 | if (!contact_hdr) { |
| 925 | PJSUA_UNLOCK(); |
| 926 | return; |
| 927 | } |
| 928 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 929 | buddy->contact.ptr = (char*) |
| 930 | pj_pool_alloc(pjsua_var.pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 931 | buddy->contact.slen = pjsip_uri_print( PJSIP_URI_IN_CONTACT_HDR, |
| 932 | contact_hdr->uri, |
| 933 | buddy->contact.ptr, |
| 934 | PJSIP_MAX_URL_SIZE); |
| 935 | if (buddy->contact.slen < 0) |
| 936 | buddy->contact.slen = 0; |
| 937 | |
| 938 | PJSUA_UNLOCK(); |
| 939 | } |
| 940 | |
| 941 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 942 | /* Callback called when we receive NOTIFY */ |
| 943 | static void pjsua_evsub_on_rx_notify(pjsip_evsub *sub, |
| 944 | pjsip_rx_data *rdata, |
| 945 | int *p_st_code, |
| 946 | pj_str_t **p_st_text, |
| 947 | pjsip_hdr *res_hdr, |
| 948 | pjsip_msg_body **p_body) |
| 949 | { |
| 950 | pjsua_buddy *buddy; |
| 951 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 952 | PJSUA_LOCK(); |
| 953 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 954 | 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] | 955 | if (buddy) { |
| 956 | /* Update our info. */ |
| 957 | pjsip_pres_get_status(sub, &buddy->status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* The default is to send 200 response to NOTIFY. |
| 961 | * Just leave it there.. |
| 962 | */ |
| 963 | PJ_UNUSED_ARG(rdata); |
| 964 | PJ_UNUSED_ARG(p_st_code); |
| 965 | PJ_UNUSED_ARG(p_st_text); |
| 966 | PJ_UNUSED_ARG(res_hdr); |
| 967 | PJ_UNUSED_ARG(p_body); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 968 | |
| 969 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | |
| 973 | /* Event subscription callback. */ |
| 974 | static pjsip_evsub_user pres_callback = |
| 975 | { |
| 976 | &pjsua_evsub_on_state, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 977 | &pjsua_evsub_on_tsx_state, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 978 | |
| 979 | NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless |
| 980 | * we want to authenticate |
| 981 | */ |
| 982 | |
| 983 | &pjsua_evsub_on_rx_notify, |
| 984 | |
| 985 | NULL, /* on_client_refresh: Use default behaviour, which is to |
| 986 | * refresh client subscription. */ |
| 987 | |
| 988 | NULL, /* on_server_timeout: Use default behaviour, which is to send |
| 989 | * NOTIFY to terminate. |
| 990 | */ |
| 991 | }; |
| 992 | |
| 993 | |
| 994 | /* It does what it says.. */ |
| 995 | static void subscribe_buddy_presence(unsigned index) |
| 996 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 997 | pjsua_buddy *buddy; |
| 998 | int acc_id; |
| 999 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1000 | pj_str_t contact; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1001 | pjsip_dialog *dlg; |
| 1002 | pjsip_tx_data *tdata; |
| 1003 | pj_status_t status; |
| 1004 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1005 | buddy = &pjsua_var.buddy[index]; |
| 1006 | acc_id = pjsua_acc_find_for_outgoing(&buddy->uri); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1007 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1008 | acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1009 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1010 | PJ_LOG(4,(THIS_FILE, "Using account %d for buddy %d subscription", |
| 1011 | acc_id, index)); |
| 1012 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1013 | /* Generate suitable Contact header */ |
| 1014 | status = pjsua_acc_create_uac_contact(pjsua_var.pool, &contact, |
| 1015 | acc_id, &buddy->uri); |
| 1016 | if (status != PJ_SUCCESS) { |
| 1017 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); |
| 1018 | return; |
| 1019 | } |
| 1020 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1021 | /* Create UAC dialog */ |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1022 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1023 | &acc->cfg.id, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1024 | &contact, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1025 | &buddy->uri, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1026 | NULL, &dlg); |
| 1027 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1028 | pjsua_perror(THIS_FILE, "Unable to create dialog", |
| 1029 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1030 | return; |
| 1031 | } |
| 1032 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1033 | status = pjsip_pres_create_uac( dlg, &pres_callback, |
| 1034 | PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub); |
| 1035 | if (status != PJ_SUCCESS) { |
| 1036 | pjsua_var.buddy[index].sub = NULL; |
| 1037 | pjsua_perror(THIS_FILE, "Unable to create presence client", |
| 1038 | status); |
| 1039 | pjsip_dlg_terminate(dlg); |
| 1040 | return; |
| 1041 | } |
| 1042 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1043 | /* If account is locked to specific transport, then lock dialog |
| 1044 | * to this transport too. |
| 1045 | */ |
| 1046 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 1047 | pjsip_tpselector tp_sel; |
| 1048 | |
| 1049 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
| 1050 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 1051 | } |
| 1052 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1053 | /* Set route-set */ |
| 1054 | if (!pj_list_empty(&acc->route_set)) { |
| 1055 | pjsip_dlg_set_route_set(dlg, &acc->route_set); |
| 1056 | } |
| 1057 | |
| 1058 | /* Set credentials */ |
| 1059 | if (acc->cred_cnt) { |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1060 | pjsip_auth_clt_set_credentials( &dlg->auth_sess, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1061 | acc->cred_cnt, acc->cred); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1062 | } |
Benny Prijono | 1d8d608 | 2006-04-29 12:38:25 +0000 | [diff] [blame] | 1063 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1064 | pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1065 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1066 | status = pjsip_pres_initiate(buddy->sub, -1, &tdata); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1067 | if (status != PJ_SUCCESS) { |
Benny Prijono | a6992c5 | 2007-06-05 22:58:32 +0000 | [diff] [blame^] | 1068 | if (buddy->sub) { |
| 1069 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1070 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1071 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1072 | pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE", |
| 1073 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1074 | return; |
| 1075 | } |
| 1076 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1077 | pjsua_process_msg_data(tdata, NULL); |
| 1078 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1079 | status = pjsip_pres_send_request(buddy->sub, tdata); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1080 | if (status != PJ_SUCCESS) { |
Benny Prijono | a6992c5 | 2007-06-05 22:58:32 +0000 | [diff] [blame^] | 1081 | if (buddy->sub) { |
| 1082 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1083 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1084 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1085 | pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE", |
| 1086 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1087 | return; |
| 1088 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | /* It does what it says... */ |
| 1093 | static void unsubscribe_buddy_presence(unsigned index) |
| 1094 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1095 | pjsua_buddy *buddy; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1096 | pjsip_tx_data *tdata; |
| 1097 | pj_status_t status; |
| 1098 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1099 | buddy = &pjsua_var.buddy[index]; |
| 1100 | |
| 1101 | if (buddy->sub == NULL) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1102 | return; |
| 1103 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1104 | if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 1105 | pjsua_var.buddy[index].sub = NULL; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1106 | return; |
| 1107 | } |
| 1108 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1109 | status = pjsip_pres_initiate( buddy->sub, 0, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1110 | if (status == PJ_SUCCESS) { |
| 1111 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1112 | status = pjsip_pres_send_request( buddy->sub, tdata ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1113 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1114 | |
Benny Prijono | 48da92e | 2007-05-16 08:56:30 +0000 | [diff] [blame] | 1115 | if (status != PJ_SUCCESS && buddy->sub) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1116 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1117 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1118 | pjsua_perror(THIS_FILE, "Unable to unsubscribe presence", |
| 1119 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | /* It does what it says.. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1125 | static void refresh_client_subscriptions(void) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1126 | { |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1127 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1128 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1129 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1130 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1131 | if (!pjsua_var.buddy[i].uri.slen) |
| 1132 | continue; |
| 1133 | |
| 1134 | if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1135 | subscribe_buddy_presence(i); |
| 1136 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1137 | } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1138 | unsubscribe_buddy_presence(i); |
| 1139 | |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1144 | /* Timer callback to re-create client subscription */ |
| 1145 | static void pres_timer_cb(pj_timer_heap_t *th, |
| 1146 | pj_timer_entry *entry) |
| 1147 | { |
| 1148 | pj_time_val delay = { PJSUA_PRES_TIMER, 0 }; |
| 1149 | |
| 1150 | PJ_UNUSED_ARG(th); |
| 1151 | |
| 1152 | PJSUA_LOCK(); |
| 1153 | |
| 1154 | entry->id = PJ_FALSE; |
| 1155 | refresh_client_subscriptions(); |
| 1156 | |
| 1157 | pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, &delay); |
| 1158 | entry->id = PJ_TRUE; |
| 1159 | |
| 1160 | PJSUA_UNLOCK(); |
| 1161 | } |
| 1162 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1163 | |
| 1164 | /* |
| 1165 | * Init presence |
| 1166 | */ |
| 1167 | pj_status_t pjsua_pres_init() |
| 1168 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1169 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1170 | pj_status_t status; |
| 1171 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1172 | status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1173 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1174 | pjsua_perror(THIS_FILE, "Unable to register pjsua presence module", |
| 1175 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1178 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 1179 | reset_buddy(i); |
| 1180 | } |
| 1181 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1182 | return status; |
| 1183 | } |
| 1184 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1185 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1186 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1187 | * Start presence subsystem. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1188 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1189 | pj_status_t pjsua_pres_start(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1190 | { |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1191 | /* Start presence timer to re-subscribe to buddy's presence when |
| 1192 | * subscription has failed. |
| 1193 | */ |
| 1194 | if (pjsua_var.pres_timer.id == PJ_FALSE) { |
| 1195 | pj_time_val pres_interval = {PJSUA_PRES_TIMER, 0}; |
| 1196 | |
| 1197 | pjsua_var.pres_timer.cb = &pres_timer_cb; |
| 1198 | pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.pres_timer, |
| 1199 | &pres_interval); |
| 1200 | } |
| 1201 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1202 | return PJ_SUCCESS; |
| 1203 | } |
| 1204 | |
| 1205 | |
| 1206 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1207 | * Refresh presence subscriptions |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1208 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1209 | void pjsua_pres_refresh() |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1210 | { |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 1211 | unsigned i; |
| 1212 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1213 | refresh_client_subscriptions(); |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 1214 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1215 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1216 | if (pjsua_var.acc[i].valid) |
| 1217 | refresh_server_subscription(i); |
| 1218 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | /* |
| 1223 | * Shutdown presence. |
| 1224 | */ |
| 1225 | void pjsua_pres_shutdown(void) |
| 1226 | { |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1227 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1228 | |
Benny Prijono | 7a5f510 | 2007-05-29 00:33:09 +0000 | [diff] [blame] | 1229 | if (pjsua_var.pres_timer.id != 0) { |
| 1230 | pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.pres_timer); |
| 1231 | pjsua_var.pres_timer.id = PJ_FALSE; |
| 1232 | } |
| 1233 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1234 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1235 | if (!pjsua_var.acc[i].valid) |
| 1236 | continue; |
| 1237 | pjsua_pres_delete_acc(i); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1240 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 1241 | pjsua_var.buddy[i].monitor = 0; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1242 | } |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1243 | |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 1244 | pjsua_pres_refresh(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1245 | } |