| [427854b] | 1 | // | 
|---|
| [083203b] | 2 | // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| [427854b] | 6 | // | 
|---|
|  | 7 | // devicedriver.cfa -- | 
|---|
|  | 8 | // | 
|---|
| [083203b] | 9 | // Author           : Peter A. Buhr | 
|---|
|  | 10 | // Created On       : Sat Mar 16 15:30:34 2019 | 
|---|
|  | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [3ac5fd8] | 12 | // Last Modified On : Thu Aug 15 18:45:45 2024 | 
|---|
|  | 13 | // Update Count     : 96 | 
|---|
| [427854b] | 14 | // | 
|---|
| [083203b] | 15 |  | 
|---|
|  | 16 | #include <fstream.hfa> | 
|---|
|  | 17 | #include <coroutine.hfa> | 
|---|
|  | 18 |  | 
|---|
|  | 19 | enum Status { CONT, MSG, ESTX, ELNTH, ECRC }; | 
|---|
| [93c86ac] | 20 |  | 
|---|
| [083203b] | 21 | coroutine Driver { | 
|---|
|  | 22 | Status status; | 
|---|
|  | 23 | char * msg, byte; | 
|---|
|  | 24 | }; // Driver | 
|---|
|  | 25 |  | 
|---|
| [93c86ac] | 26 | void ?{}( Driver & d, char * m ) { d.msg = m; }                 // constructor | 
|---|
|  | 27 |  | 
|---|
|  | 28 | Status next( Driver & d, char b ) with( d ) {                   // called by interrupt handler | 
|---|
|  | 29 | byte = b; resume( d ); return status;                           // resume coroutine at last suspend | 
|---|
| [083203b] | 30 | } // next | 
|---|
|  | 31 |  | 
|---|
| [7cfd6d4] | 32 | void checkCRC( Driver & d, unsigned int sum ) with( d ) { | 
|---|
| [427854b] | 33 | suspend; | 
|---|
| [7cfd6d4] | 34 | unsigned short int crc = byte << 8;                                     // sign extension over written | 
|---|
| [427854b] | 35 | suspend; | 
|---|
| [7cfd6d4] | 36 | // prevent sign extension for signed char | 
|---|
|  | 37 | status = (crc | (unsigned char)byte) == sum ? MSG : ECRC; | 
|---|
|  | 38 | } // checkCRC | 
|---|
|  | 39 |  | 
|---|
| [083203b] | 40 | void main( Driver & d ) with( d ) { | 
|---|
|  | 41 | enum { STX = '\002', ESC = '\033', ETX = '\003', MaxMsg = 64 }; | 
|---|
|  | 42 | msg: for () {                                                                                 // parse message | 
|---|
|  | 43 | status = CONT; | 
|---|
|  | 44 | unsigned int lnth = 0, sum = 0; | 
|---|
| [427854b] | 45 | while ( byte != STX ) suspend; | 
|---|
| [083203b] | 46 | emsg: for () { | 
|---|
| [427854b] | 47 | suspend; | 
|---|
| [083203b] | 48 | choose ( byte ) {                                                       // process byte | 
|---|
|  | 49 | case STX: | 
|---|
| [427854b] | 50 | status = ESTX; suspend; continue msg; | 
|---|
| [083203b] | 51 | case ETX: | 
|---|
|  | 52 | break emsg; | 
|---|
|  | 53 | case ESC: | 
|---|
| [427854b] | 54 | suspend; | 
|---|
| [083203b] | 55 | } // choose | 
|---|
|  | 56 | if ( lnth >= MaxMsg ) {                                         // buffer full ? | 
|---|
| [427854b] | 57 | status = ELNTH; suspend; continue msg; | 
|---|
| [083203b] | 58 | } // if | 
|---|
|  | 59 | msg[lnth++] = byte; | 
|---|
|  | 60 | sum += byte; | 
|---|
|  | 61 | } // for | 
|---|
|  | 62 | msg[lnth] = '\0';                                                               // terminate string | 
|---|
| [7cfd6d4] | 63 | checkCRC( d, sum );                                                             // refactor CRC check | 
|---|
| [427854b] | 64 | suspend; | 
|---|
| [083203b] | 65 | } // for | 
|---|
|  | 66 | } // main | 
|---|
|  | 67 |  | 
|---|
|  | 68 | int main() { | 
|---|
|  | 69 | char msg[65], byte; | 
|---|
|  | 70 | Driver driver = { msg }; | 
|---|
| [e7fdd76] | 71 |  | 
|---|
|  | 72 | sin | nlOn;                                                                                     // read newline (all) characters | 
|---|
| [3ac5fd8] | 73 | try { | 
|---|
|  | 74 | for () {                                                                                // read until end of file | 
|---|
|  | 75 | sin | byte;                                                                     // read one character | 
|---|
|  | 76 | choose( next( driver, byte ) ) {                        // analyse character | 
|---|
|  | 77 | case CONT: ; | 
|---|
|  | 78 | case MSG: sout | "msg:" | msg; | 
|---|
|  | 79 | case ESTX: sout | "STX in message"; | 
|---|
|  | 80 | case ELNTH: sout | "message too long"; | 
|---|
|  | 81 | case ECRC: sout | "CRC failure"; | 
|---|
|  | 82 | } // choose | 
|---|
|  | 83 | } // for | 
|---|
|  | 84 | } catch( end_of_file * ) { | 
|---|
|  | 85 | } // try | 
|---|
| [083203b] | 86 | } // main | 
|---|
|  | 87 |  | 
|---|
|  | 88 | // Local Variables: // | 
|---|
|  | 89 | // compile-command: "cfa -g -Wall -Wextra devicedriver.cfa" // | 
|---|
|  | 90 | // End: // | 
|---|