android5.1 蓝牙上电流程

2019-04-13 14:03发布

从UI 开始追踪代码,研究如何传递到driver层。 bluetoothenabler.java     @Override
    public void onSwitchChanged(Switch switchView, boolean isChecked) {
        if (mLocalAdapter != null) {
            mLocalAdapter.setBluetoothEnabled(isChecked);
        }
    } LocalBluetoothAdapter.java     public void setBluetoothEnabled(boolean enabled) {
        boolean success = enabled
                ? mAdapter.enable()
                : mAdapter.disable();
    } BluetoothAdapter.java     public boolean enable() {
        try {
            return mManagerService.enable(ActivityThread.currentPackageName());
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    } 经过一些转换,最终调用
BluetoothManagerService.java     private void handleEnable(boolean quietMode) {
        mQuietEnable = quietMode;         if(!mBluetooth.enable()) {
                Log.e(TAG,"IBluetooth.enable() returned false");
        } } AdapterService.java      public synchronized boolean enable(boolean quietMode) {

         Message m =
                 mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_ON);
         mAdapterStateMachine.sendMessage(m);
         return true;
     } AdapterState.java     StateMachine  机制 private class OffState extends State { public boolean processMessage(Message msg) { switch(msg.what) {
               case USER_TURN_ON: mPendingCommandState.setTurningOn(true);
                   transitionTo(mPendingCommandState);
                   sendMessageDelayed(START_TIMEOUT, START_TIMEOUT_DELAY);
                   adapterService.processStart(); } AdatperService.java void processStart() {       mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.STARTED)); } private class PendingCommandState extends State { public boolean processMessage(Message msg) { case STARTED:   { boolean ret = adapterService.enableNative();} } AdapterService.java  调用jni      路径packageappsluetoothsrccomandroidluetoothtservice
native boolean enableNative(); com_android_bluetooth_btservice_AdapterService.cpp       路径packagesappsluetoothjni static jboolean enableNative(JNIEnv* env, jobject obj) {
    ALOGV("%s:",__FUNCTION__);
    int ret = sBluetoothInterface->enable();
}
函数指针sBluetoothInterface  指向bluetooth.c      externalluetoothluedroiditfsrc
static int enable( void )
{
    return btif_enable_bluetooth();
} Btif_core.c   路径externalluetoothluedroiditfsrc
bt_status_t btif_enable_bluetooth(void)
{
    bte_main_enable();
} Bte_main.c   路径externalluetoothluedroidmain
void bte_main_enable()
{
      bte_hci_enable();
}
void bte_main_enable()
{
    bte_hci_enable();
} static void bte_hci_enable(void)
{ bt_hc_if->set_power(BT_HC_CHIP_PWR_ON); } Bt_hci_bdroid.c   路径externalluetoothluedroidhcisrc
static void set_power(bt_hc_chip_power_state_t state)
{
    int pwr_state;

    BTHCDBG("set_power %d", state);

    /* Calling vendor-specific part */
    pwr_state = (state == BT_HC_CHIP_PWR_ON) ? BT_VND_PWR_ON : BT_VND_PWR_OFF;

    vendor_send_command(BT_VND_OP_POWER_CTRL, &pwr_state);
}
Vendor.c     路径externalluetoothluedroidhcisrc
int vendor_send_command(bt_vendor_opcode_t opcode, void *param){   return vendor_interface->op(opcode, param);
} Bt_vendor_brcm.c    路径hardwareroadcomlibbtsrc
static int op(bt_vendor_opcode_t opcode, void *param)
{     switch(opcode)
    {
        case BT_VND_OP_POWER_CTRL:
            {
                int *state = (int *) param;
                if (*state == BT_VND_PWR_OFF)
                    upio_set_bluetooth_power(UPIO_BT_POWER_OFF);
                else if (*state == BT_VND_PWR_ON)
                    upio_set_bluetooth_power(UPIO_BT_POWER_ON);
            }
            break; }
Upio.c  路径hardwareroadcomlibbtsrc
int upio_set_bluetooth_power(int on)
{     int sz;
    int fd = -1;
    int ret = -1;
    char buffer = '0';

    switch(on)
    {
        case UPIO_BT_POWER_OFF:
            buffer = '0';
            break;

        case UPIO_BT_POWER_ON:
            buffer = '1';
            break;
    }

    if (is_emulator_context())
    {
        /* if new value is same as current, return -1 */
        if (bt_emul_enable == on)
            return ret;

        UPIODBG("set_bluetooth_power [emul] %d", on);

        bt_emul_enable = on;
        return 0;
    }

    /* check if we have rfkill interface */
    if (is_rfkill_disabled())
        return 0;

    if (rfkill_id == -1)
    {
        if (init_rfkill())
            return ret;
    }

    fd = open(rfkill_state_path, O_WRONLY);

    if (fd < 0)
    {
        ALOGE("set_bluetooth_power : open(%s) for write failed: %s (%d)",
            rfkill_state_path, strerror(errno), errno);
        return ret;
    }

    sz = write(fd, &buffer, 1);

    if (sz < 0) {
        ALOGE("set_bluetooth_power : write(%s) failed: %s (%d)",
            rfkill_state_path, strerror(errno),errno);
    }
    else
        ret = 0;

    if (fd >= 0)
        close(fd);

    return ret;
} 通过设备节点写数据,从而通知driver进行上下电操作。(以上是代码逻辑分析)