Add some comments plus fix a possible mem leak
[eleutheria.git] / cdev / test.c
blob0a42cea0b4b695da0ab1bd7cd758477ddd1b9e03
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/ioctl.h>
6 #include <sys/mydev.h>
7 #include <prop/proplib.h>
9 int main()
11 struct mydev_params params;
12 prop_dictionary_t pd;
13 prop_string_t ps;
14 int devfd;
16 params.number = 42;
17 strcpy(params.string, "Hello World");
19 /* Open device */
20 if ((devfd = open("/dev/mydev", O_RDONLY, 0)) < 0) {
21 fprintf(stderr, "Failed to open /dev/mydev\n");
22 exit(EXIT_FAILURE);
25 /* Send ioctl request in the traditional way */
26 if (ioctl(devfd, MYDEVTEST, &params) < 0) {
27 close(devfd);
28 err("ioctl()");
31 /* Create dictionary and add a <key, value> pair in it */
32 pd = prop_dictionary_create();
33 if (pd == NULL) {
34 close(devfd);
35 err("prop_dictionary_create()");
38 ps = prop_string_create_cstring("key");
39 if (ps == NULL) {
40 close(devfd);
41 prop_object_release(pd);
42 err("prop_string_create_cstring()");
45 if (prop_dictionary_set(pd, "value", ps) == false) {
46 close(devfd);
47 prop_object_release(ps);
48 prop_object_release(pd);
49 err("prop_dictionary_set()");
52 prop_object_release(ps);
54 /* Send dictionary to kernel space */
55 prop_dictionary_send_ioctl(pd, devfd, MYDEVSETPROPS);
57 prop_object_release(pd);
59 close(devfd);
61 return EXIT_SUCCESS;