xref: /btstack/port/stm32-l451-miromico-sx1280/example/gatt_rgb.c (revision f87ef305b409d68c211c8f87ddf873cd6a7a4495)
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 MATTHIAS
24  * RINGWALD 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 static btstack_packet_callback_registration_t hci_event_callback_registration;
69 
70 static bool connected;
71 
72 static void  heartbeat_handler_led_on(struct btstack_timer_source *ts);
73 static void  heartbeat_handler_led_off(struct btstack_timer_source *ts);
74 
75 const uint8_t adv_data[] = {
76         // Flags general discoverable, BR/EDR not supported
77         0x02, 0x01, 0x06,
78         // Service 16-bit: FFF0
79         0x03, 0x02, 0xF0, 0xFF,
80         // Manufacturer specific data (aa bb - address 78:A5:04:82:1B:A4)
81         0x09, 0xFF, 0x04, 0x1E, 0x78, 0xA5, 0x04, 0x82, 0x1A, 0xAA,
82         // Name
83         0x08, 0x09, 'D', 'E', 'L', 'I', 'G', 'H', 'T',
84 };
85 const uint8_t adv_data_len = sizeof(adv_data);
86 
87 static void heartbeat_handler_led_off(struct btstack_timer_source *ts){
88     HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_SET);
89     heartbeat.process = &heartbeat_handler_led_on;
90     btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS - HEARTBEAT_BLINK_MS);
91     btstack_run_loop_add_timer(ts);
92 }
93 
94 static void heartbeat_handler_led_on(struct btstack_timer_source *ts){
95     HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
96     printf("BLINK\n");
97     heartbeat.process = &heartbeat_handler_led_off;
98     btstack_run_loop_set_timer(ts, HEARTBEAT_BLINK_MS);
99     btstack_run_loop_add_timer(ts);
100 }
101 
102 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
103     UNUSED(channel);
104     UNUSED(size);
105 
106     switch (packet_type) {
107         case HCI_EVENT_PACKET:
108             switch (hci_event_packet_get_type(packet)) {
109                 case HCI_EVENT_LE_META:
110                     switch (hci_event_le_meta_get_subevent_code(packet)){
111                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
112                             printf("Connected\n");
113                             btstack_run_loop_remove_timer(&heartbeat);
114                             HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
115                             break;
116                         default:
117                             break;
118                     }
119                     break;
120                 case HCI_EVENT_DISCONNECTION_COMPLETE:
121                     printf("Disconnected\n");
122                     // restart heartbeat
123                     heartbeat_handler_led_on(&heartbeat);
124                     break;
125             }
126             break;
127     }
128 }
129 
130 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){
131     UNUSED(connection_handle);
132     return 0;
133 }
134 
135 void set_rgb(uint8_t r, uint8_t g, uint8_t b){
136     printf("R: %u,  G: %u, B: %u\n", r, g, b);
137     HAL_GPIO_WritePin(LED_R_PORT, LED_R_PIN, r > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
138     HAL_GPIO_WritePin(LED_G_PORT, LED_G_PIN, g > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
139     HAL_GPIO_WritePin(LED_B_PORT, LED_B_PIN, b > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
140 }
141 
142 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){
143     UNUSED(transaction_mode);
144     UNUSED(offset);
145     UNUSED(buffer_size);
146 
147     if (att_handle == ATT_CHARACTERISTIC_FFF3_01_VALUE_HANDLE) {
148         set_rgb(buffer[4], buffer[5], buffer[6]);
149     }
150     return 0;
151 }
152 
153 int btstack_main(void);
154 int btstack_main(void)
155 {
156     l2cap_init();
157 
158     // setup le device db
159     le_device_db_init();
160 
161     // setup SM: Display only
162     sm_init();
163 
164     // setup ATT server
165     att_server_init(profile_data, att_read_callback, att_write_callback);
166 
167     // setup advertisements
168     uint16_t adv_int_min = 0x0030;
169     uint16_t adv_int_max = 0x0030;
170     uint8_t adv_type = 0;
171     bd_addr_t null_addr;
172     memset(null_addr, 0, 6);
173     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
174     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
175     gap_advertisements_enable(1);
176 
177     // register for ATT event
178     att_server_register_packet_handler(packet_handler);
179 
180     // set one-shot timer
181     heartbeat.process = &heartbeat_handler_led_on;
182     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
183     btstack_run_loop_add_timer(&heartbeat);
184 
185     // LEDs init
186     GPIO_InitTypeDef GPIO_InitStruct = {0};
187 
188     // enable GPIO for Port H
189     __HAL_RCC_GPIOH_CLK_ENABLE();
190 
191     GPIO_InitStruct.Pin = LED_SMALL_PIN;
192     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
193     GPIO_InitStruct.Pull = GPIO_NOPULL;
194     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
195     HAL_GPIO_Init(LED_SMALL_PORT, &GPIO_InitStruct);
196 
197     GPIO_InitStruct.Pin = LED_R_PIN;
198     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
199     GPIO_InitStruct.Pull = GPIO_NOPULL;
200     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
201     HAL_GPIO_Init(LED_R_PORT, &GPIO_InitStruct);
202 
203     GPIO_InitStruct.Pin = LED_G_PIN;
204     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
205     GPIO_InitStruct.Pull = GPIO_NOPULL;
206     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
207     HAL_GPIO_Init(LED_G_PORT, &GPIO_InitStruct);
208 
209     GPIO_InitStruct.Pin = LED_B_PIN;
210     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
211     GPIO_InitStruct.Pull = GPIO_NOPULL;
212     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
213     HAL_GPIO_Init(LED_B_PORT, &GPIO_InitStruct);
214 
215     HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
216 
217     HAL_GPIO_WritePin(LED_R_PORT, LED_R_PIN, GPIO_PIN_SET);
218     HAL_GPIO_WritePin(LED_G_PORT, LED_G_PIN, GPIO_PIN_SET);
219     HAL_GPIO_WritePin(LED_B_PORT, LED_B_PIN, GPIO_PIN_SET);
220 
221 
222     // turn on!
223 	hci_power_control(HCI_POWER_ON);
224 
225     return 0;
226 }
227 /* EXAMPLE_END */
228