1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
/*
* Copyright (c) 2017 TOYOTA MOTOR CORPORATION
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdarg.h>
#include <sys/socket.h>
#include <iostream>
#include <algorithm>
#include <thread>
#include <errno.h>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <libhomescreen.hpp>
#define ELOG(args,...) _ELOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
#define DLOG(args,...) _DLOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
using namespace std;
static void _DLOG(const char* func, const int line, const char* log, ...);
static void _ELOG(const char* func, const int line, const char* log, ...);
static bool has_verb(const string& verb);
static const char API[] = "homescreen";
const std::vector<std::string> LibHomeScreen::api_list {
std::string("ping"), // debug do not use
std::string("tap_shortcut"), // HomeScreen Application only
std::string("on_screen_message"),
std::string("subscribe"),
std::string("unsubscribe")
};
const std::vector<std::string> LibHomeScreen::event_list {
std::string("tap_shortcut"),
std::string("on_screen_message"),
std::string("none")
};
/**
* websocket
*/
static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
{
static_cast<LibHomeScreen*>(closure)->on_hangup(NULL,wsj);
}
static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
/* LibHomeScreen is not called from other process */
}
static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
{
static_cast<LibHomeScreen*>(closure)->on_event(NULL,event,msg);
}
static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
{
static_cast<LibHomeScreen*>(closure)->on_reply(NULL,msg);
}
/**
* constructor
*/
LibHomeScreen::LibHomeScreen()
{
}
/**
* destructor
*/
LibHomeScreen::~LibHomeScreen()
{
if(mploop)
{
sd_event_unref(mploop);
}
if(sp_websock != NULL)
{
afb_wsj1_unref(sp_websock);
}
}
/**
* 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
*
*/
int LibHomeScreen::init(const int port, const string& token)
{
int ret = 0;
if(port > 0 && token.size() > 0)
{
mport = port;
mtoken = token;
}
else
{
ELOG("port and token should be > 0, Initial port and token uses.");
}
ret = initialize_websocket();
if(ret != 0 )
{
ELOG("Failed to initialize websocket");
}
else{
DLOG("Initialized");
}
this->runEventloop();
return ret;
}
/**
* 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
*/
void LibHomeScreen::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))
{
onEvent = event_cb;
onReply = reply_cb;
onHangup = hangup_cb;
}
int LibHomeScreen::initialize_websocket()
{
mploop = NULL;
onEvent = nullptr;
onReply = nullptr;
int ret = sd_event_default(&mploop);
if(ret < 0)
{
ELOG("Failed to create event loop");
goto END;
}
/* Initialize interface from websocket */
minterface.on_hangup = _on_hangup_static;
minterface.on_call = _on_call_static; /* Is this necessary? */
minterface.on_event = _on_event_static;
muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
if(sp_websock == NULL)
{
ELOG("Failed to create websocket connection");
goto END;
}
/* creates the evsrc */
//ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
return 0;
END:
if(mploop)
{
sd_event_unref(mploop);
}
return -1;
}
static void *event_loop_run(void *args)
{
struct sd_event* loop = (struct sd_event*)(args);
DLOG("start eventloop");
for(;;)
sd_event_run(loop, 30000000);
}
/**
* 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
*
*/
int LibHomeScreen::runEventloop()
{
if(mploop && sp_websock)
{
pthread_t thread_id;
int ret = pthread_create(&thread_id, NULL, event_loop_run, mploop);
if(ret != 0)
{
ELOG("Cannot run eventloop due to error:%d", errno);
return -1;
}
else
return thread_id;
}
else
{
ELOG("Connecting is not established yet");
return -1;
}
}
/**
* 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.
*/
int LibHomeScreen::tapShortcut(const char* application_name)
{
if(!sp_websock)
{
return -1;
}
struct json_object* j_obj = json_object_new_object();
struct json_object* val = json_object_new_string(application_name);
json_object_object_add(j_obj, "application_name", val);
return this->call("tap_shortcut", j_obj);
}
/**
* 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.
*/
int LibHomeScreen::onScreenMessage(const char* display_message)
{
if(!sp_websock)
{
return -1;
}
struct json_object* j_obj = json_object_new_object();
struct json_object* val = json_object_new_string(display_message);
json_object_object_add(j_obj, "display_message", val);
return this->call("on_screen_message", j_obj);
}
/**
* Setting Event Handler
*
* Setting event handler for Homescreen
*
* #### Parameters
* - et [in] : event name
* - f [in] : event handler
*
* #### Return
* Nothing
*/
void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
{
if (et >= 1 && et <= 2) {
switch (et) {
case Event_TapShortcut:
this->subscribe(LibHomeScreen::event_list[0]);
break;
case Event_OnScreenMessage:
this->subscribe(LibHomeScreen::event_list[1]);
break;
}
this->handlers[et] = std::move(f);
}
}
/**
* This function calls the API of HomeScreen via WebSocket
*
* #### 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.
*
*/
int LibHomeScreen::call(const string& verb, struct json_object* arg)
{
int ret;
if(!sp_websock)
{
return -1;
}
if (!has_verb(verb))
{
ELOG("verb doesn't exit");
return -1;
}
ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
if (ret < 0) {
ELOG("Failed to call verb:%s",verb.c_str());
}
return ret;
}
/**
* 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.
*
*/
int LibHomeScreen::call(const char* verb, struct json_object* arg)
{
int ret;
if(!sp_websock)
{
return -1;
}
if (!has_verb(string(verb)))
{
ELOG("verb doesn't exit");
return -1;
}
ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
if (ret < 0) {
ELOG("Failed to call verb:%s",verb);
}
return ret;
}
/**
* 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.
*
*/
int LibHomeScreen::subscribe(const string& event_name)
{
if(!sp_websock)
{
return -1;
}
struct json_object* j_obj = json_object_new_object();
json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
if (ret < 0) {
ELOG("Failed to call verb:%s",__FUNCTION__);
}
return ret;
}
/**
* 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.
*
*/
int LibHomeScreen::unsubscribe(const string& event_name)
{
if(!sp_websock)
{
return -1;
}
struct json_object* j_obj = json_object_new_object();
json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
if (ret < 0) {
ELOG("Failed to call verb:%s",__FUNCTION__);
}
return ret;
}
/************* Callback Function *************/
void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
{
DLOG("%s called", __FUNCTION__);
if(onHangup != nullptr)
{
onHangup();
}
}
void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
}
/*
* event is like "homescreen/tap_shortcut"
* msg is like {"event":"homescreen\/tap_shortcut","data":{"application_name":"hoge"},"jtype":"afb-event"}
* so you can get
event name : struct json_object obj = json_object_object_get(msg,"event")
*/
void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
{
cout << "[libhomescreen on_event]: " << event << " (" << afb_wsj1_msg_object_s(msg) << ")" << endl;
if (strstr(event, API) == NULL) {
return;
}
struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
struct json_object *json_data = json_object_object_get(ev_contents, "data");
if(onEvent != nullptr)
{
const string ev(event);
onEvent(ev, ev_contents);
}
const char* event_only = strchr(event, '/');
if (event_only != nullptr) {
event_only = event_only + 1;
}
if (strcasecmp(event_only, LibHomeScreen::event_list[0].c_str()) == 0) {
auto i = this->handlers.find(Event_TapShortcut);
struct json_object *json_application_name = json_object_object_get(json_data, "application_name");
const char* application_name = json_object_get_string(json_application_name);
if ( i != this->handlers.end() ) {
i->second(application_name);
}
}
else if (strcasecmp(event_only, LibHomeScreen::event_list[1].c_str()) == 0) {
auto i = this->handlers.find(Event_OnScreenMessage);
struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
const char* display_message = json_object_get_string(json_display_message);
if ( i != this->handlers.end() ) {
i->second(display_message);
}
}
json_object_put(ev_contents);
}
/**
* 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]"}})
*/
void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
{
cout << "[libhomescreen on_reply]: " << " (" << afb_wsj1_msg_object_s(msg) << ")" << endl;
if(onReply != nullptr)
{
struct json_object* reply = afb_wsj1_msg_object_j(msg);
onReply(reply);
json_object_put(reply);
}
}
/* Internal Function in libhomescreen */
static void _ELOG(const char* func, const int line, const char* log, ...)
{
char *message;
va_list args;
va_start(args, log);
if (log == NULL || vasprintf(&message, log, args) < 0)
message = NULL;
cout << "[libhomescreen ERROR]" << func << "(" << line << "):" << message << endl;
va_end(args);
free(message);
}
static void _DLOG(const char* func, const int line, const char* log, ...)
{
char *message;
va_list args;
va_start(args, log);
if (log == NULL || vasprintf(&message, log, args) < 0)
message = NULL;
cout << "[libhomescreen DEBUG]" << func << "(" << line << "):" << message << endl;
va_end(args);
free(message);
}
static bool has_verb(const string& verb)
{
DLOG("verb is %s", verb.c_str());
if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())
return true;
else
return false;
}
|