diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-06-12 09:36:17 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-06-12 09:36:17 +0200 |
commit | 4788a840abe82f2c0f20b027589085d29051763c (patch) | |
tree | d33ff23a53b548287dfb7c34a4c678375f07fa22 | |
parent | 96c638490f1e8a871c3072ae3dfab2b1e7a02b7f (diff) |
Migration to binding v2
Change-Id: I05e93ff74db3f9e7b31a7348ea0fa23d16b737c7
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | high-can-binding/high-can-binding-hat.cpp | 54 | ||||
-rw-r--r-- | high-can-binding/high-can-binding-hat.hpp | 14 | ||||
-rw-r--r-- | high-can-binding/high-can-binding.cpp | 12 | ||||
-rw-r--r-- | high-can-binding/high.cpp | 48 | ||||
-rw-r--r-- | high-can-binding/high.hpp | 3 |
5 files changed, 58 insertions, 73 deletions
diff --git a/high-can-binding/high-can-binding-hat.cpp b/high-can-binding/high-can-binding-hat.cpp index 01f6ab1..0bf7090 100644 --- a/high-can-binding/high-can-binding-hat.cpp +++ b/high-can-binding/high-can-binding-hat.cpp @@ -1,47 +1,35 @@ #include "high-can-binding-hat.hpp" #include <cstddef> /// Interface between the daemon and the binding -const struct afb_binding_interface *binder_interface; -extern "C" -{ - #include <afb/afb-service-itf.h> - struct afb_service srvitf; -}; -static const struct afb_verb_desc_v1 verbs[]= + +static int init_service(); + +static const struct afb_verb_v2 verbs[]= { - { .name= "subscribe", .session= AFB_SESSION_NONE, .callback= subscribe, .info= "subscribe to notification of CAN bus messages." }, - { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe, .info= "unsubscribe a previous subscription." }, - { .name= "get", .session= AFB_SESSION_NONE, .callback= get, .info= "high can get viwi request." }, + { .verb= "subscribe", .callback= subscribe, .auth = NULL, .session = 0 }, + { .verb= "unsubscribe", .callback= unsubscribe, .auth = NULL, .session = 0 }, + { .verb= "get", .callback= get, .auth = NULL, .session = 0 }, + { .verb= NULL, .callback=NULL, .auth = NULL, .session = 0 } }; -static const struct afb_binding binding_desc { - AFB_BINDING_VERSION_1, - { - "High level CAN bus service", - "high-can", - verbs - } +const struct afb_binding_v2 afbBindingV2 = { + .api = "high-can", + .specification = "", + .verbs = verbs, + .preinit = NULL, + .init = init_service, + .onevent = onEvent, + .noconcurrency = 1 }; -const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf) -{ - binder_interface = itf; - NOTICE(binder_interface, "high level afbBindingV1Register"); - - return &binding_desc; -} /// @brief Initialize the binding. /// -/// @param[in] service Structure which represent the Application Framework Binder. -/// /// @return Exit code, zero if success. -int afbBindingV1ServiceInit(struct afb_service service) +int init_service() { - srvitf = service; - //NOTICE(binder_interface, "before afb_daemon_require_api"); - //afb_daemon_require_api(binder_interface->daemon, "low-can", 1); - NOTICE(binder_interface, "high level binding is initializing"); - initHigh(service); - NOTICE(binder_interface, "high level binding is initialized and running"); + NOTICE("high level binding is initializing"); + afb_daemon_require_api("low-can", 1); + initHigh(); + NOTICE("high level binding is initialized and running"); return 0; } diff --git a/high-can-binding/high-can-binding-hat.hpp b/high-can-binding/high-can-binding-hat.hpp index b91f083..487ee72 100644 --- a/high-can-binding/high-can-binding-hat.hpp +++ b/high-can-binding/high-can-binding-hat.hpp @@ -3,15 +3,13 @@ #include <systemd/sd-event.h> extern "C" { - #define AFB_BINDING_VERSION 1 + #define AFB_BINDING_VERSION 2 #include <afb/afb-binding.h> }; -extern "C" struct afb_binding_interface; -extern "C" struct afb_service srvitf; -extern const struct afb_binding_interface *binder_interface; - void subscribe(struct afb_req request); - void unsubscribe(struct afb_req request); - void get(struct afb_req request); - void initHigh(afb_service service); + void onEvent(const char *event, struct json_object *object); + void subscribe(afb_req request); + void unsubscribe(afb_req request); + void get(afb_req request); + void initHigh(); int ticked(sd_event_source *source, unsigned long t, void *data); diff --git a/high-can-binding/high-can-binding.cpp b/high-can-binding/high-can-binding.cpp index bbcc941..4232ae2 100644 --- a/high-can-binding/high-can-binding.cpp +++ b/high-can-binding/high-can-binding.cpp @@ -22,12 +22,12 @@ High high; /// @brief callback for receiving message from low binding. Treatment itself is made in High class. -extern void afbBindingV1ServiceEvent(const char *event, struct json_object *object) +void onEvent(const char *event, json_object *object) { high.treatMessage(object); } /// @brief entry point for client subscription request. Treatment itself is made in High class. -void subscribe(struct afb_req request) +void subscribe(afb_req request) { if(high.subscribe(request)) afb_req_success(request, NULL, NULL); @@ -36,7 +36,7 @@ void subscribe(struct afb_req request) } /// @brief entry point for client un-subscription request. Treatment itself is made in High class. -void unsubscribe(struct afb_req request) +void unsubscribe(afb_req request) { if(high.unsubscribe(request)) afb_req_success(request, NULL, NULL); @@ -45,7 +45,7 @@ void unsubscribe(struct afb_req request) } /// @brief entry point for get requests. Treatment itself is made in High class. -void get(struct afb_req request) +void get(afb_req request) { json_object *jobj; if(high.get(request, &jobj)) { @@ -66,9 +66,9 @@ int ticked(sd_event_source *source, long unsigned int t, void* data) /// @brief Initialize the binding. /// /// @param[in] service Structure which represent the Application Framework Binder. -void initHigh(struct afb_service service) +void initHigh() { - high.parseConfigAndSubscribe(service); + high.parseConfigAndSubscribe(); } diff --git a/high-can-binding/high.cpp b/high-can-binding/high.cpp index 82f95cb..b37fda6 100644 --- a/high-can-binding/high.cpp +++ b/high-can-binding/high.cpp @@ -48,9 +48,7 @@ High::High() /// @brief Reads the json configuration and generates accordingly the resources container. An UID is generated for each resource. /// Makes necessary subscriptions to low-level, eventually with a frequency. /// -/// @param[in] afb_service: the service to call for subscriptions. -/// -void High::parseConfigAndSubscribe(struct afb_service service) +void High::parseConfigAndSubscribe() { json_object *config = json_object_from_file("high.json"); json_object *jvalue, *jarray1, *jarray2, *obj; @@ -88,7 +86,7 @@ void High::parseConfigAndSubscribe(struct afb_service service) const std::string uri = name + id; jvalue = json_object_array_get_idx(jarray2, i); if(properties.find(name) == properties.end()) { - NOTICE(binder_interface, "Unable to find name %s in properties", name.c_str()); + NOTICE("Unable to find name %s in properties", name.c_str()); continue; } const std::map<std::string, Property> props = properties[name]; @@ -101,7 +99,7 @@ void High::parseConfigAndSubscribe(struct afb_service service) json_object_object_foreach(jvalue, key, val) { const std::string value = json_object_get_string(val); if(props.find(key) == props.end()) { - NOTICE(binder_interface, "Unable to find key %s in properties", value.c_str()); + NOTICE("Unable to find key %s in properties", value.c_str()); continue; } Property prop = props.at(key); @@ -109,7 +107,7 @@ void High::parseConfigAndSubscribe(struct afb_service service) const std::string canMessage = value.substr(2, value.size() - 1); const std::vector<std::string> params = split(canMessage, ','); if(params.size() != 2) { - NOTICE(binder_interface, "Invalid CAN message definition %s", value.c_str()); + NOTICE("Invalid CAN message definition %s", value.c_str()); continue; } prop.lowMessageName = params.at(0); @@ -129,7 +127,7 @@ void High::parseConfigAndSubscribe(struct afb_service service) else if(prop.type == "int") prop.value_int = 0; else - NOTICE(binder_interface, "ERROR 2! unexpected type in parseConfig %s %s", prop.description.c_str(), prop.type.c_str()); + NOTICE("ERROR 2! unexpected type in parseConfig %s %s", prop.description.c_str(), prop.type.c_str()); } else { prop.value_string= std::string(value); } @@ -157,14 +155,14 @@ void High::parseConfigAndSubscribe(struct afb_service service) } json_object *dummy; const std::string js = json_object_get_string(jobj); - if(afb_service_call_sync(service, "low-can", "subscribe", jobj, &dummy) != 1) - NOTICE(binder_interface, "high-can subscription to low-can FAILED %s", js.c_str()); + if(afb_service_call_sync("low-can", "subscribe", jobj, &dummy) != 1) + NOTICE("high-can subscription to low-can FAILED %s", js.c_str()); else - NOTICE(binder_interface, "high-can subscribed to low-can %s", js.c_str()); + NOTICE("high-can subscribed to low-can %s", js.c_str()); json_object_put(dummy); } json_object_put(config); - NOTICE(binder_interface, "configuration loaded"); + NOTICE("configuration loaded"); } /// @brief Create and start a systemD timer. Only one timer is created per frequency. @@ -177,7 +175,7 @@ void High::startTimer(const int &t) return; struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); - sd_event_add_time(afb_daemon_get_event_loop(binder_interface->daemon), NULL, CLOCK_MONOTONIC, (ts.tv_sec + 1) * 1000000, 0, &ticked, new int(t)); + sd_event_add_time(afb_daemon_get_event_loop(), NULL, CLOCK_MONOTONIC, (ts.tv_sec + 1) * 1000000, 0, &ticked, new int(t)); } High::~High() { @@ -186,9 +184,9 @@ High::~High() /// @brief callback called after subscription to low-level binding. /// -void High::callBackFromSubscribe(void *handle, int iserror, struct json_object *result) +void High::callBackFromSubscribe(void *handle, int iserror, json_object *result) { - NOTICE(binder_interface, "high level callBackFromSubscribe method called %s", json_object_get_string(result)); + NOTICE("high level callBackFromSubscribe method called %s", json_object_get_string(result)); } /// @brief Entry point for all timer events. Treats all requests linked to the specific timer frequency. @@ -199,7 +197,7 @@ void High::callBackFromSubscribe(void *handle, int iserror, struct json_object * void High::tick(sd_event_source *source, const long &now, void *interv) { const int interval = *(int*)interv; - NOTICE(binder_interface, "tick! %d %ld", interval, now); + NOTICE("tick! %d %ld", interval, now); bool hasEvents = false; if(timedEvents.find(interval) != timedEvents.end()) { std::vector<TimedEvent> evts = timedEvents[interval]; @@ -225,7 +223,7 @@ void High::tick(sd_event_source *source, const long &now, void *interv) evts.erase(evts.begin() + i); timedEvents[interval] = evts; } - //NOTICE(binder_interface, "%s event pushed to %d subscribers", e.eventName.c_str(), nbSubscribers); + //NOTICE("%s event pushed to %d subscribers", e.eventName.c_str(), nbSubscribers); } if(evts.size() > 0) hasEvents = true; @@ -234,7 +232,7 @@ void High::tick(sd_event_source *source, const long &now, void *interv) sd_event_source_set_time(source, now + interval * 1000); sd_event_source_set_enabled(source, SD_EVENT_ON); } else { - //NOTICE(binder_interface, "timer removed %d", interval); + //NOTICE("timer removed %d", interval); delete (int*)interv; if(timers.find(interval) != timers.end()) { timers.erase(interval); @@ -255,10 +253,10 @@ void High::treatMessage(json_object *message) json_object_object_get_ex(message, "value", &jvalue); const std::string messageName(json_object_get_string(nameJson)); if(lowMessagesToObjects.find(messageName) == lowMessagesToObjects.end()) { - NOTICE(binder_interface, "message not linked to any object %s", json_object_get_string(message)); + NOTICE("message not linked to any object %s", json_object_get_string(message)); return; } -// NOTICE(binder_interface, "message received %s", json_object_get_string(message)); +// NOTICE("message received %s", json_object_get_string(message)); const std::set<std::string> objects = lowMessagesToObjects.at(messageName); std::vector<std::string> candidateMessages; for(const std::string &uri : objects) { @@ -283,7 +281,7 @@ void High::treatMessage(json_object *message) else if(property.type == "int") property.value_int = json_object_get_int(jvalue); else - NOTICE(binder_interface, "ERROR 3! unexpected type %s %s", property.description.c_str(), property.type.c_str()); + NOTICE("ERROR 3! unexpected type %s %s", property.description.c_str(), property.type.c_str()); properties[foundProperty] = property; registeredObjects[uri] = properties; } @@ -344,7 +342,7 @@ json_object *High::generateJson(const std::string &messageObject, std::vector<st const int value = p.second.value_int; json_object_object_add(json, p.first.c_str(), json_object_new_int(value)); } else { - NOTICE(binder_interface, "ERROR 1! unexpected type %s %s %s", p.first.c_str(), p.second.description.c_str(), p.second.type.c_str()); + NOTICE("ERROR 1! unexpected type %s %s %s", p.first.c_str(), p.second.description.c_str(), p.second.type.c_str()); } } return json; @@ -388,7 +386,7 @@ bool High::subscribe(afb_req request) if(events.find(message) != events.end()) { event = events.at(message); } else { - event = afb_daemon_make_event(binder_interface->daemon, p.first.c_str()); + event = afb_daemon_make_event(p.first.c_str()); events[message] = event; } if (afb_event_is_valid(event) && afb_req_subscribe(request, event) == 0) { @@ -411,10 +409,10 @@ bool High::subscribe(afb_req request) char ext[20]; sprintf(ext, "_%d", ms); std::string messageName = message + std::string(ext); - //NOTICE(binder_interface, "subscribe with interval %s", messageName.c_str()); - afbEvent = afb_daemon_make_event(binder_interface->daemon, messageName.c_str()); + //NOTICE("subscribe with interval %s", messageName.c_str()); + afbEvent = afb_daemon_make_event(messageName.c_str()); if (!afb_event_is_valid(afbEvent)) { - NOTICE(binder_interface, "unable to create event"); + NOTICE("unable to create event"); return false; } TimedEvent e; diff --git a/high-can-binding/high.hpp b/high-can-binding/high.hpp index c015f4a..de3a471 100644 --- a/high-can-binding/high.hpp +++ b/high-can-binding/high.hpp @@ -9,6 +9,7 @@ #include <systemd/sd-event.h> extern "C" { + #define AFB_BINDING_VERSION 2 #include <afb/afb-binding.h> }; @@ -43,7 +44,7 @@ public: void tick(sd_event_source *source, const long &now, void *interv); void startTimer(const int &t); ~High(); - void parseConfigAndSubscribe(afb_service service); + void parseConfigAndSubscribe(); static bool startsWith(const std::string &s, const std::string &val); static void callBackFromSubscribe(void *handle, int iserror, json_object *result); private: |