source: rtems-libbsd/testsuite/usbserial01/init.c @ 666a568

55-freebsd-126-freebsd-12
Last change on this file since 666a568 was 666a568, checked in by Sebastian Huber <sebastian.huber@…>, on 08/25/17 at 12:23:18

Include missing <string.h> and <limits.h>

Fix warnings.

Update #2132.
Update #2133.

  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*-
2 * COPYRIGHT (c) 2017 Kevin Kirspel
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/malloc.h>
28#include <assert.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <errno.h>
33#include <fcntl.h>
34
35#include <rtems/console.h>
36#include <rtems/shell.h>
37#include <rtems/bsd/bsd.h>
38
39#define TEST_NAME "LIBBSD USB SERIAL"
40
41#define USB_SERIAL_TEST_BUFFER_SIZE 48
42#define PRIO_OPEN  (RTEMS_MAXIMUM_PRIORITY - 12)
43#define PRIO_READ  (RTEMS_MAXIMUM_PRIORITY - 11)
44#define PRIO_WRITE (RTEMS_MAXIMUM_PRIORITY - 10)
45
46struct usb_test_message {
47        int fd;
48        char rbuf[USB_SERIAL_TEST_BUFFER_SIZE];
49        char wbuf[USB_SERIAL_TEST_BUFFER_SIZE];
50};
51
52static rtems_id oid, rid, wid, omid, rmid, wmid;
53static volatile bool kill_otask, kill_rtask, kill_wtask;
54static volatile bool otask_active, rtask_active, wtask_active;
55
56static void
57usb_serial_read_task(rtems_task_argument arg)
58{
59        struct usb_test_message msg;
60        size_t size;
61        uint32_t end_time;
62        int bytes, index;
63
64        rtask_active = true;
65        kill_rtask = false;
66        while (!kill_rtask) {
67                rtems_message_queue_receive(rmid, &msg, &size, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
68                index = 0;
69                msg.rbuf[0] = 0;
70                end_time = rtems_clock_get_ticks_since_boot() + RTEMS_MILLISECONDS_TO_TICKS(500);
71                while ((rtems_clock_get_ticks_since_boot() < end_time) && (index < sizeof(msg.rbuf))) {
72                        bytes = read(msg.fd, &msg.rbuf[index], sizeof(msg.rbuf) - index - 1);
73                        if (bytes < 0) {
74                                msg.fd = -1;
75                                rtems_message_queue_send(omid, &msg, sizeof(msg));
76                                printf("serial device read error: %d\n", errno);
77                        } else if (bytes > 0) {
78                                index += bytes;
79                                msg.rbuf[index] = 0;
80                                if (strcmp(msg.rbuf, msg.wbuf) == 0) {
81                                        break;
82                                }
83                        }
84                        rtems_task_wake_after(RTEMS_MILLISECONDS_TO_TICKS(10));
85                }
86                printf("serial device read: %s - %s\n", msg.rbuf, strcmp(msg.rbuf, msg.wbuf) == 0 ? "PASS" : "FAIL");
87        }
88        rtask_active = false;
89}
90
91static void
92usb_serial_write_task(rtems_task_argument arg)
93{
94        struct usb_test_message msg;
95        size_t size;
96        int bytes, write_len, count = 0;
97
98        wtask_active = true;
99        kill_wtask = false;
100        while (!kill_wtask) {
101                rtems_message_queue_receive(wmid, &msg, &size, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
102                count++;
103                sprintf(msg.wbuf, "Hello World %d", count);
104                write_len = strlen(msg.wbuf);
105                bytes = write(msg.fd, msg.wbuf, write_len);
106                if (bytes < 0) {
107                        msg.fd = -1;
108                        rtems_message_queue_send(omid, &msg, sizeof(msg));
109                        printf("serial device write error: %d\n", errno);
110                } else if (bytes != write_len) {
111                        printf("serial device failed to write all bytes: %d\n", bytes);
112                } else {
113                        printf("serial device write: %s\n", msg.wbuf);
114                        rtems_message_queue_send(rmid, &msg, sizeof(msg));
115                }
116        }
117        wtask_active = false;
118}
119
120static void
121usb_serial_open_task(rtems_task_argument arg)
122{
123        rtems_status_code sc;
124        struct usb_test_message msg;
125        struct termios t;
126        size_t size;
127        int fd, iret;
128
129        fd = -2;
130        otask_active = true;
131        kill_otask = false;
132        while (!kill_otask) {
133                sc = rtems_message_queue_receive(omid, &msg, &size, RTEMS_WAIT, RTEMS_MILLISECONDS_TO_TICKS(1000));
134                if (sc == RTEMS_SUCCESSFUL) {
135                        if (fd >= 0) {
136                                close(fd);
137                                printf("serial device closed\n");
138                        }
139                        fd = msg.fd;
140                }
141                if (fd == -1) {
142                        fd = open("/dev/ttyU0", LIBIO_FLAGS_READ_WRITE);
143                        if (fd != -1) {
144                                printf("serial device opened\n");
145                                //get terminal settings
146                                iret = tcgetattr (fd, &t);
147                                assert(iret != -1);
148                                //set timeouts to zero for non-blocking mode
149                                t.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
150                                //t.c_iflag &= ~(BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP);
151                                //t.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP);
152                                t.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
153                                t.c_iflag |= IXON;    // Enable XON/XOFF flow control on output.
154                                t.c_cflag &= ~(CSIZE | PARENB);
155                                t.c_oflag &= ~(OPOST);
156                                t.c_cflag |= (CS8 | CREAD);
157                                t.c_cc[VMIN] = 0;
158                                t.c_cc[VTIME] = 0;
159                                //save settings
160                                iret = tcsetattr (fd, TCSANOW, &t);
161                                assert(iret != -1);
162                                msg.fd = fd;
163                                rtems_message_queue_send(wmid, &msg, sizeof(msg));
164                        }
165                } else {
166                        msg.fd = fd;
167                        rtems_message_queue_send(wmid, &msg, sizeof(msg));
168                }
169        }
170        otask_active = false;
171}
172
173static void
174Init(rtems_task_argument arg)
175{
176        rtems_status_code sc;
177        struct usb_test_message msg;
178
179        (void) arg;
180        puts("*** " TEST_NAME " TEST ***");
181
182        sc = rtems_message_queue_create(
183                rtems_build_name ('M', 'U', 'O', 'P'),
184                16,
185                sizeof(struct usb_test_message),
186                RTEMS_PRIORITY,
187                &omid
188        );
189        assert(sc == RTEMS_SUCCESSFUL);
190
191        sc = rtems_message_queue_create(
192                rtems_build_name ('M', 'U', 'R', 'D'),
193                16,
194                sizeof(struct usb_test_message),
195                RTEMS_PRIORITY,
196                &rmid
197        );
198        assert(sc == RTEMS_SUCCESSFUL);
199
200        sc = rtems_message_queue_create(
201                rtems_build_name ('M', 'U', 'W', 'R'),
202                16,
203                sizeof(struct usb_test_message),
204                RTEMS_PRIORITY,
205                &wmid
206        );
207        assert(sc == RTEMS_SUCCESSFUL);
208
209        sc = rtems_task_create(
210                rtems_build_name('U', 'S', 'B', 'R'),
211                PRIO_READ,
212                RTEMS_MINIMUM_STACK_SIZE,
213                RTEMS_DEFAULT_MODES,
214                RTEMS_FLOATING_POINT,
215                &rid
216        );
217        assert(sc == RTEMS_SUCCESSFUL);
218
219        sc = rtems_task_create(
220                rtems_build_name('U', 'S', 'B', 'W'),
221                PRIO_WRITE,
222                RTEMS_MINIMUM_STACK_SIZE,
223                RTEMS_DEFAULT_MODES,
224                RTEMS_FLOATING_POINT,
225                &wid
226        );
227        assert(sc == RTEMS_SUCCESSFUL);
228
229        sc = rtems_task_create(
230                rtems_build_name('U', 'S', 'B', 'O'),
231                PRIO_OPEN,
232                RTEMS_MINIMUM_STACK_SIZE,
233                RTEMS_DEFAULT_MODES,
234                RTEMS_FLOATING_POINT,
235                &oid
236        );
237        assert(sc == RTEMS_SUCCESSFUL);
238
239        sc = rtems_task_start(rid, usb_serial_read_task, 0);
240        assert(sc == RTEMS_SUCCESSFUL);
241
242        sc = rtems_task_start(oid, usb_serial_open_task, 0);
243        assert(sc == RTEMS_SUCCESSFUL);
244
245        sc = rtems_task_start(wid, usb_serial_write_task, 0);
246        assert(sc == RTEMS_SUCCESSFUL);
247
248        sc = rtems_bsd_initialize();
249        assert(sc == RTEMS_SUCCESSFUL);
250
251        msg.fd = -1;
252        rtems_message_queue_send(omid, &msg, sizeof(msg));
253
254        sc = rtems_shell_init("SHLL", 16 * 1024, 1, CONSOLE_DEVICE_NAME,
255            false, true, NULL);
256        assert(sc == RTEMS_SUCCESSFUL);
257
258        kill_otask = true;
259        kill_rtask = true;
260        kill_wtask = true;
261        while (otask_active || rtask_active || wtask_active) {
262                rtems_task_wake_after(RTEMS_MILLISECONDS_TO_TICKS(10));
263        }
264
265        sc = rtems_message_queue_delete(wmid);
266        assert(sc == RTEMS_SUCCESSFUL);
267
268        sc = rtems_message_queue_delete(rmid);
269        assert(sc == RTEMS_SUCCESSFUL);
270
271        sc = rtems_message_queue_delete(omid);
272        assert(sc == RTEMS_SUCCESSFUL);
273
274        exit(0);
275}
276
277#define CONFIGURE_MICROSECONDS_PER_TICK 1000
278
279#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
280#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
281#define CONFIGURE_APPLICATION_NEEDS_STUB_DRIVER
282#define CONFIGURE_APPLICATION_NEEDS_ZERO_DRIVER
283#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
284
285#define CONFIGURE_MAXIMUM_DRIVERS 32
286
287#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 32
288
289#define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1
290
291#define CONFIGURE_UNLIMITED_OBJECTS
292#define CONFIGURE_UNIFIED_WORK_AREAS
293
294#define CONFIGURE_STACK_CHECKER_ENABLED
295
296#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
297
298#define CONFIGURE_INIT_TASK_STACK_SIZE (32 * 1024)
299#define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
300#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
301
302#define CONFIGURE_INIT
303
304#include <rtems/confdefs.h>
305
306#include <bsp/nexus-devices.h>
307
308SYSINIT_DRIVER_REFERENCE(uplcom, uhub);
309
310#define CONFIGURE_SHELL_COMMANDS_INIT
311
312#include <bsp/irq-info.h>
313
314#include <rtems/netcmds-config.h>
315
316#define CONFIGURE_SHELL_USER_COMMANDS \
317        &bsp_interrupt_shell_command,       \
318        &rtems_shell_STTY_Command,          \
319        &rtems_shell_SYSCTL_Command
320
321#define CONFIGURE_SHELL_COMMAND_CPUUSE
322#define CONFIGURE_SHELL_COMMAND_PERIODUSE
323#define CONFIGURE_SHELL_COMMAND_STACKUSE
324#define CONFIGURE_SHELL_COMMAND_PROFREPORT
325
326#define CONFIGURE_SHELL_COMMAND_CP
327#define CONFIGURE_SHELL_COMMAND_PWD
328#define CONFIGURE_SHELL_COMMAND_LS
329#define CONFIGURE_SHELL_COMMAND_LN
330#define CONFIGURE_SHELL_COMMAND_LSOF
Note: See TracBrowser for help on using the repository browser.