• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * coap_resource_internal.h -- generic resource handling
3  *
4  * Copyright (C) 2010,2011,2014-2023 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * This file is part of the CoAP library libcoap. Please see README for terms
9  * of use.
10  */
11 
12 /**
13  * @file coap_resource_internal.h
14  * @brief Generic resource internal handling
15  */
16 
17 #ifndef COAP_RESOURCE_INTERNAL_H_
18 #define COAP_RESOURCE_INTERNAL_H_
19 
20 #include "coap_internal.h"
21 #include "coap_uthash_internal.h"
22 
23 #if COAP_SERVER_SUPPORT
24 /**
25  * @ingroup internal_api
26  * @defgroup coap_resource_internal Resources
27  * Internal API for handling resources
28  * @{
29  */
30 
31 /**
32  * Limits the number of subscribers for each resource that this server support.
33  * Zero means there is no maximum.
34  */
35 #ifndef COAP_RESOURCE_MAX_SUBSCRIBER
36 #define COAP_RESOURCE_MAX_SUBSCRIBER 0
37 #endif /* COAP_RESOURCE_MAX_SUBSCRIBER */
38 
39 /**
40 * Abstraction of attribute associated with a resource.
41 */
42 struct coap_attr_t {
43   struct coap_attr_t *next; /**< Pointer to next in chain or NULL */
44   coap_str_const_t *name;   /**< Name of the attribute */
45   coap_str_const_t *value;  /**< Value of the attribute (can be NULL) */
46   int flags;
47 };
48 
49 /**
50 * Abstraction of resource that can be attached to coap_context_t.
51 * The key is uri_path.
52 */
53 struct coap_resource_t {
54   unsigned int dirty:1;          /**< set to 1 if resource has changed */
55   unsigned int partiallydirty:1; /**< set to 1 if some subscribers have not yet
56                                   *   been notified of the last change */
57   unsigned int observable:1;     /**< can be observed */
58   unsigned int cacheable:1;      /**< can be cached */
59   unsigned int is_unknown:1;     /**< resource created for unknown handler */
60   unsigned int is_proxy_uri:1;   /**< resource created for proxy URI handler */
61 
62   /**
63    * Used to store handlers for the seven coap methods @c GET, @c POST, @c PUT,
64    * @c DELETE, @c FETCH, @c PATCH and @c IPATCH.
65    * coap_dispatch() will pass incoming requests to handle_request() and then
66    * to the handler that corresponds to its request method or generate a 4.05
67    * response if no handler is available.
68    */
69   coap_method_handler_t handler[7];
70 
71   UT_hash_handle hh;
72 
73   coap_attr_t *link_attr; /**< attributes to be included with the link format */
74   coap_subscription_t *subscribers;  /**< list of observers for this resource */
75 
76   /**
77    * Request URI Path for this resource. This field will point into static
78    * or allocated memory which must remain there for the duration of the
79    * resource.
80    */
81   coap_str_const_t *uri_path;  /**< the key used for hash lookup for this
82                                     resource */
83   int flags; /**< zero or more COAP_RESOURCE_FLAGS_* or'd together */
84 
85   /**
86   * The next value for the Observe option. This field must be increased each
87   * time the resource changes. Only the lower 24 bits are sent.
88   */
89   unsigned int observe;
90 
91   /**
92    * Pointer back to the context that 'owns' this resource.
93    */
94   coap_context_t *context;
95 
96   /**
97    * Count of valid names this host is known by (proxy support)
98    */
99   size_t proxy_name_count;
100 
101   /**
102    * Array valid names this host is known by (proxy support)
103    */
104   coap_str_const_t **proxy_name_list;
105 
106   /**
107    * This pointer is under user control. It can be used to store context for
108    * the coap handler.
109    */
110   void *user_data;
111 
112 };
113 
114 /**
115  * Deletes all resources from given @p context and frees their storage.
116  *
117  * @param context The CoAP context with the resources to be deleted.
118  */
119 void coap_delete_all_resources(coap_context_t *context);
120 
121 #define RESOURCES_ADD(r, obj) \
122   HASH_ADD(hh, (r), uri_path->s[0], (obj)->uri_path->length, (obj))
123 
124 #define RESOURCES_DELETE(r, obj) \
125   HASH_DELETE(hh, (r), (obj))
126 
127 #define RESOURCES_ITER(r,tmp)  \
128   coap_resource_t *tmp, *rtmp; \
129   HASH_ITER(hh, (r), tmp, rtmp)
130 
131 #define RESOURCES_FIND(r, k, res) {                     \
132     HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); \
133   }
134 
135 /**
136  * Deletes an attribute.
137  * Note: This is for internal use only, as it is not deleted from its chain.
138  *
139  * @param attr Pointer to a previously created attribute.
140  *
141  */
142 void coap_delete_attr(coap_attr_t *attr);
143 
144 coap_print_status_t coap_print_wellknown(coap_context_t *,
145                                          unsigned char *,
146                                          size_t *, size_t,
147                                          const coap_string_t *);
148 
149 /** @} */
150 
151 #endif /* COAP_SERVER_SUPPORT */
152 
153 #endif /* COAP_RESOURCE_INTERNAL_H_ */
154