From 50d87b3696390d30f8801bcdff908e33ed7e61aa Mon Sep 17 00:00:00 2001 From: Marius Vlad Date: Wed, 22 Jan 2020 21:42:06 +0200 Subject: [PATCH] launcher/: Convert launcher to agl-compositor - adds pws/ directory just like homescreen to be able to get list of runnables apps and be able to spawn them with the help of libafbwsc - removes libhomescreen libwindowmanger call from main and qmake files Signed-off-by: Marius Vlad Change-Id: I2f6aebfe05a23389796e801300cb616000bdef2b --- launcher.pro | 4 +- launcher/launcher.pro | 6 +- launcher/src/applicationhandler.cpp | 30 +++ launcher/src/applicationhandler.h | 36 +++ launcher/src/applicationlauncher.cpp | 22 +- launcher/src/applicationlauncher.h | 7 +- launcher/src/applicationmodel.cpp | 8 +- launcher/src/applicationmodel.h | 2 + launcher/src/main.cpp | 91 +------ package/config.xml | 4 - pws/hmi-debug.h | 70 +++++ pws/launcher.cpp | 108 ++++++++ pws/launcher.h | 70 +++++ pws/pws.cpp | 501 +++++++++++++++++++++++++++++++++++ pws/pws.h | 100 +++++++ pws/pws.pri | 16 ++ pws/pws.pro | 23 ++ 17 files changed, 998 insertions(+), 100 deletions(-) create mode 100644 launcher/src/applicationhandler.cpp create mode 100644 launcher/src/applicationhandler.h create mode 100644 pws/hmi-debug.h create mode 100644 pws/launcher.cpp create mode 100644 pws/launcher.h create mode 100644 pws/pws.cpp create mode 100644 pws/pws.h create mode 100644 pws/pws.pri create mode 100644 pws/pws.pro diff --git a/launcher.pro b/launcher.pro index e142b63..9f6c696 100644 --- a/launcher.pro +++ b/launcher.pro @@ -16,9 +16,9 @@ TEMPLATE = subdirs load(configure) -SUBDIRS = interfaces \ +SUBDIRS = pws interfaces \ launcher \ package -launcher.depends = interfaces +launcher.depends = interfaces pws package.depends += launcher diff --git a/launcher/launcher.pro b/launcher/launcher.pro index e16e9ce..bf1989e 100644 --- a/launcher/launcher.pro +++ b/launcher/launcher.pro @@ -18,22 +18,22 @@ TARGET = launcher QT = qml quick dbus websockets CONFIG += c++11 link_pkgconfig DESTDIR = $${OUT_PWD}/../package/root/bin -PKGCONFIG += qlibwindowmanager libhomescreen include(../interfaces/interfaces.pri) +include(../pws/pws.pri) SOURCES += \ src/main.cpp \ src/applicationmodel.cpp \ src/appinfo.cpp \ src/applicationlauncher.cpp \ - src/homescreenhandler.cpp + src/applicationhandler.cpp HEADERS += \ src/applicationlauncher.h \ src/applicationmodel.h \ src/appinfo.h \ - src/homescreenhandler.h + src/applicationhandler.h OTHER_FILES += \ README.md diff --git a/launcher/src/applicationhandler.cpp b/launcher/src/applicationhandler.cpp new file mode 100644 index 0000000..38f5220 --- /dev/null +++ b/launcher/src/applicationhandler.cpp @@ -0,0 +1,30 @@ +#include +#include + +#include "applicationhandler.h" + + +ApplicationHandler::ApplicationHandler(QObject *parent, Launcher *launcher) : + QObject(parent) +{ + m_launcher = launcher; +} + +ApplicationHandler::~ApplicationHandler() +{ +} + +size_t +ApplicationHandler::getRunnables(void) +{ + QString data; + size_t runnables = 0; + + if (m_launcher == nullptr) + return runnables; + + runnables = m_launcher->get_list_runnables(data); + emit initAppList(data); + + return runnables; +} diff --git a/launcher/src/applicationhandler.h b/launcher/src/applicationhandler.h new file mode 100644 index 0000000..5a5508e --- /dev/null +++ b/launcher/src/applicationhandler.h @@ -0,0 +1,36 @@ +#ifndef APPLICATIONHANDLER_H +#define APPLICATIONHANDLER_H + +#include +#include +#include + +#include "launcher.h" + +using namespace std; + +class ApplicationHandler : public QObject +{ + Q_OBJECT +public: + explicit ApplicationHandler(QObject *parent = 0, Launcher *launcher = 0); + ~ApplicationHandler(); + + //void init(int port, const char* token, QLibWindowmanager *qwm, QString myname); + //Q_INVOKABLE void tapShortcut(QString application_id); + Q_INVOKABLE size_t getRunnables(void); + + //void onRep(struct json_object* reply_contents); + //static void* myThis; + //static void onRep_static(struct json_object* reply_contents); + +signals: + void initAppList(QString data); + void appListUpdate(QStringList info); + +private: + QString m_myname; + Launcher *m_launcher; +}; + +#endif // APPLICATIONHANDLER_H diff --git a/launcher/src/applicationlauncher.cpp b/launcher/src/applicationlauncher.cpp index 19ea2e3..9dcc3fc 100644 --- a/launcher/src/applicationlauncher.cpp +++ b/launcher/src/applicationlauncher.cpp @@ -18,13 +18,9 @@ #include "applicationlauncher.h" -#include "afm_user_daemon_proxy.h" - #include "hmi-debug.h" -extern org::AGL::afm::user *afm_user_daemon_proxy; - -ApplicationLauncher::ApplicationLauncher(QObject *parent) +ApplicationLauncher::ApplicationLauncher(const QString &conn_str, QObject *parent) : QObject(parent) , m_launching(false) , m_timeout(new QTimer(this)) @@ -43,15 +39,23 @@ ApplicationLauncher::ApplicationLauncher(QObject *parent) connect(this, &ApplicationLauncher::currentChanged, [&]() { setLaunching(false); }); + + m_launching = false; + m_launcher = new Launcher(conn_str, parent); + + if (m_launcher->setup_pws_connection() != 0) + HMI_DEBUG("Launcher","ApplicationLauncher failed to set-up connection to afm-system-daemon"); } int ApplicationLauncher::launch(const QString &application) { int result = -1; - HMI_DEBUG("launch","ApplicationLauncher launch %s.", application.toStdString().c_str()); + HMI_DEBUG("Launcher","ApplicationLauncher launch %s.", application.toStdString().c_str()); - result = afm_user_daemon_proxy->start(application).value().toInt(); - HMI_DEBUG("launch","ApplicationLauncher pid: %d.", result); + if (m_launcher->connection_is_set()) + result = m_launcher->start(application); + + HMI_DEBUG("Launcher","ApplicationLauncher pid: %d.", result); if (result > 1) { setLaunching(true); @@ -83,3 +87,5 @@ void ApplicationLauncher::setCurrent(const QString ¤t) m_current = current; emit currentChanged(current); } + + diff --git a/launcher/src/applicationlauncher.h b/launcher/src/applicationlauncher.h index d205994..4cb8e79 100644 --- a/launcher/src/applicationlauncher.h +++ b/launcher/src/applicationlauncher.h @@ -19,8 +19,11 @@ #ifndef APPLICATIONLAUNCHER_H #define APPLICATIONLAUNCHER_H +#include #include +#include "launcher.h" + class QTimer; class ApplicationLauncher : public QObject @@ -29,10 +32,11 @@ class ApplicationLauncher : public QObject Q_PROPERTY(bool launching READ isLaunching NOTIFY launchingChanged) Q_PROPERTY(QString current READ current WRITE setCurrent NOTIFY currentChanged) public: - explicit ApplicationLauncher(QObject *parent = NULL); + explicit ApplicationLauncher(const QString &afm_conn_str, QObject *parent = NULL); bool isLaunching() const; QString current() const; + Launcher *get_launcher(void) { return m_launcher; } signals: void newAppRequestsToBeVisible(int pid); @@ -50,6 +54,7 @@ private: bool m_launching; QString m_current; QTimer *m_timeout; + Launcher *m_launcher; }; #endif // APPLICATIONLAUNCHER_H diff --git a/launcher/src/applicationmodel.cpp b/launcher/src/applicationmodel.cpp index 261e43e..ccb5d06 100644 --- a/launcher/src/applicationmodel.cpp +++ b/launcher/src/applicationmodel.cpp @@ -21,13 +21,13 @@ #include "hmi-debug.h" +#include +#include +#include + #include #include -#include "afm_user_daemon_proxy.h" - -extern org::AGL::afm::user *afm_user_daemon_proxy; - class ApplicationModel::Private { public: diff --git a/launcher/src/applicationmodel.h b/launcher/src/applicationmodel.h index 780e575..0b64c5d 100644 --- a/launcher/src/applicationmodel.h +++ b/launcher/src/applicationmodel.h @@ -18,6 +18,8 @@ #ifndef APPLICATIONMODEL_H #define APPLICATIONMODEL_H +#include +#include #include class ApplicationModel : public QAbstractListModel diff --git a/launcher/src/main.cpp b/launcher/src/main.cpp index e550948..6d20ec8 100644 --- a/launcher/src/main.cpp +++ b/launcher/src/main.cpp @@ -23,47 +23,23 @@ #include #include #include +#include + +#include +#include -#include #include "applicationlauncher.h" #include "applicationmodel.h" +#include "applicationhandler.h" #include "appinfo.h" -#include "afm_user_daemon_proxy.h" -#include "homescreenhandler.h" -#include "hmi-debug.h" - -// XXX: We want this DBus connection to be shared across the different -// QML objects, is there another way to do this, a nice way, perhaps? -org::AGL::afm::user *afm_user_daemon_proxy; - -namespace { -struct Cleanup { - static inline void cleanup(org::AGL::afm::user *p) { - delete p; - afm_user_daemon_proxy = Q_NULLPTR; - } -}; - -} +#define CONNECT_STR "unix:/run/platform/apis/ws/afm-main" int main(int argc, char *argv[]) { QString myname = QString("launcher"); QGuiApplication a(argc, argv); - // use launch process - QScopedPointer afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user", - "/org/AGL/afm/user", - QDBusConnection::sessionBus(), - 0)); - ::afm_user_daemon_proxy = afm_user_daemon_proxy.data(); - - QCoreApplication::setOrganizationDomain("LinuxFoundation"); - QCoreApplication::setOrganizationName("AutomotiveGradeLinux"); - QCoreApplication::setApplicationName(myname); - QCoreApplication::setApplicationVersion("0.1.0"); - QCommandLineParser parser; parser.addPositionalArgument("port", a.translate("main", "port for binding")); parser.addPositionalArgument("secret", a.translate("main", "secret for binding")); @@ -80,44 +56,17 @@ int main(int argc, char *argv[]) token = positionalArguments.takeFirst(); } - HMI_DEBUG("launcher","port = %d, token = %s", port, token.toStdString().c_str()); - // import C++ class to QML qmlRegisterType("AppModel", 1, 0, "ApplicationModel"); // DBus qDBusRegisterMetaType(); - qDBusRegisterMetaType >(); - - ApplicationLauncher *launcher = new ApplicationLauncher(); - QLibWindowmanager* layoutHandler = new QLibWindowmanager(); - if(layoutHandler->init(port,token) != 0){ - exit(EXIT_FAILURE); - } - - AGLScreenInfo screenInfo(layoutHandler->get_scale_factor()); + qDBusRegisterMetaType>(); - if (layoutHandler->requestSurface(myname) != 0) { - exit(EXIT_FAILURE); - } - - layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, myname](json_object *object) { - layoutHandler->endDraw(myname); - }); - - layoutHandler->set_event_handler(QLibWindowmanager::Event_Visible, [layoutHandler, launcher](json_object *object) { - QString label = QString(json_object_get_string( json_object_object_get(object, "drawing_name") )); - qDebug() << label; - QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label == "HomeScreen" ? "Home" : label)); - }); - - layoutHandler->set_event_handler(QLibWindowmanager::Event_Invisible, [layoutHandler, launcher](json_object *object) { - const char* label = json_object_get_string( json_object_object_get(object, "drawing_name") ); - HMI_DEBUG("launch", "surface %s Event_Invisible", label); - }); - - HomescreenHandler* homescreenHandler = new HomescreenHandler(); - homescreenHandler->init(port, token.toStdString().c_str(), layoutHandler, myname); + ApplicationLauncher *launcher = + new ApplicationLauncher(CONNECT_STR, &a); + ApplicationHandler *homescreenHandler = + new ApplicationHandler(nullptr, launcher->get_launcher()); QUrl bindingAddress; bindingAddress.setScheme(QStringLiteral("ws")); @@ -129,28 +78,14 @@ int main(int argc, char *argv[]) query.addQueryItem(QStringLiteral("token"), token); bindingAddress.setQuery(query); - const QByteArray hack_delay = qgetenv("HMI_LAUNCHER_STARTUP_DELAY"); - int delay_time = 1; - - if (!hack_delay.isEmpty()) { - delay_time = (QString::fromLocal8Bit(hack_delay)).toInt(); - } - - QThread::sleep(delay_time); - qDebug("Sleep %d sec to resolve race condtion between HomeScreen and Launcher", delay_time); - // mail.qml loading QQmlApplicationEngine engine; - engine.rootContext()->setContextProperty(QStringLiteral("layoutHandler"), layoutHandler); + engine.rootContext()->setContextProperty(QStringLiteral("homescreenHandler"), homescreenHandler); engine.rootContext()->setContextProperty(QStringLiteral("launcher"), launcher); - engine.rootContext()->setContextProperty(QStringLiteral("screenInfo"), &screenInfo); engine.load(QUrl(QStringLiteral("qrc:/Launcher.qml"))); - homescreenHandler->getRunnables(); - QObject *root = engine.rootObjects().first(); - QQuickWindow *window = qobject_cast(root); - QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface())); + homescreenHandler->getRunnables(); return a.exec(); } diff --git a/package/config.xml b/package/config.xml index 36de348..15daca3 100644 --- a/package/config.xml +++ b/package/config.xml @@ -6,10 +6,6 @@ This is a demo application for launcher TOYOTA APL 2.0 - - - - diff --git a/pws/hmi-debug.h b/pws/hmi-debug.h new file mode 100644 index 0000000..250a836 --- /dev/null +++ b/pws/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/pws/launcher.cpp b/pws/launcher.cpp new file mode 100644 index 0000000..e3f1652 --- /dev/null +++ b/pws/launcher.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2020 Collabora Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "launcher.h" +#include "pws.h" + +int +Launcher::setup_pws_connection(void) +{ + pws = pws_data_source_init(afm_sock_name.toStdString().c_str()); + if (!pws) + return -1; + + connection_set = true; + return 0; +} + +bool +Launcher::connection_is_set(void) +{ + return connection_set; +} + +int +Launcher::start(const QString &app) +{ + int rid = -1; + + if (!connection_set) + return rid; + + rid = pws_start_process(pws, app.toStdString().c_str()); + if (rid > 0) + applications.insert(app, rid); + + return rid; +} + +bool +Launcher::terminate(const QString &app) +{ + if (!connection_set) + return -1; + + if (pws_stop_process(pws, app.toStdString().c_str())) + return true; + + return false; +} + +bool +Launcher::is_running(const QString &app) +{ + int rid = -1; + + if (!connection_set) + return false; + + rid = pws_check_process_is_running(pws, app.toStdString().c_str()); + /* remove it from QHash if it was there and current no longer shows up */ + if (rid > 0) { + return true; + } else { + if (applications.contains(app)) + applications.remove(app); + } + + return false; +} + +size_t +Launcher::get_list_runnables(QString &qstr) +{ + size_t items = 0; + struct json_object *json; + + if (!connection_set) + return false; + + items = pws_get_list_runnables(pws, &json); + if (json) { + const char *json_str = json_object_to_json_string(json); + qstr = QString(json_str); + } else { + qstr = nullptr; + } + + /* necessary as pws_get_list_runnables won't free() the json reply on + * its own */ + pws_data_source_reply_destroy(pws); + + return items; +} diff --git a/pws/launcher.h b/pws/launcher.h new file mode 100644 index 0000000..ba1e84b --- /dev/null +++ b/pws/launcher.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020 Collabora Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LAUNCHER_H +#define LAUNCHER_H + +#include +#include +#include +#include +#include + +#include + +#include "pws.h" + +class Launcher : public QObject +{ + Q_OBJECT + +public: + Launcher(const QString &sock_name, QObject *parent = nullptr) : + QObject(parent), afm_sock_name(sock_name) + { + pws = nullptr; + connection_set = false; + } + + ~Launcher() + { + destroy(); + } + + // call this before to any start-up + int setup_pws_connection(void); + bool connection_is_set(void); + +public slots: + int start(const QString &app); + bool terminate(const QString &app); + bool is_running(const QString &app); + size_t get_list_runnables(QString &data); + +private: + struct pws_data_source *pws; + bool connection_set; + QString afm_sock_name; + + QHash applications; // stores the apps started + + void destroy(void) + { + pws_data_source_destroy(pws); + } +}; + +#endif // LAUNCHER_H diff --git a/pws/pws.cpp b/pws/pws.cpp new file mode 100644 index 0000000..6268076 --- /dev/null +++ b/pws/pws.cpp @@ -0,0 +1,501 @@ +/* + * Copyright (c) 2020 Collabora Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include +#include +#include "pws.h" + +#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE) +#define JSON_C_TO_STRING_NOSLASHESCAPE 0 +#endif + +#define PWS_DEBUG + +static void +idle(struct pws_data_source *pws_d_source) +{ + struct sd_event *loop = pws_d_source->loop; + + for (;;) { + if (!pws_d_source->callcount) + break; + + sd_event_run(loop, TIMEOUT_SD_LOOP); + } +} + +static void +dec_callcount(struct pws_data_source *pws_d_source) +{ + if (!pws_d_source) + return; + + pws_d_source->callcount--; +} + +static void +inc_callcount(struct pws_data_source *pws_d_source) +{ + if (!pws_d_source) + return; + + pws_d_source->callcount++; +} + +static void +on_pws_reply(void *closure, void *request, struct json_object *result, + const char *error, const char *info) +{ + struct pws_data_source *pws_d_source = + static_cast(closure); + + assert(pws_d_source != NULL); + +#ifdef PWS_DEBUG + fprintf(stdout, "ON-REPLY %s: %s %s\n%s\n", (char*) request, error, + info ?: "", + json_object_to_json_string_ext(result, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE)); + + fflush(stdout); +#endif + + /* in case of an error do not set the reply */ + if (!info && !error) { + /* should be cleaned-up after a proper request */ + assert(pws_d_source->reply_valid == false); + + pws_d_source->reply = result; + pws_d_source->reply_valid = true; + } else { + fprintf(stdout, "ON-REPLY: err: %s, request: %s, info %s\n", + error, (char *) request, info); + } + + /* necessary when getting the reply to exit idle() */ + dec_callcount(pws_d_source); +} + +static void +on_pws_event_create(void *closure, uint16_t event_id, const char *event_name) +{ + struct pws_data_source *pws_d_source = + static_cast(closure); + (void) pws_d_source; + +#ifdef PWS_DEBUG + fprintf(stdout, "ON-EVENT-CREATE: [%d:%s]\n", event_id, event_name); + fflush(stdout); +#endif +} + +static void +on_pws_event_remove(void *closure, uint16_t event_id) +{ + struct pws_data_source *pws_d_source = + static_cast(closure); + + (void) pws_d_source; +#ifdef PWS_DEBUG + fprintf(stdout, "ON-EVENT-REMOVE: [%d]\n", event_id); + fflush(stdout); +#endif +} + +static void +on_pws_event_subscribe(void *closure, void *request, uint16_t event_id) +{ + struct pws_data_source *pws_d_source = + static_cast(closure); + + (void) pws_d_source; +#ifdef PWS_DEBUG + fprintf(stdout, "ON-EVENT-SUBSCRIBE %s: [%d]\n", (char*)request, event_id); + fflush(stdout); +#endif +} + +static void +on_pws_event_unsubscribe(void *closure, void *request, uint16_t event_id) +{ + struct pws_data_source *pws_d_source = + static_cast(closure); + + (void) pws_d_source; +#ifdef PWS_DEBUG + fprintf(stdout, "ON-EVENT-UNSUBSCRIBE %s: [%d]\n", (char*)request, event_id); + fflush(stdout); +#endif +} + +static void +on_pws_event_push(void *closure, uint16_t event_id, struct json_object *data) +{ + struct pws_data_source *pws_d_source = + static_cast(closure); + + (void) pws_d_source; +#ifdef PWS_DEBUG + fprintf(stdout, "ON-EVENT-PUSH: [%d]\n%s\n", + event_id, + json_object_to_json_string_ext(data, JSON_C_TO_STRING_NOSLASHESCAPE)); + fprintf(stdout, "ON-EVENT-PUSH: [%d]\n%s\n", + event_id, + json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY | + JSON_C_TO_STRING_NOSLASHESCAPE)); + + fflush(stdout); +#endif +} + +static void +on_pws_event_broadcast(void *closure, const char *event_name, + struct json_object *data, + const afb_proto_ws_uuid_t uuid, uint8_t hop) +{ + struct pws_data_source *pws_d_source = + static_cast(closure); + + (void) pws_d_source; + (void) uuid; + (void) hop; + +#ifdef PWS_DEBUG + fprintf(stdout, "ON-EVENT-BROADCAST: [%s]\n%s\n", + event_name, + json_object_to_json_string_ext(data, JSON_C_TO_STRING_NOSLASHESCAPE)); + fprintf(stdout, "ON-EVENT-BROADCAST: [%s]\n%s\n", + event_name, + json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY | + JSON_C_TO_STRING_NOSLASHESCAPE)); + fflush(stdout); +#endif +} + +/* called when pws hangsup */ +static void +on_pws_hangup(void *closure) +{ + struct pws_data_source *pws_d_source = + static_cast(closure); + + (void) pws_d_source; +#ifdef PWS_DEBUG + printf("ON-HANGUP\n"); + fflush(stdout); +#endif + + exit(EXIT_FAILURE); +} + +/* makes a call */ +static int +pws_call(const char *verb, const char *object, + struct pws_data_source *pws_d_source) +{ + char *key; + struct json_object *o; + enum json_tokener_error jerr; + + struct afb_proto_ws *pws = pws_d_source->pws; + int rc; + + assert(pws != NULL); + + /* allocates an id for the request */ + rc = asprintf(&key, "%d:%s", ++pws_d_source->num_id, verb); + if (rc < 0) + return -1; + + /* echo the command if asked */ + fprintf(stdout, "SEND-CALL: %s %s\n", verb, object?:"null"); + + inc_callcount(pws_d_source); + + if (object == NULL || object[0] == 0) { + o = NULL; + } else { + o = json_tokener_parse_verbose(object, &jerr); + if (jerr != json_tokener_success) + o = json_object_new_string(object); + } + + /* send the request */ + rc = afb_proto_ws_client_call(pws, verb, o, 0, 0, key, NULL); + json_object_put(o); + if (rc < 0) { + fprintf(stderr, "calling %s(%s) failed: %m\n", verb, object ?: ""); + dec_callcount(pws_d_source); + free(key); + return -1; + } + + free(key); + return 0; +} + +/* the callback interface for pws */ +static struct afb_proto_ws_client_itf pws_itf = { + .on_reply = on_pws_reply, + .on_event_create = on_pws_event_create, + .on_event_remove = on_pws_event_remove, + .on_event_subscribe = on_pws_event_subscribe, + .on_event_unsubscribe = on_pws_event_unsubscribe, + .on_event_push = on_pws_event_push, + .on_event_broadcast = on_pws_event_broadcast, +}; + +struct pws_data_source * +pws_data_source_init(const char *connect_to) +{ + struct afb_proto_ws *pws; + struct sd_event *sd_loop; + struct pws_data_source *pws_d_source = nullptr; + + if (!connect_to) { + fprintf(stderr, "Failed to get a connect_to\n"); + return nullptr; + } + + if (sd_event_default(&sd_loop) < 0) { + fprintf(stderr, "Failed to get a default event\n"); + return nullptr; + } + + pws_d_source = + static_cast(calloc(1, sizeof(struct pws_data_source))); + + if (!pws_d_source) { + fprintf(stderr, "Failed to allocate memory\n"); + return nullptr; + } + + pws = afb_ws_client_connect_api(sd_loop, connect_to, + &pws_itf, pws_d_source); + if (!pws) { + fprintf(stderr, "Failed to create a afb_proto_ws\n"); + return nullptr; + } + + afb_proto_ws_on_hangup(pws, on_pws_hangup); + + pws_d_source->pws = pws; + pws_d_source->loop = sd_loop; + pws_d_source->callcount = 0; + pws_d_source->num_id = 0; + + pws_d_source->reply = nullptr; + pws_d_source->reply_valid = false; + + return pws_d_source; +} + +static int +pws_do_call(struct pws_data_source *pws_d_source, + const char *verb, const char *object) +{ + if (!verb || !object || !pws_d_source) + return -1; + + if (pws_call(verb, object, pws_d_source) < 0) + return -1; + + idle(pws_d_source); + + return 0; +} + +void +pws_data_source_destroy(struct pws_data_source *pws_d_source) +{ + assert(pws_d_source != nullptr); + + sd_event_unref(pws_d_source->loop); + free(pws_d_source); +} + +void +pws_data_source_reply_destroy(struct pws_data_source *pws) +{ + assert(pws->reply_valid == true); + pws->reply_valid = false; + free(pws->reply); +} + + +int +pws_start_process(struct pws_data_source *pws, const char *afm_name) +{ + int rid = -1; + if (pws_do_call(pws, "start", afm_name) < 0) + return -1; + + if (pws->reply_valid) { + struct json_object *result = pws->reply; + + rid = json_object_get_int(result); + fprintf(stdout, "ON-REPLY %s\n", + json_object_to_json_string_ext(result, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE)); + + pws_data_source_reply_destroy(pws); + } + + return rid; +} + +bool +pws_stop_process(struct pws_data_source *pws, const char *afm_name) +{ + bool term = false; + if (pws_do_call(pws, "terminate", afm_name) < 0) + return term; + + if (pws->reply_valid) { + struct json_object *result = pws->reply; + + term = json_object_get_boolean(result); +#ifdef PWS_DEBUG + fprintf(stdout, "ON-REPLY %s\n", + json_object_to_json_string_ext(result, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE)); +#endif + pws_data_source_reply_destroy(pws); + } + + return term; +} + + +int +pws_check_process_is_running(struct pws_data_source *pws, const char *afm_name) +{ + int rid = -1; + + if (pws_do_call(pws, "state", afm_name) < 0) + return rid; + + if (pws->reply_valid) { + struct json_object *result = pws->reply; + struct json_object *obj; + + json_object_object_get_ex(result, "runid", &obj); + if (obj) { + rid = json_object_get_int(obj); + fprintf(stdout, "Found rid %d\n", rid); + } +#ifdef PWS_DEBUG + fprintf(stdout, "ON-REPLY: %s\n", + json_object_to_json_string_ext(result, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE)); +#endif + pws_data_source_reply_destroy(pws); + } + + return rid; +} + +size_t +pws_list_runners(struct pws_data_source *pws) +{ + size_t items = 0; + if (pws_do_call(pws, "runners", "true") < 0) + return items; + + if (pws->reply_valid) { + struct json_object *result = pws->reply; + + /* at least one is running */ + items = json_object_array_length(result); + +#ifdef PWS_DEBUG + fprintf(stdout, "found %ld items\n", items); + fprintf(stdout, "ON-REPLY: %s\n", + json_object_to_json_string_ext(result, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE)); +#endif + pws_data_source_reply_destroy(pws); + } + + return items; +} + +size_t +pws_list_runnables(struct pws_data_source *pws) +{ + size_t items = 0; + if (pws_do_call(pws, "runnables", "true") < 0) + return items; + + if (pws->reply_valid) { + struct json_object *result = pws->reply; + + items = json_object_array_length(result); +#ifdef PWS_DEBUG + fprintf(stdout, "found %ld items\n", items); + fprintf(stdout, "ON-REPLY: %s\n", + json_object_to_json_string_ext(result, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE)); +#endif + pws_data_source_reply_destroy(pws); + } + + return items; +} + +/* + * need to free(json_string) once you're done with it + * + * call pws_data_source_reply_destroy(pws) once you're done with it. + */ +size_t +pws_get_list_runnables(struct pws_data_source *pws, struct json_object **json) +{ + size_t items = 0; + if (pws_do_call(pws, "runnables", "true") < 0) + return items; + + fprintf(stdout, "pws_get_list_runnables()\n"); + HMI_DEBUG("Launcher", "pws_get_list_runnables()"); + + if (pws->reply_valid) { + struct json_object *result = pws->reply; + + items = json_object_array_length(result); +#ifdef PWS_DEBUG + fprintf(stdout, "found %ld items\n", items); + HMI_DEBUG("Launcher", "pws_get_list_runnables(), found %ld items", items); + fprintf(stdout, "ON-REPLY: %s\n", + json_object_to_json_string_ext(result, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE)); +#endif + if (items == 0) { + fprintf(stdout, "pws_get_list_runnables() turn items %d, bails sooner\n", items); + *json = NULL; + return items; + } + + fprintf(stdout, "pws_get_list_runnables() json reply is set\n"); + *json = result; + } + + fprintf(stdout, "pws_get_list_runnables() turn items %d\n", items); + return items; +} diff --git a/pws/pws.h b/pws/pws.h new file mode 100644 index 0000000..4e49320 --- /dev/null +++ b/pws/pws.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2020 Collabora Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PWS_HEADER_H +#define PWS_HEADER_H + +#include +#include "hmi-debug.h" + +/* linker will complain it can't find them and given they're only for C */ +extern "C" { +#include "afb/afb-ws-client.h" +#include "afb/afb-proto-ws.h" +} + +#define TIMEOUT_SD_LOOP 30000000 + +struct pws_data_source_json_reply; + +struct pws_data_source { + struct sd_event *loop; + struct afb_proto_ws *pws; + int callcount; + int num_id; /* key id */ + + struct json_object *reply; + bool reply_valid; +}; + +void +pws_data_source_reply_destroy(struct pws_data_source *pws); + +/* inits a connection to connect_to + * + */ +struct pws_data_source * +pws_data_source_init(const char *connect_to); + +/* destroys connection init'ed by pws_data_source_init + */ +void +pws_data_source_destroy(struct pws_data_source *pws); + + +/* + * starts the app by @afm_name + * + * returns 0 in case of success, negative in case of failure + * + */ +int +pws_start_process(struct pws_data_source *pws, const char *afm_name); + +/* + * terminates the app by @afm_name + * + * returns true case of success, false in case of failure + */ +bool +pws_stop_process(struct pws_data_source *pws, const char *afm_name); + +/* returns pid or -1 in case the @afm_name is not running + * + */ +int +pws_check_process_is_running(struct pws_data_source *pws, const char *afm_name); + +/* returns #no of (current) active, running applications + * + * (prints) as well + */ +size_t +pws_list_runners(struct pws_data_source *pws); + +/* returns #no of (all) applications + * + * (prints) as well + */ +size_t +pws_list_runnables(struct pws_data_source *pws); + +/* + */ +size_t +pws_get_list_runnables(struct pws_data_source *pws, struct json_object **json_obj); + +#endif diff --git a/pws/pws.pri b/pws/pws.pri new file mode 100644 index 0000000..530c61e --- /dev/null +++ b/pws/pws.pri @@ -0,0 +1,16 @@ +# Copyright (C) 2019 Collabora Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +INCLUDEPATH += $$PWD $$OUT_PWD/../pws/ +LIBS += -L$$OUT_PWD/../pws/ -lpws -lafbwsc -lsystemd -ljson-c diff --git a/pws/pws.pro b/pws/pws.pro new file mode 100644 index 0000000..e661adb --- /dev/null +++ b/pws/pws.pro @@ -0,0 +1,23 @@ +# Copyright (C) 2019 Collabora Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +TEMPLATE = lib +TARGET = pws +CONFIG += staticlib +PKGCONFIG += libafbwsc + +HEADERS += pws.h launcher.h + +SOURCES += pws.cpp launcher.cpp + -- 2.16.6