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 Oct 20 20:19:21 2015 +0000
Revision:
2:a03dd7e58d8f
Parent:
1:ab04e3d36ade
vector read problem

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 2:a03dd7e58d8f 16 void Server::add(char* uri, void (*fnc)(Request*), Method method)
sillevl 1:ab04e3d36ade 17 {
sillevl 2:a03dd7e58d8f 18 /* Resource res;
sillevl 2:a03dd7e58d8f 19 res.uri = uri;
sillevl 2:a03dd7e58d8f 20 res.function = fnc;
sillevl 2:a03dd7e58d8f 21 res.method = method;
sillevl 2:a03dd7e58d8f 22 resources.push_back(res);
sillevl 2:a03dd7e58d8f 23 printf("added resource to server: %s (%d), function*: %d\r\n", res.uri, res.function, res.method);*/
sillevl 1:ab04e3d36ade 24 }
sillevl 1:ab04e3d36ade 25
sillevl 1:ab04e3d36ade 26 void Server::waitForRequest()
sillevl 1:ab04e3d36ade 27 {
sillevl 1:ab04e3d36ade 28 char buffer[256];
sillevl 1:ab04e3d36ade 29 printf("\r\nWaiting for UDP packet...\r\n");
sillevl 1:ab04e3d36ade 30 int size = server.receiveFrom(client, buffer, sizeof(buffer));
sillevl 1:ab04e3d36ade 31 buffer[size] = '\0';
sillevl 1:ab04e3d36ade 32
sillevl 1:ab04e3d36ade 33 char uriBuffer[32];
sillevl 1:ab04e3d36ade 34 int recvURILen;
sillevl 1:ab04e3d36ade 35 int msgId;
sillevl 1:ab04e3d36ade 36 uint8_t token[20];
sillevl 1:ab04e3d36ade 37 int tokenLength;
sillevl 1:ab04e3d36ade 38
sillevl 1:ab04e3d36ade 39 CoapPDU *recvPDU = new CoapPDU((uint8_t*)buffer,256, size);
sillevl 1:ab04e3d36ade 40 if(recvPDU->validate()) {
sillevl 1:ab04e3d36ade 41 recvPDU->printHuman();
sillevl 1:ab04e3d36ade 42
sillevl 1:ab04e3d36ade 43 recvPDU->getURI(uriBuffer,32,&recvURILen);
sillevl 1:ab04e3d36ade 44 msgId = recvPDU->getMessageID();
sillevl 1:ab04e3d36ade 45 tokenLength = recvPDU->getTokenLength();
sillevl 1:ab04e3d36ade 46 memcpy(token, recvPDU->getTokenPointer(), tokenLength);
sillevl 1:ab04e3d36ade 47 }
sillevl 1:ab04e3d36ade 48 delete recvPDU;
sillevl 1:ab04e3d36ade 49
sillevl 2:a03dd7e58d8f 50 /* printf("#resources: %d\r\n", resources.size());
sillevl 2:a03dd7e58d8f 51
sillevl 2:a03dd7e58d8f 52 printf("test: %s\r\n", resources[0].uri);
sillevl 2:a03dd7e58d8f 53 printf("test: %s\r\n", resources[1].uri);*/
sillevl 2:a03dd7e58d8f 54
sillevl 2:a03dd7e58d8f 55 for(int j=0; j < resources.size(); j++){
sillevl 2:a03dd7e58d8f 56 printf("...");
sillevl 2:a03dd7e58d8f 57 char* uri = (char*)resources.at(j).uri;
sillevl 2:a03dd7e58d8f 58 printf("finding uri: %s",uri);
sillevl 2:a03dd7e58d8f 59 if(strcmp(uriBuffer, uri) == 0){
sillevl 2:a03dd7e58d8f 60 printf(" OK");
sillevl 2:a03dd7e58d8f 61 Request* req = new Request();
sillevl 2:a03dd7e58d8f 62 fnc(req);
sillevl 2:a03dd7e58d8f 63 delete req;
sillevl 2:a03dd7e58d8f 64 }
sillevl 2:a03dd7e58d8f 65 printf("\r\n");
sillevl 2:a03dd7e58d8f 66 }
sillevl 2:a03dd7e58d8f 67
sillevl 1:ab04e3d36ade 68 if(strcmp(uriBuffer, "/hello") == 0){
sillevl 2:a03dd7e58d8f 69 Request* req = new Request();
sillevl 2:a03dd7e58d8f 70 fnc(req);
sillevl 2:a03dd7e58d8f 71 delete req;
sillevl 2:a03dd7e58d8f 72
sillevl 1:ab04e3d36ade 73 CoapPDU *pdu = new CoapPDU();
sillevl 1:ab04e3d36ade 74 pdu->setType(CoapPDU::COAP_ACKNOWLEDGEMENT);
sillevl 1:ab04e3d36ade 75 pdu->setCode(CoapPDU::COAP_CONTENT);
sillevl 1:ab04e3d36ade 76 pdu->setMessageID(msgId);
sillevl 1:ab04e3d36ade 77 pdu->setToken(token,4);
sillevl 1:ab04e3d36ade 78
sillevl 1:ab04e3d36ade 79 uint8_t txt[] = "Hello world";
sillevl 1:ab04e3d36ade 80 pdu->setPayload(txt, 11);
sillevl 1:ab04e3d36ade 81
sillevl 1:ab04e3d36ade 82 server.sendTo(client, (char*) pdu->getPDUPointer(),pdu->getPDULength());
sillevl 1:ab04e3d36ade 83 delete pdu;
sillevl 1:ab04e3d36ade 84 } else {
sillevl 1:ab04e3d36ade 85
sillevl 1:ab04e3d36ade 86 CoapPDU *pdu = new CoapPDU();
sillevl 1:ab04e3d36ade 87 pdu->setType(CoapPDU::COAP_ACKNOWLEDGEMENT);
sillevl 1:ab04e3d36ade 88 pdu->setCode(CoapPDU::COAP_NOT_FOUND);
sillevl 1:ab04e3d36ade 89 pdu->setMessageID(msgId);
sillevl 1:ab04e3d36ade 90 pdu->setToken(token,4);
sillevl 1:ab04e3d36ade 91
sillevl 1:ab04e3d36ade 92 server.sendTo(client, (char*) pdu->getPDUPointer(),pdu->getPDULength());
sillevl 1:ab04e3d36ade 93 delete pdu;
sillevl 1:ab04e3d36ade 94 }
sillevl 1:ab04e3d36ade 95 }