X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Flibhomescreen.cpp;h=dd5d3630acc33c25fd474871006bf2a12652332c;hb=869d5d3259969836a007c90c20e36eece5aa202b;hp=84e3472ef088f83776ade339d76193f117077273;hpb=c0146077906057bf97688a617870228eaad0cf54;p=src%2Flibhomescreen.git diff --git a/src/libhomescreen.cpp b/src/libhomescreen.cpp index 84e3472..dd5d363 100644 --- a/src/libhomescreen.cpp +++ b/src/libhomescreen.cpp @@ -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 #include #include +#include #include #include "hmi-debug.h" @@ -46,7 +48,8 @@ const std::vector LibHomeScreen::api_list { std::string("hideWindow"), std::string("replyShowWindow"), std::string("showNotification"), - std::string("showInformation") + std::string("showInformation"), + std::string("getRunnables") }; const std::vector LibHomeScreen::event_list { @@ -58,6 +61,7 @@ const std::vector 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,22 +109,23 @@ 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); + } } /** * This function is initializer * * #### Parameters - * - port [in] : This argument should be specified to the port number to be used for websocket - * - token [in] : This argument should be specified to the token to be used for websocket + * - hostname [in] : This argument should be specified to the hostname to be used for websocket + * - port [in] : This argument should be specified to the port number to be used for websocket + * - token [in] : This argument should be specified to the token to be used for websocket * * #### Return * Nothing @@ -123,20 +134,16 @@ LibHomeScreen::~LibHomeScreen() * Use this constructor * */ -int LibHomeScreen::init(const int port, const string& token) +int LibHomeScreen::init(const char *hostname, const int port, const char *token) { int ret = 0; - if(port > 0 && token.size() > 0) - { - mport = port; - mtoken = token; - } - else + if(port < 0 || token == nullptr || token[0] == 0) { HMI_ERROR("libhomescreen","port and token should be > 0, Initial port and token uses."); } - ret = initialize_websocket(); + ret = initialize_websocket(hostname, port, token); + if(ret != 0 ) { HMI_ERROR("libhomescreen","Failed to initialize websocket"); @@ -148,6 +155,25 @@ int LibHomeScreen::init(const int port, const string& token) return ret; } +/** + * This function is initializer + * + * #### Parameters + * - port [in] : This argument should be specified to the port number to be used for websocket + * - token [in] : This argument should be specified to the token to be used for websocket + * + * #### Return + * Nothing + * + * #### Note + * Use this constructor + * + */ +int LibHomeScreen::init(const int port, const string& token) +{ + return init(nullptr, port, token.c_str()); +} + /** * This function register callback function for reply/event message from home screen * @@ -177,18 +203,25 @@ 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; minterface.on_event = _on_event_static; - muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/ + sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this); if(sp_websock == NULL) { @@ -201,13 +234,19 @@ int LibHomeScreen::initialize_websocket() return 0; END: - if(mploop) - { - sd_event_unref(mploop); - } return -1; } +int LibHomeScreen::initialize_websocket(const char *hostname, const int port, const char *token) +{ + if (hostname == nullptr) + hostname = "localhost"; + muri = std::string("ws://") + hostname + ":" + to_string(port) + "/api?token=" + token; /*To be modified*/ + + return initialize_websocket(); +} + + /** * Sending ShortCut Icon tapped event * @@ -294,7 +333,7 @@ int LibHomeScreen::onScreenReply(const char* reply_message) */ void LibHomeScreen::set_event_handler(enum EventType et, handler_func f) { - if (et >= 1 && et <= 7) { + if (et > Event_Min && et < Event_Max) { switch (et) { case Event_ShowWindow: this->subscribe(LibHomeScreen::event_list[0]); @@ -317,6 +356,9 @@ void LibHomeScreen::set_event_handler(enum EventType et, handler_func f) case Event_ShowInformation: this->subscribe(LibHomeScreen::event_list[6]); break; + case Event_AppListChanged: + this->subscribe(LibHomeScreen::event_list[7]); + break; } this->handlers[et] = std::move(f); @@ -588,6 +630,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 +742,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); + } + } } /**