l2cap_client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 Google, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #if (defined(L2CAP_CLIENT_INCLUDED) && L2CAP_CLIENT_INCLUDED == TRUE)
  19. #include <string.h>
  20. #include "common/bt_trace.h"
  21. #include "common/bt_defs.h"
  22. #include "device/bdaddr.h"
  23. #include "osi/allocator.h"
  24. #include "osi/buffer.h"
  25. #include "osi/list.h"
  26. #include "osi/osi.h"
  27. #include "stack/l2cap_client.h"
  28. #include "stack/l2c_api.h"
  29. struct l2cap_client_t {
  30. l2cap_client_callbacks_t callbacks;
  31. void *context;
  32. uint16_t local_channel_id;
  33. uint16_t remote_mtu;
  34. bool configured_self;
  35. bool configured_peer;
  36. bool is_congested;
  37. list_t *outbound_fragments;
  38. };
  39. static void connect_completed_cb(uint16_t local_channel_id, uint16_t error_code);
  40. static void config_request_cb(uint16_t local_channel_id, tL2CAP_CFG_INFO *requested_parameters);
  41. static void config_completed_cb(uint16_t local_channel_id, tL2CAP_CFG_INFO *negotiated_parameters);
  42. static void disconnect_request_cb(uint16_t local_channel_id, bool ack_required);
  43. static void disconnect_completed_cb(uint16_t local_channel_id, uint16_t error_code);
  44. static void congestion_cb(uint16_t local_channel_id, bool is_congested);
  45. static void read_ready_cb(uint16_t local_channel_id, BT_HDR *packet);
  46. static void write_completed_cb(uint16_t local_channel_id, uint16_t packets_completed);
  47. static void fragment_packet(l2cap_client_t *client, buffer_t *packet);
  48. static void dispatch_fragments(l2cap_client_t *client);
  49. static l2cap_client_t *find(uint16_t local_channel_id);
  50. // From the Bluetooth Core specification.
  51. static const uint16_t L2CAP_MTU_DEFAULT = 672;
  52. static const uint16_t L2CAP_MTU_MINIMUM = 48;
  53. static const tL2CAP_APPL_INFO l2cap_callbacks = {
  54. .pL2CA_ConnectCfm_Cb = connect_completed_cb,
  55. .pL2CA_ConfigInd_Cb = config_request_cb,
  56. .pL2CA_ConfigCfm_Cb = config_completed_cb,
  57. .pL2CA_DisconnectInd_Cb = disconnect_request_cb,
  58. .pL2CA_DisconnectCfm_Cb = disconnect_completed_cb,
  59. .pL2CA_CongestionStatus_Cb = congestion_cb,
  60. .pL2CA_DataInd_Cb = read_ready_cb,
  61. .pL2CA_TxComplete_Cb = write_completed_cb,
  62. };
  63. static list_t *l2cap_clients; // A list of l2cap_client_t. Container does not own objects.
  64. buffer_t *l2cap_buffer_new(size_t size)
  65. {
  66. buffer_t *buf = buffer_new(size + L2CAP_MIN_OFFSET);
  67. buffer_t *slice = NULL;
  68. if (buf) {
  69. slice = buffer_new_slice(buf, size);
  70. }
  71. buffer_free(buf);
  72. return slice;
  73. }
  74. l2cap_client_t *l2cap_client_new(const l2cap_client_callbacks_t *callbacks, void *context)
  75. {
  76. assert(callbacks != NULL);
  77. assert(callbacks->connected != NULL);
  78. assert(callbacks->disconnected != NULL);
  79. assert(callbacks->read_ready != NULL);
  80. assert(callbacks->write_ready != NULL);
  81. if (!l2cap_clients) {
  82. l2cap_clients = list_new(NULL);
  83. if (!l2cap_clients) {
  84. L2CAP_TRACE_ERROR("%s unable to allocate space for L2CAP client list.", __func__);
  85. return NULL;
  86. }
  87. }
  88. l2cap_client_t *ret = (l2cap_client_t *)osi_calloc(sizeof(l2cap_client_t));
  89. if (!ret) {
  90. L2CAP_TRACE_ERROR("%s unable to allocate L2CAP client.", __func__);
  91. goto error;
  92. }
  93. ret->callbacks = *callbacks;
  94. ret->context = context;
  95. ret->remote_mtu = L2CAP_MTU_DEFAULT;
  96. ret->outbound_fragments = list_new(NULL);
  97. if (!ret) {
  98. L2CAP_TRACE_ERROR("%s unable to allocate outbound L2CAP fragment list.", __func__);
  99. goto error;
  100. }
  101. list_append(l2cap_clients, ret);
  102. return ret;
  103. error:;
  104. osi_free(ret);
  105. return NULL;
  106. }
  107. void l2cap_client_free(l2cap_client_t *client)
  108. {
  109. if (!client) {
  110. return;
  111. }
  112. list_remove(l2cap_clients, client);
  113. l2cap_client_disconnect(client);
  114. list_free(client->outbound_fragments);
  115. osi_free(client);
  116. }
  117. bool l2cap_client_connect(l2cap_client_t *client, const bt_bdaddr_t *remote_bdaddr, uint16_t psm)
  118. {
  119. assert(client != NULL);
  120. assert(remote_bdaddr != NULL);
  121. assert(psm != 0);
  122. assert(!bdaddr_is_empty(remote_bdaddr));
  123. assert(client->local_channel_id == 0);
  124. assert(!client->configured_self);
  125. assert(!client->configured_peer);
  126. assert(!L2C_INVALID_PSM(psm));
  127. client->local_channel_id = L2CA_ConnectReq(psm, (uint8_t *)remote_bdaddr);
  128. if (!client->local_channel_id) {
  129. L2CAP_TRACE_ERROR("%s unable to create L2CAP connection.", __func__);
  130. return false;
  131. }
  132. L2CA_SetConnectionCallbacks(client->local_channel_id, &l2cap_callbacks);
  133. return true;
  134. }
  135. void l2cap_client_disconnect(l2cap_client_t *client)
  136. {
  137. assert(client != NULL);
  138. if (client->local_channel_id && !L2CA_DisconnectReq(client->local_channel_id)) {
  139. L2CAP_TRACE_ERROR("%s unable to send disconnect message for LCID 0x%04x.", __func__, client->local_channel_id);
  140. }
  141. client->local_channel_id = 0;
  142. client->remote_mtu = L2CAP_MTU_DEFAULT;
  143. client->configured_self = false;
  144. client->configured_peer = false;
  145. client->is_congested = false;
  146. for (const list_node_t *node = list_begin(client->outbound_fragments); node != list_end(client->outbound_fragments); node = list_next(node)) {
  147. osi_free(list_node(node));
  148. }
  149. list_clear(client->outbound_fragments);
  150. }
  151. bool l2cap_client_is_connected(const l2cap_client_t *client)
  152. {
  153. assert(client != NULL);
  154. return client->local_channel_id != 0 && client->configured_self && client->configured_peer;
  155. }
  156. bool l2cap_client_write(l2cap_client_t *client, buffer_t *packet)
  157. {
  158. assert(client != NULL);
  159. assert(packet != NULL);
  160. assert(l2cap_client_is_connected(client));
  161. if (client->is_congested) {
  162. return false;
  163. }
  164. fragment_packet(client, packet);
  165. dispatch_fragments(client);
  166. return true;
  167. }
  168. static void connect_completed_cb(uint16_t local_channel_id, uint16_t error_code)
  169. {
  170. assert(local_channel_id != 0);
  171. l2cap_client_t *client = find(local_channel_id);
  172. if (!client) {
  173. L2CAP_TRACE_ERROR("%s unable to find L2CAP client for LCID 0x%04x.", __func__, local_channel_id);
  174. return;
  175. }
  176. if (error_code != L2CAP_CONN_OK) {
  177. L2CAP_TRACE_ERROR("%s error connecting L2CAP channel: %d.", __func__, error_code);
  178. client->callbacks.disconnected(client, client->context);
  179. return;
  180. }
  181. // Use default L2CAP parameters.
  182. tL2CAP_CFG_INFO desired_parameters = { 0 };
  183. if (!L2CA_ConfigReq(local_channel_id, &desired_parameters)) {
  184. L2CAP_TRACE_ERROR("%s error sending L2CAP config parameters.", __func__);
  185. client->callbacks.disconnected(client, client->context);
  186. }
  187. }
  188. static void config_request_cb(uint16_t local_channel_id, tL2CAP_CFG_INFO *requested_parameters)
  189. {
  190. tL2CAP_CFG_INFO response = { 0 };
  191. l2cap_client_t *client = find(local_channel_id);
  192. if (!client) {
  193. L2CAP_TRACE_ERROR("%s unable to find L2CAP client matching LCID 0x%04x.", __func__, local_channel_id);
  194. return;
  195. }
  196. response.result = L2CAP_CFG_OK;
  197. if (requested_parameters->mtu_present) {
  198. // Make sure the peer chose an MTU at least as large as the minimum L2CAP MTU defined
  199. // by the Bluetooth Core spec.
  200. if (requested_parameters->mtu < L2CAP_MTU_MINIMUM) {
  201. response.mtu = L2CAP_MTU_MINIMUM;
  202. response.mtu_present = true;
  203. response.result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
  204. } else {
  205. client->remote_mtu = requested_parameters->mtu;
  206. }
  207. }
  208. if (requested_parameters->fcr_present) {
  209. if (requested_parameters->fcr.mode != L2CAP_FCR_BASIC_MODE) {
  210. response.fcr_present = true;
  211. response.fcr = requested_parameters->fcr;
  212. response.fcr.mode = L2CAP_FCR_BASIC_MODE;
  213. response.result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
  214. }
  215. }
  216. if (!L2CA_ConfigRsp(local_channel_id, &response)) {
  217. L2CAP_TRACE_ERROR("%s unable to send config response for LCID 0x%04x.", __func__, local_channel_id);
  218. l2cap_client_disconnect(client);
  219. return;
  220. }
  221. // If we've configured both endpoints, let the listener know we've connected.
  222. client->configured_peer = true;
  223. if (l2cap_client_is_connected(client)) {
  224. client->callbacks.connected(client, client->context);
  225. }
  226. }
  227. static void config_completed_cb(uint16_t local_channel_id, tL2CAP_CFG_INFO *negotiated_parameters)
  228. {
  229. l2cap_client_t *client = find(local_channel_id);
  230. if (!client) {
  231. L2CAP_TRACE_ERROR("%s unable to find L2CAP client matching LCID 0x%04x.", __func__, local_channel_id);
  232. return;
  233. }
  234. switch (negotiated_parameters->result) {
  235. // We'll get another configuration response later.
  236. case L2CAP_CFG_PENDING:
  237. break;
  238. case L2CAP_CFG_UNACCEPTABLE_PARAMS:
  239. // TODO: see if we can renegotiate parameters instead of dropping the connection.
  240. L2CAP_TRACE_WARNING("%s dropping L2CAP connection due to unacceptable config parameters.\n", __func__);
  241. l2cap_client_disconnect(client);
  242. break;
  243. case L2CAP_CFG_OK:
  244. // If we've configured both endpoints, let the listener know we've connected.
  245. client->configured_self = true;
  246. if (l2cap_client_is_connected(client)) {
  247. client->callbacks.connected(client, client->context);
  248. }
  249. break;
  250. // Failure, no further parameter negotiation possible.
  251. default:
  252. L2CAP_TRACE_WARNING("%s L2CAP parameter negotiation failed with error code %d.\n", __func__, negotiated_parameters->result);
  253. l2cap_client_disconnect(client);
  254. break;
  255. }
  256. }
  257. static void disconnect_request_cb(uint16_t local_channel_id, bool ack_required)
  258. {
  259. l2cap_client_t *client = find(local_channel_id);
  260. if (!client) {
  261. L2CAP_TRACE_ERROR("%s unable to find L2CAP client with LCID 0x%04x.\n", __func__, local_channel_id);
  262. return;
  263. }
  264. if (ack_required) {
  265. L2CA_DisconnectRsp(local_channel_id);
  266. }
  267. // We already sent a disconnect response so this LCID is now invalid.
  268. client->local_channel_id = 0;
  269. l2cap_client_disconnect(client);
  270. client->callbacks.disconnected(client, client->context);
  271. }
  272. static void disconnect_completed_cb(uint16_t local_channel_id, UNUSED_ATTR uint16_t error_code)
  273. {
  274. assert(local_channel_id != 0);
  275. l2cap_client_t *client = find(local_channel_id);
  276. if (!client) {
  277. L2CAP_TRACE_ERROR("%s unable to find L2CAP client with LCID 0x%04x.\n", __func__, local_channel_id);
  278. return;
  279. }
  280. client->local_channel_id = 0;
  281. l2cap_client_disconnect(client);
  282. client->callbacks.disconnected(client, client->context);
  283. }
  284. static void congestion_cb(uint16_t local_channel_id, bool is_congested)
  285. {
  286. assert(local_channel_id != 0);
  287. l2cap_client_t *client = find(local_channel_id);
  288. if (!client) {
  289. L2CAP_TRACE_ERROR("%s unable to find L2CAP client matching LCID 0x%04x.\n", __func__, local_channel_id);
  290. return;
  291. }
  292. client->is_congested = is_congested;
  293. if (!is_congested) {
  294. // If we just decongested, dispatch whatever we have left over in our queue.
  295. // Once that's done, if we're still decongested, notify the listener so it
  296. // can start writing again.
  297. dispatch_fragments(client);
  298. if (!client->is_congested) {
  299. client->callbacks.write_ready(client, client->context);
  300. }
  301. }
  302. }
  303. static void read_ready_cb(uint16_t local_channel_id, BT_HDR *packet)
  304. {
  305. assert(local_channel_id != 0);
  306. l2cap_client_t *client = find(local_channel_id);
  307. if (!client) {
  308. L2CAP_TRACE_ERROR("%s unable to find L2CAP client matching LCID 0x%04x.\n", __func__, local_channel_id);
  309. return;
  310. }
  311. // TODO(sharvil): eliminate copy from BT_HDR.
  312. buffer_t *buffer = buffer_new(packet->len);
  313. memcpy(buffer_ptr(buffer), packet->data + packet->offset, packet->len);
  314. osi_free(packet);
  315. client->callbacks.read_ready(client, buffer, client->context);
  316. buffer_free(buffer);
  317. }
  318. static void write_completed_cb(UNUSED_ATTR uint16_t local_channel_id, UNUSED_ATTR uint16_t packets_completed)
  319. {
  320. // Do nothing. We update congestion state based on the congestion callback
  321. // and we've already removed items from outbound_fragments list so we don't
  322. // really care how many packets were successfully dispatched.
  323. }
  324. static void fragment_packet(l2cap_client_t *client, buffer_t *packet)
  325. {
  326. assert(client != NULL);
  327. assert(packet != NULL);
  328. // TODO(sharvil): eliminate copy into BT_HDR.
  329. BT_HDR *bt_packet = osi_malloc(buffer_length(packet) + L2CAP_MIN_OFFSET);
  330. bt_packet->offset = L2CAP_MIN_OFFSET;
  331. bt_packet->len = buffer_length(packet);
  332. memcpy(bt_packet->data + bt_packet->offset, buffer_ptr(packet), buffer_length(packet));
  333. for (;;) {
  334. if (bt_packet->len <= client->remote_mtu) {
  335. if (bt_packet->len > 0) {
  336. list_append(client->outbound_fragments, bt_packet);
  337. } else {
  338. osi_free(bt_packet);
  339. }
  340. break;
  341. }
  342. BT_HDR *fragment = osi_malloc(client->remote_mtu + L2CAP_MIN_OFFSET);
  343. fragment->offset = L2CAP_MIN_OFFSET;
  344. fragment->len = client->remote_mtu;
  345. memcpy(fragment->data + fragment->offset, bt_packet->data + bt_packet->offset, client->remote_mtu);
  346. list_append(client->outbound_fragments, fragment);
  347. bt_packet->offset += client->remote_mtu;
  348. bt_packet->len -= client->remote_mtu;
  349. }
  350. }
  351. static void dispatch_fragments(l2cap_client_t *client)
  352. {
  353. assert(client != NULL);
  354. assert(!client->is_congested);
  355. while (!list_is_empty(client->outbound_fragments)) {
  356. BT_HDR *packet = (BT_HDR *)list_front(client->outbound_fragments);
  357. list_remove(client->outbound_fragments, packet);
  358. switch (L2CA_DataWrite(client->local_channel_id, packet)) {
  359. case L2CAP_DW_CONGESTED:
  360. client->is_congested = true;
  361. return;
  362. case L2CAP_DW_FAILED:
  363. L2CAP_TRACE_ERROR("%s error writing data to L2CAP connection LCID 0x%04x; disconnecting.", __func__, client->local_channel_id);
  364. l2cap_client_disconnect(client);
  365. return;
  366. case L2CAP_DW_SUCCESS:
  367. break;
  368. }
  369. }
  370. }
  371. static l2cap_client_t *find(uint16_t local_channel_id)
  372. {
  373. assert(local_channel_id != 0);
  374. for (const list_node_t *node = list_begin(l2cap_clients); node != list_end(l2cap_clients); node = list_next(node)) {
  375. l2cap_client_t *client = (l2cap_client_t *)list_node(node);
  376. if (client->local_channel_id == local_channel_id) {
  377. return client;
  378. }
  379. }
  380. return NULL;
  381. }
  382. #endif /*L2CAP_CLIENT_INCLUDED*/