/* * Copyright (C) 2016 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. */ #include #include #include #include namespace nvram { // Descriptors for the message types. // IMPORTANT: The field numbers specified here correspond to protocol buffer // fields on the wire. While they are arbitrary, they should never be // reordered, reassigned, or overloaded once defined. template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &GetInfoResponse::total_size), MakeField(2, &GetInfoResponse::available_size), MakeField(3, &GetInfoResponse::max_spaces), MakeField(4, &GetInfoResponse::space_list), MakeField(5, &GetInfoResponse::max_space_size)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &CreateSpaceRequest::index), MakeField(2, &CreateSpaceRequest::size), MakeField(3, &CreateSpaceRequest::controls), MakeField(4, &CreateSpaceRequest::authorization_value)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &GetSpaceInfoRequest::index)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &GetSpaceInfoResponse::size), MakeField(2, &GetSpaceInfoResponse::controls), MakeField(3, &GetSpaceInfoResponse::read_locked), MakeField(4, &GetSpaceInfoResponse::write_locked)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &DeleteSpaceRequest::index), MakeField(2, &DeleteSpaceRequest::authorization_value)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &WriteSpaceRequest::index), MakeField(2, &WriteSpaceRequest::buffer), MakeField(3, &WriteSpaceRequest::authorization_value)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &ReadSpaceRequest::index), MakeField(2, &ReadSpaceRequest::authorization_value)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &ReadSpaceResponse::buffer)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &LockSpaceWriteRequest::index), MakeField(2, &LockSpaceWriteRequest::authorization_value)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(MakeField(1, &LockSpaceReadRequest::index), MakeField(2, &LockSpaceReadRequest::authorization_value)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList(); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList( MakeOneOfField(1, &Request::payload, COMMAND_GET_INFO), MakeOneOfField(2, &Request::payload, COMMAND_CREATE_SPACE), MakeOneOfField(3, &Request::payload, COMMAND_GET_SPACE_INFO), MakeOneOfField(4, &Request::payload, COMMAND_DELETE_SPACE), MakeOneOfField(5, &Request::payload, COMMAND_DISABLE_CREATE), MakeOneOfField(6, &Request::payload, COMMAND_WRITE_SPACE), MakeOneOfField(7, &Request::payload, COMMAND_READ_SPACE), MakeOneOfField(8, &Request::payload, COMMAND_LOCK_SPACE_WRITE), MakeOneOfField(9, &Request::payload, COMMAND_LOCK_SPACE_READ)); }; template<> struct DescriptorForType { static constexpr auto kFields = MakeFieldList( MakeField(1, &Response::result), MakeOneOfField(2, &Response::payload, COMMAND_GET_INFO), MakeOneOfField(3, &Response::payload, COMMAND_CREATE_SPACE), MakeOneOfField(4, &Response::payload, COMMAND_GET_SPACE_INFO), MakeOneOfField(5, &Response::payload, COMMAND_DELETE_SPACE), MakeOneOfField(6, &Response::payload, COMMAND_DISABLE_CREATE), MakeOneOfField(7, &Response::payload, COMMAND_WRITE_SPACE), MakeOneOfField(8, &Response::payload, COMMAND_READ_SPACE), MakeOneOfField(9, &Response::payload, COMMAND_LOCK_SPACE_WRITE), MakeOneOfField(10, &Response::payload, COMMAND_LOCK_SPACE_READ)); }; template bool Encode(const Message& msg, Blob* blob) { BlobOutputStreamBuffer stream(blob); return nvram::proto::Encode(msg, &stream) && stream.Truncate(); } template bool Encode(const Message& msg, void* buffer, size_t* size) { ArrayOutputStreamBuffer stream(buffer, *size); if (!nvram::proto::Encode(msg, &stream)) { return false; } *size = stream.bytes_written(); return true; } template bool Decode(const uint8_t* data, size_t size, Message* msg) { InputStreamBuffer stream(data, size); return nvram::proto::Decode(msg, &stream) && stream.Done(); } // Instantiate the templates for the |Request| and |Response| message types. template NVRAM_EXPORT bool Encode(const Request&, Blob*); template NVRAM_EXPORT bool Encode(const Request&, void*, size_t*); template NVRAM_EXPORT bool Decode(const uint8_t*, size_t, Request*); template NVRAM_EXPORT bool Encode(const Response&, Blob*); template NVRAM_EXPORT bool Encode(const Response&, void*, size_t*); template NVRAM_EXPORT bool Decode(const uint8_t*, size_t, Response*); } // namespace nvram