/* * * Copyright 2019, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef TEEUI_GENERICMESSAGES_H_ #define TEEUI_GENERICMESSAGES_H_ #include #include #include #include #include namespace teeui { enum class Command : uint32_t { Invalid, PromptUserConfirmation, FetchConfirmationResult, DeliverTestCommand, Abort, }; using Protocol = uint32_t; template struct Cmd {}; static constexpr const Protocol kProtoGeneric = 0; static constexpr const Protocol kProtoInvalid = std::numeric_limits::max(); #define DECLARE_GENERIC_COMMAND(cmd) using Cmd##cmd = Cmd DECLARE_GENERIC_COMMAND(PromptUserConfirmation); DECLARE_GENERIC_COMMAND(FetchConfirmationResult); DECLARE_GENERIC_COMMAND(DeliverTestCommand); DECLARE_GENERIC_COMMAND(Abort); using PromptUserConfirmationMsg = Message, MsgString, MsgVector>; using PromptUserConfirmationResponse = Message; using DeliverTestCommandMessage = Message; using DeliverTestCommandResponse = Message; using AbortMsg = Message; using ResultMsg = Message, MsgVector>; using FetchConfirmationResult = Message; template inline WriteStream write(WriteStream out, Cmd) { volatile Protocol* protoptr = reinterpret_cast(out.pos()); out += sizeof(Protocol); if (out) *protoptr = proto; volatile CmdT* cmdptr = reinterpret_cast(out.pos()); out += sizeof(CmdT); if (out) *cmdptr = cmd; return out; } template WriteStream write(Message, WriteStream out, const Tail&... tail) { out = write(out, CmdSpec()); return write(Message(), out, tail...); } template std::tuple read(Message, Fields...>, ReadStream in) { return read(Message(), in); } template struct msg2tuple, T...>> { using type = std::tuple; }; std::tuple readU32(ReadStream in); template std::tuple readCmd(ReadStream in) { static_assert(sizeof(CmdT) == sizeof(uint32_t), "Can only be used with types the size of uint32_t"); auto [stream, value] = readU32(in); if (stream) return {stream, CmdT(value)}; return {stream, Invalid}; } template CmdT peakCmd(ReadStream in) { auto [_, cmd] = readCmd(in); return cmd; } std::tuple readCommand(ReadStream in); Command peakCommand(ReadStream in); std::tuple readProtocol(ReadStream in); Protocol peakProtocol(ReadStream in); } // namespace teeui #endif // TEEUI_GENERICMESSAGES_H_