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:
25:7adc1d174b74
added method to get the ip address

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sillevl 1:ab04e3d36ade 1 #pragma once
sillevl 1:ab04e3d36ade 2
sillevl 5:1924c60356d0 3
sillevl 1:ab04e3d36ade 4 #include "coap.h"
sillevl 1:ab04e3d36ade 5 #include "cantcoap.h"
sillevl 4:34a62b7cb2f9 6 #include "response.h"
sillevl 5:1924c60356d0 7 #include "request.h"
sillevl 4:34a62b7cb2f9 8
sillevl 3:e03960f91763 9 #include <vector>
sillevl 1:ab04e3d36ade 10
sillevl 24:8319d71d6749 11 enum CoapCode;
sillevl 24:8319d71d6749 12
sillevl 17:3e1d176e2d4a 13 /** Coap Server class
sillevl 17:3e1d176e2d4a 14 * Used to process and respond to coap requests
sillevl 17:3e1d176e2d4a 15 *
sillevl 17:3e1d176e2d4a 16 * @code
sillevl 17:3e1d176e2d4a 17 * #include "mbed.h"
sillevl 17:3e1d176e2d4a 18 * #include "coap.h"
sillevl 17:3e1d176e2d4a 19 *
sillevl 17:3e1d176e2d4a 20 * void get_demo(Request* req, Response* res)
sillevl 17:3e1d176e2d4a 21 * {
sillevl 17:3e1d176e2d4a 22 * // do your stuff here
sillevl 17:3e1d176e2d4a 23 * }
sillevl 17:3e1d176e2d4a 24 *
sillevl 17:3e1d176e2d4a 25 * int main() {
sillevl 17:3e1d176e2d4a 26 * Server server;
sillevl 17:3e1d176e2d4a 27 *
sillevl 25:7adc1d174b74 28 * server.add("/demo", &get_led, GET);
sillevl 17:3e1d176e2d4a 29 *
sillevl 17:3e1d176e2d4a 30 * while(1)
sillevl 17:3e1d176e2d4a 31 * {
sillevl 17:3e1d176e2d4a 32 * server.waitForRequest();
sillevl 17:3e1d176e2d4a 33 * };
sillevl 17:3e1d176e2d4a 34 * }
sillevl 17:3e1d176e2d4a 35 * @endcode
sillevl 17:3e1d176e2d4a 36 */
sillevl 1:ab04e3d36ade 37 class Server{
sillevl 1:ab04e3d36ade 38
sillevl 1:ab04e3d36ade 39 public:
sillevl 17:3e1d176e2d4a 40 /** Create a new Server instance */
sillevl 1:ab04e3d36ade 41 Server();
sillevl 1:ab04e3d36ade 42
sillevl 17:3e1d176e2d4a 43 /** Add a coap resource
sillevl 17:3e1d176e2d4a 44 *
sillevl 17:3e1d176e2d4a 45 * @param uri The resource URI to respond on
sillevl 17:3e1d176e2d4a 46 * @param function The corresponding function to call when a request for this resource is received
sillevl 17:3e1d176e2d4a 47 * @param method The method to respond to (GET, POST, PUT or DELETE)
sillevl 17:3e1d176e2d4a 48 *
sillevl 17:3e1d176e2d4a 49 */
sillevl 23:019c530468b4 50 void add(char* uri, void (*function)(Request*, Response*), CoapCode method);
sillevl 17:3e1d176e2d4a 51
sillevl 17:3e1d176e2d4a 52 /** Wait for a request and process that request
sillevl 17:3e1d176e2d4a 53 * @note This is usually placed inside a loop
sillevl 17:3e1d176e2d4a 54 */
sillevl 1:ab04e3d36ade 55 void waitForRequest();
sillevl 1:ab04e3d36ade 56
sillevl 17:3e1d176e2d4a 57 /** Enable broadcast functionality
sillevl 17:3e1d176e2d4a 58 * @param broadcast Boolean value to enable or disable broadcasting functionality
sillevl 17:3e1d176e2d4a 59 */
sillevl 16:62a87be3f0b8 60 void enableBroadcast(int broadcast = true);
sillevl 16:62a87be3f0b8 61
sillevl 29:62113a57353b 62 char* getIpAddress();
sillevl 29:62113a57353b 63
sillevl 1:ab04e3d36ade 64
sillevl 1:ab04e3d36ade 65 private:
sillevl 1:ab04e3d36ade 66 EthernetInterface eth;
sillevl 1:ab04e3d36ade 67 UDPSocket server;
sillevl 1:ab04e3d36ade 68 Endpoint client;
sillevl 1:ab04e3d36ade 69
sillevl 3:e03960f91763 70 struct Resource{
sillevl 3:e03960f91763 71 char* uri;
sillevl 5:1924c60356d0 72 void (*function)(Request*, Response*);
sillevl 3:e03960f91763 73 int method;
sillevl 3:e03960f91763 74 };
sillevl 3:e03960f91763 75
sillevl 3:e03960f91763 76 std::vector<Resource> resources;
sillevl 1:ab04e3d36ade 77
sillevl 11:642eaabf1c2b 78 int findResource(char* uri, CoapPDU::Code method);
sillevl 7:1bed29e1b0a4 79
sillevl 4:34a62b7cb2f9 80 static const int UDP_BUFFER_SIZE = 256;
sillevl 4:34a62b7cb2f9 81 static const int TOKEN_BUFFER_SIZE = 8;
sillevl 4:34a62b7cb2f9 82 static const int URI_BUFFER_SIZE = 64;
sillevl 4:34a62b7cb2f9 83
sillevl 1:ab04e3d36ade 84 };