pocsag.c 16 KB

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