1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2019 Aaron Tsui <aaron.tsui@outlook.com>
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/types.h>
10 #include <stddef.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <zephyr/sys/printk.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/kernel.h>
16 
17 #include <zephyr/bluetooth/bluetooth.h>
18 #include <zephyr/bluetooth/hci.h>
19 #include <zephyr/bluetooth/conn.h>
20 #include <zephyr/bluetooth/uuid.h>
21 #include <zephyr/bluetooth/gatt.h>
22 #include <zephyr/bluetooth/services/bas.h>
23 
24 #include "hts.h"
25 
26 static const struct bt_data ad[] = {
27 	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
28 	BT_DATA_BYTES(BT_DATA_UUID16_ALL,
29 		      BT_UUID_16_ENCODE(BT_UUID_HTS_VAL),
30 		      BT_UUID_16_ENCODE(BT_UUID_DIS_VAL),
31 		      BT_UUID_16_ENCODE(BT_UUID_BAS_VAL)),
32 };
33 
34 static const struct bt_data sd[] = {
35 	BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
36 };
37 
connected(struct bt_conn * conn,uint8_t err)38 static void connected(struct bt_conn *conn, uint8_t err)
39 {
40 	if (err) {
41 		printk("Connection failed, err 0x%02x %s\n", err, bt_hci_err_to_str(err));
42 	} else {
43 		printk("Connected\n");
44 	}
45 }
46 
disconnected(struct bt_conn * conn,uint8_t reason)47 static void disconnected(struct bt_conn *conn, uint8_t reason)
48 {
49 	printk("Disconnected, reason 0x%02x %s\n", reason, bt_hci_err_to_str(reason));
50 }
51 
52 BT_CONN_CB_DEFINE(conn_callbacks) = {
53 	.connected = connected,
54 	.disconnected = disconnected,
55 };
56 
bt_ready(void)57 static void bt_ready(void)
58 {
59 	int err;
60 
61 	printk("Bluetooth initialized\n");
62 
63 	hts_init();
64 
65 	err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
66 	if (err) {
67 		printk("Advertising failed to start (err %d)\n", err);
68 		return;
69 	}
70 
71 	printk("Advertising successfully started\n");
72 }
73 
auth_cancel(struct bt_conn * conn)74 static void auth_cancel(struct bt_conn *conn)
75 {
76 	char addr[BT_ADDR_LE_STR_LEN];
77 
78 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
79 
80 	printk("Pairing cancelled: %s\n", addr);
81 }
82 
83 static struct bt_conn_auth_cb auth_cb_display = {
84 	.cancel = auth_cancel,
85 };
86 
bas_notify(void)87 static void bas_notify(void)
88 {
89 	uint8_t battery_level = bt_bas_get_battery_level();
90 
91 	battery_level--;
92 
93 	if (!battery_level) {
94 		battery_level = 100U;
95 	}
96 
97 	bt_bas_set_battery_level(battery_level);
98 }
99 
main(void)100 int main(void)
101 {
102 	int err;
103 
104 	err = bt_enable(NULL);
105 	if (err) {
106 		printk("Bluetooth init failed (err %d)\n", err);
107 		return 0;
108 	}
109 
110 	bt_ready();
111 
112 	bt_conn_auth_cb_register(&auth_cb_display);
113 
114 	/* Implement indicate. At the moment there is no suitable way
115 	 * of starting delayed work so we do it here
116 	 */
117 	while (1) {
118 		k_sleep(K_SECONDS(1));
119 
120 		/* Temperature measurements simulation */
121 		hts_indicate();
122 
123 		/* Battery level simulation */
124 		bas_notify();
125 	}
126 	return 0;
127 }
128