| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 The Android Open Source Project | 
|  | 3 | * | 
|  | 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 | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 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. | 
|  | 15 | */ | 
|  | 16 |  | 
| Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_RWX | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 18 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 19 | #include "adb_io.h" | 
|  | 20 |  | 
|  | 21 | #include <unistd.h> | 
|  | 22 |  | 
| Elliott Hughes | e1a5500 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 23 | #include <base/stringprintf.h> | 
|  | 24 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 25 | #include "adb_trace.h" | 
| Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 26 | #include "adb_utils.h" | 
| Elliott Hughes | e1a5500 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 27 | #include "sysdeps.h" | 
| Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | bool SendProtocolString(int fd, const std::string& s) { | 
|  | 30 | int length = s.size(); | 
|  | 31 | if (length > 0xffff) { | 
|  | 32 | length = 0xffff; | 
|  | 33 | } | 
|  | 34 |  | 
| Elliott Hughes | e1a5500 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 35 | return WriteFdFmt(fd, "%04x", length) && WriteFdExactly(fd, s); | 
| Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
|  | 38 | bool SendOkay(int fd) { | 
|  | 39 | return WriteFdExactly(fd, "OKAY", 4); | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | bool SendFail(int fd, const std::string& reason) { | 
|  | 43 | return WriteFdExactly(fd, "FAIL", 4) && SendProtocolString(fd, reason); | 
|  | 44 | } | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 45 |  | 
|  | 46 | bool ReadFdExactly(int fd, void* buf, size_t len) { | 
|  | 47 | char* p = reinterpret_cast<char*>(buf); | 
|  | 48 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 49 | size_t len0 = len; | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | D("readx: fd=%d wanted=%zu\n", fd, len); | 
|  | 52 | while (len > 0) { | 
| Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 53 | int r = adb_read(fd, p, len); | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 54 | if (r > 0) { | 
|  | 55 | len -= r; | 
|  | 56 | p += r; | 
|  | 57 | } else if (r == -1) { | 
|  | 58 | D("readx: fd=%d error %d: %s\n", fd, errno, strerror(errno)); | 
|  | 59 | return false; | 
|  | 60 | } else { | 
|  | 61 | D("readx: fd=%d disconnected\n", fd); | 
|  | 62 | errno = 0; | 
|  | 63 | return false; | 
|  | 64 | } | 
|  | 65 | } | 
|  | 66 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 67 | D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len); | 
|  | 68 | if (ADB_TRACING) { | 
|  | 69 | dump_hex(reinterpret_cast<const unsigned char*>(buf), len0); | 
|  | 70 | } | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 71 |  | 
|  | 72 | return true; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | bool WriteFdExactly(int fd, const void* buf, size_t len) { | 
|  | 76 | const char* p = reinterpret_cast<const char*>(buf); | 
|  | 77 | int r; | 
|  | 78 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 79 | D("writex: fd=%d len=%d: ", fd, (int)len); | 
|  | 80 | if (ADB_TRACING) { | 
|  | 81 | dump_hex(reinterpret_cast<const unsigned char*>(buf), len); | 
|  | 82 | } | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 83 |  | 
|  | 84 | while (len > 0) { | 
| Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 85 | r = adb_write(fd, p, len); | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 86 | if (r == -1) { | 
|  | 87 | D("writex: fd=%d error %d: %s\n", fd, errno, strerror(errno)); | 
|  | 88 | if (errno == EAGAIN) { | 
|  | 89 | adb_sleep_ms(1); // just yield some cpu time | 
|  | 90 | continue; | 
|  | 91 | } else if (errno == EPIPE) { | 
|  | 92 | D("writex: fd=%d disconnected\n", fd); | 
|  | 93 | errno = 0; | 
|  | 94 | return false; | 
| Dan Albert | 08f66bc | 2015-03-19 18:09:59 -0700 | [diff] [blame] | 95 | } else { | 
|  | 96 | return false; | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 97 | } | 
|  | 98 | } else { | 
|  | 99 | len -= r; | 
|  | 100 | p += r; | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 | return true; | 
|  | 104 | } | 
|  | 105 |  | 
| Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 106 | bool WriteFdExactly(int fd, const char* str) { | 
|  | 107 | return WriteFdExactly(fd, str, strlen(str)); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | bool WriteFdExactly(int fd, const std::string& str) { | 
|  | 111 | return WriteFdExactly(fd, str.c_str(), str.size()); | 
|  | 112 | } | 
|  | 113 |  | 
| Elliott Hughes | e1a5500 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 114 | bool WriteFdFmt(int fd, const char* fmt, ...) { | 
|  | 115 | std::string str; | 
|  | 116 |  | 
|  | 117 | va_list ap; | 
|  | 118 | va_start(ap, fmt); | 
|  | 119 | android::base::StringAppendV(&str, fmt, ap); | 
|  | 120 | va_end(ap); | 
|  | 121 |  | 
|  | 122 | return WriteFdExactly(fd, str); | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 123 | } |