|
1 | | -from amaranth import Module |
| 1 | +from amaranth import Module, Const |
2 | 2 | from amaranth.lib.wiring import In, Out, Component, connect |
3 | 3 |
|
4 | 4 | from printer import Printer |
| 5 | +from stream_mux import StreamMux |
| 6 | +from stream_demux import StreamDemux |
| 7 | +from string_match import StringMatch |
5 | 8 |
|
6 | 9 | import session |
7 | 10 |
|
@@ -35,45 +38,108 @@ class SimpleLedHttp(Component): |
35 | 38 | def elaborate(self, _platform): |
36 | 39 | m = Module() |
37 | 40 |
|
38 | | - response = "\r\n".join( |
| 41 | + ## Input parsers |
| 42 | + parser_demux = m.submodules.parser_demux = StreamDemux(mux_width=2, stream_width=8) |
| 43 | + connect(m, self.session.inbound.data, parser_demux.input) |
| 44 | + |
| 45 | + # Match the header for an HTTP/1.0 request to the LED path. |
| 46 | + # TODO: If we want to match more than one path, could probably have some common |
| 47 | + # matching for the method and protocol. Also, if we want to get out of the |
| 48 | + # stone age, this could be HTTP/1.1. |
| 49 | + led_header = "POST /led HTTP/1.0\r\n" |
| 50 | + led_header_matcher = m.submodules.led_header_matcher = StringMatch(led_header) |
| 51 | + HEADER_PARSER_LED = 0 |
| 52 | + connect(m, led_header_matcher.input, parser_demux.outs[HEADER_PARSER_LED]) |
| 53 | + |
| 54 | + # Last parser is just a sink |
| 55 | + HEADER_PARSER_SINK = 1 |
| 56 | + m.d.comb += parser_demux.outs[HEADER_PARSER_SINK].ready.eq(1) |
| 57 | + |
| 58 | + ## Responders |
| 59 | + response_mux = m.submodules.response_mux = StreamMux(mux_width=2, stream_width=8) |
| 60 | + connect(m, response_mux.out, self.session.outbound.data) |
| 61 | + |
| 62 | + ok_response = "\r\n".join( |
39 | 63 | ["HTTP/1.0 200 OK", |
40 | 64 | "Host: Fomu", |
41 | 65 | "Content-Type: text/plain; charset=utf-8", |
42 | 66 | "", |
43 | 67 | "", |
44 | 68 | '👍']) + "\r\n" |
| 69 | + ok_response = ok_response.encode("utf-8") |
| 70 | + ok_printer = m.submodules.ok_printer = Printer(ok_response) |
| 71 | + RESPONSE_OK = 0 |
| 72 | + connect(m, ok_printer.output, response_mux.input[RESPONSE_OK]) |
45 | 73 |
|
46 | | - response = response.encode("utf-8") |
47 | | - |
48 | | - printer = m.submodules.printer = Printer(response) |
49 | | - |
50 | | - connect(m, printer.output, self.session.outbound.data) |
| 74 | + not_found_response = "\r\n".join( |
| 75 | + ["HTTP/1.0 404 Not Found", |
| 76 | + "Host: Fomu", |
| 77 | + "Content-Type: text/plain; charset=utf-8", |
| 78 | + "", |
| 79 | + "", |
| 80 | + '👎']) + "\r\n" |
| 81 | + not_found_response = not_found_response.encode("utf-8") |
| 82 | + not_found_printer = m.submodules.not_found_printer = Printer(not_found_response) |
| 83 | + RESPONSE_404 = 1 |
| 84 | + connect(m, not_found_printer.output, response_mux.input[RESPONSE_404]) |
51 | 85 |
|
52 | 86 | with m.FSM(): |
| 87 | + with m.State("reset"): |
| 88 | + m.d.comb += [ |
| 89 | + led_header_matcher.reset.eq(1) |
| 90 | + ] |
| 91 | + m.next = "idle" |
53 | 92 | with m.State("idle"): |
| 93 | + m.d.comb += led_header_matcher.reset.eq(0) |
| 94 | + m.d.sync += parser_demux.select.eq(HEADER_PARSER_LED) |
| 95 | + m.d.sync += response_mux.select.eq(RESPONSE_OK) |
54 | 96 | m.next = "idle" |
55 | 97 | with m.If(self.session.inbound.active): |
56 | | - m.next = "parsing" |
| 98 | + m.next = "parsing_start" |
57 | 99 | m.d.sync += self.session.outbound.active.eq(1) |
58 | | - with m.State("parsing"): |
| 100 | + with m.State("parsing_start"): |
59 | 101 | m.d.sync += self.session.outbound.active.eq(1) |
60 | | - m.next = "parsing" |
| 102 | + m.next = "parsing_start" |
| 103 | + m.d.sync += parser_demux.select.eq(HEADER_PARSER_LED) |
| 104 | + # Input finished before header matched, or header failed to match |
| 105 | + with m.If(~self.session.inbound.active | led_header_matcher.rejected): |
| 106 | + m.next = "writing" |
| 107 | + m.d.sync += [ |
| 108 | + response_mux.select.eq(RESPONSE_404), |
| 109 | + parser_demux.select.eq(HEADER_PARSER_SINK), |
| 110 | + not_found_printer.en.eq(1), |
| 111 | + ] |
| 112 | + # header matched successfully |
| 113 | + with m.If(led_header_matcher.accepted): |
| 114 | + m.next = "parsing_remainder" |
| 115 | + m.d.sync += parser_demux.select.eq(HEADER_PARSER_SINK) |
| 116 | + with m.State("parsing_remainder"): # TODO: #3 - Parse headers + body. |
61 | 117 | # Consume inbound data (drop it on the floor) |
62 | | - m.d.comb += self.session.inbound.data.ready.eq(1) |
| 118 | + m.next = "parsing_remainder" |
63 | 119 | with m.If(~self.session.inbound.active): |
64 | 120 | # All the input is done. |
65 | | - # TODO: #3 -- we should make this state transition |
66 | | - # when we have completed reading the request |
67 | | - # OR if the inbound session becomes inactive. |
68 | 121 | m.next = "writing" |
69 | | - m.d.sync += printer.en.eq(1) # one-shot |
| 122 | + m.d.sync += [ |
| 123 | + response_mux.select.eq(RESPONSE_OK), |
| 124 | + ok_printer.en.eq(1), |
| 125 | + ] |
70 | 126 | with m.State("writing"): |
71 | | - m.d.sync += printer.en.eq(0) |
72 | | - m.d.sync += self.session.outbound.active.eq(1) |
| 127 | + m.d.sync += [ |
| 128 | + ok_printer.en.eq(0), |
| 129 | + not_found_printer.en.eq(0), |
| 130 | + self.session.outbound.active.eq(1), |
| 131 | + ] |
73 | 132 | m.next = "writing" |
74 | | - with m.If(printer.done): |
75 | | - m.next = "idle" |
| 133 | + with m.If( ((response_mux.select == RESPONSE_OK) & ok_printer.done) |
| 134 | + | ((response_mux.select == RESPONSE_404) & not_found_printer.done) |
| 135 | + ): |
76 | 136 | m.d.sync += self.session.outbound.active.eq(0) |
| 137 | + # Can finish writing before all the input is collected, |
| 138 | + # since a bad request migh trigger an early 404. Wait |
| 139 | + # until the input is done before returning to the reset |
| 140 | + # state. |
| 141 | + with m.If(~self.session.inbound.active): |
| 142 | + m.next = "reset" |
77 | 143 |
|
78 | 144 | # TODO: #3 - Implement for real. Currently has a sync block so simple_led_http_test |
79 | 145 | # will elaborate. |
|
0 commit comments