File: lnp.c

package info (click to toggle)
brickos 0.9.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,700 kB
  • ctags: 1,727
  • sloc: ansic: 9,139; cpp: 860; makefile: 717; asm: 693; sh: 123; perl: 61
file content (544 lines) | stat: -rw-r--r-- 13,843 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*! \file   lnp.c
    \brief  Implementation: link networking protocol
    \author Markus L. Noga <markus@noga.de>
*/

/*
 *  The contents of this file are subject to the Mozilla Public License
 *  Version 1.0 (the "License"); you may not use this file except in
 *  compliance with the License. You may obtain a copy of the License at
 *  http://www.mozilla.org/MPL/
 *
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 *  License for the specific language governing rights and limitations
 *  under the License.
 *
 *  The Original Code is legOS code, released October 17, 1999.
 *
 *  The Initial Developer of the Original Code is Markus L. Noga.
 *  Portions created by Markus L. Noga are Copyright (C) 1999
 *  Markus L. Noga. All Rights Reserved.
 *
 *  Contributor(s): Markus L. Noga <markus@noga.de>
 */

/*
 *  2000.05.01 - Paolo Masetti <paolo.masetti@itlug.org>
 *
 *  - IR lcd now reflect IR mode (near/far)
 *
 *  2001.09.10 - Zhengrong Zang <mikezang@iname.com>
 *
 *  - Remote control buttons
 *  - Standard firmware async message
 *
 *  2002.04.23 - Ted Hess <thess@kitschensync.net>
 *
 *  - Integrate Ross Crawford/Zhengrong Zang remote control message parser
 *    and RCX op-code dispatcher
 *
 */

#include <sys/lnp.h>

#ifdef CONF_LNP

#include <sys/lnp-logical.h>
#include <sys/irq.h>
#include <stdlib.h>

#ifdef CONF_VIS
#include <dlcd.h>
#include <sys/lcd.h>
#endif

#ifdef CONF_AUTOSHUTOFF
#include <sys/timeout.h>
#endif

#include <string.h>

///////////////////////////////////////////////////////////////////////////////
//
// Variables
//
///////////////////////////////////////////////////////////////////////////////

//! LNP host address (may be changed by dll utility --node=a option)
//! Default vaule in config.h may be overidden at compile type
unsigned char lnp_hostaddr = (CONF_LNP_HOSTADDR << 4) & CONF_LNP_HOSTMASK;

//! the timeout counter in ms
volatile unsigned short lnp_timeout_counter;

//! the timeout length in ms
unsigned short lnp_timeout=LNP_BYTE_TIMEOUT;

//! the integrity layer state
lnp_integrity_state_t lnp_integrity_state;

//! there are no ports for integrity layer packets, so just one handler.
/*! FIXME: uninitialized
*/
volatile lnp_integrity_handler_t lnp_integrity_handler;

//! addressing layer packets may be directed to a variety of ports.
/*! FIXME: uninitialized
    FIXME: inefficient if LNP_PORT_MASK doesn't adhere to 0...01...1 scheme.
*/
volatile lnp_addressing_handler_t lnp_addressing_handler[LNP_PORTMASK+1];

#if !defined(CONF_MM)
static char lnp_buffer[260];
#endif // CONF_MM

#if defined(CONF_RCX_PROTOCOL)
//! remote handler
lnp_remote_handler_t lnp_remote_handler;

//! RCX protocol header bytes excluding first byte
const unsigned char lnp_rcx_header[LNP_RCX_HEADER_LENGTH] = {0xff,0x00};

//! remote header bytes excluding first 3 bytes
const unsigned char lnp_rcx_remote_op[LNP_RCX_REMOTE_OP_LENGTH] = {0xd2,0x2d};


//! temp cells for RCX firmware protocol assembly
unsigned char lnp_rcx_temp0;
unsigned char lnp_rcx_temp1;

//! checksum from RCX protocol
unsigned char lnp_rcx_checksum;

#endif

#if defined(CONF_RCX_MESSAGE)
//! message header bytes excluding first 3 bytes
const unsigned char lnp_rcx_msg_op[LNP_RCX_MSG_OP_LENGTH] = {0xf7,0x08};

//! message from standard firmware
unsigned char lnp_rcx_message;

#endif

///////////////////////////////////////////////////////////////////////////////
//
// Functions
//
///////////////////////////////////////////////////////////////////////////////

#define lnp_checksum_init(sum)  (unsigned char)((sum) = 0xff)
#define lnp_checksum_step(sum,d)  (unsigned char)((sum) += (d))

#ifdef CONF_HOST
unsigned char lnp_checksum_copy( unsigned char *dest,
                                 const unsigned char *data, 
                                 unsigned length )
{
  unsigned char a = 0xff;
  unsigned char t;

  do {
    t = *data++;
    a += t;
    *dest++ = t;
    length--;
  } while (length > 0);

  return a;
}
#else
__asm__(
	".text\n"
	"_lnp_checksum_copy:\n"
	";; r0: dest, r1: data, r2: length;\n"
	
	"    add.w r0,r2         ; r2: end \n"
	"    mov.b #0xff,r3l     ; r3l: a  \n"
	
	"0:\n"
	"    mov.b @r1+,r3h      ; r3h = *data++ \n"
	"    add.b r3h,r3l       ; a += r3h      \n"
	"    mov.b r3h,@r0       ; *dest++ = r3h \n"
	"    adds  #1,r0 \n"
	"    cmp.w r0,r2 \n"
	"    bne   0b \n"
	
	"    sub.w r0,r0 \n"
	"    mov.b r3l,r0l \n"
	"    rts \n"
	);
#endif

//! send a LNP integrity layer packet of given length
/*! \return 0 on success.
*/
int lnp_integrity_write(const unsigned char *data,unsigned char length) {
  int r;
#if defined(CONF_MM)
  char* buffer_ptr = malloc(length+3);
#else // CONF_MM
  char* buffer_ptr = lnp_buffer;
#endif // CONF_MM
  unsigned char c = lnp_checksum_copy( buffer_ptr+2, data, length);
  lnp_checksum_step( c, buffer_ptr[0]=0xf0 );
  lnp_checksum_step( c, buffer_ptr[1]=length );
  buffer_ptr[length+2] = c;
  r = lnp_logical_write(buffer_ptr,length+3);
#if defined(CONF_MM)
  free(buffer_ptr); 
#endif // CONF_MM
  return r;
}

//! send a LNP addressing layer packet of given length
/*! \return 0 on success.
*/
int lnp_addressing_write(const unsigned char *data,unsigned char length,
                         unsigned char dest,unsigned char srcport) {
  int r;
#if defined(CONF_MM)
  char* buffer_ptr = malloc(length+5);
#else // CONF_MM
  char* buffer_ptr = lnp_buffer;
#endif // CONF_MM
  unsigned char c = lnp_checksum_copy( buffer_ptr+4, data, length );
  lnp_checksum_step( c, buffer_ptr[0]=0xf1 );
  lnp_checksum_step( c, buffer_ptr[1]=length+2 );
  lnp_checksum_step( c, buffer_ptr[2]=dest );
  lnp_checksum_step( c, buffer_ptr[3]=
                   (lnp_hostaddr | (srcport & LNP_PORTMASK)) );
  buffer_ptr[length+4] = c;
  r = lnp_logical_write(buffer_ptr,length+5);
#if defined(CONF_MM)
  free(buffer_ptr); 
#endif // CONF_MM
  return r;
}

//! handle LNP packet from the integrity layer
void lnp_receive_packet(const unsigned char *data) {
  unsigned char header=*(data++);
  unsigned char length=*(data++);
  lnp_integrity_handler_t intgh;
  lnp_addressing_handler_t addrh;

  // only handle non-degenerate packets in boot protocol 0xf0
  //
  switch(header) {
    case 0xf0:        // raw integrity layer packet, no addressing.
      intgh = lnp_integrity_handler;
      if(intgh) {
#ifdef CONF_AUTOSHUTOFF
        shutoff_restart();
#endif
        intgh(data,length);
      }
      break;

    case 0xf1:        // addressing layer.
      if(length>2) {
        unsigned char dest=*(data++);

        if(lnp_hostaddr == (dest & LNP_HOSTMASK)) {
          unsigned char port=dest & LNP_PORTMASK;
          addrh = lnp_addressing_handler[port];
          if(addrh) {
            unsigned char src=*(data++);
#ifdef CONF_AUTOSHUTOFF
            shutoff_restart();
#endif
            addrh(data,length-2,src);
          }
        }
      }

  } // switch(header)
}

//! receive a byte, decoding LNP packets with a state machine.
void lnp_integrity_byte(unsigned char b) {
  static unsigned char buffer[256+3];
  static int bytesRead,endOfData;
  static unsigned char chk;

  if(lnp_integrity_state==LNPwaitHeader)
    bytesRead=0;

  buffer[bytesRead++]=b;

  switch(lnp_integrity_state) {
    case LNPwaitHeader:
      // valid headers are 0xf0 .. 0xf7
      //
      if(((b & 0xf8) == 0xf0) || (b == 0x55)) {
#ifdef CONF_VIS
        if (lnp_logical_range_is_far()) {
          dlcd_show(LCD_IR_UPPER);
          dlcd_show(LCD_IR_LOWER);
        } else {
          dlcd_hide(LCD_IR_UPPER);
          dlcd_show(LCD_IR_LOWER);
        }
#ifndef CONF_LCD_REFRESH
        lcd_refresh();
#endif
#endif
        // Init checksum
        lnp_checksum_init( chk );
    
        // switch on protocol header
        if (b == 0x55) {
#if defined(CONF_RCX_PROTOCOL) || defined(CONF_RCX_MESSAGE)
          // 0x55 is header for standard firmware message
          lnp_integrity_state = LNPwaitRMH1;
#else
          lnp_integrity_reset();
#endif
        } else {
          lnp_integrity_state++;
        }
      }
      break;

    case LNPwaitLength:
      endOfData=b+2;
      lnp_integrity_state++;
      break;

    case LNPwaitData:
      if(bytesRead==endOfData)
  lnp_integrity_state++;
      break;

    case LNPwaitCRC:
      if(b==chk)
  lnp_receive_packet(buffer);
      lnp_integrity_reset();
    break;

#if defined(CONF_RCX_PROTOCOL) || defined (CONF_RCX_MESSAGE)
  // state machine to handle remote
    case LNPwaitRMH1:
    case LNPwaitRMH2:
      // waiting for header bytes
      if ( b == lnp_rcx_header[ lnp_integrity_state-LNPwaitRMH1 ] )
        lnp_integrity_state++;
      else
        lnp_integrity_reset();
      break;

    case LNPwaitRMH3:
    case LNPwaitRMH4:
      if ( b == lnp_rcx_remote_op[ lnp_integrity_state-LNPwaitRMH3 ] )
        lnp_integrity_state++;
#if defined(CONF_RCX_MESSAGE)
      else if ( b == lnp_rcx_msg_op[ lnp_integrity_state-LNPwaitRMH3 ] )
        lnp_integrity_state = LNPwaitMH4;
#endif
      else
        lnp_integrity_reset();
      break;

    case LNPwaitRB0:
      lnp_rcx_temp0 = b;
      lnp_integrity_state++;
      break;

    case LNPwaitRB0I:
      if ( (unsigned char)~b == lnp_rcx_temp0 )
        lnp_integrity_state++;
      else
        lnp_integrity_reset();
      break;

    case LNPwaitRB1:
      lnp_rcx_temp1 = b;
      lnp_integrity_state++;
      break;

    case LNPwaitRB1I:
      if ( (unsigned char)~b == lnp_rcx_temp1 )
        lnp_integrity_state++;
      else
        lnp_integrity_reset();
      break;

    case LNPwaitRC:
      lnp_rcx_checksum = 0xd2 + lnp_rcx_temp0 + lnp_rcx_temp1;
      if ( b == lnp_rcx_checksum )
        lnp_integrity_state++;
      else
        lnp_integrity_reset();
      break;

    case LNPwaitRCI:
      // if checksum valid and remote handler has been installed, call remote handler
    if ( b == (unsigned char)~lnp_rcx_checksum) {
#if defined(CONF_RCX_MESSAGE)
     // if a message, set message number and exit
     if (lnp_rcx_temp1 & 0x07)
     {
        lnp_rcx_message = (lnp_rcx_temp1 > 2) ? 3 : lnp_rcx_temp1;
     } 
     else
#endif
     {
        // Invoke remote handler if any
        lnp_remote_handler_t rmth = lnp_remote_handler;
        if (rmth)
          rmth( (lnp_rcx_temp0<<8)+lnp_rcx_temp1 );
        }
      }
      // reset state machine when done
      lnp_integrity_reset();
      break;
#endif

#if defined(CONF_RCX_MESSAGE)
    // state machine to handle RCX protocol messages
    case LNPwaitMH3:
    case LNPwaitMH4:
      if ( b == lnp_rcx_msg_op[ lnp_integrity_state-LNPwaitMH3 ] )
        lnp_integrity_state++;
      else
        lnp_integrity_reset();
      break;

    case LNPwaitMN:
      lnp_rcx_temp0 = b;
      lnp_integrity_state++;
      break;

    case LNPwaitMNC:
      if ( (unsigned char)~b == lnp_rcx_temp0 )
        lnp_integrity_state++;
      else
        lnp_integrity_reset();
      break;

    case LNPwaitMC:
      lnp_rcx_temp1 = 0xf7 + lnp_rcx_temp0;

      if (b == lnp_rcx_temp1)
         lnp_integrity_state++;
      else
        lnp_integrity_reset();
      break;

    case LNPwaitMCC:
      // set message variable if it is valid message
      if ( (unsigned char)~b == lnp_rcx_temp1 )
        lnp_rcx_message = lnp_rcx_temp0;
      // reset state machine
      lnp_integrity_reset();
      break;
#endif
  }
  // Accumulate checksum
  lnp_checksum_step( chk, b );
}

//! reset the integrity layer on error or timeout.
#if defined(CONF_RCX_COMPILER) || defined(CONF_HOST)
void lnp_integrity_reset(void) {
#else
HANDLER_WRAPPER("lnp_integrity_reset","lnp_integrity_reset_core");
void lnp_integrity_reset_core(void) {
#endif
#ifndef CONF_HOST
  if(tx_state>TX_IDLE) {
    txend_handler();
    tx_state=TX_COLL;
  } else
#endif
  if(lnp_integrity_state!=LNPwaitHeader) {
    lnp_integrity_state=LNPwaitHeader;

#ifdef CONF_VIS
    dlcd_hide(LCD_IR_LOWER);
    dlcd_hide(LCD_IR_UPPER);
#ifndef CONF_LCD_REFRESH
    lcd_refresh();
#endif
#endif
  }
}

//! return whether a packet is currently being received
/*! \return 1 if yes, else zero
*/
int lnp_integrity_active(void) {
  return lnp_integrity_state!=LNPwaitHeader;
}

//! reset the inter-byte timeout counter.
#if defined(CONF_RCX_COMPILER) || defined(CONF_HOST)
void lnp_timeout_reset(void) {
#else
HANDLER_WRAPPER("lnp_timeout_reset","lnp_timeout_reset_core");
void lnp_timeout_reset_core(void) {
#endif
  lnp_timeout_counter=lnp_timeout;
}

//! set the inter-byte timeout and reset the timeout counter to that value.
/*! \param  timeout the new timeout value
*/
void lnp_timeout_set(unsigned short timeout) {
  lnp_timeout_counter=lnp_timeout=timeout;
}

//! Initialise protocol handlers
/*! Adressing port 0 is reserved for the program handler.
*/
void lnp_init(void) {
  int k;
  
  for(k=1; k<=LNP_PORTMASK; k++)
    lnp_addressing_handler[k]=LNP_DUMMY_ADDRESSING;
  lnp_integrity_handler=LNP_DUMMY_INTEGRITY;

#if defined(CONF_RCX_PROTOCOL)
  lnp_remote_handler=LNP_DUMMY_REMOTE;
#endif
#if defined(CONF_RCX_MESSAGE)
  clear_msg();
#endif
}

#ifdef CONF_RCX_MESSAGE
wakeup_t msg_received(wakeup_t m) {
    return (m == 0 ? lnp_rcx_message != 0 : lnp_rcx_message == m);
}

//! send a standard firmware message
/*! \return 0 on success.
 */
int send_msg(unsigned char msg)
{
  int r;
#if defined(CONF_MM)
  char* buffer_ptr = malloc(9);
#else // CONF_MM
  char* buffer_ptr = lnp_buffer;
#endif // CONF_MM
  buffer_ptr[0]=0x55;
  buffer_ptr[1]=0xff;
  buffer_ptr[2]=0x00;
  buffer_ptr[3]=0xf7;
  buffer_ptr[4]=0x08;
  buffer_ptr[5]=msg;
  buffer_ptr[6]=(unsigned char) (0xff-msg);
  buffer_ptr[7]=(unsigned char) (0xf7+msg);
  buffer_ptr[8]=(unsigned char) (0x08-msg);
  r = lnp_logical_write(buffer_ptr,9);
#if defined(CONF_MM)
  free(buffer_ptr); 
#endif // CONF_MM
  return r;
}
#endif

#endif // CONF_LNP