subghz_test_packet.c 9.1 KB

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