h
Dependencies: C027_Support mbed-dev
Fork of C027_SupportTest_coapp by
Coap/coap_msg.h@40:da5f84f19a24, 2017-08-25 (annotated)
- Committer:
- iftaziz
- Date:
- Fri Aug 25 10:40:13 2017 +0000
- Revision:
- 40:da5f84f19a24
- Parent:
- 34:d6ce8f961b8b
iazi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ranaumarnaeem | 34:d6ce8f961b8b | 1 | /* |
ranaumarnaeem | 34:d6ce8f961b8b | 2 | * Copyright (c) 2015 Keith Cullen. |
ranaumarnaeem | 34:d6ce8f961b8b | 3 | * All Rights Reserved. |
ranaumarnaeem | 34:d6ce8f961b8b | 4 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 5 | * Redistribution and use in source and binary forms, with or without |
ranaumarnaeem | 34:d6ce8f961b8b | 6 | * modification, are permitted provided that the following conditions |
ranaumarnaeem | 34:d6ce8f961b8b | 7 | * are met: |
ranaumarnaeem | 34:d6ce8f961b8b | 8 | * 1. Redistributions of source code must retain the above copyright |
ranaumarnaeem | 34:d6ce8f961b8b | 9 | * notice, this list of conditions and the following disclaimer. |
ranaumarnaeem | 34:d6ce8f961b8b | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
ranaumarnaeem | 34:d6ce8f961b8b | 11 | * notice, this list of conditions and the following disclaimer in the |
ranaumarnaeem | 34:d6ce8f961b8b | 12 | * documentation and/or other materials provided with the distribution. |
ranaumarnaeem | 34:d6ce8f961b8b | 13 | * 3. The name of the author may not be used to endorse or promote products |
ranaumarnaeem | 34:d6ce8f961b8b | 14 | * derived from this software without specific prior written permission. |
ranaumarnaeem | 34:d6ce8f961b8b | 15 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR |
ranaumarnaeem | 34:d6ce8f961b8b | 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
ranaumarnaeem | 34:d6ce8f961b8b | 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
ranaumarnaeem | 34:d6ce8f961b8b | 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
ranaumarnaeem | 34:d6ce8f961b8b | 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
ranaumarnaeem | 34:d6ce8f961b8b | 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
ranaumarnaeem | 34:d6ce8f961b8b | 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
ranaumarnaeem | 34:d6ce8f961b8b | 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
ranaumarnaeem | 34:d6ce8f961b8b | 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
ranaumarnaeem | 34:d6ce8f961b8b | 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
ranaumarnaeem | 34:d6ce8f961b8b | 26 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 27 | |
ranaumarnaeem | 34:d6ce8f961b8b | 28 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 29 | * @file coap_msg.h |
ranaumarnaeem | 34:d6ce8f961b8b | 30 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 31 | * @brief Include file for the FreeCoAP message parser/formatter library |
ranaumarnaeem | 34:d6ce8f961b8b | 32 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 33 | |
ranaumarnaeem | 34:d6ce8f961b8b | 34 | #ifndef COAP_MSG_H |
ranaumarnaeem | 34:d6ce8f961b8b | 35 | #define COAP_MSG_H |
ranaumarnaeem | 34:d6ce8f961b8b | 36 | |
ranaumarnaeem | 34:d6ce8f961b8b | 37 | #include <stddef.h> |
ranaumarnaeem | 34:d6ce8f961b8b | 38 | |
ranaumarnaeem | 34:d6ce8f961b8b | 39 | #define htons(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) |
ranaumarnaeem | 34:d6ce8f961b8b | 40 | #define ntohs(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) |
ranaumarnaeem | 34:d6ce8f961b8b | 41 | |
ranaumarnaeem | 34:d6ce8f961b8b | 42 | #define COAP_MSG_VER 0x01 /**< CoAP version */ |
ranaumarnaeem | 34:d6ce8f961b8b | 43 | #define COAP_MSG_MAX_TOKEN_LEN 8 /**< Maximum token length */ |
ranaumarnaeem | 34:d6ce8f961b8b | 44 | #define COAP_MSG_MAX_CODE_CLASS 7 /**< Maximum code class */ |
ranaumarnaeem | 34:d6ce8f961b8b | 45 | #define COAP_MSG_MAX_CODE_DETAIL 31 /**< Maximum code detail */ |
ranaumarnaeem | 34:d6ce8f961b8b | 46 | #define COAP_MSG_MAX_MSG_ID ((1 << 16) - 1) /**< Maximum message ID */ |
ranaumarnaeem | 34:d6ce8f961b8b | 47 | |
ranaumarnaeem | 34:d6ce8f961b8b | 48 | #define COAP_MSG_OP_URI_PATH_NUM 11 /**< Uri-path option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 49 | #define COAP_MSG_OP_URI_PATH_MAX_LEN 256 /**< Maximum buffer length for a reconstructed URI path */ |
ranaumarnaeem | 34:d6ce8f961b8b | 50 | |
ranaumarnaeem | 34:d6ce8f961b8b | 51 | #define COAP_MSG_MAX_BUF_LEN 1152 /**< Maximum buffer length for header and payload */ |
ranaumarnaeem | 34:d6ce8f961b8b | 52 | |
ranaumarnaeem | 34:d6ce8f961b8b | 53 | #define coap_msg_op_num_is_critical(num) ((num) & 1) /**< Indicate if an option is critical */ |
ranaumarnaeem | 34:d6ce8f961b8b | 54 | #define coap_msg_op_num_is_unsafe(num) ((num) & 2) /**< Indicate if an option is unsafe to forward */ |
ranaumarnaeem | 34:d6ce8f961b8b | 55 | #define coap_msg_op_num_no_cache_key(num) ((num & 0x1e) == 0x1c) /**< Indicate if an option is not part of the cache key */ |
ranaumarnaeem | 34:d6ce8f961b8b | 56 | |
ranaumarnaeem | 34:d6ce8f961b8b | 57 | #define coap_msg_op_get_num(op) ((op)->num) /**< Get the option number from an option */ |
ranaumarnaeem | 34:d6ce8f961b8b | 58 | #define coap_msg_op_set_num(op, num) ((op)->num = (num)) /**< Set the option number in an option */ |
ranaumarnaeem | 34:d6ce8f961b8b | 59 | #define coap_msg_op_get_len(op) ((op)->len) /**< Get the option length from an option */ |
ranaumarnaeem | 34:d6ce8f961b8b | 60 | #define coap_msg_op_set_len(op, len) ((op)->len = (len)) /**< Set the option length in an option */ |
ranaumarnaeem | 34:d6ce8f961b8b | 61 | #define coap_msg_op_get_val(op) ((op)->val) /**< Get the option value from an option */ |
ranaumarnaeem | 34:d6ce8f961b8b | 62 | #define coap_msg_op_set_val(op, val) ((op)->val = (val)) /**< Set the option value in an option */ |
ranaumarnaeem | 34:d6ce8f961b8b | 63 | #define coap_msg_op_get_next(op) ((op)->next) /**< Get the next pointer from an option */ |
ranaumarnaeem | 34:d6ce8f961b8b | 64 | #define coap_msg_op_set_next(op, next_op) ((op)->next = (next_op)) /**< Set the next pointer in an option */ |
ranaumarnaeem | 34:d6ce8f961b8b | 65 | |
ranaumarnaeem | 34:d6ce8f961b8b | 66 | #define coap_msg_get_ver(msg) ((msg)->ver) /**< Get the version from a message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 67 | #define coap_msg_get_type(msg) ((msg)->type) /**< Get the type from a message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 68 | #define coap_msg_get_token_len(msg) ((msg)->token_len) /**< Get the token length from a message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 69 | #define coap_msg_get_code_class(msg) ((msg)->code_class) /**< Get the code class from a message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 70 | #define coap_msg_get_code_detail(msg) ((msg)->code_detail) /**< Get the code detail from a message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 71 | #define coap_msg_get_msg_id(msg) ((msg)->msg_id) /**< Get the message ID from message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 72 | #define coap_msg_get_token(msg) ((msg)->token) /**< Get the token from a message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 73 | #define coap_msg_get_first_op(msg) ((msg)->op_list.first) /**< Get the first option from a message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 74 | #define coap_msg_get_payload(msg) ((msg)->payload) /**< Get the payload from a message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 75 | #define coap_msg_get_payload_len(msg) ((msg)->payload_len) /**< Get the payload length from a message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 76 | #define coap_msg_is_empty(msg) (((msg)->code_class == 0) && ((msg)->code_detail == 0)) |
ranaumarnaeem | 34:d6ce8f961b8b | 77 | /**< Indicate if a message is empty */ |
ranaumarnaeem | 34:d6ce8f961b8b | 78 | |
ranaumarnaeem | 34:d6ce8f961b8b | 79 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 80 | * @brief Message type enumeration |
ranaumarnaeem | 34:d6ce8f961b8b | 81 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 82 | typedef enum |
ranaumarnaeem | 34:d6ce8f961b8b | 83 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 84 | COAP_MSG_CON = 0x0, /**< Confirmable message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 85 | COAP_MSG_NON = 0x1, /**< Non-confirmable message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 86 | COAP_MSG_ACK = 0x2, /**< Acknowledgement message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 87 | COAP_MSG_RST = 0x3 /**< Reset message */ |
ranaumarnaeem | 34:d6ce8f961b8b | 88 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 89 | coap_msg_type_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 90 | |
ranaumarnaeem | 34:d6ce8f961b8b | 91 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 92 | * @brief Code class enumeration |
ranaumarnaeem | 34:d6ce8f961b8b | 93 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 94 | typedef enum |
ranaumarnaeem | 34:d6ce8f961b8b | 95 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 96 | COAP_MSG_REQ = 0, /**< Request */ |
ranaumarnaeem | 34:d6ce8f961b8b | 97 | COAP_MSG_SUCCESS = 2, /**< Success response */ |
ranaumarnaeem | 34:d6ce8f961b8b | 98 | COAP_MSG_CLIENT_ERR = 4, /**< Client error response */ |
ranaumarnaeem | 34:d6ce8f961b8b | 99 | COAP_MSG_SERVER_ERR = 5, /**< Server error response */ |
ranaumarnaeem | 34:d6ce8f961b8b | 100 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 101 | coap_msg_class_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 102 | |
ranaumarnaeem | 34:d6ce8f961b8b | 103 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 104 | * @brief Code detail enumeration |
ranaumarnaeem | 34:d6ce8f961b8b | 105 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 106 | typedef enum |
ranaumarnaeem | 34:d6ce8f961b8b | 107 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 108 | COAP_MSG_GET = 1, /**< Get request method */ |
ranaumarnaeem | 34:d6ce8f961b8b | 109 | COAP_MSG_POST = 2, /**< Post request method */ |
ranaumarnaeem | 34:d6ce8f961b8b | 110 | COAP_MSG_PUT = 3, /**< Put request method */ |
ranaumarnaeem | 34:d6ce8f961b8b | 111 | COAP_MSG_DELETE =4 /**< Delete request method */ |
ranaumarnaeem | 34:d6ce8f961b8b | 112 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 113 | coap_msg_method_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 114 | |
ranaumarnaeem | 34:d6ce8f961b8b | 115 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 116 | * @brief Success response code detail enumeration |
ranaumarnaeem | 34:d6ce8f961b8b | 117 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 118 | typedef enum |
ranaumarnaeem | 34:d6ce8f961b8b | 119 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 120 | COAP_MSG_CREATED = 1, /**< Created success response */ |
ranaumarnaeem | 34:d6ce8f961b8b | 121 | COAP_MSG_DELETED = 2, /**< Deleted success response */ |
ranaumarnaeem | 34:d6ce8f961b8b | 122 | COAP_MSG_VALID = 3, /**< Valid success response */ |
ranaumarnaeem | 34:d6ce8f961b8b | 123 | COAP_MSG_CHANGED = 4, /**< Changed success response */ |
ranaumarnaeem | 34:d6ce8f961b8b | 124 | COAP_MSG_CONTENT = 5 /**< Content success response */ |
ranaumarnaeem | 34:d6ce8f961b8b | 125 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 126 | coap_msg_success_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 127 | |
ranaumarnaeem | 34:d6ce8f961b8b | 128 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 129 | * @brief Client error response code detail enumeration |
ranaumarnaeem | 34:d6ce8f961b8b | 130 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 131 | typedef enum |
ranaumarnaeem | 34:d6ce8f961b8b | 132 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 133 | COAP_MSG_BAD_REQ = 0, /**< Bad request client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 134 | COAP_MSG_UNAUTHORIZED = 1, /**< Unauthorized client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 135 | COAP_MSG_BAD_OPTION = 2, /**< Bad option client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 136 | COAP_MSG_FORBIDDEN = 3, /**< Forbidden client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 137 | COAP_MSG_NOT_FOUND = 4, /**< Not found client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 138 | COAP_MSG_METHOD_NOT_ALLOWED = 5, /**< Method not allowed client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 139 | COAP_MSG_NOT_ACCEPTABLE = 6, /**< Not acceptable client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 140 | COAP_MSG_PRECOND_FAILED = 12, /**< Precondition failed client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 141 | COAP_MSG_REQ_ENT_TOO_LARGE = 13, /**< Request entity too large client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 142 | COAP_MSG_UNSUP_CONT_FMT = 15 /**< Unsupported content-format client error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 143 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 144 | coap_msg_client_err_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 145 | |
ranaumarnaeem | 34:d6ce8f961b8b | 146 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 147 | * @brief Server error response code detail enumeration |
ranaumarnaeem | 34:d6ce8f961b8b | 148 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 149 | typedef enum |
ranaumarnaeem | 34:d6ce8f961b8b | 150 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 151 | COAP_MSG_INT_SERVER_ERR = 0, /**< Internal server error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 152 | COAP_MSG_NOT_IMPL = 1, /**< Not implemented server error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 153 | COAP_MSG_BAD_GATEWAY = 2, /**< Bad gateway server error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 154 | COAP_MSG_SERV_UNAVAIL = 3, /**< Service unavailable server error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 155 | COAP_MSG_GATEWAY_TIMEOUT = 4, /**< Gateway timeout server error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 156 | COAP_MSG_PROXY_NOT_SUP = 5 /**< Proxying not supported server error */ |
ranaumarnaeem | 34:d6ce8f961b8b | 157 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 158 | coap_msg_server_err_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 159 | |
ranaumarnaeem | 34:d6ce8f961b8b | 160 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 161 | * @brief Option number enumeration |
ranaumarnaeem | 34:d6ce8f961b8b | 162 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 163 | typedef enum |
ranaumarnaeem | 34:d6ce8f961b8b | 164 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 165 | COAP_MSG_IF_MATCH = 1, /**< If-Match option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 166 | COAP_MSG_URI_HOST = 3, /**< URI-Host option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 167 | COAP_MSG_ETAG = 4, /**< Entity-Tag option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 168 | COAP_MSG_IF_NONE_MATCH = 5, /**< If-None-Match option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 169 | COAP_MSG_URI_PORT = 7, /**< URI-Port option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 170 | COAP_MSG_LOCATION_PATH = 8, /**< Location-Path option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 171 | COAP_MSG_URI_PATH = 11, /**< URI-Path option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 172 | COAP_MSG_CONTENT_FORMAT = 12, /**< Content-Format option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 173 | COAP_MSG_MAX_AGE = 14, /**< Max-Age option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 174 | COAP_MSG_URI_QUERY = 15, /**< URI-Query option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 175 | COAP_MSG_ACCEPT = 17, /**< Accept option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 176 | COAP_MSG_LOCATION_QUERY = 20, /**< Location-Query option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 177 | COAP_MSG_PROXY_URI = 35, /**< Proxy-URI option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 178 | COAP_MSG_PROXY_SCHEME = 39, /**< Proxy-Scheme option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 179 | COAP_MSG_SIZE1 = 60 /**< Size1 option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 180 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 181 | coap_msg_op_num_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 182 | |
ranaumarnaeem | 34:d6ce8f961b8b | 183 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 184 | * @brief Option structure |
ranaumarnaeem | 34:d6ce8f961b8b | 185 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 186 | typedef struct coap_msg_op |
ranaumarnaeem | 34:d6ce8f961b8b | 187 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 188 | unsigned num; /**< Option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 189 | unsigned len; /**< Option length */ |
ranaumarnaeem | 34:d6ce8f961b8b | 190 | char *val; /**< Pointer to a buffer containing the option value */ |
ranaumarnaeem | 34:d6ce8f961b8b | 191 | struct coap_msg_op *next; /**< Pointer to the next option structure in the list */ |
ranaumarnaeem | 34:d6ce8f961b8b | 192 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 193 | coap_msg_op_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 194 | |
ranaumarnaeem | 34:d6ce8f961b8b | 195 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 196 | * @brief Option linked-list structure |
ranaumarnaeem | 34:d6ce8f961b8b | 197 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 198 | typedef struct |
ranaumarnaeem | 34:d6ce8f961b8b | 199 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 200 | coap_msg_op_t *first; /**< Pointer to the first option structure in the list */ |
ranaumarnaeem | 34:d6ce8f961b8b | 201 | coap_msg_op_t *last; /**< Pointer to the last option structure in the list */ |
ranaumarnaeem | 34:d6ce8f961b8b | 202 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 203 | coap_msg_op_list_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 204 | |
ranaumarnaeem | 34:d6ce8f961b8b | 205 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 206 | * @brief Message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 207 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 208 | typedef struct |
ranaumarnaeem | 34:d6ce8f961b8b | 209 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 210 | unsigned ver; /**< CoAP version */ |
ranaumarnaeem | 34:d6ce8f961b8b | 211 | coap_msg_type_t type; /**< Message type */ |
ranaumarnaeem | 34:d6ce8f961b8b | 212 | unsigned token_len; /**< Token length */ |
ranaumarnaeem | 34:d6ce8f961b8b | 213 | unsigned code_class; /**< Code class */ |
ranaumarnaeem | 34:d6ce8f961b8b | 214 | unsigned code_detail; /**< Code detail */ |
ranaumarnaeem | 34:d6ce8f961b8b | 215 | unsigned msg_id; /**< Message ID */ |
ranaumarnaeem | 34:d6ce8f961b8b | 216 | char token[COAP_MSG_MAX_TOKEN_LEN]; /**< Token value */ |
ranaumarnaeem | 34:d6ce8f961b8b | 217 | coap_msg_op_list_t op_list; /**< Option list */ |
ranaumarnaeem | 34:d6ce8f961b8b | 218 | char *payload; /**< Pointer to a buffer containing the payload */ |
ranaumarnaeem | 34:d6ce8f961b8b | 219 | size_t payload_len; /**< Length of the payload */ |
ranaumarnaeem | 34:d6ce8f961b8b | 220 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 221 | coap_msg_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 222 | |
ranaumarnaeem | 34:d6ce8f961b8b | 223 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 224 | * @brief Check if option is recognized |
ranaumarnaeem | 34:d6ce8f961b8b | 225 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 226 | * @param[in] num Option number |
ranaumarnaeem | 34:d6ce8f961b8b | 227 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 228 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 229 | * @retval 1 Option is recognized |
ranaumarnaeem | 34:d6ce8f961b8b | 230 | * @retval 0 Option is not recognized |
ranaumarnaeem | 34:d6ce8f961b8b | 231 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 232 | int coap_msg_op_num_is_recognized(unsigned num); |
ranaumarnaeem | 34:d6ce8f961b8b | 233 | |
ranaumarnaeem | 34:d6ce8f961b8b | 234 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 235 | * @brief Generate a random string of bytes |
ranaumarnaeem | 34:d6ce8f961b8b | 236 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 237 | * @param[out] buf Pointer to the buffer to store the random string |
ranaumarnaeem | 34:d6ce8f961b8b | 238 | * @param[in] len Length of the buffer |
ranaumarnaeem | 34:d6ce8f961b8b | 239 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 240 | void coap_msg_gen_rand_str(char *buf, size_t len); |
ranaumarnaeem | 34:d6ce8f961b8b | 241 | |
ranaumarnaeem | 34:d6ce8f961b8b | 242 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 243 | * @brief Initialise a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 244 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 245 | * @param[out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 246 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 247 | void coap_msg_create(coap_msg_t *msg); |
ranaumarnaeem | 34:d6ce8f961b8b | 248 | |
ranaumarnaeem | 34:d6ce8f961b8b | 249 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 250 | * @brief Deinitialise a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 251 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 252 | * @param[in,out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 253 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 254 | void coap_msg_destroy(coap_msg_t *msg); |
ranaumarnaeem | 34:d6ce8f961b8b | 255 | |
ranaumarnaeem | 34:d6ce8f961b8b | 256 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 257 | * @brief Deinitialise and initialise a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 258 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 259 | * @param[in,out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 260 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 261 | void coap_msg_reset(coap_msg_t *msg); |
ranaumarnaeem | 34:d6ce8f961b8b | 262 | |
ranaumarnaeem | 34:d6ce8f961b8b | 263 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 264 | * @brief Check that all of the critical options in a message are recognized |
ranaumarnaeem | 34:d6ce8f961b8b | 265 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 266 | * @param[in] msg Pointer to message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 267 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 268 | * @returns Operation status or bad option number |
ranaumarnaeem | 34:d6ce8f961b8b | 269 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 270 | * @retval >0 Bad option number |
ranaumarnaeem | 34:d6ce8f961b8b | 271 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 272 | unsigned coap_msg_check_critical_ops(coap_msg_t *msg); |
ranaumarnaeem | 34:d6ce8f961b8b | 273 | |
ranaumarnaeem | 34:d6ce8f961b8b | 274 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 275 | * @brief Check that all of the unsafe options in a message are recognized |
ranaumarnaeem | 34:d6ce8f961b8b | 276 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 277 | * @param[in] msg Pointer to message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 278 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 279 | * @returns Operation status or bad option number |
ranaumarnaeem | 34:d6ce8f961b8b | 280 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 281 | * @retval >0 Bad option number |
ranaumarnaeem | 34:d6ce8f961b8b | 282 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 283 | unsigned coap_msg_check_unsafe_ops(coap_msg_t *msg); |
ranaumarnaeem | 34:d6ce8f961b8b | 284 | |
ranaumarnaeem | 34:d6ce8f961b8b | 285 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 286 | * @brief Extract the type and message ID values from a message |
ranaumarnaeem | 34:d6ce8f961b8b | 287 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 288 | * If a message contains a format error, this function |
ranaumarnaeem | 34:d6ce8f961b8b | 289 | * will attempt to extract the type and message ID so |
ranaumarnaeem | 34:d6ce8f961b8b | 290 | * that a reset message can be returned to the sender. |
ranaumarnaeem | 34:d6ce8f961b8b | 291 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 292 | * @param[in] buf Pointer to a buffer containing the message |
ranaumarnaeem | 34:d6ce8f961b8b | 293 | * @param[in] len Length of the buffer |
ranaumarnaeem | 34:d6ce8f961b8b | 294 | * @param[out] type Pointer to field to store the type value |
ranaumarnaeem | 34:d6ce8f961b8b | 295 | * @param[out] msg_id Pointer to a field to store the message ID value |
ranaumarnaeem | 34:d6ce8f961b8b | 296 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 297 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 298 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 299 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 300 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 301 | int coap_msg_parse_type_msg_id(char *buf, size_t len, unsigned *type, unsigned *msg_id); |
ranaumarnaeem | 34:d6ce8f961b8b | 302 | |
ranaumarnaeem | 34:d6ce8f961b8b | 303 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 304 | * @brief Parse a message |
ranaumarnaeem | 34:d6ce8f961b8b | 305 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 306 | * @param[in,out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 307 | * @param[in] buf Pointer to a buffer containing the message |
ranaumarnaeem | 34:d6ce8f961b8b | 308 | * @param[in] len Length of the buffer |
ranaumarnaeem | 34:d6ce8f961b8b | 309 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 310 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 311 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 312 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 313 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 314 | int coap_msg_parse(coap_msg_t *msg, char *buf, size_t len); |
ranaumarnaeem | 34:d6ce8f961b8b | 315 | |
ranaumarnaeem | 34:d6ce8f961b8b | 316 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 317 | * @brief Set the type in a message |
ranaumarnaeem | 34:d6ce8f961b8b | 318 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 319 | * @param[out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 320 | * @param[in] type Message type |
ranaumarnaeem | 34:d6ce8f961b8b | 321 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 322 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 323 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 324 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 325 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 326 | int coap_msg_set_type(coap_msg_t *msg, unsigned type); |
ranaumarnaeem | 34:d6ce8f961b8b | 327 | |
ranaumarnaeem | 34:d6ce8f961b8b | 328 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 329 | * @brief Set the code in a message |
ranaumarnaeem | 34:d6ce8f961b8b | 330 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 331 | * @param[out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 332 | * @param[in] code_class Code class |
ranaumarnaeem | 34:d6ce8f961b8b | 333 | * @param[in] code_detail Code detail |
ranaumarnaeem | 34:d6ce8f961b8b | 334 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 335 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 336 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 337 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 338 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 339 | int coap_msg_set_code(coap_msg_t *msg, unsigned code_class, unsigned code_detail); |
ranaumarnaeem | 34:d6ce8f961b8b | 340 | |
ranaumarnaeem | 34:d6ce8f961b8b | 341 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 342 | * @brief Set the message ID in a message |
ranaumarnaeem | 34:d6ce8f961b8b | 343 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 344 | * @param[out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 345 | * @param[in] msg_id Message ID |
ranaumarnaeem | 34:d6ce8f961b8b | 346 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 347 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 348 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 349 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 350 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 351 | int coap_msg_set_msg_id(coap_msg_t *msg, unsigned msg_id); |
ranaumarnaeem | 34:d6ce8f961b8b | 352 | |
ranaumarnaeem | 34:d6ce8f961b8b | 353 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 354 | * @brief Set the token in a message |
ranaumarnaeem | 34:d6ce8f961b8b | 355 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 356 | * @param[out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 357 | * @param[in] buf Pointer to a buffer containing the token |
ranaumarnaeem | 34:d6ce8f961b8b | 358 | * @param[in] len Length of the buffer |
ranaumarnaeem | 34:d6ce8f961b8b | 359 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 360 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 361 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 362 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 363 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 364 | int coap_msg_set_token(coap_msg_t *msg, char *buf, size_t len); |
ranaumarnaeem | 34:d6ce8f961b8b | 365 | |
ranaumarnaeem | 34:d6ce8f961b8b | 366 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 367 | * @brief Add a token to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 368 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 369 | * @param[in,out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 370 | * @param[in] num Option number |
ranaumarnaeem | 34:d6ce8f961b8b | 371 | * @param[in] len Option length |
ranaumarnaeem | 34:d6ce8f961b8b | 372 | * @param[in] val Pointer to a buffer containing the option value |
ranaumarnaeem | 34:d6ce8f961b8b | 373 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 374 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 375 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 376 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 377 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 378 | int coap_msg_add_op(coap_msg_t *msg, unsigned num, unsigned len, const char *val); |
ranaumarnaeem | 34:d6ce8f961b8b | 379 | |
ranaumarnaeem | 34:d6ce8f961b8b | 380 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 381 | * @brief Set the payload in a message |
ranaumarnaeem | 34:d6ce8f961b8b | 382 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 383 | * Free the buffer in the message structure containing |
ranaumarnaeem | 34:d6ce8f961b8b | 384 | * the current payload if there is one, allocate a buffer |
ranaumarnaeem | 34:d6ce8f961b8b | 385 | * to contain the new payload and copy the buffer argument |
ranaumarnaeem | 34:d6ce8f961b8b | 386 | * into the new payload buffer. |
ranaumarnaeem | 34:d6ce8f961b8b | 387 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 388 | * @param[in,out] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 389 | * @param[in] buf Pointer to a buffer containing the payload |
ranaumarnaeem | 34:d6ce8f961b8b | 390 | * @param[in] len Length of the buffer |
ranaumarnaeem | 34:d6ce8f961b8b | 391 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 392 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 393 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 394 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 395 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 396 | int coap_msg_set_payload(coap_msg_t *msg, char *buf, size_t len); |
ranaumarnaeem | 34:d6ce8f961b8b | 397 | |
ranaumarnaeem | 34:d6ce8f961b8b | 398 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 399 | * @brief Format a message |
ranaumarnaeem | 34:d6ce8f961b8b | 400 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 401 | * @param[in] msg Pointer to a message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 402 | * @param[out] buf Pointer to a buffer to contain the formatted message |
ranaumarnaeem | 34:d6ce8f961b8b | 403 | * @param[in] len Length of the buffer |
ranaumarnaeem | 34:d6ce8f961b8b | 404 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 405 | * @returns Length of the formatted message or error code |
ranaumarnaeem | 34:d6ce8f961b8b | 406 | * @retval >0 Length of the formatted message |
ranaumarnaeem | 34:d6ce8f961b8b | 407 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 408 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 409 | int coap_msg_format(coap_msg_t *msg, char *buf, size_t len); |
ranaumarnaeem | 34:d6ce8f961b8b | 410 | |
ranaumarnaeem | 34:d6ce8f961b8b | 411 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 412 | * @brief Copy a message |
ranaumarnaeem | 34:d6ce8f961b8b | 413 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 414 | * @param[in,out] dst Pointer to the destination message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 415 | * @param[in] src Pointer to the source message structure |
ranaumarnaeem | 34:d6ce8f961b8b | 416 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 417 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 418 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 419 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 420 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 421 | int coap_msg_copy(coap_msg_t *dst, coap_msg_t *src); |
ranaumarnaeem | 34:d6ce8f961b8b | 422 | |
ranaumarnaeem | 34:d6ce8f961b8b | 423 | #endif |
ranaumarnaeem | 34:d6ce8f961b8b | 424 |