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@7:1bed29e1b0a4, 2015-10-21 (annotated)
- Committer:
- sillevl
- Date:
- Wed Oct 21 10:23:21 2015 +0000
- Revision:
- 7:1bed29e1b0a4
- Parent:
- 6:0c477f5b79ff
- Child:
- 11:642eaabf1c2b
added message type support and added abstraction to request class
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 | 5:1924c60356d0 | 16 | void Server::add(char* uri, void (*fnc)(Request*, 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 | 6:0c477f5b79ff | 30 | CoapPDU *req = new CoapPDU((uint8_t*)buffer,256, size); |
sillevl | 5:1924c60356d0 | 31 | if(req->validate()) { |
sillevl | 7:1bed29e1b0a4 | 32 | |
sillevl | 7:1bed29e1b0a4 | 33 | char uriBuffer[URI_BUFFER_SIZE]; |
sillevl | 7:1bed29e1b0a4 | 34 | int uriLength; |
sillevl | 7:1bed29e1b0a4 | 35 | uint8_t token[TOKEN_BUFFER_SIZE]; |
sillevl | 7:1bed29e1b0a4 | 36 | uint8_t tokenLength; |
sillevl | 7:1bed29e1b0a4 | 37 | |
sillevl | 5:1924c60356d0 | 38 | req->printHuman(); |
sillevl | 1:ab04e3d36ade | 39 | |
sillevl | 5:1924c60356d0 | 40 | req->getURI(uriBuffer,URI_BUFFER_SIZE,&uriLength); |
sillevl | 5:1924c60356d0 | 41 | tokenLength = req->getTokenLength(); |
sillevl | 5:1924c60356d0 | 42 | memcpy(token, req->getTokenPointer(), tokenLength); |
sillevl | 7:1bed29e1b0a4 | 43 | |
sillevl | 7:1bed29e1b0a4 | 44 | int resourceIndex = findResource(uriBuffer, req->getCode()); |
sillevl | 3:e03960f91763 | 45 | |
sillevl | 7:1bed29e1b0a4 | 46 | |
sillevl | 7:1bed29e1b0a4 | 47 | CoapPDU *res = new CoapPDU(); |
sillevl | 7:1bed29e1b0a4 | 48 | res->setMessageID(req->getMessageID()); |
sillevl | 7:1bed29e1b0a4 | 49 | res->setToken(token,4); |
sillevl | 7:1bed29e1b0a4 | 50 | |
sillevl | 7:1bed29e1b0a4 | 51 | switch(req->getType()){ |
sillevl | 7:1bed29e1b0a4 | 52 | case Response::COAP_NON_CONFIRMABLE: |
sillevl | 7:1bed29e1b0a4 | 53 | res->setType(Response::COAP_NON_CONFIRMABLE); |
sillevl | 7:1bed29e1b0a4 | 54 | break; |
sillevl | 7:1bed29e1b0a4 | 55 | case Response::COAP_CONFIRMABLE: |
sillevl | 7:1bed29e1b0a4 | 56 | res->setType(Response::COAP_ACKNOWLEDGEMENT); |
sillevl | 7:1bed29e1b0a4 | 57 | } |
sillevl | 7:1bed29e1b0a4 | 58 | |
sillevl | 7:1bed29e1b0a4 | 59 | res->setCode(Response::COAP_NOT_FOUND); |
sillevl | 7:1bed29e1b0a4 | 60 | |
sillevl | 7:1bed29e1b0a4 | 61 | if(resourceIndex != -1){ |
sillevl | 7:1bed29e1b0a4 | 62 | res->setCode(Response::COAP_NOT_IMPLEMENTED); |
sillevl | 7:1bed29e1b0a4 | 63 | resources[resourceIndex].function((Request*)req, (Response*)res); |
sillevl | 7:1bed29e1b0a4 | 64 | } |
sillevl | 7:1bed29e1b0a4 | 65 | |
sillevl | 7:1bed29e1b0a4 | 66 | server.sendTo(client, (char*) res->getPDUPointer(),res->getPDULength()); |
sillevl | 7:1bed29e1b0a4 | 67 | |
sillevl | 7:1bed29e1b0a4 | 68 | delete res; |
sillevl | 7:1bed29e1b0a4 | 69 | delete req; |
sillevl | 7:1bed29e1b0a4 | 70 | } |
sillevl | 7:1bed29e1b0a4 | 71 | } |
sillevl | 7:1bed29e1b0a4 | 72 | |
sillevl | 7:1bed29e1b0a4 | 73 | int Server::findResource(char* uri, CoapPDU::Code code) |
sillevl | 7:1bed29e1b0a4 | 74 | { |
sillevl | 3:e03960f91763 | 75 | for(int i = 0; i < resources.size(); i++){ |
sillevl | 7:1bed29e1b0a4 | 76 | if(strcmp(uri, resources[i].uri) == 0) return i; |
sillevl | 1:ab04e3d36ade | 77 | } |
sillevl | 7:1bed29e1b0a4 | 78 | return -1; |
sillevl | 1:ab04e3d36ade | 79 | } |