[v2,01/12] lib: Add plugin_option type and protocol

Submitted by Sam Mendoza-Jonas on July 25, 2017, 6:48 a.m.

Details

Message ID 20170725064910.31478-2-sam@mendozajonas.com
State Accepted
Headers show

Commit Message

Sam Mendoza-Jonas July 25, 2017, 6:48 a.m.
Add a new struct 'plugin_option' to represent pb-plugins that are
installed on the system. This consists of plugin metadata and an array
of installed executables.
This also adds two new pb-protocol actions to advertise the addition of
a new plugin_option, and to remove known plugin_options.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
---
 lib/pb-protocol/pb-protocol.c | 104 ++++++++++++++++++++++++++++++++++++++++++
 lib/pb-protocol/pb-protocol.h |   8 ++++
 lib/types/types.h             |  15 ++++++
 3 files changed, 127 insertions(+)

Patch hide | download patch | download mbox

diff --git a/lib/pb-protocol/pb-protocol.c b/lib/pb-protocol/pb-protocol.c
index 18edf57..ed61fe1 100644
--- a/lib/pb-protocol/pb-protocol.c
+++ b/lib/pb-protocol/pb-protocol.c
@@ -340,6 +340,26 @@  int pb_protocol_url_len(const char *url)
 	return 4 + optional_strlen(url);
 }
 
+
+int pb_protocol_plugin_option_len(const struct plugin_option *opt)
+{
+	unsigned int i, len = 0;
+
+	len += 4 + optional_strlen(opt->id);
+	len += 4 + optional_strlen(opt->name);
+	len += 4 + optional_strlen(opt->vendor);
+	len += 4 + optional_strlen(opt->vendor_id);
+	len += 4 + optional_strlen(opt->version);
+	len += 4 + optional_strlen(opt->date);
+	len += 4 + optional_strlen(opt->plugin_file);
+
+	len += 4; /* n_executables */
+	for (i = 0; i < opt->n_executables; i++)
+		len += 4 + optional_strlen(opt->executables[i]);
+
+	return len;
+}
+
 int pb_protocol_serialise_device(const struct device *dev,
 		char *buf, int buf_len)
 {
@@ -611,6 +631,32 @@  int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
 	return 0;
 }
 
+int pb_protocol_serialise_plugin_option(const struct plugin_option *opt,
+		char *buf, int buf_len)
+{
+	char *pos = buf;
+	unsigned int i;
+
+	pos += pb_protocol_serialise_string(pos, opt->id);
+	pos += pb_protocol_serialise_string(pos, opt->name);
+	pos += pb_protocol_serialise_string(pos, opt->vendor);
+	pos += pb_protocol_serialise_string(pos, opt->vendor_id);
+	pos += pb_protocol_serialise_string(pos, opt->version);
+	pos += pb_protocol_serialise_string(pos, opt->date);
+	pos += pb_protocol_serialise_string(pos, opt->plugin_file);
+
+	*(uint32_t *)pos = __cpu_to_be32(opt->n_executables);
+	pos += 4;
+
+	for (i = 0; i < opt->n_executables; i++)
+		pos += pb_protocol_serialise_string(pos, opt->executables[i]);
+
+	assert(pos <= buf + buf_len);
+	(void)buf_len;
+
+	return 0;
+}
+
 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
 {
 	int total_len, rc;
@@ -1150,3 +1196,61 @@  int pb_protocol_deserialise_config(struct config *config,
 out:
 	return rc;
 }
+
+int pb_protocol_deserialise_plugin_option(struct plugin_option *opt,
+		const struct pb_protocol_message *message)
+{
+	unsigned int len, i, tmp;
+	const char *pos;
+	int rc = -1;
+	char *str;
+
+	len = message->payload_len;
+	pos = message->payload;
+
+	if (read_string(opt, &pos, &len, &str))
+		goto out;
+	opt->id = str;
+
+	if (read_string(opt, &pos, &len, &str))
+		goto out;
+	opt->name = str;
+
+	if (read_string(opt, &pos, &len, &str))
+		goto out;
+	opt->vendor = str;
+
+	if (read_string(opt, &pos, &len, &str))
+		goto out;
+	opt->vendor_id = str;
+
+	if (read_string(opt, &pos, &len, &str))
+		goto out;
+	opt->version = str;
+
+	if (read_string(opt, &pos, &len, &str))
+		goto out;
+	opt->date = str;
+
+	if (read_string(opt, &pos, &len, &str))
+		goto out;
+	opt->plugin_file = str;
+
+	if (read_u32(&pos, &len, &tmp))
+		goto out;
+	opt->n_executables = tmp;
+
+	opt->executables = talloc_zero_array(opt, char *, opt->n_executables);
+	if (!opt->executables)
+		goto out;
+
+	for (i = 0; i < opt->n_executables; i++) {
+		if (read_string(opt, &pos, &len, &str))
+			goto out;
+		opt->executables[i] = talloc_strdup(opt, str);
+	}
+
+	rc = 0;
+out:
+	return rc;
+}
diff --git a/lib/pb-protocol/pb-protocol.h b/lib/pb-protocol/pb-protocol.h
index a8cd206..ce876d0 100644
--- a/lib/pb-protocol/pb-protocol.h
+++ b/lib/pb-protocol/pb-protocol.h
@@ -23,6 +23,8 @@  enum pb_protocol_action {
 	PB_PROTOCOL_ACTION_CONFIG		= 0x9,
 	PB_PROTOCOL_ACTION_REINIT		= 0xa,
 	PB_PROTOCOL_ACTION_ADD_URL		= 0xb,
+	PB_PROTOCOL_ACTION_PLUGIN_OPTION_ADD	= 0xc,
+	PB_PROTOCOL_ACTION_PLUGINS_REMOVE	= 0xd,
 };
 
 struct pb_protocol_message {
@@ -40,6 +42,7 @@  int pb_protocol_boot_status_len(const struct status *status);
 int pb_protocol_system_info_len(const struct system_info *sysinfo);
 int pb_protocol_config_len(const struct config *config);
 int pb_protocol_url_len(const char *url);
+int pb_protocol_plugin_option_len(const struct plugin_option *opt);
 int pb_protocol_device_cmp(const struct device *a, const struct device *b);
 
 int pb_protocol_boot_option_cmp(const struct boot_option *a,
@@ -62,6 +65,8 @@  int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
 int pb_protocol_serialise_config(const struct config *config,
 		char *buf, int buf_len);
 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len);
+int pb_protocol_serialise_plugin_option(const struct plugin_option *opt,
+		char *buf, int buf_len);
 
 int pb_protocol_write_message(int fd, struct pb_protocol_message *message);
 
@@ -87,4 +92,7 @@  int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
 
 int pb_protocol_deserialise_config(struct config *config,
 		const struct pb_protocol_message *message);
+
+int pb_protocol_deserialise_plugin_option(struct plugin_option *opt,
+		const struct pb_protocol_message *message);
 #endif /* _PB_PROTOCOL_H */
diff --git a/lib/types/types.h b/lib/types/types.h
index 7f4ae1f..36841cd 100644
--- a/lib/types/types.h
+++ b/lib/types/types.h
@@ -60,6 +60,21 @@  struct boot_option {
 	void		*ui_info;
 };
 
+struct plugin_option {
+	char		*id;
+	char		*name;
+	char		*vendor;
+	char		*vendor_id;
+	char		*version;
+	char		*date;
+	char		*plugin_file;
+
+	unsigned int	n_executables;
+	char		**executables;
+
+	void		*ui_info;
+};
+
 struct boot_command {
 	char *option_id;
 	char *boot_image_file;