8 #include <libubox/blobmsg_json.h>
11 #include "interface.h"
12 #include "interface-ip.h"
15 static LIST_HEAD(handlers
);
16 static int proto_fd
, main_fd
;
18 struct proto_shell_handler
{
19 struct list_head list
;
20 struct proto_handler proto
;
21 struct config_param_list config
;
26 struct proto_shell_state
{
27 struct interface_proto_state proto
;
28 struct blob_attr
*config
;
31 #define DUMP_SUFFIX " dump"
34 proto_shell_handler(struct interface_proto_state
*proto
,
35 enum interface_proto_cmd cmd
, bool force
)
39 case PROTO_CMD_TEARDOWN
:
46 proto_shell_free(struct interface_proto_state
*proto
)
48 struct proto_shell_state
*state
;
50 state
= container_of(proto
, struct proto_shell_state
, proto
);
55 struct interface_proto_state
*
56 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
57 struct blob_attr
*attr
)
59 struct proto_shell_state
*state
;
61 state
= calloc(1, sizeof(*state
));
62 state
->config
= malloc(blob_pad_len(attr
));
66 memcpy(state
->config
, attr
, blob_pad_len(attr
));
67 state
->proto
.free
= proto_shell_free
;
68 state
->proto
.handler
= proto_shell_handler
;
78 proto_shell_parse_config(struct config_param_list
*config
, struct json_object
*obj
)
80 struct blobmsg_policy
*attrs
;
81 char *str_buf
, *str_cur
;
85 attrs
= calloc(1, sizeof(*attrs
));
89 config
->n_params
= json_object_array_length(obj
);
90 config
->params
= attrs
;
91 for (i
= 0; i
< config
->n_params
; i
++) {
92 struct json_object
*cur
, *name
, *type
;
94 cur
= json_object_array_get_idx(obj
, i
);
95 if (!cur
|| json_object_get_type(cur
) != json_type_array
)
98 name
= json_object_array_get_idx(cur
, 0);
99 if (!name
|| json_object_get_type(name
) != json_type_string
)
102 type
= json_object_array_get_idx(cur
, 1);
103 if (!type
|| json_object_get_type(type
) != json_type_int
)
106 attrs
[i
].name
= json_object_get_string(name
);
107 attrs
[i
].type
= json_object_get_int(type
);
108 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
111 str_len
+= strlen(attrs
[i
].name
+ 1);
114 str_buf
= malloc(str_len
);
119 for (i
= 0; i
< config
->n_params
; i
++) {
120 const char *name
= attrs
[i
].name
;
122 attrs
[i
].name
= str_cur
;
123 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
130 config
->n_params
= 0;
135 proto_shell_add_handler(const char *script
, struct json_object
*obj
)
137 struct proto_shell_handler
*handler
;
138 struct proto_handler
*proto
;
139 json_object
*config
, *tmp
;
143 if (json_object_get_type(obj
) != json_type_object
)
146 tmp
= json_object_object_get(obj
, "name");
147 if (!tmp
|| json_object_get_type(tmp
) != json_type_string
)
150 name
= json_object_get_string(tmp
);
152 handler
= calloc(1, sizeof(*handler
) +
158 strcpy(handler
->script_name
, script
);
160 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
163 proto
= &handler
->proto
;
165 proto
->config_params
= &handler
->config
;
166 proto
->attach
= proto_shell_attach
;
168 config
= json_object_object_get(obj
, "config");
169 if (config
&& json_object_get_type(config
) == json_type_array
)
170 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
172 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
173 add_proto_handler(proto
);
176 static void proto_shell_add_script(const char *name
)
178 struct json_tokener
*tok
= NULL
;
179 struct json_object
*obj
;
180 static char buf
[512];
181 char *start
, *end
, *cmd
;
185 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
186 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
193 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
200 end
= memchr(start
, '\n', len
);
205 tok
= json_tokener_new();
207 obj
= json_tokener_parse_ex(tok
, start
, len
);
208 if (!is_error(obj
)) {
209 proto_shell_add_handler(name
, obj
);
210 json_object_put(obj
);
211 json_tokener_free(tok
);
217 len
= buflen
- (start
- buf
);
220 } while (!feof(f
) && !ferror(f
));
223 json_tokener_free(tok
);
228 void __init
proto_shell_init(void)
233 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
237 if (chdir(main_path
)) {
238 perror("chdir(main path)");
242 if (chdir("./proto"))
245 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
249 glob("./*.sh", 0, NULL
, &g
);
250 for (i
= 0; i
< g
.gl_pathc
; i
++)
251 proto_shell_add_script(g
.gl_pathv
[i
]);
253 if (list_empty(&handlers
))
258 if (list_empty(&handlers
))