diff options
author | Yuta Doi <yuta-d@witz-inc.co.jp> | 2018-05-09 13:47:24 +0900 |
---|---|---|
committer | Yuta Doi <yuta-d@witz-inc.co.jp> | 2018-05-09 13:47:24 +0900 |
commit | d9656f69e340d674d164d8ef30b69cdfb9f976f0 (patch) | |
tree | e4aec250ba19dd2f2bc1c74f4852b027e1b7c2dc /src | |
parent | 026af4256b9b69ffd52972d6da0cb394dff2b0e6 (diff) |
Add headlamp ON/OFF and car stop/run event
Change-Id: I65c543cfce9dcf1c4fdd21af1a3129eceec2b172
Signed-off-by: Yuta Doi <yuta-d@witz-inc.co.jp>
Diffstat (limited to 'src')
-rw-r--r-- | src/libwindowmanager.cpp | 83 | ||||
-rw-r--r-- | src/libwindowmanager.h | 14 |
2 files changed, 63 insertions, 34 deletions
diff --git a/src/libwindowmanager.cpp b/src/libwindowmanager.cpp index 21f7645..581ee0d 100644 --- a/src/libwindowmanager.cpp +++ b/src/libwindowmanager.cpp @@ -49,13 +49,17 @@ extern "C" { class LibWindowmanager::Impl { friend class LibWindowmanager; - const std::vector<std::string> kListEventName{ - std::string("active"), - std::string("inactive"), - std::string("visible"), - std::string("invisible"), - std::string("syncdraw"), - std::string("flushdraw") + const std::vector<const char*> kListEventName{ + "active", + "inactive", + "visible", + "invisible", + "syncdraw", + "flushdraw", + "headlamp_off", + "headlamp_on", + "car_stop", + "car_run", }; /* Key for json obejct */ @@ -89,8 +93,10 @@ class LibWindowmanager::Impl { int api_call(const char *verb, json_object *object, const std::function<void(bool, json_object *)> &onReply); + LibWindowmanager::EventType makeEventType(char const *et); public: + void event(char const *et); void event(char const *et, json_object *object); private: int runEventLoop(); @@ -149,7 +155,7 @@ void onEvent(void *closure, const char *event, afb_wsj1_msg *msg) { reinterpret_cast<LibWindowmanager::Impl *>(closure)->event(event, val); } else { - HMI_ERROR("libwm", "Not found key \"data\""); + reinterpret_cast<LibWindowmanager::Impl *>(closure)->event(event); } } @@ -557,23 +563,26 @@ void LibWindowmanager::Impl::set_event_handler(enum EventType et, handler_fun fu TRACE(); HMI_DEBUG("libwm", "called"); + // Check EventType + if ((et < Event_Val_Min) || (et > Event_Val_Max)) { + HMI_ERROR("libwm", "Invalid EventType: %d", et); + return; + } + // Subscribe event struct json_object* j = json_object_new_object(); json_object_object_add(j, "event", json_object_new_int(et)); int ret = afb_wsj1_call_j(this->wsj1, wmAPI, "wm_subscribe", j, _on_reply_static, this); if (0 > ret) { - HMI_ERROR("libwm", "Failed to subscribe event: %s", kListEventName[et].c_str()); + HMI_ERROR("libwm", "Failed to subscribe event: %s", kListEventName[et]); } // Set event handler - if (et >= Event_Active && et <= Event_FlushDraw) { - this->handlers[et] = std::move(func); - } + this->handlers[et] = std::move(func); } -namespace { -std::pair<bool, LibWindowmanager::EventType> make_event_type(char const *et) { +LibWindowmanager::EventType LibWindowmanager::Impl::makeEventType(char const *et) { // Event have the form "$API/$EVENT", just try to find the first / and // get on with it. char const *et2 = strchr(et, '/'); @@ -581,25 +590,19 @@ std::pair<bool, LibWindowmanager::EventType> make_event_type(char const *et) { et = et2 + 1; } -#define ET(N, A) \ - do { \ - if (strcasecmp(et, N) == 0) \ - return std::pair<bool, LibWindowmanager::EventType>( \ - true, CONCAT(LibWindowmanager::Event_, A)); \ - } while (false) + int i; + for (i=Event_Val_Min; i<=Event_Val_Max; i++) { + if (0 == strcmp(this->kListEventName[i], et)) { + break; + } + } - ET("active", Active); - ET("inactive", Inactive); - ET("visible", Visible); - ET("invisible", Invisible); - ET("syncdraw", SyncDraw); - ET("flushdraw", FlushDraw); -#undef ET + if (Event_Val_Max < i) { + return Event_Val_Invalid; + } - return std::pair<bool, LibWindowmanager::EventType>(false, - LibWindowmanager::Event_Active); + return (EventType)i; } -} // namespace /// object will be json_object_put int LibWindowmanager::Impl::api_call( @@ -668,15 +671,29 @@ int LibWindowmanager::Impl::api_call( return rc; } +void LibWindowmanager::Impl::event(char const *et) { + TRACE(); + EventType event_type = makeEventType(et); + if (0 > event_type) { + HMI_ERROR("libwm", "Unknown event type string '%s'", et); + return; + } + + auto i = this->handlers.find(event_type); + if (i != this->handlers.end()) { + i->second(nullptr); + } +} + void LibWindowmanager::Impl::event(char const *et, json_object *object) { TRACE(); - auto oet = make_event_type(et); - if (!oet.first) { + EventType event_type = makeEventType(et); + if (0 > event_type) { HMI_ERROR("libwm", "Unknown event type string '%s'", et); return; } - auto i = this->handlers.find(oet.second); + auto i = this->handlers.find(event_type); if (i != this->handlers.end()) { json_object *val; const char *label; diff --git a/src/libwindowmanager.h b/src/libwindowmanager.h index b2b9496..d47f400 100644 --- a/src/libwindowmanager.h +++ b/src/libwindowmanager.h @@ -44,7 +44,11 @@ public: const char *kKeyIviId = "ivi_id"; enum EventType { - Event_Active = 0, + Event_Val_Invalid = -1, + + Event_Val_Min = 0, + + Event_Active = Event_Val_Min, Event_Inactive, Event_Visible, @@ -52,6 +56,14 @@ public: Event_SyncDraw, Event_FlushDraw, + + Event_HeadlampOff, + Event_HeadlampOn, + + Event_CarStop, + Event_CarRun, + + Event_Val_Max = Event_CarRun, }; int init(int port, char const *token); |