btstack.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "btstack_run_loop_embedded.h"
  2. #include "btstack.h"
  3. typedef struct {
  4. uint16_t (*att_read_callback)(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
  5. int (*att_write_callback)(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
  6. void (*packet_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  7. const uint8_t *ble_device_db;
  8. }blestack_init_t;
  9. static btstack_packet_callback_registration_t hci_event_callback_registration;
  10. extern const hci_transport_t * hci_transport_ram_instance(void);
  11. static btstack_packet_handler_t att_handler = (btstack_packet_handler_t)0;
  12. static void att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
  13. {
  14. if(packet_type != HCI_EVENT_PACKET) return;
  15. if(att_handler == (btstack_packet_handler_t)0) return;
  16. if((packet[0] == ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE) ||
  17. (packet[0] == ATT_EVENT_MTU_EXCHANGE_COMPLETE) ||
  18. (packet[0] == ATT_EVENT_CAN_SEND_NOW)) {
  19. att_handler(HCI_EVENT_PACKET, channel, packet, size);
  20. }
  21. }
  22. void ble_stack_init(void *init)
  23. {
  24. // start with BTstack init - especially configure HCI Transport
  25. btstack_memory_init();
  26. btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
  27. hci_dump_open((const char *)1, HCI_DUMP_STDOUT);
  28. // init HCI
  29. hci_init(hci_transport_ram_instance(), (void*)0);
  30. blestack_init_t *btstack_init = (blestack_init_t *)init;
  31. // inform about BTstack state
  32. hci_event_callback_registration.callback = btstack_init->packet_handler;
  33. hci_add_event_handler(&hci_event_callback_registration);
  34. l2cap_init();
  35. l2cap_register_packet_handler(btstack_init->packet_handler);
  36. // setup le device db
  37. le_device_db_init();
  38. // setup SM: Display only
  39. sm_init();
  40. sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
  41. sm_set_authentication_requirements(SM_AUTHREQ_BONDING);
  42. // setup ATT server
  43. att_server_init(btstack_init->ble_device_db,
  44. btstack_init->att_read_callback,
  45. btstack_init->att_write_callback);
  46. att_handler = btstack_init->packet_handler;
  47. att_server_register_packet_handler(att_packet_handler);
  48. // turn on!
  49. hci_power_control(HCI_POWER_ON);
  50. }
  51. void ble_stack_schedule(void)
  52. {
  53. btstack_run_loop_execute();
  54. }