Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

radio.c 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <string.h>
  2. #include "radio.h"
  3. #include "encryption.h"
  4. #include "crc.h"
  5. bool Send_Message(PACKET * packet, uint64_t * salt);
  6. bool Send_Get_Salt_Message(PACKET * packet, uint64_t * salt)
  7. {
  8. memset((uint8_t*)packet, 0, PACKET_LENGTH); //Reinitialize the buffer with zeros
  9. packet->payload.reportData.packetIdentifier.elementCount = 0;
  10. packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_GET_SALT;
  11. return Send_Message(packet, salt);
  12. }
  13. bool Send_Report_Message(PACKET * packet, uint64_t * salt, struct bme280_data * sensorData)
  14. {
  15. packet->payload.reportData.packetIdentifier.elementCount = 3;
  16. packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_REPORT;
  17. /* Fill in the payload */
  18. packet->payload.reportData.valueTypeTemperature = VALUE_TYPE_TEMPERATURE;
  19. packet->payload.reportData.temperature = sensorData->temperature/10;
  20. packet->payload.reportData.valueTypePressure = VALUE_TYPE_PRESSURE;
  21. packet->payload.reportData.pressure = sensorData->pressure;
  22. packet->payload.reportData.valueTypeHumidity = VALUE_TYPE_HUMIDITY;
  23. packet->payload.reportData.humidity = sensorData->humidity * 100/1024;
  24. return Send_Message(packet, salt);
  25. }
  26. bool Send_Message(PACKET * packet, uint64_t * salt)
  27. {
  28. bool success;
  29. uint16_t crc;
  30. (*salt) &= ~(1ull<<63); /* Set the most significant bit to 0 to indicate a packet from the device to the base station */
  31. packet->salt = *salt;
  32. /* Calculate the CRC */
  33. crc = Calculate_Crc(packet->payload.buffer, PACKET_PAYLOAD_BUFFER_LENGTH);
  34. packet->crc = crc;
  35. Encrypt((uint32_t*) packet->payload.buffer,
  36. PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
  37. *salt);
  38. success = NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
  39. /* Increment salt */
  40. (*salt) += (1 << 8);
  41. return success;
  42. }
  43. bool Read_Salt_Message(PACKET * packet, uint64_t * salt)
  44. {
  45. uint16_t crcRemainder = 0xFFFF;
  46. uint64_t baseStationSalt = 0x0;
  47. baseStationSalt = packet->salt;
  48. Decrypt((uint32_t*)packet->payload.buffer,
  49. PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc),
  50. baseStationSalt);
  51. crcRemainder = Calculate_Crc(packet->payload.buffer,
  52. PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc));
  53. if (crcRemainder != 0)
  54. {
  55. return false;
  56. }
  57. if ((packet->payload.saltData.packetIdentifier.packetType != PACKET_TYPE_SALT) ||
  58. (packet->payload.saltData.packetIdentifier.elementCount != 0))
  59. {
  60. return false;
  61. }
  62. memcpy((uint8_t*)salt, packet->payload.saltData.salt, 7);
  63. *salt = (*salt) << 8;
  64. return true;
  65. }