Library for handling subset of coap functionality by radio transmitter.

Dependencies:   nRF24L01P cantcoap

Dependents:   server2

Committer:
thewiztory
Date:
Fri Jan 25 02:10:16 2019 +0000
Revision:
5:b8d21be6b36c
Parent:
4:9f635ab44d8e
-

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ka_myk 0:6a6f97ca5572 1 //
Ka_myk 0:6a6f97ca5572 2 // Created by Kamil Mykitiuk on 2019-01-18.
Ka_myk 0:6a6f97ca5572 3 //
Ka_myk 0:6a6f97ca5572 4
Ka_myk 0:6a6f97ca5572 5 #include "coapServer.h"
Ka_myk 2:c3ca8b8526e0 6 #include "dbg.h"
Ka_myk 0:6a6f97ca5572 7
Ka_myk 4:9f635ab44d8e 8 int CoapServer::nextIndex() {
Ka_myk 4:9f635ab44d8e 9 this->lastMessageIndex = (this->lastMessageIndex+1) % this->queueMax;
Ka_myk 4:9f635ab44d8e 10 return this->lastMessageIndex;
Ka_myk 4:9f635ab44d8e 11 }
Ka_myk 4:9f635ab44d8e 12
Ka_myk 4:9f635ab44d8e 13 int CoapServer::ackRepeatedPost(CoapPDU& request, int pipe) {
Ka_myk 4:9f635ab44d8e 14 CoapPDU response = CoapPDU();
Ka_myk 4:9f635ab44d8e 15 response.setVersion(1);
Ka_myk 4:9f635ab44d8e 16 response.setType(CoapPDU::COAP_ACKNOWLEDGEMENT);
Ka_myk 4:9f635ab44d8e 17 response.setCode(CoapPDU::COAP_CREATED);
Ka_myk 4:9f635ab44d8e 18 char requestUri[32];
Ka_myk 4:9f635ab44d8e 19 int requestUriLen = 0;
Ka_myk 4:9f635ab44d8e 20 request.getURI(requestUri, 32, &requestUriLen);
Ka_myk 4:9f635ab44d8e 21 if(response.setURI(requestUri, requestUriLen) != 0) {
Ka_myk 4:9f635ab44d8e 22 return COAP_ERROR;
Ka_myk 4:9f635ab44d8e 23 }
Ka_myk 4:9f635ab44d8e 24 if(radioWrapper.write(response.getPDUPointer(), response.getPDULength(), pipe) < 0) {
Ka_myk 4:9f635ab44d8e 25 return RADIO_NOT_WORKING;
Ka_myk 4:9f635ab44d8e 26 }
Ka_myk 4:9f635ab44d8e 27 return SILENT;
Ka_myk 4:9f635ab44d8e 28 }
Ka_myk 4:9f635ab44d8e 29
Ka_myk 4:9f635ab44d8e 30 int CoapServer::listen(uint8_t* buffer, int len, METHOD* requestMethod, int* uriNumber, int* pipe) {
Ka_myk 0:6a6f97ca5572 31 uint8_t requestBuffer[radioWrapper.packetSize()];
Ka_myk 4:9f635ab44d8e 32 int ret = radioWrapper.read(requestBuffer, radioWrapper.packetSize(), this->listeningTimeout, pipe);
Ka_myk 4:9f635ab44d8e 33 DBG("READ: %d", ret);
Ka_myk 0:6a6f97ca5572 34 if (ret < 0) {
Ka_myk 0:6a6f97ca5572 35 return RADIO_NOT_WORKING;
Ka_myk 4:9f635ab44d8e 36 } else if(ret > 0) {
Ka_myk 4:9f635ab44d8e 37 CoapPDU request(requestBuffer, radioWrapper.packetSize(), ret);
Ka_myk 0:6a6f97ca5572 38 if (request.validate()) {
thewiztory 5:b8d21be6b36c 39 //request.printHuman();
Ka_myk 4:9f635ab44d8e 40 for (int i = 0; i < idQ.size(); ++i) { // check if messageId is known
Ka_myk 4:9f635ab44d8e 41 if(idQ[i] == request.getMessageID()) {
Ka_myk 4:9f635ab44d8e 42 if(request.getCode() == CoapPDU::COAP_POST) { // dla posta odpowiadamy pustym
Ka_myk 4:9f635ab44d8e 43 return ackRepeatedPost(request, *pipe);
Ka_myk 4:9f635ab44d8e 44 }
Ka_myk 4:9f635ab44d8e 45 } else { // geta obsługujemy od nowa
Ka_myk 4:9f635ab44d8e 46 break;
Ka_myk 4:9f635ab44d8e 47 }
Ka_myk 4:9f635ab44d8e 48 }
Ka_myk 4:9f635ab44d8e 49
Ka_myk 4:9f635ab44d8e 50 // zapamiętaj dane
Ka_myk 4:9f635ab44d8e 51 int index = nextIndex();
Ka_myk 4:9f635ab44d8e 52 this->idQ[index] = request.getMessageID();
Ka_myk 4:9f635ab44d8e 53 this->tokenQ[index] = request.getTokenPointer()[0];
Ka_myk 4:9f635ab44d8e 54
Ka_myk 0:6a6f97ca5572 55 if (request.getPayloadLength() <= len) {
Ka_myk 0:6a6f97ca5572 56 std::memcpy(buffer, request.getPayloadPointer(), static_cast<size_t>(request.getPayloadLength()));
Ka_myk 4:9f635ab44d8e 57
Ka_myk 0:6a6f97ca5572 58 if (request.getCode() == CoapPDU::COAP_GET) {
Ka_myk 0:6a6f97ca5572 59 *requestMethod = GET;
Ka_myk 0:6a6f97ca5572 60 } else if (request.getCode() == CoapPDU::COAP_POST) {
Ka_myk 0:6a6f97ca5572 61 *requestMethod = POST;
Ka_myk 0:6a6f97ca5572 62 }
Ka_myk 4:9f635ab44d8e 63
Ka_myk 0:6a6f97ca5572 64 char requestUri[32];
Ka_myk 0:6a6f97ca5572 65 int requestUriLen = 0;
Ka_myk 0:6a6f97ca5572 66 request.getURI(requestUri, 32, &requestUriLen);
Ka_myk 0:6a6f97ca5572 67 bool foundMatchingUri = false;
Ka_myk 4:9f635ab44d8e 68 DBG("URI: %s %d", requestUri, requestUriLen);
Ka_myk 4:9f635ab44d8e 69
Ka_myk 1:1d936c763440 70 for (int i = 0; i < urisNumber; ++i) {
Ka_myk 4:9f635ab44d8e 71 if(std::strcmp(requestUri, this->uris[i].data()) == 0) {
Ka_myk 0:6a6f97ca5572 72 *uriNumber = i;
Ka_myk 0:6a6f97ca5572 73 foundMatchingUri = true;
Ka_myk 0:6a6f97ca5572 74 }
Ka_myk 0:6a6f97ca5572 75 }
Ka_myk 4:9f635ab44d8e 76 if(!foundMatchingUri) {
Ka_myk 0:6a6f97ca5572 77 return UNKNOWN_URI;
Ka_myk 0:6a6f97ca5572 78 }
Ka_myk 4:9f635ab44d8e 79
Ka_myk 0:6a6f97ca5572 80 return request.getPayloadLength();
Ka_myk 0:6a6f97ca5572 81 } else {
Ka_myk 0:6a6f97ca5572 82 return SMALL_BUFFER;
Ka_myk 0:6a6f97ca5572 83 }
Ka_myk 0:6a6f97ca5572 84 }
Ka_myk 0:6a6f97ca5572 85 }
Ka_myk 4:9f635ab44d8e 86 return SILENT;
Ka_myk 0:6a6f97ca5572 87 }
Ka_myk 0:6a6f97ca5572 88
Ka_myk 4:9f635ab44d8e 89 int CoapServer::respond(int uri, uint8_t* buffer, int len, CoapPDU::Code responseCode, int pipe) {
Ka_myk 0:6a6f97ca5572 90 CoapPDU response = CoapPDU();
Ka_myk 4:9f635ab44d8e 91 response.setVersion(1);
Ka_myk 4:9f635ab44d8e 92 response.setType(CoapPDU::COAP_ACKNOWLEDGEMENT);
Ka_myk 4:9f635ab44d8e 93 response.setMessageID(this->idQ[this->lastMessageIndex]);
Ka_myk 4:9f635ab44d8e 94 uint8_t token = this->tokenQ[this->lastMessageIndex];
Ka_myk 4:9f635ab44d8e 95 response.setToken(&token, 1);
Ka_myk 0:6a6f97ca5572 96 response.setCode(responseCode);
Ka_myk 4:9f635ab44d8e 97 response.setURI((char*) uris[uri].data(), (int)uris[uri].size());
Ka_myk 4:9f635ab44d8e 98 if(len > 0) {
Ka_myk 4:9f635ab44d8e 99 response.setPayload(buffer, len);
Ka_myk 2:c3ca8b8526e0 100 }
Ka_myk 4:9f635ab44d8e 101 int ret = radioWrapper.write(response.getPDUPointer(), response.getPDULength(), pipe);
Ka_myk 4:9f635ab44d8e 102 if(ret < 0) {
Ka_myk 0:6a6f97ca5572 103 return RADIO_NOT_WORKING;
Ka_myk 0:6a6f97ca5572 104 }
Ka_myk 2:c3ca8b8526e0 105 return ret;
Ka_myk 0:6a6f97ca5572 106 }
Ka_myk 0:6a6f97ca5572 107
Ka_myk 4:9f635ab44d8e 108 CoapServer::CoapServer(unsigned queueMax, int timeout, std::string* uris, int urisNumber, int channel,
Ka_myk 4:9f635ab44d8e 109 unsigned long long tx_address, unsigned long long rx_addresses[]) : queueMax(queueMax), listeningTimeout(timeout),radioWrapper(channel, tx_address, rx_addresses), urisNumber(urisNumber), uris(uris){
Ka_myk 4:9f635ab44d8e 110 idQ.resize(queueMax, 0);
Ka_myk 4:9f635ab44d8e 111 tokenQ.resize(queueMax, 0);
Ka_myk 4:9f635ab44d8e 112 }