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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [lwip_tcpip/] [current/] [tests/] [ppp.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
//####ECOSGPLCOPYRIGHTBEGIN####
3
// -------------------------------------------
4
// This file is part of eCos, the Embedded Configurable Operating System.
5
// Copyright (C) 2008 Free Software Foundation
6
//
7
// eCos is free software; you can redistribute it and/or modify it under
8
// the terms of the GNU General Public License as published by the Free
9
// Software Foundation; either version 2 or (at your option) any later version.
10
//
11
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
12
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
// for more details.
15
//
16
// You should have received a copy of the GNU General Public License along
17
// with eCos; if not, write to the Free Software Foundation, Inc.,
18
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19
//
20
// As a special exception, if other files instantiate templates or use macros
21
// or inline functions from this file, or you compile this file and link it
22
// with other works to produce a work based on this file, this file does not
23
// by itself cause the resulting work to be covered by the GNU General Public
24
// License. However the source code for this file must still be made available
25
// in accordance with section (3) of the GNU General Public License.
26
//
27
// This exception does not invalidate any other reasons why a work based on
28
// this file might be covered by the GNU General Public License.
29
// -------------------------------------------
30
//####ECOSGPLCOPYRIGHTEND####
31
//==========================================================================
32
 
33
// Simple ppp test
34
 
35
#include <stdio.h>
36
 
37
#include <cyg/hal/hal_arch.h>
38
#include <cyg/infra/diag.h>
39
#include <cyg/infra/testcase.h>
40
#include <cyg/kernel/kapi.h>
41
 
42
#include <pkgconf/net_lwip.h>
43
 
44
#include <lwip.h>
45
#include <lwip/dns.h>
46
 
47
#include "netif/ppp/ppp.h"
48
#include "netif/ppp/chat.h"
49
 
50
#ifdef CYGFUN_LWIP_MODE_SIMPLE
51
#if PPP_SUPPORT
52
#if LWIP_TCP
53
 
54
#define NUM_RUNS        5
55
#define CHAT_TIMEOUT    30
56
 
57
#define STACK_SIZE      CYGNUM_HAL_STACK_SIZE_TYPICAL
58
 
59
static char main_stack[STACK_SIZE];
60
static cyg_thread main_thread;
61
static cyg_handle_t main_handle;
62
 
63
static struct chat_item chat_items[] = {
64
    { CHAT_ABORT,   "NO CARRIER" },
65
    { CHAT_ABORT,   "NO DIALTONE" },
66
    { CHAT_ABORT,   "BUSY" },
67
    { CHAT_ABORT,   "ERROR" },
68
    { CHAT_SEND,    "+++" },
69
    { CHAT_SLEEP,   (char *) 2000 },
70
    { CHAT_SEND,    "AT\r\n" },
71
    { CHAT_WAIT,    "OK" },
72
    { CHAT_SEND,    "ATZ\r\n" },
73
    { CHAT_WAIT,    "OK" },
74
    { CHAT_SEND,    "ATE0\r\n" },
75
    { CHAT_WAIT,    "OK" },
76
    { CHAT_SEND_CB, (char *) 0 },   // AT+CGDCONT
77
    { CHAT_WAIT,    "OK" },
78
    { CHAT_SEND,    (char *) 1 },   // ATD
79
    { CHAT_WAIT,    "CONNECT" },
80
    { CHAT_SLEEP,   (char *) 1000 },
81
    { CHAT_LAST,    0 },
82
};
83
 
84
enum test_state {
85
    TEST_INITIAL,
86
    TEST_CHAT_INIT,
87
    TEST_CHAT_RUN,
88
    TEST_CHAT_FINISH,
89
    TEST_PPP_INIT,
90
    TEST_PPP_INIT_WAIT,
91
    TEST_PPP_UP,
92
    TEST_PPP_CLOSE,
93
    TEST_PPP_CLOSE_WAIT,
94
    TEST_NEXT_RUN,
95
    TEST_FINISH,
96
};
97
 
98
enum req_state {
99
    REQ_DNS_INIT,
100
    REQ_DNS_WAIT,
101
    REQ_DNS_FAILED,
102
    REQ_DNS_SUCCESS,
103
};
104
 
105
static enum test_state test_state;
106
static enum req_state req_state;
107
static sio_fd_t sd;
108
static chat_t chat;
109
static int pd;
110
static struct ip_addr host_addr;
111
static int run = 1;
112
static int success = 0;
113
static int chat_ok;
114
 
115
static void
116
chat_cb(chat_t chat, chat_err_t err, void *arg)
117
{
118
    switch (err) {
119
    case CHAT_ERR_OK:
120
        CYG_TEST_INFO("Chat OK");
121
        chat_ok = 1;
122
        break;
123
    case CHAT_ERR_ABORT:
124
        CYG_TEST_INFO("Chat aborted");
125
        chat_ok = 0;
126
        break;
127
    case CHAT_ERR_TIMEOUT:
128
        CYG_TEST_INFO("Chat timeout");
129
        chat_ok = 0;
130
        break;
131
    }
132
 
133
    test_state = TEST_CHAT_FINISH;
134
}
135
 
136
static void
137
chat_send_cb(chat_t chat, int id, char *buf, size_t len, void *arg)
138
{
139
    switch (id) {
140
    case 0:
141
        snprintf(buf, len, "AT+CGDCONT=1,\"IP\",\"%s\"\r\n", CYGDAT_NET_LWIP_PPP_TEST_APN);
142
        break;
143
    case 1:
144
        snprintf(buf, len, "ATD%s\r\n", CYGDAT_NET_LWIP_PPP_TEST_NUMBER);
145
        break;
146
    }
147
}
148
 
149
static void
150
ppp_status_cb(void *ctx, int err, void *arg)
151
{
152
    switch (err) {
153
    case PPPERR_NONE:
154
        CYG_TEST_INFO("PPP link up");
155
        // Setup DNS server
156
        {
157
            struct ppp_addrs *addrs = (struct ppp_addrs *) arg;
158
            dns_setserver(2, &addrs->dns1);
159
        }
160
 
161
        if (test_state == TEST_PPP_INIT_WAIT)
162
            test_state = TEST_PPP_UP;
163
        break;
164
    case PPPERR_PARAM:
165
        CYG_TEST_INFO("Invalid parameter");
166
        test_state = TEST_PPP_CLOSE_WAIT;
167
        break;
168
    case PPPERR_OPEN:
169
        CYG_TEST_INFO("Unable to open PPP session");
170
        test_state = TEST_PPP_CLOSE_WAIT;
171
        break;
172
    case PPPERR_DEVICE:
173
        CYG_TEST_INFO("Invalid I/O device for PPP");
174
        test_state = TEST_PPP_CLOSE_WAIT;
175
        break;
176
    case PPPERR_ALLOC:
177
        CYG_TEST_INFO("Unable to allocate resources");
178
        test_state = TEST_PPP_CLOSE_WAIT;
179
        break;
180
    case PPPERR_USER:
181
        CYG_TEST_INFO("User interrupt");
182
        test_state = TEST_PPP_CLOSE_WAIT;
183
        break;
184
    case PPPERR_CONNECT:
185
        CYG_TEST_INFO("Connection lost");
186
        test_state = TEST_PPP_CLOSE_WAIT;
187
        break;
188
    case PPPERR_AUTHFAIL:
189
        CYG_TEST_INFO("Failed authentication challenge");
190
        test_state = TEST_PPP_CLOSE_WAIT;
191
        break;
192
    case PPPERR_PROTOCOL:
193
        CYG_TEST_INFO("Failed to meet protocol");
194
        test_state = TEST_PPP_CLOSE_WAIT;
195
        break;
196
    }
197
}
198
 
199
static void
200
dns_found_cb(const char *name, struct ip_addr *addr, void *arg)
201
{
202
    if (addr) {
203
        host_addr = *addr;
204
        req_state = REQ_DNS_SUCCESS;
205
    } else {
206
        req_state = REQ_DNS_FAILED;
207
    }
208
}
209
 
210
 
211
static void
212
handle_req_state(void)
213
{
214
    switch (req_state) {
215
    case REQ_DNS_INIT:
216
        CYG_TEST_INFO("Trying to resolve host name");
217
        if (dns_gethostbyname(CYGDAT_NET_LWIP_PPP_TEST_HOST, &host_addr,
218
                              dns_found_cb, NULL) == ERR_OK) {
219
            // Cached
220
            req_state = REQ_DNS_SUCCESS;
221
            break;
222
        }
223
        req_state = REQ_DNS_WAIT;
224
        break;
225
    case REQ_DNS_WAIT:
226
        break;
227
    case REQ_DNS_FAILED:
228
        CYG_TEST_INFO("Failed to resolve host name");
229
        test_state = TEST_PPP_CLOSE;
230
        break;
231
    case REQ_DNS_SUCCESS:
232
        CYG_TEST_INFO("Successfully resolved host name");
233
        success++;
234
        test_state = TEST_PPP_CLOSE;
235
        break;
236
    }
237
}
238
 
239
void
240
main_thread_entry(cyg_addrword_t p)
241
{
242
    CYG_TEST_INFO("Initializing lwIP");
243
    cyg_lwip_simple_init();
244
 
245
    test_state = TEST_CHAT_INIT;
246
    req_state = REQ_DNS_INIT;
247
 
248
    while (1) {
249
        cyg_lwip_simple_poll();
250
        switch (test_state) {
251
        case TEST_INITIAL:
252
            diag_printf("INFO:<Starting test run (%d/%d)>\n", run, NUM_RUNS);
253
            test_state = TEST_CHAT_INIT;
254
            break;
255
        case TEST_CHAT_INIT:
256
            CYG_TEST_INFO("Initializing chat");
257
            sd = sio_open(SIO_DEV_PPPOS);
258
            if (!sd)
259
                CYG_TEST_FAIL_FINISH("Cannot open serial");
260
            chat = chat_new(sd, chat_items, CHAT_TIMEOUT, chat_cb, chat_send_cb, NULL);
261
            if (!chat)
262
                CYG_TEST_FAIL_FINISH("Cannot allocate chat");
263
            test_state = TEST_CHAT_RUN;
264
            break;
265
        case TEST_CHAT_RUN:
266
            chat_poll(chat);
267
            break;
268
        case TEST_CHAT_FINISH:
269
            chat_free(chat);
270
            if (chat_ok)
271
                test_state = TEST_PPP_INIT;
272
            else
273
                test_state = TEST_NEXT_RUN;
274
            break;
275
        case TEST_PPP_INIT:
276
            CYG_TEST_INFO("Initializing PPP");
277
            if (ppp_init() != ERR_OK)
278
                CYG_TEST_FAIL_FINISH("Cannot initialize PPP");
279
            pd = ppp_open_serial(sd, ppp_status_cb, (void *) pd);
280
            if (pd < 0)
281
                CYG_TEST_FAIL_FINISH("Cannot open PPP over serial");
282
            ppp_set_auth(PPPAUTHTYPE_ANY, CYGDAT_NET_LWIP_PPP_TEST_USERNAME,
283
                                          CYGDAT_NET_LWIP_PPP_TEST_PASSWORD);
284
            test_state = TEST_PPP_INIT_WAIT;
285
            break;
286
        case TEST_PPP_INIT_WAIT:
287
            ppp_poll(pd);
288
            break;
289
        case TEST_PPP_UP:
290
            ppp_poll(pd);
291
            handle_req_state();
292
            break;
293
        case TEST_PPP_CLOSE:
294
            ppp_close(pd);
295
            test_state = TEST_PPP_CLOSE_WAIT;
296
            break;
297
        case TEST_PPP_CLOSE_WAIT:
298
            ppp_poll(pd);
299
            if (!ppp_is_open(pd))
300
                // Escape from data mode
301
                sio_write(sd, (u8_t *) "+++", 3);
302
                cyg_thread_delay(200);
303
                test_state = TEST_NEXT_RUN;
304
            break;
305
        case TEST_NEXT_RUN:
306
            if (run < NUM_RUNS) {
307
                run++;
308
                test_state = TEST_INITIAL;
309
            } else {
310
                test_state = TEST_FINISH;
311
            }
312
            break;
313
        case TEST_FINISH:
314
            test_state = TEST_CHAT_INIT;
315
            diag_printf("INFO:<Test done (%d/%d) successful runs\n",
316
                        success, NUM_RUNS);
317
            break;
318
        }
319
        cyg_thread_yield();
320
    }
321
}
322
 
323
void
324
ppptest_main(void)
325
{
326
    CYG_TEST_INIT();
327
    CYG_TEST_INFO("Testing ppp");
328
 
329
    // Create a main thread, so we can run the scheduler and have time 'pass'
330
    cyg_thread_create(
331
        10,                 // Priority - just a number
332
        main_thread_entry,  // Entry
333
        0,                  // Entry parameter
334
        "main",             // Name
335
        main_stack,         // Stack
336
        STACK_SIZE,         // Size
337
        &main_handle,       // Handle
338
        &main_thread        // Thread data structure
339
    );
340
    cyg_thread_resume(main_handle);
341
    cyg_scheduler_start();
342
 
343
    CYG_TEST_FAIL_FINISH("Not reached");
344
}
345
 
346
externC void
347
cyg_start(void)
348
{
349
    ppptest_main();
350
}
351
 
352
#else // LWIP_TCP
353
#define N_A_MSG "TCP support disabled"
354
#endif // LWIP_TCP
355
 
356
#else // PPP_SUPPORT
357
#define N_A_MSG "PPP support disabled"
358
#endif // PPP_SUPPORT
359
 
360
#else // CYGFUN_LWIP_MODE_SIMPLE
361
#define N_A_MSG "Not configured in simple mode"
362
#endif // CYGFUN_LWIP_MODE_SIMPLE
363
 
364
#ifdef N_A_MSG
365
externC void
366
cyg_start(void)
367
{
368
    CYG_TEST_INIT();
369
    CYG_TEST_NA(N_A_MSG);
370
}
371
#endif // N_A_MSG

powered by: WebSVN 2.1.0

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