Add Binder support to weaved and remove D-Bus interface
Added binder-based IPC to weaved instead of D-Bus. Removed the
old weave commands based on D-Bus and redesigned client library
interface to be more in line with how Binder operates.
BUG: 23782171, 25523591
Change-Id: Ic39a6a2edf2e033e506d233919c9d04e4fab8d01
diff --git a/common/binder_utils.cc b/common/binder_utils.cc
new file mode 100644
index 0000000..6f66040
--- /dev/null
+++ b/common/binder_utils.cc
@@ -0,0 +1,65 @@
+// Copyright 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 "common/binder_utils.h"
+
+#include <base/json/json_reader.h>
+#include <base/json/json_writer.h>
+
+namespace weaved {
+namespace binder_utils {
+
+android::binder::Status ToStatus(bool success, weave::ErrorPtr* error) {
+ if (success)
+ return android::binder::Status::ok();
+ return android::binder::Status::fromServiceSpecificError(
+ 1, android::String8{error->get()->GetMessage().c_str()});
+}
+
+bool StatusToError(android::binder::Status status, brillo::ErrorPtr* error) {
+ if (status.isOk())
+ return true;
+ brillo::Error::AddTo(error, FROM_HERE, "binder",
+ std::to_string(status.exceptionCode()),
+ status.exceptionMessage().string());
+ return false;
+}
+
+android::String16 ToString16(const base::Value& value) {
+ std::string json;
+ base::JSONWriter::Write(value, &json);
+ return ToString16(json);
+}
+
+android::binder::Status ParseDictionary(
+ const android::String16& json,
+ std::unique_ptr<base::DictionaryValue>* dict) {
+ int error = 0;
+ std::string message;
+ std::unique_ptr<base::Value> value{
+ base::JSONReader::ReadAndReturnError(ToString(json), base::JSON_PARSE_RFC,
+ &error, &message)
+ .release()};
+ base::DictionaryValue* dict_value = nullptr;
+ if (!value || !value->GetAsDictionary(&dict_value)) {
+ return android::binder::Status::fromServiceSpecificError(
+ error, android::String8{message.c_str()});
+ }
+ dict->reset(dict_value);
+ value.release(); // |dict| now owns the object.
+ return android::binder::Status::ok();
+}
+
+} // namespace binder_utils
+} // namespace weaved