Debian Codegraph @

Xpjproject/pjsip/include/pjsip-simple/evsub.h

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 #ifndef __PJSIP_SIMPLE_EVSUB_H__
20 #define __PJSIP_SIMPLE_EVSUB_H__
21
22 /**
23 * @file evsub.h
24 * @brief SIP Specific Event Notification Extension (RFC 3265)
25 */
26
27 #include <pjsip-simple/types.h>
28
29
30 /**
31 * @defgroup PJSIP_EVENT_NOT SIP Event Notification (RFC 3265) Module
32 * @ingroup PJSIP_SIMPLE
33 * @brief Core Event Subscription framework, used by presence, call transfer, etc.
34 * @{
35 *
36 * This module provides the implementation of SIP Extension for SIP Specific
37 * Event Notification (RFC 3265). It extends PJSIP by supporting SUBSCRIBE and
38 * NOTIFY methods.
39 *
40 * This module itself is extensible; new event packages can be registered to
41 * this module to handle specific extensions (such as presence).
42 */
43
44 PJ_BEGIN_DECL
45
46
47 /**
48 * Opaque type for event subscription session.
49 */
50 typedef struct pjsip_evsub pjsip_evsub;
51
52
53 /**
54 * This enumeration describes basic subscription state as described in the
55 * RFC 3265. The standard specifies that extensions may define additional
56 * states. In the case where the state is not known, the subscription state
57 * will be set to PJSIP_EVSUB_STATE_UNKNOWN, and the token will be kept
58 * in state_str member of the susbcription structure.
59 */
60 typedef enum pjsip_evsub_state
61 {
62 PJSIP_EVSUB_STATE_NULL, /**< State is NULL. */
63 PJSIP_EVSUB_STATE_SENT, /**< Client has sent SUBSCRIBE request. */
64 PJSIP_EVSUB_STATE_ACCEPTED, /**< 2xx response to SUBSCRIBE has been
65 sent/received. */
66 PJSIP_EVSUB_STATE_PENDING, /**< Subscription is pending. */
67 PJSIP_EVSUB_STATE_ACTIVE, /**< Subscription is active. */
68 PJSIP_EVSUB_STATE_TERMINATED,/**< Subscription is terminated. */
69 PJSIP_EVSUB_STATE_UNKNOWN, /**< Subscription state can not be determined.
70 Application can query the state by
71 calling #pjsip_evsub_get_state_name().*/
72 } pjsip_evsub_state;
73
74
75 /**
76 * Some options for the event subscription.
77 */
78 enum
79 {
80 /**
81 * If this flag is set, then outgoing request to create subscription
82 * will not have id in the Event header (e.g. in REFER request). But if
83 * there is an id in the incoming NOTIFY, that id will be used.
84 */
85 PJSIP_EVSUB_NO_EVENT_ID = 1,
86 };
87
88
89 /**
90 * This structure describes callback that is registered by application or
91 * package to receive notifications about subscription events.
92 */
93 struct pjsip_evsub_user
94 {
95 /**
96 * This callback is called when subscription state has changed.
97 * Application MUST be prepared to receive NULL event and events with
98 * type other than PJSIP_EVENT_TSX_STATE
99 *
100 * This callback is OPTIONAL.
101 *
102 * @param sub The subscription instance.
103 * @param event The event that has caused the state to change,
104 * which may be NULL or may have type other than
105 * PJSIP_EVENT_TSX_STATE.
106 */
107 void (*on_evsub_state)( pjsip_evsub *sub, pjsip_event *event);
108
109 /**
110 * This callback is called when transaction state has changed.
111 *
112 * @param sub The subscription instance.
113 * @param tsx Transaction.
114 * @param event The event.
115 */
116 void (*on_tsx_state)(pjsip_evsub *sub, pjsip_transaction *tsx,
117 pjsip_event *event);
118
119 /**
120 * This callback is called when incoming SUBSCRIBE (or any method that
121 * establishes the subscription in the first place) is received. It
122 * allows application to specify what response should be sent to
123 * remote, along with additional headers and message body to be put
124 * in the response.
125 *
126 * This callback is only applicable and required for UAS.
127 *
128 * Upon receiving this callback, implementation MUST send NOTIFY request.
129 * The suggested behavior is to call #pjsip_evsub_current_notify(),
130 * since this function takes care about unsubscription request and
131 * calculates the appropriate expiration interval.
132 *
133 * @param sub The subscription instance.
134 * @param rdata The received SUBSCRIBE request.
135 * @param p_st_code Application MUST set the value of this argument with
136 * final status code (200-699) upon returning from the
137 * callback. Only applicable for refresh request. For
138 * unsubscription, the library will always reply with
139 * 200.
140 * @param p_st_text Custom status text, if any.
141 * @param res_hdr Upon return, application can put additional headers
142 * to be sent in the response in this list.
143 * @param p_body Application MAY specify message body to be sent in
144 * the response.
145 */
146 void (*on_rx_refresh)( pjsip_evsub *sub,
147 pjsip_rx_data *rdata,
148 int *p_st_code,
149 pj_str_t **p_st_text,
150 pjsip_hdr *res_hdr,
151 pjsip_msg_body **p_body);
152
153 /**
154 * This callback is called when client/subscriber received incoming
155 * NOTIFY request. It allows the application to specify what response
156 * should be sent to remote, along with additional headers and message
157 * body to be put in the response.
158 *
159 * This callback is OPTIONAL. When it is not implemented, the default
160 * behavior is to respond incoming NOTIFY request with 200 (OK).
161 *
162 * @param sub The subscription instance.
163 * @param rdata The received NOTIFY request.
164 * @param p_st_code Application MUST set the value of this argument with
165 * final status code (200-699) upon returning from the
166 * callback.
167 * @param p_st_text Custom status text, if any.
168 * @param res_hdr Upon return, application can put additional headers
169 * to be sent in the response in this list.
170 * @param p_body Application MAY specify message body to be sent in
171 * the response.
172 */
173 void (*on_rx_notify)(pjsip_evsub *sub,
174 pjsip_rx_data *rdata,
175 int *p_st_code,
176 pj_str_t **p_st_text,
177 pjsip_hdr *res_hdr,
178 pjsip_msg_body **p_body);
179
180 /**
181 * This callback is called when it is time for the client to refresh
182 * the subscription.
183 *
184 * This callback is OPTIONAL when PJSIP package such as presence or
185 * refer is used; the event package will refresh subscription by sending
186 * SUBSCRIBE with the interval set to current/last interval.
187 *
188 * @param sub The subscription instance.
189 */
190 void (*on_client_refresh)(pjsip_evsub *sub);
191
192 /**
193 * This callback is called when server doesn't receive subscription
194 * refresh after the specified subscription interval.
195 *
196 * This callback is OPTIONAL when PJSIP package such as presence or
197 * refer is used; the event package send NOTIFY to terminate the
198 * subscription.
199 */
200 void (*on_server_timeout)(pjsip_evsub *sub);
201
202 };
203
204
205 /**
206 * @see pjsip_evsub_user
207 */
208 typedef struct pjsip_evsub_user pjsip_evsub_user;
209
210
211 /**
212 * SUBSCRIBE method constant. @see pjsip_get_subscribe_method()
213 */
214 PJ_DECL_DATA(const pjsip_method) pjsip_subscribe_method;
215
216 /**
217 * NOTIFY method constant. @see pjsip_get_notify_method()
218 */
219 PJ_DECL_DATA(const pjsip_method) pjsip_notify_method;
220
221 /**
222 * SUBSCRIBE method constant.
223 */
224 PJ_DECL(const pjsip_method*) pjsip_get_subscribe_method(void);
225
226 /**
227 * NOTIFY method constant.
228 */
229 PJ_DECL(const pjsip_method*) pjsip_get_notify_method(void);
230
231
232 /**
233 * Initialize the event subscription module and register the module to the
234 * specified endpoint.
235 *
236 * @param endpt The endpoint instance.
237 *
238 * @return PJ_SUCCESS if module can be created and registered
239 * successfully.
240 */
241 PJ_DECL(pj_status_t) pjsip_evsub_init_module(pjsip_endpoint *endpt);
242
243
244 /**
245 * Get the event subscription module instance that was previously created
246 * and registered to endpoint.
247 *
248 * @return The event subscription module instance.
249 */
250 PJ_DECL(pjsip_module*) pjsip_evsub_instance(void);
251
252
253 /**
254 * Register event package to the event subscription framework.
255 *
256 * @param pkg_mod The module that implements the event package being
257 * registered.
258 * @param event_name Event package identification.
259 * @param expires Default subscription expiration time, in seconds.
260 * @param accept_cnt Number of strings in Accept array. The value must
261 * not be greater than PJSIP_GENERIC_ARRAY_MAX_COUNT.
262 * @param accept Array of Accept value.
263 *
264 * @return PJ_SUCCESS on success.
265 */
266 PJ_DECL(pj_status_t) pjsip_evsub_register_pkg( pjsip_module *pkg_mod,
267 const pj_str_t *event_name,
268 unsigned expires,
269 unsigned accept_cnt,
270 const pj_str_t accept[]);
271
272 /**
273 * Get the Allow-Events header. This header is built based on the packages
274 * that are registered to the evsub module.
275 *
276 * @param m Pointer to event subscription module instance, or
277 * NULL to use default instance (equal to
278 * #pjsip_evsub_instance()).
279 *
280 * @return The Allow-Events header.
281 */
282 PJ_DECL(const pjsip_hdr*) pjsip_evsub_get_allow_events_hdr(
283 const pjsip_module *m);
284
285
286 /**
287 * Create client subscription session.
288 *
289 * @param dlg The underlying dialog to use.
290 * @param user_cb Callback to receive event subscription notifications.
291 * @param event Event name.
292 * @param option Bitmask of options.
293 * @param p_evsub Pointer to receive event subscription instance.
294 *
295 * @return PJ_SUCCESS on success.
296 */
297 PJ_DECL(pj_status_t) pjsip_evsub_create_uac( pjsip_dialog *dlg,
298 const pjsip_evsub_user *user_cb,
299 const pj_str_t *event,
300 unsigned option,
301 pjsip_evsub **p_evsub);
302
303 /**
304 * Create server subscription session.
305 *
306 * @param dlg The underlying dialog to use.
307 * @param user_cb Callback to receive event subscription notifications.
308 * @param rdata The incoming request that creates the event
309 * subscription, such as SUBSCRIBE or REFER.
310 * @param option Bitmask of options.
311 * @param p_evsub Pointer to receive event subscription instance.
312 *
313 * @return PJ_SUCCESS on success.
314 */
315 PJ_DECL(pj_status_t) pjsip_evsub_create_uas( pjsip_dialog *dlg,
316 const pjsip_evsub_user *user_cb,
317 pjsip_rx_data *rdata,
318 unsigned option,
319 pjsip_evsub **p_evsub);
320
321 /**
322 * Forcefully destroy the subscription session. This function should only
323 * be called on special condition, such as when the subscription
324 * initialization has failed. For other conditions, application MUST terminate
325 * the subscription by sending the appropriate un(SUBSCRIBE) or NOTIFY.
326 *
327 * @param sub The event subscription.
328 * @param notify Specify whether the state notification callback
329 * should be called.
330 *
331 * @return PJ_SUCCESS if subscription session has been destroyed.
332 */
333 PJ_DECL(pj_status_t) pjsip_evsub_terminate( pjsip_evsub *sub,
334 pj_bool_t notify );
335
336
337 /**
338 * Get subscription state.
339 *
340 * @param sub Event subscription instance.
341 *
342 * @return Subscription state.
343 */
344 PJ_DECL(pjsip_evsub_state) pjsip_evsub_get_state(const pjsip_evsub *sub);
345
346
347 /**
348 * Get the string representation of the subscription state.
349 *
350 * @param sub Event subscription instance.
351 *
352 * @return NULL terminated string.
353 */
354 PJ_DECL(const char*) pjsip_evsub_get_state_name(const pjsip_evsub *sub);
355
356
357 /**
358 * Get subscription termination reason, if any. If remote did not
359 * send termination reason, this function will return empty string.
360 *
361 * @param sub Event subscription instance.
362 *
363 * @return NULL terminated string.
364 */
365 PJ_DECL(const pj_str_t*) pjsip_evsub_get_termination_reason(
366 const pjsip_evsub *sub);
367
368
369 /**
370 * Get subscription expiration time.
371 *
372 * @param sub Event subscription instance.
373 *
374 * @return Subscription expiration time, in seconds.
375 */
376 PJ_DECL(pj_uint32_t) pjsip_evsub_get_expires(const pjsip_evsub *sub);
377
378
379 /**
380 * Call this function to create request to initiate subscription, to
381 * refresh subcription, or to request subscription termination.
382 *
383 * @param sub Client subscription instance.
384 * @param method The method that establishes the subscription, such as
385 * SUBSCRIBE or REFER. If this argument is NULL, then
386 * SUBSCRIBE will be used.
387 * @param expires Subscription expiration. If the value is set to zero,
388 * this will request unsubscription. If the value is
389 * PJSIP_EXPIRES_NOT_SPECIFIED, default expiration
390 * as defined by the package will be used.
391 * @param p_tdata Pointer to receive the request.
392 *
393 * @return PJ_SUCCESS on success.
394 */
395 PJ_DECL(pj_status_t) pjsip_evsub_initiate( pjsip_evsub *sub,
396 const pjsip_method *method,
397 pj_uint32_t expires,
398 pjsip_tx_data **p_tdata);
399
400
401 /**
402 * Add a list of headers to the subscription instance. The list of headers
403 * will be added to outgoing presence subscription requests.
404 *
405 * @param sub Subscription instance.
406 * @param hdr_list List of headers to be added.
407 *
408 * @return PJ_SUCCESS on success.
409 */
410 PJ_DECL(pj_status_t) pjsip_evsub_add_header( pjsip_evsub *sub,
411 const pjsip_hdr *hdr_list );
412
413
414 /**
415 * Accept the incoming subscription request by sending 2xx response to
416 * incoming SUBSCRIBE request.
417 *
418 * @param sub Server subscription instance.
419 * @param rdata The incoming subscription request message.
420 * @param st_code Status code, which MUST be final response.
421 * @param hdr_list Optional list of headers to be added in the response.
422 *
423 * @return PJ_SUCCESS on success.
424 */
425 PJ_DECL(pj_status_t) pjsip_evsub_accept( pjsip_evsub *sub,
426 pjsip_rx_data *rdata,
427 int st_code,
428 const pjsip_hdr *hdr_list );
429
430
431 /**
432 * For notifier, create NOTIFY request to subscriber, and set the state
433 * of the subscription.
434 *
435 * @param sub The server subscription (notifier) instance.
436 * @param state New state to set.
437 * @param state_str The state string name, if state contains value other
438 * than active, pending, or terminated. Otherwise this
439 * argument is ignored.
440 * @param reason Specify reason if new state is terminated, otherwise
441 * put NULL.
442 * @param p_tdata Pointer to receive request message.
443 *
444 * @return PJ_SUCCESS on success.
445 */
446 PJ_DECL(pj_status_t) pjsip_evsub_notify( pjsip_evsub *sub,
447 pjsip_evsub_state state,
448 const pj_str_t *state_str,
449 const pj_str_t *reason,
450 pjsip_tx_data **p_tdata);
451
452
453 /**
454 * For notifier, create a NOTIFY request that reflects current subscription
455 * status.
456 *
457 * @param sub The server subscription instance.
458 * @param p_tdata Pointer to receive the request messge.
459 *
460 * @return PJ_SUCCESS on success.
461 */
462 PJ_DECL(pj_status_t) pjsip_evsub_current_notify( pjsip_evsub *sub,
463 pjsip_tx_data **p_tdata );
464
465
466
467 /**
468 * Send request message that was previously created with initiate(), notify(),
469 * or current_notify(). Application may also send request created with other
470 * functions, e.g. authentication. But the request MUST be either request
471 * that creates/refresh subscription or NOTIFY request.
472 *
473 * @param sub The event subscription object.
474 * @param tdata Request message to be send.
475 *
476 * @return PJ_SUCCESS on success.
477 */
478 PJ_DECL(pj_status_t) pjsip_evsub_send_request( pjsip_evsub *sub,
479 pjsip_tx_data *tdata);
480
481
482
483 /**
484 * Get the event subscription instance associated with the specified
485 * transaction.
486 *
487 * @param tsx The transaction.
488 *
489 * @return The event subscription instance registered in the
490 * transaction, if any.
491 */
492 PJ_DECL(pjsip_evsub*) pjsip_tsx_get_evsub(const pjsip_transaction *tsx);
493
494
495 /**
496 * Set event subscription's module data.
497 *
498 * @param sub The event subscription.
499 * @param mod_id The module id.
500 * @param data Arbitrary data.
501 */
502 PJ_DECL(void) pjsip_evsub_set_mod_data( pjsip_evsub *sub, unsigned mod_id,
503 void *data );
504
505
506 /**
507 * Get event subscription's module data.
508 *
509 * @param sub The event subscription.
510 * @param mod_id The module id.
511 *
512 * @return Data previously set at the specified id.
513 */
514 PJ_DECL(void*) pjsip_evsub_get_mod_data( const pjsip_evsub *sub,
515 unsigned mod_id );
516
517
518 /**
519 * Increment the event subscription's group lock.
520 *
521 * @param sub The server subscription instance.
522 *
523 * @return PJ_SUCCESS on success.
524 */
525 PJ_DEF(pj_status_t) pjsip_evsub_add_ref(pjsip_evsub *sub);
526
527
528 /**
529 * Decrement the event subscription's group lock.
530 *
531 * @param sub The server subscription instance.
532 *
533 * @return PJ_SUCCESS on success.
534 */
535 PJ_DEF(pj_status_t) pjsip_evsub_dec_ref(pjsip_evsub *sub);
536
537
538 /**
539 * Sets, resets, or cancels the UAS subscription timeout.
540 * If there is an existing timer, it is cancelled before any
541 * other action. A timeout of 0 is ignored except that any
542 * existing timer is cancelled.
543 *
544 * The API is intended to be used to restore UAS' subscription
545 * timer from backup in the case of failure, and should not be
546 * used to modify an ongoing subscription's timeout.
547 *
548 * @param sub The server subscription instance.
549 * @param seconds The new timeout.
550 */
551 PJ_DEF(void) pjsip_evsub_uas_set_timeout(pjsip_evsub *sub,
552 pj_uint32_t seconds);
553
554
555 PJ_END_DECL
556
557 /**
558 * @}
559 */
560
561 #endif /* __PJSIP_SIMPLE_EVSUB_H__ */