This is CoAP library with a focus on simplicity. It offers minimal CoAP PDU construction and decoding to and from byte buffers.

Committer:
ashleymills
Date:
Tue Oct 08 14:36:01 2013 +0000
Revision:
0:3d62a105fd34
Child:
1:5eec2844ad47
CoAP library with a focus on simplicity. Init.

Who changed what in which revision?

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