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