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