diff --git a/doc/manual/docs/examples/intro.md b/doc/manual/docs/examples/intro.md index 2a4b65b58..ce587fdc4 100644 --- a/doc/manual/docs/examples/intro.md +++ b/doc/manual/docs/examples/intro.md @@ -19,7 +19,7 @@ embedded system with a Bluetooth chipset connected via UART. btstack_memory_init(); // select embedded run loop - run_loop_init(RUN_LOOP_EMBEDDED); + run_loop_init(run_loop_embedded_get_instance()); // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT hci_dump_open(NULL, HCI_DUMP_STDOUT); diff --git a/doc/manual/docs/how_to.md b/doc/manual/docs/how_to.md index 50ba0f77e..b977850fa 100644 --- a/doc/manual/docs/how_to.md +++ b/doc/manual/docs/how_to.md @@ -98,7 +98,7 @@ embedded systems: - run_loop_init(RUN_LOOP_EMBEDDED); + run_loop_init(run_loop_embedded_get_instance()); The Run loop API is provided [here](appendix/apis/#sec:runLoopAPIAppendix). To enable the use of timers, make sure that you defined HAVE_TICK in the diff --git a/example/daemon/inquiry.c b/example/daemon/inquiry.c index 8b6845817..d19e66802 100644 --- a/example/daemon/inquiry.c +++ b/example/daemon/inquiry.c @@ -47,6 +47,7 @@ #include #include "btstack_client.h" +#include "run_loop_posix.h" #define MAX_DEVICES 10 struct device { @@ -235,7 +236,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe int main (int argc, const char * argv[]){ // start stack - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); int err = bt_open(); if (err) { printf("Failed to open connection to BTdaemon\n"); diff --git a/example/daemon/l2cap-server.c b/example/daemon/l2cap-server.c index ee7059dc4..3304b05a2 100644 --- a/example/daemon/l2cap-server.c +++ b/example/daemon/l2cap-server.c @@ -47,6 +47,7 @@ #include #include "btstack_client.h" +#include "run_loop_posix.h" #include "hci_cmds.h" #include "classic/sdp_util.h" @@ -196,7 +197,7 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint } int main (int argc, const char * argv[]){ - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); int err = bt_open(); if (err) { printf("Failed to open connection to BTdaemon\n"); diff --git a/example/daemon/l2cap-throughput.c b/example/daemon/l2cap-throughput.c index 7581c74ab..18d6b1027 100644 --- a/example/daemon/l2cap-throughput.c +++ b/example/daemon/l2cap-throughput.c @@ -47,6 +47,7 @@ #include #include "btstack_client.h" +#include "run_loop_posix.h" #include "hci_cmds.h" #define PSM_TEST 0xdead @@ -200,7 +201,7 @@ int main (int argc, const char * argv[]){ } } - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); int err = bt_open(); if (err) { printf("Failed to open connection to BTdaemon\n"); diff --git a/example/daemon/le_scan.c b/example/daemon/le_scan.c index fb634f0cc..61d9c51be 100644 --- a/example/daemon/le_scan.c +++ b/example/daemon/le_scan.c @@ -45,6 +45,7 @@ #include #include "btstack_client.h" +#include "run_loop_posix.h" static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ @@ -79,7 +80,7 @@ int main (int argc, const char * argv[]){ printf("le_scan started\n"); printf("- connecting to BTstack Daemon\n"); // start stack - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); int err = bt_open(); if (err) { printf("-> Failed to open connection to BTstack Daemon\n"); diff --git a/example/daemon/rfcomm-cat.c b/example/daemon/rfcomm-cat.c index 090ae098c..e3492eda3 100644 --- a/example/daemon/rfcomm-cat.c +++ b/example/daemon/rfcomm-cat.c @@ -54,6 +54,7 @@ #include #include "btstack_client.h" +#include "run_loop_posix.h" #include "classic/sdp_util.h" // input from command line arguments @@ -162,7 +163,7 @@ int main (int argc, const char * argv[]){ arg++; } - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); int err = bt_open(); if (err) { fprintf(stderr,"Failed to open connection to BTdaemon, err %d\n",err); diff --git a/example/daemon/rfcomm-echo.c b/example/daemon/rfcomm-echo.c index 06c87617b..d90592f28 100644 --- a/example/daemon/rfcomm-echo.c +++ b/example/daemon/rfcomm-echo.c @@ -50,6 +50,7 @@ #include #include "btstack_client.h" +#include "run_loop_posix.h" #include "classic/sdp_util.h" // input from command line arguments @@ -150,7 +151,7 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint int main (int argc, const char * argv[]){ - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); int err = bt_open(); if (err) { fprintf(stderr,"Failed to open connection to BTdaemon, err %d\n",err); diff --git a/example/daemon/rfcomm-test.c b/example/daemon/rfcomm-test.c index 3fcd25589..96e0b12e1 100644 --- a/example/daemon/rfcomm-test.c +++ b/example/daemon/rfcomm-test.c @@ -50,6 +50,7 @@ #include #include "btstack_client.h" +#include "run_loop_posix.h" #include "classic/sdp_util.h" #define NUM_ROWS 25 @@ -176,7 +177,7 @@ int main (int argc, const char * argv[]){ create_test_data(); printf("created test data: \n%s\n", test_data); - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); int err = bt_open(); if (err) { fprintf(stderr,"Failed to open connection to BTdaemon, err %d\n",err); diff --git a/example/daemon/test.c b/example/daemon/test.c index 2f8682781..72620c106 100644 --- a/example/daemon/test.c +++ b/example/daemon/test.c @@ -47,6 +47,8 @@ #include #include "btstack_client.h" +#include "run_loop.h" +#include "run_loop_posix.h" #include "hci_cmds.h" // bd_addr_t addr = {0x00, 0x03, 0xc9, 0x3d, 0x77, 0x43 }; // Think Outside Keyboard @@ -98,7 +100,7 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint } int main (int argc, const char * argv[]){ - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); int err = bt_open(); if (err) { printf("Failed to open connection to BTdaemon\n"); diff --git a/example/embedded/Makefile.inc b/example/embedded/Makefile.inc index 75d38416d..b1d1d0c1e 100644 --- a/example/embedded/Makefile.inc +++ b/example/embedded/Makefile.inc @@ -10,7 +10,7 @@ CFLAGS += -I${BTSTACK_ROOT}/src CORE += \ btstack_memory.c \ - linked_list.c \ + bk_linked_list.c \ memory_pool.c \ run_loop.c \ diff --git a/platform/cocoa/run_loop_cocoa.h b/platform/cocoa/run_loop_cocoa.h new file mode 100644 index 000000000..953146b0f --- /dev/null +++ b/platform/cocoa/run_loop_cocoa.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2014 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + +/* + * run_loop_cocoa.h + * Functionality special to the CoreFoundation run loop + */ + +#ifndef __RUN_LOOP_COCOA_H +#define __RUN_LOOP_COCOA_H + +#include "run_loop.h" + +#if defined __cplusplus +extern "C" { +#endif + +/** + * Provide run_loop_cocoa instance + */ +const run_loop_t * run_loop_cocoa_get_instance(void){ + +/* API_END */ + +#if defined __cplusplus +} +#endif + +#endif // __RUN_LOOP_COCOA_H diff --git a/platform/cocoa/run_loop_cocoa.m b/platform/cocoa/run_loop_cocoa.m index 6d3443777..9a623cc17 100644 --- a/platform/cocoa/run_loop_cocoa.m +++ b/platform/cocoa/run_loop_cocoa.m @@ -51,6 +51,7 @@ #include static struct timeval init_tv; +static const run_loop_t run_loop_cocoa; static void theCFRunLoopTimerCallBack (CFRunLoopTimerRef timer,void *info){ timer_source_t * ts = (timer_source_t*)info; @@ -173,7 +174,14 @@ void run_loop_cocoa_dump_timer(void){ return; } -run_loop_t run_loop_cocoa = { +/** + * Provide run_loop_embedded instance + */ +const run_loop_t * run_loop_cocoa_get_instance(void){ + return &run_loop_cocoa; +} + +static const run_loop_t run_loop_cocoa = { &run_loop_cocoa_init, &run_loop_cocoa_add_data_source, &run_loop_cocoa_remove_data_source, diff --git a/platform/daemon/src/daemon.c b/platform/daemon/src/daemon.c index 59aa23d46..cef7fa764 100644 --- a/platform/daemon/src/daemon.c +++ b/platform/daemon/src/daemon.c @@ -58,6 +58,7 @@ #include "btstack.h" #include "bk_linked_list.h" #include "run_loop.h" +#include "run_loop_posix.h" #include "hci_cmds.h" #include "version.h" @@ -1985,7 +1986,7 @@ int main (int argc, char * const * argv){ remote_device_db = &REMOTE_DEVICE_DB; #endif - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); // init power management notifications if (control && control->register_for_power_notifications){ diff --git a/platform/embedded/run_loop_embedded.c b/platform/embedded/run_loop_embedded.c index 0dd23a53b..4bafd49fd 100644 --- a/platform/embedded/run_loop_embedded.c +++ b/platform/embedded/run_loop_embedded.c @@ -76,6 +76,8 @@ #define TIMER_SUPPORT #endif +static const run_loop_t run_loop_embedded; + // the run loop static bk_linked_list_t data_sources; @@ -243,7 +245,6 @@ void run_loop_embedded_trigger(void){ } static void run_loop_embedded_init(void){ - data_sources = NULL; #ifdef TIMER_SUPPORT @@ -257,7 +258,14 @@ static void run_loop_embedded_init(void){ #endif } -const run_loop_t run_loop_embedded = { +/** + * Provide run_loop_embedded instance + */ +const run_loop_t * run_loop_embedded_get_instance(void){ + return &run_loop_embedded; +} + +static const run_loop_t run_loop_embedded = { &run_loop_embedded_init, &run_loop_embedded_add_data_source, &run_loop_embedded_remove_data_source, diff --git a/platform/embedded/run_loop_embedded.h b/platform/embedded/run_loop_embedded.h index fc5f15e23..38687db58 100644 --- a/platform/embedded/run_loop_embedded.h +++ b/platform/embedded/run_loop_embedded.h @@ -55,6 +55,10 @@ extern "C" { #endif +/** + * Provide run_loop_embedded instance + */ +const run_loop_t * run_loop_embedded_get_instance(void); // hack to fix HCI timer handling #ifdef HAVE_TICK diff --git a/platform/posix/src/run_loop_posix.c b/platform/posix/src/run_loop_posix.c index 183627baf..ed35dddb3 100644 --- a/platform/posix/src/run_loop_posix.c +++ b/platform/posix/src/run_loop_posix.c @@ -42,10 +42,10 @@ */ #include "run_loop.h" - +#include "run_loop_private.h" +#include "run_loop_posix.h" #include "bk_linked_list.h" #include "debug.h" -#include "run_loop_private.h" #ifdef _WIN32 #include "Winsock2.h" @@ -263,7 +263,8 @@ static uint32_t run_loop_posix_get_time_ms(void){ + (current_tv.tv_usec - init_tv.tv_usec) / 1000; } -run_loop_t run_loop_posix = { + +static const run_loop_t run_loop_posix = { &run_loop_posix_init, &run_loop_posix_add_data_source, &run_loop_posix_remove_data_source, @@ -274,3 +275,11 @@ run_loop_t run_loop_posix = { &run_loop_posix_dump_timer, &run_loop_posix_get_time_ms, }; + +/** + * Provide run_loop_posix instance + */ +const run_loop_t * run_loop_posix_get_instance(void){ + return &run_loop_posix; +} + diff --git a/platform/posix/src/run_loop_posix.h b/platform/posix/src/run_loop_posix.h new file mode 100644 index 000000000..6c439792c --- /dev/null +++ b/platform/posix/src/run_loop_posix.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2014 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + +/* + * run_loop_posix.h + * Functionality special to the POSIX run loop + */ + +#ifndef __RUN_LOOP_POSIX_H +#define __RUN_LOOP_POSIX_H + +#include "run_loop.h" + +#if defined __cplusplus +extern "C" { +#endif + +/** + * Provide run_loop_posix instance + */ +const run_loop_t * run_loop_posix_get_instance(void); + +/* API_END */ + +#if defined __cplusplus +} +#endif + +#endif // __RUN_LOOP_POSIX_H diff --git a/port/arduino/BTstack.cpp b/port/arduino/BTstack.cpp index 08752eacf..6e5579bc8 100644 --- a/port/arduino/BTstack.cpp +++ b/port/arduino/BTstack.cpp @@ -749,7 +749,7 @@ void BTstackManager::setup(void){ printf("BTstackManager::setup()\n"); btstack_memory_init(); - run_loop_init(RUN_LOOP_EMBEDDED); + run_loop_init(run_loop_embedded_get_instance()); hci_transport_t * transport = hci_transport_h4_dma_instance(); bt_control_t * control = bt_control_em9301_instance(); diff --git a/port/arduino/Makefile b/port/arduino/Makefile index 334f2892e..a93d8b8c0 100644 --- a/port/arduino/Makefile +++ b/port/arduino/Makefile @@ -9,7 +9,7 @@ VERSION=`sed -n -e 's/^.*BTSTACK_VERSION \"\(.*\)\"/\1/p' ${BTSTACK_ROOT}/src/ve BTSTACK_PACKAGE=/tmp/btstack ARCHIVE=btstack-arduino-${VERSION}.zip -SRC_FILES = btstack_memory.c linked_list.c memory_pool.c run_loop.c +SRC_FILES = btstack_memory.c bk_linked_list.c memory_pool.c run_loop.c SRC_FILES += hci_dump.c hci.c hci_cmds.c utils.c l2cap.c BLE_FILES = ad_parser.c att.c att_server.c att_dispatch.c att_db_util.c le_device_db_memory.c gatt_client.c BLE_FILES += sm.c ancs_client_lib.h ancs_client_lib.c diff --git a/port/daemon/example/Makefile.in b/port/daemon/example/Makefile.in index a5216c0d2..f66d19466 100644 --- a/port/daemon/example/Makefile.in +++ b/port/daemon/example/Makefile.in @@ -4,6 +4,7 @@ CC = @CC@ LDFLAGS = @LDFLAGS@ -lBTstack -L../src CFLAGS = @CFLAGS@ \ -I$(BTSTACK_ROOT)/platform/daemon/src \ + -I$(BTSTACK_ROOT)/platform/posix/src \ -I$(BTSTACK_ROOT)/src \ -I.. prefix = @prefix@ diff --git a/port/daemon/src/Makefile.in b/port/daemon/src/Makefile.in index a0a68a37e..b0533f253 100644 --- a/port/daemon/src/Makefile.in +++ b/port/daemon/src/Makefile.in @@ -5,7 +5,8 @@ prefix = @prefix@ CC = @CC@ LDFLAGS = @LDFLAGS@ CFLAGS = @CFLAGS@ \ - -I $(BTSTACK_ROOT)/platform/src/daemon \ + -I $(BTSTACK_ROOT)/platform/daemon/src \ + -I $(BTSTACK_ROOT)/platform/posix/src \ -I $(BTSTACK_ROOT)/src \ -I.. BTSTACK_LIB_LDFLAGS = @BTSTACK_LIB_LDFLAGS@ @@ -29,7 +30,7 @@ libBTstack_SOURCES = \ socket_connection.c \ hci_cmds.c \ daemon_cmds.c \ - linked_list.c \ + bk_linked_list.c \ run_loop.c \ sdp_util.c \ utils.c \ diff --git a/port/ez430-rf2560/Makefile b/port/ez430-rf2560/Makefile index 2e35788b3..317e08463 100644 --- a/port/ez430-rf2560/Makefile +++ b/port/ez430-rf2560/Makefile @@ -34,20 +34,20 @@ CFLAGS += \ LDFLAGS = -mmcu=msp430f5438a CORE = \ - hal_tick.c \ - hal_cpu.c \ + bk_linked_list.c \ + btstack_memory.c \ hal_board.c \ hal_compat.c \ + hal_cpu.c \ hal_led.c \ + hal_tick.c \ hal_usb.c \ hci_dump.c \ - btstack_memory.c \ - linked_list.c \ + main.c \ memory_pool.c \ run_loop.c \ run_loop_embedded.c \ utils.c \ - main.c \ COMMON = \ hal_uart_dma.c \ diff --git a/port/ez430-rf2560/src/main.c b/port/ez430-rf2560/src/main.c index 41e207244..7e2ff252d 100644 --- a/port/ez430-rf2560/src/main.c +++ b/port/ez430-rf2560/src/main.c @@ -87,7 +87,7 @@ int main(void) /// GET STARTED with BTstack /// btstack_memory_init(); - run_loop_init(RUN_LOOP_EMBEDDED); + run_loop_init(run_loop_embedded_get_instance()); // init HCI hci_transport_t * transport = hci_transport_h4_dma_instance(); diff --git a/port/ios/CocoaTouch/src/BTstackCocoaAppDelegate.m b/port/ios/CocoaTouch/src/BTstackCocoaAppDelegate.m index 3ea08417c..6167bd1de 100644 --- a/port/ios/CocoaTouch/src/BTstackCocoaAppDelegate.m +++ b/port/ios/CocoaTouch/src/BTstackCocoaAppDelegate.m @@ -46,6 +46,7 @@ #include "btstack_client.h" #include "run_loop.h" +#include "run_loop_cocoa.h" #include "hci_cmds.h" void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ @@ -70,7 +71,7 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint [window makeKeyAndVisible]; // start Bluetooth - run_loop_init(RUN_LOOP_COCOA); + run_loop_init(run_loop_cocoa_get_instance()); int res = bt_open(); if (res){ diff --git a/port/ios/CocoaTouch/src/BTstackManager.m b/port/ios/CocoaTouch/src/BTstackManager.m index c1c488e0c..4db23de04 100644 --- a/port/ios/CocoaTouch/src/BTstackManager.m +++ b/port/ios/CocoaTouch/src/BTstackManager.m @@ -32,6 +32,8 @@ #import #import "btstack.h" +#import "run_loop.h" +#import "run_loop_cocoa.h" #import #define INQUIRY_INTERVAL 3 @@ -69,7 +71,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe [self setListeners:[[NSMutableSet alloc] init]]; // Use Cocoa run loop - run_loop_init(RUN_LOOP_COCOA); + run_loop_init(run_loop_cocoa_get_instance()); // our packet handler bt_register_packet_handler(packet_handler); diff --git a/port/ios/PrefsBundle/BluetoothController.m b/port/ios/PrefsBundle/BluetoothController.m index d90a49a0d..b3f26afbf 100644 --- a/port/ios/PrefsBundle/BluetoothController.m +++ b/port/ios/PrefsBundle/BluetoothController.m @@ -30,6 +30,8 @@ */ #import "BluetoothController.h" +#include "run_loop.h" +#include "run_loop_cocoa.h" #pragma mark callback handler static void btstackStoppedCallback(CFNotificationCenterRef center, @@ -83,7 +85,7 @@ static BluetoothController* sharedInstance = nil; CFNotificationSuspensionBehaviorDrop); // suspension behavior // set up libBTstack - run_loop_init(RUN_LOOP_COCOA); + run_loop_init(run_loop_cocoa_get_instance()); bt_register_packet_handler(bt_packet_handler); return self; diff --git a/port/ios/src/Makefile b/port/ios/src/Makefile index 0f3c1193c..a609c2b83 100644 --- a/port/ios/src/Makefile +++ b/port/ios/src/Makefile @@ -17,10 +17,10 @@ VPATH += $(BTSTACK_ROOT)/src \ LIBRARY_NAME = libBTstack libBTstack_FILES = \ + bk_linked_list.c \ daemon_cmds.c \ hci_cmds.c \ hci_dump.c \ - linked_list.c \ run_loop.c \ sdp_util.c \ utils.c \ diff --git a/port/libusb/main.c b/port/libusb/main.c index b54e222c6..1d8413901 100644 --- a/port/libusb/main.c +++ b/port/libusb/main.c @@ -50,6 +50,7 @@ #include "btstack-config.h" #include "run_loop.h" +#include "run_loop_posix.h" #include "hal_led.h" #include "debug.h" @@ -84,7 +85,7 @@ int main(int argc, const char * argv[]){ /// GET STARTED with BTstack /// btstack_memory_init(); - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER); diff --git a/port/msp-exp430f5438-cc2564b/Makefile b/port/msp-exp430f5438-cc2564b/Makefile index e2e32d9a2..6b4a43568 100644 --- a/port/msp-exp430f5438-cc2564b/Makefile +++ b/port/msp-exp430f5438-cc2564b/Makefile @@ -34,20 +34,20 @@ CFLAGS += \ LDFLAGS = -mmcu=msp430f5438a CORE = \ - hal_tick.c \ - hal_cpu.c \ + bk_linked_list.c \ + btstack_memory.c \ hal_board.c \ - hal_led.c \ hal_compat.c \ + hal_cpu.c \ + hal_led.c \ + hal_tick.c \ hal_usb.c \ hci_dump.c \ - btstack_memory.c \ - linked_list.c \ + main.c \ memory_pool.c \ run_loop.c \ run_loop_embedded.c \ utils.c \ - main.c \ COMMON = \ hal_uart_dma.c \ diff --git a/port/msp-exp430f5438-cc2564b/example/hid_demo.c b/port/msp-exp430f5438-cc2564b/example/hid_demo.c index 1aa7568f7..b25ae3b30 100644 --- a/port/msp-exp430f5438-cc2564b/example/hid_demo.c +++ b/port/msp-exp430f5438-cc2564b/example/hid_demo.c @@ -364,7 +364,7 @@ int main(void){ /// GET STARTED /// btstack_memory_init(); - run_loop_init(RUN_LOOP_EMBEDDED); + run_loop_init(run_loop_embedded_get_instance()); // init HCI hci_transport_t * transport = hci_transport_h4_dma_instance(); diff --git a/port/msp-exp430f5438-cc2564b/src/main.c b/port/msp-exp430f5438-cc2564b/src/main.c index 53007397c..8a6efeb61 100644 --- a/port/msp-exp430f5438-cc2564b/src/main.c +++ b/port/msp-exp430f5438-cc2564b/src/main.c @@ -87,7 +87,7 @@ static void hw_setup(void){ static void btstack_setup(void){ /// GET STARTED with BTstack /// btstack_memory_init(); - run_loop_init(RUN_LOOP_EMBEDDED); + run_loop_init(run_loop_embedded_get_instance()); // init HCI hci_transport_t * transport = hci_transport_h4_dma_instance(); diff --git a/port/msp430f5229lp-cc2564b/src/main.c b/port/msp430f5229lp-cc2564b/src/main.c index ac6be774d..76778031c 100644 --- a/port/msp430f5229lp-cc2564b/src/main.c +++ b/port/msp430f5229lp-cc2564b/src/main.c @@ -94,7 +94,7 @@ static void btstack_setup(void){ /// GET STARTED with BTstack /// btstack_memory_init(); - run_loop_init(RUN_LOOP_EMBEDDED); + run_loop_init(run_loop_embedded_get_instance()); // init HCI hci_transport_t * transport = hci_transport_h4_dma_instance(); diff --git a/port/pic32-harmony/app.X/nbproject/configurations.xml b/port/pic32-harmony/app.X/nbproject/configurations.xml index 8dfc02d55..c8e37822c 100644 --- a/port/pic32-harmony/app.X/nbproject/configurations.xml +++ b/port/pic32-harmony/app.X/nbproject/configurations.xml @@ -35,7 +35,7 @@ ../../../src/hci_cmds.h - ../../../src/linked_list.h + ../../../src/bk_linked_list.h ../../../src/memory_pool.h ../../../src/run_loop.h ../../../src/utils.h @@ -127,7 +127,7 @@ ../../../src/hci_transport_h4_dma.c ../../../src/l2cap.c ../../../src/l2cap_signaling.c - ../../../src/linked_list.c + ../../../src/bk_linked_list.c ../../../src/memory_pool.c ../../../src/classic/remote_device_db_memory.c ../../../src/classic/rfcomm.c diff --git a/port/pic32-harmony/src/btstack_port.c b/port/pic32-harmony/src/btstack_port.c index 6cf045447..aa69cc6fe 100644 --- a/port/pic32-harmony/src/btstack_port.c +++ b/port/pic32-harmony/src/btstack_port.c @@ -219,7 +219,7 @@ void BTSTACK_Initialize ( void ) printf("\n\nBTstack_Initialize()\n"); btstack_memory_init(); - run_loop_init(RUN_LOOP_EMBEDDED); + run_loop_init(run_loop_embedded_get_instance()); hci_dump_open(NULL, HCI_DUMP_STDOUT); diff --git a/port/posix-cc2564b/main.c b/port/posix-cc2564b/main.c index 291c77051..64ee20198 100644 --- a/port/posix-cc2564b/main.c +++ b/port/posix-cc2564b/main.c @@ -90,7 +90,7 @@ int main(int argc, const char * argv[]){ /// GET STARTED with BTstack /// btstack_memory_init(); - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER); diff --git a/port/posix-h4/main.c b/port/posix-h4/main.c index 5a7d30739..265302dc0 100644 --- a/port/posix-h4/main.c +++ b/port/posix-h4/main.c @@ -87,7 +87,7 @@ int main(int argc, const char * argv[]){ /// GET STARTED with BTstack /// btstack_memory_init(); - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER); diff --git a/port/posix-stlc2500d/main.c b/port/posix-stlc2500d/main.c index 7ce32958a..701aefa85 100644 --- a/port/posix-stlc2500d/main.c +++ b/port/posix-stlc2500d/main.c @@ -90,7 +90,7 @@ int main(int argc, const char * argv[]){ /// GET STARTED with BTstack /// btstack_memory_init(); - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER); diff --git a/port/posix-wl183x/main.c b/port/posix-wl183x/main.c index 291c77051..64ee20198 100644 --- a/port/posix-wl183x/main.c +++ b/port/posix-wl183x/main.c @@ -90,7 +90,7 @@ int main(int argc, const char * argv[]){ /// GET STARTED with BTstack /// btstack_memory_init(); - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER); diff --git a/port/stm32-f103rb-nucleo/Makefile b/port/stm32-f103rb-nucleo/Makefile index 3228d6f13..cc4aa8d1d 100644 --- a/port/stm32-f103rb-nucleo/Makefile +++ b/port/stm32-f103rb-nucleo/Makefile @@ -18,7 +18,7 @@ VPATH += $(BTSTACK_ROOT)/chipset/cc256x CORE = \ main.c \ btstack_memory.c \ - linked_list.c \ + bk_linked_list.c \ memory_pool.c \ run_loop.c \ run_loop_embedded.c diff --git a/port/stm32-f103rb-nucleo/main.c b/port/stm32-f103rb-nucleo/main.c index 01bc33576..3209dd4c8 100644 --- a/port/stm32-f103rb-nucleo/main.c +++ b/port/stm32-f103rb-nucleo/main.c @@ -49,6 +49,7 @@ #include #include "run_loop.h" +#include "run_loop_embedded.h" #include "hci.h" #include "bt_control_cc256x.h" #include "btstack_memory.h" @@ -419,7 +420,7 @@ int main(void) // start with BTstack init - especially configure HCI Transport btstack_memory_init(); - run_loop_init(RUN_LOOP_EMBEDDED); + run_loop_init(run_loop_embedded_get_instance()); // init HCI hci_transport_t * transport = hci_transport_h4_dma_instance(); diff --git a/port/wiced/main.c b/port/wiced/main.c index 7e2606b97..262089d4d 100644 --- a/port/wiced/main.c +++ b/port/wiced/main.c @@ -35,12 +35,16 @@ * */ -#include "wiced.h" -#include "platform_bluetooth.h" #include "bt_control_bcm.h" #include "btstack.h" +#include "run_loop_wiced.h" + #include "generated_mac_address.txt" +#include "platform_bluetooth.h" +#include "wiced.h" + + // see generated_mac_address.txt - "macaddr=02:0A:F7:3d:76:be" static const char * wifi_mac_address = NVRAM_GENERATED_MAC_ADDRESS; @@ -81,7 +85,7 @@ void application_start(void){ // start with BTstack init - especially configure HCI Transport btstack_memory_init(); - run_loop_init(RUN_LOOP_WICED); + run_loop_init(run_loop_wiced_get_instance()); // enable full log output while porting // hci_dump_open(NULL, HCI_DUMP_STDOUT); diff --git a/port/wiced/run_loop_wiced.c b/port/wiced/run_loop_wiced.c index 403e92ce6..6b660c044 100644 --- a/port/wiced/run_loop_wiced.c +++ b/port/wiced/run_loop_wiced.c @@ -57,6 +57,8 @@ typedef struct function_call { void * arg; } function_call_t; +static const run_loop_t run_loop_wiced; + static wiced_queue_t run_loop_queue; // the run loop @@ -171,7 +173,14 @@ static void run_loop_wiced_run_loop_init(void){ wiced_rtos_init_queue(&run_loop_queue, "BTstack Run Loop", sizeof(function_call_t), 5); } -const run_loop_t run_loop_wiced = { +/** + * @brief Provide run_loop_posix instance for use with run_loop_init + */ +const run_loop_t * run_loop_wiced_get_instance(void){ + return &run_loop_wiced; +} + +static const run_loop_t run_loop_wiced = { &run_loop_wiced_run_loop_init, &run_loop_wiced_add_data_source, &run_loop_wiced_remove_data_source, diff --git a/port/wiced/run_loop_wiced.h b/port/wiced/run_loop_wiced.h index 2e21364ce..08783e39a 100644 --- a/port/wiced/run_loop_wiced.h +++ b/port/wiced/run_loop_wiced.h @@ -48,6 +48,11 @@ #include "run_loop.h" #include "wiced.h" +/** + * @brief Provide run_loop_posix instance for use with run_loop_init + */ +const run_loop_t * run_loop_wiced_get_instance(void); + /* * @brief Execute code on BTstack run loop. Can be used to control BTstack from a different thread */ diff --git a/src/hci.c b/src/hci.c index b4324c1b9..c6d45fbd9 100644 --- a/src/hci.c +++ b/src/hci.c @@ -47,6 +47,10 @@ #include "hci.h" #include "gap.h" +#ifdef HAVE_TICK +#include "run_loop_embedded.h" +#endif + #ifdef HAVE_BLE #include "gap.h" #endif @@ -203,7 +207,7 @@ static void hci_connection_timeout_handler(timer_source_t *timer){ } #endif #ifdef HAVE_TICK - if (run_loop_embedded_ticks_for_ms() > connection->timestamp + run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ + if (run_loop_embedded_get_ticks() > connection->timestamp + run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ // connections might be timed out hci_emit_l2cap_check_timeout(connection); } diff --git a/src/run_loop.c b/src/run_loop.c index ee5b4cf32..b8b267f93 100644 --- a/src/run_loop.c +++ b/src/run_loop.c @@ -51,30 +51,20 @@ #include "debug.h" #include "btstack-config.h" -static run_loop_t * the_run_loop = NULL; +static const run_loop_t * the_run_loop = NULL; extern const run_loop_t run_loop_embedded; -#ifdef USE_POSIX_RUN_LOOP -extern run_loop_t run_loop_posix; -#endif - -#ifdef USE_COCOA_RUN_LOOP -extern run_loop_t run_loop_cocoa; -#endif - -#ifdef HAVE_WICED -extern run_loop_t run_loop_wiced; -#endif - // assert run loop initialized static void run_loop_assert(void){ -#ifndef EMBEDDED if (!the_run_loop){ log_error("ERROR: run_loop function called before run_loop_init!"); +#ifdef EMBEDDED exit(10); - } +#else + while(1); #endif + } } @@ -147,43 +137,16 @@ void run_loop_execute(void){ } // init must be called before any other run_loop call -void run_loop_init(RUN_LOOP_TYPE type){ -#ifndef EMBEDDED +void run_loop_init(const run_loop_t * run_loop){ if (the_run_loop){ log_error("ERROR: run loop initialized twice!"); - exit(10); - } -#endif - switch (type) { #ifdef EMBEDDED -#ifdef HAVE_WICED - case RUN_LOOP_WICED: - the_run_loop = &run_loop_wiced; - break; + while(1); #else - case RUN_LOOP_EMBEDDED: - the_run_loop = &run_loop_embedded; - break; + exit(10); #endif -#endif -#ifdef USE_POSIX_RUN_LOOP - case RUN_LOOP_POSIX: - the_run_loop = &run_loop_posix; - break; -#endif -#ifdef USE_COCOA_RUN_LOOP - case RUN_LOOP_COCOA: - the_run_loop = &run_loop_cocoa; - break; -#endif - default: - -#ifndef EMBEDDED - log_error("ERROR: invalid run loop type %u selected!", type); - exit(10); -#endif - break; } + the_run_loop = run_loop; the_run_loop->init(); } diff --git a/src/run_loop.h b/src/run_loop.h index da7268859..504c59416 100644 --- a/src/run_loop.h +++ b/src/run_loop.h @@ -58,13 +58,6 @@ extern "C" { #endif -typedef enum { - RUN_LOOP_POSIX = 1, - RUN_LOOP_COCOA, - RUN_LOOP_EMBEDDED, - RUN_LOOP_WICED, -} RUN_LOOP_TYPE; - typedef struct data_source { linked_item_t item; int fd; // <-- file descriptor to watch or 0 @@ -84,6 +77,15 @@ typedef struct timer { /* API_START */ +typedef struct run_loop run_loop_t; + +/** + * @brief Init main run loop. Must be called before any other run loop call. + * + * Use run_loop_$(RUN_LOOP_TYPE)_get_instance() from run_loop_$(RUN_LOOP_TYPE).h to get instance + */ +void run_loop_init(const run_loop_t * run_loop); + /** * @brief Set timer based on current time in milliseconds. */ @@ -106,11 +108,6 @@ int run_loop_remove_timer(timer_source_t *timer); */ uint32_t run_loop_get_time_ms(void); -/** - * @brief Init must be called before any other run_loop call. Use RUN_LOOP_EMBEDDED for embedded devices. - */ -void run_loop_init(RUN_LOOP_TYPE type); - /** * @brief Set data source callback. */ diff --git a/src/run_loop_private.h b/src/run_loop_private.h index 88d499dbe..b1bfa4eff 100644 --- a/src/run_loop_private.h +++ b/src/run_loop_private.h @@ -58,7 +58,7 @@ extern "C" { void run_loop_timer_dump(void); // internal use only -typedef struct { +struct run_loop { void (*init)(void); void (*add_data_source)(data_source_t *dataSource); int (*remove_data_source)(data_source_t *dataSource); @@ -68,7 +68,7 @@ typedef struct { void (*execute)(void); void (*dump_timer)(void); uint32_t (*get_time_ms)(void); -} run_loop_t; +}; #if defined __cplusplus } diff --git a/test/ble_client/Makefile b/test/ble_client/Makefile index a003b8d52..53ea0aa0c 100644 --- a/test/ble_client/Makefile +++ b/test/ble_client/Makefile @@ -12,16 +12,16 @@ VPATH += ${BTSTACK_ROOT}/src VPATH += ${BTSTACK_ROOT}/platform/posix/src COMMON = \ - utils.c \ + ad_parser.c \ + bk_linked_list.c \ btstack_memory.c \ - memory_pool.c \ - linked_list.c \ - run_loop.c \ - run_loop_posix.c \ + hci.c \ hci_cmds.c \ hci_dump.c \ - hci.c \ - ad_parser.c \ + memory_pool.c \ + run_loop.c \ + run_loop_posix.c \ + utils.c \ COMMON_OBJ = $(COMMON:.c=.o) diff --git a/test/gatt_client/Makefile b/test/gatt_client/Makefile index 1a22d9c3c..441a6dab2 100644 --- a/test/gatt_client/Makefile +++ b/test/gatt_client/Makefile @@ -12,18 +12,18 @@ VPATH += ${BTSTACK_ROOT}/src/ble VPATH += ${BTSTACK_ROOT}/platform/posix/src COMMON = \ - utils.c \ + ad_parser.c \ + att.c \ + att_dispatch.c \ + bk_linked_list.c \ btstack_memory.c \ - memory_pool.c \ - linked_list.c \ + gatt_client.c \ hci_cmds.c \ hci_dump.c \ - att_dispatch.c \ - att.c \ - ad_parser.c \ - gatt_client.c \ le_device_db_memory.c \ + memory_pool.c \ mock.c + utils.c \ COMMON_OBJ = $(COMMON:.c=.o) diff --git a/test/hfp/Makefile b/test/hfp/Makefile index e41b0f3cd..35218064d 100644 --- a/test/hfp/Makefile +++ b/test/hfp/Makefile @@ -8,38 +8,38 @@ CPPUTEST_HOME = ${BTSTACK_ROOT}/test/cpputest include ${BTSTACK_ROOT}/example/embedded/Makefile.inc COMMON = \ + sdp.c \ + sdp_query_rfcomm.c \ + bk_linked_list.c \ btstack_memory.c \ - linked_list.c \ - memory_pool.c \ - run_loop.c \ - run_loop_posix.c \ hci.c \ hci_cmds.c \ hci_dump.c \ l2cap.c \ l2cap_signaling.c \ - rfcomm.c \ + memory_pool.c \ remote_device_db_memory.c \ - sdp_util.c \ - utils.c \ + rfcomm.c \ + run_loop.c \ + run_loop_posix.c \ sdp_client.c \ sdp_parser.c \ sdp_query_util.c \ - sdp_query_rfcomm.c \ - sdp.c \ + sdp_util.c \ + utils.c \ MOCK = \ - btstack_memory.c \ - linked_list.c \ - memory_pool.c \ - remote_device_db_memory.c \ - hci_cmds.c \ - hci_dump.c \ - sdp_util.c \ - utils.c \ mock.c \ test_sequences.c \ + bk_linked_list.c \ + btstack_memory.c \ + hci_cmds.c \ + hci_dump.c \ + memory_pool.c \ + remote_device_db_memory.c \ + sdp_util.c \ + utils.c \ COMMON_OBJ = $(COMMON:.c=.o) MOCK_OBJ = $(MOCK:.c=.o) diff --git a/test/linked_list/Makefile b/test/linked_list/Makefile index 90a31f6f1..968f6f031 100644 --- a/test/linked_list/Makefile +++ b/test/linked_list/Makefile @@ -13,7 +13,7 @@ VPATH += ${BTSTACK_ROOT}/src VPATH += ${BTSTACK_ROOT}/platform/posix/src COMMON = \ - linked_list.c \ + bk_linked_list.c \ COMMON_OBJ = $(COMMON:.c=.o) diff --git a/test/remote_device_db/Makefile b/test/remote_device_db/Makefile index 56d770953..5726ece74 100644 --- a/test/remote_device_db/Makefile +++ b/test/remote_device_db/Makefile @@ -28,7 +28,7 @@ MEMORY = \ btstack_memory.c \ hci_dump.c \ remote_device_db_memory.c \ - linked_list.c + bk_linked_list.c FS_OBJ = $(FS:.c=.o) MEMORY_OBJ = $(MEMORY:.c=.o) diff --git a/test/security_manager/Makefile b/test/security_manager/Makefile index 656c94103..8a79ca4a1 100644 --- a/test/security_manager/Makefile +++ b/test/security_manager/Makefile @@ -15,15 +15,15 @@ COMMON = \ utils.c \ btstack_memory.c \ memory_pool.c \ - linked_list.c \ + bk_linked_list.c \ run_loop.c \ run_loop_posix.c \ hci_cmds.c \ hci_dump.c \ sm.c \ le_device_db_memory.c \ - rijndael.c \ - mock.c + rijndael.c \ + mock.c \ COMMON_OBJ = $(COMMON:.c=.o) diff --git a/test/security_manager/security_manager.c b/test/security_manager/security_manager.c index 1f3133878..8ea1c148e 100644 --- a/test/security_manager/security_manager.c +++ b/test/security_manager/security_manager.c @@ -175,7 +175,7 @@ void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){ TEST_GROUP(SecurityManager){ void setup(void){ btstack_memory_init(); - run_loop_init(RUN_LOOP_POSIX); + run_loop_init(run_loop_posix_get_instance()); sm_init(); sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); sm_set_authentication_requirements( SM_AUTHREQ_BONDING );