diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/AwsClient.cpp | 410 | ||||
-rw-r--r-- | src/AwsClient.h | 77 | ||||
-rw-r--r-- | src/AzureClient.cpp | 296 | ||||
-rw-r--r-- | src/AzureClient.h | 55 | ||||
-rwxr-xr-x | src/CMakeLists.txt | 8 | ||||
-rwxr-xr-x | src/ClientManager.cpp | 433 | ||||
-rwxr-xr-x | src/ClientManager.h | 118 | ||||
-rw-r--r-- | src/CloudClient.h | 37 | ||||
-rw-r--r-- | src/CloudType.h | 30 | ||||
-rwxr-xr-x | src/cloudproxy-bindings.cpp | 329 | ||||
-rwxr-xr-x | src/export.map | 1 | ||||
-rwxr-xr-x | src/hmi-debug.h | 82 | ||||
-rwxr-xr-x | src/utils.h | 108 |
13 files changed, 1322 insertions, 662 deletions
diff --git a/src/AwsClient.cpp b/src/AwsClient.cpp new file mode 100644 index 0000000..856eef0 --- /dev/null +++ b/src/AwsClient.cpp @@ -0,0 +1,410 @@ +#include "AwsClient.h" + +#include <filesystem> + +#include "aws_iot_config.h" +#include "aws_iot_mqtt_client_interface.h" + +#include <nlohmann/json.hpp> + +#include "CloudType.h" +#include "ClientManager.h" + +#include <glib.h> +#include <afb/afb-binding.h> // for AFB_* logger + +namespace +{ + +void aws_subscribe_callback(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, + IoT_Publish_Message_Params *params, void *pData) +{ + (void)pClient; + AFB_INFO("AWS subscribe callback: %.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *) params->payload); + + if (!pData) + { + AFB_INFO("AwsClient is null"); + return; + } + + AwsClient* aws = (AwsClient*)pData; + + std::string topic; + if (topicName && topicNameLen > 0) + topic.assign(topicName, topicNameLen); + + if (topic != aws->conf().mqtt_in_topic) + { + AFB_WARNING("Received message topic is not equal to configured: '%s'", topic.c_str()); + return; + } + + if (!params || !params->payload || params->payloadLen <= 0) + { + AFB_WARNING("Received message doesn't contain payload"); + return; + } + + const auto& pmsg = AwsClient::parseMessage(std::string((char*)params->payload, params->payloadLen)); + const auto& appid = pmsg.first; + const auto& data = pmsg.second; + if (appid.empty()) + { + AFB_ERROR("Can't parse received message"); + return; + } + + ClientManager::instance().emitReceivedMessage(appid, CloudType::Aws, data); +} + +void aws_disconnect_callback(AWS_IoT_Client *pClient, void *data) +{ + (void)data; + AFB_WARNING("AWS MQTT disconnect"); + IoT_Error_t rc = IoT_Error_t::FAILURE; + + if(pClient) + return; + + if(aws_iot_is_autoreconnect_enabled(pClient)) + { + AFB_INFO("Auto Reconnect is enabled. Reconnecting attempt will start now"); + } + else + { + AFB_WARNING("Auto Reconnect not enabled. Starting manual reconnect..."); + rc = aws_iot_mqtt_attempt_reconnect(pClient); + + if(IoT_Error_t::NETWORK_RECONNECTED == rc) + AFB_WARNING("Manual Reconnect Successful"); + else + AFB_WARNING("Manual Reconnect Failed - %d", rc); + } +} + +} // end namespace + +AwsClient::AwsClient() = default; + +AwsClient::~AwsClient() +{ + stop(); +} + +// static +std::pair<std::string, std::string> AwsClient::parseMessage(const std::string& msg) +{ + std::string appid; + std::string data; + std::string cloud; + try + { + const nlohmann::json& jmsg = nlohmann::json::parse(msg); + appid = jmsg.at("application_id").get<std::string>(); + data = jmsg.at("data").get<std::string>(); + } + catch (const std::exception& ex) + { + AFB_ERROR("Can't parse message (%s): %s", msg.c_str(), ex.what()); + appid.clear(); // indicate error + } + + return {appid, data}; +} + +// static +std::string AwsClient::getApplicationId(const std::string& msg) +{ + std::string appid; + try + { + const nlohmann::json& jmsg = nlohmann::json::parse(msg); + appid = jmsg.at("application_id").get<std::string>(); + } + catch (const std::exception& ex) + { + AFB_ERROR("Can't parse message (%s): %s", msg.c_str(), ex.what()); + } + + return appid; +} + +void AwsClient::main_loop() +{ + IoT_Publish_Message_Params params; + params.qos = QoS::QOS1; + params.isRetained = 0; + + while(!m_thread_stop) + { + IoT_Error_t rc = aws_iot_mqtt_yield(m_aws_client.get(), 100); + if(IoT_Error_t::NETWORK_ATTEMPTING_RECONNECT == rc) + { + // If the client is attempting to reconnect we will skip the rest of the loop. + continue; + } + + std::string data; + { + std::lock_guard<std::mutex> lock(m_mutex); + if (m_queue.empty()) + continue; + + data = std::move(m_queue.front()); + m_queue.pop_front(); + } + params.payload = const_cast<char*>(data.c_str()); + params.payloadLen = data.length(); + + + std::string appid{getApplicationId(data)}; + if (appid.empty()) + { + AFB_ERROR("Can't obtain application_id from scheduled message (msg will be dropped)"); + // ClientManager::instance().emitSendMessageConfirmation(appid, false); + continue; + } + + rc = IoT_Error_t::FAILURE; + if (!appid.empty()) + { + rc = aws_iot_mqtt_publish(m_aws_client.get(), m_conf.mqtt_out_topic.c_str(), (uint16_t)m_conf.mqtt_out_topic.length(), ¶ms); + if (rc == IoT_Error_t::MQTT_REQUEST_TIMEOUT_ERROR) + { + AFB_WARNING("Publish ack not received"); + //???? rc = IoT_Error_t::SUCCESS; + } + } + + ClientManager::instance().emitSendMessageConfirmation(appid, CloudType::Aws, rc == IoT_Error_t::SUCCESS); + } +} + +void AwsClient::start() +{ + if (m_started) + return; + + if (!m_aws_client || !m_connected) + { + AFB_ERROR("Can't start AWS thread: connection is not created"); + return; + } + + m_thread_stop = false; + m_thread = std::thread(&AwsClient::main_loop, this); + m_started = true; +} + +void AwsClient::stop() +{ + if (m_thread.joinable()) + { + AFB_DEBUG("Wait AWS thread..."); + m_thread_stop = true; + m_thread.join(); + AFB_DEBUG("AWS thread joined"); + + if (m_connected) + { + AFB_DEBUG("AWS disconnecting..."); + aws_iot_mqtt_unsubscribe(m_aws_client.get(), m_conf.mqtt_in_topic.c_str(), (uint16_t)m_conf.mqtt_in_topic.length()); + aws_iot_mqtt_disconnect(m_aws_client.get()); + aws_iot_mqtt_yield(m_aws_client.get(), 100); + AFB_DEBUG("AWS disconnected"); + } + } + + m_started = false; +} + +bool AwsClient::createConnection() +{ + AFB_NOTICE("%s called", __FUNCTION__); + + if (m_aws_client) + { + AFB_ERROR("AWS connection already created"); + return false; + } + + m_aws_client.reset(new AWS_IoT_Client); + + IoT_Client_Init_Params mqttInitParams = iotClientInitParamsDefault; + IoT_Client_Connect_Params connectParams = iotClientConnectParamsDefault; + + AFB_DEBUG("rootCA cert %s", m_conf.root_ca_cert_path.c_str()); + AFB_DEBUG("client cert %s", m_conf.device_cert_path.c_str()); + AFB_DEBUG("client key %s", m_conf.device_priv_key_path.c_str()); + + mqttInitParams.enableAutoReconnect = false; // We enable this later below + mqttInitParams.pHostURL = const_cast<char*>(m_conf.mqtt_host.c_str()); + mqttInitParams.port = m_conf.mqtt_port; + mqttInitParams.pRootCALocation = const_cast<char*>(m_conf.root_ca_cert_path.c_str()); + mqttInitParams.pDeviceCertLocation = const_cast<char*>(m_conf.device_cert_path.c_str()); + mqttInitParams.pDevicePrivateKeyLocation = const_cast<char*>(m_conf.device_priv_key_path.c_str()); + mqttInitParams.mqttCommandTimeout_ms = 20000; + mqttInitParams.tlsHandshakeTimeout_ms = 5000; + mqttInitParams.isSSLHostnameVerify = true; + mqttInitParams.disconnectHandler = aws_disconnect_callback; + mqttInitParams.disconnectHandlerData = nullptr; + + IoT_Error_t rc = aws_iot_mqtt_init(m_aws_client.get(), &mqttInitParams); + if(IoT_Error_t::SUCCESS != rc) + { + AFB_ERROR("aws_iot_mqtt_init returned error: %d ", rc); + return false; + } + + connectParams.keepAliveIntervalInSec = 600; + connectParams.isCleanSession = true; + connectParams.MQTTVersion = MQTT_Ver_t::MQTT_3_1_1; + connectParams.pClientID = const_cast<char*>(m_conf.mqtt_client_id.c_str()); + connectParams.clientIDLen = (uint16_t)m_conf.mqtt_client_id.size(); + connectParams.isWillMsgPresent = false; + + AFB_NOTICE("Connecting..."); + rc = aws_iot_mqtt_connect(m_aws_client.get(), &connectParams); + if(IoT_Error_t::SUCCESS != rc) + { + AFB_NOTICE("Error(%d) connecting to %s:%d", rc, mqttInitParams.pHostURL, mqttInitParams.port); + return false; + } + + AFB_NOTICE("Subscribing..."); + rc = aws_iot_mqtt_subscribe(m_aws_client.get(), m_conf.mqtt_in_topic.c_str(), (uint16_t)m_conf.mqtt_in_topic.length(), QOS1, aws_subscribe_callback, this); + if(IoT_Error_t::SUCCESS != rc) + { + AFB_ERROR("Error subscribing: %d ", rc); + return false; + } + + /* + Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h + #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL + #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL + */ + rc = aws_iot_mqtt_autoreconnect_set_status(m_aws_client.get(), true); + if(IoT_Error_t::SUCCESS != rc) + { + AFB_ERROR("Unable to set AutoReconnect to true: %d", rc); + return false; + } + + m_connected = true; + + if (!m_started) + start(); + + return true; +} + +bool AwsClient::sendMessage(const std::string& appid, const std::string& data) +{ + if (!m_aws_client || !m_connected) + { + AFB_ERROR("AwsClient is not ready for message sending"); + return false; + } + + nlohmann::json jmsg{ + {"application_id", appid}, + {"data", data} + }; + + const std::string& msg = jmsg.dump(); + + { + std::lock_guard<std::mutex> lock(m_mutex); + + if (m_queue.size() < MAX_QUEUE_SIZE) + { + m_queue.push_back(msg); + AFB_DEBUG("AWS queue size: %lu", m_queue.size()); + return true; + } + else + { + AFB_ERROR("Can't send message: AWS queue size exceeded: %lu", m_queue.size()); + } + } + + return false; +} + +bool AwsClient::enabled() const +{ + return m_conf.enabled; +} + +bool AwsClient::connected() const +{ + return m_connected; +} + +const AwsClientConfig& AwsClient::conf() const +{ + return m_conf; +} + +bool AwsClient::loadConf(GKeyFile* conf_file) +{ + g_autoptr(GError) error = nullptr; + + auto read_string = [&conf_file](const char* section, const char* key, std::string& set_to) + { + g_autoptr(GError) error = nullptr; + g_autofree gchar *value = g_key_file_get_string(conf_file, section, key, &error); + if (value == nullptr) + { + AFB_ERROR("Can't read %s/%s from config", section, key); + return false; + } + if (!value[0]) + { + AFB_ERROR("Value %s/%s is empty", section, key); + return false; + } + set_to = value; + return true; + }; + + auto check_file = [](const std::string& path) + { + if (!std::filesystem::exists(path)) + { + AFB_ERROR("File %s doesn't exists", path.c_str()); + return false; + } + return true; + }; + + //-- AWS parameters: + m_conf.enabled = g_key_file_get_boolean(conf_file, "AwsCloudConnection", "Enabled", &error); + + if (!m_conf.enabled) + return true; // don't check other settings in this case + + if (!read_string("AwsCloudConnection", "MqttHost", m_conf.mqtt_host) || + !read_string("AwsCloudConnection", "MqttClientId", m_conf.mqtt_client_id) || + !read_string("AwsCloudConnection", "MqttOutTopic", m_conf.mqtt_out_topic) || + !read_string("AwsCloudConnection", "MqttInTopic", m_conf.mqtt_in_topic) || + !read_string("AwsCloudConnection", "ThingName", m_conf.thing_name) || + !read_string("AwsCloudConnection", "RootCaCert", m_conf.root_ca_cert_path) || + !read_string("AwsCloudConnection", "DeviceCert", m_conf.device_cert_path) || + !read_string("AwsCloudConnection", "DevicePrivKey", m_conf.device_priv_key_path)) + return false; + + m_conf.mqtt_port = (uint16_t)g_key_file_get_uint64(conf_file, "AwsCloudConnection", "MqttPort", &error); + if (!m_conf.mqtt_port) + { + AFB_ERROR("Value AwsCloudConnection/MqttPort is not configured"); + return false; + } + + if (!check_file(m_conf.root_ca_cert_path) || !check_file(m_conf.device_cert_path) || !check_file(m_conf.device_priv_key_path)) + return false; + + return true; +} diff --git a/src/AwsClient.h b/src/AwsClient.h new file mode 100644 index 0000000..6ab15c4 --- /dev/null +++ b/src/AwsClient.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2020 MERA + * + * 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. + */ + +#pragma once + +#include "CloudClient.h" +#include <string> +#include <thread> +#include <atomic> +#include <deque> +#include <mutex> + + +struct AwsClientConfig +{ + bool enabled{false}; + std::string mqtt_host; + uint16_t mqtt_port{433}; + std::string mqtt_client_id; + std::string mqtt_out_topic; + std::string mqtt_in_topic; + std::string thing_name; + std::string root_ca_cert_path; + std::string device_cert_path; + std::string device_priv_key_path; +}; + +typedef struct _Client AWS_IoT_Client; + +class AwsClient : public CloudClient +{ +public: + static constexpr size_t MAX_QUEUE_SIZE{10000}; + + AwsClient(); + ~AwsClient(); + + bool sendMessage(const std::string& appid, const std::string& data) override; + bool createConnection() override; + + bool enabled() const override; + bool connected() const override; + bool loadConf(GKeyFile* conf_file) override; + + const AwsClientConfig& conf() const; + + static std::pair<std::string, std::string> parseMessage(const std::string& msg); + static std::string getApplicationId(const std::string& msg); + +protected: + void main_loop(); + void start(); + void stop(); + +protected: + std::unique_ptr<AWS_IoT_Client> m_aws_client; + AwsClientConfig m_conf; + std::thread m_thread; + std::atomic_bool m_thread_stop{false}; + std::atomic_bool m_started{false}; + bool m_connected{false}; + std::deque<std::string> m_queue; + std::mutex m_mutex; +}; diff --git a/src/AzureClient.cpp b/src/AzureClient.cpp new file mode 100644 index 0000000..e381922 --- /dev/null +++ b/src/AzureClient.cpp @@ -0,0 +1,296 @@ +/* + * Copyright (C) 2020 MERA + * + * 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 "AzureClient.h" + +#include <iothub.h> +#include <iothub_device_client.h> +#include <iothub_client_options.h> +#include <iothub_message.h> +#include <iothubtransportmqtt.h> +#include <azure_c_shared_utility/threadapi.h> // ThreadAPI_Sleep() +#include <azure_c_shared_utility/tickcounter.h> // tickcounter_ms_t + +#include "utils.h" +#include "CloudType.h" +#include "ClientManager.h" + +#include <glib.h> +#include <afb/afb-binding.h> // for AFB_* logger + +namespace +{ + +void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* user_context) +{ + AFB_NOTICE("%s called: result %d, reason %d", __FUNCTION__, result, reason); + + (void)reason; + (void)user_context; + // This sample DOES NOT take into consideration network outages. + if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED && reason == IOTHUB_CLIENT_CONNECTION_OK) + { + AFB_NOTICE("The device client is connected to iothub"); + } + else + { + AFB_NOTICE("The device client has been disconnected"); + } +} + +IOTHUBMESSAGE_DISPOSITION_RESULT receive_msg_callback(IOTHUB_MESSAGE_HANDLE message, void* user_context) +{ + AFB_NOTICE("%s called", __FUNCTION__); + (void)user_context; + + IOTHUBMESSAGE_CONTENT_TYPE content_type = IoTHubMessage_GetContentType(message); + + if (content_type == IOTHUBMESSAGE_BYTEARRAY) + { + const unsigned char* buff_msg; + size_t buff_len; + + if (IoTHubMessage_GetByteArray(message, &buff_msg, &buff_len) != IOTHUB_MESSAGE_OK) + { + AFB_ERROR("Failure retrieving byte array message"); + } + else + { + AFB_NOTICE("Received Binary message, size %d, data '%.*s'", (int)buff_len, (int)buff_len, buff_msg); + } + + const char* app_id = IoTHubMessage_GetProperty(message, "application_id"); + AFB_NOTICE("Received property 'application_id': %s", (app_id ? app_id : "<unavailable>")); + + if (app_id && app_id[0]) + ClientManager::instance().emitReceivedMessage(app_id, CloudType::Azure, std::string((const char*)buff_msg, buff_len)); + else + AFB_ERROR("Can't emit SendMessageConfirmation: appid is not valid"); + } + else if (content_type == IOTHUBMESSAGE_STRING) + { + const char* string_msg = IoTHubMessage_GetString(message); + if (string_msg == nullptr) + { + AFB_NOTICE("Failure retrieving String message"); + } + else + { + AFB_NOTICE("Received String message, size %lu, data '%s'", strlen(string_msg), string_msg); + } + + const char* app_id = IoTHubMessage_GetProperty(message, "application_id"); + AFB_NOTICE("Received property 'application_id': %s", (app_id ? app_id : "<unavailable>")); + + if (app_id && app_id[0]) + ClientManager::instance().emitReceivedMessage(app_id, CloudType::Azure, string_msg); + else + AFB_ERROR("Can't emit SendMessageConfirmation: appid is not valid"); + } + else + { + AFB_ERROR("Unsupported message content type"); + } + + return IOTHUBMESSAGE_ACCEPTED; +} + + +int device_method_callback(const char* method_name, const unsigned char* payload, size_t size, unsigned char** response, size_t* resp_size, void* userContextCallback) +{ + AFB_NOTICE("%s called, method_name %s", __FUNCTION__, method_name); + + const char* device_id = (const char*)userContextCallback; + + int status = 501; + const char* RESPONSE_STRING = "{ \"Response\": \"Unknown method requested.\" }"; + + AFB_NOTICE("Device Method called for device %s", device_id); + AFB_NOTICE("Device Method name: %s", method_name); + AFB_NOTICE("Device Method payload: %.*s", (int)size, (const char*)payload); + + + AFB_NOTICE("Response status: %d", status); + AFB_NOTICE("Response payload: %s", RESPONSE_STRING); + + *resp_size = strlen(RESPONSE_STRING); + if ((*response = (unsigned char*)malloc(*resp_size)) == nullptr) + { + status = -1; + } + else + { + memcpy(*response, RESPONSE_STRING, *resp_size); + } + + return status; +} + + +void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) +{ + AFB_NOTICE("%s called, result %d", __FUNCTION__, result); + (void)userContextCallback; + // When a message is sent this callback will get invoked + + AFB_NOTICE("Confirmation callback result %s", MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); + + const char* appid = (const char*)userContextCallback; + if (!appid || !appid[0]) + { + AFB_ERROR("Confirmation callback: appid is not set"); + + if (userContextCallback) + free(userContextCallback); + + return; + } + + ClientManager::instance().emitSendMessageConfirmation(appid, CloudType::Azure, result == IOTHUB_CLIENT_CONFIRMATION_OK); + free(userContextCallback); +} + +} //end namespace + +AzureClient::AzureClient() = default; + +AzureClient::~AzureClient() +{ + if (m_iot_inited) + { + if (m_azure_client && *m_azure_client) + IoTHubDeviceClient_Destroy(*m_azure_client); + + IoTHub_Deinit(); + } +} + +bool AzureClient::createConnection() +{ + AFB_NOTICE("%s called", __FUNCTION__); + + if (m_iot_inited) + { + AFB_ERROR("Azure IoT already initalized"); + return false; + } + + // Init Azure API: + { + int res = IoTHub_Init(); + m_iot_inited = true; + + if (res) + { + AFB_ERROR("Azure IoTHub_Init() failed: %d", res); + return false; + } + } + + if (m_azure_client) + { + AFB_ERROR("connection already created"); + return false; + } + + IOTHUB_DEVICE_CLIENT_HANDLE device_handle = IoTHubDeviceClient_CreateFromConnectionString(m_conf.device_connection_string.c_str(), MQTT_Protocol); + if (!device_handle) + { + AFB_ERROR("Failure creating Azure IoTHubDeviceClient device"); + return false; + } + + bool traceOn = false; + IoTHubDeviceClient_SetOption(device_handle, OPTION_LOG_TRACE, &traceOn); + IoTHubDeviceClient_SetConnectionStatusCallback(device_handle, connection_status_callback, nullptr); + IoTHubDeviceClient_SetMessageCallback(device_handle, receive_msg_callback, nullptr); + IoTHubDeviceClient_SetDeviceMethodCallback(device_handle, device_method_callback, nullptr); + + tickcounter_ms_t ms_delay = 10; + IoTHubDeviceClient_SetOption(device_handle, OPTION_DO_WORK_FREQUENCY_IN_MS, &ms_delay); // DoWork multithread + + m_azure_client.reset(new IOTHUB_DEVICE_CLIENT_HANDLE{device_handle}); + + return true; +} + + +bool AzureClient::sendMessage(const std::string& appid, const std::string& data) +{ + if (!m_azure_client) + { + AFB_ERROR("AzureClient is not ready for message sending"); + return false; + } + + IOTHUB_MESSAGE_HANDLE message_handle = IoTHubMessage_CreateFromString(data.c_str()); + + utils::scope_exit message_handle_destroy([&message_handle](){ + // The message is copied to the sdk, so the we can destroy it + if (message_handle) + IoTHubMessage_Destroy(message_handle); + }); + + if (!message_handle) + { + AFB_ERROR("Can't create IoTHubMessage message"); + return false; + } + + IoTHubMessage_SetProperty(message_handle, "application_id", appid.c_str()); + + if (IoTHubDeviceClient_SendEventAsync(*m_azure_client, message_handle, send_confirm_callback, strdup(appid.c_str()))) + { + AFB_ERROR("Can't send IoTHubMessage message"); + return false; + } + + return true; +} + +bool AzureClient::enabled() const +{ + return m_conf.enabled; +} + +bool AzureClient::connected() const +{ + return (m_azure_client && *m_azure_client); +} + +bool AzureClient::loadConf(GKeyFile* conf_file) +{ + g_autoptr(GError) error = nullptr; + + // Azure parameters: + m_conf.enabled = g_key_file_get_boolean(conf_file, "AzureCloudConnection", "Enabled", &error); + + g_autofree gchar *value = g_key_file_get_string(conf_file, "AzureCloudConnection", "DeviceConnectionString", &error); + if (value == nullptr) + { + AFB_ERROR("can't read AzureCloudConnection/DeviceConnectionString from config"); + return false; + } + + m_conf.device_connection_string = value; + if (m_conf.device_connection_string.empty()) + { + AFB_ERROR("AzureCloudConnection/DeviceConnectionString is empty"); + return false; + } + + return true; +} diff --git a/src/AzureClient.h b/src/AzureClient.h new file mode 100644 index 0000000..ea69814 --- /dev/null +++ b/src/AzureClient.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2020 MERA + * + * 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. + */ + +#pragma once + +#include "CloudClient.h" + +#include <memory> + + +struct AzureClientConfig +{ + bool enabled{false}; + std::string device_connection_string; +}; + +typedef struct IOTHUB_CLIENT_CORE_INSTANCE_TAG* IOTHUB_CLIENT_CORE_HANDLE; +typedef IOTHUB_CLIENT_CORE_HANDLE IOTHUB_DEVICE_CLIENT_HANDLE; + +class AzureClient : public CloudClient +{ +public: + AzureClient(); + ~AzureClient(); + + bool sendMessage(const std::string& appid, const std::string& data) override; + bool createConnection() override; + + bool enabled() const override; + bool connected() const override; + bool loadConf(GKeyFile* conf_file) override; + + const AzureClientConfig& conf() const; + + static std::pair<std::string, std::string> parseMessage(const std::string& msg); + static std::string getApplicationId(const std::string& msg); + +protected: + std::unique_ptr<IOTHUB_DEVICE_CLIENT_HANDLE> m_azure_client; + AzureClientConfig m_conf; + bool m_iot_inited{false}; +}; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fde17a5..eec5b09 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,14 +16,18 @@ PROJECT_TARGET_ADD(cloudproxy-binding) -add_definitions(-DAFB_BINDING_VERSION=3) +add_definitions(-DAFB_BINDING_VERSION=3 -D_GNU_SOURCE) set(BINDING_SOURCES cloudproxy-bindings.cpp utils.h - hmi-debug.h ClientManager.h ClientManager.cpp + CloudType.h + AwsClient.h + AwsClient.cpp + AzureClient.h + AzureClient.cpp ) set(AZURE_DEP_LIBS iothub_client) diff --git a/src/ClientManager.cpp b/src/ClientManager.cpp index 74d672d..cbda0e2 100755 --- a/src/ClientManager.cpp +++ b/src/ClientManager.cpp @@ -1,215 +1,218 @@ -/*
- * Copyright (C) 2020 MERA
- *
- * 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 "ClientManager.h"
-#include "hmi-debug.h"
-
-#include <set>
-#include <json-c/json.h>
-
-// static
-void ClientManager::cbRemoveClientCtx(void *data)
-{
- ClientManager::instance().removeClient((ClientManager::ClientCtx*)data);
-}
-
-
-// static
-ClientManager& ClientManager::instance()
-{
- static ClientManager instance;
- return instance;
-}
-
-
-ClientManager::~ClientManager()
-{
- std::lock_guard<std::mutex> lock(this->m_mutex);
- for (auto cl : m_clients)
- delete cl.second;
-}
-
-
-ClientManager::ClientCtx* ClientManager::addClient(afb_req_t req, const std::string& appid)
-{
- ClientCtx* ctx = (ClientCtx*)afb_req_context_get(req);
- if (!ctx)
- {
- HMI_NOTICE("cloudproxy-service", "create new session for %s", appid.c_str());
- ctx = new ClientCtx{appid, afb_api_make_event(req->api, appid.c_str())};
- afb_req_session_set_LOA(req, 1);
- afb_req_context_set(req, ctx, cbRemoveClientCtx);
- }
-
- m_clients[appid] = ctx;
- return ctx;
-}
-
-
-void ClientManager::removeClient(ClientCtx* ctx)
-{
- if(!ctx)
- {
- HMI_ERROR("cloudproxy-service", "data is nullptr");
- return;
- }
-
- HMI_NOTICE("cloudproxy-service", "remove app %s", ctx->appid.c_str());
- std::lock_guard<std::mutex> lock(this->m_mutex);
-
- auto it = m_clients.find(ctx->appid);
- if (it != m_clients.end())
- {
- delete it->second;
- m_clients.erase(it);
- }
-}
-
-
-bool ClientManager::handleRequest(afb_req_t request, const std::string& verb, const std::string& appid)
-{
- HMI_NOTICE("cloudproxy-service", "handleRequest: verb='%s', appid='%s'", verb.c_str(), appid.c_str());
- std::lock_guard<std::mutex> lock(this->m_mutex);
-
- if (appid.empty())
- {
- HMI_ERROR("cloudproxy-service", "appid is empty");
- return false;
- }
-
- auto client_it = m_clients.find(appid);
- if (verb != "subscribe" && client_it == m_clients.end())
- {
- HMI_NOTICE("cloudproxy-service", "client with appid '%s' is not registered", appid.c_str());
- return false;
- }
-
- if (verb == "subscribe")
- {
- const char *value = afb_req_value(request, "event");
- if(!value)
- {
- HMI_ERROR("cloudproxy-service", "Can't subscribe: event name is not defined");
- return false;
- }
- std::string req_event{value};
- HMI_NOTICE("cloudproxy-service", "subscribe req: appid '%s', event '%s'", appid.c_str(), req_event.c_str());
-
- if (!isSupportedEvent(req_event))
- {
- HMI_ERROR("cloudproxy-service", "event '%s' is not supported", req_event.c_str());
- return false;
- }
-
- ClientCtx* ctx = addClient(request, appid);
- ctx->subs_events.insert(req_event);
- if(!ctx->subscription)
- {
- if(afb_req_subscribe(request, ctx->event) == 0)
- {
- ctx->subscription = true;
- }
- else
- {
- HMI_ERROR("cloudproxy-service", "API error in afb_req_subscribe");
- return false;
- }
- }
-
- return true;
- }
- else if (verb == "unsubscribe")
- {
- const char *value = afb_req_value(request, "event");
- if(!value)
- {
- HMI_ERROR("cloudproxy-service", "Can't unsubscribe: event name is not defined");
- return false;
- }
- std::string req_event{value};
- HMI_NOTICE("cloudproxy-service", "unsubscribe req: appid '%s', event '%s'", appid.c_str(), req_event.c_str());
-
- ClientCtx* ctx{client_it->second};
- ctx->subs_events.erase(req_event);
-
- if(ctx->subs_events.empty())
- {
- if (afb_req_unsubscribe(request, ctx->event) != 0)
- HMI_ERROR("cloudproxy-service", "API error in afb_req_unsubscribe");
-
- ctx->subscription = false;
- }
-
- return true;
- }
-
- HMI_NOTICE("cloudproxy-service", "Unsupported verb '%s'", verb.c_str());
- return false;
-}
-
-
-bool ClientManager::isSupportedEvent(const std::string& event)
-{
- const std::set<std::string> event_list{
- "sendMessageConfirmation",
- "receivedMessage"
- };
-
- return (event_list.end() != event_list.find(event));
-}
-
-
-bool ClientManager::emitReceivedMessage(const std::string& appid, const std::string& data)
-{
- std::lock_guard<std::mutex> lock(this->m_mutex);
-
- auto it = m_clients.find(appid);
- if (it == m_clients.end())
- {
- HMI_WARNING("cloudproxy-service", "Client with appid '%s' is not present in list", appid.c_str());
- // print app list
- for (const auto& i : m_clients)
- HMI_DEBUG("cloudproxy-service", "Client list: appid '%s' - '%s'", i.first.c_str(), i.second->appid.c_str());
-
- return false;
- }
-
- json_object* push_obj = json_object_new_object();
- json_object_object_add(push_obj, "type", json_object_new_string("receivedMessage"));
- json_object_object_add(push_obj, "data", json_object_new_string(data.c_str()));
- return (0 == afb_event_push(it->second->event, push_obj));
-}
-
-bool ClientManager::emitSendMessageConfirmation(const std::string& appid, bool result)
-{
- std::lock_guard<std::mutex> lock(this->m_mutex);
-
- auto it = m_clients.find(appid);
- if (it == m_clients.end())
- {
- HMI_WARNING("cloudproxy-service", "Client with appid '%s' is not present in list", appid.c_str());
- // print app list
- for (const auto& i : m_clients)
- HMI_DEBUG("cloudproxy-service", "Client list: appid '%s' - '%s'", i.first.c_str(), i.second->appid.c_str());
-
- return false;
- }
-
- json_object* push_obj = json_object_new_object();
- json_object_object_add(push_obj, "type", json_object_new_string("sendMessageConfirmation"));
- json_object_object_add(push_obj, "result", json_object_new_boolean(result));
- return (0 == afb_event_push(it->second->event, push_obj));
-}
+/* + * Copyright (C) 2020 MERA + * + * 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 "ClientManager.h" +#include "CloudType.h" + +#include <set> +#include <json-c/json.h> +#include <afb/afb-binding.h> // for AFB_* logger + +// static +void ClientManager::cbRemoveClientCtx(void *data) +{ + ClientManager::instance().removeClient((ClientManager::ClientCtx*)data); +} + + +// static +ClientManager& ClientManager::instance() +{ + static ClientManager instance; + return instance; +} + + +ClientManager::~ClientManager() +{ + std::lock_guard<std::mutex> lock(this->m_mutex); + for (auto cl : m_clients) + delete cl.second; +} + + +ClientManager::ClientCtx* ClientManager::addClient(afb_req_t req, const std::string& appid) +{ + ClientCtx* ctx = (ClientCtx*)afb_req_context_get(req); + if (!ctx) + { + AFB_NOTICE("create new session for %s", appid.c_str()); + ctx = new ClientCtx{appid, afb_api_make_event(req->api, appid.c_str())}; + afb_req_session_set_LOA(req, 1); + afb_req_context_set(req, ctx, cbRemoveClientCtx); + } + + m_clients[appid] = ctx; + return ctx; +} + + +void ClientManager::removeClient(ClientCtx* ctx) +{ + if(!ctx) + { + AFB_ERROR("data is nullptr"); + return; + } + + AFB_NOTICE("remove app %s", ctx->appid.c_str()); + std::lock_guard<std::mutex> lock(this->m_mutex); + + auto it = m_clients.find(ctx->appid); + if (it != m_clients.end()) + { + delete it->second; + m_clients.erase(it); + } +} + + +bool ClientManager::handleRequest(afb_req_t request, const std::string& verb, const std::string& appid) +{ + AFB_NOTICE("handleRequest: verb='%s', appid='%s'", verb.c_str(), appid.c_str()); + std::lock_guard<std::mutex> lock(this->m_mutex); + + if (appid.empty()) + { + AFB_ERROR("appid is empty"); + return false; + } + + auto client_it = m_clients.find(appid); + if (verb != "subscribe" && client_it == m_clients.end()) + { + AFB_NOTICE("client with appid '%s' is not registered", appid.c_str()); + return false; + } + + if (verb == "subscribe") + { + const char *value = afb_req_value(request, "event"); + if(!value) + { + AFB_ERROR("Can't subscribe: event name is not defined"); + return false; + } + std::string req_event{value}; + AFB_NOTICE("subscribe req: appid '%s', event '%s'", appid.c_str(), req_event.c_str()); + + if (!isSupportedEvent(req_event)) + { + AFB_ERROR("event '%s' is not supported", req_event.c_str()); + return false; + } + + ClientCtx* ctx = addClient(request, appid); + ctx->subs_events.insert(req_event); + if(!ctx->subscription) + { + if(afb_req_subscribe(request, ctx->event) == 0) + { + ctx->subscription = true; + } + else + { + AFB_ERROR("API error in afb_req_subscribe"); + return false; + } + } + + return true; + } + else if (verb == "unsubscribe") + { + const char *value = afb_req_value(request, "event"); + if(!value) + { + AFB_ERROR("Can't unsubscribe: event name is not defined"); + return false; + } + std::string req_event{value}; + AFB_NOTICE("unsubscribe req: appid '%s', event '%s'", appid.c_str(), req_event.c_str()); + + ClientCtx* ctx{client_it->second}; + ctx->subs_events.erase(req_event); + + if(ctx->subs_events.empty()) + { + if (afb_req_unsubscribe(request, ctx->event) != 0) + AFB_ERROR("API error in afb_req_unsubscribe"); + + ctx->subscription = false; + } + + return true; + } + + AFB_NOTICE("Unsupported verb '%s'", verb.c_str()); + return false; +} + + +bool ClientManager::isSupportedEvent(const std::string& event) +{ + const std::set<std::string> event_list{ + "sendMessageConfirmation", + "receivedMessage" + }; + + return (event_list.end() != event_list.find(event)); +} + + +bool ClientManager::emitReceivedMessage(const std::string& appid, const std::string& cloud_type, const std::string& data) +{ + std::lock_guard<std::mutex> lock(this->m_mutex); + + auto it = m_clients.find(appid); + if (it == m_clients.end()) + { + AFB_WARNING("Client with appid '%s' is not present in list", appid.c_str()); + // print app list + for (const auto& i : m_clients) + AFB_DEBUG("Client list: appid '%s' - '%s'", i.first.c_str(), i.second->appid.c_str()); + + return false; + } + + json_object* push_obj = json_object_new_object(); + json_object_object_add(push_obj, "type", json_object_new_string("receivedMessage")); + json_object_object_add(push_obj, "cloud_type", json_object_new_string(cloud_type.c_str())); + json_object_object_add(push_obj, "data", json_object_new_string(data.c_str())); + return (0 == afb_event_push(it->second->event, push_obj)); +} + +bool ClientManager::emitSendMessageConfirmation(const std::string& appid, const std::string& cloud_type, bool result) +{ + std::lock_guard<std::mutex> lock(this->m_mutex); + + auto it = m_clients.find(appid); + if (it == m_clients.end()) + { + AFB_WARNING("Client with appid '%s' is not present in list", appid.c_str()); + // print app list + for (const auto& i : m_clients) + AFB_DEBUG("Client list: appid '%s' - '%s'", i.first.c_str(), i.second->appid.c_str()); + + return false; + } + + json_object* push_obj = json_object_new_object(); + json_object_object_add(push_obj, "type", json_object_new_string("sendMessageConfirmation")); + json_object_object_add(push_obj, "cloud_type", json_object_new_string(cloud_type.c_str())); + json_object_object_add(push_obj, "result", json_object_new_boolean(result)); + return (0 == afb_event_push(it->second->event, push_obj)); +} diff --git a/src/ClientManager.h b/src/ClientManager.h index a1038e1..d6348d1 100755 --- a/src/ClientManager.h +++ b/src/ClientManager.h @@ -1,59 +1,59 @@ -/*
- * Copyright (C) 2020 MERA
- *
- * 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.
- */
-
-#pragma once
-
-#include <afb/afb-binding.h>
-
-#include <map>
-#include <unordered_set>
-#include <mutex>
-#include <string>
-
-class ClientManager
-{
-public:
- struct ClientCtx
- {
- std::string appid;
- afb_event_t event;
- std::unordered_set<std::string> subs_events;
- bool subscription{false};
- };
-
- ClientManager() = default;
- ~ClientManager();
- ClientManager(const ClientManager&) = delete;
- void operator=(const ClientManager&) = delete;
-
- bool handleRequest(afb_req_t request, const std::string& verb, const std::string& appid);
- bool emitReceivedMessage(const std::string& appid, const std::string& data);
- bool emitSendMessageConfirmation(const std::string& appid, bool result);
-
- static ClientManager& instance();
-
- // don't use it directly
- static void cbRemoveClientCtx(void *data);
-
-protected:
- ClientCtx* addClient(afb_req_t req, const std::string& appid);
- void removeClient(ClientCtx* ctx);
- bool isSupportedEvent(const std::string& event);
-
-protected:
- std::mutex m_mutex;
- std::map<std::string, ClientCtx*> m_clients;
-};
+/* + * Copyright (C) 2020 MERA + * + * 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. + */ + +#pragma once + +#include <afb/afb-binding.h> + +#include <map> +#include <unordered_set> +#include <mutex> +#include <string> + +class ClientManager +{ +public: + struct ClientCtx + { + std::string appid; + afb_event_t event; + std::unordered_set<std::string> subs_events{}; + bool subscription{false}; + }; + + ClientManager() = default; + ~ClientManager(); + ClientManager(const ClientManager&) = delete; + void operator=(const ClientManager&) = delete; + + bool handleRequest(afb_req_t request, const std::string& verb, const std::string& appid); + bool emitReceivedMessage(const std::string& appid, const std::string& cloud_type, const std::string& data); + bool emitSendMessageConfirmation(const std::string& appid, const std::string& cloud_type, bool result); + + static ClientManager& instance(); + + // don't use it directly + static void cbRemoveClientCtx(void *data); + +protected: + ClientCtx* addClient(afb_req_t req, const std::string& appid); + void removeClient(ClientCtx* ctx); + bool isSupportedEvent(const std::string& event); + +protected: + std::mutex m_mutex; + std::map<std::string, ClientCtx*> m_clients; +}; diff --git a/src/CloudClient.h b/src/CloudClient.h new file mode 100644 index 0000000..f59906f --- /dev/null +++ b/src/CloudClient.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2020 MERA + * + * 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. + */ + +#pragma once + +#include <string> + +typedef struct _GKeyFile GKeyFile; + +class CloudClient +{ +public: + CloudClient() = default; + virtual ~CloudClient() = default; + CloudClient(const CloudClient&) = delete; + CloudClient& operator= (const CloudClient&) = delete; + + virtual bool sendMessage(const std::string& appid, const std::string& data) = 0; + virtual bool createConnection() = 0; + + virtual bool enabled() const = 0; + virtual bool connected() const = 0; + virtual bool loadConf(GKeyFile* conf_file) = 0; +}; diff --git a/src/CloudType.h b/src/CloudType.h new file mode 100644 index 0000000..fd4f801 --- /dev/null +++ b/src/CloudType.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2020 MERA + * + * 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. + */ + +#pragma once + +#include <string> + +struct CloudType +{ + static constexpr const char* Azure{"azure"}; + static constexpr const char* Aws {"aws"}; + + inline static bool isSupported(const std::string& type) + { + return (type == Azure || type == Aws); + } +}; diff --git a/src/cloudproxy-bindings.cpp b/src/cloudproxy-bindings.cpp index 1a40730..c69e9c7 100755 --- a/src/cloudproxy-bindings.cpp +++ b/src/cloudproxy-bindings.cpp @@ -20,37 +20,20 @@ #include <memory> #include <algorithm> -#include <iothub.h> -#include <iothub_device_client.h> -#include <iothub_client_options.h> -#include <iothub_message.h> -#include <iothubtransportmqtt.h> -#include <azure_c_shared_utility/threadapi.h> // ThreadAPI_Sleep() -#include <azure_c_shared_utility/tickcounter.h> // tickcounter_ms_t - -#include "hmi-debug.h" #include "utils.h" #include "ClientManager.h" +#include "CloudType.h" +#include "AwsClient.h" +#include "AzureClient.h" #include <glib.h> -static const char* API_name{"cloudproxy"}; - -static std::string g_connectionString; -static IOTHUB_DEVICE_CLIENT_HANDLE g_device_handle{nullptr}; -static bool g_iot_inited{false}; - - +namespace +{ -static utils::scope_exit g_destroy([]{ - if (g_iot_inited) - { - if (g_device_handle) - IoTHubDeviceClient_Destroy(g_device_handle); +std::map<std::string, std::unique_ptr<CloudClient>> g_clouds; - IoTHub_Deinit(); - } -}); +} static bool loadConf() @@ -60,7 +43,7 @@ static bool loadConf() const char *p = getenv("AFM_APP_INSTALL_DIR"); if(!p) { - HMI_ERROR("cloudproxy-service", "AFM_APP_INSTALL_DIR is not set, try to find conf in %s", DEFAULT_ETC_PATH); + AFB_ERROR("AFM_APP_INSTALL_DIR is not set, try to find conf in %s", DEFAULT_ETC_PATH); p = DEFAULT_ETC_PATH; } @@ -70,273 +53,127 @@ static bool loadConf() g_autoptr(GKeyFile) conf_file = g_key_file_new(); g_autoptr(GError) error = nullptr; + AFB_DEBUG("Load config file: %s", conf_path.c_str()); if (!conf_file || !g_key_file_load_from_file(conf_file, conf_path.c_str(), G_KEY_FILE_NONE, &error)) { - HMI_ERROR("cloudproxy-service", "can't load file %s", conf_path.c_str()); - return false; - } - - g_autofree gchar *value = g_key_file_get_string(conf_file, "AzureCloudConnection", "DeviceConnectionString", &error); - if (value == nullptr) - { - HMI_ERROR("cloudproxy-service", "can't read DeviceConnectionString from config %d", conf_path.c_str()); - return false; - } - - g_connectionString = value; - if (g_connectionString.empty()) - { - HMI_ERROR("cloudproxy-service", "DeviceConnectionString is empty"); + AFB_ERROR("Can't load file %s", conf_path.c_str()); return false; } - return true; -} - - -//-------------- Iot callbacks -static void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* user_context) -{ - HMI_NOTICE("cloudproxy-service", "%s called: result %d, reason %d", __FUNCTION__, result, reason); - - (void)reason; - (void)user_context; - // This sample DOES NOT take into consideration network outages. - if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED && reason == IOTHUB_CLIENT_CONNECTION_OK) - { - HMI_NOTICE("cloudproxy-service", "The device client is connected to iothub"); - } - else - { - HMI_NOTICE("cloudproxy-service", "The device client has been disconnected"); - } -} - -static IOTHUBMESSAGE_DISPOSITION_RESULT receive_msg_callback(IOTHUB_MESSAGE_HANDLE message, void* user_context) -{ - HMI_NOTICE("cloudproxy-service", "%s called", __FUNCTION__); - (void)user_context; - const char* messageId; - const char* correlationId; - - IOTHUBMESSAGE_CONTENT_TYPE content_type = IoTHubMessage_GetContentType(message); - - if (content_type == IOTHUBMESSAGE_BYTEARRAY) + //-- Azure parameters: { - const unsigned char* buff_msg; - size_t buff_len; + std::unique_ptr<CloudClient> azure{new AzureClient}; + if (!azure->loadConf(conf_file)) + return false; - if (IoTHubMessage_GetByteArray(message, &buff_msg, &buff_len) != IOTHUB_MESSAGE_OK) - { - HMI_ERROR("cloudproxy-service", "Failure retrieving byte array message"); - } - else - { - HMI_NOTICE("cloudproxy-service", "Received Binary message, size %d, data '%.*s'", (int)buff_len, (int)buff_len, buff_msg); - } - - const char* app_id = IoTHubMessage_GetProperty(message, "application_id"); - HMI_NOTICE("cloudproxy-service", "Received property 'application_id': %s", (app_id ? app_id : "<unavailable>")); - - if (app_id && app_id[0]) - ClientManager::instance().emitReceivedMessage(app_id, std::string((const char*)buff_msg, buff_len)); - else - HMI_ERROR("cloudproxy-service", "Can't emit SendMessageConfirmation: appid is not valid"); - } - else if (content_type == IOTHUBMESSAGE_STRING) - { - const char* string_msg = IoTHubMessage_GetString(message); - if (string_msg == nullptr) - { - HMI_NOTICE("cloudproxy-service", "Failure retrieving String message"); - } - else - { - HMI_NOTICE("cloudproxy-service", "Received String message, size %d, data '%s'", strlen(string_msg), string_msg); - } - - const char* app_id = IoTHubMessage_GetProperty(message, "application_id"); - HMI_NOTICE("cloudproxy-service", "Received property 'application_id': %s", (app_id ? app_id : "<unavailable>")); - - if (app_id && app_id[0]) - ClientManager::instance().emitReceivedMessage(app_id, string_msg); + if (azure->enabled()) + g_clouds[CloudType::Azure] = std::move(azure); else - HMI_ERROR("cloudproxy-service", "Can't emit SendMessageConfirmation: appid is not valid"); - } - else - { - HMI_ERROR("cloudproxy-service", "Unsupported message content type"); - } - - return IOTHUBMESSAGE_ACCEPTED; -} - - -static int device_method_callback(const char* method_name, const unsigned char* payload, size_t size, unsigned char** response, size_t* resp_size, void* userContextCallback) -{ - HMI_NOTICE("cloudproxy-service", "%s called, method_name %s", __FUNCTION__, method_name); - - const char* device_id = (const char*)userContextCallback; - char* end = nullptr; - int newInterval; - - int status = 501; - const char* RESPONSE_STRING = "{ \"Response\": \"Unknown method requested.\" }"; - - HMI_NOTICE("cloudproxy-service", "Device Method called for device %s", device_id); - HMI_NOTICE("cloudproxy-service", "Device Method name: %s", method_name); - HMI_NOTICE("cloudproxy-service", "Device Method payload: %.*s", (int)size, (const char*)payload); - - HMI_NOTICE("cloudproxy-service", "Response status: %d", status); - HMI_NOTICE("cloudproxy-service", "Response payload: %s", RESPONSE_STRING); - - *resp_size = strlen(RESPONSE_STRING); - if ((*response = (unsigned char*)malloc(*resp_size)) == NULL) - { - status = -1; - } - else - { - memcpy(*response, RESPONSE_STRING, *resp_size); + g_clouds[CloudType::Azure].reset(); } - return status; -} - - -static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) -{ - HMI_NOTICE("cloudproxy-service", "%s called, result %d", __FUNCTION__, result); - (void)userContextCallback; - // When a message is sent this callback will get invoked - - HMI_NOTICE("cloudproxy-service", "Confirmation callback result %s", MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); - - const char* appid = (const char*)userContextCallback; - if (!appid || !appid[0]) + //-- AWS parameters: { - HMI_ERROR("cloudproxy-service", "Confirmation callback: appid is not set"); - - if (userContextCallback) - free(userContextCallback); + std::unique_ptr<CloudClient> aws{new AwsClient}; + if (!aws->loadConf(conf_file)) + return false; - return; - } - - ClientManager::instance().emitSendMessageConfirmation(appid, result == IOTHUB_CLIENT_CONFIRMATION_OK); - free(userContextCallback); -} -//-------------- - -//-------------- help functions -static bool createConnection() -{ - HMI_NOTICE("cloudproxy-service", "%s called", __FUNCTION__); - - if (g_device_handle) - { - HMI_WARNING("cloudproxy-service", "connection already created"); - return true; + if (aws->enabled()) + g_clouds[CloudType::Aws] = std::move(aws); + else + g_clouds[CloudType::Aws].reset(); } - g_device_handle = IoTHubDeviceClient_CreateFromConnectionString(g_connectionString.c_str(), MQTT_Protocol); - if (!g_device_handle) + if (!std::any_of(g_clouds.begin(), g_clouds.end(), [](const auto& c){ return !!c.second; })) { - HMI_ERROR("cloudproxy-service", "Failure creating IoTHubDeviceClient device"); + AFB_ERROR("All cloud connection types are disabled by configuration"); return false; } - bool traceOn = false; - IoTHubDeviceClient_SetOption(g_device_handle, OPTION_LOG_TRACE, &traceOn); - IoTHubDeviceClient_SetConnectionStatusCallback(g_device_handle, connection_status_callback, nullptr); - IoTHubDeviceClient_SetMessageCallback(g_device_handle, receive_msg_callback, nullptr); - IoTHubDeviceClient_SetDeviceMethodCallback(g_device_handle, device_method_callback, nullptr); - - tickcounter_ms_t ms_delay = 10; - IoTHubDeviceClient_SetOption(g_device_handle, OPTION_DO_WORK_FREQUENCY_IN_MS, &ms_delay); // DoWork multithread - return true; } -//-------------- - static void pingSample(afb_req_t request) { static int pingcount = 0; afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount); - HMI_NOTICE("cloudproxy-service", "Verbosity macro at level notice invoked at ping invocation count = %d", pingcount); + AFB_NOTICE("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount); pingcount++; } -static bool initAzureSdk() -{ - //Allow program to try to establish connection several times - if (!g_iot_inited) - { - if(IoTHub_Init()) - { - HMI_ERROR("cloudproxy-service","Azure IoTHub_Init() failed"); - } - else - { - g_iot_inited = true; - } - } - - return g_iot_inited; -} - static void sendMessage(afb_req_t request) { - HMI_NOTICE("cloudproxy-service", "%s called", __FUNCTION__); + AFB_NOTICE("%s called", __FUNCTION__); json_object* object = afb_req_json(request); if (!object) { - HMI_ERROR("cloudproxy-service", "Can't parse request"); + AFB_ERROR("Can't parse request"); afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); return; } const std::string appid{utils::get_application_id(request)}; - std::string data; - json_object *obj_data; - if(!json_object_object_get_ex(object, "data", &obj_data)) + if (appid.empty()) { - HMI_ERROR("cloudproxy-service", "can't obtain application_id or data from request"); + AFB_ERROR("can't obtain application_id from request"); + afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); return; } - data = json_object_get_string(obj_data); - if (!g_device_handle && !createConnection()) + auto read_string = [object](const char* tag, std::string& read_to) + { + json_object* jobj{nullptr}; + if (!json_object_object_get_ex(object, tag, &jobj)) + { + AFB_ERROR("can't obtain %s from request", tag); + return false; + } + read_to = json_object_get_string(jobj); + return true; + }; + + std::string cloud_type; + if (!read_string("cloud_type", cloud_type)) { - HMI_ERROR("cloudproxy-service", "Can't create connection to cloud"); afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); return; } - IOTHUB_MESSAGE_HANDLE message_handle = IoTHubMessage_CreateFromString(data.c_str()); + std::string data; + if (!read_string("data", data)) + { + afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); + return; + } - utils::scope_exit message_handle_destroy([&message_handle](){ - // The message is copied to the sdk, so the we can destroy it - if (message_handle) - IoTHubMessage_Destroy(message_handle); - }); + if (!CloudType::isSupported(cloud_type)) + { + AFB_ERROR("Unsupported cloud type is requested: '%s'", cloud_type.c_str()); + afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); + return; + } - if (!message_handle) + if (!g_clouds[cloud_type]) { - HMI_ERROR("cloudproxy-service", "Can't create IoTHubMessage message"); + AFB_ERROR("%s cloud connection is disabled by config", cloud_type.c_str()); afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); return; } - IoTHubMessage_SetProperty(message_handle, "application_id", appid.c_str()); + CloudClient* client{g_clouds[cloud_type.c_str()].get()}; + if (!client->connected() && !client->createConnection()) + { + AFB_ERROR("Can't create connection to %s cloud", cloud_type.c_str()); + afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); + return; + } - if (IoTHubDeviceClient_SendEventAsync(g_device_handle, message_handle, send_confirm_callback, strdup(appid.c_str()))) + if (!client->sendMessage(appid, data)) { - HMI_ERROR("cloudproxy-service", "Can't send IoTHubMessage message"); + AFB_ERROR("Can't send message to %s cloud", cloud_type.c_str()); afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); return; } @@ -347,19 +184,19 @@ static void sendMessage(afb_req_t request) static void subscribe(afb_req_t request) { - HMI_NOTICE("cloudproxy-service", "%s called", __FUNCTION__); + AFB_NOTICE("%s called", __FUNCTION__); std::string req_appid{utils::get_application_id(request)}; if(req_appid.empty()) { - HMI_ERROR("cloudproxy-service", "Can't subscribe: empty appid"); + AFB_ERROR("Can't subscribe: empty appid"); afb_req_fail_f(request, "%s failed: application_id is not defined in request", __FUNCTION__); return; } if (!ClientManager::instance().handleRequest(request, __FUNCTION__, req_appid)) { - HMI_ERROR("cloudproxy-service", "%s failed in handleRequest", __FUNCTION__); + AFB_ERROR("%s failed in handleRequest", __FUNCTION__); afb_req_fail_f(request, "%s failed", __FUNCTION__); } else @@ -370,19 +207,19 @@ static void subscribe(afb_req_t request) static void unsubscribe(afb_req_t request) { - HMI_NOTICE("cloudproxy-service", "%s called", __FUNCTION__); + AFB_NOTICE("%s called", __FUNCTION__); std::string req_appid{utils::get_application_id(request)}; if(req_appid.empty()) { - HMI_ERROR("cloudproxy-service", "Can't unsubscribe: empty appid"); + AFB_ERROR("Can't unsubscribe: empty appid"); afb_req_fail_f(request, "%s failed: application_id is not defined in request", __FUNCTION__); return; } if (!ClientManager::instance().handleRequest(request, __FUNCTION__, req_appid)) { - HMI_ERROR("cloudproxy-service", "%s failedin handleRequest", __FUNCTION__); + AFB_ERROR("%s failedin handleRequest", __FUNCTION__); afb_req_fail_f(request, "%s failed", __FUNCTION__); } else @@ -406,17 +243,11 @@ static const afb_verb_t verbs[]= { static int preinit(afb_api_t api) { - HMI_NOTICE("cloudproxy-service", "binding preinit (was register)"); + AFB_NOTICE("binding preinit (was register)"); if (!loadConf()) { - HMI_ERROR("cloudproxy-service", "Can't load configuration file or configuration is wrong"); - return -1; - } - - if (!initAzureSdk()) - { - HMI_ERROR("cloudproxy-service", "Can't initialize Azure SDK"); + AFB_ERROR("Can't load configuration file or configuration is wrong"); return -1; } @@ -426,8 +257,8 @@ static int preinit(afb_api_t api) static int init(afb_api_t api) { - HMI_NOTICE("cloudproxy-service","binding init"); - return (g_iot_inited ? 0 : -1); + AFB_NOTICE("binding init"); + return 0; } const afb_binding_t afbBindingExport = { diff --git a/src/export.map b/src/export.map deleted file mode 100755 index f3961c0..0000000 --- a/src/export.map +++ /dev/null @@ -1 +0,0 @@ -{ global: afbBindingV*; local: *; };
\ No newline at end of file diff --git a/src/hmi-debug.h b/src/hmi-debug.h deleted file mode 100755 index c8e8638..0000000 --- a/src/hmi-debug.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2017 TOYOTA MOTOR CORPORATION - * - * 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 __HMI_DEBUG_H__ -#define __HMI_DEBUG_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -#include <time.h> -#include <stdio.h> -#include <stdarg.h> -#include <string.h> -#include <stdlib.h> - -#include <sys/types.h> // gettid() -#include <unistd.h> -#include <sys/syscall.h> - - enum LOG_LEVEL{ - LOG_LEVEL_NONE = 0, - LOG_LEVEL_ERROR, - LOG_LEVEL_WARNING, - LOG_LEVEL_NOTICE, - LOG_LEVEL_INFO, - LOG_LEVEL_DEBUG, - LOG_LEVEL_MAX = LOG_LEVEL_DEBUG - }; - -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) - -#define HMI_ERROR(prefix, args,...) _HMI_LOG(LOG_LEVEL_ERROR, __FILENAME__, __FUNCTION__, __LINE__, prefix, args, ##__VA_ARGS__) -#define HMI_WARNING(prefix, args,...) _HMI_LOG(LOG_LEVEL_WARNING, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__) -#define HMI_NOTICE(prefix, args,...) _HMI_LOG(LOG_LEVEL_NOTICE, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__) -#define HMI_INFO(prefix, args,...) _HMI_LOG(LOG_LEVEL_INFO, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__) -#define HMI_DEBUG(prefix, args,...) _HMI_LOG(LOG_LEVEL_DEBUG, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__) - - static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"}; - - static void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...) - { - //SA: WARN: const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG")); - const int log_level = LOG_LEVEL_MAX; - if(log_level < level) - { - return; - } - - char *message; - struct timespec tp; - unsigned int time; - - clock_gettime(CLOCK_REALTIME, &tp); - time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000); - - va_list args; - va_start(args, log); - if (log == NULL || vasprintf(&message, log, args) < 0) - message = NULL; - fprintf(stderr, "[%10.3f] [%s %s] [%s, %s(), Line:%d] [thread %llx]>>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, (unsigned long long)syscall(__NR_gettid), message); - va_end(args); - free(message); - } - -#ifdef __cplusplus -} -#endif -#endif //__HMI_DEBUG_H__ diff --git a/src/utils.h b/src/utils.h index 9069c4a..fa28b09 100755 --- a/src/utils.h +++ b/src/utils.h @@ -1,54 +1,54 @@ -/*
- * Copyright (C) 2020 MERA
- *
- * 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.
- */
-
-#pragma once
-
-#include <functional>
-#include <string>
-
-#include <afb/afb-binding.h>
-
-namespace utils
-{
- class scope_exit
- {
- public:
- explicit scope_exit(std::function<void()> func)
- : m_f(func)
- {}
- ~scope_exit()
- {
- if (!!m_f)
- m_f();
- }
-
- private:
- std::function<void()> m_f;
- };
-
- std::string get_application_id(const afb_req_t request)
- {
- char *app_id = afb_req_get_application_id(request);
- std::string appid;
- if(app_id)
- {
- appid = std::string(app_id);
- free(app_id);
- }
-
- return appid;
- }
-}
+/* + * Copyright (C) 2020 MERA + * + * 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. + */ + +#pragma once + +#include <functional> +#include <string> + +#include <afb/afb-binding.h> + +namespace utils +{ + class scope_exit + { + public: + explicit scope_exit(std::function<void()> func) + : m_f(func) + {} + ~scope_exit() + { + if (!!m_f) + m_f(); + } + + private: + std::function<void()> m_f; + }; + + inline std::string get_application_id(const afb_req_t request) + { + char *app_id = afb_req_get_application_id(request); + std::string appid; + if(app_id) + { + appid = std::string(app_id); + free(app_id); + } + + return appid; + } +} |