1*503a627eSMilanka RingwaldWe already had two listings with the Bluetooth SIG, but no official BTstack v1.0 release. After the second listing, we decided that it's time for a major overhaul of the API - making it easier for new users. 2*503a627eSMilanka Ringwald 3*503a627eSMilanka RingwaldIn the following, we provide an overview of the changes and guidelines for updating an existing code base. At the end, we present a command line tool and as an alternative a Web service, that can simplify the migration to the new v1.0 API. 4*503a627eSMilanka Ringwald 5*503a627eSMilanka Ringwald# Changes 6*503a627eSMilanka Ringwald 7*503a627eSMilanka Ringwald## Repository structure 8*503a627eSMilanka Ringwald 9*503a627eSMilanka Ringwald### *include/btstack* folder 10*503a627eSMilanka RingwaldThe header files had been split between *src/* and *include/btstack*/. Now, the *include/* folder is gone and the headers are provided in *src/*, *src/classic/*, *src/ble/* or by the *platform/* folders. There's a new *src/btstack.h* header that includes all BTstack headers. 11*503a627eSMilanka Ringwald 12*503a627eSMilanka Ringwald### Plural folder names 13*503a627eSMilanka RingwaldFolder with plural names have been renamed to the singular form, examples: *doc*, *chipset*, *platform*, *tool*. 14*503a627eSMilanka Ringwald 15*503a627eSMilanka Ringwald### ble and src folders 16*503a627eSMilanka RingwaldThe *ble* folder has been renamed into *src/ble*. Files that are relevant for using BTstack in Classic mode, have been moved to *src/classic*. Files in *src* that are specific to individual platforms have been moved to *platform* or *port*. 17*503a627eSMilanka Ringwald 18*503a627eSMilanka Ringwald### platform and port folders 19*503a627eSMilanka RingwaldThe *platforms* folder has been split into *platform* and "port" folders. The *port* folder contains a complete BTstack port of for a specific setup consisting of an MCU and a Bluetooth chipset. Code that didn't belong to the BTstack core in *src* have been moved to *platform* or *port* as well, e.g. *hci_transport_h4_dma.c*. 20*503a627eSMilanka Ringwald 21*503a627eSMilanka Ringwald### Common file names 22*503a627eSMilanka RingwaldTypes with generic names like *linked_list* have been prefixed with btstack_ (e.g. btstack_linked_list.h) to avoid problems when integrating with other environments like vendor SDKs. 23*503a627eSMilanka Ringwald 24*503a627eSMilanka Ringwald## Defines and event names 25*503a627eSMilanka RingwaldAll defines that originate from the Bluetooth Specification are now in *src/bluetooth.h*. Addition defines by BTstack are collected in *src/btstack_defines.h*. All events names have the form MODULE_EVENT_NAME now. 26*503a627eSMilanka Ringwald 27*503a627eSMilanka Ringwald## Function names 28*503a627eSMilanka Ringwald- The _internal suffix has been an artifact from the iOS port. All public functions with the _internal suffix have been stripped of this suffix. 29*503a627eSMilanka Ringwald- Types with generic names like linked_list have been prefixed with btstack_ (e.g. btstack_linked_list) to avoid problems when integrating with other environments like vendor SDKs. 30*503a627eSMilanka Ringwald 31*503a627eSMilanka Ringwald## Packet Handlers 32*503a627eSMilanka RingwaldWe streamlined the use of packet handlers and their types. Most callbacks are now of type *btstack_packet_handler_t* and receive a pointer to an HCI Event packet. Often a void * connection was the first argument - this has been removed. 33*503a627eSMilanka Ringwald 34*503a627eSMilanka RingwaldTo facilitate working with HCI Events and get rid of manually calculating offsets into packets, we're providing auto-generated getters for all fields of all events in *src/hci_event.h*. All functions are defined as static inline, so they are not wasting any program memory if not used. If used, the memory footprint should be identical to accessing the field directly via offsets into the packet. Feel free to start using these getters as needed. 35*503a627eSMilanka Ringwald 36*503a627eSMilanka Ringwald## Event Forwarding 37*503a627eSMilanka RingwaldIn the past, events have been forwarded up the stack from layer to layer, with the undesired effect that an app that used both *att_server* and *security_manager* would get each HCI event twice. To fix this and other potential issues, this has been cleaned up by providing a way to register multiple listeners for HCI events. If you need to receive HCI or GAP events, you can now directly register your callback with *hci_add_event_handler*. 38*503a627eSMilanka Ringwald 39*503a627eSMilanka Ringwald## util folder 40*503a627eSMilanka RingwaldThe *utils* folder has been renamed into *btstack_util* to avoid compile issues with other frameworks. 41*503a627eSMilanka Ringwald 42*503a627eSMilanka Ringwald- The functions to read/store values in little/bit endian have been renamed into big/little_endian_read/write_16/24/32. 43*503a627eSMilanka Ringwald- The functions to reverse bytes swap16/24/32/48/64/128/X habe been renamed to reverse_16/24/32/48/64/128/X. 44*503a627eSMilanka Ringwald 45*503a627eSMilanka Ringwald## btstack_config.h 46*503a627eSMilanka Ringwald- *btstack-config.h* is now *btstack_config.h* 47*503a627eSMilanka Ringwald- Defines have been sorted: HAVE_ specify features that are particular to your port. ENABLE_ features can be added as needed. 48*503a627eSMilanka Ringwald- _NO_ has been replaced with _NR_ for the BTstack static memory allocation, e.g., *MAX_NO_HCI_CONNECTIONS8 -> MAX_NR_HCI_CONNECTIONS* 49*503a627eSMilanka Ringwald- The #define EMBEDDED is dropped, i.e. the distinction if the API is for embedded or not has been removed. 50*503a627eSMilanka Ringwald 51*503a627eSMilanka Ringwald## Linked List 52*503a627eSMilanka Ringwald- The file has been renamed to btstack_linked_list. 53*503a627eSMilanka Ringwald- All functions are prefixed with btstack_. 54*503a627eSMilanka Ringwald- The user data field has been removed as well. 55*503a627eSMilanka Ringwald 56*503a627eSMilanka Ringwald## Run Loop 57*503a627eSMilanka Ringwald- The files have been renamed to *btstack_run_loop_* 58*503a627eSMilanka Ringwald- To allow for simpler integration of custom run loop implementations, *run_loop_init(...)* is now called with a pointer to a *run_loop_t* function table instead of using an enum that needs to be defined inside the BTstack sources. 59*503a627eSMilanka Ringwald- Timers now have a new context field that can be used by the user. 60*503a627eSMilanka Ringwald 61*503a627eSMilanka Ringwald## HCI Setup 62*503a627eSMilanka RingwaldIn the past, hci_init(...) was called with an hci_transport_t, the transport configuration, remote_device_t and a bt_control_t objects. Besides cleaning up the types (see remote_device_db and bt_control below), hci_init only requires the hci_transport_t and it's configuration. 63*503a627eSMilanka Ringwald 64*503a627eSMilanka RingwaldThe used *btstack_chipset_t*, *bt_control_t*, or *link_key_db_t* can now be set with *hci_set_chipset*, *hci_set_control*, and *hci_set_link_key_db* respectively. 65*503a627eSMilanka Ringwald 66*503a627eSMilanka Ringwald### remote_device_db 67*503a627eSMilanka RingwaldHas been split into *src/classic/btstack_link_key_db*, *platform/daemon/btstack_device_name_db*, and *platform/daemon/rfcomm_service_db*. 68*503a627eSMilanka Ringwald 69*503a627eSMilanka Ringwald### bt_control 70*503a627eSMilanka RingwaldHas been split into *src/btstack_chipset.h* and *src/bstack_control.h* 71*503a627eSMilanka Ringwald 72*503a627eSMilanka Ringwald## HCI / GAP 73*503a627eSMilanka RingwaldHCI functions that are commonly placed in GAP have been moved from *src/hci.h* to *src/gap.h* 74*503a627eSMilanka Ringwald 75*503a627eSMilanka Ringwald## RFCOMM 76*503a627eSMilanka RingwaldIn contrast to L2CAP, RFCOMM did not allow to register individual packet handlers for each service and outgoing connection. This has been fixed and the general *rfcomm_register_packet_handler(...)* has been removed. 77*503a627eSMilanka Ringwald 78*503a627eSMilanka Ringwald## SPP Server 79*503a627eSMilanka RingwaldThe function to create an SPP SDP record has been moved into *spp_server.h* 80*503a627eSMilanka Ringwald 81*503a627eSMilanka Ringwald## SDP Client 82*503a627eSMilanka Ringwald- SDP Query structs have been replaced by HCI events. You can use btstack_event.h to access the fields. 83*503a627eSMilanka Ringwald 84*503a627eSMilanka Ringwald## SDP Server 85*503a627eSMilanka Ringwald- Has been renamed to *src/classic/sdp_server.h*. 86*503a627eSMilanka Ringwald- The distinction if the API is for embedded or not has been removed. 87*503a627eSMilanka Ringwald 88*503a627eSMilanka Ringwald## Security Manager 89*503a627eSMilanka Ringwald- In all Security Manager calls that refer to an active connection, pass in the current handle instead of addr + addr type. 90*503a627eSMilanka Ringwald- All Security Manager events are now regular HCI Events instead of sm_* structs 91*503a627eSMilanka Ringwald- Multiple packet handler can be registered with *sm_add_event_handler(...)* similar to HCI. 92*503a627eSMilanka Ringwald 93*503a627eSMilanka Ringwald## GATT Client 94*503a627eSMilanka Ringwald- All GATT Client events are now regular HCI Events instead of gatt_* structs. 95*503a627eSMilanka Ringwald- The subclient_id has been replaced by a complete handler for GATT Client requests 96*503a627eSMilanka Ringwald 97*503a627eSMilanka Ringwald## ANCS Client 98*503a627eSMilanka RingwaldRenamed to *src/ble/ancs_client* 99*503a627eSMilanka Ringwald 100*503a627eSMilanka Ringwald## Flow control / DAEMON_EVENT_HCI_PACKET_SENT 101*503a627eSMilanka Ringwald 102*503a627eSMilanka RingwaldIn BTstack, you can only send a packet with most protocols (L2CAP, RFCOMM, ATT) if the outgoing buffer is free and also per-protocol constraints are satisfied, e.g., there are outgoing credits for RFCOMM. 103*503a627eSMilanka Ringwald 104*503a627eSMilanka RingwaldBefore v1.0, we suggested to check with *l2cap_can_send_packet_now(..)* or *rfcomm_can_send_packet(..)* whenever an HCI event was received. This has been cleaned up and streamlined in v1.0. 105*503a627eSMilanka Ringwald 106*503a627eSMilanka RingwaldNow, when there is a need to send a packet, you can call *rcomm_request_can_send_now(..)* / *l2cap_request_can_send_now(..)* to let BTstack know that you want to send a packet. As soon as it becomes possible to send a RFCOMM_EVENT_CAN_SEND_NOW/L2CAP_EVENT_CAN_SEND_NOW will be emitted and you can directly react on that and send a packet. After that, you can request another "can send event" if needed. 107*503a627eSMilanka Ringwald 108*503a627eSMilanka Ringwald 109*503a627eSMilanka Ringwald## Daemon 110*503a627eSMilanka Ringwald- Not tested since API migration! 111*503a627eSMilanka Ringwald- Moved into *platform/daemon/* 112*503a627eSMilanka Ringwald- Header for clients: *platform/daemon/btstack_client.h* 113*503a627eSMilanka Ringwald- Java bindings are now at *platform/daemon/bindings* 114*503a627eSMilanka Ringwald 115*503a627eSMilanka Ringwald## Migration to v1.0 with a script 116*503a627eSMilanka Ringwald 117*503a627eSMilanka RingwaldTo simplify the migration to the new v1.0 API, we've provided a tool in *tool/migration_to_v1.0* that fixes include path changes, handles all function renames and also updates the packet handlers to the btstack_packet_handler_t type. It also has a few rules that try to rename file names in Makefile as good as possible. 118*503a627eSMilanka Ringwald 119*503a627eSMilanka RingwaldIt's possible to migrate most of the provided embedded examples to v1.0. The one change that it cannot handle is the switch from structs to HCI Events for the SDP, GATT, and ANCS clients. The migration tool updates the packet handler signature to btstack_packet_handler_t, but it does not convert the field accesses to sue the appropriate getters in *btstack_event.h*. This has to be done manually, but it is straight forward. E.g., to read the field *status* of the GATT_EVENT_QUERY_COMPLETE, you call call gatt_event_query_complete_get_status(packet). 120*503a627eSMilanka Ringwald 121*503a627eSMilanka Ringwald### Requirements 122*503a627eSMilanka Ringwald 123*503a627eSMilanka Ringwald- bash 124*503a627eSMilanka Ringwald- sed 125*503a627eSMilanka Ringwald- [Coccinelle](http://coccinelle.lip6.fr/). On Debian-based distributions, it's available via apt. On OS X, it can be installed via Homebrew. 126*503a627eSMilanka Ringwald 127*503a627eSMilanka Ringwald### Usage 128*503a627eSMilanka Ringwald 129*503a627eSMilanka Ringwald tool/migration_to_v1.0/migration.sh PATH_TO_ORIGINAL_PROJECT PATH_TO_MIGRATED_PROJECT 130*503a627eSMilanka Ringwald 131*503a627eSMilanka RingwaldThe tool first creates a copy of the original project and then uses sed and coccinelle to update the source files. 132*503a627eSMilanka Ringwald 133*503a627eSMilanka Ringwald## Migration to v1.0 with a Web Service 134*503a627eSMilanka Ringwald 135*503a627eSMilanka RingwaldBlueKitchen GmbH is providing a [web service](http://buildbot.bluekitchen-gmbh.com/migration) to help migrate your sources if you don't want or cannot install Coccinelle yourself. 136