xref: /btstack/example/dut_mode_classic.c (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
1486d2354SMatthias Ringwald /*
2486d2354SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3486d2354SMatthias Ringwald  *
4486d2354SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5486d2354SMatthias Ringwald  * modification, are permitted provided that the following conditions
6486d2354SMatthias Ringwald  * are met:
7486d2354SMatthias Ringwald  *
8486d2354SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9486d2354SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10486d2354SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11486d2354SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12486d2354SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13486d2354SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14486d2354SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15486d2354SMatthias Ringwald  *    from this software without specific prior written permission.
16486d2354SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17486d2354SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18486d2354SMatthias Ringwald  *    monetary gain.
19486d2354SMatthias Ringwald  *
20486d2354SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21486d2354SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22486d2354SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25486d2354SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26486d2354SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27486d2354SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28486d2354SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29486d2354SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30486d2354SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31486d2354SMatthias Ringwald  * SUCH DAMAGE.
32486d2354SMatthias Ringwald  *
33486d2354SMatthias Ringwald  * Please inquire about commercial licensing options at
34486d2354SMatthias Ringwald  * [email protected]
35486d2354SMatthias Ringwald  *
36486d2354SMatthias Ringwald  */
37486d2354SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "dut_mode_classic.c"
39486d2354SMatthias Ringwald 
40486d2354SMatthias Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(dut_mode_classic): Testing - Enable Device Under Test (DUT) Mode for Classic
42486d2354SMatthias Ringwald  *
43486d2354SMatthias Ringwald  * @text DUT mode can be used for production testing. This example just configures
4458d7a529SMilanka Ringwald  * the Bluetooth Controller for DUT mode.
45486d2354SMatthias Ringwald  */
46486d2354SMatthias Ringwald // *****************************************************************************
47486d2354SMatthias Ringwald 
48486d2354SMatthias Ringwald #include <stdint.h>
49486d2354SMatthias Ringwald #include <stdio.h>
50486d2354SMatthias Ringwald #include <stdlib.h>
51486d2354SMatthias Ringwald #include <string.h>
52486d2354SMatthias Ringwald 
53486d2354SMatthias Ringwald #include "btstack.h"
54486d2354SMatthias Ringwald 
55486d2354SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
56486d2354SMatthias Ringwald 
57486d2354SMatthias Ringwald /* @section Bluetooth Logic
58486d2354SMatthias Ringwald  *
59a75ba47bSMatthias Ringwald  * @text When BTstack is up and running, send Enable Device Under Test Mode Command and
60a75ba47bSMatthias Ringwald  * print its result.
61486d2354SMatthias Ringwald  */
62486d2354SMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)63486d2354SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
64486d2354SMatthias Ringwald     UNUSED(channel);
65486d2354SMatthias Ringwald     UNUSED(size);
66486d2354SMatthias Ringwald 
67486d2354SMatthias Ringwald     static int enable_dut_mode = 0;
68486d2354SMatthias Ringwald 
69486d2354SMatthias Ringwald     // only handle HCI Events
70486d2354SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
71486d2354SMatthias Ringwald 
72486d2354SMatthias Ringwald     // wait for stack for complete startup
73486d2354SMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
74486d2354SMatthias Ringwald         case BTSTACK_EVENT_STATE:
75486d2354SMatthias Ringwald             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
76486d2354SMatthias Ringwald                 enable_dut_mode = 1;
77486d2354SMatthias Ringwald             }
78486d2354SMatthias Ringwald             break;
79486d2354SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
80486d2354SMatthias Ringwald             if (hci_event_command_complete_get_command_opcode(packet) == hci_enable_device_under_test_mode.opcode){
81486d2354SMatthias Ringwald                 uint8_t status = hci_event_command_complete_get_return_parameters(packet)[0];
826058cb0dSMatthias Ringwald                 printf("Enable Device Under Test Mode: %s\n", (status != ERROR_CODE_SUCCESS) ? "Failed" : "OK");
83486d2354SMatthias Ringwald             }
84486d2354SMatthias Ringwald             break;
85486d2354SMatthias Ringwald         default:
86486d2354SMatthias Ringwald             break;
87486d2354SMatthias Ringwald     }
88486d2354SMatthias Ringwald 
89486d2354SMatthias Ringwald     // enable DUT mode when ready
90486d2354SMatthias Ringwald     if (enable_dut_mode && hci_can_send_command_packet_now()){
91486d2354SMatthias Ringwald         enable_dut_mode = 0;
92486d2354SMatthias Ringwald         hci_send_cmd(&hci_enable_device_under_test_mode);
93486d2354SMatthias Ringwald     }
94486d2354SMatthias Ringwald }
95486d2354SMatthias Ringwald 
96486d2354SMatthias Ringwald /* @text For more details on discovering remote devices, please see
97486d2354SMatthias Ringwald  * Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices).
98486d2354SMatthias Ringwald  */
99486d2354SMatthias Ringwald 
100486d2354SMatthias Ringwald 
101486d2354SMatthias Ringwald /* @section Main Application Setup
102486d2354SMatthias Ringwald  *
103486d2354SMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
104486d2354SMatthias Ringwald  * It registers the HCI packet handler and starts the Bluetooth stack.
105486d2354SMatthias Ringwald  */
106486d2354SMatthias Ringwald 
107486d2354SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup packet handler  */
108486d2354SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])109486d2354SMatthias Ringwald int btstack_main(int argc, const char * argv[]) {
110486d2354SMatthias Ringwald     (void)argc;
111486d2354SMatthias Ringwald     (void)argv;
112486d2354SMatthias Ringwald 
1131d927f3aSMatthias Ringwald     // disable Secure Simple Pairinng
1141d927f3aSMatthias Ringwald     gap_ssp_set_enable(0);
1151d927f3aSMatthias Ringwald 
116486d2354SMatthias Ringwald     // make device connectable
117486d2354SMatthias Ringwald     // @note: gap_connectable_control will be enabled when an L2CAP service
118486d2354SMatthias Ringwald     // (e.g. RFCOMM) is initialized). Therefore, it's not needed in regular applications
119486d2354SMatthias Ringwald     gap_connectable_control(1);
120486d2354SMatthias Ringwald 
121486d2354SMatthias Ringwald     // make device discoverable
122486d2354SMatthias Ringwald     gap_discoverable_control(1);
123486d2354SMatthias Ringwald 
124a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
125a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
126a4fe6467SMatthias Ringwald 
127486d2354SMatthias Ringwald     // turn on!
128486d2354SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
129486d2354SMatthias Ringwald 
130486d2354SMatthias Ringwald     return 0;
131486d2354SMatthias Ringwald }
132486d2354SMatthias Ringwald /* LISTING_END */
133486d2354SMatthias Ringwald /* EXAMPLE_END */
134