holtek.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include "holtek.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://pdf1.alldatasheet.com/datasheet-pdf/view/82103/HOLTEK/HT640.html
  10. * https://fccid.io/OJM-CMD-HHLR-XXXA
  11. *
  12. */
  13. #define TAG "SubGhzProtocolHoltek"
  14. #define HOLTEK_HEADER_MASK 0xF000000000
  15. #define HOLTEK_HEADER 0x5000000000
  16. static const SubGhzBlockConst subghz_protocol_holtek_const = {
  17. .te_short = 430,
  18. .te_long = 870,
  19. .te_delta = 100,
  20. .min_count_bit_for_found = 40,
  21. };
  22. struct SubGhzProtocolDecoderHoltek {
  23. SubGhzProtocolDecoderBase base;
  24. SubGhzBlockDecoder decoder;
  25. SubGhzBlockGeneric generic;
  26. };
  27. struct SubGhzProtocolEncoderHoltek {
  28. SubGhzProtocolEncoderBase base;
  29. SubGhzProtocolBlockEncoder encoder;
  30. SubGhzBlockGeneric generic;
  31. };
  32. typedef enum {
  33. HoltekDecoderStepReset = 0,
  34. HoltekDecoderStepFoundStartBit,
  35. HoltekDecoderStepSaveDuration,
  36. HoltekDecoderStepCheckDuration,
  37. } HoltekDecoderStep;
  38. const SubGhzProtocolDecoder subghz_protocol_holtek_decoder = {
  39. .alloc = subghz_protocol_decoder_holtek_alloc,
  40. .free = subghz_protocol_decoder_holtek_free,
  41. .feed = subghz_protocol_decoder_holtek_feed,
  42. .reset = subghz_protocol_decoder_holtek_reset,
  43. .get_hash_data = subghz_protocol_decoder_holtek_get_hash_data,
  44. .serialize = subghz_protocol_decoder_holtek_serialize,
  45. .deserialize = subghz_protocol_decoder_holtek_deserialize,
  46. .get_string = subghz_protocol_decoder_holtek_get_string,
  47. };
  48. const SubGhzProtocolEncoder subghz_protocol_holtek_encoder = {
  49. .alloc = subghz_protocol_encoder_holtek_alloc,
  50. .free = subghz_protocol_encoder_holtek_free,
  51. .deserialize = subghz_protocol_encoder_holtek_deserialize,
  52. .stop = subghz_protocol_encoder_holtek_stop,
  53. .yield = subghz_protocol_encoder_holtek_yield,
  54. };
  55. const SubGhzProtocol subghz_protocol_holtek = {
  56. .name = SUBGHZ_PROTOCOL_HOLTEK_NAME,
  57. .type = SubGhzProtocolTypeStatic,
  58. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_315 |
  59. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load |
  60. SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  61. .decoder = &subghz_protocol_holtek_decoder,
  62. .encoder = &subghz_protocol_holtek_encoder,
  63. };
  64. void* subghz_protocol_encoder_holtek_alloc(SubGhzEnvironment* environment) {
  65. UNUSED(environment);
  66. SubGhzProtocolEncoderHoltek* instance = malloc(sizeof(SubGhzProtocolEncoderHoltek));
  67. instance->base.protocol = &subghz_protocol_holtek;
  68. instance->generic.protocol_name = instance->base.protocol->name;
  69. instance->encoder.repeat = 10;
  70. instance->encoder.size_upload = 128;
  71. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  72. instance->encoder.is_running = false;
  73. return instance;
  74. }
  75. void subghz_protocol_encoder_holtek_free(void* context) {
  76. furi_assert(context);
  77. SubGhzProtocolEncoderHoltek* instance = context;
  78. free(instance->encoder.upload);
  79. free(instance);
  80. }
  81. /**
  82. * Generating an upload from data.
  83. * @param instance Pointer to a SubGhzProtocolEncoderHoltek instance
  84. * @return true On success
  85. */
  86. static bool subghz_protocol_encoder_holtek_get_upload(SubGhzProtocolEncoderHoltek* instance) {
  87. furi_assert(instance);
  88. size_t index = 0;
  89. size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
  90. if(size_upload > instance->encoder.size_upload) {
  91. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  92. return false;
  93. } else {
  94. instance->encoder.size_upload = size_upload;
  95. }
  96. //Send header
  97. instance->encoder.upload[index++] =
  98. level_duration_make(false, (uint32_t)subghz_protocol_holtek_const.te_short * 36);
  99. //Send start bit
  100. instance->encoder.upload[index++] =
  101. level_duration_make(true, (uint32_t)subghz_protocol_holtek_const.te_short);
  102. //Send key data
  103. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  104. if(bit_read(instance->generic.data, i - 1)) {
  105. //send bit 1
  106. instance->encoder.upload[index++] =
  107. level_duration_make(false, (uint32_t)subghz_protocol_holtek_const.te_long);
  108. instance->encoder.upload[index++] =
  109. level_duration_make(true, (uint32_t)subghz_protocol_holtek_const.te_short);
  110. } else {
  111. //send bit 0
  112. instance->encoder.upload[index++] =
  113. level_duration_make(false, (uint32_t)subghz_protocol_holtek_const.te_short);
  114. instance->encoder.upload[index++] =
  115. level_duration_make(true, (uint32_t)subghz_protocol_holtek_const.te_long);
  116. }
  117. }
  118. return true;
  119. }
  120. bool subghz_protocol_encoder_holtek_deserialize(void* context, FlipperFormat* flipper_format) {
  121. furi_assert(context);
  122. SubGhzProtocolEncoderHoltek* instance = context;
  123. bool res = false;
  124. do {
  125. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  126. FURI_LOG_E(TAG, "Deserialize error");
  127. break;
  128. }
  129. if(instance->generic.data_count_bit !=
  130. subghz_protocol_holtek_const.min_count_bit_for_found) {
  131. FURI_LOG_E(TAG, "Wrong number of bits in key");
  132. break;
  133. }
  134. //optional parameter parameter
  135. flipper_format_read_uint32(
  136. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  137. if(!subghz_protocol_encoder_holtek_get_upload(instance)) break;
  138. instance->encoder.is_running = true;
  139. res = true;
  140. } while(false);
  141. return res;
  142. }
  143. void subghz_protocol_encoder_holtek_stop(void* context) {
  144. SubGhzProtocolEncoderHoltek* instance = context;
  145. instance->encoder.is_running = false;
  146. }
  147. LevelDuration subghz_protocol_encoder_holtek_yield(void* context) {
  148. SubGhzProtocolEncoderHoltek* instance = context;
  149. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  150. instance->encoder.is_running = false;
  151. return level_duration_reset();
  152. }
  153. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  154. if(++instance->encoder.front == instance->encoder.size_upload) {
  155. instance->encoder.repeat--;
  156. instance->encoder.front = 0;
  157. }
  158. return ret;
  159. }
  160. void* subghz_protocol_decoder_holtek_alloc(SubGhzEnvironment* environment) {
  161. UNUSED(environment);
  162. SubGhzProtocolDecoderHoltek* instance = malloc(sizeof(SubGhzProtocolDecoderHoltek));
  163. instance->base.protocol = &subghz_protocol_holtek;
  164. instance->generic.protocol_name = instance->base.protocol->name;
  165. return instance;
  166. }
  167. void subghz_protocol_decoder_holtek_free(void* context) {
  168. furi_assert(context);
  169. SubGhzProtocolDecoderHoltek* instance = context;
  170. free(instance);
  171. }
  172. void subghz_protocol_decoder_holtek_reset(void* context) {
  173. furi_assert(context);
  174. SubGhzProtocolDecoderHoltek* instance = context;
  175. instance->decoder.parser_step = HoltekDecoderStepReset;
  176. }
  177. void subghz_protocol_decoder_holtek_feed(void* context, bool level, uint32_t duration) {
  178. furi_assert(context);
  179. SubGhzProtocolDecoderHoltek* instance = context;
  180. switch(instance->decoder.parser_step) {
  181. case HoltekDecoderStepReset:
  182. if((!level) && (DURATION_DIFF(duration, subghz_protocol_holtek_const.te_short * 36) <
  183. subghz_protocol_holtek_const.te_delta * 36)) {
  184. //Found Preambula
  185. instance->decoder.parser_step = HoltekDecoderStepFoundStartBit;
  186. }
  187. break;
  188. case HoltekDecoderStepFoundStartBit:
  189. if((level) && (DURATION_DIFF(duration, subghz_protocol_holtek_const.te_short) <
  190. subghz_protocol_holtek_const.te_delta)) {
  191. //Found StartBit
  192. instance->decoder.parser_step = HoltekDecoderStepSaveDuration;
  193. instance->decoder.decode_data = 0;
  194. instance->decoder.decode_count_bit = 0;
  195. } else {
  196. instance->decoder.parser_step = HoltekDecoderStepReset;
  197. }
  198. break;
  199. case HoltekDecoderStepSaveDuration:
  200. //save duration
  201. if(!level) {
  202. if(duration >= ((uint32_t)subghz_protocol_holtek_const.te_short * 10 +
  203. subghz_protocol_holtek_const.te_delta)) {
  204. if(instance->decoder.decode_count_bit ==
  205. subghz_protocol_holtek_const.min_count_bit_for_found) {
  206. if((instance->decoder.decode_data & HOLTEK_HEADER_MASK) == HOLTEK_HEADER) {
  207. instance->generic.data = instance->decoder.decode_data;
  208. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  209. if(instance->base.callback)
  210. instance->base.callback(&instance->base, instance->base.context);
  211. }
  212. }
  213. instance->decoder.decode_data = 0;
  214. instance->decoder.decode_count_bit = 0;
  215. instance->decoder.parser_step = HoltekDecoderStepFoundStartBit;
  216. break;
  217. } else {
  218. instance->decoder.te_last = duration;
  219. instance->decoder.parser_step = HoltekDecoderStepCheckDuration;
  220. }
  221. } else {
  222. instance->decoder.parser_step = HoltekDecoderStepReset;
  223. }
  224. break;
  225. case HoltekDecoderStepCheckDuration:
  226. if(level) {
  227. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_holtek_const.te_short) <
  228. subghz_protocol_holtek_const.te_delta) &&
  229. (DURATION_DIFF(duration, subghz_protocol_holtek_const.te_long) <
  230. subghz_protocol_holtek_const.te_delta * 2)) {
  231. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  232. instance->decoder.parser_step = HoltekDecoderStepSaveDuration;
  233. } else if(
  234. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_holtek_const.te_long) <
  235. subghz_protocol_holtek_const.te_delta * 2) &&
  236. (DURATION_DIFF(duration, subghz_protocol_holtek_const.te_short) <
  237. subghz_protocol_holtek_const.te_delta)) {
  238. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  239. instance->decoder.parser_step = HoltekDecoderStepSaveDuration;
  240. } else {
  241. instance->decoder.parser_step = HoltekDecoderStepReset;
  242. }
  243. } else {
  244. instance->decoder.parser_step = HoltekDecoderStepReset;
  245. }
  246. break;
  247. }
  248. }
  249. /**
  250. * Analysis of received data
  251. * @param instance Pointer to a SubGhzBlockGeneric* instance
  252. */
  253. static void subghz_protocol_holtek_check_remote_controller(SubGhzBlockGeneric* instance) {
  254. if((instance->data & HOLTEK_HEADER_MASK) == HOLTEK_HEADER) {
  255. instance->serial =
  256. subghz_protocol_blocks_reverse_key((instance->data >> 16) & 0xFFFFF, 20);
  257. uint16_t btn = instance->data & 0xFFFF;
  258. if((btn & 0xf) != 0xA) {
  259. instance->btn = 0x1 << 4 | (btn & 0xF);
  260. } else if(((btn >> 4) & 0xF) != 0xA) {
  261. instance->btn = 0x2 << 4 | ((btn >> 4) & 0xF);
  262. } else if(((btn >> 8) & 0xF) != 0xA) {
  263. instance->btn = 0x3 << 4 | ((btn >> 8) & 0xF);
  264. } else if(((btn >> 12) & 0xF) != 0xA) {
  265. instance->btn = 0x4 << 4 | ((btn >> 12) & 0xF);
  266. } else {
  267. instance->btn = 0;
  268. }
  269. } else {
  270. instance->serial = 0;
  271. instance->btn = 0;
  272. instance->cnt = 0;
  273. }
  274. }
  275. uint8_t subghz_protocol_decoder_holtek_get_hash_data(void* context) {
  276. furi_assert(context);
  277. SubGhzProtocolDecoderHoltek* instance = context;
  278. return subghz_protocol_blocks_get_hash_data(
  279. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  280. }
  281. bool subghz_protocol_decoder_holtek_serialize(
  282. void* context,
  283. FlipperFormat* flipper_format,
  284. SubGhzRadioPreset* preset) {
  285. furi_assert(context);
  286. SubGhzProtocolDecoderHoltek* instance = context;
  287. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  288. }
  289. bool subghz_protocol_decoder_holtek_deserialize(void* context, FlipperFormat* flipper_format) {
  290. furi_assert(context);
  291. SubGhzProtocolDecoderHoltek* instance = context;
  292. bool ret = false;
  293. do {
  294. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  295. break;
  296. }
  297. if(instance->generic.data_count_bit !=
  298. subghz_protocol_holtek_const.min_count_bit_for_found) {
  299. FURI_LOG_E(TAG, "Wrong number of bits in key");
  300. break;
  301. }
  302. ret = true;
  303. } while(false);
  304. return ret;
  305. }
  306. void subghz_protocol_decoder_holtek_get_string(void* context, FuriString* output) {
  307. furi_assert(context);
  308. SubGhzProtocolDecoderHoltek* instance = context;
  309. subghz_protocol_holtek_check_remote_controller(&instance->generic);
  310. furi_string_cat_printf(
  311. output,
  312. "%s %dbit\r\n"
  313. "Key:0x%lX%08lX\r\n"
  314. "Sn:0x%05lX Btn:%X ",
  315. instance->generic.protocol_name,
  316. instance->generic.data_count_bit,
  317. (uint32_t)((instance->generic.data >> 32) & 0xFFFFFFFF),
  318. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  319. instance->generic.serial,
  320. instance->generic.btn >> 4);
  321. if((instance->generic.btn & 0xF) == 0xE) {
  322. furi_string_cat_printf(output, "ON\r\n");
  323. } else if(((instance->generic.btn & 0xF) == 0xB)) {
  324. furi_string_cat_printf(output, "OFF\r\n");
  325. }
  326. }