From 24f0d1c533a716db29ae1d1748464a86d170da45 Mon Sep 17 00:00:00 2001 From: Stathis Kamperis Date: Mon, 18 Feb 2008 02:14:22 +0200 Subject: [PATCH] Add some comments --- cdev/mydev.c | 24 +++++++++++++----------- cdev/mydev.h | 2 -- cdev/test.c | 41 +++++++++++++++++++++++++++++++---------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/cdev/mydev.c b/cdev/mydev.c index cfcb4ca..fc7395f 100644 --- a/cdev/mydev.c +++ b/cdev/mydev.c @@ -16,7 +16,7 @@ struct mydev_softc { void mydevattach(struct device *parent, struct device *self, void *aux); int mydevopen(dev_t dev, int flags, int fmt, struct lwp *process); int mydevclose(dev_t dev, int flags, int fmt, struct lwp *process); -int mydevioctl(dev_t dev, u_long cmd, void *data, +int mydevioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *process); /* Just define the character dev handlers because that is all we need */ @@ -69,11 +69,11 @@ mydevclose(dev_t dev, int flags, int fmt, struct lwp *process) * Handle the ioctl for the dev. */ int -mydevioctl(dev_t dev, u_long cmd, void *data, int flags, +mydevioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *process) { - prop_dictionary_t dict, odict; - prop_object_t po; + prop_dictionary_t dict; + prop_string_t ps; struct mydev_params *params = (struct mydev_params *)data; const struct plistref *pref; int error; @@ -92,15 +92,17 @@ mydevioctl(dev_t dev, u_long cmd, void *data, int flags, error = prop_dictionary_copyin_ioctl(pref, cmd, &dict); if (error) return error; - odict = mydevprops; - mydevprops = dict; - prop_object_release(odict); - po = prop_dictionary_get(mydevprops, "key"); - - val = prop_string_cstring(po); - printf(" = (%s, %s)\n", "key", val); + ps = prop_dictionary_get(dict, "key"); + if (ps == NULL) { + printf("prop_dictionary_get()\n"); + return -1; + } + val = prop_string_cstring(ps); + prop_object_release(ps); + printf(" = (%s, %s)\n", "key", val == NULL ? "null" : val); + prop_object_release(dict); break; default: diff --git a/cdev/mydev.h b/cdev/mydev.h index dec9a95..ea0dcbd 100644 --- a/cdev/mydev.h +++ b/cdev/mydev.h @@ -15,8 +15,6 @@ struct mydev_params char string[80]; }; -prop_dictionary_t mydevprops; - #define MYDEVTEST _IOW('S', 0x1, struct mydev_params) #define MYDEVSETPROPS _IOW('S', 0x2, struct mydev_params) diff --git a/cdev/test.c b/cdev/test.c index e28ac3a..0a42cea 100644 --- a/cdev/test.c +++ b/cdev/test.c @@ -11,31 +11,52 @@ int main() struct mydev_params params; prop_dictionary_t pd; prop_string_t ps; - int mydev_dev; + int devfd; params.number = 42; strcpy(params.string, "Hello World"); - if ((mydev_dev = open("/dev/mydev", O_RDONLY, 0)) < 0) { + /* Open device */ + if ((devfd = open("/dev/mydev", O_RDONLY, 0)) < 0) { fprintf(stderr, "Failed to open /dev/mydev\n"); - exit(2); + exit(EXIT_FAILURE); } - if (ioctl(mydev_dev, MYDEVTEST, ¶ms) < 0) { - perror("ioctl failed"); - exit(2); + /* Send ioctl request in the traditional way */ + if (ioctl(devfd, MYDEVTEST, ¶ms) < 0) { + close(devfd); + err("ioctl()"); } + /* Create dictionary and add a pair in it */ pd = prop_dictionary_create(); + if (pd == NULL) { + close(devfd); + err("prop_dictionary_create()"); + } ps = prop_string_create_cstring("key"); - prop_dictionary_set(pd, "value", ps); + if (ps == NULL) { + close(devfd); + prop_object_release(pd); + err("prop_string_create_cstring()"); + } + + if (prop_dictionary_set(pd, "value", ps) == false) { + close(devfd); + prop_object_release(ps); + prop_object_release(pd); + err("prop_dictionary_set()"); + } + prop_object_release(ps); - prop_dictionary_send_ioctl(pd, mydev_dev, MYDEVSETPROPS); + /* Send dictionary to kernel space */ + prop_dictionary_send_ioctl(pd, devfd, MYDEVSETPROPS); prop_object_release(pd); - close(mydev_dev); + + close(devfd); - exit(0); + return EXIT_SUCCESS; } -- 2.11.4.GIT