Add USB state publisher 15/141515/1
authorSomin Kim <somin926.kim@samsung.com>
Tue, 1 Aug 2017 02:18:03 +0000 (11:18 +0900)
committerSomin Kim <somin926.kim@samsung.com>
Tue, 1 Aug 2017 02:18:03 +0000 (11:18 +0900)
Change-Id: If94e569afb6f561a775130b1a67435168cb68547
Signed-off-by: Somin Kim <somin926.kim@samsung.com>
src/server/publisher/UsbState.cpp [new file with mode: 0644]

diff --git a/src/server/publisher/UsbState.cpp b/src/server/publisher/UsbState.cpp
new file mode 100644 (file)
index 0000000..9f5bfc0
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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 <system_info.h>
+#include <runtime_info.h>
+
+#include <ContextTypes.h>
+#include <JobSchedulerTypesPrivate.h>
+#include <job_scheduler_types_internal.h>
+#include "../ContextPublisher.h"
+#include "../IContextObserver.h"
+
+using namespace ctx;
+
+namespace {
+       class UsbState : public ContextPublisher {
+       public:
+               UsbState(uid_t uid);
+               ~UsbState();
+
+               const char* getUri() const;
+
+       protected:
+               void read();
+               void subscribe();
+               void unsubscribe();
+
+       private:
+               static void __changedCb(runtime_info_key_e infoKey, void* userData);
+       };
+}
+
+REGISTER_PUBLISHER(URI(USB), UsbState)
+
+UsbState::UsbState(uid_t uid)
+{
+       bool supported = false;
+       system_info_get_platform_bool("tizen.org/feature/usb.host", &supported);
+       IF_FAIL_THROW(supported, E_SUPPORT);
+
+       _D("Created");
+}
+
+UsbState::~UsbState()
+{
+       unsubscribe();
+
+       _D("Destroyed");
+}
+
+const char* UsbState::getUri() const
+{
+       return URI(USB);
+}
+
+void UsbState::read()
+{
+       bool connected = false;
+       int err = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &connected);
+
+       if (IS_FAILED(err))
+               _E("Getting runtime information failed (%d)", err);
+
+       __data[NAME(IS_CONNECTED)] = (connected ? VALUE(TRUE) : VALUE(FALSE));
+}
+
+void UsbState::subscribe()
+{
+       int err = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED, __changedCb, this);
+
+       if (IS_FAILED(err))
+               _E("Setting changed callback failed (%d)", err);
+}
+
+void UsbState::unsubscribe()
+{
+       runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED);
+}
+
+void UsbState::__changedCb(runtime_info_key_e infoKey, void* userData)
+{
+       UsbState* pubs = static_cast<UsbState*>(userData);
+       pubs->read();
+       pubs->notifyObservers();
+}