Dummy commit to test new ssh key
[eleutheria.git] / cdev / testdev.c
blobebd3525c9e51134eaf101259cf23b0d8ca6ed0ed
1 /*
2 * Compile with:
3 * gcc testdev.c -o testdev -I /usr/src/sys -lprop -Wall -W -ansi
4 */
6 #include <err.h> /* for err() */
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h> /* for close() */
12 #include <sys/ioctl.h>
13 #include <sys/mydev.h>
14 #include <prop/proplib.h>
16 int main(void)
18 char buffer[1000];
19 struct mydev_params params;
20 prop_dictionary_t pd;
21 prop_string_t ps;
22 int devfd, ret;
24 /* Open device */
25 if ((devfd = open("/dev/mydev", O_RDONLY, 0)) < 0) {
26 fprintf(stderr, "Failed to open /dev/mydev\n");
27 exit(EXIT_FAILURE);
30 /* Send ioctl request in the traditional way */
31 params.number = 42;
32 strncpy(params.string, "Hello World", MAX_STR);
34 if (ioctl(devfd, MYDEVOLDIOCTL, &params) == -1) {
35 close(devfd);
36 err(EXIT_FAILURE, "ioctl()");
39 /* Create dictionary and add a <key, value> pair in it */
40 pd = prop_dictionary_create();
41 if (pd == NULL) {
42 close(devfd);
43 err(EXIT_FAILURE, "prop_dictionary_create()");
46 ps = prop_string_create_cstring("value");
47 if (ps == NULL) {
48 close(devfd);
49 prop_object_release(pd);
50 err(EXIT_FAILURE, "prop_string_create_cstring()");
53 if (prop_dictionary_set(pd, "key", ps) == false) {
54 close(devfd);
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);
73 /* Close device */
74 if (close(devfd) == -1)
75 err(EXIT_FAILURE, "close()");
77 return EXIT_SUCCESS;