HomeScreenBinding
Public Types | Public Member Functions | Static Public Attributes | List of all members
LibHomeScreen Class Reference

#include <libhomescreen.hpp>

Public Types

enum  EventType { Event_TapShortcut = 1, Event_OnScreenMessage }
 
using handler_func = std::function< void(const char *)>
 

Public Member Functions

 LibHomeScreen ()
 
 ~LibHomeScreen ()
 
 LibHomeScreen (const LibHomeScreen &)=delete
 
LibHomeScreenoperator= (const LibHomeScreen &)=delete
 
int init (const int port, const std::string &token)
 
int runEventloop ()
 
int tapShortcut (const char *application_name)
 
int onScreenMessage (const char *display_message)
 
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 call (const std::string &verb, struct json_object *arg)
 
int call (const char *verb, struct json_object *arg)
 
int subscribe (const std::string &event_name)
 
int unsubscribe (const std::string &event_name)
 
void on_hangup (void *closure, struct afb_wsj1 *wsj)
 
void on_call (void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
 
void on_event (void *closure, const char *event, struct afb_wsj1_msg *msg)
 
void on_reply (void *closure, struct afb_wsj1_msg *msg)
 

Static Public Attributes

static const std::vector< std::string > api_list
 
static const std::vector< std::string > event_list
 

Detailed Description

Definition at line 32 of file libhomescreen.hpp.

Member Typedef Documentation

◆ handler_func

using LibHomeScreen::handler_func = std::function<void(const char*)>

Definition at line 41 of file libhomescreen.hpp.

Member Enumeration Documentation

◆ EventType

Enumerator
Event_TapShortcut 
Event_OnScreenMessage 

Definition at line 43 of file libhomescreen.hpp.

Constructor & Destructor Documentation

◆ LibHomeScreen() [1/2]

LibHomeScreen::LibHomeScreen ( )

constructor

Definition at line 85 of file libhomescreen.cpp.

86 {
87 
88 }

◆ ~LibHomeScreen()

LibHomeScreen::~LibHomeScreen ( )

destructor

Definition at line 93 of file libhomescreen.cpp.

94 {
95  if(mploop)
96  {
97  sd_event_unref(mploop);
98  }
99  if(sp_websock != NULL)
100  {
101  free(sp_websock);
102  }
103 }

◆ LibHomeScreen() [2/2]

LibHomeScreen::LibHomeScreen ( const LibHomeScreen )
delete

Member Function Documentation

◆ call() [1/2]

int LibHomeScreen::call ( const std::string &  verb,
struct json_object *  arg 
)

◆ call() [2/2]

int LibHomeScreen::call ( const char *  verb,
struct json_object *  arg 
)

This function calls the API of HomeScreen via WebSocket This function is overload function of "call"

Parameters

  • verb [in] : This argument should be specified to the API name (e.g. "tap_shortcut")
  • arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object

Return

  • Returns 0 on success or -1 in case of error.

Note

To call HomeScreen's APIs, the application should set its function name, arguments to JSON format.

Definition at line 369 of file libhomescreen.cpp.

370 {
371  int ret;
372  if(!sp_websock)
373  {
374  return -1;
375  }
376  if (!has_verb(string(verb)))
377  {
378  ELOG("verb doesn't exit");
379  return -1;
380  }
381  ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
382  if (ret < 0) {
383  ELOG("Failed to call verb:%s",verb);
384  }
385  return ret;
386 }
static const char API[]
#define ELOG(args,...)
static bool has_verb(const string &verb)
static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)

◆ init()

int LibHomeScreen::init ( const int  port,
const std::string &  token 
)

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

Definition at line 119 of file libhomescreen.cpp.

120 {
121  int ret = 0;
122  if(port > 0 && token.size() > 0)
123  {
124  mport = port;
125  mtoken = token;
126  }
127  else
128  {
129  ELOG("port and token should be > 0, Initial port and token uses.");
130  }
131 
132  ret = initialize_websocket();
133  if(ret != 0 )
134  {
135  ELOG("Failed to initialize websocket");
136  }
137  else{
138  DLOG("Initialized");
139  }
140 
141  return ret;
142 }
#define DLOG(args,...)
#define ELOG(args,...)

◆ on_call()

void LibHomeScreen::on_call ( void *  closure,
const char *  api,
const char *  verb,
struct afb_wsj1_msg *  msg 
)

Definition at line 457 of file libhomescreen.cpp.

458 {
459 }

◆ on_event()

void LibHomeScreen::on_event ( void *  closure,
const char *  event,
struct afb_wsj1_msg *  msg 
)

Definition at line 467 of file libhomescreen.cpp.

468 {
469  cout << "[libhomescreen on_event]: " << event << " (" << afb_wsj1_msg_object_s(msg) << ")" << endl;
470 
471  if (strstr(event, API) == NULL) {
472  return;
473  }
474 
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");
477 
478  if(onEvent != nullptr)
479  {
480  const string ev(event);
481  onEvent(ev, ev_contents);
482  }
483 
484  const char* event_only = strchr(event, '/');
485  if (event_only != nullptr) {
486  event_only = event_only + 1;
487  }
488 
489  if (strcasecmp(event_only, LibHomeScreen::event_list[0].c_str()) == 0) {
490  auto i = this->handlers.find(Event_TapShortcut);
491 
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);
494 
495  if ( i != this->handlers.end() ) {
496  i->second(application_name);
497  }
498  }
499  else if (strcasecmp(event_only, LibHomeScreen::event_list[1].c_str()) == 0) {
500 
501  auto i = this->handlers.find(Event_OnScreenMessage);
502 
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);
505 
506  if ( i != this->handlers.end() ) {
507  i->second(display_message);
508  }
509 
510  }
511 
512  json_object_put(ev_contents);
513 }
static const char API[]
static const std::vector< std::string > event_list

◆ on_hangup()

void LibHomeScreen::on_hangup ( void *  closure,
struct afb_wsj1 *  wsj 
)

Definition at line 448 of file libhomescreen.cpp.

449 {
450  DLOG("%s called", __FUNCTION__);
451  if(onHangup != nullptr)
452  {
453  onHangup();
454  }
455 }
#define DLOG(args,...)

◆ on_reply()

void LibHomeScreen::on_reply ( void *  closure,
struct afb_wsj1_msg *  msg 
)

msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}}) msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})

Definition at line 519 of file libhomescreen.cpp.

520 {
521  cout << "[libhomescreen on_reply]: " << " (" << afb_wsj1_msg_object_s(msg) << ")" << endl;
522  if(onReply != nullptr)
523  {
524  struct json_object* reply = afb_wsj1_msg_object_j(msg);
525  onReply(reply);
526 
527  json_object_put(reply);
528  }
529 }

◆ onScreenMessage()

int LibHomeScreen::onScreenMessage ( const char *  display_message)

HomeScreenアプリに表示するメッセージイベントの発行

各アプリからHomeScreenアプリケーションのOnScreenに表示するイベントを発行する

Parameters

  • display_message [in] : 表示するメッセージ

Return

  • Returns 0 on success or -1 in case of error.

Definition at line 280 of file libhomescreen.cpp.

281 {
282  if(!sp_websock)
283  {
284  return -1;
285  }
286 
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);
291 }
int call(const std::string &verb, struct json_object *arg)

◆ operator=()

LibHomeScreen& LibHomeScreen::operator= ( const LibHomeScreen )
delete

◆ registerCallback()

void LibHomeScreen::registerCallback ( void(*)(const std::string &event, struct json_object *event_contents)  event_cb,
void(*)(struct json_object *reply_contents)  reply_cb,
void(*)(void)  hangup_cb = nullptr 
)

This function register callback function for reply/event message from home screen

Parameters

  • event_cb [in] : This argument should be specified to the callback for subscribed event
  • reply_cb [in] : This argument should be specified to the reply callback for call function

Return

Nothing

Note

Event callback is invoked by home screen for event you subscribed. If you would like to get event, please call subscribe function before/after this function

Definition at line 158 of file libhomescreen.cpp.

162 {
163  onEvent = event_cb;
164  onReply = reply_cb;
165  onHangup = hangup_cb;
166 }

◆ runEventloop()

int LibHomeScreen::runEventloop ( )

This function start receiving the reply/event message from home screen

Parameters

Nothing

Return

  • Returns thread_id on success or -1 in case of error.

Note

Definition at line 224 of file libhomescreen.cpp.

225 {
226  if(mploop && sp_websock)
227  {
228  pthread_t thread_id;
229  int ret = pthread_create(&thread_id, NULL, event_loop_run, mploop);
230  if(ret != 0)
231  {
232  ELOG("Cannot run eventloop due to error:%d", errno);
233  return -1;
234  }
235  else
236  return thread_id;
237  }
238  else
239  {
240  ELOG("Connecting is not established yet");
241  return -1;
242  }
243 }
#define ELOG(args,...)
static void * event_loop_run(void *args)

◆ set_event_handler()

void LibHomeScreen::set_event_handler ( enum EventType  et,
handler_func  f 
)

イベントハンドラの登録

各アプリからHomeScreenアプリケーションのOnScreenに表示するイベントを発行する

Parameters

  • et [in] : 対象のイベント
  • f [in] : イベントハンドラ

Return

Nothing

Definition at line 305 of file libhomescreen.cpp.

306 {
307  if (et >= 1 && et <= 2) {
308  switch (et) {
309  case Event_TapShortcut:
311  break;
314  break;
315  }
316 
317  this->handlers[et] = std::move(f);
318  }
319 }
int subscribe(const std::string &event_name)
static const std::vector< std::string > event_list

◆ subscribe()

int LibHomeScreen::subscribe ( const std::string &  event_name)

Register callback function for each event

Parameters

  • event_name [in] : This argument should be specified to the event name

Return

  • Returns 0 on success or -1 in case of error.

Note

This function enables to get an event to your callback function.

Definition at line 401 of file libhomescreen.cpp.

402 {
403  if(!sp_websock)
404  {
405  return -1;
406  }
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()));
409 
410  int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
411  if (ret < 0) {
412  ELOG("Failed to call verb:%s",__FUNCTION__);
413  }
414  return ret;
415 }
static const char API[]
#define ELOG(args,...)
static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)

◆ tapShortcut()

int LibHomeScreen::tapShortcut ( const char *  application_name)

ショートカットアイコンがタップされたイベントの発行

HomeScreenアプリケーションにて各アプリアイコンがタップされたときにイベントを発行する

Parameters

  • application_name [in] : タップされたアプリケーションの名前(label)

Return

  • Returns 0 on success or -1 in case of error.

Definition at line 256 of file libhomescreen.cpp.

257 {
258  if(!sp_websock)
259  {
260  return -1;
261  }
262 
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);
267 }
int call(const std::string &verb, struct json_object *arg)

◆ unsubscribe()

int LibHomeScreen::unsubscribe ( const std::string &  event_name)

Unregister callback function for each event

Parameters

  • event_name [in] : This argument should be specified to the event name

Return

  • Returns 0 on success or -1 in case of error.

Note

This function disables to get an event to your callback function.

Definition at line 430 of file libhomescreen.cpp.

431 {
432  if(!sp_websock)
433  {
434  return -1;
435  }
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()));
438 
439  int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
440  if (ret < 0) {
441  ELOG("Failed to call verb:%s",__FUNCTION__);
442  }
443  return ret;
444 }
static const char API[]
#define ELOG(args,...)
static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)

Member Data Documentation

◆ api_list

const std::vector< std::string > LibHomeScreen::api_list
static
Initial value:
{
std::string("ping"),
std::string("tap_shortcut"),
std::string("on_screen_message"),
std::string("subscribe"),
std::string("unsubscribe")
}

Definition at line 48 of file libhomescreen.hpp.

◆ event_list

const std::vector< std::string > LibHomeScreen::event_list
static
Initial value:
{
std::string("tap_shortcut"),
std::string("on_screen_message"),
std::string("none")
}

Definition at line 49 of file libhomescreen.hpp.


The documentation for this class was generated from the following files: