adb_client.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <limits.h>
  6. #include <stdarg.h>
  7. #include <zipfile/zipfile.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include "sysdeps.h"
  11. #define TRACE_TAG TRACE_ADB
  12. #include "adb_client.h"
  13. static transport_type __adb_transport = kTransportAny;
  14. static const char* __adb_serial = NULL;
  15. static int __adb_server_port = DEFAULT_ADB_PORT;
  16. void adb_set_transport(transport_type type, const char* serial)
  17. {
  18. __adb_transport = type;
  19. __adb_serial = serial;
  20. }
  21. void adb_set_tcp_specifics(int server_port)
  22. {
  23. __adb_server_port = server_port;
  24. }
  25. int adb_get_emulator_console_port(void)
  26. {
  27. const char* serial = __adb_serial;
  28. int port;
  29. if (serial == NULL) {
  30. /* if no specific device was specified, we need to look at */
  31. /* the list of connected devices, and extract an emulator */
  32. /* name from it. two emulators is an error */
  33. char* tmp = adb_query("host:devices");
  34. char* p = tmp;
  35. if(!tmp) {
  36. printf("no emulator connected\n");
  37. return -1;
  38. }
  39. while (*p) {
  40. char* q = strchr(p, '\n');
  41. if (q != NULL)
  42. *q++ = 0;
  43. else
  44. q = p + strlen(p);
  45. if (!memcmp(p, LOCAL_CLIENT_PREFIX, sizeof(LOCAL_CLIENT_PREFIX)-1)) {
  46. if (serial != NULL) { /* more than one emulator listed */
  47. free(tmp);
  48. return -2;
  49. }
  50. serial = p;
  51. }
  52. p = q;
  53. }
  54. free(tmp);
  55. if (serial == NULL)
  56. return -1; /* no emulator found */
  57. }
  58. else {
  59. if (memcmp(serial, LOCAL_CLIENT_PREFIX, sizeof(LOCAL_CLIENT_PREFIX)-1) != 0)
  60. return -1; /* not an emulator */
  61. }
  62. serial += sizeof(LOCAL_CLIENT_PREFIX)-1;
  63. port = strtol(serial, NULL, 10);
  64. return port;
  65. }
  66. static char __adb_error[256] = { 0 };
  67. const char *adb_error(void)
  68. {
  69. return __adb_error;
  70. }
  71. static int switch_socket_transport(int fd)
  72. {
  73. char service[64];
  74. char tmp[5];
  75. int len;
  76. if (__adb_serial)
  77. snprintf(service, sizeof service, "host:transport:%s", __adb_serial);
  78. else {
  79. char* transport_type = "???";
  80. switch (__adb_transport) {
  81. case kTransportUsb:
  82. transport_type = "transport-usb";
  83. break;
  84. case kTransportLocal:
  85. transport_type = "transport-local";
  86. break;
  87. case kTransportAny:
  88. transport_type = "transport-any";
  89. break;
  90. case kTransportHost:
  91. // no switch necessary
  92. return 0;
  93. break;
  94. }
  95. snprintf(service, sizeof service, "host:%s", transport_type);
  96. }
  97. len = strlen(service);
  98. snprintf(tmp, sizeof tmp, "%04x", len);
  99. if(writex(fd, tmp, 4) || writex(fd, service, len)) {
  100. strcpy(__adb_error, "write failure during connection");
  101. adb_close(fd);
  102. return -1;
  103. }
  104. D("Switch transport in progress\n");
  105. if(adb_status(fd)) {
  106. adb_close(fd);
  107. D("Switch transport failed\n");
  108. return -1;
  109. }
  110. D("Switch transport success\n");
  111. return 0;
  112. }
  113. int adb_status(int fd)
  114. {
  115. unsigned char buf[5];
  116. unsigned len;
  117. if(readx(fd, buf, 4)) {
  118. strcpy(__adb_error, "protocol fault (no status)");
  119. return -1;
  120. }
  121. if(!memcmp(buf, "OKAY", 4)) {
  122. return 0;
  123. }
  124. if(memcmp(buf, "FAIL", 4)) {
  125. sprintf(__adb_error,
  126. "protocol fault (status %02x %02x %02x %02x?!)",
  127. buf[0], buf[1], buf[2], buf[3]);
  128. return -1;
  129. }
  130. if(readx(fd, buf, 4)) {
  131. strcpy(__adb_error, "protocol fault (status len)");
  132. return -1;
  133. }
  134. buf[4] = 0;
  135. len = strtoul((char*)buf, 0, 16);
  136. if(len > 255) len = 255;
  137. if(readx(fd, __adb_error, len)) {
  138. strcpy(__adb_error, "protocol fault (status read)");
  139. return -1;
  140. }
  141. __adb_error[len] = 0;
  142. return -1;
  143. }
  144. int _adb_connect(const char *service)
  145. {
  146. char tmp[5];
  147. int len;
  148. int fd;
  149. D("_adb_connect: %s\n", service);
  150. len = strlen(service);
  151. if((len < 1) || (len > 1024)) {
  152. strcpy(__adb_error, "service name too long");
  153. return -1;
  154. }
  155. snprintf(tmp, sizeof tmp, "%04x", len);
  156. fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
  157. if(fd < 0) {
  158. strcpy(__adb_error, "cannot connect to daemon");
  159. return -2;
  160. }
  161. if (memcmp(service,"host",4) != 0 && switch_socket_transport(fd)) {
  162. return -1;
  163. }
  164. if(writex(fd, tmp, 4) || writex(fd, service, len)) {
  165. strcpy(__adb_error, "write failure during connection");
  166. adb_close(fd);
  167. return -1;
  168. }
  169. if(adb_status(fd)) {
  170. adb_close(fd);
  171. return -1;
  172. }
  173. D("_adb_connect: return fd %d\n", fd);
  174. return fd;
  175. }
  176. int adb_connect(const char *service)
  177. {
  178. // first query the adb server's version
  179. int fd = _adb_connect("host:version");
  180. D("adb_connect: service %s\n", service);
  181. if(fd == -2) {
  182. fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
  183. __adb_server_port);
  184. start_server:
  185. if(launch_server(__adb_server_port)) {
  186. fprintf(stderr,"* failed to start daemon *\n");
  187. return -1;
  188. } else {
  189. fprintf(stdout,"* daemon started successfully *\n");
  190. }
  191. /* give the server some time to start properly and detect devices */
  192. adb_sleep_ms(3000);
  193. // fall through to _adb_connect
  194. } else {
  195. // if server was running, check its version to make sure it is not out of date
  196. char buf[100];
  197. int n;
  198. int version = ADB_SERVER_VERSION - 1;
  199. // if we have a file descriptor, then parse version result
  200. if(fd >= 0) {
  201. if(readx(fd, buf, 4)) goto error;
  202. buf[4] = 0;
  203. n = strtoul(buf, 0, 16);
  204. if(n > (int)sizeof(buf)) goto error;
  205. if(readx(fd, buf, n)) goto error;
  206. adb_close(fd);
  207. if (sscanf(buf, "%04x", &version) != 1) goto error;
  208. } else {
  209. // if fd is -1, then check for "unknown host service",
  210. // which would indicate a version of adb that does not support the version command
  211. if (strcmp(__adb_error, "unknown host service") != 0)
  212. return fd;
  213. }
  214. if(version != ADB_SERVER_VERSION) {
  215. printf("adb server is out of date. killing...\n");
  216. fd = _adb_connect("host:kill");
  217. adb_close(fd);
  218. /* XXX can we better detect its death? */
  219. adb_sleep_ms(2000);
  220. goto start_server;
  221. }
  222. }
  223. // if the command is start-server, we are done.
  224. if (!strcmp(service, "host:start-server"))
  225. return 0;
  226. fd = _adb_connect(service);
  227. if(fd == -2) {
  228. fprintf(stderr,"** daemon still not running");
  229. }
  230. D("adb_connect: return fd %d\n", fd);
  231. return fd;
  232. error:
  233. adb_close(fd);
  234. return -1;
  235. }
  236. int adb_command(const char *service)
  237. {
  238. int fd = adb_connect(service);
  239. if(fd < 0) {
  240. return -1;
  241. }
  242. if(adb_status(fd)) {
  243. adb_close(fd);
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. char *adb_query(const char *service)
  249. {
  250. char buf[5];
  251. unsigned n;
  252. char *tmp;
  253. D("adb_query: %s\n", service);
  254. int fd = adb_connect(service);
  255. if(fd < 0) {
  256. fprintf(stderr,"error: %s\n", __adb_error);
  257. return 0;
  258. }
  259. if(readx(fd, buf, 4)) goto oops;
  260. buf[4] = 0;
  261. n = strtoul(buf, 0, 16);
  262. if(n > 1024) goto oops;
  263. tmp = malloc(n + 1);
  264. if(tmp == 0) goto oops;
  265. if(readx(fd, tmp, n) == 0) {
  266. tmp[n] = 0;
  267. adb_close(fd);
  268. return tmp;
  269. }
  270. free(tmp);
  271. oops:
  272. adb_close(fd);
  273. return 0;
  274. }