Library for handling subset of coap functionality by radio transmitter.
Dependencies: nRF24L01P cantcoap
coapServer.cpp@2:c3ca8b8526e0, 2019-01-20 (annotated)
- Committer:
- Ka_myk
- Date:
- Sun Jan 20 23:27:10 2019 +0000
- Revision:
- 2:c3ca8b8526e0
- Parent:
- 1:1d936c763440
- Child:
- 4:9f635ab44d8e
should work somehow;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:6a6f97ca5572 | 8 | int CoapServer::listen(uint8_t* buffer, int len, METHOD* requestMethod, int* uriNumber) { |
Ka_myk | 0:6a6f97ca5572 | 9 | uint8_t requestBuffer[radioWrapper.packetSize()]; |
Ka_myk | 0:6a6f97ca5572 | 10 | int ret = radioWrapper.read(requestBuffer, radioWrapper.packetSize(), this->listeningTimeout); |
Ka_myk | 2:c3ca8b8526e0 | 11 | DBG("REC: %d \r\n", ret); |
Ka_myk | 0:6a6f97ca5572 | 12 | if (ret < 0) { |
Ka_myk | 0:6a6f97ca5572 | 13 | return RADIO_NOT_WORKING; |
Ka_myk | 2:c3ca8b8526e0 | 14 | } else if (ret > 0) { |
Ka_myk | 2:c3ca8b8526e0 | 15 | CoapPDU request(requestBuffer, ret, radioWrapper.packetSize()); |
Ka_myk | 0:6a6f97ca5572 | 16 | if (request.validate()) { |
Ka_myk | 2:c3ca8b8526e0 | 17 | request.printHuman(); |
Ka_myk | 0:6a6f97ca5572 | 18 | this->lastMessageId = request.getMessageID(); |
Ka_myk | 2:c3ca8b8526e0 | 19 | std::memcpy(this->lastMessageToken, request.getTokenPointer(), 1); |
Ka_myk | 0:6a6f97ca5572 | 20 | if (request.getPayloadLength() <= len) { |
Ka_myk | 0:6a6f97ca5572 | 21 | std::memcpy(buffer, request.getPayloadPointer(), static_cast<size_t>(request.getPayloadLength())); |
Ka_myk | 0:6a6f97ca5572 | 22 | if (request.getCode() == CoapPDU::COAP_GET) { |
Ka_myk | 0:6a6f97ca5572 | 23 | *requestMethod = GET; |
Ka_myk | 0:6a6f97ca5572 | 24 | } else if (request.getCode() == CoapPDU::COAP_POST) { |
Ka_myk | 0:6a6f97ca5572 | 25 | *requestMethod = POST; |
Ka_myk | 0:6a6f97ca5572 | 26 | } |
Ka_myk | 0:6a6f97ca5572 | 27 | char requestUri[32]; |
Ka_myk | 0:6a6f97ca5572 | 28 | int requestUriLen = 0; |
Ka_myk | 0:6a6f97ca5572 | 29 | request.getURI(requestUri, 32, &requestUriLen); |
Ka_myk | 0:6a6f97ca5572 | 30 | bool foundMatchingUri = false; |
Ka_myk | 2:c3ca8b8526e0 | 31 | DBG("URI: %s", requestUri); |
Ka_myk | 1:1d936c763440 | 32 | for (int i = 0; i < urisNumber; ++i) { |
Ka_myk | 2:c3ca8b8526e0 | 33 | if (std::strcmp(requestUri, this->uris[i].data()) == 0) { |
Ka_myk | 0:6a6f97ca5572 | 34 | *uriNumber = i; |
Ka_myk | 0:6a6f97ca5572 | 35 | foundMatchingUri = true; |
Ka_myk | 0:6a6f97ca5572 | 36 | } |
Ka_myk | 0:6a6f97ca5572 | 37 | } |
Ka_myk | 2:c3ca8b8526e0 | 38 | if (!foundMatchingUri) { |
Ka_myk | 0:6a6f97ca5572 | 39 | return UNKNOWN_URI; |
Ka_myk | 0:6a6f97ca5572 | 40 | } |
Ka_myk | 0:6a6f97ca5572 | 41 | return request.getPayloadLength(); |
Ka_myk | 0:6a6f97ca5572 | 42 | } else { |
Ka_myk | 0:6a6f97ca5572 | 43 | return SMALL_BUFFER; |
Ka_myk | 0:6a6f97ca5572 | 44 | } |
Ka_myk | 0:6a6f97ca5572 | 45 | } |
Ka_myk | 0:6a6f97ca5572 | 46 | } |
Ka_myk | 0:6a6f97ca5572 | 47 | return 0; |
Ka_myk | 0:6a6f97ca5572 | 48 | } |
Ka_myk | 0:6a6f97ca5572 | 49 | |
Ka_myk | 0:6a6f97ca5572 | 50 | int CoapServer::respond(int uri, uint8_t* buffer, int len, CoapPDU::Code responseCode) { |
Ka_myk | 0:6a6f97ca5572 | 51 | CoapPDU response = CoapPDU(); |
Ka_myk | 0:6a6f97ca5572 | 52 | prepareAckPDU(response); |
Ka_myk | 0:6a6f97ca5572 | 53 | response.setCode(responseCode); |
Ka_myk | 2:c3ca8b8526e0 | 54 | |
Ka_myk | 2:c3ca8b8526e0 | 55 | DBG("uri: %d, %s", (int) uris[uri].size(), uris[uri].data()); |
Ka_myk | 2:c3ca8b8526e0 | 56 | if (response.addOption(CoapPDU::COAP_OPTION_URI_PATH, (int) uris[uri].size(), (uint8_t*) uris[uri].data()) != 0) { |
Ka_myk | 2:c3ca8b8526e0 | 57 | return COAP_ERROR; |
Ka_myk | 2:c3ca8b8526e0 | 58 | } |
Ka_myk | 2:c3ca8b8526e0 | 59 | DBG("SEND RESPONSE"); |
Ka_myk | 0:6a6f97ca5572 | 60 | response.setPayload(buffer, len); |
Ka_myk | 2:c3ca8b8526e0 | 61 | response.printHuman(); |
Ka_myk | 0:6a6f97ca5572 | 62 | int ret = radioWrapper.write(response.getPDUPointer(), response.getPDULength()); |
Ka_myk | 2:c3ca8b8526e0 | 63 | if (ret < 0) { |
Ka_myk | 0:6a6f97ca5572 | 64 | return RADIO_NOT_WORKING; |
Ka_myk | 0:6a6f97ca5572 | 65 | } |
Ka_myk | 2:c3ca8b8526e0 | 66 | return ret; |
Ka_myk | 0:6a6f97ca5572 | 67 | } |
Ka_myk | 0:6a6f97ca5572 | 68 | |
Ka_myk | 0:6a6f97ca5572 | 69 | void CoapServer::prepareAckPDU(CoapPDU& coapPDU) { |
Ka_myk | 0:6a6f97ca5572 | 70 | coapPDU.setVersion(1); |
Ka_myk | 0:6a6f97ca5572 | 71 | coapPDU.setType(CoapPDU::COAP_ACKNOWLEDGEMENT); |
Ka_myk | 0:6a6f97ca5572 | 72 | coapPDU.setMessageID(this->lastMessageId); |
Ka_myk | 2:c3ca8b8526e0 | 73 | coapPDU.setToken(this->lastMessageToken, 1); |
Ka_myk | 0:6a6f97ca5572 | 74 | } |
Ka_myk | 1:1d936c763440 | 75 | |
Ka_myk | 2:c3ca8b8526e0 | 76 | CoapServer::CoapServer(int timeout, std::string* uris, int urisNumber, int channel, unsigned long long rx_address, |
Ka_myk | 2:c3ca8b8526e0 | 77 | unsigned long long tx_address) : |
Ka_myk | 2:c3ca8b8526e0 | 78 | listeningTimeout(timeout), radioWrapper(channel, rx_address, tx_address), urisNumber(urisNumber), uris(uris) {} |