api-v3: First draft
[src/app-framework-binder.git] / src / afb-autoset.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28
29 #include "verbose.h"
30 #include "afb-api-ws.h"
31 #include "afb-api-so.h"
32 #include "afb-apiset.h"
33 #include "afb-autoset.h"
34
35 static void cleanup(void *closure)
36 {
37         struct afb_apiset *call_set = closure;
38         afb_apiset_unref(call_set);
39 }
40
41 static int onlack(void *closure, struct afb_apiset *set, const char *name, int (*create)(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set))
42 {
43         struct afb_apiset *call_set = closure;
44         char *path;
45         const char *base;
46         size_t lbase, lname;
47
48         base = afb_apiset_name(set);
49         lbase = strlen(base);
50         lname = strlen(name);
51
52         path = alloca(2 + lbase + lname);
53         memcpy(path, base, lbase);
54         path[lbase] = '/';
55         memcpy(&path[lbase + 1], name, lname + 1);
56
57         return create(path, set, call_set);
58 }
59
60 static int add(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set, int (*callback)(void *, struct afb_apiset *, const char*))
61 {
62         struct afb_apiset *ownset;
63
64         /* create a sub-apiset */
65         ownset = afb_apiset_create_subset_last(declare_set, path, 3600);
66         if (!ownset) {
67                 ERROR("Can't create apiset autoset-ws %s", path);
68                 return -1;
69         }
70
71         /* set the onlack behaviour on this set */
72         afb_apiset_onlack_set(ownset, callback, afb_apiset_addref(call_set), cleanup);
73         return 0;
74 }
75
76 static int create_ws(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
77 {
78         return afb_api_ws_add_client(path, declare_set, call_set, 0) >= 0;
79 }
80
81 static int onlack_ws(void *closure, struct afb_apiset *set, const char *name)
82 {
83         return onlack(closure, set, name, create_ws);
84 }
85
86 int afb_autoset_add_ws(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
87 {
88         return add(path, declare_set, call_set, onlack_ws);
89 }
90
91 static int create_so(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
92 {
93         return afb_api_so_add_binding(path, declare_set, call_set) >= 0;
94 }
95
96 static int onlack_so(void *closure, struct afb_apiset *set, const char *name)
97 {
98         return onlack(closure, set, name, create_so);
99 }
100
101 int afb_autoset_add_so(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
102 {
103         return add(path, declare_set, call_set, onlack_so);
104 }