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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [ppp/] [current/] [src/] [chat.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      src/chat.c
4
//
5
//      PPP CHAT scripting
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 2003 Free Software Foundation, Inc.                        
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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    nickg
43
// Contributors: nickg
44
// Date:         2003-06-08
45
// Purpose:      Chat script handling
46
// Description: 
47
//
48
//####DESCRIPTIONEND####
49
//
50
//==========================================================================
51
 
52
//=====================================================================
53
 
54
#include <pkgconf/system.h>
55
#include <pkgconf/net.h>
56
#include <pkgconf/io_fileio.h>
57
 
58
 
59
#include <stdio.h>
60
#include <ctype.h>
61
#include <stdlib.h>
62
#include <string.h>
63
#include <errno.h>
64
 
65
#include "cyg/ppp/syslog.h"
66
 
67
#include <cyg/io/io.h>
68
#include <cyg/io/serial.h>
69
 
70
#include <cyg/kernel/kapi.h>
71
 
72
#include <cyg/ppp/ppp.h>
73
 
74
//=====================================================================
75
 
76
//#undef db_printf
77
//#define db_printf diag_printf
78
 
79
//=====================================================================
80
// Expect return values
81
 
82
#define CHAT_MATCH      0
83
#define CHAT_ABORT      1
84
#define CHAT_TIMEOUT    2
85
#define CHAT_FAIL       -1
86
 
87
//=====================================================================
88
// Configuration constants
89
 
90
#define CHAT_MAX_ABORTS CYGNUM_PPP_CHAT_ABORTS_MAX
91
#define CHAT_ABORT_SIZE CYGNUM_PPP_CHAT_ABORTS_SIZE
92
 
93
#define CHAT_STRING_LENGTH CYGNUM_PPP_CHAT_STRING_LENGTH
94
 
95
//=====================================================================
96
// Local variables
97
 
98
// Array of ABORT strings
99
static char cyg_ppp_chat_abort[CHAT_MAX_ABORTS][CHAT_ABORT_SIZE];
100
static int cyg_ppp_chat_abort_count;
101
 
102
// Response timeout
103
static int cyg_ppp_chat_timeout;
104
// timeout alarm
105
static cyg_alarm cyg_ppp_chat_alarm_obj;
106
static cyg_handle_t cyg_ppp_chat_alarm;
107
static cyg_handle_t cyg_ppp_chat_thread;
108
 
109
// Handle on serial device
110
static cyg_io_handle_t cyg_ppp_chat_handle;
111
 
112
// String buffers
113
char cyg_ppp_chat_buffer[CHAT_STRING_LENGTH];
114
static char cyg_ppp_chat_expect_buffer[CHAT_STRING_LENGTH];
115
 
116
//=====================================================================
117
// Timeout alarm function
118
//
119
// The alarm is set whenever we start looking for a match, if the alarm
120
// goes off before we find a match, then it kicks the thread out of any
121
// wait it is in.
122
 
123
static void chat_alarm(cyg_handle_t alarm, cyg_addrword_t data)
124
{
125
    db_printf("%s(%d)\n",__PRETTY_FUNCTION__,__LINE__);
126
    cyg_thread_release( cyg_ppp_chat_thread );
127
}
128
 
129
//=====================================================================
130
// Match
131
//
132
// Looks for the supplied string in the input. Returns zero on timeout
133
// or other error, 1 if a match is found.
134
 
135
static int cyg_ppp_chat_match( char *str )
136
{
137
    cyg_uint64 trigger = (cyg_ppp_chat_timeout * 100) + cyg_current_time();
138
    char *s = cyg_ppp_chat_buffer;
139
    int matchlen = strlen(str);
140
    int matched = 0;
141
    int aborted = 0;
142
 
143
    db_printf("%s(%d) timeout %d trigger %d\n",__PRETTY_FUNCTION__,__LINE__,
144
              cyg_ppp_chat_timeout,(long)trigger);
145
    db_printf(" current time %d\n",(long)cyg_current_time());
146
 
147
    if( matchlen == 0 )
148
        return 1;
149
 
150
    cyg_alarm_initialize( cyg_ppp_chat_alarm,
151
                          trigger,
152
 
153
 
154
    while(!(matched || aborted))
155
    {
156
        Cyg_ErrNo err;
157
        char c;
158
        cyg_uint32 len = 1;
159
        int i;
160
 
161
        err = cyg_io_read( cyg_ppp_chat_handle, &c, &len );
162
 
163
        if( err != 0 )
164
        {
165
            db_printf("%s(%d) err %d\n",__PRETTY_FUNCTION__,__LINE__,err);
166
            break;
167
        }
168
 
169
        db_printf("%c",c);
170
 
171
        *s++ = c;
172
 
173
        // If we have enough characters to match the expect string,
174
        // and the current character matches the last character of the
175
        // expect string, then it is worth trying to compare the strings.
176
 
177
        if( s - cyg_ppp_chat_buffer >= matchlen &&
178
            c == str[matchlen - 1] &&
179
            strncmp( s - matchlen, str, matchlen ) == 0 )
180
        {
181
            matched = 1;
182
            break;
183
        }
184
 
185
        for( i = 0 ; i < cyg_ppp_chat_abort_count; i++ )
186
        {
187
            int abortlen = strlen( cyg_ppp_chat_abort[i] );
188
 
189
            if( s - cyg_ppp_chat_buffer >= abortlen &&
190
                strncmp( s - abortlen, cyg_ppp_chat_abort[i], abortlen ) == 0 )
191
            {
192
                syslog(LOG_ERR,"ABORT: %s\n",cyg_ppp_chat_abort[i]);
193
                aborted = 1;
194
                break;
195
            }
196
        }
197
 
198
        // If we have reached the end of the buffer, shuffle it down
199
        // leaving the last matchlen characters in place.
200
        if( s >= &cyg_ppp_chat_buffer[sizeof(cyg_ppp_chat_buffer)-1] )
201
        {
202
            strncpy( cyg_ppp_chat_buffer, s-matchlen, matchlen );
203
            s = cyg_ppp_chat_buffer + matchlen;
204
        }
205
    }
206
 
207
    cyg_alarm_disable( cyg_ppp_chat_alarm );
208
 
209
    return matched;
210
}
211
 
212
//=====================================================================
213
// Send string
214
//
215
// Send the string to the remote end, dealing with any escapes or
216
// special values. We currently implement the "EOT" and "BREAK"
217
// strings (or would if the device drivers did), and just the \c
218
// escape sequence.
219
 
220
static int cyg_ppp_chat_send( const char *s )
221
{
222
    cyg_bool crlf = true;
223
 
224
    db_printf("%s(%d) %s\n",__PRETTY_FUNCTION__,__LINE__,s);
225
 
226
    if( strcmp( s, "EOT" ) == 0 )
227
    {
228
        s = "\x04";
229
        crlf = false;
230
    }
231
    else if( strcmp( s, "BREAK" ) == 0 )
232
    {
233
        // cannot do this at present
234
    }
235
 
236
    for( ; *s != 0 ; s++ )
237
    {
238
        char c = *s;
239
        cyg_uint32 one = 1;
240
 
241
        // Look for escape sequences and process them
242
        if( c == '\\' )
243
        {
244
            c = *++s;
245
 
246
            if( c == 0 )
247
                break;
248
 
249
            switch( c )
250
            {
251
            case 'c':
252
                crlf = false;
253
                continue;
254
            }
255
        }
256
 
257
        cyg_io_write( cyg_ppp_chat_handle, &c, &one );
258
    }
259
 
260
    if( crlf )
261
    {
262
        cyg_uint32 len = 2;
263
        s = "\r\n";
264
        cyg_io_write( cyg_ppp_chat_handle, s, &len );
265
    }
266
 
267
    return 0;
268
}
269
 
270
//=====================================================================
271
// find_sep
272
//
273
// Split the pointed-to string at the next '-' character.
274
 
275
static char *find_sep( char **str )
276
{
277
    char *s = *str;
278
    char *res = s;
279
 
280
    if( *s == 0 )
281
        return NULL;
282
 
283
    while( *s != 0 && *s != '-' )
284
        s++;
285
 
286
    if( *s == '-' )
287
        *s++ = 0;
288
 
289
    *str = s;
290
    return res;
291
}
292
 
293
 
294
//=====================================================================
295
// expect processing
296
//
297
// Parse the expect string. The return value indicates what it is: a
298
// match string, an ABORT, a TIMEOUT or a failure.
299
 
300
static int cyg_ppp_chat_expect( const char *str )
301
{
302
    char *expect;
303
    char *reply;
304
    char *s;
305
 
306
    db_printf("%s(%d) %s\n",__PRETTY_FUNCTION__,__LINE__,str);
307
 
308
    // Look for command strings
309
    if( strcmp( str, "ABORT" ) == 0 )
310
        return CHAT_ABORT;
311
 
312
    if( strcmp( str, "TIMEOUT" ) == 0 )
313
        return CHAT_TIMEOUT;
314
 
315
    // Copy the string over to a writable buffer, so we can handle the
316
    // sub-expect strings
317
    strncpy( cyg_ppp_chat_expect_buffer, str, sizeof(cyg_ppp_chat_expect_buffer) );
318
    s = cyg_ppp_chat_expect_buffer;
319
 
320
    while(1)
321
    {
322
        // Split the string at the next '-'
323
        expect = find_sep( &s );
324
 
325
        // All done, it looks like we succeeded!
326
        if( expect == NULL )
327
            return CHAT_MATCH;
328
 
329
        db_printf("%s(%d) expect %s\n",__PRETTY_FUNCTION__,__LINE__,expect);
330
 
331
        // Get and response string in case the match fails.
332
        reply = find_sep( &s );
333
 
334
        db_printf("%s(%d) reply %s\n",__PRETTY_FUNCTION__,__LINE__,reply);
335
 
336
        // Try a match.
337
        if( cyg_ppp_chat_match( expect ) )
338
            return CHAT_MATCH;
339
 
340
        // The match failed. If there is no reply string, then the
341
        // whole thing has failed.
342
        if( reply == NULL )
343
            break;
344
 
345
        // Send the reply string an loop around to pick up the next
346
        // expect string.
347
        cyg_ppp_chat_send( reply );
348
    }
349
 
350
    return CHAT_FAIL;
351
}
352
 
353
//=====================================================================
354
// Chat entry point
355
//
356
// This is the main CHAT entry point. It is given the name of a device
357
// plus a pointer to the expect script in argv format. The device
358
// should already have been set up to the correct baud rate, bits,
359
// parity, flow control etc.
360
 
361
externC cyg_int32 cyg_ppp_chat( const char *devname,
362
                                const char *script[] )
363
{
364
    const char *s;
365
    Cyg_ErrNo err;
366
    cyg_int32 result = 1;
367
    cyg_uint32 zero = 0;
368
    cyg_uint32 len = sizeof(zero);
369
 
370
    cyg_ppp_chat_thread = cyg_thread_self();
371
    cyg_ppp_chat_abort_count = 0;
372
    cyg_ppp_chat_timeout = 45;
373
 
374
    // Clear the result area
375
    memset(cyg_ppp_chat_buffer, 0, CHAT_STRING_LENGTH);
376
 
377
    while ((err = cyg_io_lookup(devname, &cyg_ppp_chat_handle)) < 0) {
378
        if (err != 0)
379
            syslog(LOG_ERR, "Failed to open %s: %d", devname,err);
380
    }
381
 
382
    // Flush the serial input before starting
383
    cyg_io_get_config( cyg_ppp_chat_handle,
384
                       CYG_IO_GET_CONFIG_SERIAL_INPUT_FLUSH,
385
                       &zero, &len);
386
 
387
    // Set up timeout alarm.
388
    cyg_alarm_create( cyg_real_time_clock(),
389
                      chat_alarm,
390
                      (cyg_addrword_t)0,
391
                      &cyg_ppp_chat_alarm,
392
                      &cyg_ppp_chat_alarm_obj);
393
 
394
 
395
    // Now loop over script handling the elements in turn
396
    while( (s = *script++) != NULL )
397
    {
398
        int what = cyg_ppp_chat_expect( s );
399
 
400
        db_printf("%s(%d) what %d\n",__PRETTY_FUNCTION__,__LINE__,what);
401
 
402
        if( what == CHAT_FAIL )
403
        {
404
            result = 0;
405
            break;
406
        }
407
 
408
        if( (s = *script++) != NULL )
409
        {
410
            if( what == CHAT_MATCH )
411
                cyg_ppp_chat_send( s );
412
            else switch( what )
413
            {
414
            case CHAT_ABORT:
415
                // An abort string, add it to the table.
416
                if( cyg_ppp_chat_abort_count >= CHAT_MAX_ABORTS )
417
                {
418
                    syslog(LOG_ERR,"Too many ABORT strings\n");
419
                }
420
                else if( strlen( s ) >= CHAT_ABORT_SIZE )
421
                    syslog(LOG_ERR,"Abort string too long\n");
422
                else
423
                {
424
                    strncpy( cyg_ppp_chat_abort[cyg_ppp_chat_abort_count++],
425
                             s, CHAT_ABORT_SIZE );
426
                }
427
                break;
428
 
429
            case CHAT_TIMEOUT:
430
                // A new timeout value
431
                cyg_ppp_chat_timeout = atoi( s );
432
                db_printf("New timeout >%s< %d\n",s,cyg_ppp_chat_timeout);
433
                break;
434
            }
435
        }
436
    }
437
 
438
    if (s==NULL)
439
    {
440
        // the script ran to completion
441
        result = 1;
442
    }
443
 
444
    // Finally, wait for the serial device to drain 
445
        cyg_io_get_config( cyg_ppp_chat_handle,
446
                           CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN,
447
                           &zero, &len);
448
 
449
    return result;
450
}
451
 
452
//=====================================================================
453
// End of chat.c
454
 

powered by: WebSVN 2.1.0

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