src.nth.io/

summaryrefslogtreecommitdiff
path: root/main/src/lock.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/lock.c')
-rw-r--r--main/src/lock.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/main/src/lock.c b/main/src/lock.c
new file mode 100644
index 0000000..3d01383
--- /dev/null
+++ b/main/src/lock.c
@@ -0,0 +1,125 @@
+#include <stdio.h>
+#include <string.h>
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+#include <freertos/timers.h>
+#include <freertos/queue.h>
+#include <esp_log.h>
+#include <driver/gpio.h>
+#include <driver/adc.h>
+
+#include <hap.h>
+
+#include <hap_apple_servs.h>
+#include <hap_apple_chars.h>
+
+#include <app_wifi.h>
+#include <app_hap_setup_payload.h>
+
+#include <event_queue.h>
+#include <lock.h>
+
+#define INTERCOM_LOCK_GPIO_LOCKED 0
+#define INTERCOM_LOCK_GPIO_UNLOCKED 1
+
+#define HAP_LOCK_TARGET_STATE_UNSECURED 0
+#define HAP_LOCK_TARGET_STATE_SECURED 1
+
+static hap_val_t HAP_LOCK_CURRENT_STATE_UNSECURED = {.u = 0};
+static hap_val_t HAP_LOCK_CURRENT_STATE_SECURED = {.u = 1};
+
+static TimerHandle_t intercom_lock_timer = NULL; // lock the door when timer triggered
+static hap_char_t *intercom_lock_current_state;
+static hap_char_t *intercom_lock_target_state;
+
+void intercom_lock_unsecure()
+{
+ ESP_LOGI(TAG, "Intercom unlock event processed");
+
+ gpio_set_level(CONFIG_HOMEKIT_INTERCOM_LOCK_GPIO_PIN, INTERCOM_LOCK_GPIO_UNLOCKED);
+ hap_char_update_val(intercom_lock_current_state, &HAP_LOCK_CURRENT_STATE_UNSECURED);
+
+ xTimerReset(intercom_lock_timer, 10);
+}
+
+void intercom_lock_secure()
+{
+ ESP_LOGI(TAG, "Intercom lock event processed");
+
+ gpio_set_level(CONFIG_HOMEKIT_INTERCOM_LOCK_GPIO_PIN, INTERCOM_LOCK_GPIO_LOCKED);
+ hap_char_update_val(intercom_lock_current_state, &HAP_LOCK_CURRENT_STATE_SECURED);
+}
+
+void intercom_lock_timeout()
+{
+ ESP_LOGI(TAG, "Intercom lock timeout event processed");
+
+ intercom_event_queue_lock_secure();
+ hap_val_t target_lock_secured = {.u = HAP_LOCK_TARGET_STATE_SECURED};
+ hap_char_update_val(intercom_lock_target_state, &target_lock_secured);
+}
+
+int intercom_lock_write_cb(hap_write_data_t write_data[], int count, void *serv_priv, void *write_priv)
+{
+ int i, ret = HAP_SUCCESS;
+ hap_write_data_t *write;
+ for (i = 0; i < count; i++)
+ {
+ write = &write_data[i];
+ if (!strcmp(hap_char_get_type_uuid(write->hc), HAP_CHAR_UUID_LOCK_TARGET_STATE))
+ {
+ ESP_LOGI(TAG, "Received Write. Intercom lock %d", write->val.u);
+
+ switch (write->val.u)
+ {
+ case HAP_LOCK_TARGET_STATE_UNSECURED:
+ intercom_event_queue_lock_unsecure();
+ break;
+ case HAP_LOCK_TARGET_STATE_SECURED:
+ intercom_event_queue_lock_secure();
+ break;
+ }
+
+ /* Update target state */
+ hap_char_update_val(write->hc, &(write->val));
+ *(write->status) = HAP_STATUS_SUCCESS;
+ }
+ else
+ {
+ *(write->status) = HAP_STATUS_RES_ABSENT;
+ }
+ }
+ return ret;
+}
+
+void intercom_lock_timer_cb(TimerHandle_t timer)
+{
+ ESP_LOGI(TAG, "Intercom lock timer fired - event queued");
+
+ intercom_event_queue_lock_timeout();
+}
+
+hap_serv_t *intercom_lock_init(uint32_t key_gpio_pin)
+{
+ hap_serv_t *intercom_lock_service = hap_serv_lock_mechanism_create(HAP_LOCK_CURRENT_STATE_SECURED.u, HAP_LOCK_TARGET_STATE_SECURED);
+ hap_serv_add_char(intercom_lock_service, hap_char_name_create("Intercom Lock"));
+
+ intercom_lock_current_state = hap_serv_get_char_by_uuid(intercom_lock_service, HAP_CHAR_UUID_LOCK_CURRENT_STATE);
+ intercom_lock_target_state = hap_serv_get_char_by_uuid(intercom_lock_service, HAP_CHAR_UUID_LOCK_TARGET_STATE);
+
+ hap_serv_set_write_cb(intercom_lock_service, intercom_lock_write_cb); /* Set the write callback for the service */
+
+ intercom_lock_timer = xTimerCreate("intercom_lock_timer", pdMS_TO_TICKS(CONFIG_HOMEKIT_INTERCOM_LOCK_TIMEOUT), pdFALSE, 0, intercom_lock_timer_cb);
+
+ gpio_config_t io_conf;
+
+ io_conf.intr_type = GPIO_INTR_DISABLE; /* Disable interrupt */
+ io_conf.pin_bit_mask = 1 << key_gpio_pin; /* Bit mask of the pins */
+ io_conf.mode = GPIO_MODE_OUTPUT; /* Set as input mode */
+ io_conf.pull_up_en = GPIO_PULLUP_DISABLE; /* Disable internal pull-up */
+ io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE; /* Enable internal pull-down */
+
+ gpio_config(&io_conf); /* Set the GPIO configuration */
+
+ return intercom_lock_service;
+}