| 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 |  | 
|  | 17 | #ifndef ADB_IO_H | 
|  | 18 | #define ADB_IO_H | 
|  | 19 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 20 | #include <sys/types.h> | 
|  | 21 |  | 
| Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 22 | #include <string> | 
|  | 23 |  | 
|  | 24 | // Sends the protocol "OKAY" message. | 
|  | 25 | bool SendOkay(int fd); | 
|  | 26 |  | 
|  | 27 | // Sends the protocol "FAIL" message, with the given failure reason. | 
|  | 28 | bool SendFail(int fd, const std::string& reason); | 
|  | 29 |  | 
|  | 30 | // Writes a protocol-format string; a four hex digit length followed by the string data. | 
|  | 31 | bool SendProtocolString(int fd, const std::string& s); | 
|  | 32 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 33 | /* | 
|  | 34 | * Reads exactly len bytes from fd into buf. | 
|  | 35 | * | 
|  | 36 | * Returns false if there is an error or if EOF was reached before len bytes | 
|  | 37 | * were read. If EOF was found, errno will be set to 0. | 
|  | 38 | * | 
|  | 39 | * If this function fails, the contents of buf are undefined. | 
|  | 40 | */ | 
|  | 41 | bool ReadFdExactly(int fd, void *buf, size_t len); | 
|  | 42 |  | 
|  | 43 | /* | 
|  | 44 | * Writes exactly len bytes from buf to fd. | 
|  | 45 | * | 
|  | 46 | * Returns false if there is an error or if the fd was closed before the write | 
|  | 47 | * completed. If the other end of the fd (such as in a socket, pipe, or fifo), | 
|  | 48 | * is closed, errno will be set to 0. | 
|  | 49 | */ | 
| Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 50 | bool WriteFdExactly(int fd, const void* buf, size_t len); | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 51 |  | 
| Elliott Hughes | e1a5500 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 52 | // Same as above, but for strings. | 
| Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 53 | bool WriteFdExactly(int fd, const char* s); | 
|  | 54 | bool WriteFdExactly(int fd, const std::string& s); | 
|  | 55 |  | 
| Elliott Hughes | e1a5500 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 56 | // Same as above, but formats the string to send. | 
|  | 57 | bool WriteFdFmt(int fd, const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))); | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 58 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 59 | #endif /* ADB_IO_H */ |