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:
28:4f71d9d10011
added method to get the ip address

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