mcap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
  7. * Copyright (C) 2010 Signove
  8. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  9. *
  10. */
  11. #define MCAP_VERSION 0x0100 /* current version 01.00 */
  12. /* bytes to get MCAP Supported Procedures */
  13. #define MCAP_SUP_PROC 0x06
  14. /* maximum transmission unit for channels */
  15. #define MCAP_CC_MTU 48
  16. #define MCAP_DC_MTU 65535
  17. /* MCAP Standard Op Codes */
  18. #define MCAP_ERROR_RSP 0x00
  19. #define MCAP_MD_CREATE_MDL_REQ 0x01
  20. #define MCAP_MD_CREATE_MDL_RSP 0x02
  21. #define MCAP_MD_RECONNECT_MDL_REQ 0x03
  22. #define MCAP_MD_RECONNECT_MDL_RSP 0x04
  23. #define MCAP_MD_ABORT_MDL_REQ 0x05
  24. #define MCAP_MD_ABORT_MDL_RSP 0x06
  25. #define MCAP_MD_DELETE_MDL_REQ 0x07
  26. #define MCAP_MD_DELETE_MDL_RSP 0x08
  27. /* MCAP Clock Sync Op Codes */
  28. #define MCAP_MD_SYNC_CAP_REQ 0x11
  29. #define MCAP_MD_SYNC_CAP_RSP 0x12
  30. #define MCAP_MD_SYNC_SET_REQ 0x13
  31. #define MCAP_MD_SYNC_SET_RSP 0x14
  32. #define MCAP_MD_SYNC_INFO_IND 0x15
  33. /* MCAP Response codes */
  34. #define MCAP_SUCCESS 0x00
  35. #define MCAP_INVALID_OP_CODE 0x01
  36. #define MCAP_INVALID_PARAM_VALUE 0x02
  37. #define MCAP_INVALID_MDEP 0x03
  38. #define MCAP_MDEP_BUSY 0x04
  39. #define MCAP_INVALID_MDL 0x05
  40. #define MCAP_MDL_BUSY 0x06
  41. #define MCAP_INVALID_OPERATION 0x07
  42. #define MCAP_RESOURCE_UNAVAILABLE 0x08
  43. #define MCAP_UNSPECIFIED_ERROR 0x09
  44. #define MCAP_REQUEST_NOT_SUPPORTED 0x0A
  45. #define MCAP_CONFIGURATION_REJECTED 0x0B
  46. /* MDL IDs */
  47. #define MCAP_MDLID_RESERVED 0x0000
  48. #define MCAP_MDLID_INITIAL 0x0001
  49. #define MCAP_MDLID_FINAL 0xFEFF
  50. #define MCAP_ALL_MDLIDS 0xFFFF
  51. /* MDEP IDs */
  52. #define MCAP_MDEPID_INITIAL 0x00
  53. #define MCAP_MDEPID_FINAL 0x7F
  54. /* CSP special values */
  55. #define MCAP_BTCLOCK_IMMEDIATE 0xffffffffUL
  56. #define MCAP_TMSTAMP_DONTSET 0xffffffffffffffffULL
  57. #define MCAP_BTCLOCK_MAX 0x0fffffff
  58. #define MCAP_BTCLOCK_FIELD (MCAP_BTCLOCK_MAX + 1)
  59. #define MCAP_CTRL_CACHED 0x01 /* MCL is cached */
  60. #define MCAP_CTRL_STD_OP 0x02 /* Support for standard op codes */
  61. #define MCAP_CTRL_SYNC_OP 0x04 /* Support for synchronization commands */
  62. #define MCAP_CTRL_CONN 0x08 /* MCL is in connecting process */
  63. #define MCAP_CTRL_FREE 0x10 /* MCL is marked as releasable */
  64. #define MCAP_CTRL_NOCACHE 0x20 /* MCL is marked as not cacheable */
  65. /*
  66. * MCAP Request Packet Format
  67. */
  68. typedef struct {
  69. uint8_t op;
  70. uint16_t mdl;
  71. uint8_t mdep;
  72. uint8_t conf;
  73. } __attribute__ ((packed)) mcap_md_create_mdl_req;
  74. typedef struct {
  75. uint8_t op;
  76. uint16_t mdl;
  77. } __attribute__ ((packed)) mcap_md_req;
  78. /* MCAP Response Packet Format */
  79. typedef struct {
  80. uint8_t op;
  81. uint8_t rc;
  82. uint16_t mdl;
  83. uint8_t data[0];
  84. } __attribute__ ((packed)) mcap_rsp;
  85. /* MCAP Clock Synchronization Protocol */
  86. typedef struct {
  87. uint8_t op;
  88. uint16_t timest;
  89. } __attribute__ ((packed)) mcap_md_sync_cap_req;
  90. typedef struct {
  91. uint8_t op;
  92. uint8_t rc;
  93. } __attribute__ ((packed)) mcap_md_sync_rsp;
  94. typedef struct {
  95. uint8_t op;
  96. uint8_t rc;
  97. uint8_t btclock;
  98. uint16_t sltime;
  99. uint16_t timestnr;
  100. uint16_t timestna;
  101. } __attribute__ ((packed)) mcap_md_sync_cap_rsp;
  102. typedef struct {
  103. uint8_t op;
  104. uint8_t timestui;
  105. uint32_t btclock;
  106. uint64_t timestst;
  107. } __attribute__ ((packed)) mcap_md_sync_set_req;
  108. typedef struct {
  109. int8_t op;
  110. uint8_t rc;
  111. uint32_t btclock;
  112. uint64_t timestst;
  113. uint16_t timestsa;
  114. } __attribute__ ((packed)) mcap_md_sync_set_rsp;
  115. typedef struct {
  116. uint8_t op;
  117. uint32_t btclock;
  118. uint64_t timestst;
  119. uint16_t timestsa;
  120. } __attribute__ ((packed)) mcap_md_sync_info_ind;
  121. typedef enum {
  122. /* MCAP Error Response Codes */
  123. MCAP_ERROR_INVALID_OP_CODE = 1,
  124. MCAP_ERROR_INVALID_PARAM_VALUE,
  125. MCAP_ERROR_INVALID_MDEP,
  126. MCAP_ERROR_MDEP_BUSY,
  127. MCAP_ERROR_INVALID_MDL,
  128. MCAP_ERROR_MDL_BUSY,
  129. MCAP_ERROR_INVALID_OPERATION,
  130. MCAP_ERROR_RESOURCE_UNAVAILABLE,
  131. MCAP_ERROR_UNSPECIFIED_ERROR,
  132. MCAP_ERROR_REQUEST_NOT_SUPPORTED,
  133. MCAP_ERROR_CONFIGURATION_REJECTED,
  134. /* MCAP Internal Errors */
  135. MCAP_ERROR_INVALID_ARGS,
  136. MCAP_ERROR_ALREADY_EXISTS,
  137. MCAP_ERROR_REQ_IGNORED,
  138. MCAP_ERROR_MCL_CLOSED,
  139. MCAP_ERROR_FAILED
  140. } McapError;
  141. typedef enum {
  142. MCAP_MDL_CB_INVALID,
  143. MCAP_MDL_CB_CONNECTED, /* mcap_mdl_event_cb */
  144. MCAP_MDL_CB_CLOSED, /* mcap_mdl_event_cb */
  145. MCAP_MDL_CB_DELETED, /* mcap_mdl_event_cb */
  146. MCAP_MDL_CB_ABORTED, /* mcap_mdl_event_cb */
  147. MCAP_MDL_CB_REMOTE_CONN_REQ, /* mcap_remote_mdl_conn_req_cb */
  148. MCAP_MDL_CB_REMOTE_RECONN_REQ /* mcap_remote_mdl_reconn_req_cb */
  149. } McapMclCb;
  150. typedef enum {
  151. MCL_CONNECTED,
  152. MCL_PENDING,
  153. MCL_ACTIVE,
  154. MCL_IDLE
  155. } MCLState;
  156. typedef enum {
  157. MCL_ACCEPTOR,
  158. MCL_INITIATOR
  159. } MCLRole;
  160. typedef enum {
  161. MCL_AVAILABLE,
  162. MCL_WAITING_RSP
  163. } MCAPCtrl;
  164. typedef enum {
  165. MDL_WAITING,
  166. MDL_CONNECTED,
  167. MDL_DELETING,
  168. MDL_CLOSED
  169. } MDLState;
  170. struct mcap_csp;
  171. struct mcap_mdl_op_cb;
  172. struct mcap_instance;
  173. struct mcap_mcl;
  174. struct mcap_mdl;
  175. struct sync_info_ind_data;
  176. /************ Callbacks ************/
  177. /* MDL callbacks */
  178. typedef void (* mcap_mdl_event_cb) (struct mcap_mdl *mdl, gpointer data);
  179. typedef void (* mcap_mdl_operation_conf_cb) (struct mcap_mdl *mdl, uint8_t conf,
  180. GError *err, gpointer data);
  181. typedef void (* mcap_mdl_operation_cb) (struct mcap_mdl *mdl, GError *err,
  182. gpointer data);
  183. typedef void (* mcap_mdl_notify_cb) (GError *err, gpointer data);
  184. /* Next function should return an MCAP appropriate response code */
  185. typedef uint8_t (* mcap_remote_mdl_conn_req_cb) (struct mcap_mcl *mcl,
  186. uint8_t mdepid, uint16_t mdlid,
  187. uint8_t *conf, gpointer data);
  188. typedef uint8_t (* mcap_remote_mdl_reconn_req_cb) (struct mcap_mdl *mdl,
  189. gpointer data);
  190. /* MCL callbacks */
  191. typedef void (* mcap_mcl_event_cb) (struct mcap_mcl *mcl, gpointer data);
  192. typedef void (* mcap_mcl_connect_cb) (struct mcap_mcl *mcl, GError *err,
  193. gpointer data);
  194. /* CSP callbacks */
  195. typedef void (* mcap_info_ind_event_cb) (struct mcap_mcl *mcl,
  196. struct sync_info_ind_data *data);
  197. typedef void (* mcap_sync_cap_cb) (struct mcap_mcl *mcl,
  198. uint8_t mcap_err,
  199. uint8_t btclockres,
  200. uint16_t synclead,
  201. uint16_t tmstampres,
  202. uint16_t tmstampacc,
  203. GError *err,
  204. gpointer data);
  205. typedef void (* mcap_sync_set_cb) (struct mcap_mcl *mcl,
  206. uint8_t mcap_err,
  207. uint32_t btclock,
  208. uint64_t timestamp,
  209. uint16_t accuracy,
  210. GError *err,
  211. gpointer data);
  212. struct mcap_mdl_cb {
  213. mcap_mdl_event_cb mdl_connected; /* Remote device has created a MDL */
  214. mcap_mdl_event_cb mdl_closed; /* Remote device has closed a MDL */
  215. mcap_mdl_event_cb mdl_deleted; /* Remote device requested deleting a MDL */
  216. mcap_mdl_event_cb mdl_aborted; /* Remote device aborted the mdl creation */
  217. mcap_remote_mdl_conn_req_cb mdl_conn_req; /* Remote device requested creating a MDL */
  218. mcap_remote_mdl_reconn_req_cb mdl_reconn_req; /* Remote device requested reconnecting a MDL */
  219. gpointer user_data; /* User data */
  220. };
  221. struct mcap_instance {
  222. bdaddr_t src; /* Source address */
  223. GIOChannel *ccio; /* Control Channel IO */
  224. GIOChannel *dcio; /* Data Channel IO */
  225. GSList *mcls; /* MCAP instance list */
  226. GSList *cached; /* List with all cached MCLs (MAX_CACHED macro) */
  227. BtIOSecLevel sec; /* Security level */
  228. mcap_mcl_event_cb mcl_connected_cb; /* New MCL connected */
  229. mcap_mcl_event_cb mcl_reconnected_cb; /* Old MCL has been reconnected */
  230. mcap_mcl_event_cb mcl_disconnected_cb; /* MCL disconnected */
  231. mcap_mcl_event_cb mcl_uncached_cb; /* MCL has been removed from MCAP cache */
  232. mcap_info_ind_event_cb mcl_sync_infoind_cb; /* (CSP Central) Received info indication */
  233. gpointer user_data; /* Data to be provided in callbacks */
  234. int ref; /* Reference counter */
  235. gboolean csp_enabled; /* CSP: functionality enabled */
  236. };
  237. struct mcap_mcl {
  238. struct mcap_instance *mi; /* MCAP instance where this MCL belongs */
  239. bdaddr_t addr; /* Device address */
  240. GIOChannel *cc; /* MCAP Control Channel IO */
  241. guint wid; /* MCL Watcher id */
  242. GSList *mdls; /* List of Data Channels shorted by mdlid */
  243. MCLState state; /* Current MCL State */
  244. MCLRole role; /* Initiator or acceptor of this MCL */
  245. MCAPCtrl req; /* Request control flag */
  246. struct mcap_mdl_op_cb *priv_data; /* Temporal data to manage responses */
  247. struct mcap_mdl_cb *cb; /* MDL callbacks */
  248. guint tid; /* Timer id for waiting for a response */
  249. uint8_t *lcmd; /* Last command sent */
  250. int ref; /* References counter */
  251. uint8_t ctrl; /* MCL control flag */
  252. uint16_t next_mdl; /* id used to create next MDL */
  253. struct mcap_csp *csp; /* CSP control structure */
  254. };
  255. struct mcap_mdl {
  256. struct mcap_mcl *mcl; /* MCL where this MDL belongs */
  257. GIOChannel *dc; /* MCAP Data Channel IO */
  258. guint wid; /* MDL Watcher id */
  259. uint16_t mdlid; /* MDL id */
  260. uint8_t mdep_id; /* MCAP Data End Point */
  261. MDLState state; /* MDL state */
  262. int ref; /* References counter */
  263. };
  264. struct sync_info_ind_data {
  265. uint32_t btclock;
  266. uint64_t timestamp;
  267. uint16_t accuracy;
  268. };
  269. /************ Operations ************/
  270. /* MDL operations */
  271. gboolean mcap_create_mdl(struct mcap_mcl *mcl,
  272. uint8_t mdepid,
  273. uint8_t conf,
  274. mcap_mdl_operation_conf_cb connect_cb,
  275. gpointer user_data,
  276. GDestroyNotify destroy,
  277. GError **err);
  278. gboolean mcap_reconnect_mdl(struct mcap_mdl *mdl,
  279. mcap_mdl_operation_cb reconnect_cb,
  280. gpointer user_data,
  281. GDestroyNotify destroy,
  282. GError **err);
  283. gboolean mcap_delete_all_mdls(struct mcap_mcl *mcl,
  284. mcap_mdl_notify_cb delete_cb,
  285. gpointer user_data,
  286. GDestroyNotify destroy,
  287. GError **err);
  288. gboolean mcap_delete_mdl(struct mcap_mdl *mdl,
  289. mcap_mdl_notify_cb delete_cb,
  290. gpointer user_data,
  291. GDestroyNotify destroy,
  292. GError **err);
  293. gboolean mcap_connect_mdl(struct mcap_mdl *mdl,
  294. uint8_t mode,
  295. uint16_t dcpsm,
  296. mcap_mdl_operation_cb connect_cb,
  297. gpointer user_data,
  298. GDestroyNotify destroy,
  299. GError **err);
  300. gboolean mcap_mdl_abort(struct mcap_mdl *mdl,
  301. mcap_mdl_notify_cb abort_cb,
  302. gpointer user_data,
  303. GDestroyNotify destroy,
  304. GError **err);
  305. int mcap_mdl_get_fd(struct mcap_mdl *mdl);
  306. uint16_t mcap_mdl_get_mdlid(struct mcap_mdl *mdl);
  307. struct mcap_mdl *mcap_mdl_ref(struct mcap_mdl *mdl);
  308. void mcap_mdl_unref(struct mcap_mdl *mdl);
  309. /* MCL operations */
  310. gboolean mcap_create_mcl(struct mcap_instance *mi,
  311. const bdaddr_t *addr,
  312. uint16_t ccpsm,
  313. mcap_mcl_connect_cb connect_cb,
  314. gpointer user_data,
  315. GDestroyNotify destroy,
  316. GError **err);
  317. void mcap_close_mcl(struct mcap_mcl *mcl, gboolean cache);
  318. gboolean mcap_mcl_set_cb(struct mcap_mcl *mcl, gpointer user_data,
  319. GError **gerr, McapMclCb cb1, ...);
  320. void mcap_mcl_get_addr(struct mcap_mcl *mcl, bdaddr_t *addr);
  321. struct mcap_mcl *mcap_mcl_ref(struct mcap_mcl *mcl);
  322. void mcap_mcl_unref(struct mcap_mcl *mcl);
  323. /* CSP operations */
  324. void mcap_enable_csp(struct mcap_instance *mi);
  325. void mcap_disable_csp(struct mcap_instance *mi);
  326. uint64_t mcap_get_timestamp(struct mcap_mcl *mcl,
  327. struct timespec *given_time);
  328. uint32_t mcap_get_btclock(struct mcap_mcl *mcl);
  329. void mcap_sync_cap_req(struct mcap_mcl *mcl,
  330. uint16_t reqacc,
  331. mcap_sync_cap_cb cb,
  332. gpointer user_data,
  333. GError **err);
  334. void mcap_sync_set_req(struct mcap_mcl *mcl,
  335. uint8_t update,
  336. uint32_t btclock,
  337. uint64_t timestamp,
  338. mcap_sync_set_cb cb,
  339. gpointer user_data,
  340. GError **err);
  341. /* MCAP main operations */
  342. struct mcap_instance *mcap_create_instance(const bdaddr_t *src,
  343. BtIOSecLevel sec, uint16_t ccpsm,
  344. uint16_t dcpsm,
  345. mcap_mcl_event_cb mcl_connected,
  346. mcap_mcl_event_cb mcl_reconnected,
  347. mcap_mcl_event_cb mcl_disconnected,
  348. mcap_mcl_event_cb mcl_uncached,
  349. mcap_info_ind_event_cb mcl_sync_info_ind,
  350. gpointer user_data,
  351. GError **gerr);
  352. void mcap_release_instance(struct mcap_instance *mi);
  353. struct mcap_instance *mcap_instance_ref(struct mcap_instance *mi);
  354. void mcap_instance_unref(struct mcap_instance *mi);
  355. uint16_t mcap_get_ctrl_psm(struct mcap_instance *mi, GError **err);
  356. uint16_t mcap_get_data_psm(struct mcap_instance *mi, GError **err);
  357. gboolean mcap_set_data_chan_mode(struct mcap_instance *mi, uint8_t mode,
  358. GError **err);
  359. int mcap_send_data(int sock, const void *buf, uint32_t size);
  360. void proc_sync_cmd(struct mcap_mcl *mcl, uint8_t *cmd, uint32_t len);
  361. void mcap_sync_init(struct mcap_mcl *mcl);
  362. void mcap_sync_stop(struct mcap_mcl *mcl);