ancs_client_demo.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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__ "ancs_client_demo.c"
  38. // *****************************************************************************
  39. //
  40. // ANCS Client Demo
  41. //
  42. // TODO: query full text upon notification using control point
  43. // TODO: present notifications in human readable form
  44. //
  45. // *****************************************************************************
  46. #include <stdint.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <inttypes.h>
  51. #include "btstack_config.h"
  52. #include "btstack_run_loop.h"
  53. #include "btstack_debug.h"
  54. #include "btstack_event.h"
  55. #include "btstack_memory.h"
  56. #include "gap.h"
  57. #include "hci.h"
  58. #include "hci_dump.h"
  59. #include "l2cap.h"
  60. #include "ble/ancs_client.h"
  61. #include "ble/att_db.h"
  62. #include "ble/att_server.h"
  63. #include "ble/gatt_client.h"
  64. #include "ble/le_device_db.h"
  65. #include "ble/sm.h"
  66. // ancs_client_demo.gatt contains the declaration of the provided GATT Services + Characteristics
  67. // ancs_client_demo.h contains the binary representation of ancs_client_demo.gatt
  68. // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py ancs_client_demo.gatt ancs_client_demo.h
  69. // it needs to be regenerated when the GATT Database declared in ancs_client_demo.gatt file is modified
  70. #include "ancs_client_demo.h"
  71. static const uint8_t adv_data[] = {
  72. // Flags general discoverable, BR/EDR not supported
  73. 0x02, 0x01, 0x06,
  74. // Name
  75. 0x05, 0x09, 'A', 'N', 'C', 'S',
  76. // Service Solicitation, 128-bit UUIDs - ANCS (little endian)
  77. 0x11,0x15,0xD0,0x00,0x2D,0x12,0x1E,0x4B,0x0F,0xA4,0x99,0x4E,0xCE,0xB5,0x31,0xF4,0x05,0x79
  78. };
  79. static uint8_t adv_data_len = sizeof(adv_data);
  80. static btstack_packet_callback_registration_t hci_event_callback_registration;
  81. static btstack_packet_callback_registration_t sm_event_callback_registration;
  82. static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  83. UNUSED(channel);
  84. UNUSED(size);
  85. switch (packet_type) {
  86. case HCI_EVENT_PACKET:
  87. switch (hci_event_packet_get_type(packet)) {
  88. case SM_EVENT_JUST_WORKS_REQUEST:
  89. sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
  90. printf("Just Works Confirmed.\n");
  91. break;
  92. case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
  93. printf("Passkey display: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
  94. break;
  95. }
  96. break;
  97. }
  98. }
  99. static void ancs_callback(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  100. UNUSED(packet_type);
  101. UNUSED(channel);
  102. UNUSED(size);
  103. const char * attribute_name;
  104. if (hci_event_packet_get_type(packet) != HCI_EVENT_ANCS_META) return;
  105. switch (hci_event_ancs_meta_get_subevent_code(packet)){
  106. case ANCS_SUBEVENT_CLIENT_CONNECTED:
  107. printf("ANCS Client: Connected\n");
  108. break;
  109. case ANCS_SUBEVENT_CLIENT_DISCONNECTED:
  110. printf("ANCS Client: Disconnected\n");
  111. break;
  112. case ANCS_SUBEVENT_CLIENT_NOTIFICATION:
  113. attribute_name = ancs_client_attribute_name_for_id(ancs_subevent_client_notification_get_attribute_id(packet));
  114. if (!attribute_name) break;
  115. printf("Notification: %s - %s\n", attribute_name, ancs_subevent_client_notification_get_text(packet));
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. int btstack_main(int argc, const char * argv[]);
  122. int btstack_main(int argc, const char * argv[]){
  123. (void)argc;
  124. (void)argv;
  125. printf("BTstack ANCS Client starting up...\n");
  126. // set up l2cap_le
  127. l2cap_init();
  128. // setup le device db
  129. le_device_db_init();
  130. // setup SM: Display only
  131. sm_init();
  132. sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
  133. sm_set_authentication_requirements( SM_AUTHREQ_BONDING );
  134. // register for HCI events
  135. hci_event_callback_registration.callback = &app_packet_handler;
  136. hci_add_event_handler(&hci_event_callback_registration);
  137. // register for SM events
  138. sm_event_callback_registration.callback = &app_packet_handler;
  139. sm_add_event_handler(&sm_event_callback_registration);
  140. // setup ATT server
  141. att_server_init(profile_data, NULL, NULL);
  142. // setup ANCS Client
  143. ancs_client_init();
  144. // register for ATT Serer events
  145. att_server_register_packet_handler(app_packet_handler);
  146. // setup GATT client
  147. gatt_client_init();
  148. // register for ancs events
  149. ancs_client_register_callback(&ancs_callback);
  150. // setup advertisements
  151. uint16_t adv_int_min = 0x0030;
  152. uint16_t adv_int_max = 0x0030;
  153. uint8_t adv_type = 0;
  154. bd_addr_t null_addr;
  155. memset(null_addr, 0, 6);
  156. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  157. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  158. gap_advertisements_enable(1);
  159. // turn on!
  160. hci_power_control(HCI_POWER_ON);
  161. return 0;
  162. }