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
20#include <sys/types.h>
21
22#include <string>
23
24// Sends the protocol "OKAY" message.
25bool SendOkay(int fd);
26
27// Sends the protocol "FAIL" message, with the given failure reason.
28bool SendFail(int fd, const std::string& reason);
29
30// Writes a protocol-format string; a four hex digit length followed by the string data.
31bool SendProtocolString(int fd, const std::string& s);
32
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 */
41bool 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 */
50bool WriteFdExactly(int fd, const void* buf, size_t len);
51
52// Same as above, but for strings.
53bool WriteFdExactly(int fd, const char* s);
54bool WriteFdExactly(int fd, const std::string& s);
55
56// Same as above, but formats the string to send.
57bool WriteFdFmt(int fd, const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
58
59#endif /* ADB_IO_H */
60