3 * gcc testdev.c -o testdev -I /usr/src/sys -lprop -Wall -W -ansi
6 #include <err.h> /* for err() */
11 #include <unistd.h> /* for close() */
12 #include <sys/ioctl.h>
13 #include <sys/mydev.h>
14 #include <prop/proplib.h>
19 struct mydev_params params
;
25 if ((devfd
= open("/dev/mydev", O_RDONLY
, 0)) < 0) {
26 fprintf(stderr
, "Failed to open /dev/mydev\n");
30 /* Send ioctl request in the traditional way */
32 strncpy(params
.string
, "Hello World", MAX_STR
);
34 if (ioctl(devfd
, MYDEVOLDIOCTL
, ¶ms
) == -1) {
36 err(EXIT_FAILURE
, "ioctl()");
39 /* Create dictionary and add a <key, value> pair in it */
40 pd
= prop_dictionary_create();
43 err(EXIT_FAILURE
, "prop_dictionary_create()");
46 ps
= prop_string_create_cstring("value");
49 prop_object_release(pd
);
50 err(EXIT_FAILURE
, "prop_string_create_cstring()");
53 if (prop_dictionary_set(pd
, "key", ps
) == false) {
55 prop_object_release(ps
);
56 prop_object_release(pd
);
57 err(EXIT_FAILURE
, "prop_dictionary_set()");
60 prop_object_release(ps
);
62 /* Send dictionary to kernel space */
63 prop_dictionary_send_ioctl(pd
, devfd
, MYDEVSETPROPS
);
65 prop_object_release(pd
);
67 /* Read data from device */
68 if ((ret
= read(devfd
, buffer
, sizeof buffer
)) < 0)
69 err(EXIT_FAILURE
, "read()");
71 printf("testdev: ret = %d, buffer = %s\n", ret
, buffer
);
74 if (close(devfd
) == -1)
75 err(EXIT_FAILURE
, "close()");