Android-cuttlefish cvd tool
common_message_types.h
Go to the documentation of this file.
1/*
2 *
3 * Copyright 2019, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef TEEUI_COMMONMESSAGETYPES_H_
19#define TEEUI_COMMONMESSAGETYPES_H_
20
22
23#include <tuple>
24
25#include <stdint.h>
26
27#include <teeui/static_vec.h>
28
29namespace teeui {
30
31enum class UIOption : uint32_t;
32enum class ResponseCode : uint32_t;
33enum class MessageSize : uint32_t;
34enum class TestKeyBits : uint8_t;
35enum class TestModeCommands : uint64_t;
36
37enum class UIOption : uint32_t {
40};
41
42enum class ResponseCode : uint32_t {
43 OK = 0u,
44 Canceled = 1u,
45 Aborted = 2u,
47 Ignored = 4u,
48 SystemError = 5u,
49 Unimplemented = 6u,
50 Unexpected = 7u,
51 UIError = 0x10000,
55};
56
57enum class MessageSize : uint32_t {
58 MAX = 6144u,
59};
60
61enum class TestKeyBits : uint8_t {
62 BYTE = 165,
63};
64
65enum class TestModeCommands : uint64_t {
66 OK_EVENT = 0ull,
67 CANCEL_EVENT = 1ull,
68};
69
71template <typename T> using MsgVector = static_vec<T>;
72
73template <typename T> inline const uint8_t* copyField(T& field, const uint8_t*(&pos)) {
74 auto& s = bytesCast(field);
75 std::copy(pos, pos + sizeof(T), s);
76 return pos + sizeof(T);
77}
78
79template <typename T> inline uint8_t* copyField(const T& field, uint8_t*(&pos)) {
80 auto& s = bytesCast(field);
81 return std::copy(s, &s[sizeof(T)], pos);
82}
83
89template <typename T> std::tuple<ReadStream, MsgVector<T>> readSimpleVecInPlace(ReadStream in) {
90 std::tuple<ReadStream, MsgVector<T>> result;
91 ReadStream::ptr_t pos = nullptr;
92 size_t read_size = 0;
93 std::tie(std::get<0>(result), pos, read_size) = read(in);
94 if (!std::get<0>(result) || read_size % sizeof(T)) {
95 std::get<0>(result).bad();
96 return result;
97 }
98 std::get<1>(result) =
99 MsgVector<T>(reinterpret_cast<T*>(const_cast<uint8_t*>(pos)),
100 reinterpret_cast<T*>(const_cast<uint8_t*>(pos)) + (read_size / sizeof(T)));
101 return result;
102}
103
104template <typename T> WriteStream writeSimpleVec(WriteStream out, const MsgVector<T>& vec) {
105 return write(out, reinterpret_cast<const uint8_t*>(vec.data()), vec.size() * sizeof(T));
106}
107
108// ResponseCode
109inline std::tuple<ReadStream, ResponseCode> read(Message<ResponseCode>, ReadStream in) {
110 return readSimpleType<ResponseCode>(in);
111}
113 return write(out, bytesCast(v));
114}
115
116// TestModeCommands
117inline std::tuple<ReadStream, TestModeCommands> read(Message<TestModeCommands>, ReadStream in) {
118 return readSimpleType<TestModeCommands>(in);
119}
121 return write(out, bytesCast(v));
122}
123
124namespace msg {
125
126// MsgVector<uint8_t>
127inline std::tuple<ReadStream, MsgVector<uint8_t>> read(Message<MsgVector<uint8_t>>, ReadStream in) {
128 return readSimpleVecInPlace<uint8_t>(in);
129}
131 return writeSimpleVec(out, v);
132}
133
134// MsgString
135inline std::tuple<ReadStream, MsgString> read(Message<MsgString>, ReadStream in) {
136 return readSimpleVecInPlace<char>(in);
137}
138inline WriteStream write(WriteStream out, const MsgString& v) {
139 return writeSimpleVec(out, v);
140}
141
142// MsgVector<UIOption>
143inline std::tuple<ReadStream, MsgVector<UIOption>> read(Message<MsgVector<UIOption>>,
144 ReadStream in) {
145 return readSimpleVecInPlace<UIOption>(in);
146}
148 return writeSimpleVec(out, v);
149}
150
151} // namespace msg
152
153// teeui::Array<uint8_t, size>
154template <size_t size>
155inline std::tuple<teeui::ReadStream, teeui::Array<uint8_t, size>>
157 std::tuple<teeui::ReadStream, teeui::Array<uint8_t, size>> result;
158 teeui::ReadStream& in_ = std::get<0>(result);
159 auto& result_ = std::get<1>(result);
160 teeui::ReadStream::ptr_t pos = nullptr;
161 size_t read_size = 0;
162 std::tie(in_, pos, read_size) = read(in);
163 if (!in_) return result;
164 if (read_size != size) {
165 in_.bad();
166 return result;
167 }
168 std::copy(pos, pos + size, result_.data());
169 return result;
170}
171template <size_t size>
173 return write(out, v.data(), v.size());
174}
175
176} // namespace teeui
177
178#endif // TEEUI_COMMONMESSAGETYPES_H_
Definition: utils.h:39
constexpr size_t size() const
Definition: utils.h:59
T * data()
Definition: utils.h:57
Definition: msg_formatting.h:33
Definition: msg_formatting.h:53
Definition: static_vec.h:40
size_t size() const
Definition: static_vec.h:56
T * data()
Definition: static_vec.h:54
uint32_t size
Definition: io.h:2
std::tuple< ReadStream, MsgVector< uint8_t > > read(Message< MsgVector< uint8_t > >, ReadStream in)
Definition: common_message_types.h:127
WriteStream write(WriteStream out, const MsgVector< uint8_t > &v)
Definition: common_message_types.h:130
StreamState< const uint8_t > ReadStream
Definition: msg_formatting.h:110
StreamState< uint8_t > WriteStream
Definition: msg_formatting.h:109
Definition: layout.h:28
std::tuple< ReadStream, ResponseCode > read(Message< ResponseCode >, ReadStream in)
Definition: common_message_types.h:109
ResponseCode
Definition: common_message_types.h:42
TestKeyBits
Definition: common_message_types.h:61
std::tuple< ReadStream, MsgVector< T > > readSimpleVecInPlace(ReadStream in)
Definition: common_message_types.h:89
WriteStream writeSimpleVec(WriteStream out, const MsgVector< T > &vec)
Definition: common_message_types.h:104
UIOption
Definition: common_message_types.h:37
WriteStream write(WriteStream out, const ResponseCode &v)
Definition: common_message_types.h:112
const uint8_t * copyField(T &field, const uint8_t *(&pos))
Definition: common_message_types.h:73
auto bytesCast(const T &v) -> const uint8_t(&)[sizeof(T)]
Definition: utils.h:78
MessageSize
Definition: common_message_types.h:57
TestModeCommands
Definition: common_message_types.h:65