if-main.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* SPDX-License-Identifier: Apache-2.0 */
  2. /*
  3. * Copyright (C) 2013 Intel Corporation
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <signal.h>
  10. #include <fcntl.h>
  11. #include <errno.h>
  12. #include <sys/socket.h>
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15. #include <sys/time.h>
  16. #include <sys/un.h>
  17. #include <poll.h>
  18. #include <hardware/audio.h>
  19. #include <hardware/bluetooth.h>
  20. #include <hardware/bt_av.h>
  21. #include <hardware/bt_hh.h>
  22. #include <hardware/bt_pan.h>
  23. #include <hardware/bt_sock.h>
  24. #include <hardware/bt_hf.h>
  25. #include <hardware/bt_hl.h>
  26. #include "hal.h"
  27. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  28. #include <hardware/bt_hf_client.h>
  29. #include <hardware/bt_mce.h>
  30. #endif
  31. #include <hardware/bt_rc.h>
  32. #include <hardware/bt_gatt.h>
  33. #include <hardware/bt_gatt_types.h>
  34. #include <hardware/bt_gatt_client.h>
  35. #include <hardware/bt_gatt_server.h>
  36. extern audio_hw_device_t *if_audio;
  37. /* Interfaces from hal that can be populated during application lifetime */
  38. extern const bt_interface_t *if_bluetooth;
  39. extern const btav_interface_t *if_av;
  40. extern const btrc_interface_t *if_rc;
  41. extern const bthf_interface_t *if_hf;
  42. extern const bthh_interface_t *if_hh;
  43. extern const btpan_interface_t *if_pan;
  44. extern const bthl_interface_t *if_hl;
  45. extern const btsock_interface_t *if_sock;
  46. extern const btgatt_interface_t *if_gatt;
  47. extern const btgatt_server_interface_t *if_gatt_server;
  48. extern const btgatt_client_interface_t *if_gatt_client;
  49. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  50. extern const btrc_ctrl_interface_t *if_rc_ctrl;
  51. extern const bthf_client_interface_t *if_hf_client;
  52. extern const btmce_interface_t *if_mce;
  53. extern const btav_interface_t *if_av_sink;
  54. #endif
  55. /*
  56. * Structure defines top level interfaces that can be used in test tool
  57. * this will contain values as: bluetooth, av, gatt, socket, pan...
  58. */
  59. struct interface {
  60. const char *name; /* interface name */
  61. struct method *methods; /* methods available for this interface */
  62. };
  63. extern const struct interface audio_if;
  64. extern const struct interface sco_if;
  65. extern const struct interface bluetooth_if;
  66. extern const struct interface av_if;
  67. extern const struct interface rc_if;
  68. extern const struct interface gatt_if;
  69. extern const struct interface gatt_client_if;
  70. extern const struct interface gatt_server_if;
  71. extern const struct interface pan_if;
  72. extern const struct interface sock_if;
  73. extern const struct interface hf_if;
  74. extern const struct interface hh_if;
  75. extern const struct interface hl_if;
  76. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  77. extern const struct interface ctrl_rc_if;
  78. extern const struct interface hf_client_if;
  79. extern const struct interface mce_if;
  80. extern const struct interface av_sink_if;
  81. #endif
  82. /* Interfaces that will show up in tool (first part of command line) */
  83. extern const struct interface *interfaces[];
  84. #define METHOD(name, func, comp, help) {name, func, comp, help}
  85. #define STD_METHOD(m) {#m, m##_p, NULL, NULL}
  86. #define STD_METHODC(m) {#m, m##_p, m##_c, NULL}
  87. #define STD_METHODH(m, h) {#m, m##_p, NULL, h}
  88. #define STD_METHODCH(m, h) {#m, m##_p, m##_c, h}
  89. #define END_METHOD {"", NULL, NULL, NULL}
  90. /*
  91. * Function to parse argument for function, argv[0] and argv[1] are already
  92. * parsed before this function is called and contain interface and method name
  93. * up to argc - 1 arguments are finished and should be used to decide which
  94. * function enumeration function to return
  95. */
  96. typedef void (*parse_and_call)(int argc, const char **argv);
  97. /*
  98. * This is prototype of function that will return string for given number.
  99. * Purpose is to enumerate string for auto completion.
  100. * Function of this type will always be called in loop.
  101. * First time function is called i = 0, then if function returns non-NULL
  102. * it will be called again till for some value of i it will return NULL
  103. */
  104. typedef const char *(*enum_func)(void *user, int i);
  105. /*
  106. * This is prototype of function that when given argc, argv will
  107. * fill enum_func with pointer to function that will enumerate
  108. * parameters for argc argument, user will be passed to enum_func.
  109. */
  110. typedef void (*tab_complete)(int argc, const char **argv, enum_func *enum_func,
  111. void **user);
  112. /*
  113. * For each method there is name and two functions to parse command line
  114. * and call proper hal function on.
  115. */
  116. struct method {
  117. const char *name;
  118. parse_and_call func;
  119. tab_complete complete;
  120. const char *help;
  121. };
  122. int haltest_error(const char *format, ...)
  123. __attribute__((format(printf, 1, 2)));
  124. int haltest_info(const char *format, ...)__attribute__((format(printf, 1, 2)));
  125. int haltest_warn(const char *format, ...)__attribute__((format(printf, 1, 2)));
  126. /* Enumerator for discovered devices, to be used as tab completion enum_func */
  127. const char *enum_devices(void *v, int i);
  128. const char *interface_name(void *v, int i);
  129. const char *command_name(void *v, int i);
  130. void add_remote_device(const bt_bdaddr_t *addr);
  131. bool close_hw_bt_dev(void);
  132. const struct interface *get_interface(const char *name);
  133. struct method *get_method(struct method *methods, const char *name);
  134. struct method *get_command(const char *name);
  135. const struct method *get_interface_method(const char *iname,
  136. const char *mname);
  137. #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
  138. /* Helper macro for executing function on interface and printing BT_STATUS */
  139. #define EXEC(f, ...) \
  140. { \
  141. if (f) { \
  142. int err = f(__VA_ARGS__); \
  143. haltest_info("%s: %s\n", #f, bt_status_t2str(err)); \
  144. } else { \
  145. haltest_info("%s is NULL\n", #f); \
  146. } \
  147. }
  148. /* Helper macro for executing void function on interface */
  149. #define EXECV(f, ...) \
  150. { \
  151. (void) f(__VA_ARGS__); \
  152. haltest_info("%s: void\n", #f); \
  153. }
  154. #define RETURN_IF_NULL(x) \
  155. do { if (!x) { haltest_error("%s is NULL\n", #x); return; } } while (0)
  156. #define VERIFY_ADDR_ARG(n, adr) \
  157. do { \
  158. if (n < argc) {\
  159. str2bt_bdaddr_t(argv[n], adr); \
  160. } else { \
  161. haltest_error("No address specified\n");\
  162. return;\
  163. } \
  164. } while (0)