From bb38a76d1b00e93187967aa2880e79e671f02eba Mon Sep 17 00:00:00 2001 From: zheng_wenlong Date: Mon, 3 Sep 2018 10:39:15 +0900 Subject: Update lastest code from agl-service-homescreen-2017 Update the lastest code from agl-service-homescreen-2017. To impove homescreen-service's extensibility. "Change homescreen-service from c project to c++ project" Relative Commit: https://gerrit.automotivelinux.org/gerrit/#/c/16351/ Change-Id: Id7e50fa61fdfcff30f69588aa1da4d93695edfc1 Signed-off-by: zheng_wenlong Signed-off-by: wang_zhiqiang --- src/CMakeLists.txt | 4 +- src/hmi-debug.h | 7 ++ src/homescreen.c | 314 ----------------------------------------------------- src/homescreen.cpp | 306 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/hs-helper.c | 156 -------------------------- src/hs-helper.cpp | 162 +++++++++++++++++++++++++++ src/hs-helper.h | 12 +- 7 files changed, 480 insertions(+), 481 deletions(-) delete mode 100644 src/homescreen.c create mode 100644 src/homescreen.cpp delete mode 100644 src/hs-helper.c create mode 100644 src/hs-helper.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 363cdae..dbd7fbe 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -23,8 +23,8 @@ FIND_PACKAGE(Threads) pkg_check_modules(hs_binding_depends afb-daemon glib-2.0 gio-2.0 gio-unix-2.0 json-c) set(binding_hs_sources - homescreen.c - hs-helper.c) + homescreen.cpp + hs-helper.cpp) link_libraries(-Wl,--as-needed -Wl,--gc-sections -Wl,--no-undefined) include_directories(${PROJECT_SOURCE_DIR}/include) diff --git a/src/hmi-debug.h b/src/hmi-debug.h index 2a744ba..3c71ff5 100644 --- a/src/hmi-debug.h +++ b/src/hmi-debug.h @@ -17,6 +17,10 @@ #ifndef __HMI_DEBUG_H__ #define __HMI_DEBUG_H__ +#ifdef __cplusplus +extern "C" { +#endif + #include #include #include @@ -67,4 +71,7 @@ static void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, c free(message); } +#ifdef __cplusplus +} +#endif #endif //__HMI_DEBUG_H__ \ No newline at end of file diff --git a/src/homescreen.c b/src/homescreen.c deleted file mode 100644 index f1a8fcd..0000000 --- a/src/homescreen.c +++ /dev/null @@ -1,314 +0,0 @@ -/* - * 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. - */ - -#define _GNU_SOURCE -#define AFB_BINDING_VERSION 2 -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include "hs-helper.h" -#include "hmi-debug.h" - -#define COMMAND_EVENT_NUM 4 -#define EVENT_SUBSCRIBE_ERROR_CODE 100 - -/* To Do hash table is better */ -struct event{ - const char* name; - struct afb_event* event; - }; - -static struct event event_list[COMMAND_EVENT_NUM]; - -static struct afb_event ev_tap_shortcut; -static struct afb_event ev_on_screen_message; -static struct afb_event ev_on_screen_reply; -static struct afb_event ev_reserved; - -static const char _error[] = "error"; - -static const char _application_name[] = "application_name"; -static const char _display_message[] = "display_message"; -static const char _reply_message[] = "reply_message"; - -/* -********** Method of HomeScreen Service (API) ********** -*/ - -static void pingSample(struct afb_req request) -{ - static int pingcount = 0; - afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount); - HMI_NOTICE("homescreen-service","Verbosity macro at level notice invoked at ping invocation count = %d", pingcount); - pingcount++; -} - -/** - * tap_shortcut notify for homescreen - * When Shortcut area is tapped, notify these applciations - * - * #### Parameters - * Request key - * - application_name : application name - * - * #### Return - * Nothing - * - */ -static void tap_shortcut (struct afb_req request) -{ - HMI_NOTICE("homescreen-service","called."); - - int ret = 0; - const char* value = afb_req_value(request, _application_name); - if (value) { - - HMI_NOTICE("homescreen-service","request params = %s.", value); - - struct json_object* push_obj = json_object_new_object(); - hs_add_object_to_json_object_str( push_obj, 2, - _application_name, value); - afb_event_push(ev_tap_shortcut, push_obj); - } else { - afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__); - return; - } - - // response to HomeScreen - struct json_object *res = json_object_new_object(); - hs_add_object_to_json_object_func(res, __FUNCTION__, 2, - _error, ret); - afb_req_success(request, res, "afb_event_push event [tap_shortcut]"); -} - -/** - * HomeScreen OnScreen message - * - * #### Parameters - * Request key - * - display_message : message for display - * - * #### Return - * Nothing - * - */ -static void on_screen_message (struct afb_req request) -{ - HMI_NOTICE("homescreen-service","called."); - - int ret = 0; - const char* value = afb_req_value(request, _display_message); - if (value) { - - HMI_NOTICE("homescreen-service","request params = %s.", value); - - struct json_object* push_obj = json_object_new_object(); - hs_add_object_to_json_object_str( push_obj, 2, - _display_message, value); - afb_event_push(ev_on_screen_message, push_obj); - } else { - afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__); - return; - } - - // response to HomeScreen - struct json_object *res = json_object_new_object(); - hs_add_object_to_json_object_func(res, __FUNCTION__, 2, - _error, ret); - afb_req_success(request, res, "afb_event_push event [on_screen_message]"); -} - -/** - * HomeScreen OnScreen Reply - * - * #### Parameters - * Request key - * - reply_message : message for reply - * - * #### Return - * Nothing - * - */ -static void on_screen_reply (struct afb_req request) -{ - HMI_NOTICE("homescreen-service","called."); - - int ret = 0; - const char* value = afb_req_value(request, _reply_message); - if (value) { - - HMI_NOTICE("homescreen-service","request params = %s.", value); - - struct json_object* push_obj = json_object_new_object(); - hs_add_object_to_json_object_str( push_obj, 2, - _reply_message, value); - afb_event_push(ev_on_screen_reply, push_obj); - } else { - afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__); - return; - } - - // response to HomeScreen - struct json_object *res = json_object_new_object(); - hs_add_object_to_json_object_func(res, __FUNCTION__, 2, - _error, ret); - afb_req_success(request, res, "afb_event_push event [on_screen_reply]"); -} - -/** - * Subscribe event - * - * #### Parameters - * - event : Event name. Event list is written in libhomescreen.cpp - * - * #### Return - * Nothing - * - * #### Note - * - */ -static void subscribe(struct afb_req request) -{ - const char *value = afb_req_value(request, "event"); - HMI_NOTICE("homescreen-service","value is %s", value); - int ret = 0; - if(value) { - int index = hs_search_event_name_index(value); - if(index < 0) - { - HMI_NOTICE("homescreen-service","dedicated event doesn't exist"); - ret = EVENT_SUBSCRIBE_ERROR_CODE; - } - else - { - afb_req_subscribe(request, *event_list[index].event); - } - } - else{ - HMI_NOTICE("homescreen-service","Please input event name"); - ret = EVENT_SUBSCRIBE_ERROR_CODE; - } - /*create response json object*/ - struct json_object *res = json_object_new_object(); - hs_add_object_to_json_object_func(res, __FUNCTION__, 2, - _error, ret); - afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value); -} - -/** - * Unsubscribe event - * - * #### Parameters - * - event : Event name. Event list is written in libhomescreen.cpp - * - * #### Return - * Nothing - * - * #### Note - * - */ -static void unsubscribe(struct afb_req request) -{ - const char *value = afb_req_value(request, "event"); - HMI_NOTICE("homescreen-service","value is %s", value); - int ret = 0; - if(value) { - int index = hs_search_event_name_index(value); - if(index < 0) - { - HMI_NOTICE("homescreen-service","dedicated event doesn't exist"); - ret = EVENT_SUBSCRIBE_ERROR_CODE; - } - else - { - afb_req_unsubscribe(request, *event_list[index].event); - } - } - else{ - HMI_NOTICE("homescreen-service","Please input event name"); - ret = EVENT_SUBSCRIBE_ERROR_CODE; - } - /*create response json object*/ - struct json_object *res = json_object_new_object(); - hs_add_object_to_json_object_func(res, __FUNCTION__, 2, - _error, ret); - afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value); -} - -/* - * array of the verbs exported to afb-daemon - */ -static const struct afb_verb_v2 verbs[]= { - /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL */ - { .verb = "ping", .session = AFB_SESSION_NONE, .callback = pingSample, .auth = NULL }, - { .verb = "tap_shortcut", .session = AFB_SESSION_NONE, .callback = tap_shortcut, .auth = NULL }, - { .verb = "on_screen_message", .session = AFB_SESSION_NONE, .callback = on_screen_message, .auth = NULL }, - { .verb = "on_screen_reply", .session = AFB_SESSION_NONE, .callback = on_screen_reply, .auth = NULL }, - { .verb = "subscribe", .session = AFB_SESSION_NONE, .callback = subscribe, .auth = NULL }, - { .verb = "unsubscribe", .session = AFB_SESSION_NONE, .callback = unsubscribe, .auth = NULL }, - {NULL } /* marker for end of the array */ -}; - -static int preinit() -{ - HMI_NOTICE("homescreen-service","binding preinit (was register)"); - return 0; -} - -static int init() -{ - HMI_NOTICE("homescreen-service","binding init"); - - ev_tap_shortcut = afb_daemon_make_event(evlist[0]); - ev_on_screen_message = afb_daemon_make_event(evlist[1]); - ev_on_screen_reply = afb_daemon_make_event(evlist[2]); - ev_reserved = afb_daemon_make_event(evlist[3]); - - event_list[0].name = evlist[0]; - event_list[0].event = &ev_tap_shortcut; - - event_list[1].name = evlist[1]; - event_list[1].event = &ev_on_screen_message; - - event_list[2].name = evlist[2]; - event_list[2].event = &ev_on_screen_reply; - - event_list[3].name = evlist[3]; - event_list[3].event = &ev_reserved; - - return 0; -} - -static void onevent(const char *event, struct json_object *object) -{ - HMI_NOTICE("homescreen-service","on_event %s", event); -} - -const struct afb_binding_v2 afbBindingV2 = { - .api = "homescreen", - .specification = NULL, - .verbs = verbs, - .preinit = preinit, - .init = init, - .onevent = onevent -}; diff --git a/src/homescreen.cpp b/src/homescreen.cpp new file mode 100644 index 0000000..85ea99f --- /dev/null +++ b/src/homescreen.cpp @@ -0,0 +1,306 @@ +/* + * 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. + */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include "hs-helper.h" +#include "hmi-debug.h" + +#define COMMAND_EVENT_NUM 4 +#define EVENT_SUBSCRIBE_ERROR_CODE 100 + +/* To Do hash table is better */ +struct event{ + const char* name; + struct afb_event* event; + }; + +static struct event event_list[COMMAND_EVENT_NUM]; + +static struct afb_event ev_tap_shortcut; +static struct afb_event ev_on_screen_message; +static struct afb_event ev_on_screen_reply; +static struct afb_event ev_reserved; + +static const char _error[] = "error"; + +static const char _application_name[] = "application_name"; +static const char _display_message[] = "display_message"; +static const char _reply_message[] = "reply_message"; + +/* +********** Method of HomeScreen Service (API) ********** +*/ + +static void pingSample(struct afb_req request) +{ + static int pingcount = 0; + afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount); + HMI_NOTICE("homescreen-service","Verbosity macro at level notice invoked at ping invocation count = %d", pingcount); + pingcount++; +} + +/** + * tap_shortcut notify for homescreen + * When Shortcut area is tapped, notify these applciations + * + * #### Parameters + * Request key + * - application_name : application name + * + * #### Return + * Nothing + * + */ +static void tap_shortcut (struct afb_req request) +{ + HMI_NOTICE("homescreen-service","called."); + + int ret = 0; + const char* value = afb_req_value(request, _application_name); + if (value) { + + HMI_NOTICE("homescreen-service","request params = %s.", value); + + struct json_object* push_obj = json_object_new_object(); + hs_add_object_to_json_object_str( push_obj, 2, + _application_name, value); + afb_event_push(ev_tap_shortcut, push_obj); + } else { + afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__); + return; + } + + // response to HomeScreen + struct json_object *res = json_object_new_object(); + hs_add_object_to_json_object_func(res, __FUNCTION__, 2, + _error, ret); + afb_req_success(request, res, "afb_event_push event [tap_shortcut]"); +} + +/** + * HomeScreen OnScreen message + * + * #### Parameters + * Request key + * - display_message : message for display + * + * #### Return + * Nothing + * + */ +static void on_screen_message (struct afb_req request) +{ + HMI_NOTICE("homescreen-service","called."); + + int ret = 0; + const char* value = afb_req_value(request, _display_message); + if (value) { + + HMI_NOTICE("homescreen-service","request params = %s.", value); + + struct json_object* push_obj = json_object_new_object(); + hs_add_object_to_json_object_str( push_obj, 2, + _display_message, value); + afb_event_push(ev_on_screen_message, push_obj); + } else { + afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__); + return; + } + + // response to HomeScreen + struct json_object *res = json_object_new_object(); + hs_add_object_to_json_object_func(res, __FUNCTION__, 2, + _error, ret); + afb_req_success(request, res, "afb_event_push event [on_screen_message]"); +} + +/** + * HomeScreen OnScreen Reply + * + * #### Parameters + * Request key + * - reply_message : message for reply + * + * #### Return + * Nothing + * + */ +static void on_screen_reply (struct afb_req request) +{ + HMI_NOTICE("homescreen-service","called."); + + int ret = 0; + const char* value = afb_req_value(request, _reply_message); + if (value) { + + HMI_NOTICE("homescreen-service","request params = %s.", value); + + struct json_object* push_obj = json_object_new_object(); + hs_add_object_to_json_object_str( push_obj, 2, + _reply_message, value); + afb_event_push(ev_on_screen_reply, push_obj); + } else { + afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__); + return; + } + + // response to HomeScreen + struct json_object *res = json_object_new_object(); + hs_add_object_to_json_object_func(res, __FUNCTION__, 2, + _error, ret); + afb_req_success(request, res, "afb_event_push event [on_screen_reply]"); +} + +/** + * Subscribe event + * + * #### Parameters + * - event : Event name. Event list is written in libhomescreen.cpp + * + * #### Return + * Nothing + * + * #### Note + * + */ +static void subscribe(struct afb_req request) +{ + const char *value = afb_req_value(request, "event"); + HMI_NOTICE("homescreen-service","value is %s", value); + int ret = 0; + if(value) { + int index = hs_search_event_name_index(value); + if(index < 0) + { + HMI_NOTICE("homescreen-service","dedicated event doesn't exist"); + ret = EVENT_SUBSCRIBE_ERROR_CODE; + } + else + { + afb_req_subscribe(request, *event_list[index].event); + } + } + else{ + HMI_NOTICE("homescreen-service","Please input event name"); + ret = EVENT_SUBSCRIBE_ERROR_CODE; + } + /*create response json object*/ + struct json_object *res = json_object_new_object(); + hs_add_object_to_json_object_func(res, __FUNCTION__, 2, + _error, ret); + afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value); +} + +/** + * Unsubscribe event + * + * #### Parameters + * - event : Event name. Event list is written in libhomescreen.cpp + * + * #### Return + * Nothing + * + * #### Note + * + */ +static void unsubscribe(struct afb_req request) +{ + const char *value = afb_req_value(request, "event"); + HMI_NOTICE("homescreen-service","value is %s", value); + int ret = 0; + if(value) { + int index = hs_search_event_name_index(value); + if(index < 0) + { + HMI_NOTICE("homescreen-service","dedicated event doesn't exist"); + ret = EVENT_SUBSCRIBE_ERROR_CODE; + } + else + { + afb_req_unsubscribe(request, *event_list[index].event); + } + } + else{ + HMI_NOTICE("homescreen-service","Please input event name"); + ret = EVENT_SUBSCRIBE_ERROR_CODE; + } + /*create response json object*/ + struct json_object *res = json_object_new_object(); + hs_add_object_to_json_object_func(res, __FUNCTION__, 2, + _error, ret); + afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value); +} + +/* + * array of the verbs exported to afb-daemon + */ +static const struct afb_verb_v2 verbs[]= { + /* VERB'S NAME FUNCTION TO CALL authorisation some info SESSION MANAGEMENT */ + { .verb = "ping", .callback = pingSample, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE }, + { .verb = "tap_shortcut", .callback = tap_shortcut, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE }, + { .verb = "on_screen_message", .callback = on_screen_message, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE }, + { .verb = "on_screen_reply", .callback = on_screen_reply, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE }, + { .verb = "subscribe", .callback = subscribe, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE }, + { .verb = "unsubscribe", .callback = unsubscribe, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE }, + {NULL } /* marker for end of the array */ +}; + +static int preinit() +{ + HMI_NOTICE("homescreen-service","binding preinit (was register)"); + return 0; +} + +static int init() +{ + HMI_NOTICE("homescreen-service","binding init"); + + ev_tap_shortcut = afb_daemon_make_event(evlist[0]); + ev_on_screen_message = afb_daemon_make_event(evlist[1]); + ev_on_screen_reply = afb_daemon_make_event(evlist[2]); + ev_reserved = afb_daemon_make_event(evlist[3]); + + event_list[0].name = evlist[0]; + event_list[0].event = &ev_tap_shortcut; + + event_list[1].name = evlist[1]; + event_list[1].event = &ev_on_screen_message; + + event_list[2].name = evlist[2]; + event_list[2].event = &ev_on_screen_reply; + + event_list[3].name = evlist[3]; + event_list[3].event = &ev_reserved; + + return 0; +} + +static void onevent(const char *event, struct json_object *object) +{ + HMI_NOTICE("homescreen-service","on_event %s", event); +} + +const struct afb_binding_v2 afbBindingV2 = { + .api = "homescreen", + .specification = NULL, + .info = NULL, + .verbs = verbs, + .preinit = preinit, + .init = init, + .onevent = onevent +}; diff --git a/src/hs-helper.c b/src/hs-helper.c deleted file mode 100644 index 3415510..0000000 --- a/src/hs-helper.c +++ /dev/null @@ -1,156 +0,0 @@ -/* - * 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 "hs-helper.h" -#include -#include -#include -#include -#include - -REQ_ERROR get_value_uint16(const struct afb_req request, const char *source, uint16_t *out_id) -{ - char* endptr; - const char* tmp = afb_req_value (request, source); - if(!tmp) - { - return REQ_FAIL; - } - long tmp_id = strtol(tmp,&endptr,10); - - /* error check of range */ - if( (tmp_id > UINT16_MAX) || (tmp_id < 0) ) - { - return OUT_RANGE; - } - if(*endptr != '\0') - { - return NOT_NUMBER; - } - - *out_id = (uint16_t)tmp_id; - return REQ_OK; -} - -REQ_ERROR get_value_int16(const struct afb_req request, const char *source, int16_t *out_id) -{ - char* endptr; - const char* tmp = afb_req_value (request, source); - if(!tmp) - { - return REQ_FAIL; - } - long tmp_id = strtol(tmp,&endptr,10); - - /* error check of range */ - if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) ) - { - return OUT_RANGE; - } - if(*endptr != '\0') - { - return NOT_NUMBER; - } - - *out_id = (int16_t)tmp_id; - return REQ_OK; -} - -REQ_ERROR get_value_int32(const struct afb_req request, const char *source, int32_t *out_id) -{ - char* endptr; - const char* tmp = afb_req_value (request, source); - if(!tmp) - { - return REQ_FAIL; - } - long tmp_id = strtol(tmp,&endptr,10); - - /* error check of range */ - if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) ) - { - return OUT_RANGE; - } - if(*endptr != '\0') - { - return NOT_NUMBER; - } - - *out_id = (int32_t)tmp_id; - return REQ_OK; -} - -void hs_add_object_to_json_object(struct json_object* j_obj, int count,...) -{ - va_list args; - va_start(args, count); - for(int i = 0; i < count; ++i ) - { - char *key = va_arg(args, char*); - int value = va_arg(args, int); - json_object_object_add(j_obj, key, json_object_new_int((int32_t)value)); - ++i; - } - va_end(args); -} - -void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...) -{ - va_list args; - va_start(args, count); - for(int i = 0; i < count; ++i ) - { - char *key = va_arg(args, char*); - char *value = va_arg(args, char*); - json_object_object_add(j_obj, key, json_object_new_string(value)); - ++i; - } - va_end(args); -} - - -void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...) -{ - va_list args; - va_start(args, count); - - json_object_object_add(j_obj,"verb", json_object_new_string(verb_name)); - - for(int i = 0; i < count; ++i ) - { - char *key = va_arg(args, char*); - int value = va_arg(args, int); - json_object_object_add(j_obj, key, json_object_new_int((int32_t)value)); - ++i; - } - va_end(args); -} - -int hs_search_event_name_index(const char* value) -{ - size_t buf_size = 50; - size_t size = sizeof evlist / sizeof *evlist; - int ret = -1; - for(size_t i = 0 ; i < size ; ++i) - { - if(!strncmp(value, evlist[i], buf_size)) - { - ret = i; - break; - } - } - return ret; -} diff --git a/src/hs-helper.cpp b/src/hs-helper.cpp new file mode 100644 index 0000000..452ae24 --- /dev/null +++ b/src/hs-helper.cpp @@ -0,0 +1,162 @@ +/* + * 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 +#include +#include "hs-helper.h" + + +const char* evlist[] = { + "tap_shortcut", + "on_screen_message", + "on_screen_reply", + "reserved" + }; + + +REQ_ERROR get_value_uint16(const struct afb_req request, const char *source, uint16_t *out_id) +{ + char* endptr; + const char* tmp = afb_req_value (request, source); + if(!tmp) + { + return REQ_FAIL; + } + long tmp_id = strtol(tmp,&endptr,10); + + /* error check of range */ + if( (tmp_id > UINT16_MAX) || (tmp_id < 0) ) + { + return OUT_RANGE; + } + if(*endptr != '\0') + { + return NOT_NUMBER; + } + + *out_id = (uint16_t)tmp_id; + return REQ_OK; +} + +REQ_ERROR get_value_int16(const struct afb_req request, const char *source, int16_t *out_id) +{ + char* endptr; + const char* tmp = afb_req_value (request, source); + if(!tmp) + { + return REQ_FAIL; + } + long tmp_id = strtol(tmp,&endptr,10); + + /* error check of range */ + if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) ) + { + return OUT_RANGE; + } + if(*endptr != '\0') + { + return NOT_NUMBER; + } + + *out_id = (int16_t)tmp_id; + return REQ_OK; +} + +REQ_ERROR get_value_int32(const struct afb_req request, const char *source, int32_t *out_id) +{ + char* endptr; + const char* tmp = afb_req_value (request, source); + if(!tmp) + { + return REQ_FAIL; + } + long tmp_id = strtol(tmp,&endptr,10); + + /* error check of range */ + if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) ) + { + return OUT_RANGE; + } + if(*endptr != '\0') + { + return NOT_NUMBER; + } + + *out_id = (int32_t)tmp_id; + return REQ_OK; +} + +void hs_add_object_to_json_object(struct json_object* j_obj, int count,...) +{ + va_list args; + va_start(args, count); + for(int i = 0; i < count; ++i ) + { + char *key = va_arg(args, char*); + int value = va_arg(args, int); + json_object_object_add(j_obj, key, json_object_new_int((int32_t)value)); + ++i; + } + va_end(args); +} + +void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...) +{ + va_list args; + va_start(args, count); + for(int i = 0; i < count; ++i ) + { + char *key = va_arg(args, char*); + char *value = va_arg(args, char*); + json_object_object_add(j_obj, key, json_object_new_string(value)); + ++i; + } + va_end(args); +} + + +void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...) +{ + va_list args; + va_start(args, count); + + json_object_object_add(j_obj,"verb", json_object_new_string(verb_name)); + + for(int i = 0; i < count; ++i ) + { + char *key = va_arg(args, char*); + int value = va_arg(args, int); + json_object_object_add(j_obj, key, json_object_new_int((int32_t)value)); + ++i; + } + va_end(args); +} + +int hs_search_event_name_index(const char* value) +{ + size_t buf_size = 50; + size_t size = sizeof evlist / sizeof *evlist; + int ret = -1; + for(size_t i = 0 ; i < size ; ++i) + { + if(!strncmp(value, evlist[i], buf_size)) + { + ret = i; + break; + } + } + return ret; +} diff --git a/src/hs-helper.h b/src/hs-helper.h index 5ce9eb5..fe74303 100644 --- a/src/hs-helper.h +++ b/src/hs-helper.h @@ -18,9 +18,8 @@ #define HOMESCREEN_HELPER_H #define AFB_BINDING_VERSION 2 #include -#include -#include -#include +#include + typedef enum REQ_ERROR { @@ -30,12 +29,7 @@ typedef enum REQ_ERROR OUT_RANGE }REQ_ERROR; -static const char* evlist[] = { - "tap_shortcut", - "on_screen_message", - "on_screen_reply", - "reserved" - }; +extern const char* evlist[]; REQ_ERROR get_value_uint16(const struct afb_req request, const char *source, uint16_t *out_id); REQ_ERROR get_value_int16(const struct afb_req request, const char *source, int16_t *out_id); -- cgit 1.2.3-korg