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