Adapted to LoRa Semtech + Nucleo
Dependents: LoRaWAN-lmic-app LoRaWAN-lmic-app LoRaWAN-test-10secs LoRaPersonalizedDeviceForEverynet ... more
Fork of cantcoap by
cantcoap.h@2:d0d57af6c9df, 2015-11-20 (annotated)
- Committer:
- pnysten
- Date:
- Fri Nov 20 12:30:26 2015 +0000
- Revision:
- 2:d0d57af6c9df
- Parent:
- 1:5eec2844ad47
?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pnysten | 2:d0d57af6c9df | 1 | #pragma once |
pnysten | 2:d0d57af6c9df | 2 | #pragma clang diagnostic ignored "-Wdeprecated-writable-strings" |
pnysten | 2:d0d57af6c9df | 3 | #pragma clang diagnostic ignored "-Wconstant-logical-operand" |
pnysten | 2:d0d57af6c9df | 4 | #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" |
ashleymills | 0:3d62a105fd34 | 5 | |
pnysten | 2:d0d57af6c9df | 6 | /// Copyright (c) 2013, Ashley Mills. |
pnysten | 2:d0d57af6c9df | 7 | //#include <unistd.h> |
ashleymills | 1:5eec2844ad47 | 8 | #include <stdint.h> |
pnysten | 2:d0d57af6c9df | 9 | #include "dbg.h" |
ashleymills | 0:3d62a105fd34 | 10 | |
ashleymills | 0:3d62a105fd34 | 11 | #define COAP_HDR_SIZE 4 |
ashleymills | 0:3d62a105fd34 | 12 | #define COAP_OPTION_HDR_BYTE 1 |
ashleymills | 0:3d62a105fd34 | 13 | |
ashleymills | 0:3d62a105fd34 | 14 | // CoAP PDU format |
ashleymills | 0:3d62a105fd34 | 15 | |
ashleymills | 0:3d62a105fd34 | 16 | // 0 1 2 3 |
ashleymills | 0:3d62a105fd34 | 17 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
ashleymills | 0:3d62a105fd34 | 18 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
ashleymills | 0:3d62a105fd34 | 19 | // |Ver| T | TKL | Code | Message ID | |
ashleymills | 0:3d62a105fd34 | 20 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
ashleymills | 0:3d62a105fd34 | 21 | // | Token (if any, TKL bytes) ... |
ashleymills | 0:3d62a105fd34 | 22 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
ashleymills | 0:3d62a105fd34 | 23 | // | Options (if any) ... |
ashleymills | 0:3d62a105fd34 | 24 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
ashleymills | 0:3d62a105fd34 | 25 | // |1 1 1 1 1 1 1 1| Payload (if any) ... |
ashleymills | 0:3d62a105fd34 | 26 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
ashleymills | 0:3d62a105fd34 | 27 | |
ashleymills | 0:3d62a105fd34 | 28 | class CoapPDU { |
ashleymills | 0:3d62a105fd34 | 29 | |
ashleymills | 0:3d62a105fd34 | 30 | |
ashleymills | 0:3d62a105fd34 | 31 | public: |
ashleymills | 0:3d62a105fd34 | 32 | /// CoAP message types. Note, values only work as enum. |
ashleymills | 0:3d62a105fd34 | 33 | enum Type { |
ashleymills | 0:3d62a105fd34 | 34 | COAP_CONFIRMABLE=0x00, |
ashleymills | 0:3d62a105fd34 | 35 | COAP_NON_CONFIRMABLE=0x10, |
ashleymills | 0:3d62a105fd34 | 36 | COAP_ACKNOWLEDGEMENT=0x20, |
ashleymills | 0:3d62a105fd34 | 37 | COAP_RESET=0x30 |
ashleymills | 0:3d62a105fd34 | 38 | }; |
ashleymills | 0:3d62a105fd34 | 39 | |
ashleymills | 0:3d62a105fd34 | 40 | // CoAP response codes. |
ashleymills | 0:3d62a105fd34 | 41 | enum Code { |
ashleymills | 0:3d62a105fd34 | 42 | COAP_EMPTY=0x00, |
ashleymills | 0:3d62a105fd34 | 43 | COAP_GET, |
ashleymills | 0:3d62a105fd34 | 44 | COAP_POST, |
ashleymills | 0:3d62a105fd34 | 45 | COAP_PUT, |
ashleymills | 0:3d62a105fd34 | 46 | COAP_DELETE, |
ashleymills | 0:3d62a105fd34 | 47 | COAP_CREATED=0x41, |
ashleymills | 0:3d62a105fd34 | 48 | COAP_DELETED, |
ashleymills | 0:3d62a105fd34 | 49 | COAP_VALID, |
ashleymills | 0:3d62a105fd34 | 50 | COAP_CHANGED, |
ashleymills | 0:3d62a105fd34 | 51 | COAP_CONTENT, |
ashleymills | 0:3d62a105fd34 | 52 | COAP_BAD_REQUEST=0x80, |
ashleymills | 0:3d62a105fd34 | 53 | COAP_UNAUTHORIZED, |
ashleymills | 0:3d62a105fd34 | 54 | COAP_BAD_OPTION, |
ashleymills | 0:3d62a105fd34 | 55 | COAP_FORBIDDEN, |
ashleymills | 0:3d62a105fd34 | 56 | COAP_NOT_FOUND, |
ashleymills | 0:3d62a105fd34 | 57 | COAP_METHOD_NOT_ALLOWED, |
ashleymills | 0:3d62a105fd34 | 58 | COAP_NOT_ACCEPTABLE, |
ashleymills | 0:3d62a105fd34 | 59 | COAP_PRECONDITION_FAILED=0x8C, |
ashleymills | 0:3d62a105fd34 | 60 | COAP_REQUEST_ENTITY_TOO_LARGE=0x8D, |
ashleymills | 0:3d62a105fd34 | 61 | COAP_UNSUPPORTED_CONTENT_FORMAT=0x8F, |
ashleymills | 0:3d62a105fd34 | 62 | COAP_INTERNAL_SERVER_ERROR=0xA0, |
ashleymills | 0:3d62a105fd34 | 63 | COAP_NOT_IMPLEMENTED, |
ashleymills | 0:3d62a105fd34 | 64 | COAP_BAD_GATEWAY, |
ashleymills | 0:3d62a105fd34 | 65 | COAP_SERVICE_UNAVAILABLE, |
ashleymills | 0:3d62a105fd34 | 66 | COAP_GATEWAY_TIMEOUT, |
ashleymills | 1:5eec2844ad47 | 67 | COAP_PROXYING_NOT_SUPPORTED, |
ashleymills | 1:5eec2844ad47 | 68 | COAP_UNDEFINED_CODE=0xFF |
ashleymills | 0:3d62a105fd34 | 69 | }; |
ashleymills | 0:3d62a105fd34 | 70 | |
ashleymills | 0:3d62a105fd34 | 71 | /// CoAP option numbers. |
ashleymills | 0:3d62a105fd34 | 72 | enum Option { |
ashleymills | 0:3d62a105fd34 | 73 | COAP_OPTION_IF_MATCH=1, |
ashleymills | 0:3d62a105fd34 | 74 | COAP_OPTION_URI_HOST=3, |
ashleymills | 0:3d62a105fd34 | 75 | COAP_OPTION_ETAG, |
ashleymills | 0:3d62a105fd34 | 76 | COAP_OPTION_IF_NONE_MATCH, |
ashleymills | 0:3d62a105fd34 | 77 | COAP_OPTION_OBSERVE, |
ashleymills | 0:3d62a105fd34 | 78 | COAP_OPTION_URI_PORT, |
ashleymills | 0:3d62a105fd34 | 79 | COAP_OPTION_LOCATION_PATH, |
ashleymills | 0:3d62a105fd34 | 80 | COAP_OPTION_URI_PATH=11, |
ashleymills | 0:3d62a105fd34 | 81 | COAP_OPTION_CONTENT_FORMAT, |
ashleymills | 0:3d62a105fd34 | 82 | COAP_OPTION_MAX_AGE=14, |
ashleymills | 0:3d62a105fd34 | 83 | COAP_OPTION_URI_QUERY, |
ashleymills | 0:3d62a105fd34 | 84 | COAP_OPTION_ACCEPT=17, |
ashleymills | 0:3d62a105fd34 | 85 | COAP_OPTION_LOCATION_QUERY=20, |
ashleymills | 0:3d62a105fd34 | 86 | COAP_OPTION_BLOCK2=23, |
ashleymills | 0:3d62a105fd34 | 87 | COAP_OPTION_BLOCK1=27, |
ashleymills | 0:3d62a105fd34 | 88 | COAP_OPTION_SIZE2, |
ashleymills | 0:3d62a105fd34 | 89 | COAP_OPTION_PROXY_URI=35, |
ashleymills | 0:3d62a105fd34 | 90 | COAP_OPTION_PROXY_SCHEME=39, |
ashleymills | 0:3d62a105fd34 | 91 | COAP_OPTION_SIZE1=60 |
ashleymills | 0:3d62a105fd34 | 92 | }; |
ashleymills | 0:3d62a105fd34 | 93 | |
ashleymills | 0:3d62a105fd34 | 94 | /// CoAP content-formats. |
ashleymills | 0:3d62a105fd34 | 95 | enum ContentFormat { |
ashleymills | 0:3d62a105fd34 | 96 | COAP_CONTENT_FORMAT_TEXT_PLAIN = 0, |
ashleymills | 0:3d62a105fd34 | 97 | COAP_CONTENT_FORMAT_APP_LINK = 40, |
ashleymills | 0:3d62a105fd34 | 98 | COAP_CONTENT_FORMAT_APP_XML, |
ashleymills | 0:3d62a105fd34 | 99 | COAP_CONTENT_FORMAT_APP_OCTET, |
ashleymills | 0:3d62a105fd34 | 100 | COAP_CONTENT_FORMAT_APP_EXI = 47, |
ashleymills | 0:3d62a105fd34 | 101 | COAP_CONTENT_FORMAT_APP_JSON = 50 |
ashleymills | 0:3d62a105fd34 | 102 | }; |
ashleymills | 0:3d62a105fd34 | 103 | |
ashleymills | 0:3d62a105fd34 | 104 | /// Sequence of these is returned by CoapPDU::getOptions() |
ashleymills | 0:3d62a105fd34 | 105 | struct CoapOption { |
ashleymills | 0:3d62a105fd34 | 106 | uint16_t optionDelta; |
ashleymills | 0:3d62a105fd34 | 107 | uint16_t optionNumber; |
ashleymills | 0:3d62a105fd34 | 108 | uint16_t optionValueLength; |
ashleymills | 0:3d62a105fd34 | 109 | int totalLength; |
ashleymills | 0:3d62a105fd34 | 110 | uint8_t *optionPointer; |
ashleymills | 0:3d62a105fd34 | 111 | uint8_t *optionValuePointer; |
ashleymills | 0:3d62a105fd34 | 112 | }; |
ashleymills | 0:3d62a105fd34 | 113 | |
ashleymills | 0:3d62a105fd34 | 114 | // construction and destruction |
ashleymills | 0:3d62a105fd34 | 115 | CoapPDU(); |
ashleymills | 0:3d62a105fd34 | 116 | CoapPDU(uint8_t *pdu, int pduLength); |
ashleymills | 0:3d62a105fd34 | 117 | CoapPDU(uint8_t *buffer, int bufferLength, int pduLength); |
ashleymills | 0:3d62a105fd34 | 118 | ~CoapPDU(); |
ashleymills | 0:3d62a105fd34 | 119 | int reset(); |
ashleymills | 0:3d62a105fd34 | 120 | int validate(); |
ashleymills | 0:3d62a105fd34 | 121 | |
ashleymills | 0:3d62a105fd34 | 122 | // version |
ashleymills | 0:3d62a105fd34 | 123 | int setVersion(uint8_t version); |
ashleymills | 0:3d62a105fd34 | 124 | uint8_t getVersion(); |
ashleymills | 0:3d62a105fd34 | 125 | |
ashleymills | 0:3d62a105fd34 | 126 | // message type |
ashleymills | 0:3d62a105fd34 | 127 | void setType(CoapPDU::Type type); |
ashleymills | 0:3d62a105fd34 | 128 | CoapPDU::Type getType(); |
ashleymills | 0:3d62a105fd34 | 129 | |
ashleymills | 0:3d62a105fd34 | 130 | // tokens |
ashleymills | 0:3d62a105fd34 | 131 | int setTokenLength(uint8_t tokenLength); |
ashleymills | 0:3d62a105fd34 | 132 | int getTokenLength(); |
ashleymills | 0:3d62a105fd34 | 133 | uint8_t* getTokenPointer(); |
pnysten | 2:d0d57af6c9df | 134 | long getLongToken(); |
ashleymills | 0:3d62a105fd34 | 135 | int setToken(uint8_t *token, uint8_t tokenLength); |
ashleymills | 0:3d62a105fd34 | 136 | |
ashleymills | 0:3d62a105fd34 | 137 | // message code |
ashleymills | 0:3d62a105fd34 | 138 | void setCode(CoapPDU::Code code); |
ashleymills | 0:3d62a105fd34 | 139 | CoapPDU::Code getCode(); |
ashleymills | 1:5eec2844ad47 | 140 | CoapPDU::Code httpStatusToCode(int httpStatus); |
ashleymills | 0:3d62a105fd34 | 141 | |
ashleymills | 0:3d62a105fd34 | 142 | // message ID |
ashleymills | 0:3d62a105fd34 | 143 | int setMessageID(uint16_t messageID); |
ashleymills | 0:3d62a105fd34 | 144 | uint16_t getMessageID(); |
ashleymills | 0:3d62a105fd34 | 145 | |
ashleymills | 0:3d62a105fd34 | 146 | // options |
ashleymills | 0:3d62a105fd34 | 147 | int addOption(uint16_t optionNumber, uint16_t optionLength, uint8_t *optionValue); |
ashleymills | 0:3d62a105fd34 | 148 | // gets a list of all options |
ashleymills | 0:3d62a105fd34 | 149 | CoapOption* getOptions(); |
ashleymills | 0:3d62a105fd34 | 150 | int getNumOptions(); |
ashleymills | 0:3d62a105fd34 | 151 | // shorthand helpers |
ashleymills | 1:5eec2844ad47 | 152 | int setURI(char *uri); |
ashleymills | 0:3d62a105fd34 | 153 | int setURI(char *uri, int urilen); |
ashleymills | 0:3d62a105fd34 | 154 | int getURI(char *dst, int dstlen, int *outLen); |
ashleymills | 1:5eec2844ad47 | 155 | int addURIQuery(char *query); |
ashleymills | 0:3d62a105fd34 | 156 | |
ashleymills | 0:3d62a105fd34 | 157 | // content format helper |
ashleymills | 0:3d62a105fd34 | 158 | int setContentFormat(CoapPDU::ContentFormat format); |
ashleymills | 0:3d62a105fd34 | 159 | |
ashleymills | 0:3d62a105fd34 | 160 | // payload |
ashleymills | 0:3d62a105fd34 | 161 | uint8_t* mallocPayload(int bytes); |
ashleymills | 0:3d62a105fd34 | 162 | int setPayload(uint8_t *value, int len); |
ashleymills | 0:3d62a105fd34 | 163 | uint8_t* getPayloadPointer(); |
ashleymills | 0:3d62a105fd34 | 164 | int getPayloadLength(); |
ashleymills | 0:3d62a105fd34 | 165 | uint8_t* getPayloadCopy(); |
ashleymills | 0:3d62a105fd34 | 166 | |
ashleymills | 0:3d62a105fd34 | 167 | // pdu |
ashleymills | 0:3d62a105fd34 | 168 | int getPDULength(); |
ashleymills | 0:3d62a105fd34 | 169 | uint8_t* getPDUPointer(); |
ashleymills | 0:3d62a105fd34 | 170 | void setPDULength(int len); |
ashleymills | 0:3d62a105fd34 | 171 | |
ashleymills | 0:3d62a105fd34 | 172 | // debugging |
ashleymills | 0:3d62a105fd34 | 173 | static void printBinary(uint8_t b); |
ashleymills | 0:3d62a105fd34 | 174 | void print(); |
ashleymills | 0:3d62a105fd34 | 175 | void printBin(); |
ashleymills | 0:3d62a105fd34 | 176 | void printHex(); |
ashleymills | 0:3d62a105fd34 | 177 | void printOptionHuman(uint8_t *option); |
ashleymills | 0:3d62a105fd34 | 178 | void printHuman(); |
ashleymills | 0:3d62a105fd34 | 179 | void printPDUAsCArray(); |
ashleymills | 0:3d62a105fd34 | 180 | |
ashleymills | 0:3d62a105fd34 | 181 | private: |
ashleymills | 0:3d62a105fd34 | 182 | // variables |
ashleymills | 0:3d62a105fd34 | 183 | uint8_t *_pdu; |
ashleymills | 0:3d62a105fd34 | 184 | int _pduLength; |
ashleymills | 0:3d62a105fd34 | 185 | |
ashleymills | 0:3d62a105fd34 | 186 | int _constructedFromBuffer; |
ashleymills | 0:3d62a105fd34 | 187 | int _bufferLength; |
ashleymills | 0:3d62a105fd34 | 188 | |
ashleymills | 0:3d62a105fd34 | 189 | uint8_t *_payloadPointer; |
ashleymills | 0:3d62a105fd34 | 190 | int _payloadLength; |
ashleymills | 0:3d62a105fd34 | 191 | |
ashleymills | 0:3d62a105fd34 | 192 | int _numOptions; |
ashleymills | 0:3d62a105fd34 | 193 | uint16_t _maxAddedOptionNumber; |
ashleymills | 0:3d62a105fd34 | 194 | |
ashleymills | 0:3d62a105fd34 | 195 | // functions |
ashleymills | 0:3d62a105fd34 | 196 | void shiftPDUUp(int shiftOffset, int shiftAmount); |
ashleymills | 0:3d62a105fd34 | 197 | void shiftPDUDown(int startLocation, int shiftOffset, int shiftAmount); |
ashleymills | 0:3d62a105fd34 | 198 | uint8_t codeToValue(CoapPDU::Code c); |
ashleymills | 0:3d62a105fd34 | 199 | |
ashleymills | 0:3d62a105fd34 | 200 | // option stuff |
ashleymills | 0:3d62a105fd34 | 201 | int findInsertionPosition(uint16_t optionNumber, uint16_t *prevOptionNumber); |
ashleymills | 0:3d62a105fd34 | 202 | int computeExtraBytes(uint16_t n); |
ashleymills | 0:3d62a105fd34 | 203 | int insertOption(int insertionPosition, uint16_t optionDelta, uint16_t optionValueLength, uint8_t *optionValue); |
ashleymills | 0:3d62a105fd34 | 204 | uint16_t getOptionDelta(uint8_t *option); |
ashleymills | 0:3d62a105fd34 | 205 | void setOptionDelta(int optionPosition, uint16_t optionDelta); |
ashleymills | 0:3d62a105fd34 | 206 | uint16_t getOptionValueLength(uint8_t *option); |
ashleymills | 0:3d62a105fd34 | 207 | |
ashleymills | 0:3d62a105fd34 | 208 | }; |
ashleymills | 0:3d62a105fd34 | 209 | |
ashleymills | 0:3d62a105fd34 | 210 | /* |
ashleymills | 0:3d62a105fd34 | 211 | #define COAP_CODE_EMPTY 0x00 |
ashleymills | 0:3d62a105fd34 | 212 | |
ashleymills | 0:3d62a105fd34 | 213 | // method codes 0.01-0.31 |
ashleymills | 0:3d62a105fd34 | 214 | #define COAP_CODE_GET 0x01 |
ashleymills | 0:3d62a105fd34 | 215 | #define COAP_CODE_POST 0x02 |
ashleymills | 0:3d62a105fd34 | 216 | #define COAP_CODE_PUT 0x03 |
ashleymills | 0:3d62a105fd34 | 217 | #define COAP_CODE_DELETE 0x04 |
ashleymills | 0:3d62a105fd34 | 218 | |
ashleymills | 0:3d62a105fd34 | 219 | // Response codes 2.00 - 5.31 |
ashleymills | 0:3d62a105fd34 | 220 | // 2.00 - 2.05 |
ashleymills | 0:3d62a105fd34 | 221 | #define COAP_CODE_CREATED 0x41 |
ashleymills | 0:3d62a105fd34 | 222 | #define COAP_CODE_DELETED 0x42 |
ashleymills | 0:3d62a105fd34 | 223 | #define COAP_CODE_VALID 0x43 |
ashleymills | 0:3d62a105fd34 | 224 | #define COAP_CODE_CHANGED 0x44 |
ashleymills | 0:3d62a105fd34 | 225 | #define COAP_CODE_CONTENT 0x45 |
ashleymills | 0:3d62a105fd34 | 226 | |
ashleymills | 0:3d62a105fd34 | 227 | // 4.00 - 4.15 |
ashleymills | 0:3d62a105fd34 | 228 | #define COAP_CODE_BAD_REQUEST 0x80 |
ashleymills | 0:3d62a105fd34 | 229 | #define COAP_CODE_UNAUTHORIZED 0x81 |
ashleymills | 0:3d62a105fd34 | 230 | #define COAP_CODE_BAD_OPTION 0x82 |
ashleymills | 0:3d62a105fd34 | 231 | #define COAP_CODE_FORBIDDEN 0x83 |
ashleymills | 0:3d62a105fd34 | 232 | #define COAP_CODE_NOT_FOUND 0x84 |
ashleymills | 0:3d62a105fd34 | 233 | #define COAP_CODE_METHOD_NOT_ALLOWED 0x85 |
ashleymills | 0:3d62a105fd34 | 234 | #define COAP_CODE_NOT_ACCEPTABLE 0x86 |
ashleymills | 0:3d62a105fd34 | 235 | #define COAP_CODE_PRECONDITION_FAILED 0x8C |
ashleymills | 0:3d62a105fd34 | 236 | #define COAP_CODE_REQUEST_ENTITY_TOO_LARGE 0x8D |
ashleymills | 0:3d62a105fd34 | 237 | #define COAP_CODE_UNSUPPORTED_CONTENT_FORMAT 0x8F |
ashleymills | 0:3d62a105fd34 | 238 | |
ashleymills | 0:3d62a105fd34 | 239 | // 5.00 - 5.05 |
ashleymills | 0:3d62a105fd34 | 240 | #define COAP_CODE_INTERNAL_SERVER_ERROR 0xA0 |
ashleymills | 0:3d62a105fd34 | 241 | #define COAP_CODE_NOT_IMPLEMENTED 0xA1 |
ashleymills | 0:3d62a105fd34 | 242 | #define COAP_CODE_BAD_GATEWAY 0xA2 |
ashleymills | 0:3d62a105fd34 | 243 | #define COAP_CODE_SERVICE_UNAVAILABLE 0xA3 |
ashleymills | 0:3d62a105fd34 | 244 | #define COAP_CODE_GATEWAY_TIMEOUT 0xA4 |
ashleymills | 0:3d62a105fd34 | 245 | #define COAP_CODE_PROXYING_NOT_SUPPORTED 0xA5 |
pnysten | 2:d0d57af6c9df | 246 | */ |