diff options
author | Krzysztof Opasiak <k.opasiak@samsung.com> | 2014-03-03 14:33:27 +0100 |
---|---|---|
committer | Krzysztof Opasiak <k.opasiak@samsung.com> | 2014-03-11 09:25:12 +0100 |
commit | 063507ca70086e0b40faf74212fbaf9bfc5384dc (patch) | |
tree | 952dd412d4377dbf673aed2cdfee363656f57ba1 | |
parent | bf0a163d8b747787e21bed358cade4c2e35e51c3 (diff) | |
download | libusbg-063507ca70086e0b40faf74212fbaf9bfc5384dc.tar.gz libusbg-063507ca70086e0b40faf74212fbaf9bfc5384dc.tar.bz2 libusbg-063507ca70086e0b40faf74212fbaf9bfc5384dc.zip |
libusbg: Change usbg_get_function_attrs() to return error code.
usbg_get_function_attrs() may fail due to many reasons
so it should notify the user about exact reason of failure
instead of returning only NULL pointer.
Add also USBG_ERROR_NOT_SUPPORTED to usbg_set_function_attrs().
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r-- | include/usbg/usbg.h | 5 | ||||
-rw-r--r-- | src/usbg.c | 77 |
2 files changed, 56 insertions, 26 deletions
diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h index 1bb652e..ae53d22 100644 --- a/include/usbg/usbg.h +++ b/include/usbg/usbg.h @@ -189,6 +189,7 @@ typedef enum { USBG_ERROR_EXIST = -6, USBG_ERROR_NO_DEV = -7, USBG_ERROR_BUSY = -8, + USBG_ERROR_NOT_SUPPORTED = -9, USBG_ERROR_OTHER_ERROR = -99 } usbg_error; @@ -641,9 +642,9 @@ extern usbg_function_type usbg_get_function_type(usbg_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. + * @return 0 on success usbg_error if error occurred. */ -extern usbg_function_attrs *usbg_get_function_attrs(usbg_function *f, +extern int usbg_get_function_attrs(usbg_function *f, usbg_function_attrs *f_attrs); /** @@ -365,43 +365,76 @@ static void usbg_free_state(usbg_state *s) free(s); } - -static void usbg_parse_function_attrs(usbg_function *f, +static int usbg_parse_function_net_attrs(usbg_function *f, usbg_function_attrs *f_attrs) { struct ether_addr *addr; char str_addr[40]; + int ret; + + ret = usbg_read_string(f->path, f->name, "dev_addr", str_addr); + if (ret != USBG_SUCCESS) + goto out; + + addr = ether_aton(str_addr); + if (addr) { + f_attrs->net.dev_addr = *addr; + } else { + ret = USBG_ERROR_IO; + goto out; + } + + ret = usbg_read_string(f->path, f->name, "host_addr", str_addr); + if (ret != USBG_SUCCESS) + goto out; + + addr = ether_aton(str_addr); + if (addr) { + f_attrs->net.host_addr = *addr; + } else { + ret = USBG_ERROR_IO; + goto out; + } + + ret = usbg_read_string(f->path, f->name, "ifname", f_attrs->net.ifname); + if (ret != USBG_SUCCESS) + goto out; + + ret = usbg_read_dec(f->path, f->name, "qmult", &(f_attrs->net.qmult)); + +out: + return ret; +} + +static int usbg_parse_function_attrs(usbg_function *f, + usbg_function_attrs *f_attrs) +{ + int ret; switch (f->type) { case F_SERIAL: case F_ACM: case F_OBEX: - usbg_read_dec(f->path, f->name, "port_num", &(f_attrs->serial.port_num)); + ret = usbg_read_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: - usbg_read_string(f->path, f->name, "dev_addr", str_addr); - addr = ether_aton(str_addr); - if (addr) - f_attrs->net.dev_addr = *addr; - - usbg_read_string(f->path, f->name, "host_addr", str_addr); - addr = ether_aton(str_addr); - if(addr) - f_attrs->net.host_addr = *addr; - - usbg_read_string(f->path, f->name, "ifname", f_attrs->net.ifname); - usbg_read_dec(f->path, f->name, "qmult", &(f_attrs->net.qmult)); + ret = usbg_parse_function_net_attrs(f, f_attrs); break; case F_PHONET: - usbg_read_string(f->path, f->name, "ifname", f_attrs->phonet.ifname); + ret = usbg_read_string(f->path, f->name, "ifname", + f_attrs->phonet.ifname); break; default: ERROR("Unsupported function type\n"); + ret = USBG_ERROR_NOT_SUPPORTED; } + + return ret; } static int usbg_parse_functions(char *path, usbg_gadget *g) @@ -1516,15 +1549,10 @@ usbg_function_type usbg_get_function_type(usbg_function *f) return f->type; } -usbg_function_attrs *usbg_get_function_attrs(usbg_function *f, - usbg_function_attrs *f_attrs) +int usbg_get_function_attrs(usbg_function *f, usbg_function_attrs *f_attrs) { - if (f && f_attrs) - usbg_parse_function_attrs(f, f_attrs); - else - f_attrs = NULL; - - return f_attrs; + return f && f_attrs ? usbg_parse_function_attrs(f, f_attrs) + : USBG_ERROR_INVALID_PARAM; } int usbg_set_function_net_attrs(usbg_function *f, usbg_f_net_attrs *attrs) @@ -1577,6 +1605,7 @@ int usbg_set_function_attrs(usbg_function *f, usbg_function_attrs *f_attrs) break; default: ERROR("Unsupported function type\n"); + ret = USBG_ERROR_NOT_SUPPORTED; } return ret; |