Coap Client and Server
Dependencies: DebugLib EthernetInterface cantcoap mbed-rtos
Fork of yeswecancoap by
YesWeCanCoap
Is a small coap client and server library for mbed based on the cantcoap library.
Import librarycantcoap
This is CoAP library with a focus on simplicity. It offers minimal CoAP PDU construction and decoding to and from byte buffers.
yeswecancoap server enables easy implementation of coap resources, each with a dedicated function. When the function is registered by the server, it will do the rest.
Coap server example
Repository: YesWeCanCoap-example
Coap client example
under construction
request.h@29:62113a57353b, 2015-11-17 (annotated)
- Committer:
- sillevl
- Date:
- Tue Nov 17 16:44:39 2015 +0000
- Revision:
- 29:62113a57353b
- Parent:
- 25:7adc1d174b74
added method to get the ip address
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sillevl | 1:ab04e3d36ade | 1 | #pragma once |
sillevl | 1:ab04e3d36ade | 2 | |
sillevl | 5:1924c60356d0 | 3 | #include "cantcoap.h" |
sillevl | 5:1924c60356d0 | 4 | |
sillevl | 24:8319d71d6749 | 5 | |
sillevl | 24:8319d71d6749 | 6 | /** Coap Request class |
sillevl | 24:8319d71d6749 | 7 | * This class contains the coap request. It let's all the information from the request to process a response. |
sillevl | 24:8319d71d6749 | 8 | */ |
sillevl | 6:0c477f5b79ff | 9 | class Request : protected CoapPDU |
sillevl | 5:1924c60356d0 | 10 | { |
sillevl | 1:ab04e3d36ade | 11 | public: |
sillevl | 24:8319d71d6749 | 12 | /** Memory-managed constructor. Buffer for PDU is dynamically sized and allocated by the object. |
sillevl | 24:8319d71d6749 | 13 | * When using this constructor, the CoapPDU class will allocate space for the PDU. |
sillevl | 24:8319d71d6749 | 14 | * Contrast this with the parameterized constructors, which allow the use of an external buffer. |
sillevl | 24:8319d71d6749 | 15 | * |
sillevl | 24:8319d71d6749 | 16 | * Note, the PDU container and space can be reused by issuing a CoapPDU::reset(). If the new PDU exceeds the |
sillevl | 24:8319d71d6749 | 17 | * space of the previously allocated memory, then further memory will be dynamically allocated. |
sillevl | 24:8319d71d6749 | 18 | * |
sillevl | 24:8319d71d6749 | 19 | * Deleting the object will free the Object container and all dynamically allocated memory. |
sillevl | 24:8319d71d6749 | 20 | * |
sillevl | 24:8319d71d6749 | 21 | * \note It would have been nice to use something like UDP_CORK or MSG_MORE, to allow separate buffers |
sillevl | 24:8319d71d6749 | 22 | * for token, options, and payload but these FLAGS aren't implemented for UDP in LwIP so stuck with one buffer for now. |
sillevl | 24:8319d71d6749 | 23 | * |
sillevl | 24:8319d71d6749 | 24 | * CoAP version defaults to 1. |
sillevl | 24:8319d71d6749 | 25 | * |
sillevl | 25:7adc1d174b74 | 26 | * \sa Request::Request(uint8_t *pdu, int pduLength), Request::Request::(uint8_t *buffer, int bufferLength, int pduLength), |
sillevl | 25:7adc1d174b74 | 27 | * Request:Request()~ |
sillevl | 24:8319d71d6749 | 28 | * |
sillevl | 24:8319d71d6749 | 29 | */ |
sillevl | 3:e03960f91763 | 30 | Request(); |
sillevl | 24:8319d71d6749 | 31 | |
sillevl | 24:8319d71d6749 | 32 | /** Memory-managed constructor. Buffer for PDU is dynamically sized and allocated by the object. |
sillevl | 24:8319d71d6749 | 33 | * When using this constructor, the CoapPDU class will allocate space for the PDU. |
sillevl | 24:8319d71d6749 | 34 | * Contrast this with the parameterized constructors, which allow the use of an external buffer. |
sillevl | 24:8319d71d6749 | 35 | * |
sillevl | 24:8319d71d6749 | 36 | * Note, the PDU container and space can be reused by issuing a CoapPDU::reset(). If the new PDU exceeds the |
sillevl | 24:8319d71d6749 | 37 | * space of the previously allocated memory, then further memory will be dynamically allocated. |
sillevl | 24:8319d71d6749 | 38 | * |
sillevl | 24:8319d71d6749 | 39 | * Deleting the object will free the Object container and all dynamically allocated memory. |
sillevl | 24:8319d71d6749 | 40 | * |
sillevl | 24:8319d71d6749 | 41 | * \note It would have been nice to use something like UDP_CORK or MSG_MORE, to allow separate buffers |
sillevl | 24:8319d71d6749 | 42 | * for token, options, and payload but these FLAGS aren't implemented for UDP in LwIP so stuck with one buffer for now. |
sillevl | 24:8319d71d6749 | 43 | * |
sillevl | 24:8319d71d6749 | 44 | * CoAP version defaults to 1. |
sillevl | 24:8319d71d6749 | 45 | * |
sillevl | 25:7adc1d174b74 | 46 | * \sa Request::Request(uint8_t *pdu, int pduLength), Request::Request::(uint8_t *buffer, int bufferLength, int pduLength), |
sillevl | 25:7adc1d174b74 | 47 | * Request:Request()~ |
sillevl | 24:8319d71d6749 | 48 | * |
sillevl | 24:8319d71d6749 | 49 | */ |
sillevl | 5:1924c60356d0 | 50 | Request(uint8_t *pdu, int pduLength); |
sillevl | 24:8319d71d6749 | 51 | |
sillevl | 24:8319d71d6749 | 52 | /** Construct a PDU using an external buffer. No copy of the buffer is made. |
sillevl | 25:7adc1d174b74 | 53 | * This differs from CoapPDU::CoapPDU(uint8_t *pdu, int pduLength) in that the buffer may be larger |
sillevl | 25:7adc1d174b74 | 54 | * than the actual CoAP PDU contained int the buffer. This is typically used when a large buffer is reused |
sillevl | 25:7adc1d174b74 | 55 | * multiple times. Note that \b pduLength can be 0. |
sillevl | 25:7adc1d174b74 | 56 | * |
sillevl | 25:7adc1d174b74 | 57 | * If an actual CoAP PDU is passed in the buffer, \b pduLength should match its length. CoapPDU::validate() must |
sillevl | 25:7adc1d174b74 | 58 | * be called to initiate the object before member functions can be used. |
sillevl | 24:8319d71d6749 | 59 | * |
sillevl | 24:8319d71d6749 | 60 | * A PDU constructed in this manner must be validated with CoapPDU::validate() before the member variables will be accessible. |
sillevl | 24:8319d71d6749 | 61 | * |
sillevl | 24:8319d71d6749 | 62 | * \warning The validation call parses the PDU structure to set some internal parameters. If you do |
sillevl | 24:8319d71d6749 | 63 | * not validate the PDU, then the behaviour of member access functions will be undefined. |
sillevl | 24:8319d71d6749 | 64 | * |
sillevl | 24:8319d71d6749 | 65 | * The buffer can be reused by issuing a CoapPDU::reset() but the class will not change the size of the buffer. If the |
sillevl | 24:8319d71d6749 | 66 | * newly constructed PDU exceeds the size of the buffer, the function called (for example CoapPDU::addOption) will fail. |
sillevl | 24:8319d71d6749 | 67 | * |
sillevl | 24:8319d71d6749 | 68 | * Deleting this object will only delete the Object container and will not delete the PDU buffer. |
sillevl | 24:8319d71d6749 | 69 | * |
sillevl | 25:7adc1d174b74 | 70 | * \param buffer A buffer which either contains a CoAP PDU or is intended to be used to construct one. |
sillevl | 25:7adc1d174b74 | 71 | * \param bufferLength The length of the buffer |
sillevl | 25:7adc1d174b74 | 72 | * \param pduLength If the buffer contains a CoAP PDU, this specifies the length of the PDU within the buffer. |
sillevl | 25:7adc1d174b74 | 73 | * |
sillevl | 25:7adc1d174b74 | 74 | * \sa Request::Request(), Request::Request(uint8_t *pdu, int pduLength) |
sillevl | 24:8319d71d6749 | 75 | */ |
sillevl | 5:1924c60356d0 | 76 | Request(uint8_t *buffer, int bufferLength, int pduLength); |
sillevl | 3:e03960f91763 | 77 | |
sillevl | 7:1bed29e1b0a4 | 78 | using CoapPDU::Type; |
sillevl | 7:1bed29e1b0a4 | 79 | using CoapPDU::Code; |
sillevl | 7:1bed29e1b0a4 | 80 | |
sillevl | 7:1bed29e1b0a4 | 81 | using CoapPDU::getType; |
sillevl | 7:1bed29e1b0a4 | 82 | using CoapPDU::getCode; |
sillevl | 7:1bed29e1b0a4 | 83 | |
sillevl | 24:8319d71d6749 | 84 | /** Get the payload content send with the request |
sillevl | 24:8319d71d6749 | 85 | * @return Pointer to the content |
sillevl | 24:8319d71d6749 | 86 | */ |
sillevl | 7:1bed29e1b0a4 | 87 | char* getContent(); |
sillevl | 24:8319d71d6749 | 88 | |
sillevl | 24:8319d71d6749 | 89 | /** Get the lenght of the content |
sillevl | 25:7adc1d174b74 | 90 | * @code |
sillevl | 25:7adc1d174b74 | 91 | * void get_hello(Request* req, Response* res) |
sillevl | 25:7adc1d174b74 | 92 | * { |
sillevl | 25:7adc1d174b74 | 93 | * if(req->hasContent()){ |
sillevl | 25:7adc1d174b74 | 94 | * printf("Content: %s\r\n", req->getContent()); |
sillevl | 25:7adc1d174b74 | 95 | * } |
sillevl | 25:7adc1d174b74 | 96 | * } |
sillevl | 25:7adc1d174b74 | 97 | * @endcode |
sillevl | 24:8319d71d6749 | 98 | * @return integer containing the length of the content |
sillevl | 24:8319d71d6749 | 99 | */ |
sillevl | 7:1bed29e1b0a4 | 100 | int getContentLength(); |
sillevl | 24:8319d71d6749 | 101 | |
sillevl | 24:8319d71d6749 | 102 | /** Check if the request has content |
sillevl | 24:8319d71d6749 | 103 | * @return boolean value depending on if the request contains any content |
sillevl | 24:8319d71d6749 | 104 | */ |
sillevl | 7:1bed29e1b0a4 | 105 | int hasContent(); |
sillevl | 7:1bed29e1b0a4 | 106 | |
sillevl | 7:1bed29e1b0a4 | 107 | /* |
sillevl | 7:1bed29e1b0a4 | 108 | int getMessageId(); |
sillevl | 7:1bed29e1b0a4 | 109 | int getToken();*/ |
sillevl | 7:1bed29e1b0a4 | 110 | |
sillevl | 7:1bed29e1b0a4 | 111 | //using getContentFormat(); |
sillevl | 7:1bed29e1b0a4 | 112 | |
sillevl | 7:1bed29e1b0a4 | 113 | //getOptions(); |
sillevl | 7:1bed29e1b0a4 | 114 | |
sillevl | 7:1bed29e1b0a4 | 115 | |
sillevl | 1:ab04e3d36ade | 116 | }; |