| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef AWS_MQTT_MQTT5_ENCODER_H |
| 7 |
#define AWS_MQTT_MQTT5_ENCODER_H |
| 8 |
|
| 9 |
#include <aws/mqtt/mqtt.h> |
| 10 |
|
| 11 |
#include <aws/common/array_list.h> |
| 12 |
#include <aws/common/byte_buf.h> |
| 13 |
|
| 14 |
#include <aws/mqtt/v5/mqtt5_types.h> |
| 15 |
|
| 16 |
struct aws_mqtt5_client; |
| 17 |
struct aws_mqtt5_encoder; |
| 18 |
struct aws_mqtt5_outbound_topic_alias_resolver; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
enum aws_mqtt5_encoding_step_type { |
| 30 |
|
| 31 |
AWS_MQTT5_EST_U8, |
| 32 |
|
| 33 |
|
| 34 |
AWS_MQTT5_EST_U16, |
| 35 |
|
| 36 |
|
| 37 |
AWS_MQTT5_EST_U32, |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
AWS_MQTT5_EST_VLI, |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
AWS_MQTT5_EST_CURSOR, |
| 51 |
|
| 52 |
|
| 53 |
AWS_MQTT5_EST_STREAM, |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
struct aws_mqtt5_encoding_step { |
| 60 |
enum aws_mqtt5_encoding_step_type type; |
| 61 |
union { |
| 62 |
uint8_t value_u8; |
| 63 |
uint16_t value_u16; |
| 64 |
uint32_t value_u32; |
| 65 |
struct aws_byte_cursor value_cursor; |
| 66 |
struct aws_input_stream *value_stream; |
| 67 |
} value; |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
typedef int(aws_mqtt5_encode_begin_packet_type_fn)(struct aws_mqtt5_encoder *encoder, const void *view); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
struct aws_mqtt5_encoder_function_table { |
| 80 |
aws_mqtt5_encode_begin_packet_type_fn *encoders_by_packet_type[16]; |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
struct aws_mqtt5_encoder_options { |
| 87 |
struct aws_mqtt5_client *client; |
| 88 |
const struct aws_mqtt5_encoder_function_table *encoders; |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
struct aws_mqtt5_encoder { |
| 95 |
struct aws_mqtt5_encoder_options config; |
| 96 |
|
| 97 |
struct aws_array_list encoding_steps; |
| 98 |
size_t current_encoding_step_index; |
| 99 |
|
| 100 |
struct aws_mqtt5_outbound_topic_alias_resolver *topic_alias_resolver; |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
enum aws_mqtt5_encoding_result { |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
AWS_MQTT5_ER_ERROR, |
| 123 |
|
| 124 |
|
| 125 |
AWS_MQTT5_ER_FINISHED, |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
AWS_MQTT5_ER_OUT_OF_ROOM, |
| 133 |
}; |
| 134 |
|
| 135 |
AWS_EXTERN_C_BEGIN |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
AWS_MQTT_API int aws_mqtt5_encoder_init( |
| 146 |
struct aws_mqtt5_encoder *encoder, |
| 147 |
struct aws_allocator *allocator, |
| 148 |
struct aws_mqtt5_encoder_options *options); |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
AWS_MQTT_API void aws_mqtt5_encoder_clean_up(struct aws_mqtt5_encoder *encoder); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
AWS_MQTT_API void aws_mqtt5_encoder_reset(struct aws_mqtt5_encoder *encoder); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
AWS_MQTT_API int aws_mqtt5_encoder_append_packet_encoding( |
| 175 |
struct aws_mqtt5_encoder *encoder, |
| 176 |
enum aws_mqtt5_packet_type packet_type, |
| 177 |
const void *packet_view); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
AWS_MQTT_API enum aws_mqtt5_encoding_result aws_mqtt5_encoder_encode_to_buffer( |
| 196 |
struct aws_mqtt5_encoder *encoder, |
| 197 |
struct aws_byte_buf *buffer); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
AWS_MQTT_API void aws_mqtt5_encoder_set_outbound_topic_alias_resolver( |
| 206 |
struct aws_mqtt5_encoder *encoder, |
| 207 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver); |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
AWS_MQTT_API extern const struct aws_mqtt5_encoder_function_table *g_aws_mqtt5_encoder_default_function_table; |
| 214 |
|
| 215 |
AWS_EXTERN_C_END |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
AWS_EXTERN_C_BEGIN |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
AWS_MQTT_API int aws_mqtt5_packet_view_get_encoded_size( |
| 233 |
enum aws_mqtt5_packet_type packet_type, |
| 234 |
const void *packet_view, |
| 235 |
size_t *packet_size); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
AWS_MQTT_API int aws_mqtt5_encode_variable_length_integer(struct aws_byte_buf *buf, uint32_t value); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
AWS_MQTT_API int aws_mqtt5_get_variable_length_encode_size(size_t value, size_t *encode_size); |
| 254 |
|
| 255 |
AWS_MQTT_API void aws_mqtt5_encoder_push_step_u8(struct aws_mqtt5_encoder *encoder, uint8_t value); |
| 256 |
|
| 257 |
AWS_MQTT_API void aws_mqtt5_encoder_push_step_u16(struct aws_mqtt5_encoder *encoder, uint16_t value); |
| 258 |
|
| 259 |
AWS_MQTT_API void aws_mqtt5_encoder_push_step_u32(struct aws_mqtt5_encoder *encoder, uint32_t value); |
| 260 |
|
| 261 |
AWS_MQTT_API int aws_mqtt5_encoder_push_step_vli(struct aws_mqtt5_encoder *encoder, uint32_t value); |
| 262 |
|
| 263 |
AWS_MQTT_API void aws_mqtt5_encoder_push_step_cursor(struct aws_mqtt5_encoder *encoder, struct aws_byte_cursor value); |
| 264 |
|
| 265 |
AWS_MQTT_API size_t aws_mqtt5_compute_user_property_encode_length( |
| 266 |
const struct aws_mqtt5_user_property *properties, |
| 267 |
size_t user_property_count); |
| 268 |
|
| 269 |
AWS_MQTT_API void aws_mqtt5_add_user_property_encoding_steps( |
| 270 |
struct aws_mqtt5_encoder *encoder, |
| 271 |
const struct aws_mqtt5_user_property *user_properties, |
| 272 |
size_t user_property_count); |
| 273 |
|
| 274 |
AWS_EXTERN_C_END |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
#define ADD_ENCODE_STEP_U8(encoder, value) aws_mqtt5_encoder_push_step_u8(encoder, (uint8_t)(value)) |
| 279 |
#define ADD_ENCODE_STEP_U16(encoder, value) aws_mqtt5_encoder_push_step_u16(encoder, (uint16_t)(value)) |
| 280 |
#define ADD_ENCODE_STEP_U32(encoder, value) aws_mqtt5_encoder_push_step_u32(encoder, (uint32_t)(value)) |
| 281 |
#define ADD_ENCODE_STEP_CURSOR(encoder, cursor) aws_mqtt5_encoder_push_step_cursor(encoder, (cursor)) |
| 282 |
|
| 283 |
#define ADD_ENCODE_STEP_VLI(encoder, value) \ |
| 284 |
if (aws_mqtt5_encoder_push_step_vli(encoder, (value))) { \ |
| 285 |
return AWS_OP_ERR; \ |
| 286 |
} |
| 287 |
|
| 288 |
#define ADD_ENCODE_STEP_LENGTH_PREFIXED_CURSOR(encoder, cursor) \ |
| 289 |
{ \ |
| 290 |
aws_mqtt5_encoder_push_step_u16(encoder, (uint16_t)((cursor).len)); \ |
| 291 |
aws_mqtt5_encoder_push_step_cursor(encoder, (cursor)); \ |
| 292 |
} |
| 293 |
|
| 294 |
#define ADD_ENCODE_STEP_OPTIONAL_LENGTH_PREFIXED_CURSOR(encoder, cursor_ptr) \ |
| 295 |
if (cursor_ptr != NULL) { \ |
| 296 |
ADD_ENCODE_STEP_LENGTH_PREFIXED_CURSOR(encoder, *cursor_ptr); \ |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#define ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY(encoder, property_value, value_ptr) \ |
| 302 |
if ((value_ptr) != NULL) { \ |
| 303 |
ADD_ENCODE_STEP_U8(encoder, property_value); \ |
| 304 |
ADD_ENCODE_STEP_U8(encoder, *(value_ptr)); \ |
| 305 |
} |
| 306 |
|
| 307 |
#define ADD_ENCODE_STEP_OPTIONAL_U16_PROPERTY(encoder, property_value, value_ptr) \ |
| 308 |
if ((value_ptr) != NULL) { \ |
| 309 |
ADD_ENCODE_STEP_U8(encoder, property_value); \ |
| 310 |
ADD_ENCODE_STEP_U16(encoder, *(value_ptr)); \ |
| 311 |
} |
| 312 |
|
| 313 |
#define ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY(encoder, property_value, value_ptr) \ |
| 314 |
if ((value_ptr) != NULL) { \ |
| 315 |
ADD_ENCODE_STEP_U8(encoder, property_value); \ |
| 316 |
ADD_ENCODE_STEP_U32(encoder, *(value_ptr)); \ |
| 317 |
} |
| 318 |
|
| 319 |
#define ADD_ENCODE_STEP_OPTIONAL_VLI_PROPERTY(encoder, property_value, value_ptr) \ |
| 320 |
if ((value_ptr) != NULL) { \ |
| 321 |
ADD_ENCODE_STEP_U8(encoder, property_value); \ |
| 322 |
ADD_ENCODE_STEP_VLI(encoder, *(value_ptr)); \ |
| 323 |
} |
| 324 |
|
| 325 |
#define ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(encoder, property_type, cursor_ptr) \ |
| 326 |
if ((cursor_ptr) != NULL) { \ |
| 327 |
ADD_ENCODE_STEP_U8(encoder, property_type); \ |
| 328 |
ADD_ENCODE_STEP_U16(encoder, (cursor_ptr)->len); \ |
| 329 |
ADD_ENCODE_STEP_CURSOR(encoder, *(cursor_ptr)); \ |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
#define ADD_OPTIONAL_U8_PROPERTY_LENGTH(property_ptr, length) \ |
| 338 |
if ((property_ptr) != NULL) { \ |
| 339 |
(length) += 2; \ |
| 340 |
} |
| 341 |
|
| 342 |
#define ADD_OPTIONAL_U16_PROPERTY_LENGTH(property_ptr, length) \ |
| 343 |
if ((property_ptr) != NULL) { \ |
| 344 |
(length) += 3; \ |
| 345 |
} |
| 346 |
|
| 347 |
#define ADD_OPTIONAL_U32_PROPERTY_LENGTH(property_ptr, length) \ |
| 348 |
if ((property_ptr) != NULL) { \ |
| 349 |
(length) += 5; \ |
| 350 |
} |
| 351 |
|
| 352 |
#define ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(property_ptr, length) \ |
| 353 |
if ((property_ptr) != NULL) { \ |
| 354 |
(length) += 3 + ((property_ptr)->len); \ |
| 355 |
} |
| 356 |
|
| 357 |
#endif |