Library for handling subset of coap functionality by radio transmitter.

Dependencies:   nRF24L01P cantcoap

Dependents:   server2

Committer:
Ka_myk
Date:
Fri Jan 18 14:12:24 2019 +0000
Revision:
0:6a6f97ca5572
Child:
1:1d936c763440
initial version

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 0:6a6f97ca5572 6
Ka_myk 0:6a6f97ca5572 7 int CoapServer::listen(uint8_t* buffer, int len, METHOD* requestMethod, int* uriNumber) {
Ka_myk 0:6a6f97ca5572 8 uint8_t requestBuffer[radioWrapper.packetSize()];
Ka_myk 0:6a6f97ca5572 9 int ret = radioWrapper.read(requestBuffer, radioWrapper.packetSize(), this->listeningTimeout);
Ka_myk 0:6a6f97ca5572 10 if (ret < 0) {
Ka_myk 0:6a6f97ca5572 11 return RADIO_NOT_WORKING;
Ka_myk 0:6a6f97ca5572 12 } else {
Ka_myk 0:6a6f97ca5572 13 CoapPDU request(requestBuffer, radioWrapper.packetSize(), ret);
Ka_myk 0:6a6f97ca5572 14 if (request.validate()) {
Ka_myk 0:6a6f97ca5572 15
Ka_myk 0:6a6f97ca5572 16 this->lastMessageId = request.getMessageID();
Ka_myk 0:6a6f97ca5572 17 std::memcpy(this->lastMessageToken, request.getTokenPointer(), 4);
Ka_myk 0:6a6f97ca5572 18 if (request.getPayloadLength() <= len) {
Ka_myk 0:6a6f97ca5572 19 std::memcpy(buffer, request.getPayloadPointer(), static_cast<size_t>(request.getPayloadLength()));
Ka_myk 0:6a6f97ca5572 20 if (request.getCode() == CoapPDU::COAP_GET) {
Ka_myk 0:6a6f97ca5572 21 *requestMethod = GET;
Ka_myk 0:6a6f97ca5572 22 } else if (request.getCode() == CoapPDU::COAP_POST) {
Ka_myk 0:6a6f97ca5572 23 *requestMethod = POST;
Ka_myk 0:6a6f97ca5572 24 }
Ka_myk 0:6a6f97ca5572 25 char requestUri[32];
Ka_myk 0:6a6f97ca5572 26 int requestUriLen = 0;
Ka_myk 0:6a6f97ca5572 27 request.getURI(requestUri, 32, &requestUriLen);
Ka_myk 0:6a6f97ca5572 28 bool foundMatchingUri = false;
Ka_myk 0:6a6f97ca5572 29 for (int i = 0; i < urisNumber(); ++i) {
Ka_myk 0:6a6f97ca5572 30 if(std::strcmp(requestUri, (*(this->uris))[i].data()) == 0) {
Ka_myk 0:6a6f97ca5572 31 *uriNumber = i;
Ka_myk 0:6a6f97ca5572 32 foundMatchingUri = true;
Ka_myk 0:6a6f97ca5572 33 }
Ka_myk 0:6a6f97ca5572 34 }
Ka_myk 0:6a6f97ca5572 35 if(!foundMatchingUri) {
Ka_myk 0:6a6f97ca5572 36 return UNKNOWN_URI;
Ka_myk 0:6a6f97ca5572 37 }
Ka_myk 0:6a6f97ca5572 38 return request.getPayloadLength();
Ka_myk 0:6a6f97ca5572 39 } else {
Ka_myk 0:6a6f97ca5572 40 return SMALL_BUFFER;
Ka_myk 0:6a6f97ca5572 41 }
Ka_myk 0:6a6f97ca5572 42 }
Ka_myk 0:6a6f97ca5572 43 }
Ka_myk 0:6a6f97ca5572 44 return 0;
Ka_myk 0:6a6f97ca5572 45 }
Ka_myk 0:6a6f97ca5572 46
Ka_myk 0:6a6f97ca5572 47
Ka_myk 0:6a6f97ca5572 48
Ka_myk 0:6a6f97ca5572 49 int CoapServer::urisNumber() {
Ka_myk 0:6a6f97ca5572 50 return sizeof(*uris)/ sizeof(std::string);
Ka_myk 0:6a6f97ca5572 51 }
Ka_myk 0:6a6f97ca5572 52
Ka_myk 0:6a6f97ca5572 53 int CoapServer::respond(int uri, uint8_t* buffer, int len, CoapPDU::Code responseCode) {
Ka_myk 0:6a6f97ca5572 54 CoapPDU response = CoapPDU();
Ka_myk 0:6a6f97ca5572 55 prepareAckPDU(response);
Ka_myk 0:6a6f97ca5572 56 response.setCode(responseCode);
Ka_myk 0:6a6f97ca5572 57 response.setPayload(buffer, len);
Ka_myk 0:6a6f97ca5572 58 response.setURI((char*) uris[uri]->data(), (int)uris[uri]->size());
Ka_myk 0:6a6f97ca5572 59 int ret = radioWrapper.write(response.getPDUPointer(), response.getPDULength());
Ka_myk 0:6a6f97ca5572 60 if(ret < 0) {
Ka_myk 0:6a6f97ca5572 61 return RADIO_NOT_WORKING;
Ka_myk 0:6a6f97ca5572 62 }
Ka_myk 0:6a6f97ca5572 63 return 0;
Ka_myk 0:6a6f97ca5572 64 }
Ka_myk 0:6a6f97ca5572 65
Ka_myk 0:6a6f97ca5572 66 void CoapServer::prepareAckPDU(CoapPDU& coapPDU) {
Ka_myk 0:6a6f97ca5572 67 coapPDU.setVersion(1);
Ka_myk 0:6a6f97ca5572 68 coapPDU.setType(CoapPDU::COAP_ACKNOWLEDGEMENT);
Ka_myk 0:6a6f97ca5572 69 coapPDU.setMessageID(this->lastMessageId);
Ka_myk 0:6a6f97ca5572 70 coapPDU.setToken(this->lastMessageToken, 4);
Ka_myk 0:6a6f97ca5572 71 }