OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [lwip_tcpip/] [current/] [src/] [ecos/] [simple.c] - Blame information for rev 865

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      simple.c
4
//
5
//      lwIP simple mode (single-threaded) support.
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 2008, 2009 Free Software Foundation
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
// -------------------------------------------
36
//####ECOSGPLCOPYRIGHTEND####
37
//==========================================================================
38
//#####DESCRIPTIONBEGIN####
39
//
40
// Author(s):    Simon Kallweit
41
// Contributors:
42
// Date:         2008-12-08
43
// Purpose:
44
// Description:  lwIP simple mode (single-threaded) support.
45
//
46
//####DESCRIPTIONEND####
47
//
48
//==========================================================================
49
 
50
#include <pkgconf/kernel.h>
51
#include <pkgconf/system.h>
52
#include <pkgconf/net_lwip.h>
53
 
54
#include "lwip.h"
55
#include "lwip/opt.h"
56
#include "lwip/sys.h"
57
#include "lwip/memp.h"
58
#include "lwip/ip_addr.h"
59
#include "lwip/init.h"
60
#include "lwip/tcp.h"
61
#include "lwip/ip_frag.h"
62
#include "lwip/dhcp.h"
63
#include "lwip/autoip.h"
64
#include "lwip/igmp.h"
65
#include "lwip/dns.h"
66
#include "lwip/memp.h"
67
#include "lwip/netif.h"
68
 
69
#include "netif/loopif.h"
70
#include "netif/slipif.h"
71
#include "netif/etharp.h"
72
#include "netif/ppp/ppp.h"
73
#include "netif/ppp/chat.h"
74
 
75
#include <cyg/infra/diag.h>
76
#include <cyg/kernel/kapi.h>
77
 
78
#ifdef CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT
79
#include <cyg/hal/hal_if.h>            // HAL_CTRLC_CHECK
80
#endif
81
 
82
#ifdef CYGPKG_LWIP_ETH
83
#include <cyg/io/eth/eth_drv.h>
84
#include <cyg/io/eth/netdev.h>
85
#endif
86
 
87
#if LWIP_HAVE_LOOPIF && defined(CYGIMP_LWIP_LOOPIF_INSTANCE)
88
static struct netif loopif;
89
#endif
90
 
91
#if LWIP_HAVE_SLIPIF && defined(CYGIMP_LWIP_SLIPIF_INSTANCE)
92
static struct netif slipif;
93
#endif
94
 
95
#ifdef CYGPKG_LWIP_ETH
96
 
97
// Ethernet driver table
98
CYG_HAL_TABLE_BEGIN(__NETDEVTAB__, netdev);
99
CYG_HAL_TABLE_END(__NETDEVTAB_END__, netdev);
100
 
101
// Received packet
102
struct recv_packet {
103
    struct netif *netif;
104
    struct pbuf *p;
105
};
106
 
107
#define MAX_RECV_PACKETS    (PBUF_POOL_SIZE + 1)
108
static struct recv_packet recv_packet[MAX_RECV_PACKETS];
109
static int recv_packet_read;
110
static int recv_packet_write;
111
static int recv_packet_count;
112
 
113
#endif // CYGPKG_LWIP_ETH
114
 
115
// lwIP interval timer
116
struct lwip_timer {
117
    cyg_uint32 time;            // Time counter in ms
118
    cyg_uint32 interval;        // Interval in ms
119
    void (*timer_func)(void);   // lwIP timer to call
120
};
121
 
122
// List of lwIP interval timers
123
static struct lwip_timer lwip_timers[] = {
124
#if LWIP_TCP
125
    { 0, TCP_TMR_INTERVAL,          tcp_tmr },
126
#endif
127
#if LWIP_ARP
128
    { 0, ARP_TMR_INTERVAL,          etharp_tmr },
129
#endif
130
#if LWIP_DHCP
131
    { 0, DHCP_FINE_TIMER_MSECS,     dhcp_fine_tmr },
132
    { 0, DHCP_COARSE_TIMER_MSECS,   dhcp_coarse_tmr },
133
#endif
134
#if IP_REASSEMBLY
135
    { 0, IP_TMR_INTERVAL,           ip_reass_tmr },
136
#endif
137
#if LWIP_AUTOIP
138
    { 0, AUTOIP_TMR_INTERVAL,       autoip_tmr },
139
#endif
140
#if LWIP_IGMP
141
    { 0, IGMP_TMR_INTERVAL,         igmp_tmr },
142
#endif
143
#if LWIP_DNS
144
    { 0, DNS_TMR_INTERVAL,          dns_tmr },
145
#endif
146
#if PPP_SUPPORT
147
    { 0, PPP_TMR_INTERVAL,          ppp_tmr },
148
    { 0, CHAT_TMR_INTERVAL,         chat_tmr },
149
#endif
150
};
151
 
152
#define NUM_LWIP_TIMERS (sizeof(lwip_timers) / sizeof(lwip_timers[0]))
153
 
154
#define TICKS_TO_MS(_ticks_) \
155
    ((_ticks_) * (CYGNUM_HAL_RTC_NUMERATOR / CYGNUM_HAL_RTC_DENOMINATOR / 1000000LL))
156
 
157
static cyg_tick_count_t last_ticks;
158
 
159
 
160
#if (LWIP_HAVE_LOOPIF && defined(CYGIMP_LWIP_LOOPIF_INSTANCE)) || \
161
    (LWIP_HAVE_SLIPIF && defined(CYGIMP_LWIP_SLIPIF_INSTANCE))
162
 
163
//
164
// Sets an IP address.
165
//
166
static void
167
set_ip_addr(struct ip_addr *addr, u8_t a, u8_t b, u8_t c, u8_t d)
168
{
169
    IP4_ADDR(addr, a, b, c, d);
170
}
171
 
172
//
173
// Process an incoming IP or ethernet packet.
174
//
175
static err_t
176
simple_input(struct pbuf *p, struct netif *netif)
177
{
178
#if LWIP_ARP
179
    if (netif->flags & NETIF_FLAG_ETHARP) {
180
        ethernet_input(p, netif);
181
    } else
182
#endif
183
    {
184
        ip_input(p, netif);
185
    }
186
 
187
    return ERR_OK;
188
}
189
 
190
#endif // (LWIP_HAVE_LOOPIF && defined(CYGIMP_LWIP_LOOPIF_INSTANCE)) ||
191
       // (LWIP_HAVE_SLIPIF && defined(CYGIMP_LWIP_SLIPIF_INSTANCE))
192
 
193
#ifdef CYGFUN_LWIP_SHOW_NETIF_CONFIG
194
 
195
static void
196
show_netif_config(struct netif *netif)
197
{
198
    diag_printf(
199
        "%c%c%d: IP: %d.%d.%d.%d Submask: %d.%d.%d.%d Gateway: %d.%d.%d.%d\n",
200
        netif->name[0], netif->name[1], netif->num,
201
        ip4_addr1(&netif->ip_addr), ip4_addr2(&netif->ip_addr),
202
        ip4_addr3(&netif->ip_addr), ip4_addr4(&netif->ip_addr),
203
        ip4_addr1(&netif->netmask), ip4_addr2(&netif->netmask),
204
        ip4_addr3(&netif->netmask), ip4_addr4(&netif->netmask),
205
        ip4_addr1(&netif->gw), ip4_addr2(&netif->gw),
206
        ip4_addr3(&netif->gw), ip4_addr4(&netif->gw)
207
    );
208
}
209
 
210
static void
211
netif_status_callback(struct netif *netif)
212
{
213
    if (netif_is_up(netif))
214
        show_netif_config(netif);
215
}
216
 
217
#endif // CYGFUN_LWIP_SHOW_NETIF_CONFIG
218
 
219
 
220
//
221
// Initialize lwIP for simple (single-threaded) mode.
222
//
223
void
224
cyg_lwip_simple_init(void)
225
{
226
    static int initialized;
227
 
228
    // Only initialize once
229
    if (initialized)
230
        return;
231
    initialized = 1;
232
 
233
    // Initialize the lwIP stack
234
    lwip_init();
235
 
236
#if LWIP_HAVE_LOOPIF && defined(CYGIMP_LWIP_LOOPIF_INSTANCE)
237
    {
238
        struct ip_addr addr, netmask, gateway;
239
 
240
        // Setup default loopback device instance
241
        set_ip_addr(&addr, CYGDAT_LWIP_LOOPIF_ADDR);
242
        set_ip_addr(&netmask, CYGDAT_LWIP_LOOPIF_NETMASK);
243
        set_ip_addr(&gateway, CYGDAT_LWIP_LOOPIF_GATEWAY);
244
        netif_add(&loopif, &addr, &netmask, &gateway, NULL,
245
                  loopif_init, simple_input);
246
#ifdef CYGDAT_LWIP_LOOPIF_DEFAULT
247
        netif_set_default(&loopif);
248
#endif
249
#ifdef CYGFUN_LWIP_SHOW_NETIF_CONFIG
250
        netif_set_status_callback(&loopif, netif_status_callback);
251
#endif
252
        netif_set_up(&loopif);
253
    }
254
#endif
255
 
256
#if LWIP_HAVE_SLIPIF && defined(CYGIMP_LWIP_SLIPIF_INSTANCE)
257
    {
258
        struct ip_addr addr, netmask, gateway;
259
 
260
        // Setup default SLIP device instance
261
        set_ip_addr(&addr, CYGDAT_LWIP_SLIPIF_ADDR);
262
        set_ip_addr(&netmask, CYGDAT_LWIP_SLIPIF_NETMASK);
263
        set_ip_addr(&gateway, CYGDAT_LWIP_SLIPIF_GATEWAY);
264
        netif_add(&slipif, &addr, &netmask, &gateway, NULL,
265
                  slipif_init, simple_input);
266
#ifdef CYGDAT_LWIP_SLIPIF_DEFAULT
267
        netif_set_default(&slipif);
268
#endif
269
#ifdef CYGFUN_LWIP_SHOW_NETIF_CONFIG
270
        netif_set_status_callback(&slipif, netif_status_callback);
271
#endif
272
        netif_set_up(&slipif);
273
    }
274
#endif
275
 
276
#if PPP_SUPPORT
277
    {
278
        // Setup PPP instance
279
    }
280
#endif
281
 
282
#ifdef CYGPKG_LWIP_ETH
283
    {
284
        cyg_netdevtab_entry_t *t;
285
 
286
        // Initialize network devices
287
        for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) {
288
            if (t->init(t)) {
289
                t->status = CYG_NETDEVTAB_STATUS_AVAIL;
290
            } else {
291
                // Device not [currently] available
292
                t->status = 0;
293
            }
294
        }
295
    }
296
#endif
297
 
298
    cyg_lwip_simple_restart();
299
}
300
 
301
//
302
// Restarts lwIP when not polled for a longer period.
303
//
304
void
305
cyg_lwip_simple_restart(void)
306
{
307
    last_ticks = cyg_current_time();
308
}
309
 
310
//
311
// Polls the lwIP stack.
312
//
313
void
314
cyg_lwip_simple_poll(void)
315
{
316
    cyg_tick_count_t ticks;
317
    cyg_uint32 delta;
318
    int i;
319
 
320
#if LWIP_HAVE_SLIPIF && defined(CYGIMP_LWIP_SLIPIF_INSTANCE)
321
    // Poll SLIP device
322
    slipif_poll(&slipif);
323
#endif
324
 
325
#if PPP_SUPPORT
326
    // Poll PPP device
327
#endif
328
 
329
#ifdef CYGPKG_LWIP_ETH
330
    {
331
        cyg_netdevtab_entry_t *t;
332
 
333
        // Poll ethernet devices
334
        for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) {
335
            struct eth_drv_sc *sc = (struct eth_drv_sc *)t->device_instance;
336
            if (sc->state & ETH_DRV_NEEDS_DELIVERY) {
337
#if defined(CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT)
338
                cyg_bool was_ctrlc_int;
339
#endif
340
                sc->state &= ~ETH_DRV_NEEDS_DELIVERY;
341
#if defined(CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT)
342
                was_ctrlc_int = HAL_CTRLC_CHECK(sc->funs->int_vector(sc),
343
                                                (int) sc);
344
                if (!was_ctrlc_int) // Fall through and run normal code
345
#endif
346
                    sc->funs->deliver(sc);
347
            }
348
        }
349
 
350
        // Deliver received packets
351
        while (recv_packet_count > 0) {
352
            ethernet_input(recv_packet[recv_packet_read].p,
353
                           recv_packet[recv_packet_read].netif);
354
            recv_packet_read = (recv_packet_read + 1) % MAX_RECV_PACKETS;
355
            recv_packet_count--;
356
        }
357
    }
358
#endif
359
 
360
    // Process timers
361
    ticks = cyg_current_time();
362
    delta = TICKS_TO_MS(ticks - last_ticks);
363
    last_ticks = ticks;
364
 
365
    // The following check 'delta > 0' rejects a potential wrap-around in the
366
    // system ticks counter.
367
    if (delta > 0) {
368
        for (i = 0; i < NUM_LWIP_TIMERS; i++) {
369
            lwip_timers[i].time += delta;
370
            if (lwip_timers[i].time >= lwip_timers[i].interval) {
371
                lwip_timers[i].timer_func();
372
                lwip_timers[i].time -= lwip_timers[i].interval;
373
            }
374
        }
375
    }
376
}
377
 
378
#ifdef CYGPKG_LWIP_ETH
379
 
380
//
381
// Called from the network driver to indicate a new netif.
382
//
383
void
384
lwip_eth_drv_new(struct netif *netif)
385
{
386
#ifdef CYGFUN_LWIP_SHOW_NETIF_CONFIG
387
    netif_set_status_callback(netif, netif_status_callback);
388
#endif
389
}
390
 
391
//
392
// Called from network driver to indicate interrupt.
393
//
394
void
395
lwip_eth_drv_dsr(void)
396
{
397
}
398
 
399
//
400
// Called from the network driver when a packet was received.
401
//
402
err_t
403
lwip_eth_drv_input(struct pbuf *p, struct netif *netif)
404
{
405
    if (recv_packet_count >= MAX_RECV_PACKETS)
406
        return ERR_MEM;
407
 
408
    recv_packet[recv_packet_write].netif = netif;
409
    recv_packet[recv_packet_write].p = p;
410
    recv_packet_write = (recv_packet_write + 1) % MAX_RECV_PACKETS;
411
    recv_packet_count++;
412
 
413
    return ERR_OK;
414
}
415
 
416
#endif // CYGPKG_LWIP_ETH

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.