evsub.h 20 KB

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