Loading...
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 135 136 137 138 | /* main.c - Application main entry point */ /* * Copyright (c) 2015-2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/types.h> #include <stddef.h> #include <string.h> #include <errno.h> #include <sys/printk.h> #include <sys/byteorder.h> #include <zephyr.h> #include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <bluetooth/conn.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> #include <bluetooth/services/bas.h> #include <bluetooth/services/hrs.h> struct bt_conn *default_conn; static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x0d, 0x18, 0x0f, 0x18, 0x0a, 0x18), }; static void connected(struct bt_conn *conn, u8_t err) { if (err) { printk("Connection failed (err 0x%02x)\n", err); } else { default_conn = bt_conn_ref(conn); printk("Connected\n"); } } static void disconnected(struct bt_conn *conn, u8_t reason) { printk("Disconnected (reason 0x%02x)\n", reason); if (default_conn) { bt_conn_unref(default_conn); default_conn = NULL; } } static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, }; static void bt_ready(void) { int err; printk("Bluetooth initialized\n"); err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0); if (err) { printk("Advertising failed to start (err %d)\n", err); return; } printk("Advertising successfully started\n"); } static void auth_cancel(struct bt_conn *conn) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Pairing cancelled: %s\n", addr); } static struct bt_conn_auth_cb auth_cb_display = { .cancel = auth_cancel, }; static void bas_notify(void) { u8_t battery_level = bt_gatt_bas_get_battery_level(); battery_level--; if (!battery_level) { battery_level = 100U; } bt_gatt_bas_set_battery_level(battery_level); } static void hrs_notify(void) { static u8_t heartrate = 90U; /* Heartrate measurements simulation */ heartrate++; if (heartrate == 160U) { heartrate = 90U; } bt_gatt_hrs_notify(heartrate); } void main(void) { int err; err = bt_enable(NULL); if (err) { printk("Bluetooth init failed (err %d)\n", err); return; } bt_ready(); bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&auth_cb_display); /* Implement notification. At the moment there is no suitable way * of starting delayed work so we do it here */ while (1) { k_sleep(MSEC_PER_SEC); /* Heartrate measurements simulation */ hrs_notify(); /* Battery level simulation */ bas_notify(); } } |