Add message declarations for access-controlled NVRAM.
am: 38899ed684

* commit '38899ed6848e3d9d81a3e0a87e3798163fe664ec':
  Add message declarations for access-controlled NVRAM.
diff --git a/Android.mk b/Android.mk
index 27d0edc..e262057 100644
--- a/Android.mk
+++ b/Android.mk
@@ -21,7 +21,8 @@
 LOCAL_SRC_FILES := \
 	blob.cpp \
 	io.cpp \
-	message_codec.cpp
+	message_codec.cpp \
+	nvram_messages.cpp
 LOCAL_STATIC_LIBRARIES := libbase
 LOCAL_CFLAGS := -Wall -Werror -Wextra -fvisibility=hidden
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
diff --git a/include/nvram/nvram_messages.h b/include/nvram/nvram_messages.h
new file mode 100644
index 0000000..77647eb
--- /dev/null
+++ b/include/nvram/nvram_messages.h
@@ -0,0 +1,171 @@
+/*
+ * 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.
+ */
+
+#ifndef NVRAM_NVRAM_MESSAGES_H_
+#define NVRAM_NVRAM_MESSAGES_H_
+
+#include <hardware/nvram_defs.h>
+
+#include <nvram/blob.h>
+#include <nvram/compiler.h>
+#include <nvram/struct.h>
+#include <nvram/tagged_union.h>
+#include <nvram/vector.h>
+
+namespace nvram {
+
+enum Command {
+  COMMAND_GET_INFO = 1,
+  COMMAND_CREATE_SPACE = 2,
+  COMMAND_GET_SPACE_INFO = 3,
+  COMMAND_DELETE_SPACE = 4,
+  COMMAND_DISABLE_CREATE = 5,
+  COMMAND_WRITE_SPACE = 6,
+  COMMAND_READ_SPACE = 7,
+  COMMAND_LOCK_SPACE_WRITE = 8,
+  COMMAND_LOCK_SPACE_READ = 9,
+};
+
+// COMMAND_GET_INFO request/response.
+struct GetInfoRequest {};
+
+struct GetInfoResponse {
+  uint64_t total_size = 0;
+  uint64_t available_size = 0;
+  uint32_t max_spaces = 0;
+  Vector<uint32_t> space_list;
+};
+
+// COMMAND_CREATE_SPACE request/response.
+struct CreateSpaceRequest {
+  uint32_t index = 0;
+  uint64_t size = 0;
+  Vector<nvram_control_t> controls;
+  Blob authorization_value;
+};
+
+struct CreateSpaceResponse {};
+
+// COMMAND_GET_SPACE_INFO request/response.
+struct GetSpaceInfoRequest {
+  uint32_t index = 0;
+};
+
+struct GetSpaceInfoResponse {
+  uint64_t size = 0;
+  Vector<nvram_control_t> controls;
+  bool read_locked = false;
+  bool write_locked = false;
+};
+
+// COMMAND_DELETE_SPACE request/response.
+struct DeleteSpaceRequest {
+  uint32_t index = 0;
+  Blob authorization_value;
+};
+
+struct DeleteSpaceResponse {};
+
+// COMMAND_DISABLE_CREATE request/response.
+struct DisableCreateRequest {};
+
+struct DisableCreateResponse {};
+
+// COMMAND_WRITE_SPACE request/response.
+struct WriteSpaceRequest {
+  uint32_t index = 0;
+  Blob buffer;
+  Blob authorization_value;
+};
+
+struct WriteSpaceResponse {};
+
+// COMMAND_READ_SPACE request/response.
+struct ReadSpaceRequest {
+  uint32_t index = 0;
+  Blob authorization_value;
+};
+
+struct ReadSpaceResponse {
+  Blob buffer;
+};
+
+// COMMAND_LOCK_SPACE_WRITE request/response.
+struct LockSpaceWriteRequest {
+  uint32_t index = 0;
+  Blob authorization_value;
+};
+
+struct LockSpaceWriteResponse {};
+
+// COMMAND_LOCK_SPACE_READ request/response.
+struct LockSpaceReadRequest {
+  uint32_t index = 0;
+  Blob authorization_value;
+};
+
+struct LockSpaceReadResponse {};
+
+// Generic request message, carrying command-specific payload. The slot set in
+// the payload determines the requested command.
+using RequestUnion = TaggedUnion<
+    Command,
+    TaggedUnionMember<COMMAND_GET_INFO, GetInfoRequest>,
+    TaggedUnionMember<COMMAND_CREATE_SPACE, CreateSpaceRequest>,
+    TaggedUnionMember<COMMAND_GET_SPACE_INFO, GetSpaceInfoRequest>,
+    TaggedUnionMember<COMMAND_DELETE_SPACE, DeleteSpaceRequest>,
+    TaggedUnionMember<COMMAND_DISABLE_CREATE, DisableCreateRequest>,
+    TaggedUnionMember<COMMAND_WRITE_SPACE, WriteSpaceRequest>,
+    TaggedUnionMember<COMMAND_READ_SPACE, ReadSpaceRequest>,
+    TaggedUnionMember<COMMAND_LOCK_SPACE_WRITE, LockSpaceWriteRequest>,
+    TaggedUnionMember<COMMAND_LOCK_SPACE_READ, LockSpaceReadRequest>>;
+struct Request {
+  RequestUnion payload;
+};
+
+// Generic response message, carrying a result code and command-specific
+// payload.
+using ResponseUnion = TaggedUnion<
+    Command,
+    TaggedUnionMember<COMMAND_GET_INFO, GetInfoResponse>,
+    TaggedUnionMember<COMMAND_CREATE_SPACE, CreateSpaceResponse>,
+    TaggedUnionMember<COMMAND_GET_SPACE_INFO, GetSpaceInfoResponse>,
+    TaggedUnionMember<COMMAND_DELETE_SPACE, DeleteSpaceResponse>,
+    TaggedUnionMember<COMMAND_DISABLE_CREATE, DisableCreateResponse>,
+    TaggedUnionMember<COMMAND_WRITE_SPACE, WriteSpaceResponse>,
+    TaggedUnionMember<COMMAND_READ_SPACE, ReadSpaceResponse>,
+    TaggedUnionMember<COMMAND_LOCK_SPACE_WRITE, LockSpaceWriteResponse>,
+    TaggedUnionMember<COMMAND_LOCK_SPACE_READ, LockSpaceReadResponse>>;
+struct Response {
+  nvram_result_t result = NV_RESULT_SUCCESS;
+  ResponseUnion payload;
+};
+
+// Encoding and decoding functions. Template instantiations are provided for the
+// |Request| and |Response| wrapper types declared above.
+
+// Encode |msg| to |blob|. Returns true if successful.
+template <typename Message>
+bool Encode(const Message& msg, Blob* blob);
+
+// Decode |msg| from the |data| buffer, which contains |size| bytes. Returns
+// true if successful.
+template <typename Message>
+bool Decode(const uint8_t* data, size_t size, Message* msg);
+
+}  // namespace nvram
+
+#endif  // NVRAM_NVRAM_MESSAGES_H_
diff --git a/nvram_messages.cpp b/nvram_messages.cpp
new file mode 100644
index 0000000..97c618f
--- /dev/null
+++ b/nvram_messages.cpp
@@ -0,0 +1,169 @@
+/*
+ * 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 <nvram/nvram_messages.h>
+
+#include <nvram/blob.h>
+#include <nvram/io.h>
+#include <nvram/proto.hpp>
+
+namespace nvram {
+
+// Descriptors for the message types.
+template<> struct DescriptorForType<GetInfoRequest> {
+  static constexpr auto kFields = MakeFieldList();
+};
+
+template<> struct DescriptorForType<GetInfoResponse> {
+  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));
+};
+
+template<> struct DescriptorForType<CreateSpaceRequest> {
+  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<CreateSpaceResponse> {
+  static constexpr auto kFields = MakeFieldList();
+};
+
+template<> struct DescriptorForType<GetSpaceInfoRequest> {
+  static constexpr auto kFields =
+      MakeFieldList(MakeField(1, &GetSpaceInfoRequest::index));
+};
+
+template<> struct DescriptorForType<GetSpaceInfoResponse> {
+  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<DeleteSpaceRequest> {
+  static constexpr auto kFields =
+      MakeFieldList(MakeField(1, &DeleteSpaceRequest::index),
+                    MakeField(2, &DeleteSpaceRequest::authorization_value));
+};
+
+template<> struct DescriptorForType<DeleteSpaceResponse> {
+  static constexpr auto kFields = MakeFieldList();
+};
+
+template<> struct DescriptorForType<DisableCreateRequest> {
+  static constexpr auto kFields = MakeFieldList();
+};
+
+template<> struct DescriptorForType<DisableCreateResponse> {
+  static constexpr auto kFields = MakeFieldList();
+};
+
+template<> struct DescriptorForType<WriteSpaceRequest> {
+  static constexpr auto kFields =
+      MakeFieldList(MakeField(1, &WriteSpaceRequest::index),
+                    MakeField(2, &WriteSpaceRequest::buffer),
+                    MakeField(3, &WriteSpaceRequest::authorization_value));
+};
+
+template<> struct DescriptorForType<WriteSpaceResponse> {
+  static constexpr auto kFields = MakeFieldList();
+};
+
+template<> struct DescriptorForType<ReadSpaceRequest> {
+  static constexpr auto kFields =
+      MakeFieldList(MakeField(1, &ReadSpaceRequest::index),
+                    MakeField(2, &ReadSpaceRequest::authorization_value));
+};
+
+template<> struct DescriptorForType<ReadSpaceResponse> {
+  static constexpr auto kFields =
+      MakeFieldList(MakeField(1, &ReadSpaceResponse::buffer));
+};
+
+template<> struct DescriptorForType<LockSpaceWriteRequest> {
+  static constexpr auto kFields =
+      MakeFieldList(MakeField(1, &LockSpaceWriteRequest::index),
+                    MakeField(2, &LockSpaceWriteRequest::authorization_value));
+};
+
+template<> struct DescriptorForType<LockSpaceWriteResponse> {
+  static constexpr auto kFields = MakeFieldList();
+};
+
+template<> struct DescriptorForType<LockSpaceReadRequest> {
+  static constexpr auto kFields =
+      MakeFieldList(MakeField(1, &LockSpaceReadRequest::index),
+                    MakeField(2, &LockSpaceReadRequest::authorization_value));
+};
+
+template<> struct DescriptorForType<LockSpaceReadResponse> {
+  static constexpr auto kFields = MakeFieldList();
+};
+
+template<> struct DescriptorForType<Request> {
+  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<Response> {
+  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 <typename Message>
+bool Encode(const Message& msg, Blob* blob) {
+  BlobOutputStreamBuffer stream(blob);
+  return nvram::proto::Encode(msg, &stream) && stream.Truncate();
+}
+
+template <typename Message>
+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<Request>(const Request&, Blob*);
+template NVRAM_EXPORT bool Decode<Request>(const uint8_t*, size_t, Request*);
+
+template NVRAM_EXPORT bool Encode<Response>(const Response&, Blob*);
+template NVRAM_EXPORT bool Decode<Response>(const uint8_t*, size_t, Response*);
+
+}  // namespace nvram