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