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:
Wed Oct 21 08:51:38 2015 +0000
Revision:
6:0c477f5b79ff
Parent:
5:1924c60356d0
Child:
7:1bed29e1b0a4
polymophic request and responses

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sillevl 1:ab04e3d36ade 1 #include "server.h"
sillevl 1:ab04e3d36ade 2
sillevl 1:ab04e3d36ade 3
sillevl 1:ab04e3d36ade 4 Server::Server()
sillevl 1:ab04e3d36ade 5 {
sillevl 1:ab04e3d36ade 6 debug_init();
sillevl 1:ab04e3d36ade 7 debug_set_newline("\r\n");
sillevl 1:ab04e3d36ade 8
sillevl 1:ab04e3d36ade 9 eth.init(); //Use DHCP
sillevl 1:ab04e3d36ade 10 eth.connect();
sillevl 1:ab04e3d36ade 11 printf("\r\nServer IP Address is %s\r\n", eth.getIPAddress());
sillevl 1:ab04e3d36ade 12
sillevl 1:ab04e3d36ade 13 server.bind(5683);
sillevl 1:ab04e3d36ade 14 }
sillevl 1:ab04e3d36ade 15
sillevl 5:1924c60356d0 16 void Server::add(char* uri, void (*fnc)(Request*, Response*), Method method)
sillevl 1:ab04e3d36ade 17 {
sillevl 3:e03960f91763 18 Resource res = {uri, fnc, method};
sillevl 3:e03960f91763 19 resources.push_back(res);
sillevl 1:ab04e3d36ade 20 }
sillevl 1:ab04e3d36ade 21
sillevl 1:ab04e3d36ade 22 void Server::waitForRequest()
sillevl 1:ab04e3d36ade 23 {
sillevl 3:e03960f91763 24
sillevl 4:34a62b7cb2f9 25 char buffer[UDP_BUFFER_SIZE];
sillevl 1:ab04e3d36ade 26 printf("\r\nWaiting for UDP packet...\r\n");
sillevl 1:ab04e3d36ade 27 int size = server.receiveFrom(client, buffer, sizeof(buffer));
sillevl 1:ab04e3d36ade 28 buffer[size] = '\0';
sillevl 1:ab04e3d36ade 29
sillevl 4:34a62b7cb2f9 30 char uriBuffer[URI_BUFFER_SIZE];
sillevl 4:34a62b7cb2f9 31 int uriLength;
sillevl 4:34a62b7cb2f9 32 uint8_t token[TOKEN_BUFFER_SIZE];
sillevl 4:34a62b7cb2f9 33 uint8_t tokenLength;
sillevl 1:ab04e3d36ade 34
sillevl 6:0c477f5b79ff 35 CoapPDU *req = new CoapPDU((uint8_t*)buffer,256, size);
sillevl 5:1924c60356d0 36 if(req->validate()) {
sillevl 5:1924c60356d0 37 req->printHuman();
sillevl 1:ab04e3d36ade 38
sillevl 5:1924c60356d0 39 req->getURI(uriBuffer,URI_BUFFER_SIZE,&uriLength);
sillevl 5:1924c60356d0 40 tokenLength = req->getTokenLength();
sillevl 5:1924c60356d0 41 memcpy(token, req->getTokenPointer(), tokenLength);
sillevl 1:ab04e3d36ade 42 }
sillevl 3:e03960f91763 43
sillevl 1:ab04e3d36ade 44
sillevl 3:e03960f91763 45 for(int i = 0; i < resources.size(); i++){
sillevl 3:e03960f91763 46 if(strcmp(uriBuffer, resources[i].uri) == 0){
sillevl 3:e03960f91763 47
sillevl 6:0c477f5b79ff 48 CoapPDU *res = new CoapPDU();
sillevl 4:34a62b7cb2f9 49 res->setType(Response::COAP_ACKNOWLEDGEMENT);
sillevl 4:34a62b7cb2f9 50 res->setCode(Response::COAP_CONTENT);
sillevl 5:1924c60356d0 51 res->setMessageID(req->getMessageID());
sillevl 4:34a62b7cb2f9 52 res->setToken(token,4);
sillevl 3:e03960f91763 53
sillevl 6:0c477f5b79ff 54 resources[i].function((Request*)req, (Response*)res);
sillevl 3:e03960f91763 55
sillevl 4:34a62b7cb2f9 56 server.sendTo(client, (char*) res->getPDUPointer(),res->getPDULength());
sillevl 4:34a62b7cb2f9 57 delete res;
sillevl 3:e03960f91763 58
sillevl 3:e03960f91763 59 }
sillevl 1:ab04e3d36ade 60 }
sillevl 3:e03960f91763 61
sillevl 5:1924c60356d0 62 delete req;
sillevl 1:ab04e3d36ade 63 }