subghz_test_packet.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "subghz_test_packet.h"
  2. #include "../subghz_i.h"
  3. #include "../helpers/subghz_testing.h"
  4. #include <math.h>
  5. #include <furi.h>
  6. #include <furi_hal.h>
  7. #include <input/input.h>
  8. #include <toolbox/level_duration.h>
  9. #include <lib/subghz/protocols/princeton_for_testing.h>
  10. #define SUBGHZ_TEST_PACKET_COUNT 500
  11. struct SubGhzTestPacket {
  12. View* view;
  13. osTimerId_t timer;
  14. SubGhzDecoderPrinceton* decoder;
  15. SubGhzEncoderPrinceton* encoder;
  16. volatile size_t packet_rx;
  17. SubGhzTestPacketCallback callback;
  18. void* context;
  19. };
  20. typedef enum {
  21. SubGhzTestPacketModelStatusRx,
  22. SubGhzTestPacketModelStatusOnlyRx,
  23. SubGhzTestPacketModelStatusTx,
  24. } SubGhzTestPacketModelStatus;
  25. typedef struct {
  26. uint8_t frequency;
  27. uint32_t real_frequency;
  28. FuriHalSubGhzPath path;
  29. float rssi;
  30. size_t packets;
  31. SubGhzTestPacketModelStatus status;
  32. } SubGhzTestPacketModel;
  33. volatile bool subghz_test_packet_overrun = false;
  34. void subghz_test_packet_set_callback(
  35. SubGhzTestPacket* subghz_test_packet,
  36. SubGhzTestPacketCallback callback,
  37. void* context) {
  38. furi_assert(subghz_test_packet);
  39. furi_assert(callback);
  40. subghz_test_packet->callback = callback;
  41. subghz_test_packet->context = context;
  42. }
  43. static void subghz_test_packet_rx_callback(bool level, uint32_t duration, void* context) {
  44. furi_assert(context);
  45. SubGhzTestPacket* instance = context;
  46. subghz_decoder_princeton_for_testing_parse(instance->decoder, level, duration);
  47. }
  48. //todo
  49. static void subghz_test_packet_rx_pt_callback(SubGhzDecoderPrinceton* parser, void* context) {
  50. UNUSED(parser);
  51. furi_assert(context);
  52. SubGhzTestPacket* instance = context;
  53. instance->packet_rx++;
  54. }
  55. static void subghz_test_packet_rssi_timer_callback(void* context) {
  56. furi_assert(context);
  57. SubGhzTestPacket* instance = context;
  58. with_view_model(
  59. instance->view, (SubGhzTestPacketModel * model) {
  60. if(model->status == SubGhzTestPacketModelStatusRx) {
  61. model->rssi = furi_hal_subghz_get_rssi();
  62. model->packets = instance->packet_rx;
  63. } else if(model->status == SubGhzTestPacketModelStatusTx) {
  64. model->packets =
  65. SUBGHZ_TEST_PACKET_COUNT -
  66. subghz_encoder_princeton_for_testing_get_repeat_left(instance->encoder);
  67. }
  68. return true;
  69. });
  70. }
  71. static void subghz_test_packet_draw(Canvas* canvas, SubGhzTestPacketModel* model) {
  72. char buffer[64];
  73. canvas_set_color(canvas, ColorBlack);
  74. canvas_set_font(canvas, FontPrimary);
  75. canvas_draw_str(canvas, 0, 8, "CC1101 Packet Test");
  76. canvas_set_font(canvas, FontSecondary);
  77. // Frequency
  78. snprintf(
  79. buffer,
  80. sizeof(buffer),
  81. "Freq: %03ld.%03ld.%03ld Hz",
  82. model->real_frequency / 1000000 % 1000,
  83. model->real_frequency / 1000 % 1000,
  84. model->real_frequency % 1000);
  85. canvas_draw_str(canvas, 0, 20, buffer);
  86. // Path
  87. char* path_name = "Unknown";
  88. if(model->path == FuriHalSubGhzPathIsolate) {
  89. path_name = "isolate";
  90. } else if(model->path == FuriHalSubGhzPath433) {
  91. path_name = "433MHz";
  92. } else if(model->path == FuriHalSubGhzPath315) {
  93. path_name = "315MHz";
  94. } else if(model->path == FuriHalSubGhzPath868) {
  95. path_name = "868MHz";
  96. }
  97. snprintf(buffer, sizeof(buffer), "Path: %d - %s", model->path, path_name);
  98. canvas_draw_str(canvas, 0, 31, buffer);
  99. snprintf(buffer, sizeof(buffer), "Packets: %d", model->packets);
  100. canvas_draw_str(canvas, 0, 42, buffer);
  101. if(model->status == SubGhzTestPacketModelStatusRx) {
  102. snprintf(
  103. buffer,
  104. sizeof(buffer),
  105. "RSSI: %ld.%ld dBm",
  106. (int32_t)(model->rssi),
  107. (int32_t)fabs(model->rssi * 10) % 10);
  108. canvas_draw_str(canvas, 0, 53, buffer);
  109. } else {
  110. canvas_draw_str(canvas, 0, 53, "TX");
  111. }
  112. }
  113. static bool subghz_test_packet_input(InputEvent* event, void* context) {
  114. furi_assert(context);
  115. SubGhzTestPacket* instance = context;
  116. if(event->key == InputKeyBack || event->type != InputTypeShort) {
  117. return false;
  118. }
  119. with_view_model(
  120. instance->view, (SubGhzTestPacketModel * model) {
  121. if(model->status == SubGhzTestPacketModelStatusRx) {
  122. furi_hal_subghz_stop_async_rx();
  123. } else if(model->status == SubGhzTestPacketModelStatusTx) {
  124. subghz_encoder_princeton_for_testing_stop(instance->encoder, furi_hal_get_tick());
  125. furi_hal_subghz_stop_async_tx();
  126. }
  127. if(event->key == InputKeyLeft) {
  128. if(model->frequency > 0) model->frequency--;
  129. } else if(event->key == InputKeyRight) {
  130. if(model->frequency < subghz_frequencies_count_testing - 1) model->frequency++;
  131. } else if(event->key == InputKeyDown) {
  132. if(model->path > 0) model->path--;
  133. } else if(event->key == InputKeyUp) {
  134. if(model->path < FuriHalSubGhzPath868) model->path++;
  135. } else if(event->key == InputKeyOk) {
  136. if(model->status == SubGhzTestPacketModelStatusRx) {
  137. model->status = SubGhzTestPacketModelStatusTx;
  138. } else {
  139. model->status = SubGhzTestPacketModelStatusRx;
  140. }
  141. }
  142. model->real_frequency =
  143. furi_hal_subghz_set_frequency(subghz_frequencies_testing[model->frequency]);
  144. furi_hal_subghz_set_path(model->path);
  145. if(model->status == SubGhzTestPacketModelStatusRx) {
  146. furi_hal_subghz_start_async_rx(subghz_test_packet_rx_callback, instance);
  147. } else {
  148. subghz_encoder_princeton_for_testing_set(
  149. instance->encoder,
  150. 0x00AABBCC,
  151. SUBGHZ_TEST_PACKET_COUNT,
  152. subghz_frequencies_testing[model->frequency]);
  153. if(!furi_hal_subghz_start_async_tx(
  154. subghz_encoder_princeton_for_testing_yield, instance->encoder)) {
  155. model->status = SubGhzTestPacketModelStatusOnlyRx;
  156. instance->callback(SubGhzTestPacketEventOnlyRx, instance->context);
  157. }
  158. }
  159. return true;
  160. });
  161. return true;
  162. }
  163. void subghz_test_packet_enter(void* context) {
  164. furi_assert(context);
  165. SubGhzTestPacket* instance = context;
  166. furi_hal_subghz_reset();
  167. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  168. with_view_model(
  169. instance->view, (SubGhzTestPacketModel * model) {
  170. model->frequency = subghz_frequencies_433_92_testing;
  171. model->real_frequency =
  172. furi_hal_subghz_set_frequency(subghz_frequencies_testing[model->frequency]);
  173. model->path = FuriHalSubGhzPathIsolate; // isolate
  174. model->rssi = 0.0f;
  175. model->status = SubGhzTestPacketModelStatusRx;
  176. return true;
  177. });
  178. furi_hal_subghz_start_async_rx(subghz_test_packet_rx_callback, instance);
  179. osTimerStart(instance->timer, osKernelGetTickFreq() / 4);
  180. }
  181. void subghz_test_packet_exit(void* context) {
  182. furi_assert(context);
  183. SubGhzTestPacket* instance = context;
  184. osTimerStop(instance->timer);
  185. // Reinitialize IC to default state
  186. with_view_model(
  187. instance->view, (SubGhzTestPacketModel * model) {
  188. if(model->status == SubGhzTestPacketModelStatusRx) {
  189. furi_hal_subghz_stop_async_rx();
  190. } else if(model->status == SubGhzTestPacketModelStatusTx) {
  191. subghz_encoder_princeton_for_testing_stop(instance->encoder, furi_hal_get_tick());
  192. furi_hal_subghz_stop_async_tx();
  193. }
  194. return true;
  195. });
  196. furi_hal_subghz_sleep();
  197. }
  198. SubGhzTestPacket* subghz_test_packet_alloc() {
  199. SubGhzTestPacket* instance = malloc(sizeof(SubGhzTestPacket));
  200. // View allocation and configuration
  201. instance->view = view_alloc();
  202. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubGhzTestPacketModel));
  203. view_set_context(instance->view, instance);
  204. view_set_draw_callback(instance->view, (ViewDrawCallback)subghz_test_packet_draw);
  205. view_set_input_callback(instance->view, subghz_test_packet_input);
  206. view_set_enter_callback(instance->view, subghz_test_packet_enter);
  207. view_set_exit_callback(instance->view, subghz_test_packet_exit);
  208. instance->timer =
  209. osTimerNew(subghz_test_packet_rssi_timer_callback, osTimerPeriodic, instance, NULL);
  210. instance->decoder = subghz_decoder_princeton_for_testing_alloc();
  211. subghz_decoder_princeton_for_testing_set_callback(
  212. instance->decoder, subghz_test_packet_rx_pt_callback, instance);
  213. instance->encoder = subghz_encoder_princeton_for_testing_alloc();
  214. return instance;
  215. }
  216. void subghz_test_packet_free(SubGhzTestPacket* instance) {
  217. furi_assert(instance);
  218. subghz_decoder_princeton_for_testing_free(instance->decoder);
  219. subghz_encoder_princeton_for_testing_free(instance->encoder);
  220. osTimerDelete(instance->timer);
  221. view_free(instance->view);
  222. free(instance);
  223. }
  224. View* subghz_test_packet_get_view(SubGhzTestPacket* instance) {
  225. furi_assert(instance);
  226. return instance->view;
  227. }