modify subscribe opportunity
[src/libhomescreen.git] / src / libhomescreen.cpp
index 84e3472..71afa66 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
+ * Copyright (c) 2018,2019 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.
@@ -25,6 +26,7 @@
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
+#include <thread>
 
 #include <libhomescreen.hpp>
 #include "hmi-debug.h"
@@ -46,7 +48,8 @@ const std::vector<std::string> LibHomeScreen::api_list {
        std::string("hideWindow"),
        std::string("replyShowWindow"),
        std::string("showNotification"),
-       std::string("showInformation")
+       std::string("showInformation"),
+       std::string("getRunnables")
 };
 
 const std::vector<std::string> LibHomeScreen::event_list {
@@ -58,6 +61,7 @@ const std::vector<std::string> LibHomeScreen::event_list {
        std::string("replyShowWindow"),
        std::string("showNotification"),
        std::string("showInformation"),
+       std::string("application-list-changed"),
        std::string("none")
 };
 
@@ -87,6 +91,12 @@ static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
 }
 
 
+static void event_loop_run(struct sd_event* loop){
+       sd_event_loop(loop);
+       sd_event_unref(loop);
+}
+
+
 /**
  * constructor
  */
@@ -99,14 +109,14 @@ LibHomeScreen::LibHomeScreen()
  */
 LibHomeScreen::~LibHomeScreen()
 {
-       if(mploop)
-       {
-               sd_event_unref(mploop);
-       }
        if(sp_websock != NULL)
        {
                afb_wsj1_unref(sp_websock);
        }
+       if(mploop)
+       {
+               sd_event_exit(mploop, 0);
+       }
 }
 
 /**
@@ -177,13 +187,19 @@ int LibHomeScreen::initialize_websocket()
        mploop = NULL;
        onEvent = nullptr;
        onReply = nullptr;
-       int ret = sd_event_default(&mploop);
+       int ret = sd_event_new(&mploop);
        if(ret < 0)
        {
                HMI_ERROR("libhomescreen","Failed to create event loop");
                goto END;
        }
 
+       {
+               // enforce context to avoid initialization/goto error
+               std::thread th(event_loop_run, mploop);
+               th.detach();
+       }
+
        /* Initialize interface from websocket */
        minterface.on_hangup = _on_hangup_static;
        minterface.on_call = _on_call_static;
@@ -201,10 +217,6 @@ int LibHomeScreen::initialize_websocket()
 
        return 0;
 END:
-       if(mploop)
-       {
-               sd_event_unref(mploop);
-       }
        return -1;
 }
 
@@ -294,35 +306,31 @@ int LibHomeScreen::onScreenReply(const char* reply_message)
  */
 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
 {
-       if (et >= 1 && et <= 7) {
-               switch (et) {
-                       case Event_ShowWindow:
-                               this->subscribe(LibHomeScreen::event_list[0]);
-                               break;
-                       case Event_OnScreenMessage:
-                               this->subscribe(LibHomeScreen::event_list[1]);
-                               break;
-                       case Event_OnScreenReply:
-                               this->subscribe(LibHomeScreen::event_list[2]);
-                               break;
-                       case Event_HideWindow:
-                               this->subscribe(LibHomeScreen::event_list[3]);
-                               break;
-                       case Event_ReplyShowWindow:
-                               this->subscribe(LibHomeScreen::event_list[4]);
-                               break;
-                       case Event_ShowNotification:
-                               this->subscribe(LibHomeScreen::event_list[5]);
-                               break;
-                       case Event_ShowInformation:
-                               this->subscribe(LibHomeScreen::event_list[6]);
-                               break;
-               }
-
+       if (et > Event_Min && et < Event_Max) {
                this->handlers[et] = std::move(f);
        }
 }
 
+/**
+ * This function subscribe HomeScreen event
+ *
+ * #### Parameters
+ * None
+ *
+ * #### Return
+ * - Nothing
+ *
+ * #### Note
+ * To call HomeScreen's subscribe APIs.
+ *
+ */
+void LibHomeScreen::publishSubscription(void)
+{
+       for(auto &it : handlers) {
+               this->subscribe(LibHomeScreen::event_list[it.first - 1]);
+       }
+}
+
 /**
  * This function calls the API of HomeScreen via WebSocket
  *
@@ -588,6 +596,23 @@ int LibHomeScreen::showInformation(json_object* json)
        return this->call("showInformation", json);
 }
 
+/**
+ * get runnables list
+ *
+ * Call HomeScreen Service's getRunnables verb to get runnalbes list.
+ *
+ * #### Parameters
+ * - Nothing
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of error.
+ *
+ */
+int LibHomeScreen::getRunnables(void)
+{
+       return this->call("getRunnables", nullptr);
+}
+
 
 /************* Callback Function *************/
 
@@ -683,6 +708,12 @@ void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_m
                        i->second(json_data);
                }
        }
+       else if (strcasecmp(event_type, LibHomeScreen::event_list[7].c_str()) == 0) {
+               auto i = this->handlers.find(Event_AppListChanged);
+               if ( i != this->handlers.end() ) {
+                       i->second(json_data);
+               }
+       }
 }
 
 /**