You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2016/09/29 01:34:12 UTC
[05/49] incubator-mynewt-core git commit: directory re-org
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/mn_socket/src/arch/sim/native_itf.c
----------------------------------------------------------------------
diff --git a/sys/mn_socket/src/arch/sim/native_itf.c b/sys/mn_socket/src/arch/sim/native_itf.c
deleted file mode 100644
index 78607e7..0000000
--- a/sys/mn_socket/src/arch/sim/native_itf.c
+++ /dev/null
@@ -1,212 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <ifaddrs.h>
-#include <net/if.h>
-#include <string.h>
-#include <errno.h>
-#include <limits.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-#include "mn_socket/mn_socket.h"
-#include "native_sock_priv.h"
-
-static uint8_t
-itf_flags(int if_flags)
-{
- uint8_t flags;
-
- flags = 0;
-
- if ((if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) {
- flags |= MN_ITF_F_UP;
- }
- if (if_flags & IFF_MULTICAST) {
- flags |= MN_ITF_F_MULTICAST;
- }
- return flags;
-}
-
-int
-native_sock_itf_getnext(struct mn_itf *mi)
-{
- int prev_idx, cur_idx;
- struct ifaddrs *ifap;
- struct ifaddrs *ifa;
- int rc;
-
- if (mi->mif_name[0] == '\0') {
- prev_idx = 0;
- } else {
- prev_idx = mi->mif_idx;
- }
- mi->mif_idx = UCHAR_MAX;
- rc = getifaddrs(&ifap);
- if (rc < 0) {
- rc = native_sock_err_to_mn_err(errno);
- return rc;
- }
-
- rc = MN_ENOBUFS;
- for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
- cur_idx = if_nametoindex(ifa->ifa_name);
- if (cur_idx <= prev_idx || cur_idx >= mi->mif_idx) {
- continue;
- }
- strncpy(mi->mif_name, ifa->ifa_name, sizeof(mi->mif_name));
- mi->mif_idx = cur_idx;
- mi->mif_flags = itf_flags(ifa->ifa_flags);
- rc = 0;
- }
- freeifaddrs(ifap);
- return rc;
-}
-
-static int
-addrcmp(uint8_t fam1, void *addr1, uint8_t fam2, void *addr2, int alen)
-{
- if (fam1 != fam2) {
- return fam1 - fam2;
- }
- return memcmp(addr1, addr2, alen);
-}
-
-static int
-plen(void *addr, int alen)
-{
- int i;
- int j;
- uint8_t b;
-
- for (i = 0; i < alen; i++) {
- b = ((uint8_t *)addr)[i];
- if (b == 0xff) {
- continue;
- }
- for (j = 0; j < 7; j++) {
- if ((b & (0x80 >> j)) == 0) {
- return i * 8 + j;
- }
- }
- }
- return alen * 8;
-}
-
-int
-native_sock_itf_addr(int idx, uint32_t *addr)
-{
- struct ifaddrs *ifap;
- struct ifaddrs *ifa;
- struct sockaddr_in *sin;
- int rc;
-
- rc = getifaddrs(&ifap);
- if (rc < 0) {
- rc = native_sock_err_to_mn_err(errno);
- return rc;
- }
-
- rc = MN_EADDRNOTAVAIL;
- for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
- if (if_nametoindex(ifa->ifa_name) != idx) {
- continue;
- }
- if (ifa->ifa_addr->sa_family == AF_INET) {
- sin = (struct sockaddr_in *)ifa->ifa_addr;
- *addr = sin->sin_addr.s_addr;
- rc = 0;
- break;
- }
- }
- freeifaddrs(ifap);
- return rc;
-}
-
-int
-native_sock_itf_addr_getnext(struct mn_itf *mi, struct mn_itf_addr *mia)
-{
- struct ifaddrs *ifap;
- struct ifaddrs *ifa;
- struct sockaddr_in *sin;
- struct sockaddr_in6 *sin6;
- int rc;
- uint8_t prev_family;
- uint8_t prev_addr[16];
-
- rc = getifaddrs(&ifap);
- if (rc < 0) {
- rc = native_sock_err_to_mn_err(errno);
- return rc;
- }
-
- prev_family = mia->mifa_family;
- memcpy(prev_addr, &mia->mifa_addr, sizeof(mia->mifa_addr));
- mia->mifa_family = UCHAR_MAX;
- memset(&mia->mifa_addr, 0xff, sizeof(mia->mifa_addr));
-
- rc = MN_ENOBUFS;
-
- for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
- if (if_nametoindex(ifa->ifa_name) != mi->mif_idx) {
- continue;
- }
- if (ifa->ifa_addr->sa_family == AF_INET) {
- sin = (struct sockaddr_in *)ifa->ifa_addr;
- if (addrcmp(MN_AF_INET, &sin->sin_addr,
- prev_family, prev_addr,
- sizeof(struct in_addr)) <= 0) {
- continue;
- }
- if (addrcmp(MN_AF_INET, &sin->sin_addr,
- mia->mifa_family, &mia->mifa_addr,
- sizeof(struct in_addr)) >= 0) {
- continue;
- }
- mia->mifa_family = MN_AF_INET;
- memcpy(&mia->mifa_addr, &sin->sin_addr, sizeof(struct in_addr));
-
- sin = (struct sockaddr_in *)ifa->ifa_netmask;
- mia->mifa_plen = plen(&sin->sin_addr, sizeof(struct in_addr));
- } else if (ifa->ifa_addr->sa_family == AF_INET6) {
- sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
- if (addrcmp(MN_AF_INET6, &sin6->sin6_addr,
- prev_family, prev_addr,
- sizeof(struct in6_addr)) <= 0) {
- continue;
- }
- if (addrcmp(MN_AF_INET6, &sin6->sin6_addr,
- mia->mifa_family, &mia->mifa_addr,
- sizeof(struct in6_addr)) >= 0) {
- continue;
- }
- mia->mifa_family = MN_AF_INET6;
- memcpy(&mia->mifa_addr, &sin6->sin6_addr, sizeof(struct in6_addr));
-
- sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
- mia->mifa_plen = plen(&sin6->sin6_addr, sizeof(struct in6_addr));
- } else {
- continue;
- }
- rc = 0;
- }
- freeifaddrs(ifap);
- return rc;
-}
-
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/mn_socket/src/arch/sim/native_sock.c
----------------------------------------------------------------------
diff --git a/sys/mn_socket/src/arch/sim/native_sock.c b/sys/mn_socket/src/arch/sim/native_sock.c
deleted file mode 100644
index 819120c..0000000
--- a/sys/mn_socket/src/arch/sim/native_sock.c
+++ /dev/null
@@ -1,753 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <sys/socket.h>
-#include <unistd.h>
-#include <errno.h>
-#include <netinet/in.h>
-#include <string.h>
-#include <poll.h>
-#include <assert.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <stdio.h>
-
-#include <os/os.h>
-#include <os/os_mbuf.h>
-#include "mn_socket/mn_socket.h"
-#include "mn_socket/mn_socket_ops.h"
-
-#include "mn_socket/arch/sim/native_sock.h"
-#include "native_sock_priv.h"
-
-#define NATIVE_SOCK_MAX 8
-#define NATIVE_SOCK_MAX_UDP 2048
-#define NATIVE_SOCK_POLL_ITVL (OS_TICKS_PER_SEC / 5)
-#define SOCK_STACK_SZ 4096
-#define SOCK_PRIO 2
-
-static int native_sock_create(struct mn_socket **sp, uint8_t domain,
- uint8_t type, uint8_t proto);
-static int native_sock_close(struct mn_socket *);
-static int native_sock_connect(struct mn_socket *, struct mn_sockaddr *);
-static int native_sock_bind(struct mn_socket *, struct mn_sockaddr *);
-static int native_sock_listen(struct mn_socket *, uint8_t qlen);
-static int native_sock_sendto(struct mn_socket *, struct os_mbuf *,
- struct mn_sockaddr *);
-static int native_sock_recvfrom(struct mn_socket *, struct os_mbuf **,
- struct mn_sockaddr *);
-static int native_sock_getsockopt(struct mn_socket *, uint8_t level,
- uint8_t name, void *val);
-static int native_sock_setsockopt(struct mn_socket *, uint8_t level,
- uint8_t name, void *val);
-
-static int native_sock_getsockname(struct mn_socket *, struct mn_sockaddr *);
-static int native_sock_getpeername(struct mn_socket *, struct mn_sockaddr *);
-
-static struct native_sock {
- struct mn_socket ns_sock;
- int ns_fd;
- unsigned int ns_poll:1;
- unsigned int ns_listen:1;
- uint8_t ns_type;
- uint8_t ns_pf;
- struct os_sem ns_sem;
- STAILQ_HEAD(, os_mbuf_pkthdr) ns_rx;
- struct os_mbuf *ns_tx;
-} native_socks[NATIVE_SOCK_MAX];
-
-static struct native_sock_state {
- struct pollfd poll_fds[NATIVE_SOCK_MAX];
- int poll_fd_cnt;
- struct os_mutex mtx;
- struct os_task task;
-} native_sock_state;
-
-static const struct mn_socket_ops native_sock_ops = {
- .mso_create = native_sock_create,
- .mso_close = native_sock_close,
-
- .mso_bind = native_sock_bind,
- .mso_connect = native_sock_connect,
- .mso_listen = native_sock_listen,
-
- .mso_sendto = native_sock_sendto,
- .mso_recvfrom = native_sock_recvfrom,
-
- .mso_getsockopt = native_sock_getsockopt,
- .mso_setsockopt = native_sock_setsockopt,
-
- .mso_getsockname = native_sock_getsockname,
- .mso_getpeername = native_sock_getpeername,
-
- .mso_itf_getnext = native_sock_itf_getnext,
- .mso_itf_addr_getnext = native_sock_itf_addr_getnext
-};
-
-static struct native_sock *
-native_get_sock(void)
-{
- int i;
- struct native_sock *ns;
-
- for (i = 0; i < NATIVE_SOCK_MAX; i++) {
- if (native_socks[i].ns_fd < 0) {
- ns = &native_socks[i];
- ns->ns_poll = 0;
- ns->ns_listen = 0;
- return ns;
- }
- }
- return NULL;
-}
-
-static struct native_sock *
-native_find_sock(int fd)
-{
- int i;
-
- for (i = 0; i < NATIVE_SOCK_MAX; i++) {
- if (native_socks[i].ns_fd == fd) {
- return &native_socks[i];
- }
- }
- return NULL;
-}
-
-static void
-native_sock_poll_rebuild(struct native_sock_state *nss)
-{
- struct native_sock *ns;
- int i;
- int j;
-
- os_mutex_pend(&nss->mtx, OS_WAIT_FOREVER);
- for (i = 0, j = 0; i < NATIVE_SOCK_MAX; i++) {
- ns = &native_socks[i];
- if (ns->ns_fd < 0) {
- continue;
- }
- if (!ns->ns_poll) {
- continue;
- }
- nss->poll_fds[j].fd = ns->ns_fd;
- nss->poll_fds[j].events = POLLIN;
- nss->poll_fds[j].revents = 0;
- j++;
- }
- nss->poll_fd_cnt = j;
- os_mutex_release(&nss->mtx);
-}
-
-int
-native_sock_err_to_mn_err(int err)
-{
- switch (err) {
- case 0:
- return 0;
- case EAGAIN:
- case EINPROGRESS:
- return MN_EAGAIN;
- case ENOTCONN:
- return MN_ENOTCONN;
- case ETIMEDOUT:
- return MN_ETIMEDOUT;
- case ENOMEM:
- return MN_ENOBUFS;
- case EADDRINUSE:
- return MN_EADDRINUSE;
- case EADDRNOTAVAIL:
- return MN_EADDRNOTAVAIL;
- default:
- return MN_EINVAL;
- }
-}
-
-static int
-native_sock_mn_addr_to_addr(struct mn_sockaddr *ms, struct sockaddr *sa,
- int *sa_len)
-{
- struct sockaddr_in *sin = (struct sockaddr_in *)sa;
- struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
- struct mn_sockaddr_in *msin = (struct mn_sockaddr_in *)ms;
- struct mn_sockaddr_in6 *msin6 = (struct mn_sockaddr_in6 *)ms;
-
- switch (ms->msa_family) {
- case MN_AF_INET:
- sin->sin_family = AF_INET;
-#ifndef MN_LINUX
- sin->sin_len = sizeof(*sin);
-#endif
- sin->sin_addr.s_addr = msin->msin_addr.s_addr;
- sin->sin_port = msin->msin_port;
- *sa_len = sizeof(*sin);
- break;
- case MN_AF_INET6:
- sin6->sin6_family = AF_INET6;
-#ifndef MN_LINUX
- sin6->sin6_len = sizeof(*sin6);
-#endif
- sin6->sin6_port = msin6->msin6_port;
- sin6->sin6_flowinfo = msin6->msin6_flowinfo;
- memcpy(&sin6->sin6_addr, &msin6->msin6_addr, sizeof(msin6->msin6_addr));
- sin6->sin6_scope_id = msin6->msin6_scope_id;
- *sa_len = sizeof(*sin6);
- break;
- default:
- return MN_EPROTONOSUPPORT;
- }
- return 0;
-}
-
-static int
-native_sock_addr_to_mn_addr(struct sockaddr *sa, struct mn_sockaddr *ms)
-{
- struct mn_sockaddr_in *msin = (struct mn_sockaddr_in *)ms;
- struct mn_sockaddr_in6 *msin6 = (struct mn_sockaddr_in6 *)ms;
- struct sockaddr_in *sin = (struct sockaddr_in *)sa;
- struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
-
- switch (sa->sa_family) {
- case AF_INET:
- msin->msin_family = MN_AF_INET;
- msin->msin_len = sizeof(*msin);
- msin->msin_addr.s_addr = sin->sin_addr.s_addr;
- msin->msin_port = sin->sin_port;
- break;
- case AF_INET6:
- msin6->msin6_family = MN_AF_INET6;
- msin6->msin6_len = sizeof(*msin6);
- msin6->msin6_port = sin6->sin6_port;
- msin6->msin6_flowinfo = sin6->sin6_flowinfo;
- memcpy(&msin6->msin6_addr, &sin6->sin6_addr, sizeof(msin6->msin6_addr));
- msin6->msin6_scope_id = sin6->sin6_scope_id;
- break;
- default:
- return MN_EPROTONOSUPPORT;
- }
- return 0;
-}
-
-static int
-native_sock_create(struct mn_socket **sp, uint8_t domain,
- uint8_t type, uint8_t proto)
-{
- struct native_sock_state *nss = &native_sock_state;
- struct native_sock *ns;
- int idx;
-
- switch (domain) {
- case MN_PF_INET:
- domain = PF_INET;
- break;
- case MN_PF_INET6:
- domain = PF_INET6;
- break;
- default:
- return MN_EPROTONOSUPPORT;
- }
-
- switch (type) {
- case MN_SOCK_DGRAM:
- type = SOCK_DGRAM;
- break;
- case MN_SOCK_STREAM:
- type = SOCK_STREAM;
- break;
- case 0:
- break;
- default:
- return MN_EPROTONOSUPPORT;
- }
-
- os_mutex_pend(&nss->mtx, OS_WAIT_FOREVER);
- ns = native_get_sock();
- if (!ns) {
- os_mutex_release(&nss->mtx);
- return MN_ENOBUFS;
- }
- os_sem_init(&ns->ns_sem, 0);
- idx = socket(domain, type, proto);
- ns->ns_fd = idx;
- ns->ns_pf = domain;
- ns->ns_type = type;
- os_mutex_release(&nss->mtx);
- if (idx < 0) {
- return MN_ENOBUFS;
- }
- *sp = &ns->ns_sock;
- return 0;
-}
-
-static int
-native_sock_close(struct mn_socket *s)
-{
- struct native_sock_state *nss = &native_sock_state;
- struct native_sock *ns = (struct native_sock *)s;
- struct os_mbuf_pkthdr *m;
-
- os_mutex_pend(&nss->mtx, OS_WAIT_FOREVER);
- close(ns->ns_fd);
- ns->ns_fd = -1;
-
- /*
- * When socket is closed, we must free all mbufs which might be
- * queued in it.
- */
- while ((m = STAILQ_FIRST(&ns->ns_rx))) {
- STAILQ_REMOVE_HEAD(&ns->ns_rx, omp_next);
- os_mbuf_free_chain(OS_MBUF_PKTHDR_TO_MBUF(m));
- }
- native_sock_poll_rebuild(nss);
- os_mutex_release(&nss->mtx);
- return 0;
-}
-
-static int
-native_sock_connect(struct mn_socket *s, struct mn_sockaddr *addr)
-{
- struct native_sock_state *nss = &native_sock_state;
- struct native_sock *ns = (struct native_sock *)s;
- struct sockaddr_storage ss;
- struct sockaddr *sa = (struct sockaddr *)&ss;
- int rc;
- int sa_len;
-
- rc = native_sock_mn_addr_to_addr(addr, sa, &sa_len);
- if (rc) {
- return rc;
- }
- os_mutex_pend(&nss->mtx, OS_WAIT_FOREVER);
- if (connect(ns->ns_fd, sa, sa_len)) {
- rc = errno;
- os_mutex_release(&nss->mtx);
- return native_sock_err_to_mn_err(rc);
- }
- ns->ns_poll = 1;
- native_sock_poll_rebuild(nss);
- os_mutex_release(&nss->mtx);
- mn_socket_writable(s, 0);
- return 0;
-}
-
-static int
-native_sock_bind(struct mn_socket *s, struct mn_sockaddr *addr)
-{
- struct native_sock_state *nss = &native_sock_state;
- struct native_sock *ns = (struct native_sock *)s;
- struct sockaddr_storage ss;
- struct sockaddr *sa = (struct sockaddr *)&ss;
- int rc;
- int sa_len;
- int val = 1;
-
- rc = native_sock_mn_addr_to_addr(addr, sa, &sa_len);
- if (rc) {
- return rc;
- }
-
- os_mutex_pend(&nss->mtx, OS_WAIT_FOREVER);
- if (ns->ns_type == SOCK_STREAM) {
- rc = setsockopt(ns->ns_fd, SOL_SOCKET, SO_REUSEADDR, &val,
- sizeof(val));
- if (rc) {
- goto err;
- }
- }
- rc = ioctl(ns->ns_fd, FIONBIO, (char *)&val);
- if (rc) {
- goto err;
- }
- if (bind(ns->ns_fd, sa, sa_len)) {
- goto err;
- }
- if (ns->ns_type == SOCK_DGRAM) {
- ns->ns_poll = 1;
- native_sock_poll_rebuild(nss);
- }
- os_mutex_release(&nss->mtx);
- return 0;
-err:
- rc = errno;
- os_mutex_release(&nss->mtx);
- return native_sock_err_to_mn_err(rc);
-}
-
-static int
-native_sock_listen(struct mn_socket *s, uint8_t qlen)
-{
- struct native_sock_state *nss = &native_sock_state;
- struct native_sock *ns = (struct native_sock *)s;
- int rc;
-
- os_mutex_pend(&nss->mtx, OS_WAIT_FOREVER);
- if (listen(ns->ns_fd, qlen)) {
- rc = errno;
- os_mutex_release(&nss->mtx);
- return native_sock_err_to_mn_err(rc);
- }
- ns->ns_poll = 1;
- ns->ns_listen = 1;
- native_sock_poll_rebuild(nss);
- os_mutex_release(&nss->mtx);
- return 0;
-}
-
-/*
- * TX routine for stream sockets (TCP). The data to send is pointed
- * by ns_tx.
- * Keep sending mbufs until socket says that it can't take anymore.
- * then wait for send event notification before continuing.
- */
-static int
-native_sock_stream_tx(struct native_sock *ns, int notify)
-{
- struct native_sock_state *nss = &native_sock_state;
- struct os_mbuf *m;
- struct os_mbuf *n;
- int rc;
-
- rc = 0;
-
- os_mutex_pend(&nss->mtx, OS_TIMEOUT_NEVER);
- while (ns->ns_tx && rc == 0) {
- m = ns->ns_tx;
- n = SLIST_NEXT(m, om_next);
- rc = write(ns->ns_fd, m->om_data, m->om_len);
- if (rc == m->om_len) {
- ns->ns_tx = n;
- os_mbuf_free(m);
- rc = 0;
- } else {
- rc = errno;
- }
- }
- os_mutex_release(&nss->mtx);
- if (rc) {
- if (rc == EAGAIN) {
- rc = 0;
- } else {
- rc = native_sock_err_to_mn_err(rc);
- }
- }
- if (notify) {
- if (ns->ns_tx == NULL) {
- mn_socket_writable(&ns->ns_sock, 0);
- } else {
- mn_socket_writable(&ns->ns_sock, rc);
- }
- }
- return rc;
-}
-
-static int
-native_sock_sendto(struct mn_socket *s, struct os_mbuf *m,
- struct mn_sockaddr *addr)
-{
- struct native_sock *ns = (struct native_sock *)s;
- struct sockaddr_storage ss;
- struct sockaddr *sa = (struct sockaddr *)&ss;
- uint8_t tmpbuf[NATIVE_SOCK_MAX_UDP];
- struct os_mbuf *o;
- int sa_len;
- int off;
- int rc;
-
- if (ns->ns_type == SOCK_DGRAM) {
- rc = native_sock_mn_addr_to_addr(addr, sa, &sa_len);
- if (rc) {
- return rc;
- }
- off = 0;
- for (o = m; o; o = SLIST_NEXT(o, om_next)) {
- if (off + o->om_len > sizeof(tmpbuf)) {
- return MN_ENOBUFS;
- }
- os_mbuf_copydata(o, 0, o->om_len, &tmpbuf[off]);
- off += o->om_len;
- }
- rc = sendto(ns->ns_fd, tmpbuf, off, 0, sa, sa_len);
- if (rc != off) {
- return native_sock_err_to_mn_err(errno);
- }
- os_mbuf_free_chain(m);
- return 0;
- } else {
- if (ns->ns_tx) {
- return MN_EAGAIN;
- }
- ns->ns_tx = m;
-
- rc = native_sock_stream_tx(ns, 0);
- return rc;
- }
-}
-
-static int
-native_sock_recvfrom(struct mn_socket *s, struct os_mbuf **mp,
- struct mn_sockaddr *addr)
-{
- struct native_sock *ns = (struct native_sock *)s;
- struct sockaddr_storage ss;
- struct sockaddr *sa = (struct sockaddr *)&ss;
- uint8_t tmpbuf[NATIVE_SOCK_MAX_UDP];
- struct os_mbuf *m;
- socklen_t slen;
- int rc;
-
- slen = sizeof(ss);
- if (ns->ns_type == SOCK_DGRAM) {
- rc = recvfrom(ns->ns_fd, tmpbuf, sizeof(tmpbuf), 0, sa, &slen);
- } else {
- rc = getpeername(ns->ns_fd, sa, &slen);
- if (rc == 0) {
- rc = read(ns->ns_fd, tmpbuf, sizeof(tmpbuf));
- }
- }
- if (rc < 0) {
- return native_sock_err_to_mn_err(errno);
- }
- if (ns->ns_type == SOCK_STREAM && rc == 0) {
- mn_socket_readable(&ns->ns_sock, MN_ECONNABORTED);
- ns->ns_poll = 0;
- native_sock_poll_rebuild(&native_sock_state);
- return MN_ECONNABORTED;
- }
-
- m = os_msys_get_pkthdr(rc, 0);
- if (!m) {
- return MN_ENOBUFS;
- }
- os_mbuf_copyinto(m, 0, tmpbuf, rc);
- *mp = m;
- if (addr) {
- native_sock_addr_to_mn_addr(sa, addr);
- }
- return 0;
-}
-
-static int
-native_sock_getsockopt(struct mn_socket *s, uint8_t level, uint8_t name,
- void *val)
-{
- return MN_EPROTONOSUPPORT;
-}
-
-static int
-native_sock_setsockopt(struct mn_socket *s, uint8_t level, uint8_t name,
- void *val)
-{
- struct native_sock *ns = (struct native_sock *)s;
- int rc;
- uint32_t val32;
- struct group_req greq;
- struct sockaddr_in *sin;
- struct sockaddr_in6 *sin6;
- struct mn_mreq *mreq;
-
- if (level == MN_SO_LEVEL) {
- switch (name) {
- case MN_MCAST_JOIN_GROUP:
- case MN_MCAST_LEAVE_GROUP:
- mreq = val;
- memset(&greq, 0, sizeof(greq));
- greq.gr_interface = mreq->mm_idx;
- if (mreq->mm_family == MN_AF_INET) {
- sin = (struct sockaddr_in *)&greq.gr_group;
-#ifndef MN_LINUX
- sin->sin_len = sizeof(*sin);
-#endif
- sin->sin_family = AF_INET;
- memcpy(&sin->sin_addr, &mreq->mm_addr, sizeof(struct in_addr));
- level = IPPROTO_IP;
- } else {
- sin6 = (struct sockaddr_in6 *)&greq.gr_group;
-#ifndef MN_LINUX
- sin6->sin6_len = sizeof(*sin6);
-#endif
- sin6->sin6_family = AF_INET6;
- memcpy(&sin6->sin6_addr, &mreq->mm_addr,
- sizeof(struct in6_addr));
- level = IPPROTO_IPV6;
- }
-
- if (name == MN_MCAST_JOIN_GROUP) {
- name = MCAST_JOIN_GROUP;
- } else {
- name = MCAST_LEAVE_GROUP;
- }
- rc = setsockopt(ns->ns_fd, level, name, &greq, sizeof(greq));
- if (rc) {
- return native_sock_err_to_mn_err(errno);
- }
- return 0;
- case MN_MCAST_IF:
- if (ns->ns_pf == AF_INET) {
- level = IPPROTO_IP;
- name = IP_MULTICAST_IF;
- rc = native_sock_itf_addr(*(int *)val, &val32);
- if (rc) {
- return rc;
- }
- } else {
- level = IPPROTO_IPV6;
- name = IPV6_MULTICAST_IF;
- val32 = *(uint32_t *)val;
- }
- rc = setsockopt(ns->ns_fd, level, name, &val32, sizeof(val32));
- if (rc) {
- return native_sock_err_to_mn_err(errno);
- }
- return 0;
- }
- }
- return MN_EPROTONOSUPPORT;
-}
-
-static int
-native_sock_getsockname(struct mn_socket *s, struct mn_sockaddr *addr)
-{
- struct native_sock *ns = (struct native_sock *)s;
- struct sockaddr_storage ss;
- struct sockaddr *sa = (struct sockaddr *)&ss;
- socklen_t len;
- int rc;
-
- len = sizeof(struct sockaddr_storage);
- rc = getsockname(ns->ns_fd, sa, &len);
- if (rc) {
- return native_sock_err_to_mn_err(errno);
- }
- rc = native_sock_addr_to_mn_addr(sa, addr);
- if (rc) {
- return rc;
- }
- return 0;
-}
-
-static int
-native_sock_getpeername(struct mn_socket *s, struct mn_sockaddr *addr)
-{
- struct native_sock *ns = (struct native_sock *)s;
- struct sockaddr_storage ss;
- struct sockaddr *sa = (struct sockaddr *)&ss;
- socklen_t len;
- int rc;
-
- len = sizeof(struct sockaddr_storage);
- rc = getpeername(ns->ns_fd, sa, &len);
- if (rc) {
- return native_sock_err_to_mn_err(errno);
- }
- rc = native_sock_addr_to_mn_addr(sa, addr);
- if (rc) {
- return rc;
- }
- return 0;
-}
-
-/*
- * XXX should do this task with SIGIO as well.
- */
-static void
-socket_task(void *arg)
-{
- struct native_sock_state *nss = arg;
- struct native_sock *ns, *new_ns;
- struct sockaddr_storage ss;
- struct sockaddr *sa = (struct sockaddr *)&ss;
- int i;
- socklen_t slen;
- int rc;
-
- os_mutex_pend(&nss->mtx, OS_WAIT_FOREVER);
- while (1) {
- os_mutex_release(&nss->mtx);
- os_time_delay(NATIVE_SOCK_POLL_ITVL);
- os_mutex_pend(&nss->mtx, OS_WAIT_FOREVER);
- if (nss->poll_fd_cnt) {
- rc = poll(nss->poll_fds, nss->poll_fd_cnt, 0);
- } else {
- rc = 0;
- }
- if (rc == 0) {
- continue;
- }
- for (i = 0; i < nss->poll_fd_cnt; i++) {
- if (!nss->poll_fds[i].revents) {
- continue;
- }
- nss->poll_fds[i].revents = 0;
- ns = native_find_sock(nss->poll_fds[i].fd);
- assert(ns);
- if (ns->ns_listen) {
- new_ns = native_get_sock();
- if (!new_ns) {
- continue;
- }
- slen = sizeof(ss);
- new_ns->ns_fd = accept(ns->ns_fd, sa, &slen);
- if (new_ns->ns_fd < 0) {
- continue;
- }
- new_ns->ns_type = ns->ns_type;
- new_ns->ns_sock.ms_ops = &native_sock_ops;
- os_mutex_release(&nss->mtx);
- if (mn_socket_newconn(&ns->ns_sock, &new_ns->ns_sock)) {
- /*
- * should close
- */
- }
- os_mutex_pend(&nss->mtx, OS_WAIT_FOREVER);
- new_ns->ns_poll = 1;
- native_sock_poll_rebuild(nss);
- } else {
- mn_socket_readable(&ns->ns_sock, 0);
- }
- }
- }
-}
-
-int
-native_sock_init(void)
-{
- struct native_sock_state *nss = &native_sock_state;
- int i;
- os_stack_t *sp;
-
- for (i = 0; i < NATIVE_SOCK_MAX; i++) {
- native_socks[i].ns_fd = -1;
- STAILQ_INIT(&native_socks[i].ns_rx);
- }
- sp = malloc(sizeof(os_stack_t) * SOCK_STACK_SZ);
- if (!sp) {
- return -1;
- }
- os_mutex_init(&nss->mtx);
- i = os_task_init(&nss->task, "socket", socket_task, &native_sock_state,
- SOCK_PRIO, OS_WAIT_FOREVER, sp, SOCK_STACK_SZ);
- if (i) {
- return -1;
- }
- i = mn_socket_ops_reg(&native_sock_ops);
- if (i) {
- return -1;
- }
-
- return 0;
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/mn_socket/src/arch/sim/native_sock_priv.h
----------------------------------------------------------------------
diff --git a/sys/mn_socket/src/arch/sim/native_sock_priv.h b/sys/mn_socket/src/arch/sim/native_sock_priv.h
deleted file mode 100644
index 108cbeb..0000000
--- a/sys/mn_socket/src/arch/sim/native_sock_priv.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef __NATIVE_SOCK_PRIV_H_
-#define __NATIVE_SOCK_PRIV_H_
-
-struct mn_itf;
-struct mn_itf_addr;
-
-int native_sock_itf_getnext(struct mn_itf *);
-int native_sock_itf_addr_getnext(struct mn_itf *, struct mn_itf_addr *);
-int native_sock_itf_addr(int idx, uint32_t *addr);
-
-int native_sock_err_to_mn_err(int err);
-
-#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/mn_socket/src/mn_socket.c
----------------------------------------------------------------------
diff --git a/sys/mn_socket/src/mn_socket.c b/sys/mn_socket/src/mn_socket.c
deleted file mode 100644
index 1bf2b4c..0000000
--- a/sys/mn_socket/src/mn_socket.c
+++ /dev/null
@@ -1,138 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <inttypes.h>
-#include <assert.h>
-
-#include <os/os.h>
-
-#include "mn_socket/mn_socket.h"
-#include "mn_socket/mn_socket_ops.h"
-
-/*
- * Currently there can be just one provider of sockets.
- */
-static const struct mn_socket_ops *mn_sock_tgt;
-
-/** All zeros. */
-const uint32_t nm_in6addr_any[4];
-
-int
-mn_socket_ops_reg(const struct mn_socket_ops *ops)
-{
- if (mn_sock_tgt) {
- /*
- * XXXX for now.
- */
- return -1;
- }
- mn_sock_tgt = ops;
- return 0;
-}
-
-int
-mn_socket(struct mn_socket **sp, uint8_t domain, uint8_t type, uint8_t proto)
-{
- int rc;
-
- *sp = NULL;
- /*
- * XXX Look up where socket should go.
- */
- if (!mn_sock_tgt) {
- return MN_EINVAL;
- }
- rc = mn_sock_tgt->mso_create(sp, domain, type, proto);
- if (*sp) {
- (*sp)->ms_ops = mn_sock_tgt;
- }
- return rc;
-}
-
-int
-mn_bind(struct mn_socket *s, struct mn_sockaddr *addr)
-{
- return s->ms_ops->mso_bind(s, addr);
-}
-
-int
-mn_connect(struct mn_socket *s, struct mn_sockaddr *addr)
-{
- return s->ms_ops->mso_connect(s, addr);
-}
-
-int
-mn_listen(struct mn_socket *s, uint8_t qlen)
-{
- return s->ms_ops->mso_listen(s, qlen);
-}
-
-int
-mn_recvfrom(struct mn_socket *s, struct os_mbuf **mp, struct mn_sockaddr *from)
-{
- return s->ms_ops->mso_recvfrom(s, mp, from);
-}
-
-int
-mn_sendto(struct mn_socket *s, struct os_mbuf *m, struct mn_sockaddr *to)
-{
- return s->ms_ops->mso_sendto(s, m, to);
-}
-
-int
-mn_getsockopt(struct mn_socket *s, uint8_t level, uint8_t name, void *val)
-{
- return s->ms_ops->mso_getsockopt(s, level, name, val);
-}
-
-int
-mn_setsockopt(struct mn_socket *s, uint8_t level, uint8_t name, void *val)
-{
- return s->ms_ops->mso_setsockopt(s, level, name, val);
-}
-
-int
-mn_getsockname(struct mn_socket *s, struct mn_sockaddr *addr)
-{
- return s->ms_ops->mso_getsockname(s, addr);
-}
-
-int
-mn_getpeername(struct mn_socket *s, struct mn_sockaddr *addr)
-{
- return s->ms_ops->mso_getpeername(s, addr);
-}
-
-int
-mn_close(struct mn_socket *s)
-{
- return s->ms_ops->mso_close(s);
-}
-
-int
-mn_itf_getnext(struct mn_itf *mi)
-{
- return mn_sock_tgt->mso_itf_getnext(mi);
-}
-
-int
-mn_itf_addr_getnext(struct mn_itf *mi, struct mn_itf_addr *mia)
-{
- return mn_sock_tgt->mso_itf_addr_getnext(mi, mia);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/mn_socket/src/mn_socket_aconv.c
----------------------------------------------------------------------
diff --git a/sys/mn_socket/src/mn_socket_aconv.c b/sys/mn_socket/src/mn_socket_aconv.c
deleted file mode 100644
index 7e48d65..0000000
--- a/sys/mn_socket/src/mn_socket_aconv.c
+++ /dev/null
@@ -1,98 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <ctype.h>
-#include <stdio.h>
-#include <os/endian.h>
-#include "mn_socket/mn_socket.h"
-
-int
-mn_inet_pton(int af, const char *src, void *dst)
-{
- const char *ch_src;
- uint8_t *ch_tgt;
- int val;
- int cnt;
-
- if (af == MN_PF_INET) {
- cnt = 0;
- ch_tgt = dst;
- val = 0;
- for (ch_src = src; *ch_src; ch_src++) {
- if (cnt > 4) {
- return 0;
- }
- if (isdigit(*ch_src)) {
- val = val * 10 + *ch_src - '0';
- if (val > 255) {
- return 0;
- }
- *ch_tgt = val;
- } else if (*ch_src == '.') {
- ++cnt;
- val = 0;
- ch_tgt++;
- } else {
- return 0;
- }
- }
- return 1;
- } else {
- /*
- * Add code here. XXX
- */
- return 0;
- }
-}
-
-const char *
-mn_inet_ntop(int af, const void *src_v, void *dst, int len)
-{
- const unsigned char *src = src_v;
- const struct mn_in6_addr *a6;
- int rc;
- int i;
-
- if (af == MN_PF_INET) {
- rc = snprintf(dst, len, "%u.%u.%u.%u",
- src[0], src[1], src[2], src[3]);
- if (rc >= len) {
- return NULL;
- } else {
- return dst;
- }
- } else {
- a6 = src_v;
- rc = 0;
-
- for (i = 0; i < sizeof(*a6); i += 2) {
- rc += snprintf(dst + rc, len - rc, "%x",
- htons(*(uint16_t *)&a6->s_addr[i]));
- if (rc >= len) {
- return NULL;
- }
- if (i < sizeof(*a6) - 2) {
- rc += snprintf(dst + rc, len - rc, ":");
- if (rc >= len) {
- return NULL;
- }
- }
- }
- return dst;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/mn_socket/test/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/mn_socket/test/pkg.yml b/sys/mn_socket/test/pkg.yml
deleted file mode 100644
index b46d24c..0000000
--- a/sys/mn_socket/test/pkg.yml
+++ /dev/null
@@ -1,30 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-pkg.name: sys/mn_socket/test
-pkg.type: unittest
-pkg.description: "Mynewt socket unit tests."
-pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-
-pkg.deps:
- - libs/testutil
- - sys/mn_socket
-
-pkg.deps.SELFTEST:
- - libs/console/stub
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/mn_socket/test/src/mn_sock_test.c
----------------------------------------------------------------------
diff --git a/sys/mn_socket/test/src/mn_sock_test.c b/sys/mn_socket/test/src/mn_sock_test.c
deleted file mode 100644
index 39acd2b..0000000
--- a/sys/mn_socket/test/src/mn_sock_test.c
+++ /dev/null
@@ -1,895 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <stdio.h>
-#include <string.h>
-
-#include "syscfg/syscfg.h"
-#include "os/os.h"
-#include "testutil/testutil.h"
-
-#include "mn_socket/mn_socket.h"
-#include "mn_socket/arch/sim/native_sock.h"
-
-#define TEST_STACK_SIZE 4096
-#define TEST_PRIO 22
-static os_stack_t test_stack[OS_STACK_ALIGN(TEST_STACK_SIZE)];
-static struct os_task test_task;
-
-static struct os_sem test_sem;
-
-#define MB_CNT 10
-#define MB_SZ 512
-static uint8_t test_mbuf_area[MB_CNT * MB_SZ];
-static struct os_mempool test_mbuf_mpool;
-static struct os_mbuf_pool test_mbuf_pool;
-
-TEST_CASE(inet_pton_test)
-{
- int rc;
- uint8_t addr[8];
- struct test_vec {
- char *str;
- uint8_t cmp[4];
- };
- struct test_vec ok_vec[] = {
- { "1.1.1.1", { 1, 1, 1, 1 } },
- { "1.2.3.4", { 1, 2, 3, 4 } },
- { "010.001.255.255", { 10, 1, 255, 255 } },
- { "001.002.005.006", { 1, 2, 5, 6 } }
- };
- struct test_vec invalid_vec[] = {
- { "a.b.c.d" },
- { "1a.b3.4.2" },
- { "1.3.4.2a" },
- { "1111.3.4.2" },
- { "3.256.1.0" },
- };
- int i;
-
- for (i = 0; i < sizeof(ok_vec) / sizeof(ok_vec[0]); i++) {
- memset(addr, 0xa5, sizeof(addr));
- rc = mn_inet_pton(MN_PF_INET, ok_vec[i].str, addr);
- TEST_ASSERT(rc == 1);
- TEST_ASSERT(!memcmp(ok_vec[i].cmp, addr, sizeof(uint32_t)));
- TEST_ASSERT(addr[5] == 0xa5);
- }
- for (i = 0; i < sizeof(invalid_vec) / sizeof(invalid_vec[0]); i++) {
- rc = mn_inet_pton(MN_PF_INET, invalid_vec[i].str, addr);
- TEST_ASSERT(rc == 0);
- }
-}
-
-TEST_CASE(inet_ntop_test)
-{
- const char *rstr;
- char addr[48];
- struct test_vec {
- char *str;
- uint8_t cmp[4];
- };
- struct test_vec ok_vec[] = {
- { "1.1.1.1", { 1, 1, 1, 1 } },
- { "1.2.3.4", { 1, 2, 3, 4 } },
- { "255.1.255.255", { 255, 1, 255, 255 } },
- { "1.2.5.6", { 1, 2, 5, 6 } }
- };
- int i;
-
- for (i = 0; i < sizeof(ok_vec) / sizeof(ok_vec[0]); i++) {
- memset(addr, 0xa5, sizeof(addr));
- rstr = mn_inet_ntop(MN_PF_INET, ok_vec[i].cmp, addr, sizeof(addr));
- TEST_ASSERT(rstr);
- TEST_ASSERT(!strcmp(ok_vec[i].str, addr));
- }
- rstr = mn_inet_ntop(MN_PF_INET, ok_vec[0].cmp, addr, 1);
- TEST_ASSERT(rstr == NULL);
-
- /* does not have space to null terminate */
- rstr = mn_inet_ntop(MN_PF_INET, ok_vec[0].cmp, addr, 7);
- TEST_ASSERT(rstr == NULL);
-}
-
-void
-sock_open_close(void)
-{
- struct mn_socket *sock;
- int rc;
-
- rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(sock);
- TEST_ASSERT(rc == 0);
- mn_close(sock);
-
- rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_STREAM, 0);
- TEST_ASSERT(sock);
- TEST_ASSERT(rc == 0);
- mn_close(sock);
-
- rc = mn_socket(&sock, MN_PF_INET6, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(sock);
- TEST_ASSERT(rc == 0);
- mn_close(sock);
-
- rc = mn_socket(&sock, MN_PF_INET6, MN_SOCK_STREAM, 0);
- TEST_ASSERT(sock);
- TEST_ASSERT(rc == 0);
- mn_close(sock);
-}
-
-void
-sock_listen(void)
-{
- struct mn_socket *sock;
- struct mn_sockaddr_in msin;
- int rc;
-
- rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_STREAM, 0);
- TEST_ASSERT(rc == 0);
-
- msin.msin_family = MN_PF_INET;
- msin.msin_len = sizeof(msin);
- msin.msin_port = htons(12444);
-
- mn_inet_pton(MN_PF_INET, "127.0.0.1", &msin.msin_addr);
-
- rc = mn_bind(sock, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- rc = mn_listen(sock, 2);
- TEST_ASSERT(rc == 0);
-
- mn_close(sock);
-}
-
-void
-stc_writable(void *cb_arg, int err)
-{
- int *i;
-
- TEST_ASSERT(err == 0);
- i = (int *)cb_arg;
- *i = *i + 1;
-}
-
-int
-stc_newconn(void *cb_arg, struct mn_socket *new)
-{
- struct mn_socket **r_sock;
-
- r_sock = cb_arg;
- *r_sock = new;
-
- os_sem_release(&test_sem);
- return 0;
-}
-
-void
-sock_tcp_connect(void)
-{
- struct mn_socket *listen_sock;
- struct mn_socket *sock;
- struct mn_sockaddr_in msin;
- struct mn_sockaddr_in msin2;
- int rc;
- union mn_socket_cb listen_cbs = {
- .listen.newconn = stc_newconn,
- };
- union mn_socket_cb sock_cbs = {
- .socket.writable = stc_writable
- };
- int connected = 0;
- struct mn_socket *new_sock = NULL;
-
- rc = mn_socket(&listen_sock, MN_PF_INET, MN_SOCK_STREAM, 0);
- TEST_ASSERT(rc == 0);
-
- msin.msin_family = MN_PF_INET;
- msin.msin_len = sizeof(msin);
- msin.msin_port = htons(12445);
-
- mn_inet_pton(MN_PF_INET, "127.0.0.1", &msin.msin_addr);
-
- mn_socket_set_cbs(listen_sock, &new_sock, &listen_cbs);
- rc = mn_bind(listen_sock, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- rc = mn_listen(listen_sock, 2);
- TEST_ASSERT(rc == 0);
-
- rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_STREAM, 0);
- TEST_ASSERT(rc == 0);
-
- mn_socket_set_cbs(sock, &connected, &sock_cbs);
-
- rc = mn_connect(sock, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(connected == 1);
- TEST_ASSERT(new_sock != NULL);
-
- /*
- * Check endpoint data matches
- */
- rc = mn_getsockname(sock, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
- rc = mn_getpeername(new_sock, (struct mn_sockaddr *)&msin2);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(!memcmp(&msin, &msin2, sizeof(msin)));
-
- rc = mn_getsockname(new_sock, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
- rc = mn_getpeername(sock, (struct mn_sockaddr *)&msin2);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(!memcmp(&msin, &msin2, sizeof(msin)));
-
-
- if (new_sock) {
- mn_close(new_sock);
- }
- mn_close(sock);
- mn_close(listen_sock);
-}
-
-void
-sud_readable(void *cb_arg, int err)
-{
- os_sem_release(&test_sem);
-}
-
-void
-sock_udp_data(void)
-{
- struct mn_socket *sock1;
- struct mn_socket *sock2;
- struct mn_sockaddr_in msin;
- struct mn_sockaddr_in msin2;
- int rc;
- union mn_socket_cb sock_cbs = {
- .socket.readable = sud_readable
- };
- struct os_mbuf *m;
- char data[] = "1234567890";
-
- rc = mn_socket(&sock1, MN_PF_INET, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(rc == 0);
- mn_socket_set_cbs(sock1, NULL, &sock_cbs);
-
- rc = mn_socket(&sock2, MN_PF_INET, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(rc == 0);
- mn_socket_set_cbs(sock2, NULL, &sock_cbs);
-
- msin.msin_family = MN_PF_INET;
- msin.msin_len = sizeof(msin);
- msin.msin_port = htons(12445);
-
- mn_inet_pton(MN_PF_INET, "127.0.0.1", &msin.msin_addr);
-
- rc = mn_bind(sock1, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- msin2.msin_family = MN_PF_INET;
- msin2.msin_len = sizeof(msin2);
- msin2.msin_port = 0;
- msin2.msin_addr.s_addr = 0;
- rc = mn_bind(sock2, (struct mn_sockaddr *)&msin2);
- TEST_ASSERT(rc == 0);
-
- m = os_msys_get(sizeof(data), 0);
- TEST_ASSERT(m);
- rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
- TEST_ASSERT(rc == 0);
- rc = mn_sendto(sock2, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- /*
- * Wait for the packet.
- */
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == 0);
-
- rc = mn_recvfrom(sock1, &m, (struct mn_sockaddr *)&msin2);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(m != NULL);
- TEST_ASSERT(msin2.msin_family == MN_AF_INET);
- TEST_ASSERT(msin2.msin_len == sizeof(msin2));
- TEST_ASSERT(msin2.msin_port != 0);
- TEST_ASSERT(msin2.msin_addr.s_addr != 0);
-
- if (m) {
- TEST_ASSERT(OS_MBUF_IS_PKTHDR(m));
- TEST_ASSERT(OS_MBUF_PKTLEN(m) == sizeof(data));
- TEST_ASSERT(m->om_len == sizeof(data));
- TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
- }
-
- rc = mn_sendto(sock1, m, (struct mn_sockaddr *)&msin2);
- TEST_ASSERT(rc == 0);
-
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == 0);
-
- rc = mn_recvfrom(sock2, &m, (struct mn_sockaddr *)&msin2);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(m != NULL);
- if (m) {
- TEST_ASSERT(OS_MBUF_IS_PKTHDR(m));
- TEST_ASSERT(OS_MBUF_PKTLEN(m) == sizeof(data));
- TEST_ASSERT(m->om_len == sizeof(data));
- TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
- os_mbuf_free_chain(m);
- }
-
- mn_close(sock1);
- mn_close(sock2);
-}
-
-void
-std_writable(void *cb_arg, int err)
-{
- int *i;
-
- TEST_ASSERT(err == 0);
- i = (int *)cb_arg;
- if (i) {
- *i = *i + 1;
- }
-}
-
-void
-std_readable(void *cb_arg, int err)
-{
- os_sem_release(&test_sem);
-}
-
-static union mn_socket_cb sud_sock_cbs = {
- .socket.writable = std_writable,
- .socket.readable = std_readable
-};
-
-int
-std_newconn(void *cb_arg, struct mn_socket *new)
-{
- struct mn_socket **r_sock;
-
- r_sock = cb_arg;
- *r_sock = new;
-
- mn_socket_set_cbs(new, NULL, &sud_sock_cbs);
-
- os_sem_release(&test_sem);
- return 0;
-}
-
-void
-sock_tcp_data(void)
-{
- struct mn_socket *listen_sock;
- struct mn_socket *sock;
- struct mn_sockaddr_in msin;
- int rc;
- union mn_socket_cb listen_cbs = {
- .listen.newconn = std_newconn,
- };
- int connected = 0;
- struct mn_socket *new_sock = NULL;
- struct os_mbuf *m;
- char data[] = "1234567890";
-
- rc = mn_socket(&listen_sock, MN_PF_INET, MN_SOCK_STREAM, 0);
- TEST_ASSERT(rc == 0);
-
- msin.msin_family = MN_PF_INET;
- msin.msin_len = sizeof(msin);
- msin.msin_port = htons(12447);
-
- mn_inet_pton(MN_PF_INET, "127.0.0.1", &msin.msin_addr);
-
- mn_socket_set_cbs(listen_sock, &new_sock, &listen_cbs);
- rc = mn_bind(listen_sock, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- rc = mn_listen(listen_sock, 2);
- TEST_ASSERT(rc == 0);
-
- rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_STREAM, 0);
- TEST_ASSERT(rc == 0);
-
- mn_socket_set_cbs(sock, &connected, &sud_sock_cbs);
-
- rc = mn_connect(sock, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(connected == 1);
- TEST_ASSERT(new_sock != NULL);
-
- m = os_msys_get(sizeof(data), 0);
- TEST_ASSERT(m);
- rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
- TEST_ASSERT(rc == 0);
- rc = mn_sendto(new_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- /*
- * Wait for the packet.
- */
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == 0);
-
- memset(&msin, 0, sizeof(msin));
- rc = mn_recvfrom(sock, &m, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(m != NULL);
- TEST_ASSERT(msin.msin_family == MN_AF_INET);
- TEST_ASSERT(msin.msin_len == sizeof(msin));
- TEST_ASSERT(msin.msin_port != 0);
- TEST_ASSERT(msin.msin_addr.s_addr != 0);
- os_mbuf_free_chain(m);
-
- if (new_sock) {
- mn_close(new_sock);
- }
- mn_close(sock);
- mn_close(listen_sock);
-}
-
-void
-sock_itf_list(void)
-{
- struct mn_itf itf;
- struct mn_itf_addr itf_addr;
- int if_cnt = 0;
- int seen_127;
- struct mn_in_addr addr127;
- char addr_str[64];
- int rc;
-
- mn_inet_pton(MN_PF_INET, "127.0.0.1", &addr127);
-
- memset(&itf, 0, sizeof(itf));
-
- while (1) {
- rc = mn_itf_getnext(&itf);
- if (rc) {
- break;
- }
- printf("%d: %x %s\n", itf.mif_idx, itf.mif_flags, itf.mif_name);
- memset(&itf_addr, 0, sizeof(itf_addr));
- while (1) {
- rc = mn_itf_addr_getnext(&itf, &itf_addr);
- if (rc) {
- break;
- }
- if (itf_addr.mifa_family == MN_AF_INET &&
- !memcmp(&itf_addr.mifa_addr, &addr127, sizeof(addr127))) {
- seen_127 = 1;
- }
- addr_str[0] = '\0';
- mn_inet_ntop(itf_addr.mifa_family, &itf_addr.mifa_addr,
- addr_str, sizeof(addr_str));
- printf(" %s/%d\n", addr_str, itf_addr.mifa_plen);
- }
- if_cnt++;
- }
- TEST_ASSERT(if_cnt > 0);
- TEST_ASSERT(seen_127);
-}
-
-static int
-first_ll_addr(struct mn_sockaddr_in6 *ra)
-{
- struct mn_itf itf;
- struct mn_itf_addr itf_addr;
- int rc;
- struct mn_in6_addr *addr;
-
- memset(&itf, 0, sizeof(itf));
- addr = (struct mn_in6_addr *)&itf_addr.mifa_addr;
- while (1) {
- rc = mn_itf_getnext(&itf);
- if (rc) {
- break;
- }
- memset(&itf_addr, 0, sizeof(itf_addr));
- while (1) {
- rc = mn_itf_addr_getnext(&itf, &itf_addr);
- if (rc) {
- break;
- }
- if (itf_addr.mifa_family == MN_AF_INET6 &&
- addr->s_addr[0] == 0xfe && addr->s_addr[1] == 0x80) {
- memset(ra, 0, sizeof(*ra));
- ra->msin6_family = MN_AF_INET6;
- ra->msin6_len = sizeof(*ra);
- ra->msin6_scope_id = itf.mif_idx;
- memcpy(&ra->msin6_addr, addr, sizeof(*addr));
- return 0;
- }
- }
- }
- return -1;
-}
-
-void
-sul_readable(void *cb_arg, int err)
-{
- os_sem_release(&test_sem);
-}
-
-void
-sock_udp_ll(void)
-{
- struct mn_socket *sock1;
- struct mn_socket *sock2;
- struct mn_sockaddr_in6 msin;
- struct mn_sockaddr_in6 msin2;
- int rc;
- union mn_socket_cb sock_cbs = {
- .socket.readable = sul_readable
- };
- struct os_mbuf *m;
- char data[] = "1234567890";
-
- rc = mn_socket(&sock1, MN_PF_INET6, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(rc == 0);
- mn_socket_set_cbs(sock1, NULL, &sock_cbs);
-
- rc = mn_socket(&sock2, MN_PF_INET6, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(rc == 0);
- mn_socket_set_cbs(sock2, NULL, &sock_cbs);
-
- rc = first_ll_addr(&msin);
- if (rc != 0) {
- printf("No ipv6 address present?\n");
- return;
- }
- msin.msin6_port = htons(12445);
-
- rc = mn_bind(sock1, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- rc = mn_getsockname(sock1, (struct mn_sockaddr *)&msin2);
- TEST_ASSERT(rc == 0);
-
- m = os_msys_get(sizeof(data), 0);
- TEST_ASSERT(m);
- rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
- TEST_ASSERT(rc == 0);
- rc = mn_sendto(sock2, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin2);
- TEST_ASSERT(rc == 0);
-
- /*
- * Wait for the packet.
- */
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == 0);
-
- rc = mn_recvfrom(sock1, &m, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(m != NULL);
-
- if (m) {
- TEST_ASSERT(OS_MBUF_IS_PKTHDR(m));
- TEST_ASSERT(OS_MBUF_PKTLEN(m) == sizeof(data));
- TEST_ASSERT(m->om_len == sizeof(data));
- TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
- os_mbuf_free_chain(m);
- }
-
- mn_close(sock1);
- mn_close(sock2);
-}
-
-static int
-sock_find_multicast_if(void)
-{
- struct mn_itf itf;
-
- memset(&itf, 0, sizeof(itf));
-
- while (1) {
- if (mn_itf_getnext(&itf)) {
- break;
- }
- if ((itf.mif_flags & MN_ITF_F_UP) == 0) {
- continue;
- }
- if (itf.mif_flags & MN_ITF_F_MULTICAST) {
- return itf.mif_idx;
- }
- }
- return -1;
-}
-
-void
-sum4_readable(void *cb_arg, int err)
-{
- os_sem_release(&test_sem);
-}
-
-static void
-sock_udp_mcast_v4(void)
-{
- int loop_if_idx;
- struct mn_socket *rx_sock;
- struct mn_socket *tx_sock;
- struct mn_sockaddr_in msin;
- union mn_socket_cb sock_cbs = {
- .socket.readable = sum4_readable
- };
- struct os_mbuf *m;
- char data[] = "1234567890";
- int rc;
- struct mn_mreq mreq;
- loop_if_idx = sock_find_multicast_if();
- TEST_ASSERT(loop_if_idx > 0);
-
- msin.msin_family = MN_AF_INET;
- msin.msin_len = sizeof(msin);
- msin.msin_port = htons(44344);
- memset(&msin.msin_addr, 0, sizeof(msin.msin_addr));
-
- rc = mn_socket(&rx_sock, MN_PF_INET, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(rc == 0);
- mn_socket_set_cbs(rx_sock, NULL, &sock_cbs);
-
- rc = mn_bind(rx_sock, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- rc = mn_socket(&tx_sock, MN_PF_INET, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(rc == 0);
-
- rc = mn_setsockopt(tx_sock, MN_SO_LEVEL, MN_MCAST_IF, &loop_if_idx);
- TEST_ASSERT(rc == 0);
-
- m = os_msys_get(sizeof(data), 0);
- rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
- TEST_ASSERT(rc == 0);
-
- /*
- * multicast tgt
- */
- mn_inet_pton(MN_PF_INET, "224.0.2.241", &msin.msin_addr);
-
- rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- /*
- * RX socket has not joined group yet.
- */
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC / 2);
- TEST_ASSERT(rc == OS_TIMEOUT);
-
- mreq.mm_idx = loop_if_idx;
- mreq.mm_family = MN_AF_INET;
- mreq.mm_addr.v4.s_addr = msin.msin_addr.s_addr;
-
- /*
- * Now join it.
- */
- rc = mn_setsockopt(rx_sock, MN_SO_LEVEL, MN_MCAST_JOIN_GROUP, &mreq);
- TEST_ASSERT(rc == 0);
-
- m = os_msys_get(sizeof(data), 0);
- rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
- TEST_ASSERT(rc == 0);
-
- rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == 0);
-
- rc = mn_recvfrom(rx_sock, &m, NULL);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(m != NULL);
- TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
- os_mbuf_free_chain(m);
-
- /*
- * Then leave
- */
- rc = mn_setsockopt(rx_sock, MN_SO_LEVEL, MN_MCAST_LEAVE_GROUP, &mreq);
- TEST_ASSERT(rc == 0);
-
- m = os_msys_get(sizeof(data), 0);
- TEST_ASSERT(m);
- rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
- TEST_ASSERT(rc == 0);
-
- rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
- TEST_ASSERT(rc == 0);
-
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == OS_TIMEOUT);
-
- mn_close(rx_sock);
- mn_close(tx_sock);
-}
-
-static void
-sock_udp_mcast_v6(void)
-{
- int loop_if_idx;
- struct mn_socket *rx_sock;
- struct mn_socket *tx_sock;
- struct mn_sockaddr_in6 msin6;
- union mn_socket_cb sock_cbs = {
- .socket.readable = sum4_readable
- };
- struct os_mbuf *m;
- char data[] = "1234567890";
- int rc;
- struct mn_mreq mreq;
- uint8_t mcast_addr[16] = {
- 0xff, 2, 0, 0,
- 0, 0, 0, 0,
- 0, 0, 0, 0,
- 0, 0, 0, 2
- };
-
- loop_if_idx = sock_find_multicast_if();
- TEST_ASSERT(loop_if_idx > 0);
-
- msin6.msin6_family = MN_AF_INET6;
- msin6.msin6_len = sizeof(msin6);
- msin6.msin6_port = htons(44344);
- memset(&msin6.msin6_addr, 0, sizeof(msin6.msin6_addr));
-
- rc = mn_socket(&rx_sock, MN_PF_INET6, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(rc == 0);
- mn_socket_set_cbs(rx_sock, NULL, &sock_cbs);
-
- rc = mn_bind(rx_sock, (struct mn_sockaddr *)&msin6);
- TEST_ASSERT(rc == 0);
-
- rc = mn_socket(&tx_sock, MN_PF_INET6, MN_SOCK_DGRAM, 0);
- TEST_ASSERT(rc == 0);
-
- rc = mn_setsockopt(tx_sock, MN_SO_LEVEL, MN_MCAST_IF, &loop_if_idx);
- TEST_ASSERT(rc == 0);
-
- m = os_msys_get(sizeof(data), 0);
- rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
- TEST_ASSERT(rc == 0);
-
- /*
- * multicast tgt
- */
- memcpy(&msin6.msin6_addr, mcast_addr, sizeof(mcast_addr));
-
- rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin6);
- TEST_ASSERT(rc == 0);
-
- /*
- * RX socket has not joined group yet.
- */
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC / 2);
- TEST_ASSERT(rc == OS_TIMEOUT);
-
- mreq.mm_idx = loop_if_idx;
- mreq.mm_family = MN_AF_INET6;
- memcpy(&mreq.mm_addr.v6.s_addr, msin6.msin6_addr.s_addr,
- sizeof(msin6.msin6_addr.s_addr));
-
- /*
- * Now join it.
- */
- rc = mn_setsockopt(rx_sock, MN_SO_LEVEL, MN_MCAST_JOIN_GROUP, &mreq);
- TEST_ASSERT(rc == 0);
-
- m = os_msys_get(sizeof(data), 0);
- rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
- TEST_ASSERT(rc == 0);
-
- rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin6);
- TEST_ASSERT(rc == 0);
-
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == 0);
-
- rc = mn_recvfrom(rx_sock, &m, NULL);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(m != NULL);
- TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
- os_mbuf_free_chain(m);
-
- /*
- * Then leave
- */
- rc = mn_setsockopt(rx_sock, MN_SO_LEVEL, MN_MCAST_LEAVE_GROUP, &mreq);
- TEST_ASSERT(rc == 0);
-
- m = os_msys_get(sizeof(data), 0);
- TEST_ASSERT(m);
- rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
- TEST_ASSERT(rc == 0);
-
- rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin6);
- TEST_ASSERT(rc == 0);
-
- rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
- TEST_ASSERT(rc == OS_TIMEOUT);
-
- mn_close(rx_sock);
- mn_close(tx_sock);
-}
-
-void
-mn_socket_test_handler(void *arg)
-{
- sock_open_close();
- sock_listen();
- sock_tcp_connect();
- sock_udp_data();
- sock_tcp_data();
- sock_itf_list();
- sock_udp_ll();
- sock_udp_mcast_v4();
- sock_udp_mcast_v6();
- tu_restart();
-}
-
-TEST_CASE(socket_tests)
-{
- os_init();
- native_sock_init();
-
- os_sem_init(&test_sem, 0);
-
- os_task_init(&test_task, "mn_socket_test", mn_socket_test_handler, NULL,
- TEST_PRIO, OS_WAIT_FOREVER, test_stack, TEST_STACK_SIZE);
- os_start();
-}
-
-TEST_SUITE(mn_socket_test_all)
-{
- int rc;
-
- rc = os_mempool_init(&test_mbuf_mpool, MB_CNT, MB_SZ, test_mbuf_area, "mb");
- TEST_ASSERT(rc == 0);
- rc = os_mbuf_pool_init(&test_mbuf_pool, &test_mbuf_mpool, MB_CNT, MB_CNT);
- TEST_ASSERT(rc == 0);
- rc = os_msys_register(&test_mbuf_pool);
- TEST_ASSERT(rc == 0);
-
- inet_pton_test();
- inet_ntop_test();
-
- socket_tests();
-}
-
-#if MYNEWT_VAL(SELFTEST)
-
-int
-main(int argc, char **argv)
-{
- tu_config.tc_print_results = 1;
- tu_init();
-
- mn_socket_test_all();
-
- return tu_any_failed;
-}
-#endif
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/reboot/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/reboot/pkg.yml b/sys/reboot/pkg.yml
index fd173e2..9e88f71 100644
--- a/sys/reboot/pkg.yml
+++ b/sys/reboot/pkg.yml
@@ -26,13 +26,13 @@ pkg.keywords:
- logging
pkg.deps:
- - libs/os
+ - kernel/os
- libs/util
- sys/log
- libs/imgmgr
- sys/config
pkg.deps.REBOOT_LOG_FCB:
- - sys/fcb
+ - fs/fcb
pkg.req_apis:
- console
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/shell/include/shell/shell.h
----------------------------------------------------------------------
diff --git a/sys/shell/include/shell/shell.h b/sys/shell/include/shell/shell.h
new file mode 100644
index 0000000..48351a0
--- /dev/null
+++ b/sys/shell/include/shell/shell.h
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef __SHELL_H__
+#define __SHELL_H__
+
+#include <os/os.h>
+
+typedef int (*shell_cmd_func_t)(int argc, char **argv);
+struct shell_cmd {
+ char *sc_cmd;
+ shell_cmd_func_t sc_cmd_func;
+ STAILQ_ENTRY(shell_cmd) sc_next;
+};
+
+int shell_cmd_register(struct shell_cmd *sc);
+
+#define SHELL_NLIP_PKT_START1 (6)
+#define SHELL_NLIP_PKT_START2 (9)
+#define SHELL_NLIP_DATA_START1 (4)
+#define SHELL_NLIP_DATA_START2 (20)
+
+typedef int (*shell_nlip_input_func_t)(struct os_mbuf *, void *arg);
+int shell_nlip_input_register(shell_nlip_input_func_t nf, void *arg);
+int shell_nlip_output(struct os_mbuf *m);
+
+void shell_console_rx_cb(void);
+void shell_init(void);
+
+int shell_cmd_list_lock(void);
+int shell_cmd_list_unlock(void);
+
+#endif /* __SHELL_H__ */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/shell/include/shell/shell_prompt.h
----------------------------------------------------------------------
diff --git a/sys/shell/include/shell/shell_prompt.h b/sys/shell/include/shell/shell_prompt.h
new file mode 100644
index 0000000..f0e4dd3
--- /dev/null
+++ b/sys/shell/include/shell/shell_prompt.h
@@ -0,0 +1,25 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_SHELL_PROMPT_
+#define H_SHELL_PROMPT_
+
+int shell_prompt_cmd(int argc, char **argv);
+
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/shell/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/shell/pkg.yml b/sys/shell/pkg.yml
new file mode 100644
index 0000000..c4449e5
--- /dev/null
+++ b/sys/shell/pkg.yml
@@ -0,0 +1,50 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: sys/shell
+pkg.description: Command processor for console-based applications.
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+ - kernel/os
+ - libs/util
+ - encoding/base64
+ - encoding/crc
+pkg.req_apis:
+ - console
+
+pkg.init_function: shell_init
+pkg.init_stage: 5
+
+pkg.syscfg_defs:
+ SHELL_TASK:
+ description: 'TBD'
+ value: 0
+ SHELL_TASK_PRIO:
+ description: 'TBD'
+ type: 'task_priority'
+ value: 'any'
+ SHELL_STACK_SIZE:
+ description: 'TBD'
+ value: 512
+ SHELL_MAX_INPUT_LEN:
+ description: 'TBD'
+ value: 256
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/sys/shell/src/shell.c b/sys/shell/src/shell.c
new file mode 100644
index 0000000..58481de
--- /dev/null
+++ b/sys/shell/src/shell.c
@@ -0,0 +1,579 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "console/console.h"
+#include "console/prompt.h"
+#include "os/os.h"
+#include "os/endian.h"
+#include "base64/base64.h"
+#include "crc/crc16.h"
+#include "shell/shell.h"
+#include "shell_priv.h"
+
+static shell_nlip_input_func_t g_shell_nlip_in_func;
+static void *g_shell_nlip_in_arg;
+
+static struct os_mqueue g_shell_nlip_mq;
+
+#define OS_EVENT_T_CONSOLE_RDY (OS_EVENT_T_PERUSER)
+#define SHELL_HELP_PER_LINE 6
+#define SHELL_MAX_ARGS 20
+
+static os_stack_t shell_stack[OS_STACK_ALIGN(MYNEWT_VAL(SHELL_STACK_SIZE))];
+
+static int shell_echo_cmd(int argc, char **argv);
+static int shell_help_cmd(int argc, char **argv);
+int shell_prompt_cmd(int argc, char **argv);
+
+static struct shell_cmd g_shell_echo_cmd = {
+ .sc_cmd = "echo",
+ .sc_cmd_func = shell_echo_cmd
+};
+static struct shell_cmd g_shell_help_cmd = {
+ .sc_cmd = "?",
+ .sc_cmd_func = shell_help_cmd
+};
+static struct shell_cmd g_shell_prompt_cmd = {
+ .sc_cmd = "prompt",
+ .sc_cmd_func = shell_prompt_cmd
+};
+static struct shell_cmd g_shell_os_tasks_display_cmd = {
+ .sc_cmd = "tasks",
+ .sc_cmd_func = shell_os_tasks_display_cmd
+};
+static struct shell_cmd g_shell_os_mpool_display_cmd = {
+ .sc_cmd = "mempools",
+ .sc_cmd_func = shell_os_mpool_display_cmd
+};
+static struct shell_cmd g_shell_os_date_cmd = {
+ .sc_cmd = "date",
+ .sc_cmd_func = shell_os_date_cmd
+};
+
+static struct os_task shell_task;
+static struct os_eventq shell_evq;
+static struct os_event console_rdy_ev;
+
+static struct os_mutex g_shell_cmd_list_lock;
+
+static char *shell_line;
+static int shell_line_len;
+static char *argv[SHELL_MAX_ARGS];
+
+static STAILQ_HEAD(, shell_cmd) g_shell_cmd_list =
+ STAILQ_HEAD_INITIALIZER(g_shell_cmd_list);
+
+static struct os_mbuf *g_nlip_mbuf;
+static uint16_t g_nlip_expected_len;
+
+int
+shell_cmd_list_lock(void)
+{
+ int rc;
+
+ if (!os_started()) {
+ return (0);
+ }
+
+ rc = os_mutex_pend(&g_shell_cmd_list_lock, OS_WAIT_FOREVER);
+ if (rc != 0) {
+ goto err;
+ }
+ return (0);
+err:
+ return (rc);
+}
+
+int
+shell_cmd_list_unlock(void)
+{
+ int rc;
+
+ if (!os_started()) {
+ return (0);
+ }
+
+ rc = os_mutex_release(&g_shell_cmd_list_lock);
+ if (rc != 0) {
+ goto err;
+ }
+ return (0);
+err:
+ return (rc);
+}
+
+int
+shell_cmd_register(struct shell_cmd *sc)
+{
+ int rc;
+
+ /* Add the command that is being registered. */
+ rc = shell_cmd_list_lock();
+ if (rc != 0) {
+ goto err;
+ }
+
+ STAILQ_INSERT_TAIL(&g_shell_cmd_list, sc, sc_next);
+
+ rc = shell_cmd_list_unlock();
+ if (rc != 0) {
+ goto err;
+ }
+
+ return (0);
+err:
+ return (rc);
+}
+
+static int
+shell_cmd(char *cmd, char **argv, int argc)
+{
+ struct shell_cmd *sc;
+ int rc;
+
+ rc = shell_cmd_list_lock();
+ if (rc != 0) {
+ goto err;
+ }
+
+ STAILQ_FOREACH(sc, &g_shell_cmd_list, sc_next) {
+ if (!strcmp(sc->sc_cmd, cmd)) {
+ break;
+ }
+ }
+
+ rc = shell_cmd_list_unlock();
+ if (rc != 0) {
+ goto err;
+ }
+
+ if (sc) {
+ sc->sc_cmd_func(argc, argv);
+ } else {
+ console_printf("Unknown command %s\n", cmd);
+ }
+
+ return (0);
+err:
+ return (rc);
+}
+
+static int
+shell_process_command(char *line, int len)
+{
+ char *tok;
+ char *tok_ptr;
+ int argc;
+
+ tok_ptr = NULL;
+ tok = strtok_r(line, " ", &tok_ptr);
+ argc = 0;
+ while (argc < SHELL_MAX_ARGS - 1 && tok != NULL) {
+ argv[argc++] = tok;
+
+ tok = strtok_r(NULL, " ", &tok_ptr);
+ }
+
+ /* Terminate the argument list with a null pointer. */
+ argv[argc] = NULL;
+
+ if (argc) {
+ (void) shell_cmd(argv[0], argv, argc);
+ }
+ else {
+ console_printf("\n");
+ }
+ console_print_prompt();
+ return (0);
+}
+
+
+static int
+shell_nlip_process(char *data, int len)
+{
+ uint16_t copy_len;
+ int rc;
+ struct os_mbuf *m;
+ uint16_t crc;
+
+ rc = base64_decode(data, data);
+ if (rc < 0) {
+ goto err;
+ }
+ len = rc;
+
+ if (g_nlip_mbuf == NULL) {
+ if (len < 2) {
+ rc = -1;
+ goto err;
+ }
+
+ g_nlip_expected_len = ntohs(*(uint16_t *) data);
+ g_nlip_mbuf = os_msys_get_pkthdr(g_nlip_expected_len, 0);
+ if (!g_nlip_mbuf) {
+ rc = -1;
+ goto err;
+ }
+
+ data += sizeof(uint16_t);
+ len -= sizeof(uint16_t);
+ }
+
+ copy_len = min(g_nlip_expected_len - OS_MBUF_PKTHDR(g_nlip_mbuf)->omp_len,
+ len);
+
+ rc = os_mbuf_copyinto(g_nlip_mbuf, OS_MBUF_PKTHDR(g_nlip_mbuf)->omp_len,
+ data, copy_len);
+ if (rc != 0) {
+ goto err;
+ }
+
+ if (OS_MBUF_PKTHDR(g_nlip_mbuf)->omp_len == g_nlip_expected_len) {
+ if (g_shell_nlip_in_func) {
+ crc = CRC16_INITIAL_CRC;
+ for (m = g_nlip_mbuf; m; m = SLIST_NEXT(m, om_next)) {
+ crc = crc16_ccitt(crc, m->om_data, m->om_len);
+ }
+ if (crc == 0 && g_nlip_expected_len >= sizeof(crc)) {
+ os_mbuf_adj(g_nlip_mbuf, -sizeof(crc));
+ g_shell_nlip_in_func(g_nlip_mbuf, g_shell_nlip_in_arg);
+ } else {
+ os_mbuf_free_chain(g_nlip_mbuf);
+ }
+ } else {
+ os_mbuf_free_chain(g_nlip_mbuf);
+ }
+ g_nlip_mbuf = NULL;
+ g_nlip_expected_len = 0;
+ }
+
+ return (0);
+err:
+ return (rc);
+}
+
+static int
+shell_nlip_mtx(struct os_mbuf *m)
+{
+#define SHELL_NLIP_MTX_BUF_SIZE (12)
+ uint8_t readbuf[SHELL_NLIP_MTX_BUF_SIZE];
+ char encodebuf[BASE64_ENCODE_SIZE(SHELL_NLIP_MTX_BUF_SIZE)];
+ char pkt_seq[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
+ char esc_seq[2] = { SHELL_NLIP_DATA_START1, SHELL_NLIP_DATA_START2 };
+ uint16_t totlen;
+ uint16_t dlen;
+ uint16_t off;
+ uint16_t crc;
+ int rb_off;
+ int elen;
+ uint16_t nwritten;
+ uint16_t linelen;
+ int rc;
+ struct os_mbuf *tmp;
+ void *ptr;
+
+ /* Convert the mbuf into a packet.
+ *
+ * starts with 06 09
+ * base64 encode:
+ * - total packet length (uint16_t)
+ * - data
+ * - crc
+ * base64 encoded data must be less than 122 bytes per line to
+ * avoid overflows and adhere to convention.
+ *
+ * continuation packets are preceded by 04 20 until the entire
+ * buffer has been sent.
+ */
+ crc = CRC16_INITIAL_CRC;
+ for (tmp = m; tmp; tmp = SLIST_NEXT(tmp, om_next)) {
+ crc = crc16_ccitt(crc, tmp->om_data, tmp->om_len);
+ }
+ crc = htons(crc);
+ ptr = os_mbuf_extend(m, sizeof(crc));
+ if (!ptr) {
+ rc = -1;
+ goto err;
+ }
+ memcpy(ptr, &crc, sizeof(crc));
+
+ totlen = OS_MBUF_PKTHDR(m)->omp_len;
+ nwritten = 0;
+ off = 0;
+
+ /* Start a packet */
+ console_write(pkt_seq, sizeof(pkt_seq));
+
+ linelen = 0;
+
+ rb_off = 2;
+ dlen = htons(totlen);
+ memcpy(readbuf, &dlen, sizeof(dlen));
+
+ while (totlen > 0) {
+ dlen = min(SHELL_NLIP_MTX_BUF_SIZE - rb_off, totlen);
+
+ rc = os_mbuf_copydata(m, off, dlen, readbuf + rb_off);
+ if (rc != 0) {
+ goto err;
+ }
+ off += dlen;
+
+ /* If the next packet will overwhelm the line length, truncate
+ * this line.
+ */
+ if (linelen +
+ BASE64_ENCODE_SIZE(min(SHELL_NLIP_MTX_BUF_SIZE - rb_off,
+ totlen - dlen)) >= 120) {
+ elen = base64_encode(readbuf, dlen + rb_off, encodebuf, 1);
+ console_write(encodebuf, elen);
+ console_write("\n", 1);
+ console_write(esc_seq, sizeof(esc_seq));
+ linelen = 0;
+ } else {
+ elen = base64_encode(readbuf, dlen + rb_off, encodebuf, 0);
+ console_write(encodebuf, elen);
+ linelen += elen;
+ }
+
+ rb_off = 0;
+
+ nwritten += elen;
+ totlen -= dlen;
+ }
+
+ elen = base64_pad(encodebuf, linelen);
+ console_write(encodebuf, elen);
+
+ console_write("\n", 1);
+
+ return (0);
+err:
+ return (rc);
+}
+
+static void
+shell_nlip_mqueue_process(void)
+{
+ struct os_mbuf *m;
+
+ /* Copy data out of the mbuf 12 bytes at a time and write it to
+ * the console.
+ */
+ while (1) {
+ m = os_mqueue_get(&g_shell_nlip_mq);
+ if (!m) {
+ break;
+ }
+
+ (void) shell_nlip_mtx(m);
+
+ os_mbuf_free_chain(m);
+ }
+}
+
+int
+shell_nlip_input_register(shell_nlip_input_func_t nf, void *arg)
+{
+ g_shell_nlip_in_func = nf;
+ g_shell_nlip_in_arg = arg;
+
+ return (0);
+}
+
+int
+shell_nlip_output(struct os_mbuf *m)
+{
+ int rc;
+
+ rc = os_mqueue_put(&g_shell_nlip_mq, &shell_evq, m);
+ if (rc != 0) {
+ goto err;
+ }
+
+ return (0);
+err:
+ return (rc);
+}
+
+static void
+shell_read_console(void)
+{
+ int rc;
+ int full_line;
+
+ while (1) {
+ rc = console_read(shell_line + shell_line_len,
+ MYNEWT_VAL(SHELL_MAX_INPUT_LEN) - shell_line_len, &full_line);
+ if (rc <= 0 && !full_line) {
+ break;
+ }
+ shell_line_len += rc;
+ if (full_line) {
+ if (shell_line_len > 2) {
+ if (shell_line[0] == SHELL_NLIP_PKT_START1 &&
+ shell_line[1] == SHELL_NLIP_PKT_START2) {
+ if (g_nlip_mbuf) {
+ os_mbuf_free_chain(g_nlip_mbuf);
+ g_nlip_mbuf = NULL;
+ }
+ g_nlip_expected_len = 0;
+
+ rc = shell_nlip_process(&shell_line[2], shell_line_len - 2);
+ } else if (shell_line[0] == SHELL_NLIP_DATA_START1 &&
+ shell_line[1] == SHELL_NLIP_DATA_START2) {
+ rc = shell_nlip_process(&shell_line[2], shell_line_len - 2);
+ } else {
+ shell_process_command(shell_line, shell_line_len);
+ }
+ } else {
+ shell_process_command(shell_line, shell_line_len);
+ }
+ shell_line_len = 0;
+ }
+ }
+}
+
+
+static void
+shell_task_func(void *arg)
+{
+ struct os_event *ev;
+
+ while (1) {
+ ev = os_eventq_get(&shell_evq);
+ assert(ev != NULL);
+
+ switch (ev->ev_type) {
+ case OS_EVENT_T_CONSOLE_RDY:
+ // Read and process all available lines on the console.
+ shell_read_console();
+ break;
+ case OS_EVENT_T_MQUEUE_DATA:
+ shell_nlip_mqueue_process();
+ break;
+ }
+ }
+}
+
+/**
+ * This function is called from the console APIs when data is available
+ * to be read. This is either a full line, or when the
+ * console buffer (default = 128) is full.
+ */
+void
+shell_console_rx_cb(void)
+{
+ os_eventq_put(&shell_evq, &console_rdy_ev);
+}
+
+static int
+shell_echo_cmd(int argc, char **argv)
+{
+ int i;
+
+ for (i = 1; i < argc; i++) {
+ console_write(argv[i], strlen(argv[i]));
+ console_write(" ", sizeof(" ")-1);
+ }
+ console_write("\n", sizeof("\n")-1);
+
+ return (0);
+}
+
+static int
+shell_help_cmd(int argc, char **argv)
+{
+ int rc;
+ int i = 0;
+ struct shell_cmd *sc;
+
+ rc = shell_cmd_list_lock();
+ if (rc != 0) {
+ return -1;
+ }
+ console_printf("Commands:\n");
+ STAILQ_FOREACH(sc, &g_shell_cmd_list, sc_next) {
+ console_printf("%9s ", sc->sc_cmd);
+ if (i++ % SHELL_HELP_PER_LINE == SHELL_HELP_PER_LINE - 1) {
+ console_printf("\n");
+ }
+ }
+ if (i % SHELL_HELP_PER_LINE) {
+ console_printf("\n");
+ }
+ shell_cmd_list_unlock();
+
+ return (0);
+}
+
+void
+shell_init(void)
+{
+#if !MYNEWT_VAL(SHELL_TASK)
+ return;
+#endif
+
+ int rc;
+
+ free(shell_line);
+ shell_line = NULL;
+
+#if MYNEWT_VAL(SHELL_MAX_INPUT_LEN) > 0
+ shell_line = malloc(MYNEWT_VAL(SHELL_MAX_INPUT_LEN));
+ SYSINIT_PANIC_ASSERT(shell_line != NULL);
+#endif
+
+ rc = os_mutex_init(&g_shell_cmd_list_lock);
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = shell_cmd_register(&g_shell_echo_cmd);
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = shell_cmd_register(&g_shell_help_cmd);
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = shell_cmd_register(&g_shell_prompt_cmd);
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = shell_cmd_register(&g_shell_os_tasks_display_cmd);
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = shell_cmd_register(&g_shell_os_mpool_display_cmd);
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = shell_cmd_register(&g_shell_os_date_cmd);
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ os_eventq_init(&shell_evq);
+ os_mqueue_init(&g_shell_nlip_mq, NULL);
+ console_rdy_ev.ev_type = OS_EVENT_T_CONSOLE_RDY;
+ console_init(shell_console_rx_cb);
+
+ rc = os_task_init(&shell_task, "shell", shell_task_func,
+ NULL, MYNEWT_VAL(SHELL_TASK_PRIO), OS_WAIT_FOREVER, shell_stack,
+ MYNEWT_VAL(SHELL_STACK_SIZE));
+ SYSINIT_PANIC_ASSERT(rc == 0);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/shell/src/shell_os.c
----------------------------------------------------------------------
diff --git a/sys/shell/src/shell_os.c b/sys/shell/src/shell_os.c
new file mode 100644
index 0000000..283d429
--- /dev/null
+++ b/sys/shell/src/shell_os.c
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "os/os.h"
+
+#include "os/queue.h"
+#include "os/os_time.h"
+
+#include "console/console.h"
+#include "shell/shell.h"
+#include "shell_priv.h"
+
+#include <assert.h>
+#include <string.h>
+#include <util/datetime.h>
+
+int
+shell_os_tasks_display_cmd(int argc, char **argv)
+{
+ struct os_task *prev_task;
+ struct os_task_info oti;
+ char *name;
+ int found;
+
+ name = NULL;
+ found = 0;
+
+ if (argc > 1 && strcmp(argv[1], "")) {
+ name = argv[1];
+ }
+
+ console_printf("Tasks: \n");
+ prev_task = NULL;
+ while (1) {
+ prev_task = os_task_info_get_next(prev_task, &oti);
+ if (prev_task == NULL) {
+ break;
+ }
+
+ if (name) {
+ if (strcmp(name, oti.oti_name)) {
+ continue;
+ } else {
+ found = 1;
+ }
+ }
+
+ console_printf(" %s (prio: %u, tid: %u, lcheck: %lu, ncheck: %lu, "
+ "flags: 0x%x, ssize: %u, susage: %u, cswcnt: %lu, "
+ "tot_run_time: %lums)\n",
+ oti.oti_name, oti.oti_prio, oti.oti_taskid,
+ (unsigned long)oti.oti_last_checkin,
+ (unsigned long)oti.oti_next_checkin, oti.oti_flags,
+ oti.oti_stksize, oti.oti_stkusage, (unsigned long)oti.oti_cswcnt,
+ (unsigned long)oti.oti_runtime);
+
+ }
+
+ if (name && !found) {
+ console_printf("Couldn't find task with name %s\n", name);
+ }
+
+ return (0);
+}
+
+int
+shell_os_mpool_display_cmd(int argc, char **argv)
+{
+ struct os_mempool *mp;
+ struct os_mempool_info omi;
+ char *name;
+ int found;
+
+ name = NULL;
+ found = 0;
+
+ if (argc > 1 && strcmp(argv[1], "")) {
+ name = argv[1];
+ }
+
+ console_printf("Mempools: \n");
+ mp = NULL;
+ while (1) {
+ mp = os_mempool_info_get_next(mp, &omi);
+ if (mp == NULL) {
+ break;
+ }
+
+ if (name) {
+ if (strcmp(name, omi.omi_name)) {
+ continue;
+ } else {
+ found = 1;
+ }
+ }
+
+ console_printf(" %s (blksize: %d, nblocks: %d, nfree: %d)\n",
+ omi.omi_name, omi.omi_block_size, omi.omi_num_blocks,
+ omi.omi_num_free);
+ }
+
+ if (name && !found) {
+ console_printf("Couldn't find a memory pool with name %s\n",
+ name);
+ }
+
+ return (0);
+}
+
+int
+shell_os_date_cmd(int argc, char **argv)
+{
+ struct os_timeval tv;
+ struct os_timezone tz;
+ char buf[DATETIME_BUFSIZE];
+ int rc;
+
+ argc--; argv++; /* skip command name */
+
+ if (argc == 0) {
+ /* Display the current datetime */
+ rc = os_gettimeofday(&tv, &tz);
+ assert(rc == 0);
+ rc = format_datetime(&tv, &tz, buf, sizeof(buf));
+ assert(rc == 0);
+ console_printf("%s\n", buf);
+ } else if (argc == 1) {
+ /* Set the current datetime */
+ rc = parse_datetime(*argv, &tv, &tz);
+ if (rc == 0) {
+ rc = os_settimeofday(&tv, &tz);
+ } else {
+ console_printf("Invalid datetime\n");
+ }
+ } else {
+ rc = -1;
+ }
+
+ return (rc);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/shell/src/shell_priv.h
----------------------------------------------------------------------
diff --git a/sys/shell/src/shell_priv.h b/sys/shell/src/shell_priv.h
new file mode 100644
index 0000000..4e035e0
--- /dev/null
+++ b/sys/shell/src/shell_priv.h
@@ -0,0 +1,27 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __SHELL_PRIV_H_
+#define __SHELL_PRIV_H_
+
+int shell_os_tasks_display_cmd(int argc, char **argv);
+int shell_os_mpool_display_cmd(int argc, char **argv);
+int shell_os_date_cmd(int argc, char **argv);
+
+#endif /* __SHELL_PRIV_H_ */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/sys/shell/src/shell_prompt.c
----------------------------------------------------------------------
diff --git a/sys/shell/src/shell_prompt.c b/sys/shell/src/shell_prompt.c
new file mode 100644
index 0000000..d1402ad
--- /dev/null
+++ b/sys/shell/src/shell_prompt.c
@@ -0,0 +1,69 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include "shell/shell.h"
+#include <console/console.h>
+
+static char shell_prompt = '>';
+
+void console_set_prompt(char p);
+
+/**
+ * Handles the 'prompt' command
+ * with set argument, sets the prompt to the provided char
+ * with the show argument, echos the current prompt
+ * otherwise echos the prompt and the usage message
+ */
+int
+shell_prompt_cmd(int argc, char **argv)
+{
+ int rc;
+
+ rc = shell_cmd_list_lock();
+ if (rc != 0) {
+ return -1;
+ }
+ if(argc > 1){
+ if(!strcmp(argv[1], "show")){
+ console_printf("Prompt character: %c\n", shell_prompt);
+ }
+ else if (!strcmp(argv[1],"set")){
+ shell_prompt = argv[2][0];
+ console_printf("Prompt set to: %c\n", argv[2][0]);
+ console_set_prompt(argv[2][0]);
+ }
+ else {
+ goto usage;
+ }
+
+ }
+ else {
+ goto usage;
+
+ }
+usage:
+ console_printf("Usage: prompt [set|show] [prompt_char]\n");
+
+ shell_cmd_list_unlock();
+
+ return (0);
+
+}
+
\ No newline at end of file