2222
2323extern const constants HW ;
2424
25+ const char * ERR_OK = "OK\n" ;
26+ const char * ERR_BAD_REQUEST = "ERROR 1000 bad request\n" ;
27+ const char * ERR_INVALID_CARD = "ERROR 1001 invalid card\n" ;
28+ const char * ERR_WRITE = "ERROR 1002 write failed\n" ;
29+
2530typedef struct line {
2631 char bytes [64 ];
2732 int ix ;
@@ -77,11 +82,13 @@ void on_uart1_rx();
7782bool on_uart_monitor (repeating_timer_t * );
7883void uart0_rxchar (uint8_t );
7984void uart1_rxchar (uint8_t );
80- void uart_rxchar (uint8_t , line * );
81- void uart_exec (char * );
85+ void uart_flush (uint8_t );
86+ void uart_rxchar (const uart_inst_t * , uint8_t , line * );
87+ void uart_exec (const uart_inst_t * , char * );
88+ void uart_write (const uart_inst_t * , const char * );
8289
83- void uart_swipe (char * msg );
84- void uart_keypad (char * msg );
90+ void uart_swipe (const uart_inst_t * uart , char * msg );
91+ void uart_keypad (const uart_inst_t * uart , char * msg );
8592
8693void UART_init () {
8794 // ... uart0
@@ -173,23 +180,32 @@ bool on_uart_monitor(repeating_timer_t *rt) {
173180}
174181
175182void UART_rx (uart_inst_t * uart , buffer * b ) {
176- buffer_flush (b , uart0_rxchar );
183+ if (uart == uart0 ) {
184+ buffer_flush (b , uart0_rxchar );
185+ } else if (uart == uart1 ) {
186+ buffer_flush (b , uart1_rxchar );
187+ } else {
188+ buffer_flush (b , uart_flush );
189+ }
177190}
178191
179192void uart0_rxchar (uint8_t ch ) {
180- uart_rxchar (ch , & UART .UART0 .line );
193+ uart_rxchar (uart0 , ch , & UART .UART0 .line );
181194}
182195
183196void uart1_rxchar (uint8_t ch ) {
184- uart_rxchar (ch , & UART .UART1 .line );
197+ uart_rxchar (uart1 , ch , & UART .UART1 .line );
198+ }
199+
200+ void uart_flush (uint8_t ch ) {
185201}
186202
187- void uart_rxchar (uint8_t ch , line * line ) {
203+ void uart_rxchar (const uart_inst_t * uart , uint8_t ch , line * line ) {
188204 switch (ch ) {
189205 case CR :
190206 case LF :
191207 if (line -> ix > 0 ) {
192- uart_exec (line -> bytes );
208+ uart_exec (uart , line -> bytes );
193209 }
194210
195211 memset (line -> bytes , 0 , sizeof (line -> bytes ));
@@ -204,26 +220,27 @@ void uart_rxchar(uint8_t ch, line *line) {
204220 }
205221}
206222
207- void uart_exec (char * msg ) {
223+ void uart_exec (const uart_inst_t * uart , char * msg ) {
208224 char s [64 ];
209225
210226 snprintf (s , sizeof (s ), "%s" , msg );
211227 debugf (LOGTAG , s );
212228
213229 if (strncasecmp (msg , "swipe " , 6 ) == 0 ) {
214- uart_swipe (& msg [6 ]);
230+ uart_swipe (uart , & msg [6 ]);
215231 } else if (strncasecmp (msg , "code " , 5 ) == 0 ) {
216- uart_keypad (& msg [5 ]);
232+ uart_keypad (uart , & msg [5 ]);
217233 }
218234}
219235
220- void uart_swipe (char * msg ) {
236+ void uart_swipe (const uart_inst_t * uart , char * msg ) {
221237 char * token = strtok (msg , " ," );
222238
223239 if (token != NULL ) {
224240 uint32_t u32 ;
225241
226242 if (sscanf (msg , "%u" , & u32 ) < 1 ) {
243+ uart_write (uart , ERR_BAD_REQUEST );
227244 return ;
228245 }
229246
@@ -232,10 +249,12 @@ void uart_swipe(char *msg) {
232249 char * code ;
233250
234251 if (facility_code < 1 || facility_code > 255 || card > 65535 ) {
252+ uart_write (uart , ERR_BAD_REQUEST );
235253 return ;
236254 }
237255
238256 if (!write_card (facility_code , card )) {
257+ uart_write (uart , ERR_WRITE );
239258 debugf (LOGTAG , "card %u%05u error" , facility_code , card );
240259 return ;
241260 }
@@ -244,27 +263,39 @@ void uart_swipe(char *msg) {
244263 int N = strlen (code );
245264 for (int i = 0 ; i < N ; i ++ ) {
246265 if (!write_keycode (code [i ])) {
266+ uart_write (uart , ERR_WRITE );
247267 debugf (LOGTAG , "keycode %c error" , code [i ]);
248268 return ;
249269 }
250270 }
251271 }
272+
273+ uart_write (uart , ERR_OK );
252274 }
253275}
254276
255- void uart_keypad (char * msg ) {
277+ void uart_keypad (const uart_inst_t * uart , char * msg ) {
256278 int N = strlen (msg );
257279
258- for (int i = 0 ; i < N ; i ++ ) {
259- if (!write_keycode (msg [i ])) {
260- debugf (LOGTAG , "keycode %c error" , msg [i ]);
261- return ;
280+ if (N > 0 ) {
281+ for (int i = 0 ; i < N ; i ++ ) {
282+ if (!write_keycode (msg [i ])) {
283+ uart_write (uart , ERR_WRITE );
284+ debugf (LOGTAG , "keycode %c error" , msg [i ]);
285+ return ;
286+ }
262287 }
288+
289+ uart_write (uart , ERR_OK );
263290 }
264291}
265292
266- // bool uart0_write(const uint8_t *bytes, int N) {
267- // uart_write_blocking(uart0, bytes, N);
268- //
269- // return true;
270- // }
293+ void uart_write (const uart_inst_t * uart , const char * msg ) {
294+ int N = strlen (msg );
295+
296+ if (uart == uart0 ) {
297+ uart_write_blocking (uart0 , msg , N );
298+ } else if (uart == uart1 ) {
299+ uart_write_blocking (uart1 , msg , N );
300+ }
301+ }
0 commit comments