xref: /btstack/port/stm32-l451-miromico-sx1280/example/gatt_rgb.c (revision affe4dee353d8ffbbc6509f89c400df293c36640)
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 void  heartbeat_handler_led_on(struct btstack_timer_source *ts);
71 static void  heartbeat_handler_led_off(struct btstack_timer_source *ts);
72 
73 const uint8_t adv_data[] = {
74         // Flags general discoverable, BR/EDR not supported
75         0x02, 0x01, 0x06,
76         // Service 16-bit: FFF0
77         0x03, 0x02, 0xF0, 0xFF,
78         // Manufacturer specific data (aa bb - address 78:A5:04:82:1B:A4)
79         0x09, 0xFF, 0x04, 0x1E, 0x78, 0xA5, 0x04, 0x82, 0x1A, 0xAA,
80         // Name
81         0x08, 0x09, 'D', 'E', 'L', 'I', 'G', 'H', 'T',
82 };
83 const uint8_t adv_data_len = sizeof(adv_data);
84 
85 static void heartbeat_handler_led_off(struct btstack_timer_source *ts){
86     HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_SET);
87     heartbeat.process = &heartbeat_handler_led_on;
88     btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS - HEARTBEAT_BLINK_MS);
89     btstack_run_loop_add_timer(ts);
90 }
91 
92 static void heartbeat_handler_led_on(struct btstack_timer_source *ts){
93     HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
94     printf("BLINK\n");
95     heartbeat.process = &heartbeat_handler_led_off;
96     btstack_run_loop_set_timer(ts, HEARTBEAT_BLINK_MS);
97     btstack_run_loop_add_timer(ts);
98 }
99 
100 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
101     UNUSED(channel);
102     UNUSED(size);
103 
104     switch (packet_type) {
105         case HCI_EVENT_PACKET:
106             switch (hci_event_packet_get_type(packet)) {
107                 case HCI_EVENT_LE_META:
108                     switch (hci_event_le_meta_get_subevent_code(packet)){
109                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
110                             printf("Connected\n");
111                             btstack_run_loop_remove_timer(&heartbeat);
112                             HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
113                             break;
114                         default:
115                             break;
116                     }
117                     break;
118                 case HCI_EVENT_DISCONNECTION_COMPLETE:
119                     printf("Disconnected\n");
120                     // restart heartbeat
121                     heartbeat_handler_led_on(&heartbeat);
122                     break;
123             }
124             break;
125     }
126 }
127 
128 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){
129     UNUSED(connection_handle);
130     return 0;
131 }
132 
133 void set_rgb(uint8_t r, uint8_t g, uint8_t b){
134     printf("R: %u,  G: %u, B: %u\n", r, g, b);
135     HAL_GPIO_WritePin(LED_R_PORT, LED_R_PIN, r > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
136     HAL_GPIO_WritePin(LED_G_PORT, LED_G_PIN, g > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
137     HAL_GPIO_WritePin(LED_B_PORT, LED_B_PIN, b > 128 ? GPIO_PIN_RESET : GPIO_PIN_SET);
138 }
139 
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);
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 HCI events
176     hci_event_callback_registration.callback = &packet_handler;
177     hci_add_event_handler(&hci_event_callback_registration);
178 
179     // register for ATT event
180     att_server_register_packet_handler(packet_handler);
181 
182     // set one-shot timer
183     heartbeat.process = &heartbeat_handler_led_on;
184     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
185     btstack_run_loop_add_timer(&heartbeat);
186 
187     // LEDs init
188     GPIO_InitTypeDef GPIO_InitStruct = {0};
189 
190     // enable GPIO for Port H
191     __HAL_RCC_GPIOH_CLK_ENABLE();
192 
193     GPIO_InitStruct.Pin = LED_SMALL_PIN;
194     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
195     GPIO_InitStruct.Pull = GPIO_NOPULL;
196     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
197     HAL_GPIO_Init(LED_SMALL_PORT, &GPIO_InitStruct);
198 
199     GPIO_InitStruct.Pin = LED_R_PIN;
200     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
201     GPIO_InitStruct.Pull = GPIO_NOPULL;
202     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
203     HAL_GPIO_Init(LED_R_PORT, &GPIO_InitStruct);
204 
205     GPIO_InitStruct.Pin = LED_G_PIN;
206     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
207     GPIO_InitStruct.Pull = GPIO_NOPULL;
208     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
209     HAL_GPIO_Init(LED_G_PORT, &GPIO_InitStruct);
210 
211     GPIO_InitStruct.Pin = LED_B_PIN;
212     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
213     GPIO_InitStruct.Pull = GPIO_NOPULL;
214     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
215     HAL_GPIO_Init(LED_B_PORT, &GPIO_InitStruct);
216 
217     HAL_GPIO_WritePin(LED_SMALL_PORT, LED_SMALL_PIN, GPIO_PIN_RESET);
218 
219     HAL_GPIO_WritePin(LED_R_PORT, LED_R_PIN, GPIO_PIN_SET);
220     HAL_GPIO_WritePin(LED_G_PORT, LED_G_PIN, GPIO_PIN_SET);
221     HAL_GPIO_WritePin(LED_B_PORT, LED_B_PIN, GPIO_PIN_SET);
222 
223 
224     // turn on!
225 	hci_power_control(HCI_POWER_ON);
226 
227     return 0;
228 }
229 /* EXAMPLE_END */
230