4 static struct blob_buf b
;
5 static struct ubus_msg_buf
*retmsg
;
6 static int *retmsg_data
;
8 static struct blob_attr
*attrbuf
[UBUS_ATTR_MAX
];
10 typedef int (*ubus_cmd_cb
)(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
);
12 static const struct blob_attr_info ubus_policy
[UBUS_ATTR_MAX
] = {
13 [UBUS_ATTR_SIGNATURE
] = { .type
= BLOB_ATTR_NESTED
},
14 [UBUS_ATTR_OBJTYPE
] = { .type
= BLOB_ATTR_INT32
},
15 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
18 struct blob_attr
**ubus_parse_msg(struct blob_attr
*msg
)
20 blob_parse(msg
, attrbuf
, ubus_policy
, UBUS_ATTR_MAX
);
24 static void ubus_msg_init(struct ubus_msg_buf
*ub
, uint8_t type
, uint16_t seq
, uint32_t peer
)
32 static struct ubus_msg_buf
*ubus_msg_from_blob(bool shared
)
34 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), shared
);
37 static struct ubus_msg_buf
*ubus_reply_from_blob(struct ubus_msg_buf
*ub
, bool shared
)
39 struct ubus_msg_buf
*new;
41 new = ubus_msg_new(b
.head
, blob_raw_len(b
.head
), shared
);
45 ubus_msg_init(new, UBUS_MSG_DATA
, ub
->hdr
.seq
, ub
->hdr
.peer
);
49 bool ubusd_send_hello(struct ubus_client
*cl
)
51 struct ubus_msg_buf
*ub
;
54 ub
= ubus_msg_from_blob(true);
58 ubus_msg_init(ub
, UBUS_MSG_HELLO
, 0, cl
->id
.id
);
59 ubus_msg_send(cl
, ub
);
63 static int ubusd_send_pong(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
65 ub
->hdr
.type
= UBUS_MSG_DATA
;
66 ubus_msg_send(cl
, ubus_msg_ref(ub
));
70 static int ubusd_handle_publish(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
72 struct ubus_object
*obj
;
73 struct blob_attr
**attr
;
75 attr
= ubus_parse_msg(ub
->data
);
76 obj
= ubusd_create_object(cl
, attr
);
78 return UBUS_STATUS_INVALID_ARGUMENT
;
81 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
82 if (attr
[UBUS_ATTR_SIGNATURE
])
83 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
.id
);
85 ub
= ubus_reply_from_blob(ub
, true);
87 return UBUS_STATUS_NO_DATA
;
89 ubus_msg_send(cl
, ub
);
93 static void ubusd_send_obj(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct ubus_object
*obj
)
95 struct ubus_method
*m
;
101 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, obj
->path
.key
);
102 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
104 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
105 list_for_each_entry(m
, &obj
->type
->methods
, list
)
106 blob_put(&b
, blob_id(m
->data
), blob_data(m
->data
), blob_len(m
->data
));
107 blob_nest_end(&b
, s
);
109 ub
= ubus_reply_from_blob(ub
, true);
113 ubus_msg_send(cl
, ub
);
116 static int ubusd_handle_lookup(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
118 struct ubus_object
*obj
;
119 struct blob_attr
**attr
;
121 bool wildcard
= false;
125 attr
= ubus_parse_msg(ub
->data
);
126 if (!attr
[UBUS_ATTR_OBJPATH
]) {
127 avl_for_each_element(&path
, obj
, path
)
128 ubusd_send_obj(cl
, ub
, obj
);
132 objpath
= blob_data(attr
[UBUS_ATTR_OBJPATH
]);
133 len
= strlen(objpath
);
134 if (objpath
[len
- 1] != '*') {
135 obj
= avl_find_element(&path
, objpath
, obj
, path
);
137 return UBUS_STATUS_NOT_FOUND
;
139 ubusd_send_obj(cl
, ub
, obj
);
146 obj
= avl_find_ge_element(&path
, objpath
, obj
, path
);
148 return UBUS_STATUS_NOT_FOUND
;
150 while (!strncmp(objpath
, obj
->path
.key
, len
)) {
152 ubusd_send_obj(cl
, ub
, obj
);
153 if (obj
== avl_last_element(&path
, obj
, path
))
155 obj
= avl_next_element(obj
, path
);
159 return UBUS_STATUS_NOT_FOUND
;
164 static int ubusd_handle_invoke(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
166 return UBUS_STATUS_NOT_FOUND
;
169 static const ubus_cmd_cb handlers
[__UBUS_MSG_LAST
] = {
170 [UBUS_MSG_PING
] = ubusd_send_pong
,
171 [UBUS_MSG_PUBLISH
] = ubusd_handle_publish
,
172 [UBUS_MSG_LOOKUP
] = ubusd_handle_lookup
,
173 [UBUS_MSG_INVOKE
] = ubusd_handle_invoke
,
176 void ubusd_receive_message(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
178 ubus_cmd_cb cb
= NULL
;
181 retmsg
->hdr
.seq
= ub
->hdr
.seq
;
182 retmsg
->hdr
.peer
= ub
->hdr
.peer
;
184 if (ub
->hdr
.type
< __UBUS_MSG_LAST
)
185 cb
= handlers
[ub
->hdr
.type
];
190 ret
= UBUS_STATUS_INVALID_COMMAND
;
194 *retmsg_data
= htonl(ret
);
195 ubus_msg_send(cl
, ubus_msg_ref(retmsg
));
198 static void __init
ubusd_proto_init(void)
200 blob_buf_init(&b
, 0);
201 blob_put_int32(&b
, UBUS_ATTR_STATUS
, 0);
203 retmsg
= ubus_msg_from_blob(false);
207 retmsg
->hdr
.type
= UBUS_MSG_STATUS
;
208 retmsg_data
= blob_data(blob_data(retmsg
->data
));