Adapted to LoRa Semtech + Nucleo
Dependents: LoRaWAN-lmic-app LoRaWAN-lmic-app LoRaWAN-test-10secs LoRaPersonalizedDeviceForEverynet ... more
Fork of cantcoap by
cantcoap.h
00001 #pragma once 00002 #pragma clang diagnostic ignored "-Wdeprecated-writable-strings" 00003 #pragma clang diagnostic ignored "-Wconstant-logical-operand" 00004 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" 00005 00006 /// Copyright (c) 2013, Ashley Mills. 00007 //#include <unistd.h> 00008 #include <stdint.h> 00009 #include "dbg.h" 00010 00011 #define COAP_HDR_SIZE 4 00012 #define COAP_OPTION_HDR_BYTE 1 00013 00014 // CoAP PDU format 00015 00016 // 0 1 2 3 00017 // 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 00018 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 00019 // |Ver| T | TKL | Code | Message ID | 00020 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 00021 // | Token (if any, TKL bytes) ... 00022 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 00023 // | Options (if any) ... 00024 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 00025 // |1 1 1 1 1 1 1 1| Payload (if any) ... 00026 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 00027 00028 class CoapPDU { 00029 00030 00031 public: 00032 /// CoAP message types. Note, values only work as enum. 00033 enum Type { 00034 COAP_CONFIRMABLE=0x00, 00035 COAP_NON_CONFIRMABLE=0x10, 00036 COAP_ACKNOWLEDGEMENT=0x20, 00037 COAP_RESET=0x30 00038 }; 00039 00040 // CoAP response codes. 00041 enum Code { 00042 COAP_EMPTY=0x00, 00043 COAP_GET, 00044 COAP_POST, 00045 COAP_PUT, 00046 COAP_DELETE, 00047 COAP_CREATED=0x41, 00048 COAP_DELETED, 00049 COAP_VALID, 00050 COAP_CHANGED, 00051 COAP_CONTENT, 00052 COAP_BAD_REQUEST=0x80, 00053 COAP_UNAUTHORIZED, 00054 COAP_BAD_OPTION, 00055 COAP_FORBIDDEN, 00056 COAP_NOT_FOUND, 00057 COAP_METHOD_NOT_ALLOWED, 00058 COAP_NOT_ACCEPTABLE, 00059 COAP_PRECONDITION_FAILED=0x8C, 00060 COAP_REQUEST_ENTITY_TOO_LARGE=0x8D, 00061 COAP_UNSUPPORTED_CONTENT_FORMAT=0x8F, 00062 COAP_INTERNAL_SERVER_ERROR=0xA0, 00063 COAP_NOT_IMPLEMENTED, 00064 COAP_BAD_GATEWAY, 00065 COAP_SERVICE_UNAVAILABLE, 00066 COAP_GATEWAY_TIMEOUT, 00067 COAP_PROXYING_NOT_SUPPORTED, 00068 COAP_UNDEFINED_CODE=0xFF 00069 }; 00070 00071 /// CoAP option numbers. 00072 enum Option { 00073 COAP_OPTION_IF_MATCH=1, 00074 COAP_OPTION_URI_HOST=3, 00075 COAP_OPTION_ETAG, 00076 COAP_OPTION_IF_NONE_MATCH, 00077 COAP_OPTION_OBSERVE, 00078 COAP_OPTION_URI_PORT, 00079 COAP_OPTION_LOCATION_PATH, 00080 COAP_OPTION_URI_PATH=11, 00081 COAP_OPTION_CONTENT_FORMAT, 00082 COAP_OPTION_MAX_AGE=14, 00083 COAP_OPTION_URI_QUERY, 00084 COAP_OPTION_ACCEPT=17, 00085 COAP_OPTION_LOCATION_QUERY=20, 00086 COAP_OPTION_BLOCK2=23, 00087 COAP_OPTION_BLOCK1=27, 00088 COAP_OPTION_SIZE2, 00089 COAP_OPTION_PROXY_URI=35, 00090 COAP_OPTION_PROXY_SCHEME=39, 00091 COAP_OPTION_SIZE1=60 00092 }; 00093 00094 /// CoAP content-formats. 00095 enum ContentFormat { 00096 COAP_CONTENT_FORMAT_TEXT_PLAIN = 0, 00097 COAP_CONTENT_FORMAT_APP_LINK = 40, 00098 COAP_CONTENT_FORMAT_APP_XML, 00099 COAP_CONTENT_FORMAT_APP_OCTET, 00100 COAP_CONTENT_FORMAT_APP_EXI = 47, 00101 COAP_CONTENT_FORMAT_APP_JSON = 50 00102 }; 00103 00104 /// Sequence of these is returned by CoapPDU::getOptions() 00105 struct CoapOption { 00106 uint16_t optionDelta; 00107 uint16_t optionNumber; 00108 uint16_t optionValueLength; 00109 int totalLength; 00110 uint8_t *optionPointer; 00111 uint8_t *optionValuePointer; 00112 }; 00113 00114 // construction and destruction 00115 CoapPDU(); 00116 CoapPDU(uint8_t *pdu, int pduLength); 00117 CoapPDU(uint8_t *buffer, int bufferLength, int pduLength); 00118 ~CoapPDU(); 00119 int reset(); 00120 int validate(); 00121 00122 // version 00123 int setVersion(uint8_t version); 00124 uint8_t getVersion(); 00125 00126 // message type 00127 void setType(CoapPDU::Type type); 00128 CoapPDU::Type getType(); 00129 00130 // tokens 00131 int setTokenLength(uint8_t tokenLength); 00132 int getTokenLength(); 00133 uint8_t* getTokenPointer(); 00134 long getLongToken(); 00135 int setToken(uint8_t *token, uint8_t tokenLength); 00136 00137 // message code 00138 void setCode(CoapPDU::Code code); 00139 CoapPDU::Code getCode(); 00140 CoapPDU::Code httpStatusToCode(int httpStatus); 00141 00142 // message ID 00143 int setMessageID(uint16_t messageID); 00144 uint16_t getMessageID(); 00145 00146 // options 00147 int addOption(uint16_t optionNumber, uint16_t optionLength, uint8_t *optionValue); 00148 // gets a list of all options 00149 CoapOption* getOptions(); 00150 int getNumOptions(); 00151 // shorthand helpers 00152 int setURI(char *uri); 00153 int setURI(char *uri, int urilen); 00154 int getURI(char *dst, int dstlen, int *outLen); 00155 int addURIQuery(char *query); 00156 00157 // content format helper 00158 int setContentFormat(CoapPDU::ContentFormat format); 00159 00160 // payload 00161 uint8_t* mallocPayload(int bytes); 00162 int setPayload(uint8_t *value, int len); 00163 uint8_t* getPayloadPointer(); 00164 int getPayloadLength(); 00165 uint8_t* getPayloadCopy(); 00166 00167 // pdu 00168 int getPDULength(); 00169 uint8_t* getPDUPointer(); 00170 void setPDULength(int len); 00171 00172 // debugging 00173 static void printBinary(uint8_t b); 00174 void print(); 00175 void printBin(); 00176 void printHex(); 00177 void printOptionHuman(uint8_t *option); 00178 void printHuman(); 00179 void printPDUAsCArray(); 00180 00181 private: 00182 // variables 00183 uint8_t *_pdu; 00184 int _pduLength; 00185 00186 int _constructedFromBuffer; 00187 int _bufferLength; 00188 00189 uint8_t *_payloadPointer; 00190 int _payloadLength; 00191 00192 int _numOptions; 00193 uint16_t _maxAddedOptionNumber; 00194 00195 // functions 00196 void shiftPDUUp(int shiftOffset, int shiftAmount); 00197 void shiftPDUDown(int startLocation, int shiftOffset, int shiftAmount); 00198 uint8_t codeToValue(CoapPDU::Code c); 00199 00200 // option stuff 00201 int findInsertionPosition(uint16_t optionNumber, uint16_t *prevOptionNumber); 00202 int computeExtraBytes(uint16_t n); 00203 int insertOption(int insertionPosition, uint16_t optionDelta, uint16_t optionValueLength, uint8_t *optionValue); 00204 uint16_t getOptionDelta(uint8_t *option); 00205 void setOptionDelta(int optionPosition, uint16_t optionDelta); 00206 uint16_t getOptionValueLength(uint8_t *option); 00207 00208 }; 00209 00210 /* 00211 #define COAP_CODE_EMPTY 0x00 00212 00213 // method codes 0.01-0.31 00214 #define COAP_CODE_GET 0x01 00215 #define COAP_CODE_POST 0x02 00216 #define COAP_CODE_PUT 0x03 00217 #define COAP_CODE_DELETE 0x04 00218 00219 // Response codes 2.00 - 5.31 00220 // 2.00 - 2.05 00221 #define COAP_CODE_CREATED 0x41 00222 #define COAP_CODE_DELETED 0x42 00223 #define COAP_CODE_VALID 0x43 00224 #define COAP_CODE_CHANGED 0x44 00225 #define COAP_CODE_CONTENT 0x45 00226 00227 // 4.00 - 4.15 00228 #define COAP_CODE_BAD_REQUEST 0x80 00229 #define COAP_CODE_UNAUTHORIZED 0x81 00230 #define COAP_CODE_BAD_OPTION 0x82 00231 #define COAP_CODE_FORBIDDEN 0x83 00232 #define COAP_CODE_NOT_FOUND 0x84 00233 #define COAP_CODE_METHOD_NOT_ALLOWED 0x85 00234 #define COAP_CODE_NOT_ACCEPTABLE 0x86 00235 #define COAP_CODE_PRECONDITION_FAILED 0x8C 00236 #define COAP_CODE_REQUEST_ENTITY_TOO_LARGE 0x8D 00237 #define COAP_CODE_UNSUPPORTED_CONTENT_FORMAT 0x8F 00238 00239 // 5.00 - 5.05 00240 #define COAP_CODE_INTERNAL_SERVER_ERROR 0xA0 00241 #define COAP_CODE_NOT_IMPLEMENTED 0xA1 00242 #define COAP_CODE_BAD_GATEWAY 0xA2 00243 #define COAP_CODE_SERVICE_UNAVAILABLE 0xA3 00244 #define COAP_CODE_GATEWAY_TIMEOUT 0xA4 00245 #define COAP_CODE_PROXYING_NOT_SUPPORTED 0xA5 00246 */
Generated on Wed Jul 20 2022 04:38:21 by
