pocsag.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include "pocsag.h"
  2. #include <inttypes.h>
  3. #include <lib/flipper_format/flipper_format_i.h>
  4. #include <furi/core/string.h>
  5. #define TAG "POCSAG"
  6. static const SubGhzBlockConst pocsag_const = {
  7. .te_short = 833,
  8. .te_delta = 100,
  9. };
  10. // Minimal amount of sync bits (interleaving zeros and ones)
  11. #define POCSAG_MIN_SYNC_BITS 32
  12. #define POCSAG_CW_BITS 32
  13. #define POCSAG_CW_MASK 0xFFFFFFFF
  14. #define POCSAG_FRAME_SYNC_CODE 0x7CD215D8
  15. #define POCSAG_IDLE_CODE_WORD 0x7A89C197
  16. #define POCSAG_FUNC_NUM 0
  17. #define POCSAG_FUNC_ALERT1 1
  18. #define POCSAG_FUNC_ALERT2 2
  19. #define POCSAG_FUNC_ALPHANUM 3
  20. static const char* func_msg[] = {"\e#Num:\e# ", "\e#Alert\e#", "\e#Alert:\e# ", "\e#Msg:\e# "};
  21. static const char* bcd_chars = "*U -)(";
  22. struct SubGhzProtocolDecoderPocsag {
  23. SubGhzProtocolDecoderBase base;
  24. SubGhzBlockDecoder decoder;
  25. PCSGBlockGeneric generic;
  26. uint8_t codeword_idx;
  27. uint32_t ric;
  28. uint8_t func;
  29. // partially decoded character
  30. uint8_t char_bits;
  31. uint8_t char_data;
  32. // message being decoded
  33. FuriString* msg;
  34. // Done messages, ready to be serialized/deserialized
  35. FuriString* done_msg;
  36. };
  37. typedef struct SubGhzProtocolDecoderPocsag SubGhzProtocolDecoderPocsag;
  38. typedef enum {
  39. PocsagDecoderStepReset = 0,
  40. PocsagDecoderStepFoundSync,
  41. PocsagDecoderStepFoundPreamble,
  42. PocsagDecoderStepMessage,
  43. } PocsagDecoderStep;
  44. void* subghz_protocol_decoder_pocsag_alloc(SubGhzEnvironment* environment) {
  45. UNUSED(environment);
  46. SubGhzProtocolDecoderPocsag* instance = malloc(sizeof(SubGhzProtocolDecoderPocsag));
  47. instance->base.protocol = &subghz_protocol_pocsag;
  48. instance->generic.protocol_name = instance->base.protocol->name;
  49. instance->msg = furi_string_alloc();
  50. instance->done_msg = furi_string_alloc();
  51. if(instance->generic.result_msg == NULL) {
  52. instance->generic.result_msg = furi_string_alloc();
  53. }
  54. if(instance->generic.result_ric == NULL) {
  55. instance->generic.result_ric = furi_string_alloc();
  56. }
  57. return instance;
  58. }
  59. void subghz_protocol_decoder_pocsag_free(void* context) {
  60. furi_assert(context);
  61. SubGhzProtocolDecoderPocsag* instance = context;
  62. furi_string_free(instance->msg);
  63. furi_string_free(instance->done_msg);
  64. free(instance);
  65. }
  66. void subghz_protocol_decoder_pocsag_reset(void* context) {
  67. furi_assert(context);
  68. SubGhzProtocolDecoderPocsag* instance = context;
  69. instance->decoder.parser_step = PocsagDecoderStepReset;
  70. instance->decoder.decode_data = 0UL;
  71. instance->decoder.decode_count_bit = 0;
  72. instance->codeword_idx = 0;
  73. instance->char_bits = 0;
  74. instance->char_data = 0;
  75. furi_string_reset(instance->msg);
  76. furi_string_reset(instance->done_msg);
  77. furi_string_reset(instance->generic.result_msg);
  78. furi_string_reset(instance->generic.result_ric);
  79. }
  80. static void pocsag_decode_address_word(SubGhzProtocolDecoderPocsag* instance, uint32_t data) {
  81. instance->ric = (data >> 13);
  82. instance->ric = (instance->ric << 3) | (instance->codeword_idx >> 1);
  83. instance->func = (data >> 11) & 0b11;
  84. }
  85. static bool decode_message_alphanumeric(SubGhzProtocolDecoderPocsag* instance, uint32_t data) {
  86. for(uint8_t i = 0; i < 20; i++) {
  87. instance->char_data >>= 1;
  88. if(data & (1 << 30)) {
  89. instance->char_data |= 1 << 6;
  90. }
  91. instance->char_bits++;
  92. if(instance->char_bits == 7) {
  93. if(instance->char_data == 0) return false;
  94. furi_string_push_back(instance->msg, instance->char_data);
  95. instance->char_data = 0;
  96. instance->char_bits = 0;
  97. }
  98. data <<= 1;
  99. }
  100. return true;
  101. }
  102. static void decode_message_numeric(SubGhzProtocolDecoderPocsag* instance, uint32_t data) {
  103. // 5 groups with 4 bits each
  104. uint8_t val;
  105. for(uint8_t i = 0; i < 5; i++) {
  106. val = (data >> (27 - i * 4)) & 0b1111;
  107. // reverse the order of 4 bits
  108. val = (val & 0x5) << 1 | (val & 0xA) >> 1;
  109. val = (val & 0x3) << 2 | (val & 0xC) >> 2;
  110. if(val <= 9)
  111. val += '0';
  112. else
  113. val = bcd_chars[val - 10];
  114. furi_string_push_back(instance->msg, val);
  115. }
  116. }
  117. // decode message word, maintaining instance state for partial decoding. Return true if more data
  118. // might follow or false if end of message reached.
  119. static bool pocsag_decode_message_word(SubGhzProtocolDecoderPocsag* instance, uint32_t data) {
  120. switch(instance->func) {
  121. case POCSAG_FUNC_ALERT2:
  122. case POCSAG_FUNC_ALPHANUM:
  123. return decode_message_alphanumeric(instance, data);
  124. case POCSAG_FUNC_NUM:
  125. decode_message_numeric(instance, data);
  126. return true;
  127. }
  128. return false;
  129. }
  130. // Function called when current message got decoded, but other messages might follow
  131. static void pocsag_message_done(SubGhzProtocolDecoderPocsag* instance) {
  132. // append the message to the long-term storage string
  133. furi_string_printf(instance->generic.result_ric, "\e#RIC: %" PRIu32 "\e# | ", instance->ric);
  134. furi_string_cat_str(instance->generic.result_ric, func_msg[instance->func]);
  135. if(instance->func != POCSAG_FUNC_ALERT1) {
  136. furi_string_cat(instance->done_msg, instance->msg);
  137. }
  138. furi_string_cat_str(instance->done_msg, " ");
  139. furi_string_cat(instance->generic.result_msg, instance->done_msg);
  140. // reset the state
  141. instance->char_bits = 0;
  142. instance->char_data = 0;
  143. furi_string_reset(instance->msg);
  144. }
  145. void subghz_protocol_decoder_pocsag_feed(void* context, bool level, uint32_t duration) {
  146. furi_assert(context);
  147. SubGhzProtocolDecoderPocsag* instance = context;
  148. // reset state - waiting for 32 bits of interleaving 1s and 0s
  149. if(instance->decoder.parser_step == PocsagDecoderStepReset) {
  150. if(DURATION_DIFF(duration, pocsag_const.te_short) < pocsag_const.te_delta) {
  151. // POCSAG signals are inverted
  152. subghz_protocol_blocks_add_bit(&instance->decoder, !level);
  153. if(instance->decoder.decode_count_bit == POCSAG_MIN_SYNC_BITS) {
  154. instance->decoder.parser_step = PocsagDecoderStepFoundSync;
  155. }
  156. } else if(instance->decoder.decode_count_bit > 0) {
  157. subghz_protocol_decoder_pocsag_reset(context);
  158. }
  159. return;
  160. }
  161. int bits_count = duration / pocsag_const.te_short;
  162. uint32_t extra = duration - pocsag_const.te_short * bits_count;
  163. if(DURATION_DIFF(extra, pocsag_const.te_short) < pocsag_const.te_delta)
  164. bits_count++;
  165. else if(extra > pocsag_const.te_delta) {
  166. // in non-reset state we faced the error signal - we reached the end of the packet, flush data
  167. if(furi_string_size(instance->done_msg) > 0) {
  168. if(instance->base.callback)
  169. instance->base.callback(&instance->base, instance->base.context);
  170. }
  171. subghz_protocol_decoder_pocsag_reset(context);
  172. return;
  173. }
  174. uint32_t codeword;
  175. // handle state machine for every incoming bit
  176. while(bits_count-- > 0) {
  177. subghz_protocol_blocks_add_bit(&instance->decoder, !level);
  178. switch(instance->decoder.parser_step) {
  179. case PocsagDecoderStepFoundSync:
  180. if((instance->decoder.decode_data & POCSAG_CW_MASK) == POCSAG_FRAME_SYNC_CODE) {
  181. instance->decoder.parser_step = PocsagDecoderStepFoundPreamble;
  182. instance->decoder.decode_count_bit = 0;
  183. instance->decoder.decode_data = 0UL;
  184. }
  185. break;
  186. case PocsagDecoderStepFoundPreamble:
  187. // handle codewords
  188. if(instance->decoder.decode_count_bit == POCSAG_CW_BITS) {
  189. codeword = (uint32_t)(instance->decoder.decode_data & POCSAG_CW_MASK);
  190. switch(codeword) {
  191. case POCSAG_IDLE_CODE_WORD:
  192. instance->codeword_idx++;
  193. break;
  194. case POCSAG_FRAME_SYNC_CODE:
  195. instance->codeword_idx = 0;
  196. break;
  197. default:
  198. // Here we expect only address messages
  199. if(codeword >> 31 == 0) {
  200. pocsag_decode_address_word(instance, codeword);
  201. instance->decoder.parser_step = PocsagDecoderStepMessage;
  202. }
  203. instance->codeword_idx++;
  204. }
  205. instance->decoder.decode_count_bit = 0;
  206. instance->decoder.decode_data = 0UL;
  207. }
  208. break;
  209. case PocsagDecoderStepMessage:
  210. if(instance->decoder.decode_count_bit == POCSAG_CW_BITS) {
  211. codeword = (uint32_t)(instance->decoder.decode_data & POCSAG_CW_MASK);
  212. switch(codeword) {
  213. case POCSAG_IDLE_CODE_WORD:
  214. // Idle during the message stops the message
  215. instance->codeword_idx++;
  216. instance->decoder.parser_step = PocsagDecoderStepFoundPreamble;
  217. pocsag_message_done(instance);
  218. break;
  219. case POCSAG_FRAME_SYNC_CODE:
  220. instance->codeword_idx = 0;
  221. break;
  222. default:
  223. // In this state, both address and message words can arrive
  224. if(codeword >> 31 == 0) {
  225. pocsag_message_done(instance);
  226. pocsag_decode_address_word(instance, codeword);
  227. } else {
  228. if(!pocsag_decode_message_word(instance, codeword)) {
  229. instance->decoder.parser_step = PocsagDecoderStepFoundPreamble;
  230. pocsag_message_done(instance);
  231. }
  232. }
  233. instance->codeword_idx++;
  234. }
  235. instance->decoder.decode_count_bit = 0;
  236. instance->decoder.decode_data = 0UL;
  237. }
  238. break;
  239. }
  240. }
  241. }
  242. uint8_t subghz_protocol_decoder_pocsag_get_hash_data(void* context) {
  243. furi_assert(context);
  244. SubGhzProtocolDecoderPocsag* instance = context;
  245. uint8_t hash = 0;
  246. for(size_t i = 0; i < furi_string_size(instance->done_msg); i++)
  247. hash ^= furi_string_get_char(instance->done_msg, i);
  248. return hash;
  249. }
  250. SubGhzProtocolStatus subghz_protocol_decoder_pocsag_serialize(
  251. void* context,
  252. FlipperFormat* flipper_format,
  253. SubGhzRadioPreset* preset) {
  254. furi_assert(context);
  255. SubGhzProtocolDecoderPocsag* instance = context;
  256. uint32_t msg_len;
  257. if(SubGhzProtocolStatusOk !=
  258. pcsg_block_generic_serialize(&instance->generic, flipper_format, preset))
  259. return SubGhzProtocolStatusError;
  260. msg_len = furi_string_size(instance->done_msg);
  261. if(!flipper_format_write_uint32(flipper_format, "MsgLen", &msg_len, 1)) {
  262. FURI_LOG_E(TAG, "Error adding MsgLen");
  263. return SubGhzProtocolStatusError;
  264. }
  265. uint8_t* s = (uint8_t*)furi_string_get_cstr(instance->done_msg);
  266. if(!flipper_format_write_hex(flipper_format, "Msg", s, msg_len)) {
  267. FURI_LOG_E(TAG, "Error adding Msg");
  268. return SubGhzProtocolStatusError;
  269. }
  270. return SubGhzProtocolStatusOk;
  271. }
  272. SubGhzProtocolStatus
  273. subghz_protocol_decoder_pocsag_deserialize(void* context, FlipperFormat* flipper_format) {
  274. furi_assert(context);
  275. SubGhzProtocolDecoderPocsag* instance = context;
  276. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  277. uint32_t msg_len;
  278. uint8_t* buf;
  279. do {
  280. if(SubGhzProtocolStatusOk !=
  281. pcsg_block_generic_deserialize(&instance->generic, flipper_format)) {
  282. break;
  283. }
  284. if(!flipper_format_read_uint32(flipper_format, "MsgLen", &msg_len, 1)) {
  285. FURI_LOG_E(TAG, "Missing MsgLen");
  286. break;
  287. }
  288. buf = malloc(msg_len);
  289. if(!flipper_format_read_hex(flipper_format, "Msg", buf, msg_len)) {
  290. FURI_LOG_E(TAG, "Missing Msg");
  291. free(buf);
  292. break;
  293. }
  294. furi_string_set_strn(instance->done_msg, (const char*)buf, msg_len);
  295. free(buf);
  296. ret = SubGhzProtocolStatusOk;
  297. } while(false);
  298. return ret;
  299. }
  300. void subhz_protocol_decoder_pocsag_get_string(void* context, FuriString* output) {
  301. furi_assert(context);
  302. SubGhzProtocolDecoderPocsag* instance = context;
  303. furi_string_cat_printf(output, "%s\r\n", instance->generic.protocol_name);
  304. furi_string_cat(output, instance->done_msg);
  305. }
  306. const SubGhzProtocolDecoder subghz_protocol_pocsag_decoder = {
  307. .alloc = subghz_protocol_decoder_pocsag_alloc,
  308. .free = subghz_protocol_decoder_pocsag_free,
  309. .reset = subghz_protocol_decoder_pocsag_reset,
  310. .feed = subghz_protocol_decoder_pocsag_feed,
  311. .get_hash_data = subghz_protocol_decoder_pocsag_get_hash_data,
  312. .serialize = subghz_protocol_decoder_pocsag_serialize,
  313. .deserialize = subghz_protocol_decoder_pocsag_deserialize,
  314. .get_string = subhz_protocol_decoder_pocsag_get_string,
  315. };
  316. const SubGhzProtocol subghz_protocol_pocsag = {
  317. .name = SUBGHZ_PROTOCOL_POCSAG_NAME,
  318. .type = SubGhzProtocolTypeStatic,
  319. .flag = SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Save |
  320. SubGhzProtocolFlag_Load,
  321. .decoder = &subghz_protocol_pocsag_decoder,
  322. };