int Zigbee_Set_PanID(int fd, byte *pan_id) { byte cmd[8] = {0xfc, 0x02, 0x91, 0x01}; byte rbuf[2] = {0, 0}; int err = 0; if (pan_id == NULL) return -1; cmd[4] = pan_id[0]; cmd[5] = pan_id[1]; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, rbuf, 2, 5)) != 2) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 2); #endif if (memcmp(rbuf, pan_id, 2) != 0) return -1; return 0; }
int Zigbee_Read_MAC(int fd, byte *mac) { byte cmd[8] = {0xfc, 0x00, 0x91, 0x08, 0xa8, 0xb8}; int err = 0; if (mac == NULL) return -1; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, mac, 8, 5)) != 8) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(mac, 8); #endif return 0; }
usint Zigbee_Read_RouterAddr(int fd) { byte cmd[8] = {0xfc, 0x33, 0xd4, 0xa1, 0xa2, 0x01}; byte rbuf[2] = {0}; usint res = 0; int err = 0; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, rbuf, 2, 5)) != 2) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 2); #endif res = (rbuf[0] << 8) | (rbuf[1]); return res; }
int Zigbee_Get_channel(int fd) { byte cmd[8] = {0xfc, 0x00, 0x91, 0x0d, 0x34, 0x2b}; byte rbuf[6] = {0}; int err = 0; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, rbuf, 6, 5)) != 6) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 6); #endif return rbuf[5]; }
int Zigbee_Get_type(int fd) { byte cmd[8] = {0xfc, 0x00, 0x91, 0x0b, 0xcb, 0xeb}; byte res0[6] = {0x43, 0x6f, 0x6f, 0x72, 0x64, 0x69}; byte res1[6] = {0x52, 0x6f, 0x75, 0x74, 0x65, 0x72}; byte rbuf[6] = {0}; int err = 0; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, rbuf, 6, 5)) != 6) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 6); #endif if (memcmp(rbuf, res0, 6) == 0) return 0; else if (memcmp(rbuf, res1, 6) == 0) return 1; else return -1; }
usint Zigbee_Test_SerialPort(int fd) { byte cmd[8] = {0xfc, 0x00, 0x91, 0x07, 0x97, 0xa7}; byte res[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0xff}; byte rbuf[8] = {0}; int err = 0; if (zigbee_send_cmd(fd, cmd) < 0) return -1; memset(rbuf, 0, 8); if ((err = io_readn(fd, rbuf, 8, 5)) != 8) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 8); #endif if (memcmp(res, rbuf, 6) == 0) return 0; else return -1; }
int Zigbee_Read_ShortAddr(int fd, byte *short_addr) { byte cmd[8] = {0xfc, 0x00, 0x91, 0x04, 0xc4, 0xd4}; int err = 0; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, short_addr, 2, 5)) != 2) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(short_addr, 2); #endif return 0; }
int Zigbee_Read_PanID(int fd, byte *pan_id) { byte cmd[8] = {0xfc, 0x00, 0x91, 0x03, 0xa3, 0xb3}; int err = 0; if (pan_id == NULL) return -1; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, pan_id, 2, 5)) != 2) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(pan_id, 2); #endif return 0; }
static void print_message(byte *buf, int len) { int i; logcat(""); for(i = 0; i<len; i++) { logcat_raw("0x%02x ", buf[i]); if (((i+1)%16) == 0) logcat("\n"); } logcat("\n"); }
int Zigbee_Set_Bitrate(int fd, int speed) { byte cmd[8] = {0xfc, 0x01, 0x91, 0x06, 0x00, 0xf6}; byte rbuf[6] = {0}; int err = 0; switch (speed) { case 9600: cmd[4] = 1; break; case 19200: cmd[4] = 2; break; case 38400: cmd[4] = 3; break; case 57600: cmd[4] = 4; break; case 115200: cmd[4] = 5; break; default: logcat("Zigbee: Invalid uart speed.\n"); return -1; } if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, rbuf, 6, 5)) != 6) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 6); #endif err = rbuf[0]*100000 + rbuf[1]*10000 + rbuf[2]*1000 + rbuf[3]*100; if (err != (speed / 100)) return -1; return 0; }
int Sensor_Zigbee_ReadData(byte *buf, int len) { int fd; byte rbuf[256] = {0}; byte res[] = {0xa5,0x06,0x01,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0xba,0xbb,0xb5}; int err = 0; int timeout = 65; if ((buf == NULL) || (len > 256)) return -1; if ((fd = Zigbee_Get_Device(ZIGBEE_UART_SPEED)) < 0) return -1; #ifdef _DEBUG logcat("Zigbee Start to Read %d Bytes Data.\n", len); #endif memset(rbuf, 0, 256); if ((err = io_readn(fd, rbuf, len, timeout)) != len) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } Zigbee_Release_Device(fd); return -1; } else { #ifdef _DEBUG logcat("Zigbee Read Data: "); debug_out(rbuf, len); #endif } if ((memcmp(rbuf, res, 4))) // || (memcmp(rbuf + 8, res + 8, 5))) err = -1; else { memcpy(buf, rbuf, len); err = 0; } Zigbee_Release_Device(fd); return err; }
void Camera_SetPreset(byte addr, byte index) { byte cmd[7] = {0xff, 0x01, 0x00, 0x03, 0x00, 0x01, 0x05}; Enter_func(); logcat("Camera_SetPreset: index = %d \n", index); cmd[1] = addr; cmd[5] = index; Camera_SendCmd(cmd, 7); }
int Zigbee_Get_Device(int speed) { int fd; fd = uart_open_dev(ZIGBEE_UART_NAME); if (fd == -1) { logcat("serial port open error: %s\n", strerror(errno)); return -1; } uart_set_speed(fd, speed); if(uart_set_parity(fd, 8, 1, 'N') == -1) { logcat ("Set Parity Error\n"); return -1; } return fd; }
int Zigbee_Set_type(int fd, int type) { byte *cmd = NULL, *res = NULL; byte cmd0[8] = {0xfc, 0x00, 0x91, 0x09, 0xa9, 0xc9}; byte cmd1[8] = {0xfc, 0x00, 0x91, 0x0a, 0xba, 0xda}; byte res0[8] = {0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x00, 0x19}; byte res1[8] = {0x52, 0x6f, 0x75, 0x74, 0x65, 0x3b, 0x00, 0x19}; byte rbuf[8] = {0}; int err = 0; if (type == 0) { cmd = cmd0; res = res0; } else if (type == 1) { cmd = cmd1; res = res1; } else return -1; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, rbuf, 8, 10)) != 8) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 8); #endif if (memcmp(rbuf, res, 8) != 0) return -1; return 0; }
int Zigbee_Device_Init(void) { byte pan_id[2] = {0x19, 0x9b}; int channel = 0x16; int type = 0; /* Coordinator */ int bitrate; byte rbuf[8] = {0}; int fd; Device_power_ctl(DEVICE_ZIGBEE_CHIP, 1); Device_power_ctl(DEVICE_ZIGBEE_12V, 0); if ((fd = Zigbee_Get_Device(ZIGBEE_UART_SPEED)) < 0) return -1; bitrate = Zigbee_Get_BitRate(fd); logcat("Zigbee Get Bitrate: %d .\n", bitrate); if (bitrate != 9600) { Zigbee_Set_Bitrate(fd, 9600); if(Zigbee_Reset(fd) < 0) return -1; uart_set_speed(fd, 9600); } if (Zigbee_Get_type(fd) != type) { /* Set Zigbee to Coordinator Mode */ if(Zigbee_Set_type(fd, type) < 0) return -1; } if(Zigbee_Read_PanID(fd, rbuf) < 0) return -1; if (memcmp(rbuf, pan_id, 2) != 0) { if(Zigbee_Set_PanID(fd, pan_id) < 0) return -1; } if (Zigbee_Get_channel(fd) != channel) { if(Zigbee_Set_Channel(fd, channel) < 0) return -1; } if(Zigbee_Reset(fd) < 0) return -1; Zigbee_Release_Device(fd); Device_power_ctl(DEVICE_ZIGBEE_12V, 1); return 0; }
int Zigbee_Set_RouterAddr(int fd, usint addr) { byte cmd[8] = {0xfc, 0x32, 0xc3, 0x00, 0x00, 0x01}; byte rbuf[2] = {0}; usint res = 0; int err = 0; if ((addr < 1) || (addr > 0xff00)) return -1; cmd[3] = (addr & 0xff00) >> 8; cmd[4] = addr & 0x00ff; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, rbuf, 2, 5)) != 2) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 2); #endif res = (rbuf[0] << 8) | (rbuf[1]); if (res != addr) return -1; return 0; }
int Zigbee_Set_TransType(int fd, int type) { byte cmd[8] = {0xfc, 0x01, 0x91, 0x64, 0x58, 0x00}; byte res[6] = {0x06, 0x07, 0x08, 0x09, 0x0a, 0x00}; byte rbuf[6] = {0}; int err = 0; if ((type < 0) || (type > 0x07)) type = 0; cmd[5] = type; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, rbuf, 6, 5)) != 6) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 6); #endif res[5] = type; if (memcmp(rbuf, res, 6) != 0) return -1; return 0; }
int Zigbee_Set_Channel(int fd, int channel) { byte cmd[8] = {0xfc, 0x01, 0x91, 0x0c, 0x00, 0x1a}; byte rbuf[5] = {0}; int err = 0; unsigned int data = 0; if ((channel < 0x0b) || (channel > 0x1a)) return -1; else cmd[4] = channel; if (zigbee_send_cmd(fd, cmd) < 0) return -1; if ((err = io_readn(fd, rbuf, 5, 5)) != 5) { if (err < 0) { logcat("read uart: %s\n", strerror(errno)); } else { logcat("nread %d bytes\n", err); } return -1; } #ifdef _DEBUG logcat("Zigbee Cmd Return: "); debug_out(rbuf, 5); #endif memcpy(&data, rbuf, 4); if ((rbuf[4] != channel) || (!(data & (1 << channel)))) return -1; return 0; }
static int zigbee_send_cmd(int fd, byte *cmd) { int err = 0; if (cmd == NULL) return -1; cmd[6] = sum_check(cmd, 6); #ifdef _DEBUG logcat("Zigbee Send Command: "); debug_out(cmd, 7); #endif err = io_writen(fd, cmd, 7); if (err > 0) logcat("uart write %d bytes sucess.\n", err); else { logcat("write error, ret = %d\n", err); return -1; } return 0; }
int Camera_SendCmd(byte *cmd, int len) { int fd; int err = 0; byte cmd_stop[7] = {0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01}; pthread_mutex_lock(&rs485_mutex); av_rs485_used = 1; fd = uart_open_dev(UART_PORT_RS485); if (fd == -1) { logcat("serial port open error: %s\n", strerror(errno)); pthread_mutex_unlock(&rs485_mutex); return -1; } uart_set_speed(fd, UART_RS485_SPEDD); if(uart_set_parity(fd, 8, 1, 'N') == -1) { logcat ("Set Parity Error\n"); pthread_mutex_unlock(&rs485_mutex); return -1; } system("echo 1 >/sys/devices/platform/gpio-power.0/rs485_direction"); cmd[len - 1] = checksum((cmd + 1), 5); err = io_writen(fd, cmd, len); if (err > 0) logcat("RS485: Send Command Sucess.\n"); else { logcat("write error, ret = %d\n", err); pthread_mutex_unlock(&rs485_mutex); return -1; } usleep(ACTION_INTERVAL); /* Stop Cmd */ cmd_stop[6] = checksum((cmd_stop + 1), 5); err = io_writen(fd, cmd_stop, 7); if (err == 7) logcat("RS485: Send Stop Command Sucess.\n"); else { logcat("write error, ret = %d\n", err); pthread_mutex_unlock(&rs485_mutex); return -1; } #ifdef _DEBUG print_message(cmd, len); #endif pthread_mutex_unlock(&rs485_mutex); return 0; }
void receiveFunction(CADBTestCEDlg* dlg) { const int arrayLength = 4096; char recvBuf[arrayLength]; //while(true) { int recvLen = recv(dlg->sockClient,recvBuf,arrayLength,0); if (recvLen == SOCKET_ERROR) { int lastError = WSAGetLastError(); wchar_t wLog[MAX_PATH]; _stprintf(wLog, L"connect failed! error = %d", lastError); MessageBox(NULL, wLog, NULL, MB_OK); } recvBuf[recvLen] = '\0'; wchar_t wLog[512]; MultiByteToWideChar(GetACP(), 0, recvBuf, -1, wLog, 512); logcat(wLog); //processDownload(recvBuf, dlg); //} //std::string success_str("transfaner ok"); //send(dlg->sockClient, success_str.c_str(), success_str.length(), 0); char testBuf[4096]; memset(testBuf, 1, 4096); send(dlg->sockClient, testBuf, 4096, 0); closesocket(dlg->sockClient); //dlg->displayInfoBuffer.Append(L"全部下载完成!"); //dlg->displayInfoBuffer.Append(L"\r\n"); //dlg->displayInfo.SetWindowTextW(dlg->displayInfoBuffer); /* printf("%s\n",recvBuf); TCHAR wLog[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, recvBuf, recvLen, wLog, MAX_PATH); displayInfoBuffer.Append(wLog); displayInfoBuffer.Append(L"\r\n"); displayInfo.SetWindowTextW(displayInfoBuffer); send(sockClient,"hello",strlen("hello")+1,0);//发送数据 closesocket(sockClient);//关闭连接 WSACleanup(); */ }
/* downloads a decrypted image from Walrus based on the manifest URL, * saves it to outfile */ static int walrus_request (const char * walrus_op, const char * verb, const char * requested_url, const char * outfile, const int do_compress) { int code = ERROR; char url [BUFSIZE]; strncpy (url, requested_url, BUFSIZE); #if defined(CAN_GZIP) if (do_compress) snprintf (url, BUFSIZE, "%s%s", requested_url, "?IsCompressed=true"); #endif logprintfl (EUCAINFO, "walrus_request(): downloading %s\n", outfile); logprintfl (EUCAINFO, " from %s\n", url); /* isolate the PATH in the URL as it will be needed for signing */ char * url_path; if (strncasecmp (url, "http://", 7)!=0) { logprintfl (EUCAERROR, "walrus_request(): URL must start with http://...\n"); return code; } if ((url_path=strchr(url+7, '/'))==NULL) { /* find first '/' after hostname */ logprintfl (EUCAERROR, "walrus_request(): URL has no path\n"); return code; } if (euca_init_cert()) { logprintfl (EUCAERROR, "walrus_request(): failed to initialize certificate\n"); return code; } FILE * fp = fopen64 (outfile, "w"); if (fp==NULL) { logprintfl (EUCAERROR, "walrus_request(): failed to open %s for writing\n", outfile); return code; } CURL * curl; CURLcode result; curl = curl_easy_init (); if (curl==NULL) { logprintfl (EUCAERROR, "walrus_request(): could not initialize libcurl\n"); fclose(fp); return code; } char error_msg [CURL_ERROR_SIZE]; curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, error_msg); curl_easy_setopt (curl, CURLOPT_URL, url); curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, write_header); if (strncmp (verb, "GET", 4)==0) { curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L); } else if (strncmp (verb, "HEAD", 5)==0) { /* TODO: HEAD isn't very useful atm since we don't look at headers */ curl_easy_setopt (curl, CURLOPT_NOBODY, 1L); } else { fclose(fp); logprintfl (EUCAERROR, "walrus_request(): invalid HTTP verb %s\n", verb); return ERROR; /* TODO: dealloc structs before returning! */ } /* set up the default write function, but possibly override * it below, if compression is desired and possible */ struct request params; params.fp = fp; curl_easy_setopt (curl, CURLOPT_WRITEDATA, ¶ms); curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data); #if defined(CAN_GZIP) if (do_compress) { curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data_zlib); } #endif struct curl_slist * headers = NULL; /* beginning of a DLL with headers */ headers = curl_slist_append (headers, "Authorization: Euca"); char op_hdr [STRSIZE]; if(walrus_op != NULL) { snprintf (op_hdr, STRSIZE, "EucaOperation: %s", walrus_op); headers = curl_slist_append (headers, op_hdr); } time_t t = time(NULL); char * date_str = asctime(localtime(&t)); /* points to a static area */ if (date_str==NULL) { fclose(fp); return ERROR; } assert (strlen(date_str)+7<=STRSIZE); date_str [strlen(date_str)-1] = '\0'; /* trim off the newline */ char date_hdr [STRSIZE]; snprintf (date_hdr, STRSIZE, "Date: %s", date_str); headers = curl_slist_append (headers, date_hdr); char * cert_str = euca_get_cert (0); /* read the cloud-wide cert */ if (cert_str==NULL) { fclose(fp); return ERROR; } char * cert64_str = base64_enc ((unsigned char *)cert_str, strlen(cert_str)); assert (strlen(cert64_str)+11<=BUFSIZE); char cert_hdr [BUFSIZE]; snprintf (cert_hdr, BUFSIZE, "EucaCert: %s", cert64_str); logprintfl (EUCADEBUG2, "walrus_request(): base64 certificate, %s\n", get_string_stats(cert64_str)); headers = curl_slist_append (headers, cert_hdr); free (cert64_str); free (cert_str); char * sig_str = euca_sign_url (verb, date_str, url_path); /* create Walrus-compliant sig */ if (sig_str==NULL) { fclose(fp); return ERROR; } assert (strlen(sig_str)+16<=BUFSIZE); char sig_hdr [BUFSIZE]; snprintf (sig_hdr, BUFSIZE, "EucaSignature: %s", sig_str); headers = curl_slist_append (headers, sig_hdr); curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers); /* register headers */ if (walrus_op) { logprintfl (EUCADEBUG, "walrus_request(): writing %s/%s output to %s\n", verb, walrus_op, outfile); } else { logprintfl (EUCADEBUG, "walrus_request(): writing %s output to %s\n", verb, outfile); } int retries = TOTAL_RETRIES; int timeout = FIRST_TIMEOUT; do { params.total_wrote = 0L; params.total_calls = 0L; #if defined(CAN_GZIP) if (do_compress) { /* allocate zlib inflate state */ params.strm.zalloc = Z_NULL; params.strm.zfree = Z_NULL; params.strm.opaque = Z_NULL; params.strm.avail_in = 0; params.strm.next_in = Z_NULL; params.ret = inflateInit2 (&(params.strm), 31); if (params.ret != Z_OK) { zerr (params.ret, "walrus_request"); break; } } #endif result = curl_easy_perform (curl); /* do it */ logprintfl (EUCADEBUG, "walrus_request(): wrote %ld bytes in %ld writes\n", params.total_wrote, params.total_calls); #if defined(CAN_GZIP) if (do_compress) { inflateEnd(&(params.strm)); if (params.ret != Z_STREAM_END) { zerr (params.ret, "walrus_request"); } } #endif if (result) { // curl error (connection or transfer failed) logprintfl (EUCAERROR, "walrus_request(): %s (%d)\n", error_msg, result); if (retries > 0) { logprintfl (EUCAERROR, " download retry %d of %d will commence in %d seconds\n", retries, TOTAL_RETRIES, timeout); } sleep (timeout); fseek (fp, 0L, SEEK_SET); timeout <<= 1; retries--; } else { long httpcode; curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &httpcode); /* TODO: pull out response message, too */ switch (httpcode) { case 200L: /* all good */ logprintfl (EUCAINFO, "walrus_request(): saved image in %s\n", outfile); code = OK; retries = 0; break; case 408L: /* timeout, retry */ logprintfl (EUCAWARN, "walrus_request(): server responded with HTTP code %ld (timeout), retrying\n", httpcode); logcat (EUCADEBUG, outfile); /* dump the error from outfile into the log */ break; default: /* some kind of error */ logprintfl (EUCAERROR, "walrus_request(): server responded with HTTP code %ld, retrying\n", httpcode); logcat (EUCADEBUG, outfile); /* dump the error from outfile into the log */ } } } while (code!=OK && retries>0); fclose (fp); if ( code != OK ) { logprintfl (EUCAINFO, "walrus_request(): due to error, removing %s\n", outfile); remove (outfile); } free (sig_str); curl_slist_free_all (headers); curl_easy_cleanup (curl); return code; }
int adb_commandline(int argc, char **argv) { char buf[4096]; int no_daemon = 0; int is_daemon = 0; int persist = 0; int r; int quote; transport_type ttype = kTransportAny; char* serial = NULL; /* If defined, this should be an absolute path to * the directory containing all of the various system images * for a particular product. If not defined, and the adb * command requires this information, then the user must * specify the path using "-p". */ gProductOutPath = getenv("ANDROID_PRODUCT_OUT"); if (gProductOutPath == NULL || gProductOutPath[0] == '\0') { gProductOutPath = NULL; } // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint serial = getenv("ANDROID_SERIAL"); /* modifiers and flags */ while(argc > 0) { if(!strcmp(argv[0],"nodaemon")) { no_daemon = 1; } else if (!strcmp(argv[0], "fork-server")) { /* this is a special flag used only when the ADB client launches the ADB Server */ is_daemon = 1; } else if(!strcmp(argv[0],"persist")) { persist = 1; } else if(!strncmp(argv[0], "-p", 2)) { const char *product = NULL; if (argv[0][2] == '\0') { if (argc < 2) return usage(); product = argv[1]; argc--; argv++; } else { product = argv[1] + 2; } gProductOutPath = find_product_out_path(product); if (gProductOutPath == NULL) { fprintf(stderr, "adb: could not resolve \"-p %s\"\n", product); return usage(); } } else if (argv[0][0]=='-' && argv[0][1]=='s') { if (isdigit(argv[0][2])) { serial = argv[0] + 2; } else { if(argc < 2) return usage(); serial = argv[1]; argc--; argv++; } } else if (!strcmp(argv[0],"-d")) { ttype = kTransportUsb; } else if (!strcmp(argv[0],"-e")) { ttype = kTransportLocal; } else { /* out of recognized modifiers and flags */ break; } argc--; argv++; } adb_set_transport(ttype, serial); if ((argc > 0) && (!strcmp(argv[0],"server"))) { if (no_daemon || is_daemon) { r = adb_main(is_daemon); } else { r = launch_server(); } if(r) { fprintf(stderr,"* could not start server *\n"); } return r; } top: if(argc == 0) { return usage(); } /* adb_connect() commands */ if(!strcmp(argv[0], "devices")) { char *tmp; snprintf(buf, sizeof buf, "host:%s", argv[0]); tmp = adb_query(buf); if(tmp) { printf("List of devices attached \n"); printf("%s\n", tmp); return 0; } else { return 1; } } if(!strcmp(argv[0], "connect") || !strcmp(argv[0], "disconnect")) { char *tmp; if (argc != 2) { fprintf(stderr, "Usage: adb %s <host>:<port>\n", argv[0]); return 1; } snprintf(buf, sizeof buf, "host:%s:%s", argv[0], argv[1]); tmp = adb_query(buf); if(tmp) { printf("%s\n", tmp); return 0; } else { return 1; } } if (!strcmp(argv[0], "emu")) { return adb_send_emulator_command(argc, argv); } if(!strcmp(argv[0], "shell")) { int r; int fd; if(argc < 2) { return interactive_shell(); } snprintf(buf, sizeof buf, "shell:%s", argv[1]); argc -= 2; argv += 2; while(argc-- > 0) { strcat(buf, " "); /* quote empty strings and strings with spaces */ quote = (**argv == 0 || strchr(*argv, ' ')); if (quote) strcat(buf, "\""); strcat(buf, *argv++); if (quote) strcat(buf, "\""); } for(;;) { fd = adb_connect(buf); if(fd >= 0) { read_and_dump(fd); adb_close(fd); r = 0; } else { fprintf(stderr,"error: %s\n", adb_error()); r = -1; } if(persist) { fprintf(stderr,"\n- waiting for device -\n"); adb_sleep_ms(1000); do_cmd(ttype, serial, "wait-for-device", 0); } else { return r; } } } if(!strcmp(argv[0], "kill-server")) { int fd; fd = _adb_connect("host:kill"); if(fd == -1) { fprintf(stderr,"* server not running *\n"); return 1; } return 0; } if(!strcmp(argv[0], "remount") || !strcmp(argv[0], "reboot") || !strcmp(argv[0], "reboot-bootloader") || !strcmp(argv[0], "tcpip") || !strcmp(argv[0], "usb") || !strcmp(argv[0], "root")) { char command[100]; if (!strcmp(argv[0], "reboot-bootloader")) snprintf(command, sizeof(command), "reboot:bootloader"); else if (argc > 1) snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]); else snprintf(command, sizeof(command), "%s:", argv[0]); int fd = adb_connect(command); if(fd >= 0) { read_and_dump(fd); adb_close(fd); return 0; } fprintf(stderr,"error: %s\n", adb_error()); return 1; } if(!strcmp(argv[0], "bugreport")) { if (argc != 1) return usage(); do_cmd(ttype, serial, "shell", "bugreport", 0); return 0; } /* adb_command() wrapper commands */ if(!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) { char* service = argv[0]; if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) { if (ttype == kTransportUsb) { service = "wait-for-usb"; } else if (ttype == kTransportLocal) { service = "wait-for-local"; } else { service = "wait-for-any"; } } format_host_command(buf, sizeof buf, service, ttype, serial); if (adb_command(buf)) { D("failure: %s *\n",adb_error()); fprintf(stderr,"error: %s\n", adb_error()); return 1; } /* Allow a command to be run after wait-for-device, * e.g. 'adb wait-for-device shell'. */ if(argc > 1) { argc--; argv++; goto top; } return 0; } if(!strcmp(argv[0], "forward")) { if(argc != 3) return usage(); if (serial) { snprintf(buf, sizeof buf, "host-serial:%s:forward:%s;%s",serial, argv[1], argv[2]); } else if (ttype == kTransportUsb) { snprintf(buf, sizeof buf, "host-usb:forward:%s;%s", argv[1], argv[2]); } else if (ttype == kTransportLocal) { snprintf(buf, sizeof buf, "host-local:forward:%s;%s", argv[1], argv[2]); } else { snprintf(buf, sizeof buf, "host:forward:%s;%s", argv[1], argv[2]); } if(adb_command(buf)) { fprintf(stderr,"error: %s\n", adb_error()); return 1; } return 0; } /* do_sync_*() commands */ if(!strcmp(argv[0], "ls")) { if(argc != 2) return usage(); return do_sync_ls(argv[1]); } if(!strcmp(argv[0], "push")) { if(argc != 3) return usage(); return do_sync_push(argv[1], argv[2], 0 /* no verify APK */); } if(!strcmp(argv[0], "pull")) { if (argc == 2) { return do_sync_pull(argv[1], "."); } else if (argc == 3) { return do_sync_pull(argv[1], argv[2]); } else { return usage(); } } if(!strcmp(argv[0], "install")) { if (argc < 2) return usage(); return install_app(ttype, serial, argc, argv); } if(!strcmp(argv[0], "uninstall")) { if (argc < 2) return usage(); return uninstall_app(ttype, serial, argc, argv); } if(!strcmp(argv[0], "sync")) { char *srcarg, *android_srcpath, *data_srcpath; int listonly = 0; int ret; if(argc < 2) { /* No local path was specified. */ srcarg = NULL; } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) { listonly = 1; if (argc == 3) { srcarg = argv[2]; } else { srcarg = NULL; } } else if(argc == 2) { /* A local path or "android"/"data" arg was specified. */ srcarg = argv[1]; } else { return usage(); } ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath); if(ret != 0) return usage(); if(android_srcpath != NULL) ret = do_sync_sync(android_srcpath, "/system", listonly); if(ret == 0 && data_srcpath != NULL) ret = do_sync_sync(data_srcpath, "/data", listonly); free(android_srcpath); free(data_srcpath); return ret; } /* passthrough commands */ if(!strcmp(argv[0],"get-state") || !strcmp(argv[0],"get-serialno")) { char *tmp; format_host_command(buf, sizeof buf, argv[0], ttype, serial); tmp = adb_query(buf); if(tmp) { printf("%s\n", tmp); return 0; } else { return 1; } } /* other commands */ if(!strcmp(argv[0],"status-window")) { status_window(ttype, serial); return 0; } if(!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat")) { return logcat(ttype, serial, argc, argv); } if(!strcmp(argv[0],"ppp")) { return ppp(argc, argv); } if (!strcmp(argv[0], "start-server")) { return adb_connect("host:start-server"); } if (!strcmp(argv[0], "jdwp")) { int fd = adb_connect("jdwp"); if (fd >= 0) { read_and_dump(fd); adb_close(fd); return 0; } else { fprintf(stderr, "error: %s\n", adb_error()); return -1; } } /* "adb /?" is a common idiom under Windows */ if(!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) { help(); return 0; } if(!strcmp(argv[0], "version")) { version(stdout); return 0; } usage(); return 1; }