mirror of
https://github.com/Ai-Thinker-Open/Ai-Thinker-Open_RTL8710BX_ALIOS_SDK.git
synced 2025-07-31 19:31:05 +00:00
rel_1.6.0 init
This commit is contained in:
commit
27b3e2883d
19359 changed files with 8093121 additions and 0 deletions
107
Living_SDK/platform/mcu/linux/csp/lwip/include/arch/cc.h
Normal file
107
Living_SDK/platform/mcu/linux/csp/lwip/include/arch/cc.h
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_ARCH_CC_H
|
||||
#define LWIP_ARCH_CC_H
|
||||
|
||||
/* see https://sourceforge.net/p/predef/wiki/OperatingSystems/ */
|
||||
#if defined __linux__
|
||||
#define LWIP_UNIX_LINUX
|
||||
#elif defined __APPLE__
|
||||
#define LWIP_UNIX_MACH
|
||||
#elif defined __OpenBSD__
|
||||
#define LWIP_UNIX_OPENBSD
|
||||
#elif defined __CYGWIN__
|
||||
#define LWIP_UNIX_CYGWIN
|
||||
#endif
|
||||
|
||||
/* Include some files for defining library routines */
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
//#define LWIP_COMPAT_MUTEX 1
|
||||
#define LWIP_MAILBOX_QUEUE 1
|
||||
|
||||
#define LWIP_TIMEVAL_PRIVATE 0
|
||||
|
||||
#define LWIP_NO_INTTYPES_H 1
|
||||
|
||||
#if LWIP_NO_INTTYPES_H
|
||||
#define X8_F "02x"
|
||||
#define U16_F "u"
|
||||
#define S16_F "d"
|
||||
#define X16_F "x"
|
||||
#define U32_F "u"
|
||||
#define S32_F "d"
|
||||
#define X32_F "x"
|
||||
#define SZT_F U32_F
|
||||
#endif
|
||||
|
||||
/* Define platform endianness */
|
||||
#ifndef BYTE_ORDER
|
||||
#define BYTE_ORDER LITTLE_ENDIAN
|
||||
#endif /* BYTE_ORDER */
|
||||
|
||||
/* Compiler hints for packing structures */
|
||||
#define PACK_STRUCT_FIELD(x) x
|
||||
#define PACK_STRUCT_STRUCT __attribute__((packed))
|
||||
#define PACK_STRUCT_BEGIN
|
||||
#define PACK_STRUCT_END
|
||||
|
||||
/* prototypes for printf() and abort() */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
extern int csp_printf(const char *fmt, ...);
|
||||
|
||||
/* Plaform specific diagnostic output */
|
||||
#define LWIP_PLATFORM_DIAG(x) do {csp_printf x;} while(0)
|
||||
|
||||
#define LWIP_PLATFORM_ASSERT(x) do {csp_printf("Assert at line %d in %s - %s\n", \
|
||||
__LINE__, __FILE__, x); fflush(NULL); *(int *)0=0;} while(0)
|
||||
|
||||
#ifdef LWIP_NOASSERT_ON_ERROR
|
||||
#define LWIP_ERROR(message, expression, handler) do { if (!(expression)) { \
|
||||
handler;}} while(0)
|
||||
#endif
|
||||
|
||||
#define LWIP_RAND() ((u32_t)rand())
|
||||
|
||||
struct sio_status_s;
|
||||
typedef struct sio_status_s sio_status_t;
|
||||
#define sio_fd_t sio_status_t*
|
||||
#define __sio_fd_t_defined
|
||||
|
||||
#endif /* LWIP_ARCH_CC_H */
|
||||
287
Living_SDK/platform/mcu/linux/csp/lwip/include/lwipopts.h
Normal file
287
Living_SDK/platform/mcu/linux/csp/lwip/include/lwipopts.h
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
/**
|
||||
* @file
|
||||
*
|
||||
* lwIP Options Configuration
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_LWIPOPTS_H
|
||||
#define LWIP_LWIPOPTS_H
|
||||
|
||||
#include "lwip/arch.h"
|
||||
|
||||
/**
|
||||
* Include user defined options first. Anything not defined in these files
|
||||
* will be set to standard values. Override anything you dont like!
|
||||
*/
|
||||
|
||||
/*
|
||||
-------------- NO SYS --------------
|
||||
*/
|
||||
#define NO_SYS 0
|
||||
#define SYS_LIGHTWEIGHT_PROT (NO_SYS == 0)
|
||||
|
||||
/*
|
||||
---------- Memory options ----------
|
||||
*/
|
||||
#define MEM_ALIGNMENT 4
|
||||
#define MEM_SIZE 0x8000
|
||||
|
||||
#define MEM_LIBC_MALLOC 1
|
||||
#if MEM_LIBC_MALLOC
|
||||
#include <aos/aos.h>
|
||||
#define mem_clib_malloc aos_malloc
|
||||
#define mem_clib_free aos_free
|
||||
#define mem_clib_calloc(n, m) aos_zalloc( (n) * (m) )
|
||||
#endif
|
||||
|
||||
#define MEMP_MEM_MALLOC 1
|
||||
#define MEMP_OVERFLOW_CHECK 1
|
||||
|
||||
|
||||
/*
|
||||
---------- Internal Memory Pool Sizes ----------
|
||||
*/
|
||||
#define MEMP_NUM_PBUF 8
|
||||
#define MEMP_NUM_RAW_PCB 2
|
||||
#define MEMP_NUM_UDP_PCB 4
|
||||
#define MEMP_NUM_TCP_PCB 4
|
||||
#define MEMP_NUM_TCP_PCB_LISTEN 4
|
||||
#define MEMP_NUM_TCP_SEG 12
|
||||
#define MEMP_NUM_REASSDATA 4
|
||||
#define MEMP_NUM_FRAG_PBUF 8
|
||||
#define MEMP_NUM_ARP_QUEUE 8
|
||||
#define MEMP_NUM_NETBUF 2
|
||||
#define MEMP_NUM_NETCONN 10
|
||||
#define MEMP_NUM_TCPIP_MSG_API 4
|
||||
#define MEMP_NUM_TCPIP_MSG_INPKT 12
|
||||
#define PBUF_POOL_SIZE 12
|
||||
|
||||
/*
|
||||
---------- ARP options ----------
|
||||
*/
|
||||
#define LWIP_ARP 1
|
||||
|
||||
/*
|
||||
---------- IP options ----------
|
||||
*/
|
||||
#define LWIP_IPV4 1
|
||||
|
||||
#ifndef LWIP_IPV6
|
||||
#define LWIP_IPV6 1
|
||||
#endif
|
||||
|
||||
#define IP_FORWARD 1
|
||||
#define IP_FORWARD_ALLOW_TX_ON_RX_NETIF 1
|
||||
|
||||
#define IP_OPTIONS_ALLOWED 1
|
||||
#define IP_REASSEMBLY 0
|
||||
#define IP_FRAG 0
|
||||
#define IP_REASS_MAXAGE 3
|
||||
#define IP_REASS_MAX_PBUFS 4
|
||||
#define IP_FRAG_USES_STATIC_BUF 0
|
||||
#define IP_DEFAULT_TTL 255
|
||||
|
||||
/*
|
||||
---------- ICMP options ----------
|
||||
*/
|
||||
#define LWIP_ICMP 1
|
||||
#define LWIP_ICMP6 1
|
||||
#define CHECKSUM_CHECK_ICMP6 0
|
||||
#define LWIP_MULTICAST_PING 1
|
||||
|
||||
/*
|
||||
---------- RAW options ----------
|
||||
*/
|
||||
#define LWIP_RAW 1
|
||||
|
||||
/*
|
||||
---------- DHCP options ----------
|
||||
*/
|
||||
#define LWIP_DHCP 1
|
||||
|
||||
|
||||
/*
|
||||
---------- AUTOIP options ----------
|
||||
*/
|
||||
#define LWIP_AUTOIP 0
|
||||
|
||||
/*
|
||||
---------- SNMP options ----------
|
||||
*/
|
||||
#define LWIP_SNMP 0
|
||||
|
||||
/*
|
||||
---------- IGMP options ----------
|
||||
*/
|
||||
#define LWIP_IGMP 1
|
||||
|
||||
/*
|
||||
---------- DNS options -----------
|
||||
*/
|
||||
#define LWIP_DNS 1
|
||||
|
||||
/*
|
||||
---------- UDP options ----------
|
||||
*/
|
||||
#define LWIP_UDP 1
|
||||
|
||||
/*
|
||||
---------- TCP options ----------
|
||||
*/
|
||||
#define LWIP_TCP 1
|
||||
#define LWIP_LISTEN_BACKLOG 0
|
||||
#define TCP_MSS 1460
|
||||
#define TCP_WND (2 * TCP_MSS)
|
||||
#define TCP_SND_BUF (2 * TCP_MSS)
|
||||
|
||||
/*
|
||||
---------- Pbuf options ----------
|
||||
*/
|
||||
#define PBUF_LINK_HLEN 16
|
||||
|
||||
//#define PBUF_POOL_BUFSIZE LWIP_MEM_ALIGN_SIZE(TCP_MSS+40+PBUF_LINK_HLEN)
|
||||
#define PBUF_POOL_BUFSIZE 512
|
||||
|
||||
/*
|
||||
---------- Network Interfaces options ----------
|
||||
*/
|
||||
|
||||
/*
|
||||
---------- LOOPIF options ----------
|
||||
*/
|
||||
#define LWIP_NETIF_LOOPBACK 1
|
||||
#define LWIP_HAVE_LOOPIF 1
|
||||
#define LWIP_NETIF_LOOPBACK_MULTITHREADING 1
|
||||
#define LWIP_LOOPBACK_MAX_PBUFS 8
|
||||
|
||||
/*
|
||||
---------- Thread options ----------
|
||||
*/
|
||||
#define TCPIP_MBOX_SIZE 16
|
||||
#define DEFAULT_ACCEPTMBOX_SIZE 8
|
||||
#define DEFAULT_RAW_RECVMBOX_SIZE 4
|
||||
#define DEFAULT_UDP_RECVMBOX_SIZE 8
|
||||
#define DEFAULT_TCP_RECVMBOX_SIZE 8
|
||||
|
||||
#define LWIP_TCPIP_CORE_LOCKING 1
|
||||
#define LWIP_TCPIP_CORE_LOCKING_INPUT 1
|
||||
|
||||
#define ETHIF_IN_TASK_STACKSIZE 512 /* unit 4 byte */
|
||||
#define ETHIF_IN_TASK_PRIO 10
|
||||
#define TCPIP_THREAD_STACKSIZE 1024/* unit 4 byte */
|
||||
#define TCPIP_THREAD_PRIO 5
|
||||
|
||||
/*
|
||||
---------- Sequential layer options ----------
|
||||
*/
|
||||
|
||||
#define LWIP_NETCONN 8
|
||||
|
||||
/*
|
||||
---------- Socket options ----------
|
||||
*/
|
||||
#define LWIP_SOCKET 1
|
||||
#define LWIP_COMPAT_SOCKETS 1
|
||||
#define LWIP_POSIX_SOCKETS_IO_NAMES 1
|
||||
#define LWIP_SO_SNDTIMEO 1
|
||||
#define LWIP_SO_RCVTIMEO 1
|
||||
#define SO_REUSE 1
|
||||
|
||||
/*
|
||||
---------- Statistics options ----------
|
||||
*/
|
||||
#define LWIP_STATS 1
|
||||
#define LWIP_STATS_DISPLAY 1
|
||||
|
||||
/*
|
||||
---------- Checksum options ----------
|
||||
*/
|
||||
|
||||
/*
|
||||
---------- IPv6 options ---------------
|
||||
*/
|
||||
|
||||
/*
|
||||
---------- Hook options ---------------
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_AOS_MESH
|
||||
#define LWIP_DECLARE_HOOK \
|
||||
struct netif *lwip_hook_ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest); \
|
||||
int lwip_hook_mesh_is_mcast_subscribed(const ip6_addr_t *dest);
|
||||
#define LWIP_HOOK_IP6_ROUTE(src, dest) lwip_hook_ip6_route(src, dest)
|
||||
#define LWIP_HOOK_MESH_IS_MCAST_SUBSCRIBED(dest) lwip_hook_mesh_is_mcast_subscribed(dest)
|
||||
#endif
|
||||
|
||||
/*
|
||||
---------- Debugging options ----------
|
||||
*/
|
||||
//#define LWIP_DEBUG
|
||||
|
||||
#define LWIP_DBG_MIN_LEVEL LWIP_DBG_LEVEL_ALL
|
||||
#define LWIP_DBG_TYPES_ON (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT)
|
||||
|
||||
#define MEM_DEBUG LWIP_DBG_OFF
|
||||
#define MEMP_DEBUG LWIP_DBG_OFF
|
||||
#define PBUF_DEBUG LWIP_DBG_ON
|
||||
#define API_LIB_DEBUG LWIP_DBG_ON
|
||||
#define API_MSG_DEBUG LWIP_DBG_ON
|
||||
#define TCPIP_DEBUG LWIP_DBG_ON
|
||||
#define NETIF_DEBUG LWIP_DBG_ON
|
||||
#define SOCKETS_DEBUG LWIP_DBG_ON
|
||||
#define IP_DEBUG LWIP_DBG_ON
|
||||
#define IP_REASS_DEBUG LWIP_DBG_ON
|
||||
#define RAW_DEBUG LWIP_DBG_ON
|
||||
#define ICMP_DEBUG LWIP_DBG_ON
|
||||
#define UDP_DEBUG LWIP_DBG_ON
|
||||
#define TCP_DEBUG LWIP_DBG_ON
|
||||
#define TCP_INPUT_DEBUG LWIP_DBG_ON
|
||||
#define TCP_OUTPUT_DEBUG LWIP_DBG_ON
|
||||
#define TCP_RTO_DEBUG LWIP_DBG_ON
|
||||
#define TCP_CWND_DEBUG LWIP_DBG_ON
|
||||
#define TCP_WND_DEBUG LWIP_DBG_ON
|
||||
#define TCP_FR_DEBUG LWIP_DBG_ON
|
||||
#define TCP_QLEN_DEBUG LWIP_DBG_ON
|
||||
#define TCP_RST_DEBUG LWIP_DBG_ON
|
||||
|
||||
/*
|
||||
---------- Performance tracking options ----------
|
||||
*/
|
||||
|
||||
/*
|
||||
---------- PPP options ----------
|
||||
*/
|
||||
#define PPP_SUPPORT 0
|
||||
|
||||
#endif /* LWIP_LWIPOPTS_H */
|
||||
42
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/delif.h
Normal file
42
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/delif.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_DELIF_H
|
||||
#define LWIP_DELIF_H
|
||||
|
||||
#include "lwip/netif.h"
|
||||
|
||||
#include "lwip/pbuf.h"
|
||||
|
||||
err_t delif_init(struct netif *netif);
|
||||
err_t delif_init_thread(struct netif *netif);
|
||||
|
||||
#endif /* LWIP_DELIF_H */
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_DROPIF_H
|
||||
#define LWIP_DROPIF_H
|
||||
|
||||
#include "lwip/netif.h"
|
||||
|
||||
#include "lwip/pbuf.h"
|
||||
|
||||
err_t dropif_init(struct netif *netif);
|
||||
|
||||
#endif /* LWIP_DROPIF_H */
|
||||
58
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/fifo.h
Normal file
58
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/fifo.h
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef FIFO_H
|
||||
#define FIFO_H
|
||||
|
||||
#include "lwip/sys.h"
|
||||
|
||||
/** How many bytes in fifo */
|
||||
#define FIFOSIZE 2048
|
||||
|
||||
/** fifo data structure, this one is passed to all fifo functions */
|
||||
typedef struct fifo_t {
|
||||
u8_t data[FIFOSIZE+10]; /* data segment, +10 is a hack probably not needed.. FIXME! */
|
||||
int dataslot; /* index to next char to be read */
|
||||
int emptyslot; /* index to next empty slot */
|
||||
int len; /* len probably not needed, may be calculated from dataslot and emptyslot in conjunction with FIFOSIZE */
|
||||
|
||||
sys_sem_t sem; /* semaphore protecting simultaneous data manipulation */
|
||||
sys_sem_t getSem; /* sepaphore used to signal new data if getWaiting is set */
|
||||
u8_t getWaiting; /* flag used to indicate that fifoget is waiting for data. fifoput is suposed to clear */
|
||||
/* this flag prior to signaling the getSem semaphore */
|
||||
} fifo_t;
|
||||
|
||||
|
||||
/**
|
||||
* Get a character from fifo
|
||||
* Blocking call.
|
||||
* @param fifo pointer to fifo data structure
|
||||
* @return character read from fifo
|
||||
*/
|
||||
u8_t fifoGet(fifo_t * fifo);
|
||||
|
||||
/**
|
||||
* Get a character from fifo
|
||||
* Non blocking call.
|
||||
* @param fifo pointer to fifo data structure
|
||||
* @return character read from fifo, or < zero if non was available
|
||||
*/
|
||||
s16_t fifoGetNonBlock(fifo_t * fifo);
|
||||
|
||||
/**
|
||||
* fifoput is called by the signalhandler when new data has arrived (or some other event is indicated)
|
||||
* fifoput reads directly from the serialport and is thus highly dependent on unix arch at this moment
|
||||
* @param fifo pointer to fifo data structure
|
||||
* @param fd unix file descriptor
|
||||
*/
|
||||
void fifoPut(fifo_t * fifo, int fd);
|
||||
|
||||
/**
|
||||
* fifoinit initiate fifo
|
||||
* @param fifo pointer to fifo data structure, allocated by the user
|
||||
*/
|
||||
void fifoInit(fifo_t * fifo);
|
||||
|
||||
#endif
|
||||
|
||||
30
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/list.h
Normal file
30
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/list.h
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LWIP_LIST_H
|
||||
#define LWIP_LIST_H
|
||||
|
||||
struct elem;
|
||||
|
||||
struct list {
|
||||
struct elem *first, *last;
|
||||
int size, elems;
|
||||
};
|
||||
|
||||
struct elem {
|
||||
struct elem *next;
|
||||
void *data;
|
||||
};
|
||||
|
||||
struct list *list_new(int size);
|
||||
int list_push(struct list *list, void *data);
|
||||
void *list_pop(struct list *list);
|
||||
void *list_first(struct list *list);
|
||||
int list_elems(struct list *list);
|
||||
void list_delete(struct list *list);
|
||||
int list_remove(struct list *list, void *elem);
|
||||
void list_map(struct list *list, void (* func)(void *arg));
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_PCAPIF_H
|
||||
#define LWIP_PCAPIF_H
|
||||
|
||||
#include "lwip/netif.h"
|
||||
|
||||
err_t pcapif_init(struct netif *netif);
|
||||
|
||||
#endif /* LWIP_PCAPIF_H */
|
||||
64
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/sio.h
Normal file
64
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/sio.h
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef SIO_UNIX_H
|
||||
#define SIO_UNIX_H
|
||||
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "netif/fifo.h"
|
||||
/*#include "netif/pppif.h"*/
|
||||
|
||||
struct sio_status_s {
|
||||
int fd;
|
||||
fifo_t myfifo;
|
||||
};
|
||||
|
||||
/* BAUDRATE is defined in sio.c as it is implementation specific */
|
||||
/** Baudrates */
|
||||
typedef enum sioBaudrates {
|
||||
SIO_BAUD_9600,
|
||||
SIO_BAUD_19200,
|
||||
SIO_BAUD_38400,
|
||||
SIO_BAUD_57600,
|
||||
SIO_BAUD_115200
|
||||
} sioBaudrates;
|
||||
|
||||
/**
|
||||
* Poll for a new character from incoming data stream
|
||||
* @param siostat siostatus struct, contains sio instance data, given by sio_open
|
||||
* @return char read from input stream, or < 0 if no char was available
|
||||
*/
|
||||
s16_t sio_poll(sio_status_t * siostat);
|
||||
|
||||
/**
|
||||
* Parse incoming characters until a string str is recieved, blocking call
|
||||
* @param str zero terminated string to expect
|
||||
* @param siostat siostatus struct, contains sio instance data, given by sio_open
|
||||
*/
|
||||
void sio_expect_string(u8_t *str, sio_status_t * siostat);
|
||||
|
||||
/**
|
||||
* Write a char to output data stream
|
||||
* @param str pointer to a zero terminated string
|
||||
* @param siostat siostatus struct, contains sio instance data, given by sio_open
|
||||
*/
|
||||
void sio_send_string(u8_t *str, sio_status_t * siostat);
|
||||
|
||||
/**
|
||||
* Flush outbuffer (send everything in buffer now), useful if some layer below is
|
||||
* holding on to data, waitng to fill a buffer
|
||||
* @param siostat siostatus struct, contains sio instance data, given by sio_open
|
||||
*/
|
||||
void sio_flush( sio_status_t * siostat );
|
||||
|
||||
/**
|
||||
* Change baudrate of port, may close and reopen port
|
||||
* @param baud new baudrate
|
||||
* @param siostat siostatus struct, contains sio instance data, given by sio_open
|
||||
*/
|
||||
void sio_change_baud( sioBaudrates baud, sio_status_t * siostat );
|
||||
|
||||
#endif
|
||||
|
||||
42
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/tapif.h
Normal file
42
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/tapif.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_TAPIF_H
|
||||
#define LWIP_TAPIF_H
|
||||
|
||||
#include "lwip/netif.h"
|
||||
|
||||
err_t tapif_init(struct netif *netif);
|
||||
#if NO_SYS
|
||||
int tapif_select(struct netif *netif);
|
||||
#endif /* NO_SYS */
|
||||
|
||||
#endif /* LWIP_TAPIF_H */
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_NETIF_TCPDUMP_H
|
||||
#define LWIP_NETIF_TCPDUMP_H
|
||||
|
||||
#include "lwip/pbuf.h"
|
||||
|
||||
void tcpdump_init(void);
|
||||
void tcpdump(struct pbuf *p);
|
||||
|
||||
#endif /* LWIP_NETIF_TCPDUMP_H */
|
||||
41
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/tunif.h
Normal file
41
Living_SDK/platform/mcu/linux/csp/lwip/include/netif/tunif.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_TUNIF_H
|
||||
#define LWIP_TUNIF_H
|
||||
|
||||
#include "lwip/netif.h"
|
||||
|
||||
#include "lwip/pbuf.h"
|
||||
|
||||
err_t tunif_init(struct netif *netif);
|
||||
|
||||
#endif /* LWIP_TUNIF_H */
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_UNIXIF_H
|
||||
#define LWIP_UNIXIF_H
|
||||
|
||||
#include "lwip/netif.h"
|
||||
|
||||
err_t unixif_init_server(struct netif *netif);
|
||||
err_t unixif_init_client(struct netif *netif);
|
||||
|
||||
#endif /* LWIP_UNIXIF_H */
|
||||
52
Living_SDK/platform/mcu/linux/csp/lwip/lwip_linuxhost.c
Normal file
52
Living_SDK/platform/mcu/linux/csp/lwip/lwip_linuxhost.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/tcpip.h"
|
||||
#include "lwip/dns.h"
|
||||
|
||||
#include "netif/tapif.h"
|
||||
#include "netif/etharp.h"
|
||||
|
||||
static ip4_addr_t ipaddr, netmask, gw;
|
||||
static struct netif netif;
|
||||
#define IP_MAX_LEN 17
|
||||
static void tcpip_init_done_callback(void *arg)
|
||||
{
|
||||
char ip_str[IP_MAX_LEN] = {0}, nm_str[IP_MAX_LEN] = {0}, gw_str[IP_MAX_LEN] = {0};
|
||||
/* startup defaults (may be overridden by one or more opts) */
|
||||
IP4_ADDR(&gw, 192,168,0,1);
|
||||
IP4_ADDR(&ipaddr, 192,168,0,2);
|
||||
IP4_ADDR(&netmask, 255,255,255,0);
|
||||
#if 0
|
||||
ip4_addr_t dnssrv;
|
||||
IP4_ADDR(&dnssrv,10,65,1,2);
|
||||
dns_setserver(0, (ip_addr_t *)&dnssrv);
|
||||
#endif
|
||||
strncpy(ip_str, ip4addr_ntoa(&ipaddr), sizeof(ip_str));
|
||||
ip_str[IP_MAX_LEN-1] = 0;
|
||||
strncpy(nm_str, ip4addr_ntoa(&netmask), sizeof(nm_str));
|
||||
nm_str[IP_MAX_LEN-1] = 0;
|
||||
strncpy(gw_str, ip4addr_ntoa(&gw), sizeof(gw_str));
|
||||
gw_str[IP_MAX_LEN-1] = 0;
|
||||
printf("Host at %s mask %s gateway %s\n", ip_str, nm_str, gw_str);
|
||||
|
||||
netif_add(&netif, &ipaddr, &netmask, &gw, NULL, tapif_init, ethernet_input);
|
||||
netif_set_default(&netif);
|
||||
netif_set_up(&netif);
|
||||
}
|
||||
|
||||
void krhino_lwip_init(int enable_tapif)
|
||||
{
|
||||
tcpip_init(NULL, NULL);
|
||||
|
||||
if (enable_tapif)
|
||||
tcpip_init_done_callback(NULL);
|
||||
printf("TCP/IP initialized.\n");
|
||||
}
|
||||
386
Living_SDK/platform/mcu/linux/csp/lwip/netif/delif.c
Normal file
386
Living_SDK/platform/mcu/linux/csp/lwip/netif/delif.c
Normal file
|
|
@ -0,0 +1,386 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/pbuf.h"
|
||||
|
||||
#if !NO_SYS
|
||||
|
||||
#include "lwip/debug.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lwip/def.h"
|
||||
#include "netif/delif.h"
|
||||
|
||||
#ifdef LWIP_UNIX_LINUX
|
||||
#include "netif/tapif.h"
|
||||
#else /* LWIP_UNIX_LINUX */
|
||||
#include "netif/tunif.h"
|
||||
#endif /* LWIP_UNIX_LINUX */
|
||||
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/timeouts.h"
|
||||
|
||||
#ifndef DELIF_DEBUG
|
||||
#define DELIF_DEBUG LWIP_DBG_OFF
|
||||
#endif
|
||||
|
||||
#define DELIF_INPUT_DROPRATE 0.1
|
||||
#define DELIF_OUTPUT_DROPRATE 0.1
|
||||
|
||||
#define DELIF_INPUT_DELAY 500 /* Miliseconds. */
|
||||
#define DELIF_OUTPUT_DELAY 500 /* Miliseconds. */
|
||||
|
||||
#define DELIF_TIMEOUT 10
|
||||
|
||||
struct delif {
|
||||
err_t (* input)(struct pbuf *p, struct netif *inp);
|
||||
struct netif *netif;
|
||||
};
|
||||
|
||||
struct delif_pbuf {
|
||||
struct delif_pbuf *next;
|
||||
struct pbuf *p;
|
||||
ip_addr_t ipaddr;
|
||||
unsigned int time;
|
||||
};
|
||||
|
||||
static struct delif_pbuf *input_list = NULL;
|
||||
static struct delif_pbuf *output_list = NULL;
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
delif_input_timeout(void *arg)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct delif *delif;
|
||||
struct delif_pbuf *dp;
|
||||
unsigned int timeout, now;
|
||||
|
||||
timeout = DELIF_TIMEOUT;
|
||||
|
||||
netif = (struct netif*)arg;
|
||||
delif = (struct delif*)netif->state;
|
||||
|
||||
/* Check if there is anything on the input list. */
|
||||
dp = input_list;
|
||||
while (dp != NULL) {
|
||||
now = sys_now();
|
||||
|
||||
if (dp->time <= now) {
|
||||
delif->input(dp->p, netif);
|
||||
if (dp->next != NULL) {
|
||||
if (dp->next->time > now) {
|
||||
timeout = dp->next->time - now;
|
||||
} else {
|
||||
timeout = 0;
|
||||
}
|
||||
LWIP_DEBUGF(DELIF_DEBUG, ("delif_output_timeout: timeout %u.\n", timeout));
|
||||
|
||||
}
|
||||
input_list = dp->next;
|
||||
free(dp);
|
||||
dp = input_list;
|
||||
} else {
|
||||
dp = dp->next;
|
||||
}
|
||||
}
|
||||
|
||||
sys_timeout(timeout, delif_input_timeout, arg);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
delif_output_timeout(void *arg)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct delif *delif;
|
||||
struct delif_pbuf *dp;
|
||||
unsigned int timeout, now;
|
||||
|
||||
timeout = DELIF_TIMEOUT;
|
||||
|
||||
netif = (struct netif*)arg;
|
||||
delif = (struct delif*)netif->state;
|
||||
|
||||
/* Check if there is anything on the output list. */
|
||||
dp = output_list;
|
||||
while (dp != NULL) {
|
||||
now = sys_now();
|
||||
if (dp->time <= now) {
|
||||
LWIP_DEBUGF(DELIF_DEBUG, ("delif_output_timeout: now %u dp->time %u\n",
|
||||
now, dp->time));
|
||||
|
||||
#if LWIP_IPV4
|
||||
if(!IP_IS_V6_VAL(dp->ipaddr)) {
|
||||
delif->netif->output(delif->netif, dp->p, ip_2_ip4(&dp->ipaddr));
|
||||
}
|
||||
#endif /* LWIP_IPV4 */
|
||||
|
||||
#if LWIP_IPV6
|
||||
if(IP_IS_V6_VAL(dp->ipaddr)) {
|
||||
delif->netif->output_ip6(delif->netif, dp->p, ip_2_ip6(&dp->ipaddr));
|
||||
}
|
||||
#endif /* LWIP_IPV6 */
|
||||
|
||||
if (dp->next != NULL) {
|
||||
if (dp->next->time > now) {
|
||||
timeout = dp->next->time - now;
|
||||
} else {
|
||||
timeout = 0;
|
||||
}
|
||||
LWIP_DEBUGF(DELIF_DEBUG, ("delif_output_timeout: timeout %u.\n", timeout));
|
||||
|
||||
}
|
||||
pbuf_free(dp->p);
|
||||
|
||||
output_list = dp->next;
|
||||
free(dp);
|
||||
dp = output_list;
|
||||
} else {
|
||||
dp = dp->next;
|
||||
}
|
||||
}
|
||||
|
||||
sys_timeout(timeout, delif_output_timeout, arg);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static err_t
|
||||
delif_output(struct netif *netif, struct pbuf *p, const ip_addr_t *ipaddr)
|
||||
{
|
||||
struct delif_pbuf *dp, *np;
|
||||
char *data;
|
||||
|
||||
LWIP_UNUSED_ARG(netif);
|
||||
|
||||
LWIP_DEBUGF(DELIF_DEBUG, ("delif_output\n"));
|
||||
|
||||
#ifdef DELIF_OUTPUT_DROPRATE
|
||||
if (((double)rand()/(double)RAND_MAX) < DELIF_OUTPUT_DROPRATE) {
|
||||
LWIP_DEBUGF(DELIF_DEBUG, ("delif_output: Packet dropped\n"));
|
||||
return ERR_BUF;
|
||||
}
|
||||
#endif /* DELIF_OUTPUT_DROPRATE */
|
||||
|
||||
LWIP_DEBUGF(DELIF_DEBUG, ("delif_output\n"));
|
||||
|
||||
dp = (struct delif_pbuf*)malloc(sizeof(struct delif_pbuf));
|
||||
data = (char*)malloc(p->tot_len);
|
||||
|
||||
pbuf_copy_partial(p, data, p->tot_len, 0);
|
||||
|
||||
dp->p = pbuf_alloc(PBUF_LINK, 0, PBUF_ROM);
|
||||
dp->p->payload = data;
|
||||
dp->p->len = p->tot_len;
|
||||
dp->p->tot_len = p->tot_len;
|
||||
dp->ipaddr = *ipaddr;
|
||||
dp->time = sys_now() + DELIF_OUTPUT_DELAY;
|
||||
dp->next = NULL;
|
||||
if (output_list == NULL) {
|
||||
output_list = dp;
|
||||
} else {
|
||||
for(np = output_list; np->next != NULL; np = np->next);
|
||||
np->next = dp;
|
||||
}
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
#if LWIP_IPV4
|
||||
static err_t
|
||||
delif_output4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
|
||||
{
|
||||
ip_addr_t ip;
|
||||
if (ipaddr != NULL) {
|
||||
ip_addr_copy_from_ip4(ip, *ipaddr);
|
||||
} else {
|
||||
ip = *IP_ADDR_ANY;
|
||||
}
|
||||
return delif_output(netif, p, &ip);
|
||||
}
|
||||
#endif /* LWIP_IPV4 */
|
||||
|
||||
#if LWIP_IPV6
|
||||
static err_t
|
||||
delif_output6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
|
||||
{
|
||||
ip_addr_t ip;
|
||||
if (ipaddr != NULL) {
|
||||
ip_addr_copy_from_ip6(ip, *ipaddr);
|
||||
} else {
|
||||
ip = *IP6_ADDR_ANY;
|
||||
}
|
||||
return delif_output(netif, p, &ip);
|
||||
}
|
||||
#endif /* LWIP_IPV6 */
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static err_t
|
||||
delif_input(struct pbuf *p, struct netif *inp)
|
||||
{
|
||||
struct delif_pbuf *dp, *np;
|
||||
|
||||
LWIP_UNUSED_ARG(inp);
|
||||
|
||||
LWIP_DEBUGF(DELIF_DEBUG, ("delif_input\n"));
|
||||
#ifdef DELIF_INPUT_DROPRATE
|
||||
if (((double)rand()/(double)RAND_MAX) < DELIF_INPUT_DROPRATE) {
|
||||
LWIP_DEBUGF(DELIF_DEBUG, ("delif_input: Packet dropped\n"));
|
||||
pbuf_free(p);
|
||||
return ERR_OK;
|
||||
}
|
||||
#endif /* DELIF_INPUT_DROPRATE */
|
||||
|
||||
dp = (struct delif_pbuf*)malloc(sizeof(struct delif_pbuf));
|
||||
dp->p = p;
|
||||
dp->time = sys_now() + DELIF_INPUT_DELAY;
|
||||
dp->next = NULL;
|
||||
|
||||
if (input_list == NULL) {
|
||||
input_list = dp;
|
||||
} else {
|
||||
for(np = input_list; np->next != NULL; np = np->next);
|
||||
np->next = dp;
|
||||
}
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
err_t
|
||||
delif_init(struct netif *netif)
|
||||
{
|
||||
struct delif *del;
|
||||
|
||||
del = (struct delif*)malloc(sizeof(struct delif));
|
||||
if (!del) {
|
||||
return ERR_MEM;
|
||||
}
|
||||
netif->state = del;
|
||||
netif->name[0] = 'd';
|
||||
netif->name[1] = 'e';
|
||||
|
||||
#if LWIP_IPV4
|
||||
netif->output = delif_output4;
|
||||
#endif /* LWIP_IPV4 */
|
||||
#if LWIP_IPV6
|
||||
netif->output_ip6 = delif_output6;
|
||||
#endif /* LWIP_IPV6 */
|
||||
|
||||
del->netif = (struct netif*)malloc(sizeof(struct netif));
|
||||
if (!del->netif) {
|
||||
free(del);
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
#ifdef LWIP_UNIX_LINUX
|
||||
tapif_init(del->netif);
|
||||
#else /* LWIP_UNIX_LINUX */
|
||||
tunif_init(del->netif);
|
||||
#endif /* LWIP_UNIX_LINUX */
|
||||
|
||||
del->input = netif->input;
|
||||
del->netif->input = delif_input;
|
||||
|
||||
sys_timeout(DELIF_TIMEOUT, delif_input_timeout, netif);
|
||||
sys_timeout(DELIF_TIMEOUT, delif_output_timeout, netif);
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
delif_thread(void *arg)
|
||||
{
|
||||
struct netif *netif = (struct netif*)arg;
|
||||
struct delif *del;
|
||||
sys_sem_t sem;
|
||||
|
||||
del = (struct delif*)netif->state;
|
||||
#ifdef LWIP_UNIX_LINUX
|
||||
tapif_init(del->netif);
|
||||
#else /* LWIP_UNIX_LINUX */
|
||||
tunif_init(del->netif);
|
||||
#endif /* LWIP_UNIX_LINUX */
|
||||
|
||||
sys_timeout(DELIF_TIMEOUT, delif_input_timeout, netif);
|
||||
sys_timeout(DELIF_TIMEOUT, delif_output_timeout, netif);
|
||||
|
||||
if(sys_sem_new(&sem, 0) != ERR_OK) {
|
||||
LWIP_ASSERT("Failed to create semaphore", 0);
|
||||
}
|
||||
sys_sem_wait(&sem);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
err_t
|
||||
delif_init_thread(struct netif *netif)
|
||||
{
|
||||
struct delif *del;
|
||||
|
||||
LWIP_DEBUGF(DELIF_DEBUG, ("delif_init_thread\n"));
|
||||
|
||||
del = (struct delif*)malloc(sizeof(struct delif));
|
||||
if (!del) {
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
netif->state = del;
|
||||
netif->name[0] = 'd';
|
||||
netif->name[1] = 'e';
|
||||
|
||||
del->netif = (struct netif*)malloc(sizeof(struct netif));
|
||||
if (!del->netif) {
|
||||
free(del);
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
#if LWIP_IPV4
|
||||
netif->output = delif_output4;
|
||||
netif_set_ipaddr(del->netif, netif_ip4_addr(netif));
|
||||
netif_set_gw(del->netif, netif_ip4_gw(netif));
|
||||
netif_set_netmask(del->netif, netif_ip4_netmask(netif));
|
||||
#endif /* LWIP_IPV4 */
|
||||
|
||||
#if LWIP_IPV6
|
||||
{
|
||||
int i;
|
||||
netif->output_ip6 = delif_output6;
|
||||
for(i=0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
|
||||
netif_ip6_addr_set(del->netif, i, netif_ip6_addr(netif, i));
|
||||
}
|
||||
}
|
||||
#endif /* LWIP_IPV6 */
|
||||
|
||||
del->input = netif->input;
|
||||
del->netif->input = delif_input;
|
||||
|
||||
sys_thread_new("delif_thread", delif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
#endif /* !NO_SYS */
|
||||
143
Living_SDK/platform/mcu/linux/csp/lwip/netif/fifo.c
Normal file
143
Living_SDK/platform/mcu/linux/csp/lwip/netif/fifo.c
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
/* Author: Magnus Ivarsson <magnus.ivarsson@volvo.com> */
|
||||
|
||||
/* ---------------------------------------------- */
|
||||
/* --- fifo 4 unix ------------------------------ */
|
||||
/* ---------------------------------------------- */
|
||||
#include "lwip/err.h"
|
||||
#include "netif/fifo.h"
|
||||
#include "lwip/debug.h"
|
||||
#include "lwip/def.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/arch.h"
|
||||
#include <unistd.h>
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
#ifndef SIO_FIFO_DEBUG
|
||||
#define SIO_FIFO_DEBUG LWIP_DBG_OFF
|
||||
#endif
|
||||
|
||||
u8_t fifoGet(fifo_t * fifo)
|
||||
{
|
||||
u8_t c;
|
||||
|
||||
sys_sem_wait(&fifo->sem); /* enter critical section */
|
||||
|
||||
if (fifo->dataslot == fifo->emptyslot)
|
||||
{
|
||||
fifo->getWaiting = TRUE; /* tell putFifo to signal us when data is available */
|
||||
sys_sem_signal(&fifo->sem); /* leave critical section (allow input from serial port..) */
|
||||
sys_sem_wait(&fifo->getSem); /* wait 4 data */
|
||||
sys_sem_wait(&fifo->sem); /* reenter critical section */
|
||||
}
|
||||
|
||||
c = fifo->data[fifo->dataslot++];
|
||||
fifo->len--;
|
||||
|
||||
if (fifo->dataslot == FIFOSIZE)
|
||||
{
|
||||
fifo->dataslot = 0;
|
||||
}
|
||||
sys_sem_signal(&fifo->sem); /* leave critical section */
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
s16_t fifoGetNonBlock(fifo_t * fifo)
|
||||
{
|
||||
u16_t c;
|
||||
|
||||
sys_sem_wait(&fifo->sem); /* enter critical section */
|
||||
|
||||
if (fifo->dataslot == fifo->emptyslot)
|
||||
{
|
||||
/* empty fifo */
|
||||
c = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
c = fifo->data[fifo->dataslot++];
|
||||
fifo->len--;
|
||||
|
||||
if (fifo->dataslot == FIFOSIZE)
|
||||
{
|
||||
fifo->dataslot = 0;
|
||||
}
|
||||
}
|
||||
sys_sem_signal(&fifo->sem); /* leave critical section */
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
void fifoPut(fifo_t * fifo, int fd)
|
||||
{
|
||||
/* FIXME: mutex around struct data.. */
|
||||
int cnt=0;
|
||||
|
||||
sys_sem_wait(&fifo->sem ); /* enter critical */
|
||||
|
||||
LWIP_DEBUGF( SIO_FIFO_DEBUG,("fifoput: len%d dat%d empt%d --> ", fifo->len, fifo->dataslot, fifo->emptyslot ) );
|
||||
|
||||
if ( fifo->emptyslot < fifo->dataslot )
|
||||
{
|
||||
cnt = read( fd, &fifo->data[fifo->emptyslot], fifo->dataslot - fifo->emptyslot );
|
||||
}
|
||||
else
|
||||
{
|
||||
cnt = read( fd, &fifo->data[fifo->emptyslot], FIFOSIZE-fifo->emptyslot );
|
||||
}
|
||||
fifo->emptyslot += cnt;
|
||||
fifo->len += cnt;
|
||||
|
||||
LWIP_DEBUGF( SIO_FIFO_DEBUG,("len%d dat%d empt%d\n", fifo->len, fifo->dataslot, fifo->emptyslot ) );
|
||||
|
||||
if ( fifo->len > FIFOSIZE )
|
||||
{
|
||||
printf( "ERROR: fifo overrun detected len=%d, flushing\n", fifo->len );
|
||||
fifo->dataslot = 0;
|
||||
fifo->emptyslot = 0;
|
||||
fifo->len = 0;
|
||||
}
|
||||
|
||||
if ( fifo->emptyslot == FIFOSIZE )
|
||||
{
|
||||
fifo->emptyslot = 0;
|
||||
LWIP_DEBUGF( SIO_FIFO_DEBUG, ("(WRAP) ") );
|
||||
|
||||
sys_sem_signal(&fifo->sem ); /* leave critical */
|
||||
fifoPut( fifo, fd );
|
||||
return;
|
||||
}
|
||||
if ( fifo->getWaiting )
|
||||
{
|
||||
fifo->getWaiting = FALSE;
|
||||
sys_sem_signal(&fifo->getSem );
|
||||
}
|
||||
|
||||
sys_sem_signal(&fifo->sem ); /* leave critical */
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void fifoInit(fifo_t * fifo)
|
||||
{
|
||||
fifo->dataslot = 0;
|
||||
fifo->emptyslot = 0;
|
||||
fifo->len = 0;
|
||||
if(sys_sem_new(&fifo->sem, 1) != ERR_OK) { /* critical section 1=free to enter */
|
||||
LWIP_ASSERT("Failed to create semaphore", 0);
|
||||
}
|
||||
if(sys_sem_new(&fifo->getSem, 0) != ERR_OK) { /* 0 = no one waiting */
|
||||
LWIP_ASSERT("Failed to create semaphore", 0);
|
||||
}
|
||||
fifo->getWaiting = FALSE;
|
||||
}
|
||||
153
Living_SDK/platform/mcu/linux/csp/lwip/netif/list.c
Normal file
153
Living_SDK/platform/mcu/linux/csp/lwip/netif/list.c
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <netif/list.h>
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
struct list *
|
||||
list_new(int size)
|
||||
{
|
||||
struct list *list;
|
||||
list = (struct list *)malloc(sizeof(struct list));
|
||||
list->first = list->last = NULL;
|
||||
list->size = size;
|
||||
list->elems = 0;
|
||||
return list;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
int
|
||||
list_push(struct list *list, void *data)
|
||||
{
|
||||
struct elem *elem;
|
||||
|
||||
if (list->elems < list->size) {
|
||||
elem = (struct elem *)malloc(sizeof(struct elem));
|
||||
elem->data = data;
|
||||
elem->next = NULL;
|
||||
if (list->last != NULL) {
|
||||
list->last->next = elem;
|
||||
}
|
||||
list->last = elem;
|
||||
if (list->first == NULL) {
|
||||
list->first = elem;
|
||||
}
|
||||
list->elems++;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void *
|
||||
list_pop(struct list *list)
|
||||
{
|
||||
struct elem *elem;
|
||||
void *data;
|
||||
|
||||
if (list->elems > 0) {
|
||||
elem = list->first;
|
||||
if (elem == list->last) {
|
||||
list->last = elem->next;
|
||||
}
|
||||
list->first = elem->next;
|
||||
|
||||
list->elems--;
|
||||
|
||||
data = elem->data;
|
||||
free(elem);
|
||||
|
||||
return data;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void *
|
||||
list_first(struct list *list)
|
||||
{
|
||||
return list->first;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
int
|
||||
list_elems(struct list *list)
|
||||
{
|
||||
return list->elems;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void
|
||||
list_delete(struct list *list)
|
||||
{
|
||||
while (list_pop(list) != NULL);
|
||||
free(list);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
int
|
||||
list_remove(struct list *list, void *elem)
|
||||
{
|
||||
struct elem *e, *p;
|
||||
|
||||
p = NULL;
|
||||
for(e = list->first; e != NULL; e = e->next) {
|
||||
if (e->data == elem) {
|
||||
if (p != NULL) {
|
||||
p->next = e->next;
|
||||
} else {
|
||||
list->first = e->next;
|
||||
}
|
||||
if (list->last == e) {
|
||||
list->last = p;
|
||||
if (p != NULL) {
|
||||
p->next = NULL;
|
||||
}
|
||||
}
|
||||
free(e);
|
||||
list->elems--;
|
||||
return 1;
|
||||
}
|
||||
p = e;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void
|
||||
list_map(struct list *list, void (* func)(void *arg))
|
||||
{
|
||||
struct elem *e;
|
||||
|
||||
for(e = list->first; e != NULL; e = e->next) {
|
||||
func(e->data);
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
216
Living_SDK/platform/mcu/linux/csp/lwip/netif/pcapif.c
Normal file
216
Living_SDK/platform/mcu/linux/csp/lwip/netif/pcapif.c
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef linux /* Apparently, this doesn't work under Linux. */
|
||||
|
||||
#include "lwip/debug.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/stat.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <pcap.h>
|
||||
|
||||
#include "netif/etharp.h"
|
||||
|
||||
#include "lwip/stats.h"
|
||||
|
||||
#include "lwip/def.h"
|
||||
#include "lwip/mem.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "netif/unixif.h"
|
||||
#include "lwip/sys.h"
|
||||
|
||||
#include "lwip/ip.h"
|
||||
|
||||
|
||||
#if defined(LWIP_DEBUG) && defined(LWIP_TCPDUMP)
|
||||
#include "netif/tcpdump.h"
|
||||
#endif /* LWIP_DEBUG && LWIP_TCPDUMP */
|
||||
|
||||
struct pcapif {
|
||||
pcap_t *pd;
|
||||
sys_sem_t sem;
|
||||
u8_t pkt[2048];
|
||||
u32_t len;
|
||||
u32_t lasttime;
|
||||
struct pbuf *p;
|
||||
struct eth_addr *ethaddr;
|
||||
};
|
||||
|
||||
static char errbuf[PCAP_ERRBUF_SIZE];
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static err_t
|
||||
pcapif_output(struct netif *netif, struct pbuf *p,
|
||||
ip_addr_t *ipaddr)
|
||||
{
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
timeout(void *arg)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct pcapif *pcapif;
|
||||
struct pbuf *p;
|
||||
struct eth_hdr *ethhdr;
|
||||
|
||||
netif = (struct netif *)arg;
|
||||
pcapif = netif->state;
|
||||
ethhdr = (struct eth_hdr *)pcapif->pkt;
|
||||
|
||||
|
||||
if (lwip_htons(ethhdr->type) != ETHTYPE_IP ||
|
||||
ip_lookup(pcapif->pkt + 14, netif)) {
|
||||
|
||||
/* We allocate a pbuf chain of pbufs from the pool. */
|
||||
p = pbuf_alloc(PBUF_LINK, pcapif->len, PBUF_POOL);
|
||||
|
||||
if (p != NULL) {
|
||||
pbuf_take(p, pcapif->pkt, pcapif->len);
|
||||
|
||||
#if defined(LWIP_DEBUG) && defined(LWIP_TCPDUMP)
|
||||
tcpdump(p);
|
||||
#endif /* LWIP_DEBUG && LWIP_TCPDUMP */
|
||||
|
||||
ethhdr = p->payload;
|
||||
switch (lwip_htons(ethhdr->type)) {
|
||||
/* IP or ARP packet? */
|
||||
case ETHTYPE_IP:
|
||||
case ETHTYPE_ARP:
|
||||
#if PPPOE_SUPPORT
|
||||
/* PPPoE packet? */
|
||||
case ETHTYPE_PPPOEDISC:
|
||||
case ETHTYPE_PPPOE:
|
||||
#endif /* PPPOE_SUPPORT */
|
||||
/* full packet send to tcpip_thread to process */
|
||||
if (netif->input(p, netif) != ERR_OK) {
|
||||
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
|
||||
pbuf_free(p);
|
||||
p = NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
pbuf_free(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
printf("ip_lookup dropped\n");
|
||||
}
|
||||
|
||||
sys_sem_signal(&pcapif->sem);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
callback(u_char *arg, const struct pcap_pkthdr *hdr, const u_char *pkt)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct pcapif *pcapif;
|
||||
u32_t time, lasttime;
|
||||
|
||||
netif = (struct netif *)arg;
|
||||
pcapif = netif->state;
|
||||
|
||||
pcapif->len = hdr->len;
|
||||
|
||||
bcopy(pkt, pcapif->pkt, hdr->len);
|
||||
|
||||
time = hdr->ts.tv_sec * 1000 + hdr->ts.tv_usec / 1000;
|
||||
|
||||
lasttime = pcapif->lasttime;
|
||||
pcapif->lasttime = time;
|
||||
|
||||
|
||||
if (lasttime == 0) {
|
||||
sys_timeout(1000, timeout, netif);
|
||||
} else {
|
||||
sys_timeout(time - lasttime, timeout, netif);
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
pcapif_thread(void *arg)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct pcapif *pcapif;
|
||||
netif = arg;
|
||||
pcapif = netif->state;
|
||||
|
||||
while (1) {
|
||||
pcap_loop(pcapif->pd, 1, callback, (u_char *)netif);
|
||||
sys_sem_wait(&pcapif->sem);
|
||||
if (pcapif->p != NULL) {
|
||||
netif->input(pcapif->p, netif);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
err_t
|
||||
pcapif_init(struct netif *netif)
|
||||
{
|
||||
struct pcapif *p;
|
||||
|
||||
p = malloc(sizeof(struct pcapif));
|
||||
if (p == NULL)
|
||||
return ERR_MEM;
|
||||
netif->state = p;
|
||||
netif->name[0] = 'p';
|
||||
netif->name[1] = 'c';
|
||||
netif->output = pcapif_output;
|
||||
|
||||
p->pd = pcap_open_offline("pcapdump", errbuf);
|
||||
if (p->pd == NULL) {
|
||||
printf("pcapif_init: failed %s\n", errbuf);
|
||||
return ERR_IF;
|
||||
}
|
||||
|
||||
if(sys_sem_new(&p->sem, 0) != ERR_OK) {
|
||||
LWIP_ASSERT("Failed to create semaphore", 0);
|
||||
}
|
||||
p->p = NULL;
|
||||
p->lasttime = 0;
|
||||
|
||||
sys_thread_new("pcapif_thread", pcapif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
#endif /* linux */
|
||||
487
Living_SDK/platform/mcu/linux/csp/lwip/netif/sio.c
Normal file
487
Living_SDK/platform/mcu/linux/csp/lwip/netif/sio.c
Normal file
|
|
@ -0,0 +1,487 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
/* Author: Magnus Ivarsson <magnus.ivarsson@volvo.com> */
|
||||
|
||||
/* to get rid of implicit function declarations */
|
||||
#define _XOPEN_SOURCE 600
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include "netif/sio.h"
|
||||
#include "netif/fifo.h"
|
||||
#include "lwip/debug.h"
|
||||
#include "lwip/def.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/arch.h"
|
||||
#include "lwip/sio.h"
|
||||
#include "netif/ppp/ppp_opts.h"
|
||||
|
||||
/* Following #undefs are here to keep compiler from issuing warnings
|
||||
about them being double defined. (They are defined in lwip/inet.h
|
||||
as well as the Unix #includes below.) */
|
||||
#undef htonl
|
||||
#undef ntohl
|
||||
#undef htons
|
||||
#undef ntohs
|
||||
#undef HTONL
|
||||
#undef NTOHL
|
||||
#undef HTONS
|
||||
#undef NTOHS
|
||||
|
||||
#define __USE_BSD
|
||||
#define __USE_MISC
|
||||
#define _GNU_SOURCE
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#if defined(LWIP_UNIX_OPENBSD)
|
||||
#include <util.h>
|
||||
#endif
|
||||
#include <termios.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <sys/signal.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifndef LWIP_HAVE_SLIPIF
|
||||
#define LWIP_HAVE_SLIPIF 0
|
||||
#endif
|
||||
|
||||
#if (PPP_SUPPORT || LWIP_HAVE_SLIPIF) && defined(LWIP_UNIX_LINUX)
|
||||
#include <pty.h>
|
||||
#endif
|
||||
|
||||
/*#define BAUDRATE B19200 */
|
||||
/*#define BAUDRATE B57600 */
|
||||
#define BAUDRATE B115200
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
/* for all of you who dont define SIO_DEBUG in debug.h */
|
||||
#ifndef SIO_DEBUG
|
||||
#define SIO_DEBUG 0
|
||||
#endif
|
||||
|
||||
|
||||
/* typedef struct siostruct_t */
|
||||
/* { */
|
||||
/* sio_status_t *sio; */
|
||||
/* } siostruct_t; */
|
||||
|
||||
/** array of ((siostruct*)netif->state)->sio structs */
|
||||
static sio_status_t statusar[4];
|
||||
|
||||
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
|
||||
/* --private-functions----------------------------------------------------------------- */
|
||||
/**
|
||||
* Signal handler for ttyXX0 to indicate bytes received
|
||||
* one per interface is needed since we cannot send a instance number / pointer as callback argument (?)
|
||||
*/
|
||||
static void signal_handler_IO_0( int status )
|
||||
{
|
||||
LWIP_UNUSED_ARG(status);
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("SigHand: rxSignal channel 0\n"));
|
||||
fifoPut( &statusar[0].myfifo, statusar[0].fd );
|
||||
}
|
||||
|
||||
/**
|
||||
* Signal handler for ttyXX1 to indicate bytes received
|
||||
* one per interface is needed since we cannot send a instance number / pointer as callback argument (?)
|
||||
*/
|
||||
static void signal_handler_IO_1( int status )
|
||||
{
|
||||
LWIP_UNUSED_ARG(status);
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("SigHand: rxSignal channel 1\n"));
|
||||
fifoPut( &statusar[1].myfifo, statusar[1].fd );
|
||||
}
|
||||
#endif /* ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF) */
|
||||
|
||||
/**
|
||||
* Initiation of serial device
|
||||
* @param device string with the device name and path, eg. "/dev/ttyS0"
|
||||
* @param devnum device number
|
||||
* @param siostat status
|
||||
* @return file handle to serial dev.
|
||||
*/
|
||||
static int sio_init( char * device, int devnum, sio_status_t * siostat )
|
||||
{
|
||||
struct termios oldtio,newtio;
|
||||
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
|
||||
struct sigaction saio; /* definition of signal action */
|
||||
#endif
|
||||
int fd;
|
||||
LWIP_UNUSED_ARG(siostat);
|
||||
LWIP_UNUSED_ARG(devnum);
|
||||
|
||||
/* open the device to be non-blocking (read will return immediately) */
|
||||
fd = open( device, O_RDWR | O_NOCTTY | O_NONBLOCK );
|
||||
if ( fd < 0 )
|
||||
{
|
||||
perror( device );
|
||||
exit( -1 );
|
||||
}
|
||||
|
||||
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
|
||||
/* install the signal handler before making the device asynchronous */
|
||||
switch ( devnum )
|
||||
{
|
||||
case 0:
|
||||
LWIP_DEBUGF( SIO_DEBUG, ("sioinit, signal_handler_IO_0\n") );
|
||||
saio.sa_handler = signal_handler_IO_0;
|
||||
break;
|
||||
case 1:
|
||||
LWIP_DEBUGF( SIO_DEBUG, ("sioinit, signal_handler_IO_1\n") );
|
||||
saio.sa_handler = signal_handler_IO_1;
|
||||
break;
|
||||
default:
|
||||
LWIP_DEBUGF( SIO_DEBUG,("sioinit, devnum not allowed\n") );
|
||||
break;
|
||||
}
|
||||
sigemptyset(&saio.sa_mask);
|
||||
saio.sa_flags = 0;
|
||||
#if defined(LWIP_UNIX_LINUX)
|
||||
saio.sa_restorer = NULL;
|
||||
#endif /* LWIP_UNIX_LINUX */
|
||||
sigaction( SIGIO,&saio,NULL );
|
||||
|
||||
/* allow the process to receive SIGIO */
|
||||
if ( fcntl( fd, F_SETOWN, getpid( ) ) != 0)
|
||||
{
|
||||
perror( device );
|
||||
exit( -1 );
|
||||
}
|
||||
/* Make the file descriptor asynchronous (the manual page says only
|
||||
O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
|
||||
if ( fcntl( fd, F_SETFL, FASYNC ) != 0)
|
||||
{
|
||||
perror( device );
|
||||
exit( -1 );
|
||||
}
|
||||
#else
|
||||
if ( fcntl( fd, F_SETFL, 0 ) != 0)
|
||||
{
|
||||
perror( device );
|
||||
exit( -1 );
|
||||
}
|
||||
|
||||
#endif /* ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF) */
|
||||
|
||||
tcgetattr( fd,&oldtio ); /* save current port settings */
|
||||
/* set new port settings */
|
||||
/* see 'man termios' for further settings */
|
||||
memset(&newtio, 0, sizeof(newtio));
|
||||
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD | CRTSCTS;
|
||||
newtio.c_iflag = 0;
|
||||
newtio.c_oflag = 0;
|
||||
newtio.c_lflag = 0; /*ECHO; */
|
||||
newtio.c_cc[VMIN] = 1; /* Read 1 byte at a time, no timer */
|
||||
newtio.c_cc[VTIME] = 0;
|
||||
|
||||
tcsetattr( fd,TCSANOW,&newtio );
|
||||
tcflush( fd, TCIOFLUSH );
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static void sio_speed( int fd, int speed )
|
||||
{
|
||||
struct termios oldtio,newtio;
|
||||
/* int fd; */
|
||||
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_speed[%d]: baudcode:%d enter\n", fd, speed));
|
||||
|
||||
if ( fd < 0 )
|
||||
{
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_speed[%d]: fd ERROR\n", fd));
|
||||
exit( -1 );
|
||||
}
|
||||
|
||||
tcgetattr( fd,&oldtio ); /* get current port settings */
|
||||
|
||||
/* set new port settings
|
||||
* see 'man termios' for further settings */
|
||||
memset(&newtio, 0, sizeof(newtio));
|
||||
newtio.c_cflag = speed | CS8 | CLOCAL | CREAD; /* | CRTSCTS; */
|
||||
newtio.c_iflag = 0;
|
||||
newtio.c_oflag = 0;
|
||||
newtio.c_lflag = 0; /*ECHO; */
|
||||
newtio.c_cc[VMIN] = 1; /* Read 1 byte at a time, no timer */
|
||||
newtio.c_cc[VTIME] = 0;
|
||||
|
||||
tcsetattr( fd,TCSANOW,&newtio );
|
||||
tcflush( fd, TCIOFLUSH );
|
||||
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_speed[%d]: leave\n", fd));
|
||||
}
|
||||
|
||||
/* --public-functions----------------------------------------------------------------------------- */
|
||||
void sio_send( u8_t c, sio_status_t * siostat )
|
||||
{
|
||||
/* sio_status_t * siostat = ((siostruct_t*)netif->state)->sio; */
|
||||
|
||||
if ( write( siostat->fd, &c, 1 ) <= 0 )
|
||||
{
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_send[%d]: write refused\n", siostat->fd));
|
||||
}
|
||||
}
|
||||
|
||||
void sio_send_string( u8_t *str, sio_status_t * siostat )
|
||||
{
|
||||
/* sio_status_t * siostat = ((siostruct_t*)netif->state)->sio; */
|
||||
int len = strlen( (const char *)str );
|
||||
|
||||
if ( write( siostat->fd, str, len ) <= 0 )
|
||||
{
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_send_string[%d]: write refused\n", siostat->fd));
|
||||
}
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_send_string[%d]: sent: %s\n", siostat->fd, str));
|
||||
}
|
||||
|
||||
|
||||
void sio_flush( sio_status_t * siostat )
|
||||
{
|
||||
LWIP_UNUSED_ARG(siostat);
|
||||
/* not implemented in unix as it is not needed */
|
||||
/*sio_status_t * siostat = ((siostruct_t*)netif->state)->sio; */
|
||||
}
|
||||
|
||||
|
||||
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
|
||||
/*u8_t sio_recv( struct netif * netif )*/
|
||||
u8_t sio_recv( sio_status_t * siostat )
|
||||
{
|
||||
/* sio_status_t * siostat = ((siostruct_t*)netif->state)->sio; */
|
||||
return fifoGet( &(siostat->myfifo) );
|
||||
}
|
||||
|
||||
s16_t sio_poll(sio_status_t * siostat)
|
||||
{
|
||||
/* sio_status_t * siostat = ((siostruct_t*)netif->state)->sio;*/
|
||||
return fifoGetNonBlock( &(siostat->myfifo) );
|
||||
}
|
||||
|
||||
|
||||
void sio_expect_string( u8_t *str, sio_status_t * siostat )
|
||||
{
|
||||
/* sio_status_t * siostat = ((siostruct_t*)netif->state)->sio;*/
|
||||
u8_t c;
|
||||
int finger=0;
|
||||
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_expect_string[%d]: %s\n", siostat->fd, str));
|
||||
while ( 1 )
|
||||
{
|
||||
c=fifoGet( &(siostat->myfifo) );
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("_%c", c));
|
||||
if ( c==str[finger] )
|
||||
{
|
||||
finger++;
|
||||
} else if ( finger > 0 )
|
||||
{
|
||||
/*it might fit in the beginning? */
|
||||
if ( str[0] == c )
|
||||
{
|
||||
finger = 1;
|
||||
}
|
||||
}
|
||||
if ( 0 == str[finger] )
|
||||
break; /* done, we have a match */
|
||||
}
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_expect_string[%d]: [match]\n", siostat->fd));
|
||||
}
|
||||
#endif /* ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF) */
|
||||
|
||||
#if (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
|
||||
u32_t sio_write(sio_status_t * siostat, u8_t *buf, u32_t size)
|
||||
{
|
||||
ssize_t wsz = write( siostat->fd, buf, size );
|
||||
return wsz < 0 ? 0 : wsz;
|
||||
}
|
||||
|
||||
u32_t sio_read(sio_status_t * siostat, u8_t *buf, u32_t size)
|
||||
{
|
||||
ssize_t rsz = read( siostat->fd, buf, size );
|
||||
return rsz < 0 ? 0 : rsz;
|
||||
}
|
||||
|
||||
void sio_read_abort(sio_status_t * siostat)
|
||||
{
|
||||
LWIP_UNUSED_ARG(siostat);
|
||||
printf("sio_read_abort[%d]: not yet implemented for unix\n", siostat->fd);
|
||||
}
|
||||
#endif /* (PPP_SUPPORT || LWIP_HAVE_SLIPIF) */
|
||||
|
||||
sio_fd_t sio_open(u8_t devnum)
|
||||
{
|
||||
char dev[20];
|
||||
|
||||
/* would be nice with dynamic memory alloc */
|
||||
sio_status_t * siostate = &statusar[ devnum ];
|
||||
/* siostruct_t * tmp; */
|
||||
|
||||
|
||||
/* tmp = (siostruct_t*)(netif->state); */
|
||||
/* tmp->sio = siostate; */
|
||||
|
||||
/* tmp = (siostruct_t*)(netif->state); */
|
||||
|
||||
/* ((sio_status_t*)(tmp->sio))->fd = 0; */
|
||||
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_open: for devnum %d\n", devnum));
|
||||
|
||||
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
|
||||
fifoInit( &siostate->myfifo );
|
||||
#endif /* ! PPP_SUPPORT */
|
||||
|
||||
snprintf( dev, sizeof(dev), "/dev/ttyS%d", devnum );
|
||||
|
||||
if ( (devnum == 1) || (devnum == 0) )
|
||||
{
|
||||
if ( ( siostate->fd = sio_init( dev, devnum, siostate ) ) == 0 )
|
||||
{
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_open: ERROR opening serial device dev=%s\n", dev));
|
||||
abort( );
|
||||
return NULL;
|
||||
}
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: dev=%s open.\n", siostate->fd, dev));
|
||||
}
|
||||
#if PPP_SUPPORT
|
||||
else if (devnum == 2) {
|
||||
pid_t childpid;
|
||||
char name[256];
|
||||
childpid = forkpty(&siostate->fd, name, NULL, NULL);
|
||||
if(childpid < 0) {
|
||||
perror("forkpty");
|
||||
exit (1);
|
||||
}
|
||||
if(childpid == 0) {
|
||||
execl("/usr/sbin/pppd", "pppd",
|
||||
"ms-dns", "198.168.100.7",
|
||||
"local", "crtscts",
|
||||
"debug",
|
||||
#ifdef LWIP_PPP_CHAP_TEST
|
||||
"auth",
|
||||
"require-chap",
|
||||
"remotename", "lwip",
|
||||
#else
|
||||
"noauth",
|
||||
#endif
|
||||
#if LWIP_IPV6
|
||||
"+ipv6",
|
||||
#endif
|
||||
"192.168.1.1:192.168.1.2",
|
||||
NULL);
|
||||
perror("execl pppd");
|
||||
exit (1);
|
||||
} else {
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: spawned pppd pid %d on %s\n",
|
||||
siostate->fd, childpid, name));
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
#if LWIP_HAVE_SLIPIF
|
||||
else if (devnum == 3) {
|
||||
pid_t childpid;
|
||||
/* create PTY pair */
|
||||
siostate->fd = posix_openpt(O_RDWR | O_NOCTTY);
|
||||
if (siostate->fd < 0) {
|
||||
perror("open pty master");
|
||||
exit (1);
|
||||
}
|
||||
if (grantpt(siostate->fd) != 0) {
|
||||
perror("grant pty master");
|
||||
exit (1);
|
||||
}
|
||||
if (unlockpt(siostate->fd) != 0) {
|
||||
perror("unlock pty master");
|
||||
exit (1);
|
||||
}
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: for %s\n",
|
||||
siostate->fd, ptsname(siostate->fd)));
|
||||
/* fork for slattach */
|
||||
childpid = fork();
|
||||
if(childpid < 0) {
|
||||
perror("fork");
|
||||
exit (1);
|
||||
}
|
||||
if(childpid == 0) {
|
||||
/* esteblish SLIP interface on host side connected to PTY slave */
|
||||
execl("/sbin/slattach", "slattach",
|
||||
"-d", "-v", "-L", "-p", "slip",
|
||||
ptsname(siostate->fd),
|
||||
NULL);
|
||||
perror("execl slattach");
|
||||
exit (1);
|
||||
} else {
|
||||
int ret;
|
||||
char buf[1024];
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: spawned slattach pid %d on %s\n",
|
||||
siostate->fd, childpid, ptsname(siostate->fd)));
|
||||
/* wait a moment for slattach startup */
|
||||
sleep(1);
|
||||
/* configure SLIP interface on host side as P2P interface */
|
||||
snprintf(buf, sizeof(buf),
|
||||
"/sbin/ifconfig sl0 mtu %d %s pointopoint %s up",
|
||||
SLIP_MAX_SIZE, "192.168.2.1", "192.168.2.2");
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: system(\"%s\");\n", siostate->fd, buf));
|
||||
ret = system(buf);
|
||||
if (ret < 0) {
|
||||
perror("ifconfig failed");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* LWIP_HAVE_SLIPIF */
|
||||
else
|
||||
{
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_open: device %s (%d) is not supported\n", dev, devnum));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return siostate;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
void sio_change_baud( sioBaudrates baud, sio_status_t * siostat )
|
||||
{
|
||||
/* sio_status_t * siostat = ((siostruct_t*)netif->state)->sio;*/
|
||||
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_change_baud[%d]\n", siostat->fd));
|
||||
|
||||
switch ( baud )
|
||||
{
|
||||
case SIO_BAUD_9600:
|
||||
sio_speed( siostat->fd, B9600 );
|
||||
break;
|
||||
case SIO_BAUD_19200:
|
||||
sio_speed( siostat->fd, B19200 );
|
||||
break;
|
||||
case SIO_BAUD_38400:
|
||||
sio_speed( siostat->fd, B38400 );
|
||||
break;
|
||||
case SIO_BAUD_57600:
|
||||
sio_speed( siostat->fd, B57600 );
|
||||
break;
|
||||
case SIO_BAUD_115200:
|
||||
sio_speed( siostat->fd, B115200 );
|
||||
break;
|
||||
|
||||
default:
|
||||
LWIP_DEBUGF(SIO_DEBUG, ("sio_change_baud[%d]: Unknown baudrate, code:%d\n",
|
||||
siostat->fd, baud));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
412
Living_SDK/platform/mcu/linux/csp/lwip/netif/tapif.c
Normal file
412
Living_SDK/platform/mcu/linux/csp/lwip/netif/tapif.c
Normal file
|
|
@ -0,0 +1,412 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
//#include <sys/uio.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/tcpip.h"
|
||||
|
||||
#include "lwip/debug.h"
|
||||
#include "lwip/def.h"
|
||||
#include "lwip/ip.h"
|
||||
#include "lwip/mem.h"
|
||||
#include "lwip/stats.h"
|
||||
#include "lwip/snmp.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/timeouts.h"
|
||||
#include "netif/etharp.h"
|
||||
#include "lwip/ethip6.h"
|
||||
|
||||
#if defined(LWIP_DEBUG) && defined(LWIP_TCPDUMP)
|
||||
#include "netif/tcpdump.h"
|
||||
#endif /* LWIP_DEBUG && LWIP_TCPDUMP */
|
||||
|
||||
#include "netif/tapif.h"
|
||||
|
||||
#define IFCONFIG_BIN "/sbin/ifconfig "
|
||||
|
||||
#if defined(LWIP_UNIX_LINUX)
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/if.h>
|
||||
#include <linux/if_tun.h>
|
||||
|
||||
#include <semaphore.h>
|
||||
#include <cpu_event.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
/*
|
||||
* Creating a tap interface requires special privileges. If the interfaces
|
||||
* is created in advance with `tunctl -u <user>` it can be opened as a regular
|
||||
* user. The network must already be configured. If DEVTAP_IF is defined it
|
||||
* will be opened instead of creating a new tap device.
|
||||
*
|
||||
* You can also use PRECONFIGURED_TAPIF environment variable to do so.
|
||||
*/
|
||||
#ifndef DEVTAP_DEFAULT_IF
|
||||
#define DEVTAP_DEFAULT_IF "tap0"
|
||||
#endif
|
||||
#ifndef DEVTAP
|
||||
#define DEVTAP "/dev/net/tun"
|
||||
#endif
|
||||
#define NETMASK_ARGS "netmask %d.%d.%d.%d"
|
||||
#define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d " NETMASK_ARGS
|
||||
#elif defined(LWIP_UNIX_OPENBSD)
|
||||
#define DEVTAP "/dev/tun0"
|
||||
#define NETMASK_ARGS "netmask %d.%d.%d.%d"
|
||||
#define IFCONFIG_ARGS "tun0 inet %d.%d.%d.%d " NETMASK_ARGS " link0"
|
||||
#else /* others */
|
||||
#define DEVTAP "/dev/tap0"
|
||||
#define NETMASK_ARGS "netmask %d.%d.%d.%d"
|
||||
#define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d " NETMASK_ARGS
|
||||
#endif
|
||||
|
||||
/* Define those to better describe your network interface. */
|
||||
#define IFNAME0 't'
|
||||
#define IFNAME1 'p'
|
||||
|
||||
#ifndef TAPIF_DEBUG
|
||||
#define TAPIF_DEBUG LWIP_DBG_OFF
|
||||
#endif
|
||||
|
||||
struct tapif
|
||||
{
|
||||
/* Add whatever per-interface state that is needed here. */
|
||||
int fd;
|
||||
};
|
||||
|
||||
/* Forward declarations. */
|
||||
static void *tapif_thread(void *arg);
|
||||
|
||||
/* For select thread, the driver layer of lwip */
|
||||
static pthread_t tid;
|
||||
|
||||
/* For pthread sem signal */
|
||||
static sem_t pthread_sem;
|
||||
|
||||
/* For sigmask, ignore SYSALARM signal */
|
||||
static sigset_t sigset;
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void low_level_init(struct netif *netif)
|
||||
{
|
||||
struct tapif *tapif;
|
||||
#if LWIP_IPV4
|
||||
int ret;
|
||||
char buf[1024];
|
||||
#endif /* LWIP_IPV4 */
|
||||
char *preconfigured_tapif = getenv("PRECONFIGURED_TAPIF");
|
||||
|
||||
tapif = (struct tapif *)netif->state;
|
||||
|
||||
/* Obtain MAC address from network interface. */
|
||||
|
||||
/* (We just fake an address...) */
|
||||
netif->hwaddr[0] = 0x02;
|
||||
netif->hwaddr[1] = 0x12;
|
||||
netif->hwaddr[2] = 0x34;
|
||||
netif->hwaddr[3] = 0x56;
|
||||
netif->hwaddr[4] = 0x78;
|
||||
netif->hwaddr[5] = 0xab;
|
||||
netif->hwaddr_len = 6;
|
||||
|
||||
/* device capabilities */
|
||||
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
|
||||
|
||||
tapif->fd = open(DEVTAP, O_RDWR);
|
||||
LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: fd %d\n", tapif->fd));
|
||||
if (tapif->fd == -1) {
|
||||
#ifdef LWIP_UNIX_LINUX
|
||||
perror("tapif_init: try running \"modprobe tun\" or rebuilding your kernel with CONFIG_TUN; cannot open "DEVTAP);
|
||||
#else /* LWIP_UNIX_LINUX */
|
||||
perror("tapif_init: cannot open "DEVTAP);
|
||||
#endif /* LWIP_UNIX_LINUX */
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#ifdef LWIP_UNIX_LINUX
|
||||
{
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
|
||||
if (preconfigured_tapif) {
|
||||
strncpy(ifr.ifr_name, preconfigured_tapif, sizeof(ifr.ifr_name));
|
||||
} else {
|
||||
strncpy(ifr.ifr_name, DEVTAP_DEFAULT_IF, sizeof(ifr.ifr_name));
|
||||
}
|
||||
ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0; /* ensure \0 termination */
|
||||
|
||||
ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
|
||||
if (ioctl(tapif->fd, TUNSETIFF, (void *) &ifr) < 0) {
|
||||
perror("tapif_init: "DEVTAP" ioctl TUNSETIFF");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
#endif /* LWIP_UNIX_LINUX */
|
||||
|
||||
netif_set_link_up(netif);
|
||||
|
||||
if (preconfigured_tapif == NULL) {
|
||||
#if LWIP_IPV4
|
||||
snprintf(buf, 1024, IFCONFIG_BIN IFCONFIG_ARGS,
|
||||
ip4_addr1(netif_ip4_gw(netif)),
|
||||
ip4_addr2(netif_ip4_gw(netif)),
|
||||
ip4_addr3(netif_ip4_gw(netif)),
|
||||
ip4_addr4(netif_ip4_gw(netif))
|
||||
#ifdef NETMASK_ARGS
|
||||
,
|
||||
ip4_addr1(netif_ip4_netmask(netif)),
|
||||
ip4_addr2(netif_ip4_netmask(netif)),
|
||||
ip4_addr3(netif_ip4_netmask(netif)),
|
||||
ip4_addr4(netif_ip4_netmask(netif))
|
||||
#endif /* NETMASK_ARGS */
|
||||
);
|
||||
|
||||
LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: system(\"%s\");\n", buf));
|
||||
ret = system(buf);
|
||||
if (ret < 0) {
|
||||
perror("ifconfig failed");
|
||||
exit(1);
|
||||
}
|
||||
if (ret != 0) {
|
||||
printf("ifconfig returned %d\n", ret);
|
||||
}
|
||||
#else /* LWIP_IPV4 */
|
||||
perror("todo: support IPv6 support for non-preconfigured tapif");
|
||||
exit(1);
|
||||
#endif /* LWIP_IPV4 */
|
||||
}
|
||||
|
||||
/* it has to be pthread, as it needs to access linux functions */
|
||||
pthread_create(&tid, NULL, tapif_thread, netif);
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* low_level_output():
|
||||
*
|
||||
* Should do the actual transmission of the packet. The packet is
|
||||
* contained in the pbuf that is passed to the function. This pbuf
|
||||
* might be chained.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
static err_t low_level_output(struct netif *netif, struct pbuf *p)
|
||||
{
|
||||
struct tapif *tapif = (struct tapif *)netif->state;
|
||||
char buf[1514];
|
||||
ssize_t written;
|
||||
|
||||
/* initiate transfer(); */
|
||||
pbuf_copy_partial(p, buf, p->tot_len, 0);
|
||||
|
||||
/* signal that packet should be sent(); */
|
||||
written = write(tapif->fd, buf, p->tot_len);
|
||||
if (written == -1) {
|
||||
MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
|
||||
perror("tapif: write");
|
||||
}
|
||||
else {
|
||||
MIB2_STATS_NETIF_ADD(netif, ifoutoctets, written);
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* low_level_input():
|
||||
*
|
||||
* Should allocate a pbuf and transfer the bytes of the incoming
|
||||
* packet from the interface into the pbuf.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static struct pbuf* low_level_input(struct netif *netif, void *buf, int len)
|
||||
{
|
||||
struct pbuf *p;
|
||||
|
||||
MIB2_STATS_NETIF_ADD(netif, ifinoctets, len);
|
||||
|
||||
/* We allocate a pbuf chain of pbufs from the pool. */
|
||||
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
|
||||
if (p != NULL) {
|
||||
pbuf_take(p, buf, len);
|
||||
/* acknowledge that packet has been read(); */
|
||||
} else {
|
||||
/* drop packet(); */
|
||||
MIB2_STATS_NETIF_INC(netif, ifindiscards);
|
||||
LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: could not allocate pbuf\n"));
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* tapif_input():
|
||||
*
|
||||
* This function should be called when a packet is ready to be read
|
||||
* from the interface. It uses the function low_level_input() that
|
||||
* should handle the actual reception of bytes from the network
|
||||
* interface.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
struct netif_buf {
|
||||
struct netif *netif;
|
||||
int len;
|
||||
char buf[0];
|
||||
};
|
||||
|
||||
static void tapif_input(struct netif_buf *nbuf)
|
||||
{
|
||||
struct netif *netif = nbuf->netif;
|
||||
void *buf = nbuf->buf;
|
||||
int len = nbuf->len;
|
||||
struct pbuf *p = low_level_input(netif, buf, len);
|
||||
|
||||
if (p == NULL) {
|
||||
#if LINK_STATS
|
||||
LINK_STATS_INC(link.recv);
|
||||
#endif /* LINK_STATS */
|
||||
LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_input: low_level_input returned NULL\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
LOCK_TCPIP_CORE();
|
||||
if (netif->input(p, netif) != ERR_OK) {
|
||||
LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: netif input error\n"));
|
||||
pbuf_free(p);
|
||||
}
|
||||
UNLOCK_TCPIP_CORE();
|
||||
|
||||
cpu_event_free(nbuf);
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* tapif_init():
|
||||
*
|
||||
* Should be called at the beginning of the program to set up the
|
||||
* network interface. It calls the function low_level_init() to do the
|
||||
* actual setup of the hardware.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
err_t tapif_init(struct netif *netif)
|
||||
{
|
||||
struct tapif *tapif = (struct tapif *)mem_malloc(sizeof(struct tapif));
|
||||
|
||||
if (tapif == NULL) {
|
||||
LWIP_DEBUGF(NETIF_DEBUG, ("tapif_init: out of memory for tapif\n"));
|
||||
return ERR_MEM;
|
||||
}
|
||||
netif->state = tapif;
|
||||
MIB2_INIT_NETIF(netif, snmp_ifType_other, 100000000);
|
||||
|
||||
netif->name[0] = IFNAME0;
|
||||
netif->name[1] = IFNAME1;
|
||||
#if LWIP_IPV4
|
||||
netif->output = etharp_output;
|
||||
#endif /* LWIP_IPV4 */
|
||||
#if LWIP_IPV6
|
||||
netif->output_ip6 = ethip6_output;
|
||||
#endif /* LWIP_IPV6 */
|
||||
netif->linkoutput = low_level_output;
|
||||
netif->mtu = 1500;
|
||||
|
||||
low_level_init(netif);
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void *tapif_thread(void *arg)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct tapif *tapif;
|
||||
fd_set fdset;
|
||||
int ret;
|
||||
|
||||
ret = sem_init(&pthread_sem, 0, 0);
|
||||
|
||||
netif = (struct netif *)arg;
|
||||
tapif = (struct tapif *)netif->state;
|
||||
|
||||
sigaddset(&sigset, SIGALRM);
|
||||
ret = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
|
||||
|
||||
if(ret != 0) {
|
||||
perror("sigmask failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(tapif->fd, &fdset);
|
||||
|
||||
/* Wait for a packet to arrive. */
|
||||
ret = select(tapif->fd + 1, &fdset, NULL, NULL, NULL);
|
||||
if(ret == 1) {
|
||||
struct netif_buf *nbuf = cpu_event_malloc(sizeof(*nbuf) + 2048);
|
||||
nbuf->len = 2048;
|
||||
nbuf->netif = netif;
|
||||
|
||||
/* Obtain the size of the packet and put it into the "len"
|
||||
variable. */
|
||||
nbuf->len = read(tapif->fd, nbuf->buf, nbuf->len);
|
||||
if (nbuf->len < 0) {
|
||||
perror("tapif read eror");
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Handle incoming packet. */
|
||||
cpu_call_handler((cpu_event_handler)tapif_input, nbuf);
|
||||
} else if(ret == -1) {
|
||||
perror("tapif_thread: select");
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
202
Living_SDK/platform/mcu/linux/csp/lwip/netif/tcpdump.c
Normal file
202
Living_SDK/platform/mcu/linux/csp/lwip/netif/tcpdump.c
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lwip/opt.h"
|
||||
|
||||
#if (LWIP_IPV4 || LWIP_IPV6) && LWIP_TCP /* @todo: fix IPv6 */
|
||||
|
||||
#include "netif/tcpdump.h"
|
||||
#include "lwip/ip.h"
|
||||
#include "lwip/ip4.h"
|
||||
#include "lwip/ip6.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/priv/tcp_priv.h"
|
||||
#include "lwip/udp.h"
|
||||
#include "lwip/inet.h"
|
||||
#include "lwip/inet_chksum.h"
|
||||
|
||||
#ifndef TCPDUMP_DEBUG
|
||||
#define TCPDUMP_DEBUG LWIP_DBG_OFF
|
||||
#endif
|
||||
|
||||
static FILE *file = NULL;
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void
|
||||
tcpdump_init(void)
|
||||
{
|
||||
#define TCPDUMP_FNAME "/tmp/tcpdump"
|
||||
file = fopen(TCPDUMP_FNAME, "w");
|
||||
if (file == NULL) {
|
||||
perror("tcpdump_init: cannot open \""TCPDUMP_FNAME"\" for writing");
|
||||
}
|
||||
LWIP_DEBUGF(TCPDUMP_DEBUG, ("tcpdump: file %s\n", TCPDUMP_FNAME));
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void
|
||||
tcpdump(struct pbuf *p)
|
||||
{
|
||||
#if LWIP_IPV4
|
||||
ip_addr_t src, dst;
|
||||
struct ip_hdr *iphdr;
|
||||
#if LWIP_UDP
|
||||
struct udp_hdr *udphdr;
|
||||
#endif
|
||||
#if LWIP_TCP
|
||||
struct tcp_hdr *tcphdr;
|
||||
char flags[5];
|
||||
int i;
|
||||
int len;
|
||||
int offset;
|
||||
#endif
|
||||
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
iphdr = (struct ip_hdr *)p->payload;
|
||||
|
||||
#if LWIP_IPV6
|
||||
if(IPH_V(iphdr) == 6) {
|
||||
struct ip6_hdr *ip6hdr = (struct ip6_hdr*)iphdr;
|
||||
ip_addr_copy_from_ip6(src, ip6hdr->src);
|
||||
ip_addr_copy_from_ip6(dst, ip6hdr->dest);
|
||||
fprintf(file, "%s > %s: (IPv6, unsupported) ",
|
||||
ip_ntoa(&src),
|
||||
ip_ntoa(&dst));
|
||||
return; /* not supported */
|
||||
}
|
||||
#endif
|
||||
|
||||
if(IPH_V(iphdr) == 4) {
|
||||
ip_addr_copy_from_ip4(src, iphdr->src);
|
||||
ip_addr_copy_from_ip4(dst, iphdr->dest);
|
||||
}
|
||||
|
||||
switch (IPH_PROTO(iphdr)) {
|
||||
#if LWIP_TCP
|
||||
case IP_PROTO_TCP:
|
||||
tcphdr = (struct tcp_hdr *)((char *)iphdr + IPH_HL(iphdr));
|
||||
|
||||
pbuf_header(p, -IP_HLEN);
|
||||
if (ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len, &src, &dst) != 0) {
|
||||
LWIP_DEBUGF(TCPDUMP_DEBUG, ("tcpdump: IP checksum failed!\n"));
|
||||
/*
|
||||
fprintf(file, "chksum 0x%lx ", tcphdr->chksum);
|
||||
tcphdr->chksum = 0;
|
||||
fprintf(file, "should be 0x%lx ", inet_chksum_pseudo(p, (ip_addr_t *)&(iphdr->src),
|
||||
(ip_addr_t *)&(iphdr->dest), IP_PROTO_TCP, p->tot_len));*/
|
||||
fprintf(file, "!chksum ");
|
||||
}
|
||||
|
||||
i = 0;
|
||||
if (TCPH_FLAGS(tcphdr) & TCP_SYN) {
|
||||
flags[i++] = 'S';
|
||||
}
|
||||
if (TCPH_FLAGS(tcphdr) & TCP_PSH) {
|
||||
flags[i++] = 'P';
|
||||
}
|
||||
if (TCPH_FLAGS(tcphdr) & TCP_FIN) {
|
||||
flags[i++] = 'F';
|
||||
}
|
||||
if (TCPH_FLAGS(tcphdr) & TCP_RST) {
|
||||
flags[i++] = 'R';
|
||||
}
|
||||
if (i == 0) {
|
||||
flags[i++] = '.';
|
||||
}
|
||||
flags[i++] = 0;
|
||||
|
||||
fprintf(file, "%s.%u > %s.%u: ",
|
||||
ip_ntoa(&src),
|
||||
lwip_ntohs(tcphdr->src),
|
||||
ip_ntoa(&dst),
|
||||
lwip_ntohs(tcphdr->dest));
|
||||
offset = TCPH_HDRLEN(tcphdr);
|
||||
|
||||
len = lwip_ntohs(IPH_LEN(iphdr)) - offset * 4 - IP_HLEN;
|
||||
if (len != 0 || flags[0] != '.') {
|
||||
fprintf(file, "%s %u:%u(%u) ", flags, lwip_ntohl(tcphdr->seqno),
|
||||
lwip_ntohl(tcphdr->seqno) + len, len);
|
||||
}
|
||||
if (TCPH_FLAGS(tcphdr) & TCP_ACK) {
|
||||
fprintf(file, "ack %u ", lwip_ntohl(tcphdr->ackno));
|
||||
}
|
||||
fprintf(file, "wnd %u\n", lwip_ntohs(tcphdr->wnd));
|
||||
|
||||
fflush(file);
|
||||
|
||||
pbuf_header(p, IP_HLEN);
|
||||
break;
|
||||
#endif /* LWIP_TCP */
|
||||
|
||||
#if LWIP_UDP
|
||||
case IP_PROTO_UDP:
|
||||
udphdr = (struct udp_hdr *)((char *)iphdr + IPH_HL(iphdr));
|
||||
|
||||
pbuf_header(p, -IP_HLEN);
|
||||
if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len, &src, &dst) != 0) {
|
||||
LWIP_DEBUGF(TCPDUMP_DEBUG, ("tcpdump: IP checksum failed!\n"));
|
||||
/*
|
||||
fprintf(file, "chksum 0x%lx ", tcphdr->chksum);
|
||||
tcphdr->chksum = 0;
|
||||
fprintf(file, "should be 0x%lx ", ip_chksum_pseudo(p, &src, &dst, IP_PROTO_TCP, p->tot_len));*/
|
||||
fprintf(file, "!chksum ");
|
||||
}
|
||||
|
||||
fprintf(file, "%s.%u > %s.%u: ",
|
||||
ip_ntoa(&src),
|
||||
lwip_ntohs(udphdr->src),
|
||||
ip_ntoa(&dst),
|
||||
lwip_ntohs(udphdr->dest));
|
||||
fprintf(file, "U ");
|
||||
len = lwip_ntohs(IPH_LEN(iphdr)) - sizeof(struct udp_hdr) - IP_HLEN;
|
||||
fprintf(file, " %d\n", len);
|
||||
|
||||
fflush(file);
|
||||
|
||||
pbuf_header(p, IP_HLEN);
|
||||
break;
|
||||
#endif /* LWIP_UDP */
|
||||
default:
|
||||
LWIP_DEBUGF(TCPDUMP_DEBUG, ("unhandled IP protocol: %d\n", (int)IPH_PROTO(iphdr)));
|
||||
break;
|
||||
}
|
||||
#else
|
||||
LWIP_UNUSED_ARG(p);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* LWIP_IPV4 && LWIP_TCP */
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
303
Living_SDK/platform/mcu/linux/csp/lwip/netif/tunif.c
Normal file
303
Living_SDK/platform/mcu/linux/csp/lwip/netif/tunif.c
Normal file
|
|
@ -0,0 +1,303 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "netif/tunif.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
//#include <sys/uio.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
|
||||
#include "lwip/debug.h"
|
||||
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/def.h"
|
||||
#include "lwip/ip.h"
|
||||
#include "lwip/mem.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/sys.h"
|
||||
|
||||
#if LWIP_IPV4 /* @todo: IPv6 */
|
||||
#if !NO_SYS
|
||||
|
||||
#define IFNAME0 't'
|
||||
#define IFNAME1 'n'
|
||||
|
||||
#ifndef TUNIF_DEBUG
|
||||
#define TUNIF_DEBUG LWIP_DBG_OFF
|
||||
#endif
|
||||
|
||||
#define IFCONFIG_CALL "/sbin/ifconfig tun inet %d.%d.%d.%d %d.%d.%d.%d"
|
||||
|
||||
struct tunif {
|
||||
/* Add whatever per-interface state that is needed here. */
|
||||
int fd;
|
||||
};
|
||||
|
||||
/* Forward declarations. */
|
||||
static void tunif_input(struct netif *netif);
|
||||
static err_t tunif_output(struct netif *netif, struct pbuf *p,
|
||||
const ip4_addr_t *ipaddr);
|
||||
|
||||
static void tunif_thread(void *data);
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
low_level_init(struct netif *netif)
|
||||
{
|
||||
struct tunif *tunif;
|
||||
char buf[sizeof(IFCONFIG_CALL) + 50];
|
||||
|
||||
tunif = (struct tunif *)netif->state;
|
||||
|
||||
/* Obtain MAC address from network interface. */
|
||||
|
||||
/* Do whatever else is needed to initialize interface. */
|
||||
|
||||
tunif->fd = open("/dev/net/tun", O_RDWR);
|
||||
LWIP_DEBUGF(TUNIF_DEBUG, ("tunif_init: fd %d\n", tunif->fd));
|
||||
if (tunif->fd == -1) {
|
||||
perror("tunif_init");
|
||||
exit(1);
|
||||
}
|
||||
sprintf(buf, IFCONFIG_CALL,
|
||||
ip4_addr1(netif_ip4_gw(netif)),
|
||||
ip4_addr2(netif_ip4_gw(netif)),
|
||||
ip4_addr3(netif_ip4_gw(netif)),
|
||||
ip4_addr4(netif_ip4_gw(netif)),
|
||||
ip4_addr1(netif_ip4_addr(netif)),
|
||||
ip4_addr2(netif_ip4_addr(netif)),
|
||||
ip4_addr3(netif_ip4_addr(netif)),
|
||||
ip4_addr4(netif_ip4_addr(netif)));
|
||||
|
||||
LWIP_DEBUGF(TUNIF_DEBUG, ("tunif_init: system(\"%s\");\n", buf));
|
||||
system(buf);
|
||||
sys_thread_new("tunif_thread", tunif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
||||
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* low_level_output():
|
||||
*
|
||||
* Should do the actual transmission of the packet. The packet is
|
||||
* contained in the pbuf that is passed to the function. This pbuf
|
||||
* might be chained.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
static err_t
|
||||
low_level_output(struct tunif *tunif, struct pbuf *p)
|
||||
{
|
||||
char buf[1500];
|
||||
int rnd_val;
|
||||
|
||||
/* initiate transfer(); */
|
||||
|
||||
rnd_val = rand();
|
||||
if (((double)rnd_val/(double)RAND_MAX) < 0.4) {
|
||||
printf("drop\n");
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
pbuf_copy_partial(p, buf, p->tot_len, 0);
|
||||
|
||||
/* signal that packet should be sent(); */
|
||||
if (write(tunif->fd, buf, p->tot_len) == -1) {
|
||||
perror("tunif: write");
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* low_level_input():
|
||||
*
|
||||
* Should allocate a pbuf and transfer the bytes of the incoming
|
||||
* packet from the interface into the pbuf.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static struct pbuf *
|
||||
low_level_input(struct tunif *tunif)
|
||||
{
|
||||
struct pbuf *p;
|
||||
ssize_t len;
|
||||
char buf[1500];
|
||||
|
||||
/* Obtain the size of the packet and put it into the "len"
|
||||
variable. */
|
||||
len = read(tunif->fd, buf, sizeof(buf));
|
||||
if((len <= 0) || (len > 0xffff)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* if (((double)rand()/(double)RAND_MAX) < 0.1) {
|
||||
printf("drop\n");
|
||||
return NULL;
|
||||
}*/
|
||||
|
||||
/* We allocate a pbuf chain of pbufs from the pool. */
|
||||
p = pbuf_alloc(PBUF_LINK, (u16_t)len, PBUF_POOL);
|
||||
|
||||
if (p != NULL) {
|
||||
pbuf_take(p, buf, (u16_t)len);
|
||||
/* acknowledge that packet has been read(); */
|
||||
} else {
|
||||
/* drop packet(); */
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
tunif_thread(void *arg)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct tunif *tunif;
|
||||
fd_set fdset;
|
||||
int ret;
|
||||
|
||||
netif = (struct netif *)arg;
|
||||
tunif = (struct tunif *)netif->state;
|
||||
|
||||
while (1) {
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(tunif->fd, &fdset);
|
||||
|
||||
/* Wait for a packet to arrive. */
|
||||
ret = select(tunif->fd + 1, &fdset, NULL, NULL, NULL);
|
||||
|
||||
printf("-------select--------%d\n",ret);
|
||||
if (ret == 1) {
|
||||
/* Handle incoming packet. */
|
||||
tunif_input(netif);
|
||||
} else if (ret == -1) {
|
||||
perror("tunif_thread: select");
|
||||
}
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* tunif_output():
|
||||
*
|
||||
* This function is called by the TCP/IP stack when an IP packet
|
||||
* should be sent. It calls the function called low_level_output() to
|
||||
* do the actuall transmission of the packet.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static err_t
|
||||
tunif_output(struct netif *netif, struct pbuf *p,
|
||||
const ip4_addr_t *ipaddr)
|
||||
{
|
||||
struct tunif *tunif;
|
||||
LWIP_UNUSED_ARG(ipaddr);
|
||||
|
||||
tunif = (struct tunif *)netif->state;
|
||||
|
||||
return low_level_output(tunif, p);
|
||||
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* tunif_input():
|
||||
*
|
||||
* This function should be called when a packet is ready to be read
|
||||
* from the interface. It uses the function low_level_input() that
|
||||
* should handle the actual reception of bytes from the network
|
||||
* interface.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
tunif_input(struct netif *netif)
|
||||
{
|
||||
struct tunif *tunif;
|
||||
struct pbuf *p;
|
||||
|
||||
|
||||
tunif = (struct tunif *)netif->state;
|
||||
|
||||
p = low_level_input(tunif);
|
||||
|
||||
if (p == NULL) {
|
||||
LWIP_DEBUGF(TUNIF_DEBUG, ("tunif_input: low_level_input returned NULL\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* CS: ip_lookup() was removed */
|
||||
if (ip_lookup(p->payload, netif)) {
|
||||
#endif
|
||||
netif->input(p, netif);
|
||||
#if 0
|
||||
}
|
||||
#endif
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/*
|
||||
* tunif_init():
|
||||
*
|
||||
* Should be called at the beginning of the program to set up the
|
||||
* network interface. It calls the function low_level_init() to do the
|
||||
* actual setup of the hardware.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
err_t
|
||||
tunif_init(struct netif *netif)
|
||||
{
|
||||
struct tunif *tunif;
|
||||
|
||||
tunif = (struct tunif *)mem_malloc(sizeof(struct tunif));
|
||||
if (!tunif) {
|
||||
return ERR_MEM;
|
||||
}
|
||||
netif->state = tunif;
|
||||
netif->name[0] = IFNAME0;
|
||||
netif->name[1] = IFNAME1;
|
||||
netif->output = tunif_output;
|
||||
|
||||
|
||||
low_level_init(netif);
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
#endif /* !NO_SYS */
|
||||
#endif /* LWIP_IPV4 */
|
||||
494
Living_SDK/platform/mcu/linux/csp/lwip/netif/unixif.c
Normal file
494
Living_SDK/platform/mcu/linux/csp/lwip/netif/unixif.c
Normal file
|
|
@ -0,0 +1,494 @@
|
|||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "lwip/debug.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
/*#include <sys/uio.h>*/
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/stat.h>
|
||||
/*#include <netinet/in.h> */
|
||||
/*#include <arpa/inet.h> */
|
||||
|
||||
|
||||
#include "lwip/stats.h"
|
||||
|
||||
#include "lwip/def.h"
|
||||
#include "lwip/mem.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "netif/list.h"
|
||||
#include "netif/unixif.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/timeouts.h"
|
||||
|
||||
#if LWIP_IPV4 /* @todo: IPv6 */
|
||||
#if !NO_SYS
|
||||
|
||||
#include "netif/tcpdump.h"
|
||||
|
||||
#define UNIXIF_BPS 512000
|
||||
#define UNIXIF_QUEUELEN 6
|
||||
/*#define UNIXIF_DROP_FIRST */
|
||||
|
||||
#ifndef UNIXIF_DEBUG
|
||||
#define UNIXIF_DEBUG LWIP_DBG_OFF
|
||||
#endif
|
||||
|
||||
struct unixif_buf {
|
||||
struct pbuf *p;
|
||||
unsigned short len, tot_len;
|
||||
void *payload;
|
||||
};
|
||||
|
||||
struct unixif {
|
||||
int fd;
|
||||
sys_sem_t sem;
|
||||
struct list *q;
|
||||
};
|
||||
|
||||
static void unixif_thread(void *arg);
|
||||
static void unixif_thread2(void *arg);
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static int
|
||||
unix_socket_client(const char *name)
|
||||
{
|
||||
int fd;
|
||||
#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
|
||||
int len;
|
||||
#endif
|
||||
struct sockaddr_un unix_addr;
|
||||
|
||||
/* create a Unix domain stream socket */
|
||||
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
|
||||
perror("unixif: unix_socket_client: socket");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
/* fill socket address structure w/our address */
|
||||
memset(&unix_addr, 0, sizeof(unix_addr));
|
||||
unix_addr.sun_family = AF_UNIX;
|
||||
snprintf(unix_addr.sun_path, sizeof(unix_addr.sun_path), "%s%05d", "/var/tmp/", getpid());
|
||||
#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
|
||||
len = sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family) +
|
||||
strlen(unix_addr.sun_path) + 1;
|
||||
unix_addr.sun_len = len;
|
||||
#endif /* LWIP_UNIX_LINUX */
|
||||
|
||||
unlink(unix_addr.sun_path); /* in case it already exists */
|
||||
if (bind(fd, (struct sockaddr *) &unix_addr,
|
||||
sizeof(struct sockaddr_un)) < 0) {
|
||||
perror("unixif: unix_socket_client: socket");
|
||||
return(-1);
|
||||
}
|
||||
if (chmod(unix_addr.sun_path, S_IRWXU | S_IRWXO) < 0) {
|
||||
perror("unixif: unix_socket_client: socket");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
/* fill socket address structure w/server's addr */
|
||||
memset(&unix_addr, 0, sizeof(unix_addr));
|
||||
unix_addr.sun_family = AF_UNIX;
|
||||
strncpy(unix_addr.sun_path, name, sizeof(unix_addr.sun_path));
|
||||
unix_addr.sun_path[sizeof(unix_addr.sun_path)-1] = 0; /* ensure \0 termination */
|
||||
#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
|
||||
len = sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family) +
|
||||
strlen(unix_addr.sun_path) + 1;
|
||||
unix_addr.sun_len = len;
|
||||
#endif /* LWIP_UNIX_LINUX */
|
||||
if (connect(fd, (struct sockaddr *) &unix_addr,
|
||||
sizeof(struct sockaddr_un)) < 0) {
|
||||
perror("unixif: unix_socket_client: socket");
|
||||
return(-1);
|
||||
}
|
||||
return(fd);
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static int
|
||||
unix_socket_server(const char *name)
|
||||
{
|
||||
int fd;
|
||||
#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
|
||||
int len;
|
||||
#endif
|
||||
struct sockaddr_un unix_addr;
|
||||
|
||||
/* create a Unix domain stream socket */
|
||||
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
|
||||
perror("unixif: unix_socket_server: socket");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
unlink(name); /* in case it already exists */
|
||||
|
||||
/* fill in socket address structure */
|
||||
memset(&unix_addr, 0, sizeof(unix_addr));
|
||||
unix_addr.sun_family = AF_UNIX;
|
||||
strncpy(unix_addr.sun_path, name, sizeof(unix_addr.sun_path));
|
||||
unix_addr.sun_path[sizeof(unix_addr.sun_path)-1] = 0; /* ensure \0 termination */
|
||||
#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
|
||||
len = sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family) +
|
||||
strlen(unix_addr.sun_path) + 1;
|
||||
unix_addr.sun_len = len;
|
||||
#endif /* LWIP_UNIX_LINUX */
|
||||
|
||||
/* bind the name to the descriptor */
|
||||
if (bind(fd, (struct sockaddr *) &unix_addr,
|
||||
sizeof(struct sockaddr_un)) < 0) {
|
||||
perror("unixif: unix_socket_server: bind");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
if (chmod(unix_addr.sun_path, S_IRWXU | S_IRWXO) < 0) {
|
||||
perror("unixif: unix_socket_server: chmod");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
|
||||
if (listen(fd, 5) < 0) { /* tell kernel we're a server */
|
||||
perror("unixif: unix_socket_server: listen");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
return(fd);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
unixif_input_handler(void *data)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct unixif *unixif;
|
||||
char buf[1532];
|
||||
int len, plen;
|
||||
struct pbuf *p;
|
||||
|
||||
netif = (struct netif *)data;
|
||||
unixif = (struct unixif *)netif->state;
|
||||
|
||||
len = read(unixif->fd, &plen, sizeof(int));
|
||||
if (len == -1) {
|
||||
perror("unixif_irq_handler: read");
|
||||
abort();
|
||||
}
|
||||
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_irq_handler: len == %d plen == %d bytes\n", len, plen));
|
||||
if (len == sizeof(int)) {
|
||||
|
||||
if (plen < 20 || plen > 1500) {
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("plen %d!\n", plen));
|
||||
return;
|
||||
}
|
||||
|
||||
len = read(unixif->fd, buf, plen);
|
||||
if (len == -1) {
|
||||
perror("unixif_irq_handler: read");
|
||||
abort();
|
||||
}
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_irq_handler: read %d bytes\n", len));
|
||||
p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL);
|
||||
|
||||
if (p != NULL) {
|
||||
pbuf_take(p, buf, len);
|
||||
pbuf_realloc(p, len);
|
||||
LINK_STATS_INC(link.recv);
|
||||
#if LWIP_IPV4 && LWIP_TCP
|
||||
tcpdump(p);
|
||||
#endif
|
||||
netif->input(p, netif);
|
||||
} else {
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_irq_handler: could not allocate pbuf\n"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
unixif_thread(void *arg)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct unixif *unixif;
|
||||
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_thread: started.\n"));
|
||||
|
||||
netif = (struct netif *)arg;
|
||||
unixif = (struct unixif *)netif->state;
|
||||
|
||||
|
||||
while (1) {
|
||||
sys_sem_wait(&unixif->sem);
|
||||
unixif_input_handler(netif);
|
||||
}
|
||||
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
unixif_thread2(void *arg)
|
||||
{
|
||||
struct netif *netif;
|
||||
struct unixif *unixif;
|
||||
fd_set fdset;
|
||||
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_thread2: started.\n"));
|
||||
|
||||
netif = (struct netif *)arg;
|
||||
unixif = (struct unixif *)netif->state;
|
||||
|
||||
while (1) {
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(unixif->fd, &fdset);
|
||||
|
||||
if (select(unixif->fd + 1, &fdset, NULL, NULL, NULL) > 0) {
|
||||
sys_sem_signal(&unixif->sem);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void unixif_output_timeout(void *arg);
|
||||
|
||||
static err_t
|
||||
unixif_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
|
||||
{
|
||||
struct unixif *unixif;
|
||||
struct unixif_buf *buf;
|
||||
LWIP_UNUSED_ARG(ipaddr);
|
||||
|
||||
unixif = (struct unixif *)netif->state;
|
||||
|
||||
buf = (struct unixif_buf *)malloc(sizeof(struct unixif_buf));
|
||||
buf->p = p;
|
||||
buf->len = p->len;
|
||||
buf->tot_len = p->tot_len;
|
||||
buf->payload = p->payload;
|
||||
|
||||
if (list_elems(unixif->q) == 0) {
|
||||
pbuf_ref(p);
|
||||
list_push(unixif->q, buf);
|
||||
sys_timeout((double)p->tot_len * 8000.0 / UNIXIF_BPS, unixif_output_timeout,
|
||||
netif);
|
||||
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: first on list\n"));
|
||||
|
||||
} else {
|
||||
pbuf_ref(p);
|
||||
if (list_push(unixif->q, buf) == 0) {
|
||||
#ifdef UNIXIF_DROP_FIRST
|
||||
struct unixif_buf *buf2;
|
||||
|
||||
buf2 = list_pop(unixif->q);
|
||||
pbuf_free(buf2->p);
|
||||
free(buf2);
|
||||
list_push(unixif->q, buf);
|
||||
#else
|
||||
free(buf);
|
||||
pbuf_free(p);
|
||||
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: drop\n"));
|
||||
|
||||
#endif /* UNIXIF_DROP_FIRST */
|
||||
LINK_STATS_INC(link.drop);
|
||||
|
||||
} else {
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: on list\n"));
|
||||
}
|
||||
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
unixif_output_timeout(void *arg)
|
||||
{
|
||||
struct pbuf *p, *q;
|
||||
int i, j, len;
|
||||
unsigned short plen, ptot_len;
|
||||
struct unixif_buf *buf;
|
||||
void *payload;
|
||||
struct netif *netif;
|
||||
struct unixif *unixif;
|
||||
char *data;
|
||||
|
||||
netif = (struct netif *)arg;
|
||||
unixif = (struct unixif *)netif->state;
|
||||
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output_timeout\n"));
|
||||
|
||||
/* buf = unixif->q[0];
|
||||
unixif->q[0] = unixif->q[1];
|
||||
unixif->q[1] = NULL;*/
|
||||
buf = (struct unixif_buf *)list_pop(unixif->q);
|
||||
|
||||
p = buf->p;
|
||||
|
||||
plen = p->len;
|
||||
ptot_len = p->tot_len;
|
||||
payload = p->payload;
|
||||
|
||||
p->len = buf->len;
|
||||
p->tot_len = buf->tot_len;
|
||||
p->payload = buf->payload;
|
||||
|
||||
|
||||
if (p->tot_len == 0) {
|
||||
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("p->len!\n"));
|
||||
abort();
|
||||
}
|
||||
data = (char *)malloc(p->tot_len);
|
||||
|
||||
i = 0;
|
||||
for(q = p; q != NULL; q = q->next) {
|
||||
for(j = 0; j < q->len; j++) {
|
||||
data[i] = ((char *)q->payload)[j];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: sending %d (%d) bytes\n",
|
||||
p->len, p->tot_len));
|
||||
|
||||
len = p->tot_len;
|
||||
if (write(unixif->fd, &len, sizeof(int)) == -1) {
|
||||
perror("unixif_output: write");
|
||||
abort();
|
||||
}
|
||||
|
||||
if (write(unixif->fd, data, p->tot_len) == -1) {
|
||||
perror("unixif_output: write");
|
||||
abort();
|
||||
}
|
||||
#if LWIP_IPV4 && LWIP_TCP
|
||||
tcpdump(p);
|
||||
#endif
|
||||
LINK_STATS_INC(link.xmit);
|
||||
|
||||
free(data);
|
||||
free(buf);
|
||||
p->len = plen;
|
||||
p->tot_len = ptot_len;
|
||||
p->payload = payload;
|
||||
|
||||
pbuf_free(p);
|
||||
|
||||
/* if (unixif->q[0] != NULL) {
|
||||
sys_timeout(unixif->q[0]->tot_len * 8000 / UNIXIF_BPS,
|
||||
unixif_output_timeout, netif);
|
||||
}*/
|
||||
if (list_elems(unixif->q) > 0) {
|
||||
sys_timeout(((struct unixif_buf *)list_first(unixif->q))->tot_len *
|
||||
8000.0 / UNIXIF_BPS,
|
||||
unixif_output_timeout, netif);
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
err_t
|
||||
unixif_init_server(struct netif *netif)
|
||||
{
|
||||
int fd, fd2;
|
||||
struct sockaddr_un addr;
|
||||
socklen_t len;
|
||||
struct unixif *unixif;
|
||||
|
||||
fd = unix_socket_server("/tmp/unixif");
|
||||
|
||||
if (fd == -1) {
|
||||
perror("unixif_server");
|
||||
abort();
|
||||
}
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_server: fd %d\n", fd));
|
||||
|
||||
unixif = (struct unixif *)malloc(sizeof(struct unixif));
|
||||
if (!unixif) {
|
||||
return ERR_MEM;
|
||||
}
|
||||
netif->state = unixif;
|
||||
netif->name[0] = 'u';
|
||||
netif->name[1] = 'n';
|
||||
netif->output = unixif_output;
|
||||
unixif->q = list_new(UNIXIF_QUEUELEN);
|
||||
|
||||
printf("Now run ./simnode.\n");
|
||||
len = sizeof(addr);
|
||||
fd2 = accept(fd, (struct sockaddr *)&addr, &len);
|
||||
|
||||
if (fd2 == -1) {
|
||||
perror("unixif_accept");
|
||||
abort();
|
||||
}
|
||||
|
||||
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_accept: %d\n", fd2));
|
||||
|
||||
unixif->fd = fd2;
|
||||
if(sys_sem_new(&unixif->sem, 0) != ERR_OK) {
|
||||
LWIP_ASSERT("Failed to create semaphore", 0);
|
||||
}
|
||||
sys_thread_new("unixif_thread", unixif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
||||
sys_thread_new("unixif_thread2", unixif_thread2, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
err_t
|
||||
unixif_init_client(struct netif *netif)
|
||||
{
|
||||
struct unixif *unixif;
|
||||
unixif = (struct unixif *)malloc(sizeof(struct unixif));
|
||||
if (!unixif) {
|
||||
return ERR_MEM;
|
||||
}
|
||||
netif->state = unixif;
|
||||
netif->name[0] = 'u';
|
||||
netif->name[1] = 'n';
|
||||
netif->output = unixif_output;
|
||||
|
||||
unixif->fd = unix_socket_client("/tmp/unixif");
|
||||
if (unixif->fd == -1) {
|
||||
perror("unixif_init");
|
||||
abort();
|
||||
}
|
||||
unixif->q = list_new(UNIXIF_QUEUELEN);
|
||||
if(sys_sem_new(&unixif->sem, 0) != ERR_OK) {
|
||||
LWIP_ASSERT("Failed to create semaphore", 0);
|
||||
}
|
||||
sys_thread_new("unixif_thread", unixif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
||||
sys_thread_new("unixif_thread2", unixif_thread2, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
||||
return ERR_OK;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
#endif /* !NO_SYS */
|
||||
#endif /* LWIP_IPV4 */
|
||||
130
Living_SDK/platform/mcu/linux/csp/lwip/udp_server.c
Normal file
130
Living_SDK/platform/mcu/linux/csp/lwip/udp_server.c
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
|
||||
|
||||
#define BUFSZ 1024
|
||||
|
||||
|
||||
/*
|
||||
void UDPDataRecv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
|
||||
struct ip_addr *addr, u16_t port)
|
||||
{
|
||||
// char *a;
|
||||
// uint8_t i=0;
|
||||
struct pbuf *q;
|
||||
struct ip_addr destAddr = *addr;
|
||||
if(p != NULL)
|
||||
{
|
||||
for(q=p; q != NULL; q = p->next)
|
||||
{
|
||||
// c = q->payload;
|
||||
// a=q->payload;
|
||||
//pbuf_copy_partial(p, UDPData, p->len, 0);
|
||||
udp_sendto(upcb,q->payload,&destAddr,5000);
|
||||
}
|
||||
}
|
||||
//pbuf_free(p);
|
||||
}
|
||||
*/
|
||||
/*
|
||||
void UDPNetInit(void)
|
||||
{
|
||||
struct udp_pcb * UDPpcb;
|
||||
struct ip_addr ipaddr1;
|
||||
struct ip_addr netmask;
|
||||
struct ip_addr gw;
|
||||
IP4_ADDR(&ipaddr1,192,168,1,125);
|
||||
IP4_ADDR(&netmask, 255, 255, 255, 0); //子网掩码
|
||||
IP4_ADDR(&gw, 192, 168, 1, 1); //网关
|
||||
//
|
||||
// Start listening for incoming TFTP requests.
|
||||
//
|
||||
UDPpcb = udp_new();
|
||||
// UDPpcb->so_options |= SOF_BROADCAST;
|
||||
udp_bind(UDPpcb,IP_ADDR_ANY,5000);
|
||||
udp_connect(UDPpcb,IP_ADDR_BROADCAST,4000);
|
||||
udp_recv(UDPpcb, UDPDataRecv, NULL);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
void udpserv(void* paramemter)
|
||||
{
|
||||
int sock;
|
||||
int bytes_read;
|
||||
char *recv_data;
|
||||
uint32_t addr_len;
|
||||
struct sockaddr_in server_addr, client_addr;
|
||||
|
||||
/* 分配接收用的数据缓冲 */
|
||||
recv_data = malloc(BUFSZ);
|
||||
if (recv_data == NULL)
|
||||
{
|
||||
/* 分配内存失败,返回 */
|
||||
printf("No memory\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
|
||||
if ((sock = lwip_socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
||||
{
|
||||
printf("Socket error\n");
|
||||
|
||||
/* 释放接收用的数据缓冲 */
|
||||
free(recv_data);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 初始化服务端地址 */
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = lwip_htons(5000);
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero));
|
||||
|
||||
|
||||
/* 绑定socket到服务端地址 */
|
||||
if (lwip_bind(sock,(struct sockaddr *)&server_addr,
|
||||
sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
/* 绑定地址失败 */
|
||||
printf("Bind error\n");
|
||||
|
||||
/* 释放接收用的数据缓冲 */
|
||||
free(recv_data);
|
||||
return;
|
||||
}
|
||||
|
||||
addr_len = sizeof(struct sockaddr);
|
||||
printf("UDPServer Waiting for client on port 5000...\n");
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 从sock中收取最大BUFSZ - 1字节数据 */
|
||||
bytes_read = lwip_recvfrom(sock, recv_data, BUFSZ - 1, 0,
|
||||
(struct sockaddr *)&client_addr, &addr_len);
|
||||
/* UDP不同于TCP,它基本不会出现收取的数据失败的情况,除非设置了超时等待 */
|
||||
|
||||
recv_data[bytes_read] = '\0'; /* 把末端清零 */
|
||||
|
||||
/* 输出接收的数据 */
|
||||
printf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr),
|
||||
ntohs(client_addr.sin_port));
|
||||
printf("%s", recv_data);
|
||||
|
||||
/* 如果接收数据是exit,退出 */
|
||||
if (strcmp(recv_data, "exit") == 0)
|
||||
{
|
||||
lwip_close(sock);
|
||||
|
||||
/* 释放接收用的数据缓冲 */
|
||||
free(recv_data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
51
Living_SDK/platform/mcu/linux/include/arg_options.h
Normal file
51
Living_SDK/platform/mcu/linux/include/arg_options.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef ARG_OPTIONS_H
|
||||
#define ARG_OPTIONS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char **argv;
|
||||
#ifdef TFS_EMULATE
|
||||
int id2_index;
|
||||
#endif
|
||||
int log_level;
|
||||
|
||||
struct {
|
||||
bool enable;
|
||||
bool tapif;
|
||||
} lwip;
|
||||
|
||||
struct {
|
||||
bool enable;
|
||||
} mesh;
|
||||
|
||||
struct {
|
||||
bool enable;
|
||||
} dda;
|
||||
|
||||
struct {
|
||||
bool enable;
|
||||
} ddm;
|
||||
|
||||
struct {
|
||||
bool enable;
|
||||
} alink;
|
||||
|
||||
struct {
|
||||
bool per_pid;
|
||||
} flash;
|
||||
|
||||
struct {
|
||||
bool enable;
|
||||
} cli;
|
||||
} options_t;
|
||||
|
||||
void parse_options(options_t *config);
|
||||
|
||||
#endif
|
||||
|
||||
15
Living_SDK/platform/mcu/linux/include/vfs_trap.h
Executable file
15
Living_SDK/platform/mcu/linux/include/vfs_trap.h
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef AOS_VFS_TRAP_H
|
||||
#define AOS_VFS_TRAP_H
|
||||
|
||||
int trap_open(const char *path, int flags);
|
||||
int trap_read(int fd, void *buf, int len);
|
||||
int trap_write(int fd, const void *buf, int len);
|
||||
int trap_close(int fd);
|
||||
int trap_fcntl(int fd, int cmd, int val);
|
||||
|
||||
#endif
|
||||
|
||||
99
Living_SDK/platform/mcu/linux/linux.mk
Normal file
99
Living_SDK/platform/mcu/linux/linux.mk
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
HOST_OPENOCD := linux
|
||||
|
||||
NAME := linuximpl
|
||||
|
||||
no_with_lwip ?= 1
|
||||
|
||||
|
||||
$(NAME)_COMPONENTS := log arch.linux
|
||||
|
||||
ifeq ($(openssl),1)
|
||||
GLOBAL_LDFLAGS += -lssl -lcrypto
|
||||
endif
|
||||
|
||||
ifeq ($(gcov),1)
|
||||
GLOBAL_DEFINES += GCOV_ENABLE
|
||||
GLOBAL_CFLAGS += -fprofile-arcs -ftest-coverage
|
||||
GLOBAL_LDFLAGS += --coverage
|
||||
endif
|
||||
|
||||
ifeq ($(gprof),1)
|
||||
GLOBAL_CFLAGS += -pg
|
||||
GLOBAL_LDFLAGS += -pg
|
||||
endif
|
||||
|
||||
ifeq ($(valgrind), 1)
|
||||
GLOBAL_CFLAGS += $(shell [ -f /usr/include/valgrind/valgrind.h ] && echo -DHAVE_VALGRIND_VALGRIND_H)
|
||||
GLOBAL_CFLAGS += $(shell [ -f /usr/include/valgrind.h ] && echo -DHAVE_VALGRIND_H)
|
||||
endif
|
||||
|
||||
$(NAME)_CFLAGS += -Wall -Werror -Wno-unused-variable -Wno-unused-parameter -Wno-implicit-function-declaration
|
||||
$(NAME)_CFLAGS += -Wno-type-limits -Wno-sign-compare -Wno-pointer-sign -Wno-uninitialized
|
||||
$(NAME)_CFLAGS += -Wno-return-type -Wno-unused-function -Wno-unused-but-set-variable
|
||||
$(NAME)_CFLAGS += -Wno-unused-value -Wno-strict-aliasing
|
||||
|
||||
$(NAME)_INCLUDES += .
|
||||
GLOBAL_INCLUDES += include include/aos csp/lwip/include
|
||||
GLOBAL_LDFLAGS += -lpthread -lm -lrt
|
||||
GLOBAL_DEFINES += CONFIG_AOS_RHINO_MMREGION
|
||||
GLOBAL_DEFINES += CONFIG_YSH_CMD_DUMPSYS
|
||||
GLOBAL_CFLAGS += -Wall -Wno-missing-field-initializers -Wno-strict-aliasing -Wno-address -Wno-unused-result
|
||||
GLOBAL_CFLAGS += -Wno-unused-variable -Wno-unused-but-set-variable
|
||||
GLOBAL_DEFINES += CSP_LINUXHOST
|
||||
GLOBAL_DEFINES += CONFIG_LOGMACRO_DETAILS
|
||||
GLOBAL_DEFINES += CONFIG_AOS_FATFS_SUPPORT
|
||||
GLOBAL_DEFINES += CONFIG_AOS_FATFS_SUPPORT_MMC
|
||||
GLOBAL_DEFINES += CONFIG_AOS_FOTA_BREAKPOINT
|
||||
|
||||
$(NAME)_SOURCES :=
|
||||
|
||||
ifneq ($(vcall),posix)
|
||||
$(NAME)_SOURCES += soc/soc_impl.c
|
||||
$(NAME)_SOURCES += soc/hook_impl.c
|
||||
$(NAME)_SOURCES += soc/trace_impl.c
|
||||
else
|
||||
$(NAME)_DEFINES += CONFIG_VCALL_POSIX
|
||||
endif
|
||||
|
||||
$(NAME)_SOURCES += soc/uart.c
|
||||
|
||||
# mcu
|
||||
$(NAME)_SOURCES += main/arg_options.c
|
||||
$(NAME)_SOURCES += main/main.c
|
||||
$(NAME)_SOURCES += main/hw.c
|
||||
$(NAME)_SOURCES += main/wifi_port.c
|
||||
$(NAME)_SOURCES += main/nand.c
|
||||
$(NAME)_SOURCES += main/vfs_trap.c
|
||||
|
||||
ifneq (,$(filter modules.fs.fatfs,$(COMPONENTS)))
|
||||
$(NAME)_SOURCES += main/sdmmc.c
|
||||
endif
|
||||
|
||||
ifeq ($(linux80211),1)
|
||||
$(NAME)_SOURCES += csp/wifi/common.c
|
||||
$(NAME)_SOURCES += csp/wifi/linux.c
|
||||
$(NAME)_SOURCES += csp/wifi/osdep.c
|
||||
ifeq ($(HOST_ARCH), rockchiplinux)
|
||||
$(NAME)_SOURCES += csp/wifi/mesh-rk1108.c
|
||||
else
|
||||
$(NAME)_SOURCES += csp/wifi/mesh.c
|
||||
endif
|
||||
$(NAME)_SOURCES += csp/wifi/radiotap/radiotap.c
|
||||
$(NAME)_DEFINES += LINUX_MESH_80211
|
||||
$(NAME)_CFLAGS += -Wno-unused-but-set-variable
|
||||
endif
|
||||
|
||||
ifneq (,$(filter protocols.net,$(COMPONENTS)))
|
||||
LWIP := 1
|
||||
endif
|
||||
|
||||
ifeq (1,$(LWIP))
|
||||
$(NAME)_SOURCES += \
|
||||
csp/lwip/netif/delif.c \
|
||||
csp/lwip/netif/fifo.c \
|
||||
csp/lwip/netif/tapif.c \
|
||||
csp/lwip/netif/tcpdump.c \
|
||||
csp/lwip/netif/tunif.c
|
||||
|
||||
$(NAME)_SOURCES += csp/lwip/lwip_linuxhost.c
|
||||
endif
|
||||
101
Living_SDK/platform/mcu/linux/main/arg_options.c
Normal file
101
Living_SDK/platform/mcu/linux/main/arg_options.c
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <arg_options.h>
|
||||
#include <aos/aos.h>
|
||||
|
||||
static void shift_argv(options_t *options, int i)
|
||||
{
|
||||
options->argc --;
|
||||
while (i < options->argc) {
|
||||
options->argv[i] = options->argv[i+1];
|
||||
i ++;
|
||||
}
|
||||
}
|
||||
|
||||
void parse_options(options_t *options)
|
||||
{
|
||||
char **argv = options->argv;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < options->argc;) {
|
||||
if (!strcmp(argv[i], "--mesh-master")) {
|
||||
options->cli.enable = false;
|
||||
i ++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[i], "--no-cli")) {
|
||||
options->cli.enable = false;
|
||||
shift_argv(options, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[i], "--per-pid-flash")) {
|
||||
options->flash.per_pid = true;
|
||||
shift_argv(options, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[i], "--no-per-pid-flash")) {
|
||||
options->flash.per_pid = false;
|
||||
shift_argv(options, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[i], "--tapif")) {
|
||||
options->lwip.tapif = true;
|
||||
shift_argv(options, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[i], "--no-tapif")) {
|
||||
options->lwip.tapif = false;
|
||||
shift_argv(options, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[i], "--log")) {
|
||||
shift_argv(options, i);
|
||||
if (i >= options->argc) {
|
||||
options->log_level = AOS_LL_DEBUG;
|
||||
continue;
|
||||
}
|
||||
|
||||
const char *ll = argv[i];
|
||||
if (strcmp(ll, "fatal") == 0)
|
||||
options->log_level = AOS_LL_FATAL;
|
||||
else if (strcmp(ll, "error") == 0)
|
||||
options->log_level = AOS_LL_ERROR;
|
||||
else if (strcmp(ll, "warn") == 0)
|
||||
options->log_level = AOS_LL_WARN;
|
||||
else if (strcmp(ll, "info") == 0)
|
||||
options->log_level = AOS_LL_INFO;
|
||||
else if (strcmp(ll, "debug") == 0)
|
||||
options->log_level = AOS_LL_DEBUG;
|
||||
shift_argv(options, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(argv[i], "-i")) {
|
||||
i ++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i + 1 >= options->argc) {
|
||||
i ++;
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef TFS_EMULATE
|
||||
options->id2_index = atoi(argv[i + 1]);
|
||||
#endif
|
||||
shift_argv(options, i);
|
||||
shift_argv(options, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
261
Living_SDK/platform/mcu/linux/main/hw.c
Normal file
261
Living_SDK/platform/mcu/linux/main/hw.c
Normal file
|
|
@ -0,0 +1,261 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
/**
|
||||
* caution
|
||||
* linuxhost hw.c won't use any lwip functionalities,
|
||||
* disable WITH_LWIP to avoid close() -> lwip_close()
|
||||
*/
|
||||
#undef WITH_LWIP
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <hal/soc/soc.h>
|
||||
#include <hal/soc/timer.h>
|
||||
#include <hal/base.h>
|
||||
#include <hal/wifi.h>
|
||||
#include <arg_options.h>
|
||||
|
||||
#define TAG "hw"
|
||||
|
||||
static bool per_pid_flash;
|
||||
static int open_flash(int pno, bool w)
|
||||
{
|
||||
char fn[64];
|
||||
int flash_fd;
|
||||
|
||||
if (per_pid_flash)
|
||||
snprintf(fn, sizeof(fn), "./aos_partition_%d_%d.bin", getpid(), pno);
|
||||
else
|
||||
snprintf(fn, sizeof(fn), "./aos_partition_%d.bin", pno);
|
||||
|
||||
if(w)
|
||||
flash_fd = open(fn, O_RDWR);
|
||||
else
|
||||
flash_fd = open(fn, O_RDONLY);
|
||||
|
||||
if (flash_fd >= 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
umask(0111);
|
||||
flash_fd = creat(fn, S_IRWXU | S_IRWXG);
|
||||
if (flash_fd < 0)
|
||||
goto out;
|
||||
|
||||
close(flash_fd);
|
||||
flash_fd = open(fn, O_RDWR);
|
||||
out:
|
||||
return flash_fd;
|
||||
}
|
||||
|
||||
int32_t hal_flash_write(hal_partition_t pno, uint32_t* poff, const void* buf ,uint32_t buf_size)
|
||||
{
|
||||
int ret;
|
||||
char *tmp, *new_val;
|
||||
|
||||
int flash_fd = open_flash(pno, true);
|
||||
if (flash_fd < 0)
|
||||
return -1;
|
||||
|
||||
char *origin = (char *)malloc(buf_size);
|
||||
if (!origin) {
|
||||
ret = -1;
|
||||
goto exit;
|
||||
}
|
||||
memset(origin, -1, buf_size);
|
||||
|
||||
ret = pread(flash_fd, origin, buf_size, *poff);
|
||||
if (ret < 0) {
|
||||
perror("error reading flash:");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
tmp = origin;
|
||||
new_val = (char *)buf;
|
||||
for (int i = 0; i < buf_size; i++) {
|
||||
(*tmp) &= (*new_val);
|
||||
new_val++;
|
||||
tmp++;
|
||||
}
|
||||
|
||||
ret = pwrite(flash_fd, origin, buf_size, *poff);
|
||||
if (ret < 0)
|
||||
perror("error writing flash:");
|
||||
else
|
||||
*poff += ret;
|
||||
|
||||
exit:
|
||||
close(flash_fd);
|
||||
free(origin);
|
||||
return ret < 0 ? ret : 0;
|
||||
}
|
||||
|
||||
int32_t hal_flash_read(hal_partition_t pno, uint32_t* poff, void* buf, uint32_t buf_size)
|
||||
{
|
||||
int flash_fd;
|
||||
|
||||
if (poff == NULL)
|
||||
return -1;
|
||||
|
||||
flash_fd = open_flash(pno, false);
|
||||
if (flash_fd < 0)
|
||||
return -1;
|
||||
|
||||
int ret = pread(flash_fd, buf, buf_size, *poff);
|
||||
if (ret < 0)
|
||||
perror("error reading flash:");
|
||||
else
|
||||
*poff += ret;
|
||||
close(flash_fd);
|
||||
|
||||
return ret < 0 ? ret : 0;
|
||||
}
|
||||
|
||||
int32_t hal_flash_erase(hal_partition_t in_partition, uint32_t off_set,
|
||||
uint32_t size)
|
||||
{
|
||||
int ret;
|
||||
int flash_fd = open_flash(in_partition, true);
|
||||
if (flash_fd < 0)
|
||||
return -1;
|
||||
|
||||
char *buf = (char *)malloc(size);
|
||||
if (!buf) {
|
||||
ret = -1;
|
||||
goto exit;
|
||||
}
|
||||
memset(buf, -1, size);
|
||||
|
||||
ret = pwrite(flash_fd, buf, size, off_set);
|
||||
if (ret < 0)
|
||||
perror("error erase flash:");
|
||||
|
||||
exit:
|
||||
close(flash_fd);
|
||||
free(buf);
|
||||
return ret < 0 ? ret : 0;
|
||||
}
|
||||
|
||||
void hal_reboot(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#ifdef VCALL_RHINO
|
||||
#include <k_api.h>
|
||||
#define us2tick(us) \
|
||||
((us * RHINO_CONFIG_TICKS_PER_SECOND + 999999) / 1000000)
|
||||
|
||||
static void _timer_cb(void *timer, void *arg)
|
||||
{
|
||||
timer_dev_t *tmr = arg;
|
||||
tmr->config.cb(tmr->config.arg);
|
||||
}
|
||||
|
||||
int32_t hal_timer_init(timer_dev_t *tim)
|
||||
{
|
||||
if (tim->config.reload_mode == TIMER_RELOAD_AUTO) {
|
||||
krhino_timer_dyn_create((ktimer_t **)&tim->priv, "hwtmr", _timer_cb,
|
||||
us2tick(tim->config.period), us2tick(tim->config.period), tim, 0);
|
||||
}
|
||||
else {
|
||||
krhino_timer_dyn_create((ktimer_t **)&tim->priv, "hwtmr", _timer_cb,
|
||||
us2tick(tim->config.period), 0, tim, 0);
|
||||
}
|
||||
}
|
||||
|
||||
int hal_timer_start(timer_dev_t *tmr)
|
||||
{
|
||||
return krhino_timer_start(tmr->priv);
|
||||
}
|
||||
|
||||
void hal_timer_stop(timer_dev_t *tmr)
|
||||
{
|
||||
krhino_timer_stop(tmr->priv);
|
||||
krhino_timer_dyn_del(tmr->priv);
|
||||
tmr->priv = NULL;
|
||||
}
|
||||
|
||||
int csp_printf(const char *fmt, ...)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
|
||||
va_list args;
|
||||
int ret;
|
||||
|
||||
RHINO_CRITICAL_ENTER();
|
||||
|
||||
va_start(args, fmt);
|
||||
ret = vprintf(fmt, args);
|
||||
va_end(args);
|
||||
|
||||
fflush(stdout);
|
||||
|
||||
RHINO_CRITICAL_EXIT();
|
||||
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
int csp_printf(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
int ret;
|
||||
|
||||
va_start(args, fmt);
|
||||
ret = vprintf(fmt, args);
|
||||
va_end(args);
|
||||
|
||||
fflush(stdout);
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(DEV_SAL_MK3060)
|
||||
extern hal_wifi_module_t aos_wifi_module_mk3060;
|
||||
#else
|
||||
extern hal_wifi_module_t sim_aos_wifi_linux;
|
||||
#endif
|
||||
uart_dev_t uart_0;
|
||||
|
||||
void linux_wifi_register(void);
|
||||
void hw_start_hal(options_t *poptions)
|
||||
{
|
||||
uart_0.port = 0;
|
||||
uart_0.config.baud_rate = 921600;
|
||||
uart_0.config.data_width = DATA_WIDTH_8BIT;
|
||||
uart_0.config.parity = NO_PARITY;
|
||||
uart_0.config.stop_bits = STOP_BITS_1;
|
||||
uart_0.config.flow_control = FLOW_CONTROL_DISABLED;
|
||||
|
||||
per_pid_flash = poptions->flash.per_pid;
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
if (poptions->cli.enable)
|
||||
hal_uart_init(&uart_0);
|
||||
#endif
|
||||
|
||||
#ifdef AOS_HAL
|
||||
#if defined(DEV_SAL_MK3060)
|
||||
hal_wifi_register_module(&aos_wifi_module_mk3060);
|
||||
#else
|
||||
hal_wifi_register_module(&sim_aos_wifi_linux);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef LINUX_MESH_80211
|
||||
linux_wifi_register();
|
||||
#endif
|
||||
}
|
||||
191
Living_SDK/platform/mcu/linux/main/main.c
Executable file
191
Living_SDK/platform/mcu/linux/main/main.c
Executable file
|
|
@ -0,0 +1,191 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <sys/resource.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <aos/aos.h>
|
||||
#include <arg_options.h>
|
||||
|
||||
#ifndef CONFIG_VCALL_POSIX
|
||||
#include <k_api.h>
|
||||
#endif
|
||||
|
||||
#define TAG "main"
|
||||
|
||||
#ifdef TFS_EMULATE
|
||||
extern int tfs_emulate_id2_index;
|
||||
#endif
|
||||
|
||||
extern void krhino_lwip_init(int enable_tapif);
|
||||
/* check in gcc sources gcc/gcov-io.h for the prototype */
|
||||
extern void __gcov_flush(void);
|
||||
extern void rl_free_line_state(void);
|
||||
extern void rl_cleanup_after_signal(void);
|
||||
extern void hw_start_hal(options_t *poptions);
|
||||
extern void trace_start();
|
||||
extern void netmgr_init(void);
|
||||
extern int aos_framework_init(void);
|
||||
extern int aos_cli_init(void);
|
||||
extern void cpu_tmr_sync(void);
|
||||
|
||||
static options_t options = { 0 };
|
||||
static kinit_t kinit = { 0 };
|
||||
|
||||
static void aos_features_init(void);
|
||||
static void signal_handler(int signo);
|
||||
static int setrlimit_for_vfs(void);
|
||||
|
||||
static void exit_clean(void)
|
||||
{
|
||||
char fn[64] = {0};
|
||||
snprintf(fn, sizeof(fn), "rm -f ./aos_partition_%d_*", getpid());
|
||||
system(fn);
|
||||
}
|
||||
|
||||
static void app_entry(void *arg)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
kinit.argc = options.argc;
|
||||
kinit.argv = options.argv;
|
||||
kinit.cli_enable = options.cli.enable;
|
||||
|
||||
#ifndef CONFIG_VCALL_POSIX
|
||||
cpu_tmr_sync();
|
||||
#endif
|
||||
|
||||
aos_features_init();
|
||||
|
||||
#ifdef CONFIG_AOS_MESHYTS
|
||||
options.flash.per_pid = true;
|
||||
#else
|
||||
options.flash.per_pid = false;
|
||||
#endif
|
||||
hw_start_hal(&options);
|
||||
|
||||
aos_kernel_init(&kinit);
|
||||
}
|
||||
|
||||
static void start_app(void)
|
||||
{
|
||||
#ifndef CONFIG_VCALL_POSIX
|
||||
#if (RHINO_CONFIG_CPU_NUM > 1)
|
||||
ktask_t *app_task;
|
||||
krhino_task_cpu_dyn_create(&app_task, "app_task", 0, 20, 0, 2048, app_entry, 0, 1);
|
||||
#else
|
||||
aos_task_new("app", app_entry, NULL, 8192);
|
||||
#endif
|
||||
#else
|
||||
aos_task_new("app", app_entry, NULL, 8192);
|
||||
#endif
|
||||
}
|
||||
|
||||
int csp_get_args(const char ***pargv)
|
||||
{
|
||||
*pargv = (const char **)options.argv;
|
||||
return options.argc;
|
||||
}
|
||||
|
||||
void aos_features_init(void)
|
||||
{
|
||||
#ifdef CONFIG_NET_LWIP
|
||||
if (options.lwip.enable) {
|
||||
krhino_lwip_init(options.lwip.tapif);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void signal_handler(int signo)
|
||||
{
|
||||
LOGD(TAG, "Received signal %d\n", signo);
|
||||
|
||||
#ifdef ENABLE_GCOV
|
||||
__gcov_flush();
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_AOS_DDA
|
||||
rl_free_line_state ();
|
||||
rl_cleanup_after_signal ();
|
||||
#endif
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#define ARCH_MAX_NOFILE 64
|
||||
int setrlimit_for_vfs(void)
|
||||
{
|
||||
int ret;
|
||||
struct rlimit rlmt;
|
||||
|
||||
getrlimit(RLIMIT_NOFILE, &rlmt);
|
||||
if (rlmt.rlim_cur > ARCH_MAX_NOFILE) {
|
||||
rlmt.rlim_cur = ARCH_MAX_NOFILE;
|
||||
ret = setrlimit(RLIMIT_NOFILE, &rlmt);
|
||||
if (ret != 0) {
|
||||
LOGE(TAG, "setrlimit error: %s", strerror(errno));
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
LOGD(TAG, "Limit max open files to %d", (int)rlmt.rlim_cur);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int ret;
|
||||
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
|
||||
options.argc = argc;
|
||||
options.argv = argv;
|
||||
options.lwip.enable = true;
|
||||
#if defined(TAPIF_DEFAULT_OFF) || !defined(WITH_LWIP)
|
||||
options.lwip.tapif = false;
|
||||
#else
|
||||
options.lwip.tapif = true;
|
||||
#endif
|
||||
options.log_level = AOS_LL_WARN;
|
||||
options.cli.enable = true;
|
||||
|
||||
#if defined(CONFIG_AOS_DDA) || defined(ENABLE_GCOV)
|
||||
signal(SIGINT, signal_handler);
|
||||
#endif
|
||||
#ifdef CONFIG_AOS_UT
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
atexit(exit_clean);
|
||||
|
||||
aos_init();
|
||||
|
||||
ret = setrlimit_for_vfs();
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
parse_options(&options);
|
||||
|
||||
aos_set_log_level(options.log_level);
|
||||
|
||||
#ifdef TFS_EMULATE
|
||||
tfs_emulate_id2_index = options.id2_index;
|
||||
#endif
|
||||
|
||||
start_app();
|
||||
|
||||
aos_start();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int board_cli_init(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
190
Living_SDK/platform/mcu/linux/main/nand.c
Normal file
190
Living_SDK/platform/mcu/linux/main/nand.c
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <aos/aos.h>
|
||||
#include <hal/soc/nand.h>
|
||||
|
||||
#define CONFIG_FLASH_START_ADDR 0
|
||||
#define CONFIG_FLASH_PAGE_SIZE 512
|
||||
#define CONFIG_FLASH_SPARE_SIZE 16
|
||||
#define CONFIG_FLASH_BLOCK_SIZE 32
|
||||
#define CONFIG_FLASH_ZONE_SIZE 128
|
||||
#define CONFIG_FLASH_ZONE_NUM 1
|
||||
|
||||
static void nand_ll_init(int fd)
|
||||
{
|
||||
int flash_size;
|
||||
|
||||
flash_size = (CONFIG_FLASH_PAGE_SIZE + CONFIG_FLASH_SPARE_SIZE) *
|
||||
CONFIG_FLASH_BLOCK_SIZE * CONFIG_FLASH_ZONE_SIZE * CONFIG_FLASH_ZONE_NUM;
|
||||
|
||||
char *buffer = (char *)malloc(flash_size);
|
||||
memset(buffer, 0xff, flash_size);
|
||||
pwrite(fd, buffer, flash_size, CONFIG_FLASH_START_ADDR);
|
||||
free(buffer);
|
||||
|
||||
}
|
||||
|
||||
static int nand_open(int w)
|
||||
{
|
||||
int fd;
|
||||
char path[64] = {0};
|
||||
snprintf(path, sizeof(path), "./aos_partition_%d_nand.bin", getpid());
|
||||
|
||||
if (w)
|
||||
fd = open(path, O_RDWR);
|
||||
else
|
||||
fd = open(path, O_RDONLY);
|
||||
|
||||
if (fd < 0) {
|
||||
umask(0111);
|
||||
fd = creat(path, S_IRWXU | S_IRWXG);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
nand_ll_init(fd);
|
||||
close(fd);
|
||||
fd = open(path, O_RDWR);
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
int32_t hal_nand_init(nand_dev_t *nand)
|
||||
{
|
||||
// to ensure the simulation file is created!
|
||||
int fd = nand_open(1);
|
||||
if (fd < 0)
|
||||
return -EIO;
|
||||
close(fd);
|
||||
|
||||
nand->base_addr = CONFIG_FLASH_START_ADDR;
|
||||
nand->config.page_size = CONFIG_FLASH_PAGE_SIZE;
|
||||
nand->config.spare_area_size = CONFIG_FLASH_SPARE_SIZE;
|
||||
nand->config.block_size = CONFIG_FLASH_BLOCK_SIZE;
|
||||
nand->config.zone_size = CONFIG_FLASH_ZONE_SIZE;
|
||||
nand->config.zone_number = CONFIG_FLASH_ZONE_NUM;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_nand_finalize(nand_dev_t *nand)
|
||||
{
|
||||
memset(nand, 0, sizeof(nand_dev_t));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_nand_read_page(nand_dev_t *nand, nand_addr_t *addr, uint8_t *data, uint32_t page_count)
|
||||
{
|
||||
int nbytes, location, full_page_size;
|
||||
int fd = nand_open(0);
|
||||
|
||||
if (fd < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
location = ((addr->zone * (nand->config.zone_size)) + addr->block) * (nand->config.block_size) + addr->page;
|
||||
full_page_size = nand->config.page_size + nand->config.spare_area_size;
|
||||
|
||||
if (data) {
|
||||
while (page_count) {
|
||||
nbytes = pread(fd, data, nand->config.page_size, location * full_page_size);
|
||||
location++;
|
||||
page_count--;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return nbytes < 0 ? nbytes : 0;
|
||||
}
|
||||
|
||||
int32_t hal_nand_write_page(nand_dev_t *nand, nand_addr_t *addr, uint8_t *data, uint32_t page_count)
|
||||
{
|
||||
int nbytes, location, full_page_size;
|
||||
int fd = nand_open(1);
|
||||
|
||||
if (fd < 0)
|
||||
return -EIO;
|
||||
|
||||
location = ((addr->zone * (nand->config.zone_size)) + addr->block) * (nand->config.block_size) + addr->page;
|
||||
full_page_size = nand->config.page_size + nand->config.spare_area_size;
|
||||
|
||||
if (data) {
|
||||
while (page_count) {
|
||||
nbytes = pwrite(fd, data, nand->config.page_size, location * full_page_size);
|
||||
location++;
|
||||
page_count--;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return nbytes < 0 ? nbytes : 0;
|
||||
}
|
||||
|
||||
int32_t hal_nand_read_spare(nand_dev_t *nand, nand_addr_t *addr, uint8_t *data, uint32_t data_len)
|
||||
{
|
||||
int nbytes, location, full_page_size;
|
||||
int fd = nand_open(0);
|
||||
|
||||
if (fd < 0)
|
||||
return -EIO;
|
||||
|
||||
location = ((addr->zone * (nand->config.zone_size)) + addr->block) * (nand->config.block_size) + addr->page;
|
||||
full_page_size = nand->config.page_size + nand->config.spare_area_size;
|
||||
|
||||
if (data) {
|
||||
nbytes = pread(fd, data, nand->config.spare_area_size, location * full_page_size + nand->config.page_size);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return nbytes < 0 ? nbytes : 0;
|
||||
}
|
||||
|
||||
int32_t hal_nand_write_spare(nand_dev_t *nand, nand_addr_t *addr, uint8_t *data, uint32_t data_len)
|
||||
{
|
||||
int nbytes, location, full_page_size;
|
||||
int fd = nand_open(1);
|
||||
|
||||
if (fd < 0)
|
||||
return -EIO;
|
||||
|
||||
location = ((addr->zone * (nand->config.zone_size)) + addr->block) * (nand->config.block_size) + addr->page;
|
||||
full_page_size = nand->config.page_size + nand->config.spare_area_size;
|
||||
|
||||
if (data) {
|
||||
nbytes = pwrite(fd, data, nand->config.spare_area_size, location * full_page_size + nand->config.page_size);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return nbytes < 0 ? nbytes : 0;
|
||||
|
||||
}
|
||||
|
||||
int32_t hal_nand_erase_block(nand_dev_t *nand, nand_addr_t *addr)
|
||||
{
|
||||
char *buffer;
|
||||
int nbytes, location, block_size, full_page_size;
|
||||
int fd = nand_open(1);
|
||||
|
||||
if (fd < 0)
|
||||
return -EIO;
|
||||
|
||||
location = ((addr->zone * (nand->config.zone_size)) + addr->block) * (nand->config.block_size);
|
||||
full_page_size = nand->config.page_size + nand->config.spare_area_size;
|
||||
block_size = (nand->config.block_size) * full_page_size;
|
||||
|
||||
buffer = (char *)malloc(block_size);
|
||||
if (!buffer)
|
||||
return -EIO;
|
||||
|
||||
memset(buffer, 0xff, block_size);
|
||||
nbytes = pwrite(fd, buffer, block_size, location * full_page_size);
|
||||
|
||||
close(fd);
|
||||
free(buffer);
|
||||
return nbytes < 0 ? nbytes : 0;
|
||||
}
|
||||
105
Living_SDK/platform/mcu/linux/main/sdmmc.c
Normal file
105
Living_SDK/platform/mcu/linux/main/sdmmc.c
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <hal/soc/sd.h>
|
||||
|
||||
#define SD_BLOCK_SIZE 512
|
||||
|
||||
static int sdmmc_open(int w)
|
||||
{
|
||||
int fd;
|
||||
char path[64] = {0};
|
||||
snprintf(path, sizeof(path), "./aos_partition_%d_sdcard.bin", getpid());
|
||||
|
||||
if (w)
|
||||
fd = open(path, O_RDWR);
|
||||
else
|
||||
fd = open(path, O_RDONLY);
|
||||
|
||||
if (fd < 0) {
|
||||
umask(0111);
|
||||
fd = creat(path, S_IRWXU | S_IRWXG);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
close(fd);
|
||||
fd = open(path, O_RDWR);
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
static int sdmmc_read(uint8_t *buff, uint32_t sector, uint32_t count)
|
||||
{
|
||||
int fd = sdmmc_open(0);
|
||||
if (fd < 0)
|
||||
return -EIO;
|
||||
|
||||
int ret = pread(fd, buff, (count * SD_BLOCK_SIZE), (sector * SD_BLOCK_SIZE));
|
||||
close(fd);
|
||||
|
||||
if (ret < 0)
|
||||
return -EIO;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sdmmc_write(const char *buff, uint32_t sector, uint32_t count)
|
||||
{
|
||||
int fd = sdmmc_open(1);
|
||||
if (fd < 0)
|
||||
return -EIO;
|
||||
|
||||
int ret = pwrite(fd, buff, (count * SD_BLOCK_SIZE), (sector * SD_BLOCK_SIZE));
|
||||
close(fd);
|
||||
|
||||
if (ret < 0)
|
||||
return -EIO;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_sd_init(sd_dev_t *sd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_sd_blks_read(sd_dev_t *sd, uint8_t *data, uint32_t blk_addr, uint32_t blks, uint32_t timeout)
|
||||
{
|
||||
return sdmmc_read(data, blk_addr, blks);
|
||||
}
|
||||
|
||||
int32_t hal_sd_blks_write(sd_dev_t *sd, uint8_t *data, uint32_t blk_addr, uint32_t blks, uint32_t timeout)
|
||||
{
|
||||
return sdmmc_write(data, blk_addr, blks);
|
||||
}
|
||||
|
||||
int32_t hal_sd_erase(sd_dev_t *sd, uint32_t blk_start_addr, uint32_t blk_end_addr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_sd_stat_get(sd_dev_t *sd, hal_sd_stat *stat)
|
||||
{
|
||||
*stat = SD_STAT_TRANSFER;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_sd_info_get(sd_dev_t *sd, hal_sd_info_t *info)
|
||||
{
|
||||
info->blk_nums = 256;
|
||||
info->blk_size = SD_BLOCK_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_sd_finalize(sd_dev_t *sd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
56
Living_SDK/platform/mcu/linux/main/sensor.c
Normal file
56
Living_SDK/platform/mcu/linux/main/sensor.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include "hal/hal.h"
|
||||
#include "aos/aos.h"
|
||||
|
||||
static const char *TAG = "yoc_sensor";
|
||||
|
||||
static const sensor_node_t g_sensor_list[] = {
|
||||
{SENSOR_TYPE_TEMPERATURE,"tempearture"}
|
||||
};
|
||||
|
||||
static int hce_sensor_init(hal_sensor_module_t *m, void *something)
|
||||
{
|
||||
LOGI(TAG, "hce_sensor_init ");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hce_sensor_get_sensor_list(hal_sensor_module_t *m, sensor_node_t const **list)
|
||||
{
|
||||
*list = g_sensor_list;
|
||||
return sizeof g_sensor_list / sizeof(sensor_node_t);
|
||||
}
|
||||
|
||||
static int hce_sensor_enable(hal_sensor_module_t *m, sensor_type type)
|
||||
{
|
||||
LOGI(TAG, "hce_sensor_enable handle= %d!", type);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hce_sensor_disable(hal_sensor_module_t *m, sensor_type type)
|
||||
{
|
||||
LOGI(TAG, "hce_sensor_disable handle= %d!", type);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hce_sensor_read(hal_sensor_module_t *m, sensor_type type, char *buf, int buf_size)
|
||||
{
|
||||
LOGI(TAG, "hce_sensor_read handle= %d!", type);
|
||||
return 0;
|
||||
}
|
||||
|
||||
hal_sensor_module_t linuxhost_sensor_module = {
|
||||
.base.name = "yoc_sensor_module_linuxhost",
|
||||
.init = hce_sensor_init,
|
||||
.get_sensor_list = hce_sensor_get_sensor_list,
|
||||
.enable = hce_sensor_enable,
|
||||
.disable = hce_sensor_disable,
|
||||
.read = hce_sensor_read,
|
||||
};
|
||||
44
Living_SDK/platform/mcu/linux/main/vfs_trap.c
Executable file
44
Living_SDK/platform/mcu/linux/main/vfs_trap.c
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int trap_open(const char *path, int flags)
|
||||
{
|
||||
#ifdef WITH_LWIP
|
||||
return -ENOSYS;
|
||||
#else
|
||||
int fd = open(path, flags);
|
||||
if (fd < 0) {
|
||||
fd = open(path, O_RDWR | O_CREAT, 0644);
|
||||
}
|
||||
return fd;
|
||||
#endif
|
||||
}
|
||||
|
||||
int trap_read(int fd, void *buf, int len)
|
||||
{
|
||||
return read(fd, buf, len);
|
||||
}
|
||||
|
||||
int trap_write(int fd, const void *buf, int len)
|
||||
{
|
||||
return write(fd, buf, len);
|
||||
}
|
||||
|
||||
int trap_close(int fd)
|
||||
{
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
int trap_fcntl(int fd, int cmd, int val)
|
||||
{
|
||||
return fcntl(fd, cmd, val);
|
||||
}
|
||||
|
||||
177
Living_SDK/platform/mcu/linux/main/wifi_port.c
Normal file
177
Living_SDK/platform/mcu/linux/main/wifi_port.c
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <hal/base.h>
|
||||
#include <hal/wifi.h>
|
||||
|
||||
static uint8_t fixmac[6] = {0xd8,0x96,0xe0,0x03,0x04,0x01};
|
||||
|
||||
static int wifi_init(hal_wifi_module_t *m)
|
||||
{
|
||||
printf("wifi init success!!\n");
|
||||
return 0;
|
||||
};
|
||||
|
||||
static void wifi_get_mac_addr(hal_wifi_module_t *m, uint8_t *mac)
|
||||
{
|
||||
(void)m;
|
||||
printf("wifi_get_mac_addr!!\n");
|
||||
memcpy(mac, fixmac, 6);
|
||||
};
|
||||
|
||||
static int wifi_start(hal_wifi_module_t *m, hal_wifi_init_type_t *init_para)
|
||||
{
|
||||
(void)init_para;
|
||||
|
||||
hal_wifi_ip_stat_t ip_stat;
|
||||
|
||||
char ip[] = "127.0.0.1";
|
||||
char mask[] = "255.255.255.0";
|
||||
char gate[] = "127.0.0.1";
|
||||
|
||||
memcpy(ip_stat.ip, ip, sizeof(ip));
|
||||
memcpy(ip_stat.mask, mask, sizeof(mask));
|
||||
memcpy(ip_stat.gate, gate, sizeof(gate));
|
||||
memcpy(ip_stat.mac, fixmac, 6);
|
||||
|
||||
if (m->ev_cb->stat_chg != NULL) {
|
||||
m->ev_cb->stat_chg(m, NOTIFY_STATION_UP, NULL);
|
||||
}
|
||||
|
||||
if (m->ev_cb->ip_got != NULL) {
|
||||
m->ev_cb->ip_got(m, &ip_stat, NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int wifi_start_adv(hal_wifi_module_t *m, hal_wifi_init_type_adv_t *init_para_adv)
|
||||
{
|
||||
(void)init_para_adv;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_ip_stat(hal_wifi_module_t *m, hal_wifi_ip_stat_t *out_net_para, hal_wifi_type_t wifi_type)
|
||||
{
|
||||
(void)wifi_type;
|
||||
|
||||
char ip[4] = {0x7f, 0x00, 0x00, 0x01};
|
||||
char mask[4] = {0xFF, 0xFF, 0xFF, 0x00};
|
||||
char gate[4] = {0x7f, 0x00, 0x00, 0x01};
|
||||
|
||||
memcpy(out_net_para->ip, ip, 4);
|
||||
memcpy(out_net_para->mask, mask, 4);
|
||||
memcpy(out_net_para->gate, gate, 4);
|
||||
memcpy(out_net_para->mac, fixmac, 6);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_link_stat(hal_wifi_module_t *m, hal_wifi_link_stat_t *out_stat)
|
||||
{
|
||||
out_stat->is_connected = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void start_scan(hal_wifi_module_t *m)
|
||||
{
|
||||
hal_wifi_scan_result_t scan_ret;
|
||||
ap_list_t aplist = {{0}, 0};
|
||||
scan_ret.ap_list = (ap_list_t *)&aplist;
|
||||
scan_ret.ap_num = 1;
|
||||
scan_ret.ap_list->ap_power = 80;
|
||||
memcpy(scan_ret.ap_list->ssid, "test", 5);
|
||||
|
||||
if (m->ev_cb->scan_compeleted != NULL) {
|
||||
m->ev_cb->scan_compeleted(m, &scan_ret, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void start_scan_adv(hal_wifi_module_t *m)
|
||||
{
|
||||
}
|
||||
|
||||
static int power_off(hal_wifi_module_t *m)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int power_on(hal_wifi_module_t *m)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int suspend(hal_wifi_module_t *m)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int suspend_station(hal_wifi_module_t *m)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int suspend_soft_ap(hal_wifi_module_t *m)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int set_channel(hal_wifi_module_t *m, int ch)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void start_monitor(hal_wifi_module_t *m)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void stop_monitor(hal_wifi_module_t *m)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void register_monitor_cb(hal_wifi_module_t *m, monitor_data_cb_t fn)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void register_wlan_mgnt_monitor_cb(hal_wifi_module_t *m, monitor_data_cb_t fn)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static int wlan_send_80211_raw_frame(hal_wifi_module_t *m, uint8_t *buf, int len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
hal_wifi_module_t sim_aos_wifi_linux = {
|
||||
.base.name = "sim_aos_wifi_linux",
|
||||
.init = wifi_init,
|
||||
.get_mac_addr = wifi_get_mac_addr,
|
||||
.start = wifi_start,
|
||||
.start_adv = wifi_start_adv,
|
||||
.get_ip_stat = get_ip_stat,
|
||||
.get_link_stat = get_link_stat,
|
||||
.start_scan = start_scan,
|
||||
.start_scan_adv = start_scan_adv,
|
||||
.power_off = power_off,
|
||||
.power_on = power_on,
|
||||
.suspend = suspend,
|
||||
.suspend_station = suspend_station,
|
||||
.suspend_soft_ap = suspend_soft_ap,
|
||||
.set_channel = set_channel,
|
||||
.start_monitor = start_monitor,
|
||||
.stop_monitor = stop_monitor,
|
||||
.register_monitor_cb = register_monitor_cb,
|
||||
.register_wlan_mgnt_monitor_cb = register_wlan_mgnt_monitor_cb,
|
||||
.wlan_send_80211_raw_frame = wlan_send_80211_raw_frame
|
||||
};
|
||||
|
||||
75
Living_SDK/platform/mcu/linux/soc/hook_impl.c
Executable file
75
Living_SDK/platform/mcu/linux/soc/hook_impl.c
Executable file
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
void soc_hw_timer_init()
|
||||
{
|
||||
}
|
||||
|
||||
#if (RHINO_CONFIG_USER_HOOK > 0)
|
||||
volatile uint64_t cpu_flag;
|
||||
void krhino_idle_pre_hook(void)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
uint8_t cpu;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
cpu = cpu_cur_get();
|
||||
cpu_flag |= (1 << cpu);
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
}
|
||||
|
||||
void krhino_idle_hook(void)
|
||||
{
|
||||
cpu_idle_hook();
|
||||
}
|
||||
|
||||
void krhino_init_hook(void)
|
||||
{
|
||||
#if (RHINO_CONFIG_HW_COUNT > 0)
|
||||
soc_hw_timer_init();
|
||||
#endif
|
||||
cpu_init_hook();
|
||||
}
|
||||
#endif
|
||||
|
||||
void krhino_start_hook(void)
|
||||
{
|
||||
#if (RHINO_CONFIG_TASK_SCHED_STATS > 0)
|
||||
krhino_task_sched_stats_reset();
|
||||
#endif
|
||||
cpu_start_hook();
|
||||
}
|
||||
|
||||
void krhino_task_create_hook(ktask_t *task)
|
||||
{
|
||||
cpu_task_create_hook(task);
|
||||
}
|
||||
|
||||
void krhino_task_del_hook(ktask_t *task, res_free_t *arg)
|
||||
{
|
||||
cpu_task_del_hook(task, arg);
|
||||
}
|
||||
|
||||
void krhino_task_switch_hook(ktask_t *orgin, ktask_t *dest)
|
||||
{
|
||||
(void)orgin;
|
||||
(void)dest;
|
||||
}
|
||||
|
||||
void krhino_tick_hook(void)
|
||||
{
|
||||
}
|
||||
|
||||
void krhino_task_abort_hook(ktask_t *task)
|
||||
{
|
||||
(void)task;
|
||||
}
|
||||
|
||||
void krhino_mm_alloc_hook(void *mem, size_t size)
|
||||
{
|
||||
(void)mem;
|
||||
(void)size;
|
||||
}
|
||||
71
Living_SDK/platform/mcu/linux/soc/soc_impl.c
Normal file
71
Living_SDK/platform/mcu/linux/soc/soc_impl.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <k_api.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <execinfo.h>
|
||||
|
||||
#define _linux_backtrace_depth 10
|
||||
|
||||
#if (RHINO_CONFIG_HW_COUNT > 0)
|
||||
hr_timer_t soc_hr_hw_cnt_get(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef GCOV_ENABLE
|
||||
#define SYS_DYN_POOL_SIZE (1024 *1024)
|
||||
#else
|
||||
#define SYS_DYN_POOL_SIZE (512 *1024)
|
||||
#endif /* GCOV_ENABLE */
|
||||
|
||||
#if (RHINO_CONFIG_CPU_NUM > 1)
|
||||
#undef SYS_DYN_POOL_SIZE
|
||||
#define SYS_DYN_POOL_SIZE (1024 *1024)
|
||||
#endif
|
||||
|
||||
size_t sys_pool_start[SYS_DYN_POOL_SIZE / sizeof(size_t)];
|
||||
k_mm_region_t g_mm_region[] = {{(uint8_t*)&sys_pool_start,SYS_DYN_POOL_SIZE}};
|
||||
int g_region_num = sizeof(g_mm_region)/sizeof(k_mm_region_t);
|
||||
|
||||
void _linux_backtrace()
|
||||
{
|
||||
void *array[_linux_backtrace_depth];
|
||||
size_t size;
|
||||
char **strings;
|
||||
size_t e;
|
||||
|
||||
size = backtrace (array,_linux_backtrace_depth);
|
||||
strings = (char **)backtrace_symbols (array, size);
|
||||
|
||||
fprintf(stderr, "Stack trace:\n");
|
||||
for (e = 0; e < size; e++) {
|
||||
fprintf(stderr, "%d %s \n",e,strings[e]);
|
||||
}
|
||||
|
||||
free (strings);
|
||||
}
|
||||
|
||||
#if (RHINO_CONFIG_TASK_STACK_CUR_CHECK > 0)
|
||||
size_t soc_get_cur_sp()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
void soc_err_proc(kstat_t err)
|
||||
{
|
||||
printf("kernel panic,err %d!\n",err);
|
||||
_linux_backtrace();
|
||||
if(err == RHINO_TASK_STACK_OVF){
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
krhino_err_proc_t g_err_proc = soc_err_proc;
|
||||
|
||||
233
Living_SDK/platform/mcu/linux/soc/trace_impl.c
Normal file
233
Living_SDK/platform/mcu/linux/soc/trace_impl.c
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <k_api.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <aos/network.h>
|
||||
#include <hal/trace.h>
|
||||
#include <aos/aos.h>
|
||||
|
||||
/* CLI Support */
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
/* Trace Open*/
|
||||
#if (RHINO_CONFIG_TRACE > 0)
|
||||
#define TRACE_TASK_STACK_SIZE 512
|
||||
|
||||
extern struct k_fifo trace_fifo;
|
||||
extern int32_t set_filter_task(const char * task_name);
|
||||
extern void set_event_mask(const uint32_t mask);
|
||||
extern void trace_deinit(void);
|
||||
static ktask_t *trace_task;
|
||||
static uint32_t trace_buf[1024];
|
||||
static volatile int trace_is_started;
|
||||
static char *filter_task;
|
||||
static char *ip_addr;
|
||||
static uint16_t ip_port = 8000;
|
||||
static int sockfd;
|
||||
|
||||
void *trace_hal_init()
|
||||
{
|
||||
int fd;
|
||||
struct sockaddr_in servaddr;
|
||||
|
||||
TRACE_INIT();
|
||||
|
||||
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
aos_cli_printf("create socket (%s:%u) error: %s(errno: %d)\r\n", ip_addr, ip_port, strerror(errno),errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = htons(ip_port);
|
||||
inet_aton(ip_addr, &servaddr.sin_addr);
|
||||
|
||||
if (connect(fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
|
||||
aos_cli_printf("connect (%s:%u) error: %s(errno: %d)\r\n", ip_addr, ip_port, strerror(errno),errno);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
aos_cli_printf("connected on (%s:%u)\r\n", ip_addr, ip_port);
|
||||
|
||||
return (void *)fd;
|
||||
}
|
||||
|
||||
|
||||
ssize_t trace_hal_send(void *handle, void *buf, size_t len)
|
||||
{
|
||||
int len_send = 0;
|
||||
int len_total_send = 0;
|
||||
|
||||
while (1) {
|
||||
len_send = send((int)handle, (buf + len_total_send) , len, 0);
|
||||
if (len_send < 0) {
|
||||
aos_cli_printf("send (%s:%u) msg error: %s(errno: %d)\r\n", ip_addr, ip_port, strerror(errno), errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
len_total_send += len_send;
|
||||
len -= len_send;
|
||||
if (len == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return len_send;
|
||||
}
|
||||
|
||||
ssize_t trace_hal_recv(void *handle, void *buf)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void trace_hal_deinit(void *handle)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = (int)handle;
|
||||
close(fd);
|
||||
*(int *)handle = -1;
|
||||
sockfd = -1;
|
||||
}
|
||||
|
||||
void trace_stop(void)
|
||||
{
|
||||
set_filter_task(NULL);
|
||||
set_event_mask(0);
|
||||
|
||||
trace_deinit();
|
||||
|
||||
trace_hal_deinit((void *)sockfd);
|
||||
|
||||
aos_cli_printf("trace (%s:%u) stop....\r\n", ip_addr, ip_port);
|
||||
|
||||
if (ip_addr) {
|
||||
aos_free(ip_addr);
|
||||
ip_addr = NULL;
|
||||
}
|
||||
|
||||
if (filter_task){
|
||||
aos_free(filter_task);
|
||||
filter_task = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void trace_entry(void *arg)
|
||||
{
|
||||
uint32_t len;
|
||||
while (1) {
|
||||
if (trace_is_started == 0 ){
|
||||
if (sockfd > 0) {
|
||||
trace_stop();
|
||||
}
|
||||
|
||||
krhino_task_sleep(200);
|
||||
} else {
|
||||
if (sockfd <= 0) {
|
||||
sockfd = (int)trace_hal_init();
|
||||
}
|
||||
|
||||
if (sockfd > 0) {
|
||||
len = fifo_out_all(&trace_fifo, trace_buf);
|
||||
if (len > 0) {
|
||||
trace_hal_send((void *)sockfd, trace_buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
krhino_task_sleep(20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_trace_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
const char *rtype = argc > 1 ? argv[1] : "";
|
||||
int ret = 0;
|
||||
|
||||
if (strcmp(rtype, "start") == 0) {
|
||||
trace_is_started = 1;
|
||||
|
||||
if (argc == 3 || argc == 4) {
|
||||
if (ip_addr == NULL) {
|
||||
ip_addr = (char *) aos_malloc(strlen(argv[2])+1);
|
||||
if (ip_addr == NULL) {
|
||||
k_err_proc(RHINO_NO_MEM);
|
||||
}
|
||||
|
||||
strncpy(ip_addr, argv[2], strlen(argv[2]));
|
||||
ip_addr[strlen(argv[2])] = '\0';
|
||||
}
|
||||
|
||||
if (argc ==4){
|
||||
ip_port = atoi(argv[3]);
|
||||
}
|
||||
} else {
|
||||
aos_cli_printf("trace must specify the host ip (port)... \r\n");
|
||||
return ;
|
||||
}
|
||||
|
||||
if (trace_task == NULL && krhino_task_dyn_create(&trace_task, "trace_task", NULL, 3,
|
||||
0, TRACE_TASK_STACK_SIZE, trace_entry, 1) != RHINO_SUCCESS) {
|
||||
aos_cli_printf("trace task creat fail \r\n");
|
||||
}
|
||||
} else if (strcmp(rtype, "task") == 0) {
|
||||
if (argc != 3) {
|
||||
return;
|
||||
}
|
||||
if (filter_task) {
|
||||
aos_free(filter_task);
|
||||
}
|
||||
|
||||
filter_task = (char *) aos_malloc(strlen(argv[2])+1);
|
||||
if (filter_task == NULL) {
|
||||
k_err_proc(RHINO_NO_MEM);
|
||||
}
|
||||
|
||||
strncpy(filter_task, argv[2], strlen(argv[2]));
|
||||
filter_task[strlen(argv[2])] = '\0';
|
||||
|
||||
set_filter_task(filter_task);
|
||||
} else if (strcmp(rtype, "event") == 0) {
|
||||
if (argc != 3) {
|
||||
return;
|
||||
}
|
||||
|
||||
set_event_mask(atoi(argv[2]));
|
||||
} else if (strcmp(rtype, "stop") == 0) {
|
||||
trace_is_started = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static struct cli_command ncmd = {
|
||||
.name = "trace",
|
||||
.help = "trace [start ip (port) | task task_name| event event_id| stop]",
|
||||
.function = handle_trace_cmd,
|
||||
};
|
||||
|
||||
void trace_start(void)
|
||||
{
|
||||
aos_cli_register_command(&ncmd);
|
||||
}
|
||||
#else
|
||||
void trace_start(void)
|
||||
{
|
||||
printf("trace config close!!!\r\n");
|
||||
}
|
||||
#endif /* Trace end*/
|
||||
#else
|
||||
void trace_start(void)
|
||||
{
|
||||
printf("trace should have cli to control!!!\r\n");
|
||||
}
|
||||
#endif /*Cli end*/
|
||||
|
||||
|
||||
|
||||
408
Living_SDK/platform/mcu/linux/soc/uart.c
Normal file
408
Living_SDK/platform/mcu/linux/soc/uart.c
Normal file
|
|
@ -0,0 +1,408 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <execinfo.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/select.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <semaphore.h>
|
||||
#include <cpu_event.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include "hal/soc/soc.h"
|
||||
|
||||
#ifdef VCALL_RHINO
|
||||
#include <k_api.h>
|
||||
|
||||
typedef unsigned char
|
||||
UINT8; /* Unsigned 8 bit quantity */
|
||||
typedef signed char
|
||||
INT8; /* Signed 8 bit quantity */
|
||||
typedef unsigned short
|
||||
UINT16; /* Unsigned 16 bit quantity */
|
||||
typedef signed short
|
||||
INT16; /* Signed 16 bit quantity */
|
||||
typedef uint32_t
|
||||
UINT32; /* Unsigned 32 bit quantity */
|
||||
typedef int32_t
|
||||
INT32; /* Signed 32 bit quantity */
|
||||
|
||||
static struct termios term_orig;
|
||||
static sigset_t sigset;
|
||||
|
||||
#define MAX_UART_NUM 1
|
||||
#define CLI_BUFQUE_NUM 2
|
||||
|
||||
enum _uart_status_e {
|
||||
_UART_STATUS_CLOSED = 0,
|
||||
_UART_STATUS_OPENED,
|
||||
};
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t uart;
|
||||
uint8_t status;
|
||||
kbuf_queue_t *bufque;
|
||||
kmutex_t tx_mutex;
|
||||
pthread_t threaid;
|
||||
} _uart_drv_t;
|
||||
|
||||
static _uart_drv_t _uart_drv[MAX_UART_NUM];
|
||||
|
||||
extern int32_t uart_read_byte( uint8_t *rx_byte );
|
||||
extern void uart_write_byte( uint8_t byte );
|
||||
extern uint8_t uart_is_tx_fifo_empty( void );
|
||||
extern uint8_t uart_is_tx_fifo_full( void );
|
||||
extern void uart_set_tx_stop_end_int( uint8_t set );
|
||||
|
||||
static void exit_cleanup(void)
|
||||
{
|
||||
tcsetattr(0, TCSANOW, &term_orig);
|
||||
}
|
||||
|
||||
static void cli_input_handler(char *nbuf)
|
||||
{
|
||||
_uart_drv_t *pdrv = &_uart_drv[0];
|
||||
|
||||
krhino_buf_queue_send(pdrv->bufque, nbuf, 1);
|
||||
cpu_event_free(nbuf);
|
||||
return;
|
||||
}
|
||||
|
||||
static void * clirev_thread(void * arg)
|
||||
{
|
||||
fd_set fdset;
|
||||
int ret;
|
||||
|
||||
|
||||
sigaddset(&sigset, SIGALRM);
|
||||
ret = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
|
||||
|
||||
if(ret != 0) {
|
||||
perror("sigmask failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(0, &fdset);
|
||||
|
||||
/* Wait for a packet to arrive. */
|
||||
ret = select(1, &fdset, NULL, NULL, NULL);
|
||||
if(ret == 1) {
|
||||
char *nbuf = cpu_event_malloc(8);
|
||||
|
||||
/* Obtain the size of the packet and put it into the "len"
|
||||
variable. */
|
||||
int readlen = read(0, nbuf, 1);
|
||||
if (readlen < 0) {
|
||||
perror("cli read eror");
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Handle incoming packet. */
|
||||
cpu_call_handler((cpu_event_handler)cli_input_handler, nbuf);
|
||||
} else if(ret == -1) {
|
||||
perror("cli_thread: select");
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int32_t hal_uart_init(uart_dev_t *uart)
|
||||
{
|
||||
_uart_drv_t *pdrv = &_uart_drv[0];
|
||||
struct termios term_vi;
|
||||
kstat_t stat;
|
||||
|
||||
if (pdrv->status == _UART_STATUS_CLOSED) {
|
||||
krhino_mutex_create( &pdrv->tx_mutex, "TX_MUTEX" );
|
||||
pdrv->status = _UART_STATUS_OPENED;
|
||||
tcgetattr(0, &term_orig);
|
||||
term_vi = term_orig;
|
||||
term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG ON- allow intr's
|
||||
term_vi.c_iflag &= (~IXON & ~ICRNL);
|
||||
tcsetattr(0, TCSANOW, &term_vi);
|
||||
atexit(exit_cleanup);
|
||||
stat = krhino_buf_queue_dyn_create(&pdrv->bufque, "cli", 256 , CLI_BUFQUE_NUM);
|
||||
if(stat != RHINO_SUCCESS) {
|
||||
return stat;
|
||||
}
|
||||
|
||||
/* it has to be pthread, as it needs to access linux functions */
|
||||
pthread_create(&pdrv->threaid, NULL, clirev_thread, NULL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_uart_finalize(uart_dev_t *uart)
|
||||
{
|
||||
_uart_drv_t *pdrv = &_uart_drv[uart->port];
|
||||
|
||||
krhino_mutex_del(&pdrv->tx_mutex);
|
||||
pdrv->status = _UART_STATUS_CLOSED;
|
||||
tcsetattr(0, TCSANOW, &term_orig);
|
||||
krhino_buf_queue_dyn_del(pdrv->bufque);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_uart_send(uart_dev_t *uart, const void *data, uint32_t size, uint32_t timeout)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
_uart_drv_t *pdrv = &_uart_drv[uart->port];
|
||||
|
||||
krhino_mutex_lock(&pdrv->tx_mutex, RHINO_WAIT_FOREVER);
|
||||
|
||||
for ( i = 0; i < size; i++ ) {
|
||||
putchar( ((uint8_t *)data)[i] );
|
||||
}
|
||||
|
||||
krhino_mutex_unlock( &pdrv->tx_mutex );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_uart_recv_II(uart_dev_t *uart, void *data, uint32_t expect_size, uint32_t *recv_size, uint32_t timeout)
|
||||
{
|
||||
uint32_t readlen = 0;
|
||||
uint32_t totallen = 0;
|
||||
kstat_t retval;
|
||||
_uart_drv_t *pdrv = &_uart_drv[uart->port];
|
||||
|
||||
while(1) {
|
||||
retval = krhino_buf_queue_recv(pdrv->bufque, RHINO_WAIT_FOREVER, data, &readlen);
|
||||
if(retval != RHINO_SUCCESS) {
|
||||
if (recv_size) {
|
||||
*recv_size = totallen;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
totallen += readlen;
|
||||
if(totallen >= expect_size) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (recv_size) {
|
||||
*recv_size = totallen;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
#ifdef AOS_ATCMD
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <hal/atcmd.h>
|
||||
|
||||
static int at_uart_fd = -1;
|
||||
|
||||
static int read_and_discard_all_data(const int fd)
|
||||
{
|
||||
int was_msg_already_printed = 0;
|
||||
|
||||
while (1) {
|
||||
char buffer[1024];
|
||||
const ssize_t read_count = read(fd, buffer, sizeof(buffer));
|
||||
|
||||
if (read_count == 0) {
|
||||
/* "EOF" or "connection closed at the other end"*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (read_count > 0) {
|
||||
if (!was_msg_already_printed) {
|
||||
printf("Some stale data was discarded.\r\n");
|
||||
was_msg_already_printed = 1;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
assert(read_count == -1); /* According to the specification. */
|
||||
|
||||
const int errno_code = errno;
|
||||
|
||||
if (errno_code == EINTR)
|
||||
continue;
|
||||
|
||||
if (errno_code == EAGAIN ||
|
||||
errno_code == EWOULDBLOCK) {
|
||||
/**
|
||||
* We know that the file descriptor has been opened with
|
||||
* O_NONBLOCK or O_NDELAY, and these codes mean that there
|
||||
* is no data to read at present.
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Some other error has occurred. */
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t hal_uart_init(uart_dev_t *uart)
|
||||
{
|
||||
char out[128];
|
||||
int fd;
|
||||
struct termios t_opt;
|
||||
speed_t baud;
|
||||
|
||||
if (uart->port != AT_UART_PORT) return 0;
|
||||
|
||||
if ((at_uart_fd = open(AT_UART_LINUX_DEV,
|
||||
O_RDWR | O_NOCTTY | O_NDELAY)) == -1) {
|
||||
printf("open at uart failed\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch(uart->config.baud_rate) {
|
||||
case 115200: baud = B115200; break;
|
||||
case 921600: baud = B921600; break;
|
||||
default: baud = B115200; break;
|
||||
}
|
||||
|
||||
fd = at_uart_fd;
|
||||
/* set the serial port parameters */
|
||||
fcntl(fd, F_SETFL, 0);
|
||||
if (0 != tcgetattr(fd, &t_opt))
|
||||
return -1;
|
||||
|
||||
if (0 != cfsetispeed(&t_opt, baud))
|
||||
return -1;
|
||||
|
||||
if (0 != cfsetospeed(&t_opt, baud))
|
||||
return -1;
|
||||
|
||||
// 8N1, flow control, etc.
|
||||
t_opt.c_cflag |= (CLOCAL | CREAD);
|
||||
if (uart->config.parity == NO_PARITY)
|
||||
t_opt.c_cflag &= ~PARENB;
|
||||
if (uart->config.stop_bits == STOP_BITS_1)
|
||||
t_opt.c_cflag &= ~CSTOPB;
|
||||
else
|
||||
t_opt.c_cflag |= CSTOPB;
|
||||
t_opt.c_cflag &= ~CSIZE;
|
||||
switch (uart->config.data_width) {
|
||||
case DATA_WIDTH_5BIT:
|
||||
t_opt.c_cflag |= CS5;
|
||||
break;
|
||||
case DATA_WIDTH_6BIT:
|
||||
t_opt.c_cflag |= CS6;
|
||||
break;
|
||||
case DATA_WIDTH_7BIT:
|
||||
t_opt.c_cflag |= CS7;
|
||||
break;
|
||||
case DATA_WIDTH_8BIT:
|
||||
t_opt.c_cflag |= CS8;
|
||||
break;
|
||||
default:
|
||||
t_opt.c_cflag |= CS8;
|
||||
break;
|
||||
}
|
||||
t_opt.c_lflag &= ~(ECHO | ECHOE | ISIG | ICANON);
|
||||
if (uart->config.flow_control == FLOW_CONTROL_DISABLED)
|
||||
t_opt.c_cflag &= ~CRTSCTS;
|
||||
|
||||
/**
|
||||
* AT is going to use a binary protocol, so make sure to
|
||||
* turn off any CR/LF translation and the like.
|
||||
*/
|
||||
t_opt.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL);
|
||||
|
||||
t_opt.c_oflag &= ~OPOST;
|
||||
t_opt.c_cc[VMIN] = 0;
|
||||
t_opt.c_cc[VTIME] = 5;
|
||||
|
||||
if (0 != tcsetattr(fd, TCSANOW, &t_opt)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("open at uart succeed\r\n");
|
||||
|
||||
// clear uart buffer
|
||||
read_and_discard_all_data(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_uart_finalize(uart_dev_t *uart)
|
||||
{
|
||||
if (uart->port == AT_UART_PORT) close(at_uart_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_uart_send(uart_dev_t *uart, const void *data,
|
||||
uint32_t size, uint32_t timeout)
|
||||
{
|
||||
uint32_t ret, rmd = size;
|
||||
|
||||
if (uart->port == AT_UART_PORT) {
|
||||
while (rmd > 0) {
|
||||
ret = write(at_uart_fd, data + size - rmd, rmd);
|
||||
if (ret == -1) {
|
||||
printf("write uart fd failed on error: %d.\r\n", errno);
|
||||
return -1;
|
||||
}
|
||||
rmd -= ret;
|
||||
}
|
||||
}
|
||||
else write(1, data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_uart_recv_II(uart_dev_t *uart, void *data, uint32_t expect_size,
|
||||
uint32_t *recv_size, uint32_t timeout)
|
||||
{
|
||||
int fd, n;
|
||||
|
||||
if (uart->port == AT_UART_PORT) fd = at_uart_fd;
|
||||
else fd = 1;
|
||||
|
||||
if ((n = read(fd, data, expect_size)) == -1) {
|
||||
//printf("read failed\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (uart->port != AT_UART_PORT && *(char *)data == '\n')
|
||||
*(char *)data = '\r';
|
||||
if (recv_size)
|
||||
*recv_size = n;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else // #ifdef AOS_ATCMD
|
||||
int32_t hal_uart_init(uart_dev_t *uart)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_uart_finalize(uart_dev_t *uart)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t hal_uart_send(uart_dev_t *uart, const void *data, uint32_t size, uint32_t timeout)
|
||||
{
|
||||
write(1, data, size);
|
||||
}
|
||||
|
||||
int32_t hal_uart_recv_II(uart_dev_t *uart, void *data, uint32_t expect_size, uint32_t *recv_size, uint32_t timeout)
|
||||
{
|
||||
int n = read(1, data, expect_size);
|
||||
if (*(char *)data == '\n')
|
||||
*(char *)data = '\r';
|
||||
if (recv_size)
|
||||
*recv_size = n;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
3
Living_SDK/platform/mcu/linux/tools/gdbinit
Normal file
3
Living_SDK/platform/mcu/linux/tools/gdbinit
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
handle SIG34 noprint nostop
|
||||
handle SIGUSR1 noprint nostop
|
||||
handle SIGUSR2 noprint nostop
|
||||
2
Living_SDK/platform/mcu/linux/tools/tapdown.sh
Executable file
2
Living_SDK/platform/mcu/linux/tools/tapdown.sh
Executable file
|
|
@ -0,0 +1,2 @@
|
|||
#!/bin/sh
|
||||
openvpn --rmtun --dev $1
|
||||
3
Living_SDK/platform/mcu/linux/tools/tapup.sh
Executable file
3
Living_SDK/platform/mcu/linux/tools/tapup.sh
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
sudo openvpn --mktun --dev $1 --user `id -un`
|
||||
sudo ifconfig $1 192.168.0.1 netmask 255.255.255.0
|
||||
3
Living_SDK/platform/mcu/linux/tools/tunup.sh
Executable file
3
Living_SDK/platform/mcu/linux/tools/tunup.sh
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
sudo openvpn --mktun --dev $1 --user `id -un`
|
||||
sudo ifconfig $1 10.0.0.1 netmask 255.255.0.0
|
||||
107
Living_SDK/platform/mcu/linux/ucube.py
Executable file
107
Living_SDK/platform/mcu/linux/ucube.py
Executable file
|
|
@ -0,0 +1,107 @@
|
|||
aos_global_config.set('no_with_lwip', 1)
|
||||
|
||||
src = Split('''
|
||||
soc/uart.c
|
||||
main/arg_options.c
|
||||
main/main.c
|
||||
main/hw.c
|
||||
main/wifi_port.c
|
||||
main/nand.c
|
||||
main/vfs_trap.c
|
||||
''')
|
||||
|
||||
global_cflags = Split('''
|
||||
-m32
|
||||
-std=gnu99
|
||||
-Wall
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-strict-aliasing -Wno-address
|
||||
-Wno-unused-result
|
||||
-lpthread
|
||||
-lm
|
||||
-lrt
|
||||
-DDEBUG
|
||||
-ggdb
|
||||
''')
|
||||
|
||||
global_macros = Split('''
|
||||
SYSINFO_PRODUCT_MODEL=\\"ALI_AOS_LINUXHOST\\"
|
||||
SYSINFO_DEVICE_NAME=\\"LINUXHOST\\"
|
||||
CONFIG_AOS_RHINO_MMREGION
|
||||
CONFIG_YSH_CMD_DUMPSYS
|
||||
CSP_LINUXHOST
|
||||
CONFIG_LOGMACRO_DETAILS
|
||||
CONFIG_AOS_FATFS_SUPPORT
|
||||
CONFIG_AOS_FATFS_SUPPORT_MMC
|
||||
CONFIG_AOS_FOTA_BREAKPOINT
|
||||
''')
|
||||
|
||||
component = aos_mcu_component('linuximpl', '', src)
|
||||
|
||||
component.set_global_arch('linux')
|
||||
|
||||
component.add_global_cflags(*global_cflags)
|
||||
component.add_global_asflags('-m32')
|
||||
component.add_global_ldflags('-m32', '-lpthread', '-lm', '-lrt', '-lreadline', '-lncurses')
|
||||
component.add_global_macros(*global_macros)
|
||||
|
||||
@post_config
|
||||
def linuximpl_post_config(component):
|
||||
comp_names = [comp.name for comp in aos_global_config.components]
|
||||
if 'fatfs' in comp_names:
|
||||
component.add_sources('main/sdmmc.c')
|
||||
|
||||
if 'net' in comp_names:
|
||||
aos_global_config.set('LWIP', 1)
|
||||
|
||||
|
||||
linuximpl_post_config(component)
|
||||
|
||||
LWIP = aos_global_config.get('LWIP')
|
||||
if LWIP == 1:
|
||||
lwip_src = Split('''
|
||||
csp/lwip/netif/delif.c
|
||||
csp/lwip/netif/fifo.c
|
||||
csp/lwip/netif/list.c
|
||||
csp/lwip/netif/tapif.c
|
||||
csp/lwip/netif/tcpdump.c
|
||||
csp/lwip/netif/tunif.c
|
||||
csp/lwip/lwip_linuxhost.c
|
||||
''')
|
||||
for s in lwip_src:
|
||||
component.add_sources(s)
|
||||
### can't work end ###
|
||||
|
||||
if aos_global_config.app == 'yts':
|
||||
src_tmp = Split('''
|
||||
main/sdmmc.c
|
||||
csp/lwip/netif/delif.c
|
||||
csp/lwip/netif/fifo.c
|
||||
csp/lwip/netif/list.c
|
||||
csp/lwip/netif/tapif.c
|
||||
csp/lwip/netif/tcpdump.c
|
||||
csp/lwip/netif/tunif.c
|
||||
csp/lwip/lwip_linuxhost.c
|
||||
''')
|
||||
for s in src_tmp:
|
||||
component.add_sources(s)
|
||||
|
||||
if aos_global_config.get('vcall') == 'posix':
|
||||
component.add_macros("CONFIG_VCALL_POSIX")
|
||||
else:
|
||||
src_tmp = Split('''
|
||||
soc/soc_impl.c
|
||||
soc/hook_impl.c
|
||||
soc/trace_impl.c
|
||||
''')
|
||||
for s in src_tmp:
|
||||
component.add_sources(s)
|
||||
|
||||
component.add_comp_deps('utility/log', 'platform/arch/linux', 'kernel/vcall', 'kernel/init')
|
||||
|
||||
component.add_global_includes('include', 'csp/lwip/include')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue