lib: Link fp_dev to fp_img_dev and back

So it's easy to switch between the 2 structs, and remove fp_img_dev->dev
direct access, as well as fp_dev->priv "user data" usage.
This commit is contained in:
Bastien Nocera 2018-09-14 17:30:01 +02:00
parent 170da7fec1
commit 161c3ccf1c
6 changed files with 83 additions and 44 deletions

View file

@ -622,7 +622,7 @@ static struct fp_img_dev *dev_to_img_dev(struct fp_dev *dev)
{
if (dev->drv->type != DRIVER_IMAGING)
return NULL;
return dev->priv;
return dev->img_dev;
}
/**

View file

@ -68,6 +68,9 @@ struct fp_dev {
uint32_t devtype;
void *priv;
/* only valid if drv->type == DRIVER_IMAGING */
struct fp_img_dev *img_dev;
int nr_enroll_stages;
/* read-only to drivers */
@ -136,8 +139,9 @@ enum fp_imgdev_verify_state {
};
struct fp_img_dev {
struct fp_dev *dev;
libusb_device_handle *udev;
struct fp_dev *parent;
enum fp_imgdev_action action;
int action_state;

View file

@ -19,3 +19,34 @@
#include "fp_internal.h"
#include <glib.h>
/**
* FP_DEV:
* @dev: a struct #fp_img_dev
*
* Returns the struct #fp_dev associated with @dev, or %NULL on failure.
*/
struct fp_dev *
FP_DEV(struct fp_img_dev *dev)
{
struct fp_img_dev *imgdev;
g_return_val_if_fail (dev, NULL);
imgdev = (struct fp_img_dev *) dev;
return imgdev->parent;
}
/**
* FP_IMG_DEV:
* @dev: a struct #fp_dev representing an imaging device.
*
* Returns: a struct #fp_img_dev or %NULL on failure.
*/
struct fp_img_dev *
FP_IMG_DEV(struct fp_dev *dev)
{
g_return_val_if_fail (dev, NULL);
g_return_val_if_fail (dev->drv, NULL);
g_return_val_if_fail (dev->drv->type != DRIVER_IMAGING, NULL);
return dev->img_dev;
}

View file

@ -26,3 +26,6 @@ struct fp_dev;
* appropriate functions.
*/
struct fp_img_dev;
struct fp_dev *FP_DEV (struct fp_img_dev *dev);
struct fp_img_dev *FP_IMG_DEV (struct fp_dev *dev);

View file

@ -58,7 +58,7 @@ struct fp_img *fpi_img_new(size_t length)
struct fp_img *fpi_img_new_for_imgdev(struct fp_img_dev *imgdev)
{
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(imgdev->dev->drv);
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(FP_DEV(imgdev)->drv);
int width = imgdrv->img_width;
int height = imgdrv->img_height;
struct fp_img *img = fpi_img_new(width * height);
@ -349,7 +349,7 @@ int fpi_img_to_print_data(struct fp_img_dev *imgdev, struct fp_img *img,
/* FIXME: space is wasted if we dont hit the max minutiae count. would
* be good to make this dynamic. */
print = fpi_print_data_new(imgdev->dev);
print = fpi_print_data_new(FP_DEV(imgdev));
item = fpi_print_data_item_new(sizeof(struct xyt_struct));
print->type = PRINT_DATA_NBIS_MINUTIAE;
minutiae_to_xyt(img->minutiae, img->width, img->height, item->data);
@ -555,7 +555,7 @@ fpi_imgdev_get_user_data(struct fp_img_dev *imgdev)
struct fp_dev *
fpi_imgdev_get_dev(struct fp_img_dev *imgdev)
{
return imgdev->dev;
return FP_DEV(imgdev);
}
enum fp_imgdev_enroll_state

View file

@ -33,9 +33,11 @@ static int img_dev_open(struct fp_dev *dev, unsigned long driver_data)
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(dev->drv);
int r = 0;
imgdev->dev = dev;
/* Set up back pointers */
dev->img_dev = imgdev;
imgdev->parent = dev;
imgdev->enroll_stage = 0;
dev->priv = imgdev;
dev->nr_enroll_stages = IMG_ENROLL_STAGES;
/* for consistency in driver code, allow udev access through imgdev */
@ -57,30 +59,29 @@ err:
void fpi_imgdev_open_complete(struct fp_img_dev *imgdev, int status)
{
fpi_drvcb_open_complete(imgdev->dev, status);
fpi_drvcb_open_complete(FP_DEV(imgdev), status);
}
static void img_dev_close(struct fp_dev *dev)
{
struct fp_img_dev *imgdev = dev->priv;
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(dev->drv);
if (imgdrv->close)
imgdrv->close(imgdev);
imgdrv->close(dev->img_dev);
else
fpi_drvcb_close_complete(dev);
}
void fpi_imgdev_close_complete(struct fp_img_dev *imgdev)
{
fpi_drvcb_close_complete(imgdev->dev);
fpi_drvcb_close_complete(FP_DEV(imgdev));
g_free(imgdev);
}
static int dev_change_state(struct fp_img_dev *imgdev,
enum fp_imgdev_state state)
{
struct fp_driver *drv = imgdev->dev->drv;
struct fp_driver *drv = FP_DEV(imgdev)->drv;
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(drv);
if (!imgdrv->change_state)
@ -92,7 +93,7 @@ static int dev_change_state(struct fp_img_dev *imgdev,
* image after freeing the old one. */
static int sanitize_image(struct fp_img_dev *imgdev, struct fp_img **_img)
{
struct fp_driver *drv = imgdev->dev->drv;
struct fp_driver *drv = FP_DEV(imgdev)->drv;
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(drv);
struct fp_img *img = *_img;
@ -150,7 +151,7 @@ void fpi_imgdev_report_finger_status(struct fp_img_dev *imgdev,
if (r == FP_ENROLL_COMPLETE) {
imgdev->enroll_data = NULL;
}
fpi_drvcb_enroll_stage_completed(imgdev->dev, r,
fpi_drvcb_enroll_stage_completed(FP_DEV(imgdev), r,
r == FP_ENROLL_COMPLETE ? data : NULL,
img);
/* the callback can cancel enrollment, so recheck current
@ -163,18 +164,18 @@ void fpi_imgdev_report_finger_status(struct fp_img_dev *imgdev,
}
break;
case IMG_ACTION_VERIFY:
fpi_drvcb_report_verify_result(imgdev->dev, r, img);
fpi_drvcb_report_verify_result(FP_DEV(imgdev), r, img);
imgdev->action_result = 0;
fp_print_data_free(data);
break;
case IMG_ACTION_IDENTIFY:
fpi_drvcb_report_identify_result(imgdev->dev, r,
fpi_drvcb_report_identify_result(FP_DEV(imgdev), r,
imgdev->identify_match_offset, img);
imgdev->action_result = 0;
fp_print_data_free(data);
break;
case IMG_ACTION_CAPTURE:
fpi_drvcb_report_capture_result(imgdev->dev, r, img);
fpi_drvcb_report_capture_result(FP_DEV(imgdev), r, img);
imgdev->action_result = 0;
break;
default:
@ -185,14 +186,14 @@ void fpi_imgdev_report_finger_status(struct fp_img_dev *imgdev,
static void verify_process_img(struct fp_img_dev *imgdev)
{
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(imgdev->dev->drv);
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(FP_DEV(imgdev)->drv);
int match_score = imgdrv->bz3_threshold;
int r;
if (match_score == 0)
match_score = BOZORTH3_DEFAULT_THRESHOLD;
r = fpi_img_compare_print_data(imgdev->dev->verify_data,
r = fpi_img_compare_print_data(FP_DEV(imgdev)->verify_data,
imgdev->acquire_data);
if (r >= match_score)
@ -205,7 +206,7 @@ static void verify_process_img(struct fp_img_dev *imgdev)
static void identify_process_img(struct fp_img_dev *imgdev)
{
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(imgdev->dev->drv);
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(FP_DEV(imgdev)->drv);
int match_score = imgdrv->bz3_threshold;
size_t match_offset;
int r;
@ -214,7 +215,7 @@ static void identify_process_img(struct fp_img_dev *imgdev)
match_score = BOZORTH3_DEFAULT_THRESHOLD;
r = fpi_img_compare_print_data_to_gallery(imgdev->acquire_data,
imgdev->dev->identify_gallery, match_score, &match_offset);
FP_DEV(imgdev)->identify_gallery, match_score, &match_offset);
imgdev->action_result = r;
imgdev->identify_match_offset = match_offset;
@ -272,7 +273,7 @@ void fpi_imgdev_image_captured(struct fp_img_dev *imgdev, struct fp_img *img)
switch (imgdev->action) {
case IMG_ACTION_ENROLL:
if (!imgdev->enroll_data) {
imgdev->enroll_data = fpi_print_data_new(imgdev->dev);
imgdev->enroll_data = fpi_print_data_new(FP_DEV(imgdev));
}
BUG_ON(g_slist_length(print->prints) != 1);
/* Move print data from acquire data into enroll_data */
@ -283,7 +284,7 @@ void fpi_imgdev_image_captured(struct fp_img_dev *imgdev, struct fp_img *img)
fp_print_data_free(imgdev->acquire_data);
imgdev->acquire_data = NULL;
imgdev->enroll_stage++;
if (imgdev->enroll_stage == imgdev->dev->nr_enroll_stages)
if (imgdev->enroll_stage == FP_DEV(imgdev)->nr_enroll_stages)
imgdev->action_result = FP_ENROLL_COMPLETE;
else
imgdev->action_result = FP_ENROLL_PASS;
@ -313,16 +314,16 @@ void fpi_imgdev_session_error(struct fp_img_dev *imgdev, int error)
BUG_ON(error == 0);
switch (imgdev->action) {
case IMG_ACTION_ENROLL:
fpi_drvcb_enroll_stage_completed(imgdev->dev, error, NULL, NULL);
fpi_drvcb_enroll_stage_completed(FP_DEV(imgdev), error, NULL, NULL);
break;
case IMG_ACTION_VERIFY:
fpi_drvcb_report_verify_result(imgdev->dev, error, NULL);
fpi_drvcb_report_verify_result(FP_DEV(imgdev), error, NULL);
break;
case IMG_ACTION_IDENTIFY:
fpi_drvcb_report_identify_result(imgdev->dev, error, 0, NULL);
fpi_drvcb_report_identify_result(FP_DEV(imgdev), error, 0, NULL);
break;
case IMG_ACTION_CAPTURE:
fpi_drvcb_report_capture_result(imgdev->dev, error, NULL);
fpi_drvcb_report_capture_result(FP_DEV(imgdev), error, NULL);
break;
default:
fp_err("unhandled action %d", imgdev->action);
@ -336,16 +337,16 @@ void fpi_imgdev_activate_complete(struct fp_img_dev *imgdev, int status)
switch (imgdev->action) {
case IMG_ACTION_ENROLL:
fpi_drvcb_enroll_started(imgdev->dev, status);
fpi_drvcb_enroll_started(FP_DEV(imgdev), status);
break;
case IMG_ACTION_VERIFY:
fpi_drvcb_verify_started(imgdev->dev, status);
fpi_drvcb_verify_started(FP_DEV(imgdev), status);
break;
case IMG_ACTION_IDENTIFY:
fpi_drvcb_identify_started(imgdev->dev, status);
fpi_drvcb_identify_started(FP_DEV(imgdev), status);
break;
case IMG_ACTION_CAPTURE:
fpi_drvcb_capture_started(imgdev->dev, status);
fpi_drvcb_capture_started(FP_DEV(imgdev), status);
break;
default:
fp_err("unhandled action %d", imgdev->action);
@ -364,16 +365,16 @@ void fpi_imgdev_deactivate_complete(struct fp_img_dev *imgdev)
switch (imgdev->action) {
case IMG_ACTION_ENROLL:
fpi_drvcb_enroll_stopped(imgdev->dev);
fpi_drvcb_enroll_stopped(FP_DEV(imgdev));
break;
case IMG_ACTION_VERIFY:
fpi_drvcb_verify_stopped(imgdev->dev);
fpi_drvcb_verify_stopped(FP_DEV(imgdev));
break;
case IMG_ACTION_IDENTIFY:
fpi_drvcb_identify_stopped(imgdev->dev);
fpi_drvcb_identify_stopped(FP_DEV(imgdev));
break;
case IMG_ACTION_CAPTURE:
fpi_drvcb_capture_stopped(imgdev->dev);
fpi_drvcb_capture_stopped(FP_DEV(imgdev));
break;
default:
fp_err("unhandled action %d", imgdev->action);
@ -386,7 +387,7 @@ void fpi_imgdev_deactivate_complete(struct fp_img_dev *imgdev)
int fpi_imgdev_get_img_width(struct fp_img_dev *imgdev)
{
struct fp_driver *drv = imgdev->dev->drv;
struct fp_driver *drv = FP_DEV(imgdev)->drv;
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(drv);
int width = imgdrv->img_width;
@ -398,7 +399,7 @@ int fpi_imgdev_get_img_width(struct fp_img_dev *imgdev)
int fpi_imgdev_get_img_height(struct fp_img_dev *imgdev)
{
struct fp_driver *drv = imgdev->dev->drv;
struct fp_driver *drv = FP_DEV(imgdev)->drv;
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(drv);
int height = imgdrv->img_height;
@ -410,7 +411,7 @@ int fpi_imgdev_get_img_height(struct fp_img_dev *imgdev)
static int dev_activate(struct fp_img_dev *imgdev, enum fp_imgdev_state state)
{
struct fp_driver *drv = imgdev->dev->drv;
struct fp_driver *drv = FP_DEV(imgdev)->drv;
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(drv);
if (!imgdrv->activate)
@ -420,7 +421,7 @@ static int dev_activate(struct fp_img_dev *imgdev, enum fp_imgdev_state state)
static void dev_deactivate(struct fp_img_dev *imgdev)
{
struct fp_driver *drv = imgdev->dev->drv;
struct fp_driver *drv = FP_DEV(imgdev)->drv;
struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(drv);
if (!imgdrv->deactivate)
@ -430,7 +431,7 @@ static void dev_deactivate(struct fp_img_dev *imgdev)
static int generic_acquire_start(struct fp_dev *dev, int action)
{
struct fp_img_dev *imgdev = dev->priv;
struct fp_img_dev *imgdev = dev->img_dev;
int r;
fp_dbg("action %d", action);
imgdev->action = action;
@ -484,7 +485,7 @@ static int img_dev_capture_start(struct fp_dev *dev)
static int img_dev_enroll_stop(struct fp_dev *dev)
{
struct fp_img_dev *imgdev = dev->priv;
struct fp_img_dev *imgdev = dev->img_dev;
BUG_ON(imgdev->action != IMG_ACTION_ENROLL);
generic_acquire_stop(imgdev);
return 0;
@ -492,7 +493,7 @@ static int img_dev_enroll_stop(struct fp_dev *dev)
static int img_dev_verify_stop(struct fp_dev *dev, gboolean iterating)
{
struct fp_img_dev *imgdev = dev->priv;
struct fp_img_dev *imgdev = dev->img_dev;
BUG_ON(imgdev->action != IMG_ACTION_VERIFY);
generic_acquire_stop(imgdev);
return 0;
@ -500,7 +501,7 @@ static int img_dev_verify_stop(struct fp_dev *dev, gboolean iterating)
static int img_dev_identify_stop(struct fp_dev *dev, gboolean iterating)
{
struct fp_img_dev *imgdev = dev->priv;
struct fp_img_dev *imgdev = dev->img_dev;
BUG_ON(imgdev->action != IMG_ACTION_IDENTIFY);
generic_acquire_stop(imgdev);
imgdev->identify_match_offset = 0;
@ -509,7 +510,7 @@ static int img_dev_identify_stop(struct fp_dev *dev, gboolean iterating)
static int img_dev_capture_stop(struct fp_dev *dev)
{
struct fp_img_dev *imgdev = dev->priv;
struct fp_img_dev *imgdev = dev->img_dev;
BUG_ON(imgdev->action != IMG_ACTION_CAPTURE);
generic_acquire_stop(imgdev);
return 0;