blob: 0bcd63a957357d5832ea47b5aa411ef33955a33d [file] [log] [blame]
Benny Prijono834aee32006-02-19 01:38:06 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
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 event_notify.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 * @{
34 *
35 * This module provides the implementation of SIP Extension for SIP Specific
36 * Event Notification (RFC 3265). It extends PJSIP by supporting SUBSCRIBE and
37 * NOTIFY methods.
38 *
39 * This module itself is extensible; new event packages can be registered to
40 * this module to handle specific extensions (such as presence).
41 */
42
43PJ_BEGIN_DECL
44
45
46/**
47 * Opaque type for event subscription session.
48 */
49typedef struct pjsip_evsub pjsip_evsub;
50
51
52/**
53 * This enumeration describes basic subscription state as described in the
54 * RFC 3265. The standard specifies that extensions may define additional
55 * states. In the case where the state is not known, the subscription state
56 * will be set to PJSIP_EVSUB_STATE_UNKNOWN, and the token will be kept
57 * in state_str member of the susbcription structure.
58 */
59enum pjsip_evsub_state
60{
61 PJSIP_EVSUB_STATE_NULL, /**< State is NULL. */
62 PJSIP_EVSUB_STATE_SENT, /**< Client has sent SUBSCRIBE request. */
63 PJSIP_EVSUB_STATE_ACCEPTED, /**< 2xx response to SUBSCRIBE has been
64 sent/received. */
65 PJSIP_EVSUB_STATE_PENDING, /**< Subscription is pending. */
66 PJSIP_EVSUB_STATE_ACTIVE, /**< Subscription is active. */
67 PJSIP_EVSUB_STATE_TERMINATED,/**< Subscription is terminated. */
68 PJSIP_EVSUB_STATE_UNKNOWN, /**< Subscription state can not be determined.
69 Application can query the state by
70 calling #pjsip_evsub_get_state_name().*/
71};
72
73/**
74 * @see pjsip_evsub_state
75 */
76typedef enum pjsip_evsub_state pjsip_evsub_state;
77
78
Benny Prijono26ff9062006-02-21 23:47:00 +000079/**
80 * Some options for the event subscription.
81 */
82enum
83{
84 /**
85 * If this flag is set, then outgoing request to create subscription
86 * will not have id in the Event header (e.g. in REFER request). But if
87 * there is an id in the incoming NOTIFY, that id will be used.
88 */
89 PJSIP_EVSUB_NO_EVENT_ID = 1,
90};
91
Benny Prijono834aee32006-02-19 01:38:06 +000092
93/**
94 * This structure describes callback that is registered by application or
95 * package to receive notifications about subscription events.
96 */
97struct pjsip_evsub_user
98{
99 /**
100 * This callback is called when subscription state has changed.
101 * Application MUST be prepared to receive NULL event and events with
102 * type other than PJSIP_EVENT_TSX_STATE
103 *
104 * This callback is OPTIONAL.
105 *
106 * @param sub The subscription instance.
107 * @param event The event that has caused the state to change,
108 * which may be NULL or may have type other than
109 * PJSIP_EVENT_TSX_STATE.
110 */
111 void (*on_evsub_state)( pjsip_evsub *sub, pjsip_event *event);
112
113 /**
114 * This callback is called when transaction state has changed.
115 *
116 * @param sub The subscription instance.
117 * @param tsx Transaction.
118 * @param event The event.
119 */
120 void (*on_tsx_state)(pjsip_evsub *sub, pjsip_transaction *tsx,
121 pjsip_event *event);
122
123 /**
124 * This callback is called when incoming SUBSCRIBE (or any method that
125 * establishes the subscription in the first place) is received. It
126 * allows application to specify what response should be sent to
127 * remote, along with additional headers and message body to be put
128 * in the response.
129 *
130 * This callback is OPTIONAL.
131 *
132 * However, implementation MUST send NOTIFY request upon receiving this
133 * callback. The suggested behavior is to call
134 * #pjsip_evsub_last_notify(), since this function takes care
135 * about unsubscription request and calculates the appropriate expiration
136 * interval.
137 */
138 void (*on_rx_refresh)( pjsip_evsub *sub,
139 pjsip_rx_data *rdata,
140 int *p_st_code,
141 pj_str_t **p_st_text,
142 pjsip_hdr *res_hdr,
143 pjsip_msg_body **p_body);
144
145 /**
146 * This callback is called when client/subscriber received incoming
147 * NOTIFY request. It allows the application to specify what response
148 * should be sent to remote, along with additional headers and message
149 * body to be put in the response.
150 *
151 * This callback is OPTIONAL. When it is not implemented, the default
152 * behavior is to respond incoming NOTIFY request with 200 (OK).
153 *
154 * @param sub The subscription instance.
155 * @param rdata The received NOTIFY request.
156 * @param p_st_code Application MUST set the value of this argument with
157 * final status code (200-699) upon returning from the
158 * callback.
159 * @param p_st_text Custom status text, if any.
160 * @param res_hdr Upon return, application can put additional headers
161 * to be sent in the response in this list.
162 * @param p_body Application MAY specify message body to be sent in
163 * the response.
164 */
165 void (*on_rx_notify)(pjsip_evsub *sub,
166 pjsip_rx_data *rdata,
167 int *p_st_code,
168 pj_str_t **p_st_text,
169 pjsip_hdr *res_hdr,
170 pjsip_msg_body **p_body);
171
172 /**
173 * This callback is called when it is time for the client to refresh
174 * the subscription.
175 *
Benny Prijono8c715612006-02-25 02:04:42 +0000176 * This callback is OPTIONAL when PJSIP package such as presence or
177 * refer is used; the event package will refresh subscription by sending
178 * SUBSCRIBE with the interval set to current/last interval.
Benny Prijono834aee32006-02-19 01:38:06 +0000179 *
180 * @param sub The subscription instance.
181 */
182 void (*on_client_refresh)(pjsip_evsub *sub);
183
184 /**
185 * This callback is called when server doesn't receive subscription
186 * refresh after the specified subscription interval.
187 *
Benny Prijono8c715612006-02-25 02:04:42 +0000188 * This callback is OPTIONAL when PJSIP package such as presence or
189 * refer is used; the event package send NOTIFY to terminate the
190 * subscription.
Benny Prijono834aee32006-02-19 01:38:06 +0000191 */
192 void (*on_server_timeout)(pjsip_evsub *sub);
193
194};
195
196
197/**
198 * @see pjsip_evsub_user
199 */
200typedef struct pjsip_evsub_user pjsip_evsub_user;
201
202
203/**
204 * SUBSCRIBE method constant.
205 */
206extern const pjsip_method pjsip_subscribe_method;
207
208/**
209 * NOTIFY method constant.
210 */
211extern const pjsip_method pjsip_notify_method;
212
213
214
215/**
216 * Initialize the event subscription module and register the module to the
217 * specified endpoint.
218 *
219 * @param endpt The endpoint instance.
220 *
221 * @return PJ_SUCCESS if module can be created and registered
222 * successfully.
223 */
224PJ_DECL(pj_status_t) pjsip_evsub_init_module(pjsip_endpoint *endpt);
225
226
227/**
228 * Get the event subscription module instance that was previously created
229 * and registered to endpoint.
230 *
231 * @return The event subscription module instance.
232 */
233PJ_DECL(pjsip_module*) pjsip_evsub_instance(void);
234
235
236/**
237 * Register event package to the event subscription framework.
238 *
239 * @param pkg_mod The module that implements the event package being
240 * registered.
241 * @param event_name Event package identification.
242 * @param expires Default subscription expiration time, in seconds.
243 * @param accept_cnt Number of strings in Accept array.
244 * @param accept Array of Accept value.
245 *
246 * @return PJ_SUCCESS on success.
247 */
248PJ_DECL(pj_status_t) pjsip_evsub_register_pkg( pjsip_module *pkg_mod,
249 const pj_str_t *event_name,
250 unsigned expires,
251 unsigned accept_cnt,
252 const pj_str_t accept[]);
253
254
255/**
256 * Create client subscription session.
257 *
258 * @param dlg The underlying dialog to use.
259 * @param user_cb Callback to receive event subscription notifications.
260 * @param event Event name.
Benny Prijono26ff9062006-02-21 23:47:00 +0000261 * @param option Bitmask of options.
Benny Prijono834aee32006-02-19 01:38:06 +0000262 * @param p_evsub Pointer to receive event subscription instance.
263 *
264 * @return PJ_SUCCESS on success.
265 */
266PJ_DECL(pj_status_t) pjsip_evsub_create_uac( pjsip_dialog *dlg,
267 const pjsip_evsub_user *user_cb,
268 const pj_str_t *event,
Benny Prijono26ff9062006-02-21 23:47:00 +0000269 unsigned option,
Benny Prijono834aee32006-02-19 01:38:06 +0000270 pjsip_evsub **p_evsub);
271
272/**
273 * Create server subscription session.
274 *
275 * @param dlg The underlying dialog to use.
276 * @param user_cb Callback to receive event subscription notifications.
277 * @param rdata The incoming request that creates the event
278 * subscription, such as SUBSCRIBE or REFER.
Benny Prijono26ff9062006-02-21 23:47:00 +0000279 * @param option Bitmask of options.
Benny Prijono834aee32006-02-19 01:38:06 +0000280 * @param p_evsub Pointer to receive event subscription instance.
281 *
282 * @return PJ_SUCCESS on success.
283 */
284PJ_DECL(pj_status_t) pjsip_evsub_create_uas( pjsip_dialog *dlg,
285 const pjsip_evsub_user *user_cb,
286 pjsip_rx_data *rdata,
Benny Prijono26ff9062006-02-21 23:47:00 +0000287 unsigned option,
Benny Prijono834aee32006-02-19 01:38:06 +0000288 pjsip_evsub **p_evsub);
289
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000290/**
291 * Forcefully destroy the subscription session. This function should only
292 * be called on special condition, such as when the subscription
293 * initialization has failed. For other conditions, application MUST terminate
294 * the subscription by sending the appropriate un(SUBSCRIBE) or NOTIFY.
295 *
296 * @param sub The event subscription.
297 * @param notify Specify whether the state notification callback
298 * should be called.
299 *
300 * @return PJ_SUCCESS if subscription session has been destroyed.
301 */
302PJ_DECL(pj_status_t) pjsip_evsub_terminate( pjsip_evsub *sub,
303 pj_bool_t notify );
304
Benny Prijono834aee32006-02-19 01:38:06 +0000305
306/**
307 * Get subscription state.
308 *
309 * @param sub Event subscription instance.
310 *
311 * @return Subscription state.
312 */
313PJ_DECL(pjsip_evsub_state) pjsip_evsub_get_state(pjsip_evsub *sub);
314
315
316/**
317 * Get the string representation of the subscription state.
318 *
319 * @param sub Event subscription instance.
320 *
321 * @return NULL terminated string.
322 */
323PJ_DECL(const char*) pjsip_evsub_get_state_name(pjsip_evsub *sub);
324
325
326/**
327 * Call this function to create request to initiate subscription, to
328 * refresh subcription, or to request subscription termination.
329 *
330 * @param sub Client subscription instance.
331 * @param method The method that establishes the subscription, such as
332 * SUBSCRIBE or REFER. If this argument is NULL, then
333 * SUBSCRIBE will be used.
334 * @param expires Subscription expiration. If the value is set to zero,
335 * this will request unsubscription. If the value is
336 * negative, default expiration as defined by the package
337 * will be used.
338 * @param p_tdata Pointer to receive the request.
339 *
340 * @return PJ_SUCCESS on success.
341 */
342PJ_DECL(pj_status_t) pjsip_evsub_initiate( pjsip_evsub *sub,
343 const pjsip_method *method,
344 pj_int32_t expires,
345 pjsip_tx_data **p_tdata);
346
347
348/**
349 * Accept the incoming subscription request by sending 2xx response to
350 * incoming SUBSCRIBE request.
351 *
352 * @param sub Server subscription instance.
353 * @param rdata The incoming subscription request message.
354 * @param st_code Status code, which MUST be final response.
355 * @param hdr_list Optional list of headers to be added in the response.
356 *
357 * @return PJ_SUCCESS on success.
358 */
359PJ_DECL(pj_status_t) pjsip_evsub_accept( pjsip_evsub *sub,
360 pjsip_rx_data *rdata,
361 int st_code,
362 const pjsip_hdr *hdr_list );
363
364
365/**
366 * For notifier, create NOTIFY request to subscriber, and set the state
367 * of the subscription.
368 *
369 * @param sub The server subscription (notifier) instance.
370 * @param state New state to set.
371 * @param state_str The state string name, if state contains value other
372 * than active, pending, or terminated. Otherwise this
373 * argument is ignored.
374 * @param reason Specify reason if new state is terminated, otherwise
375 * put NULL.
376 * @param p_tdata Pointer to receive request message.
377 *
378 * @return PJ_SUCCESS on success.
379 */
380PJ_DECL(pj_status_t) pjsip_evsub_notify( pjsip_evsub *sub,
381 pjsip_evsub_state state,
382 const pj_str_t *state_str,
383 const pj_str_t *reason,
384 pjsip_tx_data **p_tdata);
385
386
387/**
388 * For notifier, create a NOTIFY request that reflects current subscription
389 * status.
390 *
391 * @param sub The server subscription instance.
392 * @param p_tdata Pointer to receive the request messge.
393 *
394 * @return PJ_SUCCESS on success.
395 */
396PJ_DECL(pj_status_t) pjsip_evsub_current_notify( pjsip_evsub *sub,
397 pjsip_tx_data **p_tdata );
398
399
400
401/**
Benny Prijono26ff9062006-02-21 23:47:00 +0000402 * Send request message that was previously created with initiate(), notify(),
403 * or current_notify(). Application may also send request created with other
404 * functions, e.g. authentication. But the request MUST be either request
405 * that creates/refresh subscription or NOTIFY request.
Benny Prijono834aee32006-02-19 01:38:06 +0000406 *
407 * @param sub The event subscription object.
408 * @param tdata Request message to be send.
409 *
410 * @return PJ_SUCCESS on success.
411 */
412PJ_DECL(pj_status_t) pjsip_evsub_send_request( pjsip_evsub *sub,
413 pjsip_tx_data *tdata);
414
415
416
417/**
Benny Prijono8c715612006-02-25 02:04:42 +0000418 * Get the event subscription instance associated with the specified
419 * transaction.
Benny Prijono834aee32006-02-19 01:38:06 +0000420 *
421 * @param tsx The transaction.
422 *
423 * @return The event subscription instance registered in the
424 * transaction, if any.
425 */
426PJ_DECL(pjsip_evsub*) pjsip_tsx_get_evsub(pjsip_transaction *tsx);
427
428
429/**
430 * Set event subscription's module data.
431 *
432 * @param sub The event subscription.
433 * @param index The module id.
434 * @param data Arbitrary data.
435 */
436PJ_DECL(void) pjsip_evsub_set_mod_data( pjsip_evsub *sub, unsigned mod_id,
437 void *data );
438
439
440/**
441 * Get event subscription's module data.
442 *
443 * @param sub The event subscription.
444 * @param mod_id The module id.
445 *
446 * @return Data previously set at the specified id.
447 */
448PJ_DECL(void*) pjsip_evsub_get_mod_data( pjsip_evsub *sub, unsigned mod_id );
449
450
451
452PJ_END_DECL
453
454/**
455 * @}
456 */
457
458#endif /* __PJSIP_SIMPLE_EVSUB_H__ */