Contiki-NG
Loading...
Searching...
No Matches
mqtt-prop.h
1/*
2 * Copyright (c) 2020, Alexandru-Ioan Pop - https://alexandruioan.me
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/*---------------------------------------------------------------------------*/
31#ifndef MQTT_PROP_H_
32#define MQTT_PROP_H_
33/*---------------------------------------------------------------------------*/
34#include "mqtt.h"
35
36#include <stdarg.h>
37/*---------------------------------------------------------------------------*/
38/* If not using memb, you must provide a pointer to
39 * statically-allocated memory to register_prop()
40 */
41#ifdef MQTT_PROP_CONF_PROP_USE_MEMB
42#define MQTT_PROP_USE_MEMB MQTT_PROP_CONF_PROP_USE_MEMB
43#else
44#define MQTT_PROP_USE_MEMB 1
45#endif
46/*---------------------------------------------------------------------------*/
47/* Number of output property lists */
48#define MQTT_PROP_MAX_OUT_PROP_LISTS 1
49
50/* Number of output properties that will be declared, regardless of
51 * message type
52 */
53#define MQTT_PROP_MAX_OUT_PROPS 2
54
55/* Max length of 1 property in bytes */
56#define MQTT_PROP_MAX_PROP_LENGTH 32
57/* Max number of bytes in Variable Byte Integer representation of
58 * total property length
59 */
60#define MQTT_PROP_MAX_PROP_LEN_BYTES 2
61/* Max number of topic aliases (when receiving) */
62#define MQTT_PROP_MAX_NUM_TOPIC_ALIASES 1
63
64#define MQTT_PROP_LIST_NONE NULL
65/*----------------------------------------------------------------------------*/
66struct mqtt_prop_list {
67 /* Total length of properties */
68 uint32_t properties_len;
69 uint8_t properties_len_enc[MQTT_PROP_MAX_PROP_LEN_BYTES];
70 uint8_t properties_len_enc_bytes;
71 LIST_STRUCT(props);
72};
73
74/* This struct represents output packet Properties (MQTTv5.0). */
75struct mqtt_prop_out_property {
76 /* Used by the list interface, must be first in the struct. */
77 struct mqtt_prop_out_property *next;
78
79 /* Property identifier (as an MQTT Variable Byte Integer)
80 * The maximum ID is currently 0x2A so 1 byte is sufficient
81 * (the range of 1 VBI byte is 0x00 - 0x7F)
82 */
83 mqtt_vhdr_prop_t id;
84 /* Property length */
85 uint32_t property_len;
86 /* Property value */
87 uint8_t val[MQTT_PROP_MAX_PROP_LENGTH];
88};
89
90struct mqtt_prop_bin_data {
91 uint16_t len;
92 uint8_t data[MQTT_PROP_MAX_PROP_LENGTH];
93};
94
95struct mqtt_prop_auth_event {
96 struct mqtt_string auth_method;
97 struct mqtt_prop_bin_data auth_data;
98};
99/*----------------------------------------------------------------------------*/
100void mqtt_prop_print_input_props(struct mqtt_connection *conn);
101
102uint32_t mqtt_prop_encode(struct mqtt_prop_out_property **prop_out, mqtt_vhdr_prop_t prop_id,
103 va_list args);
104
105void mqtt_prop_parse_connack_props(struct mqtt_connection *conn);
106
107void mqtt_prop_parse_auth_props(struct mqtt_connection *conn, struct mqtt_prop_auth_event *event);
108
109void mqtt_prop_decode_input_props(struct mqtt_connection *conn);
110
111/* Switch argument order to avoid undefined behavior from having a type
112 that undergoes argument promotion immediately before ", ...". */
113#if MQTT_PROP_USE_MEMB
114#define mqtt_prop_register(l, out, msg, id, ...) \
115 mqtt_prop_register_internal(l, msg, id, out, __VA_ARGS__)
116#else
117#define mqtt_prop_register(l, prop, out, msg, id, ...) \
118 mqtt_prop_register_internal(l, prop, msg, id, out, __VA_ARGS__)
119#endif /* MQTT_PROP_USE_MEMB */
120
121uint8_t mqtt_prop_register_internal(struct mqtt_prop_list **prop_list,
122#if !MQTT_PROP_USE_MEMB
123 struct mqtt_prop_out_property *prop,
124#endif
125 mqtt_msg_type_t msg,
126 mqtt_vhdr_prop_t prop_id,
127 struct mqtt_prop_out_property **prop_out, ...);
128
129void mqtt_prop_create_list(struct mqtt_prop_list **prop_list_out);
130
131void mqtt_prop_print_list(struct mqtt_prop_list *prop_list, mqtt_vhdr_prop_t prop_id);
132
133void mqtt_prop_clear_list(struct mqtt_prop_list **prop_list);
134
135void mqtt_props_init();
136/*---------------------------------------------------------------------------*/
137#endif /* MQTT_PROP_H_ */
138/*---------------------------------------------------------------------------*/
#define LIST_STRUCT(name)
Declare a linked list inside a structure declaraction.
Definition list.h:112
Header file for the Contiki MQTT engine.