1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
3  * Author: gkalsi@google.com (Gurjant Kalsi)
4  *
5  * Use of this source code is governed by a MIT-style
6  * license that can be found in the LICENSE file or at
7  * https://opensource.org/licenses/MIT
8  */
9 
10 #include <app.h>
11 #include <assert.h>
12 #include <lk/err.h>
13 #include <dev/usb/class/cdcserial.h>
14 #include <kernel/thread.h>
15 
16 static uint8_t rxbuf[64];
17 static cdcserial_channel_t *cdc_chan;
18 
cdctest_setup(cdcserial_channel_t * chan)19 void cdctest_setup(cdcserial_channel_t *chan) {
20     cdc_chan = chan;
21 }
22 
cdctest_init(const struct app_descriptor * app)23 static void cdctest_init(const struct app_descriptor *app) {
24 }
25 
26 // Read bytes from CDC Serial and write them back to the stream.
cdctest_entry(const struct app_descriptor * app,void * args)27 static void cdctest_entry(const struct app_descriptor *app, void *args) {
28     assert(cdc_chan != NULL);
29     while (true) {
30         ssize_t bytes = cdcserial_read(cdc_chan, sizeof(rxbuf), rxbuf);
31         if (bytes == ERR_NOT_READY) {
32             // USB is not ready yet.
33             thread_sleep(100);
34             continue;
35         } else if (bytes < 0) {
36             printf("Error reading bytes from CDC Serial: %ld\n", bytes);
37             break;
38         }
39 
40         cdcserial_write(cdc_chan, bytes, rxbuf);
41     }
42 }
43 
44 APP_START(usbtest)
45 .init = cdctest_init,
46 .entry = cdctest_entry,
47 APP_END
48