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@28:4f71d9d10011, 2015-10-23 (annotated)
- Committer:
- sillevl
- Date:
- Fri Oct 23 11:29:59 2015 +0000
- Revision:
- 28:4f71d9d10011
- Parent:
- 27:22436933025a
- Child:
- 29:62113a57353b
made server non-blocking
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 | 28:4f71d9d10011 | 14 | server.set_blocking(false, 100); |
sillevl | 1:ab04e3d36ade | 15 | } |
sillevl | 1:ab04e3d36ade | 16 | |
sillevl | 16:62a87be3f0b8 | 17 | void Server::enableBroadcast(int broadcast) |
sillevl | 16:62a87be3f0b8 | 18 | { |
sillevl | 16:62a87be3f0b8 | 19 | server.set_broadcasting(broadcast); |
sillevl | 16:62a87be3f0b8 | 20 | } |
sillevl | 16:62a87be3f0b8 | 21 | |
sillevl | 23:019c530468b4 | 22 | void Server::add(char* uri, void (*fnc)(Request*, Response*), CoapCode method) |
sillevl | 1:ab04e3d36ade | 23 | { |
sillevl | 3:e03960f91763 | 24 | Resource res = {uri, fnc, method}; |
sillevl | 3:e03960f91763 | 25 | resources.push_back(res); |
sillevl | 1:ab04e3d36ade | 26 | } |
sillevl | 1:ab04e3d36ade | 27 | |
sillevl | 1:ab04e3d36ade | 28 | void Server::waitForRequest() |
sillevl | 1:ab04e3d36ade | 29 | { |
sillevl | 3:e03960f91763 | 30 | |
sillevl | 4:34a62b7cb2f9 | 31 | char buffer[UDP_BUFFER_SIZE]; |
sillevl | 21:8cf7042bbfb7 | 32 | //printf("\r\nWaiting for UDP packet...\r\n"); |
sillevl | 1:ab04e3d36ade | 33 | int size = server.receiveFrom(client, buffer, sizeof(buffer)); |
sillevl | 1:ab04e3d36ade | 34 | buffer[size] = '\0'; |
sillevl | 1:ab04e3d36ade | 35 | |
sillevl | 6:0c477f5b79ff | 36 | CoapPDU *req = new CoapPDU((uint8_t*)buffer,256, size); |
sillevl | 5:1924c60356d0 | 37 | if(req->validate()) { |
sillevl | 7:1bed29e1b0a4 | 38 | |
sillevl | 7:1bed29e1b0a4 | 39 | char uriBuffer[URI_BUFFER_SIZE]; |
sillevl | 7:1bed29e1b0a4 | 40 | int uriLength; |
sillevl | 7:1bed29e1b0a4 | 41 | uint8_t token[TOKEN_BUFFER_SIZE]; |
sillevl | 7:1bed29e1b0a4 | 42 | uint8_t tokenLength; |
sillevl | 7:1bed29e1b0a4 | 43 | |
sillevl | 21:8cf7042bbfb7 | 44 | //req->printHuman(); |
sillevl | 1:ab04e3d36ade | 45 | |
sillevl | 5:1924c60356d0 | 46 | req->getURI(uriBuffer,URI_BUFFER_SIZE,&uriLength); |
sillevl | 5:1924c60356d0 | 47 | tokenLength = req->getTokenLength(); |
sillevl | 5:1924c60356d0 | 48 | memcpy(token, req->getTokenPointer(), tokenLength); |
sillevl | 7:1bed29e1b0a4 | 49 | |
sillevl | 7:1bed29e1b0a4 | 50 | int resourceIndex = findResource(uriBuffer, req->getCode()); |
sillevl | 3:e03960f91763 | 51 | |
sillevl | 7:1bed29e1b0a4 | 52 | |
sillevl | 7:1bed29e1b0a4 | 53 | CoapPDU *res = new CoapPDU(); |
sillevl | 7:1bed29e1b0a4 | 54 | res->setMessageID(req->getMessageID()); |
sillevl | 7:1bed29e1b0a4 | 55 | res->setToken(token,4); |
sillevl | 7:1bed29e1b0a4 | 56 | |
sillevl | 7:1bed29e1b0a4 | 57 | switch(req->getType()){ |
sillevl | 27:22436933025a | 58 | case CoapPDU::COAP_NON_CONFIRMABLE: |
sillevl | 27:22436933025a | 59 | res->setType(CoapPDU::COAP_NON_CONFIRMABLE); |
sillevl | 7:1bed29e1b0a4 | 60 | break; |
sillevl | 27:22436933025a | 61 | case CoapPDU::COAP_CONFIRMABLE: |
sillevl | 27:22436933025a | 62 | res->setType(CoapPDU::COAP_ACKNOWLEDGEMENT); |
sillevl | 7:1bed29e1b0a4 | 63 | } |
sillevl | 7:1bed29e1b0a4 | 64 | |
sillevl | 27:22436933025a | 65 | res->setCode(CoapPDU::COAP_NOT_FOUND); |
sillevl | 7:1bed29e1b0a4 | 66 | |
sillevl | 7:1bed29e1b0a4 | 67 | if(resourceIndex != -1){ |
sillevl | 27:22436933025a | 68 | res->setCode(CoapPDU::COAP_NOT_IMPLEMENTED); |
sillevl | 7:1bed29e1b0a4 | 69 | resources[resourceIndex].function((Request*)req, (Response*)res); |
sillevl | 7:1bed29e1b0a4 | 70 | } |
sillevl | 7:1bed29e1b0a4 | 71 | |
sillevl | 7:1bed29e1b0a4 | 72 | server.sendTo(client, (char*) res->getPDUPointer(),res->getPDULength()); |
sillevl | 7:1bed29e1b0a4 | 73 | |
sillevl | 7:1bed29e1b0a4 | 74 | delete res; |
sillevl | 7:1bed29e1b0a4 | 75 | delete req; |
sillevl | 7:1bed29e1b0a4 | 76 | } |
sillevl | 7:1bed29e1b0a4 | 77 | } |
sillevl | 7:1bed29e1b0a4 | 78 | |
sillevl | 11:642eaabf1c2b | 79 | int Server::findResource(char* uri, CoapPDU::Code method) |
sillevl | 7:1bed29e1b0a4 | 80 | { |
sillevl | 3:e03960f91763 | 81 | for(int i = 0; i < resources.size(); i++){ |
sillevl | 11:642eaabf1c2b | 82 | if(strcmp(uri, resources[i].uri) == 0 && method == resources[i].method) return i; |
sillevl | 1:ab04e3d36ade | 83 | } |
sillevl | 7:1bed29e1b0a4 | 84 | return -1; |
sillevl | 1:ab04e3d36ade | 85 | } |