sm_pairing_peripheral.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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__ "sm_pairing_peripheral.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(sm_pairing_peripheral): LE Peripheral - Test pairing combinations
  40. *
  41. * @text Depending on the Authentication requiremens and IO Capabilities,
  42. * the pairing process uses different short and long term key generation method.
  43. * This example helps explore the different options incl. LE Secure Connections.
  44. */
  45. // *****************************************************************************
  46. #include <stdint.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <inttypes.h>
  51. #include "sm_pairing_peripheral.h"
  52. #include "btstack.h"
  53. /* @section Main Application Setup
  54. *
  55. * @text Listing MainConfiguration shows main application code.
  56. * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled
  57. * ATT Database generated from $sm_pairing_peripheral.gatt$. Finally, it configures the advertisements
  58. * and boots the Bluetooth stack.
  59. * In this example, the Advertisement contains the Flags attribute, the device name, and a 16-bit (test) service 0x1111
  60. * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported.
  61. * Various examples for IO Capabilites and Authentication Requirements are given below.
  62. */
  63. /* LISTING_START(MainConfiguration): Setup stack to advertise */
  64. static btstack_packet_callback_registration_t sm_event_callback_registration;
  65. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  66. const uint8_t adv_data[] = {
  67. // Flags general discoverable, BR/EDR not supported
  68. 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
  69. // Name
  70. 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'M', ' ', 'P', 'a', 'i', 'r', 'i', 'n', 'g',
  71. // Incomplete List of 16-bit Service Class UUIDs -- 1111 - only valid for testing!
  72. 0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x11, 0x11,
  73. };
  74. const uint8_t adv_data_len = sizeof(adv_data);
  75. static void sm_peripheral_setup(void){
  76. l2cap_init();
  77. // setup le device db
  78. le_device_db_init();
  79. // setup SM: Display only
  80. sm_init();
  81. /**
  82. * Choose ONE of the following configurations
  83. * Bonding is disabled to allow for repeated testing. It can be enabled by or'ing
  84. * SM_AUTHREQ_BONDING to the authentication requirements like this:
  85. * sm_set_authentication_requirements( X | SM_AUTHREQ_BONDING)
  86. */
  87. // LE Legacy Pairing, Just Works
  88. // sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
  89. // sm_set_authentication_requirements(0);
  90. // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays
  91. // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
  92. // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION);
  93. // sm_use_fixed_passkey_in_display_role(123456);
  94. #ifdef ENABLE_LE_SECURE_CONNECTIONS
  95. // enable LE Secure Connections Only mode - disables Legacy pairing
  96. // sm_set_secure_connections_only_mode(true);
  97. // LE Secure Connections, Just Works
  98. // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
  99. // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION);
  100. // LE Secure Connections, Numeric Comparison
  101. sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
  102. sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
  103. // LE Secure Pairing, Passkey entry initiator enter, responder (us) displays
  104. // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
  105. // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
  106. // sm_use_fixed_passkey_in_display_role(123456);
  107. // LE Secure Pairing, Passkey entry initiator displays, responder (us) enter
  108. // sm_set_io_capabilities(IO_CAPABILITY_KEYBOARD_ONLY);
  109. // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
  110. #endif
  111. // setup ATT server
  112. att_server_init(profile_data, NULL, NULL);
  113. // setup advertisements
  114. uint16_t adv_int_min = 0x0030;
  115. uint16_t adv_int_max = 0x0030;
  116. uint8_t adv_type = 0;
  117. bd_addr_t null_addr;
  118. memset(null_addr, 0, 6);
  119. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  120. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  121. gap_advertisements_enable(1);
  122. // register for SM events
  123. sm_event_callback_registration.callback = &packet_handler;
  124. sm_add_event_handler(&sm_event_callback_registration);
  125. // register for ATT
  126. att_server_register_packet_handler(packet_handler);
  127. }
  128. /* LISTING_END */
  129. /*
  130. * @section Packet Handler
  131. *
  132. * @text The packet handler is used to:
  133. * - report connect/disconnect
  134. * - handle Security Manager events
  135. */
  136. /* LISTING_START(packetHandler): Packet Handler */
  137. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  138. UNUSED(channel);
  139. UNUSED(size);
  140. hci_con_handle_t con_handle;
  141. bd_addr_t addr;
  142. switch (packet_type) {
  143. case HCI_EVENT_PACKET:
  144. switch (hci_event_packet_get_type(packet)) {
  145. case HCI_EVENT_LE_META:
  146. switch (hci_event_le_meta_get_subevent_code(packet)) {
  147. case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
  148. printf("Connection complete\n");
  149. // Uncomment the next lines to trigger explicit pairing on connect
  150. // con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
  151. // sm_send_security_request(con_handle);
  152. UNUSED(con_handle);
  153. break;
  154. default:
  155. break;
  156. }
  157. break;
  158. case SM_EVENT_JUST_WORKS_REQUEST:
  159. printf("Just Works requested\n");
  160. sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
  161. break;
  162. case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
  163. printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
  164. sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
  165. break;
  166. case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
  167. printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
  168. break;
  169. case SM_EVENT_IDENTITY_CREATED:
  170. sm_event_identity_created_get_identity_address(packet, addr);
  171. printf("Identity created: type %u address %s\n", sm_event_identity_created_get_identity_addr_type(packet), bd_addr_to_str(addr));
  172. break;
  173. case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
  174. sm_event_identity_resolving_succeeded_get_identity_address(packet, addr);
  175. printf("Identity resolved: type %u address %s\n", sm_event_identity_resolving_succeeded_get_identity_addr_type(packet), bd_addr_to_str(addr));
  176. break;
  177. case SM_EVENT_IDENTITY_RESOLVING_FAILED:
  178. sm_event_identity_created_get_address(packet, addr);
  179. printf("Identity resolving failed\n");
  180. break;
  181. case SM_EVENT_PAIRING_COMPLETE:
  182. switch (sm_event_pairing_complete_get_status(packet)){
  183. case ERROR_CODE_SUCCESS:
  184. printf("Pairing complete, success\n");
  185. break;
  186. case ERROR_CODE_CONNECTION_TIMEOUT:
  187. printf("Pairing failed, timeout\n");
  188. break;
  189. case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION:
  190. printf("Pairing failed, disconnected\n");
  191. break;
  192. case ERROR_CODE_AUTHENTICATION_FAILURE:
  193. printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet));
  194. break;
  195. default:
  196. break;
  197. }
  198. break;
  199. default:
  200. break;
  201. }
  202. break;
  203. }
  204. }
  205. /* LISTING_END */
  206. int btstack_main(void);
  207. int btstack_main(void)
  208. {
  209. sm_peripheral_setup();
  210. // turn on!
  211. hci_power_control(HCI_POWER_ON);
  212. return 0;
  213. }
  214. /* EXAMPLE_END */