Coap Client and Server
Dependencies: DebugLib EthernetInterface cantcoap mbed-rtos
Fork of yeswecancoap by
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
Diff: server.cpp
- Revision:
- 3:e03960f91763
- Parent:
- 1:ab04e3d36ade
- Child:
- 4:34a62b7cb2f9
diff -r ab04e3d36ade -r e03960f91763 server.cpp --- a/server.cpp Tue Oct 20 18:57:05 2015 +0000 +++ b/server.cpp Tue Oct 20 20:57:45 2015 +0000 @@ -13,13 +13,15 @@ server.bind(5683); } -void Server::add(char* uri, void (*fnc)(Request), Method method) +void Server::add(char* uri, void (*fnc)(CoapPDU*, CoapPDU*), Method method) { - this->fnc = fnc; + Resource res = {uri, fnc, method}; + resources.push_back(res); } void Server::waitForRequest() { + char buffer[256]; printf("\r\nWaiting for UDP packet...\r\n"); int size = server.receiveFrom(client, buffer, sizeof(buffer)); @@ -40,29 +42,24 @@ tokenLength = recvPDU->getTokenLength(); memcpy(token, recvPDU->getTokenPointer(), tokenLength); } - delete recvPDU; + - if(strcmp(uriBuffer, "/hello") == 0){ - 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; + 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); + + resources[i].function(recvPDU, pdu); + + server.sendTo(client, (char*) pdu->getPDUPointer(),pdu->getPDULength()); + delete pdu; + + } } + + delete recvPDU; } \ No newline at end of file