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@1:ab04e3d36ade, 2015-10-20 (annotated)
- Committer:
- sillevl
- Date:
- Tue Oct 20 18:57:05 2015 +0000
- Revision:
- 1:ab04e3d36ade
- Child:
- 2:a03dd7e58d8f
- Child:
- 3:e03960f91763
first working coap server
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 | 1:ab04e3d36ade | 16 | void Server::add(char* uri, void (*fnc)(Request), Method method) |
sillevl | 1:ab04e3d36ade | 17 | { |
sillevl | 1:ab04e3d36ade | 18 | this->fnc = fnc; |
sillevl | 1:ab04e3d36ade | 19 | } |
sillevl | 1:ab04e3d36ade | 20 | |
sillevl | 1:ab04e3d36ade | 21 | void Server::waitForRequest() |
sillevl | 1:ab04e3d36ade | 22 | { |
sillevl | 1:ab04e3d36ade | 23 | char buffer[256]; |
sillevl | 1:ab04e3d36ade | 24 | printf("\r\nWaiting for UDP packet...\r\n"); |
sillevl | 1:ab04e3d36ade | 25 | int size = server.receiveFrom(client, buffer, sizeof(buffer)); |
sillevl | 1:ab04e3d36ade | 26 | buffer[size] = '\0'; |
sillevl | 1:ab04e3d36ade | 27 | |
sillevl | 1:ab04e3d36ade | 28 | char uriBuffer[32]; |
sillevl | 1:ab04e3d36ade | 29 | int recvURILen; |
sillevl | 1:ab04e3d36ade | 30 | int msgId; |
sillevl | 1:ab04e3d36ade | 31 | uint8_t token[20]; |
sillevl | 1:ab04e3d36ade | 32 | int tokenLength; |
sillevl | 1:ab04e3d36ade | 33 | |
sillevl | 1:ab04e3d36ade | 34 | CoapPDU *recvPDU = new CoapPDU((uint8_t*)buffer,256, size); |
sillevl | 1:ab04e3d36ade | 35 | if(recvPDU->validate()) { |
sillevl | 1:ab04e3d36ade | 36 | recvPDU->printHuman(); |
sillevl | 1:ab04e3d36ade | 37 | |
sillevl | 1:ab04e3d36ade | 38 | recvPDU->getURI(uriBuffer,32,&recvURILen); |
sillevl | 1:ab04e3d36ade | 39 | msgId = recvPDU->getMessageID(); |
sillevl | 1:ab04e3d36ade | 40 | tokenLength = recvPDU->getTokenLength(); |
sillevl | 1:ab04e3d36ade | 41 | memcpy(token, recvPDU->getTokenPointer(), tokenLength); |
sillevl | 1:ab04e3d36ade | 42 | } |
sillevl | 1:ab04e3d36ade | 43 | delete recvPDU; |
sillevl | 1:ab04e3d36ade | 44 | |
sillevl | 1:ab04e3d36ade | 45 | if(strcmp(uriBuffer, "/hello") == 0){ |
sillevl | 1:ab04e3d36ade | 46 | CoapPDU *pdu = new CoapPDU(); |
sillevl | 1:ab04e3d36ade | 47 | pdu->setType(CoapPDU::COAP_ACKNOWLEDGEMENT); |
sillevl | 1:ab04e3d36ade | 48 | pdu->setCode(CoapPDU::COAP_CONTENT); |
sillevl | 1:ab04e3d36ade | 49 | pdu->setMessageID(msgId); |
sillevl | 1:ab04e3d36ade | 50 | pdu->setToken(token,4); |
sillevl | 1:ab04e3d36ade | 51 | |
sillevl | 1:ab04e3d36ade | 52 | uint8_t txt[] = "Hello world"; |
sillevl | 1:ab04e3d36ade | 53 | pdu->setPayload(txt, 11); |
sillevl | 1:ab04e3d36ade | 54 | |
sillevl | 1:ab04e3d36ade | 55 | server.sendTo(client, (char*) pdu->getPDUPointer(),pdu->getPDULength()); |
sillevl | 1:ab04e3d36ade | 56 | delete pdu; |
sillevl | 1:ab04e3d36ade | 57 | } else { |
sillevl | 1:ab04e3d36ade | 58 | |
sillevl | 1:ab04e3d36ade | 59 | CoapPDU *pdu = new CoapPDU(); |
sillevl | 1:ab04e3d36ade | 60 | pdu->setType(CoapPDU::COAP_ACKNOWLEDGEMENT); |
sillevl | 1:ab04e3d36ade | 61 | pdu->setCode(CoapPDU::COAP_NOT_FOUND); |
sillevl | 1:ab04e3d36ade | 62 | pdu->setMessageID(msgId); |
sillevl | 1:ab04e3d36ade | 63 | pdu->setToken(token,4); |
sillevl | 1:ab04e3d36ade | 64 | |
sillevl | 1:ab04e3d36ade | 65 | server.sendTo(client, (char*) pdu->getPDUPointer(),pdu->getPDULength()); |
sillevl | 1:ab04e3d36ade | 66 | delete pdu; |
sillevl | 1:ab04e3d36ade | 67 | } |
sillevl | 1:ab04e3d36ade | 68 | } |