Add usbg_get_function_attrs() and usbg_get_function_type()
to aviod direct access to function structure members.

Add usbg_set_function_attrs() to allow set all function\
attributes with one call.

Signed-off-by: Krzysztof Opasiak <k.opas...@samsung.com>
---
 examples/gadget-acm-ecm.c |    6 ++---
 include/usbg/usbg.h       |   30 +++++++++++++++++++--
 src/usbg.c                |   64 +++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 93 insertions(+), 7 deletions(-)

diff --git a/examples/gadget-acm-ecm.c b/examples/gadget-acm-ecm.c
index a50bf1c..b6f0c94 100644
--- a/examples/gadget-acm-ecm.c
+++ b/examples/gadget-acm-ecm.c
@@ -51,19 +51,19 @@ int main(void)
        usbg_set_gadget_manufacturer(g, LANG_US_ENG, "Foo Inc.");
        usbg_set_gadget_product(g, LANG_US_ENG, "Bar Gadget");
 
-       f_acm0 = usbg_create_function(g, F_ACM, "usb0");
+       f_acm0 = usbg_create_function(g, F_ACM, "usb0", NULL);
        if (!f_acm0) {
                fprintf(stderr, "Error creating acm0 function\n");
                goto out2;
        }
 
-       f_acm1 = usbg_create_function(g, F_ACM, "usb1");
+       f_acm1 = usbg_create_function(g, F_ACM, "usb1", NULL);
        if (!f_acm1) {
                fprintf(stderr, "Error creating acm1 function\n");
                goto out2;
        }
 
-       f_ecm = usbg_create_function(g, F_ECM, "usb0");
+       f_ecm = usbg_create_function(g, F_ECM, "usb0", NULL);
        if (!f_ecm) {
                fprintf(stderr, "Error creating ecm function\n");
                goto out2;
diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index 43bf429..bca99e2 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -441,13 +441,15 @@ extern void usbg_set_gadget_product(struct gadget *g, int 
lang, char *prd);
 /* USB function allocation and configuration */
 
 /**
- * @brief Create a new USB gadget function
+ * @brief Create a new USB gadget function and set its attributes
  * @param g Pointer to gadget
  * @param type Type of function
  * @param instance Function instance name
+ * @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(struct gadget *g, enum 
function_type type, char *instance);
+extern struct function *usbg_create_function(struct gadget *g, enum 
function_type type,
+               char *instance, union attrs* f_attrs);
 
 /* USB configurations allocation and configuration */
 
@@ -589,6 +591,30 @@ extern char *usbg_get_gadget_udc(struct gadget* g, char 
*buf, size_t len);
  */
 
 /**
+ * @brief Get type of given function
+ * @param f Pointer to function
+ * @return Type of function
+ * @warning Pointer to function has to be valid.
+ */
+extern enum function_type usbg_get_function_type(struct function *f);
+
+/**
+ * @brief Get attributes of given function
+ * @param f Pointer to function
+ * @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,
+               union attrs *f_attrs);
+
+/**
+ * @brief Set attributes of given function
+ * @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);
+
+/**
  * @brief Set USB function network device address
  * @param f Pointer to function
  * @param addr Pointer to Ethernet address
diff --git a/src/usbg.c b/src/usbg.c
index 10025bf..3f1d5a9 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -841,7 +841,8 @@ void usbg_set_gadget_product(struct gadget *g, int lang, 
char *prd)
        usbg_write_string(path, "", "product", prd);
 }
 
-struct function *usbg_create_function(struct gadget *g, enum function_type 
type, char *instance)
+struct function *usbg_create_function(struct gadget *g, enum function_type 
type,
+               char *instance, union attrs* f_attrs)
 {
        char fpath[USBG_MAX_PATH_LENGTH];
        char name[USBG_MAX_STR_LENGTH];
@@ -880,7 +881,11 @@ struct function *usbg_create_function(struct gadget *g, 
enum function_type type,
                return NULL;
        }
 
-       usbg_parse_function_attrs(f);
+       if(f_attrs) {
+               usbg_set_function_attrs(f, f_attrs);
+       } else {
+               usbg_parse_function_attrs(f);
+       }
 
        INSERT_TAILQ_STRING_ORDER(&g->functions, fhead, name, f, fnode);
 
@@ -1104,6 +1109,61 @@ void usbg_disable_gadget(struct gadget *g)
  * USB function-specific attribute configuration
  */
 
+enum function_type usbg_get_function_type(struct function *f)
+{
+       return f->type;
+}
+
+union attrs *usbg_get_function_attrs(struct function *f, union attrs *f_attrs)
+{
+       if (f && f_attrs) {
+               *f_attrs = f->attr;
+       } else {
+               f_attrs = NULL;
+       }
+
+       return f_attrs;
+}
+
+void usbg_set_function_attrs(struct function *f, union attrs *f_attrs)
+{
+       char *addr;
+
+       if(!f || !f_attrs) {
+               return;
+       }
+
+       f->attr = *f_attrs;
+
+       switch (f->type) {
+       case F_SERIAL:
+       case F_ACM:
+       case F_OBEX:
+               usbg_write_dec(f->path, f->name, "port_num", 
f_attrs->serial.port_num);
+               break;
+       case F_ECM:
+       case F_SUBSET:
+       case F_NCM:
+       case F_EEM:
+       case F_RNDIS:
+               addr = ether_ntoa(&f_attrs->net.dev_addr);
+               usbg_write_string(f->path, f->name, "dev_addr", addr);
+
+               addr = ether_ntoa(&f_attrs->net.host_addr);
+               usbg_write_string(f->path, f->name, "host_addr", addr);
+
+               usbg_write_string(f->path, f->name, "ifname", 
f_attrs->net.ifname);
+
+               usbg_write_dec(f->path, f->name, "qmult", f_attrs->net.qmult);
+               break;
+       case F_PHONET:
+               usbg_write_string(f->path, f->name, "ifname", 
f_attrs->phonet.ifname);
+               break;
+       default:
+               ERROR("Unsupported function type\n");
+       }
+}
+
 void usbg_set_net_dev_addr(struct function *f, struct ether_addr *dev_addr)
 {
        char *str_addr;
-- 
1.7.9.5

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

Reply via email to