blob: bd3b86977fde4e31a2d24cd00429b3456c71e9e4 [file] [log] [blame]
Dan Albertcc731cc2015-02-24 21:26:58 -08001/*
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 Albertcc731cc2015-02-24 21:26:58 -080020#include <sys/types.h>
21
Elliott Hughese67f1f82015-04-30 17:32:03 -070022#include <string>
23
Dan Albert459df8f2015-07-08 13:50:42 -070024#include "base/macros.h"
25
Elliott Hughese67f1f82015-04-30 17:32:03 -070026// Sends the protocol "OKAY" message.
27bool SendOkay(int fd);
28
29// Sends the protocol "FAIL" message, with the given failure reason.
30bool SendFail(int fd, const std::string& reason);
31
32// Writes a protocol-format string; a four hex digit length followed by the string data.
33bool SendProtocolString(int fd, const std::string& s);
34
Dan Albertcc731cc2015-02-24 21:26:58 -080035/*
36 * Reads exactly len bytes from fd into buf.
37 *
38 * Returns false if there is an error or if EOF was reached before len bytes
39 * were read. If EOF was found, errno will be set to 0.
40 *
41 * If this function fails, the contents of buf are undefined.
42 */
43bool ReadFdExactly(int fd, void *buf, size_t len);
44
45/*
46 * Writes exactly len bytes from buf to fd.
47 *
48 * Returns false if there is an error or if the fd was closed before the write
49 * completed. If the other end of the fd (such as in a socket, pipe, or fifo),
50 * is closed, errno will be set to 0.
51 */
Elliott Hughese67f1f82015-04-30 17:32:03 -070052bool WriteFdExactly(int fd, const void* buf, size_t len);
Dan Albertcc731cc2015-02-24 21:26:58 -080053
Elliott Hughesab52c182015-05-01 17:04:38 -070054// Same as above, but for strings.
Elliott Hughese67f1f82015-04-30 17:32:03 -070055bool WriteFdExactly(int fd, const char* s);
56bool WriteFdExactly(int fd, const std::string& s);
57
Elliott Hughesab52c182015-05-01 17:04:38 -070058// Same as above, but formats the string to send.
Dan Albert459df8f2015-07-08 13:50:42 -070059bool WriteFdFmt(int fd, const char* fmt, ...) ATTRIBUTE_FORMAT(2, 3);
Dan Albertcc731cc2015-02-24 21:26:58 -080060
Dan Albertcc731cc2015-02-24 21:26:58 -080061#endif /* ADB_IO_H */