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.

122 lines
3.8KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. #include "stm_audio_bootloader/qpsk/packet_decoder.h"
  25. #include "stmlib/utils/crc32.h"
  26. namespace stm_audio_bootloader {
  27. void PacketDecoder::Init(uint16_t max_sync_duration) {
  28. Reset();
  29. packet_count_ = 0;
  30. max_sync_duration_ = max_sync_duration;
  31. }
  32. void PacketDecoder::ParseSyncHeader(uint8_t symbol) {
  33. if (!((1 << symbol) & expected_symbols_)) {
  34. state_ = PACKET_DECODER_STATE_ERROR_SYNC;
  35. return;
  36. }
  37. switch (symbol) {
  38. case 4:
  39. ++sync_blank_size_;
  40. if (sync_blank_size_ >= max_sync_duration_ && packet_count_) {
  41. state_ = PACKET_DECODER_STATE_END_OF_TRANSMISSION;
  42. return;
  43. }
  44. expected_symbols_ = (1 << 1) | (1 << 2) | (1 << 4);
  45. preamble_remaining_size_ = kPreambleSize;
  46. break;
  47. case 3:
  48. expected_symbols_ = (1 << 0);
  49. --preamble_remaining_size_;
  50. break;
  51. case 2:
  52. expected_symbols_ = (1 << 1);
  53. break;
  54. case 1:
  55. expected_symbols_ = (1 << 2) | (1 << 3);
  56. break;
  57. case 0:
  58. expected_symbols_ = (1 << 3);
  59. --preamble_remaining_size_;
  60. break;
  61. }
  62. if (preamble_remaining_size_ == 0) {
  63. state_ = PACKET_DECODER_STATE_DECODING_PACKET;
  64. packet_size_ = 0;
  65. packet_[packet_size_] = 0;
  66. symbol_count_ = 0;
  67. }
  68. }
  69. PacketDecoderState PacketDecoder::ProcessSymbol(uint8_t symbol) {
  70. switch (state_) {
  71. case PACKET_DECODER_STATE_SYNCING:
  72. ParseSyncHeader(symbol);
  73. break;
  74. case PACKET_DECODER_STATE_DECODING_PACKET:
  75. packet_[packet_size_] |= symbol;
  76. ++symbol_count_;
  77. if (symbol_count_ == kSymbolMask + 1) {
  78. symbol_count_ = 0;
  79. ++packet_size_;
  80. if (packet_size_ == kPacketSize + 4) {
  81. uint32_t crc = crc32(0, packet_, kPacketSize);
  82. uint32_t expected_crc = \
  83. (static_cast<uint32_t>(packet_[kPacketSize + 0]) << 24) | \
  84. (static_cast<uint32_t>(packet_[kPacketSize + 1]) << 16) | \
  85. (static_cast<uint32_t>(packet_[kPacketSize + 2]) << 8) | \
  86. (static_cast<uint32_t>(packet_[kPacketSize + 3]) << 0);
  87. state_ = crc == expected_crc \
  88. ? PACKET_DECODER_STATE_OK
  89. : PACKET_DECODER_STATE_ERROR_CRC;
  90. ++packet_count_;
  91. } else {
  92. packet_[packet_size_] = 0;
  93. }
  94. } else {
  95. packet_[packet_size_] <<= kSymbolShift;
  96. }
  97. break;
  98. case PACKET_DECODER_STATE_OK:
  99. case PACKET_DECODER_STATE_ERROR_SYNC:
  100. case PACKET_DECODER_STATE_ERROR_CRC:
  101. case PACKET_DECODER_STATE_END_OF_TRANSMISSION:
  102. break;
  103. }
  104. return state_;
  105. }
  106. } // namespace stm_audio_bootloader