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
server.cpp@4:34a62b7cb2f9, 2015-10-21 (annotated)
- Committer:
- sillevl
- Date:
- Wed Oct 21 08:10:12 2015 +0000
- Revision:
- 4:34a62b7cb2f9
- Parent:
- 3:e03960f91763
- Child:
- 5:1924c60356d0
added response class and did some small refactoring
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:34a62b7cb2f9 | 16 | void Server::add(char* uri, void (*fnc)(CoapPDU*, Response*), Method method) |
sillevl | 1:ab04e3d36ade | 17 | { |
sillevl | 3:e03960f91763 | 18 | Resource res = {uri, fnc, method}; |
sillevl | 3:e03960f91763 | 19 | resources.push_back(res); |
sillevl | 1:ab04e3d36ade | 20 | } |
sillevl | 1:ab04e3d36ade | 21 | |
sillevl | 1:ab04e3d36ade | 22 | void Server::waitForRequest() |
sillevl | 1:ab04e3d36ade | 23 | { |
sillevl | 3:e03960f91763 | 24 | |
sillevl | 4:34a62b7cb2f9 | 25 | char buffer[UDP_BUFFER_SIZE]; |
sillevl | 1:ab04e3d36ade | 26 | printf("\r\nWaiting for UDP packet...\r\n"); |
sillevl | 1:ab04e3d36ade | 27 | int size = server.receiveFrom(client, buffer, sizeof(buffer)); |
sillevl | 1:ab04e3d36ade | 28 | buffer[size] = '\0'; |
sillevl | 1:ab04e3d36ade | 29 | |
sillevl | 4:34a62b7cb2f9 | 30 | char uriBuffer[URI_BUFFER_SIZE]; |
sillevl | 4:34a62b7cb2f9 | 31 | int uriLength; |
sillevl | 4:34a62b7cb2f9 | 32 | uint8_t token[TOKEN_BUFFER_SIZE]; |
sillevl | 4:34a62b7cb2f9 | 33 | uint8_t tokenLength; |
sillevl | 1:ab04e3d36ade | 34 | |
sillevl | 1:ab04e3d36ade | 35 | CoapPDU *recvPDU = new CoapPDU((uint8_t*)buffer,256, size); |
sillevl | 1:ab04e3d36ade | 36 | if(recvPDU->validate()) { |
sillevl | 1:ab04e3d36ade | 37 | recvPDU->printHuman(); |
sillevl | 1:ab04e3d36ade | 38 | |
sillevl | 4:34a62b7cb2f9 | 39 | recvPDU->getURI(uriBuffer,URI_BUFFER_SIZE,&uriLength); |
sillevl | 1:ab04e3d36ade | 40 | tokenLength = recvPDU->getTokenLength(); |
sillevl | 1:ab04e3d36ade | 41 | memcpy(token, recvPDU->getTokenPointer(), tokenLength); |
sillevl | 1:ab04e3d36ade | 42 | } |
sillevl | 3:e03960f91763 | 43 | |
sillevl | 1:ab04e3d36ade | 44 | |
sillevl | 3:e03960f91763 | 45 | for(int i = 0; i < resources.size(); i++){ |
sillevl | 3:e03960f91763 | 46 | if(strcmp(uriBuffer, resources[i].uri) == 0){ |
sillevl | 3:e03960f91763 | 47 | |
sillevl | 4:34a62b7cb2f9 | 48 | Response *res = new Response(); |
sillevl | 4:34a62b7cb2f9 | 49 | res->setType(Response::COAP_ACKNOWLEDGEMENT); |
sillevl | 4:34a62b7cb2f9 | 50 | res->setCode(Response::COAP_CONTENT); |
sillevl | 4:34a62b7cb2f9 | 51 | res->setMessageID(recvPDU->getMessageID()); |
sillevl | 4:34a62b7cb2f9 | 52 | res->setToken(token,4); |
sillevl | 3:e03960f91763 | 53 | |
sillevl | 4:34a62b7cb2f9 | 54 | resources[i].function(recvPDU, res); |
sillevl | 3:e03960f91763 | 55 | |
sillevl | 4:34a62b7cb2f9 | 56 | server.sendTo(client, (char*) res->getPDUPointer(),res->getPDULength()); |
sillevl | 4:34a62b7cb2f9 | 57 | delete res; |
sillevl | 3:e03960f91763 | 58 | |
sillevl | 3:e03960f91763 | 59 | } |
sillevl | 1:ab04e3d36ade | 60 | } |
sillevl | 3:e03960f91763 | 61 | |
sillevl | 3:e03960f91763 | 62 | delete recvPDU; |
sillevl | 1:ab04e3d36ade | 63 | } |