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 12:52:04 2015 +0000
Revision:
16:62a87be3f0b8
Parent:
11:642eaabf1c2b
Child:
17:3e1d176e2d4a
added function to enable broadcast for the server;

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 16:62a87be3f0b8 16 void Server::enableBroadcast(int broadcast)
sillevl 16:62a87be3f0b8 17 {
sillevl 16:62a87be3f0b8 18 server.set_broadcasting(broadcast);
sillevl 16:62a87be3f0b8 19 }
sillevl 16:62a87be3f0b8 20
sillevl 11:642eaabf1c2b 21 void Server::add(char* uri, void (*fnc)(Request*, Response*), CoapPDU::Code method)
sillevl 1:ab04e3d36ade 22 {
sillevl 3:e03960f91763 23 Resource res = {uri, fnc, method};
sillevl 3:e03960f91763 24 resources.push_back(res);
sillevl 1:ab04e3d36ade 25 }
sillevl 1:ab04e3d36ade 26
sillevl 1:ab04e3d36ade 27 void Server::waitForRequest()
sillevl 1:ab04e3d36ade 28 {
sillevl 3:e03960f91763 29
sillevl 4:34a62b7cb2f9 30 char buffer[UDP_BUFFER_SIZE];
sillevl 1:ab04e3d36ade 31 printf("\r\nWaiting for UDP packet...\r\n");
sillevl 1:ab04e3d36ade 32 int size = server.receiveFrom(client, buffer, sizeof(buffer));
sillevl 1:ab04e3d36ade 33 buffer[size] = '\0';
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 7:1bed29e1b0a4 37
sillevl 7:1bed29e1b0a4 38 char uriBuffer[URI_BUFFER_SIZE];
sillevl 7:1bed29e1b0a4 39 int uriLength;
sillevl 7:1bed29e1b0a4 40 uint8_t token[TOKEN_BUFFER_SIZE];
sillevl 7:1bed29e1b0a4 41 uint8_t tokenLength;
sillevl 7:1bed29e1b0a4 42
sillevl 5:1924c60356d0 43 req->printHuman();
sillevl 1:ab04e3d36ade 44
sillevl 5:1924c60356d0 45 req->getURI(uriBuffer,URI_BUFFER_SIZE,&uriLength);
sillevl 5:1924c60356d0 46 tokenLength = req->getTokenLength();
sillevl 5:1924c60356d0 47 memcpy(token, req->getTokenPointer(), tokenLength);
sillevl 7:1bed29e1b0a4 48
sillevl 7:1bed29e1b0a4 49 int resourceIndex = findResource(uriBuffer, req->getCode());
sillevl 3:e03960f91763 50
sillevl 7:1bed29e1b0a4 51
sillevl 7:1bed29e1b0a4 52 CoapPDU *res = new CoapPDU();
sillevl 7:1bed29e1b0a4 53 res->setMessageID(req->getMessageID());
sillevl 7:1bed29e1b0a4 54 res->setToken(token,4);
sillevl 7:1bed29e1b0a4 55
sillevl 7:1bed29e1b0a4 56 switch(req->getType()){
sillevl 7:1bed29e1b0a4 57 case Response::COAP_NON_CONFIRMABLE:
sillevl 7:1bed29e1b0a4 58 res->setType(Response::COAP_NON_CONFIRMABLE);
sillevl 7:1bed29e1b0a4 59 break;
sillevl 7:1bed29e1b0a4 60 case Response::COAP_CONFIRMABLE:
sillevl 7:1bed29e1b0a4 61 res->setType(Response::COAP_ACKNOWLEDGEMENT);
sillevl 7:1bed29e1b0a4 62 }
sillevl 7:1bed29e1b0a4 63
sillevl 7:1bed29e1b0a4 64 res->setCode(Response::COAP_NOT_FOUND);
sillevl 7:1bed29e1b0a4 65
sillevl 7:1bed29e1b0a4 66 if(resourceIndex != -1){
sillevl 7:1bed29e1b0a4 67 res->setCode(Response::COAP_NOT_IMPLEMENTED);
sillevl 7:1bed29e1b0a4 68 resources[resourceIndex].function((Request*)req, (Response*)res);
sillevl 7:1bed29e1b0a4 69 }
sillevl 7:1bed29e1b0a4 70
sillevl 7:1bed29e1b0a4 71 server.sendTo(client, (char*) res->getPDUPointer(),res->getPDULength());
sillevl 7:1bed29e1b0a4 72
sillevl 7:1bed29e1b0a4 73 delete res;
sillevl 7:1bed29e1b0a4 74 delete req;
sillevl 7:1bed29e1b0a4 75 }
sillevl 7:1bed29e1b0a4 76 }
sillevl 7:1bed29e1b0a4 77
sillevl 11:642eaabf1c2b 78 int Server::findResource(char* uri, CoapPDU::Code method)
sillevl 7:1bed29e1b0a4 79 {
sillevl 3:e03960f91763 80 for(int i = 0; i < resources.size(); i++){
sillevl 11:642eaabf1c2b 81 if(strcmp(uri, resources[i].uri) == 0 && method == resources[i].method) return i;
sillevl 1:ab04e3d36ade 82 }
sillevl 7:1bed29e1b0a4 83 return -1;
sillevl 1:ab04e3d36ade 84 }