Library for handling subset of coap functionality by radio transmitter.

Dependencies:   nRF24L01P cantcoap

Dependents:   server2

Committer:
Ka_myk
Date:
Sun Jan 20 13:48:02 2019 +0000
Revision:
1:1d936c763440
Parent:
0:6a6f97ca5572
Child:
2:c3ca8b8526e0
try to make it work;

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 1:1d936c763440 29 for (int i = 0; i < urisNumber; ++i) {
Ka_myk 1:1d936c763440 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 int CoapServer::respond(int uri, uint8_t* buffer, int len, CoapPDU::Code responseCode) {
Ka_myk 0:6a6f97ca5572 48 CoapPDU response = CoapPDU();
Ka_myk 0:6a6f97ca5572 49 prepareAckPDU(response);
Ka_myk 0:6a6f97ca5572 50 response.setCode(responseCode);
Ka_myk 0:6a6f97ca5572 51 response.setPayload(buffer, len);
Ka_myk 1:1d936c763440 52 response.setURI((char*) uris[uri].data(), (int)uris[uri].size());
Ka_myk 0:6a6f97ca5572 53 int ret = radioWrapper.write(response.getPDUPointer(), response.getPDULength());
Ka_myk 0:6a6f97ca5572 54 if(ret < 0) {
Ka_myk 0:6a6f97ca5572 55 return RADIO_NOT_WORKING;
Ka_myk 0:6a6f97ca5572 56 }
Ka_myk 0:6a6f97ca5572 57 return 0;
Ka_myk 0:6a6f97ca5572 58 }
Ka_myk 0:6a6f97ca5572 59
Ka_myk 0:6a6f97ca5572 60 void CoapServer::prepareAckPDU(CoapPDU& coapPDU) {
Ka_myk 0:6a6f97ca5572 61 coapPDU.setVersion(1);
Ka_myk 0:6a6f97ca5572 62 coapPDU.setType(CoapPDU::COAP_ACKNOWLEDGEMENT);
Ka_myk 0:6a6f97ca5572 63 coapPDU.setMessageID(this->lastMessageId);
Ka_myk 0:6a6f97ca5572 64 coapPDU.setToken(this->lastMessageToken, 4);
Ka_myk 0:6a6f97ca5572 65 }
Ka_myk 1:1d936c763440 66
Ka_myk 1:1d936c763440 67 CoapServer::CoapServer(int timeout, std::string* uris, int urisNumber, int channel, unsigned long long rx_address, unsigned long long tx_address):
Ka_myk 1:1d936c763440 68 listeningTimeout(timeout), radioWrapper(channel, rx_address, tx_address), urisNumber(urisNumber), uris(uris) {}