diff options
| author | chenyong <[email protected]> | 2018-08-24 20:39:10 +0800 |
|---|---|---|
| committer | chenyong <[email protected]> | 2018-08-25 16:01:31 +0800 |
| commit | 192b17751868b4aa9a95ad34bb90dcd54071e5df (patch) | |
| tree | 405f3c6ebd8c33a57eac4a0f80ef3abd3e8a18ec /components/net/sal_socket/src/sal_socket.c | |
| parent | 0d737de882bbdaaa726552f9dae19aa402eb7ce6 (diff) | |
[at][sal] Add query and unregistered protocol family functions
Diffstat (limited to 'components/net/sal_socket/src/sal_socket.c')
| -rw-r--r-- | components/net/sal_socket/src/sal_socket.c | 130 |
1 files changed, 102 insertions, 28 deletions
diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c index 71aaa2c658..920ce720c2 100644 --- a/components/net/sal_socket/src/sal_socket.c +++ b/components/net/sal_socket/src/sal_socket.c @@ -50,7 +50,7 @@ static struct rt_mutex sal_core_lock; static rt_bool_t init_ok = RT_FALSE; /** - * SAL (Socket Abstraction Layer) initialization. + * SAL (Socket Abstraction Layer) initialize. * * @return result * >= 0: initialize success @@ -64,7 +64,7 @@ int sal_init(void) } /* clean sal socket table */ - memset(&socket_table, 0, sizeof(socket_table)); + rt_memset(&socket_table, 0, sizeof(socket_table)); /* create sal socket lock */ rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO); @@ -76,12 +76,12 @@ int sal_init(void) INIT_COMPONENT_EXPORT(sal_init); /** - * this function will register the current protocol family to the global array of protocol families. + * This function will register protocol family to the global array of protocol families. * - * @param pf protocol families structure + * @param pf protocol family object * - * @return 0 : protocol families structure index - * -1 : the global array of available protocol families is full + * @return >=0 : protocol family object index + * -1 : the global array of available protocol families is full */ int sal_proto_family_register(const struct proto_family *pf) { @@ -91,6 +91,18 @@ int sal_proto_family_register(const struct proto_family *pf) /* disable interrupt */ level = rt_hw_interrupt_disable(); + /* check protocol family is already registered */ + for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++) + { + if(rt_strcmp(proto_families[idx].name, pf->name) == 0) + { + /* enable interrupt */ + rt_hw_interrupt_enable(level); + LOG_E("%s protocol family is already registered!", pf->name); + return -1; + } + } + /* find an empty protocol family entry */ for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM && proto_families[idx].create; idx++); @@ -99,10 +111,10 @@ int sal_proto_family_register(const struct proto_family *pf) { /* enable interrupt */ rt_hw_interrupt_enable(level); - return -1; } + rt_strncpy(proto_families[idx].name, pf->name, rt_strlen(pf->name)); proto_families[idx].family = pf->family; proto_families[idx].sec_family = pf->sec_family; proto_families[idx].create = pf->create; @@ -119,11 +131,62 @@ int sal_proto_family_register(const struct proto_family *pf) } /** - * this function will get socket structure by sal socket descriptor + * This function removes a previously registered protocol family object. + * + * @param pf protocol family object + * + * @return >=0 : unregister protocol family index + * -1 : unregister failed + */ +int sal_proto_family_unregister(const struct proto_family *pf) +{ + int idx = 0; + + RT_ASSERT(pf != RT_NULL); + + for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++) + { + if(rt_strcmp(proto_families[idx].name, pf->name) == 0) + { + rt_memset(&proto_families[idx], 0x00, sizeof(struct proto_family)); + + return idx; + } + } + + return -1; +} + +/** + * This function will get protocol family by name. + * + * @param name protocol family name + * + * @return protocol family object + */ +struct proto_family *sal_proto_family_find(const char *name) +{ + int idx = 0; + + RT_ASSERT(name != RT_NULL); + + for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++) + { + if (rt_strcmp(proto_families[idx].name, name) == 0) + { + return &proto_families[idx]; + } + } + + return RT_NULL; +} + +/** + * This function will get sal socket object by sal socket descriptor. * * @param socket sal socket index * - * @return socket structure of the current sal socket index + * @return sal socket object of the current sal socket index */ struct sal_socket *sal_get_socket(int socket) { @@ -145,7 +208,7 @@ struct sal_socket *sal_get_socket(int socket) } /** - * this function will lock sal socket. + * This function will lock sal socket. * * @note please don't invoke it on ISR. */ @@ -161,7 +224,7 @@ static void sal_lock(void) } /** - * this function will lock sal socket. + * This function will lock sal socket. * * @note please don't invoke it on ISR. */ @@ -171,7 +234,7 @@ static void sal_unlock(void) } /** - * this function will get protocol family structure by family type + * This function will get protocol family structure by family type * * @param family protocol family * @@ -201,17 +264,17 @@ static struct proto_family *get_proto_family(int family) } /** - * this function will initialize socket structure and set socket options + * This function will initialize sal socket object and set socket options * * @param family protocol family * @param type socket type * @param protocol transfer Protocol - * @param res socket structure address + * @param res sal socket object address * * @return 0 : socket initialize success * -1 : input the wrong family * -2 : input the wrong socket type - * -3 : get protocol family structure failed + * -3 : get protocol family object failed * -4 : set socket options failed */ static int socket_init(int family, int type, int protocol, struct sal_socket **res) @@ -234,7 +297,7 @@ static int socket_init(int family, int type, int protocol, struct sal_socket **r sock->type = type; sock->protocol = protocol; - /* get socket protocol family structure */ + /* get socket protocol family object */ if ((pf = get_proto_family(family)) == RT_NULL) { return -3; @@ -301,11 +364,6 @@ __result: } -/** - * this function will return a empty sal socket structure address - * - * @return sal socket structure address - */ static int socket_new(void) { struct sal_socket *sock; @@ -415,7 +473,7 @@ int sal_shutdown(int socket, int how) if (sock->ops->shutdown((int) sock->user_data, how) == 0) { - memset(sock, 0x00, sizeof(struct sal_socket)); + rt_memset(sock, 0x00, sizeof(struct sal_socket)); return 0; } @@ -622,7 +680,7 @@ int sal_closesocket(int socket) if (sock->ops->closesocket((int) sock->user_data) == 0) { - memset(sock, 0x00, sizeof(struct sal_socket)); + rt_memset(sock, 0x00, sizeof(struct sal_socket)); return 0; } @@ -669,12 +727,18 @@ int sal_poll(struct dfs_fd *file, struct rt_pollreq *req) struct hostent *sal_gethostbyname(const char *name) { int i; + struct hostent *hst; for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i) { if (proto_families[i].gethostbyname) { - return proto_families[i].gethostbyname(name); + hst = proto_families[i].gethostbyname(name); + if (hst != RT_NULL) + { + return hst; + } + continue; } } @@ -684,13 +748,18 @@ struct hostent *sal_gethostbyname(const char *name) int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) { - int i; + int i, res; for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i) { if (proto_families[i].gethostbyname_r) { - return proto_families[i].gethostbyname_r(name, ret, buf, buflen, result, h_errnop); + res = proto_families[i].gethostbyname_r(name, ret, buf, buflen, result, h_errnop); + if (res == 0) + { + return res; + } + continue; } } @@ -716,13 +785,18 @@ int sal_getaddrinfo(const char *nodename, const struct addrinfo *hints, struct addrinfo **res) { - int i; + int i, ret; for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i) { if (proto_families[i].getaddrinfo) { - return proto_families[i].getaddrinfo(nodename, servname, hints, res); + ret = proto_families[i].getaddrinfo(nodename, servname, hints, res); + if (ret == 0) + { + return ret; + } + continue; } } |
