Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- bluetooth.old 2011-02-07 03:05:27.885987350 -0500
- +++ bluetooth.c 2011-02-16 22:53:36.716303796 -0500
- @@ -23,6 +23,11 @@
- #include <sys/types.h>
- #include <sys/stat.h>
- +#include <stdio.h>
- +#include <stdlib.h>
- +
- +#include <sys/mman.h>
- +
- #include <cutils/log.h>
- #include <cutils/properties.h>
- @@ -45,7 +50,7 @@
- static int rfkill_id = -1;
- static char *rfkill_state_path = NULL;
- -
- +//////////////////////////////////////////////////////////////////////
- static int init_rfkill() {
- char path[64];
- char buf[16];
- @@ -71,6 +76,7 @@
- return 0;
- }
- +//////////////////////////////////////////////////////////////////////
- static int check_bluetooth_power() {
- int sz;
- int fd = -1;
- @@ -108,6 +114,7 @@
- return ret;
- }
- +//////////////////////////////////////////////////////////////////////
- static int set_bluetooth_power(int on) {
- int sz;
- int fd = -1;
- @@ -137,6 +144,7 @@
- return ret;
- }
- +//////////////////////////////////////////////////////////////////////
- static inline int create_hci_sock() {
- int sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
- if (sk < 0) {
- @@ -146,12 +154,261 @@
- return sk;
- }
- +struct RefBase_s{
- + unsigned int magic;
- + unsigned int base;
- +};
- +
- +static const char const * REF_FILE = "/data/bluedroid_ref";
- +static const unsigned int MAGIC = 0x55665566;
- +
- +static int inc_atomic()
- +{
- + struct RefBase_s ref = {magic : MAGIC, base : 0};
- + /*l_type l_whence l_start l_len l_pid*/
- + struct flock fl = {F_RDLCK, SEEK_SET, 0, 0, 0};
- + int fd = -1;
- + int ret = -1;
- + int magic = MAGIC;
- +
- + LOGV("Enter inc_atomic");
- +
- + if ((fd = open(REF_FILE, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO)) == -1)
- + {
- + LOGW("btdroid warning: open or create bluedroid_ref error");
- + ret = -2;
- + goto out;
- + }
- +
- + LOGV("Trying to get lock in inc_atomic....");
- +
- + if (fcntl(fd, F_SETLKW, &fl) == -1)
- + {
- + LOGW("btdroid waring: F_SETLKW failed");
- + ret = -1;
- + goto out;
- + }
- +
- + LOGV("got lock");
- +
- + if (read(fd, (void *)&ref, sizeof(ref)) == -1)
- + {
- + LOGW("btdroid waring: read reference count error");
- + ret = -1;
- + goto out;
- + }
- +
- + if(ref.magic != MAGIC)
- + {
- + LOGV("btdroid reference file magic number is 0x5566");
- + ref.magic = MAGIC;
- + ref.base = 0;
- + }
- +
- + if(ref.base < 2)
- + {
- + ref.base += 1;
- + }
- + else
- + {
- + //do not increment the counter beyond 2
- + LOGW("btdroid waring: btdroid reference count already 2, count = %d", ref.base);
- + }
- +
- + fl.l_type = F_UNLCK;
- +
- + if (fcntl(fd, F_SETLK, &fl) == -1)
- + {
- + LOGW("btdroid waring: F_SETLK : F_UNLCK failed");
- + ret = -1;
- + goto out;
- + }
- +
- + fl.l_type = F_WRLCK;
- +
- + if (fcntl(fd, F_SETLKW, &fl) == -1)
- + {
- + LOGW("btdroid waring: F_SETLKW : F_WRLCK failed");
- + ret = -1;
- + goto out;
- + }
- +
- + lseek(fd, 0, SEEK_SET);
- +
- + if (write(fd, (const void*)&ref, sizeof(ref)) == -1)
- + {
- + LOGW("btdroid waring: write reference count failed");
- + ret = -1;
- + goto out;
- + }
- +
- + ret = ref.base;
- + LOGV("Exit inc_atomic ref = %d", ret);
- +out:
- + if(fd != -1) close(fd);
- + return ret;
- +}
- +
- +
- +static int dec_atomic()
- +{
- + struct RefBase_s ref = {magic : MAGIC, base : 0};
- + /*l_type l_whence l_start l_len l_pid*/
- + struct flock fl = {F_RDLCK, SEEK_SET, 0, 0, 0};
- + int fd = -1;
- + int ret = -1;
- + int magic = MAGIC;
- +
- + LOGV("Enter dec_atomic");
- +
- + if ((fd = open(REF_FILE, O_RDWR)) == -1)
- + {
- + LOGW("btdroid warning: open or create bluedroid_ref error");
- + ret = -2;
- + goto out;
- + }
- +
- + LOGV("Trying to get lock in inc_atomic....");
- +
- + if (fcntl(fd, F_SETLKW, &fl) == -1)
- + {
- + LOGW("btdroid waring: F_SETLKW failed");
- + ret = -1;
- + goto out;
- + }
- +
- + LOGV("got lock");
- +
- + if (read(fd, (void *)&ref, sizeof(ref)) == -1)
- + {
- + LOGW("btdroid waring: read reference count error");
- + ret = -1;
- + goto out;
- + }
- +
- + if(ref.magic != MAGIC)
- + {
- + LOGV("btdroid reference file magic number is 0x5566");
- + ref.magic = MAGIC;
- + ref.base = 1;
- + }
- +
- + if(ref.base == 0)
- + {
- + LOGW("btdroid waring: dereference btdroid reference base = %d", ref.base);
- + ref.base = 1;
- + }
- +
- + ref.base -= 1;
- +
- + fl.l_type = F_UNLCK;
- +
- + if (fcntl(fd, F_SETLK, &fl) == -1)
- + {
- + LOGW("btdroid waring: F_SETLK : F_UNLCK failed");
- + ret = -1;
- + goto out;
- + }
- +
- + fl.l_type = F_WRLCK;
- +
- + if (fcntl(fd, F_SETLKW, &fl) == -1)
- + {
- + LOGW("btdroid waring: F_SETLKW : F_WRLCK failed");
- + ret = -1;
- + goto out;
- + }
- +
- + lseek(fd, 0, SEEK_SET);
- +
- + if (write(fd, (const void*)&ref, sizeof(ref)) == -1)
- + {
- + LOGW("btdroid waring: write reference count failed");
- + ret = -1;
- + goto out;
- + }
- +
- + ret = ref.base;
- + LOGV("Exit inc_atomic ref = %d", ret);
- +out:
- + if(fd != -1) close(fd);
- + return ret;
- +}
- +
- +//////////////////////////////////////////////////////////////////////
- int bt_enable() {
- LOGV(__FUNCTION__);
- int ret = -1;
- int hci_sock = -1;
- int attempt;
- + int refcount = 0;
- +
- + if(bt_is_enabled() == 1)
- + {
- + LOGI("bt has been enabled already. inc reference count only");
- + refcount = inc_atomic();
- + LOGV("after inc : reference count = %d", refcount);
- + return 0;
- + }
- +
- + 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;
- + // }
- +
- + LOGI("Starting bt_start");
- + if (property_set("ctl.start", "bt_start") < 0) {
- + LOGE("Failed to start bt_start");
- + 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 daemon");
- + if (property_set("ctl.start", "bluetoothd") < 0) {
- + LOGE("Failed to start bluetoothd");
- + goto out;
- + }
- + sleep(HCID_START_DELAY_SEC);
- +
- + ret = 0;
- +
- +out:
- + if (hci_sock >= 0) close(hci_sock);
- + if(0 == ret)
- + {
- + refcount = inc_atomic();
- + LOGV("after inc : reference count = %d", refcount);
- + }
- + return ret;
- +}
- +
- +//////////////////////////////////////////////////////////////////////
- +int _bt_enable() {
- + LOGV(__FUNCTION__);
- +
- + int ret = -1;
- + int hci_sock = -1;
- + int attempt;
- if (set_bluetooth_power(1) < 0) goto out;
- @@ -178,7 +435,7 @@
- goto out;
- }
- - LOGI("Starting bluetoothd deamon");
- + LOGI("Starting bluetoothd daemon");
- if (property_set("ctl.start", "bluetoothd") < 0) {
- LOGE("Failed to start bluetoothd");
- goto out;
- @@ -192,13 +449,24 @@
- return ret;
- }
- +//////////////////////////////////////////////////////////////////////
- int bt_disable() {
- LOGV(__FUNCTION__);
- int ret = -1;
- int hci_sock = -1;
- + int refcount = 0;
- - LOGI("Stopping bluetoothd deamon");
- + refcount = dec_atomic();
- +
- + LOGV("after dec : reference count = %d", refcount);
- + if(refcount > 0)
- + {
- + LOGV("bt_disable: other app using bt so just do noting");
- + return 0;
- + }
- +
- + LOGI("Stopping bluetoothd daemon");
- if (property_set("ctl.stop", "bluetoothd") < 0) {
- LOGE("Error stopping bluetoothd");
- goto out;
- @@ -209,7 +477,47 @@
- if (hci_sock < 0) goto out;
- ioctl(hci_sock, HCIDEVDOWN, HCI_DEV_ID);
- - LOGI("Stopping hciattach deamon");
- + // LOGI("Stopping hciattach daemon");
- + // if (property_set("ctl.stop", "hciattach") < 0) {
- + // LOGE("Error stopping hciattach");
- + // goto out;
- + // }
- +
- + LOGI("Starting bt_stop");
- + if (property_set("ctl.start", "bt_stop") < 0) {
- + LOGE("Failed to start bt_stop");
- + goto out;
- + }
- +
- + if (set_bluetooth_power(0) < 0) {
- + goto out;
- + }
- + ret = 0;
- +
- +out:
- + if (hci_sock >= 0) close(hci_sock);
- + return ret;
- +}
- +
- +//////////////////////////////////////////////////////////////////////
- +int _bt_disable() {
- + LOGV(__FUNCTION__);
- +
- + int ret = -1;
- + int hci_sock = -1;
- +
- + LOGI("Stopping bluetoothd daemon");
- + if (property_set("ctl.stop", "bluetoothd") < 0) {
- + LOGE("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);
- +
- + LOGI("Stopping hciattach daemon");
- if (property_set("ctl.stop", "hciattach") < 0) {
- LOGE("Error stopping hciattach");
- goto out;
- @@ -225,6 +533,7 @@
- return ret;
- }
- +//////////////////////////////////////////////////////////////////////
- int bt_is_enabled() {
- LOGV(__FUNCTION__);
- @@ -269,3 +578,4 @@
- }
- return 0;
- }
- +
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement