-
Notifications
You must be signed in to change notification settings - Fork 4
/
gdo.c
executable file
·2026 lines (1769 loc) · 71 KB
/
gdo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GdoLib - A library for controlling garage door openers.
* Copyright (C) 2024 Konnected Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "secplus.h"
#include "gdo_priv.h"
#include <string.h>
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>
/***************************** LOCAL FUNCTION DECLARATIONS ****************************/
static void obst_isr_handler(void* arg);
static void gdo_main_task(void* arg);
static void gdo_sync_task(void* arg);
static void v1_status_timer_cb(void* arg);
static void motion_detect_timer_cb(void* arg);
static void door_position_sync_timer_cb(void* arg);
static void scheduled_cmd_timer_cb(void* arg);
static void scheduled_event_timer_cb(void* arg);
static void obst_timer_cb(void* arg);
static void get_paired_devices(gdo_paired_device_type_t type);
static void update_light_state(gdo_light_state_t light_state);
static void update_lock_state(gdo_lock_state_t lock_state);
static void update_learn_state(gdo_learn_state_t learn_state);
static void handle_light_action(gdo_light_action_t light_action);
static void update_obstruction_state(gdo_obstruction_state_t obs_state);
static void update_motion_state(gdo_motion_state_t motion_state);
static void update_motor_state(gdo_motor_state_t motor_state);
static void update_button_state(gdo_button_state_t button_state);
static void update_openings(uint8_t nibble, uint16_t openings);
static void update_ttc(uint16_t ttc_seconds);
static void update_paired_devices(gdo_paired_device_type_t type, uint8_t count);
static void update_battery_state(gdo_battery_state_t battery_state);
static void update_door_state(const gdo_door_state_t door_state);
static void decode_packet(uint8_t *packet);
static esp_err_t get_status();
static esp_err_t get_openings();
static esp_err_t send_door_action(gdo_door_action_t action);
static esp_err_t transmit_packet(uint8_t *packet);
static esp_err_t queue_command(gdo_command_t command, uint8_t nibble, uint8_t byte1, uint8_t byte2);
static esp_err_t queue_v1_command(gdo_v1_command_t command);
static esp_err_t schedule_command(gdo_sched_cmd_args_t *cmd_args, uint32_t time_us);
static esp_err_t schedule_event(gdo_event_type_t event, uint32_t time_us);
static esp_err_t gdo_v1_toggle_cmd(gdo_v1_command_t cmd);
static esp_err_t queue_event(gdo_event_t event);
/******************************** GLOBAL VARIABLES ************************************/
static gdo_event_callback_t g_event_callback;
static gdo_status_t g_status = {
.protocol = 0,
.door = GDO_DOOR_STATE_UNKNOWN,
.light = GDO_LIGHT_STATE_MAX,
.lock = GDO_LOCK_STATE_MAX,
.motion = GDO_MOTION_STATE_MAX,
.motor = GDO_MOTOR_STATE_MAX,
.button = GDO_BUTTON_STATE_MAX,
.battery = GDO_BATT_STATE_UNKNOWN,
.learn = GDO_LEARN_STATE_MAX,
.paired_devices = {GDO_PAIRED_DEVICE_COUNT_UNKNOWN,
GDO_PAIRED_DEVICE_COUNT_UNKNOWN,
GDO_PAIRED_DEVICE_COUNT_UNKNOWN,
GDO_PAIRED_DEVICE_COUNT_UNKNOWN,
GDO_PAIRED_DEVICE_COUNT_UNKNOWN},
.synced = false,
.openings = 0,
.ttc_seconds = 0,
.open_ms = 0,
.close_ms = 0,
.door_position = -1,
.door_target = -1,
.client_id = 0x666,
.rolling_code = 0,
.toggle_only = false,
.last_move_direction = GDO_DOOR_STATE_UNKNOWN,
};
static bool g_protocol_forced;
static gdo_config_t g_config;
static uint32_t g_door_start_moving_ms;
static TaskHandle_t gdo_main_task_handle;
static TaskHandle_t gdo_sync_task_handle;
static QueueHandle_t gdo_tx_queue;
static QueueHandle_t gdo_event_queue;
static esp_timer_handle_t motion_detect_timer;
static esp_timer_handle_t door_position_sync_timer;
static esp_timer_handle_t obst_timer;
static void *g_user_cb_arg;
static uint32_t g_tx_delay_ms = 50;
static portMUX_TYPE gdo_spinlock = portMUX_INITIALIZER_UNLOCKED;
/******************************* PUBLIC API FUNCTIONS **********************************/
/**
* @brief Initializes the GDO driver.
* @param config The configuration for the GDO driver.
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if the config is invalid, ESP_ERR_NO_MEM if memory allocation fails,
* ESP_ERR_INVALID_STATE if the driver is already initialized.
*/
esp_err_t gdo_init(const gdo_config_t *config) {
esp_err_t err = ESP_OK;
if (!config || config->uart_num >= UART_NUM_MAX ||
config->uart_tx_pin >= GPIO_NUM_MAX || config->uart_rx_pin >= GPIO_NUM_MAX) {
return ESP_ERR_INVALID_ARG;
}
if (gdo_tx_queue) { // using this as a proxy for the driver being initialized
return ESP_ERR_INVALID_STATE;
}
g_config = *config;
esp_timer_create_args_t timer_args = {
.callback = motion_detect_timer_cb,
.arg = NULL,
.dispatch_method = ESP_TIMER_TASK,
.name = "motion_detect_timer"
};
err = esp_timer_create(&timer_args, &motion_detect_timer);
if (err != ESP_OK) {
return err;
}
timer_args.callback = door_position_sync_timer_cb;
timer_args.arg = NULL;
timer_args.dispatch_method = ESP_TIMER_TASK;
timer_args.name = "door_position_sync_timer";
err = esp_timer_create(&timer_args, &door_position_sync_timer);
if (err != ESP_OK) {
return err;
}
if (g_config.obst_in_pin >= 0 && !g_config.obst_from_status) {
gpio_config_t io_conf = {0};
io_conf.intr_type = GPIO_INTR_NEGEDGE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL<<g_config.obst_in_pin);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gdo_obstruction_stats_t *obst_stats = (gdo_obstruction_stats_t*)calloc(1, sizeof(gdo_obstruction_stats_t));
if (!obst_stats) {
return ESP_ERR_NO_MEM;
}
err = gpio_install_isr_service(0);
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
return err;
}
err = gpio_isr_handler_add(g_config.obst_in_pin, obst_isr_handler, (void*)obst_stats);
if (err != ESP_OK) {
return err;
}
err = gpio_config(&io_conf);
if (err != ESP_OK) {
return err;
}
timer_args.callback = obst_timer_cb;
timer_args.arg = (void*)obst_stats;
timer_args.dispatch_method = ESP_TIMER_TASK;
timer_args.name = "obst_timer";
err = esp_timer_create(&timer_args, &obst_timer);
if (err != ESP_OK) {
return err;
}
}
// Begin in secplus protocol v1 as its the easiest to detect.
uart_config_t uart_config = {
.baud_rate = 1200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_EVEN,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
};
err = uart_param_config(g_config.uart_num, &uart_config);
if (err != ESP_OK) {
return err;
}
if (g_config.invert_uart) {
err = uart_set_line_inverse(g_config.uart_num, UART_SIGNAL_RXD_INV | UART_SIGNAL_TXD_INV);
if (err != ESP_OK) {
return err;
}
}
err = uart_set_pin(g_config.uart_num, g_config.uart_tx_pin, g_config.uart_rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
if (err != ESP_OK) {
return err;
}
gdo_tx_queue = xQueueCreate(16, sizeof(gdo_tx_message_t));
if (!gdo_tx_queue) {
return ESP_ERR_NO_MEM;
}
ESP_LOGI(TAG, "GDO initilized");
return ESP_OK;
}
/**
* @brief Stops and deletes the GDO driver and resets all state values to defaults.
* @return ESP_OK on success, ESP_ERR_INVALID_STATE if the driver is not initialized.
*/
esp_err_t gdo_deinit(void) {
esp_err_t err = ESP_OK;
if (!gdo_tx_queue) { // using this as a proxy for the driver being initialized
return ESP_ERR_INVALID_STATE;
}
if (gdo_main_task_handle) {
vTaskDelete(gdo_main_task_handle);
gdo_main_task_handle = NULL;
}
if (gdo_sync_task_handle) {
vTaskDelete(gdo_sync_task_handle);
gdo_sync_task_handle = NULL;
}
if (gdo_tx_queue) {
vQueueDelete(gdo_tx_queue);
gdo_tx_queue = NULL;
}
if (gdo_event_queue) {
vQueueDelete(gdo_event_queue);
gdo_event_queue = NULL;
}
if (motion_detect_timer) {
esp_timer_delete(motion_detect_timer);
motion_detect_timer = NULL;
}
if (door_position_sync_timer) {
esp_timer_delete(door_position_sync_timer);
door_position_sync_timer = NULL;
}
if (obst_timer) {
esp_timer_delete(obst_timer);
obst_timer = NULL;
}
g_protocol_forced = false;
g_status.synced = false;
g_status.protocol = 0;
g_status.door = GDO_DOOR_STATE_UNKNOWN;
g_status.light = GDO_LIGHT_STATE_MAX;
g_status.lock = GDO_LOCK_STATE_MAX;
g_status.motion = GDO_MOTION_STATE_MAX;
g_status.motor = GDO_MOTOR_STATE_MAX;
g_status.button = GDO_BUTTON_STATE_MAX;
g_status.battery = GDO_BATT_STATE_UNKNOWN;
g_status.learn = GDO_LEARN_STATE_MAX;
g_status.paired_devices.total_remotes = GDO_PAIRED_DEVICE_COUNT_UNKNOWN;
g_status.paired_devices.total_keypads = GDO_PAIRED_DEVICE_COUNT_UNKNOWN;
g_status.paired_devices.total_wall_controls = GDO_PAIRED_DEVICE_COUNT_UNKNOWN;
g_status.paired_devices.total_accessories = GDO_PAIRED_DEVICE_COUNT_UNKNOWN;
g_status.paired_devices.total_all = GDO_PAIRED_DEVICE_COUNT_UNKNOWN;
g_status.openings = 0;
g_status.ttc_seconds = 0;
g_status.open_ms = 0;
g_status.close_ms = 0;
g_status.door_position = -1;
g_status.door_target = -1;
err = gpio_reset_pin(g_config.uart_tx_pin);
if (err != ESP_OK) {
goto done;
}
err = gpio_reset_pin(g_config.uart_rx_pin);
if (err != ESP_OK) {
goto done;
}
err = uart_driver_delete(g_config.uart_num);
done:
return err;
}
/**
* @brief Starts the GDO driver and the UART.
* @param event_callback The callback function to be called when an event occurs.
* @param user_arg optional user argument to be passed to the callback.
* @return ESP_OK on success, ESP_ERR_NO_MEM if task creation fails, ESP_ERR_INVALID_STATE if the driver is not initialized.
*/
esp_err_t gdo_start(gdo_event_callback_t event_callback, void *user_arg) {
if (!gdo_tx_queue) { // using this as a proxy for the driver being initialized
return ESP_ERR_INVALID_STATE;
}
g_user_cb_arg = user_arg;
esp_err_t err = uart_driver_install(g_config.uart_num, RX_BUFFER_SIZE, 0, 32, &gdo_event_queue, 0);
if (err != ESP_OK) {
return err;
}
uart_flush(g_config.uart_num);
if (xTaskCreate(gdo_main_task, "gdo_main_task", 4096, NULL, 15, &gdo_main_task_handle) != pdPASS) {
return ESP_ERR_NO_MEM;
}
err = gdo_sync();
if (err != ESP_OK) {
return err;
}
g_event_callback = event_callback;
ESP_LOGI(TAG, "GDO Started");
return err;
}
/**
* @brief Gets the current status of the GDO.
* @param status a pointer to the status structure to be filled.
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if status is NULL.
* @note This function is performed in a critical section and should be called with caution.
*/
esp_err_t gdo_get_status(gdo_status_t *status) {
if (!status) {
return ESP_ERR_INVALID_ARG;
}
portENTER_CRITICAL(&gdo_spinlock);
*status = g_status;
portEXIT_CRITICAL(&gdo_spinlock);
return ESP_OK;
}
/**
* @brief Starts the task that syncs the state of the GDO with the controller.
* @return ESP_OK on success, ESP_ERR_NO_MEM if task creation fails, ESP_ERR_NOT_FINISHED if the task is already running,
* ESP_ERR_INVALID_STATE if the driver is not started or already synced.
*/
esp_err_t gdo_sync(void) {
if (!gdo_main_task_handle || g_status.synced) { // using this as a proxy for the driver being started
return ESP_ERR_INVALID_STATE;
}
if (!gdo_sync_task_handle) {
if (xTaskCreate(gdo_sync_task, "gdo_task", 4096, NULL, 15, &gdo_sync_task_handle) != pdPASS) {
return ESP_ERR_NO_MEM;
}
} else {
return ESP_ERR_NOT_FINISHED;
}
return ESP_OK;
}
/**
* @brief Opens the door.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_door_open(void) {
g_status.door_target = 0;
if (g_status.door == GDO_DOOR_STATE_OPENING || g_status.door == GDO_DOOR_STATE_OPEN) {
return ESP_OK;
}
if (g_status.toggle_only) {
// If the door is stopped and the last move was opening, then the toggle command will make the door close.
// So we need to send a toggle command to stop, then toggle again to open.
if (g_status.door == GDO_DOOR_STATE_STOPPED && g_status.last_move_direction == GDO_DOOR_STATE_OPENING) {
gdo_sched_cmd_args_t args = {
.cmd = (uint32_t)GDO_DOOR_ACTION_TOGGLE,
.door_cmd = true,
};
esp_err_t err = schedule_command(&args, 500 * 1000);
if (err != ESP_OK) {
return err;
}
err = schedule_command(&args, 1000 * 1000);
if (err != ESP_OK) {
return err;
}
}
return gdo_door_toggle();
}
return send_door_action(GDO_DOOR_ACTION_OPEN);
}
/**
* @brief Closes the door.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_door_close(void) {
g_status.door_target = 10000;
if (g_status.door == GDO_DOOR_STATE_CLOSING || g_status.door == GDO_DOOR_STATE_CLOSED) {
return ESP_OK;
}
if (g_status.toggle_only) {
return gdo_door_toggle();
}
return send_door_action(GDO_DOOR_ACTION_CLOSE);
}
/**
* @brief Stops the door.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_door_stop(void) {
if (g_status.door == GDO_DOOR_STATE_OPENING || g_status.door == GDO_DOOR_STATE_CLOSING) {
return send_door_action(GDO_DOOR_ACTION_STOP);
}
return ESP_OK;
}
/**
* @brief Toggles the door.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_door_toggle(void) {
if (g_status.door == GDO_DOOR_STATE_OPENING || g_status.door == GDO_DOOR_STATE_CLOSING) {
return gdo_door_stop();
}
return send_door_action(GDO_DOOR_ACTION_TOGGLE);
}
/**
* @brief Moves the door to a specific target position.
* @param target The target position to move the door to, 0-10000.
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if the target is out of range, ESP_ERR_NO_MEM if the queue
* is full, ESP_ERR_INVALID_STATE if the door position or durations are unknown or the door is moving.
*/
esp_err_t gdo_door_move_to_target(uint32_t target) {
esp_err_t err = ESP_OK;
if (target > 10000) {
return ESP_ERR_INVALID_ARG;
}
if (target == 0) {
return gdo_door_open();
}
if (target == 10000) {
return gdo_door_close();
}
if (g_status.door_position < 0 || g_status.close_ms == 0 || g_status.open_ms == 0 ||
g_status.door == GDO_DOOR_STATE_OPENING || g_status.door == GDO_DOOR_STATE_CLOSING) {
ESP_LOGW(TAG, "Unable to move to target, door position or durations are unknown or door is moving");
return ESP_ERR_INVALID_STATE;
}
gdo_door_action_t action = GDO_DOOR_ACTION_MAX;
int delta = g_status.door_position - target;
float duration_ms = 0.0f;
if (delta < 0) {
action = GDO_DOOR_ACTION_CLOSE;
duration_ms = (g_status.close_ms / 10000.f) * -delta;
} else if (delta > 0) {
action = GDO_DOOR_ACTION_OPEN;
duration_ms = (g_status.open_ms / 10000.f) * delta;
} else {
ESP_LOGD(TAG, "Door is already at target %.2f", g_status.door_position / 100.0f);
return ESP_OK;
}
if (duration_ms < 500) {
ESP_LOGW(TAG, "Duration is too short, ignoring move to target");
return ESP_ERR_INVALID_ARG;
}
gdo_sched_cmd_args_t args = {
.cmd = (uint32_t)GDO_DOOR_ACTION_STOP,
.door_cmd = true,
};
err = schedule_command(&args, duration_ms * 1000);
if (err != ESP_OK) {
return err;
}
err = send_door_action(action);
if (err == ESP_OK) {
g_status.door_target = target;
}
return err;
}
/**
* @brief Turns the light on.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_light_on(void) {
esp_err_t err = ESP_OK;
if (g_status.light == GDO_LIGHT_STATE_ON) {
return err;
}
if (g_status.protocol & GDO_PROTOCOL_SEC_PLUS_V1) {
return gdo_v1_toggle_cmd(V1_CMD_TOGGLE_LIGHT_PRESS);
} else {
err = queue_command(GDO_CMD_LIGHT, GDO_LIGHT_ACTION_ON, 0, 0);
if (err == ESP_OK) {
get_status();
}
}
return err;
}
/**
* @brief Turns the light off.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_light_off(void) {
esp_err_t err = ESP_OK;
if (g_status.light == GDO_LIGHT_STATE_OFF) {
return err;
}
if (g_status.protocol & GDO_PROTOCOL_SEC_PLUS_V1) {
return gdo_v1_toggle_cmd(V1_CMD_TOGGLE_LIGHT_PRESS);
} else {
err = queue_command(GDO_CMD_LIGHT, GDO_LIGHT_ACTION_OFF, 0, 0);
if (err == ESP_OK) {
get_status();
}
}
return err;
}
/**
* @brief Toggles the light.
* @return ESP_OK on success, ESP_ERR_NOT_FOUND if current state is unknown,
* ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_light_toggle(void) {
if (g_status.light == GDO_LIGHT_STATE_ON) {
return gdo_light_off();
} else if (g_status.light == GDO_LIGHT_STATE_OFF) {
return gdo_light_on();
} else {
return ESP_ERR_NOT_FOUND;
}
}
/**
* @brief Locks the GDO.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_lock(void) {
if (g_status.lock == GDO_LOCK_STATE_LOCKED) {
return ESP_OK;
}
if (g_status.protocol & GDO_PROTOCOL_SEC_PLUS_V1) {
return gdo_v1_toggle_cmd(V1_CMD_TOGGLE_LOCK_PRESS);
} else {
return queue_command(GDO_CMD_LOCK, GDO_LOCK_ACTION_LOCK, 0, 0);
}
}
/**
* @brief Unlocks the GDO.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_unlock(void) {
if (g_status.lock == GDO_LOCK_STATE_UNLOCKED) {
return ESP_OK;
}
if (g_status.protocol & GDO_PROTOCOL_SEC_PLUS_V1) {
return gdo_v1_toggle_cmd(V1_CMD_TOGGLE_LOCK_PRESS);
} else {
return queue_command(GDO_CMD_LOCK, GDO_LOCK_ACTION_UNLOCK, 0, 0);
}
}
/**
* @brief Toggles the lock state of the GDO.
* @return ESP_OK on success, ESP_ERR_NOT_FOUND if current state is unknown,
* ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
*/
esp_err_t gdo_lock_toggle(void) {
if (g_status.lock == GDO_LOCK_STATE_LOCKED) {
return gdo_unlock();
} else if (g_status.lock == GDO_LOCK_STATE_UNLOCKED) {
return gdo_lock();
} else {
return ESP_ERR_NOT_FOUND;
}
}
/**
* @brief Activates the learn mode on the GDO.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails,
* ESP_ERR_NOT_SUPPORTED if the protocol is secplus v1.
*/
esp_err_t gdo_activate_learn(void) {
if (g_status.protocol != GDO_PROTOCOL_SEC_PLUS_V2) {
return ESP_ERR_NOT_SUPPORTED;
}
esp_err_t err = queue_command(GDO_CMD_LEARN, GDO_LEARN_ACTION_ACTIVATE, 0, 0);
if (err != ESP_OK) {
return err;
}
return get_status();
}
/**
* @brief Deactivates the learn mode on the GDO.
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails,
* ESP_ERR_NOT_SUPPORTED if the protocol is secplus v1.
*/
esp_err_t gdo_deactivate_learn(void) {
if (g_status.protocol != GDO_PROTOCOL_SEC_PLUS_V2) {
return ESP_ERR_NOT_SUPPORTED;
}
esp_err_t err = queue_command(GDO_CMD_LEARN, GDO_LEARN_ACTION_DEACTIVATE, 0, 0);
if (err != ESP_OK) {
return err;
}
return get_status();
}
/**
* @brief Clears the paired devices from the GDO.
* @param type The type of paired devices to clear.
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if the type is invalid, ESP_ERR_NO_MEM if the queue is full,
* ESP_FAIL if the encoding fails, ESP_ERR_NOT_SUPPORTED if the protocol is secplus v1.
*/
esp_err_t gdo_clear_paired_devices(gdo_paired_device_type_t type) {
esp_err_t err = ESP_OK;
if (g_status.protocol != GDO_PROTOCOL_SEC_PLUS_V2) {
return ESP_ERR_NOT_SUPPORTED;
}
if (type >= GDO_PAIRED_DEVICE_TYPE_MAX) {
return ESP_ERR_INVALID_ARG;
}
if (type == GDO_PAIRED_DEVICE_TYPE_ALL) {
err = queue_command(GDO_CMD_CLEAR_PAIRED_DEVICES, (GDO_PAIRED_DEVICE_TYPE_REMOTE - 1), 0, 0);
if (err != ESP_OK) {
return err;
}
err = queue_command(GDO_CMD_CLEAR_PAIRED_DEVICES, (GDO_PAIRED_DEVICE_TYPE_KEYPAD - 1), 0, 0);
if (err != ESP_OK) {
return err;
}
err = queue_command(GDO_CMD_CLEAR_PAIRED_DEVICES, (GDO_PAIRED_DEVICE_TYPE_WALL_CONTROL - 1), 0, 0);
if (err != ESP_OK) {
return err;
}
err = queue_command(GDO_CMD_CLEAR_PAIRED_DEVICES, (GDO_PAIRED_DEVICE_TYPE_ACCESSORY - 1), 0, 0);
if (err != ESP_OK) {
return err;
}
} else {
err = queue_command(GDO_CMD_CLEAR_PAIRED_DEVICES, (type - 1), 0, 0);
if (err != ESP_OK) {
return err;
}
}
// return codes from theses do not indicate command success so skip the rc check
get_status();
get_paired_devices(type);
return err;
}
/**
* @brief Sets the Security+ V2 rolling code.
* @param rolling_code The rolling code to set.
* @return ESP_OK on success, ESP_ERR_INVALID_STATE if the GDO is already synced.
*/
esp_err_t gdo_set_rolling_code(uint32_t rolling_code) {
if (g_status.synced) {
return ESP_ERR_INVALID_STATE;
}
g_status.rolling_code = rolling_code;
return ESP_OK;
}
/**
* @brief Sets the Security+ V2 client id.
* @param client_id The client id to set.
* @return ESP_OK on success, ESP_ERR_INVALID_STATE if the GDO is already synced.
*/
esp_err_t gdo_set_client_id(uint32_t client_id) {
if (g_status.synced) {
return ESP_ERR_INVALID_STATE;
}
g_status.client_id = client_id;
return ESP_OK;
}
/**
* @brief Sets the protocol to use to communicate with the GDO.
* @param protocol The protocol to use.
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if the protocol is invalid,
* ESP_ERR_INVALID_STATE if the protocol is already set.
*/
esp_err_t gdo_set_protocol(gdo_protocol_type_t protocol) {
if (g_status.protocol > 0 && g_status.protocol < GDO_PROTOCOL_MAX) {
return ESP_ERR_INVALID_STATE;
}
if (protocol < GDO_PROTOCOL_MAX) {
g_status.protocol = protocol;
g_protocol_forced = protocol > 0;
return ESP_OK;
}
return ESP_ERR_INVALID_ARG;
}
/**
* @brief Sets the time the door takes to open from fully closed in milliseconds.
* @param ms The time the door takes to open from fully closed in milliseconds.
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if the ms is invalid.
*/
esp_err_t gdo_set_open_duration(uint16_t ms) {
if (ms < 1000 || ms > 65000) {
return ESP_ERR_INVALID_ARG;
}
g_status.open_ms = ms;
return ESP_OK;
}
/**
* @brief Sets the time the door takes to close from fully open in milliseconds.
* @param ms The time the door takes to close from fully open in milliseconds.
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if the ms is invalid.
*/
esp_err_t gdo_set_close_duration(uint16_t ms) {
if (ms < 1000 || ms > 65000) {
return ESP_ERR_INVALID_ARG;
}
g_status.close_ms = ms;
return ESP_OK;
}
/**
* @brief Sets the minimum time in milliseconds to wait between sending consecutive commands.
* @param ms The minimum time in milliseconds.
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if the time is invalid.
*/
esp_err_t gdo_set_min_command_interval(uint32_t ms) {
if (ms < 50) {
ESP_LOGE(TAG, "Invalid minimum command interval: %" PRIu32, ms);
return ESP_ERR_INVALID_ARG;
}
g_tx_delay_ms = ms;
return ESP_OK;
}
/**
* @brief Enables or disables the toggle only mode, may be required by openers that do not have obstruction sensors connected.
* @param toggle_only true to enable toggle only mode, false to disable.
*/
void gdo_set_toggle_only(bool toggle_only) {
g_status.toggle_only = toggle_only;
}
/************************************ LOCAL FUNCTIONS ************************************/
/**
* @brief This task stated by `gdo_sync()` to sync the state of the GDO with the controller.
* @details This task will query the GDO for the current state of the door, and device information
* in a loop until timeout or all information is received. Once complete an event of GDO_SYNC_COMPLETE
* is queued and The task will then delete itself. The status of the sync is stored in the g_status.synced.
*/
static void gdo_sync_task(void* arg) {
bool synced = true;
if (g_status.protocol != GDO_PROTOCOL_SEC_PLUS_V2) {
uart_set_baudrate(g_config.uart_num, 1200);
uart_set_parity(g_config.uart_num, UART_PARITY_EVEN);
uart_flush(g_config.uart_num);
xQueueReset(gdo_event_queue);
// Delay forever if there is a smart panel connected to allow it to come online and sync before we do anything.
ulTaskNotifyTake(pdTRUE, g_status.protocol == GDO_PROTOCOL_SEC_PLUS_V1_WITH_SMART_PANEL ? portMAX_DELAY : pdMS_TO_TICKS(2500));
if (g_status.door == GDO_DOOR_STATE_UNKNOWN) {
ESP_LOGW(TAG, "V1 panel not found, trying emulation");
esp_timer_create_args_t timer_args = {
.callback = v1_status_timer_cb,
.arg = NULL,
.dispatch_method = ESP_TIMER_TASK,
.name = "v1_status_timer"
};
esp_timer_handle_t v1_status_timer;
if (esp_timer_create(&timer_args, &v1_status_timer) != ESP_OK) {
ESP_LOGE(TAG, "Failed to create V1 status timer");
synced = false;
goto done;
} else {
uart_flush(g_config.uart_num);
xQueueReset(gdo_event_queue);
esp_timer_start_periodic(v1_status_timer, 250 * 1000);
}
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(5000));
if (g_status.door == GDO_DOOR_STATE_UNKNOWN && !g_protocol_forced) {
ESP_LOGW(TAG, "secplus V1 panel emulation failed, trying secplus V2 panel emulation");
esp_timer_stop(v1_status_timer);
esp_timer_delete(v1_status_timer);
} else {
goto done;
}
} else {
ESP_LOGI(TAG, "V1 panel found");
g_status.protocol = GDO_PROTOCOL_SEC_PLUS_V1_WITH_SMART_PANEL;
goto done;
}
}
uint32_t timeout = esp_timer_get_time() / 1000 + 5000;
uint8_t sync_stage = 0;
g_status.protocol = GDO_PROTOCOL_SEC_PLUS_V2;
uart_set_baudrate(g_config.uart_num, 9600);
uart_set_parity(g_config.uart_num, UART_PARITY_DISABLE);
uart_flush(g_config.uart_num);
xQueueReset(gdo_event_queue);
for (;;) {
if ((esp_timer_get_time() / 1000) > timeout) {
synced = false;
break;
}
vTaskDelay(pdMS_TO_TICKS((g_tx_delay_ms * 2 > 250) ? g_tx_delay_ms * 2 : 250));
if (g_status.door == GDO_DOOR_STATE_UNKNOWN) {
ESP_LOGV(TAG, "SYNC TASK: Getting status");
get_status();
continue;
} else if (sync_stage < 1) {
sync_stage = 1;
timeout += 1000;
}
if (g_status.openings == 0) {
ESP_LOGI(TAG, "SYNC TASK: Getting openings");
get_openings();
continue;
} else if (sync_stage < 2) {
sync_stage = 2;
timeout += 1000;
}
if (g_status.paired_devices.total_all == GDO_PAIRED_DEVICE_COUNT_UNKNOWN) {
ESP_LOGI(TAG, "SYNC TASK: Getting all paired devices");
get_paired_devices(GDO_PAIRED_DEVICE_TYPE_ALL);
continue;
} else if (sync_stage < 3) {
sync_stage = 3;
timeout += 1000;
}
if (g_status.paired_devices.total_remotes == GDO_PAIRED_DEVICE_COUNT_UNKNOWN) {
ESP_LOGI(TAG, "SYNC TASK: Getting remotes");
get_paired_devices(GDO_PAIRED_DEVICE_TYPE_REMOTE);
continue;
} else if (sync_stage < 4) {
sync_stage = 4;
timeout += 1000;
}
if (g_status.paired_devices.total_keypads == GDO_PAIRED_DEVICE_COUNT_UNKNOWN) {
ESP_LOGI(TAG, "SYNC TASK: Getting keypads");
get_paired_devices(GDO_PAIRED_DEVICE_TYPE_KEYPAD);
continue;
} else if (sync_stage < 5) {
sync_stage = 5;
timeout += 1000;
}
if (g_status.paired_devices.total_wall_controls == GDO_PAIRED_DEVICE_COUNT_UNKNOWN) {
ESP_LOGI(TAG, "SYNC TASK: Getting wall controls");
get_paired_devices(GDO_PAIRED_DEVICE_TYPE_WALL_CONTROL);
continue;
} else if (sync_stage < 6) {
sync_stage = 6;
timeout += 1000;
}
if (g_status.paired_devices.total_accessories == GDO_PAIRED_DEVICE_COUNT_UNKNOWN) {
ESP_LOGI(TAG, "SYNC TASK: Getting accessories");
get_paired_devices(GDO_PAIRED_DEVICE_TYPE_ACCESSORY);
continue;
}
queue_event((gdo_event_t){GDO_EVENT_PAIRED_DEVICES_UPDATE});
break;
}
done:
g_status.synced = synced;
if (synced) {
if (g_status.protocol & GDO_PROTOCOL_SEC_PLUS_V1) {
g_status.toggle_only = true;
}
} else if (!g_protocol_forced) {
g_status.protocol = 0;
}
queue_event((gdo_event_t){GDO_EVENT_SYNC_COMPLETE});
gdo_sync_task_handle = NULL;
vTaskDelete(NULL);
}
/**
* @brief Handles the obstruction interrupt and increments the count in the stats struct.
*/
static void IRAM_ATTR obst_isr_handler(void* arg) {
gdo_obstruction_stats_t *stats = (gdo_obstruction_stats_t*)arg;
++stats->count;
}
/******************************* TIMER CALLBACKS ************************************/
/**
* @brief Runs every ~50ms anch checks the count of obstruction interrupts.
* @details 3 or more interrupts in 50ms is considered clear, 0 with the pin low is asleep,
* and 0 with the pin high is obstructed.
* When the obstruction state changes an event of GDO_EVENT_OBST is queued.
*/
static void obst_timer_cb(void* arg) {
gdo_obstruction_stats_t *stats = (gdo_obstruction_stats_t*)arg;
int64_t micros_now = esp_timer_get_time();
gdo_obstruction_state_t obs_state = GDO_OBSTRUCTION_STATE_MAX;
if (stats->count > 3) {
stats->sleep_micros = 0;
obs_state = GDO_OBSTRUCTION_STATE_CLEAR;
} else if (stats->count == 0) {
if (!gpio_get_level(g_config.obst_in_pin)) {
stats->sleep_micros = micros_now;
obs_state = GDO_OBSTRUCTION_STATE_CLEAR;
} else if (micros_now - stats->sleep_micros > 700000) {
obs_state = GDO_OBSTRUCTION_STATE_OBSTRUCTED;
}
}
stats->count = 0;
if (obs_state != GDO_OBSTRUCTION_STATE_MAX && obs_state != g_status.obstruction) {
update_obstruction_state(obs_state);
queue_event((gdo_event_t){GDO_EVENT_OBST});
}
}