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