Skip to content

Commit

Permalink
Merge branch 'jjwbruijn:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasniesner authored Jan 9, 2024
2 parents d5bec87 + bd89651 commit 1d3bb07
Show file tree
Hide file tree
Showing 5 changed files with 742 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "led.h"
#include "proto.h"
#include "radio.h"
#include "subGhz.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "esp_err.h"
Expand Down Expand Up @@ -39,8 +40,8 @@ uint16_t version = 0x0019;

#define RAW_PKT_PADDING 2

uint8_t radiotxbuffer[128];
uint8_t radiorxbuffer[128];
uint8_t radiotxbuffer[135];
uint8_t radiorxbuffer[135];

static uint32_t housekeepingTimer;

Expand Down Expand Up @@ -69,8 +70,8 @@ uint8_t curNoUpdate = 0;

bool highspeedSerial = false;

void sendXferCompleteAck(uint8_t *dst);
void sendCancelXfer(uint8_t *dst);
void sendXferCompleteAck(uint8_t *dst, bool isSubGHz);
void sendCancelXfer(uint8_t *dst, bool isSubGHz);
void espNotifyAPInfo();

// tools
Expand Down Expand Up @@ -115,7 +116,7 @@ uint8_t getBlockDataLength() {
// pendingdata slot stuff
int8_t findSlotForMac(const uint8_t *mac) {
for (uint8_t c = 0; c < MAX_PENDING_MACS; c++) {
if (memcmp(mac, ((uint8_t *)&(pendingDataArr[c].targetMac)), 8) == 0) {
if (memcmp(mac, ((uint8_t *) & (pendingDataArr[c].targetMac)), 8) == 0) {
if (pendingDataArr[c].attemptsLeft != 0) {
return c;
}
Expand All @@ -133,7 +134,7 @@ int8_t findFreeSlot() {
}
int8_t findSlotForVer(const uint8_t *ver) {
for (uint8_t c = 0; c < MAX_PENDING_MACS; c++) {
if (memcmp(ver, ((uint8_t *)&(pendingDataArr[c].availdatainfo.dataVer)), 8) == 0) {
if (memcmp(ver, ((uint8_t *) & (pendingDataArr[c].availdatainfo.dataVer)), 8) == 0) {
if (pendingDataArr[c].attemptsLeft != 0) return c;
}
}
Expand Down Expand Up @@ -431,7 +432,7 @@ void espNotifyTagReturnData(uint8_t *src, uint8_t len) {
}

// process data from tag
void processBlockRequest(const uint8_t *buffer, uint8_t forceBlockDownload) {
void processBlockRequest(const uint8_t *buffer, uint8_t forceBlockDownload, bool isSubGHz) {
struct MacFrameNormal *rxHeader = (struct MacFrameNormal *)buffer;
struct blockRequest *blockReq = (struct blockRequest *)(buffer + sizeof(struct MacFrameNormal) + 1);
if (!checkCRC(blockReq, sizeof(struct blockRequest))) return;
Expand All @@ -448,15 +449,15 @@ void processBlockRequest(const uint8_t *buffer, uint8_t forceBlockDownload) {
} else {
// we're talking to another mac, let this mac know we can't accomodate another request right now
pr("BUSY!\n");
sendCancelXfer(rxHeader->src);
sendCancelXfer(rxHeader->src, isSubGHz);
return;
}
}

// check if we have data for this mac
if (findSlotForMac(rxHeader->src) == -1) {
// no data for this mac, politely tell it to fuck off
sendCancelXfer(rxHeader->src);
sendCancelXfer(rxHeader->src, isSubGHz);
return;
}

Expand Down Expand Up @@ -512,7 +513,7 @@ void processBlockRequest(const uint8_t *buffer, uint8_t forceBlockDownload) {

addCRC((void *)blockRequestAck, sizeof(struct blockRequestAck));

radioTx(radiotxbuffer);
radioTx(radiotxbuffer, isSubGHz);

// save the target for the blockdata
memcpy(dstMac, rxHeader->src, 8);
Expand All @@ -525,7 +526,7 @@ void processBlockRequest(const uint8_t *buffer, uint8_t forceBlockDownload) {
}
}

void processAvailDataReq(uint8_t *buffer) {
void processAvailDataReq(uint8_t *buffer, bool isSubGHz) {
struct MacFrameBcast *rxHeader = (struct MacFrameBcast *)buffer;
struct AvailDataReq *availDataReq = (struct AvailDataReq *)(buffer + sizeof(struct MacFrameBcast) + 1);

Expand Down Expand Up @@ -562,13 +563,13 @@ void processAvailDataReq(uint8_t *buffer) {
txHeader->fcs.srcAddrType = 3;
txHeader->seq = seq++;
addCRC(availDataInfo, sizeof(struct AvailDataInfo));
radioTx(radiotxbuffer);
radioTx(radiotxbuffer, isSubGHz);
memset(lastAckMac, 0, 8); // reset lastAckMac, so we can record if we've received exactly one ack packet
espNotifyAvailDataReq(availDataReq, rxHeader->src);
}
void processXferComplete(uint8_t *buffer) {
void processXferComplete(uint8_t *buffer, bool isSubGHz) {
struct MacFrameNormal *rxHeader = (struct MacFrameNormal *)buffer;
sendXferCompleteAck(rxHeader->src);
sendXferCompleteAck(rxHeader->src, isSubGHz);
if (memcmp(lastAckMac, rxHeader->src, 8) != 0) {
memcpy((void *)lastAckMac, (void *)rxHeader->src, 8);
espNotifyXferComplete(rxHeader->src);
Expand All @@ -577,7 +578,7 @@ void processXferComplete(uint8_t *buffer) {
}
}

void processTagReturnData(uint8_t *buffer, uint8_t len) {
void processTagReturnData(uint8_t *buffer, uint8_t len, bool isSubGHz) {
struct MacFrameBcast *rxframe = (struct MacFrameBcast *)buffer;
struct MacFrameNormal *frameHeader = (struct MacFrameNormal *)(radiotxbuffer + 1);

Expand All @@ -592,13 +593,13 @@ void processTagReturnData(uint8_t *buffer, uint8_t len) {
radiotxbuffer[2] = 0xCC; // normal frame
frameHeader->seq = seq++;
frameHeader->pan = rxframe->srcPan;
radioTx(radiotxbuffer);
radioTx(radiotxbuffer, isSubGHz);

espNotifyTagReturnData(rxframe->src, len - (sizeof(struct MacFrameBcast) + 1));
}

// send block data to the tag
void sendPart(uint8_t partNo) {
void sendPart(uint8_t partNo, bool isSubGHz) {
struct MacFrameNormal *frameHeader = (struct MacFrameNormal *)(radiotxbuffer + 1);
struct blockPart *blockPart = (struct blockPart *)(radiotxbuffer + sizeof(struct MacFrameNormal) + 2);
memset(radiotxbuffer + 1, 0, sizeof(struct blockPart) + sizeof(struct MacFrameNormal));
Expand All @@ -616,9 +617,9 @@ void sendPart(uint8_t partNo) {
frameHeader->fcs.srcAddrType = 3;
frameHeader->seq = seq++;
frameHeader->pan = dstPan;
radioTx(radiotxbuffer);
radioTx(radiotxbuffer, isSubGHz);
}
void sendBlockData() {
void sendBlockData(bool isSubGHz) {
if (getBlockDataLength() == 0) {
pr("Invalid block request received, 0 parts..\n");
requestedData.requestedParts[0] |= 0x01;
Expand All @@ -639,13 +640,13 @@ void sendBlockData() {
while (partNo < BLOCK_MAX_PARTS) {
for (uint8_t c = 0; (c < BLOCK_MAX_PARTS) && (partNo < BLOCK_MAX_PARTS); c++) {
if (requestedData.requestedParts[c / 8] & (1 << (c % 8))) {
sendPart(c);
sendPart(c, isSubGHz);
partNo++;
}
}
}
}
void sendXferCompleteAck(uint8_t *dst) {
void sendXferCompleteAck(uint8_t *dst, bool isSubGHz) {
struct MacFrameNormal *frameHeader = (struct MacFrameNormal *)(radiotxbuffer + 1);
memset(radiotxbuffer + 1, 0, sizeof(struct blockPart) + sizeof(struct MacFrameNormal));
radiotxbuffer[sizeof(struct MacFrameNormal) + 1] = PKT_XFER_COMPLETE_ACK;
Expand All @@ -658,9 +659,9 @@ void sendXferCompleteAck(uint8_t *dst) {
frameHeader->fcs.srcAddrType = 3;
frameHeader->seq = seq++;
frameHeader->pan = dstPan;
radioTx(radiotxbuffer);
radioTx(radiotxbuffer, isSubGHz);
}
void sendCancelXfer(uint8_t *dst) {
void sendCancelXfer(uint8_t *dst, bool isSubGHz) {
struct MacFrameNormal *frameHeader = (struct MacFrameNormal *)(radiotxbuffer + 1);
memset(radiotxbuffer + 1, 0, sizeof(struct blockPart) + sizeof(struct MacFrameNormal));
radiotxbuffer[sizeof(struct MacFrameNormal) + 1] = PKT_CANCEL_XFER;
Expand All @@ -673,9 +674,9 @@ void sendCancelXfer(uint8_t *dst) {
frameHeader->fcs.srcAddrType = 3;
frameHeader->seq = seq++;
frameHeader->pan = dstPan;
radioTx(radiotxbuffer);
radioTx(radiotxbuffer, isSubGHz);
}
void sendPong(void *buf) {
void sendPong(void *buf, bool isSubGHz) {
struct MacFrameBcast *rxframe = (struct MacFrameBcast *)buf;
struct MacFrameNormal *frameHeader = (struct MacFrameNormal *)(radiotxbuffer + 1);
radiotxbuffer[sizeof(struct MacFrameNormal) + 1] = PKT_PONG;
Expand All @@ -687,9 +688,10 @@ void sendPong(void *buf) {
radiotxbuffer[2] = 0xCC; // normal frame
frameHeader->seq = seq++;
frameHeader->pan = rxframe->srcPan;
radioTx(radiotxbuffer);
radioTx(radiotxbuffer, isSubGHz);
}

extern uint8_t mSelfMac[8];
void setup() {
Serial.begin(115200);

Expand All @@ -710,10 +712,20 @@ void setup() {
housekeepingTimer = millis();
}

bool isSubGhzRx = false;
void loop() {
while ((millis() - housekeepingTimer) < ((1000 * HOUSEKEEPING_INTERVAL) - 100)) {
int8_t ret = commsRxUnencrypted(radiorxbuffer);
int8_t ret = commsRxUnencrypted(radiorxbuffer, &isSubGhzRx);
if (ret > 1) {
if (0)
{
Serial.printf("RXed packet len %u :", ret);
for (int t = 0; t < ret; t++) {
Serial.printf(" %02x", radiorxbuffer[t]);
}
Serial.printf("\n");
}

led_flash(0);
// received a packet, lets see what it is
switch (getPacketType(radiorxbuffer)) {
Expand All @@ -722,35 +734,35 @@ void loop() {
// old version of the AvailDataReq struct, set all the new fields to zero, so it will pass the CRC
memset(radiorxbuffer + 1 + sizeof(struct MacFrameBcast) + sizeof(struct oldAvailDataReq), 0,
sizeof(struct AvailDataReq) - sizeof(struct oldAvailDataReq) + 2);
processAvailDataReq(radiorxbuffer);
processAvailDataReq(radiorxbuffer, isSubGhzRx);
} else if (ret == 40) {
// new version of the AvailDataReq struct
processAvailDataReq(radiorxbuffer);
processAvailDataReq(radiorxbuffer, isSubGhzRx);
}
break;
case PKT_BLOCK_REQUEST:
processBlockRequest(radiorxbuffer, 1);
processBlockRequest(radiorxbuffer, 1, isSubGhzRx);
break;
case PKT_BLOCK_PARTIAL_REQUEST:
processBlockRequest(radiorxbuffer, 0);
processBlockRequest(radiorxbuffer, 0, isSubGhzRx);
break;
case PKT_XFER_COMPLETE:
processXferComplete(radiorxbuffer);
processXferComplete(radiorxbuffer, isSubGhzRx);
break;
case PKT_PING:
sendPong(radiorxbuffer);
sendPong(radiorxbuffer, isSubGhzRx);
break;
case PKT_AVAIL_DATA_SHORTREQ:
// a short AvailDataReq is basically a very short (1 byte payload) packet that requires little preparation on the tx side, for optimal
// battery use bytes of the struct are set 0, so it passes the checksum test, and the ESP32 can detect that no interesting payload is
// sent
if (ret == 18) {
memset(radiorxbuffer + 1 + sizeof(struct MacFrameBcast), 0, sizeof(struct AvailDataReq) + 2);
processAvailDataReq(radiorxbuffer);
processAvailDataReq(radiorxbuffer, isSubGhzRx);
}
break;
case PKT_TAG_RETURN_DATA:
processTagReturnData(radiorxbuffer, ret);
processTagReturnData(radiorxbuffer, ret, isSubGhzRx);
break;
default:
Serial.printf("t=%02X\r\n", getPacketType(radiorxbuffer));
Expand All @@ -765,7 +777,7 @@ void loop() {

if (blockStartTimer) {
if (millis() > blockStartTimer) {
sendBlockData();
sendBlockData(isSubGhzRx);
blockStartTimer = 0;
}
}
Expand Down
63 changes: 51 additions & 12 deletions ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/radio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "freertos/queue.h"
#include "freertos/task.h"
#include "led.h"
#include "subGhz.h"
#include "proto.h"
#include "sdkconfig.h"
// if you get an error about soc/lp_uart_reg.h not being found,
Expand All @@ -22,6 +23,8 @@
#include <string.h>
#include <Arduino.h>

bool has_sub_ghz = false;

uint8_t mSelfMac[8];
volatile uint8_t isInTransmit = 0;
QueueHandle_t packet_buffer = NULL;
Expand Down Expand Up @@ -83,34 +86,70 @@ void radio_init(uint8_t ch) {
mSelfMac[0], mSelfMac[1], mSelfMac[2], mSelfMac[3],
mSelfMac[4], mSelfMac[5], mSelfMac[6], mSelfMac[7],
esp_ieee802154_get_short_address());


// Lets here take care of the SubGhz Init
if (!init_subGhz())
Serial.printf("Sub-GHz radio init failed\r\n");
else if (!tiRadioSetChannel(ch))
Serial.printf("SubGHz radio channel fail\r\n");
else
has_sub_ghz = true;

Serial.printf("SubGhz %s\r\n", has_sub_ghz ? "Active" : "Not Found");
if (has_sub_ghz) {
tiRadioRxFilterCfg(mSelfMac, SHORT_MAC_UNUSED, PROTO_PAN_ID, true);
tiRadioTxConfigure(mSelfMac, SHORT_MAC_UNUSED, PROTO_PAN_ID);
tiRadioRxEnable(true, false);
}
}

// uint32_t lastZbTx = 0;
bool radioTx(uint8_t *packet) {
static uint8_t txPKT[130];
bool radioTx(uint8_t *packet, bool subGhz) {
led_flash(1);
while (isInTransmit) {
if (has_sub_ghz && subGhz) {
tiRadioTxLL(packet);
} else {
static uint8_t txPKT[130];
while (isInTransmit) {
}
// while (millis() - lastZbTx < 6) {
// }
// lastZbTx = millis();
memcpy(txPKT, packet, packet[0]);
isInTransmit = 1;
esp_ieee802154_transmit(txPKT, false);
return true;
}
// while (millis() - lastZbTx < 6) {
// }
// lastZbTx = millis();
memcpy(txPKT, packet, packet[0]);
isInTransmit = 1;
esp_ieee802154_transmit(txPKT, false);
return true;
}

void radioSetChannel(uint8_t ch) {
radio_init(ch);
if (has_sub_ghz)
tiRadioSetChannel(ch);
}

void radioSetTxPower(uint8_t power) {}

int8_t commsRxUnencrypted(uint8_t *data) {
static uint8_t inner_rxPKT_out[130];
int8_t commsRxUnencrypted(uint8_t *data, bool *subGhzRx) {
int8_t rssi_sub_rx = 0;
uint8_t lqi_sub_rx = 0;

static uint8_t inner_rxPKT_out[135];
if (xQueueReceive(packet_buffer, (void *)&inner_rxPKT_out, pdMS_TO_TICKS(100)) == pdTRUE) {
memcpy(data, &inner_rxPKT_out[1], inner_rxPKT_out[0] + 1);
*subGhzRx = false; // This is Normal data
return inner_rxPKT_out[0] - 2;
}
if (has_sub_ghz) {
int32_t ret_sub_rx_len = tiRadioRxDequeuePkt(inner_rxPKT_out, sizeof(inner_rxPKT_out), &rssi_sub_rx, &lqi_sub_rx);
if (ret_sub_rx_len > 0)
{
//Serial.printf("Got Sub Ghz Len %i data: %i %u\r\n", ret_sub_rx, rssi_sub_rx, lqi_sub_rx);
memcpy(data, inner_rxPKT_out, ret_sub_rx_len);
*subGhzRx = true; // This is SubGHz data
return ret_sub_rx_len;
}
}
return 0;
}
4 changes: 2 additions & 2 deletions ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
extern uint8_t mSelfMac[8];

void radio_init(uint8_t ch);
bool radioTx(uint8_t *packet);
bool radioTx(uint8_t *packet, bool subGhz);
void radioSetChannel(uint8_t ch);
void radioSetTxPower(uint8_t power);
int8_t commsRxUnencrypted(uint8_t *data);
int8_t commsRxUnencrypted(uint8_t *data, bool *subGhzRx);
Loading

0 comments on commit 1d3bb07

Please sign in to comment.