1 #include <aros/bootloader.h>
2 #include <hidd/hostio.h>
3 #include <proto/bootloader.h>
4 #include <proto/exec.h>
13 struct Library
*OOPBase
= NULL
;
15 char *unix_files
[] = {
21 char *windows_files
[] = {
22 "NUL:", /* Reading from NUL: on Windows produces EOF, but i can't invent anything better */
24 "C:\\Windows\\System32\\EULA.txt"
27 int main (int argc
, char **argv
)
29 struct Library
*HostIOBase
= NULL
;
30 APTR BootLoaderBase
= NULL
;
32 char **files
= unix_files
;
34 OOP_Object
*hostio
= NULL
;
35 APTR fd
= vHidd_HostIO_Invalid_Handle
;
39 if ((OOPBase
= OpenLibrary("oop.library", 0)) == NULL
) {
40 fprintf(stderr
, "can't open oop.library\n");
44 HostIOBase
= OpenLibrary("DEVS:Drivers/hostio.hidd", 0);
46 fprintf(stderr
, "Can't open hostio.hidd\n");
50 if ((hostio
= OOP_NewObject(NULL
, CLID_Hidd_HostIO
, NULL
)) == NULL
) {
51 fprintf(stderr
, "can't instantiate hostio hidd\n");
55 BootLoaderBase
= OpenResource("bootloader.resource");
56 if (!BootLoaderBase
) {
57 fprintf(stderr
, "Can't open bootloader.resource\n");
60 bootldr_name
= GetBootInfo(BL_LoaderName
);
62 printf("Bootloader name: %s\n", bootldr_name
);
63 if (!strnicmp(bootldr_name
, "Windows", 7))
64 files
= windows_files
;
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
);
73 printf("failed (ioerr is %d)\n", ioerr
);
76 printf("ok (fd is 0x%p)\n", fd
);
78 printf("reading... ");
79 nbytes
= Hidd_HostIO_ReadFile(hostio
, fd
, buf
, 1024, &ioerr
, NULL
);
81 printf("failed (ioerr is %d)\n", ioerr
);
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
;
90 printf("failed (ioerr is %d)\n", ioerr
);
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
);
101 printf("failed (ioerr is %d)\n", ioerr
);
104 printf("ok (fd is 0x%p)\n", fd
);
106 printf("writing... ");
107 nbytes
= Hidd_HostIO_WriteFile(hostio
, fd
, buf
, 1024, &ioerr
, NULL
);
109 printf("failed (ioerr is %d)\n", ioerr
);
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
;
118 printf("failed (ioerr is %d)\n", ioerr
);
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
);
129 printf("failed (ioerr is %d)\n", ioerr
);
132 printf("ok (fd is 0x%p)\n", fd
);
134 printf("reading... ");
135 nbytes
= Hidd_HostIO_ReadFile(hostio
, fd
, buf
, 1024, &ioerr
, NULL
);
137 printf("failed (ioerr is %d)\n", ioerr
);
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
;
148 printf("failed (ioerr is %d)\n", ioerr
);
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
);
164 printf("failed (ioerr is %d)\n", ioerr
);
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
);
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
);
178 printf("failed (ioerr is %d)\n", ioerr
);
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
;
187 printf("failed (ioerr is %d)\n", ioerr
);
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;