xref: /btstack/example/ant_test.c (revision fcd55a0b5a8c0d39e1cebac18e790e1dbf38bd64)
195c72533SMatthias Ringwald /*
295c72533SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
395c72533SMatthias Ringwald  *
495c72533SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
595c72533SMatthias Ringwald  * modification, are permitted provided that the following conditions
695c72533SMatthias Ringwald  * are met:
795c72533SMatthias Ringwald  *
895c72533SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
995c72533SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
1095c72533SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1195c72533SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
1295c72533SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
1395c72533SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
1495c72533SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
1595c72533SMatthias Ringwald  *    from this software without specific prior written permission.
1695c72533SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
1795c72533SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
1895c72533SMatthias Ringwald  *    monetary gain.
1995c72533SMatthias Ringwald  *
2095c72533SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2195c72533SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2295c72533SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2595c72533SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2695c72533SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2795c72533SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2895c72533SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2995c72533SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3095c72533SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3195c72533SMatthias Ringwald  * SUCH DAMAGE.
3295c72533SMatthias Ringwald  *
3395c72533SMatthias Ringwald  * Please inquire about commercial licensing options at
3495c72533SMatthias Ringwald  * [email protected]
3595c72533SMatthias Ringwald  *
3695c72533SMatthias Ringwald  */
3795c72533SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "ant_test.c"
39bb2a7656SMatthias Ringwald 
4095c72533SMatthias Ringwald // *****************************************************************************
4195c72533SMatthias Ringwald // ANT + SPP Counter demo for TI CC2567
4295c72533SMatthias Ringwald // - it provides a SPP port and and sends a counter every second
4395c72533SMatthias Ringwald // - it also listens on ANT channel 33,1,1
4495c72533SMatthias Ringwald // *****************************************************************************
4595c72533SMatthias Ringwald 
4695c72533SMatthias Ringwald #include <stdio.h>
4795c72533SMatthias Ringwald 
4895c72533SMatthias Ringwald #include "btstack_chipset_cc256x.h"
4995c72533SMatthias Ringwald 
5095c72533SMatthias Ringwald #include "btstack.h"
5195c72533SMatthias Ringwald #include "ant_cmd.h"
5295c72533SMatthias Ringwald 
5395c72533SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1
5495c72533SMatthias Ringwald #define HEARTBEAT_PERIOD_MS 1000
5595c72533SMatthias Ringwald 
5695c72533SMatthias Ringwald static uint8_t   rfcomm_channel_nr = 1;
5795c72533SMatthias Ringwald static uint16_t  rfcomm_channel_id = 0;
5895c72533SMatthias Ringwald static uint8_t   spp_service_buffer[100];
5995c72533SMatthias Ringwald static btstack_timer_source_t heartbeat;
6095c72533SMatthias Ringwald 
6195c72533SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
6295c72533SMatthias Ringwald 
6395c72533SMatthias Ringwald // ant logic
6495c72533SMatthias Ringwald static enum {
6595c72533SMatthias Ringwald     ANT_IDLE,
6695c72533SMatthias Ringwald     ANT_SEND_RESET,
6795c72533SMatthias Ringwald     ANT_W4_RESET_COMPLETE,
6895c72533SMatthias Ringwald     ANT_SEND_ASSIGN_CHANNEL,
6995c72533SMatthias Ringwald     ANT_W4_ASSIGN_CHANNEL_COMPLETE,
7095c72533SMatthias Ringwald     ANT_SEND_SET_CHANNEL_ID,
7195c72533SMatthias Ringwald     ANT_W4_SET_CHANNEL_ID_COMPLETE,
7295c72533SMatthias Ringwald     ANT_SEND_CHANNEL_OPEN,
7395c72533SMatthias Ringwald     ANT_W4_CHANNEL_OPEN_COMPLETE,
7495c72533SMatthias Ringwald     ANT_ACTIVE
7595c72533SMatthias Ringwald } ant_state = ANT_IDLE;
7695c72533SMatthias Ringwald 
ant_run(void)7795c72533SMatthias Ringwald static void ant_run(void){
7895c72533SMatthias Ringwald     // check if ANT/HCI command can be sent
7995c72533SMatthias Ringwald     if (!hci_can_send_command_packet_now()) return;
8095c72533SMatthias Ringwald 
8195c72533SMatthias Ringwald     // send next command
8295c72533SMatthias Ringwald     switch(ant_state){
8395c72533SMatthias Ringwald         case ANT_SEND_RESET:
8495c72533SMatthias Ringwald             ant_state = ANT_W4_RESET_COMPLETE;
8595c72533SMatthias Ringwald             // 1. reset
8695c72533SMatthias Ringwald             printf("Send ANT Reset\n");
8795c72533SMatthias Ringwald             ant_send_cmd(&ant_reset);
8895c72533SMatthias Ringwald             break;
8995c72533SMatthias Ringwald         case ANT_SEND_ASSIGN_CHANNEL:
9095c72533SMatthias Ringwald             ant_state = ANT_W4_ASSIGN_CHANNEL_COMPLETE;
9195c72533SMatthias Ringwald             // 2. assign channel
9295c72533SMatthias Ringwald             printf("Send Assign Channel\n");
9395c72533SMatthias Ringwald             ant_send_cmd(&ant_assign_channel, 0, 0x00, 0);
9495c72533SMatthias Ringwald             break;
9595c72533SMatthias Ringwald         case ANT_SEND_SET_CHANNEL_ID:
9695c72533SMatthias Ringwald             ant_state = ANT_W4_SET_CHANNEL_ID_COMPLETE;
9795c72533SMatthias Ringwald             // 3. set channel ID
9895c72533SMatthias Ringwald             printf("Send Set Channel ID\n");
9995c72533SMatthias Ringwald             ant_send_cmd(&ant_channel_id, 0, 33, 1, 1);
10095c72533SMatthias Ringwald             break;
10195c72533SMatthias Ringwald         case ANT_SEND_CHANNEL_OPEN:
10295c72533SMatthias Ringwald             ant_state = ANT_W4_CHANNEL_OPEN_COMPLETE;
10395c72533SMatthias Ringwald             // 4. open channel
10495c72533SMatthias Ringwald             printf("Send Channel Open\n");
10595c72533SMatthias Ringwald             ant_send_cmd(&ant_open_channel, 0);
10695c72533SMatthias Ringwald             break;
10795c72533SMatthias Ringwald         default:
10895c72533SMatthias Ringwald             break;
10995c72533SMatthias Ringwald     }
11095c72533SMatthias Ringwald }
11195c72533SMatthias Ringwald 
11295c72533SMatthias Ringwald // Bluetooth logic
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)11395c72533SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
11495c72533SMatthias Ringwald     bd_addr_t event_addr;
11595c72533SMatthias Ringwald     uint8_t   rfcomm_channel_nr;
11695c72533SMatthias Ringwald     uint16_t  mtu;
11795c72533SMatthias Ringwald 
11895c72533SMatthias Ringwald     uint8_t event_code;
11995c72533SMatthias Ringwald 	// uint8_t channel;
12095c72533SMatthias Ringwald 	uint8_t message_id;
12195c72533SMatthias Ringwald 
12295c72533SMatthias Ringwald 	switch (packet_type) {
12395c72533SMatthias Ringwald 		case HCI_EVENT_PACKET:
12495c72533SMatthias Ringwald 			switch (hci_event_packet_get_type(packet)) {
12595c72533SMatthias Ringwald 
12695c72533SMatthias Ringwald 				case BTSTACK_EVENT_STATE:
12795c72533SMatthias Ringwald 					if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
12895c72533SMatthias Ringwald                         printf("BTstack is up and running\n");
12995c72533SMatthias Ringwald                         // start ANT init
13095c72533SMatthias Ringwald                         ant_state = ANT_SEND_RESET;
13195c72533SMatthias Ringwald 					}
13295c72533SMatthias Ringwald 					break;
13395c72533SMatthias Ringwald 
13495c72533SMatthias Ringwald 				case HCI_EVENT_PIN_CODE_REQUEST:
13595c72533SMatthias Ringwald 					// inform about pin code request
13695c72533SMatthias Ringwald                     printf("Pin code request - using '0000'\n\r");
13795c72533SMatthias Ringwald                     reverse_bd_addr(&packet[2], event_addr);
13895c72533SMatthias Ringwald 					hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
13995c72533SMatthias Ringwald 					break;
14095c72533SMatthias Ringwald 
14195c72533SMatthias Ringwald                 case RFCOMM_EVENT_INCOMING_CONNECTION:
14295c72533SMatthias Ringwald                     // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
14395c72533SMatthias Ringwald                     rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
14495c72533SMatthias Ringwald                     rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
14595c72533SMatthias Ringwald                     rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
14695c72533SMatthias Ringwald                     printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
14795c72533SMatthias Ringwald                     rfcomm_accept_connection(rfcomm_channel_id);
14895c72533SMatthias Ringwald                     break;
14995c72533SMatthias Ringwald 
15095c72533SMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_OPENED:
15195c72533SMatthias Ringwald                     // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
15295c72533SMatthias Ringwald                     if (rfcomm_event_channel_opened_get_status(packet)) {
153*fcd55a0bSMilanka Ringwald                         printf("RFCOMM channel open failed, status 0%02x\n", rfcomm_event_channel_opened_get_status(packet));
15495c72533SMatthias Ringwald                     } else {
15595c72533SMatthias Ringwald                         rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
15695c72533SMatthias Ringwald                         mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
15795c72533SMatthias Ringwald                         printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
15895c72533SMatthias Ringwald                     }
15995c72533SMatthias Ringwald                     break;
16095c72533SMatthias Ringwald 
16195c72533SMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_CLOSED:
16295c72533SMatthias Ringwald                     rfcomm_channel_id = 0;
16395c72533SMatthias Ringwald                     break;
16495c72533SMatthias Ringwald 
16595c72533SMatthias Ringwald 
16695c72533SMatthias Ringwald                 case 0xff:	// vendor specific -> ANT
16795c72533SMatthias Ringwald 
16895c72533SMatthias Ringwald                 	// vendor specific ant message
16995c72533SMatthias Ringwald                 	if (packet[2] != 0x00) break;
17095c72533SMatthias Ringwald                 	if (packet[3] != 0x05) break;
17195c72533SMatthias Ringwald 
17295c72533SMatthias Ringwald 					event_code = packet[7];
17395c72533SMatthias Ringwald 
17495c72533SMatthias Ringwald                 	printf("ANT Event: ");
17595c72533SMatthias Ringwald                 	printf_hexdump(packet, size);
17695c72533SMatthias Ringwald 
17795c72533SMatthias Ringwald 					switch(event_code){
17895c72533SMatthias Ringwald 
17995c72533SMatthias Ringwald 						case MESG_STARTUP_MESG_ID:
18095c72533SMatthias Ringwald                             ant_state = ANT_SEND_ASSIGN_CHANNEL;
18195c72533SMatthias Ringwald 							break;
18295c72533SMatthias Ringwald 
18395c72533SMatthias Ringwald 						case MESG_RESPONSE_EVENT_ID:
18495c72533SMatthias Ringwald 							// channel    = packet[8];
18595c72533SMatthias Ringwald 							message_id = packet[9];
18695c72533SMatthias Ringwald 							switch (message_id){
18795c72533SMatthias Ringwald 								case MESG_ASSIGN_CHANNEL_ID:
18895c72533SMatthias Ringwald                                     ant_state = ANT_SEND_SET_CHANNEL_ID;
18995c72533SMatthias Ringwald 									break;
19095c72533SMatthias Ringwald 								case MESG_CHANNEL_ID_ID:
19195c72533SMatthias Ringwald                                     ant_state = ANT_SEND_CHANNEL_OPEN;
19295c72533SMatthias Ringwald                                     break;
19395c72533SMatthias Ringwald                                 default:
19495c72533SMatthias Ringwald                                     break;
19595c72533SMatthias Ringwald 							}
19695c72533SMatthias Ringwald 							break;
19795c72533SMatthias Ringwald 						default:
19895c72533SMatthias Ringwald 							break;
19995c72533SMatthias Ringwald 					}
20095c72533SMatthias Ringwald                 	break;
20195c72533SMatthias Ringwald 
20295c72533SMatthias Ringwald 				default:
20395c72533SMatthias Ringwald 					break;
20495c72533SMatthias Ringwald 			}
20595c72533SMatthias Ringwald             break;
20695c72533SMatthias Ringwald 
20795c72533SMatthias Ringwald         default:
20895c72533SMatthias Ringwald             break;
20995c72533SMatthias Ringwald 	}
21095c72533SMatthias Ringwald 
21195c72533SMatthias Ringwald     ant_run();
21295c72533SMatthias Ringwald }
21395c72533SMatthias Ringwald 
heartbeat_handler(struct btstack_timer_source * ts)21495c72533SMatthias Ringwald static void  heartbeat_handler(struct btstack_timer_source *ts){
21595c72533SMatthias Ringwald 
21695c72533SMatthias Ringwald     if (rfcomm_channel_id){
21795c72533SMatthias Ringwald         static int counter = 0;
21895c72533SMatthias Ringwald         char lineBuffer[30];
21995c72533SMatthias Ringwald         sprintf(lineBuffer, "BTstack counter %04u\n\r", ++counter);
22095c72533SMatthias Ringwald         puts(lineBuffer);
22195c72533SMatthias Ringwald         if (rfcomm_can_send_packet_now(rfcomm_channel_id)){
222*fcd55a0bSMilanka Ringwald             uint8_t status = rfcomm_send(rfcomm_channel_id, (uint8_t*) lineBuffer, strlen(lineBuffer));
223*fcd55a0bSMilanka Ringwald             if (status) {
224*fcd55a0bSMilanka Ringwald                 printf("rfcomm_send -> status 0x%02x", status);
22595c72533SMatthias Ringwald             }
22695c72533SMatthias Ringwald         }
22795c72533SMatthias Ringwald     }
22895c72533SMatthias Ringwald 
22995c72533SMatthias Ringwald     btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
23095c72533SMatthias Ringwald     btstack_run_loop_add_timer(ts);
23195c72533SMatthias Ringwald }
23295c72533SMatthias Ringwald 
23395c72533SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])23495c72533SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
23595c72533SMatthias Ringwald 
23695c72533SMatthias Ringwald     // init L2CAP
23795c72533SMatthias Ringwald     l2cap_init();
23895c72533SMatthias Ringwald 
23995c72533SMatthias Ringwald     // init RFCOMM
24095c72533SMatthias Ringwald     rfcomm_init();
24195c72533SMatthias Ringwald     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 100);  // reserved channel, mtu=100
24295c72533SMatthias Ringwald 
24395c72533SMatthias Ringwald     // init SDP, create record for SPP and register with SDP
24495c72533SMatthias Ringwald     sdp_init();
24595c72533SMatthias Ringwald     memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
24695c72533SMatthias Ringwald     spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter");
24795c72533SMatthias Ringwald     sdp_register_service(spp_service_buffer);
248a4fe6467SMatthias Ringwald 
249a4fe6467SMatthias Ringwald     // register for HCI events
250a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
251a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
252a4fe6467SMatthias Ringwald 
253a4fe6467SMatthias Ringwald     // set local name
254a4fe6467SMatthias Ringwald     gap_set_local_name("ANT Demo");
255a4fe6467SMatthias Ringwald     // make discoverable
256a4fe6467SMatthias Ringwald     gap_discoverable_control(1);
25795c72533SMatthias Ringwald 
25895c72533SMatthias Ringwald     // set one-shot timer
25995c72533SMatthias Ringwald     heartbeat.process = &heartbeat_handler;
26095c72533SMatthias Ringwald     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
26195c72533SMatthias Ringwald     btstack_run_loop_add_timer(&heartbeat);
26295c72533SMatthias Ringwald 
26395c72533SMatthias Ringwald 	printf("Run...\n\r");
26495c72533SMatthias Ringwald  	// turn on!
26595c72533SMatthias Ringwald 	hci_power_control(HCI_POWER_ON);
26695c72533SMatthias Ringwald     return 0;
26795c72533SMatthias Ringwald }
26895c72533SMatthias Ringwald 
269