Coap Client and Server

Dependencies:   DebugLib EthernetInterface cantcoap mbed-rtos

Dependents:   COAP coap

Fork of yeswecancoap by Sille Van Landschoot

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

Committer:
sillevl
Date:
Tue Nov 17 16:44:39 2015 +0000
Revision:
29:62113a57353b
Parent:
27:22436933025a
added method to get the ip address

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sillevl 4:34a62b7cb2f9 1 #pragma once
sillevl 4:34a62b7cb2f9 2
sillevl 4:34a62b7cb2f9 3 #include "cantcoap.h"
sillevl 4:34a62b7cb2f9 4
sillevl 23:019c530468b4 5 enum CoapCode;
sillevl 23:019c530468b4 6
sillevl 24:8319d71d6749 7 /** Coap Response class
sillevl 24:8319d71d6749 8 * This class contains the response message. It let's you change response information depending on what you need to respond.
sillevl 24:8319d71d6749 9 */
sillevl 27:22436933025a 10 class Response : protected CoapPDU
sillevl 4:34a62b7cb2f9 11 {
sillevl 4:34a62b7cb2f9 12 public:
sillevl 4:34a62b7cb2f9 13
sillevl 24:8319d71d6749 14 /** Memory-managed constructor. Buffer for PDU is dynamically sized and allocated by the object.
sillevl 24:8319d71d6749 15 * When using this constructor, the CoapPDU class will allocate space for the PDU.
sillevl 24:8319d71d6749 16 * Contrast this with the parameterized constructors, which allow the use of an external buffer.
sillevl 24:8319d71d6749 17 *
sillevl 24:8319d71d6749 18 * Note, the PDU container and space can be reused by issuing a CoapPDU::reset(). If the new PDU exceeds the
sillevl 24:8319d71d6749 19 * space of the previously allocated memory, then further memory will be dynamically allocated.
sillevl 24:8319d71d6749 20 *
sillevl 24:8319d71d6749 21 * Deleting the object will free the Object container and all dynamically allocated memory.
sillevl 24:8319d71d6749 22 *
sillevl 24:8319d71d6749 23 * \note It would have been nice to use something like UDP_CORK or MSG_MORE, to allow separate buffers
sillevl 24:8319d71d6749 24 * 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 25 *
sillevl 24:8319d71d6749 26 * CoAP version defaults to 1.
sillevl 24:8319d71d6749 27 *
sillevl 25:7adc1d174b74 28 * \sa Response::Response(uint8_t *pdu, int pduLength), Response::Response::(uint8_t *buffer, int bufferLength, int pduLength),
sillevl 25:7adc1d174b74 29 * Response:Response()~
sillevl 24:8319d71d6749 30 *
sillevl 24:8319d71d6749 31 */
sillevl 4:34a62b7cb2f9 32 Response();
sillevl 24:8319d71d6749 33
sillevl 24:8319d71d6749 34 /** Memory-managed constructor. Buffer for PDU is dynamically sized and allocated by the object.
sillevl 24:8319d71d6749 35 * When using this constructor, the CoapPDU class will allocate space for the PDU.
sillevl 24:8319d71d6749 36 * Contrast this with the parameterized constructors, which allow the use of an external buffer.
sillevl 24:8319d71d6749 37 *
sillevl 24:8319d71d6749 38 * Note, the PDU container and space can be reused by issuing a CoapPDU::reset(). If the new PDU exceeds the
sillevl 24:8319d71d6749 39 * space of the previously allocated memory, then further memory will be dynamically allocated.
sillevl 24:8319d71d6749 40 *
sillevl 24:8319d71d6749 41 * Deleting the object will free the Object container and all dynamically allocated memory.
sillevl 24:8319d71d6749 42 *
sillevl 24:8319d71d6749 43 * \note It would have been nice to use something like UDP_CORK or MSG_MORE, to allow separate buffers
sillevl 24:8319d71d6749 44 * 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 45 *
sillevl 24:8319d71d6749 46 * CoAP version defaults to 1.
sillevl 24:8319d71d6749 47 *
sillevl 25:7adc1d174b74 48 * \sa Response::Response(uint8_t *pdu, int pduLength), Response::Response::(uint8_t *buffer, int bufferLength, int pduLength),
sillevl 25:7adc1d174b74 49 * Response:Response()~
sillevl 24:8319d71d6749 50 *
sillevl 24:8319d71d6749 51 */
sillevl 5:1924c60356d0 52 Response(uint8_t *pdu, int pduLength);
sillevl 24:8319d71d6749 53
sillevl 24:8319d71d6749 54 /** Construct a PDU using an external buffer. No copy of the buffer is made.
sillevl 25:7adc1d174b74 55 * This differs from CoapPDU::CoapPDU(uint8_t *pdu, int pduLength) in that the buffer may be larger
sillevl 25:7adc1d174b74 56 * than the actual CoAP PDU contained int the buffer. This is typically used when a large buffer is reused
sillevl 25:7adc1d174b74 57 * multiple times. Note that \b pduLength can be 0.
sillevl 25:7adc1d174b74 58 *
sillevl 25:7adc1d174b74 59 * If an actual CoAP PDU is passed in the buffer, \b pduLength should match its length. CoapPDU::validate() must
sillevl 25:7adc1d174b74 60 * be called to initiate the object before member functions can be used.
sillevl 24:8319d71d6749 61 *
sillevl 24:8319d71d6749 62 * A PDU constructed in this manner must be validated with CoapPDU::validate() before the member variables will be accessible.
sillevl 24:8319d71d6749 63 *
sillevl 24:8319d71d6749 64 * \warning The validation call parses the PDU structure to set some internal parameters. If you do
sillevl 24:8319d71d6749 65 * not validate the PDU, then the behaviour of member access functions will be undefined.
sillevl 24:8319d71d6749 66 *
sillevl 24:8319d71d6749 67 * 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 68 * newly constructed PDU exceeds the size of the buffer, the function called (for example CoapPDU::addOption) will fail.
sillevl 24:8319d71d6749 69 *
sillevl 24:8319d71d6749 70 * Deleting this object will only delete the Object container and will not delete the PDU buffer.
sillevl 24:8319d71d6749 71 *
sillevl 25:7adc1d174b74 72 * \param buffer A buffer which either contains a CoAP PDU or is intended to be used to construct one.
sillevl 25:7adc1d174b74 73 * \param bufferLength The length of the buffer
sillevl 25:7adc1d174b74 74 * \param pduLength If the buffer contains a CoAP PDU, this specifies the length of the PDU within the buffer.
sillevl 25:7adc1d174b74 75 *
sillevl 25:7adc1d174b74 76 * \sa Response::Response(), Response::Response(uint8_t *pdu, int pduLength)
sillevl 24:8319d71d6749 77 */
sillevl 5:1924c60356d0 78 Response(uint8_t *buffer, int bufferLength, int pduLength);
sillevl 4:34a62b7cb2f9 79
sillevl 24:8319d71d6749 80 /** Set the response code
sillevl 25:7adc1d174b74 81 * @code
sillevl 25:7adc1d174b74 82 * void get_hello(Request* req, Response* res)
sillevl 25:7adc1d174b74 83 * {
sillevl 25:7adc1d174b74 84 * // get the resource and return it
sillevl 25:7adc1d174b74 85 * res->setContent("Hello world\r\n", 13);
sillevl 25:7adc1d174b74 86 * res->setCode(CONTENT);
sillevl 25:7adc1d174b74 87 * }
sillevl 25:7adc1d174b74 88 * @endcode
sillevl 25:7adc1d174b74 89 * @code
sillevl 25:7adc1d174b74 90 * void post_hello(Request* req, Response* res)
sillevl 25:7adc1d174b74 91 * {
sillevl 25:7adc1d174b74 92 * // do something with the post content
sillevl 25:7adc1d174b74 93 * res->setCode(CHANGED);
sillevl 25:7adc1d174b74 94 * }
sillevl 25:7adc1d174b74 95 * @endcode
sillevl 25:7adc1d174b74 96 * @code
sillevl 25:7adc1d174b74 97 * void put_hello(Request* req, Response* res)
sillevl 25:7adc1d174b74 98 * {
sillevl 25:7adc1d174b74 99 * // do add content to your system
sillevl 25:7adc1d174b74 100 * res->setCode(CREATED);
sillevl 25:7adc1d174b74 101 * }
sillevl 25:7adc1d174b74 102 * @endcode
sillevl 24:8319d71d6749 103 * @param code Coap code. Default value: NOT_IMPLEMENTED (see CoapCode enum for possible values)
sillevl 24:8319d71d6749 104 */
sillevl 23:019c530468b4 105 void setCode(int code);
sillevl 24:8319d71d6749 106
sillevl 24:8319d71d6749 107 /** Set/change the response type
sillevl 24:8319d71d6749 108 * @param type Coap message type. The value is automatically set depending on the request (Confirmable or not confirmable)
sillevl 24:8319d71d6749 109 * You can change this value if needed (see CoapType enum for possible values)
sillevl 24:8319d71d6749 110 */
sillevl 7:1bed29e1b0a4 111 void setType(CoapPDU::Type type);
sillevl 24:8319d71d6749 112
sillevl 24:8319d71d6749 113 /** Set the payload content to send with the response
sillevl 25:7adc1d174b74 114 * @code
sillevl 25:7adc1d174b74 115 * void get_hello(Request* req, Response* res)
sillevl 25:7adc1d174b74 116 * {
sillevl 25:7adc1d174b74 117 * res->setContent("Hello world\r\n", 13);
sillevl 25:7adc1d174b74 118 * }
sillevl 25:7adc1d174b74 119 * @endcode
sillevl 25:7adc1d174b74 120 * @note The response code is automatically set to CONTENT when using setContent()
sillevl 24:8319d71d6749 121 * @param content The content to be send as payload
sillevl 24:8319d71d6749 122 * @param lenght The length of the content
sillevl 24:8319d71d6749 123 */
sillevl 6:0c477f5b79ff 124 void setContent(char* content, int length);
sillevl 6:0c477f5b79ff 125
sillevl 4:34a62b7cb2f9 126 protected:
sillevl 4:34a62b7cb2f9 127
sillevl 4:34a62b7cb2f9 128 };