2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 #ifndef _RES_PJSIP_PUBSUB_H
20 #define _RES_PJSIP_PUBSUB_H
22 #include "asterisk/linkedlists.h"
24 /* Forward declarations */
28 struct ast_sip_endpoint;
30 struct ast_datastore_info;
33 * \brief Opaque structure representing a publication
35 struct ast_sip_publication;
37 enum ast_sip_publish_state {
38 /*! Publication has just been initialized */
39 AST_SIP_PUBLISH_STATE_INITIALIZED,
40 /*! Publication is currently active */
41 AST_SIP_PUBLISH_STATE_ACTIVE,
42 /*! Publication has been terminated */
43 AST_SIP_PUBLISH_STATE_TERMINATED,
47 * \brief Callbacks that publication handlers will define
49 struct ast_sip_publish_handler {
50 /*! \brief The name of the event this handler deals with */
51 const char *event_name;
53 /*! \brief Publications */
54 struct ao2_container *publications;
57 * \brief Called when a PUBLISH to establish a new publication arrives.
59 * \param endpoint The endpoint from whom the PUBLISH arrived.
60 * \param resource The resource whose state is being published.
61 * \param event_configuration The name of the event type configuration to use for this resource.
62 * \return Response code for the incoming PUBLISH
64 int (*new_publication)(struct ast_sip_endpoint *endpoint, const char *resource, const char *event_configuration);
66 * \brief Called when a publication has reached its expiration.
68 void (*publish_expire)(struct ast_sip_publication *pub);
70 * \brief Published resource has changed states.
72 * The state parameter can be used to take further action. For instance,
73 * if the state is AST_SIP_PUBLISH_STATE_INITIALIZED, then this is the initial
74 * PUBLISH request. This is a good time to set up datastores on the publication
75 * or any other initial needs.
77 * AST_SIP_PUBLISH_STATE_TERMINATED is used when the remote end is terminating
78 * its publication. This is a good opportunity to free any resources associated with
81 * AST_SIP_PUBLISH_STATE_ACTIVE is used when a publication that modifies state
84 * \param pub The publication whose state has changed
85 * \param body The body of the inbound PUBLISH
86 * \param state The state of the publication
88 int (*publication_state_change)(struct ast_sip_publication *pub, pjsip_msg_body *body,
89 enum ast_sip_publish_state state);
90 AST_LIST_ENTRY(ast_sip_publish_handler) next;
94 * \brief Given a publication, get the associated endpoint
96 * \param pub The publication
97 * \retval NULL Failure
98 * \retval non-NULL The associated endpoint
100 struct ast_sip_endpoint *ast_sip_publication_get_endpoint(struct ast_sip_publication *pub);
103 * \brief Given a publication, get the resource the publication is to
105 * \param pub The publication
106 * \return The resource
108 const char *ast_sip_publication_get_resource(const struct ast_sip_publication *pub);
111 * \brief Given a publication, get the configuration name for the event type in use
113 * \param pub The publication
114 * \return The configuration name
116 const char *ast_sip_publication_get_event_configuration(const struct ast_sip_publication *pub);
119 * \brief Register a publish handler
121 * \retval 0 Handler was registered successfully
122 * \retval non-zero Handler was not registered successfully
124 int ast_sip_register_publish_handler(struct ast_sip_publish_handler *handler);
127 * \brief Unregister a publish handler
129 void ast_sip_unregister_publish_handler(struct ast_sip_publish_handler *handler);
132 * \brief Add a datastore to a SIP publication
134 * Note that SIP uses reference counted datastores. The datastore passed into this function
135 * must have been allocated using ao2_alloc() or there will be serious problems.
137 * \param publication The publication to add the datastore to
138 * \param datastore The datastore to be added to the subscription
142 int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore);
145 * \brief Retrieve a publication datastore
147 * The datastore retrieved will have its reference count incremented. When the caller is done
148 * with the datastore, the reference counted needs to be decremented using ao2_ref().
150 * \param publication The publication from which to retrieve the datastore
151 * \param name The name of the datastore to retrieve
152 * \retval NULL Failed to find the specified datastore
153 * \retval non-NULL The specified datastore
155 struct ast_datastore *ast_sip_publication_get_datastore(struct ast_sip_publication *publication, const char *name);
158 * \brief Remove a publication datastore from the publication
160 * This operation may cause the datastore's free() callback to be called if the reference
161 * count reaches zero.
163 * \param publication The publication to remove the datastore from
164 * \param name The name of the datastore to remove
166 void ast_sip_publication_remove_datastore(struct ast_sip_publication *publication, const char *name);
169 * \brief Opaque structure representing an RFC 3265 SIP subscription
171 struct ast_sip_subscription;
174 * \brief Role for the subscription that is being created
176 enum ast_sip_subscription_role {
177 /* Sending SUBSCRIBEs, receiving NOTIFYs */
179 /* Sending NOTIFYs, receiving SUBSCRIBEs */
184 * \brief Data for responses to SUBSCRIBEs and NOTIFIEs
186 * Some of PJSIP's evsub callbacks expect us to provide them
187 * with data so that they can craft a response rather than have
188 * us create our own response.
190 * Filling in the structure is optional, since the framework
191 * will automatically respond with a 200 OK response if we do
192 * not provide it with any additional data.
194 struct ast_sip_subscription_response_data {
195 /*! Status code of the response */
197 /*! Optional status text */
198 const char *status_text;
199 /*! Optional additional headers to add to the response */
200 struct ast_variable *headers;
201 /*! Optional body to add to the response */
202 struct ast_sip_body *body;
205 #define AST_SIP_MAX_ACCEPT 32
206 enum ast_sip_subscription_notify_reason {
207 /*! Initial NOTIFY for subscription */
208 AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED,
209 /*! Subscription has been renewed */
210 AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED,
211 /*! Subscription is being terminated */
212 AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED,
213 /*! Other unspecified reason */
214 AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER
217 struct ast_sip_notifier {
219 * \brief Default body type defined for the event package this notifier handles.
221 * Typically, a SUBSCRIBE request will contain one or more Accept headers that tell
222 * what format they expect the body of NOTIFY requests to use. However, every event
223 * package is required to define a default body format type to be used if a SUBSCRIBE
224 * request for the event contains no Accept header.
226 const char *default_accept;
228 * \brief Called when a SUBSCRIBE arrives attempting to establish a new subscription.
230 * The notifier is expected to return the response that should be sent to the
233 * If a 200-class response is returned, then the notifier's notify_required
234 * callback will immediately be called into with a reason of
235 * AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED.
237 * \param endpoint The endpoint from which we received the SUBSCRIBE
238 * \param resource The name of the resource to which the subscription is being made
239 * \return The response code to send to the SUBSCRIBE.
241 int (*new_subscribe)(struct ast_sip_endpoint *endpoint, const char *resource);
243 * \brief Called when an inbound subscription has been accepted.
245 * This is a prime opportunity for notifiers to add any notifier-specific
246 * data to the subscription (such as datastores) that it needs to.
248 * \note There is no need to send a NOTIFY request when this callback
251 * \param sub The new subscription
255 int (*subscription_established)(struct ast_sip_subscription *sub);
257 * \brief Supply data needed to create a NOTIFY body.
259 * The returned data must be an ao2 object. The caller of this function
260 * will be responsible for decrementing the refcount of the returned object
262 * \param sub The subscription
263 * \return An ao2 object that can be used to create a NOTIFY body.
265 void *(*get_notify_data)(struct ast_sip_subscription *sub);
268 struct ast_sip_subscriber {
270 * \brief A NOTIFY has been received.
272 * The body of the NOTIFY is provided so that it may be parsed and appropriate
273 * internal state change may be generated.
275 * The state can be used to determine if the subscription has been terminated
276 * by the far end or if this is just a typical resource state change.
278 * \param sub The subscription on which the NOTIFY arrived
279 * \param body The body of the NOTIFY
280 * \param state The subscription state
282 void (*state_change)(struct ast_sip_subscription *sub, pjsip_msg_body *body, enum pjsip_evsub_state state);
285 struct ast_sip_subscription_handler {
286 /*! The name of the event this subscriber deals with */
287 const char *event_name;
288 /*! The types of body this subscriber accepts. */
289 const char *accept[AST_SIP_MAX_ACCEPT];
291 * \brief Called when a subscription is to be destroyed
293 * The handler is not expected to send any sort of requests or responses
294 * during this callback. The handler MUST, however, begin the destruction
295 * process for the subscription during this callback.
297 void (*subscription_shutdown)(struct ast_sip_subscription *subscription);
299 * \brief Converts the subscriber to AMI
301 * \param sub The subscription
302 * \param buf The string to write AMI data
304 void (*to_ami)(struct ast_sip_subscription *sub, struct ast_str **buf);
305 /*! Subscriber callbacks for this handler */
306 struct ast_sip_subscriber *subscriber;
307 /*! Notifier callbacks for this handler */
308 struct ast_sip_notifier *notifier;
309 AST_LIST_ENTRY(ast_sip_subscription_handler) next;
313 * \brief Create a new ast_sip_subscription structure
315 * When a subscriber wishes to create a subscription, it may call this function
316 * to allocate resources and to send the initial SUBSCRIBE out.
318 * \param subscriber The subscriber that is making the request.
319 * \param endpoint The endpoint to whome the SUBSCRIBE will be sent.
320 * \param resource The resource to place in the SUBSCRIBE's Request-URI.
322 struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
323 struct ast_sip_endpoint *endpoint, const char *resource);
327 * \brief Get the endpoint that is associated with this subscription
329 * This function will increase the reference count of the endpoint. Be sure to
330 * release the reference to it when you are finished with the endpoint.
332 * \retval NULL Could not get endpoint
333 * \retval non-NULL The endpoint
335 struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscription *sub);
338 * \brief Get the serializer for the subscription
340 * Tasks that originate outside of a SIP servant thread should get the serializer
341 * and push the task to the serializer.
343 * \param sub The subscription
344 * \retval NULL Failure
345 * \retval non-NULL The subscription's serializer
347 struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub);
350 * \brief Notify a SIP subscription of a state change.
352 * This tells the pubsub core that the state of a subscribed resource has changed.
353 * The pubsub core will generate an appropriate NOTIFY request to send to the
356 * \param sub The subscription on which a state change is occurring.
357 * \param notify_data Event package-specific data used to create the NOTIFY body.
358 * \param terminate True if this NOTIFY is intended to terminate the subscription.
360 * \retval non-zero Failure
362 int ast_sip_subscription_notify(struct ast_sip_subscription *sub, void *notify_data, int terminate);
365 * \brief Retrieve the local URI for this subscription
367 * This is the local URI of the subscribed resource.
369 * \param sub The subscription
370 * \param[out] buf The buffer into which to store the URI.
371 * \param size The size of the buffer.
373 void ast_sip_subscription_get_local_uri(struct ast_sip_subscription *sub, char *buf, size_t size);
376 * \brief Retrive the remote URI for this subscription
378 * This is the remote URI as determined by the underlying SIP dialog.
380 * \param sub The subscription
381 * \param[out] buf The buffer into which to store the URI.
382 * \param size The size of the buffer.
384 void ast_sip_subscription_get_remote_uri(struct ast_sip_subscription *sub, char *buf, size_t size);
387 * \brief Get the name of the subscribed resource.
389 const char *ast_sip_subscription_get_resource_name(struct ast_sip_subscription *sub);
392 * \brief Get a header value for a subscription.
394 * For notifiers, the headers of the inbound SUBSCRIBE that started the dialog
395 * are stored on the subscription. This method allows access to the header. The
396 * return is the same as pjsip_msg_find_hdr_by_name(), meaning that it is dependent
397 * on the header being searched for.
399 * \param sub The subscription to search in.
400 * \param header The name of the header to search for.
401 * \return The discovered header, or NULL if the header cannot be found.
403 void *ast_sip_subscription_get_header(const struct ast_sip_subscription *sub, const char *header);
406 * \brief Send a request created via a PJSIP evsub method
408 * Callers of this function should take care to do so within a SIP servant
411 * \param sub The subscription on which to send the request
412 * \param tdata The request to send
414 * \retval non-zero Failure
416 int ast_sip_subscription_send_request(struct ast_sip_subscription *sub, pjsip_tx_data *tdata);
419 * \brief Alternative for ast_datastore_alloc()
421 * There are two major differences between this and ast_datastore_alloc()
422 * 1) This allocates a refcounted object
423 * 2) This will fill in a uid if one is not provided
425 * DO NOT call ast_datastore_free() on a datastore allocated in this
426 * way since that function will attempt to free the datastore rather
427 * than play nicely with its refcount.
429 * \param info Callbacks for datastore
430 * \param uid Identifier for datastore
431 * \retval NULL Failed to allocate datastore
432 * \retval non-NULL Newly allocated datastore
434 struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid);
437 * \brief Add a datastore to a SIP subscription
439 * Note that SIP uses reference counted datastores. The datastore passed into this function
440 * must have been allocated using ao2_alloc() or there will be serious problems.
442 * \param subscription The ssubscription to add the datastore to
443 * \param datastore The datastore to be added to the subscription
447 int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore);
450 * \brief Retrieve a subscription datastore
452 * The datastore retrieved will have its reference count incremented. When the caller is done
453 * with the datastore, the reference counted needs to be decremented using ao2_ref().
455 * \param subscription The subscription from which to retrieve the datastore
456 * \param name The name of the datastore to retrieve
457 * \retval NULL Failed to find the specified datastore
458 * \retval non-NULL The specified datastore
460 struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name);
463 * \brief Remove a subscription datastore from the subscription
465 * This operation may cause the datastore's free() callback to be called if the reference
466 * count reaches zero.
468 * \param subscription The subscription to remove the datastore from
469 * \param name The name of the datastore to remove
471 void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name);
474 * \brief Register a subscription handler
476 * \retval 0 Handler was registered successfully
477 * \retval non-zero Handler was not registered successfully
479 int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler);
482 * \brief Unregister a subscription handler
484 void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler);
487 * \brief Pubsub body generator
489 * A body generator is responsible for taking Asterisk content
490 * and converting it into a body format to be placed in an outbound
491 * SIP NOTIFY or PUBLISH request.
493 struct ast_sip_pubsub_body_generator {
495 * \brief Content type
496 * In "plain/text", "plain" is the type
500 * \brief Content subtype
501 * In "plain/text", "text" is the subtype
505 * \brief allocate body structure.
507 * Body generators will have this method called when a NOTIFY
508 * or PUBLISH body needs to be created. The type returned depends on
509 * the type of content being produced for the body. The data parameter
510 * is provided by the subscription handler and will vary between different
513 * \param data The subscription data provided by the event handler
514 * \retval non-NULL The allocated body
515 * \retval NULL Failure
517 void *(*allocate_body)(void *data);
519 * \brief Add content to the body of a SIP request
521 * The body of the request has already been allocated by the body generator's
522 * allocate_body callback.
524 * \param body The body of the SIP request. The type is determined by the
526 * \param data The subscription data used to populate the body. The type is
527 * determined by the content type.
529 int (*generate_body_content)(void *body, void *data);
531 * \brief Convert the body to a string.
533 * \param body The request body.
534 * \param str The converted string form of the request body
536 void (*to_string)(void *body, struct ast_str **str);
538 * \brief Deallocate resources created for the body
540 * Optional callback to destroy resources allocated for the
543 * \param body Body to be destroyed
545 void (*destroy_body)(void *body);
546 AST_LIST_ENTRY(ast_sip_pubsub_body_generator) list;
550 * \brief Body supplement
552 * Body supplements provide additions to bodies not already
553 * provided by body generators. This may include proprietary
554 * extensions, optional content, or other nonstandard fare.
556 struct ast_sip_pubsub_body_supplement {
558 * \brief Content type
559 * In "plain/text", "plain" is the type
563 * \brief Content subtype
564 * In "plain/text", "text" is the subtype
568 * \brief Add additional content to a SIP request body.
570 * A body generator will have already allocated a body and populated
571 * it with base data for the event. The supplement's duty is, if desired,
572 * to extend the body to have optional data beyond what a base RFC specifies.
574 * \param body The body of the SIP request. The type is determined by the
575 * body generator that allocated the body.
576 * \param data The subscription data used to populate the body. The type is
577 * determined by the content type.
579 int (*supplement_body)(void *body, void *data);
580 AST_LIST_ENTRY(ast_sip_pubsub_body_supplement) list;
585 * \brief Generate body content for a PUBLISH or NOTIFY
587 * This function takes a pre-allocated body and calls into registered body
588 * generators in order to fill in the body with appropriate details.
589 * The primary body generator will be called first, followed by the
590 * supplementary body generators
592 * \param content_type The content type of the body
593 * \param content_subtype The content subtype of the body
594 * \param data The data associated with body generation.
595 * \param[out] str The string representation of the generated body
597 * \retval non-zero Failure
599 int ast_sip_pubsub_generate_body_content(const char *content_type,
600 const char *content_subtype, void *data, struct ast_str **str);
604 * \brief Register a body generator with the pubsub core.
606 * This may fail if an attempt is made to register a primary body supplement
607 * for a given content type if a primary body supplement for that content type
608 * has already been registered.
610 * \param generator Body generator to register
614 int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator);
618 * \brief Unregister a body generator with the pubsub core.
620 * \param generator Body generator to unregister
622 void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator);
626 * \brief Register a body generator with the pubsub core.
628 * This may fail if an attempt is made to register a primary body supplement
629 * for a given content type if a primary body supplement for that content type
630 * has already been registered.
632 * \param generator Body generator to register
636 int ast_sip_pubsub_register_body_supplement(struct ast_sip_pubsub_body_supplement *supplement);
640 * \brief Unregister a body generator with the pubsub core.
642 * \param generator Body generator to unregister
644 void ast_sip_pubsub_unregister_body_supplement(struct ast_sip_pubsub_body_supplement *supplement);
648 * \brief Get the body type used for this subscription
650 const char *ast_sip_subscription_get_body_type(struct ast_sip_subscription *sub);
654 * \brief Get the body subtype used for this subscription
656 const char *ast_sip_subscription_get_body_subtype(struct ast_sip_subscription *sub);
658 #endif /* RES_PJSIP_PUBSUB_H */