Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @file include/coap.h
* @brief CoAP message parser
* @date 2022-11-30
*
* @copyright Copyright (c) 2022
*
*/
#ifndef _PROTOCOL_PARSERS_COAP_
#define _PROTOCOL_PARSERS_COAP_
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
#include "http.h"
/**
* @brief CoAP message type
*/
typedef enum
{
COAP_CON = 0,
COAP_NON = 1,
COAP_ACK = 2,
COAP_RST = 3
} coap_type_t;
/**
* @brief CoAP Option number
*/
typedef enum
{
COAP_URI_PATH = 11,
COAP_URI_QUERY = 15
} coap_option_t;
/**
* @brief Abstraction of a CoAP message
*/
typedef struct coap_message
{
coap_type_t type; // CoAP message type
http_method_t method; // CoAP method, analogous to HTTP
char *uri; // Message URI
uint16_t uri_len; // URI length
} coap_message_t;
////////// FUNCTIONS //////////
///// PARSING /////
/**
* @brief Parse a CoAP message.
*
* @param data pointer to the start of the CoAP message
* @param length length of the CoAP message, in bytes
* @return the parsed CoAP message
*/
coap_message_t coap_parse_message(uint8_t *data, uint16_t length);
/**
* @brief Check if a CoAP message is a request.
*
* A CoAP message is a request if its type is Confirmable or Non-Confirmable.
*
* @param coap_message CoAP message to check
* @return true if the given CoAP message is a request
* @return false if the given CoAP message is a response
*/
bool coap_is_request(coap_message_t coap_message);
///// DESTROY /////
/**
* @brief Free the memory allocated for a CoAP message.
*
* @param message the CoAP message to free
*/
void coap_free_message(coap_message_t message);
///// PRINTING /////
/**
* @brief Print a CoAP message.
*
* @param message the CoAP message to print
*/
void coap_print_message(coap_message_t message);
#endif /* _PROTOCOL_PARSERS_COAP_ */