18 #include <sys/socket.h> 32 #define ELOG(args,...) _ELOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__) 33 #define DLOG(args,...) _DLOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__) 37 static void _DLOG(
const char* func,
const int line,
const char* log, ...);
38 static void _ELOG(
const char* func,
const int line,
const char* log, ...);
39 static bool has_verb(
const string& verb);
40 static const char API[] =
"homescreen";
44 std::string(
"tap_shortcut"),
45 std::string(
"on_screen_message"),
46 std::string(
"subscribe"),
47 std::string(
"unsubscribe")
51 std::string(
"tap_shortcut"),
52 std::string(
"on_screen_message"),
66 static void _on_call_static(
void *closure,
const char *api,
const char *verb,
struct afb_wsj1_msg *msg)
73 static_cast<LibHomeScreen*
>(closure)->on_event(NULL,event,msg);
97 sd_event_unref(mploop);
99 if(sp_websock != NULL)
122 if(port > 0 && token.size() > 0)
129 ELOG(
"port and token should be > 0, Initial port and token uses.");
132 ret = initialize_websocket();
135 ELOG(
"Failed to initialize websocket");
159 void (*event_cb)(
const std::string&
event,
struct json_object* event_contents),
160 void (*reply_cb)(
struct json_object* reply_contents),
161 void (*hangup_cb)(
void))
165 onHangup = hangup_cb;
168 int LibHomeScreen::initialize_websocket()
173 int ret = sd_event_default(&mploop);
176 ELOG(
"Failed to create event loop");
184 muri +=
"ws://localhost:" + to_string(mport) +
"/api?token=" + mtoken;
185 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface,
this);
186 if(sp_websock == NULL)
188 ELOG(
"Failed to create websocket connection");
199 sd_event_unref(mploop);
206 struct sd_event* loop = (
struct sd_event*)(args);
207 DLOG(
"start eventloop");
209 sd_event_run(loop, 30000000);
226 if(mploop && sp_websock)
229 int ret = pthread_create(&thread_id, NULL,
event_loop_run, mploop);
232 ELOG(
"Cannot run eventloop due to error:%d", errno);
240 ELOG(
"Connecting is not established yet");
263 struct json_object* j_obj = json_object_new_object();
264 struct json_object* val = json_object_new_string(application_name);
265 json_object_object_add(j_obj,
"application_name", val);
266 return this->call(
"tap_shortcut", j_obj);
287 struct json_object* j_obj = json_object_new_object();
288 struct json_object* val = json_object_new_string(display_message);
289 json_object_object_add(j_obj,
"display_message", val);
290 return this->call(
"on_screen_message", j_obj);
307 if (et >= 1 && et <= 2) {
309 case Event_TapShortcut:
312 case Event_OnScreenMessage:
317 this->handlers[et] = std::move(f);
344 ELOG(
"verb doesn't exit");
349 ELOG(
"Failed to call verb:%s",verb.c_str());
378 ELOG(
"verb doesn't exit");
383 ELOG(
"Failed to call verb:%s",verb);
407 struct json_object* j_obj = json_object_new_object();
408 json_object_object_add(j_obj,
"event", json_object_new_string(event_name.c_str()));
412 ELOG(
"Failed to call verb:%s",__FUNCTION__);
436 struct json_object* j_obj = json_object_new_object();
437 json_object_object_add(j_obj,
"event", json_object_new_string(event_name.c_str()));
441 ELOG(
"Failed to call verb:%s",__FUNCTION__);
450 DLOG(
"%s called", __FUNCTION__);
451 if(onHangup !=
nullptr)
469 cout <<
"[libhomescreen on_event]: " <<
event <<
" (" << afb_wsj1_msg_object_s(msg) <<
")" << endl;
471 if (strstr(event,
API) == NULL) {
475 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
476 struct json_object *json_data = json_object_object_get(ev_contents,
"data");
478 if(onEvent !=
nullptr)
480 const string ev(event);
481 onEvent(ev, ev_contents);
484 const char* event_only = strchr(event,
'/');
485 if (event_only !=
nullptr) {
486 event_only = event_only + 1;
490 auto i = this->handlers.find(Event_TapShortcut);
492 struct json_object *json_application_name = json_object_object_get(json_data,
"application_name");
493 const char* application_name = json_object_get_string(json_application_name);
495 if ( i != this->handlers.end() ) {
496 i->second(application_name);
501 auto i = this->handlers.find(Event_OnScreenMessage);
503 struct json_object *json_display_message = json_object_object_get(json_data,
"display_message");
504 const char* display_message = json_object_get_string(json_display_message);
506 if ( i != this->handlers.end() ) {
507 i->second(display_message);
512 json_object_put(ev_contents);
521 cout <<
"[libhomescreen on_reply]: " <<
" (" << afb_wsj1_msg_object_s(msg) <<
")" << endl;
522 if(onReply !=
nullptr)
524 struct json_object* reply = afb_wsj1_msg_object_j(msg);
527 json_object_put(reply);
533 static void _ELOG(
const char* func,
const int line,
const char* log, ...)
538 if (log == NULL || vasprintf(&message, log, args) < 0)
540 cout <<
"[libhomescreen ERROR]" << func <<
"(" << line <<
"):" << message << endl;
545 static void _DLOG(
const char* func,
const int line,
const char* log, ...)
550 if (log == NULL || vasprintf(&message, log, args) < 0)
552 cout <<
"[libhomescreen DEBUG]" << func <<
"(" << line <<
"):" << message << endl;
559 DLOG(
"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)
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)
static void _ELOG(const char *func, const int line, const char *log,...)
int subscribe(const std::string &event_name)
static const std::vector< std::string > event_list
static void subscribe(struct afb_req request)
std::function< void(const char *)> handler_func
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)
static void _DLOG(const char *func, const int line, const char *log,...)
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)