holtek_ht12x.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "holtek_ht12x.h"
  2. #include "../blocks/const.h"
  3. #include "../blocks/decoder.h"
  4. #include "../blocks/encoder.h"
  5. #include "../blocks/generic.h"
  6. #include "../blocks/math.h"
  7. /*
  8. * Help
  9. * https://www.holtek.com/documents/10179/116711/HT12A_Ev130.pdf
  10. *
  11. */
  12. #define TAG "SubGhzProtocolHoltek_HT12X"
  13. #define DIP_PATTERN "%c%c%c%c%c%c%c%c"
  14. #define CNT_TO_DIP(dip) \
  15. (dip & 0x0080 ? '0' : '1'), (dip & 0x0040 ? '0' : '1'), (dip & 0x0020 ? '0' : '1'), \
  16. (dip & 0x0010 ? '0' : '1'), (dip & 0x0008 ? '0' : '1'), (dip & 0x0004 ? '0' : '1'), \
  17. (dip & 0x0002 ? '0' : '1'), (dip & 0x0001 ? '0' : '1')
  18. static const SubGhzBlockConst subghz_protocol_holtek_th12x_const = {
  19. .te_short = 320,
  20. .te_long = 640,
  21. .te_delta = 200,
  22. .min_count_bit_for_found = 12,
  23. };
  24. struct SubGhzProtocolDecoderHoltek_HT12X {
  25. SubGhzProtocolDecoderBase base;
  26. SubGhzBlockDecoder decoder;
  27. SubGhzBlockGeneric generic;
  28. uint32_t te;
  29. uint32_t last_data;
  30. };
  31. struct SubGhzProtocolEncoderHoltek_HT12X {
  32. SubGhzProtocolEncoderBase base;
  33. SubGhzProtocolBlockEncoder encoder;
  34. SubGhzBlockGeneric generic;
  35. uint32_t te;
  36. };
  37. typedef enum {
  38. Holtek_HT12XDecoderStepReset = 0,
  39. Holtek_HT12XDecoderStepFoundStartBit,
  40. Holtek_HT12XDecoderStepSaveDuration,
  41. Holtek_HT12XDecoderStepCheckDuration,
  42. } Holtek_HT12XDecoderStep;
  43. const SubGhzProtocolDecoder subghz_protocol_holtek_th12x_decoder = {
  44. .alloc = subghz_protocol_decoder_holtek_th12x_alloc,
  45. .free = subghz_protocol_decoder_holtek_th12x_free,
  46. .feed = subghz_protocol_decoder_holtek_th12x_feed,
  47. .reset = subghz_protocol_decoder_holtek_th12x_reset,
  48. .get_hash_data = subghz_protocol_decoder_holtek_th12x_get_hash_data,
  49. .serialize = subghz_protocol_decoder_holtek_th12x_serialize,
  50. .deserialize = subghz_protocol_decoder_holtek_th12x_deserialize,
  51. .get_string = subghz_protocol_decoder_holtek_th12x_get_string,
  52. };
  53. const SubGhzProtocolEncoder subghz_protocol_holtek_th12x_encoder = {
  54. .alloc = subghz_protocol_encoder_holtek_th12x_alloc,
  55. .free = subghz_protocol_encoder_holtek_th12x_free,
  56. .deserialize = subghz_protocol_encoder_holtek_th12x_deserialize,
  57. .stop = subghz_protocol_encoder_holtek_th12x_stop,
  58. .yield = subghz_protocol_encoder_holtek_th12x_yield,
  59. };
  60. const SubGhzProtocol subghz_protocol_holtek_th12x = {
  61. .name = SUBGHZ_PROTOCOL_HOLTEK_HT12X_NAME,
  62. .type = SubGhzProtocolTypeStatic,
  63. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_315 |
  64. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable |
  65. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  66. .decoder = &subghz_protocol_holtek_th12x_decoder,
  67. .encoder = &subghz_protocol_holtek_th12x_encoder,
  68. };
  69. void* subghz_protocol_encoder_holtek_th12x_alloc(SubGhzEnvironment* environment) {
  70. UNUSED(environment);
  71. SubGhzProtocolEncoderHoltek_HT12X* instance =
  72. malloc(sizeof(SubGhzProtocolEncoderHoltek_HT12X));
  73. instance->base.protocol = &subghz_protocol_holtek_th12x;
  74. instance->generic.protocol_name = instance->base.protocol->name;
  75. instance->encoder.repeat = 10;
  76. instance->encoder.size_upload = 128;
  77. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  78. instance->encoder.is_running = false;
  79. return instance;
  80. }
  81. void subghz_protocol_encoder_holtek_th12x_free(void* context) {
  82. furi_assert(context);
  83. SubGhzProtocolEncoderHoltek_HT12X* instance = context;
  84. free(instance->encoder.upload);
  85. free(instance);
  86. }
  87. /**
  88. * Generating an upload from data.
  89. * @param instance Pointer to a SubGhzProtocolEncoderHoltek_HT12X instance
  90. * @return true On success
  91. */
  92. static bool
  93. subghz_protocol_encoder_holtek_th12x_get_upload(SubGhzProtocolEncoderHoltek_HT12X* instance) {
  94. furi_assert(instance);
  95. size_t index = 0;
  96. size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
  97. if(size_upload > instance->encoder.size_upload) {
  98. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  99. return false;
  100. } else {
  101. instance->encoder.size_upload = size_upload;
  102. }
  103. //Send header
  104. instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)instance->te * 36);
  105. //Send start bit
  106. instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)instance->te);
  107. //Send key data
  108. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  109. if(bit_read(instance->generic.data, i - 1)) {
  110. //send bit 1
  111. instance->encoder.upload[index++] =
  112. level_duration_make(false, (uint32_t)instance->te * 2);
  113. instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)instance->te);
  114. } else {
  115. //send bit 0
  116. instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)instance->te);
  117. instance->encoder.upload[index++] =
  118. level_duration_make(true, (uint32_t)instance->te * 2);
  119. }
  120. }
  121. return true;
  122. }
  123. SubGhzProtocolStatus
  124. subghz_protocol_encoder_holtek_th12x_deserialize(void* context, FlipperFormat* flipper_format) {
  125. furi_assert(context);
  126. SubGhzProtocolEncoderHoltek_HT12X* instance = context;
  127. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  128. do {
  129. ret = subghz_block_generic_deserialize_check_count_bit(
  130. &instance->generic,
  131. flipper_format,
  132. subghz_protocol_holtek_th12x_const.min_count_bit_for_found);
  133. if(ret != SubGhzProtocolStatusOk) {
  134. break;
  135. }
  136. if(!flipper_format_rewind(flipper_format)) {
  137. FURI_LOG_E(TAG, "Rewind error");
  138. ret = SubGhzProtocolStatusErrorParserOthers;
  139. break;
  140. }
  141. if(!flipper_format_read_uint32(flipper_format, "TE", (uint32_t*)&instance->te, 1)) {
  142. FURI_LOG_E(TAG, "Missing TE");
  143. ret = SubGhzProtocolStatusErrorParserTe;
  144. break;
  145. }
  146. //optional parameter parameter
  147. flipper_format_read_uint32(
  148. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  149. if(!subghz_protocol_encoder_holtek_th12x_get_upload(instance)) {
  150. ret = SubGhzProtocolStatusErrorEncoderGetUpload;
  151. break;
  152. }
  153. instance->encoder.is_running = true;
  154. } while(false);
  155. return ret;
  156. }
  157. void subghz_protocol_encoder_holtek_th12x_stop(void* context) {
  158. SubGhzProtocolEncoderHoltek_HT12X* instance = context;
  159. instance->encoder.is_running = false;
  160. }
  161. LevelDuration subghz_protocol_encoder_holtek_th12x_yield(void* context) {
  162. SubGhzProtocolEncoderHoltek_HT12X* instance = context;
  163. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  164. instance->encoder.is_running = false;
  165. return level_duration_reset();
  166. }
  167. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  168. if(++instance->encoder.front == instance->encoder.size_upload) {
  169. instance->encoder.repeat--;
  170. instance->encoder.front = 0;
  171. }
  172. return ret;
  173. }
  174. void* subghz_protocol_decoder_holtek_th12x_alloc(SubGhzEnvironment* environment) {
  175. UNUSED(environment);
  176. SubGhzProtocolDecoderHoltek_HT12X* instance =
  177. malloc(sizeof(SubGhzProtocolDecoderHoltek_HT12X));
  178. instance->base.protocol = &subghz_protocol_holtek_th12x;
  179. instance->generic.protocol_name = instance->base.protocol->name;
  180. return instance;
  181. }
  182. void subghz_protocol_decoder_holtek_th12x_free(void* context) {
  183. furi_assert(context);
  184. SubGhzProtocolDecoderHoltek_HT12X* instance = context;
  185. free(instance);
  186. }
  187. void subghz_protocol_decoder_holtek_th12x_reset(void* context) {
  188. furi_assert(context);
  189. SubGhzProtocolDecoderHoltek_HT12X* instance = context;
  190. instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
  191. }
  192. void subghz_protocol_decoder_holtek_th12x_feed(void* context, bool level, uint32_t duration) {
  193. furi_assert(context);
  194. SubGhzProtocolDecoderHoltek_HT12X* instance = context;
  195. switch(instance->decoder.parser_step) {
  196. case Holtek_HT12XDecoderStepReset:
  197. if((!level) && (DURATION_DIFF(duration, subghz_protocol_holtek_th12x_const.te_short * 36) <
  198. subghz_protocol_holtek_th12x_const.te_delta * 36)) {
  199. //Found Preambula
  200. instance->decoder.parser_step = Holtek_HT12XDecoderStepFoundStartBit;
  201. }
  202. break;
  203. case Holtek_HT12XDecoderStepFoundStartBit:
  204. if((level) && (DURATION_DIFF(duration, subghz_protocol_holtek_th12x_const.te_short) <
  205. subghz_protocol_holtek_th12x_const.te_delta)) {
  206. //Found StartBit
  207. instance->decoder.parser_step = Holtek_HT12XDecoderStepSaveDuration;
  208. instance->decoder.decode_data = 0;
  209. instance->decoder.decode_count_bit = 0;
  210. instance->te = duration;
  211. } else {
  212. instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
  213. }
  214. break;
  215. case Holtek_HT12XDecoderStepSaveDuration:
  216. //save duration
  217. if(!level) {
  218. if(duration >= ((uint32_t)subghz_protocol_holtek_th12x_const.te_short * 10 +
  219. subghz_protocol_holtek_th12x_const.te_delta)) {
  220. if(instance->decoder.decode_count_bit ==
  221. subghz_protocol_holtek_th12x_const.min_count_bit_for_found) {
  222. if((instance->last_data == instance->decoder.decode_data) &&
  223. instance->last_data) {
  224. instance->te /= (instance->decoder.decode_count_bit * 3 + 1);
  225. instance->generic.data = instance->decoder.decode_data;
  226. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  227. if(instance->base.callback)
  228. instance->base.callback(&instance->base, instance->base.context);
  229. }
  230. instance->last_data = instance->decoder.decode_data;
  231. }
  232. instance->decoder.decode_data = 0;
  233. instance->decoder.decode_count_bit = 0;
  234. instance->te = 0;
  235. instance->decoder.parser_step = Holtek_HT12XDecoderStepFoundStartBit;
  236. break;
  237. } else {
  238. instance->decoder.te_last = duration;
  239. instance->te += duration;
  240. instance->decoder.parser_step = Holtek_HT12XDecoderStepCheckDuration;
  241. }
  242. } else {
  243. instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
  244. }
  245. break;
  246. case Holtek_HT12XDecoderStepCheckDuration:
  247. if(level) {
  248. instance->te += duration;
  249. if((DURATION_DIFF(
  250. instance->decoder.te_last, subghz_protocol_holtek_th12x_const.te_long) <
  251. subghz_protocol_holtek_th12x_const.te_delta * 2) &&
  252. (DURATION_DIFF(duration, subghz_protocol_holtek_th12x_const.te_short) <
  253. subghz_protocol_holtek_th12x_const.te_delta)) {
  254. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  255. instance->decoder.parser_step = Holtek_HT12XDecoderStepSaveDuration;
  256. } else if(
  257. (DURATION_DIFF(
  258. instance->decoder.te_last, subghz_protocol_holtek_th12x_const.te_short) <
  259. subghz_protocol_holtek_th12x_const.te_delta) &&
  260. (DURATION_DIFF(duration, subghz_protocol_holtek_th12x_const.te_long) <
  261. subghz_protocol_holtek_th12x_const.te_delta * 2)) {
  262. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  263. instance->decoder.parser_step = Holtek_HT12XDecoderStepSaveDuration;
  264. } else {
  265. instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
  266. }
  267. } else {
  268. instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
  269. }
  270. break;
  271. }
  272. }
  273. /**
  274. * Analysis of received data
  275. * @param instance Pointer to a SubGhzBlockGeneric* instance
  276. */
  277. static void subghz_protocol_holtek_th12x_check_remote_controller(SubGhzBlockGeneric* instance) {
  278. instance->btn = instance->data & 0x0F;
  279. instance->cnt = (instance->data >> 4) & 0xFF;
  280. }
  281. uint8_t subghz_protocol_decoder_holtek_th12x_get_hash_data(void* context) {
  282. furi_assert(context);
  283. SubGhzProtocolDecoderHoltek_HT12X* instance = context;
  284. return subghz_protocol_blocks_get_hash_data(
  285. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  286. }
  287. SubGhzProtocolStatus subghz_protocol_decoder_holtek_th12x_serialize(
  288. void* context,
  289. FlipperFormat* flipper_format,
  290. SubGhzRadioPreset* preset) {
  291. furi_assert(context);
  292. SubGhzProtocolDecoderHoltek_HT12X* instance = context;
  293. SubGhzProtocolStatus ret =
  294. subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  295. if((ret == SubGhzProtocolStatusOk) &&
  296. !flipper_format_write_uint32(flipper_format, "TE", &instance->te, 1)) {
  297. FURI_LOG_E(TAG, "Unable to add TE");
  298. ret = SubGhzProtocolStatusErrorParserTe;
  299. }
  300. return ret;
  301. }
  302. SubGhzProtocolStatus
  303. subghz_protocol_decoder_holtek_th12x_deserialize(void* context, FlipperFormat* flipper_format) {
  304. furi_assert(context);
  305. SubGhzProtocolDecoderHoltek_HT12X* instance = context;
  306. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  307. do {
  308. ret = subghz_block_generic_deserialize_check_count_bit(
  309. &instance->generic,
  310. flipper_format,
  311. subghz_protocol_holtek_th12x_const.min_count_bit_for_found);
  312. if(ret != SubGhzProtocolStatusOk) {
  313. break;
  314. }
  315. if(!flipper_format_rewind(flipper_format)) {
  316. FURI_LOG_E(TAG, "Rewind error");
  317. ret = SubGhzProtocolStatusErrorParserOthers;
  318. break;
  319. }
  320. if(!flipper_format_read_uint32(flipper_format, "TE", (uint32_t*)&instance->te, 1)) {
  321. FURI_LOG_E(TAG, "Missing TE");
  322. ret = SubGhzProtocolStatusErrorParserTe;
  323. break;
  324. }
  325. } while(false);
  326. return ret;
  327. }
  328. static void subghz_protocol_holtek_th12x_event_serialize(uint8_t event, FuriString* output) {
  329. furi_string_cat_printf(
  330. output,
  331. "%s%s%s%s\r\n",
  332. (((event >> 3) & 0x1) == 0x0 ? "B1 " : ""),
  333. (((event >> 2) & 0x1) == 0x0 ? "B2 " : ""),
  334. (((event >> 1) & 0x1) == 0x0 ? "B3 " : ""),
  335. (((event >> 0) & 0x1) == 0x0 ? "B4 " : ""));
  336. }
  337. void subghz_protocol_decoder_holtek_th12x_get_string(void* context, FuriString* output) {
  338. furi_assert(context);
  339. SubGhzProtocolDecoderHoltek_HT12X* instance = context;
  340. subghz_protocol_holtek_th12x_check_remote_controller(&instance->generic);
  341. furi_string_cat_printf(
  342. output,
  343. "%s %db\r\n"
  344. "Key:0x%03lX\r\n"
  345. "Btn: ",
  346. instance->generic.protocol_name,
  347. instance->generic.data_count_bit,
  348. (uint32_t)(instance->generic.data & 0xFFF));
  349. subghz_protocol_holtek_th12x_event_serialize(instance->generic.btn, output);
  350. furi_string_cat_printf(
  351. output,
  352. "DIP:" DIP_PATTERN "\r\n"
  353. "Te:%luus\r\n",
  354. CNT_TO_DIP(instance->generic.cnt),
  355. instance->te);
  356. }