1 /*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holders nor the names of
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * 4. Any redistribution, use, or modification is done solely for
17 * personal benefit and not for any commercial purpose or for
18 * monetary gain.
19 *
20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Please inquire about commercial licensing options at
34 * [email protected]
35 *
36 */
37
38 #define BTSTACK_FILE__ "sx1280_ble_demo.c"
39
40 // *****************************************************************************
41 /* EXAMPLE_START(sx1280_ble_demo): LE Peripheral - Control LED over GATT */
42 // *****************************************************************************
43
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "stm32l4xx_hal.h"
50
51 #include "btstack.h"
52 #include "gatt_rgb.h"
53
54 #define LED_SMALL_PORT GPIOB
55 #define LED_SMALL_PIN GPIO_PIN_8
56
57 #define LED_R_PORT GPIOH
58 #define LED_R_PIN GPIO_PIN_0
59 #define LED_G_PORT GPIOH
60 #define LED_G_PIN GPIO_PIN_1
61 #define LED_B_PORT GPIOC
62 #define LED_B_PIN GPIO_PIN_1
63
64 #define HEARTBEAT_PERIOD_MS 2000
65 #define HEARTBEAT_BLINK_MS 200
66
67 static btstack_timer_source_t heartbeat;
68
69 static void heartbeat_handler_led_on(struct btstack_timer_source *ts);
70 static void heartbeat_handler_led_off(struct btstack_timer_source *ts);
71
72 const uint8_t adv_data[] = {
73 // Flags general discoverable, BR/EDR not supported
74 0x02, 0x01, 0x06,
75 // Service 16-bit: FFF0
76 0x03, 0x02, 0xF0, 0xFF,
77 // Manufacturer specific data (aa bb - address 78:A5:04:82:1B:A4)
78 0x09, 0xFF, 0x04, 0x1E, 0x78, 0xA5, 0x04, 0x82, 0x1A, 0xAA,
79 // Name
80 0x08, 0x09, 'D', 'E', 'L', 'I', 'G', 'H', 'T',
81 };
82 const uint8_t adv_data_len = sizeof(adv_data);
83
heartbeat_handler_led_off(struct btstack_timer_source * ts)84 static void heartbeat_handler_led_off(struct btstack_timer_source *ts){
85 HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_SET);
86 heartbeat.process = &heartbeat_handler_led_on;
87 btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS - HEARTBEAT_BLINK_MS);
88 btstack_run_loop_add_timer(ts);
89 }
90
heartbeat_handler_led_on(struct btstack_timer_source * ts)91 static void heartbeat_handler_led_on(struct btstack_timer_source *ts){
92 HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
93 printf("BLINK\n");
94 heartbeat.process = &heartbeat_handler_led_off;
95 btstack_run_loop_set_timer(ts, HEARTBEAT_BLINK_MS);
96 btstack_run_loop_add_timer(ts);
97 }
98
set_rgb(uint8_t r,uint8_t g,uint8_t b)99 static void set_rgb(uint8_t r, uint8_t g, uint8_t b){
100 printf("R: %u, G: %u, B: %u\n", r, g, b);
101 HAL_GPIO_WritePin(LED_R_PORT, LED_R_PIN, r > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
102 HAL_GPIO_WritePin(LED_G_PORT, LED_G_PIN, g > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
103 HAL_GPIO_WritePin(LED_B_PORT, LED_B_PIN, b > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
104 }
105
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)106 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
107 UNUSED(channel);
108 UNUSED(size);
109
110 switch (packet_type) {
111 case HCI_EVENT_PACKET:
112 switch (hci_event_packet_get_type(packet)) {
113 case HCI_EVENT_LE_META:
114 switch (hci_event_le_meta_get_subevent_code(packet)){
115 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
116 printf("Connected\n");
117 btstack_run_loop_remove_timer(&heartbeat);
118 HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
119 break;
120 default:
121 break;
122 }
123 break;
124 case HCI_EVENT_DISCONNECTION_COMPLETE:
125 printf("Disconnected\n");
126 // restart heartbeat
127 set_rgb(0,0,0);
128 heartbeat_handler_led_on(&heartbeat);
129 break;
130 }
131 break;
132 }
133 }
134
att_read_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)135 static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
136 UNUSED(connection_handle);
137 return 0;
138 }
139
att_write_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t transaction_mode,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)140 static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
141 UNUSED(transaction_mode);
142 UNUSED(offset);
143 UNUSED(buffer_size);
144
145 if (att_handle == ATT_CHARACTERISTIC_FFF3_01_VALUE_HANDLE) {
146 set_rgb(buffer[4], buffer[5], buffer[6]);
147 }
148 return 0;
149 }
150
151 int btstack_main(void);
btstack_main(void)152 int btstack_main(void)
153 {
154 l2cap_init();
155
156 // setup le device db
157 le_device_db_init();
158
159 // setup SM: Display only
160 sm_init();
161
162 // setup ATT server
163 att_server_init(profile_data, att_read_callback, att_write_callback);
164
165 // setup advertisements
166 uint16_t adv_int_min = 0x0030;
167 uint16_t adv_int_max = 0x0030;
168 uint8_t adv_type = 0;
169 bd_addr_t null_addr;
170 memset(null_addr, 0, 6);
171 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
172 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
173 gap_advertisements_enable(1);
174
175 // register for ATT event
176 att_server_register_packet_handler(packet_handler);
177
178 // set one-shot timer
179 heartbeat.process = &heartbeat_handler_led_on;
180 btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
181 btstack_run_loop_add_timer(&heartbeat);
182
183 // LEDs init
184 GPIO_InitTypeDef GPIO_InitStruct = {0};
185
186 // enable GPIO for Port H
187 __HAL_RCC_GPIOH_CLK_ENABLE();
188
189 GPIO_InitStruct.Pin = LED_SMALL_PIN;
190 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
191 GPIO_InitStruct.Pull = GPIO_NOPULL;
192 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
193 HAL_GPIO_Init(LED_SMALL_PORT, &GPIO_InitStruct);
194
195 GPIO_InitStruct.Pin = LED_R_PIN;
196 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
197 GPIO_InitStruct.Pull = GPIO_NOPULL;
198 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
199 HAL_GPIO_Init(LED_R_PORT, &GPIO_InitStruct);
200
201 GPIO_InitStruct.Pin = LED_G_PIN;
202 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
203 GPIO_InitStruct.Pull = GPIO_NOPULL;
204 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
205 HAL_GPIO_Init(LED_G_PORT, &GPIO_InitStruct);
206
207 GPIO_InitStruct.Pin = LED_B_PIN;
208 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
209 GPIO_InitStruct.Pull = GPIO_NOPULL;
210 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
211 HAL_GPIO_Init(LED_B_PORT, &GPIO_InitStruct);
212
213 HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
214
215 HAL_GPIO_WritePin(LED_R_PORT, LED_R_PIN, GPIO_PIN_SET);
216 HAL_GPIO_WritePin(LED_G_PORT, LED_G_PIN, GPIO_PIN_SET);
217 HAL_GPIO_WritePin(LED_B_PORT, LED_B_PIN, GPIO_PIN_SET);
218
219
220 // turn on!
221 hci_power_control(HCI_POWER_ON);
222
223 return 0;
224 }
225 /* EXAMPLE_END */
226