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