main/app_main.c
author Luke Hoersten <luke@hoersten.org>
Sun, 28 Feb 2021 19:37:24 -0600
changeset 0 31a65edead29
child 1 289a6264a47e
permissions -rw-r--r--
Initial commit.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     1
/*
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     2
 * ESPRESSIF MIT License
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     3
 *
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     4
 * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     5
 *
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     6
 * Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case,
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     7
 * it is free of charge, to any person obtaining a copy of this software and associated
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     8
 * documentation files (the "Software"), to deal in the Software without restriction, including
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     9
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    10
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    11
 * to do so, subject to the following conditions:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    12
 *
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    13
 * The above copyright notice and this permission notice shall be included in all copies or
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    14
 * substantial portions of the Software.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    15
 *
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    18
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    19
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    20
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    22
 *
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    23
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    24
/* HomeKit Door Homekit
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    25
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    26
#include <stdio.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    27
#include <string.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    28
#include <freertos/FreeRTOS.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    29
#include <freertos/task.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    30
#include <freertos/timers.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    31
#include <freertos/queue.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    32
#include <esp_log.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    33
#include <driver/gpio.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    34
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    35
#include <hap.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    36
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    37
#include <hap_apple_servs.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    38
#include <hap_apple_chars.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    39
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    40
#include <app_wifi.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    41
#include <app_hap_setup_payload.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    42
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    43
static const char *TAG = "HAP door";
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    44
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    45
#define DOOR_TASK_PRIORITY  1
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    46
#define DOOR_TASK_STACKSIZE 4 * 1024
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    47
#define DOOR_TASK_NAME      "hap_door"
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    48
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    49
#define DOOR_LOCK_GPIO_PIN GPIO_NUM_21
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    50
#define DOOR_BELL_GPIO_PIN GPIO_NUM_14
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    51
#define DOOR_LOCK_GPIO_LOCKED 0
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    52
#define DOOR_LOCK_GPIO_UNLOCKED 1
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    53
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    54
#define HAP_LOCK_TARGET_STATE_UNSECURED 0
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    55
#define HAP_LOCK_TARGET_STATE_SECURED 1
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    56
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    57
static hap_val_t HAP_LOCK_CURRENT_STATE_UNSECURED = {.u = 0};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    58
static hap_val_t HAP_LOCK_CURRENT_STATE_SECURED = {.u = 1};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    59
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    60
static hap_val_t HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS = {.u = 0};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    61
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    62
#define ESP_INTR_FLAG_DEFAULT 0
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    63
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    64
static const uint8_t DOOR_EVENT_QUEUE_BELL = 1;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    65
static const uint8_t DOOR_EVENT_QUEUE_UNLOCK = 2;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    66
static const uint8_t DOOR_EVENT_QUEUE_LOCK = 3;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    67
static const uint8_t DOOR_EVENT_QUEUE_LOCK_TIMEOUT = 4;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    68
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    69
static xQueueHandle door_event_queue = NULL;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    70
static TimerHandle_t door_lock_timer = NULL;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    71
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    72
/**
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    73
 * @brief the recover door bell gpio interrupt function
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    74
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    75
static void IRAM_ATTR door_bell_isr(void* arg) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    76
	xQueueSendFromISR(door_event_queue, (void*) &DOOR_EVENT_QUEUE_BELL, NULL);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    77
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    78
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    79
/**
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    80
 * Enable a GPIO Pin for Door Bell
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    81
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    82
static void door_bell_init(uint32_t key_gpio_pin) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    83
	gpio_config_t io_conf;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    84
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    85
	io_conf.intr_type		= GPIO_INTR_NEGEDGE;	 /* Interrupt for falling edge  */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    86
	io_conf.pin_bit_mask	= 1 << key_gpio_pin;	 /* Bit mask of the pins */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    87
	io_conf.mode			= GPIO_MODE_INPUT;	 /* Set as input mode */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    88
	io_conf.pull_up_en	= GPIO_PULLUP_DISABLE;	 /* Disable internal pull-up */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    89
	io_conf.pull_down_en	= GPIO_PULLDOWN_DISABLE;		 /* Disable internal pull-down */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    90
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    91
	gpio_config(&io_conf);	 /* Set the GPIO configuration */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    92
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    93
	gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);	 /* Install gpio isr service */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    94
	gpio_isr_handler_add(key_gpio_pin, door_bell_isr, (void*)key_gpio_pin);		 /* Hook isr handler for specified gpio pin */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    95
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    96
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    97
/**
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    98
 * Enable a GPIO Pin for Door Lock
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    99
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   100
static void door_lock_init(uint32_t key_gpio_pin) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   101
	gpio_config_t io_conf;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   102
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   103
	io_conf.intr_type		= GPIO_INTR_DISABLE;	 /* Interrupt for falling edge  */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   104
	io_conf.pin_bit_mask	= 1 << key_gpio_pin;	 /* Bit mask of the pins */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   105
	io_conf.mode			= GPIO_MODE_OUTPUT;	 /* Set as input mode */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   106
	io_conf.pull_up_en	= GPIO_PULLUP_DISABLE;	 /* Disable internal pull-up */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   107
	io_conf.pull_down_en	= GPIO_PULLDOWN_DISABLE;		 /* Disable internal pull-down */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   108
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   109
	gpio_config(&io_conf);	 /* Set the GPIO configuration */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   110
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   111
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   112
/**
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   113
 * Initialize the Door Hardware. Here, we just enebale the Door Bell detection.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   114
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   115
void door_hardware_init(gpio_num_t door_bell_gpio_num, gpio_num_t door_lock_gpio_num) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   116
	int queue_len = 4;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   117
	int queue_item_size = sizeof(uint8_t);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   118
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   119
	door_event_queue = xQueueCreate(queue_len, queue_item_size);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   120
	if (door_event_queue != NULL) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   121
		door_bell_init(door_bell_gpio_num);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   122
		door_lock_init(door_lock_gpio_num);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   123
	}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   124
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   125
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   126
/* Mandatory identify routine for the accessory.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   127
 * In a real accessory, something like LED blink should be implemented
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   128
 * got visual identification
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   129
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   130
static int door_identify(hap_acc_t *ha) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   131
	ESP_LOGI(TAG, "Accessory identified");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   132
	return HAP_SUCCESS;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   133
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   134
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   135
static void door_bell_ring(hap_char_t *door_bell_current_state) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   136
	ESP_LOGI(TAG, "Door bell ring event processed");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   137
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   138
	hap_char_update_val(door_bell_current_state, &HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   139
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   140
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   141
static void door_unlock(hap_char_t *door_lock_current_state) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   142
	ESP_LOGI(TAG, "Door unlock event processed");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   143
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   144
	gpio_set_level(DOOR_LOCK_GPIO_PIN, DOOR_LOCK_GPIO_UNLOCKED);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   145
	hap_char_update_val(door_lock_current_state, &HAP_LOCK_CURRENT_STATE_UNSECURED);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   146
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   147
	xTimerReset(door_lock_timer, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   148
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   149
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   150
static void door_lock(hap_char_t *door_lock_current_state) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   151
	ESP_LOGI(TAG, "Door lock event processed");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   152
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   153
	gpio_set_level(DOOR_LOCK_GPIO_PIN, DOOR_LOCK_GPIO_LOCKED);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   154
	hap_char_update_val(door_lock_current_state, &HAP_LOCK_CURRENT_STATE_SECURED);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   155
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   156
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   157
static void door_lock_timeout(hap_char_t *door_lock_target_state) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   158
	ESP_LOGI(TAG, "Door lock timeout event processed");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   159
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   160
	xQueueSendToBack(door_event_queue, (void*) &DOOR_EVENT_QUEUE_LOCK, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   161
	hap_val_t target_lock_secured = {.u = HAP_LOCK_TARGET_STATE_SECURED};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   162
	hap_char_update_val(door_lock_target_state, &target_lock_secured);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   163
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   164
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   165
static int door_lock_write_cb(hap_write_data_t write_data[], int count, void *serv_priv, void *write_priv) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   166
	int i, ret = HAP_SUCCESS;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   167
	hap_write_data_t *write;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   168
	for (i = 0; i < count; i++) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   169
		write = &write_data[i];
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   170
		if (!strcmp(hap_char_get_type_uuid(write->hc), HAP_CHAR_UUID_LOCK_TARGET_STATE)) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   171
			ESP_LOGI(TAG, "Received Write. Door lock %d", write->val.u);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   172
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   173
			switch (write->val.u) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   174
				case HAP_LOCK_TARGET_STATE_UNSECURED:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   175
					xQueueSendToBack(door_event_queue, (void*) &DOOR_EVENT_QUEUE_UNLOCK, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   176
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   177
				case HAP_LOCK_TARGET_STATE_SECURED:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   178
					xQueueSendToBack(door_event_queue, (void*) &DOOR_EVENT_QUEUE_LOCK, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   179
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   180
			}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   181
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   182
			/* Update target state */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   183
			hap_char_update_val(write->hc, &(write->val));
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   184
			*(write->status) = HAP_STATUS_SUCCESS;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   185
		} else {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   186
			*(write->status) = HAP_STATUS_RES_ABSENT;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   187
		}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   188
	}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   189
	return ret;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   190
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   191
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   192
static void door_lock_timer_cb(TimerHandle_t timer) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   193
	ESP_LOGI(TAG, "Door lock timer fired - event queued");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   194
	xQueueSendToBack(door_event_queue, (void*) &DOOR_EVENT_QUEUE_LOCK_TIMEOUT, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   195
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   196
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   197
/*The main thread for handling the Door Accessory */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   198
static void door_thread_entry(void *p) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   199
	hap_init(HAP_TRANSPORT_WIFI);	 /* Initialize the HAP core */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   200
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   201
	/* Initialise the mandatory parameters for Accessory which will be added as
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   202
	 * the mandatory services internally
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   203
	 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   204
	hap_acc_cfg_t cfg = {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   205
		.name = "Door",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   206
		.manufacturer = "Luke Hoersten",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   207
		.model = "Esp32Door01",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   208
		.serial_num = "001122334455",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   209
		.fw_rev = "0.1.0",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   210
		.hw_rev = NULL,
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   211
		.pv = "1.1.0",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   212
		.identify_routine = door_identify,
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   213
		.cid = HAP_CID_DOOR,
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   214
	};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   215
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   216
	hap_acc_t *door_accessory = hap_acc_create(&cfg);	 /* Create accessory object */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   217
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   218
	/* Add a dummy Product Data */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   219
	uint8_t product_data[] = {'E','S','P','3','2','H','A','P'};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   220
	hap_acc_add_product_data(door_accessory, product_data, sizeof(product_data));
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   221
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   222
	hap_serv_t *door_bell_service = hap_serv_stateless_programmable_switch_create(0);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   223
	hap_serv_add_char(door_bell_service, hap_char_name_create("Doorbell"));
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   224
	hap_char_t *door_bell_current_state = hap_serv_get_char_by_uuid(door_bell_service, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   225
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   226
	hap_serv_t *door_lock_service = hap_serv_lock_mechanism_create(HAP_LOCK_CURRENT_STATE_SECURED.u, HAP_LOCK_TARGET_STATE_SECURED);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   227
	hap_serv_add_char(door_lock_service, hap_char_name_create("Door Lock"));
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   228
	hap_char_t *door_lock_current_state = hap_serv_get_char_by_uuid(door_lock_service, HAP_CHAR_UUID_LOCK_CURRENT_STATE);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   229
	hap_char_t *door_lock_target_state = hap_serv_get_char_by_uuid(door_lock_service, HAP_CHAR_UUID_LOCK_TARGET_STATE);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   230
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   231
	/* Get pointer to the door in use characteristic which we need to monitor for state changes */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   232
	hap_serv_set_write_cb(door_lock_service, door_lock_write_cb);	 /* Set the write callback for the service */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   233
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   234
	hap_acc_add_serv(door_accessory, door_bell_service);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   235
	hap_acc_add_serv(door_accessory, door_lock_service);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   236
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   237
	hap_add_accessory(door_accessory);	 /* Add the Accessory to the HomeKit Database */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   238
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   239
	door_hardware_init(DOOR_BELL_GPIO_PIN, DOOR_LOCK_GPIO_PIN);	 /* Initialize the appliance specific hardware. This enables out-in-use detection */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   240
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   241
	/* For production accessories, the setup code shouldn't be programmed on to
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   242
	 * the device. Instead, the setup info, derived from the setup code must
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   243
	 * be used. Use the factory_nvs_gen utility to generate this data and then
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   244
	 * flash it into the factory NVS partition.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   245
	 *
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   246
	 * By default, the setup ID and setup info will be read from the factory_nvs
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   247
	 * Flash partition and so, is not required to set here explicitly.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   248
	 *
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   249
	 * However, for testing purpose, this can be overridden by using hap_set_setup_code()
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   250
	 * and hap_set_setup_id() APIs, as has been done here.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   251
	 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   252
#ifdef CONFIG_HOMEKIT_USE_HARDCODED_SETUP_CODE
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   253
	/* Unique Setup code of the format xxx-xx-xxx. Default: 111-22-333 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   254
	hap_set_setup_code(CONFIG_HOMEKIT_SETUP_CODE);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   255
	/* Unique four character Setup Id. Default: ES32 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   256
	hap_set_setup_id(CONFIG_HOMEKIT_SETUP_ID);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   257
#ifdef CONFIG_APP_WIFI_USE_WAC_PROVISIONING
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   258
	app_hap_setup_payload(CONFIG_HOMEKIT_SETUP_CODE, CONFIG_HOMEKIT_SETUP_ID, true, cfg.cid);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   259
#else
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   260
	app_hap_setup_payload(CONFIG_HOMEKIT_SETUP_CODE, CONFIG_HOMEKIT_SETUP_ID, false, cfg.cid);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   261
#endif
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   262
#endif
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   263
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   264
	hap_enable_mfi_auth(HAP_MFI_AUTH_HW);	 /* Enable Hardware MFi authentication (applicable only for MFi variant of SDK) */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   265
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   266
	app_wifi_init();	 /* Initialize Wi-Fi */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   267
	hap_start();	 /* After all the initializations are done, start the HAP core */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   268
	app_wifi_start(portMAX_DELAY);	 /* Start Wi-Fi */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   269
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   270
	door_lock_timer = xTimerCreate("door_lock_timer", pdMS_TO_TICKS(CONFIG_HOMEKIT_DOOR_LOCK_TIMEOUT), pdFALSE, 0, door_lock_timer_cb);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   271
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   272
	/* Listen for doorbell state change events. Other read/write functionality will be handled by the HAP Core.  When the
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   273
	 * doorbell in Use GPIO goes low, it means doorbell is not ringing.  When the Door in Use GPIO goes high, it means
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   274
	 * the doorbell is ringing.  Applications can define own logic as per their hardware.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   275
	 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   276
	uint8_t door_event_queue_item = DOOR_EVENT_QUEUE_LOCK;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   277
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   278
	while (1) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   279
		if (xQueueReceive(door_event_queue, &door_event_queue_item, portMAX_DELAY) == pdFALSE) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   280
			ESP_LOGI(TAG, "Door event queue trigger FAIL");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   281
		} else {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   282
			switch(door_event_queue_item) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   283
				case DOOR_EVENT_QUEUE_BELL:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   284
					door_bell_ring(door_bell_current_state);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   285
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   286
				case DOOR_EVENT_QUEUE_UNLOCK:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   287
					door_unlock(door_lock_current_state);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   288
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   289
				case DOOR_EVENT_QUEUE_LOCK:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   290
					door_lock(door_lock_current_state);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   291
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   292
				case DOOR_EVENT_QUEUE_LOCK_TIMEOUT:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   293
					door_lock_timeout(door_lock_target_state);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   294
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   295
			}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   296
		}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   297
	}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   298
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   299
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   300
void app_main() {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   301
	xTaskCreate(door_thread_entry, DOOR_TASK_NAME, DOOR_TASK_STACKSIZE, NULL, DOOR_TASK_PRIORITY, NULL);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   302
}