pocsag.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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(instance->generic.result_ric, "\e#RIC: %" PRIu32 "\e# | ", instance->ric);
  147. furi_string_cat_str(instance->generic.result_ric, func_msg[instance->func]);
  148. if(instance->func != POCSAG_FUNC_ALERT1) {
  149. furi_string_cat(instance->done_msg, instance->msg);
  150. }
  151. furi_string_cat_str(instance->done_msg, " ");
  152. furi_string_cat(instance->generic.result_msg, instance->done_msg);
  153. // reset the state
  154. instance->char_bits = 0;
  155. instance->char_data = 0;
  156. furi_string_reset(instance->msg);
  157. }
  158. void subghz_protocol_decoder_pocsag_feed(void* context, bool level, uint32_t duration) {
  159. furi_assert(context);
  160. SubGhzProtocolDecoderPocsag* instance = context;
  161. // reset state - waiting for 32 bits of interleaving 1s and 0s
  162. if(instance->decoder.parser_step == PocsagDecoderStepReset) {
  163. if(DURATION_DIFF(duration, pocsag_const.te_short) < pocsag_const.te_delta) {
  164. if(instance->pocsag_timing != &pocsag_const) {
  165. //timing changed, so reset before, and override
  166. subghz_protocol_decoder_pocsag_reset(context);
  167. instance->pocsag_timing = (SubGhzBlockConst*)&pocsag_const;
  168. instance->version = 1200;
  169. }
  170. // POCSAG signals are inverted
  171. subghz_protocol_blocks_add_bit(&instance->decoder, !level);
  172. if(instance->decoder.decode_count_bit == POCSAG_MIN_SYNC_BITS) {
  173. instance->decoder.parser_step = PocsagDecoderStepFoundSync;
  174. }
  175. } else if(DURATION_DIFF(duration, pocsag512_const.te_short) < pocsag512_const.te_delta) {
  176. if(instance->pocsag_timing != &pocsag512_const) {
  177. //timing changed, so reset before, and override
  178. subghz_protocol_decoder_pocsag_reset(context);
  179. instance->pocsag_timing = (SubGhzBlockConst*)&pocsag512_const;
  180. instance->version = 512;
  181. }
  182. // POCSAG signals are inverted
  183. subghz_protocol_blocks_add_bit(&instance->decoder, !level);
  184. if(instance->decoder.decode_count_bit == POCSAG_MIN_SYNC_BITS) {
  185. instance->decoder.parser_step = PocsagDecoderStepFoundSync;
  186. }
  187. } else if(DURATION_DIFF(duration, pocsag2400_const.te_short) < pocsag2400_const.te_delta) {
  188. if(instance->pocsag_timing != &pocsag2400_const) {
  189. //timing changed, so reset before, and override
  190. subghz_protocol_decoder_pocsag_reset(context);
  191. instance->pocsag_timing = (SubGhzBlockConst*)&pocsag2400_const;
  192. instance->version = 2400;
  193. }
  194. // POCSAG signals are inverted
  195. subghz_protocol_blocks_add_bit(&instance->decoder, !level);
  196. if(instance->decoder.decode_count_bit == POCSAG_MIN_SYNC_BITS) {
  197. instance->decoder.parser_step = PocsagDecoderStepFoundSync;
  198. }
  199. } else if(instance->decoder.decode_count_bit > 0) {
  200. subghz_protocol_decoder_pocsag_reset(context);
  201. }
  202. return;
  203. }
  204. int bits_count = duration / instance->pocsag_timing->te_short;
  205. uint32_t extra = duration - instance->pocsag_timing->te_short * bits_count;
  206. if(DURATION_DIFF(extra, instance->pocsag_timing->te_short) < instance->pocsag_timing->te_delta)
  207. bits_count++;
  208. else if(extra > instance->pocsag_timing->te_delta) {
  209. // in non-reset state we faced the error signal - we reached the end of the packet, flush data
  210. if(furi_string_size(instance->done_msg) > 0) {
  211. if(instance->base.callback)
  212. instance->base.callback(&instance->base, instance->base.context);
  213. }
  214. subghz_protocol_decoder_pocsag_reset(context);
  215. return;
  216. }
  217. uint32_t codeword;
  218. // handle state machine for every incoming bit
  219. while(bits_count-- > 0) {
  220. subghz_protocol_blocks_add_bit(&instance->decoder, !level);
  221. switch(instance->decoder.parser_step) {
  222. case PocsagDecoderStepFoundSync:
  223. if((instance->decoder.decode_data & POCSAG_CW_MASK) == POCSAG_FRAME_SYNC_CODE) {
  224. instance->decoder.parser_step = PocsagDecoderStepFoundPreamble;
  225. instance->decoder.decode_count_bit = 0;
  226. instance->decoder.decode_data = 0UL;
  227. }
  228. break;
  229. case PocsagDecoderStepFoundPreamble:
  230. // handle codewords
  231. if(instance->decoder.decode_count_bit == POCSAG_CW_BITS) {
  232. codeword = (uint32_t)(instance->decoder.decode_data & POCSAG_CW_MASK);
  233. switch(codeword) {
  234. case POCSAG_IDLE_CODE_WORD:
  235. instance->codeword_idx++;
  236. break;
  237. case POCSAG_FRAME_SYNC_CODE:
  238. instance->codeword_idx = 0;
  239. break;
  240. default:
  241. // Here we expect only address messages
  242. if(codeword >> 31 == 0) {
  243. pocsag_decode_address_word(instance, codeword);
  244. instance->decoder.parser_step = PocsagDecoderStepMessage;
  245. }
  246. instance->codeword_idx++;
  247. }
  248. instance->decoder.decode_count_bit = 0;
  249. instance->decoder.decode_data = 0UL;
  250. }
  251. break;
  252. case PocsagDecoderStepMessage:
  253. if(instance->decoder.decode_count_bit == POCSAG_CW_BITS) {
  254. codeword = (uint32_t)(instance->decoder.decode_data & POCSAG_CW_MASK);
  255. switch(codeword) {
  256. case POCSAG_IDLE_CODE_WORD:
  257. // Idle during the message stops the message
  258. instance->codeword_idx++;
  259. instance->decoder.parser_step = PocsagDecoderStepFoundPreamble;
  260. pocsag_message_done(instance);
  261. break;
  262. case POCSAG_FRAME_SYNC_CODE:
  263. instance->codeword_idx = 0;
  264. break;
  265. default:
  266. // In this state, both address and message words can arrive
  267. if(codeword >> 31 == 0) {
  268. pocsag_message_done(instance);
  269. pocsag_decode_address_word(instance, codeword);
  270. } else {
  271. if(!pocsag_decode_message_word(instance, codeword)) {
  272. instance->decoder.parser_step = PocsagDecoderStepFoundPreamble;
  273. pocsag_message_done(instance);
  274. }
  275. }
  276. instance->codeword_idx++;
  277. }
  278. instance->decoder.decode_count_bit = 0;
  279. instance->decoder.decode_data = 0UL;
  280. }
  281. break;
  282. }
  283. }
  284. }
  285. uint8_t subghz_protocol_decoder_pocsag_get_hash_data(void* context) {
  286. furi_assert(context);
  287. SubGhzProtocolDecoderPocsag* instance = context;
  288. uint8_t hash = 0;
  289. for(size_t i = 0; i < furi_string_size(instance->done_msg); i++)
  290. hash ^= furi_string_get_char(instance->done_msg, i);
  291. return hash;
  292. }
  293. SubGhzProtocolStatus subghz_protocol_decoder_pocsag_serialize(
  294. void* context,
  295. FlipperFormat* flipper_format,
  296. SubGhzRadioPreset* preset) {
  297. furi_assert(context);
  298. SubGhzProtocolDecoderPocsag* instance = context;
  299. uint32_t msg_len;
  300. if(SubGhzProtocolStatusOk !=
  301. pcsg_block_generic_serialize(&instance->generic, flipper_format, preset))
  302. return SubGhzProtocolStatusError;
  303. msg_len = furi_string_size(instance->done_msg);
  304. if(!flipper_format_write_uint32(flipper_format, "MsgLen", &msg_len, 1)) {
  305. FURI_LOG_E(TAG, "Error adding MsgLen");
  306. return SubGhzProtocolStatusError;
  307. }
  308. if(!flipper_format_write_uint32(flipper_format, "PocsagVer", &instance->version, 1)) {
  309. FURI_LOG_E(TAG, "Error adding PocsagVer");
  310. return SubGhzProtocolStatusError;
  311. }
  312. uint8_t* s = (uint8_t*)furi_string_get_cstr(instance->done_msg);
  313. if(!flipper_format_write_hex(flipper_format, "Msg", s, msg_len)) {
  314. FURI_LOG_E(TAG, "Error adding Msg");
  315. return SubGhzProtocolStatusError;
  316. }
  317. return SubGhzProtocolStatusOk;
  318. }
  319. SubGhzProtocolStatus
  320. subghz_protocol_decoder_pocsag_deserialize(void* context, FlipperFormat* flipper_format) {
  321. furi_assert(context);
  322. SubGhzProtocolDecoderPocsag* instance = context;
  323. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  324. uint32_t msg_len;
  325. uint8_t* buf;
  326. do {
  327. if(SubGhzProtocolStatusOk !=
  328. pcsg_block_generic_deserialize(&instance->generic, flipper_format)) {
  329. break;
  330. }
  331. if(!flipper_format_read_uint32(flipper_format, "MsgLen", &msg_len, 1)) {
  332. FURI_LOG_E(TAG, "Missing MsgLen");
  333. break;
  334. }
  335. //optional, so compatible backwards
  336. instance->version = 1200;
  337. flipper_format_read_uint32(flipper_format, "PocsagVer", &instance->version, 1);
  338. buf = malloc(msg_len);
  339. if(!flipper_format_read_hex(flipper_format, "Msg", buf, msg_len)) {
  340. FURI_LOG_E(TAG, "Missing Msg");
  341. free(buf);
  342. break;
  343. }
  344. furi_string_set_strn(instance->done_msg, (const char*)buf, msg_len);
  345. free(buf);
  346. ret = SubGhzProtocolStatusOk;
  347. } while(false);
  348. return ret;
  349. }
  350. void subhz_protocol_decoder_pocsag_get_string(void* context, FuriString* output) {
  351. furi_assert(context);
  352. SubGhzProtocolDecoderPocsag* instance = context;
  353. furi_string_cat_printf(
  354. output, "%s %lu\r\n", instance->generic.protocol_name, instance->version);
  355. furi_string_cat_printf(output, "Addr: %lu\r\n", instance->ric);
  356. furi_string_cat(output, instance->done_msg);
  357. }
  358. const SubGhzProtocolDecoder subghz_protocol_pocsag_decoder = {
  359. .alloc = subghz_protocol_decoder_pocsag_alloc,
  360. .free = subghz_protocol_decoder_pocsag_free,
  361. .reset = subghz_protocol_decoder_pocsag_reset,
  362. .feed = subghz_protocol_decoder_pocsag_feed,
  363. .get_hash_data = subghz_protocol_decoder_pocsag_get_hash_data,
  364. .serialize = subghz_protocol_decoder_pocsag_serialize,
  365. .deserialize = subghz_protocol_decoder_pocsag_deserialize,
  366. .get_string = subhz_protocol_decoder_pocsag_get_string,
  367. };
  368. const SubGhzProtocolEncoder subghz_protocol_pocsag_encoder = {
  369. .alloc = NULL,
  370. .free = NULL,
  371. .deserialize = NULL,
  372. .stop = NULL,
  373. .yield = NULL,
  374. };
  375. const SubGhzProtocol subghz_protocol_pocsag = {
  376. .name = SUBGHZ_PROTOCOL_POCSAG_NAME,
  377. .type = SubGhzProtocolTypeStatic,
  378. .flag = SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Save |
  379. SubGhzProtocolFlag_Load,
  380. .decoder = &subghz_protocol_pocsag_decoder,
  381. .encoder = &subghz_protocol_pocsag_encoder,
  382. };