tws_api.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #ifndef TWS_API_H
  2. #define TWS_API_H
  3. #include "typedef.h"
  4. #include "classic/tws_event.h"
  5. #include "classic/tws_local_media_sync.h"
  6. #include "classic/tws_data_trans.h"
  7. #define TWS_ROLE_MASTER 0
  8. #define TWS_ROLE_SLAVE 1
  9. /*
  10. * tws 状态
  11. */
  12. #define TWS_STA_SIBLING_DISCONNECTED 0x00000001 //tws未连接
  13. #define TWS_STA_SIBLING_CONNECTED 0x00000002 //tws已连接
  14. #define TWS_STA_PHONE_DISCONNECTED 0x00000004 //手机未连接
  15. #define TWS_STA_PHONE_CONNECTED 0x00000008 //手机已连接
  16. #define TWS_STA_ESCO_OPEN 0x00000010 //正在打电话
  17. #define TWS_STA_SBC_OPEN 0x00000020 //正在播歌
  18. #define TWS_STA_MONITOR_START 0x00000040 //tws从机开始监听手机链路
  19. #define TWS_STA_LOCAL_TWS_OPEN 0x00000080 //开启local_tws
  20. #define TWS_STA_ESCO_OPEN_LINK 0x00000100 //正在打电话,create link
  21. #define TWS_STA_MONITOR_ING 0x00000200 //tws主从收到监听信息
  22. #define TWS_SYNC_CALL_TX 1
  23. #define TWS_SYNC_CALL_RX 2
  24. struct tws_sync_call {
  25. int uuid;
  26. void (*func)(int priv, int err);
  27. const char *task_name;
  28. };
  29. extern const struct tws_sync_call tws_sync_call_begin[];
  30. extern const struct tws_sync_call tws_sync_call_end[];
  31. #define list_for_each_tws_sync_call(p) \
  32. for (p = tws_sync_call_begin; p < tws_sync_call_end; p++)
  33. #define TWS_SYNC_CALL_REGISTER(sync_call) \
  34. static const struct tws_sync_call __tws_##sync_call sec(.tws_sync_call)
  35. #define TWS_FUNC_ID(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  36. typedef void (*tws_func_t)(void *data, u16 len, bool rx);
  37. struct tws_func_stub {
  38. u32 func_id;
  39. tws_func_t func; //call from irq
  40. };
  41. #define REGISTER_TWS_FUNC_STUB(stub) \
  42. static const struct tws_func_stub stub sec(.tws_func_stub)
  43. extern const struct tws_func_stub tws_func_stub_begin[];
  44. extern const struct tws_func_stub tws_func_stub_end[];
  45. static inline tws_func_t tws_function_get_by_id(u32 id)
  46. {
  47. const struct tws_func_stub *p;
  48. for (p = tws_func_stub_begin; p < tws_func_stub_end; p++) {
  49. if (p->func_id == id) {
  50. return p->func;
  51. }
  52. }
  53. return NULL;
  54. }
  55. static inline void tws_function_call_by_id(u32 id, void *data, u16 len, bool rx)
  56. {
  57. const struct tws_func_stub *p;
  58. for (p = tws_func_stub_begin; p < tws_func_stub_end; p++) {
  59. if (p->func_id == id) {
  60. p->func(data, len, rx);
  61. break;
  62. }
  63. }
  64. }
  65. /*
  66. * 通过搜索码搜索tws设备
  67. */
  68. int tws_api_search_sibling_by_code(u16 code, int timeout_ms);
  69. /*
  70. *打开可发现, 可连接,可被手机和tws搜索到
  71. */
  72. int tws_api_wait_pair_by_code(u16 code, const char *name, int timeout_ms);
  73. int tws_api_wait_pair_by_ble(u16 code, const char *name, int timeout_ms);
  74. int tws_api_wait_tws_pair(int code, const char *name);
  75. int tws_api_wait_phone_pair(int code, const char *name);
  76. int tws_wait_tws_pair(u16 code, const char *name);
  77. int tws_wait_phone_pair(u16 code, const char *name);
  78. /*
  79. *取消可发现, 可连接,可被tws搜索到
  80. */
  81. int tws_api_cancle_wait_pair();
  82. /*
  83. * 搜索并连接已经配对过的tws
  84. * timeout: 单位ms, 0 表示不超时
  85. * 返回值: 0: 函数调用成功
  86. */
  87. int tws_api_create_connection(int timeout);
  88. /*
  89. * 取消搜索已配对的tws
  90. */
  91. int tws_api_cancle_create_connection();
  92. /*
  93. * 打开可发现,可连接, 可以被手机和已配对过的tws连接
  94. */
  95. int tws_api_wait_connection();
  96. /*
  97. * 断开tws直接的连接
  98. * reason: 断开原因
  99. */
  100. int tws_api_detach(enum tws_detach_reason reason);
  101. /*
  102. * 获取主从, 播歌和打电话状态下结果不可靠,请勿调用
  103. */
  104. int tws_api_get_role();
  105. /*
  106. * 获取tws 连接的状态
  107. * 返回值: 详见顶部TWS_STA_**
  108. */
  109. int tws_api_get_tws_state();
  110. /*
  111. * 设置tws对方地址
  112. */
  113. int tws_api_set_sibling_addr(u8 *addr);
  114. /*
  115. * 获取tws对方地址
  116. */
  117. int tws_api_get_sibling_addr(u8 *addr);
  118. /*
  119. * 获取tws本地地址
  120. */
  121. int tws_api_get_local_addr(u8 *addr);
  122. /*
  123. *发送解除配对命令给对方, 成功后会收到TWS_EVENT_REMOVE_PAIRS事件
  124. */
  125. int tws_api_remove_pairs();
  126. /*
  127. * 设置本地声道
  128. * 'L': 左声道
  129. * 'R': 右声道
  130. * 'U': 双声道合并
  131. */
  132. void tws_api_set_local_channel(char channel);
  133. /*
  134. * 获取本地声道
  135. */
  136. char tws_api_get_local_channel();
  137. /*
  138. * 通过uuid,主从同步调用相同函数
  139. */
  140. int tws_api_sync_call_by_uuid(int uuid, int priv, int delay_ms);
  141. /*
  142. * tws 数据发送函数, 要求 len <= 512
  143. */
  144. int tws_api_send_data_to_sibling(void *data, u16 len, u32 func_id);
  145. int tws_api_send_data_to_slave(void *data, int len, u32 func_id);
  146. int tws_profile_init();
  147. int tws_profile_exit();
  148. int tws_api_connect_in_esco();
  149. int tws_api_cancle_connect_in_esco();
  150. /*
  151. * 使能对耳自动主从切换
  152. */
  153. void tws_api_auto_role_switch_enable();
  154. /*
  155. * 关闭对耳自动主从切换
  156. */
  157. void tws_api_auto_role_switch_disable();
  158. int tws_api_get_low_latency_state();
  159. int tws_api_low_latency_enable(bool enable);
  160. void tws_api_set_quick_connect_addr(u8 *addr);
  161. u8 *tws_api_get_quick_connect_addr();
  162. void tws_api_common_addr_en(u8 en);
  163. void tws_api_pair_all_way(u8 en);
  164. int tws_api_power_saving_mode_enable();
  165. int tws_api_power_saving_mode_disable();
  166. int tws_api_enter_pure_monitor_mode();
  167. void tws_try_connect_disable(void);
  168. void tws_conn_switch_role();
  169. void tws_api_role_switch();
  170. #endif