KNX IoT
KNX IoT Point API stack implementation
oc_api.h
Go to the documentation of this file.
1 /*
2 // Copyright (c) 2016-2019 Intel Corporation
3 // Copyright (c) 2021-2022 Cascoda Ltd
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 */
17 
84 #ifndef OC_API_H
85 #define OC_API_H
86 
87 #include "messaging/coap/oc_coap.h"
88 #include "oc_buffer_settings.h"
89 #include "oc_knx.h"
90 #include "oc_rep.h"
91 #include "oc_ri.h"
92 #include "oc_client_state.h"
93 #include "oc_signal_event_loop.h"
94 #include "port/oc_storage.h"
95 #include "api/oc_knx_client.h"
96 #include "api/oc_knx_swu.h"
97 
98 #include "oc_programming_mode.h"
99 
100 #ifdef __cplusplus
101 extern "C" {
102 #endif
103 
108 #define OC_MAX_URL_LENGTH (30)
109 
115 typedef struct
116 {
141  int (*init)(void);
142 
149  void (*signal_event_loop)(void);
150 
151 #ifdef OC_SERVER
191  void (*register_resources)(void);
192 #endif /* OC_SERVER */
193 
194 #ifdef OC_CLIENT
214  void (*requests_entry)(void);
215 #endif /* OC_CLIENT */
216 } oc_handler_t;
217 
238 typedef void (*oc_init_platform_cb_t)(void *data);
239 
266 typedef void (*oc_add_device_cb_t)(void *data);
267 
295 int oc_main_init(const oc_handler_t *handler);
296 
303 oc_clock_time_t oc_main_poll(void);
304 
308 void oc_main_shutdown(void);
309 
317 typedef void (*oc_factory_presets_cb_t)(size_t device, void *data);
318 
333 
343 typedef void (*oc_reset_cb_t)(size_t device, int reset_value, void *data);
344 
358 void oc_set_reset_cb(oc_reset_cb_t cb, void *data);
359 
367 typedef void (*oc_restart_cb_t)(size_t device, void *data);
368 
387 void oc_set_restart_cb(oc_restart_cb_t cb, void *data);
388 
397 typedef void (*oc_hostname_cb_t)(size_t device, oc_string_t host_name,
398  void *data);
399 
414 
427 typedef void (*oc_programming_mode_cb_t)(size_t device, bool programming_mode,
428  void *data);
429 
444 
485 int oc_add_device(const char *name, const char *version, const char *base,
486  const char *serial_number, oc_add_device_cb_t add_device_cb,
487  void *data);
488 
502 #define oc_set_custom_device_property(prop, value) \
503  oc_rep_set_text_string(root, prop, value)
504 
525 int oc_init_platform(const char *mfg_name,
526  oc_init_platform_cb_t init_platform_cb, void *data);
527 
541 #define oc_set_custom_platform_property(prop, value) \
542  oc_rep_set_text_string(root, prop, value)
543 
544 /* Server side */
618 oc_resource_t *oc_new_resource(const char *name, const char *uri,
619  uint8_t num_resource_types, size_t device);
620 
635  oc_interface_mask_t iface_mask);
636 
655 void oc_resource_bind_resource_type(oc_resource_t *resource, const char *type);
656 
664  oc_content_format_t content_type);
665 
676 void oc_resource_bind_dpt(oc_resource_t *resource, const char *dpt);
677 
691 
702 void oc_resource_set_discoverable(oc_resource_t *resource, bool state);
703 
718 void oc_resource_set_observable(oc_resource_t *resource, bool state);
719 
735  uint16_t seconds);
736 
769  oc_method_t method,
770  oc_request_callback_t callback,
771  void *user_data);
772 
785  oc_get_properties_cb_t get_properties,
786  void *get_props_user_data,
787  oc_set_properties_cb_t set_properties,
788  void *set_props_user_data);
789 
799  uint8_t instance);
800 
813 
828 
835 
844 
877 int oc_iterate_query(oc_request_t *request, char **key, size_t *key_len,
878  char **value, size_t *value_len);
879 
914 bool oc_iterate_query_get_values(oc_request_t *request, const char *key,
915  char **value, int *value_len);
916 
933 int oc_get_query_value(oc_request_t *request, const char *key, char **value);
934 
945 int oc_query_value_exists(oc_request_t *request, const char *key);
946 
957 
974 void oc_send_response(oc_request_t *request, oc_status_t response_code);
975 
990 void oc_send_cbor_response(oc_request_t *request, oc_status_t response_code);
991 
1008  oc_status_t response_code,
1009  size_t payload_size);
1010 
1025 void oc_send_json_response(oc_request_t *request, oc_status_t response_code);
1026 
1039  oc_status_t response_code,
1040  size_t response_length);
1041 
1052  oc_status_t response_code);
1053 
1064 bool oc_get_request_payload_raw(oc_request_t *request, const uint8_t **payload,
1065  size_t *size,
1066  oc_content_format_t *content_format);
1067 
1077 void oc_send_response_raw(oc_request_t *request, const uint8_t *payload,
1078  size_t size, oc_content_format_t content_format,
1079  oc_status_t response_code);
1080 
1092  const uint8_t **payload, size_t *size,
1093  oc_content_format_t *content_format);
1094 
1103 void oc_send_diagnostic_message(oc_request_t *request, const char *msg,
1104  size_t msg_len, oc_status_t response_code);
1105 
1115 bool oc_get_diagnostic_message(oc_client_response_t *response, const char **msg,
1116  size_t *size);
1117 
1135 
1180  oc_separate_response_t *response);
1181 
1196 
1213  oc_status_t response_code);
1214 
1233  oc_status_t response_code);
1234 
1247 int oc_notify_observers(const oc_resource_t *resource);
1248 
1249 #ifdef __cplusplus
1250 }
1251 #endif // end of doc_module_tag_server_side
1253 
1281 #include "oc_client_state.h"
1282 
1283 #ifdef __cplusplus
1284 extern "C" {
1285 #endif
1286 
1305 bool oc_do_wk_discovery_all(const char *uri_query, int scope,
1307  void *user_data);
1308 
1316 int oc_lf_number_of_entries(const char *payload, int payload_len);
1317 
1328 int oc_lf_get_entry_uri(const char *payload, int payload_len, int entry,
1329  const char **uri, int *uri_len);
1330 
1342 int oc_lf_get_entry_param(const char *payload, int payload_len, int entry,
1343  const char *param, const char **p_out, int *p_len);
1344 
1359 bool oc_do_get(const char *uri, oc_endpoint_t *endpoint, const char *query,
1360  oc_response_handler_t handler, oc_qos_t qos, void *user_data);
1361 
1408 bool oc_do_get_ex(const char *uri, oc_endpoint_t *endpoint, const char *query,
1409  oc_response_handler_t handler, oc_qos_t qos,
1410  oc_content_format_t content, oc_content_format_t accept,
1411  void *user_data);
1412 
1428 bool oc_do_delete(const char *uri, oc_endpoint_t *endpoint, const char *query,
1429  oc_response_handler_t handler, oc_qos_t qos, void *user_data);
1430 
1475 bool oc_init_put(const char *uri, oc_endpoint_t *endpoint, const char *query,
1476  oc_response_handler_t handler, oc_qos_t qos, void *user_data);
1477 
1486 bool oc_do_put(void);
1487 
1499 
1544 bool oc_init_post(const char *uri, oc_endpoint_t *endpoint, const char *query,
1545  oc_response_handler_t handler, oc_qos_t qos, void *user_data);
1546 
1555 bool oc_do_post(void);
1556 
1568 
1590 bool oc_do_observe(const char *uri, oc_endpoint_t *endpoint, const char *query,
1591  oc_response_handler_t handler, oc_qos_t qos,
1592  void *user_data);
1593 
1603 bool oc_stop_observe(const char *uri, oc_endpoint_t *endpoint);
1604 
1611 
1621 bool oc_init_multicast_update(oc_endpoint_t *mcast, const char *uri,
1622  const char *query);
1623 
1631 
1642 
1649 
1650 #ifdef OC_TCP
1662 bool oc_send_ping(bool custody, oc_endpoint_t *endpoint,
1663  uint16_t timeout_seconds, oc_response_handler_t handler,
1664  void *user_data);
1665 #endif /* OC_TCP */ // end of doc_module_tag_client_state
1667 
1686 void oc_set_delayed_callback(void *cb_data, oc_trigger_t callback,
1687  uint16_t seconds);
1688 
1698 void oc_set_delayed_callback_ms(void *cb_data, oc_trigger_t callback,
1699  uint16_t miliseconds);
1700 
1707 void oc_remove_delayed_callback(void *cb_data, oc_trigger_t callback);
1708 
1711 #define oc_signal_interrupt_handler(name) \
1712  do { \
1713  oc_process_poll(&(name##_interrupt_x)); \
1714  _oc_signal_event_loop(); \
1715  } while (0)
1716 
1718 #define oc_activate_interrupt_handler(name) \
1719  (oc_process_start(&(name##_interrupt_x), 0))
1720 
1722 #define oc_define_interrupt_handler(name) \
1723  void name##_interrupt_x_handler(void); \
1724  OC_PROCESS(name##_interrupt_x, ""); \
1725  OC_PROCESS_THREAD(name##_interrupt_x, ev, data) \
1726  { \
1727  (void)data; \
1728  OC_PROCESS_POLLHANDLER(name##_interrupt_x_handler()); \
1729  OC_PROCESS_BEGIN(); \
1730  while (oc_process_is_running(&(name##_interrupt_x))) { \
1731  OC_PROCESS_YIELD(); \
1732  } \
1733  OC_PROCESS_END(); \
1734  } \
1735  void name##_interrupt_x_handler(void) // end of doc_module_tag_common_operations
1737 #ifdef __cplusplus
1738 }
1739 #endif
1740 
1741 #endif /* OC_API_H */
void oc_stop_multicast(oc_client_response_t *response)
stop the multicast update (e.g.
int oc_lf_number_of_entries(const char *payload, int payload_len)
link format parser, retrieve the number of entries in a response
bool oc_send_ping(bool custody, oc_endpoint_t *endpoint, uint16_t timeout_seconds, oc_response_handler_t handler, void *user_data)
send CoAP ping over the TCP connection
bool oc_do_multicast_update(void)
initiate the multi-cast update
bool oc_do_delete(const char *uri, oc_endpoint_t *endpoint, const char *query, oc_response_handler_t handler, oc_qos_t qos, void *user_data)
Issue a DELETE request to delete a resource.
bool oc_do_get(const char *uri, oc_endpoint_t *endpoint, const char *query, oc_response_handler_t handler, oc_qos_t qos, void *user_data)
issues a get request with accept-content CBOR
bool oc_do_put_ex(oc_content_format_t content, oc_content_format_t accept)
Dispatch the CoAP PUT request.
void oc_close_session(oc_endpoint_t *endpoint)
close the tls session on the indicated endpoint
bool oc_init_multicast_update(oc_endpoint_t *mcast, const char *uri, const char *query)
initialize the multicast update
bool oc_do_get_ex(const char *uri, oc_endpoint_t *endpoint, const char *query, oc_response_handler_t handler, oc_qos_t qos, oc_content_format_t content, oc_content_format_t accept, void *user_data)
Issue a GET request to obtain the current value of all properties a resource.
bool oc_do_put(void)
Dispatch the CoAP PUT request wiht content type and accept type CBOR.
bool oc_do_wk_discovery_all(const char *uri_query, int scope, oc_discovery_all_handler_t handler, void *user_data)
Discover all servers that have a resource type using the site-local scope.
bool oc_do_post_ex(oc_content_format_t content, oc_content_format_t accept)
Dispatch the CoAP POST request.
bool oc_init_put(const char *uri, oc_endpoint_t *endpoint, const char *query, oc_response_handler_t handler, oc_qos_t qos, void *user_data)
Prepare the stack to issue a PUT request.
bool oc_stop_observe(const char *uri, oc_endpoint_t *endpoint)
Unsubscribe for notifications from a resource.
bool oc_init_post(const char *uri, oc_endpoint_t *endpoint, const char *query, oc_response_handler_t handler, oc_qos_t qos, void *user_data)
Prepare the stack to issue a POST request.
int oc_lf_get_entry_uri(const char *payload, int payload_len, int entry, const char **uri, int *uri_len)
link format parser, retrieve the URL of an entry.
void oc_free_server_endpoints(oc_endpoint_t *endpoint)
Free a list of endpoints from the oc_endpoint_t.
bool oc_do_post(void)
Dispatch the CoAP POST request wiht content type and accept type CBOR.
int oc_lf_get_entry_param(const char *payload, int payload_len, int entry, const char *param, const char **p_out, int *p_len)
link format parser, retrieve a parameter value
bool oc_do_observe(const char *uri, oc_endpoint_t *endpoint, const char *query, oc_response_handler_t handler, oc_qos_t qos, void *user_data)
Dispatch a GET request with the CoAP Observe option to subscribe for notifications from a resource.
void oc_set_delayed_callback(void *cb_data, oc_trigger_t callback, uint16_t seconds)
Schedule a callback to be invoked after a set number of seconds.
void oc_set_delayed_callback_ms(void *cb_data, oc_trigger_t callback, uint16_t miliseconds)
Schedule a callback to be invoked after a set number of miliseconds.
void oc_remove_delayed_callback(void *cb_data, oc_trigger_t callback)
used to cancel a delayed callback
void oc_resource_set_function_block_instance(oc_resource_t *resource, uint8_t instance)
set a resource to a specific function block instance default is instance 0, if there is just 1 instan...
int oc_notify_observers(const oc_resource_t *resource)
Notify all observers of a change to a given resource's property.
void oc_set_separate_response_buffer(oc_separate_response_t *handle)
Set a response buffer for holding the response payload.
void oc_send_cbor_response_with_payload_size(oc_request_t *request, oc_status_t response_code, size_t payload_size)
Called after the response to a GET, PUT, POST or DELETE call has been prepared completed.
void oc_resource_set_discoverable(oc_resource_t *resource, bool state)
Specify if a resource can be found using .well-known/core discover mechanisms.
void oc_init_query_iterator(void)
This resets the query iterator to the start of the URI query parameter.
oc_resource_t * oc_new_resource(const char *name, const char *uri, uint8_t num_resource_types, size_t device)
Allocate and populate a new oc_resource_t.
void oc_send_response(oc_request_t *request, oc_status_t response_code)
Called after the response to a GET, PUT, POST or DELETE call has been prepared completed,...
bool oc_get_diagnostic_message(oc_client_response_t *response, const char **msg, size_t *size)
retrieve the diagnostic payload from a response
void oc_resource_bind_content_type(oc_resource_t *resource, oc_content_format_t content_type)
set the content type on the resource
void oc_resource_bind_dpt(oc_resource_t *resource, const char *dpt)
Add a Data Point Type "dpt" property to the resource.
void oc_send_response_no_format(oc_request_t *request, oc_status_t response_code)
Called after the response to a GET, PUT, POST or DELETE call has been prepared completed.
bool oc_delete_resource(oc_resource_t *resource)
Remove a resource from the stack and delete the resource.
bool oc_add_resource(oc_resource_t *resource)
Add a resource to the stack.
int oc_iterate_query(oc_request_t *request, char **key, size_t *key_len, char **value, size_t *value_len)
Iterate through the URI query parameters and get each key=value pair.
int oc_query_value_exists(oc_request_t *request, const char *key)
Checks if a query parameter 'key' exist in the URL query parameter.
bool oc_iterate_query_get_values(oc_request_t *request, const char *key, char **value, int *value_len)
Iterate though the URI query parameters for a specific key.
void oc_resource_make_public(oc_resource_t *resource)
Expose unsecured coap:// endpoints (in addition to secured coaps:// endpoints) for this resource in /...
void oc_send_cbor_response(oc_request_t *request, oc_status_t response_code)
Called after the response to a GET, PUT, POST or DELETE call has been prepared completed.
void oc_send_linkformat_response(oc_request_t *request, oc_status_t response_code, size_t response_length)
Called after the response to a GET, PUT, POST or DELETE call has been prepared completed.
void oc_resource_bind_resource_type(oc_resource_t *resource, const char *type)
Add a Resource Type "rt" property to the resource.
void oc_send_response_raw(oc_request_t *request, const uint8_t *payload, size_t size, oc_content_format_t content_format, oc_status_t response_code)
send the request, no processing
void oc_send_separate_response(oc_separate_response_t *handle, oc_status_t response_code)
Called to send the deferred response to a GET, PUT, POST or DELETE request.
void oc_delayed_delete_resource(oc_resource_t *resource)
Schedule a callback to remove a resource.
int oc_get_query_value(oc_request_t *request, const char *key, char **value)
Get a pointer to the start of the value in a URL query parameter key=value pair.
void oc_ignore_request(oc_request_t *request)
Ignore the request.
bool oc_query_values_available(oc_request_t *request)
Checks if a query parameter are available.
void oc_resource_set_periodic_observable(oc_resource_t *resource, uint16_t seconds)
The resource will periodically notify observing clients of is property values.
bool oc_get_response_payload_raw(oc_client_response_t *response, const uint8_t **payload, size_t *size, oc_content_format_t *content_format)
retrieve the response payload, without processing
void oc_resource_bind_resource_interface(oc_resource_t *resource, oc_interface_mask_t iface_mask)
Add the supported interface(s) to the resource.
void oc_resource_set_observable(oc_resource_t *resource, bool state)
Specify that a resource should notify clients when a property has been modified.
bool oc_get_request_payload_raw(oc_request_t *request, const uint8_t **payload, size_t *size, oc_content_format_t *content_format)
retrieve the payload from the request, no processing
void oc_indicate_separate_response(oc_request_t *request, oc_separate_response_t *response)
Respond to an incoming request asynchronously.
void oc_send_diagnostic_message(oc_request_t *request, const char *msg, size_t msg_len, oc_status_t response_code)
send a diagnostic payload
void oc_resource_set_request_handler(oc_resource_t *resource, oc_method_t method, oc_request_callback_t callback, void *user_data)
Specify a request_callback for GET, PUT, POST, and DELETE methods.
void oc_resource_set_properties_cbs(oc_resource_t *resource, oc_get_properties_cb_t get_properties, void *get_props_user_data, oc_set_properties_cb_t set_properties, void *set_props_user_data)
sets the callback properties for set properties and get properties
void oc_send_empty_separate_response(oc_separate_response_t *handle, oc_status_t response_code)
Called to send the deferred response to a GET, PUT, POST or DELETE request, with an empty payload.
void oc_send_json_response(oc_request_t *request, oc_status_t response_code)
Called after the response to a GET, PUT, POST or DELETE call has been prepared completed.
void register_resources(void)
register all the resources to the stack this function registers all application level resources:
int oc_add_device(const char *name, const char *version, const char *base, const char *serial_number, oc_add_device_cb_t add_device_cb, void *data)
Add an a device to the stack.
void oc_set_reset_cb(oc_reset_cb_t cb, void *data)
Set the reset callback.
void(* oc_programming_mode_cb_t)(size_t device, bool programming_mode, void *data)
Set the programming mode callback NOTE: It is the responsibility of this callback (if registered),...
Definition: oc_api.h:427
void(* oc_init_platform_cb_t)(void *data)
Callback invoked during oc_init_platform().
Definition: oc_api.h:238
void oc_set_restart_cb(oc_restart_cb_t cb, void *data)
Set the restart callback.
void(* oc_add_device_cb_t)(void *data)
Callback invoked during oc_add_device().
Definition: oc_api.h:266
void oc_set_factory_presets_cb(oc_factory_presets_cb_t cb, void *data)
Set the factory presets callback.
void oc_main_shutdown(void)
Shutdown and free all stack related resources.
void oc_set_programming_mode_cb(oc_programming_mode_cb_t cb, void *data)
Set the programming mode callback.
int oc_init_platform(const char *mfg_name, oc_init_platform_cb_t init_platform_cb, void *data)
Initialize the platform.
void(* oc_restart_cb_t)(size_t device, void *data)
Callback invoked by the stack to invoke a restart.
Definition: oc_api.h:367
void(* oc_hostname_cb_t)(size_t device, oc_string_t host_name, void *data)
Callback invoked by the stack to set the host name.
Definition: oc_api.h:397
oc_clock_time_t oc_main_poll(void)
poll to process tasks
int oc_main_init(const oc_handler_t *handler)
Register and call handler functions responsible for controlling the stack.
void(* oc_reset_cb_t)(size_t device, int reset_value, void *data)
Callback invoked by the stack initialization to perform any application reset.
Definition: oc_api.h:343
void(* oc_factory_presets_cb_t)(size_t device, void *data)
Callback invoked by the stack initialization to perform any "factory settings", e....
Definition: oc_api.h:317
void oc_set_hostname_cb(oc_hostname_cb_t cb, void *data)
Set the host name callback.
CoAP message buffer setting implementation.
CoAP client.
oc_qos_t
Quality of Service.
oc_discovery_flags_t(* oc_discovery_all_handler_t)(const char *, int len, oc_endpoint_t *endpoint, void *)
discovery_all handler
void(* oc_response_handler_t)(oc_client_response_t *)
client response handler
implementation of /.well-known/knx and /k resources
client code for the device (s-mode)
software update
programming mode code
request and response payload handling in cbor
resource internals
oc_status_t
CoAP status codes.
Definition: oc_ri.h:152
oc_method_t
CoAP methods.
Definition: oc_ri.h:124
oc_content_format_t
payload content formats
Definition: oc_ri.h:184
oc_interface_mask_t
interface masks security access scopes defined as interfaces note that scope = 1 is not used.
Definition: oc_ri.h:261
struct oc_separate_response_s oc_separate_response_t
separate response type
Definition: oc_ri.h:238
void(* oc_request_callback_t)(oc_request_t *, oc_interface_mask_t, void *)
request callback
Definition: oc_ri.h:433
void(* oc_get_properties_cb_t)(oc_resource_t *, oc_interface_mask_t, void *)
get properties callback
Definition: oc_ri.h:456
bool(* oc_set_properties_cb_t)(oc_resource_t *, oc_rep_t *, void *)
set properties callback
Definition: oc_ri.h:450
platform abstraction for storage (e.g.
Client response information.
the endpoint information
Definition: oc_endpoint.h:78
Call back handlers that are invoked in response to oc_main_init()
Definition: oc_api.h:116
request information structure
Definition: oc_ri.h:412
resource structure
Definition: oc_ri.h:482