18 #include <sys/socket.h> 30 #include "hmi-debug.h" 34 static bool has_verb(
const string& verb);
35 static const char API[] =
"homescreen";
40 std::string(
"tap_shortcut"),
41 std::string(
"on_screen_message"),
42 std::string(
"on_screen_reply"),
43 std::string(
"subscribe"),
44 std::string(
"unsubscribe"),
45 std::string(
"showWindow"),
46 std::string(
"hideWindow"),
47 std::string(
"replyShowWindow"),
48 std::string(
"showNotification"),
49 std::string(
"showInformation")
54 std::string(
"showWindow"),
55 std::string(
"on_screen_message"),
56 std::string(
"on_screen_reply"),
57 std::string(
"hideWindow"),
58 std::string(
"replyShowWindow"),
59 std::string(
"showNotification"),
60 std::string(
"showInformation"),
74 static void _on_call_static(
void *closure,
const char *api,
const char *verb,
struct afb_wsj1_msg *msg)
79 static void _on_event_static(
void* closure,
const char* event,
struct afb_wsj1_msg *msg)
81 static_cast<LibHomeScreen*
>(closure)->on_event(NULL,event,msg);
104 sd_event_unref(mploop);
106 if(sp_websock != NULL)
108 afb_wsj1_unref(sp_websock);
129 if(port > 0 && token.size() > 0)
136 HMI_ERROR(
"libhomescreen",
"port and token should be > 0, Initial port and token uses.");
139 ret = initialize_websocket();
142 HMI_ERROR(
"libhomescreen",
"Failed to initialize websocket");
145 HMI_DEBUG(
"libhomescreen",
"Initialized");
166 void (*event_cb)(
const std::string& event,
struct json_object* event_contents),
167 void (*reply_cb)(
struct json_object* reply_contents),
168 void (*hangup_cb)(
void))
172 onHangup = hangup_cb;
175 int LibHomeScreen::initialize_websocket()
180 int ret = sd_event_default(&mploop);
183 HMI_ERROR(
"libhomescreen",
"Failed to create event loop");
191 muri +=
"ws://localhost:" + to_string(mport) +
"/api?token=" + mtoken;
192 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface,
this);
193 if(sp_websock == NULL)
195 HMI_ERROR(
"libhomescreen",
"Failed to create websocket connection");
206 sd_event_unref(mploop);
224 struct json_object* obj = json_object_new_object();
225 struct json_object* val = json_object_new_string(
"normal");
226 json_object_object_add(obj,
"area", val);
249 struct json_object* j_obj = json_object_new_object();
250 struct json_object* val = json_object_new_string(display_message);
251 json_object_object_add(j_obj,
"display_message", val);
252 return this->call(
"on_screen_message", j_obj);
273 struct json_object* j_obj = json_object_new_object();
274 struct json_object* val = json_object_new_string(reply_message);
275 json_object_object_add(j_obj,
"reply_message", val);
276 return this->call(
"on_screen_reply", j_obj);
297 if (et >= 1 && et <= 7) {
302 case Event_ShowWindow:
305 case Event_OnScreenMessage:
308 case Event_OnScreenReply:
311 case Event_HideWindow:
314 case Event_ReplyShowWindow:
317 case Event_ShowNotification:
320 case Event_ShowInformation:
325 this->handlers[et] = std::move(f);
352 HMI_ERROR(
"libhomescreen",
"verb doesn't exit");
357 HMI_ERROR(
"libhomescreen",
"Failed to call verb:%s",verb.c_str());
386 HMI_ERROR(
"libhomescreen",
"verb doesn't exit");
391 HMI_ERROR(
"libhomescreen",
"Failed to call verb:%s",verb);
415 struct json_object* j_obj = json_object_new_object();
416 json_object_object_add(j_obj,
"event", json_object_new_string(event_name.c_str()));
420 HMI_ERROR(
"libhomescreen",
"Failed to call verb");
444 struct json_object* j_obj = json_object_new_object();
445 json_object_object_add(j_obj,
"event", json_object_new_string(event_name.c_str()));
449 HMI_ERROR(
"libhomescreen",
"Failed to call verb");
474 struct json_object* j_obj = json_object_new_object();
475 struct json_object* val = json_object_new_string(application_id);
478 if (json ==
nullptr) {
479 struct json_object* j_json = json_object_new_object();
480 struct json_object* value = json_object_new_string(
"normal");
481 json_object_object_add(j_json,
"area", value);
482 json_object_object_add(j_obj,
"parameter", j_json);
485 json_object_object_add(j_obj,
"parameter", json);
488 return this->call(
"showWindow", j_obj);
510 struct json_object* j_obj = json_object_new_object();
511 struct json_object* val = json_object_new_string(application_id);
514 return this->call(
"hideWindow", j_obj);
537 if (json ==
nullptr) {
538 HMI_WARNING(
"libhomescreen",
"replyShowWindow`s parameter is null");
542 struct json_object* j_obj = json_object_new_object();
543 struct json_object* val = json_object_new_string(application_id);
545 json_object_object_add(j_obj,
"parameter", json);
547 return this->call(
"replyShowWindow", j_obj);
569 return this->call(
"showNotification", json);
591 return this->call(
"showInformation", json);
600 if(onHangup !=
nullptr)
618 HMI_DEBUG(
"libhomescreen",
"event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
620 if (strstr(event,
API) == NULL) {
624 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
625 struct json_object *json_data;
626 if(!json_object_object_get_ex(ev_contents,
"data", &json_data)) {
627 HMI_ERROR(
"libhomescreen",
"got ev_contents error.");
631 if(onEvent !=
nullptr)
633 const string ev(event);
634 onEvent(ev, ev_contents);
637 const char* event_type =
nullptr;
638 struct json_object *json_event_type;
639 if(json_object_object_get_ex(json_data,
"type", &json_event_type)) {
640 event_type = json_object_get_string(json_event_type);
643 HMI_WARNING(
"libhomescreen",
"event_type is null.");
648 auto i = this->handlers.find(Event_ShowWindow);
649 if ( i != this->handlers.end() ) {
650 i->second(json_data);
654 auto i = this->handlers.find(Event_OnScreenMessage);
655 if ( i != this->handlers.end() ) {
656 i->second(json_data);
660 auto i = this->handlers.find(Event_OnScreenReply);
661 if ( i != this->handlers.end() ) {
662 i->second(json_data);
666 auto i = this->handlers.find(Event_HideWindow);
667 if ( i != this->handlers.end() ) {
668 i->second(json_data);
672 auto i = this->handlers.find(Event_ReplyShowWindow);
673 if ( i != this->handlers.end() ) {
674 i->second(json_data);
678 auto i = this->handlers.find(Event_ShowNotification);
679 if ( i != this->handlers.end() ) {
680 i->second(json_data);
684 auto i = this->handlers.find(Event_ShowInformation);
685 if ( i != this->handlers.end() ) {
686 i->second(json_data);
697 HMI_DEBUG(
"libhomescreen",
"msg: (%s)", afb_wsj1_msg_object_s(msg));
698 if(onReply !=
nullptr)
700 struct json_object* reply = afb_wsj1_msg_object_j(msg);
707 HMI_DEBUG(
"libhomescreen",
"verb is %s", verb.c_str());
void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
void on_reply(void *closure, struct afb_wsj1_msg *msg)
static void showWindow(afb_req_t request)
int onScreenReply(const char *reply_message)
std::function< void(json_object *)> handler_func
void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
static void subscribe(afb_req_t request)
int tapShortcut(const char *application_id)
static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
int hideWindow(const char *application_id)
int showWindow(const char *application_id, json_object *json)
static const char ApplicationId[]
int replyShowWindow(const char *application_id, json_object *json)
int call(const std::string &verb, struct json_object *arg)
int showInformation(json_object *json)
void on_hangup(void *closure, struct afb_wsj1 *wsj)
int subscribe(const std::string &event_name)
#define HMI_WARNING(prefix, args,...)
static const std::vector< std::string > event_list
int init(const int port, const std::string &token)
static bool has_verb(const string &verb)
static void _on_event_static(void *closure, const char *event, struct afb_wsj1_msg *msg)
#define HMI_ERROR(prefix, args,...)
#define HMI_DEBUG(prefix, args,...)
int onScreenMessage(const char *display_message)
static const std::vector< std::string > api_list
int unsubscribe(const std::string &event_name)
void set_event_handler(enum EventType et, handler_func f)
void registerCallback(void(*event_cb)(const std::string &event, struct json_object *event_contents), void(*reply_cb)(struct json_object *reply_contents), void(*hangup_cb)(void)=nullptr)
int showNotification(json_object *json)
static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)