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

Revision:
4:34a62b7cb2f9
Parent:
3:e03960f91763
Child:
5:1924c60356d0
--- a/server.cpp	Tue Oct 20 20:57:45 2015 +0000
+++ b/server.cpp	Wed Oct 21 08:10:12 2015 +0000
@@ -13,7 +13,7 @@
     server.bind(5683);
 }
 
-void Server::add(char* uri, void (*fnc)(CoapPDU*, CoapPDU*), Method method)
+void Server::add(char* uri, void (*fnc)(CoapPDU*, Response*), Method method)
 {
     Resource res = {uri, fnc, method};
     resources.push_back(res);
@@ -22,23 +22,21 @@
 void Server::waitForRequest()
 {
 
-    char buffer[256];
+    char buffer[UDP_BUFFER_SIZE];
     printf("\r\nWaiting for UDP packet...\r\n");
     int size = server.receiveFrom(client, buffer, sizeof(buffer));
     buffer[size] = '\0';
     
-    char uriBuffer[32];
-    int recvURILen;
-    int msgId;
-    uint8_t token[20];
-    int tokenLength;
+    char uriBuffer[URI_BUFFER_SIZE];
+    int uriLength;
+    uint8_t token[TOKEN_BUFFER_SIZE];
+    uint8_t tokenLength;
 
     CoapPDU *recvPDU = new CoapPDU((uint8_t*)buffer,256, size);
     if(recvPDU->validate()) {
         recvPDU->printHuman();
 
-        recvPDU->getURI(uriBuffer,32,&recvURILen); 
-        msgId = recvPDU->getMessageID();
+        recvPDU->getURI(uriBuffer,URI_BUFFER_SIZE,&uriLength); 
         tokenLength = recvPDU->getTokenLength();
         memcpy(token, recvPDU->getTokenPointer(), tokenLength);
     }
@@ -47,16 +45,16 @@
     for(int i = 0; i < resources.size(); i++){
         if(strcmp(uriBuffer, resources[i].uri) == 0){
                
-            CoapPDU *pdu = new CoapPDU();
-            pdu->setType(CoapPDU::COAP_ACKNOWLEDGEMENT);
-            pdu->setCode(CoapPDU::COAP_CONTENT);
-            pdu->setMessageID(msgId);
-            pdu->setToken(token,4);
+            Response *res = new Response();
+            res->setType(Response::COAP_ACKNOWLEDGEMENT);
+            res->setCode(Response::COAP_CONTENT);
+            res->setMessageID(recvPDU->getMessageID());
+            res->setToken(token,4);
             
-            resources[i].function(recvPDU, pdu);
+            resources[i].function(recvPDU, res);
                     
-            server.sendTo(client, (char*) pdu->getPDUPointer(),pdu->getPDULength());
-            delete pdu;
+            server.sendTo(client, (char*) res->getPDUPointer(),res->getPDULength());
+            delete res;
             
         }
     }