xref: /btstack/example/dut_mode_classic.c (revision 486d2354f0b5317b13fb6690760c10343076d377)
1*486d2354SMatthias Ringwald /*
2*486d2354SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*486d2354SMatthias Ringwald  *
4*486d2354SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*486d2354SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*486d2354SMatthias Ringwald  * are met:
7*486d2354SMatthias Ringwald  *
8*486d2354SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*486d2354SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*486d2354SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*486d2354SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*486d2354SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*486d2354SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*486d2354SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*486d2354SMatthias Ringwald  *    from this software without specific prior written permission.
16*486d2354SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*486d2354SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*486d2354SMatthias Ringwald  *    monetary gain.
19*486d2354SMatthias Ringwald  *
20*486d2354SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*486d2354SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*486d2354SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*486d2354SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*486d2354SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*486d2354SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*486d2354SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*486d2354SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*486d2354SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*486d2354SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*486d2354SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*486d2354SMatthias Ringwald  * SUCH DAMAGE.
32*486d2354SMatthias Ringwald  *
33*486d2354SMatthias Ringwald  * Please inquire about commercial licensing options at
34*486d2354SMatthias Ringwald  * [email protected]
35*486d2354SMatthias Ringwald  *
36*486d2354SMatthias Ringwald  */
37*486d2354SMatthias Ringwald 
38*486d2354SMatthias Ringwald #define __BTSTACK_FILE__ "dut_mode_classic.c"
39*486d2354SMatthias Ringwald 
40*486d2354SMatthias Ringwald // *****************************************************************************
41*486d2354SMatthias Ringwald /* EXAMPLE_START(dut_mode_classic): Enable Device Under Test (DUT) Mode for BR/EDR
42*486d2354SMatthias Ringwald  *
43*486d2354SMatthias Ringwald  * @text DUT mode can be used for production testing. This example just configures
44*486d2354SMatthias Ringwald  * the Bluetooth Controller for DUT mode
45*486d2354SMatthias Ringwald  */
46*486d2354SMatthias Ringwald // *****************************************************************************
47*486d2354SMatthias Ringwald 
48*486d2354SMatthias Ringwald #include <stdint.h>
49*486d2354SMatthias Ringwald #include <stdio.h>
50*486d2354SMatthias Ringwald #include <stdlib.h>
51*486d2354SMatthias Ringwald #include <string.h>
52*486d2354SMatthias Ringwald 
53*486d2354SMatthias Ringwald #include "btstack.h"
54*486d2354SMatthias Ringwald 
55*486d2354SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
56*486d2354SMatthias Ringwald 
57*486d2354SMatthias Ringwald /* @section Bluetooth Logic
58*486d2354SMatthias Ringwald  *
59*486d2354SMatthias Ringwald  * @text The Bluetooth logic is implemented as a state machine within the packet
60*486d2354SMatthias Ringwald  * handler. In this example, the following states are passed sequentially:
61*486d2354SMatthias Ringwald  * INIT, and ACTIVE.
62*486d2354SMatthias Ringwald  */
63*486d2354SMatthias Ringwald 
64*486d2354SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
65*486d2354SMatthias Ringwald     UNUSED(channel);
66*486d2354SMatthias Ringwald     UNUSED(size);
67*486d2354SMatthias Ringwald 
68*486d2354SMatthias Ringwald     static int enable_dut_mode = 0;
69*486d2354SMatthias Ringwald 
70*486d2354SMatthias Ringwald     // only handle HCI Events
71*486d2354SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
72*486d2354SMatthias Ringwald 
73*486d2354SMatthias Ringwald     // wait for stack for complete startup
74*486d2354SMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
75*486d2354SMatthias Ringwald         case BTSTACK_EVENT_STATE:
76*486d2354SMatthias Ringwald             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
77*486d2354SMatthias Ringwald                 enable_dut_mode = 1;
78*486d2354SMatthias Ringwald             }
79*486d2354SMatthias Ringwald             break;
80*486d2354SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
81*486d2354SMatthias Ringwald             if (hci_event_command_complete_get_command_opcode(packet) == hci_enable_device_under_test_mode.opcode){
82*486d2354SMatthias Ringwald                 uint8_t status = hci_event_command_complete_get_return_parameters(packet)[0];
83*486d2354SMatthias Ringwald                 printf("Enable Device Under Test Mode: %s\n", status ? "Failed" : "OK");
84*486d2354SMatthias Ringwald             }
85*486d2354SMatthias Ringwald             break;
86*486d2354SMatthias Ringwald         default:
87*486d2354SMatthias Ringwald             break;
88*486d2354SMatthias Ringwald     }
89*486d2354SMatthias Ringwald 
90*486d2354SMatthias Ringwald     // enable DUT mode when ready
91*486d2354SMatthias Ringwald     if (enable_dut_mode && hci_can_send_command_packet_now()){
92*486d2354SMatthias Ringwald         enable_dut_mode = 0;
93*486d2354SMatthias Ringwald         hci_send_cmd(&hci_enable_device_under_test_mode);
94*486d2354SMatthias Ringwald     }
95*486d2354SMatthias Ringwald }
96*486d2354SMatthias Ringwald 
97*486d2354SMatthias Ringwald /* @text For more details on discovering remote devices, please see
98*486d2354SMatthias Ringwald  * Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices).
99*486d2354SMatthias Ringwald  */
100*486d2354SMatthias Ringwald 
101*486d2354SMatthias Ringwald 
102*486d2354SMatthias Ringwald /* @section Main Application Setup
103*486d2354SMatthias Ringwald  *
104*486d2354SMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
105*486d2354SMatthias Ringwald  * It registers the HCI packet handler and starts the Bluetooth stack.
106*486d2354SMatthias Ringwald  */
107*486d2354SMatthias Ringwald 
108*486d2354SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup packet handler  */
109*486d2354SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
110*486d2354SMatthias Ringwald int btstack_main(int argc, const char * argv[]) {
111*486d2354SMatthias Ringwald     (void)argc;
112*486d2354SMatthias Ringwald     (void)argv;
113*486d2354SMatthias Ringwald 
114*486d2354SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
115*486d2354SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
116*486d2354SMatthias Ringwald 
117*486d2354SMatthias Ringwald     // make device connectable
118*486d2354SMatthias Ringwald     // @note: gap_connectable_control will be enabled when an L2CAP service
119*486d2354SMatthias Ringwald     // (e.g. RFCOMM) is initialized). Therefore, it's not needed in regular applications
120*486d2354SMatthias Ringwald     gap_connectable_control(1);
121*486d2354SMatthias Ringwald 
122*486d2354SMatthias Ringwald     // make device discoverable
123*486d2354SMatthias Ringwald     gap_discoverable_control(1);
124*486d2354SMatthias Ringwald 
125*486d2354SMatthias Ringwald     // turn on!
126*486d2354SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
127*486d2354SMatthias Ringwald 
128*486d2354SMatthias Ringwald     return 0;
129*486d2354SMatthias Ringwald }
130*486d2354SMatthias Ringwald /* LISTING_END */
131*486d2354SMatthias Ringwald /* EXAMPLE_END */
132