Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 9.88 KB | None | 0 0
  1. --- bluetooth.old   2011-02-07 03:05:27.885987350 -0500
  2. +++ bluetooth.c 2011-02-16 22:53:36.716303796 -0500
  3. @@ -23,6 +23,11 @@
  4.  #include <sys/types.h>
  5.  #include <sys/stat.h>
  6.  
  7. +#include <stdio.h>
  8. +#include <stdlib.h>
  9. +
  10. +#include <sys/mman.h>
  11. +
  12.  #include <cutils/log.h>
  13.  #include <cutils/properties.h>
  14.  
  15. @@ -45,7 +50,7 @@
  16.  static int rfkill_id = -1;
  17.  static char *rfkill_state_path = NULL;
  18.  
  19. -
  20. +//////////////////////////////////////////////////////////////////////
  21.  static int init_rfkill() {
  22.      char path[64];
  23.      char buf[16];
  24. @@ -71,6 +76,7 @@
  25.      return 0;
  26.  }
  27.  
  28. +//////////////////////////////////////////////////////////////////////
  29.  static int check_bluetooth_power() {
  30.      int sz;
  31.      int fd = -1;
  32. @@ -108,6 +114,7 @@
  33.      return ret;
  34.  }
  35.  
  36. +//////////////////////////////////////////////////////////////////////
  37.  static int set_bluetooth_power(int on) {
  38.      int sz;
  39.      int fd = -1;
  40. @@ -137,6 +144,7 @@
  41.      return ret;
  42.  }
  43.  
  44. +//////////////////////////////////////////////////////////////////////
  45.  static inline int create_hci_sock() {
  46.      int sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
  47.      if (sk < 0) {
  48. @@ -146,12 +154,261 @@
  49.      return sk;
  50.  }
  51.  
  52. +struct RefBase_s{
  53. +    unsigned int magic;
  54. +    unsigned int base;
  55. +};
  56. +
  57. +static const char const * REF_FILE = "/data/bluedroid_ref";
  58. +static const unsigned int MAGIC = 0x55665566;
  59. +
  60. +static int inc_atomic()
  61. +{
  62. +    struct RefBase_s ref = {magic : MAGIC, base : 0};
  63. +                        /*l_type l_whence l_start l_len l_pid*/
  64. +    struct flock fl = {F_RDLCK,    SEEK_SET,  0,        0,      0};
  65. +    int fd = -1;
  66. +    int ret = -1;
  67. +    int magic = MAGIC;
  68. +
  69. +    LOGV("Enter inc_atomic");
  70. +
  71. +    if ((fd = open(REF_FILE, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO)) == -1)
  72. +    {
  73. +        LOGW("btdroid warning: open or create bluedroid_ref error");
  74. +        ret = -2;
  75. +        goto out;
  76. +    }
  77. +
  78. +    LOGV("Trying to get lock in inc_atomic....");
  79. +
  80. +    if (fcntl(fd, F_SETLKW, &fl) == -1)
  81. +    {
  82. +        LOGW("btdroid waring: F_SETLKW failed");
  83. +        ret = -1;
  84. +        goto out;
  85. +    }
  86. +
  87. +    LOGV("got lock");
  88. +
  89. +    if (read(fd, (void *)&ref, sizeof(ref)) == -1)
  90. +    {
  91. +        LOGW("btdroid waring: read reference count error");
  92. +        ret = -1;
  93. +        goto out;
  94. +    }
  95. +
  96. +    if(ref.magic != MAGIC)
  97. +    {
  98. +        LOGV("btdroid reference file magic number is 0x5566");
  99. +        ref.magic = MAGIC;
  100. +        ref.base = 0;    
  101. +    }
  102. +
  103. +    if(ref.base < 2)
  104. +    {
  105. +        ref.base += 1;
  106. +    }
  107. +    else
  108. +    {
  109. +        //do not increment the counter beyond 2
  110. +        LOGW("btdroid waring: btdroid reference count already 2, count = %d", ref.base);
  111. +    }
  112. +    
  113. +    fl.l_type = F_UNLCK;
  114. +
  115. +    if (fcntl(fd, F_SETLK, &fl) == -1)
  116. +    {
  117. +        LOGW("btdroid waring: F_SETLK : F_UNLCK failed");
  118. +        ret = -1;
  119. +        goto out;
  120. +    }
  121. +
  122. +    fl.l_type = F_WRLCK;
  123. +
  124. +    if (fcntl(fd, F_SETLKW, &fl) == -1)
  125. +    {
  126. +        LOGW("btdroid waring: F_SETLKW : F_WRLCK failed");
  127. +        ret = -1;
  128. +        goto out;
  129. +    }
  130. +
  131. +    lseek(fd, 0, SEEK_SET);
  132. +
  133. +    if (write(fd, (const void*)&ref, sizeof(ref)) == -1)
  134. +    {
  135. +        LOGW("btdroid waring: write reference count failed");
  136. +        ret = -1;
  137. +        goto out;
  138. +    }
  139. +
  140. +    ret = ref.base;
  141. +    LOGV("Exit inc_atomic ref = %d", ret);
  142. +out:
  143. +    if(fd != -1) close(fd);
  144. +    return ret;
  145. +}
  146. +
  147. +
  148. +static int dec_atomic()
  149. +{
  150. +    struct RefBase_s ref = {magic : MAGIC, base : 0};
  151. +                        /*l_type l_whence l_start l_len l_pid*/
  152. +    struct flock fl = {F_RDLCK,    SEEK_SET,  0,        0,      0};
  153. +    int fd = -1;
  154. +    int ret = -1;
  155. +    int magic = MAGIC;
  156. +
  157. +    LOGV("Enter dec_atomic");
  158. +
  159. +    if ((fd = open(REF_FILE, O_RDWR)) == -1)
  160. +    {
  161. +        LOGW("btdroid warning: open or create bluedroid_ref error");
  162. +        ret = -2;
  163. +        goto out;
  164. +    }
  165. +
  166. +    LOGV("Trying to get lock in inc_atomic....");
  167. +
  168. +    if (fcntl(fd, F_SETLKW, &fl) == -1)
  169. +    {
  170. +        LOGW("btdroid waring: F_SETLKW failed");
  171. +        ret = -1;
  172. +        goto out;
  173. +    }
  174. +
  175. +    LOGV("got lock");
  176. +
  177. +    if (read(fd, (void *)&ref, sizeof(ref)) == -1)
  178. +    {
  179. +        LOGW("btdroid waring: read reference count error");
  180. +        ret = -1;
  181. +        goto out;
  182. +    }
  183. +
  184. +    if(ref.magic != MAGIC)
  185. +    {
  186. +        LOGV("btdroid reference file magic number is 0x5566");
  187. +        ref.magic = MAGIC;
  188. +        ref.base = 1;    
  189. +    }
  190. +
  191. +    if(ref.base == 0)
  192. +    {
  193. +        LOGW("btdroid waring: dereference btdroid reference  base = %d", ref.base);
  194. +        ref.base = 1;
  195. +    }
  196. +    
  197. +    ref.base -= 1;
  198. +    
  199. +    fl.l_type = F_UNLCK;
  200. +
  201. +    if (fcntl(fd, F_SETLK, &fl) == -1)
  202. +    {
  203. +        LOGW("btdroid waring: F_SETLK : F_UNLCK failed");
  204. +        ret = -1;
  205. +        goto out;
  206. +    }
  207. +
  208. +    fl.l_type = F_WRLCK;
  209. +
  210. +    if (fcntl(fd, F_SETLKW, &fl) == -1)
  211. +    {
  212. +        LOGW("btdroid waring: F_SETLKW : F_WRLCK failed");
  213. +        ret = -1;
  214. +        goto out;
  215. +    }
  216. +
  217. +    lseek(fd, 0, SEEK_SET);
  218. +
  219. +    if (write(fd, (const void*)&ref, sizeof(ref)) == -1)
  220. +    {
  221. +        LOGW("btdroid waring: write reference count failed");
  222. +        ret = -1;
  223. +        goto out;
  224. +    }
  225. +
  226. +    ret = ref.base;
  227. +    LOGV("Exit inc_atomic ref = %d", ret);
  228. +out:
  229. +    if(fd != -1) close(fd);
  230. +    return ret;
  231. +}
  232. +
  233. +//////////////////////////////////////////////////////////////////////
  234.  int bt_enable() {
  235.      LOGV(__FUNCTION__);
  236.  
  237.      int ret = -1;
  238.      int hci_sock = -1;
  239.      int attempt;
  240. +    int refcount = 0;
  241. +
  242. +    if(bt_is_enabled() == 1)
  243. +    {
  244. +        LOGI("bt has been enabled already. inc reference count only");
  245. +        refcount = inc_atomic();
  246. +        LOGV("after inc : reference count = %d", refcount);
  247. +        return 0;
  248. +    }
  249. +
  250. +    if (set_bluetooth_power(1) < 0) goto out;
  251. +
  252. +    //    LOGI("Starting hciattach daemon");
  253. +    //    if (property_set("ctl.start", "hciattach") < 0) {
  254. +    //        LOGE("Failed to start hciattach");
  255. +    //        goto out;
  256. +    //    }
  257. +    
  258. +    LOGI("Starting bt_start");
  259. +    if (property_set("ctl.start", "bt_start") < 0) {
  260. +      LOGE("Failed to start bt_start");
  261. +      goto out;
  262. +    }
  263. +
  264. +    // Try for 10 seconds, this can only succeed once hciattach has sent the
  265. +    // firmware and then turned on hci device via HCIUARTSETPROTO ioctl
  266. +    for (attempt = 1000; attempt > 0;  attempt--) {
  267. +        hci_sock = create_hci_sock();
  268. +        if (hci_sock < 0) goto out;
  269. +
  270. +        if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) {
  271. +            break;
  272. +        }
  273. +        close(hci_sock);
  274. +        usleep(10000);  // 10 ms retry delay
  275. +    }
  276. +    if (attempt == 0) {
  277. +        LOGE("%s: Timeout waiting for HCI device to come up", __FUNCTION__);
  278. +        goto out;
  279. +    }
  280. +
  281. +    LOGI("Starting bluetoothd daemon");
  282. +    if (property_set("ctl.start", "bluetoothd") < 0) {
  283. +        LOGE("Failed to start bluetoothd");
  284. +        goto out;
  285. +    }
  286. +    sleep(HCID_START_DELAY_SEC);
  287. +
  288. +    ret = 0;
  289. +
  290. +out:
  291. +    if (hci_sock >= 0) close(hci_sock);
  292. +    if(0 == ret)
  293. +    {
  294. +        refcount = inc_atomic();
  295. +        LOGV("after inc : reference count = %d", refcount);
  296. +    }
  297. +    return ret;
  298. +}
  299. +
  300. +//////////////////////////////////////////////////////////////////////
  301. +int _bt_enable() {
  302. +    LOGV(__FUNCTION__);
  303. +
  304. +    int ret = -1;
  305. +    int hci_sock = -1;
  306. +    int attempt;
  307.  
  308.      if (set_bluetooth_power(1) < 0) goto out;
  309.  
  310. @@ -178,7 +435,7 @@
  311.          goto out;
  312.      }
  313.  
  314. -    LOGI("Starting bluetoothd deamon");
  315. +    LOGI("Starting bluetoothd daemon");
  316.      if (property_set("ctl.start", "bluetoothd") < 0) {
  317.          LOGE("Failed to start bluetoothd");
  318.          goto out;
  319. @@ -192,13 +449,24 @@
  320.      return ret;
  321.  }
  322.  
  323. +//////////////////////////////////////////////////////////////////////
  324.  int bt_disable() {
  325.      LOGV(__FUNCTION__);
  326.  
  327.      int ret = -1;
  328.      int hci_sock = -1;
  329. +    int refcount = 0;
  330.  
  331. -    LOGI("Stopping bluetoothd deamon");
  332. +    refcount = dec_atomic();
  333. +
  334. +    LOGV("after dec : reference count = %d", refcount);
  335. +    if(refcount > 0)
  336. +    {
  337. +        LOGV("bt_disable: other app using bt so just do noting")
  338. +        return 0;
  339. +    }
  340. +
  341. +    LOGI("Stopping bluetoothd daemon");
  342.      if (property_set("ctl.stop", "bluetoothd") < 0) {
  343.          LOGE("Error stopping bluetoothd");
  344.          goto out;
  345. @@ -209,7 +477,47 @@
  346.      if (hci_sock < 0) goto out;
  347.      ioctl(hci_sock, HCIDEVDOWN, HCI_DEV_ID);
  348.  
  349. -    LOGI("Stopping hciattach deamon");
  350. +    //    LOGI("Stopping hciattach daemon");
  351. +    //    if (property_set("ctl.stop", "hciattach") < 0) {
  352. +    //        LOGE("Error stopping hciattach");
  353. +    //        goto out;
  354. +    //    }
  355. +
  356. +    LOGI("Starting bt_stop");
  357. +    if (property_set("ctl.start", "bt_stop") < 0) {
  358. +        LOGE("Failed to start bt_stop");
  359. +        goto out;
  360. +    }
  361. +
  362. +    if (set_bluetooth_power(0) < 0) {
  363. +        goto out;
  364. +    }
  365. +    ret = 0;
  366. +
  367. +out:
  368. +    if (hci_sock >= 0) close(hci_sock);
  369. +    return ret;
  370. +}
  371. +
  372. +//////////////////////////////////////////////////////////////////////
  373. +int _bt_disable() {
  374. +    LOGV(__FUNCTION__);
  375. +
  376. +    int ret = -1;
  377. +    int hci_sock = -1;
  378. +
  379. +    LOGI("Stopping bluetoothd daemon");
  380. +    if (property_set("ctl.stop", "bluetoothd") < 0) {
  381. +        LOGE("Error stopping bluetoothd");
  382. +        goto out;
  383. +    }
  384. +    usleep(HCID_STOP_DELAY_USEC);
  385. +
  386. +    hci_sock = create_hci_sock();
  387. +    if (hci_sock < 0) goto out;
  388. +    ioctl(hci_sock, HCIDEVDOWN, HCI_DEV_ID);
  389. +
  390. +    LOGI("Stopping hciattach daemon");
  391.      if (property_set("ctl.stop", "hciattach") < 0) {
  392.          LOGE("Error stopping hciattach");
  393.          goto out;
  394. @@ -225,6 +533,7 @@
  395.      return ret;
  396.  }
  397.  
  398. +//////////////////////////////////////////////////////////////////////
  399.  int bt_is_enabled() {
  400.      LOGV(__FUNCTION__);
  401.  
  402. @@ -269,3 +578,4 @@
  403.      }
  404.      return 0;
  405.  }
  406. +
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement