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, Event_OnScreenReply }
 
using handler_func = std::function< void(json_object *)>
 

Public Member Functions

 LibHomeScreen ()
 
 ~LibHomeScreen ()
 
 LibHomeScreen (const LibHomeScreen &)=delete
 
LibHomeScreenoperator= (const LibHomeScreen &)=delete
 
int init (const int port, const std::string &token)
 
int tapShortcut (const char *application_name)
 
int onScreenMessage (const char *display_message)
 
int onScreenReply (const char *reply_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(json_object*)>

Definition at line 42 of file libhomescreen.hpp.

Member Enumeration Documentation

◆ EventType

Enumerator
Event_TapShortcut 
Event_OnScreenMessage 
Event_OnScreenReply 

Definition at line 44 of file libhomescreen.hpp.

Constructor & Destructor Documentation

◆ LibHomeScreen() [1/2]

LibHomeScreen::LibHomeScreen ( )

constructor

Definition at line 83 of file libhomescreen.cpp.

84 {
85 }

◆ ~LibHomeScreen()

LibHomeScreen::~LibHomeScreen ( )

destructor

Definition at line 90 of file libhomescreen.cpp.

91 {
92  if(mploop)
93  {
94  sd_event_unref(mploop);
95  }
96  if(sp_websock != NULL)
97  {
98  afb_wsj1_unref(sp_websock);
99  }
100 }

◆ 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 360 of file libhomescreen.cpp.

361 {
362  int ret;
363  if(!sp_websock)
364  {
365  return -1;
366  }
367  if (!has_verb(string(verb)))
368  {
369  HMI_ERROR("libhomescreen","verb doesn't exit");
370  return -1;
371  }
372  ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
373  if (ret < 0) {
374  HMI_ERROR("libhomescreen","Failed to call verb:%s",verb);
375  }
376  return ret;
377 }
#define HMI_ERROR(prefix, args,...)
Definition: hmi-debug.h:18
static const char API[]
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 116 of file libhomescreen.cpp.

117 {
118  int ret = 0;
119  if(port > 0 && token.size() > 0)
120  {
121  mport = port;
122  mtoken = token;
123  }
124  else
125  {
126  HMI_ERROR("libhomescreen","port and token should be > 0, Initial port and token uses.");
127  }
128 
129  ret = initialize_websocket();
130  if(ret != 0 )
131  {
132  HMI_ERROR("libhomescreen","Failed to initialize websocket");
133  }
134  else{
135  HMI_DEBUG("libhomescreen","Initialized");
136  }
137 
138  return ret;
139 }
#define HMI_ERROR(prefix, args,...)
Definition: hmi-debug.h:18
#define HMI_DEBUG(prefix, args,...)
Definition: hmi-debug.h:22

◆ on_call()

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

Definition at line 448 of file libhomescreen.cpp.

449 {
450 }

◆ on_event()

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

Definition at line 458 of file libhomescreen.cpp.

459 {
460  HMI_DEBUG("libhomescreen","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
461 
462  if (strstr(event, API) == NULL) {
463  return;
464  }
465 
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");
468 
469  if(onEvent != nullptr)
470  {
471  const string ev(event);
472  onEvent(ev, ev_contents);
473  }
474 
475  const char* event_only = strchr(event, '/');
476  if (event_only != nullptr) {
477  event_only = event_only + 1;
478  }
479 
480  if (strcasecmp(event_only, LibHomeScreen::event_list[0].c_str()) == 0) {
481  auto i = this->handlers.find(Event_TapShortcut);
482  if ( i != this->handlers.end() ) {
483  i->second(json_data);
484  }
485  }
486  else if (strcasecmp(event_only, LibHomeScreen::event_list[1].c_str()) == 0) {
487  auto i = this->handlers.find(Event_OnScreenMessage);
488  if ( i != this->handlers.end() ) {
489  i->second(json_data);
490  }
491  }
492  else if (strcasecmp(event_only, LibHomeScreen::event_list[2].c_str()) == 0) {
493  auto i = this->handlers.find(Event_OnScreenReply);
494  if ( i != this->handlers.end() ) {
495  i->second(json_data);
496  }
497  }
498 
499  json_object_put(ev_contents);
500 }
#define HMI_DEBUG(prefix, args,...)
Definition: hmi-debug.h:22
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 439 of file libhomescreen.cpp.

440 {
441  HMI_DEBUG("libhomescreen","%s called", __FUNCTION__);
442  if(onHangup != nullptr)
443  {
444  onHangup();
445  }
446 }
#define HMI_DEBUG(prefix, args,...)
Definition: hmi-debug.h:22

◆ 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 506 of file libhomescreen.cpp.

507 {
508  HMI_DEBUG("libhomescreen","msg: (%s)", afb_wsj1_msg_object_s(msg));
509  if(onReply != nullptr)
510  {
511  struct json_object* reply = afb_wsj1_msg_object_j(msg);
512  onReply(reply);
513 
514  json_object_put(reply);
515  }
516 }
#define HMI_DEBUG(prefix, args,...)
Definition: hmi-debug.h:22

◆ onScreenMessage()

int LibHomeScreen::onScreenMessage ( const char *  display_message)

Sending onScreen message event

Sending OnScreen message event to HomeScreen from applications

Parameters

  • display_message [in] : message for display

Return

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

Definition at line 244 of file libhomescreen.cpp.

245 {
246  if(!sp_websock)
247  {
248  return -1;
249  }
250 
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);
255 }
int call(const std::string &verb, struct json_object *arg)

◆ onScreenReply()

int LibHomeScreen::onScreenReply ( const char *  reply_message)

Sending onScreen reply event

Sending OnScreen reply event to applications from HomeScreen

Parameters

  • reply_message [in] : message for reply

Return

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

Definition at line 268 of file libhomescreen.cpp.

269 {
270  if(!sp_websock)
271  {
272  return -1;
273  }
274 
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);
279 }
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 155 of file libhomescreen.cpp.

159 {
160  onEvent = event_cb;
161  onReply = reply_cb;
162  onHangup = hangup_cb;
163 }

◆ set_event_handler()

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

Setting Event Handler

Setting event handler for Homescreen

Parameters

  • et [in] : event name
  • f [in] : event handler

Return

Nothing

Definition at line 293 of file libhomescreen.cpp.

294 {
295  if (et >= 1 && et <= 3) {
296  switch (et) {
297  case Event_TapShortcut:
299  break;
302  break;
303  case Event_OnScreenReply:
305  break;
306  }
307 
308  this->handlers[et] = std::move(f);
309  }
310 }
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 392 of file libhomescreen.cpp.

393 {
394  if(!sp_websock)
395  {
396  return -1;
397  }
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()));
400 
401  int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
402  if (ret < 0) {
403  HMI_ERROR("libhomescreen","Failed to call verb:%s",__FUNCTION__);
404  }
405  return ret;
406 }
#define HMI_ERROR(prefix, args,...)
Definition: hmi-debug.h:18
static const char API[]
static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)

◆ tapShortcut()

int LibHomeScreen::tapShortcut ( const char *  application_name)

Sending ShortCut Icon tapped event

When HomeScreen shortcut area is tapped, sending a event

Parameters

  • application_name [in] : Tapped application name (label)

Return

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

Definition at line 220 of file libhomescreen.cpp.

221 {
222  if(!sp_websock)
223  {
224  return -1;
225  }
226 
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);
231 }
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 421 of file libhomescreen.cpp.

422 {
423  if(!sp_websock)
424  {
425  return -1;
426  }
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()));
429 
430  int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
431  if (ret < 0) {
432  HMI_ERROR("libhomescreen","Failed to call verb:%s",__FUNCTION__);
433  }
434  return ret;
435 }
#define HMI_ERROR(prefix, args,...)
Definition: hmi-debug.h:18
static const char API[]
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("on_screen_reply"),
std::string("subscribe"),
std::string("unsubscribe")
}

Definition at line 50 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("on_screen_reply"),
std::string("none")
}

Definition at line 51 of file libhomescreen.hpp.


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