Debian Codegraph @

Xpjproject/pjsip/src/pjsip-simple/mwi.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 */
19 #include <pjsip-simple/mwi.h>
20 #include <pjsip-simple/errno.h>
21 #include <pjsip-simple/evsub_msg.h>
22 #include <pjsip/sip_module.h>
23 #include <pjsip/sip_endpoint.h>
24 #include <pjsip/sip_dialog.h>
25 #include <pj/assert.h>
26 #include <pj/guid.h>
27 #include <pj/log.h>
28 #include <pj/os.h>
29 #include <pj/pool.h>
30 #include <pj/string.h>
31
32
33 #define THIS_FILE "mwi.c"
34
35 /*
36 * MWI module (mod-mdi)
37 */
38 static struct pjsip_module mod_mwi =
39 {
40 NULL, NULL, /* prev, next. */
41 { "mod-mwi", 7 }, /* Name. */
42 -1, /* Id */
43 PJSIP_MOD_PRIORITY_DIALOG_USAGE,/* Priority */
44 NULL, /* load() */
45 NULL, /* start() */
46 NULL, /* stop() */
47 NULL, /* unload() */
48 NULL, /* on_rx_request() */
49 NULL, /* on_rx_response() */
50 NULL, /* on_tx_request. */
51 NULL, /* on_tx_response() */
52 NULL, /* on_tsx_state() */
53 };
54
55
56 /*
57 * This structure describe an mwi agent (both client and server)
58 */
59 typedef struct pjsip_mwi
60 {
61 pjsip_evsub *sub; /**< Event subscribtion record. */
62 pjsip_dialog *dlg; /**< The dialog. */
63 pjsip_evsub_user user_cb; /**< The user callback. */
64
65 /* These are for server subscriptions */
66 pj_pool_t *body_pool; /**< Pool to save message body */
67 pjsip_media_type mime_type; /**< MIME type of last msg body */
68 pj_str_t body; /**< Last sent message body */
69 } pjsip_mwi;
70
71
72 /*
73 * Forward decl for evsub callbacks.
74 */
75 static void mwi_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
76 static void mwi_on_evsub_tsx_state( pjsip_evsub *sub, pjsip_transaction *tsx,
77 pjsip_event *event);
78 static void mwi_on_evsub_rx_refresh( pjsip_evsub *sub,
79 pjsip_rx_data *rdata,
80 int *p_st_code,
81 pj_str_t **p_st_text,
82 pjsip_hdr *res_hdr,
83 pjsip_msg_body **p_body);
84 static void mwi_on_evsub_rx_notify( pjsip_evsub *sub,
85 pjsip_rx_data *rdata,
86 int *p_st_code,
87 pj_str_t **p_st_text,
88 pjsip_hdr *res_hdr,
89 pjsip_msg_body **p_body);
90 static void mwi_on_evsub_client_refresh(pjsip_evsub *sub);
91 static void mwi_on_evsub_server_timeout(pjsip_evsub *sub);
92
93
94 /*
95 * Event subscription callback for mwi.
96 */
97 static pjsip_evsub_user mwi_user =
98 {
99 &mwi_on_evsub_state,
100 &mwi_on_evsub_tsx_state,
101 &mwi_on_evsub_rx_refresh,
102 &mwi_on_evsub_rx_notify,
103 &mwi_on_evsub_client_refresh,
104 &mwi_on_evsub_server_timeout,
105 };
106
107
108 /*
109 * Some static constants.
110 */
111 static const pj_str_t STR_EVENT = { "Event", 5 };
112 static const pj_str_t STR_MWI = { "message-summary", 15 };
113 static const pj_str_t STR_APP_SIMPLE_SMS = { "application/simple-message-summary", 34};
114
115 /*
116 * Init mwi module.
117 */
118 PJ_DEF(pj_status_t) pjsip_mwi_init_module( pjsip_endpoint *endpt,
119 pjsip_module *mod_evsub)
120 {
121 pj_status_t status;
122 pj_str_t accept[1];
123
124 /* Check arguments. */
125 PJ_ASSERT_RETURN(endpt && mod_evsub, PJ_EINVAL);
126
127 /* Must have not been registered */
128 PJ_ASSERT_RETURN(mod_mwi.id == -1, PJ_EINVALIDOP);
129
130 /* Register to endpoint */
131 status = pjsip_endpt_register_module(endpt, &mod_mwi);
132 if (status != PJ_SUCCESS)
133 return status;
134
135 accept[0] = STR_APP_SIMPLE_SMS;
136
137 /* Register event package to event module. */
138 status = pjsip_evsub_register_pkg( &mod_mwi, &STR_MWI,
139 PJSIP_MWI_DEFAULT_EXPIRES,
140 PJ_ARRAY_SIZE(accept), accept);
141 if (status != PJ_SUCCESS) {
142 pjsip_endpt_unregister_module(endpt, &mod_mwi);
143 return status;
144 }
145
146 return PJ_SUCCESS;
147 }
148
149
150 /*
151 * Get mwi module instance.
152 */
153 PJ_DEF(pjsip_module*) pjsip_mwi_instance(void)
154 {
155 return &mod_mwi;
156 }
157
158
159 /*
160 * Create client subscription.
161 */
162 PJ_DEF(pj_status_t) pjsip_mwi_create_uac( pjsip_dialog *dlg,
163 const pjsip_evsub_user *user_cb,
164 unsigned options,
165 pjsip_evsub **p_evsub )
166 {
167 pj_status_t status;
168 pjsip_mwi *mwi;
169 pjsip_evsub *sub;
170
171 PJ_ASSERT_RETURN(dlg && p_evsub, PJ_EINVAL);
172
173 PJ_UNUSED_ARG(options);
174
175 pjsip_dlg_inc_lock(dlg);
176
177 /* Create event subscription */
178 status = pjsip_evsub_create_uac( dlg, &mwi_user, &STR_MWI,
179 options, &sub);
180 if (status != PJ_SUCCESS)
181 goto on_return;
182
183 /* Create mwi */
184 mwi = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_mwi);
185 mwi->dlg = dlg;
186 mwi->sub = sub;
187 if (user_cb)
188 pj_memcpy(&mwi->user_cb, user_cb, sizeof(pjsip_evsub_user));
189
190 /* Attach to evsub */
191 pjsip_evsub_set_mod_data(sub, mod_mwi.id, mwi);
192
193 *p_evsub = sub;
194
195 on_return:
196 pjsip_dlg_dec_lock(dlg);
197 return status;
198 }
199
200
201 /*
202 * Create server subscription.
203 */
204 PJ_DEF(pj_status_t) pjsip_mwi_create_uas( pjsip_dialog *dlg,
205 const pjsip_evsub_user *user_cb,
206 pjsip_rx_data *rdata,
207 pjsip_evsub **p_evsub )
208 {
209 pjsip_accept_hdr *accept;
210 pjsip_event_hdr *event;
211 pjsip_evsub *sub;
212 pjsip_mwi *mwi;
213 char obj_name[PJ_MAX_OBJ_NAME];
214 pj_status_t status;
215
216 /* Check arguments */
217 PJ_ASSERT_RETURN(dlg && rdata && p_evsub, PJ_EINVAL);
218
219 /* Must be request message */
220 PJ_ASSERT_RETURN(rdata->msg_info.msg->type == PJSIP_REQUEST_MSG,
221 PJSIP_ENOTREQUESTMSG);
222
223 /* Check that request is SUBSCRIBE */
224 PJ_ASSERT_RETURN(pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
225 &pjsip_subscribe_method)==0,
226 PJSIP_SIMPLE_ENOTSUBSCRIBE);
227
228 /* Check that Event header contains "mwi" */
229 event = (pjsip_event_hdr*)
230 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &STR_EVENT, NULL);
231 if (!event) {
232 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_REQUEST);
233 }
234 if (pj_stricmp(&event->event_type, &STR_MWI) != 0) {
235 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_EVENT);
236 }
237
238 /* Check that request contains compatible Accept header. */
239 accept = (pjsip_accept_hdr*)
240 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, NULL);
241 if (accept) {
242 unsigned i;
243 for (i=0; i<accept->count; ++i) {
244 if (pj_stricmp(&accept->values[i], &STR_APP_SIMPLE_SMS)==0) {
245 break;
246 }
247 }
248
249 if (i==accept->count) {
250 /* Nothing is acceptable */
251 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE);
252 }
253
254 } else {
255 /* No Accept header.
256 * Assume client supports "application/simple-message-summary"
257 */
258 }
259
260 /* Lock dialog */
261 pjsip_dlg_inc_lock(dlg);
262
263
264 /* Create server subscription */
265 status = pjsip_evsub_create_uas( dlg, &mwi_user, rdata, 0, &sub);
266 if (status != PJ_SUCCESS)
267 goto on_return;
268
269 /* Create server mwi subscription */
270 mwi = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_mwi);
271 mwi->dlg = dlg;
272 mwi->sub = sub;
273 if (user_cb)
274 pj_memcpy(&mwi->user_cb, user_cb, sizeof(pjsip_evsub_user));
275
276 pj_ansi_snprintf(obj_name, PJ_MAX_OBJ_NAME, "mwibd%p", dlg->pool);
277 mwi->body_pool = pj_pool_create(dlg->pool->factory, obj_name,
278 512, 512, NULL);
279
280 /* Attach to evsub */
281 pjsip_evsub_set_mod_data(sub, mod_mwi.id, mwi);
282
283 /* Done: */
284 *p_evsub = sub;
285
286 on_return:
287 pjsip_dlg_dec_lock(dlg);
288 return status;
289 }
290
291
292 /*
293 * Forcefully terminate mwi.
294 */
295 PJ_DEF(pj_status_t) pjsip_mwi_terminate( pjsip_evsub *sub,
296 pj_bool_t notify )
297 {
298 return pjsip_evsub_terminate(sub, notify);
299 }
300
301 /*
302 * Create SUBSCRIBE
303 */
304 PJ_DEF(pj_status_t) pjsip_mwi_initiate( pjsip_evsub *sub,
305 pj_uint32_t expires,
306 pjsip_tx_data **p_tdata)
307 {
308 return pjsip_evsub_initiate(sub, &pjsip_subscribe_method, expires,
309 p_tdata);
310 }
311
312
313 /*
314 * Accept incoming subscription.
315 */
316 PJ_DEF(pj_status_t) pjsip_mwi_accept( pjsip_evsub *sub,
317 pjsip_rx_data *rdata,
318 int st_code,
319 const pjsip_hdr *hdr_list )
320 {
321 return pjsip_evsub_accept( sub, rdata, st_code, hdr_list );
322 }
323
324 /*
325 * Create message body and attach it to the (NOTIFY) request.
326 */
327 static pj_status_t mwi_create_msg_body( pjsip_mwi *mwi,
328 pjsip_tx_data *tdata)
329 {
330 pjsip_msg_body *body;
331 pj_str_t dup_text;
332
333 PJ_ASSERT_RETURN(mwi->mime_type.type.slen && mwi->body.slen, PJ_EINVALIDOP);
334
335 /* Clone the message body and mime type */
336 pj_strdup(tdata->pool, &dup_text, &mwi->body);
337
338 /* Create the message body */
339 body = PJ_POOL_ZALLOC_T(tdata->pool, pjsip_msg_body);
340 pjsip_media_type_cp(tdata->pool, &body->content_type, &mwi->mime_type);
341 body->data = dup_text.ptr;
342 body->len = (unsigned)dup_text.slen;
343 body->print_body = &pjsip_print_text_body;
344 body->clone_data = &pjsip_clone_text_data;
345
346 /* Attach to tdata */
347 tdata->msg->body = body;
348
349 return PJ_SUCCESS;
350 }
351
352
353 /*
354 * Create NOTIFY
355 */
356 PJ_DEF(pj_status_t) pjsip_mwi_notify( pjsip_evsub *sub,
357 pjsip_evsub_state state,
358 const pj_str_t *state_str,
359 const pj_str_t *reason,
360 const pjsip_media_type *mime_type,
361 const pj_str_t *body,
362 pjsip_tx_data **p_tdata)
363 {
364 pjsip_mwi *mwi;
365 pjsip_tx_data *tdata;
366 pj_status_t status;
367
368 /* Check arguments. */
369 PJ_ASSERT_RETURN(sub && p_tdata, PJ_EINVAL);
370
371 /* Get the mwi object. */
372 mwi = (pjsip_mwi*) pjsip_evsub_get_mod_data(sub, mod_mwi.id);
373 PJ_ASSERT_RETURN(mwi != NULL, PJ_EINVALIDOP);
374
375 /* Make sure both mime_type & body are-being/have-been set. */
376 PJ_ASSERT_RETURN((mime_type && body) ||
377 (!body &&
378 mwi->mime_type.type.slen && mwi->body.slen),
379 PJ_EINVAL);
380
381 /* Lock object. */
382 pjsip_dlg_inc_lock(mwi->dlg);
383
384 /* Create the NOTIFY request. */
385 status = pjsip_evsub_notify( sub, state, state_str, reason, &tdata);
386 if (status != PJ_SUCCESS)
387 goto on_return;
388
389 /* Update the cached message body */
390 if (mime_type && body) {
391 pj_pool_reset(mwi->body_pool);
392 pjsip_media_type_cp(mwi->body_pool, &mwi->mime_type, mime_type);
393 pj_strdup(mwi->body_pool, &mwi->body, body);
394 }
395
396 /* Create message body */
397 status = mwi_create_msg_body( mwi, tdata );
398 if (status != PJ_SUCCESS)
399 goto on_return;
400
401 /* Done. */
402 *p_tdata = tdata;
403
404 on_return:
405 pjsip_dlg_dec_lock(mwi->dlg);
406 return status;
407 }
408
409
410 /*
411 * Create NOTIFY that reflect current state.
412 */
413 PJ_DEF(pj_status_t) pjsip_mwi_current_notify( pjsip_evsub *sub,
414 pjsip_tx_data **p_tdata )
415 {
416 pjsip_mwi *mwi;
417 pjsip_tx_data *tdata;
418 pj_status_t status;
419
420 /* Check arguments. */
421 PJ_ASSERT_RETURN(sub && p_tdata, PJ_EINVAL);
422
423 /* Get the mwi object. */
424 mwi = (pjsip_mwi*) pjsip_evsub_get_mod_data(sub, mod_mwi.id);
425 PJ_ASSERT_RETURN(mwi != NULL, PJ_EINVALIDOP);
426
427 /* Lock object. */
428 pjsip_dlg_inc_lock(mwi->dlg);
429
430 /* Create the NOTIFY request. */
431 status = pjsip_evsub_current_notify( sub, &tdata);
432 if (status != PJ_SUCCESS)
433 goto on_return;
434
435
436 /* Create message body to reflect the mwi status. */
437 status = mwi_create_msg_body( mwi, tdata );
438 if (status != PJ_SUCCESS)
439 goto on_return;
440
441 /* Done. */
442 *p_tdata = tdata;
443
444 on_return:
445 pjsip_dlg_dec_lock(mwi->dlg);
446 return status;
447 }
448
449
450 /*
451 * Send request.
452 */
453 PJ_DEF(pj_status_t) pjsip_mwi_send_request( pjsip_evsub *sub,
454 pjsip_tx_data *tdata )
455 {
456 return pjsip_evsub_send_request(sub, tdata);
457 }
458
459 /*
460 * This callback is called by event subscription when subscription
461 * state has changed.
462 */
463 static void mwi_on_evsub_state( pjsip_evsub *sub, pjsip_event *event)
464 {
465 pjsip_mwi *mwi;
466
467 mwi = (pjsip_mwi*) pjsip_evsub_get_mod_data(sub, mod_mwi.id);
468 PJ_ASSERT_ON_FAIL(mwi!=NULL, {return;});
469
470 if (mwi->user_cb.on_evsub_state)
471 (*mwi->user_cb.on_evsub_state)(sub, event);
472
473 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
474 if (mwi->body_pool) {
475 pj_pool_release(mwi->body_pool);
476 mwi->body_pool = NULL;
477 }
478 }
479 }
480
481 /*
482 * Called when transaction state has changed.
483 */
484 static void mwi_on_evsub_tsx_state( pjsip_evsub *sub, pjsip_transaction *tsx,
485 pjsip_event *event)
486 {
487 pjsip_mwi *mwi;
488
489 mwi = (pjsip_mwi*) pjsip_evsub_get_mod_data(sub, mod_mwi.id);
490 PJ_ASSERT_ON_FAIL(mwi!=NULL, {return;});
491
492 if (mwi->user_cb.on_tsx_state)
493 (*mwi->user_cb.on_tsx_state)(sub, tsx, event);
494 }
495
496
497 /*
498 * Called when SUBSCRIBE is received.
499 */
500 static void mwi_on_evsub_rx_refresh( pjsip_evsub *sub,
501 pjsip_rx_data *rdata,
502 int *p_st_code,
503 pj_str_t **p_st_text,
504 pjsip_hdr *res_hdr,
505 pjsip_msg_body **p_body)
506 {
507 pjsip_mwi *mwi;
508
509 mwi = (pjsip_mwi*) pjsip_evsub_get_mod_data(sub, mod_mwi.id);
510 PJ_ASSERT_ON_FAIL(mwi!=NULL, {return;});
511
512 if (mwi->user_cb.on_rx_refresh) {
513 (*mwi->user_cb.on_rx_refresh)(sub, rdata, p_st_code, p_st_text,
514 res_hdr, p_body);
515
516 } else {
517 /* Implementors MUST send NOTIFY if it implements on_rx_refresh */
518 pjsip_tx_data *tdata;
519 pj_str_t timeout = { "timeout", 7};
520 pj_status_t status;
521
522 if (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED) {
523 status = pjsip_mwi_notify( sub, PJSIP_EVSUB_STATE_TERMINATED,
524 NULL, &timeout, NULL, NULL, &tdata);
525 } else {
526 status = pjsip_mwi_current_notify(sub, &tdata);
527 }
528
529 if (status == PJ_SUCCESS)
530 pjsip_mwi_send_request(sub, tdata);
531 }
532 }
533
534
535 /*
536 * Called when NOTIFY is received.
537 */
538 static void mwi_on_evsub_rx_notify( pjsip_evsub *sub,
539 pjsip_rx_data *rdata,
540 int *p_st_code,
541 pj_str_t **p_st_text,
542 pjsip_hdr *res_hdr,
543 pjsip_msg_body **p_body)
544 {
545 pjsip_mwi *mwi;
546
547 mwi = (pjsip_mwi*) pjsip_evsub_get_mod_data(sub, mod_mwi.id);
548 PJ_ASSERT_ON_FAIL(mwi!=NULL, {return;});
549
550 /* Just notify application. */
551 if (mwi->user_cb.on_rx_notify) {
552 (*mwi->user_cb.on_rx_notify)(sub, rdata, p_st_code, p_st_text,
553 res_hdr, p_body);
554 }
555 }
556
557 /*
558 * Called when it's time to send SUBSCRIBE.
559 */
560 static void mwi_on_evsub_client_refresh(pjsip_evsub *sub)
561 {
562 pjsip_mwi *mwi;
563
564 mwi = (pjsip_mwi*) pjsip_evsub_get_mod_data(sub, mod_mwi.id);
565 PJ_ASSERT_ON_FAIL(mwi!=NULL, {return;});
566
567 if (mwi->user_cb.on_client_refresh) {
568 (*mwi->user_cb.on_client_refresh)(sub);
569 } else {
570 pj_status_t status;
571 pjsip_tx_data *tdata;
572
573 status = pjsip_mwi_initiate(sub, PJSIP_EXPIRES_NOT_SPECIFIED, &tdata);
574 if (status == PJ_SUCCESS)
575 pjsip_mwi_send_request(sub, tdata);
576 }
577 }
578
579 /*
580 * Called when no refresh is received after the interval.
581 */
582 static void mwi_on_evsub_server_timeout(pjsip_evsub *sub)
583 {
584 pjsip_mwi *mwi;
585
586 mwi = (pjsip_mwi*) pjsip_evsub_get_mod_data(sub, mod_mwi.id);
587 PJ_ASSERT_ON_FAIL(mwi!=NULL, {return;});
588
589 if (mwi->user_cb.on_server_timeout) {
590 (*mwi->user_cb.on_server_timeout)(sub);
591 } else {
592 pj_status_t status;
593 pjsip_tx_data *tdata;
594 pj_str_t reason = { "timeout", 7 };
595
596 status = pjsip_mwi_notify(sub, PJSIP_EVSUB_STATE_TERMINATED,
597 NULL, &reason, NULL, NULL, &tdata);
598 if (status == PJ_SUCCESS)
599 pjsip_mwi_send_request(sub, tdata);
600 }
601 }
602