+ if (start) {
+ HOOK(start_before, svc);
+ rc = start(to_afb_service(svc));
+ HOOK(start_after, svc, rc);
+ if (rc < 0)
+ goto error;
+ }
+
+ return svc;
+
+error:
+ svc_free(svc);
+ return NULL;
+}
+
+/*
+ * Creates a new service
+ */
+struct afb_svc *afb_svc_create_v2(
+ const char *api,
+ struct afb_apiset *apiset,
+ int share_session,
+ int (*start)(),
+ void (*on_event)(const char *event, struct json_object *object),
+ struct afb_binding_data_v2 *data
+)
+{
+ int rc;
+ struct afb_svc *svc;
+
+ /* allocates the svc handler */
+ svc = afb_svc_alloc(api, apiset, share_session);
+ if (svc == NULL)
+ goto error;
+ data->service = to_afb_service(svc);
+
+ /* initialises the listener if needed */
+ if (on_event) {
+ svc->on_event = on_event;
+ svc->listener = afb_evt_listener_create(&evt_itf, svc);
+ if (svc->listener == NULL)
+ goto error;
+ }
+
+ /* starts the svc if needed */
+ if (start) {
+ HOOK(start_before, svc);
+ rc = start();
+ HOOK(start_after, svc, rc);
+ if (rc < 0)
+ goto error;
+ }