/* main.c - Application main entry point */ /* * Copyright (c) 2015-2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include #include #include #include #include #include #include #include #define DEVICE_NAME CONFIG_BT_DEVICE_NAME #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) #define LED0_NODE DT_ALIAS(led4) #define SW0_NODE DT_ALIAS(sw6) void led_timer_expiry_function(struct k_timer *timer_id); void sleep_timer_expiry_function(struct k_timer *timer_id); //int val; K_MSGQ_DEFINE(ble_status_queue, sizeof(int), 16, 4); K_TIMER_DEFINE(led_timer, led_timer_expiry_function, NULL); // Define a timer for the LED K_TIMER_DEFINE(sleep_timer, sleep_timer_expiry_function, NULL); // Define a timer for the LED uint8_t door_status = 0; uint8_t d[3] = {0x11, 0x66, 0x00}; uint8_t current_state ,prev_state ; static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(SW0_NODE, gpios); static struct gpio_callback button_cb_data; /* * Set Advertisement data. Based on the Eddystone specification: * https://github.com/google/eddystone/blob/master/protocol-specification.md * https://github.com/google/eddystone/tree/master/eddystone-url */ static struct bt_data ad1[] = { BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR), BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x58, 0x36), BT_DATA_BYTES(BT_DATA_SVC_DATA16,0x11,0x66,0x00) }; /* Set Scan Response data */ static const struct bt_data sd[] = { BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void bt_ready(int err) { char addr_s[BT_ADDR_LE_STR_LEN]; bt_addr_le_t addr = {0}; size_t count = 1; if (err) { printk("Bluetooth init failed (err %d)\n", err); return; } printk("Bluetooth initialized\n"); /* Start advertising */ err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad1, ARRAY_SIZE(ad1), sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising failed to start (err %d)\n", err); return; } /* For connectable advertising you would use * bt_le_oob_get_local(). For non-connectable non-identity * advertising an non-resolvable private address is used; * there is no API to retrieve that. */ bt_id_get(&addr, &count); bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s)); printk("Beacon started, advertising as %s\n", addr_s); } void led_timer_expiry_function(struct k_timer *timer_id) { gpio_pin_set_dt(&led, 0); // Turn off the LED after 100ms } void sleep_timer_expiry_function(struct k_timer *timer_id) { pm_state_force(0, &(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0}); } void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { //printk("Button pressed at %" PRIu32 "\n", k_cycle_get_32()); int val= gpio_pin_get_dt(&button); k_msgq_put(&ble_status_queue, &val, K_NO_WAIT); } void update_advertisement_data(int rec_data) { k_timer_stop(&sleep_timer); printk("Door Status: %s\n", rec_data ? "Open" : "Closed"); gpio_pin_set_dt(&led, 1); k_timer_start(&led_timer, K_MSEC(50), K_NO_WAIT); // Start the timer for 100ms d[2] = rec_data; ad1[2].data = (const uint8_t *)(d); int err = bt_le_adv_update_data(ad1, ARRAY_SIZE(ad1), sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising update failed with err %d\n", err); } k_timer_start(&sleep_timer, K_MSEC(5000), K_NO_WAIT); // Start the timer for 5sec } int main(void) { int err,ret; if (!device_is_ready(led.port)) { return ; } if (!device_is_ready(button.port)) { return ; } ret = gpio_pin_configure_dt(&button, GPIO_INPUT); if (ret < 0) { return ; } ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT); if (ret < 0) { return ; } ret = gpio_pin_interrupt_configure_dt(&button,GPIO_INT_EDGE_BOTH); if (ret != 0) { printk("Error %d: failed to configure interrupt on %s pin %d\n", ret, button.port->name, button.pin); return 0; } gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin)); gpio_add_callback(button.port, &button_cb_data); printk("Starting Beacon Demo\n"); /* Initialize the Bluetooth Subsystem */ err = bt_enable(bt_ready); if (err) { printk("Bluetooth init failed (err %d)\n", err); } //k_timer_init(&led_timer, NULL, led_timer_expiry_function); while(true) { int rec_data; k_msgq_get(&ble_status_queue,&rec_data,K_FOREVER); update_advertisement_data(rec_data); } return 0; }