Yes We Can / yeswecancoap

Dependencies:   DebugLib EthernetInterface cantcoap mbed-rtos

Dependents:   COAP coap

Fork of yeswecancoap by Sille Van Landschoot

Embed: (wiki syntax)

« Back to documentation index

Response Class Reference

Response Class Reference

Coap Response class This class contains the response message. More...

#include <response.h>

Public Member Functions

 Response ()
 Memory-managed constructor.
 Response (uint8_t *pdu, int pduLength)
 Memory-managed constructor.
 Response (uint8_t *buffer, int bufferLength, int pduLength)
 Construct a PDU using an external buffer.
void setCode (int code)
 Set the response code.
void setType (CoapPDU::Type type)
 Set/change the response type.
void setContent (char *content, int length)
 Set the payload content to send with the response.

Detailed Description

Coap Response class This class contains the response message.

It let's you change response information depending on what you need to respond.

Definition at line 10 of file response.h.


Constructor & Destructor Documentation

Response (  )

Memory-managed constructor.

Buffer for PDU is dynamically sized and allocated by the object. When using this constructor, the CoapPDU class will allocate space for the PDU. Contrast this with the parameterized constructors, which allow the use of an external buffer.

Note, the PDU container and space can be reused by issuing a CoapPDU::reset(). If the new PDU exceeds the space of the previously allocated memory, then further memory will be dynamically allocated.

Deleting the object will free the Object container and all dynamically allocated memory.

Note:
It would have been nice to use something like UDP_CORK or MSG_MORE, to allow separate buffers for token, options, and payload but these FLAGS aren't implemented for UDP in LwIP so stuck with one buffer for now.

CoAP version defaults to 1.

See also:
Response::Response(uint8_t *pdu, int pduLength), Response::Response::(uint8_t *buffer, int bufferLength, int pduLength), Response:Response()~

Definition at line 4 of file response.cpp.

Response ( uint8_t *  pdu,
int  pduLength 
)

Memory-managed constructor.

Buffer for PDU is dynamically sized and allocated by the object. When using this constructor, the CoapPDU class will allocate space for the PDU. Contrast this with the parameterized constructors, which allow the use of an external buffer.

Note, the PDU container and space can be reused by issuing a CoapPDU::reset(). If the new PDU exceeds the space of the previously allocated memory, then further memory will be dynamically allocated.

Deleting the object will free the Object container and all dynamically allocated memory.

Note:
It would have been nice to use something like UDP_CORK or MSG_MORE, to allow separate buffers for token, options, and payload but these FLAGS aren't implemented for UDP in LwIP so stuck with one buffer for now.

CoAP version defaults to 1.

See also:
Response::Response(uint8_t *pdu, int pduLength), Response::Response::(uint8_t *buffer, int bufferLength, int pduLength), Response:Response()~

Definition at line 9 of file response.cpp.

Response ( uint8_t *  buffer,
int  bufferLength,
int  pduLength 
)

Construct a PDU using an external buffer.

No copy of the buffer is made. This differs from CoapPDU::CoapPDU(uint8_t *pdu, int pduLength) in that the buffer may be larger than the actual CoAP PDU contained int the buffer. This is typically used when a large buffer is reused multiple times. Note that pduLength can be 0.

If an actual CoAP PDU is passed in the buffer, pduLength should match its length. CoapPDU::validate() must be called to initiate the object before member functions can be used.

A PDU constructed in this manner must be validated with CoapPDU::validate() before the member variables will be accessible.

Warning:
The validation call parses the PDU structure to set some internal parameters. If you do not validate the PDU, then the behaviour of member access functions will be undefined.

The buffer can be reused by issuing a CoapPDU::reset() but the class will not change the size of the buffer. If the newly constructed PDU exceeds the size of the buffer, the function called (for example CoapPDU::addOption) will fail.

Deleting this object will only delete the Object container and will not delete the PDU buffer.

Parameters:
bufferA buffer which either contains a CoAP PDU or is intended to be used to construct one.
bufferLengthThe length of the buffer
pduLengthIf the buffer contains a CoAP PDU, this specifies the length of the PDU within the buffer.
See also:
Response::Response(), Response::Response(uint8_t *pdu, int pduLength)

Definition at line 14 of file response.cpp.


Member Function Documentation

void setCode ( int  code )

Set the response code.

 void get_hello(Request* req, Response* res)
 {
      // get the resource and return it
      res->setContent("Hello world\r\n", 13);
      res->setCode(CONTENT);
 }
 void post_hello(Request* req, Response* res)
 {
      // do something with the post content
      res->setCode(CHANGED);
 }
 void put_hello(Request* req, Response* res)
 {
      // do add content to your system
      res->setCode(CREATED);
 }
Parameters:
codeCoap code. Default value: NOT_IMPLEMENTED (see CoapCode enum for possible values)

Definition at line 25 of file response.cpp.

void setContent ( char *  content,
int  length 
)

Set the payload content to send with the response.

 void get_hello(Request* req, Response* res)
 {
      res->setContent("Hello world\r\n", 13);
 }
Note:
The response code is automatically set to CONTENT when using setContent()
Parameters:
contentThe content to be send as payload
lenghtThe length of the content

Definition at line 19 of file response.cpp.

void setType ( CoapPDU::Type  type )

Set/change the response type.

Parameters:
typeCoap message type. The value is automatically set depending on the request (Confirmable or not confirmable) You can change this value if needed (see CoapType enum for possible values)