diff options
author | Ralph Nathan <ralphnathan@google.com> | 2015-12-03 23:56:01 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-12-03 23:56:01 +0000 |
commit | 7d031f277875cbc61f72584e958dc6e685338141 (patch) | |
tree | 11a8c85bd99eaafbff59e6198fe2091b27d72cfc | |
parent | 60e0251cc3c7037349d369d23af659a576c6b68f (diff) | |
parent | 763c33d2c58595cda69809531d2fc1ac9b2e8aa5 (diff) | |
download | common-7d031f277875cbc61f72584e958dc6e685338141.tar.gz |
Merge "Move dbus example from device/generic/brillo."
-rw-r--r-- | dbus_example/.gitignore | 7 | ||||
-rw-r--r-- | dbus_example/Android.mk | 42 | ||||
-rw-r--r-- | dbus_example/build.gradle | 52 | ||||
-rw-r--r-- | dbus_example/constants.h | 30 | ||||
-rw-r--r-- | dbus_example/dbus-example-client.cc | 66 | ||||
-rw-r--r-- | dbus_example/dbus-example-daemon.cc | 97 |
6 files changed, 294 insertions, 0 deletions
diff --git a/dbus_example/.gitignore b/dbus_example/.gitignore new file mode 100644 index 0000000..7bb275c --- /dev/null +++ b/dbus_example/.gitignore @@ -0,0 +1,7 @@ +.gradle +.idea +gradle +gradlew +gradlew.bat +local.properties +*.iml diff --git a/dbus_example/Android.mk b/dbus_example/Android.mk new file mode 100644 index 0000000..5b97f08 --- /dev/null +++ b/dbus_example/Android.mk @@ -0,0 +1,42 @@ +# +# Copyright (C) 2015 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. +# + +LOCAL_PATH := $(call my-dir) + +dbus_exampleCommonCppExtension := .cc +dbus_exampleCommonCFlags := -Wall -Werror -Wno-sign-promo \ + -Wno-unused-parameter +dbus_exampleCommonSharedLibraries := \ + libchrome \ + libchrome-dbus \ + libdbus \ + liblog \ + +include $(CLEAR_VARS) +LOCAL_MODULE := dbus-example-daemon +LOCAL_SRC_FILES := dbus-example-daemon.cc +LOCAL_SHARED_LIBRARIES := $(dbus_exampleCommonSharedLibraries) +LOCAL_CPP_EXTENSION := $(dbus_exampleCommonCppExtension) +LOCAL_CFLAGS := $(dbus_exampleCommonCFlags) +include $(BUILD_EXECUTABLE) + +include $(CLEAR_VARS) +LOCAL_MODULE := dbus-example-client +LOCAL_SRC_FILES := dbus-example-client.cc +LOCAL_SHARED_LIBRARIES := $(dbus_exampleCommonSharedLibraries) +LOCAL_CPP_EXTENSION := $(dbus_exampleCommonCppExtension) +LOCAL_CFLAGS := $(dbus_exampleCommonCFlags) +include $(BUILD_EXECUTABLE) diff --git a/dbus_example/build.gradle b/dbus_example/build.gradle new file mode 100644 index 0000000..4271050 --- /dev/null +++ b/dbus_example/build.gradle @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 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. + */ + +/* Return the android build root. */ +def gettop = { + File cwd = new File('.').absoluteFile + while (!(new File(cwd, 'build/core/envsetup.mk')).isFile()) { + cwd = cwd.parentFile + } + return cwd +} + +ext.android_build_root = gettop() +ext.nr_threads = Runtime.runtime.availableProcessors() + +/* Invoke an Android Build System command. */ +def call_abs(cmd) { + String full_cmd = 'source "' + android_build_root + '/build/envsetup.sh" && ' + cmd + println 'Calling ' + full_cmd + exec { + commandLine 'sh', '-c', full_cmd + } +} + +task build << { + call_abs 'mm -j ' + nr_threads +} + +task clean << { + call_abs 'm clean' +} + +task buildAll << { + call_abs 'm -j ' + nr_threads +} + +task emulator(type: Exec) { + commandLine 'emulator-arm' +} diff --git a/dbus_example/constants.h b/dbus_example/constants.h new file mode 100644 index 0000000..2286440 --- /dev/null +++ b/dbus_example/constants.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2015 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 DEVICE_GENERIC_BRILLO_EXAMPLES_DBUS_CONSTANTS_H_ +#define DEVICE_GENERIC_BRILLO_EXAMPLES_DBUS_CONSTANTS_H_ + +namespace dbus_example { + +// D-Bus-related constants. +static const char kServicePath[] = "/com/android/brillo/DBusExample"; +static const char kServiceName[] = "com.android.brillo.DBusExample"; +static const char kInterface[] = "com.android.brillo.DBusExample"; +static const char kMethodName[] = "Ping"; + +} // namespace dbus_example + +#endif // DEVICE_GENERIC_BRILLO_EXAMPLES_DBUS_CONSTANTS_H_ diff --git a/dbus_example/dbus-example-client.cc b/dbus_example/dbus-example-client.cc new file mode 100644 index 0000000..b28e2ba --- /dev/null +++ b/dbus_example/dbus-example-client.cc @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2015 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 <unistd.h> + +#include <dbus/bus.h> +#include <dbus/message.h> +#include <dbus/object_proxy.h> +#include <utils/Log.h> + +#include "constants.h" + +// Used implicitly. +#undef LOG_TAG +#define LOG_TAG "dbus-example-client" + +int main(int argc, char *argv[]) { + dbus::Bus::Options options; + options.bus_type = dbus::Bus::SYSTEM; + scoped_refptr<dbus::Bus> bus(new dbus::Bus(options)); + CHECK(bus->Connect()); + dbus::ObjectProxy* proxy = bus->GetObjectProxy( + dbus_example::kServiceName, dbus::ObjectPath(dbus_example::kServicePath)); + CHECK(proxy); + + int32_t token = 1; + while (true) { + dbus::MethodCall method_call(dbus_example::kInterface, + dbus_example::kMethodName); + dbus::MessageWriter writer(&method_call); + writer.AppendInt32(token); + + ALOGI("Calling %s with %d", dbus_example::kMethodName, token); + scoped_ptr<dbus::Response> response( + proxy->CallMethodAndBlock( + &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); + if (!response) { + ALOGE("Didn't receive response from server"); + } else { + int32_t response_token = 0; + dbus::MessageReader reader(response.get()); + if (!reader.PopInt32(&response_token)) + ALOGE("Missing token in response from server"); + else + ALOGI("Received %d", response_token); + } + + token++; + sleep(1); + } + + return 0; +} diff --git a/dbus_example/dbus-example-daemon.cc b/dbus_example/dbus-example-daemon.cc new file mode 100644 index 0000000..b04464a --- /dev/null +++ b/dbus_example/dbus-example-daemon.cc @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2015 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 <base/at_exit.h> +#include <base/bind.h> +#include <base/memory/ref_counted.h> +#include <base/memory/scoped_ptr.h> +#include <base/message_loop/message_loop.h> +#include <base/run_loop.h> +#include <dbus/bus.h> +#include <dbus/exported_object.h> +#include <dbus/message.h> +#include <utils/Log.h> + +#include "constants.h" + +// Used implicitly. +#undef LOG_TAG +#define LOG_TAG "dbus-example-daemon" + +namespace dbus_example { +namespace { + +class Daemon { + public: + Daemon() : dbus_object_(nullptr) {} + ~Daemon() {} + + void Init() { + ALOGI("Connecting to system bus"); + dbus::Bus::Options options; + options.bus_type = dbus::Bus::SYSTEM; + bus_ = new dbus::Bus(options); + CHECK(bus_->Connect()); + + ALOGI("Exporting method %s on %s", kMethodName, kServicePath); + dbus_object_ = bus_->GetExportedObject(dbus::ObjectPath(kServicePath)); + CHECK(dbus_object_->ExportMethodAndBlock( + kInterface, kMethodName, + base::Bind(&Daemon::HandleMethodCall, base::Unretained(this)))); + + ALOGI("Requesting ownership of %s", kServiceName); + CHECK(bus_->RequestOwnershipAndBlock(kServiceName, + dbus::Bus::REQUIRE_PRIMARY)); + } + + private: + void HandleMethodCall(dbus::MethodCall* method_call, + dbus::ExportedObject::ResponseSender response_sender) { + int32_t token = 0; + dbus::MessageReader reader(method_call); + if (!reader.PopInt32(&token)) { + ALOGE("Request didn't have token"); + response_sender.Run(scoped_ptr<dbus::Response>( + dbus::ErrorResponse::FromMethodCall(method_call, + DBUS_ERROR_INVALID_ARGS, "Expected token argument"))); + return; + } + + scoped_ptr<dbus::Response> response = + dbus::Response::FromMethodCall(method_call); + dbus::MessageWriter writer(response.get()); + writer.AppendInt32(token); + ALOGI("Replying to request with token %d", token); + response_sender.Run(response.Pass()); + } + + scoped_refptr<dbus::Bus> bus_; + dbus::ExportedObject* dbus_object_; // weak; owned by |bus_| + + DISALLOW_COPY_AND_ASSIGN(Daemon); +}; + +} // namespace +} // namespace dbus_example + +int main(int argc, char *argv[]) { + base::AtExitManager at_exit; + base::MessageLoopForIO loop; + dbus_example::Daemon daemon; + daemon.Init(); + base::RunLoop().Run(); + return 0; +} |