From 0cecffd4565b52bd8d200f6bc9f4144f244a6515 Mon Sep 17 00:00:00 2001 From: maratsabitov Date: Fri, 8 May 2020 18:03:57 +0300 Subject: Introduce cloudproxy service Cloudproxy service allows applications to communicate with clouds. The commit includes the test applications as sample for real client imlementation. Bug-AGL: SPEC-3370 Change-Id: I82d8122e93d01451f4471cc20c706e75c16f1c29 Signed-off-by: maratsabitov --- .gitreview | 5 + CMakeLists.txt | 33 ++ LICENSE | 177 +++++++++ README.md | 50 +++ app_client_examples/README.md | 98 +++++ .../telemetry-cloud-app/app/TelemetryWorker.cpp | 116 ++++++ .../telemetry-cloud-app/app/TelemetryWorker.h | 48 +++ .../telemetry-cloud-app/app/app.pri | 3 + .../telemetry-cloud-app/app/app.pro | 35 ++ .../app/cloudproxy-client/cloudproxyclient.cpp | 261 ++++++++++++ .../app/cloudproxy-client/cloudproxyclient.h | 74 ++++ .../app/cloudproxy-client/hmi-debug.h | 70 ++++ .../images/AGL_HMI_Blue_Background_NoCar-01.png | Bin 0 -> 698586 bytes .../telemetry-cloud-app/app/images/images.qrc | 5 + .../telemetry-cloud-app/app/main.cpp | 122 ++++++ .../telemetry-cloud-app/app/mainwindow.cpp | 100 +++++ .../telemetry-cloud-app/app/mainwindow.h | 52 +++ .../telemetry-cloud-app/app/ui_mainwindow.h | 86 ++++ .../telemetry-cloud-app/autobuild/agl/autobuild | 110 +++++ .../telemetry-cloud-app/autobuild/linux/autobuild | 110 +++++ .../telemetry-cloud-app/package/config.xml | 18 + .../telemetry-cloud-app/package/icon.svg | 266 +++++++++++++ .../telemetry-cloud-app/package/package.pro | 28 ++ .../telemetry-cloud-app/telemetrycloudapp.pro | 3 + app_client_examples/test-cloud-app/app/app.pri | 3 + app_client_examples/test-cloud-app/app/app.pro | 34 ++ .../app/cloudproxy-client/cloudproxyclient.cpp | 261 ++++++++++++ .../app/cloudproxy-client/cloudproxyclient.h | 74 ++++ .../app/cloudproxy-client/hmi-debug.h | 70 ++++ .../images/AGL_HMI_Blue_Background_NoCar-01.png | Bin 0 -> 698586 bytes .../test-cloud-app/app/images/images.qrc | 5 + app_client_examples/test-cloud-app/app/main.cpp | 150 +++++++ .../test-cloud-app/app/mainwindow.cpp | 84 ++++ .../test-cloud-app/app/mainwindow.h | 55 +++ .../test-cloud-app/app/ui_mainwindow.h | 94 +++++ .../test-cloud-app/autobuild/agl/autobuild | 110 +++++ .../test-cloud-app/autobuild/linux/autobuild | 110 +++++ .../test-cloud-app/package/config.xml | 18 + .../test-cloud-app/package/icon.svg | 266 +++++++++++++ .../test-cloud-app/package/package.pro | 28 ++ .../test-cloud-app/testcloudapp.pro | 3 + autobuild/agl/autobuild | 128 ++++++ autobuild/linux/autobuild | 128 ++++++ package/root/config.xml | 23 ++ package/root/icon.svg | 283 +++++++++++++ src/CMakeLists.txt | 77 ++++ src/ClientManager.cpp | 215 ++++++++++ src/ClientManager.h | 59 +++ src/cloudproxy-bindings.cpp | 441 +++++++++++++++++++++ src/config_sample.ini | 3 + src/export.map | 1 + src/hmi-debug.h | 82 ++++ src/utils.h | 54 +++ 53 files changed, 4729 insertions(+) create mode 100644 .gitreview create mode 100755 CMakeLists.txt create mode 100644 LICENSE create mode 100644 README.md create mode 100755 app_client_examples/README.md create mode 100755 app_client_examples/telemetry-cloud-app/app/TelemetryWorker.cpp create mode 100755 app_client_examples/telemetry-cloud-app/app/TelemetryWorker.h create mode 100755 app_client_examples/telemetry-cloud-app/app/app.pri create mode 100755 app_client_examples/telemetry-cloud-app/app/app.pro create mode 100755 app_client_examples/telemetry-cloud-app/app/cloudproxy-client/cloudproxyclient.cpp create mode 100755 app_client_examples/telemetry-cloud-app/app/cloudproxy-client/cloudproxyclient.h create mode 100755 app_client_examples/telemetry-cloud-app/app/cloudproxy-client/hmi-debug.h create mode 100755 app_client_examples/telemetry-cloud-app/app/images/AGL_HMI_Blue_Background_NoCar-01.png create mode 100755 app_client_examples/telemetry-cloud-app/app/images/images.qrc create mode 100755 app_client_examples/telemetry-cloud-app/app/main.cpp create mode 100755 app_client_examples/telemetry-cloud-app/app/mainwindow.cpp create mode 100755 app_client_examples/telemetry-cloud-app/app/mainwindow.h create mode 100755 app_client_examples/telemetry-cloud-app/app/ui_mainwindow.h create mode 100755 app_client_examples/telemetry-cloud-app/autobuild/agl/autobuild create mode 100755 app_client_examples/telemetry-cloud-app/autobuild/linux/autobuild create mode 100755 app_client_examples/telemetry-cloud-app/package/config.xml create mode 100755 app_client_examples/telemetry-cloud-app/package/icon.svg create mode 100755 app_client_examples/telemetry-cloud-app/package/package.pro create mode 100755 app_client_examples/telemetry-cloud-app/telemetrycloudapp.pro create mode 100755 app_client_examples/test-cloud-app/app/app.pri create mode 100755 app_client_examples/test-cloud-app/app/app.pro create mode 100755 app_client_examples/test-cloud-app/app/cloudproxy-client/cloudproxyclient.cpp create mode 100755 app_client_examples/test-cloud-app/app/cloudproxy-client/cloudproxyclient.h create mode 100755 app_client_examples/test-cloud-app/app/cloudproxy-client/hmi-debug.h create mode 100755 app_client_examples/test-cloud-app/app/images/AGL_HMI_Blue_Background_NoCar-01.png create mode 100755 app_client_examples/test-cloud-app/app/images/images.qrc create mode 100755 app_client_examples/test-cloud-app/app/main.cpp create mode 100755 app_client_examples/test-cloud-app/app/mainwindow.cpp create mode 100755 app_client_examples/test-cloud-app/app/mainwindow.h create mode 100755 app_client_examples/test-cloud-app/app/ui_mainwindow.h create mode 100755 app_client_examples/test-cloud-app/autobuild/agl/autobuild create mode 100755 app_client_examples/test-cloud-app/autobuild/linux/autobuild create mode 100755 app_client_examples/test-cloud-app/package/config.xml create mode 100755 app_client_examples/test-cloud-app/package/icon.svg create mode 100755 app_client_examples/test-cloud-app/package/package.pro create mode 100755 app_client_examples/test-cloud-app/testcloudapp.pro create mode 100755 autobuild/agl/autobuild create mode 100755 autobuild/linux/autobuild create mode 100755 package/root/config.xml create mode 100755 package/root/icon.svg create mode 100755 src/CMakeLists.txt create mode 100755 src/ClientManager.cpp create mode 100755 src/ClientManager.h create mode 100755 src/cloudproxy-bindings.cpp create mode 100644 src/config_sample.ini create mode 100755 src/export.map create mode 100755 src/hmi-debug.h create mode 100755 src/utils.h diff --git a/.gitreview b/.gitreview new file mode 100644 index 0000000..9bb3b58 --- /dev/null +++ b/.gitreview @@ -0,0 +1,5 @@ +[gerrit] +host=gerrit.automotivelinux.org +port=29418 +project=apps/agl-service-cloudproxy +defaultbranch=master diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..a7f89ba --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,33 @@ +########################################################################### +# 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. +########################################################################### + +project(CloudProxyService C CXX) + +cmake_minimum_required(VERSION 3.0) + +set(PROJECT_NAME "CloudProxy Service") +set(PROJECT_VERSION "1.0") +set(PROJECT_DESCRIPTION "CloudProxy Service") +set(PROJECT_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}) +set(PROJECT_LIBDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}) +set(binding_install_dir ${PREFIX}/lib/cloudproxy) +set(PROJECT_BINDINGDIR ${binding_install_dir}) + +# get pkgconfig +INCLUDE(FindPkgConfig) +link_libraries(-Wl,--as-needed -Wl,--gc-sections -Wl,--no-undefined) + +add_subdirectory(src) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f13a843 --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..876e80c --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# Cloud Proxy Service + +## Overview + +*Cloud proxy service* provides possibility to send and receive data to/from clouds. + +It is allowed to use *Cloud proxy service* from several application. +The messages/events separtion between application is done within service. + +## Verbs + +``` +| Name | Description | Data | +| ------------------ |:----------------------------------------|:----------------------------------------------------| +| sendMessage | send data to the cloud | application ID, JSON {"data":"message content"} | +| subscribe | subs. for particular application id | application ID extracted from msg | +| unsubscribe | unsubs. for particular application id | application ID extracted from msg | +| ping | verify connectivity to service | N/A | +``` +## Events + +``` +| Name | Description | +| ---------------------------- |:--------------------------------------------------------| +| sendMessageConfirmation | confirmation from cloud for previously sent message | +| receivedMessage | message from cloud to be propagated to the application | +``` + +## Configuration + +Cloud proxy service requires configuration of the cloud to be used. Configuration is stored in the **config.ini** file. + +By default the configuration file **config.ini** is searched stored in + +* $AFM_APP_INSTALL_DIR/etc/config.ini - **default one** +* /etc/cloudproxy-service/etc/config.ini + +At the moment only Azure cloud is supported. + +### Azure configuration +The configuration has to contain the connection string for device: **DeviceConnectionString** in +section **[AzureCloudConnection]** + +``` +[AzureCloudConnection] +DeviceConnectionString=My=Azure=Device=Connection=String +``` + + + diff --git a/app_client_examples/README.md b/app_client_examples/README.md new file mode 100755 index 0000000..8edaddd --- /dev/null +++ b/app_client_examples/README.md @@ -0,0 +1,98 @@ +# Test cloud applications # + +## Description + +The test-cloud-app and telemetry-cloud-app are examples of an agl-cloudproxy-service client. +They are used to demonstrate the possibilities of the cloudproxy service. + +## Applications + +* test-cloud-app: Simple application demonstrating the following possibilities: + * Sending the messages to cloud over cloud proxy server + * Receiving confirmations from cloud on the sent messages + * Receiving messages from cloud + + +* telemetry-cloud-app: Application which sends the GPS coordinates to the cloud + * Sending of the messages with telemetry to the cloud + * Receiving of the messages from the cloud + +Applications use CloudProxy client as high level interface for the messages sending and receiving + +## CloudProxy client + +CloudProxy client hides the communication with CloudProxy server. It incupsulates the following actions + +* Connection to **cloudporxy** over wsj1 websocket +* Subscription to the event +* Event loop handling + +**Note:** At the moment only two event types are supported +1. Event_SendMessageConfirmation +2. Event_ReceivedMessage + +### Usage of a Cloudproxy client: + +* Include the appropriate header to appliciation + + ``` + #include "cloudproxy-client/cloudproxyclient.h" + ``` + +* Initialization of the CloudProxy client inside the application: + +``` +CloudProxyClient* g_cloudproxyclient{nullptr}; + +g_cloudproxyclient = new CloudProxyClient(); +g_cloudproxyclient->init(port, token.c_str()); + +``` + +### Message handling from/to the CloudProxy server: + + +* Subsription to the Event_SendMessageConfirmation + + + +``` +g_cloudproxyclient->set_event_handler(CloudProxyClient::Event_SendMessageConfirmation, [](json_object* object){ + json_object *j_result; + if(!json_object_object_get_ex(object, "result", &j_result)) + { + qDebug("Can't read confirmation result"); + return; + } + + qDebug("Confirmation result %d", (int)json_object_get_boolean(j_result)); + }); + + ``` + + + +* Subsription to the Event_SendMessageConfirmation + +``` +g_cloudproxyclient->set_event_handler(CloudProxyClient::Event_ReceivedMessage, [](json_object* object) + json_object *event_data; + const char* data_str{nullptr}; + if(!json_object_object_get_ex(object, "data", &event_data) || + data_str = json_object_get_string(event_data)) == nullptr) + { + qDebug("Can't read event data"); + return; + } + + qDebug("Received data: %s", data_str); + }); + ``` + + +* Message sending: + + +``` +qDebug() << "sendMessage result: " << g_cloudproxyclient->sendMessage("{\"app_key\": \"app_value1111\"}"); +``` \ No newline at end of file diff --git a/app_client_examples/telemetry-cloud-app/app/TelemetryWorker.cpp b/app_client_examples/telemetry-cloud-app/app/TelemetryWorker.cpp new file mode 100755 index 0000000..2fb2a15 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/TelemetryWorker.cpp @@ -0,0 +1,116 @@ +/* + * 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 "TelemetryWorker.h" +#include +#include +#include +#include + +#include "cloudproxy-client/cloudproxyclient.h" + +/* +left up: 56.327 43.981 +right up: 56.327 44.03 +right bottom: 56.3 44.03 +left bottom: 56.3 43.981 + +start: 56.321674 44.006383 +top: 56.329702 44.021352 + +diff -0.008028 -0.014969 +coeff lon/lat = 1,8646 + + +https://gps-coordinates.org/distance-between-coordinates.php +*/ + + +static double drand(double min, double max) +{ + double f = (double)rand() / RAND_MAX; + return min + f * (max - min); +} + + +void TelemetryWorker::send() +{ + const double lat_top{56.329702}; + const double lon_lat_coef{1.8646}; + const double lat_step{0.001}, lon_step{lat_step*lon_lat_coef}; + + if (direction == 0 && lat >= lat_top) + { + direction = 1; + qDebug() << "Change direction to " << direction; + } + else if (direction == 1 && lat <= init_lat) + { + direction = 2; + qDebug() << "Change direction to " << direction; + } + else if (direction == 2 && lat <= (init_lat - (lat_top - init_lat))) + { + direction = 3; + qDebug() << "Change direction to " << direction; + } + else if (direction == 3 && lat >= init_lat) + { + direction = 0; + qDebug() << "Change direction to " << direction; + } + + + if (direction == 0) + { + lat += lat_step; + lon += lon_step; + } + else if (direction == 1) + { + lat -= lat_step; + lon += lon_step; + } + else if (direction == 2) + { + lat -= lat_step; + lon -= lon_step; + } + else if (direction == 3) + { + lat += lat_step; + lon -= lon_step; + } + + nlohmann::json jmsg{ + {"tele_ver", "1.0"}, + {"lat", lat}, + {"lon", lon}, + {"alt", (size_t)(alt + drand(0, 10))}, + {"speed", (size_t)(speed + drand(20, 30))}, + {"ts", time(nullptr)} + }; + + { + //WARN: remove + const auto& msg{jmsg.dump()}; + qDebug() << "TelemetryWorker: " << msg.c_str(); + } + + if (cloudproxyclient) + qDebug() << "TelemetryWorker: cloud sendMessage result: " << cloudproxyclient->sendMessage(jmsg.dump()); + +} diff --git a/app_client_examples/telemetry-cloud-app/app/TelemetryWorker.h b/app_client_examples/telemetry-cloud-app/app/TelemetryWorker.h new file mode 100755 index 0000000..11210cb --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/TelemetryWorker.h @@ -0,0 +1,48 @@ +/* + * 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 + +class CloudProxyClient; + +class TelemetryWorker : public QTimer +{ + Q_OBJECT +public: + TelemetryWorker(CloudProxyClient* cloudproxy, QObject* parent = nullptr) + : QTimer(parent), + cloudproxyclient{cloudproxy} + { + } + +public slots: + void send(); + +private: + CloudProxyClient* cloudproxyclient{nullptr}; + + int direction{0}; + const double init_lat{56.306}; + const double init_lon{44.070}; + double lat{init_lat}; + double lon{init_lon}; + double alt{300.0}; + double speed{500.0}; + uint32_t engine_rpm{3000}; + int32_t engine_temp{97}; +}; diff --git a/app_client_examples/telemetry-cloud-app/app/app.pri b/app_client_examples/telemetry-cloud-app/app/app.pri new file mode 100755 index 0000000..6d710e4 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/app.pri @@ -0,0 +1,3 @@ +TEMPLATE = app +QMAKE_LFLAGS += "-Wl,--hash-style=gnu -Wl,--as-needed" +DESTDIR = $${OUT_PWD}/../package/root/bin diff --git a/app_client_examples/telemetry-cloud-app/app/app.pro b/app_client_examples/telemetry-cloud-app/app/app.pro new file mode 100755 index 0000000..351d89d --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/app.pro @@ -0,0 +1,35 @@ +TARGET = telemetrycloudapp +QT += core gui gui-private + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +DEFINES += QT_DEPRECATED_WARNINGS + +SOURCES = main.cpp \ + mainwindow.cpp \ + TelemetryWorker.cpp \ + cloudproxy-client/cloudproxyclient.cpp + +HEADERS += mainwindow.h \ + ui_mainwindow.h \ + TelemetryWorker.h \ + cloudproxy-client/cloudproxyclient.h \ + cloudproxy-client/hmi-debug.h + +CONFIG += link_pkgconfig +PKGCONFIG += libhomescreen qlibwindowmanager qtappfw-core + +CONFIG(release, debug|release) { + QMAKE_POST_LINK = $(STRIP) --strip-unneeded $(TARGET) +} + +LIBS += -ljson-c -lafbwsc -lsystemd + +RESOURCES += \ + images/images.qrc + +include(app.pri) + +DISTFILES += \ + images/AGL_HMI_Blue_Background_NoCar-01.png + diff --git a/app_client_examples/telemetry-cloud-app/app/cloudproxy-client/cloudproxyclient.cpp b/app_client_examples/telemetry-cloud-app/app/cloudproxy-client/cloudproxyclient.cpp new file mode 100755 index 0000000..0a30446 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/cloudproxy-client/cloudproxyclient.cpp @@ -0,0 +1,261 @@ +/* + * 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 +#include + +#include "cloudproxyclient.h" +#include "hmi-debug.h" + + +static const char API_name[] = "cloudproxy"; + +const std::vector CloudProxyClient::m_api_list { + std::string("ping"), + std::string("createConnection"), + std::string("sendMessage"), + std::string("destroyConnection") + }; + +const std::vector CloudProxyClient::m_event_list { + std::string("sendMessageConfirmation"), + std::string("receivedMessage") + }; + +static void event_loop_run(sd_event* loop) +{ + sd_event_loop(loop); + sd_event_unref(loop); +} + +static void on_hangup(void *closure, afb_wsj1 *wsj) +{ + HMI_DEBUG("cloudproxyclient", "called"); + (void)closure; + (void)wsj; +} + +static void on_call(void *closure, const char *api, const char *verb, afb_wsj1_msg *msg) +{ + HMI_ERROR("cloudproxyclient", "this method should not be called"); + (void)closure; + (void)api; + (void)verb; + (void)msg; +} + +static void on_event(void* closure, const char* event, afb_wsj1_msg *msg) +{ + HMI_DEBUG("cloudproxyclient", "event [%s]", (event ? event: "")); + (void)closure; + + static_cast(closure)->on_event(nullptr, event, msg); +} + +static void on_reply(void *closure, afb_wsj1_msg *msg) +{ + HMI_DEBUG("cloudproxyclient", "called"); + (void)closure; + (void)msg; +} + + +CloudProxyClient::CloudProxyClient() +{ +} + +CloudProxyClient::~CloudProxyClient() +{ + if(m_websock) + { + afb_wsj1_unref(m_websock); + } + if(m_loop) + { + sd_event_exit(m_loop, 0); + } +} + +int CloudProxyClient::init(const int port, const std::string& token) +{ + int ret = 0; + if(port <= 0 && token.size() == 0) + { + HMI_ERROR("cloudproxyclient","port and token should be > 0, Initial port and token uses."); + return -1; + } + + { + m_loop = nullptr; + int ret = sd_event_new(&m_loop); + if(ret < 0) + { + HMI_ERROR("cloudproxyclient","Failed to create event loop"); + return -1; + } + + { + // enforce context to avoid initialization/goto error + std::thread th(event_loop_run, m_loop); + th.detach(); + } + + /* Initialize interface from websocket */ + m_itf.on_hangup = ::on_hangup; + m_itf.on_call = ::on_call; + m_itf.on_event = ::on_event; + + m_uri += "ws://localhost:" + std::to_string(port) + "/api?token=" + token; + + m_websock = afb_ws_client_connect_wsj1(m_loop, m_uri.c_str(), &m_itf, this); + if(!m_websock) + { + HMI_ERROR("cloudproxyclient","Failed to create websocket connection"); + return -1; + } + } + + HMI_DEBUG("cloudproxyclient", "Initialized"); + + return ret; +} + + +int CloudProxyClient::call(const std::string& verb, json_object* arg) +{ + int ret; + if(!m_websock) + { + return -1; + } + + if (verb.empty() || m_api_list.end() == std::find(m_api_list.begin(), m_api_list.end(), verb)) + { + HMI_ERROR("cloudproxyclient","verb [%s] doesn't exit", verb.c_str()); + return -1; + } + + ret = afb_wsj1_call_j(m_websock, API_name, verb.c_str(), arg, ::on_reply, this); + if (ret < 0) + { + HMI_ERROR("cloudproxyclient", "Failed to call verb:%s", verb.c_str()); + } + + return ret; +} + + +int CloudProxyClient::sendMessage(const std::string& data) +{ + if(!m_websock) + return -1; + + json_object* j_obj = json_object_new_object(); + json_object_object_add(j_obj, "data", json_object_new_string(data.c_str())); + + return this->call("sendMessage", j_obj); +} + +void CloudProxyClient::set_event_handler(enum EventType et, handler_func f) +{ + if (et > Event_Min && et < Event_Max) + { + switch (et) + { + case Event_SendMessageConfirmation: + this->subscribe(CloudProxyClient::m_event_list[0]); + break; + case Event_ReceivedMessage: + this->subscribe(CloudProxyClient::m_event_list[1]); + break; + default: + break; + } + + this->handlers[et] = std::move(f); + } +} + +int CloudProxyClient::subscribe(const std::string& event_name) +{ + if(!m_websock) + return -1; + + json_object* j_obj = json_object_new_object(); + json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str())); + + int ret = afb_wsj1_call_j(m_websock, API_name, "subscribe", j_obj, ::on_reply, this); + if (ret < 0) + HMI_ERROR("cloudproxyclient", "subscribe filed for '%s'", event_name.c_str()); + + return ret; +} + +int CloudProxyClient::unsubscribe(const std::string& event_name) +{ + if(!m_websock) + return -1; + + json_object* j_obj = json_object_new_object(); + json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str())); + + int ret = afb_wsj1_call_j(m_websock, API_name, "unsubscribe", j_obj, ::on_reply, this); + if (ret < 0) + HMI_ERROR("cloudproxyclient", "unsubscribe filed for '%s'", event_name.c_str()); + + return ret; +} + +void CloudProxyClient::on_event(void *closure, const char *event, afb_wsj1_msg *msg) +{ + HMI_DEBUG("cloudproxyclient", "event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg)); + (void) closure; + + if (strstr(event, API_name) == nullptr) + return; + + + json_object* ev_contents = afb_wsj1_msg_object_j(msg); + json_object *json_data; + if(!json_object_object_get_ex(ev_contents, "data", &json_data)) + { + HMI_ERROR("cloudproxyclient", "got ev_contents error."); + return; + } + + const char* event_type = nullptr; + json_object *json_event_type; + if(!json_object_object_get_ex(json_data, "type", &json_event_type) || + (event_type = json_object_get_string(json_event_type)) == nullptr) + { + HMI_ERROR("cloudproxyclient", "event_type is null."); + return; + } + + const std::string et{event_type}; + if (CloudProxyClient::m_event_list[0] == et) + { + auto i = this->handlers.find(Event_SendMessageConfirmation); + if (i != this->handlers.end()) + i->second(json_data); + } + else if (CloudProxyClient::m_event_list[1] == et) + { + auto i = this->handlers.find(Event_ReceivedMessage); + if (i != this->handlers.end()) + i->second(json_data); + } +} diff --git a/app_client_examples/telemetry-cloud-app/app/cloudproxy-client/cloudproxyclient.h b/app_client_examples/telemetry-cloud-app/app/cloudproxy-client/cloudproxyclient.h new file mode 100755 index 0000000..b333a14 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/cloudproxy-client/cloudproxyclient.h @@ -0,0 +1,74 @@ +/* + * 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 +#include +#include +#include +#include +#include + +extern "C" +{ +#include +#include +} + +class CloudProxyClient +{ +public: + using handler_func = std::function; + + enum EventType { + Event_Min, + Event_SendMessageConfirmation = 1, + Event_ReceivedMessage, + Event_Max + }; + + + CloudProxyClient(); + ~CloudProxyClient(); + + CloudProxyClient(const CloudProxyClient &) = delete; + CloudProxyClient &operator=(const CloudProxyClient &) = delete; + + int init(const int port, const std::string& token); + + int sendMessage(const std::string& data); + + void set_event_handler(enum EventType et, handler_func f); + + void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg); + + int subscribe(const std::string& event_name); + int unsubscribe(const std::string& event_name); + +private: + int call(const std::string& verb, struct json_object* arg); + +private: + static const std::vector m_api_list; + static const std::vector m_event_list; + + std::map handlers; + struct afb_wsj1_itf m_itf; + struct afb_wsj1* m_websock{nullptr}; + sd_event* m_loop{nullptr}; + std::string m_uri; +}; diff --git a/app_client_examples/telemetry-cloud-app/app/cloudproxy-client/hmi-debug.h b/app_client_examples/telemetry-cloud-app/app/cloudproxy-client/hmi-debug.h new file mode 100755 index 0000000..674f5df --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/cloudproxy-client/hmi-debug.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2018 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__ + +#include +#include +#include +#include +#include + +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, ...) +{ + const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG")); + 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] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message); + va_end(args); + free(message); +} + +#endif //__HMI_DEBUG_H__ diff --git a/app_client_examples/telemetry-cloud-app/app/images/AGL_HMI_Blue_Background_NoCar-01.png b/app_client_examples/telemetry-cloud-app/app/images/AGL_HMI_Blue_Background_NoCar-01.png new file mode 100755 index 0000000..a76b140 Binary files /dev/null and b/app_client_examples/telemetry-cloud-app/app/images/AGL_HMI_Blue_Background_NoCar-01.png differ diff --git a/app_client_examples/telemetry-cloud-app/app/images/images.qrc b/app_client_examples/telemetry-cloud-app/app/images/images.qrc new file mode 100755 index 0000000..78d9b77 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/images/images.qrc @@ -0,0 +1,5 @@ + + + AGL_HMI_Blue_Background_NoCar-01.png + + diff --git a/app_client_examples/telemetry-cloud-app/app/main.cpp b/app_client_examples/telemetry-cloud-app/app/main.cpp new file mode 100755 index 0000000..e0551fe --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/main.cpp @@ -0,0 +1,122 @@ +/* + * 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 +#include +#include +#include +#include +#include + +#include "mainwindow.h" +#include +#include + +#include "cloudproxy-client/cloudproxyclient.h" + + +CloudProxyClient* g_cloudproxyclient{nullptr}; + + +int main(int argc, char *argv[]) +{ + QString graphic_role = QString("telemetrycloud"); + + QApplication app(argc, argv); + QScopedPointer window; + + QCommandLineParser parser; + parser.addPositionalArgument("port", app.translate("main", "port for binding")); + parser.addPositionalArgument("secret", app.translate("main", "secret for binding")); + parser.addHelpOption(); + parser.addVersionOption(); + parser.process(app); + QStringList positionalArguments = parser.positionalArguments(); + + if (positionalArguments.length() == 2) + { + int port = positionalArguments.takeFirst().toInt(); + QString secret = positionalArguments.takeFirst(); + QUrl bindingAddress; + bindingAddress.setScheme(QStringLiteral("ws")); + bindingAddress.setHost(QStringLiteral("localhost")); + bindingAddress.setPort(port); + bindingAddress.setPath(QStringLiteral("/api")); + QUrlQuery query; + query.addQueryItem(QStringLiteral("token"), secret); + bindingAddress.setQuery(query); + + std::string token = secret.toStdString(); + LibHomeScreen* hs = new LibHomeScreen(); + QLibWindowmanager* qwm = new QLibWindowmanager(); + + // WindowManager + if(qwm->init(port,secret) != 0){ + exit(EXIT_FAILURE); + } + AGLScreenInfo screenInfo(qwm->get_scale_factor()); + window.reset(new MainWindow(screenInfo.scale_factor())); + + // Request a surface as described in layers.json windowmanager’s file + if (qwm->requestSurface(graphic_role) != 0) { + exit(EXIT_FAILURE); + } + // Create an event callback against an event type. Here a lambda is called when SyncDraw event occurs + qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm, &graphic_role](json_object *object) { + (void)object; + qDebug() << "Surface got syncDraw!"; + qwm->endDraw(graphic_role); + }); + + // HomeScreen + hs->init(port, token.c_str()); + // Set the event handler for Event_ShowWindow which will activate the surface for windowmanager + hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [qwm, &graphic_role](json_object *object){ + (void)object; + qDebug("Surface %s got showWindow\n", graphic_role.toStdString().c_str()); + qwm->activateWindow(graphic_role); + }); + + g_cloudproxyclient = new CloudProxyClient(); + g_cloudproxyclient->init(port, token.c_str()); + + g_cloudproxyclient->set_event_handler(CloudProxyClient::Event_ReceivedMessage, [&window](json_object* object){ + qDebug("CloudProxyClient::Event_ReceivedMessage: object ptr %p", object); + + const char* str = object ? json_object_to_json_string_ext(object, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY) : ""; + if (!str) + str = ""; + qDebug("Event_SendMessageConfirmation: %s", str); + + json_object *event_data; + const char* data_str{nullptr}; + if(!json_object_object_get_ex(object, "data", &event_data) || + (data_str = json_object_get_string(event_data)) == nullptr) + { + qDebug("Can't read event data"); + return; + } + + window->updateState(QString(data_str)); + qDebug("Application received data: %s", data_str); + }); + + window->show(); + qwm->slotActivateWindow(); + } + + return app.exec(); +} diff --git a/app_client_examples/telemetry-cloud-app/app/mainwindow.cpp b/app_client_examples/telemetry-cloud-app/app/mainwindow.cpp new file mode 100755 index 0000000..aecabc0 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/mainwindow.cpp @@ -0,0 +1,100 @@ +/* + * 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 "mainwindow.h" +#include "ui_mainwindow.h" +#include + +#include +#include +#include + + +#include "cloudproxy-client/cloudproxyclient.h" +extern CloudProxyClient* g_cloudproxyclient; + + +MainWindow::MainWindow(double scale_factor, QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(scale_factor, this); + + //AGL style + setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint); + this->setStyleSheet( + "background-image:url(:/images/AGL_HMI_Blue_Background_NoCar-01.png) 0 0 0 0 stretch stretch; background: transparent;" ); +} + +void MainWindow::updateState(QVariant recvMsg) +{ + if (!recvMsg.isValid()) + return; + + // expected format: {"message": "bla-bla"} + std::string cloud_msg; + try + { + const std::string& msg = recvMsg.toString().toStdString(); + if (msg.empty()) + { + qDebug() << "Received message is empty"; + } + else + { + const nlohmann::json& jmsg = nlohmann::json::parse(msg); + cloud_msg = jmsg["message"].get(); + } + } + catch (std::exception& ex) + { + qDebug() << "Can't parse incomming message: " << ex.what(); + } + + QString str = QString::asprintf("Cloud info: %s", cloud_msg.c_str()); + ui->stateLabel->setText(str); +} + +void MainWindow::startMsgButtonClick() +{ + qDebug() << "MainWindow::startMsgButtonClick()"; + + if (started) + ui->startMsgButton->setText("Start"); + else + ui->startMsgButton->setText("Stop"); + started = !started; + + if (worker) + { + worker->stop(); + worker.reset(); + qDebug() << "Worker destroyed"; + } + + if (started) + { + worker.reset(new TelemetryWorker(g_cloudproxyclient, this)); + connect(worker.get(), SIGNAL(timeout()), worker.get(), SLOT(send())); + worker->start(1001); + } +} + + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/app_client_examples/telemetry-cloud-app/app/mainwindow.h b/app_client_examples/telemetry-cloud-app/app/mainwindow.h new file mode 100755 index 0000000..ce21845 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/mainwindow.h @@ -0,0 +1,52 @@ +/* + * 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. + */ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include + +#include "TelemetryWorker.h" + + +namespace Ui { + class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(double scale_factor, QWidget *parent = 0); + ~MainWindow(); + +public slots: + void updateState(QVariant recvMsg); + +private slots: + void startMsgButtonClick(); + +private: + Ui::MainWindow *ui; + bool started{false}; + QScopedPointer worker; +}; + +#endif // MAINWINDOW_H diff --git a/app_client_examples/telemetry-cloud-app/app/ui_mainwindow.h b/app_client_examples/telemetry-cloud-app/app/ui_mainwindow.h new file mode 100755 index 0000000..5dbcbd5 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/app/ui_mainwindow.h @@ -0,0 +1,86 @@ +/* + * 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. + */ + +#ifndef UI_MAINWINDOW_H +#define UI_MAINWINDOW_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_MainWindow +{ +public: + QWidget *centralWidget{nullptr}; + QPushButton *startMsgButton{nullptr}; + QLabel *stateLabel{nullptr}; + + void setupUi(double scale_factor, QMainWindow *MainWindow) + { + if (MainWindow->objectName().isEmpty()) + MainWindow->setObjectName(QString::fromUtf8("MainWindow")); + MainWindow->resize(1080*scale_factor, 1400*scale_factor); + centralWidget = new QWidget(MainWindow); + centralWidget->setObjectName(QString::fromUtf8("centralWidget")); + + //QString style("color: white; font-size: 24px"); + QString style("color: white"); + + startMsgButton = new QPushButton(centralWidget); + startMsgButton->setObjectName(QString::fromUtf8("startMsgButton")); + startMsgButton->setGeometry(QRect((540-242/2)*scale_factor, 20*scale_factor, 242*scale_factor, 64*scale_factor)); + startMsgButton->setStyleSheet(style); + + stateLabel = new QLabel(centralWidget); + stateLabel->setObjectName(QString::fromUtf8("label")); + stateLabel->setGeometry(QRect(40*scale_factor, 180*scale_factor, 1000*scale_factor, 420*scale_factor)); + stateLabel->setWordWrap(true); + stateLabel->setAlignment(Qt::AlignTop); + stateLabel->setStyleSheet(style); + + MainWindow->setCentralWidget(centralWidget); + + QObject::connect(startMsgButton, SIGNAL(clicked()), MainWindow, SLOT(startMsgButtonClick())); + + retranslateUi(MainWindow); + + QMetaObject::connectSlotsByName(MainWindow); + } // setupUi + + void retranslateUi(QMainWindow *MainWindow) + { + MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", nullptr)); + startMsgButton->setText(QApplication::translate("MainWindow", "Start", nullptr)); + } // retranslateUi +}; + +namespace Ui { + class MainWindow: public Ui_MainWindow {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UI_MAINWINDOW_H diff --git a/app_client_examples/telemetry-cloud-app/autobuild/agl/autobuild b/app_client_examples/telemetry-cloud-app/autobuild/agl/autobuild new file mode 100755 index 0000000..bbbc13e --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/autobuild/agl/autobuild @@ -0,0 +1,110 @@ +#!/usr/bin/make -f +# Copyright (C) 2015 - 2018 "IoT.bzh" +# Copyright (C) 2020 Konsulko Group +# Author "Romain Forlot" +# +# 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. + +THISFILE := $(lastword $(MAKEFILE_LIST)) +ROOT_DIR := $(abspath $(dir $(THISFILE))/../..) + +# Build directories +# Note that the debug directory is defined in relation to the release +# directory (BUILD_DIR), this needs to be kept in mind if over-riding +# it and building that widget types, the specific widget type variable +# (e.g. BUILD_DIR_DEBUG) may also need to be specified to yield the +# desired output hierarchy. +BUILD_DIR = $(ROOT_DIR)/build +BUILD_DIR_DEBUG = $(abspath $(BUILD_DIR)/../build-debug) + +# Output directory variable for use in pattern rules. +# This is intended for internal use only, hence the explicit override +# definition. +override OUTPUT_DIR = $(BUILD_DIR) + +# Final install directory for widgets +DEST = $(OUTPUT_DIR) + +# Default build type for release builds +BUILD_TYPE = release + +.PHONY: all help update install distclean +.PHONY: clean clean-release clean-debug clean-all +.PHONY: configure configure-release configure-debug +.PHONY: build build-release build-debug build-all +.PHONY: package package-release package-debug package-all + +help: + @echo "List of targets available:" + @echo "" + @echo "- all" + @echo "- help" + @echo "- clean" + @echo "- distclean" + @echo "- configure" + @echo "- build: compilation, link and prepare files for package into a widget" + @echo "- package: output a widget file '*.wgt'" + @echo "- install: install in $(DEST) directory" + @echo "" + @echo "Usage: ./autobuild/agl/autobuild package DEST=${HOME}/opt" + @echo "Don't use your build dir as DEST as wgt file is generated at this location" + +all: package-all + +# Target specific variable over-rides so static pattern rules can be +# used for the various type-specific targets. + +configure-debug build-debug package-debug clean-debug: OUTPUT_DIR = $(BUILD_DIR_DEBUG) +configure-debug build-debug package-debug: BUILD_TYPE = debug + +clean-release clean-debug: + @if [ -d $(OUTPUT_DIR) ]; then \ + $(MAKE) -C $(OUTPUT_DIR) $(CLEAN_ARGS) clean; \ + else \ + echo Nothing to clean; \ + fi + +clean: clean-release + +clean-all: clean-release clean-debug + +distclean: clean-all + +configure-release configure-debug: + @mkdir -p $(OUTPUT_DIR) + @if [ ! -f $(OUTPUT_DIR)/Makefile ]; then \ + (cd $(OUTPUT_DIR) && qmake CONFIG+=$(BUILD_TYPE) $(CONFIGURE_ARGS) $(ROOT_DIR)); \ + fi + +configure: configure-release + +build-release build-debug: build-%: configure-% + @$(MAKE) -C $(OUTPUT_DIR) $(BUILD_ARGS) all + +build: build-release + +build-all: build-release build-debug + +package-release package-debug: package-%: build-% + @cp $(OUTPUT_DIR)/package/*.wgt $(OUTPUT_DIR)/ + @if [ "$(abspath $(DEST))" != "$(abspath $(OUTPUT_DIR))" ]; then \ + mkdir -p $(DEST) && cp $(OUTPUT_DIR)/*.wgt $(DEST); \ + fi + + +package: package-release + +package-all: package-release package-debug + +install: build + @$(MAKE) -C $(BUILD_DIR) $(INSTALL_ARGS) install diff --git a/app_client_examples/telemetry-cloud-app/autobuild/linux/autobuild b/app_client_examples/telemetry-cloud-app/autobuild/linux/autobuild new file mode 100755 index 0000000..bbbc13e --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/autobuild/linux/autobuild @@ -0,0 +1,110 @@ +#!/usr/bin/make -f +# Copyright (C) 2015 - 2018 "IoT.bzh" +# Copyright (C) 2020 Konsulko Group +# Author "Romain Forlot" +# +# 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. + +THISFILE := $(lastword $(MAKEFILE_LIST)) +ROOT_DIR := $(abspath $(dir $(THISFILE))/../..) + +# Build directories +# Note that the debug directory is defined in relation to the release +# directory (BUILD_DIR), this needs to be kept in mind if over-riding +# it and building that widget types, the specific widget type variable +# (e.g. BUILD_DIR_DEBUG) may also need to be specified to yield the +# desired output hierarchy. +BUILD_DIR = $(ROOT_DIR)/build +BUILD_DIR_DEBUG = $(abspath $(BUILD_DIR)/../build-debug) + +# Output directory variable for use in pattern rules. +# This is intended for internal use only, hence the explicit override +# definition. +override OUTPUT_DIR = $(BUILD_DIR) + +# Final install directory for widgets +DEST = $(OUTPUT_DIR) + +# Default build type for release builds +BUILD_TYPE = release + +.PHONY: all help update install distclean +.PHONY: clean clean-release clean-debug clean-all +.PHONY: configure configure-release configure-debug +.PHONY: build build-release build-debug build-all +.PHONY: package package-release package-debug package-all + +help: + @echo "List of targets available:" + @echo "" + @echo "- all" + @echo "- help" + @echo "- clean" + @echo "- distclean" + @echo "- configure" + @echo "- build: compilation, link and prepare files for package into a widget" + @echo "- package: output a widget file '*.wgt'" + @echo "- install: install in $(DEST) directory" + @echo "" + @echo "Usage: ./autobuild/agl/autobuild package DEST=${HOME}/opt" + @echo "Don't use your build dir as DEST as wgt file is generated at this location" + +all: package-all + +# Target specific variable over-rides so static pattern rules can be +# used for the various type-specific targets. + +configure-debug build-debug package-debug clean-debug: OUTPUT_DIR = $(BUILD_DIR_DEBUG) +configure-debug build-debug package-debug: BUILD_TYPE = debug + +clean-release clean-debug: + @if [ -d $(OUTPUT_DIR) ]; then \ + $(MAKE) -C $(OUTPUT_DIR) $(CLEAN_ARGS) clean; \ + else \ + echo Nothing to clean; \ + fi + +clean: clean-release + +clean-all: clean-release clean-debug + +distclean: clean-all + +configure-release configure-debug: + @mkdir -p $(OUTPUT_DIR) + @if [ ! -f $(OUTPUT_DIR)/Makefile ]; then \ + (cd $(OUTPUT_DIR) && qmake CONFIG+=$(BUILD_TYPE) $(CONFIGURE_ARGS) $(ROOT_DIR)); \ + fi + +configure: configure-release + +build-release build-debug: build-%: configure-% + @$(MAKE) -C $(OUTPUT_DIR) $(BUILD_ARGS) all + +build: build-release + +build-all: build-release build-debug + +package-release package-debug: package-%: build-% + @cp $(OUTPUT_DIR)/package/*.wgt $(OUTPUT_DIR)/ + @if [ "$(abspath $(DEST))" != "$(abspath $(OUTPUT_DIR))" ]; then \ + mkdir -p $(DEST) && cp $(OUTPUT_DIR)/*.wgt $(DEST); \ + fi + + +package: package-release + +package-all: package-release package-debug + +install: build + @$(MAKE) -C $(BUILD_DIR) $(INSTALL_ARGS) install diff --git a/app_client_examples/telemetry-cloud-app/package/config.xml b/app_client_examples/telemetry-cloud-app/package/config.xml new file mode 100755 index 0000000..a49b1c3 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/package/config.xml @@ -0,0 +1,18 @@ + + + CloudTelemetry + + + Test Cloud App + Mera + APL 2.0 + + + + + + + + + + diff --git a/app_client_examples/telemetry-cloud-app/package/icon.svg b/app_client_examples/telemetry-cloud-app/package/icon.svg new file mode 100755 index 0000000..b9b8e67 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/package/icon.svg @@ -0,0 +1,266 @@ + + + +image/svg+xmlVIDEOPLAYER + + \ No newline at end of file diff --git a/app_client_examples/telemetry-cloud-app/package/package.pro b/app_client_examples/telemetry-cloud-app/package/package.pro new file mode 100755 index 0000000..7a138a3 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/package/package.pro @@ -0,0 +1,28 @@ + +DISTFILES = icon.svg config.xml + +copy_icon.target = $$OUT_PWD/root/icon.svg +copy_icon.depends = $$_PRO_FILE_PWD_/icon.svg +copy_icon.commands = $(COPY_FILE) \"$$replace(copy_icon.depends, /, $$QMAKE_DIR_SEP)\" \"$$replace(copy_icon.target, /, $$QMAKE_DIR_SEP)\" +QMAKE_EXTRA_TARGETS += copy_icon +PRE_TARGETDEPS += $$copy_icon.target + +copy_config.target = $$OUT_PWD/root/config.xml +copy_config.depends = $$_PRO_FILE_PWD_/config.xml +copy_config.commands = $(COPY_FILE) \"$$replace(copy_config.depends, /, $$QMAKE_DIR_SEP)\" \"$$replace(copy_config.target, /, $$QMAKE_DIR_SEP)\" +QMAKE_EXTRA_TARGETS += copy_config +PRE_TARGETDEPS += $$copy_config.target + +WGT_TYPE = +CONFIG(debug, debug|release) { + WGT_TYPE = -debug +} + +wgt.target = package +wgt.commands = wgtpkg-pack -f -o telemetrycloudapp$${WGT_TYPE}.wgt root + +QMAKE_EXTRA_TARGETS += wgt + +deploy.files = telemetrycloudapp.wgt +deploy.path = /usr/AGL/apps/autoinstall +INSTALLS += deploy diff --git a/app_client_examples/telemetry-cloud-app/telemetrycloudapp.pro b/app_client_examples/telemetry-cloud-app/telemetrycloudapp.pro new file mode 100755 index 0000000..579a952 --- /dev/null +++ b/app_client_examples/telemetry-cloud-app/telemetrycloudapp.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs +SUBDIRS = app package +package.depends += app diff --git a/app_client_examples/test-cloud-app/app/app.pri b/app_client_examples/test-cloud-app/app/app.pri new file mode 100755 index 0000000..6d710e4 --- /dev/null +++ b/app_client_examples/test-cloud-app/app/app.pri @@ -0,0 +1,3 @@ +TEMPLATE = app +QMAKE_LFLAGS += "-Wl,--hash-style=gnu -Wl,--as-needed" +DESTDIR = $${OUT_PWD}/../package/root/bin diff --git a/app_client_examples/test-cloud-app/app/app.pro b/app_client_examples/test-cloud-app/app/app.pro new file mode 100755 index 0000000..4e8ae9a --- /dev/null +++ b/app_client_examples/test-cloud-app/app/app.pro @@ -0,0 +1,34 @@ +TARGET = testcloudapp +QT += core gui gui-private + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +DEFINES += QT_DEPRECATED_WARNINGS +DEFINES += APP_INSTANCE_NUM='\\"1\\"' + +SOURCES = main.cpp \ + mainwindow.cpp \ + cloudproxy-client/cloudproxyclient.cpp + +HEADERS += mainwindow.h \ + ui_mainwindow.h \ + cloudproxy-client/cloudproxyclient.h \ + cloudproxy-client/hmi-debug.h \ + +CONFIG += link_pkgconfig +PKGCONFIG += libhomescreen qlibwindowmanager qtappfw-core + +CONFIG(release, debug|release) { + QMAKE_POST_LINK = $(STRIP) --strip-unneeded $(TARGET) +} + +LIBS += -ljson-c -lafbwsc -lsystemd + +RESOURCES += \ + images/images.qrc + +include(app.pri) + +DISTFILES += \ + images/AGL_HMI_Blue_Background_NoCar-01.png + diff --git a/app_client_examples/test-cloud-app/app/cloudproxy-client/cloudproxyclient.cpp b/app_client_examples/test-cloud-app/app/cloudproxy-client/cloudproxyclient.cpp new file mode 100755 index 0000000..cfbf943 --- /dev/null +++ b/app_client_examples/test-cloud-app/app/cloudproxy-client/cloudproxyclient.cpp @@ -0,0 +1,261 @@ +/* + * 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 +#include + +#include "cloudproxyclient.h" +#include "hmi-debug.h" + + +static const char API_name[] = "cloudproxy"; + +const std::vector CloudProxyClient::m_api_list { + std::string("ping"), + std::string("createConnection"), + std::string("sendMessage"), + std::string("destroyConnection") + }; + +const std::vector CloudProxyClient::m_event_list { + std::string("sendMessageConfirmation"), + std::string("receivedMessage") + }; + +static void event_loop_run(sd_event* loop) +{ + sd_event_loop(loop); + sd_event_unref(loop); +} + +static void on_hangup(void *closure, afb_wsj1 *wsj) +{ + HMI_DEBUG("cloudproxyclient", "called"); + (void)closure; + (void)wsj; +} + +static void on_call(void *closure, const char *api, const char *verb, afb_wsj1_msg *msg) +{ + HMI_ERROR("cloudproxyclient", "this method should not be called"); + (void)closure; + (void)api; + (void)verb; + (void)msg; +} + +static void on_event(void* closure, const char* event, afb_wsj1_msg *msg) +{ + HMI_DEBUG("cloudproxyclient", "event [%s]", (event ? event: "")); + (void)closure; + + static_cast(closure)->on_event(nullptr, event, msg); +} + +static void on_reply(void *closure, afb_wsj1_msg *msg) +{ + HMI_DEBUG("cloudproxyclient", "called"); + (void)closure; + (void)msg; +} + + +CloudProxyClient::CloudProxyClient() +{ +} + +CloudProxyClient::~CloudProxyClient() +{ + if(m_websock) + { + afb_wsj1_unref(m_websock); + } + if(m_loop) + { + sd_event_exit(m_loop, 0); + } +} + +int CloudProxyClient::init(const int port, const std::string& token) +{ + int ret = 0; + if(port <= 0 && token.size() == 0) + { + HMI_ERROR("cloudproxyclient","port and token should be > 0, Initial port and token uses."); + return -1; + } + + { + m_loop = nullptr; + int ret = sd_event_new(&m_loop); + if(ret < 0) + { + HMI_ERROR("cloudproxyclient","Failed to create event loop"); + return -1; + } + + { + // enforce context to avoid initialization/goto error + std::thread th(event_loop_run, m_loop); + th.detach(); + } + + /* Initialize interface from websocket */ + m_itf.on_hangup = ::on_hangup; + m_itf.on_call = ::on_call; + m_itf.on_event = ::on_event; + + m_uri += "ws://localhost:" + std::to_string(port) + "/api?token=" + token; + + m_websock = afb_ws_client_connect_wsj1(m_loop, m_uri.c_str(), &m_itf, this); + if(!m_websock) + { + HMI_ERROR("cloudproxyclient","Failed to create websocket connection"); + return -1; + } + } + + HMI_DEBUG("cloudproxyclient", "Initialized"); + + return ret; +} + + +int CloudProxyClient::call(const std::string& verb, json_object* arg) +{ + int ret; + if(!m_websock) + { + return -1; + } + + if (verb.empty() || m_api_list.end() == std::find(m_api_list.begin(), m_api_list.end(), verb)) + { + HMI_ERROR("cloudproxyclient","verb [%s] doesn't exit", verb.c_str()); + return -1; + } + + ret = afb_wsj1_call_j(m_websock, API_name, verb.c_str(), arg, ::on_reply, this); + if (ret < 0) + { + HMI_ERROR("cloudproxyclient", "Failed to call verb:%s", verb.c_str()); + } + + return ret; +} + + +int CloudProxyClient::sendMessage(const std::string& data) +{ + if(!m_websock) + return -1; + + json_object* j_obj = json_object_new_object(); + json_object_object_add(j_obj, "data", json_object_new_string(data.c_str())); + + return this->call("sendMessage", j_obj); +} + +void CloudProxyClient::set_event_handler(enum EventType et, handler_func f) +{ + if (et > Event_Min && et < Event_Max) + { + switch (et) + { + case Event_SendMessageConfirmation: + this->subscribe(CloudProxyClient::m_event_list[0]); + break; + case Event_ReceivedMessage: + this->subscribe(CloudProxyClient::m_event_list[1]); + break; + default: + break; + } + + this->handlers[et] = std::move(f); + } +} + +int CloudProxyClient::subscribe(const std::string& event_name) +{ + if(!m_websock) + return -1; + + json_object* j_obj = json_object_new_object(); + json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str())); + + int ret = afb_wsj1_call_j(m_websock, API_name, "subscribe", j_obj, ::on_reply, this); + if (ret < 0) + HMI_ERROR("cloudproxyclient", "subscribe filed for '%s'", event_name.c_str()); + + return ret; +} + +int CloudProxyClient::unsubscribe(const std::string& event_name) +{ + if(!m_websock) + return -1; + + json_object* j_obj = json_object_new_object(); + json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str())); + + int ret = afb_wsj1_call_j(m_websock, API_name, "unsubscribe", j_obj, ::on_reply, this); + if (ret < 0) + HMI_ERROR("cloudproxyclient", "unsubscribe filed for '%s'", event_name.c_str()); + + return ret; +} + +void CloudProxyClient::on_event(void *closure, const char *event, afb_wsj1_msg *msg) +{ + HMI_DEBUG("cloudproxyclient", "event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg)); + (void) closure; + + if (strstr(event, API_name) == nullptr) + return; + + + json_object* ev_contents = afb_wsj1_msg_object_j(msg); + json_object *json_data; + if(!json_object_object_get_ex(ev_contents, "data", &json_data)) + { + HMI_ERROR("cloudproxyclient", "got ev_contents error."); + return; + } + + const char* event_type = nullptr; + json_object *json_event_type; + if(!json_object_object_get_ex(json_data, "type", &json_event_type) || + (event_type = json_object_get_string(json_event_type)) == nullptr) + { + HMI_ERROR("cloudproxyclient", "event_type is null."); + return; + } + + const std::string et{event_type}; + if (CloudProxyClient::m_event_list[0] == et) + { + auto i = this->handlers.find(Event_SendMessageConfirmation); + if (i != this->handlers.end()) + i->second(json_data); + } + else if (CloudProxyClient::m_event_list[1] == et) + { + auto i = this->handlers.find(Event_ReceivedMessage); + if (i != this->handlers.end()) + i->second(json_data); + } +} diff --git a/app_client_examples/test-cloud-app/app/cloudproxy-client/cloudproxyclient.h b/app_client_examples/test-cloud-app/app/cloudproxy-client/cloudproxyclient.h new file mode 100755 index 0000000..b333a14 --- /dev/null +++ b/app_client_examples/test-cloud-app/app/cloudproxy-client/cloudproxyclient.h @@ -0,0 +1,74 @@ +/* + * 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 +#include +#include +#include +#include +#include + +extern "C" +{ +#include +#include +} + +class CloudProxyClient +{ +public: + using handler_func = std::function; + + enum EventType { + Event_Min, + Event_SendMessageConfirmation = 1, + Event_ReceivedMessage, + Event_Max + }; + + + CloudProxyClient(); + ~CloudProxyClient(); + + CloudProxyClient(const CloudProxyClient &) = delete; + CloudProxyClient &operator=(const CloudProxyClient &) = delete; + + int init(const int port, const std::string& token); + + int sendMessage(const std::string& data); + + void set_event_handler(enum EventType et, handler_func f); + + void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg); + + int subscribe(const std::string& event_name); + int unsubscribe(const std::string& event_name); + +private: + int call(const std::string& verb, struct json_object* arg); + +private: + static const std::vector m_api_list; + static const std::vector m_event_list; + + std::map handlers; + struct afb_wsj1_itf m_itf; + struct afb_wsj1* m_websock{nullptr}; + sd_event* m_loop{nullptr}; + std::string m_uri; +}; diff --git a/app_client_examples/test-cloud-app/app/cloudproxy-client/hmi-debug.h b/app_client_examples/test-cloud-app/app/cloudproxy-client/hmi-debug.h new file mode 100755 index 0000000..674f5df --- /dev/null +++ b/app_client_examples/test-cloud-app/app/cloudproxy-client/hmi-debug.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2018 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__ + +#include +#include +#include +#include +#include + +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, ...) +{ + const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG")); + 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] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message); + va_end(args); + free(message); +} + +#endif //__HMI_DEBUG_H__ diff --git a/app_client_examples/test-cloud-app/app/images/AGL_HMI_Blue_Background_NoCar-01.png b/app_client_examples/test-cloud-app/app/images/AGL_HMI_Blue_Background_NoCar-01.png new file mode 100755 index 0000000..a76b140 Binary files /dev/null and b/app_client_examples/test-cloud-app/app/images/AGL_HMI_Blue_Background_NoCar-01.png differ diff --git a/app_client_examples/test-cloud-app/app/images/images.qrc b/app_client_examples/test-cloud-app/app/images/images.qrc new file mode 100755 index 0000000..78d9b77 --- /dev/null +++ b/app_client_examples/test-cloud-app/app/images/images.qrc @@ -0,0 +1,5 @@ + + + AGL_HMI_Blue_Background_NoCar-01.png + + diff --git a/app_client_examples/test-cloud-app/app/main.cpp b/app_client_examples/test-cloud-app/app/main.cpp new file mode 100755 index 0000000..45a877b --- /dev/null +++ b/app_client_examples/test-cloud-app/app/main.cpp @@ -0,0 +1,150 @@ +/* + * 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 +#include +#include +#include +#include +#include + +#include "mainwindow.h" +#include +#include + +#include "cloudproxy-client/cloudproxyclient.h" + + +CloudProxyClient* g_cloudproxyclient{nullptr}; + + +int main(int argc, char *argv[]) +{ + QString graphic_role = QString("cloudapp") + QString(APP_INSTANCE_NUM); + + QApplication app(argc, argv); + QScopedPointer window; + + QCommandLineParser parser; + parser.addPositionalArgument("port", app.translate("main", "port for binding")); + parser.addPositionalArgument("secret", app.translate("main", "secret for binding")); + parser.addHelpOption(); + parser.addVersionOption(); + parser.process(app); + QStringList positionalArguments = parser.positionalArguments(); + + if (positionalArguments.length() == 2) + { + int port = positionalArguments.takeFirst().toInt(); + QString secret = positionalArguments.takeFirst(); + QUrl bindingAddress; + bindingAddress.setScheme(QStringLiteral("ws")); + bindingAddress.setHost(QStringLiteral("localhost")); + bindingAddress.setPort(port); + bindingAddress.setPath(QStringLiteral("/api")); + QUrlQuery query; + query.addQueryItem(QStringLiteral("token"), secret); + bindingAddress.setQuery(query); + + std::string token = secret.toStdString(); + LibHomeScreen* hs = new LibHomeScreen(); + QLibWindowmanager* qwm = new QLibWindowmanager(); + + // WindowManager + if(qwm->init(port,secret) != 0){ + exit(EXIT_FAILURE); + } + AGLScreenInfo screenInfo(qwm->get_scale_factor()); + window.reset(new MainWindow(screenInfo.scale_factor())); + + // Request a surface as described in layers.json windowmanager’s file + if (qwm->requestSurface(graphic_role) != 0) { + exit(EXIT_FAILURE); + } + // Create an event callback against an event type. Here a lambda is called when SyncDraw event occurs + qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm, &graphic_role](json_object *object) { + (void)object; + qDebug() << "Surface got syncDraw!"; + qwm->endDraw(graphic_role); + }); + + // HomeScreen + hs->init(port, token.c_str()); + // Set the event handler for Event_ShowWindow which will activate the surface for windowmanager + hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [qwm, &graphic_role](json_object *object){ + (void)object; + qDebug("Surface %s got showWindow\n", graphic_role.toStdString().c_str()); + qwm->activateWindow(graphic_role); + }); + + g_cloudproxyclient = new CloudProxyClient(); + g_cloudproxyclient->init(port, token.c_str()); + + static int i_confirm_good{0}, i_confirm_bad{0}, i_recv{0}; + g_cloudproxyclient->set_event_handler(CloudProxyClient::Event_SendMessageConfirmation, [&window](json_object* object){ + qDebug("CloudProxyClient::Event_SendMessageConfirmation: object ptr %p", object); + + const char* str = object ? json_object_to_json_string_ext(object, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY) : ""; + if (!str) + str = ""; + qDebug("Event_SendMessageConfirmation: %s", str); + + json_object *j_result; + if(!json_object_object_get_ex(object, "result", &j_result)) + { + qDebug("Can't read confirmation result"); + return; + } + + int result = (int)json_object_get_boolean(j_result); + + if (result) + ++i_confirm_good; + else + ++i_confirm_bad; + + window->updateStat(i_confirm_good, i_confirm_bad, i_recv, QVariant()); + qDebug("Application received confirmation result [good: %d, bad: %d]: %d", i_confirm_good, i_confirm_bad, result); + }); + g_cloudproxyclient->set_event_handler(CloudProxyClient::Event_ReceivedMessage, [&window](json_object* object){ + qDebug("CloudProxyClient::Event_ReceivedMessage: object ptr %p", object); + + const char* str = object ? json_object_to_json_string_ext(object, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY) : ""; + if (!str) + str = ""; + qDebug("Event_SendMessageConfirmation: %s", str); + + json_object *event_data; + const char* data_str{nullptr}; + if(!json_object_object_get_ex(object, "data", &event_data) || + (data_str = json_object_get_string(event_data)) == nullptr) + { + qDebug("Can't read event data"); + return; + } + + ++i_recv; + window->updateStat(i_confirm_good, i_confirm_bad, i_recv, QString(data_str)); + qDebug("Application received data [count: %d, good: %d, bad %d]: %s", i_recv, i_confirm_good, i_confirm_bad, data_str); + }); + + + window->show(); + qwm->slotActivateWindow(); + } + + return app.exec(); +} diff --git a/app_client_examples/test-cloud-app/app/mainwindow.cpp b/app_client_examples/test-cloud-app/app/mainwindow.cpp new file mode 100755 index 0000000..fda05e7 --- /dev/null +++ b/app_client_examples/test-cloud-app/app/mainwindow.cpp @@ -0,0 +1,84 @@ +/* + * 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 "mainwindow.h" +#include "ui_mainwindow.h" +#include + +#include +#include + + +#include "cloudproxy-client/cloudproxyclient.h" +extern CloudProxyClient* g_cloudproxyclient; + + +MainWindow::MainWindow(double scale_factor, QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(scale_factor, this); + + //AGL style + setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint); + this->setStyleSheet( + "background-image:url(:/images/AGL_HMI_Blue_Background_NoCar-01.png) 0 0 0 0 stretch stretch; background: transparent;" ); +} + +void MainWindow::updateStat(int confirm_good, int confirm_bad, int received, QVariant recvMsg) +{ + confirmGood = confirm_good; + confirmBad = confirm_bad; + receivedCount = received; + + if (recvMsg.isValid()) + lastReceivedMsg = recvMsg.toString(); + + updateStat(); +} + +void MainWindow::updateStat() +{ + QString str = QString::asprintf("Sent: OK=%d, NOK=%d\nConfirmed: OK=%d, NOK=%d\nReceived=%d\n\n", + sendCount, sendErrorCount, confirmGood, confirmBad, receivedCount); + str.append("Last sent message:\n"); + str.append(lastSentMsg); + str.append("\n\nLast received message:\n"); + str.append(lastReceivedMsg); + ui->statLabel->setText(str); + //QApplication::processEvents(); +} + +void MainWindow::sendMsgButtonClick() +{ + qDebug() << "MainWindow::sendMegButtonClick()"; + + std::string msg = std::string{"{\"app_key\": \"app_value_"} + std::to_string(sendCount + sendErrorCount) + "\"}"; + int res = g_cloudproxyclient->sendMessage(msg); + if (res == 0) + ++sendCount; + else + ++sendErrorCount; + + lastSentMsg = msg.c_str(); + qDebug() << "cloud sendMessage result: " << res; +} + + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/app_client_examples/test-cloud-app/app/mainwindow.h b/app_client_examples/test-cloud-app/app/mainwindow.h new file mode 100755 index 0000000..ae66729 --- /dev/null +++ b/app_client_examples/test-cloud-app/app/mainwindow.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. + */ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include + + +namespace Ui { + class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(double scale_factor, QWidget *parent = 0); + ~MainWindow(); + +public slots: + void updateStat(int confirm_good, int confirm_bad, int received, QVariant recvMsg); + +private slots: + void sendMsgButtonClick(); + void updateStat(); + +private: + Ui::MainWindow *ui; + int sendCount{0}; + int sendErrorCount{0}; + int confirmGood{0}; + int confirmBad{0}; + int receivedCount{0}; + QString lastSentMsg; + QString lastReceivedMsg; +}; + +#endif // MAINWINDOW_H diff --git a/app_client_examples/test-cloud-app/app/ui_mainwindow.h b/app_client_examples/test-cloud-app/app/ui_mainwindow.h new file mode 100755 index 0000000..3dac77e --- /dev/null +++ b/app_client_examples/test-cloud-app/app/ui_mainwindow.h @@ -0,0 +1,94 @@ +/* + * 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. + */ + +#ifndef UI_MAINWINDOW_H +#define UI_MAINWINDOW_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_MainWindow +{ +public: + QWidget *centralWidget{nullptr}; + QPushButton *sendMsgButton{nullptr}; + QLabel *appNameLabel{nullptr}; + QLabel *statLabel{nullptr}; + + void setupUi(double scale_factor, QMainWindow *MainWindow) + { + if (MainWindow->objectName().isEmpty()) + MainWindow->setObjectName(QString::fromUtf8("MainWindow")); + MainWindow->resize(1080*scale_factor, 1400*scale_factor); + centralWidget = new QWidget(MainWindow); + centralWidget->setObjectName(QString::fromUtf8("centralWidget")); + + //QString style("color: white; font-size: 24px"); + QString style("color: white"); + + sendMsgButton = new QPushButton(centralWidget); + sendMsgButton->setObjectName(QString::fromUtf8("sendMsgButton")); + sendMsgButton->setGeometry(QRect((540-242/2)*scale_factor, 100*scale_factor, 242*scale_factor, 64*scale_factor)); + sendMsgButton->setStyleSheet(style); + + appNameLabel = new QLabel(centralWidget); + appNameLabel->setObjectName(QString::fromUtf8("label")); + appNameLabel->setGeometry(QRect(40*scale_factor, 20*scale_factor, 1000*scale_factor, 32*scale_factor)); + appNameLabel->setStyleSheet(style); + + statLabel = new QLabel(centralWidget); + statLabel->setObjectName(QString::fromUtf8("label")); + statLabel->setGeometry(QRect(40*scale_factor, 180*scale_factor, 1000*scale_factor, 420*scale_factor)); + statLabel->setWordWrap(true); + statLabel->setAlignment(Qt::AlignTop); + statLabel->setStyleSheet(style); + + MainWindow->setCentralWidget(centralWidget); + + QObject::connect(sendMsgButton, SIGNAL(clicked()), MainWindow, SLOT(sendMsgButtonClick())); + + retranslateUi(MainWindow); + + QMetaObject::connectSlotsByName(MainWindow); + } // setupUi + + void retranslateUi(QMainWindow *MainWindow) + { + MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", nullptr)); + sendMsgButton->setText(QApplication::translate("MainWindow", "Send message", nullptr)); + QString name = QString("Test application ") + APP_INSTANCE_NUM; + appNameLabel->setText(QApplication::translate("MainWindow", name.toUtf8().constData(), nullptr)); + } // retranslateUi +}; + +namespace Ui { + class MainWindow: public Ui_MainWindow {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UI_MAINWINDOW_H diff --git a/app_client_examples/test-cloud-app/autobuild/agl/autobuild b/app_client_examples/test-cloud-app/autobuild/agl/autobuild new file mode 100755 index 0000000..bbbc13e --- /dev/null +++ b/app_client_examples/test-cloud-app/autobuild/agl/autobuild @@ -0,0 +1,110 @@ +#!/usr/bin/make -f +# Copyright (C) 2015 - 2018 "IoT.bzh" +# Copyright (C) 2020 Konsulko Group +# Author "Romain Forlot" +# +# 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. + +THISFILE := $(lastword $(MAKEFILE_LIST)) +ROOT_DIR := $(abspath $(dir $(THISFILE))/../..) + +# Build directories +# Note that the debug directory is defined in relation to the release +# directory (BUILD_DIR), this needs to be kept in mind if over-riding +# it and building that widget types, the specific widget type variable +# (e.g. BUILD_DIR_DEBUG) may also need to be specified to yield the +# desired output hierarchy. +BUILD_DIR = $(ROOT_DIR)/build +BUILD_DIR_DEBUG = $(abspath $(BUILD_DIR)/../build-debug) + +# Output directory variable for use in pattern rules. +# This is intended for internal use only, hence the explicit override +# definition. +override OUTPUT_DIR = $(BUILD_DIR) + +# Final install directory for widgets +DEST = $(OUTPUT_DIR) + +# Default build type for release builds +BUILD_TYPE = release + +.PHONY: all help update install distclean +.PHONY: clean clean-release clean-debug clean-all +.PHONY: configure configure-release configure-debug +.PHONY: build build-release build-debug build-all +.PHONY: package package-release package-debug package-all + +help: + @echo "List of targets available:" + @echo "" + @echo "- all" + @echo "- help" + @echo "- clean" + @echo "- distclean" + @echo "- configure" + @echo "- build: compilation, link and prepare files for package into a widget" + @echo "- package: output a widget file '*.wgt'" + @echo "- install: install in $(DEST) directory" + @echo "" + @echo "Usage: ./autobuild/agl/autobuild package DEST=${HOME}/opt" + @echo "Don't use your build dir as DEST as wgt file is generated at this location" + +all: package-all + +# Target specific variable over-rides so static pattern rules can be +# used for the various type-specific targets. + +configure-debug build-debug package-debug clean-debug: OUTPUT_DIR = $(BUILD_DIR_DEBUG) +configure-debug build-debug package-debug: BUILD_TYPE = debug + +clean-release clean-debug: + @if [ -d $(OUTPUT_DIR) ]; then \ + $(MAKE) -C $(OUTPUT_DIR) $(CLEAN_ARGS) clean; \ + else \ + echo Nothing to clean; \ + fi + +clean: clean-release + +clean-all: clean-release clean-debug + +distclean: clean-all + +configure-release configure-debug: + @mkdir -p $(OUTPUT_DIR) + @if [ ! -f $(OUTPUT_DIR)/Makefile ]; then \ + (cd $(OUTPUT_DIR) && qmake CONFIG+=$(BUILD_TYPE) $(CONFIGURE_ARGS) $(ROOT_DIR)); \ + fi + +configure: configure-release + +build-release build-debug: build-%: configure-% + @$(MAKE) -C $(OUTPUT_DIR) $(BUILD_ARGS) all + +build: build-release + +build-all: build-release build-debug + +package-release package-debug: package-%: build-% + @cp $(OUTPUT_DIR)/package/*.wgt $(OUTPUT_DIR)/ + @if [ "$(abspath $(DEST))" != "$(abspath $(OUTPUT_DIR))" ]; then \ + mkdir -p $(DEST) && cp $(OUTPUT_DIR)/*.wgt $(DEST); \ + fi + + +package: package-release + +package-all: package-release package-debug + +install: build + @$(MAKE) -C $(BUILD_DIR) $(INSTALL_ARGS) install diff --git a/app_client_examples/test-cloud-app/autobuild/linux/autobuild b/app_client_examples/test-cloud-app/autobuild/linux/autobuild new file mode 100755 index 0000000..bbbc13e --- /dev/null +++ b/app_client_examples/test-cloud-app/autobuild/linux/autobuild @@ -0,0 +1,110 @@ +#!/usr/bin/make -f +# Copyright (C) 2015 - 2018 "IoT.bzh" +# Copyright (C) 2020 Konsulko Group +# Author "Romain Forlot" +# +# 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. + +THISFILE := $(lastword $(MAKEFILE_LIST)) +ROOT_DIR := $(abspath $(dir $(THISFILE))/../..) + +# Build directories +# Note that the debug directory is defined in relation to the release +# directory (BUILD_DIR), this needs to be kept in mind if over-riding +# it and building that widget types, the specific widget type variable +# (e.g. BUILD_DIR_DEBUG) may also need to be specified to yield the +# desired output hierarchy. +BUILD_DIR = $(ROOT_DIR)/build +BUILD_DIR_DEBUG = $(abspath $(BUILD_DIR)/../build-debug) + +# Output directory variable for use in pattern rules. +# This is intended for internal use only, hence the explicit override +# definition. +override OUTPUT_DIR = $(BUILD_DIR) + +# Final install directory for widgets +DEST = $(OUTPUT_DIR) + +# Default build type for release builds +BUILD_TYPE = release + +.PHONY: all help update install distclean +.PHONY: clean clean-release clean-debug clean-all +.PHONY: configure configure-release configure-debug +.PHONY: build build-release build-debug build-all +.PHONY: package package-release package-debug package-all + +help: + @echo "List of targets available:" + @echo "" + @echo "- all" + @echo "- help" + @echo "- clean" + @echo "- distclean" + @echo "- configure" + @echo "- build: compilation, link and prepare files for package into a widget" + @echo "- package: output a widget file '*.wgt'" + @echo "- install: install in $(DEST) directory" + @echo "" + @echo "Usage: ./autobuild/agl/autobuild package DEST=${HOME}/opt" + @echo "Don't use your build dir as DEST as wgt file is generated at this location" + +all: package-all + +# Target specific variable over-rides so static pattern rules can be +# used for the various type-specific targets. + +configure-debug build-debug package-debug clean-debug: OUTPUT_DIR = $(BUILD_DIR_DEBUG) +configure-debug build-debug package-debug: BUILD_TYPE = debug + +clean-release clean-debug: + @if [ -d $(OUTPUT_DIR) ]; then \ + $(MAKE) -C $(OUTPUT_DIR) $(CLEAN_ARGS) clean; \ + else \ + echo Nothing to clean; \ + fi + +clean: clean-release + +clean-all: clean-release clean-debug + +distclean: clean-all + +configure-release configure-debug: + @mkdir -p $(OUTPUT_DIR) + @if [ ! -f $(OUTPUT_DIR)/Makefile ]; then \ + (cd $(OUTPUT_DIR) && qmake CONFIG+=$(BUILD_TYPE) $(CONFIGURE_ARGS) $(ROOT_DIR)); \ + fi + +configure: configure-release + +build-release build-debug: build-%: configure-% + @$(MAKE) -C $(OUTPUT_DIR) $(BUILD_ARGS) all + +build: build-release + +build-all: build-release build-debug + +package-release package-debug: package-%: build-% + @cp $(OUTPUT_DIR)/package/*.wgt $(OUTPUT_DIR)/ + @if [ "$(abspath $(DEST))" != "$(abspath $(OUTPUT_DIR))" ]; then \ + mkdir -p $(DEST) && cp $(OUTPUT_DIR)/*.wgt $(DEST); \ + fi + + +package: package-release + +package-all: package-release package-debug + +install: build + @$(MAKE) -C $(BUILD_DIR) $(INSTALL_ARGS) install diff --git a/app_client_examples/test-cloud-app/package/config.xml b/app_client_examples/test-cloud-app/package/config.xml new file mode 100755 index 0000000..43eceaf --- /dev/null +++ b/app_client_examples/test-cloud-app/package/config.xml @@ -0,0 +1,18 @@ + + + CloudApp1 + + + Test Cloud App + Mera + APL 2.0 + + + + + + + + + + diff --git a/app_client_examples/test-cloud-app/package/icon.svg b/app_client_examples/test-cloud-app/package/icon.svg new file mode 100755 index 0000000..b9b8e67 --- /dev/null +++ b/app_client_examples/test-cloud-app/package/icon.svg @@ -0,0 +1,266 @@ + + + +image/svg+xmlVIDEOPLAYER + + \ No newline at end of file diff --git a/app_client_examples/test-cloud-app/package/package.pro b/app_client_examples/test-cloud-app/package/package.pro new file mode 100755 index 0000000..c8af452 --- /dev/null +++ b/app_client_examples/test-cloud-app/package/package.pro @@ -0,0 +1,28 @@ + +DISTFILES = icon.svg config.xml + +copy_icon.target = $$OUT_PWD/root/icon.svg +copy_icon.depends = $$_PRO_FILE_PWD_/icon.svg +copy_icon.commands = $(COPY_FILE) \"$$replace(copy_icon.depends, /, $$QMAKE_DIR_SEP)\" \"$$replace(copy_icon.target, /, $$QMAKE_DIR_SEP)\" +QMAKE_EXTRA_TARGETS += copy_icon +PRE_TARGETDEPS += $$copy_icon.target + +copy_config.target = $$OUT_PWD/root/config.xml +copy_config.depends = $$_PRO_FILE_PWD_/config.xml +copy_config.commands = $(COPY_FILE) \"$$replace(copy_config.depends, /, $$QMAKE_DIR_SEP)\" \"$$replace(copy_config.target, /, $$QMAKE_DIR_SEP)\" +QMAKE_EXTRA_TARGETS += copy_config +PRE_TARGETDEPS += $$copy_config.target + +WGT_TYPE = +CONFIG(debug, debug|release) { + WGT_TYPE = -debug +} + +wgt.target = package +wgt.commands = wgtpkg-pack -f -o testcloudapp$${WGT_TYPE}.wgt root + +QMAKE_EXTRA_TARGETS += wgt + +deploy.files = testcloudapp.wgt +deploy.path = /usr/AGL/apps/autoinstall +INSTALLS += deploy diff --git a/app_client_examples/test-cloud-app/testcloudapp.pro b/app_client_examples/test-cloud-app/testcloudapp.pro new file mode 100755 index 0000000..579a952 --- /dev/null +++ b/app_client_examples/test-cloud-app/testcloudapp.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs +SUBDIRS = app package +package.depends += app diff --git a/autobuild/agl/autobuild b/autobuild/agl/autobuild new file mode 100755 index 0000000..16181b8 --- /dev/null +++ b/autobuild/agl/autobuild @@ -0,0 +1,128 @@ +#!/usr/bin/make -f +# Copyright (C) 2015 - 2018 "IoT.bzh" +# Copyright (C) 2020 Konsulko Group +# Author "Romain Forlot" +# +# 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. + +THISFILE := $(lastword $(MAKEFILE_LIST)) +ROOT_DIR := $(abspath $(dir $(THISFILE))/../..) + +# Build directories +# Note that the debug/test/coverage directories are defined in relation +# to the release directory (BUILD_DIR), this needs to be kept in mind +# if over-riding it and building those widget types, the specific widget +# type variable (e.g. BUILD_DIR_DEBUG) may also need to be specified +# to yield the desired output hierarchy. +BUILD_DIR = $(ROOT_DIR)/build +BUILD_DIR_DEBUG = $(abspath $(BUILD_DIR)/../build-debug) +BUILD_DIR_TEST = $(abspath $(BUILD_DIR)/../build-test) +BUILD_DIR_COVERAGE = $(abspath $(BUILD_DIR)/../build-coverage) + +# Output directory variable for use in pattern rules. +# This is intended for internal use only, hence the explicit override +# definition. +override OUTPUT_DIR = $(BUILD_DIR) + +# Final install directory for widgets +DEST = $(OUTPUT_DIR) + +# Default build type for release/test builds +BUILD_TYPE = RELEASE + +.PHONY: all help update install distclean +.PHONY: clean clean-release clean-debug clean-test clean-coverage clean-all +.PHONY: configure configure-release configure-debug configure-test configure-coverage +.PHONY: build build-release build-debug build-test build-coverage build-all +.PHONY: package package-release package-debug package-test package-coverage package-all + +help: + @echo "List of targets available:" + @echo "" + @echo "- all" + @echo "- help" + @echo "- clean" + @echo "- distclean" + @echo "- configure" + @echo "- build: compilation, link and prepare files for package into a widget" + @echo "- package: output a widget file '*.wgt'" + @echo "- install: install in your $(CMAKE_INSTALL_DIR) directory" + @echo "" + @echo "Usage: ./autobuild/agl/autobuild package DEST=${HOME}/opt" + @echo "Don't use your build dir as DEST as wgt file is generated at this location" + +all: package-all + +# Target specific variable over-rides so static pattern rules can be +# used for the various type-specific targets. + +configure-test build-test package-test clean-test: OUTPUT_DIR = $(BUILD_DIR_TEST) + +configure-coverage build-coverage package-coverage clean-coverage: OUTPUT_DIR = $(BUILD_DIR_COVERAGE) +configure-coverage build-coverage package-coverage: BUILD_TYPE = COVERAGE + +configure-debug build-debug package-debug clean-debug: OUTPUT_DIR = $(BUILD_DIR_DEBUG) +configure-debug build-debug package-debug: BUILD_TYPE = DEBUG + +clean-release clean-test clean-debug clean-coverage: + @if [ -d $(OUTPUT_DIR) ]; then \ + $(MAKE) -C $(OUTPUT_DIR) $(CLEAN_ARGS) clean; \ + else \ + echo Nothing to clean; \ + fi + +clean: clean-release + +clean-all: clean-release clean-test clean-debug clean-coverage + +distclean: clean-all + +configure-release configure-test configure-debug configure-coverage: + @mkdir -p $(OUTPUT_DIR) + @if [ ! -f $(OUTPUT_DIR)/Makefile ]; then \ + (cd $(OUTPUT_DIR) && cmake -S $(ROOT_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) $(CONFIGURE_ARGS)); \ + fi + +configure: configure-release + +build-release build-debug build-coverage: build-%: configure-% + @cmake --build $(OUTPUT_DIR) $(BUILD_ARGS) --target all + +# Kept for consistency, empty to avoid building everything for test widget +build-test: configure-test + +build: build-release + +build-all: build-release build-debug build-test build-coverage + +package-release package-debug package-coverage: package-%: build-% + @cmake --build $(OUTPUT_DIR) $(PACKAGE_ARGS) --target widget + @if [ "$(abspath $(DEST))" != "$(abspath $(OUTPUT_DIR))" ]; then \ + mkdir -p $(DEST) && cp $(OUTPUT_DIR)/*.wgt $(DEST); \ + fi + +package-test: build-test + @cmake --build $(OUTPUT_DIR) $(PACKAGE_ARGS) --target test_widget + @if [ "$(abspath $(DEST))" != "$(abspath $(OUTPUT_DIR))" ]; then \ + mkdir -p $(DEST) && cp $(OUTPUT_DIR)/*.wgt $(DEST); \ + fi + +package: package-release + +package-all: package-release package-test package-coverage package-debug + +update: configure + @cmake --build $(BUILD_DIR) --target autobuild + +install: build + @cmake --build $(BUILD_DIR) $(INSTALL_ARGS) --target install diff --git a/autobuild/linux/autobuild b/autobuild/linux/autobuild new file mode 100755 index 0000000..16181b8 --- /dev/null +++ b/autobuild/linux/autobuild @@ -0,0 +1,128 @@ +#!/usr/bin/make -f +# Copyright (C) 2015 - 2018 "IoT.bzh" +# Copyright (C) 2020 Konsulko Group +# Author "Romain Forlot" +# +# 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. + +THISFILE := $(lastword $(MAKEFILE_LIST)) +ROOT_DIR := $(abspath $(dir $(THISFILE))/../..) + +# Build directories +# Note that the debug/test/coverage directories are defined in relation +# to the release directory (BUILD_DIR), this needs to be kept in mind +# if over-riding it and building those widget types, the specific widget +# type variable (e.g. BUILD_DIR_DEBUG) may also need to be specified +# to yield the desired output hierarchy. +BUILD_DIR = $(ROOT_DIR)/build +BUILD_DIR_DEBUG = $(abspath $(BUILD_DIR)/../build-debug) +BUILD_DIR_TEST = $(abspath $(BUILD_DIR)/../build-test) +BUILD_DIR_COVERAGE = $(abspath $(BUILD_DIR)/../build-coverage) + +# Output directory variable for use in pattern rules. +# This is intended for internal use only, hence the explicit override +# definition. +override OUTPUT_DIR = $(BUILD_DIR) + +# Final install directory for widgets +DEST = $(OUTPUT_DIR) + +# Default build type for release/test builds +BUILD_TYPE = RELEASE + +.PHONY: all help update install distclean +.PHONY: clean clean-release clean-debug clean-test clean-coverage clean-all +.PHONY: configure configure-release configure-debug configure-test configure-coverage +.PHONY: build build-release build-debug build-test build-coverage build-all +.PHONY: package package-release package-debug package-test package-coverage package-all + +help: + @echo "List of targets available:" + @echo "" + @echo "- all" + @echo "- help" + @echo "- clean" + @echo "- distclean" + @echo "- configure" + @echo "- build: compilation, link and prepare files for package into a widget" + @echo "- package: output a widget file '*.wgt'" + @echo "- install: install in your $(CMAKE_INSTALL_DIR) directory" + @echo "" + @echo "Usage: ./autobuild/agl/autobuild package DEST=${HOME}/opt" + @echo "Don't use your build dir as DEST as wgt file is generated at this location" + +all: package-all + +# Target specific variable over-rides so static pattern rules can be +# used for the various type-specific targets. + +configure-test build-test package-test clean-test: OUTPUT_DIR = $(BUILD_DIR_TEST) + +configure-coverage build-coverage package-coverage clean-coverage: OUTPUT_DIR = $(BUILD_DIR_COVERAGE) +configure-coverage build-coverage package-coverage: BUILD_TYPE = COVERAGE + +configure-debug build-debug package-debug clean-debug: OUTPUT_DIR = $(BUILD_DIR_DEBUG) +configure-debug build-debug package-debug: BUILD_TYPE = DEBUG + +clean-release clean-test clean-debug clean-coverage: + @if [ -d $(OUTPUT_DIR) ]; then \ + $(MAKE) -C $(OUTPUT_DIR) $(CLEAN_ARGS) clean; \ + else \ + echo Nothing to clean; \ + fi + +clean: clean-release + +clean-all: clean-release clean-test clean-debug clean-coverage + +distclean: clean-all + +configure-release configure-test configure-debug configure-coverage: + @mkdir -p $(OUTPUT_DIR) + @if [ ! -f $(OUTPUT_DIR)/Makefile ]; then \ + (cd $(OUTPUT_DIR) && cmake -S $(ROOT_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) $(CONFIGURE_ARGS)); \ + fi + +configure: configure-release + +build-release build-debug build-coverage: build-%: configure-% + @cmake --build $(OUTPUT_DIR) $(BUILD_ARGS) --target all + +# Kept for consistency, empty to avoid building everything for test widget +build-test: configure-test + +build: build-release + +build-all: build-release build-debug build-test build-coverage + +package-release package-debug package-coverage: package-%: build-% + @cmake --build $(OUTPUT_DIR) $(PACKAGE_ARGS) --target widget + @if [ "$(abspath $(DEST))" != "$(abspath $(OUTPUT_DIR))" ]; then \ + mkdir -p $(DEST) && cp $(OUTPUT_DIR)/*.wgt $(DEST); \ + fi + +package-test: build-test + @cmake --build $(OUTPUT_DIR) $(PACKAGE_ARGS) --target test_widget + @if [ "$(abspath $(DEST))" != "$(abspath $(OUTPUT_DIR))" ]; then \ + mkdir -p $(DEST) && cp $(OUTPUT_DIR)/*.wgt $(DEST); \ + fi + +package: package-release + +package-all: package-release package-test package-coverage package-debug + +update: configure + @cmake --build $(BUILD_DIR) --target autobuild + +install: build + @cmake --build $(BUILD_DIR) $(INSTALL_ARGS) --target install diff --git a/package/root/config.xml b/package/root/config.xml new file mode 100755 index 0000000..acfdc1e --- /dev/null +++ b/package/root/config.xml @@ -0,0 +1,23 @@ + + + cloudproxy-service + + + CloudProxy service + MERA + APL 2.0 + + + + + + + + + + + + + + + diff --git a/package/root/icon.svg b/package/root/icon.svg new file mode 100755 index 0000000..6628784 --- /dev/null +++ b/package/root/icon.svg @@ -0,0 +1,283 @@ + + + +image/svg+xmlSETTINGS + \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100755 index 0000000..fc397f1 --- /dev/null +++ b/src/CMakeLists.txt @@ -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. +########################################################################### + +cmake_minimum_required(VERSION 2.8) + +set(TARGETS_ARBINDER cloudproxy-service) + +INCLUDE(FindThreads) +FIND_PACKAGE(Threads) + +pkg_check_modules(cp_binding_depends afb-daemon glib-2.0 gio-2.0 gio-unix-2.0 json-c) +set(binding_cp_sources + cloudproxy-bindings.cpp + utils.h + hmi-debug.h + ClientManager.h + ClientManager.cpp +) + +link_libraries(-Wl,--as-needed -Wl,--gc-sections -Wl,--no-undefined) + +set(AZURE_DEP_LIBS iothub_client) + +add_library(${TARGETS_ARBINDER} MODULE ${binding_cp_sources}) + +include_directories(${CMAKE_SYSROOT}/usr/include/azureiot) + +target_compile_options(${TARGETS_ARBINDER} PRIVATE ${cp_binding_depends_CFLAGS}) +if(DEFINED DEBUGMODE) + target_compile_options(${TARGETS_ARBINDER} PRIVATE -g -O0) +else(DEFINED DEBUGMODE) + target_compile_options(${TARGETS_ARBINDER} PRIVATE -g -O2) +endif(DEFINED DEBUGMODE) + +target_link_libraries(${TARGETS_ARBINDER} ${CMAKE_THREAD_LIBS_INIT} ${link_libraries} ${cp_binding_depends_LIBRARIES} ${AZURE_DEP_LIBS}) + + +set_target_properties(${TARGETS_ARBINDER} PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" + ) + +target_compile_definitions(${TARGETS_ARBINDER} + PRIVATE + AFB_BINDING_VERSION=3 + _GNU_SOURCE) + + +if(NOT EXISTS ${PROJECT_BINARY_DIR}/package) + add_custom_command(TARGET ${TARGETS_ARBINDER} POST_BUILD + COMMAND cp -rf ${PROJECT_SOURCE_DIR}/package ${PROJECT_BINARY_DIR} + ) +endif() + +add_custom_command(TARGET ${TARGETS_ARBINDER} POST_BUILD + COMMAND mkdir -p ${PROJECT_BINARY_DIR}/package/root/lib + COMMAND cp -rf ${PROJECT_BINARY_DIR}/src/${TARGETS_ARBINDER}.so ${PROJECT_BINARY_DIR}/package/root/lib + COMMAND mkdir -p ${PROJECT_BINARY_DIR}/package/root/etc + COMMAND cp -f ${PROJECT_SOURCE_DIR}/src/config_sample.ini ${PROJECT_BINARY_DIR}/package/root/etc/config.ini +) + +add_custom_target(widget DEPENDS ${PROJECT_BINARY_DIR}/package/root + COMMAND wgtpkg-pack -f -o ${PROJECT_BINARY_DIR}/${TARGETS_ARBINDER}.wgt ${PROJECT_BINARY_DIR}/package/root +) diff --git a/src/ClientManager.cpp b/src/ClientManager.cpp new file mode 100755 index 0000000..74d672d --- /dev/null +++ b/src/ClientManager.cpp @@ -0,0 +1,215 @@ +/* + * 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 +#include + +// 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 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 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 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 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 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 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)); +} diff --git a/src/ClientManager.h b/src/ClientManager.h new file mode 100755 index 0000000..a1038e1 --- /dev/null +++ b/src/ClientManager.h @@ -0,0 +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 + +#include +#include +#include +#include + +class ClientManager +{ +public: + struct ClientCtx + { + std::string appid; + afb_event_t event; + std::unordered_set 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 m_clients; +}; diff --git a/src/cloudproxy-bindings.cpp b/src/cloudproxy-bindings.cpp new file mode 100755 index 0000000..1a40730 --- /dev/null +++ b/src/cloudproxy-bindings.cpp @@ -0,0 +1,441 @@ +/* + * 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 +#include + +#include +#include + +#include +#include +#include +#include +#include +#include // ThreadAPI_Sleep() +#include // tickcounter_ms_t + +#include "hmi-debug.h" +#include "utils.h" +#include "ClientManager.h" + +#include + +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}; + + + +static utils::scope_exit g_destroy([]{ + if (g_iot_inited) + { + if (g_device_handle) + IoTHubDeviceClient_Destroy(g_device_handle); + + IoTHub_Deinit(); + } +}); + + +static bool loadConf() +{ + const char* CONF_FILE_PATH{"/etc/config.ini"}; + const char* DEFAULT_ETC_PATH{"/etc/cloudproxy-service"}; + 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); + p = DEFAULT_ETC_PATH; + } + + std::string conf_path = p; + conf_path += CONF_FILE_PATH; + + g_autoptr(GKeyFile) conf_file = g_key_file_new(); + g_autoptr(GError) error = nullptr; + + 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"); + 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) + { + const unsigned char* buff_msg; + size_t buff_len; + + 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 : "")); + + 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 : "")); + + if (app_id && app_id[0]) + ClientManager::instance().emitReceivedMessage(app_id, string_msg); + 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); + } + + 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]) + { + HMI_ERROR("cloudproxy-service", "Confirmation callback: appid is not set"); + + if (userContextCallback) + free(userContextCallback); + + 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; + } + + g_device_handle = IoTHubDeviceClient_CreateFromConnectionString(g_connectionString.c_str(), MQTT_Protocol); + if (!g_device_handle) + { + HMI_ERROR("cloudproxy-service", "Failure creating IoTHubDeviceClient device"); + 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); + 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__); + + json_object* object = afb_req_json(request); + if (!object) + { + HMI_ERROR("cloudproxy-service", "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)) + { + HMI_ERROR("cloudproxy-service", "can't obtain application_id or data from request"); + return; + } + data = json_object_get_string(obj_data); + + if (!g_device_handle && !createConnection()) + { + 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()); + + 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) + { + HMI_ERROR("cloudproxy-service", "Can't create IoTHubMessage message"); + afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); + return; + } + + IoTHubMessage_SetProperty(message_handle, "application_id", appid.c_str()); + + if (IoTHubDeviceClient_SendEventAsync(g_device_handle, message_handle, send_confirm_callback, strdup(appid.c_str()))) + { + HMI_ERROR("cloudproxy-service", "Can't send IoTHubMessage message"); + afb_req_fail_f(request, "failed", "called %s", __FUNCTION__); + return; + } + + afb_req_success(request, json_object_new_object(), __FUNCTION__); +} + + +static void subscribe(afb_req_t request) +{ + HMI_NOTICE("cloudproxy-service", "%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_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_req_fail_f(request, "%s failed", __FUNCTION__); + } + else + { + afb_req_success(request, json_object_new_object(), __FUNCTION__); + } +} + +static void unsubscribe(afb_req_t request) +{ + HMI_NOTICE("cloudproxy-service", "%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_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_req_fail_f(request, "%s failed", __FUNCTION__); + } + else + { + afb_req_success(request, json_object_new_object(), __FUNCTION__); + } +} + +/* + * array of the verbs exported to afb-daemon + */ +static const afb_verb_t verbs[]= { + /* VERB'S NAME FUNCTION TO CALL */ + { .verb="ping", .callback=pingSample }, + { .verb="sendMessage", .callback=sendMessage }, + { .verb="subscribe", .callback=subscribe }, + { .verb="unsubscribe", .callback=unsubscribe }, + {nullptr } /* marker for end of the array */ +}; + + +static int preinit(afb_api_t api) +{ + HMI_NOTICE("cloudproxy-service", "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"); + return -1; + } + + return 0; +} + + +static int init(afb_api_t api) +{ + HMI_NOTICE("cloudproxy-service","binding init"); + return (g_iot_inited ? 0 : -1); +} + +const afb_binding_t afbBindingExport = { + .api = "cloudproxy", + .specification = nullptr, + .info = nullptr, + .verbs = verbs, + .preinit = preinit, + .init = init, + .onevent = nullptr +}; diff --git a/src/config_sample.ini b/src/config_sample.ini new file mode 100644 index 0000000..fa97cd7 --- /dev/null +++ b/src/config_sample.ini @@ -0,0 +1,3 @@ +[AzureCloudConnection] +DeviceConnectionString=My=Azure=Device=Connection=String + diff --git a/src/export.map b/src/export.map new file mode 100755 index 0000000..f3961c0 --- /dev/null +++ b/src/export.map @@ -0,0 +1 @@ +{ global: afbBindingV*; local: *; }; \ No newline at end of file diff --git a/src/hmi-debug.h b/src/hmi-debug.h new file mode 100755 index 0000000..c8e8638 --- /dev/null +++ b/src/hmi-debug.h @@ -0,0 +1,82 @@ +/* + * 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 +#include +#include +#include +#include + +#include // gettid() +#include +#include + + 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 new file mode 100755 index 0000000..9069c4a --- /dev/null +++ b/src/utils.h @@ -0,0 +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 +#include + +#include + +namespace utils +{ + class scope_exit + { + public: + explicit scope_exit(std::function func) + : m_f(func) + {} + ~scope_exit() + { + if (!!m_f) + m_f(); + } + + private: + std::function 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; + } +} -- cgit 1.2.3-korg