dut_mode_classic.c 4.7 KB

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