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