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

server.cpp

Committer:
sillevl
Date:
2015-10-20
Revision:
2:a03dd7e58d8f
Parent:
1:ab04e3d36ade

File content as of revision 2:a03dd7e58d8f:

#include "server.h"


Server::Server()
{
    debug_init();
    debug_set_newline("\r\n");
    
    eth.init(); //Use DHCP
    eth.connect();
    printf("\r\nServer IP Address is %s\r\n", eth.getIPAddress());
    
    server.bind(5683);
}

void Server::add(char* uri, void (*fnc)(Request*), Method method)
{
/*    Resource res;
    res.uri = uri;
    res.function = fnc;
    res.method = method;
    resources.push_back(res);
    printf("added resource to server: %s (%d), function*: %d\r\n", res.uri, res.function, res.method);*/
}

void Server::waitForRequest()
{
    char buffer[256];
    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;

    CoapPDU *recvPDU = new CoapPDU((uint8_t*)buffer,256, size);
    if(recvPDU->validate()) {
        recvPDU->printHuman();

        recvPDU->getURI(uriBuffer,32,&recvURILen); 
        msgId = recvPDU->getMessageID();
        tokenLength = recvPDU->getTokenLength();
        memcpy(token, recvPDU->getTokenPointer(), tokenLength);
    }
    delete recvPDU;
    
/*    printf("#resources: %d\r\n", resources.size());
    
    printf("test: %s\r\n", resources[0].uri);
    printf("test: %s\r\n", resources[1].uri);*/
    
    for(int j=0; j < resources.size(); j++){
        printf("...");
        char* uri = (char*)resources.at(j).uri;
        printf("finding uri: %s",uri);
        if(strcmp(uriBuffer, uri) == 0){
            printf(" OK");
            Request* req = new Request();
            fnc(req);
            delete req;
        }
        printf("\r\n");
    }
    
    if(strcmp(uriBuffer, "/hello") == 0){
        Request* req = new Request();
        fnc(req);
        delete req;
        
        CoapPDU *pdu = new CoapPDU();
        pdu->setType(CoapPDU::COAP_ACKNOWLEDGEMENT);
        pdu->setCode(CoapPDU::COAP_CONTENT);
        pdu->setMessageID(msgId);
        pdu->setToken(token,4);
        
        uint8_t txt[] = "Hello world";
        pdu->setPayload(txt, 11); 
        
        server.sendTo(client, (char*) pdu->getPDUPointer(),pdu->getPDULength());
        delete pdu;
    } else {
        
        CoapPDU *pdu = new CoapPDU();
        pdu->setType(CoapPDU::COAP_ACKNOWLEDGEMENT);
        pdu->setCode(CoapPDU::COAP_NOT_FOUND);
        pdu->setMessageID(msgId);
        pdu->setToken(token,4);
                
        server.sendTo(client, (char*) pdu->getPDUPointer(),pdu->getPDULength());
        delete pdu;
    }
}