client version of coap

Dependencies:   nRF24L01P cantcoap3

Dependents:   client3

Revision:
2:e8823d9fa162
Parent:
1:1d936c763440
--- a/radioWrapper.cpp	Sun Jan 20 13:48:02 2019 +0000
+++ b/radioWrapper.cpp	Thu Jan 24 16:33:30 2019 +0000
@@ -1,5 +1,6 @@
 #include "radioWrapper.h"
 #include "mbed.h"
+#include "dbg.h"
 
 int RadioWrapper::read(uint8_t* buffer, int len, int timeout) {
     Timer t;
@@ -8,26 +9,41 @@
     if (len < packetSize()) {
         return -1;
     }
-    int ret = radio.read(NRF24L01P_PIPE_P0, (char*) buffer, packetSize());
-    while(ret == 0 && t.read_ms() < timeout) {    
-        wait_ms(10);
-        ret = radio.read(NRF24L01P_PIPE_P0, (char*) buffer, packetSize());
+    while(t.read_ms() < timeout) {
+        if(radio.readable(NRF24L01P_PIPE_P1)) {
+            DBG("%d", t.read_ms());
+            uint8_t message[packetSize()];
+            int ret = radio.read(NRF24L01P_PIPE_P1, (char*) message, packetSize());
+            DBG("RECIEVED %d, %s \r\n", ret, buffer);
+            t.stop();
+            if(ret>0) {
+                uint8_t encodedLen = message[0];
+                std::memcpy(buffer, message+1, encodedLen);
+                return encodedLen;
+            } else {
+                return ret;
+            }
+        }
     }
     t.stop();
-    return ret;
+    return 0;
 }
 
-int RadioWrapper::write(uint8_t* buffer, int len) {
-    // check if buffor is small enough to send in one package
+int RadioWrapper::write(uint8_t* buffer, uint8_t len) {
+    // check if buffor is small enough to send in one package + len
     if (len > packetSize()) {
         return -1;
     }
-    int ret = radio.write(NRF24L01P_PIPE_P0, (char*) buffer, packetSize());
+
+    uint8_t message[packetSize()];
+    message[0] = len;
+    std::memcpy(message+1, buffer, len);
+    int ret = radio.write(NRF24L01P_PIPE_P0, (char*) message, packetSize());
     return ret;
 }
 
 RadioWrapper::RadioWrapper(int channel, unsigned long long rx_address, unsigned long long tx_address) :
-radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2) {
+        radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2) {
     radio.powerDown();
     radio.powerUp();
 
@@ -38,6 +54,19 @@
     radio.setCrcWidth(NRF24L01P_CRC_8_BIT);
 
     radio.setTxAddress(tx_address, 4);
-    radio.setRxAddress(rx_address, 4, NRF24L01P_PIPE_P0);
+    radio.setRxAddress(tx_address, 4, NRF24L01P_PIPE_P0);
+    radio.setRxAddress(rx_address, 4, NRF24L01P_PIPE_P1);
     radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P0);
+    radio.setTransferSize(packetSize(), NRF24L01P_PIPE_P1);
+
+    radio.setReceiveMode();
+    radio.enable();
+
+    // Display the (default) setup of the nRF24L01+ chip
+    DBG( "nRF24L01+ Frequency    : %d MHz\r\n",  radio.getRfFrequency() );
+    DBG( "nRF24L01+ Output power : %d dBm\r\n",  radio.getRfOutputPower() );
+    DBG( "nRF24L01+ Data Rate    : %d kbps\r\n", radio.getAirDataRate() );
+    DBG( "nRF24L01+ TX Address   : 0x%010llX\r\n", radio.getTxAddress() );
+    DBG( "nRF24L01+ RX0 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) );
+    DBG( "nRF24L01+ RX1 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) );
 }