honeywell_wdb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "honeywell_wdb.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. #define TAG "SubGhzProtocolHoneywellWDB"
  8. /*
  9. *
  10. * https://github.com/klohner/honeywell-wireless-doorbell
  11. *
  12. */
  13. static const SubGhzBlockConst subghz_protocol_honeywell_wdb_const = {
  14. .te_short = 160,
  15. .te_long = 320,
  16. .te_delta = 60,
  17. .min_count_bit_for_found = 48,
  18. };
  19. struct SubGhzProtocolDecoderHoneywell_WDB {
  20. SubGhzProtocolDecoderBase base;
  21. SubGhzBlockDecoder decoder;
  22. SubGhzBlockGeneric generic;
  23. const char* device_type;
  24. const char* alert;
  25. uint8_t secret_knock;
  26. uint8_t relay;
  27. uint8_t lowbat;
  28. };
  29. struct SubGhzProtocolEncoderHoneywell_WDB {
  30. SubGhzProtocolEncoderBase base;
  31. SubGhzProtocolBlockEncoder encoder;
  32. SubGhzBlockGeneric generic;
  33. };
  34. typedef enum {
  35. Honeywell_WDBDecoderStepReset = 0,
  36. Honeywell_WDBDecoderStepFoundStartBit,
  37. Honeywell_WDBDecoderStepSaveDuration,
  38. Honeywell_WDBDecoderStepCheckDuration,
  39. } Honeywell_WDBDecoderStep;
  40. const SubGhzProtocolDecoder subghz_protocol_honeywell_wdb_decoder = {
  41. .alloc = subghz_protocol_decoder_honeywell_wdb_alloc,
  42. .free = subghz_protocol_decoder_honeywell_wdb_free,
  43. .feed = subghz_protocol_decoder_honeywell_wdb_feed,
  44. .reset = subghz_protocol_decoder_honeywell_wdb_reset,
  45. .get_hash_data = subghz_protocol_decoder_honeywell_wdb_get_hash_data,
  46. .serialize = subghz_protocol_decoder_honeywell_wdb_serialize,
  47. .deserialize = subghz_protocol_decoder_honeywell_wdb_deserialize,
  48. .get_string = subghz_protocol_decoder_honeywell_wdb_get_string,
  49. };
  50. const SubGhzProtocolEncoder subghz_protocol_honeywell_wdb_encoder = {
  51. .alloc = subghz_protocol_encoder_honeywell_wdb_alloc,
  52. .free = subghz_protocol_encoder_honeywell_wdb_free,
  53. .deserialize = subghz_protocol_encoder_honeywell_wdb_deserialize,
  54. .stop = subghz_protocol_encoder_honeywell_wdb_stop,
  55. .yield = subghz_protocol_encoder_honeywell_wdb_yield,
  56. };
  57. const SubGhzProtocol subghz_protocol_honeywell_wdb = {
  58. .name = SUBGHZ_PROTOCOL_HONEYWELL_WDB_NAME,
  59. .type = SubGhzProtocolTypeStatic,
  60. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM |
  61. SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
  62. SubGhzProtocolFlag_Send,
  63. .decoder = &subghz_protocol_honeywell_wdb_decoder,
  64. .encoder = &subghz_protocol_honeywell_wdb_encoder,
  65. };
  66. void* subghz_protocol_encoder_honeywell_wdb_alloc(SubGhzEnvironment* environment) {
  67. UNUSED(environment);
  68. SubGhzProtocolEncoderHoneywell_WDB* instance =
  69. malloc(sizeof(SubGhzProtocolEncoderHoneywell_WDB));
  70. instance->base.protocol = &subghz_protocol_honeywell_wdb;
  71. instance->generic.protocol_name = instance->base.protocol->name;
  72. instance->encoder.repeat = 10;
  73. instance->encoder.size_upload = 128;
  74. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  75. instance->encoder.is_running = false;
  76. return instance;
  77. }
  78. void subghz_protocol_encoder_honeywell_wdb_free(void* context) {
  79. furi_assert(context);
  80. SubGhzProtocolEncoderHoneywell_WDB* instance = context;
  81. free(instance->encoder.upload);
  82. free(instance);
  83. }
  84. /**
  85. * Generating an upload from data.
  86. * @param instance Pointer to a SubGhzProtocolEncoderHoneywell_WDB instance
  87. * @return true On success
  88. */
  89. static bool subghz_protocol_encoder_honeywell_wdb_get_upload(
  90. SubGhzProtocolEncoderHoneywell_WDB* instance) {
  91. furi_assert(instance);
  92. size_t index = 0;
  93. size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
  94. if(size_upload > instance->encoder.size_upload) {
  95. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  96. return false;
  97. } else {
  98. instance->encoder.size_upload = size_upload;
  99. }
  100. //Send header
  101. instance->encoder.upload[index++] =
  102. level_duration_make(false, (uint32_t)subghz_protocol_honeywell_wdb_const.te_short * 3);
  103. //Send key data
  104. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  105. if(bit_read(instance->generic.data, i - 1)) {
  106. //send bit 1
  107. instance->encoder.upload[index++] =
  108. level_duration_make(true, (uint32_t)subghz_protocol_honeywell_wdb_const.te_long);
  109. instance->encoder.upload[index++] =
  110. level_duration_make(false, (uint32_t)subghz_protocol_honeywell_wdb_const.te_short);
  111. } else {
  112. //send bit 0
  113. instance->encoder.upload[index++] =
  114. level_duration_make(true, (uint32_t)subghz_protocol_honeywell_wdb_const.te_short);
  115. instance->encoder.upload[index++] =
  116. level_duration_make(false, (uint32_t)subghz_protocol_honeywell_wdb_const.te_long);
  117. }
  118. }
  119. instance->encoder.upload[index++] =
  120. level_duration_make(true, (uint32_t)subghz_protocol_honeywell_wdb_const.te_short * 3);
  121. return true;
  122. }
  123. bool subghz_protocol_encoder_honeywell_wdb_deserialize(
  124. void* context,
  125. FlipperFormat* flipper_format) {
  126. furi_assert(context);
  127. SubGhzProtocolEncoderHoneywell_WDB* instance = context;
  128. bool res = false;
  129. do {
  130. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  131. FURI_LOG_E(TAG, "Deserialize error");
  132. break;
  133. }
  134. if(instance->generic.data_count_bit !=
  135. subghz_protocol_honeywell_wdb_const.min_count_bit_for_found) {
  136. FURI_LOG_E(TAG, "Wrong number of bits in key");
  137. break;
  138. }
  139. //optional parameter parameter
  140. flipper_format_read_uint32(
  141. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  142. if(!subghz_protocol_encoder_honeywell_wdb_get_upload(instance)) break;
  143. instance->encoder.is_running = true;
  144. res = true;
  145. } while(false);
  146. return res;
  147. }
  148. void subghz_protocol_encoder_honeywell_wdb_stop(void* context) {
  149. SubGhzProtocolEncoderHoneywell_WDB* instance = context;
  150. instance->encoder.is_running = false;
  151. }
  152. LevelDuration subghz_protocol_encoder_honeywell_wdb_yield(void* context) {
  153. SubGhzProtocolEncoderHoneywell_WDB* instance = context;
  154. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  155. instance->encoder.is_running = false;
  156. return level_duration_reset();
  157. }
  158. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  159. if(++instance->encoder.front == instance->encoder.size_upload) {
  160. instance->encoder.repeat--;
  161. instance->encoder.front = 0;
  162. }
  163. return ret;
  164. }
  165. void* subghz_protocol_decoder_honeywell_wdb_alloc(SubGhzEnvironment* environment) {
  166. UNUSED(environment);
  167. SubGhzProtocolDecoderHoneywell_WDB* instance =
  168. malloc(sizeof(SubGhzProtocolDecoderHoneywell_WDB));
  169. instance->base.protocol = &subghz_protocol_honeywell_wdb;
  170. instance->generic.protocol_name = instance->base.protocol->name;
  171. return instance;
  172. }
  173. void subghz_protocol_decoder_honeywell_wdb_free(void* context) {
  174. furi_assert(context);
  175. SubGhzProtocolDecoderHoneywell_WDB* instance = context;
  176. free(instance);
  177. }
  178. void subghz_protocol_decoder_honeywell_wdb_reset(void* context) {
  179. furi_assert(context);
  180. SubGhzProtocolDecoderHoneywell_WDB* instance = context;
  181. instance->decoder.parser_step = Honeywell_WDBDecoderStepReset;
  182. }
  183. void subghz_protocol_decoder_honeywell_wdb_feed(void* context, bool level, uint32_t duration) {
  184. furi_assert(context);
  185. SubGhzProtocolDecoderHoneywell_WDB* instance = context;
  186. switch(instance->decoder.parser_step) {
  187. case Honeywell_WDBDecoderStepReset:
  188. if((!level) && (DURATION_DIFF(duration, subghz_protocol_honeywell_wdb_const.te_short * 3) <
  189. subghz_protocol_honeywell_wdb_const.te_delta)) {
  190. //Found header Honeywell_WDB
  191. instance->decoder.decode_count_bit = 0;
  192. instance->decoder.decode_data = 0;
  193. instance->decoder.parser_step = Honeywell_WDBDecoderStepSaveDuration;
  194. }
  195. break;
  196. case Honeywell_WDBDecoderStepSaveDuration:
  197. if(level) { //save interval
  198. if(DURATION_DIFF(duration, subghz_protocol_honeywell_wdb_const.te_short * 3) <
  199. subghz_protocol_honeywell_wdb_const.te_delta) {
  200. if((instance->decoder.decode_count_bit ==
  201. subghz_protocol_honeywell_wdb_const.min_count_bit_for_found) &&
  202. ((instance->decoder.decode_data & 0x01) ==
  203. subghz_protocol_blocks_get_parity(
  204. instance->decoder.decode_data >> 1,
  205. subghz_protocol_honeywell_wdb_const.min_count_bit_for_found - 1))) {
  206. instance->generic.data = instance->decoder.decode_data;
  207. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  208. if(instance->base.callback)
  209. instance->base.callback(&instance->base, instance->base.context);
  210. }
  211. instance->decoder.parser_step = Honeywell_WDBDecoderStepReset;
  212. break;
  213. }
  214. instance->decoder.te_last = duration;
  215. instance->decoder.parser_step = Honeywell_WDBDecoderStepCheckDuration;
  216. } else {
  217. instance->decoder.parser_step = Honeywell_WDBDecoderStepReset;
  218. }
  219. break;
  220. case Honeywell_WDBDecoderStepCheckDuration:
  221. if(!level) {
  222. if((DURATION_DIFF(
  223. instance->decoder.te_last, subghz_protocol_honeywell_wdb_const.te_short) <
  224. subghz_protocol_honeywell_wdb_const.te_delta) &&
  225. (DURATION_DIFF(duration, subghz_protocol_honeywell_wdb_const.te_long) <
  226. subghz_protocol_honeywell_wdb_const.te_delta)) {
  227. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  228. instance->decoder.parser_step = Honeywell_WDBDecoderStepSaveDuration;
  229. } else if(
  230. (DURATION_DIFF(
  231. instance->decoder.te_last, subghz_protocol_honeywell_wdb_const.te_long) <
  232. subghz_protocol_honeywell_wdb_const.te_delta) &&
  233. (DURATION_DIFF(duration, subghz_protocol_honeywell_wdb_const.te_short) <
  234. subghz_protocol_honeywell_wdb_const.te_delta)) {
  235. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  236. instance->decoder.parser_step = Honeywell_WDBDecoderStepSaveDuration;
  237. } else
  238. instance->decoder.parser_step = Honeywell_WDBDecoderStepReset;
  239. } else {
  240. instance->decoder.parser_step = Honeywell_WDBDecoderStepReset;
  241. }
  242. break;
  243. }
  244. }
  245. /**
  246. * Analysis of received data
  247. * @param instance Pointer to a SubGhzProtocolDecoderHoneywell_WDB* instance
  248. */
  249. static void subghz_protocol_honeywell_wdb_check_remote_controller(
  250. SubGhzProtocolDecoderHoneywell_WDB* instance) {
  251. /*
  252. *
  253. * Frame bits used in Honeywell RCWL300A, RCWL330A, Series 3, 5, 9 and all Decor Series Wireless Chimes
  254. * 0000 0000 1111 1111 2222 2222 3333 3333 4444 4444 5555 5555
  255. * 7654 3210 7654 3210 7654 3210 7654 3210 7654 3210 7654 3210
  256. * XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XX.. XXX. .... KEY DATA (any change and receiver doesn't seem to recognize signal)
  257. * XXXX XXXX XXXX XXXX XXXX .... .... .... .... .... .... .... KEY ID (different for each transmitter)
  258. * .... .... .... .... .... 0000 00.. 0000 0000 00.. 000. .... KEY UNKNOWN 0 (always 0 in devices I've tested)
  259. * .... .... .... .... .... .... ..XX .... .... .... .... .... DEVICE TYPE (10 = doorbell, 01 = PIR Motion sensor)
  260. * .... .... .... .... .... .... .... .... .... ..XX ...X XXX. FLAG DATA (may be modified for possible effects on receiver)
  261. * .... .... .... .... .... .... .... .... .... ..XX .... .... ALERT (00 = normal, 01 or 10 = right-left halo light pattern, 11 = full volume alarm)
  262. * .... .... .... .... .... .... .... .... .... .... ...X .... SECRET KNOCK (0 = default, 1 if doorbell is pressed 3x rapidly)
  263. * .... .... .... .... .... .... .... .... .... .... .... X... RELAY (1 if signal is a retransmission of a received transmission, only some models)
  264. * .... .... .... .... .... .... .... .... .... .... .... .X.. FLAG UNKNOWN (0 = default, but 1 is accepted and I don't observe any effects)
  265. * .... .... .... .... .... .... .... .... .... .... .... ..X. LOWBAT (1 if battery is low, receiver gives low battery alert)
  266. * .... .... .... .... .... .... .... .... .... .... .... ...X PARITY (LSB of count of set bits in previous 47 bits)
  267. *
  268. */
  269. instance->generic.serial = (instance->generic.data >> 28) & 0xFFFFF;
  270. switch((instance->generic.data >> 20) & 0x3) {
  271. case 0x02:
  272. instance->device_type = "Doorbell";
  273. break;
  274. case 0x01:
  275. instance->device_type = "PIR-Motion";
  276. break;
  277. default:
  278. instance->device_type = "Unknown";
  279. break;
  280. }
  281. switch((instance->generic.data >> 16) & 0x3) {
  282. case 0x00:
  283. instance->alert = "Normal";
  284. break;
  285. case 0x01:
  286. case 0x02:
  287. instance->alert = "High";
  288. break;
  289. case 0x03:
  290. instance->alert = "Full";
  291. break;
  292. default:
  293. instance->alert = "Unknown";
  294. break;
  295. }
  296. instance->secret_knock = (uint8_t)((instance->generic.data >> 4) & 0x1);
  297. instance->relay = (uint8_t)((instance->generic.data >> 3) & 0x1);
  298. instance->lowbat = (uint8_t)((instance->generic.data >> 1) & 0x1);
  299. }
  300. uint8_t subghz_protocol_decoder_honeywell_wdb_get_hash_data(void* context) {
  301. furi_assert(context);
  302. SubGhzProtocolDecoderHoneywell_WDB* instance = context;
  303. return subghz_protocol_blocks_get_hash_data(
  304. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  305. }
  306. bool subghz_protocol_decoder_honeywell_wdb_serialize(
  307. void* context,
  308. FlipperFormat* flipper_format,
  309. SubGhzRadioPreset* preset) {
  310. furi_assert(context);
  311. SubGhzProtocolDecoderHoneywell_WDB* instance = context;
  312. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  313. }
  314. bool subghz_protocol_decoder_honeywell_wdb_deserialize(
  315. void* context,
  316. FlipperFormat* flipper_format) {
  317. furi_assert(context);
  318. SubGhzProtocolDecoderHoneywell_WDB* instance = context;
  319. bool ret = false;
  320. do {
  321. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  322. break;
  323. }
  324. if(instance->generic.data_count_bit !=
  325. subghz_protocol_honeywell_wdb_const.min_count_bit_for_found) {
  326. FURI_LOG_E(TAG, "Wrong number of bits in key");
  327. break;
  328. }
  329. ret = true;
  330. } while(false);
  331. return ret;
  332. }
  333. void subghz_protocol_decoder_honeywell_wdb_get_string(void* context, FuriString* output) {
  334. furi_assert(context);
  335. SubGhzProtocolDecoderHoneywell_WDB* instance = context;
  336. subghz_protocol_honeywell_wdb_check_remote_controller(instance);
  337. furi_string_cat_printf(
  338. output,
  339. "%s %dbit\r\n"
  340. "Key:0x%lX%08lX\r\n"
  341. "Sn:0x%05lX\r\n"
  342. "DT:%s Al:%s\r\n"
  343. "SK:%01X R:%01X LBat:%01X\r\n",
  344. instance->generic.protocol_name,
  345. instance->generic.data_count_bit,
  346. (uint32_t)((instance->generic.data >> 32) & 0xFFFFFFFF),
  347. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  348. instance->generic.serial,
  349. instance->device_type,
  350. instance->alert,
  351. instance->secret_knock,
  352. instance->relay,
  353. instance->lowbat);
  354. }