socket.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <glib.h>
  14. #include <stdbool.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include "lib/bluetooth.h"
  18. #include "btio/btio.h"
  19. #include "lib/sdp.h"
  20. #include "lib/sdp_lib.h"
  21. #include "src/sdp-client.h"
  22. #include "src/sdpd.h"
  23. #include "src/log.h"
  24. #include "hal-msg.h"
  25. #include "ipc-common.h"
  26. #include "ipc.h"
  27. #include "utils.h"
  28. #include "bluetooth.h"
  29. #include "socket.h"
  30. #define RFCOMM_CHANNEL_MAX 30
  31. #define OPP_DEFAULT_CHANNEL 9
  32. #define HSP_AG_DEFAULT_CHANNEL 12
  33. #define HFP_AG_DEFAULT_CHANNEL 13
  34. #define PBAP_DEFAULT_CHANNEL 15
  35. #define MAP_MAS_DEFAULT_CHANNEL 16
  36. #define SVC_HINT_OBEX 0x10
  37. /* Hardcoded MAP stuff needed for MAS SMS Instance.*/
  38. #define DEFAULT_MAS_INSTANCE 0x00
  39. #define MAP_MSG_TYPE_SMS_GSM 0x02
  40. #define MAP_MSG_TYPE_SMS_CDMA 0x04
  41. #define DEFAULT_MAS_MSG_TYPE (MAP_MSG_TYPE_SMS_GSM | MAP_MSG_TYPE_SMS_CDMA)
  42. static struct ipc *hal_ipc = NULL;
  43. struct rfcomm_sock {
  44. int channel; /* RFCOMM channel */
  45. BtIOSecLevel sec_level;
  46. /* for socket to BT */
  47. int bt_sock;
  48. guint bt_watch;
  49. /* for socket to HAL */
  50. int jv_sock;
  51. guint jv_watch;
  52. bdaddr_t dst;
  53. uint32_t service_handle;
  54. uint8_t *buf;
  55. int buf_size;
  56. };
  57. struct rfcomm_channel {
  58. bool reserved;
  59. struct rfcomm_sock *rfsock;
  60. };
  61. static bdaddr_t adapter_addr;
  62. static uint8_t hal_mode = HAL_MODE_SOCKET_DEFAULT;
  63. static const uint8_t zero_uuid[16] = { 0 };
  64. /* Simple list of RFCOMM connected sockets */
  65. static GList *connections = NULL;
  66. static struct rfcomm_channel servers[RFCOMM_CHANNEL_MAX + 1];
  67. static uint32_t test_sdp_record_uuid16 = 0;
  68. static uint32_t test_sdp_record_uuid32 = 0;
  69. static uint32_t test_sdp_record_uuid128 = 0;
  70. static int rfsock_set_buffer(struct rfcomm_sock *rfsock)
  71. {
  72. socklen_t len = sizeof(int);
  73. int rcv, snd, size, err;
  74. err = getsockopt(rfsock->bt_sock, SOL_SOCKET, SO_RCVBUF, &rcv, &len);
  75. if (err < 0) {
  76. int err = -errno;
  77. error("getsockopt(SO_RCVBUF): %s", strerror(-err));
  78. return err;
  79. }
  80. err = getsockopt(rfsock->bt_sock, SOL_SOCKET, SO_SNDBUF, &snd, &len);
  81. if (err < 0) {
  82. int err = -errno;
  83. error("getsockopt(SO_SNDBUF): %s", strerror(-err));
  84. return err;
  85. }
  86. size = MAX(rcv, snd);
  87. DBG("Set buffer size %d", size);
  88. rfsock->buf = g_malloc(size);
  89. rfsock->buf_size = size;
  90. return 0;
  91. }
  92. static void cleanup_rfsock(gpointer data)
  93. {
  94. struct rfcomm_sock *rfsock = data;
  95. DBG("rfsock %p bt_sock %d jv_sock %d", rfsock, rfsock->bt_sock,
  96. rfsock->jv_sock);
  97. if (rfsock->jv_sock >= 0)
  98. if (close(rfsock->jv_sock) < 0)
  99. error("close() fd %d failed: %s", rfsock->jv_sock,
  100. strerror(errno));
  101. if (rfsock->bt_sock >= 0)
  102. if (close(rfsock->bt_sock) < 0)
  103. error("close() fd %d: failed: %s", rfsock->bt_sock,
  104. strerror(errno));
  105. if (rfsock->bt_watch > 0)
  106. if (!g_source_remove(rfsock->bt_watch))
  107. error("bt_watch source was not found");
  108. if (rfsock->jv_watch > 0)
  109. if (!g_source_remove(rfsock->jv_watch))
  110. error("stack_watch source was not found");
  111. if (rfsock->service_handle)
  112. bt_adapter_remove_record(rfsock->service_handle);
  113. if (rfsock->buf)
  114. g_free(rfsock->buf);
  115. g_free(rfsock);
  116. }
  117. static struct rfcomm_sock *create_rfsock(int bt_sock, int *hal_sock)
  118. {
  119. int fds[2] = {-1, -1};
  120. struct rfcomm_sock *rfsock;
  121. if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) < 0) {
  122. error("socketpair(): %s", strerror(errno));
  123. *hal_sock = -1;
  124. return NULL;
  125. }
  126. rfsock = g_new0(struct rfcomm_sock, 1);
  127. rfsock->jv_sock = fds[0];
  128. *hal_sock = fds[1];
  129. rfsock->bt_sock = bt_sock;
  130. DBG("rfsock %p", rfsock);
  131. if (bt_sock < 0)
  132. return rfsock;
  133. if (rfsock_set_buffer(rfsock) < 0) {
  134. cleanup_rfsock(rfsock);
  135. return NULL;
  136. }
  137. return rfsock;
  138. }
  139. static sdp_record_t *create_rfcomm_record(uint8_t chan, uuid_t *uuid,
  140. const char *svc_name,
  141. bool has_obex)
  142. {
  143. sdp_list_t *svclass_id;
  144. sdp_list_t *seq, *proto_seq, *pbg_seq;
  145. sdp_list_t *proto[3];
  146. uuid_t l2cap_uuid, rfcomm_uuid, obex_uuid, pbg_uuid;
  147. sdp_data_t *channel;
  148. sdp_record_t *record;
  149. record = sdp_record_alloc();
  150. if (!record)
  151. return NULL;
  152. record->handle = sdp_next_handle();
  153. svclass_id = sdp_list_append(NULL, uuid);
  154. sdp_set_service_classes(record, svclass_id);
  155. sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
  156. proto[0] = sdp_list_append(NULL, &l2cap_uuid);
  157. seq = sdp_list_append(NULL, proto[0]);
  158. sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
  159. proto[1] = sdp_list_append(NULL, &rfcomm_uuid);
  160. channel = sdp_data_alloc(SDP_UINT8, &chan);
  161. proto[1] = sdp_list_append(proto[1], channel);
  162. seq = sdp_list_append(seq, proto[1]);
  163. if (has_obex) {
  164. sdp_uuid16_create(&obex_uuid, OBEX_UUID);
  165. proto[2] = sdp_list_append(NULL, &obex_uuid);
  166. seq = sdp_list_append(seq, proto[2]);
  167. }
  168. proto_seq = sdp_list_append(NULL, seq);
  169. sdp_set_access_protos(record, proto_seq);
  170. sdp_uuid16_create(&pbg_uuid, PUBLIC_BROWSE_GROUP);
  171. pbg_seq = sdp_list_append(NULL, &pbg_uuid);
  172. sdp_set_browse_groups(record, pbg_seq);
  173. if (svc_name)
  174. sdp_set_info_attr(record, svc_name, NULL, NULL);
  175. sdp_data_free(channel);
  176. sdp_list_free(proto[0], NULL);
  177. sdp_list_free(proto[1], NULL);
  178. if (has_obex)
  179. sdp_list_free(proto[2], NULL);
  180. sdp_list_free(seq, NULL);
  181. sdp_list_free(proto_seq, NULL);
  182. sdp_list_free(pbg_seq, NULL);
  183. sdp_list_free(svclass_id, NULL);
  184. return record;
  185. }
  186. static sdp_record_t *create_opp_record(uint8_t chan, const char *svc_name)
  187. {
  188. uint8_t formats[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xff };
  189. uint8_t dtd = SDP_UINT8;
  190. uuid_t uuid;
  191. sdp_list_t *seq;
  192. sdp_profile_desc_t profile[1];
  193. void *dtds[sizeof(formats)], *values[sizeof(formats)];
  194. sdp_data_t *formats_list;
  195. sdp_record_t *record;
  196. size_t i;
  197. sdp_uuid16_create(&uuid, OBEX_OBJPUSH_SVCLASS_ID);
  198. record = create_rfcomm_record(chan, &uuid, svc_name, true);
  199. if (!record)
  200. return NULL;
  201. sdp_uuid16_create(&profile[0].uuid, OBEX_OBJPUSH_PROFILE_ID);
  202. profile[0].version = 0x0100;
  203. seq = sdp_list_append(NULL, profile);
  204. sdp_set_profile_descs(record, seq);
  205. for (i = 0; i < sizeof(formats); i++) {
  206. dtds[i] = &dtd;
  207. values[i] = &formats[i];
  208. }
  209. formats_list = sdp_seq_alloc(dtds, values, sizeof(formats));
  210. sdp_attr_add(record, SDP_ATTR_SUPPORTED_FORMATS_LIST, formats_list);
  211. sdp_list_free(seq, NULL);
  212. return record;
  213. }
  214. static sdp_record_t *create_pbap_record(uint8_t chan, const char *svc_name)
  215. {
  216. sdp_list_t *seq;
  217. sdp_profile_desc_t profile[1];
  218. uint8_t formats = 0x01;
  219. sdp_record_t *record;
  220. uuid_t uuid;
  221. sdp_uuid16_create(&uuid, PBAP_PSE_SVCLASS_ID);
  222. record = create_rfcomm_record(chan, &uuid, svc_name, true);
  223. if (!record)
  224. return NULL;
  225. sdp_uuid16_create(&profile[0].uuid, PBAP_PROFILE_ID);
  226. profile[0].version = 0x0101;
  227. seq = sdp_list_append(NULL, profile);
  228. sdp_set_profile_descs(record, seq);
  229. sdp_attr_add_new(record, SDP_ATTR_SUPPORTED_REPOSITORIES, SDP_UINT8,
  230. &formats);
  231. sdp_list_free(seq, NULL);
  232. return record;
  233. }
  234. static sdp_record_t *create_mas_record(uint8_t chan, const char *svc_name)
  235. {
  236. sdp_list_t *seq;
  237. sdp_profile_desc_t profile[1];
  238. uint8_t minst, mtype;
  239. sdp_record_t *record;
  240. uuid_t uuid;
  241. int cnt, ret;
  242. switch (hal_mode) {
  243. case HAL_MODE_SOCKET_DYNAMIC_MAP:
  244. /*
  245. * Service name for MAP is passed as XXYYname
  246. * XX - instance
  247. * YY - message type
  248. */
  249. ret = sscanf(svc_name, "%02hhx%02hhx%n", &minst, &mtype, &cnt);
  250. if (ret != 2 || cnt != 4)
  251. return NULL;
  252. svc_name += 4;
  253. break;
  254. case HAL_MODE_SOCKET_DEFAULT:
  255. minst = DEFAULT_MAS_INSTANCE;
  256. mtype = DEFAULT_MAS_MSG_TYPE;
  257. break;
  258. default:
  259. return NULL;
  260. }
  261. sdp_uuid16_create(&uuid, MAP_MSE_SVCLASS_ID);
  262. record = create_rfcomm_record(chan, &uuid, svc_name, true);
  263. if (!record)
  264. return NULL;
  265. sdp_uuid16_create(&profile[0].uuid, MAP_PROFILE_ID);
  266. profile[0].version = 0x0101;
  267. seq = sdp_list_append(NULL, profile);
  268. sdp_set_profile_descs(record, seq);
  269. sdp_attr_add_new(record, SDP_ATTR_MAS_INSTANCE_ID, SDP_UINT8, &minst);
  270. sdp_attr_add_new(record, SDP_ATTR_SUPPORTED_MESSAGE_TYPES, SDP_UINT8,
  271. &mtype);
  272. sdp_list_free(seq, NULL);
  273. return record;
  274. }
  275. static sdp_record_t *create_spp_record(uint8_t chan, const char *svc_name)
  276. {
  277. sdp_record_t *record;
  278. uuid_t uuid;
  279. sdp_uuid16_create(&uuid, SERIAL_PORT_SVCLASS_ID);
  280. record = create_rfcomm_record(chan, &uuid, svc_name, false);
  281. if (!record)
  282. return NULL;
  283. return record;
  284. }
  285. static sdp_record_t *create_app_record(uint8_t chan,
  286. const uint8_t *app_uuid,
  287. const char *svc_name)
  288. {
  289. sdp_record_t *record;
  290. uuid_t uuid;
  291. sdp_uuid128_create(&uuid, app_uuid);
  292. sdp_uuid128_to_uuid(&uuid);
  293. record = create_rfcomm_record(chan, &uuid, svc_name, false);
  294. if (!record)
  295. return NULL;
  296. return record;
  297. }
  298. static const struct profile_info {
  299. uint8_t uuid[16];
  300. uint8_t channel;
  301. uint8_t svc_hint;
  302. BtIOSecLevel sec_level;
  303. sdp_record_t * (*create_record)(uint8_t chan, const char *svc_name);
  304. } profiles[] = {
  305. {
  306. .uuid = {
  307. 0x00, 0x00, 0x11, 0x08, 0x00, 0x00, 0x10, 0x00,
  308. 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
  309. },
  310. .channel = HSP_AG_DEFAULT_CHANNEL,
  311. .svc_hint = 0,
  312. .sec_level = BT_IO_SEC_MEDIUM,
  313. .create_record = NULL
  314. }, {
  315. .uuid = {
  316. 0x00, 0x00, 0x11, 0x1F, 0x00, 0x00, 0x10, 0x00,
  317. 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
  318. },
  319. .channel = HFP_AG_DEFAULT_CHANNEL,
  320. .svc_hint = 0,
  321. .sec_level = BT_IO_SEC_MEDIUM,
  322. .create_record = NULL
  323. }, {
  324. .uuid = {
  325. 0x00, 0x00, 0x11, 0x2F, 0x00, 0x00, 0x10, 0x00,
  326. 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
  327. },
  328. .channel = PBAP_DEFAULT_CHANNEL,
  329. .svc_hint = SVC_HINT_OBEX,
  330. .sec_level = BT_IO_SEC_MEDIUM,
  331. .create_record = create_pbap_record
  332. }, {
  333. .uuid = {
  334. 0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x10, 0x00,
  335. 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
  336. },
  337. .channel = OPP_DEFAULT_CHANNEL,
  338. .svc_hint = SVC_HINT_OBEX,
  339. .sec_level = BT_IO_SEC_LOW,
  340. .create_record = create_opp_record
  341. }, {
  342. .uuid = {
  343. 0x00, 0x00, 0x11, 0x32, 0x00, 0x00, 0x10, 0x00,
  344. 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
  345. },
  346. .channel = MAP_MAS_DEFAULT_CHANNEL,
  347. .svc_hint = SVC_HINT_OBEX,
  348. .sec_level = BT_IO_SEC_MEDIUM,
  349. .create_record = create_mas_record
  350. }, {
  351. .uuid = {
  352. 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x10, 0x00,
  353. 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
  354. },
  355. .channel = 0,
  356. .svc_hint = 0,
  357. .sec_level = BT_IO_SEC_MEDIUM,
  358. .create_record = create_spp_record
  359. },
  360. };
  361. static uint32_t sdp_service_register(uint8_t channel, const uint8_t *uuid,
  362. const struct profile_info *profile,
  363. const void *svc_name)
  364. {
  365. sdp_record_t *record = NULL;
  366. uint8_t svc_hint = 0;
  367. if (profile && profile->create_record) {
  368. record = profile->create_record(channel, svc_name);
  369. svc_hint = profile->svc_hint;
  370. } else if (uuid) {
  371. record = create_app_record(channel, uuid, svc_name);
  372. }
  373. if (!record)
  374. return 0;
  375. if (bt_adapter_add_record(record, svc_hint) < 0) {
  376. error("Failed to register on SDP record");
  377. sdp_record_free(record);
  378. return 0;
  379. }
  380. return record->handle;
  381. }
  382. static int bt_sock_send_fd(int sock_fd, const void *buf, int len, int send_fd)
  383. {
  384. ssize_t ret;
  385. struct msghdr msg;
  386. struct cmsghdr *cmsg;
  387. struct iovec iv;
  388. char cmsgbuf[CMSG_SPACE(sizeof(int))];
  389. DBG("len %d sock_fd %d send_fd %d", len, sock_fd, send_fd);
  390. if (sock_fd == -1 || send_fd == -1)
  391. return -1;
  392. memset(&msg, 0, sizeof(msg));
  393. memset(cmsgbuf, 0, sizeof(cmsgbuf));
  394. msg.msg_control = cmsgbuf;
  395. msg.msg_controllen = sizeof(cmsgbuf);
  396. cmsg = CMSG_FIRSTHDR(&msg);
  397. cmsg->cmsg_level = SOL_SOCKET;
  398. cmsg->cmsg_type = SCM_RIGHTS;
  399. cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
  400. memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
  401. iv.iov_base = (unsigned char *) buf;
  402. iv.iov_len = len;
  403. msg.msg_iov = &iv;
  404. msg.msg_iovlen = 1;
  405. ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
  406. if (ret < 0) {
  407. error("sendmsg(): sock_fd %d send_fd %d: %s",
  408. sock_fd, send_fd, strerror(errno));
  409. return ret;
  410. }
  411. return ret;
  412. }
  413. static const struct profile_info *get_profile_by_uuid(const uint8_t *uuid)
  414. {
  415. unsigned int i;
  416. for (i = 0; i < G_N_ELEMENTS(profiles); i++) {
  417. if (!memcmp(profiles[i].uuid, uuid, 16))
  418. return &profiles[i];
  419. }
  420. return NULL;
  421. }
  422. static int try_write_all(int fd, unsigned char *buf, int len)
  423. {
  424. int sent = 0;
  425. while (len > 0) {
  426. int written;
  427. written = write(fd, buf, len);
  428. if (written < 0) {
  429. if (errno == EINTR || errno == EAGAIN)
  430. continue;
  431. return -1;
  432. }
  433. if (!written)
  434. return 0;
  435. len -= written; buf += written; sent += written;
  436. }
  437. return sent;
  438. }
  439. static gboolean jv_sock_client_event_cb(GIOChannel *io, GIOCondition cond,
  440. gpointer data)
  441. {
  442. struct rfcomm_sock *rfsock = data;
  443. int len, sent;
  444. if (cond & G_IO_HUP) {
  445. DBG("Socket %d hang up", g_io_channel_unix_get_fd(io));
  446. goto fail;
  447. }
  448. if (cond & (G_IO_ERR | G_IO_NVAL)) {
  449. error("Socket %d error", g_io_channel_unix_get_fd(io));
  450. goto fail;
  451. }
  452. len = read(rfsock->jv_sock, rfsock->buf, rfsock->buf_size);
  453. if (len <= 0) {
  454. error("read(): %s", strerror(errno));
  455. /* Read again */
  456. return TRUE;
  457. }
  458. sent = try_write_all(rfsock->bt_sock, rfsock->buf, len);
  459. if (sent < 0) {
  460. error("write(): %s", strerror(errno));
  461. goto fail;
  462. }
  463. return TRUE;
  464. fail:
  465. DBG("rfsock %p jv_sock %d cond %d", rfsock, rfsock->jv_sock, cond);
  466. connections = g_list_remove(connections, rfsock);
  467. cleanup_rfsock(rfsock);
  468. return FALSE;
  469. }
  470. static gboolean bt_sock_event_cb(GIOChannel *io, GIOCondition cond,
  471. gpointer data)
  472. {
  473. struct rfcomm_sock *rfsock = data;
  474. int len, sent;
  475. if (cond & G_IO_HUP) {
  476. DBG("Socket %d hang up", g_io_channel_unix_get_fd(io));
  477. goto fail;
  478. }
  479. if (cond & (G_IO_ERR | G_IO_NVAL)) {
  480. error("Socket %d error", g_io_channel_unix_get_fd(io));
  481. goto fail;
  482. }
  483. len = read(rfsock->bt_sock, rfsock->buf, rfsock->buf_size);
  484. if (len <= 0) {
  485. error("read(): %s", strerror(errno));
  486. /* Read again */
  487. return TRUE;
  488. }
  489. sent = try_write_all(rfsock->jv_sock, rfsock->buf, len);
  490. if (sent < 0) {
  491. error("write(): %s", strerror(errno));
  492. goto fail;
  493. }
  494. return TRUE;
  495. fail:
  496. DBG("rfsock %p bt_sock %d cond %d", rfsock, rfsock->bt_sock, cond);
  497. connections = g_list_remove(connections, rfsock);
  498. cleanup_rfsock(rfsock);
  499. return FALSE;
  500. }
  501. static bool sock_send_accept(struct rfcomm_sock *rfsock, bdaddr_t *bdaddr,
  502. int fd_accepted)
  503. {
  504. struct hal_sock_connect_signal cmd;
  505. int len;
  506. DBG("");
  507. cmd.size = sizeof(cmd);
  508. bdaddr2android(bdaddr, cmd.bdaddr);
  509. cmd.channel = rfsock->channel;
  510. cmd.status = 0;
  511. len = bt_sock_send_fd(rfsock->jv_sock, &cmd, sizeof(cmd), fd_accepted);
  512. if (len != sizeof(cmd)) {
  513. error("Error sending accept signal");
  514. return false;
  515. }
  516. return true;
  517. }
  518. static gboolean jv_sock_server_event_cb(GIOChannel *io, GIOCondition cond,
  519. gpointer data)
  520. {
  521. struct rfcomm_sock *rfsock = data;
  522. DBG("rfsock %p jv_sock %d cond %d", rfsock, rfsock->jv_sock, cond);
  523. if (cond & G_IO_NVAL)
  524. return FALSE;
  525. if (cond & (G_IO_ERR | G_IO_HUP)) {
  526. servers[rfsock->channel].rfsock = NULL;
  527. cleanup_rfsock(rfsock);
  528. }
  529. return FALSE;
  530. }
  531. static void accept_cb(GIOChannel *io, GError *err, gpointer user_data)
  532. {
  533. struct rfcomm_sock *rfsock = user_data;
  534. struct rfcomm_sock *new_rfsock;
  535. GIOChannel *jv_io;
  536. GError *gerr = NULL;
  537. bdaddr_t dst;
  538. char address[18];
  539. int new_sock;
  540. int hal_sock;
  541. guint id;
  542. GIOCondition cond;
  543. if (err) {
  544. error("%s", err->message);
  545. return;
  546. }
  547. bt_io_get(io, &gerr,
  548. BT_IO_OPT_DEST_BDADDR, &dst,
  549. BT_IO_OPT_INVALID);
  550. if (gerr) {
  551. error("%s", gerr->message);
  552. g_error_free(gerr);
  553. g_io_channel_shutdown(io, TRUE, NULL);
  554. return;
  555. }
  556. ba2str(&dst, address);
  557. DBG("Incoming connection from %s on channel %d (rfsock %p)", address,
  558. rfsock->channel, rfsock);
  559. new_sock = g_io_channel_unix_get_fd(io);
  560. new_rfsock = create_rfsock(new_sock, &hal_sock);
  561. if (!new_rfsock) {
  562. g_io_channel_shutdown(io, TRUE, NULL);
  563. return;
  564. }
  565. DBG("new rfsock %p bt_sock %d jv_sock %d hal_sock %d", new_rfsock,
  566. new_rfsock->bt_sock, new_rfsock->jv_sock, hal_sock);
  567. if (!sock_send_accept(rfsock, &dst, hal_sock)) {
  568. cleanup_rfsock(new_rfsock);
  569. return;
  570. }
  571. connections = g_list_append(connections, new_rfsock);
  572. /* Handle events from Android */
  573. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  574. jv_io = g_io_channel_unix_new(new_rfsock->jv_sock);
  575. id = g_io_add_watch(jv_io, cond, jv_sock_client_event_cb, new_rfsock);
  576. g_io_channel_unref(jv_io);
  577. new_rfsock->jv_watch = id;
  578. /* Handle rfcomm events */
  579. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  580. id = g_io_add_watch(io, cond, bt_sock_event_cb, new_rfsock);
  581. g_io_channel_set_close_on_unref(io, FALSE);
  582. new_rfsock->bt_watch = id;
  583. }
  584. static int find_free_channel(void)
  585. {
  586. int ch;
  587. /* channel 0 is reserver so we don't use it */
  588. for (ch = 1; ch <= RFCOMM_CHANNEL_MAX; ch++) {
  589. struct rfcomm_channel *srv = &servers[ch];
  590. if (!srv->reserved && srv->rfsock == NULL)
  591. return ch;
  592. }
  593. return 0;
  594. }
  595. static BtIOSecLevel get_sec_level(uint8_t flags)
  596. {
  597. /*
  598. * HAL_SOCK_FLAG_AUTH should require MITM but in our case setting
  599. * security to BT_IO_SEC_HIGH would also require 16-digits PIN code
  600. * for pre-2.1 devices which is not what Android expects. For this
  601. * reason we ignore this flag to not break apps which use "secure"
  602. * sockets (have both auth and encrypt flags set, there is no public
  603. * API in Android which should provide proper high security socket).
  604. */
  605. return flags & HAL_SOCK_FLAG_ENCRYPT ? BT_IO_SEC_MEDIUM :
  606. BT_IO_SEC_LOW;
  607. }
  608. static uint8_t rfcomm_listen(int chan, const uint8_t *name, const uint8_t *uuid,
  609. uint8_t flags, int *hal_sock)
  610. {
  611. const struct profile_info *profile;
  612. struct rfcomm_sock *rfsock = NULL;
  613. BtIOSecLevel sec_level;
  614. GIOChannel *io, *jv_io;
  615. GIOCondition cond;
  616. GError *err = NULL;
  617. guint id;
  618. uuid_t uu;
  619. char uuid_str[32];
  620. sdp_uuid128_create(&uu, uuid);
  621. sdp_uuid2strn(&uu, uuid_str, sizeof(uuid_str));
  622. DBG("chan %d flags 0x%02x uuid %s name %s", chan, flags, uuid_str,
  623. name);
  624. if ((!memcmp(uuid, zero_uuid, sizeof(zero_uuid)) && chan <= 0) ||
  625. (chan > RFCOMM_CHANNEL_MAX)) {
  626. error("Invalid rfcomm listen params");
  627. return HAL_STATUS_INVALID;
  628. }
  629. profile = get_profile_by_uuid(uuid);
  630. if (!profile) {
  631. sec_level = get_sec_level(flags);
  632. } else {
  633. if (!profile->create_record)
  634. return HAL_STATUS_INVALID;
  635. chan = profile->channel;
  636. sec_level = profile->sec_level;
  637. }
  638. if (chan <= 0)
  639. chan = find_free_channel();
  640. if (!chan) {
  641. error("No free channels");
  642. return HAL_STATUS_BUSY;
  643. }
  644. if (servers[chan].rfsock != NULL) {
  645. error("Channel already registered (%d)", chan);
  646. return HAL_STATUS_BUSY;
  647. }
  648. DBG("chan %d sec_level %d", chan, sec_level);
  649. rfsock = create_rfsock(-1, hal_sock);
  650. if (!rfsock)
  651. return HAL_STATUS_FAILED;
  652. rfsock->channel = chan;
  653. io = bt_io_listen(accept_cb, NULL, rfsock, NULL, &err,
  654. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  655. BT_IO_OPT_CHANNEL, chan,
  656. BT_IO_OPT_SEC_LEVEL, sec_level,
  657. BT_IO_OPT_INVALID);
  658. if (!io) {
  659. error("Failed listen: %s", err->message);
  660. g_error_free(err);
  661. goto failed;
  662. }
  663. rfsock->bt_sock = g_io_channel_unix_get_fd(io);
  664. g_io_channel_set_close_on_unref(io, FALSE);
  665. g_io_channel_unref(io);
  666. /* Handle events from Android */
  667. cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  668. jv_io = g_io_channel_unix_new(rfsock->jv_sock);
  669. id = g_io_add_watch_full(jv_io, G_PRIORITY_HIGH, cond,
  670. jv_sock_server_event_cb, rfsock,
  671. NULL);
  672. g_io_channel_unref(jv_io);
  673. rfsock->jv_watch = id;
  674. DBG("rfsock %p bt_sock %d jv_sock %d hal_sock %d", rfsock,
  675. rfsock->bt_sock,
  676. rfsock->jv_sock,
  677. *hal_sock);
  678. if (write(rfsock->jv_sock, &chan, sizeof(chan)) != sizeof(chan)) {
  679. error("Error sending RFCOMM channel");
  680. goto failed;
  681. }
  682. rfsock->service_handle = sdp_service_register(chan, uuid, profile,
  683. name);
  684. servers[chan].rfsock = rfsock;
  685. return HAL_STATUS_SUCCESS;
  686. failed:
  687. cleanup_rfsock(rfsock);
  688. close(*hal_sock);
  689. return HAL_STATUS_FAILED;
  690. }
  691. static uint32_t add_test_record(uuid_t *uuid)
  692. {
  693. sdp_record_t *record;
  694. sdp_list_t *svclass_id;
  695. sdp_list_t *seq, *pbg_seq, *proto_seq, *ap_seq;
  696. sdp_list_t *proto, *proto1, *aproto;
  697. uuid_t l2cap_uuid, pbg_uuid, ap_uuid;
  698. record = sdp_record_alloc();
  699. if (!record)
  700. return 0;
  701. record->handle = sdp_next_handle();
  702. svclass_id = sdp_list_append(NULL, uuid);
  703. sdp_set_service_classes(record, svclass_id);
  704. sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
  705. proto = sdp_list_append(NULL, &l2cap_uuid);
  706. seq = sdp_list_append(NULL, proto);
  707. proto_seq = sdp_list_append(NULL, seq);
  708. sdp_set_access_protos(record, proto_seq);
  709. sdp_uuid16_create(&pbg_uuid, PUBLIC_BROWSE_GROUP);
  710. pbg_seq = sdp_list_append(NULL, &pbg_uuid);
  711. sdp_set_browse_groups(record, pbg_seq);
  712. /* Additional Protocol Descriptor List */
  713. sdp_uuid16_create(&ap_uuid, L2CAP_UUID);
  714. proto1 = sdp_list_append(NULL, &ap_uuid);
  715. ap_seq = sdp_list_append(NULL, proto1);
  716. aproto = sdp_list_append(NULL, ap_seq);
  717. sdp_set_add_access_protos(record, aproto);
  718. sdp_set_service_id(record, *uuid);
  719. sdp_set_record_state(record, 0);
  720. sdp_set_service_ttl(record, 0);
  721. sdp_set_service_avail(record, 0);
  722. sdp_set_url_attr(record, "http://www.bluez.org",
  723. "http://www.bluez.org", "http://www.bluez.org");
  724. sdp_list_free(proto, NULL);
  725. sdp_list_free(seq, NULL);
  726. sdp_list_free(proto_seq, NULL);
  727. sdp_list_free(pbg_seq, NULL);
  728. sdp_list_free(svclass_id, NULL);
  729. if (bt_adapter_add_record(record, 0) < 0) {
  730. sdp_record_free(record);
  731. return 0;
  732. }
  733. return record->handle;
  734. }
  735. static void test_sdp_cleanup(void)
  736. {
  737. if (test_sdp_record_uuid16) {
  738. bt_adapter_remove_record(test_sdp_record_uuid16);
  739. test_sdp_record_uuid16 = 0;
  740. }
  741. if (test_sdp_record_uuid32) {
  742. bt_adapter_remove_record(test_sdp_record_uuid32);
  743. test_sdp_record_uuid32 = 0;
  744. }
  745. if (test_sdp_record_uuid128) {
  746. bt_adapter_remove_record(test_sdp_record_uuid128);
  747. test_sdp_record_uuid128 = 0;
  748. }
  749. }
  750. static void test_sdp_init(void)
  751. {
  752. char uuid128[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  753. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  754. uuid_t u;
  755. sdp_uuid16_create(&u, 0xffff);
  756. test_sdp_record_uuid16 = add_test_record(&u);
  757. sdp_uuid32_create(&u, 0xffffffff);
  758. test_sdp_record_uuid32 = add_test_record(&u);
  759. sdp_uuid128_create(&u, uuid128);
  760. test_sdp_record_uuid128 = add_test_record(&u);
  761. }
  762. static uint8_t l2cap_listen(int chan, const uint8_t *name, const uint8_t *uuid,
  763. uint8_t flags, int *hal_sock)
  764. {
  765. /* TODO be more strict here? */
  766. if (strcmp("BlueZ", (const char *) name)) {
  767. error("socket: Only SDP test supported on L2CAP");
  768. return HAL_STATUS_UNSUPPORTED;
  769. }
  770. test_sdp_cleanup();
  771. test_sdp_init();
  772. *hal_sock = -1;
  773. return HAL_STATUS_SUCCESS;
  774. }
  775. static void handle_listen(const void *buf, uint16_t len)
  776. {
  777. const struct hal_cmd_socket_listen *cmd = buf;
  778. uint8_t status;
  779. int hal_sock;
  780. switch (cmd->type) {
  781. case HAL_SOCK_RFCOMM:
  782. status = rfcomm_listen(cmd->channel, cmd->name, cmd->uuid,
  783. cmd->flags, &hal_sock);
  784. break;
  785. case HAL_SOCK_L2CAP:
  786. status = l2cap_listen(cmd->channel, cmd->name, cmd->uuid,
  787. cmd->flags, &hal_sock);
  788. break;
  789. case HAL_SOCK_SCO:
  790. status = HAL_STATUS_UNSUPPORTED;
  791. break;
  792. default:
  793. status = HAL_STATUS_INVALID;
  794. break;
  795. }
  796. if (status != HAL_STATUS_SUCCESS)
  797. goto failed;
  798. ipc_send_rsp_full(hal_ipc, HAL_SERVICE_ID_SOCKET, HAL_OP_SOCKET_LISTEN,
  799. 0, NULL, hal_sock);
  800. close(hal_sock);
  801. return;
  802. failed:
  803. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_SOCKET, HAL_OP_SOCKET_LISTEN,
  804. status);
  805. }
  806. static bool sock_send_connect(struct rfcomm_sock *rfsock, bdaddr_t *bdaddr)
  807. {
  808. struct hal_sock_connect_signal cmd;
  809. int len;
  810. DBG("");
  811. memset(&cmd, 0, sizeof(cmd));
  812. cmd.size = sizeof(cmd);
  813. bdaddr2android(bdaddr, cmd.bdaddr);
  814. cmd.channel = rfsock->channel;
  815. cmd.status = 0;
  816. len = write(rfsock->jv_sock, &cmd, sizeof(cmd));
  817. if (len < 0) {
  818. error("%s", strerror(errno));
  819. return false;
  820. }
  821. if (len != sizeof(cmd)) {
  822. error("Error sending connect signal");
  823. return false;
  824. }
  825. return true;
  826. }
  827. static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
  828. {
  829. struct rfcomm_sock *rfsock = user_data;
  830. bdaddr_t *dst = &rfsock->dst;
  831. GIOChannel *jv_io;
  832. char address[18];
  833. guint id;
  834. GIOCondition cond;
  835. if (err) {
  836. error("%s", err->message);
  837. goto fail;
  838. }
  839. ba2str(dst, address);
  840. DBG("Connected to %s on channel %d (rfsock %p)", address,
  841. rfsock->channel, rfsock);
  842. if (!sock_send_connect(rfsock, dst))
  843. goto fail;
  844. /* Handle events from Android */
  845. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  846. jv_io = g_io_channel_unix_new(rfsock->jv_sock);
  847. id = g_io_add_watch(jv_io, cond, jv_sock_client_event_cb, rfsock);
  848. g_io_channel_unref(jv_io);
  849. rfsock->jv_watch = id;
  850. /* Handle rfcomm events */
  851. cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
  852. id = g_io_add_watch(io, cond, bt_sock_event_cb, rfsock);
  853. g_io_channel_set_close_on_unref(io, FALSE);
  854. rfsock->bt_watch = id;
  855. return;
  856. fail:
  857. connections = g_list_remove(connections, rfsock);
  858. cleanup_rfsock(rfsock);
  859. }
  860. static bool do_rfcomm_connect(struct rfcomm_sock *rfsock, int chan)
  861. {
  862. GIOChannel *io;
  863. GError *gerr = NULL;
  864. DBG("rfsock %p sec_level %d chan %d", rfsock, rfsock->sec_level, chan);
  865. io = bt_io_connect(connect_cb, rfsock, NULL, &gerr,
  866. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  867. BT_IO_OPT_DEST_BDADDR, &rfsock->dst,
  868. BT_IO_OPT_CHANNEL, chan,
  869. BT_IO_OPT_SEC_LEVEL, rfsock->sec_level,
  870. BT_IO_OPT_INVALID);
  871. if (!io) {
  872. error("Failed connect: %s", gerr->message);
  873. g_error_free(gerr);
  874. return false;
  875. }
  876. g_io_channel_set_close_on_unref(io, FALSE);
  877. g_io_channel_unref(io);
  878. if (write(rfsock->jv_sock, &chan, sizeof(chan)) != sizeof(chan)) {
  879. error("Error sending RFCOMM channel");
  880. return false;
  881. }
  882. rfsock->bt_sock = g_io_channel_unix_get_fd(io);
  883. rfsock_set_buffer(rfsock);
  884. rfsock->channel = chan;
  885. connections = g_list_append(connections, rfsock);
  886. return true;
  887. }
  888. static void sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
  889. {
  890. struct rfcomm_sock *rfsock = data;
  891. sdp_list_t *list;
  892. int chan;
  893. DBG("");
  894. if (err < 0) {
  895. error("Unable to get SDP record: %s", strerror(-err));
  896. goto fail;
  897. }
  898. if (!recs || !recs->data) {
  899. error("No SDP records found");
  900. goto fail;
  901. }
  902. for (list = recs; list != NULL; list = list->next) {
  903. sdp_record_t *rec = list->data;
  904. sdp_list_t *protos;
  905. if (sdp_get_access_protos(rec, &protos) < 0) {
  906. error("Unable to get proto list");
  907. goto fail;
  908. }
  909. chan = sdp_get_proto_port(protos, RFCOMM_UUID);
  910. sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free,
  911. NULL);
  912. sdp_list_free(protos, NULL);
  913. if (chan)
  914. break;
  915. }
  916. if (chan <= 0) {
  917. error("Could not get RFCOMM channel %d", chan);
  918. goto fail;
  919. }
  920. DBG("Got RFCOMM channel %d", chan);
  921. if (do_rfcomm_connect(rfsock, chan))
  922. return;
  923. fail:
  924. cleanup_rfsock(rfsock);
  925. }
  926. static uint8_t connect_rfcomm(const bdaddr_t *addr, int chan,
  927. const uint8_t *uuid, uint8_t flags,
  928. int *hal_sock)
  929. {
  930. struct rfcomm_sock *rfsock;
  931. char address[18];
  932. uuid_t uu;
  933. char uuid_str[32];
  934. sdp_uuid128_create(&uu, uuid);
  935. sdp_uuid2strn(&uu, uuid_str, sizeof(uuid_str));
  936. ba2str(addr, address);
  937. DBG("addr %s chan %d flags 0x%02x uuid %s", address, chan, flags,
  938. uuid_str);
  939. if ((!memcmp(uuid, zero_uuid, sizeof(zero_uuid)) && chan <= 0) ||
  940. !bacmp(addr, BDADDR_ANY)) {
  941. error("Invalid rfcomm connect params");
  942. return HAL_STATUS_INVALID;
  943. }
  944. rfsock = create_rfsock(-1, hal_sock);
  945. if (!rfsock)
  946. return HAL_STATUS_FAILED;
  947. DBG("rfsock %p jv_sock %d hal_sock %d", rfsock, rfsock->jv_sock,
  948. *hal_sock);
  949. rfsock->sec_level = get_sec_level(flags);
  950. bacpy(&rfsock->dst, addr);
  951. if (!memcmp(uuid, zero_uuid, sizeof(zero_uuid))) {
  952. if (!do_rfcomm_connect(rfsock, chan))
  953. goto failed;
  954. } else {
  955. if (bt_search_service(&adapter_addr, &rfsock->dst, &uu,
  956. sdp_search_cb, rfsock, NULL, 0) < 0) {
  957. error("Failed to search SDP records");
  958. goto failed;
  959. }
  960. }
  961. return HAL_STATUS_SUCCESS;
  962. failed:
  963. cleanup_rfsock(rfsock);
  964. close(*hal_sock);
  965. return HAL_STATUS_FAILED;
  966. }
  967. static void handle_connect(const void *buf, uint16_t len)
  968. {
  969. const struct hal_cmd_socket_connect *cmd = buf;
  970. bdaddr_t bdaddr;
  971. uint8_t status;
  972. int hal_sock;
  973. DBG("");
  974. android2bdaddr(cmd->bdaddr, &bdaddr);
  975. switch (cmd->type) {
  976. case HAL_SOCK_RFCOMM:
  977. status = connect_rfcomm(&bdaddr, cmd->channel, cmd->uuid,
  978. cmd->flags, &hal_sock);
  979. break;
  980. case HAL_SOCK_SCO:
  981. case HAL_SOCK_L2CAP:
  982. status = HAL_STATUS_UNSUPPORTED;
  983. break;
  984. default:
  985. status = HAL_STATUS_INVALID;
  986. break;
  987. }
  988. if (status != HAL_STATUS_SUCCESS)
  989. goto failed;
  990. ipc_send_rsp_full(hal_ipc, HAL_SERVICE_ID_SOCKET, HAL_OP_SOCKET_CONNECT,
  991. 0, NULL, hal_sock);
  992. close(hal_sock);
  993. return;
  994. failed:
  995. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_SOCKET, HAL_OP_SOCKET_CONNECT,
  996. status);
  997. }
  998. static const struct ipc_handler cmd_handlers[] = {
  999. /* HAL_OP_SOCKET_LISTEN */
  1000. { handle_listen, false, sizeof(struct hal_cmd_socket_listen) },
  1001. /* HAL_OP_SOCKET_CONNECT */
  1002. { handle_connect, false, sizeof(struct hal_cmd_socket_connect) },
  1003. };
  1004. void bt_socket_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
  1005. {
  1006. size_t i;
  1007. DBG("");
  1008. hal_mode = mode;
  1009. /*
  1010. * make sure channels assigned for profiles are reserved and not used
  1011. * for app services
  1012. */
  1013. for (i = 0; i < G_N_ELEMENTS(profiles); i++)
  1014. if (profiles[i].channel)
  1015. servers[profiles[i].channel].reserved = true;
  1016. bacpy(&adapter_addr, addr);
  1017. hal_ipc = ipc;
  1018. ipc_register(hal_ipc, HAL_SERVICE_ID_SOCKET, cmd_handlers,
  1019. G_N_ELEMENTS(cmd_handlers));
  1020. }
  1021. void bt_socket_unregister(void)
  1022. {
  1023. int ch;
  1024. DBG("");
  1025. test_sdp_cleanup();
  1026. g_list_free_full(connections, cleanup_rfsock);
  1027. for (ch = 0; ch <= RFCOMM_CHANNEL_MAX; ch++)
  1028. if (servers[ch].rfsock)
  1029. cleanup_rfsock(servers[ch].rfsock);
  1030. memset(servers, 0, sizeof(servers));
  1031. ipc_unregister(hal_ipc, HAL_SERVICE_ID_SOCKET);
  1032. hal_ipc = NULL;
  1033. }