Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1 | /* $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 | |
| 43 | PJ_BEGIN_DECL |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Opaque type for event subscription session. |
| 48 | */ |
| 49 | typedef 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 | */ |
| 59 | enum 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 | */ |
| 76 | typedef enum pjsip_evsub_state pjsip_evsub_state; |
| 77 | |
| 78 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 79 | /** |
| 80 | * Some options for the event subscription. |
| 81 | */ |
| 82 | enum |
| 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * This structure describes callback that is registered by application or |
| 95 | * package to receive notifications about subscription events. |
| 96 | */ |
| 97 | struct 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 | * |
| 176 | * This callback is OPTIONAL. When it is not implemented, the default |
| 177 | * behavior is to refresh subscription by sending SUBSCRIBE with the |
| 178 | * interval set to current/last interval. |
| 179 | * |
| 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 | * |
| 188 | * This callback is OPTIONAL. When it is not implemented, the default |
| 189 | * behavior is to send NOTIFY to terminate the subscription. |
| 190 | */ |
| 191 | void (*on_server_timeout)(pjsip_evsub *sub); |
| 192 | |
| 193 | }; |
| 194 | |
| 195 | |
| 196 | /** |
| 197 | * @see pjsip_evsub_user |
| 198 | */ |
| 199 | typedef struct pjsip_evsub_user pjsip_evsub_user; |
| 200 | |
| 201 | |
| 202 | /** |
| 203 | * SUBSCRIBE method constant. |
| 204 | */ |
| 205 | extern const pjsip_method pjsip_subscribe_method; |
| 206 | |
| 207 | /** |
| 208 | * NOTIFY method constant. |
| 209 | */ |
| 210 | extern const pjsip_method pjsip_notify_method; |
| 211 | |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * Initialize the event subscription module and register the module to the |
| 216 | * specified endpoint. |
| 217 | * |
| 218 | * @param endpt The endpoint instance. |
| 219 | * |
| 220 | * @return PJ_SUCCESS if module can be created and registered |
| 221 | * successfully. |
| 222 | */ |
| 223 | PJ_DECL(pj_status_t) pjsip_evsub_init_module(pjsip_endpoint *endpt); |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Get the event subscription module instance that was previously created |
| 228 | * and registered to endpoint. |
| 229 | * |
| 230 | * @return The event subscription module instance. |
| 231 | */ |
| 232 | PJ_DECL(pjsip_module*) pjsip_evsub_instance(void); |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Register event package to the event subscription framework. |
| 237 | * |
| 238 | * @param pkg_mod The module that implements the event package being |
| 239 | * registered. |
| 240 | * @param event_name Event package identification. |
| 241 | * @param expires Default subscription expiration time, in seconds. |
| 242 | * @param accept_cnt Number of strings in Accept array. |
| 243 | * @param accept Array of Accept value. |
| 244 | * |
| 245 | * @return PJ_SUCCESS on success. |
| 246 | */ |
| 247 | PJ_DECL(pj_status_t) pjsip_evsub_register_pkg( pjsip_module *pkg_mod, |
| 248 | const pj_str_t *event_name, |
| 249 | unsigned expires, |
| 250 | unsigned accept_cnt, |
| 251 | const pj_str_t accept[]); |
| 252 | |
| 253 | |
| 254 | /** |
| 255 | * Create client subscription session. |
| 256 | * |
| 257 | * @param dlg The underlying dialog to use. |
| 258 | * @param user_cb Callback to receive event subscription notifications. |
| 259 | * @param event Event name. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 260 | * @param option Bitmask of options. |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 261 | * @param p_evsub Pointer to receive event subscription instance. |
| 262 | * |
| 263 | * @return PJ_SUCCESS on success. |
| 264 | */ |
| 265 | PJ_DECL(pj_status_t) pjsip_evsub_create_uac( pjsip_dialog *dlg, |
| 266 | const pjsip_evsub_user *user_cb, |
| 267 | const pj_str_t *event, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 268 | unsigned option, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 269 | pjsip_evsub **p_evsub); |
| 270 | |
| 271 | /** |
| 272 | * Create server subscription session. |
| 273 | * |
| 274 | * @param dlg The underlying dialog to use. |
| 275 | * @param user_cb Callback to receive event subscription notifications. |
| 276 | * @param rdata The incoming request that creates the event |
| 277 | * subscription, such as SUBSCRIBE or REFER. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 278 | * @param option Bitmask of options. |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 279 | * @param p_evsub Pointer to receive event subscription instance. |
| 280 | * |
| 281 | * @return PJ_SUCCESS on success. |
| 282 | */ |
| 283 | PJ_DECL(pj_status_t) pjsip_evsub_create_uas( pjsip_dialog *dlg, |
| 284 | const pjsip_evsub_user *user_cb, |
| 285 | pjsip_rx_data *rdata, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 286 | unsigned option, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 287 | pjsip_evsub **p_evsub); |
| 288 | |
| 289 | |
| 290 | /** |
| 291 | * Get subscription state. |
| 292 | * |
| 293 | * @param sub Event subscription instance. |
| 294 | * |
| 295 | * @return Subscription state. |
| 296 | */ |
| 297 | PJ_DECL(pjsip_evsub_state) pjsip_evsub_get_state(pjsip_evsub *sub); |
| 298 | |
| 299 | |
| 300 | /** |
| 301 | * Get the string representation of the subscription state. |
| 302 | * |
| 303 | * @param sub Event subscription instance. |
| 304 | * |
| 305 | * @return NULL terminated string. |
| 306 | */ |
| 307 | PJ_DECL(const char*) pjsip_evsub_get_state_name(pjsip_evsub *sub); |
| 308 | |
| 309 | |
| 310 | /** |
| 311 | * Call this function to create request to initiate subscription, to |
| 312 | * refresh subcription, or to request subscription termination. |
| 313 | * |
| 314 | * @param sub Client subscription instance. |
| 315 | * @param method The method that establishes the subscription, such as |
| 316 | * SUBSCRIBE or REFER. If this argument is NULL, then |
| 317 | * SUBSCRIBE will be used. |
| 318 | * @param expires Subscription expiration. If the value is set to zero, |
| 319 | * this will request unsubscription. If the value is |
| 320 | * negative, default expiration as defined by the package |
| 321 | * will be used. |
| 322 | * @param p_tdata Pointer to receive the request. |
| 323 | * |
| 324 | * @return PJ_SUCCESS on success. |
| 325 | */ |
| 326 | PJ_DECL(pj_status_t) pjsip_evsub_initiate( pjsip_evsub *sub, |
| 327 | const pjsip_method *method, |
| 328 | pj_int32_t expires, |
| 329 | pjsip_tx_data **p_tdata); |
| 330 | |
| 331 | |
| 332 | /** |
| 333 | * Accept the incoming subscription request by sending 2xx response to |
| 334 | * incoming SUBSCRIBE request. |
| 335 | * |
| 336 | * @param sub Server subscription instance. |
| 337 | * @param rdata The incoming subscription request message. |
| 338 | * @param st_code Status code, which MUST be final response. |
| 339 | * @param hdr_list Optional list of headers to be added in the response. |
| 340 | * |
| 341 | * @return PJ_SUCCESS on success. |
| 342 | */ |
| 343 | PJ_DECL(pj_status_t) pjsip_evsub_accept( pjsip_evsub *sub, |
| 344 | pjsip_rx_data *rdata, |
| 345 | int st_code, |
| 346 | const pjsip_hdr *hdr_list ); |
| 347 | |
| 348 | |
| 349 | /** |
| 350 | * For notifier, create NOTIFY request to subscriber, and set the state |
| 351 | * of the subscription. |
| 352 | * |
| 353 | * @param sub The server subscription (notifier) instance. |
| 354 | * @param state New state to set. |
| 355 | * @param state_str The state string name, if state contains value other |
| 356 | * than active, pending, or terminated. Otherwise this |
| 357 | * argument is ignored. |
| 358 | * @param reason Specify reason if new state is terminated, otherwise |
| 359 | * put NULL. |
| 360 | * @param p_tdata Pointer to receive request message. |
| 361 | * |
| 362 | * @return PJ_SUCCESS on success. |
| 363 | */ |
| 364 | PJ_DECL(pj_status_t) pjsip_evsub_notify( pjsip_evsub *sub, |
| 365 | pjsip_evsub_state state, |
| 366 | const pj_str_t *state_str, |
| 367 | const pj_str_t *reason, |
| 368 | pjsip_tx_data **p_tdata); |
| 369 | |
| 370 | |
| 371 | /** |
| 372 | * For notifier, create a NOTIFY request that reflects current subscription |
| 373 | * status. |
| 374 | * |
| 375 | * @param sub The server subscription instance. |
| 376 | * @param p_tdata Pointer to receive the request messge. |
| 377 | * |
| 378 | * @return PJ_SUCCESS on success. |
| 379 | */ |
| 380 | PJ_DECL(pj_status_t) pjsip_evsub_current_notify( pjsip_evsub *sub, |
| 381 | pjsip_tx_data **p_tdata ); |
| 382 | |
| 383 | |
| 384 | |
| 385 | /** |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 386 | * Send request message that was previously created with initiate(), notify(), |
| 387 | * or current_notify(). Application may also send request created with other |
| 388 | * functions, e.g. authentication. But the request MUST be either request |
| 389 | * that creates/refresh subscription or NOTIFY request. |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 390 | * |
| 391 | * @param sub The event subscription object. |
| 392 | * @param tdata Request message to be send. |
| 393 | * |
| 394 | * @return PJ_SUCCESS on success. |
| 395 | */ |
| 396 | PJ_DECL(pj_status_t) pjsip_evsub_send_request( pjsip_evsub *sub, |
| 397 | pjsip_tx_data *tdata); |
| 398 | |
| 399 | |
| 400 | |
| 401 | /** |
| 402 | * Get the event subscription instance in the transaction. |
| 403 | * |
| 404 | * @param tsx The transaction. |
| 405 | * |
| 406 | * @return The event subscription instance registered in the |
| 407 | * transaction, if any. |
| 408 | */ |
| 409 | PJ_DECL(pjsip_evsub*) pjsip_tsx_get_evsub(pjsip_transaction *tsx); |
| 410 | |
| 411 | |
| 412 | /** |
| 413 | * Set event subscription's module data. |
| 414 | * |
| 415 | * @param sub The event subscription. |
| 416 | * @param index The module id. |
| 417 | * @param data Arbitrary data. |
| 418 | */ |
| 419 | PJ_DECL(void) pjsip_evsub_set_mod_data( pjsip_evsub *sub, unsigned mod_id, |
| 420 | void *data ); |
| 421 | |
| 422 | |
| 423 | /** |
| 424 | * Get event subscription's module data. |
| 425 | * |
| 426 | * @param sub The event subscription. |
| 427 | * @param mod_id The module id. |
| 428 | * |
| 429 | * @return Data previously set at the specified id. |
| 430 | */ |
| 431 | PJ_DECL(void*) pjsip_evsub_get_mod_data( pjsip_evsub *sub, unsigned mod_id ); |
| 432 | |
| 433 | |
| 434 | |
| 435 | PJ_END_DECL |
| 436 | |
| 437 | /** |
| 438 | * @} |
| 439 | */ |
| 440 | |
| 441 | #endif /* __PJSIP_SIMPLE_EVSUB_H__ */ |