18 #include <sys/socket.h> 35 static bool has_verb(
const string& verb);
36 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")
48 std::string(
"tap_shortcut"),
49 std::string(
"on_screen_message"),
50 std::string(
"on_screen_reply"),
64 static void _on_call_static(
void *closure,
const char *api,
const char *verb,
struct afb_wsj1_msg *msg)
71 static_cast<LibHomeScreen*
>(closure)->on_event(NULL,event,msg);
94 sd_event_unref(mploop);
96 if(sp_websock != NULL)
98 afb_wsj1_unref(sp_websock);
119 if(port > 0 && token.size() > 0)
126 HMI_ERROR(
"libhomescreen",
"port and token should be > 0, Initial port and token uses.");
129 ret = initialize_websocket();
132 HMI_ERROR(
"libhomescreen",
"Failed to initialize websocket");
135 HMI_DEBUG(
"libhomescreen",
"Initialized");
156 void (*event_cb)(
const std::string&
event,
struct json_object* event_contents),
157 void (*reply_cb)(
struct json_object* reply_contents),
158 void (*hangup_cb)(
void))
162 onHangup = hangup_cb;
165 int LibHomeScreen::initialize_websocket()
170 int ret = sd_event_default(&mploop);
173 HMI_ERROR(
"libhomescreen",
"Failed to create event loop");
181 muri +=
"ws://localhost:" + to_string(mport) +
"/api?token=" + mtoken;
182 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface,
this);
183 if(sp_websock == NULL)
185 HMI_ERROR(
"libhomescreen",
"Failed to create websocket connection");
196 sd_event_unref(mploop);
203 struct sd_event* loop = (
struct sd_event*)(args);
204 HMI_DEBUG(
"libhomescreen",
"start eventloop");
206 sd_event_run(loop, 30000000);
227 struct json_object* j_obj = json_object_new_object();
228 struct json_object* val = json_object_new_string(application_name);
229 json_object_object_add(j_obj,
"application_name", val);
230 return this->call(
"tap_shortcut", j_obj);
251 struct json_object* j_obj = json_object_new_object();
252 struct json_object* val = json_object_new_string(display_message);
253 json_object_object_add(j_obj,
"display_message", val);
254 return this->call(
"on_screen_message", j_obj);
275 struct json_object* j_obj = json_object_new_object();
276 struct json_object* val = json_object_new_string(reply_message);
277 json_object_object_add(j_obj,
"reply_message", val);
278 return this->call(
"on_screen_reply", j_obj);
295 if (et >= 1 && et <= 3) {
297 case Event_TapShortcut:
300 case Event_OnScreenMessage:
303 case Event_OnScreenReply:
308 this->handlers[et] = std::move(f);
335 HMI_ERROR(
"libhomescreen",
"verb doesn't exit");
340 HMI_ERROR(
"libhomescreen",
"Failed to call verb:%s",verb.c_str());
369 HMI_ERROR(
"libhomescreen",
"verb doesn't exit");
374 HMI_ERROR(
"libhomescreen",
"Failed to call verb:%s",verb);
398 struct json_object* j_obj = json_object_new_object();
399 json_object_object_add(j_obj,
"event", json_object_new_string(event_name.c_str()));
403 HMI_ERROR(
"libhomescreen",
"Failed to call verb:%s",__FUNCTION__);
427 struct json_object* j_obj = json_object_new_object();
428 json_object_object_add(j_obj,
"event", json_object_new_string(event_name.c_str()));
432 HMI_ERROR(
"libhomescreen",
"Failed to call verb:%s",__FUNCTION__);
441 HMI_DEBUG(
"libhomescreen",
"%s called", __FUNCTION__);
442 if(onHangup !=
nullptr)
460 HMI_DEBUG(
"libhomescreen",
"event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
462 if (strstr(event,
API) == NULL) {
466 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
467 struct json_object *json_data = json_object_object_get(ev_contents,
"data");
469 if(onEvent !=
nullptr)
471 const string ev(event);
472 onEvent(ev, ev_contents);
475 const char* event_only = strchr(event,
'/');
476 if (event_only !=
nullptr) {
477 event_only = event_only + 1;
481 auto i = this->handlers.find(Event_TapShortcut);
482 if ( i != this->handlers.end() ) {
483 i->second(json_data);
487 auto i = this->handlers.find(Event_OnScreenMessage);
488 if ( i != this->handlers.end() ) {
489 i->second(json_data);
493 auto i = this->handlers.find(Event_OnScreenReply);
494 if ( i != this->handlers.end() ) {
495 i->second(json_data);
499 json_object_put(ev_contents);
508 HMI_DEBUG(
"libhomescreen",
"msg: (%s)", afb_wsj1_msg_object_s(msg));
509 if(onReply !=
nullptr)
511 struct json_object* reply = afb_wsj1_msg_object_j(msg);
514 json_object_put(reply);
520 HMI_DEBUG(
"libhomescreen",
"verb is %s", verb.c_str());
void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
#define HMI_ERROR(prefix, args,...)
static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
void on_reply(void *closure, struct afb_wsj1_msg *msg)
int onScreenReply(const char *reply_message)
std::function< void(json_object *)> handler_func
#define HMI_DEBUG(prefix, args,...)
void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
int call(const std::string &verb, struct json_object *arg)
static void * event_loop_run(void *args)
void on_hangup(void *closure, struct afb_wsj1 *wsj)
int tapShortcut(const char *application_name)
int subscribe(const std::string &event_name)
static const std::vector< std::string > event_list
static void subscribe(struct afb_req request)
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)
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)
static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)