#include <sys/socket.h>
#include <iostream>
#include <algorithm>
-#include <thread>
#include <errno.h>
#include <cassert>
#include <cctype>
std::string("ping"), // debug do not use
std::string("tap_shortcut"), // HomeScreen Application only
std::string("on_screen_message"),
+ std::string("on_screen_reply"),
std::string("subscribe"),
std::string("unsubscribe")
};
const std::vector<std::string> LibHomeScreen::event_list {
std::string("tap_shortcut"),
std::string("on_screen_message"),
+ std::string("on_screen_reply"),
std::string("none")
};
*/
LibHomeScreen::LibHomeScreen()
{
-
}
/**
HMI_DEBUG("libhomescreen","Initialized");
}
- this->runEventloop();
-
return ret;
}
/* Initialize interface from websocket */
minterface.on_hangup = _on_hangup_static;
- minterface.on_call = _on_call_static; /* Is this necessary? */
+ 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);
return -1;
}
-static void *event_loop_run(void *args)
-{
- struct sd_event* loop = (struct sd_event*)(args);
- HMI_DEBUG("libhomescreen","start eventloop");
- for(;;)
- sd_event_run(loop, 30000000);
-}
-
/**
- * This function start receiving the reply/event message from home screen
+ * Sending ShortCut Icon tapped event
+ *
+ * When HomeScreen shortcut area is tapped, sending a event
*
* #### Parameters
- * Nothing
+ * - application_id [in] : Tapped application id (label)
*
* #### Return
- * - Returns thread_id on success or -1 in case of error.
- *
- * #### Note
- *
+ * - Returns 0 on success or -1 in case of error.
*/
-int LibHomeScreen::runEventloop()
+int LibHomeScreen::tapShortcut(const char* application_id)
{
- if(mploop && sp_websock)
- {
- pthread_t thread_id;
- int ret = pthread_create(&thread_id, NULL, event_loop_run, mploop);
- if(ret != 0)
- {
- HMI_ERROR("libhomescreen","Cannot run eventloop due to error:%d", errno);
- return -1;
- }
- else
- return thread_id;
- }
- else
+ if(!sp_websock)
{
- HMI_ERROR("libhomescreen","Connecting is not established yet");
return -1;
}
+
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* val = json_object_new_string(application_id);
+ json_object_object_add(j_obj, "application_id", val);
+ return this->call("tap_shortcut", j_obj);
}
/**
- * Sending ShortCut Icon tapped event
+ * Sending onScreen message event
*
- * When HomeScreen shortcut area is tapped, sending a event
+ * Sending OnScreen message event to HomeScreen from applications
*
* #### Parameters
- * - application_name [in] : Tapped application name (label)
+ * - display_message [in] : message for display
*
* #### Return
* - Returns 0 on success or -1 in case of error.
*/
-int LibHomeScreen::tapShortcut(const char* application_name)
+int LibHomeScreen::onScreenMessage(const char* display_message)
{
if(!sp_websock)
{
}
struct json_object* j_obj = json_object_new_object();
- struct json_object* val = json_object_new_string(application_name);
- json_object_object_add(j_obj, "application_name", val);
- return this->call("tap_shortcut", j_obj);
+ struct json_object* val = json_object_new_string(display_message);
+ json_object_object_add(j_obj, "display_message", val);
+ return this->call("on_screen_message", j_obj);
}
/**
- * Sending onScreen message event
+ * Sending onScreen reply event
*
- * Sending OnScreen message event to HomeScreen from applications
+ * Sending OnScreen reply event to applications from HomeScreen
*
* #### Parameters
- * - display_message [in] : message for display
+ * - reply_message [in] : message for reply
*
* #### Return
* - Returns 0 on success or -1 in case of error.
*/
-int LibHomeScreen::onScreenMessage(const char* display_message)
+int LibHomeScreen::onScreenReply(const char* reply_message)
{
if(!sp_websock)
{
}
struct json_object* j_obj = json_object_new_object();
- struct json_object* val = json_object_new_string(display_message);
- json_object_object_add(j_obj, "display_message", val);
- return this->call("on_screen_message", j_obj);
+ struct json_object* val = json_object_new_string(reply_message);
+ json_object_object_add(j_obj, "reply_message", val);
+ return this->call("on_screen_reply", j_obj);
}
/**
*
* #### Return
* Nothing
+ *
+ * #### Note
+ * Don't release json_object by json_object_put in handler_func.
+ * The resource is released by libafbwsc library.
*/
void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
{
- if (et >= 1 && et <= 2) {
+ if (et >= 1 && et <= 3) {
switch (et) {
case Event_TapShortcut:
this->subscribe(LibHomeScreen::event_list[0]);
case Event_OnScreenMessage:
this->subscribe(LibHomeScreen::event_list[1]);
break;
+ case Event_OnScreenReply:
+ this->subscribe(LibHomeScreen::event_list[2]);
+ break;
}
this->handlers[et] = std::move(f);
}
/*
-* event is like "homescreen/tap_shortcut"
-* msg is like {"event":"homescreen\/tap_shortcut","data":{"application_name":"hoge"},"jtype":"afb-event"}
+* event is like "homescreen/hvac"
+* msg is like {"event":"homescreen\/hvac","data":{"type":"tap_shortcut"},"jtype":"afb-event"}
* so you can get
event name : struct json_object obj = json_object_object_get(msg,"event")
*/
}
struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
- struct json_object *json_data = json_object_object_get(ev_contents, "data");
+ struct json_object *json_data;
+ if(!json_object_object_get_ex(ev_contents, "data", &json_data)) {
+ HMI_ERROR("libhomescreen", "got ev_contents error.");
+ return;
+ }
if(onEvent != nullptr)
{
onEvent(ev, ev_contents);
}
- const char* event_only = strchr(event, '/');
- if (event_only != nullptr) {
- event_only = event_only + 1;
+ const char* event_type = nullptr;
+ struct 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);
+ }
+ else {
+ HMI_WARNING("libhomescreen","event_type is null.");
+ return;
}
- if (strcasecmp(event_only, LibHomeScreen::event_list[0].c_str()) == 0) {
+ if (strcasecmp(event_type, LibHomeScreen::event_list[0].c_str()) == 0) {
auto i = this->handlers.find(Event_TapShortcut);
-
- struct json_object *json_application_name = json_object_object_get(json_data, "application_name");
- const char* application_name = json_object_get_string(json_application_name);
-
if ( i != this->handlers.end() ) {
- i->second(application_name);
+ i->second(json_data);
}
}
- else if (strcasecmp(event_only, LibHomeScreen::event_list[1].c_str()) == 0) {
-
+ else if (strcasecmp(event_type, LibHomeScreen::event_list[1].c_str()) == 0) {
auto i = this->handlers.find(Event_OnScreenMessage);
-
- struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
- const char* display_message = json_object_get_string(json_display_message);
-
if ( i != this->handlers.end() ) {
- i->second(display_message);
+ i->second(json_data);
+ }
+ }
+ else if (strcasecmp(event_type, LibHomeScreen::event_list[2].c_str()) == 0) {
+ auto i = this->handlers.find(Event_OnScreenReply);
+ if ( i != this->handlers.end() ) {
+ i->second(json_data);
}
-
}
-
- json_object_put(ev_contents);
}
/**
{
struct json_object* reply = afb_wsj1_msg_object_j(msg);
onReply(reply);
-
- json_object_put(reply);
}
}