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 "btstack.h"
50 #include "gatt_rgb.h"
51
52 #include "hal_data.h"
53
54 #define HEARTBEAT_PERIOD_MS 2000
55 #define HEARTBEAT_BLINK_MS 200
56
57 static btstack_timer_source_t heartbeat;
58
59 static void heartbeat_handler_led_on(struct btstack_timer_source *ts);
60 static void heartbeat_handler_led_off(struct btstack_timer_source *ts);
61
62 const uint8_t adv_data[] = {
63 // Flags general discoverable, BR/EDR not supported
64 0x02, 0x01, 0x06,
65 // Service 16-bit: FFF0
66 0x03, 0x02, 0xF0, 0xFF,
67 // Manufacturer specific data (aa bb - address 78:A5:04:82:1B:A4)
68 0x09, 0xFF, 0x04, 0x1E, 0x78, 0xA5, 0x04, 0x82, 0x1A, 0xAA,
69 // Name
70 0x08, 0x09, 'D', 'E', 'L', 'I', 'G', 'H', 'T',
71 };
72 const uint8_t adv_data_len = sizeof(adv_data);
73
heartbeat_handler_led_off(struct btstack_timer_source * ts)74 static void heartbeat_handler_led_off(struct btstack_timer_source *ts){
75 heartbeat.process = &heartbeat_handler_led_on;
76 btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS - HEARTBEAT_BLINK_MS);
77 btstack_run_loop_add_timer(ts);
78 }
79
heartbeat_handler_led_on(struct btstack_timer_source * ts)80 static void heartbeat_handler_led_on(struct btstack_timer_source *ts){
81 printf("BLINK\n");
82 heartbeat.process = &heartbeat_handler_led_off;
83 btstack_run_loop_set_timer(ts, HEARTBEAT_BLINK_MS);
84 btstack_run_loop_add_timer(ts);
85 }
86
set_rgb(uint8_t r,uint8_t g,uint8_t b)87 static void set_rgb(uint8_t r, uint8_t g, uint8_t b){
88 printf("R: %u, G: %u, B: %u\n", r, g, b);
89 // test LEDs
90 R_IOPORT_PinWrite(&g_ioport_ctrl, LED3, r > 128 ? BSP_IO_LEVEL_HIGH : BSP_IO_LEVEL_LOW);
91 R_IOPORT_PinWrite(&g_ioport_ctrl, LED2, g > 128 ? BSP_IO_LEVEL_HIGH : BSP_IO_LEVEL_LOW);
92 R_IOPORT_PinWrite(&g_ioport_ctrl, LED1, b > 128 ? BSP_IO_LEVEL_HIGH : BSP_IO_LEVEL_LOW);
93 }
94
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)95 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
96 UNUSED(channel);
97 UNUSED(size);
98
99 switch (packet_type) {
100 case HCI_EVENT_PACKET:
101 switch (hci_event_packet_get_type(packet)) {
102 case HCI_EVENT_LE_META:
103 switch (hci_event_le_meta_get_subevent_code(packet)){
104 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
105 printf("Connected\n");
106 btstack_run_loop_remove_timer(&heartbeat);
107 break;
108 default:
109 break;
110 }
111 break;
112 case HCI_EVENT_DISCONNECTION_COMPLETE:
113 printf("Disconnected\n");
114 // restart heartbeat
115 set_rgb(0,0,0);
116 heartbeat_handler_led_on(&heartbeat);
117 break;
118 }
119 break;
120 }
121 }
122
att_read_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)123 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){
124 UNUSED(connection_handle);
125 UNUSED(att_handle);
126 UNUSED(offset);
127 UNUSED(buffer);
128 UNUSED(buffer_size);
129 return 0;
130 }
131
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)132 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){
133 UNUSED(connection_handle);
134 UNUSED(transaction_mode);
135 UNUSED(offset);
136 UNUSED(buffer_size);
137
138 if (att_handle == ATT_CHARACTERISTIC_FFF3_01_VALUE_HANDLE) {
139 set_rgb(buffer[4], buffer[5], buffer[6]);
140 }
141 return 0;
142 }
143
144 int btstack_main(void);
btstack_main(void)145 int btstack_main(void)
146 {
147 l2cap_init();
148
149 // setup le device db
150 le_device_db_init();
151
152 // setup SM: Display only
153 sm_init();
154
155 // setup ATT server
156 att_server_init(profile_data, att_read_callback, att_write_callback);
157
158 // setup advertisements
159 uint16_t adv_int_min = 0x0030;
160 uint16_t adv_int_max = 0x0030;
161 uint8_t adv_type = 0;
162 bd_addr_t null_addr;
163 memset(null_addr, 0, 6);
164 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
165 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
166 gap_advertisements_enable(1);
167
168 // register for ATT event
169 att_server_register_packet_handler(packet_handler);
170
171 // set one-shot timer
172 heartbeat.process = &heartbeat_handler_led_on;
173 btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
174 btstack_run_loop_add_timer(&heartbeat);
175
176 // turn off leds
177 set_rgb(0,0,0);
178
179 // turn on!
180 hci_power_control(HCI_POWER_ON);
181
182 return 0;
183 }
184 /* EXAMPLE_END */
185