main/app_main.c
author Luke Hoersten <luke@hoersten.org>
Sun, 28 Feb 2021 20:09:32 -0600
changeset 1 289a6264a47e
parent 0 31a65edead29
child 2 6c96c21bd8f1
permissions -rw-r--r--
Remove the example copyright.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     1
/* HomeKit Door Homekit
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     2
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     3
#include <stdio.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     4
#include <string.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     5
#include <freertos/FreeRTOS.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     6
#include <freertos/task.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     7
#include <freertos/timers.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     8
#include <freertos/queue.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     9
#include <esp_log.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    10
#include <driver/gpio.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    11
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    12
#include <hap.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    13
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    14
#include <hap_apple_servs.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    15
#include <hap_apple_chars.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    16
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    17
#include <app_wifi.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    18
#include <app_hap_setup_payload.h>
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    19
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    20
static const char *TAG = "HAP door";
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    21
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    22
#define DOOR_TASK_PRIORITY  1
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    23
#define DOOR_TASK_STACKSIZE 4 * 1024
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    24
#define DOOR_TASK_NAME      "hap_door"
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    25
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    26
#define DOOR_LOCK_GPIO_PIN GPIO_NUM_21
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    27
#define DOOR_BELL_GPIO_PIN GPIO_NUM_14
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    28
#define DOOR_LOCK_GPIO_LOCKED 0
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    29
#define DOOR_LOCK_GPIO_UNLOCKED 1
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    30
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    31
#define HAP_LOCK_TARGET_STATE_UNSECURED 0
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    32
#define HAP_LOCK_TARGET_STATE_SECURED 1
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    33
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    34
static hap_val_t HAP_LOCK_CURRENT_STATE_UNSECURED = {.u = 0};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    35
static hap_val_t HAP_LOCK_CURRENT_STATE_SECURED = {.u = 1};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    36
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    37
static hap_val_t HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS = {.u = 0};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    38
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    39
#define ESP_INTR_FLAG_DEFAULT 0
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    40
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    41
static const uint8_t DOOR_EVENT_QUEUE_BELL = 1;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    42
static const uint8_t DOOR_EVENT_QUEUE_UNLOCK = 2;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    43
static const uint8_t DOOR_EVENT_QUEUE_LOCK = 3;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    44
static const uint8_t DOOR_EVENT_QUEUE_LOCK_TIMEOUT = 4;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    45
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    46
static xQueueHandle door_event_queue = NULL;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    47
static TimerHandle_t door_lock_timer = NULL;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    48
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    49
/**
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    50
 * @brief the recover door bell gpio interrupt function
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    51
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    52
static void IRAM_ATTR door_bell_isr(void* arg) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    53
	xQueueSendFromISR(door_event_queue, (void*) &DOOR_EVENT_QUEUE_BELL, NULL);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    54
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    55
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    56
/**
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    57
 * Enable a GPIO Pin for Door Bell
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    58
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    59
static void door_bell_init(uint32_t key_gpio_pin) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    60
	gpio_config_t io_conf;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    61
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    62
	io_conf.intr_type		= GPIO_INTR_NEGEDGE;	 /* Interrupt for falling edge  */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    63
	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
    64
	io_conf.mode			= GPIO_MODE_INPUT;	 /* Set as input mode */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    65
	io_conf.pull_up_en	= GPIO_PULLUP_DISABLE;	 /* Disable internal pull-up */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    66
	io_conf.pull_down_en	= GPIO_PULLDOWN_DISABLE;		 /* Disable internal pull-down */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    67
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    68
	gpio_config(&io_conf);	 /* Set the GPIO configuration */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    69
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    70
	gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);	 /* Install gpio isr service */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    71
	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
    72
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    73
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    74
/**
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    75
 * Enable a GPIO Pin for Door Lock
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    76
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    77
static void door_lock_init(uint32_t key_gpio_pin) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    78
	gpio_config_t io_conf;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    79
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    80
	io_conf.intr_type		= GPIO_INTR_DISABLE;	 /* Interrupt for falling edge  */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    81
	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
    82
	io_conf.mode			= GPIO_MODE_OUTPUT;	 /* Set as input mode */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    83
	io_conf.pull_up_en	= GPIO_PULLUP_DISABLE;	 /* Disable internal pull-up */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    84
	io_conf.pull_down_en	= GPIO_PULLDOWN_DISABLE;		 /* Disable internal pull-down */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    85
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    86
	gpio_config(&io_conf);	 /* Set the GPIO configuration */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    87
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    88
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    89
/**
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    90
 * Initialize the Door Hardware. Here, we just enebale the Door Bell detection.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    91
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    92
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
    93
	int queue_len = 4;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    94
	int queue_item_size = sizeof(uint8_t);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    95
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    96
	door_event_queue = xQueueCreate(queue_len, queue_item_size);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    97
	if (door_event_queue != NULL) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    98
		door_bell_init(door_bell_gpio_num);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    99
		door_lock_init(door_lock_gpio_num);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   100
	}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   101
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   102
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   103
/* Mandatory identify routine for the accessory.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   104
 * In a real accessory, something like LED blink should be implemented
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   105
 * got visual identification
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   106
 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   107
static int door_identify(hap_acc_t *ha) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   108
	ESP_LOGI(TAG, "Accessory identified");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   109
	return HAP_SUCCESS;
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
static void door_bell_ring(hap_char_t *door_bell_current_state) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   113
	ESP_LOGI(TAG, "Door bell ring event processed");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   114
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   115
	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
   116
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   117
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   118
static void door_unlock(hap_char_t *door_lock_current_state) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   119
	ESP_LOGI(TAG, "Door unlock event processed");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   120
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   121
	gpio_set_level(DOOR_LOCK_GPIO_PIN, DOOR_LOCK_GPIO_UNLOCKED);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   122
	hap_char_update_val(door_lock_current_state, &HAP_LOCK_CURRENT_STATE_UNSECURED);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   123
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   124
	xTimerReset(door_lock_timer, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   125
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   126
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   127
static void door_lock(hap_char_t *door_lock_current_state) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   128
	ESP_LOGI(TAG, "Door lock event processed");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   129
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   130
	gpio_set_level(DOOR_LOCK_GPIO_PIN, DOOR_LOCK_GPIO_LOCKED);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   131
	hap_char_update_val(door_lock_current_state, &HAP_LOCK_CURRENT_STATE_SECURED);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   132
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   133
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   134
static void door_lock_timeout(hap_char_t *door_lock_target_state) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   135
	ESP_LOGI(TAG, "Door lock timeout event processed");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   136
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   137
	xQueueSendToBack(door_event_queue, (void*) &DOOR_EVENT_QUEUE_LOCK, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   138
	hap_val_t target_lock_secured = {.u = HAP_LOCK_TARGET_STATE_SECURED};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   139
	hap_char_update_val(door_lock_target_state, &target_lock_secured);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   140
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   141
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   142
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
   143
	int i, ret = HAP_SUCCESS;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   144
	hap_write_data_t *write;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   145
	for (i = 0; i < count; i++) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   146
		write = &write_data[i];
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   147
		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
   148
			ESP_LOGI(TAG, "Received Write. Door lock %d", write->val.u);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   149
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   150
			switch (write->val.u) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   151
				case HAP_LOCK_TARGET_STATE_UNSECURED:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   152
					xQueueSendToBack(door_event_queue, (void*) &DOOR_EVENT_QUEUE_UNLOCK, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   153
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   154
				case HAP_LOCK_TARGET_STATE_SECURED:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   155
					xQueueSendToBack(door_event_queue, (void*) &DOOR_EVENT_QUEUE_LOCK, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   156
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   157
			}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   158
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   159
			/* Update target state */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   160
			hap_char_update_val(write->hc, &(write->val));
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   161
			*(write->status) = HAP_STATUS_SUCCESS;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   162
		} else {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   163
			*(write->status) = HAP_STATUS_RES_ABSENT;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   164
		}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   165
	}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   166
	return ret;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   167
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   168
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   169
static void door_lock_timer_cb(TimerHandle_t timer) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   170
	ESP_LOGI(TAG, "Door lock timer fired - event queued");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   171
	xQueueSendToBack(door_event_queue, (void*) &DOOR_EVENT_QUEUE_LOCK_TIMEOUT, 10);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   172
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   173
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   174
/*The main thread for handling the Door Accessory */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   175
static void door_thread_entry(void *p) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   176
	hap_init(HAP_TRANSPORT_WIFI);	 /* Initialize the HAP core */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   177
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   178
	/* Initialise the mandatory parameters for Accessory which will be added as
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   179
	 * the mandatory services internally
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   180
	 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   181
	hap_acc_cfg_t cfg = {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   182
		.name = "Door",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   183
		.manufacturer = "Luke Hoersten",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   184
		.model = "Esp32Door01",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   185
		.serial_num = "001122334455",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   186
		.fw_rev = "0.1.0",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   187
		.hw_rev = NULL,
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   188
		.pv = "1.1.0",
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   189
		.identify_routine = door_identify,
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   190
		.cid = HAP_CID_DOOR,
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   191
	};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   192
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   193
	hap_acc_t *door_accessory = hap_acc_create(&cfg);	 /* Create accessory object */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   194
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   195
	/* Add a dummy Product Data */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   196
	uint8_t product_data[] = {'E','S','P','3','2','H','A','P'};
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   197
	hap_acc_add_product_data(door_accessory, product_data, sizeof(product_data));
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   198
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   199
	hap_serv_t *door_bell_service = hap_serv_stateless_programmable_switch_create(0);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   200
	hap_serv_add_char(door_bell_service, hap_char_name_create("Doorbell"));
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   201
	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
   202
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   203
	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
   204
	hap_serv_add_char(door_lock_service, hap_char_name_create("Door Lock"));
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   205
	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
   206
	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
   207
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   208
	/* 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
   209
	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
   210
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   211
	hap_acc_add_serv(door_accessory, door_bell_service);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   212
	hap_acc_add_serv(door_accessory, door_lock_service);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   213
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   214
	hap_add_accessory(door_accessory);	 /* Add the Accessory to the HomeKit Database */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   215
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   216
	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
   217
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   218
	/* For production accessories, the setup code shouldn't be programmed on to
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   219
	 * the device. Instead, the setup info, derived from the setup code must
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   220
	 * 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
   221
	 * flash it into the factory NVS partition.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   222
	 *
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   223
	 * 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
   224
	 * Flash partition and so, is not required to set here explicitly.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   225
	 *
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   226
	 * 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
   227
	 * and hap_set_setup_id() APIs, as has been done here.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   228
	 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   229
#ifdef CONFIG_HOMEKIT_USE_HARDCODED_SETUP_CODE
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   230
	/* Unique Setup code of the format xxx-xx-xxx. Default: 111-22-333 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   231
	hap_set_setup_code(CONFIG_HOMEKIT_SETUP_CODE);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   232
	/* Unique four character Setup Id. Default: ES32 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   233
	hap_set_setup_id(CONFIG_HOMEKIT_SETUP_ID);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   234
#ifdef CONFIG_APP_WIFI_USE_WAC_PROVISIONING
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   235
	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
   236
#else
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   237
	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
   238
#endif
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   239
#endif
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   240
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   241
	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
   242
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   243
	app_wifi_init();	 /* Initialize Wi-Fi */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   244
	hap_start();	 /* After all the initializations are done, start the HAP core */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   245
	app_wifi_start(portMAX_DELAY);	 /* Start Wi-Fi */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   246
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   247
	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
   248
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   249
	/* 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
   250
	 * 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
   251
	 * the doorbell is ringing.  Applications can define own logic as per their hardware.
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   252
	 */
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   253
	uint8_t door_event_queue_item = DOOR_EVENT_QUEUE_LOCK;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   254
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   255
	while (1) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   256
		if (xQueueReceive(door_event_queue, &door_event_queue_item, portMAX_DELAY) == pdFALSE) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   257
			ESP_LOGI(TAG, "Door event queue trigger FAIL");
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   258
		} else {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   259
			switch(door_event_queue_item) {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   260
				case DOOR_EVENT_QUEUE_BELL:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   261
					door_bell_ring(door_bell_current_state);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   262
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   263
				case DOOR_EVENT_QUEUE_UNLOCK:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   264
					door_unlock(door_lock_current_state);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   265
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   266
				case DOOR_EVENT_QUEUE_LOCK:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   267
					door_lock(door_lock_current_state);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   268
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   269
				case DOOR_EVENT_QUEUE_LOCK_TIMEOUT:
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   270
					door_lock_timeout(door_lock_target_state);
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   271
					break;
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   272
			}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   273
		}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   274
	}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   275
}
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   276
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   277
void app_main() {
31a65edead29 Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
   278
	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
   279
}