X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-svc.c;h=11e6872f2d7d5ecfb693ff454e1b728f58301fec;hb=c9ba2ce49808a19a4ef982280a46256797b830ae;hp=20dcdcfc7c60b512b4bcc40393f82e780be0e23c;hpb=c710a0da4ebcc126275c42a0387ff85b2557e3ae;p=src%2Fapp-framework-binder.git diff --git a/src/afb-svc.c b/src/afb-svc.c index 20dcdcfc..11e6872f 100644 --- a/src/afb-svc.c +++ b/src/afb-svc.c @@ -104,11 +104,10 @@ const struct afb_req_itf afb_svc_req_itf = { static struct afb_session *common_session; /* - * Creates a new service + * Allocates a new service */ -struct afb_svc *afb_svc_create(int share_session, int (*init)(struct afb_service service), void (*on_event)(const char *event, struct json_object *object)) +static struct afb_svc *afb_svc_alloc(int share_session, void (*on_event)(const char *event, struct json_object *object)) { - int rc; struct afb_svc *svc; /* allocates the svc handler */ @@ -142,19 +141,73 @@ struct afb_svc *afb_svc_create(int share_session, int (*init)(struct afb_service goto error3; } + return svc; + +error3: + afb_session_unref(svc->session); +error2: + free(svc); +error: + return NULL; +} + +/* + * Creates a new service + */ +struct afb_svc *afb_svc_create(int share_session, int (*init)(struct afb_service service), void (*on_event)(const char *event, struct json_object *object)) +{ + int rc; + struct afb_svc *svc; + + /* allocates the svc handler */ + svc = afb_svc_alloc(share_session, on_event); + if (svc == NULL) + goto error; + /* initialises the svc now */ rc = init((struct afb_service){ .itf = &service_itf, .closure = svc }); if (rc < 0) - goto error4; + goto error2; return svc; -error4: +error2: if (svc->listener != NULL) afb_evt_listener_unref(svc->listener); -error3: afb_session_unref(svc->session); + free(svc); +error: + return NULL; +} + +/* + * Creates a new service + */ +struct afb_svc *afb_svc_create_v2( + int share_session, + void (*on_event)(const char *event, struct json_object *object), + int (*start)(const struct afb_binding_interface *interface, struct afb_service service), + const struct afb_binding_interface *interface) +{ + int rc; + struct afb_svc *svc; + + /* allocates the svc handler */ + svc = afb_svc_alloc(share_session, on_event); + if (svc == NULL) + goto error; + + /* initialises the svc now */ + rc = start(interface, (struct afb_service){ .itf = &service_itf, .closure = svc }); + if (rc < 0) + goto error2; + + return svc; + error2: + if (svc->listener != NULL) + afb_evt_listener_unref(svc->listener); + afb_session_unref(svc->session); free(svc); error: return NULL;