2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 // tizen specific #include <zipfile/zipfile.h>
24 #include <sys/types.h>
29 #define TRACE_TAG TRACE_SDB
30 #include "sdb_client.h"
32 static transport_type __sdb_transport = kTransportAny;
33 static const char* __sdb_serial = NULL;
35 static int __sdb_server_port = DEFAULT_SDB_PORT;
37 void sdb_set_transport(transport_type type, const char* serial)
39 __sdb_transport = type;
40 __sdb_serial = serial;
43 void sdb_set_tcp_specifics(int server_port)
45 __sdb_server_port = server_port;
48 int sdb_get_emulator_console_port(void)
50 const char* serial = __sdb_serial;
54 /* if no specific device was specified, we need to look at */
55 /* the list of connected devices, and extract an emulator */
56 /* name from it. two emulators is an error */
57 char* tmp = sdb_query("host:devices");
60 printf("no emulator connected\n");
64 char* q = strchr(p, '\n');
70 if (!memcmp(p, LOCAL_CLIENT_PREFIX, sizeof(LOCAL_CLIENT_PREFIX)-1)) {
71 if (serial != NULL) { /* more than one emulator listed */
83 return -1; /* no emulator found */
86 if (memcmp(serial, LOCAL_CLIENT_PREFIX, sizeof(LOCAL_CLIENT_PREFIX)-1) != 0)
87 return -1; /* not an emulator */
90 serial += sizeof(LOCAL_CLIENT_PREFIX)-1;
91 port = strtol(serial, NULL, 10);
95 static char __sdb_error[256] = { 0 };
97 const char *sdb_error(void)
102 static int switch_socket_transport(int fd)
109 snprintf(service, sizeof service, "host:transport:%s", __sdb_serial);
111 char* transport_type = "???";
113 switch (__sdb_transport) {
115 transport_type = "transport-usb";
117 case kTransportLocal:
118 transport_type = "transport-local";
121 transport_type = "transport-any";
124 // no switch necessary
129 snprintf(service, sizeof service, "host:%s", transport_type);
131 len = strlen(service);
132 snprintf(tmp, sizeof tmp, "%04x", len);
134 if(writex(fd, tmp, 4) || writex(fd, service, len)) {
135 strcpy(__sdb_error, "write failure during connection");
139 D("Switch transport in progress\n");
143 D("Switch transport failed\n");
146 D("Switch transport success\n");
150 int sdb_status(int fd)
152 unsigned char buf[5];
155 if(readx(fd, buf, 4)) {
156 strcpy(__sdb_error, "protocol fault (no status)");
160 if(!memcmp(buf, "OKAY", 4)) {
164 if(memcmp(buf, "FAIL", 4)) {
166 "protocol fault (status %02x %02x %02x %02x?!)",
167 buf[0], buf[1], buf[2], buf[3]);
171 if(readx(fd, buf, 4)) {
172 strcpy(__sdb_error, "protocol fault (status len)");
176 len = strtoul((char*)buf, 0, 16);
177 if(len > 255) len = 255;
178 if(readx(fd, __sdb_error, len)) {
179 strcpy(__sdb_error, "protocol fault (status read)");
182 __sdb_error[len] = 0;
186 int _sdb_connect(const char *service)
192 D("_sdb_connect: %s\n", service);
193 len = strlen(service);
194 if((len < 1) || (len > 1024)) {
195 strcpy(__sdb_error, "service name too long");
198 snprintf(tmp, sizeof tmp, "%04x", len);
200 fd = socket_loopback_client(__sdb_server_port, SOCK_STREAM);
202 strcpy(__sdb_error, "cannot connect to daemon");
206 if (memcmp(service,"host",4) != 0 && switch_socket_transport(fd)) {
210 if(writex(fd, tmp, 4) || writex(fd, service, len)) {
211 strcpy(__sdb_error, "write failure during connection");
221 D("_sdb_connect: return fd %d\n", fd);
225 int sdb_connect(const char *service)
227 // first query the sdb server's version
228 int fd = _sdb_connect("host:version");
230 D("sdb_connect: service %s\n", service);
232 fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
235 if(launch_server(__sdb_server_port)) {
236 fprintf(stderr,"* failed to start daemon *\n");
239 fprintf(stdout,"* daemon started successfully *\n");
241 /* give the server some time to start properly and detect devices */
243 // fall through to _sdb_connect
245 // if server was running, check its version to make sure it is not out of date
248 int version = SDB_SERVER_VERSION - 1;
250 // if we have a file descriptor, then parse version result
252 if(readx(fd, buf, 4)) goto error;
255 n = strtoul(buf, 0, 16);
256 if(n > (int)sizeof(buf)) goto error;
257 if(readx(fd, buf, n)) goto error;
260 if (sscanf(buf, "%04x", &version) != 1) goto error;
262 // if fd is -1, then check for "unknown host service",
263 // which would indicate a version of sdb that does not support the version command
264 if (strcmp(__sdb_error, "unknown host service") != 0)
268 if(version != SDB_SERVER_VERSION) {
269 printf("sdb server is out of date. killing...\n");
270 fd = _sdb_connect("host:kill");
273 /* XXX can we better detect its death? */
279 // if the command is start-server, we are done.
280 if (!strcmp(service, "host:start-server"))
283 fd = _sdb_connect(service);
285 fprintf(stderr,"** daemon still not running");
287 D("sdb_connect: return fd %d\n", fd);
296 int sdb_command(const char *service)
298 int fd = sdb_connect(service);
311 char *sdb_query(const char *service)
317 D("sdb_query: %s\n", service);
318 int fd = sdb_connect(service);
320 fprintf(stderr,"error: %s\n", __sdb_error);
324 if(readx(fd, buf, 4)) goto oops;
327 n = strtoul(buf, 0, 16);
328 if(n > 1024) goto oops;
331 if(tmp == 0) goto oops;
333 if(readx(fd, tmp, n) == 0) {