xref: /btstack/example/dut_mode_classic.c (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
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__ "dut_mode_classic.c"
39 
40 // *****************************************************************************
41 /* EXAMPLE_START(dut_mode_classic): Testing - Enable Device Under Test (DUT) Mode for Classic
42  *
43  * @text DUT mode can be used for production testing. This example just configures
44  * the Bluetooth Controller for DUT mode.
45  */
46 // *****************************************************************************
47 
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "btstack.h"
54 
55 static btstack_packet_callback_registration_t hci_event_callback_registration;
56 
57 /* @section Bluetooth Logic
58  *
59  * @text When BTstack is up and running, send Enable Device Under Test Mode Command and
60  * print its result.
61  */
62 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)63 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
64     UNUSED(channel);
65     UNUSED(size);
66 
67     static int enable_dut_mode = 0;
68 
69     // only handle HCI Events
70     if (packet_type != HCI_EVENT_PACKET) return;
71 
72     // wait for stack for complete startup
73     switch(hci_event_packet_get_type(packet)){
74         case BTSTACK_EVENT_STATE:
75             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
76                 enable_dut_mode = 1;
77             }
78             break;
79         case HCI_EVENT_COMMAND_COMPLETE:
80             if (hci_event_command_complete_get_command_opcode(packet) == hci_enable_device_under_test_mode.opcode){
81                 uint8_t status = hci_event_command_complete_get_return_parameters(packet)[0];
82                 printf("Enable Device Under Test Mode: %s\n", (status != ERROR_CODE_SUCCESS) ? "Failed" : "OK");
83             }
84             break;
85         default:
86             break;
87     }
88 
89     // enable DUT mode when ready
90     if (enable_dut_mode && hci_can_send_command_packet_now()){
91         enable_dut_mode = 0;
92         hci_send_cmd(&hci_enable_device_under_test_mode);
93     }
94 }
95 
96 /* @text For more details on discovering remote devices, please see
97  * Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices).
98  */
99 
100 
101 /* @section Main Application Setup
102  *
103  * @text Listing MainConfiguration shows main application code.
104  * It registers the HCI packet handler and starts the Bluetooth stack.
105  */
106 
107 /* LISTING_START(MainConfiguration): Setup packet handler  */
108 int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])109 int btstack_main(int argc, const char * argv[]) {
110     (void)argc;
111     (void)argv;
112 
113     // disable Secure Simple Pairinng
114     gap_ssp_set_enable(0);
115 
116     // make device connectable
117     // @note: gap_connectable_control will be enabled when an L2CAP service
118     // (e.g. RFCOMM) is initialized). Therefore, it's not needed in regular applications
119     gap_connectable_control(1);
120 
121     // make device discoverable
122     gap_discoverable_control(1);
123 
124     hci_event_callback_registration.callback = &packet_handler;
125     hci_add_event_handler(&hci_event_callback_registration);
126 
127     // turn on!
128     hci_power_control(HCI_POWER_ON);
129 
130     return 0;
131 }
132 /* LISTING_END */
133 /* EXAMPLE_END */
134