Added debug prints, better error return values.
authorRoman Moravcik <roman.moravcik@gmail.com>
Mon, 21 Dec 2009 20:19:31 +0000 (21:19 +0100)
committerRoman Moravcik <roman.moravcik@gmail.com>
Mon, 21 Dec 2009 20:19:31 +0000 (21:19 +0100)
src/flashlight_lib.c
src/flashlight_lib.h
test/test.c

index 8b48708..6f56ede 100644 (file)
@@ -36,81 +36,85 @@ int flashlight_get_status (FlashlightContext_t *flashlight, int *status)
 {
        struct v4l2_control ctrl;
 
+       printf ("flashlight_get_status()\n");
+
        if (flashlight == NULL) {
                printf ("flashlight_get_status: flashlight context is not valid\n");
-               return -1;
+               return ENOCONTEXT;
        }
 
        if (flashlight->fd == -1) {
                printf ("flashlight_get_status: device not openned\n");
-               return -1;
+               return ENODEVICE;
        }
 
        *status = 0;
 
        /* check short circuit fault */
        ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_SCP;
-       if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
+       if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
                printf ("flashlight_set_intensity: cannot get circuit fault status (%s)\n", strerror (errno));
-               return -1;
+               return EGENERROR;
        }
 
-       if (ctrl.id)
+       if (ctrl.value)
                *status |= FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
        else
                *status &= ~FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
 
        /* check overtemperature fault */
        ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OT;
-       if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
+       if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
                printf ("flashlight_set_intensity: cannot get overtemperature fault status (%s)\n", strerror (errno));
-               return -1;
+               return EGENERROR;
        }
 
-       if (ctrl.id)
+       if (ctrl.value)
                *status |= FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
        else
                *status &= ~FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
 
        /* check timeout fault */
        ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_TMR;
-       if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
+       if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
                printf ("flashlight_set_intensity: cannot get timeout fault status (%s)\n", strerror (errno));
-               return -1;
+               return EGENERROR;
        }
 
-       if (ctrl.id)
+       if (ctrl.value)
                *status |= FLASHLIGHT_STATUS_TIMEOUT_FAULT;
        else
                *status &= ~FLASHLIGHT_STATUS_TIMEOUT_FAULT;
 
        /* check overtemperature fault */
        ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OV;
-       if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
+       if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
                printf ("flashlight_set_intensity: cannot get overvoltage fault status (%s)\n", strerror (errno));
-               return -1;
+               return EGENERROR;
        }
 
-       if (ctrl.id)
+       if (ctrl.value)
                *status |= FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
        else
                *status &= ~FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
 
-       return 0;
+       return ENOERROR;
 }
 
 int flashlight_set_intensity (FlashlightContext_t *flashlight, int intensity)
 {
        struct v4l2_control ctrl;
 
+       printf ("flashlight_set_intensity(%d)\n", intensity);
+
        if (flashlight == NULL) {
                printf ("flashlight_set_intensity: flashlight context is not valid\n");
-               return -1;
+               return ENOCONTEXT;
        }
 
        if (flashlight->fd == -1) {
                printf ("flashlight_set_intensity: device not openned\n");
-               return -1;
+               return ENODEVICE;
        }
 
        if (intensity > flashlight->max_intensity)
@@ -121,35 +125,37 @@ int flashlight_set_intensity (FlashlightContext_t *flashlight, int intensity)
 
        if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
                printf ("flashlight_set_intensity: cannot set intensity (%s)\n", strerror (errno));
-               return -1;
+               return EGENERROR;
        }
 
-       return 0;
+       return ENOERROR;
 }
 
 int flashlight_get_intensity (FlashlightContext_t *flashlight, int *intensity)
 {
        struct v4l2_control ctrl;
 
+       printf ("flashlight_get_intensity()\n");
+
        if (flashlight == NULL) {
                printf ("flashlight_get_intensity: flashlight context is not valid\n");
-               return -1;
+               return ENOCONTEXT;
        }
 
        if (flashlight->fd == -1) {
                printf ("flashlight_get_intensity: device not openned\n");
-               return -1;
+               return ENODEVICE;
        }
 
        ctrl.id = V4L2_CID_TORCH_INTENSITY;
 
-       if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
+       if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
                printf ("flashlight_get_intensity: cannot get intensity (%s)\n", strerror (errno));
-               return -1;
+               return EGENERROR;
        }
 
        *intensity = ctrl.value;
-       return 0;
+       return ENOERROR;
 }
 
 int flashlight_open (FlashlightContext_t *flashlight, const char *device_name)
@@ -157,81 +163,87 @@ int flashlight_open (FlashlightContext_t *flashlight, const char *device_name)
        struct v4l2_queryctrl ctrl;
        struct stat st;
 
+       printf ("flashlight_open(%s)\n", device_name);
+
        if (flashlight == NULL) {
                printf ("flashlight_open: flashlight context is not valid\n");
-               return -1;
+               return ENOCONTEXT;
        }
 
        if (device_name == NULL) {
                printf ("flashlight_open: device name not specified\n");
-               return -1;
+               return EGENERROR;
        }
 
        memcpy (flashlight->device_name, device_name, sizeof(flashlight->device_name));
 
        if (stat (flashlight->device_name, &st) == -1) {
                printf ("flashlight_open: cannot identify '%s' (%s)\n", flashlight->device_name, strerror (errno));
-               return -1;
+               return EGENERROR;
        }
 
        /* check it device_name is real device */
        if (!S_ISCHR (st.st_mode)) {
                printf ("flashlight_open: %s is no device\n", flashlight->device_name);
-               return -1;
+               return EGENERROR;
        }
 
        flashlight->fd = open (flashlight->device_name, O_RDWR /* required */ | O_NONBLOCK, 0);
 
        if (flashlight->fd == -1) {
                printf ("flashlight_open: cannot open '%s' (%s)\n", flashlight->device_name, strerror (errno));
-               return -1;
+               return ENODEVICE;
        }
 
        /* query from driver minimal and maximal flashlight intensity */
        ctrl.id = V4L2_CID_TORCH_INTENSITY;
        if (ioctl (flashlight->fd, VIDIOC_QUERYCTRL, &ctrl) == -1) {
                printf ("flashlight_open: cannot get minimal and maximal flashlight intensity (%s)\n", strerror (errno));
-               return -1;
+               return EGENERROR;
        }
 
        flashlight->min_intensity = ctrl.minimum;
        flashlight->max_intensity = ctrl.maximum;
 
-       return 0;
+       return ENOERROR;
 }
 
 int flashlight_close (FlashlightContext_t *flashlight)
 {
+       printf ("flashlight_close()\n");
+
        if (flashlight == NULL) {
                printf ("flashlight_close: flashlight context is not valid\n");
-               return -1;
+               return ENOCONTEXT;
        }
 
        if (flashlight->fd != -1) {
                if (close (flashlight->fd) == -1) {
                        printf ("flashlight_close: cannot close device '%s' (%s)\n", flashlight->device_name, strerror (errno));
-                       return -1;
+                       return ENODEVICE;
                }
        }
 
        flashlight->fd = -1;
-       return 0;
+       return ENOERROR;
 }
 
 int flashlight_init (FlashlightContext_t **pRefContext)
 {
        FlashlightContext_t *flashlight = NULL;
 
+       printf ("flashlight_init()\n");
+
        if (*pRefContext != NULL) {
                printf("flashlight_init: expecting zero pointer context '*pRefContext'\n");
-               return -1;
+               return EGENERROR;
        }
 
        /* allocate memory for context structure */
        flashlight = malloc (sizeof (FlashlightContext_t));
        if (flashlight == NULL) {
                printf ("flashlight_init: unable to allocate memory for context\n");
-               return -1;
+               return ENOCONTEXT;
        }
 
        *pRefContext = flashlight;
@@ -244,34 +256,36 @@ int flashlight_init (FlashlightContext_t **pRefContext)
        flashlight->min_intensity = 0;
        flashlight->max_intensity = 11;
 
-       return 0;
+       return ENOERROR;
 }
 
 int flashlight_deinit (FlashlightContext_t *flashlight)
 {
        int intensity = 0;
 
+       printf ("flashlight_deinit()\n");
+
        if (flashlight == NULL) {
                printf ("flashlight_deinit: flashlight context is not valid\n");
-               return -1;
+               return ENOCONTEXT;
        }
 
        if (flashlight->fd != -1) {
                /* check if flashlight isn't enabled before closing device */
                if (flashlight_get_intensity (flashlight, &intensity) == -1)
-                       return -1;
+                       return EGENERROR;
 
                if (intensity > 0) {
                        if (flashlight_set_intensity (flashlight, 0) == -1)
-                               return -1;
+                               return EGENERROR;
                }
 
                if (flashlight_close(flashlight))
-                       return -1;
+                       return EGENERROR;
        }
 
        /* free allocated memory */
        free (flashlight);
 
-       return 0;
+       return ENOERROR;
 }
index aac15a4..004c291 100644 (file)
 #define FLASHLIGHT_STATUS_TIMEOUT_FAULT                        0x0004
 #define FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT            0x0008
 
+#define ENOERROR        0
+#define EGENERROR      -1
+#define ENOCONTEXT     -2
+#define ENODEVICE      -3
+
 struct FlashlightContext {
        /* device name */
        char device_name[15];
index 6e8da4f..6fb3075 100644 (file)
 int main (int argc, char **argv)
 {
        FlashlightContext_t *pContext = NULL;
-       int intensity = 0;
+       int intensity = 0, status = 0;
 
-       if (flashlight_init(&pContext) == -1) {
+       if (flashlight_init(&pContext) < 0) {
                printf ("flashlight-test: error initializing flashlight library\n");
                return -1;
        }
 
-       if (flashlight_open (pContext, "/dev/video0") == -1) {
+       if (flashlight_open (pContext, "/dev/video0") < 0) {
                printf ("flashlight-test: error openning device\n");
                return -1;
        }
 
        printf ("Enabling flash LEDs in flashlight mode\n");
-       if (flashlight_set_intensity(pContext, 11) == -1) {
+       if (flashlight_set_intensity(pContext, 11) < 0) {
                printf ("flashlight-test: error setting flashlight intensity to 11\n");
                return -1;
        }
 
-       if (flashlight_get_intensity(pContext, &intensity) == -1) {
+       if (flashlight_get_intensity(pContext, &intensity) < 0) {
                printf ("flashlight-test: error getting flashlight intensity\n");
                return -1;
        }
        printf ("Current flashlight intensity is %d\n", intensity);
 
+       if (flashlight_get_status(pContext, &status) < 0) {
+               printf ("flashlight-test: error getting flashlight status\n");
+               return -1;
+       }
+       printf ("Current flashlight status is 0x%04x\n", status);
+
        sleep(3);
 
        printf ("Disabling flash LEDs in flashlight mode\n");
-       if (flashlight_set_intensity(pContext, 0) == -1) {
+       if (flashlight_set_intensity(pContext, 0) < 0) {
                printf ("flashlight-test: error setting flashlight intensity to 0\n");
                return -1;
        }
 
-       if (flashlight_close (pContext) == -1) {
+       if (flashlight_close (pContext) < 0) {
                printf ("flashlight-test: error closing device\n");
                return -1;
        }
 
-       if (flashlight_deinit(pContext) == -1) {
+       if (flashlight_deinit(pContext) < 0) {
                printf ("flashlight-test: error deinitializing flashlight library\n");
                return -1;
        }