source: rtems-libbsd/testsuite/usbkbd01/init.c @ 15a4321

55-freebsd-126-freebsd-12
Last change on this file since 15a4321 was 15a4321, checked in by Kevin Kirspel <kevin-kirspel@…>, on 05/17/17 at 12:40:35

Add tests for USB keyboard and mouse as well as EVDEV

  • Property mode set to 100644
File size: 7.1 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 <errno.h>
32#include <fcntl.h>
33
34#include <rtems/console.h>
35#include <rtems/shell.h>
36#include <rtems/bsd/bsd.h>
37
38#define TEST_NAME "LIBBSD USB KEYBOARD"
39
40#define USB_SERIAL_TEST_BUFFER_SIZE 48
41#define PRIO_OPEN  (RTEMS_MAXIMUM_PRIORITY - 12)
42#define PRIO_READ  (RTEMS_MAXIMUM_PRIORITY - 11)
43
44struct usb_test_message {
45        int fd;
46        char rbuf[USB_SERIAL_TEST_BUFFER_SIZE];
47};
48
49static rtems_id oid, rid, omid, rmid;
50static volatile bool kill_otask, kill_rtask;
51static volatile bool otask_active, rtask_active;
52
53static void
54usb_keyboard_read_task(rtems_task_argument arg)
55{
56        rtems_status_code sc;
57        struct usb_test_message msg;
58        uint32_t size;
59        int bytes;
60
61        rtask_active = true;
62        kill_rtask = false;
63        while (!kill_rtask) {
64                while (!kill_rtask) {
65                        sc = rtems_message_queue_receive(rmid, &msg, &size, RTEMS_WAIT, RTEMS_MILLISECONDS_TO_TICKS(1000));
66                        if (sc == RTEMS_SUCCESSFUL) {
67                                if (msg.fd > 0) {
68                                        break;
69                                } else {
70                                        printf("Invalid file descriptor\n");
71                                }
72                        }
73                }
74                while (!kill_rtask) {
75                        msg.rbuf[0] = 0;
76                        bytes = read(msg.fd, &msg.rbuf[0], 1);
77                        if (bytes == 0) {
78                                printf("Got EOF from the input device\n");
79                        } else if (bytes < 0) {
80                                if (errno != EINTR && errno != EAGAIN) {
81                                        printf("Could not read from input device\n");
82                                        break;
83                                }
84                                rtems_task_wake_after(RTEMS_MILLISECONDS_TO_TICKS(10));
85                        } else {
86                                printf("%c", msg.rbuf[0]);
87                                fflush(stdout);
88                        }
89          }
90          msg.fd = -1;
91          rtems_message_queue_send(omid, &msg, sizeof(msg));
92        }
93        rtask_active = false;
94        rtems_task_delete(RTEMS_SELF);
95}
96
97static void
98usb_keyboard_open_task(rtems_task_argument arg)
99{
100        rtems_status_code sc;
101        struct usb_test_message msg;
102        struct termios t;
103        uint32_t size;
104        int fd, iret;
105
106        fd = -2;
107        otask_active = true;
108        kill_otask = false;
109        while (!kill_otask) {
110                sc = rtems_message_queue_receive(omid, &msg, &size, RTEMS_WAIT, RTEMS_MILLISECONDS_TO_TICKS(1000));
111                if (sc == RTEMS_SUCCESSFUL) {
112                        if (fd >= 0) {
113                                close(fd);
114                                printf("keyboard device closed\n");
115                        }
116                        fd = msg.fd;
117                }
118                if (fd == -1) {
119                        fd = open("/dev/ukbd0", O_RDWR | O_NONBLOCK);
120                        if (fd != -1) {
121                                printf("keyboard device opened: %d\n", fd);
122                                msg.fd = fd;
123                                rtems_message_queue_send(rmid, &msg, sizeof(msg));
124                        }
125                        else {
126                                /*printf("keyboard device open failed: %d\n", errno);*/
127                        }
128                }
129        }
130        if (fd >= 0) {
131                close(fd);
132                printf("keyboard device closed\n");
133        }
134        otask_active = false;
135        rtems_task_delete(RTEMS_SELF);
136}
137
138static void
139Init(rtems_task_argument arg)
140{
141        rtems_status_code sc;
142        struct usb_test_message msg;
143
144        (void) arg;
145        puts("*** " TEST_NAME " TEST ***");
146
147        sc = rtems_message_queue_create(
148                rtems_build_name ('M', 'U', 'O', 'P'),
149                16,
150                sizeof(struct usb_test_message),
151                RTEMS_PRIORITY,
152                &omid
153        );
154        assert(sc == RTEMS_SUCCESSFUL);
155
156        sc = rtems_message_queue_create(
157                rtems_build_name ('M', 'U', 'R', 'D'),
158                16,
159                sizeof(struct usb_test_message),
160                RTEMS_PRIORITY,
161                &rmid
162        );
163        assert(sc == RTEMS_SUCCESSFUL);
164
165        sc = rtems_task_create(
166                rtems_build_name('U', 'S', 'B', 'R'),
167                PRIO_READ,
168                RTEMS_MINIMUM_STACK_SIZE,
169                RTEMS_DEFAULT_MODES,
170                RTEMS_FLOATING_POINT,
171                &rid
172        );
173        assert(sc == RTEMS_SUCCESSFUL);
174
175        sc = rtems_task_create(
176                rtems_build_name('U', 'S', 'B', 'O'),
177                PRIO_OPEN,
178                RTEMS_MINIMUM_STACK_SIZE,
179                RTEMS_DEFAULT_MODES,
180                RTEMS_FLOATING_POINT,
181                &oid
182        );
183        assert(sc == RTEMS_SUCCESSFUL);
184
185        sc = rtems_task_start(rid, usb_keyboard_read_task, NULL);
186        assert(sc == RTEMS_SUCCESSFUL);
187
188        sc = rtems_task_start(oid, usb_keyboard_open_task, NULL);
189        assert(sc == RTEMS_SUCCESSFUL);
190
191        sc = rtems_bsd_initialize();
192        assert(sc == RTEMS_SUCCESSFUL);
193
194        msg.fd = -1;
195        rtems_message_queue_send(omid, &msg, sizeof(msg));
196
197        sc = rtems_shell_init("SHLL", 16 * 1024, 1, CONSOLE_DEVICE_NAME,
198            false, true, NULL);
199        assert(sc == RTEMS_SUCCESSFUL);
200
201        kill_otask = true;
202        kill_rtask = true;
203        while (otask_active || rtask_active) {
204                rtems_task_wake_after(RTEMS_MILLISECONDS_TO_TICKS(10));
205        }
206
207        sc = rtems_message_queue_delete(rmid);
208        assert(sc == RTEMS_SUCCESSFUL);
209
210        sc = rtems_message_queue_delete(omid);
211        assert(sc == RTEMS_SUCCESSFUL);
212
213        exit(0);
214}
215
216#define CONFIGURE_MICROSECONDS_PER_TICK 1000
217
218#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
219#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
220#define CONFIGURE_APPLICATION_NEEDS_STUB_DRIVER
221#define CONFIGURE_APPLICATION_NEEDS_ZERO_DRIVER
222#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
223
224#define CONFIGURE_MAXIMUM_DRIVERS 32
225
226#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 32
227
228#define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1
229
230#define CONFIGURE_UNLIMITED_OBJECTS
231#define CONFIGURE_UNIFIED_WORK_AREAS
232
233#define CONFIGURE_STACK_CHECKER_ENABLED
234
235#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
236
237#define CONFIGURE_INIT_TASK_STACK_SIZE (32 * 1024)
238#define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
239#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
240
241#define CONFIGURE_INIT
242
243#include <rtems/confdefs.h>
244
245#include <bsp/nexus-devices.h>
246
247SYSINIT_DRIVER_REFERENCE(ukbd, uhub);
248
249#define CONFIGURE_SHELL_COMMANDS_INIT
250
251#include <bsp/irq-info.h>
252
253#include <rtems/netcmds-config.h>
254
255#define CONFIGURE_SHELL_USER_COMMANDS \
256  &bsp_interrupt_shell_command, \
257  &rtems_shell_SYSCTL_Command
258
259#define CONFIGURE_SHELL_COMMAND_CPUUSE
260#define CONFIGURE_SHELL_COMMAND_PERIODUSE
261#define CONFIGURE_SHELL_COMMAND_STACKUSE
262#define CONFIGURE_SHELL_COMMAND_PROFREPORT
263
264#define CONFIGURE_SHELL_COMMAND_CP
265#define CONFIGURE_SHELL_COMMAND_PWD
266#define CONFIGURE_SHELL_COMMAND_LS
267#define CONFIGURE_SHELL_COMMAND_LN
268#define CONFIGURE_SHELL_COMMAND_LSOF
269#define CONFIGURE_SHELL_COMMAND_CHDIR
270#define CONFIGURE_SHELL_COMMAND_CD
271#define CONFIGURE_SHELL_COMMAND_MKDIR
272#define CONFIGURE_SHELL_COMMAND_RMDIR
273#define CONFIGURE_SHELL_COMMAND_CAT
274#define CONFIGURE_SHELL_COMMAND_MV
275#define CONFIGURE_SHELL_COMMAND_RM
276#define CONFIGURE_SHELL_COMMAND_MALLOC_INFO
277
278#include <rtems/shellconfig.h>
Note: See TracBrowser for help on using the repository browser.