2 * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/types.h>
32 #include "afb-api-ws.h"
33 #include "afb-api-so.h"
34 #include "afb-apiset.h"
35 #include "afb-autoset.h"
37 static void cleanup(void *closure)
39 struct afb_apiset *call_set = closure;
40 afb_apiset_unref(call_set);
43 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))
45 struct afb_apiset *call_set = closure;
50 base = afb_apiset_name(set);
54 path = alloca(2 + lbase + lname);
55 memcpy(path, base, lbase);
57 memcpy(&path[lbase + 1], name, lname + 1);
59 return create(path, set, call_set);
62 static int add(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set, int (*callback)(void *, struct afb_apiset *, const char*))
64 struct afb_apiset *ownset;
66 /* create a sub-apiset */
67 ownset = afb_apiset_create_subset_last(declare_set, path, 3600);
69 ERROR("Can't create apiset autoset-ws %s", path);
73 /* set the onlack behaviour on this set */
74 afb_apiset_onlack_set(ownset, callback, afb_apiset_addref(call_set), cleanup);
78 /*******************************************************************/
80 static int create_ws(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
82 return afb_api_ws_add_client(path, declare_set, call_set, 0) >= 0;
85 static int onlack_ws(void *closure, struct afb_apiset *set, const char *name)
87 return onlack(closure, set, name, create_ws);
90 int afb_autoset_add_ws(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
92 return add(path, declare_set, call_set, onlack_ws);
95 /*******************************************************************/
97 static int create_so(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
99 return afb_api_so_add_binding(path, declare_set, call_set) >= 0;
102 static int onlack_so(void *closure, struct afb_apiset *set, const char *name)
104 return onlack(closure, set, name, create_so);
107 int afb_autoset_add_so(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
109 return add(path, declare_set, call_set, onlack_so);
112 /*******************************************************************/
114 static int create_any(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
118 char sockname[PATH_MAX + 7];
120 rc = stat(path, &st);
122 switch(st.st_mode & S_IFMT) {
124 rc = afb_api_so_add_binding(path, declare_set, call_set);
127 snprintf(sockname, sizeof sockname, "unix:%s", path);
128 rc = afb_api_ws_add_client(sockname, declare_set, call_set, 0);
131 NOTICE("Unexpected autoset entry: %s", path);
139 static int onlack_any(void *closure, struct afb_apiset *set, const char *name)
141 return onlack(closure, set, name, create_any);
144 int afb_autoset_add_any(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
146 return add(path, declare_set, call_set, onlack_any);