static int do_chip_disable()
{
    LOGV(__FUNCTION__);

    int ret = -1;
    int hci_sock = -1;

    LOGI("Stopping hciattach deamon");
    hci_sock = create_hci_sock();
    if (hci_sock < 0) goto out;
    ioctl(hci_sock, HCIDEVDOWN, HCI_DEV_ID);

     LOGI("Stopping hciattach deamon");
    if (property_set("ctl.stop", "hciattach") < 0) {
        LOGE("Error stopping hciattach");
        goto out;
    }

    if (set_bluetooth_power(0) < 0) {
        goto out;
    }

    ret = 0;

out:
    if (hci_sock >= 0) close(hci_sock);
    return ret;
}
int bt_is_enabled() {
    LOGV(__FUNCTION__);

    int hci_sock = -1;
    int ret = -1;
    struct hci_dev_info dev_info;


    // Check power first
    ret = check_bluetooth_power();
    if (ret == -1 || ret == 0) goto out;

    ret = -1;

    // Power is on, now check if the HCI interface is up
    hci_sock = create_hci_sock();
    if (hci_sock < 0) goto out;

    dev_info.dev_id = HCI_DEV_ID;
    if (ioctl(hci_sock, HCIGETDEVINFO, (void *)&dev_info) < 0) {
        ret = 0;
        goto out;
    }

    ret = hci_test_bit(HCI_UP, &dev_info.flags);

out:
    if (hci_sock >= 0) close(hci_sock);
    return ret;
}
int bt_is_enabled() {
    LOGV(__FUNCTION__);

    int hci_sock = -1;
    int ret = -1;
    struct hci_dev_info dev_info;


    // Check power first
    ret = check_bluetooth_power();
    LOGI("# %s();   check ret = %d", __func__, ret);	
    if (ret == -1 || ret == 0) goto out;

    ret = -1;

    // Power is on, now check if the HCI interface is up
    hci_sock = create_hci_sock();
    if (hci_sock < 0) goto out;

    dev_info.dev_id = HCI_DEV_ID;
    if (ioctl(hci_sock, HCIGETDEVINFO, (void *)&dev_info) < 0) {
        ret = 0;
        goto out;
    }

    if (dev_info.flags & (1 << (HCI_UP & 31))) {
        ret = 1;
    } else {
        ret = 0;
    }

out:
    if (hci_sock >= 0) close(hci_sock);
    return ret;
}
Beispiel #4
0
int bt_enable() {
    LOGV(__FUNCTION__);

    int ret = -1;
    int hci_sock = -1;
    int attempt = 1;	

    if (set_bluetooth_power(1) < 0) goto out;

    LOGI("Starting hciattach daemon");
    if (property_set("ctl.start", "hciattach") < 0) {
        LOGE("Failed to start hciattach");
        set_bluetooth_power(0);
        goto out;
    }

    // Try for 10 seconds, this can only succeed once hciattach has sent the
    // firmware and then turned on hci device via HCIUARTSETPROTO ioctl
    for (attempt = 1000; attempt > 0;  attempt--) {
        hci_sock = create_hci_sock();
        if (hci_sock < 0) goto out;

        ret = ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID);

        LOGV("bt_enable: ret: %d, errno: %d", ret, errno);
        if (!ret) {
            break;
        } else if (errno == EALREADY) {
            LOGW("Bluetoothd already started, unexpectedly!");
            break;
        }

        close(hci_sock);
        usleep(100000);  // 100 ms retry delay
    }
    if (attempt == 0) {
        LOGE("%s: Timeout waiting for HCI device to come up, error- %d, ",
            __FUNCTION__, ret);
        if (property_set("ctl.stop", "hciattach") < 0) {
            LOGE("Error stopping hciattach");
        }
        set_bluetooth_power(0);
        goto out;
    }

    LOGI("Starting bluetoothd deamon");
    if (property_set("ctl.start", "bluetoothd") < 0) {
        LOGE("Failed to start bluetoothd");
        set_bluetooth_power(0);
        goto out;
    }

    ret = 0;

out:
    if (hci_sock >= 0) close(hci_sock);
    return ret;
}
Beispiel #5
0
int bt_enable() {
    LOGV(__FUNCTION__);

    int ret = -1;
    int hci_sock = -1;
    int attempt = 1000;

    if (set_bluetooth_power(1) < 0) goto out;

    LOGI("Starting hciattach daemon");
    if (property_set("ctl.start", "hciattach") < 0) {
        LOGE("Failed to start hciattach");
        goto out;
    }

    // Try for 10 seconds, this can only succeed once hciattach has sent the
    // firmware and then turned on hci device via HCIUARTSETPROTO ioctl
    for (attempt = 1000; attempt > 0;  attempt--) {
        hci_sock = create_hci_sock();
        if (hci_sock < 0) goto out;

        if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) {
            break;
        }
        close(hci_sock);
        usleep(10000);  // 10 ms retry delay
    }
    if (attempt == 0) {
        LOGE("%s: Timeout waiting for HCI device to come up", __FUNCTION__);
        goto out;
    }

    LOGI("Starting bluetoothd deamon");
    if (property_set("ctl.start", "bluetoothd") < 0) {
        attempt = 0;
        LOGE("Failed to start bluetoothd");
        goto out;
    }
    sleep(HCID_START_DELAY_SEC);

    ret = 0;

out:
    if (hci_sock >= 0) close(hci_sock);
    if(attempt == 0){
        LOGI("Starting hciattach/bluetoothd FAILED...Kill hciattach & bluetoothd...");
        bt_disable();
    }
    return ret;
}
int bt_disable() {
    ALOGV(__FUNCTION__);

    int ret = -1;
    int hci_sock = -1;

    ALOGI("Stopping bluetoothd deamon");
    if (property_set("ctl.stop", "bluetoothd") < 0) {
        ALOGE("Error stopping bluetoothd");
        goto out;
    }
    usleep(HCID_STOP_DELAY_USEC);

    hci_sock = create_hci_sock();
    if (hci_sock < 0) goto out;
    ioctl(hci_sock, HCIDEVDOWN, HCI_DEV_ID);

#ifndef BLUETOOTH_HCIATTACH_USING_PROPERTY
    ALOGI("Stopping hciattach deamon");
    if (property_set("ctl.stop", "hciattach") < 0)
#else
    ALOGI("Disable hci tranport");
    if (property_set("bluetooth.hciattach", "false") < 0)
#endif
    {
        ALOGE("Error stopping hciattach");
        goto out;
    }

#ifndef BLUETOOTH_DOES_NOT_USE_RFKILL
    if (set_bluetooth_power(0) < 0) {
        goto out;
    }
#endif
    ret = 0;

out:
    if (hci_sock >= 0) close(hci_sock);
    return ret;
}
static int do_chip_enable()
{
    LOGV(__FUNCTION__);

    int ret = -1;
    int hci_sock = -1;
    int attempt;

    if (ll_chip_enable(0) < 0)
        goto out;

    // Wait for the HCI socket to be up; this can only succeed once hciattach
    // has sent the FW and then turned on hci device via HCIUARTSETPROTO ioctl
    for (attempt = HCIA_START_ATTEMPTS; attempt > 0;  attempt--) {
        hci_sock = create_hci_sock();
        if (hci_sock < 0) goto out;

        if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) {
            break;
        }
        close(hci_sock);
        usleep(100000);  // 100 ms retry delay
        if (!((attempt-1) % (HCIA_START_ATTEMPTS / 2)))
            if (ll_chip_enable(1) < 0)
                goto out;
    }
    if (attempt == 0) {
        LOGE("%s: Timeout waiting for HCI device to come up", __FUNCTION__);
        goto out;
    }

    ret = 0;

out:
    if (hci_sock >= 0) close(hci_sock);
    return ret;
}
int bt_is_enabled() {
    ALOGV(__FUNCTION__);

    int hci_sock = -1;
    int ret = -1;
    struct hci_dev_info dev_info;


#ifndef BLUETOOTH_DOES_NOT_USE_RFKILL
    // Check power first
    ret = check_bluetooth_power();
    if (ret == -1 || ret == 0) goto out;
#endif

    ret = -1;

    // Power is on, now check if the HCI interface is up
    hci_sock = create_hci_sock();
    if (hci_sock < 0) goto out;

    dev_info.dev_id = HCI_DEV_ID;
    if (ioctl(hci_sock, HCIGETDEVINFO, (void *)&dev_info) < 0) {
        ret = 0;
        goto out;
    }

    if (dev_info.flags & (1 << (HCI_UP & 31))) {
        ret = 1;
    } else {
        ret = 0;
    }

out:
    if (hci_sock >= 0) close(hci_sock);
    return ret;
}
Beispiel #9
0
/*
 * This thread opens a Bluez HCI socket and waits for ANT messages.
 */
void *ANTHCIRxThread(void *pvHCIDevice)
{
   int ret = ANT_STATUS_SUCCESS;
   int rxSocket;
   int len;
   unsigned char buf[HCI_MAX_EVENT_SIZE];
   int result;
   struct hci_filter eventVendorFilter;
   ANT_FUNC_START();

   (void)pvHCIDevice; //unused waring

   ANT_DEBUG_D("Entering ANTHCIRxThread");

   rxSocket = create_hci_sock();
   if (rxSocket < 0)
   {
      ANT_DEBUG_E("can't open HCI socket in rx thread: %s", strerror(errno));

      ret = ANT_STATUS_FAILED;
      goto out;
   }

   eventVendorFilter.type_mask = TYPE_MASK_EVENT_PACKET;
   eventVendorFilter.event_mask[0] = 0;
   eventVendorFilter.event_mask[1] = EVENT_MASK_1_EVENT_VENDOR;
   eventVendorFilter.opcode = htobs(ANT_EVENT_VENDOR_CODE);

   if (setsockopt(rxSocket, SOL_HCI, HCI_FILTER, &eventVendorFilter, sizeof(eventVendorFilter)) < 0)
   {
      ANT_ERROR("failed to set socket options: %s", strerror(errno));

      ret = ANT_STATUS_FAILED;
      goto close;
   }

   /* continue running as long as not terminated */
   while (get_and_set_radio_status() == RADIO_STATUS_ENABLED)
   {
      struct pollfd p;
      int n;

      p.fd = rxSocket;
      p.events = POLLIN;

      ANT_DEBUG_V("    RX: Polling HCI for data...");

      /* poll socket, wait for ANT messages */
      while ((n = poll(&p, 1, 2500)) == -1)
      {
         if (errno == EAGAIN || errno == EINTR)
            continue;

         ANT_ERROR("failed to poll socket: %s", strerror(errno));

         ret = ANT_STATUS_FAILED;
         goto close;
      }

      /* we timeout once in a while */
      /* this let's us the chance to check if we were terminated */
      if (0 == n)
      {
         ANT_DEBUG_V("    RX: Timeout");
         continue;
      }

      ANT_DEBUG_D("New HCI data available, reading...");

      /* read newly arrived data */
      /* TBD: rethink assumption about single arrival */
      while ((len = read(rxSocket, buf, sizeof(buf))) < 0)
      {
         if (errno == EAGAIN || errno == EINTR)
            continue;

         ANT_ERROR("failed to read socket: %s", strerror(errno));

         ret = ANT_STATUS_FAILED;
         goto close;
      }

      hci_event_packet_t *event_packet = (hci_event_packet_t *)buf;
      int hci_payload_len = validate_hci_event_packet(event_packet, len);
      if (hci_payload_len == -1)
      {
         // part of the message is incorrect, ignore it. validate_event_packet will log error
         continue;
      }

      ANT_SERIAL(event_packet->hci_payload, hci_payload_len, 'R');

      if(RxParams.pfRxCallback != NULL)
      {
         RxParams.pfRxCallback(hci_payload_len, event_packet->hci_payload);
      }
      else
      {
         ANT_ERROR("Can't send rx message - no callback registered");
      }
   }

close:
   result = pthread_mutex_trylock(&enableLock);
   ANT_DEBUG_D("rx thread close: trylock enableLock returned %d", result);

   if (result == 0)
   {
      ANT_DEBUG_W("rx thread socket has unexpectedly crashed");
#if USE_EXTERNAL_POWER_LIBRARY
      if (RxParams.pfStateCallback)
         RxParams.pfStateCallback(RADIO_STATUS_DISABLING);
      ant_disable();
      get_and_set_radio_status();
#else
      radio_status = RADIO_STATUS_DISABLED;
#endif
      RxParams.thread = 0;
      pthread_mutex_unlock(&enableLock);
   }
   else if (result == EBUSY)
   {
      ANT_DEBUG_V("rx thread socket was closed");
   }
   else
   {
      ANT_ERROR("rx thread close: trylock failed: %s", strerror(result));
   }

   if (-1 == close(rxSocket))
   {
      ANT_ERROR("failed to close hci device (socket handle=%#x): %s", rxSocket, strerror(errno));
   }
   else
   {
      ANT_DEBUG_D("closed hci device (socket handle=%#x)", rxSocket);
   }

out:
   ANT_FUNC_END();

   pthread_exit((void *)ret);

#if defined(ANDROID)
   return 0;
#endif
}
int bt_enable() {
    ALOGV(__FUNCTION__);

    int ret = -1;
    int hci_sock = -1;
    int attempt;

#ifndef BLUETOOTH_DOES_NOT_USE_RFKILL
    if (set_bluetooth_power(1) < 0) goto out;
#endif

#ifndef BLUETOOTH_HCIATTACH_USING_PROPERTY
    ALOGI("Starting hciattach daemon");
    if (property_set("ctl.start", "hciattach") < 0)
#else
    ALOGI("Enable hci tranport");
    if (property_set("bluetooth.hciattach", "true") < 0)
#endif
    {
        ALOGE("Failed to start hciattach");
#ifndef BLUETOOTH_DOES_NOT_USE_RFKILL
        set_bluetooth_power(0);
#endif
        goto out;
    }

    // Try for 10 seconds, this can only succeed once hciattach has sent the
    // firmware and then turned on hci device via HCIUARTSETPROTO ioctl
    for (attempt = 100; attempt > 0;  attempt--) {
        hci_sock = create_hci_sock();
        if (hci_sock < 0) goto out;

        ret = ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID);

        if (!ret) {
            break;
        } else if (errno == EALREADY) {
            ALOGW("Bluetoothd already started, unexpectedly!");
            break;
        }

        ALOGI("%s: ioctl(%d, HCIDEVUP, HCI_DEV_ID) failed: %s (%d)",
              __FUNCTION__, hci_sock, strerror(errno), errno);

        close(hci_sock);
        usleep(100 * 1000);  // 100 ms retry delay
    }
    if (attempt == 0) {
        ALOGE("%s: Timeout waiting for HCI device to come up, error- %d, ",
            __FUNCTION__, ret);
        if (property_set("ctl.stop", "hciattach") < 0) {
            ALOGE("Error stopping hciattach");
        }
#ifndef BLUETOOTH_DOES_NOT_USE_RFKILL
        set_bluetooth_power(0);
#endif
        goto out;
    }

    ALOGI("Starting bluetoothd deamon");
    if (property_set("ctl.start", "bluetoothd") < 0) {
        ALOGE("Failed to start bluetoothd");
#ifndef BLUETOOTH_DOES_NOT_USE_RFKILL
        set_bluetooth_power(0);
#endif
        goto out;
    }

    ret = 0;

out:
    if (hci_sock >= 0) close(hci_sock);
    return ret;
}
Beispiel #11
0
int bt_enable() {
    LOGV(__FUNCTION__);

    int ret = -1;
    int hci_sock = -1;
    int attempt;
	
    LOGD(__FUNCTION__);

    if (set_bluetooth_power(1) < 0) goto out;
    LOGD("Starting hciattach daemon");
    if (property_set("ctl.start", "hciattach") < 0) {
        LOGE("##Failed to start hciattach");
        set_bluetooth_power(0);
        goto out;
    }

    LOGD("Starting hciattach daemon A");

    // Try for 10 seconds, this can only succeed once hciattach has sent the
    // firmware and then turned on hci device via HCIUARTSETPROTO ioctl
    #define TRY_TIME 1000
    for (attempt = TRY_TIME; attempt > 0;  attempt--) {
        hci_sock = create_hci_sock();
        if (hci_sock < 0) {
           goto out;
        }
		
        ret = ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID);
        if (!ret) {
            break;
        }else{
	        if (1==attempt)
	             LOGE("ioctl error: %s (%d)", strerror(errno), errno);
        }

        close(hci_sock);
        usleep(10000);  // 10 ms retry delay
    }
    if (attempt == 0) {
        LOGE("%s: Timeout waiting for HCI device to come up [### ERROR ###]", __FUNCTION__);
        if (property_set("ctl.stop", "hciattach") < 0) {
            LOGE("Error stopping hciattach");
        }
        set_bluetooth_power(0);
        goto out;
    }

    LOGI("%s: %d.%2d seconds\n", __FUNCTION__,(TRY_TIME - attempt)/100,(TRY_TIME - attempt)%100  );
 
    LOGI("Starting bluetoothd deamon");
    if (property_set("ctl.start", "bluetoothd") < 0) {
        LOGE("Failed to start bluetoothd");
        set_bluetooth_power(0);
        goto out;
    }

    ret = 0;

out:
    if (hci_sock >= 0) close(hci_sock);
    return ret;
}