src.nth.io/

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Hoersten <[email protected]>2021-03-12 11:56:07 -0600
committerLuke Hoersten <[email protected]>2021-03-12 11:56:07 -0600
commitc6d3d96ed6a659f7a3c1f616caebe325a3d139bd (patch)
tree470016b10b16e31a8190da4fb769725b6bd9898a
parent2d08f681e6e53b71a295b0ab05155f4315fea76f (diff)
Broke out services into their own files.
-rw-r--r--.gitignore1
-rw-r--r--main/CMakeLists.txt4
-rw-r--r--main/app_main.c249
-rw-r--r--main/include/bell.h27
-rw-r--r--main/include/event_queue.h31
-rw-r--r--main/include/lock.h29
-rw-r--r--main/src/bell.c86
-rw-r--r--main/src/event_queue.c87
-rw-r--r--main/src/lock.c125
9 files changed, 400 insertions, 239 deletions
diff --git a/.gitignore b/.gitignore
index 44f40a2..6027692 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
sdkconfig
sdkconfig.old
build/*
+esp-homekit-intercom.code-workspace
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index 0e1f552..1237afe 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -1,4 +1,4 @@
idf_component_register(
- SRCS "app_main.c"
- INCLUDE_DIRS ""
+ SRCS "src/event_queue.c" "src/lock.c" "src/bell.c" "app_main.c"
+ INCLUDE_DIRS "include"
)
diff --git a/main/app_main.c b/main/app_main.c
index 1feb9dd..22e511f 100644
--- a/main/app_main.c
+++ b/main/app_main.c
@@ -18,7 +18,9 @@
#include <app_wifi.h>
#include <app_hap_setup_payload.h>
-static const char *TAG = "HAP Intercom";
+#include <lock.h>
+#include <bell.h>
+#include <event_queue.h>
#define INTERCOM_TASK_PRIORITY 1
#define INTERCOM_TASK_STACKSIZE 4 * 1024
@@ -26,90 +28,9 @@ static const char *TAG = "HAP Intercom";
#define RESET_NETWORK_BUTTON_TIMEOUT 3
-#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 hap_val_t HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS = {.u = 0};
-
-#define ESP_INTR_FLAG_DEFAULT 0
-
-static const uint8_t INTERCOM_EVENT_QUEUE_BELL = 1;
-static const uint8_t INTERCOM_EVENT_QUEUE_UNLOCK = 2;
-static const uint8_t INTERCOM_EVENT_QUEUE_LOCK = 3;
-static const uint8_t INTERCOM_EVENT_QUEUE_LOCK_TIMEOUT = 4;
-
/* static uint8_t tlv8buff[128];
static hap_data_val_t null_tlv8 = {.buf = &tlv8buff, .buflen = 127}; */
-volatile uint32_t avg_bell_val = 0;
-volatile bool is_intercom_bell_blocked = false;
-static xQueueHandle intercom_event_queue = NULL;
-static TimerHandle_t intercom_lock_timer = NULL; // lock the door when timer triggered
-static TimerHandle_t intercom_bell_timer = NULL; // ignore new bells until timer triggered
-
-static bool is_bell_ringing(int val)
-{
- return 1.2 < val && val < 2.5;
-}
-
-/**
- * @brief the recover intercom bell gpio interrupt function
- */
-static void IRAM_ATTR intercom_bell_isr(void *arg)
-{
- if (!is_intercom_bell_blocked && is_bell_ringing(adc1_get_raw(CONFIG_HOMEKIT_INTERCOM_BELL_ADC1_CHANNEL)))
- {
- xQueueSendFromISR(intercom_event_queue, (void *)&INTERCOM_EVENT_QUEUE_BELL, NULL);
- is_intercom_bell_blocked = true;
- }
-}
-
-/**
- * Enable a GPIO Pin for Intercom Bell
- */
-static void intercom_bell_init(uint32_t key_gpio_pin)
-{
- gpio_config_t io_conf;
-
- io_conf.intr_type = GPIO_INTR_POSEDGE; /* Interrupt for rising edge */
- io_conf.pin_bit_mask = 1 << key_gpio_pin; /* Bit mask of the pins */
- io_conf.mode = GPIO_MODE_INPUT; /* 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 */
-
- gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); /* Install gpio isr service */
- gpio_isr_handler_add(key_gpio_pin, intercom_bell_isr, (void *)key_gpio_pin); /* Hook isr handler for specified gpio pin */
-
- // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#_CPPv425adc1_config_channel_atten14adc1_channel_t11adc_atten_t
- adc1_config_width(ADC_WIDTH_BIT_12);
- adc1_config_channel_atten(CONFIG_HOMEKIT_INTERCOM_BELL_ADC1_CHANNEL, ADC_ATTEN_DB_11);
-}
-
-/**
- * Enable a GPIO Pin for Intercom Lock
- */
-static void intercom_lock_init(uint32_t key_gpio_pin)
-{
- 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 */
-}
-
-/**
- * Enable a GPIO Pin for LED
- */
static void led_init(uint32_t key_gpio_pin)
{
gpio_config_t io_conf;
@@ -123,28 +44,6 @@ static void led_init(uint32_t key_gpio_pin)
gpio_config(&io_conf); /* Set the GPIO configuration */
}
-/**
- * Initialize the Intercom Hardware. Here, we just enebale the Intercom Bell detection.
- */
-void intercom_hardware_init(gpio_num_t reset_gpio_num, gpio_num_t intercom_bell_gpio_num, gpio_num_t intercom_lock_gpio_num, gpio_num_t led_gpio_num)
-{
- int queue_len = 4;
- int queue_item_size = sizeof(uint8_t);
-
- intercom_event_queue = xQueueCreate(queue_len, queue_item_size);
- if (intercom_event_queue != NULL)
- {
- /* reset_init(reset_gpio_num); */
- intercom_bell_init(intercom_bell_gpio_num);
- intercom_lock_init(intercom_lock_gpio_num);
- led_init(led_gpio_num);
- }
-}
-
-/* Mandatory identify routine for the accessory.
- * In a real accessory, something like LED blink should be implemented
- * got visual identification
- */
static int intercom_identify(hap_acc_t *ha)
{
ESP_LOGI(TAG, "Accessory identified");
@@ -160,89 +59,6 @@ static int intercom_identify(hap_acc_t *ha)
return HAP_SUCCESS;
}
-static void intercom_bell_ring(hap_char_t *intercom_bell_current_state)
-{
- ESP_LOGI(TAG, "Intercom bell ring event processed");
-
- hap_char_update_val(intercom_bell_current_state, &HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS);
-
- xTimerReset(intercom_bell_timer, 10);
-}
-
-static void intercom_unlock(hap_char_t *intercom_lock_current_state)
-{
- 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);
-}
-
-static void intercom_lock(hap_char_t *intercom_lock_current_state)
-{
- 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);
-}
-
-static void intercom_lock_timeout(hap_char_t *intercom_lock_target_state)
-{
- ESP_LOGI(TAG, "Intercom lock timeout event processed");
-
- xQueueSendToBack(intercom_event_queue, (void *)&INTERCOM_EVENT_QUEUE_LOCK, 10);
- hap_val_t target_lock_secured = {.u = HAP_LOCK_TARGET_STATE_SECURED};
- hap_char_update_val(intercom_lock_target_state, &target_lock_secured);
-}
-
-static void intercom_bell_timer_cb(TimerHandle_t timer)
-{
- ESP_LOGI(TAG, "Intercom bell timer fired");
- is_intercom_bell_blocked = false;
-}
-
-static 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:
- xQueueSendToBack(intercom_event_queue, (void *)&INTERCOM_EVENT_QUEUE_UNLOCK, 10);
- break;
- case HAP_LOCK_TARGET_STATE_SECURED:
- xQueueSendToBack(intercom_event_queue, (void *)&INTERCOM_EVENT_QUEUE_LOCK, 10);
- 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;
-}
-
-static void intercom_lock_timer_cb(TimerHandle_t timer)
-{
- ESP_LOGI(TAG, "Intercom lock timer fired - event queued");
-
- xQueueSendToBack(intercom_event_queue, (void *)&INTERCOM_EVENT_QUEUE_LOCK_TIMEOUT, 10);
-}
-
-/*The main thread for handling the Intercom Accessory */
static void intercom_thread_entry(void *p)
{
hap_init(HAP_TRANSPORT_WIFI); /* Initialize the HAP core */
@@ -268,26 +84,18 @@ static void intercom_thread_entry(void *p)
uint8_t product_data[] = {'E', 'S', 'P', '3', '2', 'H', 'A', 'P'};
hap_acc_add_product_data(intercom_accessory, product_data, sizeof(product_data));
- hap_serv_t *intercom_bell_service = hap_serv_doorbell_create(0);
- hap_serv_add_char(intercom_bell_service, hap_char_name_create("Intercom Bell"));
- hap_char_t *intercom_bell_current_state = hap_serv_get_char_by_uuid(intercom_bell_service, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT);
-
- 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"));
- hap_char_t *intercom_lock_current_state = hap_serv_get_char_by_uuid(intercom_lock_service, HAP_CHAR_UUID_LOCK_CURRENT_STATE);
- hap_char_t *intercom_lock_target_state = hap_serv_get_char_by_uuid(intercom_lock_service, HAP_CHAR_UUID_LOCK_TARGET_STATE);
-
- /* Get pointer to the intercom in use characteristic which we need to monitor for state changes */
- hap_serv_set_write_cb(intercom_lock_service, intercom_lock_write_cb); /* Set the write callback for the service */
+ if (!intercom_event_queue_init())
+ {
+ ESP_LOGI(TAG, "Failed to initialize event queue");
+ return;
+ }
- hap_acc_add_serv(intercom_accessory, intercom_bell_service);
- hap_acc_add_serv(intercom_accessory, intercom_lock_service);
+ hap_acc_add_serv(intercom_accessory, intercom_bell_init(CONFIG_HOMEKIT_INTERCOM_BELL_GPIO_PIN));
+ hap_acc_add_serv(intercom_accessory, intercom_lock_init(CONFIG_HOMEKIT_INTERCOM_LOCK_GPIO_PIN));
+ led_init(CONFIG_HOMEKIT_INTERCOM_LED_GPIO_PIN);
hap_add_accessory(intercom_accessory); /* Add the Accessory to the HomeKit Database */
- /* Initialize the appliance specific hardware. This enables out-in-use detection */
- intercom_hardware_init(CONFIG_HOMEKIT_INTERCOM_WIFI_RESET_GPIO_PIN, CONFIG_HOMEKIT_INTERCOM_BELL_GPIO_PIN, CONFIG_HOMEKIT_INTERCOM_LOCK_GPIO_PIN, CONFIG_HOMEKIT_INTERCOM_LED_GPIO_PIN);
-
/* For production accessories, the setup code shouldn't be programmed on to
* the device. Instead, the setup info, derived from the setup code must
* be used. Use the factory_nvs_gen utility to generate this data and then
@@ -317,40 +125,7 @@ static void intercom_thread_entry(void *p)
hap_start(); /* After all the initializations are done, start the HAP core */
app_wifi_start(portMAX_DELAY); /* Start Wi-Fi */
- intercom_lock_timer = xTimerCreate("intercom_lock_timer", pdMS_TO_TICKS(CONFIG_HOMEKIT_INTERCOM_LOCK_TIMEOUT), pdFALSE, 0, intercom_lock_timer_cb);
- intercom_bell_timer = xTimerCreate("intercom_bell_timer", pdMS_TO_TICKS(CONFIG_HOMEKIT_INTERCOM_LOCK_TIMEOUT), pdFALSE, 0, intercom_bell_timer_cb);
-
- /* Listen for intercom bell state change events. Other read/write functionality will be handled by the HAP Core. When the
- * intercom bell in Use GPIO goes low, it means intercom bell is not ringing. When the Intercom in Use GPIO goes high, it means
- * the intercom bell is ringing. Applications can define own logic as per their hardware.
- */
- uint8_t intercom_event_queue_item = INTERCOM_EVENT_QUEUE_LOCK;
-
- while (1)
- {
- if (xQueueReceive(intercom_event_queue, &intercom_event_queue_item, portMAX_DELAY) == pdFALSE)
- {
- ESP_LOGI(TAG, "Intercom event queue trigger FAIL");
- }
- else
- {
- switch (intercom_event_queue_item)
- {
- case INTERCOM_EVENT_QUEUE_BELL:
- intercom_bell_ring(intercom_bell_current_state);
- break;
- case INTERCOM_EVENT_QUEUE_UNLOCK:
- intercom_unlock(intercom_lock_current_state);
- break;
- case INTERCOM_EVENT_QUEUE_LOCK:
- intercom_lock(intercom_lock_current_state);
- break;
- case INTERCOM_EVENT_QUEUE_LOCK_TIMEOUT:
- intercom_lock_timeout(intercom_lock_target_state);
- break;
- }
- }
- }
+ intercom_event_queue_run();
}
void app_main()
diff --git a/main/include/bell.h b/main/include/bell.h
new file mode 100644
index 0000000..9dded29
--- /dev/null
+++ b/main/include/bell.h
@@ -0,0 +1,27 @@
+#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>
+
+bool is_bell_ringing(int val);
+
+void IRAM_ATTR intercom_bell_isr(void *arg);
+
+void intercom_bell_ring();
+
+void intercom_bell_timer_cb(TimerHandle_t timer);
+
+hap_serv_t *intercom_bell_init(uint32_t key_gpio_pin);
diff --git a/main/include/event_queue.h b/main/include/event_queue.h
new file mode 100644
index 0000000..c101c36
--- /dev/null
+++ b/main/include/event_queue.h
@@ -0,0 +1,31 @@
+#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>
+
+static const char *TAG = "HAP Intercom";
+
+void intercom_event_queue_bell_ring();
+
+void intercom_event_queue_lock_unsecure();
+
+void intercom_event_queue_lock_secure();
+
+void intercom_event_queue_lock_timeout();
+
+void intercom_event_queue_run();
+
+bool intercom_event_queue_init();
diff --git a/main/include/lock.h b/main/include/lock.h
new file mode 100644
index 0000000..983180c
--- /dev/null
+++ b/main/include/lock.h
@@ -0,0 +1,29 @@
+#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>
+
+void intercom_lock_unsecure();
+
+void intercom_lock_secure();
+
+void intercom_lock_timeout();
+
+int intercom_lock_write_cb(hap_write_data_t write_data[], int count, void *serv_priv, void *write_priv);
+
+void intercom_lock_timer_cb(TimerHandle_t timer);
+
+hap_serv_t *intercom_lock_init(uint32_t key_gpio_pin);
diff --git a/main/src/bell.c b/main/src/bell.c
new file mode 100644
index 0000000..9467bb8
--- /dev/null
+++ b/main/src/bell.c
@@ -0,0 +1,86 @@
+#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 <bell.h>
+
+#define ESP_INTR_FLAG_DEFAULT 0
+
+static hap_val_t HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS = {.u = 0};
+
+volatile bool is_intercom_bell_blocked;
+static TimerHandle_t intercom_bell_timer; // ignore new bells until timer triggered
+static hap_char_t *intercom_bell_current_state;
+
+bool is_bell_ringing(int val)
+{
+ return 1.2 < val && val < 2.5;
+}
+
+void IRAM_ATTR intercom_bell_isr(void *arg)
+{
+ if (!is_intercom_bell_blocked && is_bell_ringing(adc1_get_raw(CONFIG_HOMEKIT_INTERCOM_BELL_ADC1_CHANNEL)))
+ {
+ intercom_event_queue_bell_ring();
+ is_intercom_bell_blocked = true;
+ }
+}
+
+void intercom_bell_ring()
+{
+ ESP_LOGI(TAG, "Intercom bell ring event processed");
+
+ hap_char_update_val(intercom_bell_current_state, &HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS);
+
+ xTimerReset(intercom_bell_timer, 10);
+}
+
+void intercom_bell_timer_cb(TimerHandle_t timer)
+{
+ ESP_LOGI(TAG, "Intercom bell timer fired");
+ is_intercom_bell_blocked = false;
+}
+
+hap_serv_t *intercom_bell_init(uint32_t key_gpio_pin)
+{
+ is_intercom_bell_blocked = false;
+ intercom_bell_timer = xTimerCreate("intercom_bell_timer", pdMS_TO_TICKS(CONFIG_HOMEKIT_INTERCOM_LOCK_TIMEOUT), pdFALSE, 0, intercom_bell_timer_cb);
+
+ hap_serv_t *intercom_bell_service = hap_serv_doorbell_create(0);
+ hap_serv_add_char(intercom_bell_service, hap_char_name_create("Intercom Bell"));
+ intercom_bell_current_state = hap_serv_get_char_by_uuid(intercom_bell_service, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT);
+
+ gpio_config_t io_conf;
+
+ io_conf.intr_type = GPIO_INTR_POSEDGE; /* Interrupt for rising edge */
+ io_conf.pin_bit_mask = 1 << key_gpio_pin; /* Bit mask of the pins */
+ io_conf.mode = GPIO_MODE_INPUT; /* 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 */
+
+ gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); /* Install gpio isr service */
+ gpio_isr_handler_add(key_gpio_pin, intercom_bell_isr, (void *)key_gpio_pin); /* Hook isr handler for specified gpio pin */
+
+ // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#_CPPv425adc1_config_channel_atten14adc1_channel_t11adc_atten_t
+ adc1_config_width(ADC_WIDTH_BIT_12);
+ adc1_config_channel_atten(CONFIG_HOMEKIT_INTERCOM_BELL_ADC1_CHANNEL, ADC_ATTEN_DB_11);
+
+ return intercom_bell_service;
+}
diff --git a/main/src/event_queue.c b/main/src/event_queue.c
new file mode 100644
index 0000000..113cc56
--- /dev/null
+++ b/main/src/event_queue.c
@@ -0,0 +1,87 @@
+#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 <lock.h>
+#include <bell.h>
+#include <event_queue.h>
+
+static const int INTERCOM_EVENT_QUEUE_BELL_RING = 1;
+static const int INTERCOM_EVENT_QUEUE_LOCK_UNSECURE = 2;
+static const int INTERCOM_EVENT_QUEUE_LOCK_SECURE = 3;
+static const int INTERCOM_EVENT_QUEUE_LOCK_TIMEOUT = 4;
+
+static xQueueHandle intercom_event_queue = NULL;
+
+void intercom_event_queue_bell_ring()
+{
+ xQueueSendFromISR(intercom_event_queue, (void *)&INTERCOM_EVENT_QUEUE_BELL_RING, NULL);
+}
+
+void intercom_event_queue_lock_unsecure()
+{
+ xQueueSendToBack(intercom_event_queue, (void *)&INTERCOM_EVENT_QUEUE_LOCK_UNSECURE, 10);
+}
+
+void intercom_event_queue_lock_secure()
+{
+ xQueueSendToBack(intercom_event_queue, (void *)&INTERCOM_EVENT_QUEUE_LOCK_SECURE, 10);
+}
+
+void intercom_event_queue_lock_timeout()
+{
+ xQueueSendToBack(intercom_event_queue, (void *)&INTERCOM_EVENT_QUEUE_LOCK_TIMEOUT, 10);
+}
+
+void intercom_event_queue_run()
+{
+ uint8_t intercom_event_queue_item = INTERCOM_EVENT_QUEUE_LOCK_SECURE;
+
+ while (1)
+ {
+ if (xQueueReceive(intercom_event_queue, &intercom_event_queue_item, portMAX_DELAY) == pdFALSE)
+ {
+ ESP_LOGI(TAG, "Intercom event queue trigger FAIL");
+ }
+ else
+ {
+ switch (intercom_event_queue_item)
+ {
+ case INTERCOM_EVENT_QUEUE_BELL_RING:
+ intercom_bell_ring();
+ break;
+ case INTERCOM_EVENT_QUEUE_LOCK_UNSECURE:
+ intercom_lock_unsecure();
+ break;
+ case INTERCOM_EVENT_QUEUE_LOCK_SECURE:
+ intercom_lock_secure();
+ break;
+ case INTERCOM_EVENT_QUEUE_LOCK_TIMEOUT:
+ intercom_lock_timeout();
+ break;
+ }
+ }
+ }
+}
+
+bool intercom_event_queue_init()
+{
+ int queue_len = 4;
+ int queue_item_size = sizeof(uint8_t);
+ intercom_event_queue = xQueueCreate(queue_len, queue_item_size);
+ return intercom_event_queue != NULL;
+}
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;
+}