aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/utilities
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/utilities')
-rw-r--r--src/plugins/utilities/events/EventRouter.cpp68
-rw-r--r--src/plugins/utilities/events/EventRouter.h66
-rw-r--r--src/plugins/utilities/logging/Logger.cpp56
-rw-r--r--src/plugins/utilities/logging/Logger.h53
-rw-r--r--src/plugins/utilities/uuid/UUIDGeneration.cpp137
-rw-r--r--src/plugins/utilities/uuid/UUIDGeneration.h42
6 files changed, 422 insertions, 0 deletions
diff --git a/src/plugins/utilities/events/EventRouter.cpp b/src/plugins/utilities/events/EventRouter.cpp
new file mode 100644
index 0000000..999c3dd
--- /dev/null
+++ b/src/plugins/utilities/events/EventRouter.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. 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.
+ * A copy of the License is located at
+ *
+ * http://aws.amazon.com/apache2.0/
+ *
+ * or in the "license" file accompanying this file. This file 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 "utilities/events/EventRouter.h"
+
+static string TAG = "vshl::utilities::events::EventRouter";
+
+using Level = vshl::common::interfaces::ILogger::Level;
+
+namespace vshl {
+namespace utilities {
+namespace events {
+
+unique_ptr<EventRouter> EventRouter::create(shared_ptr<vshl::common::interfaces::ILogger> logger) {
+ return std::unique_ptr<EventRouter>(new EventRouter(logger));
+}
+
+EventRouter::EventRouter(shared_ptr<vshl::common::interfaces::ILogger> logger) : mLogger(logger) {
+}
+
+EventRouter::~EventRouter() {
+ mEventFilters.clear();
+}
+
+bool EventRouter::handleIncomingEvent(const string eventName, const string voiceAgentId, const string payload) {
+ for (auto eventFilter : mEventFilters) {
+ if (eventFilter->onIncomingEvent(eventName, voiceAgentId, payload)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool EventRouter::addEventFilter(shared_ptr<vshl::common::interfaces::IEventFilter> filter) {
+ if (!filter) {
+ mLogger->log(Level::ERROR, TAG, "Failed to add event filter. Invalid arguments.");
+ return false;
+ }
+
+ mEventFilters.insert(filter);
+ return true;
+}
+
+bool EventRouter::removeEventFilter(shared_ptr<vshl::common::interfaces::IEventFilter> filter) {
+ if (!filter) {
+ mLogger->log(Level::ERROR, TAG, "Failed to add remove filter. Invalid arguments.");
+ return false;
+ }
+
+ mEventFilters.erase(filter);
+ return true;
+}
+
+} // namespace events
+} // namespace utilities
+} // namespace vshl
diff --git a/src/plugins/utilities/events/EventRouter.h b/src/plugins/utilities/events/EventRouter.h
new file mode 100644
index 0000000..fd7f0c2
--- /dev/null
+++ b/src/plugins/utilities/events/EventRouter.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. 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.
+ * A copy of the License is located at
+ *
+ * http://aws.amazon.com/apache2.0/
+ *
+ * or in the "license" file accompanying this file. This file 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 VSHL_UTILITIES_EVENTS_EVENTMANAGER_H_
+#define VSHL_UTILITIES_EVENTS_EVENTMANAGER_H_
+
+#include <memory>
+#include <string>
+#include <unordered_set>
+
+#include "interfaces/utilities/events/IEventFilter.h"
+#include "interfaces/utilities/logging/ILogger.h"
+
+using namespace std;
+
+namespace vshl {
+namespace utilities {
+namespace events {
+/*
+ * This class is responsible for routing incoming events to
+ * the appropriate event listener for consumption.
+ * Note: The listeners should implement the IEventFilter class.
+ */
+class EventRouter {
+public:
+ static unique_ptr<EventRouter> create(shared_ptr<vshl::common::interfaces::ILogger> logger);
+
+ // Destructor
+ ~EventRouter();
+
+ // Add event filter as listerner.
+ bool addEventFilter(shared_ptr<vshl::common::interfaces::IEventFilter> filter);
+
+ // Remove event filter as listerner.
+ bool removeEventFilter(shared_ptr<vshl::common::interfaces::IEventFilter> filter);
+
+ // This method is called by the controller for routing
+ // the event to appropriate listener.
+ bool handleIncomingEvent(const string eventName, const string voiceAgentId, const string payload);
+
+private:
+ EventRouter(shared_ptr<vshl::common::interfaces::ILogger> logger);
+
+ // set of event filters.
+ unordered_set<shared_ptr<vshl::common::interfaces::IEventFilter>> mEventFilters;
+
+ // Logger
+ shared_ptr<vshl::common::interfaces::ILogger> mLogger;
+};
+
+} // namespace events
+} // namespace utilities
+} // namespace vshl
+
+#endif // VSHL_UTILITIES_EVENTS_EVENTROUTER_H_
diff --git a/src/plugins/utilities/logging/Logger.cpp b/src/plugins/utilities/logging/Logger.cpp
new file mode 100644
index 0000000..6374e19
--- /dev/null
+++ b/src/plugins/utilities/logging/Logger.cpp
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. 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.
+ * A copy of the License is located at
+ *
+ * http://aws.amazon.com/apache2.0/
+ *
+ * or in the "license" file accompanying this file. This file 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 "utilities/logging/Logger.h"
+
+namespace vshl {
+namespace utilities {
+namespace logging {
+
+// Constructor
+Logger::Logger(AFB_ApiT api) {
+ mApi = api;
+}
+
+unique_ptr<Logger> Logger::create(AFB_ApiT api) {
+ auto logger = std::unique_ptr<Logger>(new Logger(api));
+ return logger;
+}
+
+void Logger::log(Level level, const std::string& tag, const std::string& message) {
+ string format_msg = "Tag: " + tag + ", message: " + message;
+ switch (level) {
+ case Level::NOTICE:
+ AFB_ApiNotice(mApi, format_msg.c_str());
+ break;
+ case Level::WARNING:
+ AFB_ApiWarning(mApi, format_msg.c_str());
+ break;
+ case Level::DEBUG:
+ AFB_ApiDebug(mApi, format_msg.c_str());
+ break;
+ case Level::ERROR:
+ AFB_ApiError(mApi, format_msg.c_str());
+ break;
+ case Level::INFO:
+ AFB_ApiInfo(mApi, format_msg.c_str());
+ break;
+ default:
+ break;
+ }
+}
+
+} // namespace logging
+} // namespace utilities
+} // namespace vshl
diff --git a/src/plugins/utilities/logging/Logger.h b/src/plugins/utilities/logging/Logger.h
new file mode 100644
index 0000000..79c89a5
--- /dev/null
+++ b/src/plugins/utilities/logging/Logger.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. 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.
+ * A copy of the License is located at
+ *
+ * http://aws.amazon.com/apache2.0/
+ *
+ * or in the "license" file accompanying this file. This file 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 VSHL_UTILITIES_LOGGING_LOGGER_H_
+#define VSHL_UTILITIES_LOGGING_LOGGER_H_
+
+#include <memory>
+
+extern "C" {
+#define AFB_BINDING_VERSION 3
+#include "afb-definitions.h"
+#include "ctl-plugin.h"
+};
+
+#include "interfaces/utilities/logging/ILogger.h"
+
+using namespace std;
+
+namespace vshl {
+namespace utilities {
+namespace logging {
+
+class Logger : public vshl::common::interfaces::ILogger {
+public:
+ static std::unique_ptr<Logger> create(AFB_ApiT api);
+
+ // ILogger interface
+ void log(Level level, const std::string &tag,
+ const std::string &message) override;
+
+private:
+ Logger(AFB_ApiT api);
+
+ // Binding API reference
+ AFB_ApiT mApi;
+};
+
+} // namespace logging
+} // namespace utilities
+} // namespace vshl
+
+#endif // VSHL_UTILITIES_LOGGING_LOGGER_H_
diff --git a/src/plugins/utilities/uuid/UUIDGeneration.cpp b/src/plugins/utilities/uuid/UUIDGeneration.cpp
new file mode 100644
index 0000000..bb03fc6
--- /dev/null
+++ b/src/plugins/utilities/uuid/UUIDGeneration.cpp
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. 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.
+ * A copy of the License is located at
+ *
+ * http://aws.amazon.com/apache2.0/
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+// This code is copied from https://github.com/alexa/avs-device-sdk/
+// Minor modifications are made to the original file.
+
+#include <random>
+#include <chrono>
+#include <sstream>
+#include <iomanip>
+#include <mutex>
+#include <climits>
+#include <algorithm>
+#include <functional>
+
+#include "utilities/uuid/UUIDGeneration.h"
+
+namespace vshl {
+namespace utilities {
+namespace uuid {
+
+/// String to identify log entries originating from this file.
+static const std::string TAG("UUIDGeneration");
+
+/// The UUID version (Version 4), shifted into the correct position in the byte.
+static const uint8_t UUID_VERSION_VALUE = 4 << 4;
+
+/// The UUID variant (Variant 1), shifted into the correct position in the byte.
+static const size_t UUID_VARIANT_VALUE = 2 << 6;
+
+/// Separator used between UUID fields.
+static const std::string SEPARATOR("-");
+
+/// Number of bits in the replacement value.
+static const size_t MAX_NUM_REPLACEMENT_BITS = CHAR_BIT;
+
+/// Number of bits in a hex digit.
+static const size_t BITS_IN_HEX_DIGIT = 4;
+
+/**
+ * Randomly generate a string of hex digits. Before the conversion of hex to string,
+ * the function allows replacement of the bits of the first two generated hex digits.
+ * Replacement happens starting at the most significant, to the least significant bit.
+ *
+ * @param ibe A random number generator.
+ * @param numDigits The number of hex digits [0-9],[a-f] to generate.
+ * @param replacementBits The replacement value for up to the first two digits generated.
+ * @param numReplacementBits The number of bits of @c replacementBits to use.
+ *
+ * @return A hex string of length @c numDigits.
+ */
+static const std::string generateHexWithReplacement(
+ std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>& ibe,
+ unsigned int numDigits,
+ uint8_t replacementBits,
+ unsigned short numReplacementBits) {
+ if (numReplacementBits > MAX_NUM_REPLACEMENT_BITS) {
+ return "";
+ }
+
+ if (numReplacementBits > (numDigits * BITS_IN_HEX_DIGIT)) {
+ return "";
+ }
+
+ // Makes assumption that 1 digit = 4 bits.
+ std::vector<uint8_t> bytes(ceil(numDigits / 2.0));
+ std::generate(bytes.begin(), bytes.end(), std::ref(ibe));
+
+ // Replace the specified number of bits from the first byte.
+ bytes.at(0) &= (0xff >> numReplacementBits);
+ replacementBits &= (0xff << (MAX_NUM_REPLACEMENT_BITS - numReplacementBits));
+ bytes.at(0) |= replacementBits;
+
+ std::ostringstream oss;
+ for (const auto& byte : bytes) {
+ oss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
+ }
+
+ std::string bytesText = oss.str();
+ // Remove the last digit for odd numDigits case.
+ bytesText.resize(numDigits);
+
+ return bytesText;
+}
+
+/**
+ * Randomly generate a string of hex digits.
+ *
+ * @param ibe A random number generator.
+ * @param numDigits The number of hex digits [0-9],[a-f] to generate.
+ *
+ * @return A hex string of length @c numDigits.
+ */
+static const std::string generateHex(
+ std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>& ibe,
+ unsigned int numDigits) {
+ return generateHexWithReplacement(ibe, numDigits, 0, 0);
+}
+
+const std::string generateUUID() {
+ static bool seeded = false;
+ static std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t> ibe;
+ static std::mutex mutex;
+ std::unique_lock<std::mutex> lock(mutex);
+ if (!seeded) {
+ std::random_device rd;
+ ibe.seed(
+ rd() +
+ std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch())
+ .count());
+ seeded = true;
+ }
+
+ std::ostringstream uuidText;
+ uuidText << generateHex(ibe, 8) << SEPARATOR << generateHex(ibe, 4) << SEPARATOR
+ << generateHexWithReplacement(ibe, 4, UUID_VERSION_VALUE, 4) << SEPARATOR
+ << generateHexWithReplacement(ibe, 4, UUID_VARIANT_VALUE, 2) << SEPARATOR << generateHex(ibe, 12);
+
+ lock.unlock();
+
+ return uuidText.str();
+}
+
+} // namespace uuid
+} // namespace utilities
+} // namespace vshl
diff --git a/src/plugins/utilities/uuid/UUIDGeneration.h b/src/plugins/utilities/uuid/UUIDGeneration.h
new file mode 100644
index 0000000..7af9cb1
--- /dev/null
+++ b/src/plugins/utilities/uuid/UUIDGeneration.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. 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.
+ * A copy of the License is located at
+ *
+ * http://aws.amazon.com/apache2.0/
+ *
+ * or in the "license" file accompanying this file. This file 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.
+ */
+// This code is copied from https://github.com/alexa/avs-device-sdk/
+// Minor modifications are made to the original file.
+
+#ifndef VSHL_UTILITIES_UUID_UUIDGENERATION_H_
+#define VSHL_UTILITIES_UUID_UUIDGENERATION_H_
+
+#include <string>
+
+namespace vshl {
+namespace utilities {
+namespace uuid {
+
+/**
+ * Generates a variant 1, version 4 universally unique identifier (UUID) consisting of 32 hexadecimal digits.
+ * The UUID generated is of the format xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx where M indicates the version, and the two
+ * most significant bits of N indicates the variant. M is 0100 (binary) for version 4 and N is 10xx(binary) for
+ * variant 1.
+ * @see https://tools.ietf.org/html/rfc4122.
+ *
+ * @return A uuid as a string.
+ */
+const std::string generateUUID();
+
+} // namespace uuid
+} // namespace utilities
+} // namespace vshl
+
+#endif // VSHL_UTILITIES_UUID_UUIDGENERATION_H_ \ No newline at end of file