blob: 1c350c99cfd3c331b882d5149bd2742e6c6691a8 [file] [log] [blame]
Benny Prijono834aee32006-02-19 01:38:06 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono834aee32006-02-19 01:38:06 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#ifndef __PJSIP_SIMPLE_EVSUB_H__
21#define __PJSIP_SIMPLE_EVSUB_H__
22
23/**
Benny Prijonoa7b376b2008-01-25 16:06:33 +000024 * @file evsub.h
Benny Prijono834aee32006-02-19 01:38:06 +000025 * @brief SIP Specific Event Notification Extension (RFC 3265)
26 */
27
28#include <pjsip-simple/types.h>
29
30
31/**
32 * @defgroup PJSIP_EVENT_NOT SIP Event Notification (RFC 3265) Module
33 * @ingroup PJSIP_SIMPLE
Benny Prijono312aff92006-06-17 04:08:30 +000034 * @brief Core Event Subscription framework, used by presence, call transfer, etc.
Benny Prijono834aee32006-02-19 01:38:06 +000035 * @{
36 *
37 * This module provides the implementation of SIP Extension for SIP Specific
38 * Event Notification (RFC 3265). It extends PJSIP by supporting SUBSCRIBE and
39 * NOTIFY methods.
40 *
41 * This module itself is extensible; new event packages can be registered to
42 * this module to handle specific extensions (such as presence).
43 */
44
45PJ_BEGIN_DECL
46
47
48/**
49 * Opaque type for event subscription session.
50 */
51typedef struct pjsip_evsub pjsip_evsub;
52
53
54/**
55 * This enumeration describes basic subscription state as described in the
56 * RFC 3265. The standard specifies that extensions may define additional
57 * states. In the case where the state is not known, the subscription state
58 * will be set to PJSIP_EVSUB_STATE_UNKNOWN, and the token will be kept
59 * in state_str member of the susbcription structure.
60 */
61enum pjsip_evsub_state
62{
63 PJSIP_EVSUB_STATE_NULL, /**< State is NULL. */
64 PJSIP_EVSUB_STATE_SENT, /**< Client has sent SUBSCRIBE request. */
65 PJSIP_EVSUB_STATE_ACCEPTED, /**< 2xx response to SUBSCRIBE has been
66 sent/received. */
67 PJSIP_EVSUB_STATE_PENDING, /**< Subscription is pending. */
68 PJSIP_EVSUB_STATE_ACTIVE, /**< Subscription is active. */
69 PJSIP_EVSUB_STATE_TERMINATED,/**< Subscription is terminated. */
70 PJSIP_EVSUB_STATE_UNKNOWN, /**< Subscription state can not be determined.
71 Application can query the state by
72 calling #pjsip_evsub_get_state_name().*/
73};
74
75/**
76 * @see pjsip_evsub_state
77 */
78typedef enum pjsip_evsub_state pjsip_evsub_state;
79
80
Benny Prijono26ff9062006-02-21 23:47:00 +000081/**
82 * Some options for the event subscription.
83 */
84enum
85{
86 /**
87 * If this flag is set, then outgoing request to create subscription
88 * will not have id in the Event header (e.g. in REFER request). But if
89 * there is an id in the incoming NOTIFY, that id will be used.
90 */
91 PJSIP_EVSUB_NO_EVENT_ID = 1,
92};
93
Benny Prijono834aee32006-02-19 01:38:06 +000094
95/**
96 * This structure describes callback that is registered by application or
97 * package to receive notifications about subscription events.
98 */
99struct pjsip_evsub_user
100{
101 /**
102 * This callback is called when subscription state has changed.
103 * Application MUST be prepared to receive NULL event and events with
104 * type other than PJSIP_EVENT_TSX_STATE
105 *
106 * This callback is OPTIONAL.
107 *
108 * @param sub The subscription instance.
109 * @param event The event that has caused the state to change,
110 * which may be NULL or may have type other than
111 * PJSIP_EVENT_TSX_STATE.
112 */
113 void (*on_evsub_state)( pjsip_evsub *sub, pjsip_event *event);
114
115 /**
116 * This callback is called when transaction state has changed.
117 *
118 * @param sub The subscription instance.
119 * @param tsx Transaction.
120 * @param event The event.
121 */
122 void (*on_tsx_state)(pjsip_evsub *sub, pjsip_transaction *tsx,
123 pjsip_event *event);
124
125 /**
126 * This callback is called when incoming SUBSCRIBE (or any method that
127 * establishes the subscription in the first place) is received. It
128 * allows application to specify what response should be sent to
129 * remote, along with additional headers and message body to be put
130 * in the response.
131 *
132 * This callback is OPTIONAL.
133 *
134 * However, implementation MUST send NOTIFY request upon receiving this
135 * callback. The suggested behavior is to call
Benny Prijonoa7b376b2008-01-25 16:06:33 +0000136 * #pjsip_evsub_current_notify(), since this function takes care
Benny Prijono834aee32006-02-19 01:38:06 +0000137 * about unsubscription request and calculates the appropriate expiration
138 * interval.
139 */
140 void (*on_rx_refresh)( pjsip_evsub *sub,
141 pjsip_rx_data *rdata,
142 int *p_st_code,
143 pj_str_t **p_st_text,
144 pjsip_hdr *res_hdr,
145 pjsip_msg_body **p_body);
146
147 /**
148 * This callback is called when client/subscriber received incoming
149 * NOTIFY request. It allows the application to specify what response
150 * should be sent to remote, along with additional headers and message
151 * body to be put in the response.
152 *
153 * This callback is OPTIONAL. When it is not implemented, the default
154 * behavior is to respond incoming NOTIFY request with 200 (OK).
155 *
156 * @param sub The subscription instance.
157 * @param rdata The received NOTIFY request.
158 * @param p_st_code Application MUST set the value of this argument with
159 * final status code (200-699) upon returning from the
160 * callback.
161 * @param p_st_text Custom status text, if any.
162 * @param res_hdr Upon return, application can put additional headers
163 * to be sent in the response in this list.
164 * @param p_body Application MAY specify message body to be sent in
165 * the response.
166 */
167 void (*on_rx_notify)(pjsip_evsub *sub,
168 pjsip_rx_data *rdata,
169 int *p_st_code,
170 pj_str_t **p_st_text,
171 pjsip_hdr *res_hdr,
172 pjsip_msg_body **p_body);
173
174 /**
175 * This callback is called when it is time for the client to refresh
176 * the subscription.
177 *
Benny Prijono8c715612006-02-25 02:04:42 +0000178 * This callback is OPTIONAL when PJSIP package such as presence or
179 * refer is used; the event package will refresh subscription by sending
180 * SUBSCRIBE with the interval set to current/last interval.
Benny Prijono834aee32006-02-19 01:38:06 +0000181 *
182 * @param sub The subscription instance.
183 */
184 void (*on_client_refresh)(pjsip_evsub *sub);
185
186 /**
187 * This callback is called when server doesn't receive subscription
188 * refresh after the specified subscription interval.
189 *
Benny Prijono8c715612006-02-25 02:04:42 +0000190 * This callback is OPTIONAL when PJSIP package such as presence or
191 * refer is used; the event package send NOTIFY to terminate the
192 * subscription.
Benny Prijono834aee32006-02-19 01:38:06 +0000193 */
194 void (*on_server_timeout)(pjsip_evsub *sub);
195
196};
197
198
199/**
200 * @see pjsip_evsub_user
201 */
202typedef struct pjsip_evsub_user pjsip_evsub_user;
203
204
205/**
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000206 * SUBSCRIBE method constant. @see pjsip_get_subscribe_method()
207 */
208PJ_DECL_DATA(const pjsip_method) pjsip_subscribe_method;
209
210/**
211 * NOTIFY method constant. @see pjsip_get_notify_method()
212 */
213PJ_DECL_DATA(const pjsip_method) pjsip_notify_method;
214
215/**
Benny Prijono834aee32006-02-19 01:38:06 +0000216 * SUBSCRIBE method constant.
217 */
Benny Prijono90cc76e2008-07-11 00:56:07 +0000218PJ_DECL(const pjsip_method*) pjsip_get_subscribe_method(void);
Benny Prijono834aee32006-02-19 01:38:06 +0000219
220/**
221 * NOTIFY method constant.
222 */
Benny Prijono90cc76e2008-07-11 00:56:07 +0000223PJ_DECL(const pjsip_method*) pjsip_get_notify_method(void);
Benny Prijono834aee32006-02-19 01:38:06 +0000224
225
226/**
227 * Initialize the event subscription module and register the module to the
228 * specified endpoint.
229 *
230 * @param endpt The endpoint instance.
231 *
232 * @return PJ_SUCCESS if module can be created and registered
233 * successfully.
234 */
235PJ_DECL(pj_status_t) pjsip_evsub_init_module(pjsip_endpoint *endpt);
236
237
238/**
239 * Get the event subscription module instance that was previously created
240 * and registered to endpoint.
241 *
242 * @return The event subscription module instance.
243 */
244PJ_DECL(pjsip_module*) pjsip_evsub_instance(void);
245
246
247/**
248 * Register event package to the event subscription framework.
249 *
250 * @param pkg_mod The module that implements the event package being
251 * registered.
252 * @param event_name Event package identification.
253 * @param expires Default subscription expiration time, in seconds.
254 * @param accept_cnt Number of strings in Accept array.
255 * @param accept Array of Accept value.
256 *
257 * @return PJ_SUCCESS on success.
258 */
259PJ_DECL(pj_status_t) pjsip_evsub_register_pkg( pjsip_module *pkg_mod,
260 const pj_str_t *event_name,
261 unsigned expires,
262 unsigned accept_cnt,
263 const pj_str_t accept[]);
264
Benny Prijono56315612006-07-18 14:39:40 +0000265/**
266 * Get the Allow-Events header. This header is built based on the packages
267 * that are registered to the evsub module.
268 *
269 * @param m Pointer to event subscription module instance, or
270 * NULL to use default instance (equal to
271 * #pjsip_evsub_instance()).
272 *
273 * @return The Allow-Events header.
274 */
275PJ_DECL(const pjsip_hdr*) pjsip_evsub_get_allow_events_hdr(pjsip_module *m);
276
Benny Prijono834aee32006-02-19 01:38:06 +0000277
278/**
279 * Create client subscription session.
280 *
281 * @param dlg The underlying dialog to use.
282 * @param user_cb Callback to receive event subscription notifications.
283 * @param event Event name.
Benny Prijono26ff9062006-02-21 23:47:00 +0000284 * @param option Bitmask of options.
Benny Prijono834aee32006-02-19 01:38:06 +0000285 * @param p_evsub Pointer to receive event subscription instance.
286 *
287 * @return PJ_SUCCESS on success.
288 */
289PJ_DECL(pj_status_t) pjsip_evsub_create_uac( pjsip_dialog *dlg,
290 const pjsip_evsub_user *user_cb,
291 const pj_str_t *event,
Benny Prijono26ff9062006-02-21 23:47:00 +0000292 unsigned option,
Benny Prijono834aee32006-02-19 01:38:06 +0000293 pjsip_evsub **p_evsub);
294
295/**
296 * Create server subscription session.
297 *
298 * @param dlg The underlying dialog to use.
299 * @param user_cb Callback to receive event subscription notifications.
300 * @param rdata The incoming request that creates the event
301 * subscription, such as SUBSCRIBE or REFER.
Benny Prijono26ff9062006-02-21 23:47:00 +0000302 * @param option Bitmask of options.
Benny Prijono834aee32006-02-19 01:38:06 +0000303 * @param p_evsub Pointer to receive event subscription instance.
304 *
305 * @return PJ_SUCCESS on success.
306 */
307PJ_DECL(pj_status_t) pjsip_evsub_create_uas( pjsip_dialog *dlg,
308 const pjsip_evsub_user *user_cb,
309 pjsip_rx_data *rdata,
Benny Prijono26ff9062006-02-21 23:47:00 +0000310 unsigned option,
Benny Prijono834aee32006-02-19 01:38:06 +0000311 pjsip_evsub **p_evsub);
312
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000313/**
314 * Forcefully destroy the subscription session. This function should only
315 * be called on special condition, such as when the subscription
316 * initialization has failed. For other conditions, application MUST terminate
317 * the subscription by sending the appropriate un(SUBSCRIBE) or NOTIFY.
318 *
319 * @param sub The event subscription.
320 * @param notify Specify whether the state notification callback
321 * should be called.
322 *
323 * @return PJ_SUCCESS if subscription session has been destroyed.
324 */
325PJ_DECL(pj_status_t) pjsip_evsub_terminate( pjsip_evsub *sub,
326 pj_bool_t notify );
327
Benny Prijono834aee32006-02-19 01:38:06 +0000328
329/**
330 * Get subscription state.
331 *
332 * @param sub Event subscription instance.
333 *
334 * @return Subscription state.
335 */
336PJ_DECL(pjsip_evsub_state) pjsip_evsub_get_state(pjsip_evsub *sub);
337
338
339/**
340 * Get the string representation of the subscription state.
341 *
342 * @param sub Event subscription instance.
343 *
344 * @return NULL terminated string.
345 */
346PJ_DECL(const char*) pjsip_evsub_get_state_name(pjsip_evsub *sub);
347
348
349/**
Benny Prijono75130572008-07-17 13:53:41 +0000350 * Get subscription termination reason, if any. If remote did not
351 * send termination reason, this function will return empty string.
352 *
353 * @param sub Event subscription instance.
354 *
355 * @return NULL terminated string.
356 */
357PJ_DECL(const pj_str_t*) pjsip_evsub_get_termination_reason(pjsip_evsub *sub);
358
359
360/**
Benny Prijono834aee32006-02-19 01:38:06 +0000361 * Call this function to create request to initiate subscription, to
362 * refresh subcription, or to request subscription termination.
363 *
364 * @param sub Client subscription instance.
365 * @param method The method that establishes the subscription, such as
366 * SUBSCRIBE or REFER. If this argument is NULL, then
367 * SUBSCRIBE will be used.
368 * @param expires Subscription expiration. If the value is set to zero,
369 * this will request unsubscription. If the value is
370 * negative, default expiration as defined by the package
371 * will be used.
372 * @param p_tdata Pointer to receive the request.
373 *
374 * @return PJ_SUCCESS on success.
375 */
376PJ_DECL(pj_status_t) pjsip_evsub_initiate( pjsip_evsub *sub,
377 const pjsip_method *method,
378 pj_int32_t expires,
379 pjsip_tx_data **p_tdata);
380
381
382/**
383 * Accept the incoming subscription request by sending 2xx response to
384 * incoming SUBSCRIBE request.
385 *
386 * @param sub Server subscription instance.
387 * @param rdata The incoming subscription request message.
388 * @param st_code Status code, which MUST be final response.
389 * @param hdr_list Optional list of headers to be added in the response.
390 *
391 * @return PJ_SUCCESS on success.
392 */
393PJ_DECL(pj_status_t) pjsip_evsub_accept( pjsip_evsub *sub,
394 pjsip_rx_data *rdata,
395 int st_code,
396 const pjsip_hdr *hdr_list );
397
398
399/**
400 * For notifier, create NOTIFY request to subscriber, and set the state
401 * of the subscription.
402 *
403 * @param sub The server subscription (notifier) instance.
404 * @param state New state to set.
405 * @param state_str The state string name, if state contains value other
406 * than active, pending, or terminated. Otherwise this
407 * argument is ignored.
408 * @param reason Specify reason if new state is terminated, otherwise
409 * put NULL.
410 * @param p_tdata Pointer to receive request message.
411 *
412 * @return PJ_SUCCESS on success.
413 */
414PJ_DECL(pj_status_t) pjsip_evsub_notify( pjsip_evsub *sub,
415 pjsip_evsub_state state,
416 const pj_str_t *state_str,
417 const pj_str_t *reason,
418 pjsip_tx_data **p_tdata);
419
420
421/**
422 * For notifier, create a NOTIFY request that reflects current subscription
423 * status.
424 *
425 * @param sub The server subscription instance.
426 * @param p_tdata Pointer to receive the request messge.
427 *
428 * @return PJ_SUCCESS on success.
429 */
430PJ_DECL(pj_status_t) pjsip_evsub_current_notify( pjsip_evsub *sub,
431 pjsip_tx_data **p_tdata );
432
433
434
435/**
Benny Prijono26ff9062006-02-21 23:47:00 +0000436 * Send request message that was previously created with initiate(), notify(),
437 * or current_notify(). Application may also send request created with other
438 * functions, e.g. authentication. But the request MUST be either request
439 * that creates/refresh subscription or NOTIFY request.
Benny Prijono834aee32006-02-19 01:38:06 +0000440 *
441 * @param sub The event subscription object.
442 * @param tdata Request message to be send.
443 *
444 * @return PJ_SUCCESS on success.
445 */
446PJ_DECL(pj_status_t) pjsip_evsub_send_request( pjsip_evsub *sub,
447 pjsip_tx_data *tdata);
448
449
450
451/**
Benny Prijono8c715612006-02-25 02:04:42 +0000452 * Get the event subscription instance associated with the specified
453 * transaction.
Benny Prijono834aee32006-02-19 01:38:06 +0000454 *
455 * @param tsx The transaction.
456 *
457 * @return The event subscription instance registered in the
458 * transaction, if any.
459 */
460PJ_DECL(pjsip_evsub*) pjsip_tsx_get_evsub(pjsip_transaction *tsx);
461
462
463/**
464 * Set event subscription's module data.
465 *
466 * @param sub The event subscription.
Benny Prijono312aff92006-06-17 04:08:30 +0000467 * @param mod_id The module id.
Benny Prijono834aee32006-02-19 01:38:06 +0000468 * @param data Arbitrary data.
469 */
470PJ_DECL(void) pjsip_evsub_set_mod_data( pjsip_evsub *sub, unsigned mod_id,
471 void *data );
472
473
474/**
475 * Get event subscription's module data.
476 *
477 * @param sub The event subscription.
478 * @param mod_id The module id.
479 *
480 * @return Data previously set at the specified id.
481 */
482PJ_DECL(void*) pjsip_evsub_get_mod_data( pjsip_evsub *sub, unsigned mod_id );
483
484
485
486PJ_END_DECL
487
488/**
489 * @}
490 */
491
492#endif /* __PJSIP_SIMPLE_EVSUB_H__ */