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__ "ant_test.c"
39
40 // *****************************************************************************
41 // ANT + SPP Counter demo for TI CC2567
42 // - it provides a SPP port and and sends a counter every second
43 // - it also listens on ANT channel 33,1,1
44 // *****************************************************************************
45
46 #include <stdio.h>
47
48 #include "btstack_chipset_cc256x.h"
49
50 #include "btstack.h"
51 #include "ant_cmd.h"
52
53 #define RFCOMM_SERVER_CHANNEL 1
54 #define HEARTBEAT_PERIOD_MS 1000
55
56 static uint8_t rfcomm_channel_nr = 1;
57 static uint16_t rfcomm_channel_id = 0;
58 static uint8_t spp_service_buffer[100];
59 static btstack_timer_source_t heartbeat;
60
61 static btstack_packet_callback_registration_t hci_event_callback_registration;
62
63 // ant logic
64 static enum {
65 ANT_IDLE,
66 ANT_SEND_RESET,
67 ANT_W4_RESET_COMPLETE,
68 ANT_SEND_ASSIGN_CHANNEL,
69 ANT_W4_ASSIGN_CHANNEL_COMPLETE,
70 ANT_SEND_SET_CHANNEL_ID,
71 ANT_W4_SET_CHANNEL_ID_COMPLETE,
72 ANT_SEND_CHANNEL_OPEN,
73 ANT_W4_CHANNEL_OPEN_COMPLETE,
74 ANT_ACTIVE
75 } ant_state = ANT_IDLE;
76
ant_run(void)77 static void ant_run(void){
78 // check if ANT/HCI command can be sent
79 if (!hci_can_send_command_packet_now()) return;
80
81 // send next command
82 switch(ant_state){
83 case ANT_SEND_RESET:
84 ant_state = ANT_W4_RESET_COMPLETE;
85 // 1. reset
86 printf("Send ANT Reset\n");
87 ant_send_cmd(&ant_reset);
88 break;
89 case ANT_SEND_ASSIGN_CHANNEL:
90 ant_state = ANT_W4_ASSIGN_CHANNEL_COMPLETE;
91 // 2. assign channel
92 printf("Send Assign Channel\n");
93 ant_send_cmd(&ant_assign_channel, 0, 0x00, 0);
94 break;
95 case ANT_SEND_SET_CHANNEL_ID:
96 ant_state = ANT_W4_SET_CHANNEL_ID_COMPLETE;
97 // 3. set channel ID
98 printf("Send Set Channel ID\n");
99 ant_send_cmd(&ant_channel_id, 0, 33, 1, 1);
100 break;
101 case ANT_SEND_CHANNEL_OPEN:
102 ant_state = ANT_W4_CHANNEL_OPEN_COMPLETE;
103 // 4. open channel
104 printf("Send Channel Open\n");
105 ant_send_cmd(&ant_open_channel, 0);
106 break;
107 default:
108 break;
109 }
110 }
111
112 // Bluetooth logic
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)113 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
114 bd_addr_t event_addr;
115 uint8_t rfcomm_channel_nr;
116 uint16_t mtu;
117
118 uint8_t event_code;
119 // uint8_t channel;
120 uint8_t message_id;
121
122 switch (packet_type) {
123 case HCI_EVENT_PACKET:
124 switch (hci_event_packet_get_type(packet)) {
125
126 case BTSTACK_EVENT_STATE:
127 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
128 printf("BTstack is up and running\n");
129 // start ANT init
130 ant_state = ANT_SEND_RESET;
131 }
132 break;
133
134 case HCI_EVENT_PIN_CODE_REQUEST:
135 // inform about pin code request
136 printf("Pin code request - using '0000'\n\r");
137 reverse_bd_addr(&packet[2], event_addr);
138 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
139 break;
140
141 case RFCOMM_EVENT_INCOMING_CONNECTION:
142 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
143 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
144 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
145 rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
146 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
147 rfcomm_accept_connection(rfcomm_channel_id);
148 break;
149
150 case RFCOMM_EVENT_CHANNEL_OPENED:
151 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
152 if (rfcomm_event_channel_opened_get_status(packet)) {
153 printf("RFCOMM channel open failed, status 0%02x\n", rfcomm_event_channel_opened_get_status(packet));
154 } else {
155 rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
156 mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
157 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
158 }
159 break;
160
161 case RFCOMM_EVENT_CHANNEL_CLOSED:
162 rfcomm_channel_id = 0;
163 break;
164
165
166 case 0xff: // vendor specific -> ANT
167
168 // vendor specific ant message
169 if (packet[2] != 0x00) break;
170 if (packet[3] != 0x05) break;
171
172 event_code = packet[7];
173
174 printf("ANT Event: ");
175 printf_hexdump(packet, size);
176
177 switch(event_code){
178
179 case MESG_STARTUP_MESG_ID:
180 ant_state = ANT_SEND_ASSIGN_CHANNEL;
181 break;
182
183 case MESG_RESPONSE_EVENT_ID:
184 // channel = packet[8];
185 message_id = packet[9];
186 switch (message_id){
187 case MESG_ASSIGN_CHANNEL_ID:
188 ant_state = ANT_SEND_SET_CHANNEL_ID;
189 break;
190 case MESG_CHANNEL_ID_ID:
191 ant_state = ANT_SEND_CHANNEL_OPEN;
192 break;
193 default:
194 break;
195 }
196 break;
197 default:
198 break;
199 }
200 break;
201
202 default:
203 break;
204 }
205 break;
206
207 default:
208 break;
209 }
210
211 ant_run();
212 }
213
heartbeat_handler(struct btstack_timer_source * ts)214 static void heartbeat_handler(struct btstack_timer_source *ts){
215
216 if (rfcomm_channel_id){
217 static int counter = 0;
218 char lineBuffer[30];
219 sprintf(lineBuffer, "BTstack counter %04u\n\r", ++counter);
220 puts(lineBuffer);
221 if (rfcomm_can_send_packet_now(rfcomm_channel_id)){
222 uint8_t status = rfcomm_send(rfcomm_channel_id, (uint8_t*) lineBuffer, strlen(lineBuffer));
223 if (status) {
224 printf("rfcomm_send -> status 0x%02x", status);
225 }
226 }
227 }
228
229 btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
230 btstack_run_loop_add_timer(ts);
231 }
232
233 int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])234 int btstack_main(int argc, const char * argv[]){
235
236 // init L2CAP
237 l2cap_init();
238
239 // init RFCOMM
240 rfcomm_init();
241 rfcomm_register_service(packet_handler, rfcomm_channel_nr, 100); // reserved channel, mtu=100
242
243 // init SDP, create record for SPP and register with SDP
244 sdp_init();
245 memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
246 spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter");
247 sdp_register_service(spp_service_buffer);
248
249 // register for HCI events
250 hci_event_callback_registration.callback = &packet_handler;
251 hci_add_event_handler(&hci_event_callback_registration);
252
253 // set local name
254 gap_set_local_name("ANT Demo");
255 // make discoverable
256 gap_discoverable_control(1);
257
258 // set one-shot timer
259 heartbeat.process = &heartbeat_handler;
260 btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
261 btstack_run_loop_add_timer(&heartbeat);
262
263 printf("Run...\n\r");
264 // turn on!
265 hci_power_control(HCI_POWER_ON);
266 return 0;
267 }
268
269