1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/* ESP HomeKit Intercom
*/
#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>
#define INTERCOM_TASK_PRIORITY 1
#define INTERCOM_TASK_STACKSIZE 4 * 1024
#define INTERCOM_TASK_NAME "hap_intercom"
#define RESET_NETWORK_BUTTON_TIMEOUT 3
/* static uint8_t tlv8buff[128];
static hap_data_val_t null_tlv8 = {.buf = &tlv8buff, .buflen = 127}; */
static void led_init(uint32_t key_gpio_pin)
{
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE; /* Interrupt for falling edge */
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 */
}
static int intercom_identify(hap_acc_t *ha)
{
ESP_LOGI(TAG, "Accessory identified");
for (int i = 0; i < 3; i++)
{
gpio_set_level(CONFIG_HOMEKIT_INTERCOM_LED_GPIO_PIN, 1);
vTaskDelay(pdMS_TO_TICKS(500));
gpio_set_level(CONFIG_HOMEKIT_INTERCOM_LED_GPIO_PIN, 0);
vTaskDelay(pdMS_TO_TICKS(500));
}
return HAP_SUCCESS;
}
static void intercom_thread_entry(void *p)
{
hap_init(HAP_TRANSPORT_WIFI); /* Initialize the HAP core */
/* Initialise the mandatory parameters for Accessory which will be added as
* the mandatory services internally
*/
hap_acc_cfg_t cfg = {
.name = "Intercom",
.manufacturer = "Luke Hoersten",
.model = "Esp32Intercom01",
.serial_num = "001122334455",
.fw_rev = "0.1.0",
.hw_rev = NULL,
.pv = "1.1.0",
.identify_routine = intercom_identify,
.cid = HAP_CID_DOOR,
};
hap_acc_t *intercom_accessory = hap_acc_create(&cfg); /* Create accessory object */
/* Add a dummy Product Data */
uint8_t product_data[] = {'E', 'S', 'P', '3', '2', 'H', 'A', 'P'};
hap_acc_add_product_data(intercom_accessory, product_data, sizeof(product_data));
if (!intercom_event_queue_init())
{
ESP_LOGI(TAG, "Failed to initialize event queue");
return;
}
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 */
/* 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
* flash it into the factory NVS partition.
*
* By default, the setup ID and setup info will be read from the factory_nvs
* Flash partition and so, is not required to set here explicitly.
*
* However, for testing purpose, this can be overridden by using hap_set_setup_code()
* and hap_set_setup_id() APIs, as has been done here.
*/
#ifdef CONFIG_HOMEKIT_USE_HARDCODED_SETUP_CODE
/* Unique Setup code of the format xxx-xx-xxx. Default: 111-22-333 */
hap_set_setup_code(CONFIG_HOMEKIT_SETUP_CODE);
/* Unique four character Setup Id. Default: ES32 */
hap_set_setup_id(CONFIG_HOMEKIT_SETUP_ID);
#ifdef CONFIG_APP_WIFI_USE_WAC_PROVISIONING
app_hap_setup_payload(CONFIG_HOMEKIT_SETUP_CODE, CONFIG_HOMEKIT_SETUP_ID, true, cfg.cid);
#else
app_hap_setup_payload(CONFIG_HOMEKIT_SETUP_CODE, CONFIG_HOMEKIT_SETUP_ID, false, cfg.cid);
#endif
#endif
hap_enable_mfi_auth(HAP_MFI_AUTH_HW); /* Enable Hardware MFi authentication (applicable only for MFi variant of SDK) */
app_wifi_init(); /* Initialize Wi-Fi */
hap_start(); /* After all the initializations are done, start the HAP core */
app_wifi_start(portMAX_DELAY); /* Start Wi-Fi */
intercom_event_queue_run();
}
void app_main()
{
xTaskCreate(intercom_thread_entry, INTERCOM_TASK_NAME, INTERCOM_TASK_STACKSIZE, NULL, INTERCOM_TASK_PRIORITY, NULL);
}
|