Android BlueDroid (2): the process of enabling Bluetooth init, androidbluedroid
Keywords: bluedroid initNative enableNative BTIF_TASK BTU_TASKbt_hc_work_thread set_power preload GKI
Author: xubin341719 (You are welcome to reprint it. Please indicate the author. Please respect copyright. Thank you !)
Correct the mistakes and learn and make progress together !!
1. Overview of the Bluetooth enabling process, as shown in: init and enable
Similar to common function calls, the android upper layer uses APP --> Native --> JNI --> javasthinterface --> bluetooth HCIinterface. The init, set_power, and preload functions are implemented in HCI interface.
Init and enable functions:
(1) Create: btif_task/BTIF_TASK
(2) initialize BTE
(3) create: btu_task/BTU_TASK
(4) initialize HCI and serial port, and start the main HCI thread: bt_hc_callback, chip power-on, RF parameter initialization, and Bluetooth address name settings;
(5) Create: bt_hc_worker_thread Bluetooth main thread, send receiving command;
(6) initialize the Bluetooth protocol stack;
II. Implementation of initNative Functions
This part mainly starts the corresponding sock, protocol stack initialization, and btif_task, and listens for processing status messages related to the Bluetooth interface. The implementation process is as follows.
1. Call some functions (starting with adatper)
Packages \ apps \ Bluetooth \ src \ com \ android \ bluetooth \ btservice \ AdapterService. java
Public void onCreate () {super. onCreate (); if (DBG) debugLog ("onCreate"); mBinder = new AdapterServiceBinder (this); mAdapterProperties = new AdapterProperties (this); mAdapterStateMachine = AdapterState. make (this, mAdapterProperties); mJniCallbacks = new JniCallbacks (mAdapterStateMachine, mAdapterProperties); initNative (); // call the initNative function; mNativeAvailable = true; mCallbacks = new RemoteCallbackList <iblustmthcallback> (); // Load the name and address getAdapterPropertyNative (callback actionlayer. BT_PROPERTY_BDADDR); getAdapterPropertyNative (jsonactionlayer. BT_PROPERTY_BDNAME);} private native boolean initNative ();
2. Implementation of the JNI function, which is the same as other JNI implementations.
Packages \ apps \ Bluetooth \ jni \Com_android_bluetooth_btservice_AdapterService.cpp
Static JNINativeMethod sMethods [] = {/* name, signature, funcPtr */{"classInitNative", "() V", (void *) classInitNative}, {"initNative ", "() Z", (void *) initNative}, // Native function implementation ............}
Packages \ apps \ Bluetooth \ jni \ com_android_rjth_btservice_adapterservice.cpp
The specific implementation of the initNative function is implemented by calling the init function in C through the bt_interface_t struct. At the same time, the sblustmthcallbacks callback function struct is passed in. This function struct is important. The underlying state changes are implemented through the function in this callback function struct.
Static const implements * sblustmthinterface = NULL; static bool initNative (JNIEnv * env, jobject obj) {sJniCallbacksObj = env-> NewGlobalRef (env-> GetObjectField (obj, sJniCallbacksField )); if (sblustmthinterface) {int ret = sblustmthinterface-> init (& sblustmthcallbacks); // call the corresponding interface function of C if (ret! = BT_STATUS_SUCCESS) {// if an Error occurs, handle the Error; ALOGE ("Error while setting the callbacks \ n"); sblustmthinterface = NULL; return JNI_FALSE ;} if (sblustmthsocketinterface = (btsock_interface_t *) sblustmthinterface-> get_profile_interface (response) = NULL) {ALOGE ("Error getting socket interface");} return JNI_TRUE ;} return JNI_FALSE ;}
3. JNI calls the function implementation in C
C language implements upper-layer functions, and ultimately implements JAVA's action to call C functions. This is a specific step for the android system to operate on the kernel. If you are a beginner, it is recommended to clarify this part. The underlying operations of the entire android system are implemented in this way.
External \ bluetooth \ bluedroid \ btif \ src \ bluetooth. c
Static const bt_interface_t incluthinterface = {// implementation of the init function in the sizeof (incluthinterface), init, // C function corresponding to the Bluetooth interface function; enable, disable, cleanup, get_adapter_properties, get_adapter_property, identifier, start_discovery, cancel_discovery, create_bond, remove_bond, cancel_bond, pin_reply, ssp_reply, get_profile_interface, identifier, identifier, # if BLE_INCLUDED = TRUE le_test_mode, # else NULL, # endif config_hci_snoop_log };
4. Init function implementation process in Bluetooth interface functions
External \ bluetooth \ bluedroid \ btif \ src \ bluetooth. c
Static int init (bt_callbacks_t * callbacks) {ALOGI ("init");/* sanity check */if (interface_ready () = TRUE) // check whether the interface function is ready; return BT_STATUS_DONE;/* store reference to user callbacks */bt_hal_cbacks = callbacks; // It is very important to save the corresponding callback function. At the beginning, the Code ignores this part, the following section describes the implementation and functions of callback functions./* add checks for individual callbacks? */Bt_utils_init (); // tool set initialization, initializing a mutex lock. /* Init btif */btif_init_bluetooth (); // initialize the Bluetooth interface descrithinterface return BT_STATUS_SUCCESS ;}
5. Implement Bluetooth interface initialization. btif_init_bluetooth creates a BTIF task and prepares to enable the scheduling program through Bluetooth.
The specific implementation process is as follows. We will explain the code in detail below. Mainly completed:
(1) Processing of Bluetooth names in bt_config.xml files;
(2) initialize GKI and perform detailed analysis in the subsequent sections;
(3) BlueHCLibInterface initialization, implementation of power \ preload \ and other functions, BlueDreoid log level settings;
(4) BTIF_TASK thread creation, which is also important.
External \ bluetooth \ bluedroid \ btif \ src \ btif_core.c
Bt_status_t btif_init_bluetooth () {UINT8 status; btif_config_init (); // create a sock thread and initialize related data in/data/misc/bluedroid/bt_config.xml; bte_main_boot_entry (); // (1) BTE Chip Protocol Stack entry API, Bluetooth protocol stack/chip initialization, GKI init;/* As part of the init, fetch the local bd addr */memset (& btif_local_bd_addr, 0, sizeof (bt_bdaddr_t); // get the Bluetooth address and write it to the relevant file; encrypt (& btif_local_bd_addr ); /* start btif task */status = GKI_create_task ( Btif_task, BTIF_TASK, BTIF_TASK_STR, (UINT16 *) (UINT8 *) btif_task_stack + BTIF_TASK_STACK_SIZE), sizeof (btif_task_stack); // (2), Creates BTIF task and prepares BT scheduler for startup create a Bluetooth task interface and prepare if (status! = GKI_SUCCESS) return BT_STATUS_FAIL; return BT_STATUS_SUCCESS ;}
(1) BTE Chip Protocol Stack entry API, Bluetooth protocol stack/chip initialization, GKI init;
External \ bluetooth \ bluedroid \ main \ bte_main.c
Void bte_main_boot_entry (void) {/* initialize OS */GKI_init (); // 1), GKI initialization, called only once during initialization. Bte_main_in_hw_init (); // 2). initialize the static bt_hc_interface_t * bt_hc_if = NULL; bte_load_conf (BTE_STACK_CONF_FILE); // 3). initialize the bluedroid debugging information level; # if (BTTRC_INCLUDED = TRUE) // Initialize the printing information;/* Initialize trace feature */BTTRC_TraceInit (MAX_TRACE_RAM_SIZE, & BTE_TraceLogBuf [0], BTTRC_METHOD_RAM); # endif}
1) GKI initialization, called only once during initialization
Reference mutex: http://blog.csdn.net/kingmax26/article/details/5338065;
External \ bluetooth \ bluedroid \ gki \ ulinux \ gki_ulinux.c
Void GKI_init (void) {pthread_mutexattr_t attr; tGKI_ OS * p_ OS; memset (& gki_cb, 0, sizeof (gki_cb); gki_buffer_init (); // 1 )) GKI buffer, buffer pool initialization; gki_timers_init (); // 2), GKI timer initialization; gki_cb.com.OSTicks = (UINT32) times (0); pthread_mutexattr_init (& attr ); // 3) initialize the pthread_mutexattr_t structure; # ifndef _ CYGWIN _ pthread_mutexattr_settype (& attr, PTHREAD_MUTEX_RECURSIVE_NP); // 4 )), set mutex type # endif p_ OS = & gki_cb. OS; pthread_mutex_init (& p_ OS-> GKI_mutex, & attr); // 5), initialize the mutex GKI_mutex; /* pthread_mutex_init (& GKI_sched_mutex, NULL); */# if (GKI_DEBUG = TRUE) pthread_mutex_init (& p_ OS-> GKI_trace_mutex, NULL); // 6 )) initialize the mutex volume; # endif/* pthread_mutex_init (& thread_delay_mutex, NULL); * // * used in GKI_delay * // * pthread_cond_init (& thread_delay_cond, NULL ); * // * Initialiase GKI_timer_update suspend variables & mutexes to be in running state. * this works too even if necessary is defined in btld.txt */p_ OS-> no_timer_suspend = begin; pthread_mutex_init (& p_ OS-> gki_timer_mutex, NULL); 7), and initialize mutex records; # ifndef NO_GKI_RUN_RETURN pthread_cond_init (& p_ OS-> gki_timer_cond, NULL); # endif}
2) initialize the struct static bt_hc_interface_t * bt_hc_if = NULL;
Bte_main_in_hw_init ();
Assign a value to bt_hc_if:
Static const extends thhclibinterface = {sizeof (bt_hc_interface_t), init, // init function implementation in hci lib; set_power, lpm, preload, postload, transmit_buf, set_rxflow, logging, cleanup };
3) initialize the bluedroid debugging information level
Bte_load_conf (BTE_STACK_CONF_FILE );
Parse the configuration information in the bt_stack.conf file.
(2) create a Bluetooth task interface and prepare Creates BTIF task and prepares BT scheduler for startup for scheduling.
status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR, (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE), sizeof(btif_task_stack));
6. btif_task process-related processing functions
External \ bluetooth \ bluedroid \ btif \ src \ btif_dm.c
Btif_task waits for the corresponding status sent by bta_sys_sendmsg to be received for processing.
Static void btif_task (UINT32 params ){.................. For (;) {/* wait for specified events */event = GKI_wait (0 xFFFF, 0); // a separate section will be provided after inter-process communication between GKI; if (event = BT_EVT_TRIGGER_STACK_INIT) // The protocol stack Initialization is complete ;............ If (event = BT_EVT_HARDWARE_INIT_FAIL) // hardware initialization error ............ If (event & EVENT_MASK (GKI_SHUTDOWN_EVT) // receives the close message ;............ If (event & TASK_MBOX_1_EVT_MASK )............}}
Help: system diagram of the android42 Bluetooth (bluedroid) protocol stack
Blog.csdn.net/innost/article/details/9187199
Make menuconfig command Error
Obtain the target rule of menuconfig from the makefile and analyze its rule to better solve this problem. Personal suggestions, for your reference only.
Hope to solve your problem.