説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

radio.c 2.8KB

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