File: program.c

package info (click to toggle)
brickos 0.9.0.dfsg-16
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,076 kB
  • sloc: ansic: 9,139; cpp: 860; asm: 693; makefile: 654; sh: 134; perl: 61
file content (666 lines) | stat: -rw-r--r-- 16,447 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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*! \file program.c
    \brief  Implementation: program data structures and functions
    \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>
 *                  Frank Cremer <frank@demon.holly.nl>
 */

/*
 *  2000.05.01 - Paolo Masetti <paolo.masetti@itlug.org>
 *
 *  - Added "CMDirmode" for changing via LNP IR mode
 *
 *  2001.05.10 - Matt Ahrens <mahrens@acm.org>
 *
 *    - Added free memory and batter life display
 *      Press "view" repeatedly while no programs are running to see
 *
 *  2002.4.23 - Ted Hess <thess@kitschensync.net>
 *
 *  - Added Remote key handler
 */

#include <sys/program.h>

#ifdef CONF_PROGRAM

#include <sys/tm.h>
#include <string.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdlib.h>

#include <sys/lnp.h>
#include <sys/lnp-logical.h>
#include <sys/dmotor.h>
#include <sys/dsensor.h>
#include <sys/mm.h>
#include <sys/battery.h>
#include <dsound.h>
#include <remote.h>

#include <conio.h>

///////////////////////////////////////////////////////////////////////////////
//
// Global Variables
//
///////////////////////////////////////////////////////////////////////////////

volatile unsigned cprog;                //!< the current program

///////////////////////////////////////////////////////////////////////////////
//
// Internal Variables
//
///////////////////////////////////////////////////////////////////////////////

const unsigned char min_length[]={
   1, // CMDacknowledge
   2, // CMDdelete
  13, // CMDcreate
   8, // CMDoffsets
   4, // CMDdata
   2, // CMDrun
   2, // CMDirmode
   2  // CMDsethost
};

static program_t programs[PROG_MAX];      //!< the programs

static unsigned char* buffer_ptr;         //!< packet receive buffer
volatile unsigned char packet_len;        //!< packet length
volatile unsigned char packet_src;        //!< packet sender

static sem_t packet_sem;                  //!< synchronization semaphore

#if 0
#define debugs(a) { cputs(a); msleep(500); }
#define debugw(a) { cputw(a); msleep(500); }
#else
#define debugs(a)
#define debugw(a)
#endif

// Forward ref
int lrkey_handler(unsigned int etype, unsigned int key);

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

//! check if a given program is valid.
/*! \return 0 if invalid */
int program_valid(unsigned nr) {
  program_t *prog=programs+nr;

  return (nr < PROG_MAX) &&
         (prog->text_size>0) &&
         (prog->text_size+prog->data_size==prog->downloaded);
}

//! run the given program
static void program_run(unsigned nr) {
  if(program_valid(nr)) {
    program_t *prog=programs+nr;

    // initialize data segments
    //
    memcpy(prog->data,prog->data_orig,prog->data_size);
    memset(prog->bss,0,prog->bss_size);

    execi((void*) (((char*)prog->text)
            + prog->start  ),
    0,0,prog->prio,prog->stack_size);
  }
}

//! packet handler, called from interrupt
/*! allocates buffer, copies data and wakes parser task.
*/
static void packet_producer(const unsigned char *data,
                                unsigned char length,
                                unsigned char src) {
  // old packet still unhandled or empty packet?
  //
  if(packet_len>0 || length==0)
    return;

  if (buffer_ptr != 0)
    return;

  buffer_ptr = malloc(length);
  memcpy(buffer_ptr,data,length);
  packet_len=length;
  packet_src=src;
  sem_post(&packet_sem);
}

//! packet command parser task
static int packet_consumer(int argc, char *argv[]) {
  packet_cmd_t cmd;
  unsigned char nr=0;
  program_t *prog=programs; // to avoid a silly warning
  const static unsigned char acknowledge=CMDacknowledge;
  char msg[8];

  while (!shutdown_requested()) {
    // wait for new packet
    //
    packet_len=0;
    free(buffer_ptr);
    buffer_ptr = 0;
    if (sem_wait(&packet_sem) != -1) {
      if (buffer_ptr == 0)
        continue;

      debugw(*(size_t*)buffer_ptr);

      // handle trivial errors
      //
      cmd=buffer_ptr[0];
      if (cmd>=CMDlast || packet_len<min_length[cmd])
        continue;

      // handle IR CMDs
      if (cmd==CMDirmode) {
        if (buffer_ptr[1]==0) {
          debugs("nearmodeIR");
          lnp_logical_range(0);
          debugs("OK");
          lnp_addressing_write(&acknowledge,1,packet_src,0);
        } else {
          debugs("farmodeIR");
          lnp_logical_range(1);
          debugs("OK");
          lnp_addressing_write(&acknowledge,1,packet_src,0);
        }
        continue;
      }

      // Is this a request to change host address
      if (cmd == CMDsethost) {
        // ACK before we change our address
        lnp_addressing_write(&acknowledge,1,packet_src,0);
        lnp_set_hostaddr(buffer_ptr[1]);
        continue;
      }
  
      // Get program number, validate value
      if((cmd > CMDacknowledge) && (cmd <= CMDrun)) {
        nr = buffer_ptr[1];
        if(nr > PROG_MAX)
          continue;
#ifndef CONF_VIS
        cputc_hex_0(nr+1);
#endif
      }

      debugw(nr);
      prog = programs+nr;

      switch( cmd ) {
      case CMDdelete:
        debugs("dele");

        if(nb_tasks <= nb_system_tasks) {
          if(prog->text)
            free(prog->text);
          memset(prog,0,sizeof(program_t));

#ifndef CONF_VIS
          if(nr == cprog)
            cputc_0('-');
#endif
          debugs("OK");
  
          lnp_addressing_write(&acknowledge,1,packet_src,0);
        }
        break;

      case CMDcreate:
        debugs("crea");
        if(!prog->text) {
          memcpy(&(prog->text_size),buffer_ptr+2,11);

          if((prog->text=malloc(prog->text_size+
                2*prog->data_size+
                prog->bss_size  ))) {
            prog->data=prog->text+prog->text_size;
            prog->bss=prog->data+prog->data_size;
            prog->data_orig=prog->bss +prog->bss_size;
            prog->downloaded=0;

            debugs("OK");

            cputw(0);
            cprog = nr;

            msg[0]=CMDacknowledge;
            msg[1]=nr;
            memcpy(msg+2,prog,6);
            lnp_addressing_write(msg,8,packet_src,0);
          } else
            memset(prog,0,sizeof(program_t));
        }
        break;

      case CMDdata:
        debugs("data");
        if(prog->text && !program_valid(nr)) {
          size_t offset=*(size_t*)(buffer_ptr+2);
          if(offset<=prog->downloaded) {
            if(offset==prog->downloaded) {
              memcpy(prog->text+offset,buffer_ptr+4,packet_len-4);
              prog->downloaded+=packet_len-4;

              if(program_valid(nr)) {
                // copy original data segment and we're done.
                //
                memcpy(prog->data_orig,prog->data,prog->data_size);
                cls();
              } else
                cputw(prog->downloaded);
              debugs("OK");
            } else
              debugs("OLD");

            lnp_addressing_write(&acknowledge,1,packet_src,0);
          }
        }
        break;

      case CMDrun:
        debugs("run");
        if(program_valid(nr)) {
          cprog = nr;
          program_stop(0);
          program_run(nr);

          debugs("OK");
          lnp_addressing_write(&acknowledge,1,packet_src,0);
        }
        break;

      default:
        debugs("error");
      }
    }
  }
  free(buffer_ptr);
  return 0;
}

//! stop program
void program_stop(int flag) {
  int count_down = 40;

  // Kindly request that all user tasks shutdown
  shutdown_tasks(T_USER);
  // Wait a bit
  while (--count_down && (nb_tasks > nb_system_tasks)) {
    if (flag)
      cputs("STOP");
    msleep(100);
  }
    
  if (nb_tasks > nb_system_tasks) {
    // Wait no longer.
    if (flag) {
      cputs("KILL");
      // display immediately
      lcd_refresh();
    }
    killall(PRIO_HIGHEST-1);
  }

  // Reset motors, sensors, sound & LNP as
  // programs may have motors running,
  // sensors active or handlers set.
  //
  // Programs that exit on their own
  // are assumed to take the necessary
  // actions themselves.
  //
#ifdef CONF_DSOUND
  dsound_stop();
#endif
#ifdef CONF_DMOTOR
  dm_init();
#endif
#ifdef CONF_DSENSOR
  ds_init();
#endif
  lnp_init();
#ifdef CONF_LR_HANDLER
  // Reset remote button handler
  lr_init();
  lr_set_handler(lrkey_handler);
#endif
}

//! handle key input (on/off, run, program)
int key_handler(int argc, char *argv[]) {
  int c;

#ifndef CONF_VIS
  cputc_0('-');
#endif

  while (!shutdown_requested()) {
    int clear=0;
    c=getchar();

gotkey:

    debugs("key "); debugw(c);
    debugs("task"); debugw(nb_tasks);

    switch(c) {
      case KEY_ONOFF:
        cputs("OFF");
        // Kindly request all tasks shutdown
        shutdown_tasks(T_USER | T_KERNEL);
        // Except for key_handler
        ctid->tflags &= (~T_SHUTDOWN);
        // Wait a bit
        clear = 50;
        while (--clear && (nb_tasks > 2))
         msleep(100);
        // Wait no longer.
        if (nb_tasks > 2)
          killall(PRIO_HIGHEST);
        // Now key_handler should shutdown
        ctid->tflags |= T_SHUTDOWN;
        break;

      case KEY_RUN:
        // toggle: start/stop program
        if(nb_tasks > nb_system_tasks) {
          // if program running, stop it
          clear=1;
          program_stop(1);
        } else if(program_valid(cprog)) {
          program_stop(0);
          program_run(cprog);
        } else {
          cputs("NONE");
          clear=1;
        }
        break;

      case KEY_PRGM:
        // works only if no programs are running.
        if(nb_tasks <= nb_system_tasks) {
          int i;
          for(i=0; i<PROG_MAX; i++) {
            if( (++cprog)>=PROG_MAX)
              cprog=0;
            if(program_valid(cprog))
              break;
          }
          if(i==PROG_MAX) {
            cputs("NONE");
            clear=1;
#ifndef CONF_VIS
            cputc_0('-');
          }
          else
            cputc_hex_0(cprog+1);
#else
          }
#endif

        }
        break;
      case KEY_VIEW:
         // works only if no programs are running.
        if (nb_tasks > nb_system_tasks)
          break;
        /*
         * pressing the "view" button cycles through a display of the
         * amount of the amount of free memory (in decimal and
         * hexadecimal) and battery power. If a button other than "view"
         * is pressed while cycling through, we handle that button
         * ("goto gotkey").
         */
        cputs("addr");
        if ((c = getchar()) != KEY_VIEW) goto gotkey;
        cputw(lnp_hostaddr);
        while ((c = getchar()) == KEY_PRGM) {
          lnp_hostaddr += 0x10;
          lnp_hostaddr &= CONF_LNP_HOSTMASK;
          cputw(lnp_hostaddr);
        }
        if (c != KEY_VIEW) goto gotkey;
        if (program_valid(cprog)) {
          cputs("dele");
          if ((c = getchar()) != KEY_VIEW && c != KEY_PRGM) goto gotkey;
          if (c == KEY_PRGM) {
            program_t *prog=programs+cprog;
            if (prog->text)
              free(prog->text);
            memset(prog,0,sizeof(program_t));
            cputc_0('-');
          }
        }
        
        cputs("free");
        if ((c = getchar()) != KEY_VIEW) goto gotkey;
        lcd_int(mm_free_mem());
        if ((c = getchar()) != KEY_VIEW) goto gotkey;
        cputw(mm_free_mem());
        if ((c = getchar()) != KEY_VIEW) goto gotkey;

#if defined(CONF_DSENSOR)
        cputs("batt");
        if ((c = getchar()) != KEY_VIEW) goto gotkey;
        lcd_int(get_battery_mv());
        if ((c = getchar()) != KEY_VIEW) goto gotkey;
#endif // CONF_DSENSOR

        clear=1;
        break;
    }

    if(clear) {
      wait_event(dkey_released,KEY_ANY);
      cls();
    }
  }
  return 0;
}

#if defined(CONF_LR_HANDLER)
//! handle remote key input (P1-P5, A1-C1, A2-C2, stop, beep)
int lrkey_handler(unsigned int etype, unsigned int key) {
  unsigned char pnr = 0;

  // If a program is running, stop it
  //  NOTE: this LRKEY is allowed while a program is running!
  if(key == LRKEY_STOP && etype == LREVT_KEYON && nb_tasks > nb_system_tasks) {
    program_stop(1);
    return 1; // we consumed key
  }
  
  // Only interested if no program is running
  if(nb_tasks <= nb_system_tasks) {
    // Keydown events dispatched here
    if (etype == LREVT_KEYON) {
      switch (key) {
#ifdef CONF_DSOUND
        case LRKEY_BEEP:
          // Need high pitched beep-beep
          dsound_system(0);
          break;
#endif // CONF_DSOUND
        
        case LRKEY_P5:
          pnr++;
          // ... Fallthru
        case LRKEY_P4:
          pnr++;
          // ... Fallthru
        case LRKEY_P3:
          pnr++;
          // ... Fallthru
        case LRKEY_P2:
          pnr++;
          // ... Fallthru
        case LRKEY_P1:
          // Start something?
          if(program_valid(pnr)) 
          {
            cprog = pnr;
            // Reset things
            program_stop(0);
#ifdef CONF_VIS
            cputc_hex_0(pnr+1);
#ifndef CONF_LCD_REFRESH
            lcd_refresh();
#endif
#endif
            // launch Program(n)
            program_run(pnr);
          } else {
            //  no such program downloaded
           cputs("NONE");
          }
          break;

#if defined(CONF_DMOTOR)        
        // Motor on commands
        case LRKEY_A1:
          // A Motor fwd
          motor_a_dir(fwd);
          break;
        case LRKEY_A2:
          // A Motor rev
          motor_a_dir(rev);
          break;
        case LRKEY_B1:
          // B Motor fwd
          motor_b_dir(fwd);
          break;
        case LRKEY_B2:
          // B Motor rev
          motor_b_dir(rev);
          break;
        case LRKEY_C1:
          // C Motor fwd
          motor_c_dir(fwd);
          break;
        case LRKEY_C2:
          // C Motor rev
          motor_c_dir(rev);
          break;
#endif // CONF_DMOTOR
        default:
          // Not consumed
          return 0;
      }
#ifndef CONF_LCD_REFRESH
      lcd_refresh();
#endif
      // Key consumed
      return 1;
    }
  
    // Keyup events dispatched here
    if (etype == LREVT_KEYOFF) {
      switch (key) {
#if defined(CONF_DMOTOR)
        case LRKEY_A1:
        case LRKEY_A2:
          // Shut off A motor
          motor_a_dir(brake);
          break;
        case LRKEY_B1:
        case LRKEY_B2:
          // Shut off B motor
          motor_b_dir(brake);
          break;
        case LRKEY_C1:
        case LRKEY_C2:
          // Shut off C motor
          motor_c_dir(brake);
          break;
#endif // CONF_DMOTOR
        case LRKEY_P1:
        case LRKEY_P2:
        case LRKEY_P3:
        case LRKEY_P4:
        case LRKEY_P5:
        case LRKEY_STOP:
          // remove the STOP (or NONE) message
          cls();
          break;
        default:
          return 0;
      }
      // Used the key
      return 1;
    }
  }

#ifndef CONF_LCD_REFRESH
  lcd_refresh();
#endif
  // Didn't eat the key
  return 0;
}
#endif

//! initialize program support
/*! run in single tasking mode
*/
void program_init() {
  packet_len=0;
  sem_init(&packet_sem,0,0);
  execi(&packet_consumer,0,0,PRIO_HIGHEST,DEFAULT_STACK_SIZE);
  execi(&key_handler,0,0,PRIO_HIGHEST,DEFAULT_STACK_SIZE);

#ifdef CONF_LR_HANDLER
  // Setup kernel remote callback handler and dispatch thread
  lr_startup();
  lr_set_handler(lrkey_handler);
#endif

  lnp_addressing_set_handler(0,&packet_producer);
  buffer_ptr = 0;
}

//! shutdown program support
/*! run in single tasking mode
*/
void program_shutdown() {
  lnp_addressing_set_handler(0,LNP_DUMMY_ADDRESSING);
  sem_destroy(&packet_sem);

#ifdef CONF_LR_HANDLER
  lr_shutdown();
#endif
}

#endif // CONF_PROGRAM