[PATCH 22/25] libusbg: Hide definition of function structure.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hide definition of function structure to avoid direct
access to its fields. Rename it to usbg_function.

Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx>
---
 examples/gadget-acm-ecm.c |    4 +--
 examples/show-gadgets.c   |   36 ++++++++++++++++-----------
 include/usbg/usbg.h       |   53 ++++++++++++++++-----------------------
 src/usbg.c                |   60 +++++++++++++++++++++++++++------------------
 4 files changed, 81 insertions(+), 72 deletions(-)

diff --git a/examples/gadget-acm-ecm.c b/examples/gadget-acm-ecm.c
index 2a37f14..93e0c06 100644
--- a/examples/gadget-acm-ecm.c
+++ b/examples/gadget-acm-ecm.c
@@ -31,9 +31,9 @@ int main(void)
 {
 	usbg_state *s;
 	usbg_gadget *g;
-	struct function *f;
+	usbg_function *f;
 	usbg_config *c;
-	struct function *f_acm0, *f_acm1, *f_ecm;
+	usbg_function *f_acm0, *f_acm1, *f_ecm;
 	int ret = -EINVAL;
 
 	s = usbg_init("/sys/kernel/config");
diff --git a/examples/show-gadgets.c b/examples/show-gadgets.c
index f7bd3c1..f0dc182 100644
--- a/examples/show-gadgets.c
+++ b/examples/show-gadgets.c
@@ -57,15 +57,21 @@ void show_gadget(usbg_gadget *g)
 	fprintf(stdout, "  Product\t\t%s\n", g_strs.str_prd);
 }
 
-void show_function(struct function *f)
+void show_function(usbg_function *f)
 {
-	fprintf(stdout, "  Function '%s'\n", f->name);
-	switch (f->type) {
+	char buf[USBG_MAX_STR_LENGTH];
+	union attrs f_attrs;
+
+	usbg_get_function_name(f, buf, USBG_MAX_STR_LENGTH);
+	usbg_get_function_attrs(f, &f_attrs);
+
+	fprintf(stdout, "  Function '%s'\n", buf);
+	switch (usbg_get_function_type(f)) {
 	case F_SERIAL:
 	case F_ACM:
 	case F_OBEX:
 		fprintf(stdout, "    port_num\t\t%d\n",
-			f->attr.serial.port_num);
+				f_attrs.serial.port_num);
 		break;
 	case F_ECM:
 	case F_SUBSET:
@@ -73,14 +79,14 @@ void show_function(struct function *f)
 	case F_EEM:
 	case F_RNDIS:
 		fprintf(stdout, "    dev_addr\t\t%s\n",
-			ether_ntoa(&f->attr.net.dev_addr));
+				ether_ntoa(&f_attrs.net.dev_addr));
 		fprintf(stdout, "    host_addr\t\t%s\n",
-			ether_ntoa(&f->attr.net.host_addr));
-		fprintf(stdout, "    ifname\t\t%s\n", f->attr.net.ifname);
-		fprintf(stdout, "    qmult\t\t%d\n", f->attr.net.qmult);
+				ether_ntoa(&f_attrs.net.host_addr));
+		fprintf(stdout, "    ifname\t\t%s\n", f_attrs.net.ifname);
+		fprintf(stdout, "    qmult\t\t%d\n", f_attrs.net.qmult);
 		break;
 	case F_PHONET:
-		fprintf(stdout, "    ifname\t\t%s\n", f->attr.phonet.ifname);
+		fprintf(stdout, "    ifname\t\t%s\n", f_attrs.phonet.ifname);
 		break;
 	default:
 		fprintf(stdout, "    UNKNOWN\n");
@@ -90,7 +96,7 @@ void show_function(struct function *f)
 void show_config(usbg_config *c)
 {
 	struct binding *b;
-	struct function *f;
+	usbg_function *f;
 	char buf[USBG_MAX_STR_LENGTH], buf2[USBG_MAX_STR_LENGTH];
 	struct config_attrs c_attrs;
 	struct config_strs c_strs;
@@ -105,18 +111,20 @@ void show_config(usbg_config *c)
 	usbg_get_config_strs(c, &c_strs);
 	fprintf(stdout, "    configuration\t%s\n", c_strs.configuration);
 
-	usbg_for_each_binding(b, c)
-		fprintf(stdout, "    %s -> %s\n", b->name,b->target->name);
+	usbg_for_each_binding(b, c) {
+		f = b->target;
+		usbg_get_function_name(f, buf2, USBG_MAX_STR_LENGTH);
+		fprintf(stdout, "    %s -> %s\n", b->name, buf2);
+	}
 }
 
 int main(void)
 {
 	usbg_state *s;
 	usbg_gadget *g;
-	struct function *f;
+	usbg_function *f;
 	usbg_config *c;
 	struct binding *b;
-	struct function *f_acm0, *f_acm1, *f_ecm;
 
 	s = usbg_init("/sys/kernel/config");
 	if (!s) {
diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index 7706a1b..d6dc7a0 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -47,6 +47,7 @@
 struct usbg_state;
 struct usbg_gadget;
 struct usbg_config;
+struct usbg_function;
 
 /**
  * @brief State of the gadget devices in the system
@@ -64,6 +65,11 @@ typedef struct usbg_gadget usbg_gadget;
 typedef struct usbg_config usbg_config;
 
 /**
+ * @brief USB function
+ */
+typedef struct usbg_function usbg_function;
+
+/**
  * @struct gadget_attrs
  * @brief USB gadget device attributes
  */
@@ -164,23 +170,6 @@ union attrs {
 };
 
 /**
- * @struct function
- * @brief USB gadget function attributes
- */
-struct function
-{
-	TAILQ_ENTRY(function) fnode;
-	usbg_gadget *parent;
-
-	char name[USBG_MAX_NAME_LENGTH];
-	char path[USBG_MAX_PATH_LENGTH];
-
-	enum function_type type;
-	union attrs attr;
-};
-
-
-/**
  * @struct binding
  * @brief Describes a binding between a USB gadget configuration
  *	  and a USB gadget function
@@ -189,7 +178,7 @@ struct binding
 {
 	TAILQ_ENTRY(binding) bnode;
 	usbg_config *parent;
-	struct function *target;
+	usbg_function *target;
 
 	char name[USBG_MAX_NAME_LENGTH];
 	char path[USBG_MAX_PATH_LENGTH];
@@ -242,7 +231,7 @@ extern usbg_gadget *usbg_get_gadget(usbg_state *s, const char *name);
  * @param name Name of the function
  * @return Pointer to function or NULL if a matching function isn't found
  */
-extern struct function *usbg_get_function(usbg_gadget *g, const char *name);
+extern usbg_function *usbg_get_function(usbg_gadget *g, const char *name);
 
 /**
  * @brief Get a configuration by name
@@ -424,7 +413,7 @@ extern void usbg_set_gadget_product(usbg_gadget *g, int lang, char *prd);
  * @param f_attrs Function attributes to be set. If NULL setting is omitted.
  * @return Pointer to function or NULL if it cannot be created
  */
-extern struct function *usbg_create_function(usbg_gadget *g, enum function_type type,
+extern usbg_function *usbg_create_function(usbg_gadget *g, enum function_type type,
 		char *instance, union attrs* f_attrs);
 
 /**
@@ -432,7 +421,7 @@ extern struct function *usbg_create_function(usbg_gadget *g, enum function_type
  * @param f Config which name length should be returned
  * @return Length of name string or -1 if error occurred.
  */
-extern size_t usbg_get_function_name_len(struct function *f);
+extern size_t usbg_get_function_name_len(usbg_function *f);
 
 /**
  * @brieg Get config name
@@ -441,7 +430,7 @@ extern size_t usbg_get_function_name_len(struct function *f);
  * @param len Length of given buffer
  * @return Pointer to destination or NULL if error occurred.
  */
-extern char *usbg_get_function_name(struct function *f, char *buf, size_t len);
+extern char *usbg_get_function_name(usbg_function *f, char *buf, size_t len);
 
 /* USB configurations allocation and configuration */
 
@@ -536,14 +525,14 @@ extern void usbg_set_config_string(usbg_config *c, int lang, char *string);
  * @param f Pointer to function
  * @return 0 on success, -1 on failure.
  */
-extern int usbg_add_config_function(usbg_config *c, char *name, struct function *f);
+extern int usbg_add_config_function(usbg_config *c, char *name, usbg_function *f);
 
 /**
  * @brief Get target function of given binding
  * @param b Binding between configuration and function
  * @return Pointer to USB function which is target for this binding
  */
-extern struct function *usbg_get_binding_target(struct binding *b);
+extern usbg_function *usbg_get_binding_target(struct binding *b);
 
 /**
  * @brief Get binding name length
@@ -611,7 +600,7 @@ extern char *usbg_get_gadget_udc(usbg_gadget* g, char *buf, size_t len);
  * @return Type of function
  * @warning Pointer to function has to be valid.
  */
-extern enum function_type usbg_get_function_type(struct function *f);
+extern enum function_type usbg_get_function_type(usbg_function *f);
 
 /**
  * @brief Get attributes of given function
@@ -619,7 +608,7 @@ extern enum function_type usbg_get_function_type(struct function *f);
  * @param f_attrs Union to be filled
  * @return Pointer to filled structure or NULL if error occurred.
  */
-extern union attrs *usbg_get_function_attrs(struct function *f,
+extern union attrs *usbg_get_function_attrs(usbg_function *f,
 		union attrs *f_attrs);
 
 /**
@@ -627,28 +616,28 @@ extern union attrs *usbg_get_function_attrs(struct function *f,
  * @param f Pointer to function
  * @param f_attrs Attributes to be set
  */
-extern void usbg_set_function_attrs(struct function *f, union attrs *f_attrs);
+extern void usbg_set_function_attrs(usbg_function *f, union attrs *f_attrs);
 
 /**
  * @brief Set USB function network device address
  * @param f Pointer to function
  * @param addr Pointer to Ethernet address
  */
-extern void usbg_set_net_dev_addr(struct function *f, struct ether_addr *addr);
+extern void usbg_set_net_dev_addr(usbg_function *f, struct ether_addr *addr);
 
 /**
  * @brief Set USB function network host address
  * @param f Pointer to function
  * @param addr Pointer to Ethernet address
  */
-extern void usbg_set_net_host_addr(struct function *f, struct ether_addr *addr);
+extern void usbg_set_net_host_addr(usbg_function *f, struct ether_addr *addr);
 
 /**
  * @brief Set USB function network qmult
  * @param f Pointer to function
  * @param qmult Queue length multiplier
  */
-extern void usbg_set_net_qmult(struct function *f, int qmult);
+extern void usbg_set_net_qmult(usbg_function *f, int qmult);
 
 /**
  * @def usbg_for_each_gadget(g, s)
@@ -700,7 +689,7 @@ extern usbg_gadget *usbg_get_first_gadget(usbg_state *s);
  * @return Pointer to function or NULL if list is empty.
  * @note Functions are sorted in strings (name) order
  */
-extern struct function *usbg_get_first_function(usbg_gadget *g);
+extern usbg_function *usbg_get_first_function(usbg_gadget *g);
 
 /**
  * @brief Get first config in config list
@@ -730,7 +719,7 @@ extern usbg_gadget *usbg_get_next_gadget(usbg_gadget *g);
  * @pram g Pointer to current function
  * @return Next function or NULL if end of list.
  */
-extern struct function *usbg_get_next_function(struct function *f);
+extern usbg_function *usbg_get_next_function(usbg_function *f);
 
 /**
  * @brief Get the next config on a list.
diff --git a/src/usbg.c b/src/usbg.c
index 467191f..afb2ffa 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -54,7 +54,7 @@ struct usbg_gadget
 
 	TAILQ_ENTRY(usbg_gadget) gnode;
 	TAILQ_HEAD(chead, usbg_config) configs;
-	TAILQ_HEAD(fhead, function) functions;
+	TAILQ_HEAD(fhead, usbg_function) functions;
 	usbg_state *parent;
 };
 
@@ -70,6 +70,18 @@ struct usbg_config
 	struct config_strs strs;
 };
 
+struct usbg_function
+{
+	TAILQ_ENTRY(usbg_function) fnode;
+	usbg_gadget *parent;
+
+	char name[USBG_MAX_NAME_LENGTH];
+	char path[USBG_MAX_PATH_LENGTH];
+
+	enum function_type type;
+	union attrs attr;
+};
+
 /**
  * @var function_names
  * @brief Name strings for supported USB function types
@@ -252,7 +264,7 @@ static inline void usbg_write_string(char *path, char *name, char *file, char *b
 	usbg_write_buf(path, name, file, buf);
 }
 
-static void usbg_parse_function_attrs(struct function *f)
+static void usbg_parse_function_attrs(usbg_function *f)
 {
 	struct ether_addr *addr;
 	char str_addr[40];
@@ -291,7 +303,7 @@ static void usbg_parse_function_attrs(struct function *f)
 
 static int usbg_parse_functions(char *path, usbg_gadget *g)
 {
-	struct function *f;
+	usbg_function *f;
 	int i, n;
 	struct dirent **dent;
 	char fpath[USBG_MAX_PATH_LENGTH];
@@ -302,7 +314,7 @@ static int usbg_parse_functions(char *path, usbg_gadget *g)
 
 	n = scandir(fpath, &dent, file_select, alphasort);
 	for (i=0; i < n; i++) {
-		f = malloc(sizeof(struct function));
+		f = malloc(sizeof(usbg_function));
 		f->parent = g;
 		strcpy(f->name, dent[i]->d_name);
 		strcpy(f->path, fpath);
@@ -339,7 +351,7 @@ static void usbg_parse_config_bindings(usbg_config *c)
 	char bpath[USBG_MAX_PATH_LENGTH];
 	usbg_gadget *g = c->parent;
 	struct binding *b;
-	struct function *f;
+	usbg_function *f;
 
 	sprintf(bpath, "%s/%s", c->path, c->name);
 
@@ -505,7 +517,7 @@ void usbg_cleanup(usbg_state *s)
 	usbg_gadget *g;
 	usbg_config *c;
 	struct binding *b;
-	struct function *f;
+	usbg_function *f;
 
 	while (!TAILQ_EMPTY(&s->gadgets)) {
 		g = TAILQ_FIRST(&s->gadgets);
@@ -552,9 +564,9 @@ usbg_gadget *usbg_get_gadget(usbg_state *s, const char *name)
 	return NULL;
 }
 
-struct function *usbg_get_function(usbg_gadget *g, const char *name)
+usbg_function *usbg_get_function(usbg_gadget *g, const char *name)
 {
-	struct function *f;
+	usbg_function *f;
 
 	TAILQ_FOREACH(f, &g->functions, fnode)
 		if (!strcmp(f->name, name))
@@ -585,7 +597,7 @@ struct binding *usbg_get_binding(usbg_config *c, const char *name)
 	return NULL;
 }
 
-struct binding *usbg_get_link_binding(usbg_config *c, struct function *f)
+struct binding *usbg_get_link_binding(usbg_config *c, usbg_function *f)
 {
 	struct binding *b;
 
@@ -875,12 +887,12 @@ void usbg_set_gadget_product(usbg_gadget *g, int lang, char *prd)
 	usbg_write_string(path, "", "product", prd);
 }
 
-struct function *usbg_create_function(usbg_gadget *g, enum function_type type,
+usbg_function *usbg_create_function(usbg_gadget *g, enum function_type type,
 		char *instance, union attrs* f_attrs)
 {
 	char fpath[USBG_MAX_PATH_LENGTH];
 	char name[USBG_MAX_STR_LENGTH];
-	struct function *f;
+	usbg_function *f;
 	int ret;
 
 	if (!g)
@@ -898,7 +910,7 @@ struct function *usbg_create_function(usbg_gadget *g, enum function_type type,
 
 	sprintf(fpath, "%s/%s/%s/%s", g->path, g->name, FUNCTIONS_DIR, name);
 
-	f = malloc(sizeof(struct function));
+	f = malloc(sizeof(usbg_function));
 	if (!f) {
 		ERRORNO("allocating function\n");
 		return NULL;
@@ -991,12 +1003,12 @@ char *usbg_get_config_name(usbg_config *c, char *buf, size_t len)
 	return c ? strncpy(buf, c->name, len) : NULL;
 }
 
-size_t usbg_get_function_name_len(struct function *f)
+size_t usbg_get_function_name_len(usbg_function *f)
 {
 	return f ? strlen(f->name): -1;
 }
 
-char *usbg_get_function_name(struct function *f, char *buf, size_t len)
+char *usbg_get_function_name(usbg_function *f, char *buf, size_t len)
 {
 	return f ? strncpy(buf, f->name, len) : NULL;
 }
@@ -1070,7 +1082,7 @@ void usbg_set_config_string(usbg_config *c, int lang, char *str)
 	usbg_write_string(path, "", "configuration", str);
 }
 
-int usbg_add_config_function(usbg_config *c, char *name, struct function *f)
+int usbg_add_config_function(usbg_config *c, char *name, usbg_function *f)
 {
 	char bpath[USBG_MAX_PATH_LENGTH];
 	char fpath[USBG_MAX_PATH_LENGTH];
@@ -1117,7 +1129,7 @@ int usbg_add_config_function(usbg_config *c, char *name, struct function *f)
 	return 0;
 }
 
-struct function *usbg_get_binding_target(struct binding* b)
+usbg_function *usbg_get_binding_target(struct binding* b)
 {
 	return b ? b->target : NULL;
 }
@@ -1168,12 +1180,12 @@ void usbg_disable_gadget(usbg_gadget *g)
  * USB function-specific attribute configuration
  */
 
-enum function_type usbg_get_function_type(struct function *f)
+enum function_type usbg_get_function_type(usbg_function *f)
 {
 	return f->type;
 }
 
-union attrs *usbg_get_function_attrs(struct function *f, union attrs *f_attrs)
+union attrs *usbg_get_function_attrs(usbg_function *f, union attrs *f_attrs)
 {
 	if (f && f_attrs) {
 		*f_attrs = f->attr;
@@ -1184,7 +1196,7 @@ union attrs *usbg_get_function_attrs(struct function *f, union attrs *f_attrs)
 	return f_attrs;
 }
 
-void usbg_set_function_attrs(struct function *f, union attrs *f_attrs)
+void usbg_set_function_attrs(usbg_function *f, union attrs *f_attrs)
 {
 	char *addr;
 
@@ -1223,7 +1235,7 @@ void usbg_set_function_attrs(struct function *f, union attrs *f_attrs)
 	}
 }
 
-void usbg_set_net_dev_addr(struct function *f, struct ether_addr *dev_addr)
+void usbg_set_net_dev_addr(usbg_function *f, struct ether_addr *dev_addr)
 {
 	char *str_addr;
 
@@ -1233,7 +1245,7 @@ void usbg_set_net_dev_addr(struct function *f, struct ether_addr *dev_addr)
 	usbg_write_string(f->path, f->name, "dev_addr", str_addr);
 }
 
-void usbg_set_net_host_addr(struct function *f, struct ether_addr *host_addr)
+void usbg_set_net_host_addr(usbg_function *f, struct ether_addr *host_addr)
 {
 	char *str_addr;
 
@@ -1243,7 +1255,7 @@ void usbg_set_net_host_addr(struct function *f, struct ether_addr *host_addr)
 	usbg_write_string(f->path, f->name, "host_addr", str_addr);
 }
 
-void usbg_set_net_qmult(struct function *f, int qmult)
+void usbg_set_net_qmult(usbg_function *f, int qmult)
 {
 	f->attr.net.qmult = qmult;
 	usbg_write_dec(f->path, f->name, "qmult", qmult);
@@ -1254,7 +1266,7 @@ usbg_gadget *usbg_get_first_gadget(usbg_state *s)
 	return s ? TAILQ_FIRST(&s->gadgets) : NULL;
 }
 
-struct function *usbg_get_first_function(usbg_gadget *g)
+usbg_function *usbg_get_first_function(usbg_gadget *g)
 {
 	return g ? TAILQ_FIRST(&g->functions) : NULL;
 }
@@ -1274,7 +1286,7 @@ usbg_gadget *usbg_get_next_gadget(usbg_gadget *g)
 	return g ? TAILQ_NEXT(g, gnode) : NULL;
 }
 
-struct function *usbg_get_next_function(struct function *f)
+usbg_function *usbg_get_next_function(usbg_function *f)
 {
 	return f ? TAILQ_NEXT(f, fnode) : NULL;
 }
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux