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