* limitations under the License.
*/
-#include <bundle.h>
#include <stdlib.h>
#include <stdio.h>
-#include <vector>
-#include <string>
-#include <unordered_map>
-
#include "amd_config.h"
#include "amd_util.h"
#include "amd_noti.h"
-
-namespace {
-
-std::unordered_map<std::string, std::vector<noti_cb>> __listeners;
-
-} // namespace
+#include "api/amd_api_noti.hh"
int _noti_send(const char* msg, int arg1, int arg2, void* arg3, bundle* data) {
- int ret;
-
- if (!msg) {
- _E("Invalid parameter");
- return -1;
- }
-
- auto iter = __listeners.find(msg);
- if (iter == __listeners.end())
- return 0;
-
- auto& li = iter->second;
- for (noti_cb noti_callback : li) {
- if (noti_callback) {
- ret = noti_callback(msg, arg1, arg2, arg3, data);
- if (ret != NOTI_CONTINUE)
- return -1;
- }
- }
-
- return 0;
+ return amd::api::Noti::Send({msg, arg1, arg2, arg3, data});
}
int _noti_listen(const char* msg, noti_cb callback) {
- if (!msg) {
- _E("Invalid parameter");
- return -1;
- }
- auto& li = __listeners[msg];
- li.push_back(callback);
- return 0;
+ return amd::api::Noti::Listen(msg, callback);
}
int _noti_init(void) {
}
void _noti_fini(void) {
- __listeners.clear();
-}
+ amd::api::Noti::Dispose();
+}
\ No newline at end of file
*/
#include "lib/amd_api.h"
-#include "lib/amd_noti.h"
+#include "lib/amd_util.h"
#include "lib/api/amd_api_noti.h"
+#include "lib/api/amd_api_noti.hh"
+
+namespace amd {
+namespace api {
+
+namespace {
+
+std::unordered_map<std::string,
+ std::vector<std::pair<Noti::NotiCb, noti_cb>>> listeners_;
+
+} // namespace
+
+class Noti::Msg::Impl {
+ public:
+ Impl(std::string msg, int val1 = 0, int val2 = 0, void* val3 = nullptr)
+ : msg_(std::move(msg)), val1_(val1), val2_(val2), val3_(val3) {}
+ Impl(std::string msg, int val1, int val2, void* val3,
+ tizen_base::Bundle data)
+ : msg_(std::move(msg)), val1_(val1), val2_(val2), val3_(val3),
+ data_(std::move(data)) {}
+ Impl(std::string msg, int val1, int val2, void* val3,
+ const bundle* data)
+ : msg_(std::move(msg)), val1_(val1), val2_(val2), val3_(val3),
+ data_(data ? tizen_base::Bundle(const_cast<bundle*>(data),
+ false, false) : tizen_base::Bundle()) {}
+
+ private:
+ friend class Noti::Msg;
+ std::string msg_;
+ int val1_;
+ int val2_;
+ void* val3_;
+ tizen_base::Bundle data_;
+};
+
+Noti::Msg::Msg(std::string msg, int val1, int val2, void* val3)
+ : impl_(std::make_shared<Noti::Msg::Impl>(std::move(msg), val1, val2,
+ val3)) {}
+
+Noti::Msg::Msg(std::string msg, int val1, int val2, void* val3,
+ tizen_base::Bundle data)
+ : impl_(std::make_shared<Noti::Msg::Impl>(
+ std::move(msg), val1, val2, val3, std::move(data))) {}
+
+Noti::Msg::Msg(std::string msg, int val1, int val2, void* val3,
+ const bundle* data)
+ : impl_(std::make_shared<Noti::Msg::Impl>(
+ std::move(msg), val1, val2, val3, data)) {}
+
+const std::string& Noti::Msg::GetMessage() const {
+ return impl_->msg_;
+}
+
+const tizen_base::Bundle& Noti::Msg::GetBundle() const {
+ return impl_->data_;
+}
+
+int Noti::Msg::GetArg1() const {
+ return impl_->val1_;
+}
+
+int Noti::Msg::GetArg2() const {
+ return impl_->val2_;
+}
+
+void* Noti::Msg::GetArg3() const {
+ return impl_->val3_;
+}
+
+int Noti::Listen(const std::string& msg, Noti::NotiCb cb) {
+ if (msg.empty()) {
+ _E("Invalid parameter");
+ return -1;
+ }
+ auto& li = listeners_[msg];
+ li.push_back({ cb, nullptr });
+ return 0;
+}
+
+int Noti::Listen(const std::string& msg, noti_cb cb) {
+ if (msg.empty()) {
+ _E("Invalid parameter");
+ return -1;
+ }
+
+ auto& li = listeners_[msg];
+ li.push_back({ nullptr, cb });
+ return 0;
+}
+
+int Noti::Send(Noti::Msg msg) {
+ if (msg.GetMessage().empty()) {
+ _E("Invalid parameter");
+ return -1;
+ }
+
+ auto iter = listeners_.find(msg.GetMessage());
+ if (iter == listeners_.end())
+ return 0;
+
+ auto& li = iter->second;
+ for (auto& cb : li) {
+ if (cb.first) {
+ int ret = cb.first(msg);
+ if (ret != NOTI_CONTINUE)
+ return -1;
+ } else {
+ auto [arg1, arg2, arg3] = GetNotiArgs<int, int, void*>(msg);
+ int ret = cb.second(msg.GetMessage().c_str(), arg1, arg2, arg3,
+ msg.GetBundle().GetHandle());
+ if (ret != NOTI_CONTINUE)
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+void Noti::Dispose() {
+ listeners_.clear();
+}
+
+} // namespace api
+} // namespace amd
extern "C" EXPORT_API int amd_noti_send(const char *msg, int arg1, int arg2,
void* arg3, bundle* data) {
- return _noti_send(msg, arg1, arg2, arg3, data);
+ return amd::api::Noti::Send({msg, arg1, arg2, arg3, data});
}
extern "C" EXPORT_API int amd_noti_listen(const char* msg,
amd_noti_cb callback) {
- return _noti_listen(msg, (noti_cb)callback);
-}
+ return amd::api::Noti::Listen(msg, callback);
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 LIB_API_AMD_API_NOTI_HH_
+#define LIB_API_AMD_API_NOTI_HH_
+
+#include <bundle.h>
+#include <bundle_cpp.h>
+
+#include <functional>
+#include <memory>
+#include <vector>
+#include <string>
+#include <unordered_map>
+
+#include "amd_noti.h"
+
+#undef EXPORT_API
+#define EXPORT_API __attribute__ ((visibility("default")))
+
+namespace amd {
+namespace api {
+
+class EXPORT_API Noti {
+ public:
+ class Msg {
+ public:
+ Msg(std::string msg, int val1 = 0, int val2 = 0, void* val3 = nullptr);
+ Msg(std::string msg, int val1, int val2, void* val3,
+ tizen_base::Bundle data);
+ Msg(std::string msg, int val1, int val2, void* val3,
+ const bundle* data);
+
+ const std::string& GetMessage() const;
+ const tizen_base::Bundle& GetBundle() const;
+ int GetArg1() const;
+ int GetArg2() const;
+ void* GetArg3() const;
+
+ private:
+ class Impl;
+ std::shared_ptr<Impl> impl_;
+ };
+
+ using NotiCb = std::function<int (const Msg& msg)>;
+
+ enum ReturnVal {
+ CONTINUE = 0,
+ STOP = 2
+ };
+
+ static int Listen(const std::string& msg, NotiCb cb);
+ static int Listen(const std::string& msg, noti_cb cb);
+ static int Send(Msg msg);
+ static void Dispose();
+};
+
+template<class T1, class T2, class T3>
+std::tuple<T1, T2, T3> GetNotiArgs(const Noti::Msg& msg) {
+ return std::make_tuple(static_cast<T1>(msg.GetArg1()),
+ static_cast<T2>(msg.GetArg2()), reinterpret_cast<T3>(msg.GetArg3()));
+}
+
+} // namespace api
+} // namespace amd;
+
+#endif // LIB_API_AMD_API_NOTI_HH_
\ No newline at end of file