pocsag.c 16 KB

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