Add default pointer prefs
[AROS.git] / test / hostio.c
blob381e84e33cc4bde675cd8ed495f4c739daae4610
1 #include <aros/bootloader.h>
2 #include <hidd/hostio.h>
3 #include <proto/bootloader.h>
4 #include <proto/exec.h>
5 #include <proto/dos.h>
6 #include <proto/oop.h>
7 #include <oop/oop.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <string.h>
13 struct Library *OOPBase = NULL;
15 char *unix_files[] = {
16 "/dev/zero",
17 "/dev/null",
18 "/etc/motd"
21 char *windows_files[] = {
22 "NUL:", /* Reading from NUL: on Windows produces EOF, but i can't invent anything better */
23 "NUL:",
24 "C:\\Windows\\System32\\EULA.txt"
27 int main (int argc, char **argv)
29 struct Library *HostIOBase = NULL;
30 APTR BootLoaderBase = NULL;
31 STRPTR bootldr_name;
32 char **files = unix_files;
33 int failed = 1;
34 OOP_Object *hostio = NULL;
35 APTR fd = vHidd_HostIO_Invalid_Handle;
36 int nbytes, ioerr;
37 char buf[1024];
39 if ((OOPBase = OpenLibrary("oop.library", 0)) == NULL) {
40 fprintf(stderr, "can't open oop.library\n");
41 goto exit;
44 HostIOBase = OpenLibrary("DEVS:Drivers/hostio.hidd", 0);
45 if (!HostIOBase) {
46 fprintf(stderr, "Can't open hostio.hidd\n");
47 goto exit;
50 if ((hostio = OOP_NewObject(NULL, CLID_Hidd_HostIO, NULL)) == NULL) {
51 fprintf(stderr, "can't instantiate hostio hidd\n");
52 goto exit;
55 BootLoaderBase = OpenResource("bootloader.resource");
56 if (!BootLoaderBase) {
57 fprintf(stderr, "Can't open bootloader.resource\n");
58 goto exit;
60 bootldr_name = GetBootInfo(BL_LoaderName);
61 if (bootldr_name) {
62 printf("Bootloader name: %s\n", bootldr_name);
63 if (!strnicmp(bootldr_name, "Windows", 7))
64 files = windows_files;
65 } else
66 printf("Failed to obtain bootloader name, assuming UNIX host\n");
68 printf("first, a trivial file read test\n\n");
70 printf("opening %s for read... ", files[0]);
71 fd = Hidd_HostIO_OpenFile(hostio, files[0], O_RDONLY, 0, &ioerr, NULL);
72 if (ioerr != 0) {
73 printf("failed (ioerr is %d)\n", ioerr);
74 goto exit;
76 printf("ok (fd is 0x%p)\n", fd);
78 printf("reading... ");
79 nbytes = Hidd_HostIO_ReadFile(hostio, fd, buf, 1024, &ioerr, NULL);
80 if (ioerr != 0) {
81 printf("failed (ioerr is %d)\n", ioerr);
82 goto exit;
84 printf("ok (read %d bytes)\n", nbytes);
86 printf("closing file... ");
87 Hidd_HostIO_CloseFile(hostio, fd, &ioerr, NULL);
88 fd = vHidd_HostIO_Invalid_Handle;
89 if (ioerr != 0) {
90 printf("failed (ioerr is %d)\n", ioerr);
91 goto exit;
93 printf("ok\n\n\n");
96 printf("next, an equally trivial file write test\n\n");
98 printf("opening %s for write... ", files[1]);
99 fd = Hidd_HostIO_OpenFile(hostio, files[1], O_WRONLY, 0, &ioerr, NULL);
100 if (ioerr != 0) {
101 printf("failed (ioerr is %d)\n", ioerr);
102 goto exit;
104 printf("ok (fd is 0x%p)\n", fd);
106 printf("writing... ");
107 nbytes = Hidd_HostIO_WriteFile(hostio, fd, buf, 1024, &ioerr, NULL);
108 if (ioerr != 0) {
109 printf("failed (ioerr is %d)\n", ioerr);
110 goto exit;
112 printf("ok (wrote %d bytes)\n", nbytes);
114 printf("closing file... ");
115 Hidd_HostIO_CloseFile(hostio, fd, &ioerr, NULL);
116 fd = vHidd_HostIO_Invalid_Handle;
117 if (ioerr != 0) {
118 printf("failed (ioerr is %d)\n", ioerr);
119 goto exit;
121 printf("ok\n\n\n");
124 printf("just for fun, lets read and print the contents of a file\n\n");
126 printf("opening %s for read... ", files[2]);
127 fd = Hidd_HostIO_OpenFile(hostio, files[2], O_RDONLY, 0, &ioerr, NULL);
128 if (ioerr != 0) {
129 printf("failed (ioerr is %d)\n", ioerr);
130 goto exit;
132 printf("ok (fd is 0x%p)\n", fd);
134 printf("reading... ");
135 nbytes = Hidd_HostIO_ReadFile(hostio, fd, buf, 1024, &ioerr, NULL);
136 if (ioerr != 0) {
137 printf("failed (ioerr is %d)\n", ioerr);
138 goto exit;
140 printf("ok (read %d bytes)\n", nbytes);
142 printf("File contents:\n\n%.*s", nbytes, buf);
144 printf("\nclosing file... ");
145 Hidd_HostIO_CloseFile(hostio, fd, &ioerr, NULL);
146 fd = vHidd_HostIO_Invalid_Handle;
147 if (ioerr != 0) {
148 printf("failed (ioerr is %d)\n", ioerr);
149 goto exit;
151 printf("ok\n\n\n");
153 /* FIXME: it's a very bad test. First, it actually does not do what it should do because
154 reading ordinary file seems always to complete immediately. Second, Wait method should
155 not be called at all here, because the first thing it does is to check if the overlapped
156 I/O is pending. However MSDN docs say do do it *ONLY* if the I/O has actually entered
157 asynchronous mode (in this case Read method would return EWOULDBLOCK). So, it works
158 only by pure luck and demonstrates only that Wait method does not crash in the beginning.
159 May be try to write something to the serial port? */
160 printf("Now read the same file in asynchronous mode\n\n");
161 printf("opening %s for async read... ", files[2]);
162 fd = Hidd_HostIO_OpenFile(hostio, files[2], O_RDONLY|O_NONBLOCK, 0, &ioerr, NULL);
163 if (ioerr != 0) {
164 printf("failed (ioerr is %d)\n", ioerr);
165 goto exit;
167 printf("ok (fd is 0x%p)\n", fd);
168 printf("requesting read... ");
169 nbytes = Hidd_HostIO_ReadFile(hostio, fd, buf, 1024, &ioerr, NULL);
170 if ((ioerr != 0) && (ioerr != EWOULDBLOCK)) {
171 printf("failed (ioerr is %d)\n", ioerr);
172 goto exit;
174 printf("ok (read %d bytes, ioerr is %d)\n", nbytes, ioerr);
175 printf("waiting... ");
176 nbytes = Hidd_HostIO_Wait(hostio, fd, NULL, NULL, &ioerr, NULL);
177 if (ioerr != 0) {
178 printf("failed (ioerr is %d)\n", ioerr);
179 goto exit;
181 printf("ok (read %d bytes)\n", nbytes);
182 printf("File contents:\n\n%.*s", nbytes, buf);
183 printf("\nclosing file... ");
184 Hidd_HostIO_CloseFile(hostio, fd, &ioerr, NULL);
185 fd = vHidd_HostIO_Invalid_Handle;
186 if (ioerr != 0) {
187 printf("failed (ioerr is %d)\n", ioerr);
188 goto exit;
190 printf("ok\n\n\n");
192 exit:
193 if (fd != vHidd_HostIO_Invalid_Handle) Hidd_HostIO_CloseFile(hostio, fd, NULL, NULL);
194 if (hostio != NULL) OOP_DisposeObject(hostio);
195 if (HostIOBase) CloseLibrary(HostIOBase);
196 if (OOPBase != NULL) CloseLibrary(OOPBase);
198 return failed ? 1 : 0;