diff --git a/main/packets.h b/main/packets.h index f027309..9c9190c 100644 --- a/main/packets.h +++ b/main/packets.h @@ -19,6 +19,7 @@ typedef struct __attribute__((packed)) uint32_t packetIndex; uint8_t packetType; uint32_t missionTimer; + uint32_t CRCCheck; } DownBoundPacket; typedef struct __attribute__((packed)) @@ -90,6 +91,8 @@ typedef struct __attribute__((packed)) int16_t currentServoB; int16_t targetServoB; + uint8_t telemetryIndex; + } TelemetryPacket; typedef struct __attribute__((packed)) @@ -97,6 +100,7 @@ typedef struct __attribute__((packed)) char syncPhrase[10]; uint32_t packetIndex; uint8_t packetType; + uint32_t CRCCheck; } UplinkPacket; typedef struct __attribute__((packed)) diff --git a/main/radio.c b/main/radio.c index 329d270..db5d751 100644 --- a/main/radio.c +++ b/main/radio.c @@ -27,6 +27,10 @@ void send_ack(uint32_t packetIndex, uint32_t crc) uplinkHeader.packetIndex = uplinkPacketIndex++; uplinkHeader.packetType = UplinkPacketType_ACK; + uint32_t crcCheck = esp_rom_crc32_le(0, (uint8_t *)&ack, sizeof(ack)); + + uplinkHeader.CRCCheck = crcCheck; + uint8_t txBuf[256] = {0}; int offset = 0; @@ -146,6 +150,8 @@ void printTelemetryPacket(const TelemetryPacket *packet) ESP_LOGI(TAG, " Servos:"); ESP_LOGI(TAG, " Servo A: Current = %d, Target = %d", packet->currentServoA, packet->targetServoA); ESP_LOGI(TAG, " Servo B: Current = %d, Target = %d", packet->currentServoB, packet->targetServoB); + ESP_LOGI(TAG, " TelemetryIndex: %d", packet->telemetryIndex); + } void handle_downlink_packet(uint8_t *buf, uint8_t rxLen) @@ -171,6 +177,13 @@ void handle_downlink_packet(uint8_t *buf, uint8_t rxLen) uint8_t *payload = buf + sizeof(DownBoundPacket); uint32_t payloadSize = rxLen - sizeof(DownBoundPacket); + uint32_t crcCheck = esp_rom_crc32_le(0, payload, payloadSize); + + if (crcCheck != down.CRCCheck) { + ESP_LOGE(TAG, "Received BAD CRC for packet %d, crc is %ld, should be %ld", down.packetIndex, crcCheck, down.CRCCheck); + return; + } + switch (down.packetType) { case DownlinkPacketType_Telemetry: @@ -227,7 +240,7 @@ void lora_comms_task(void *pvParameters) } } - uint8_t spreadingFactor = 6; + uint8_t spreadingFactor = 7; uint8_t bandwidth = SX126X_LORA_BW_250_0; uint8_t codingRate = SX126X_LORA_CR_4_8; uint16_t preambleLength = 8;