mirror of
https://github.com/ep00ch/lwip-contrib-mac.git
synced 2024-08-28 18:29:08 +00:00
Added sys_arch.c to be able to use sys layer and sequential APIs under Win32
This commit is contained in:
parent
79ef1fb324
commit
537c17c668
@ -32,19 +32,18 @@
|
|||||||
#ifndef __ARCH_SYS_ARCH_H__
|
#ifndef __ARCH_SYS_ARCH_H__
|
||||||
#define __ARCH_SYS_ARCH_H__
|
#define __ARCH_SYS_ARCH_H__
|
||||||
|
|
||||||
#define SYS_MBOX_NULL NULL
|
/* HANDLE is used for sys_sem_t but we won't include windows.h */
|
||||||
|
typedef void* sys_sem_t;
|
||||||
#define SYS_SEM_NULL NULL
|
#define SYS_SEM_NULL NULL
|
||||||
|
|
||||||
struct sys_sem;
|
struct lwip_mbox;
|
||||||
typedef struct sys_sem * sys_sem_t;
|
typedef struct lwip_mbox* sys_mbox_t;
|
||||||
#define SYS_SEM_NULL NULL
|
|
||||||
|
|
||||||
struct sys_mbox;
|
|
||||||
typedef struct sys_mbox *sys_mbox_t;
|
|
||||||
#define SYS_MBOX_NULL NULL
|
#define SYS_MBOX_NULL NULL
|
||||||
|
|
||||||
struct sys_thread;
|
/* DWORD (thread id) is used for sys_thread_t but we won't include windows.h */
|
||||||
typedef struct sys_thread * sys_thread_t;
|
typedef u32_t sys_thread_t;
|
||||||
|
|
||||||
|
typedef u32_t sys_prot_t;
|
||||||
|
|
||||||
#endif /* __ARCH_SYS_ARCH_H__ */
|
#endif /* __ARCH_SYS_ARCH_H__ */
|
||||||
|
|
||||||
|
@ -31,6 +31,9 @@
|
|||||||
*/
|
*/
|
||||||
#ifndef __LWIPOPTS_H__
|
#ifndef __LWIPOPTS_H__
|
||||||
#define __LWIPOPTS_H__
|
#define __LWIPOPTS_H__
|
||||||
|
|
||||||
|
#define LWIP_HAVE_LOOPIF 1
|
||||||
|
|
||||||
#define LWIP_DBG_MIN_LEVEL 0
|
#define LWIP_DBG_MIN_LEVEL 0
|
||||||
#define LWIP_COMPAT_SOCKETS 1
|
#define LWIP_COMPAT_SOCKETS 1
|
||||||
#define TAPIF_DEBUG LWIP_DBG_ON
|
#define TAPIF_DEBUG LWIP_DBG_ON
|
||||||
@ -69,7 +72,8 @@
|
|||||||
|
|
||||||
#define LWIP_DBG_TYPES_ON (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT)
|
#define LWIP_DBG_TYPES_ON (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT)
|
||||||
|
|
||||||
#define NO_SYS 1
|
#define NO_SYS 0
|
||||||
|
#define LWIP_PROVIDE_ERRNO
|
||||||
|
|
||||||
/* ---------- Memory options ---------- */
|
/* ---------- Memory options ---------- */
|
||||||
/* MEM_ALIGNMENT: should be set to the alignment of the CPU for which
|
/* MEM_ALIGNMENT: should be set to the alignment of the CPU for which
|
||||||
@ -133,7 +137,7 @@ a lot of data that needs to be copied, this should be set high. */
|
|||||||
* for certain critical regions during buffer allocation, deallocation and memory
|
* for certain critical regions during buffer allocation, deallocation and memory
|
||||||
* allocation and deallocation.
|
* allocation and deallocation.
|
||||||
*/
|
*/
|
||||||
/*#define SYS_LIGHTWEIGHT_PROT 1*/
|
#define SYS_LIGHTWEIGHT_PROT 1
|
||||||
|
|
||||||
/* ---------- TCP options ---------- */
|
/* ---------- TCP options ---------- */
|
||||||
#define LWIP_TCP 1
|
#define LWIP_TCP 1
|
||||||
|
@ -84,12 +84,7 @@
|
|||||||
#define IFNAME0 'p'
|
#define IFNAME0 'p'
|
||||||
#define IFNAME1 'k'
|
#define IFNAME1 'k'
|
||||||
|
|
||||||
struct ethernetif {
|
static struct eth_addr broadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
||||||
struct eth_addr *ethaddr;
|
|
||||||
/* Add whatever per-interface state that is needed here. */
|
|
||||||
};
|
|
||||||
|
|
||||||
static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
|
|
||||||
|
|
||||||
/* Forward declarations. */
|
/* Forward declarations. */
|
||||||
static void ethernetif_input(struct netif *netif);
|
static void ethernetif_input(struct netif *netif);
|
||||||
@ -107,14 +102,8 @@ extern int packet_send(void *buffer, int len);
|
|||||||
static void
|
static void
|
||||||
low_level_init(struct netif *netif)
|
low_level_init(struct netif *netif)
|
||||||
{
|
{
|
||||||
struct ethernetif *ethernetif;
|
|
||||||
|
|
||||||
ethernetif = netif->state;
|
|
||||||
|
|
||||||
memcpy(ðaddr,ethernetif->ethaddr,6);
|
|
||||||
|
|
||||||
#ifdef NETIF_DEBUG
|
#ifdef NETIF_DEBUG
|
||||||
LWIP_DEBUGF(NETIF_DEBUG, ("pktif: eth_addr %02X%02X%02X%02X%02X%02X\n",ethernetif->ethaddr->addr[0],ethernetif->ethaddr->addr[1],ethernetif->ethaddr->addr[2],ethernetif->ethaddr->addr[3],ethernetif->ethaddr->addr[4],ethernetif->ethaddr->addr[5]));
|
LWIP_DEBUGF(NETIF_DEBUG, ("pktif: eth_addr %02X%02X%02X%02X%02X%02X\n",netif->hwaddr[0],netif->hwaddr[1],netif->hwaddr[2],netif->hwaddr[3],netif->hwaddr[4],netif->hwaddr[5]));
|
||||||
#endif /* NETIF_DEBUG */
|
#endif /* NETIF_DEBUG */
|
||||||
/* Do whatever else is needed to initialize interface. */
|
/* Do whatever else is needed to initialize interface. */
|
||||||
|
|
||||||
@ -174,10 +163,11 @@ low_level_output(struct netif *ethernetif, struct pbuf *p)
|
|||||||
*/
|
*/
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
static struct pbuf *
|
static struct pbuf *
|
||||||
low_level_input(struct netif *ethernetif)
|
low_level_input(struct netif *netif)
|
||||||
{
|
{
|
||||||
struct pbuf *p, *q;
|
struct pbuf *p, *q;
|
||||||
int start, length;
|
int start, length;
|
||||||
|
struct eth_hdr *ethhdr;
|
||||||
|
|
||||||
/* Obtain the size of the packet and put it into the "len"
|
/* Obtain the size of the packet and put it into the "len"
|
||||||
variable. */
|
variable. */
|
||||||
@ -185,6 +175,14 @@ low_level_input(struct netif *ethernetif)
|
|||||||
if (length<=0)
|
if (length<=0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
ethhdr = (struct eth_hdr*)cur_packet;
|
||||||
|
/* MAC filter: only let my MAC or broadcast through */
|
||||||
|
if((memcmp(ðhdr->dest, &netif->hwaddr, 6)) && (memcmp(ðhdr->dest, &broadcastaddr, 6))) {
|
||||||
|
/* acknowledge that packet has been read(); */
|
||||||
|
cur_length=0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* We allocate a pbuf chain of pbufs from the pool. */
|
/* We allocate a pbuf chain of pbufs from the pool. */
|
||||||
p = pbuf_alloc(PBUF_LINK, (u16_t)length, PBUF_POOL);
|
p = pbuf_alloc(PBUF_LINK, (u16_t)length, PBUF_POOL);
|
||||||
#ifdef NETIF_DEBUG
|
#ifdef NETIF_DEBUG
|
||||||
@ -266,13 +264,20 @@ ethernetif_input(struct netif *netif)
|
|||||||
|
|
||||||
if (p != NULL) {
|
if (p != NULL) {
|
||||||
|
|
||||||
#ifdef LINK_STATS
|
#if LINK_STATS
|
||||||
lwip_stats.link.recv++;
|
lwip_stats.link.recv++;
|
||||||
#endif /* LINK_STATS */
|
#endif /* LINK_STATS */
|
||||||
|
|
||||||
ethhdr = p->payload;
|
ethhdr = p->payload;
|
||||||
|
|
||||||
switch (htons(ethhdr->type)) {
|
switch (htons(ethhdr->type)) {
|
||||||
|
#if ETHARP_TCPIP_INPUT
|
||||||
|
/* IP or ARP packet? */
|
||||||
|
case ETHTYPE_IP:
|
||||||
|
case ETHTYPE_ARP:
|
||||||
|
netif->input(p, netif);
|
||||||
|
break;
|
||||||
|
#else
|
||||||
case ETHTYPE_IP:
|
case ETHTYPE_IP:
|
||||||
etharp_ip_input(netif, p);
|
etharp_ip_input(netif, p);
|
||||||
pbuf_header(p, -14);
|
pbuf_header(p, -14);
|
||||||
@ -281,6 +286,7 @@ ethernetif_input(struct netif *netif)
|
|||||||
case ETHTYPE_ARP:
|
case ETHTYPE_ARP:
|
||||||
etharp_arp_input(netif, ethernetif->ethaddr, p);
|
etharp_arp_input(netif, ethernetif->ethaddr, p);
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
default:
|
default:
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
break;
|
break;
|
||||||
@ -288,13 +294,6 @@ ethernetif_input(struct netif *netif)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
static void
|
|
||||||
arp_timer(void *arg)
|
|
||||||
{
|
|
||||||
etharp_tmr();
|
|
||||||
sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);
|
|
||||||
}
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
|
||||||
/*
|
/*
|
||||||
* ethernetif_init():
|
* ethernetif_init():
|
||||||
*
|
*
|
||||||
@ -307,10 +306,6 @@ arp_timer(void *arg)
|
|||||||
err_t
|
err_t
|
||||||
ethernetif_init(struct netif *netif)
|
ethernetif_init(struct netif *netif)
|
||||||
{
|
{
|
||||||
struct ethernetif *ethernetif;
|
|
||||||
|
|
||||||
ethernetif = mem_malloc(sizeof(struct ethernetif));
|
|
||||||
netif->state = ethernetif;
|
|
||||||
netif->name[0] = IFNAME0;
|
netif->name[0] = IFNAME0;
|
||||||
netif->name[1] = IFNAME1;
|
netif->name[1] = IFNAME1;
|
||||||
netif->linkoutput = low_level_output;
|
netif->linkoutput = low_level_output;
|
||||||
@ -319,12 +314,8 @@ ethernetif_init(struct netif *netif)
|
|||||||
netif->mtu = 1500;
|
netif->mtu = 1500;
|
||||||
netif->flags = NETIF_FLAG_BROADCAST;
|
netif->flags = NETIF_FLAG_BROADCAST;
|
||||||
netif->hwaddr_len = 6;
|
netif->hwaddr_len = 6;
|
||||||
ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
|
|
||||||
|
|
||||||
low_level_init(netif);
|
low_level_init(netif);
|
||||||
etharp_init();
|
|
||||||
|
|
||||||
sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);
|
|
||||||
|
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
398
ports/msvc6/sys_arch.c
Normal file
398
ports/msvc6/sys_arch.c
Normal file
@ -0,0 +1,398 @@
|
|||||||
|
/*
|
||||||
|
* 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>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
*********************************************************************************************************
|
||||||
|
* UCOS-II Port
|
||||||
|
*
|
||||||
|
* Target : Any processor
|
||||||
|
* Put together by : Michael Anburaj
|
||||||
|
* URL : http://geocities.com/michaelanburaj/ Email : michaelanburaj@hotmail.com
|
||||||
|
*
|
||||||
|
*********************************************************************************************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h> // sprintf() for task names
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <arch/cc.h>
|
||||||
|
|
||||||
|
#include <lwip/stats.h>
|
||||||
|
#include <lwip/arch.h>
|
||||||
|
#include <lwip/debug.h>
|
||||||
|
#include <lwip/sys.h>
|
||||||
|
|
||||||
|
#if !NO_SYS
|
||||||
|
|
||||||
|
#define MAX_QUEUE_ENTRIES 100
|
||||||
|
|
||||||
|
struct threadlist {
|
||||||
|
struct sys_timeouts timeouts;
|
||||||
|
DWORD id;
|
||||||
|
struct threadlist *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct lwip_mbox {
|
||||||
|
HANDLE sem;
|
||||||
|
void* q_mem[MAX_QUEUE_ENTRIES];
|
||||||
|
u32_t head, tail;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct threadlist *lwip_win32_threads = NULL;
|
||||||
|
LARGE_INTEGER freq, sys_start_time;
|
||||||
|
CRITICAL_SECTION critSec;
|
||||||
|
|
||||||
|
void InitSysArchProtect()
|
||||||
|
{
|
||||||
|
InitializeCriticalSection(&critSec);
|
||||||
|
}
|
||||||
|
u32_t sys_arch_protect()
|
||||||
|
{
|
||||||
|
EnterCriticalSection(&critSec);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
void sys_arch_unprotect(u32_t pval)
|
||||||
|
{
|
||||||
|
LeaveCriticalSection(&critSec);
|
||||||
|
}
|
||||||
|
void do_sleep(int ms)
|
||||||
|
{
|
||||||
|
Sleep(ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sys_init(void)
|
||||||
|
{
|
||||||
|
QueryPerformanceFrequency(&freq);
|
||||||
|
QueryPerformanceCounter(&sys_start_time);
|
||||||
|
InitSysArchProtect();
|
||||||
|
}
|
||||||
|
|
||||||
|
static LONGLONG sys_get_ms()
|
||||||
|
{
|
||||||
|
LONGLONG ret;
|
||||||
|
LARGE_INTEGER now;
|
||||||
|
QueryPerformanceCounter(&now);
|
||||||
|
ret = now.QuadPart-sys_start_time.QuadPart;
|
||||||
|
return (u32_t)(((ret)*1000)/freq.QuadPart);
|
||||||
|
}
|
||||||
|
|
||||||
|
sys_sem_t sys_sem_new(u8_t count)
|
||||||
|
{
|
||||||
|
HANDLE new_sem = NULL;
|
||||||
|
new_sem = CreateSemaphore(0, count, 100000, 0);
|
||||||
|
LWIP_ASSERT("Error creating semaphore", new_sem != NULL);
|
||||||
|
if(new_sem != NULL) {
|
||||||
|
#if LWIP_STATS
|
||||||
|
lwip_stats.sys.sem.used++;
|
||||||
|
LWIP_ASSERT("sys_sem_new() counter overflow", lwip_stats.sys.sem.used != 0 );
|
||||||
|
if (lwip_stats.sys.sem.used > lwip_stats.sys.sem.max) {
|
||||||
|
lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
|
||||||
|
}
|
||||||
|
#endif // LWIP_STATS
|
||||||
|
return new_sem;
|
||||||
|
}
|
||||||
|
|
||||||
|
// failed to allocate memory...
|
||||||
|
#if LWIP_STATS
|
||||||
|
lwip_stats.sys.sem.err++;
|
||||||
|
#endif // LWIP_STATS
|
||||||
|
return SYS_SEM_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sys_sem_free(sys_sem_t sem)
|
||||||
|
{
|
||||||
|
// parameter check
|
||||||
|
LWIP_ASSERT("sem != NULL", sem != NULL);
|
||||||
|
LWIP_ASSERT("sem != INVALID_HANDLE_VALUE", sem != INVALID_HANDLE_VALUE);
|
||||||
|
CloseHandle(sem);
|
||||||
|
|
||||||
|
#if LWIP_STATS
|
||||||
|
lwip_stats.sys.sem.used--;
|
||||||
|
LWIP_ASSERT("sys_sem_free() closed more than created", lwip_stats.sys.sem.used != (u16_t)-1);
|
||||||
|
#endif // LWIP_STATS
|
||||||
|
}
|
||||||
|
|
||||||
|
u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)
|
||||||
|
{
|
||||||
|
DWORD ret;
|
||||||
|
LONGLONG starttime, endtime;
|
||||||
|
LWIP_ASSERT("sem != NULL", sem != NULL);
|
||||||
|
LWIP_ASSERT("sem != INVALID_HANDLE_VALUE", sem != INVALID_HANDLE_VALUE);
|
||||||
|
if(!timeout)
|
||||||
|
{
|
||||||
|
// wait infinite
|
||||||
|
starttime = sys_get_ms();
|
||||||
|
ret = WaitForSingleObject(sem, INFINITE);
|
||||||
|
LWIP_ASSERT("Error waiting for mutex", ret == WAIT_OBJECT_0);
|
||||||
|
endtime = sys_get_ms();
|
||||||
|
// return the time we waited for the sem
|
||||||
|
return (u32_t)(endtime - starttime);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
starttime = sys_get_ms();
|
||||||
|
ret = WaitForSingleObject(sem, timeout);
|
||||||
|
LWIP_ASSERT("Error waiting for mutex", (ret == WAIT_OBJECT_0) || (ret == WAIT_TIMEOUT));
|
||||||
|
if(ret == WAIT_OBJECT_0)
|
||||||
|
{
|
||||||
|
endtime = sys_get_ms();
|
||||||
|
// return the time we waited for the sem
|
||||||
|
return (u32_t)(endtime - starttime);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// timeout
|
||||||
|
return SYS_ARCH_TIMEOUT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sys_sem_signal(sys_sem_t sem)
|
||||||
|
{
|
||||||
|
DWORD ret;
|
||||||
|
LWIP_ASSERT("sem != NULL", sem != NULL);
|
||||||
|
LWIP_ASSERT("sem != INVALID_HANDLE_VALUE", sem != INVALID_HANDLE_VALUE);
|
||||||
|
ret = ReleaseSemaphore(sem, 1, NULL);
|
||||||
|
LWIP_ASSERT("Error releasing mutex", ret != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sys_timeouts *sys_arch_timeouts(void)
|
||||||
|
{
|
||||||
|
struct sys_timeouts *ret = NULL;
|
||||||
|
struct threadlist *t, *new_thread;
|
||||||
|
SYS_ARCH_DECL_PROTECT(lev);
|
||||||
|
DWORD threadID;
|
||||||
|
|
||||||
|
threadID = GetCurrentThreadId();
|
||||||
|
SYS_ARCH_PROTECT(lev);
|
||||||
|
for(t = lwip_win32_threads; t != NULL; t = t->next)
|
||||||
|
{
|
||||||
|
if(t->id == threadID)
|
||||||
|
{
|
||||||
|
ret = &(t->timeouts);
|
||||||
|
SYS_ARCH_UNPROTECT(lev);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new_thread = (struct threadlist*)malloc(sizeof(struct threadlist));
|
||||||
|
LWIP_ASSERT("new_thread != NULL", new_thread != NULL);
|
||||||
|
if(new_thread != NULL) {
|
||||||
|
OutputDebugString("First call to sys_arch_timoeuts for thread");
|
||||||
|
new_thread->next = lwip_win32_threads;
|
||||||
|
lwip_win32_threads = new_thread;
|
||||||
|
new_thread->id = threadID;
|
||||||
|
new_thread->timeouts.next = NULL;
|
||||||
|
ret = &(new_thread->timeouts);
|
||||||
|
SYS_ARCH_UNPROTECT(lev);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
SYS_ARCH_UNPROTECT(lev);
|
||||||
|
LWIP_ASSERT("should not come here", 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
sys_thread_t sys_thread_new(void (* function)(void *arg), void *arg, int prio)
|
||||||
|
{
|
||||||
|
struct threadlist *new_thread;
|
||||||
|
HANDLE h;
|
||||||
|
SYS_ARCH_DECL_PROTECT(lev);
|
||||||
|
|
||||||
|
new_thread = (struct threadlist*)malloc(sizeof(struct threadlist));
|
||||||
|
LWIP_ASSERT("new_thread != NULL", new_thread != NULL);
|
||||||
|
if(new_thread != NULL) {
|
||||||
|
SYS_ARCH_PROTECT(lev);
|
||||||
|
new_thread->next = lwip_win32_threads;
|
||||||
|
lwip_win32_threads = new_thread;
|
||||||
|
new_thread->timeouts.next = NULL;
|
||||||
|
|
||||||
|
h = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)function, arg, 0, &(new_thread->id));
|
||||||
|
LWIP_ASSERT("h != 0", h != 0);
|
||||||
|
LWIP_ASSERT("h != -1", h != INVALID_HANDLE_VALUE);
|
||||||
|
|
||||||
|
SYS_ARCH_UNPROTECT(lev);
|
||||||
|
return new_thread->id;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
sys_mbox_t sys_mbox_new()
|
||||||
|
{
|
||||||
|
struct lwip_mbox *new_mbox;
|
||||||
|
new_mbox = (struct lwip_mbox*)malloc(sizeof(struct lwip_mbox));
|
||||||
|
LWIP_ASSERT("new_mbox != NULL", new_mbox != NULL);
|
||||||
|
if(new_mbox == NULL) {
|
||||||
|
#if LWIP_STATS
|
||||||
|
lwip_stats.sys.mbox.err++;
|
||||||
|
#endif // LWIP_STATS
|
||||||
|
return SYS_SEM_NULL;
|
||||||
|
}
|
||||||
|
new_mbox->sem = CreateSemaphore(0, 0, MAX_QUEUE_ENTRIES, 0);
|
||||||
|
LWIP_ASSERT("Error creating semaphore", new_mbox->sem != NULL);
|
||||||
|
if(new_mbox->sem == NULL) {
|
||||||
|
#if LWIP_STATS
|
||||||
|
lwip_stats.sys.mbox.err++;
|
||||||
|
#endif // LWIP_STATS
|
||||||
|
free(new_mbox);
|
||||||
|
new_mbox = NULL;
|
||||||
|
return SYS_SEM_NULL;
|
||||||
|
}
|
||||||
|
memset(&new_mbox->q_mem, 0, sizeof(u32_t)*MAX_QUEUE_ENTRIES);
|
||||||
|
new_mbox->head = 0;
|
||||||
|
new_mbox->tail = 0;
|
||||||
|
#if LWIP_STATS
|
||||||
|
lwip_stats.sys.mbox.used++;
|
||||||
|
LWIP_ASSERT("sys_mbox_new() counter overflow", lwip_stats.sys.mbox.used != 0 );
|
||||||
|
if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max) {
|
||||||
|
lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
|
||||||
|
}
|
||||||
|
#endif // LWIP_STATS
|
||||||
|
return new_mbox;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sys_mbox_free(sys_mbox_t mbox)
|
||||||
|
{
|
||||||
|
// parameter check
|
||||||
|
LWIP_ASSERT("sys_mbox_free ", mbox != SYS_MBOX_NULL );
|
||||||
|
LWIP_ASSERT("mbox->sem != NULL", mbox->sem != NULL);
|
||||||
|
LWIP_ASSERT("mbox->sem != INVALID_HANDLE_VALUE", mbox->sem != INVALID_HANDLE_VALUE);
|
||||||
|
|
||||||
|
CloseHandle(mbox->sem);
|
||||||
|
free(mbox);
|
||||||
|
|
||||||
|
#if LWIP_STATS
|
||||||
|
lwip_stats.sys.mbox.used--;
|
||||||
|
LWIP_ASSERT( "sys_mbox_free() ", lwip_stats.sys.mbox.used!= (u16_t)-1 );
|
||||||
|
#endif // LWIP_STATS
|
||||||
|
}
|
||||||
|
|
||||||
|
void sys_mbox_post(sys_mbox_t q, void *msg)
|
||||||
|
{
|
||||||
|
DWORD ret;
|
||||||
|
SYS_ARCH_DECL_PROTECT(lev);
|
||||||
|
|
||||||
|
// parameter check
|
||||||
|
LWIP_ASSERT("sys_mbox_free ", q != SYS_MBOX_NULL );
|
||||||
|
LWIP_ASSERT("q->sem != NULL", q->sem != NULL);
|
||||||
|
LWIP_ASSERT("q->sem != INVALID_HANDLE_VALUE", q->sem != INVALID_HANDLE_VALUE);
|
||||||
|
|
||||||
|
//printf("sys_mbox_post(%p %p)\n", q, msg);
|
||||||
|
|
||||||
|
SYS_ARCH_PROTECT(lev);
|
||||||
|
q->q_mem[q->head] = msg;
|
||||||
|
(q->head)++;
|
||||||
|
if (q->head >= MAX_QUEUE_ENTRIES) {
|
||||||
|
q->head = 0;
|
||||||
|
}
|
||||||
|
LWIP_ASSERT("mbox is full!", q->head != q->tail);
|
||||||
|
ret = ReleaseSemaphore(q->sem, 1, 0);
|
||||||
|
LWIP_ASSERT("Error releasing sem", ret != 0);
|
||||||
|
|
||||||
|
SYS_ARCH_UNPROTECT(lev);
|
||||||
|
}
|
||||||
|
|
||||||
|
u32_t sys_arch_mbox_fetch(sys_mbox_t q, void **msg, u32_t timeout)
|
||||||
|
{
|
||||||
|
DWORD ret;
|
||||||
|
LONGLONG starttime, endtime;
|
||||||
|
SYS_ARCH_DECL_PROTECT(lev);
|
||||||
|
|
||||||
|
// parameter check
|
||||||
|
LWIP_ASSERT("sys_mbox_free ", q != SYS_MBOX_NULL );
|
||||||
|
LWIP_ASSERT("q->sem != NULL", q->sem != NULL);
|
||||||
|
LWIP_ASSERT("q->sem != INVALID_HANDLE_VALUE", q->sem != INVALID_HANDLE_VALUE);
|
||||||
|
|
||||||
|
if (timeout == 0) {
|
||||||
|
timeout = INFINITE;
|
||||||
|
}
|
||||||
|
starttime = sys_get_ms();
|
||||||
|
if ((ret = WaitForSingleObject(q->sem, timeout)) == WAIT_OBJECT_0) {
|
||||||
|
SYS_ARCH_PROTECT(lev);
|
||||||
|
if(msg != NULL) {
|
||||||
|
*msg = q->q_mem[q->tail];
|
||||||
|
}
|
||||||
|
//printf("sys_arch_mbox_fetch(%p) -> %p\n", q, msg?*msg:0);
|
||||||
|
(q->tail)++;
|
||||||
|
if (q->tail >= MAX_QUEUE_ENTRIES) {
|
||||||
|
q->tail = 0;
|
||||||
|
}
|
||||||
|
SYS_ARCH_UNPROTECT(lev);
|
||||||
|
endtime = sys_get_ms();
|
||||||
|
return (u32_t)(endtime - starttime);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LWIP_ASSERT("Error waiting for sem", ret == WAIT_TIMEOUT);
|
||||||
|
if(msg != NULL) {
|
||||||
|
*msg = NULL;
|
||||||
|
}
|
||||||
|
//printf("sys_arch_mbox_fetch(%p) -> timeout\n", q);
|
||||||
|
return SYS_ARCH_TIMEOUT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
u32_t sys_arch_mbox_tryfetch(sys_mbox_t q, void **msg)
|
||||||
|
{
|
||||||
|
DWORD ret;
|
||||||
|
SYS_ARCH_DECL_PROTECT(lev);
|
||||||
|
|
||||||
|
// parameter check
|
||||||
|
LWIP_ASSERT("sys_mbox_free ", q != SYS_MBOX_NULL );
|
||||||
|
LWIP_ASSERT("q->sem != NULL", q->sem != NULL);
|
||||||
|
LWIP_ASSERT("q->sem != INVALID_HANDLE_VALUE", q->sem != INVALID_HANDLE_VALUE);
|
||||||
|
|
||||||
|
if ((ret = WaitForSingleObject(q->sem, 0)) == WAIT_OBJECT_0) {
|
||||||
|
SYS_ARCH_PROTECT(lev);
|
||||||
|
if(msg != NULL) {
|
||||||
|
*msg = q->q_mem[q->tail];
|
||||||
|
}
|
||||||
|
//printf("sys_arch_mbox_tryfetch(%p) -> %p\n", q, msg?*msg:0);
|
||||||
|
(q->tail)++;
|
||||||
|
if (q->tail >= MAX_QUEUE_ENTRIES) {
|
||||||
|
q->tail = 0;
|
||||||
|
}
|
||||||
|
SYS_ARCH_UNPROTECT(lev);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LWIP_ASSERT("Error waiting for sem", ret == WAIT_TIMEOUT);
|
||||||
|
if(msg != NULL) {
|
||||||
|
*msg = NULL;
|
||||||
|
}
|
||||||
|
//printf("sys_arch_mbox_tryfetch(%p) -> timeout\n", q);
|
||||||
|
return SYS_ARCH_TIMEOUT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* !NO_SYS */
|
@ -43,6 +43,8 @@
|
|||||||
#include "lwip/mem.h"
|
#include "lwip/mem.h"
|
||||||
#include "lwip/memp.h"
|
#include "lwip/memp.h"
|
||||||
#include "lwip/sys.h"
|
#include "lwip/sys.h"
|
||||||
|
#include "lwip/sockets.h"
|
||||||
|
#include "netif/etharp.h"
|
||||||
|
|
||||||
#include "lwip/stats.h"
|
#include "lwip/stats.h"
|
||||||
|
|
||||||
@ -78,16 +80,14 @@ int dbg_printf(const char *fmt, ...)
|
|||||||
#if LWIP_TCP
|
#if LWIP_TCP
|
||||||
static err_t netio_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
|
static err_t netio_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
|
||||||
{
|
{
|
||||||
if (err == ERR_OK && p != NULL)
|
if (err == ERR_OK && p != NULL) {
|
||||||
{
|
|
||||||
tcp_recved(pcb, p->tot_len);
|
tcp_recved(pcb, p->tot_len);
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
|
}
|
||||||
|
|
||||||
if (err == ERR_OK && p == NULL)
|
if (err == ERR_OK && p == NULL) {
|
||||||
{
|
|
||||||
tcp_arg(pcb, NULL);
|
tcp_arg(pcb, NULL);
|
||||||
tcp_sent(pcb, NULL);
|
tcp_sent(pcb, NULL);
|
||||||
tcp_recv(pcb, NULL);
|
tcp_recv(pcb, NULL);
|
||||||
@ -117,10 +117,14 @@ void netio_init(void)
|
|||||||
#endif /* LWIP_TCP */
|
#endif /* LWIP_TCP */
|
||||||
|
|
||||||
struct netif netif;
|
struct netif netif;
|
||||||
|
struct netif loop_netif;
|
||||||
|
|
||||||
void main_loop()
|
void main_loop()
|
||||||
{
|
{
|
||||||
struct ip_addr ipaddr, netmask, gw;
|
struct ip_addr ipaddr, netmask, gw;
|
||||||
|
#if LWIP_HAVE_LOOPIF
|
||||||
|
struct ip_addr loop_ipaddr, loop_netmask, loop_gw;
|
||||||
|
#endif
|
||||||
#if NO_SYS
|
#if NO_SYS
|
||||||
int last_time;
|
int last_time;
|
||||||
int timer1;
|
int timer1;
|
||||||
@ -149,13 +153,18 @@ void main_loop()
|
|||||||
#endif /* NO_SYS */
|
#endif /* NO_SYS */
|
||||||
netif_set_up(&netif);
|
netif_set_up(&netif);
|
||||||
|
|
||||||
|
#if LWIP_HAVE_LOOPIF
|
||||||
|
IP4_ADDR(&loop_gw, 127,0,0,1);
|
||||||
|
IP4_ADDR(&loop_ipaddr, 127,0,0,1);
|
||||||
|
IP4_ADDR(&loop_netmask, 255,0,0,0);
|
||||||
|
#if NO_SYS
|
||||||
|
netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, loopif_init, ip_input);
|
||||||
|
#else /* NO_SYS */
|
||||||
|
netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, loopif_init, tcpip_ethinput);
|
||||||
|
#endif /* NO_SYS */
|
||||||
|
#endif
|
||||||
/*
|
/*
|
||||||
IP4_ADDR(&gw, 127,0,0,1);
|
|
||||||
IP4_ADDR(&ipaddr, 127,0,0,1);
|
|
||||||
IP4_ADDR(&netmask, 255,0,0,0);
|
|
||||||
|
|
||||||
netif_add(&ipaddr, &netmask, &gw, loopif_init,
|
|
||||||
ip_input);
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if NO_SYS
|
#if NO_SYS
|
||||||
@ -174,32 +183,28 @@ void main_loop()
|
|||||||
last_time=clock();
|
last_time=clock();
|
||||||
timer1=0;
|
timer1=0;
|
||||||
timer2=0;
|
timer2=0;
|
||||||
done=0;
|
|
||||||
#endif /* NO_SYS */
|
#endif /* NO_SYS */
|
||||||
|
done=0;
|
||||||
|
|
||||||
while (!done)
|
while (!done) {
|
||||||
{
|
|
||||||
#if NO_SYS
|
#if NO_SYS
|
||||||
int cur_time;
|
int cur_time;
|
||||||
int time_diff;
|
int time_diff;
|
||||||
|
|
||||||
cur_time=clock();
|
cur_time=clock();
|
||||||
time_diff=cur_time-last_time;
|
time_diff=cur_time-last_time;
|
||||||
if (time_diff>0)
|
if (time_diff>0) {
|
||||||
{
|
|
||||||
last_time=cur_time;
|
last_time=cur_time;
|
||||||
timer1+=time_diff;
|
timer1+=time_diff;
|
||||||
timer2+=time_diff;
|
timer2+=time_diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timer1>10)
|
if (timer1>10) {
|
||||||
{
|
|
||||||
tcp_fasttmr();
|
tcp_fasttmr();
|
||||||
timer1=0;
|
timer1=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timer2>45)
|
if (timer2>45) {
|
||||||
{
|
|
||||||
tcp_slowtmr();
|
tcp_slowtmr();
|
||||||
timer2=0;
|
timer2=0;
|
||||||
done=_kbhit();
|
done=_kbhit();
|
||||||
@ -235,6 +240,7 @@ int main(void)
|
|||||||
mem_init();
|
mem_init();
|
||||||
memp_init();
|
memp_init();
|
||||||
pbuf_init();
|
pbuf_init();
|
||||||
|
etharp_init();
|
||||||
#if !NO_SYS
|
#if !NO_SYS
|
||||||
lwip_socket_init();
|
lwip_socket_init();
|
||||||
#endif /* !NO_SYS */
|
#endif /* !NO_SYS */
|
||||||
|
Loading…
Reference in New Issue
Block a user