Skip to content

Commit e57c6f9

Browse files
DineshDK03nashif
authored andcommitted
drivers: sensor: grow_r502a: move enroll, search, delete to application
Removed fps_enroll, fps_search and fps_delete from driver code, since they did application usage inside driver. add the following attributes to achieve the above functionality from application. 1. SENSOR_ATTR_R502A_CAPTURE 2. SENSOR_ATTR_R502A_TEMPLATE_CREATE 3. SENSOR_ATTR_R502A_RECORD_ADD 4. SENSOR_ATTR_R502A_RECORD_LOAD 5. SENSOR_ATTR_R502A_COMPARE Signed-off-by: Dinesh Kumar K <[email protected]>
1 parent 378034c commit e57c6f9

File tree

4 files changed

+192
-84
lines changed

4 files changed

+192
-84
lines changed

drivers/sensor/grow_r502a/grow_r502a.c

Lines changed: 112 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,10 @@ static int fps_create_model(const struct device *dev)
335335

336336
static int fps_store_model(const struct device *dev, uint16_t id)
337337
{
338+
struct grow_r502a_data *drv_data = dev->data;
338339
union r502a_packet rx_packet = {0};
339340
char const store_model_len = 4;
341+
int ret = 0;
340342

341343
struct led_params led_ctrl = {
342344
.ctrl_code = LED_CTRL_BREATHING,
@@ -351,11 +353,14 @@ static int fps_store_model(const struct device *dev, uint16_t id)
351353
};
352354
sys_put_be16(id, &tx_packet.data[2]);
353355

356+
k_mutex_lock(&drv_data->lock, K_FOREVER);
357+
354358
transceive_packet(dev, &tx_packet, &rx_packet, store_model_len);
355359

356360
if (rx_packet.pid != R502A_ACK_PACKET) {
357361
LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
358-
return -EIO;
362+
ret = -EIO;
363+
goto unlock;
359364
}
360365

361366
if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
@@ -366,16 +371,19 @@ static int fps_store_model(const struct device *dev, uint16_t id)
366371
LOG_INF("Fingerprint stored! at ID #%d", id);
367372
} else {
368373
LOG_ERR("Error storing model 0x%X", rx_packet.buf[R502A_CC_IDX]);
369-
return -EIO;
374+
ret = -EIO;
370375
}
371-
372-
return 0;
376+
unlock:
377+
k_mutex_unlock(&drv_data->lock);
378+
return ret;
373379
}
374380

375381
static int fps_delete_model(const struct device *dev, uint16_t id, uint16_t count)
376382
{
383+
struct grow_r502a_data *drv_data = dev->data;
377384
union r502a_packet rx_packet = {0};
378385
char const delete_model_len = 5;
386+
int ret = 0;
379387

380388
union r502a_packet tx_packet = {
381389
.pid = R502A_COMMAND_PACKET,
@@ -384,21 +392,25 @@ static int fps_delete_model(const struct device *dev, uint16_t id, uint16_t coun
384392
sys_put_be16(id, &tx_packet.data[1]);
385393
sys_put_be16(count + R502A_DELETE_COUNT_OFFSET, &tx_packet.data[3]);
386394

395+
k_mutex_lock(&drv_data->lock, K_FOREVER);
396+
387397
transceive_packet(dev, &tx_packet, &rx_packet, delete_model_len);
388398

389399
if (rx_packet.pid != R502A_ACK_PACKET) {
390400
LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
391-
return -EIO;
401+
ret = -EIO;
402+
goto unlock;
392403
}
393404

394405
if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
395406
LOG_INF("Fingerprint Deleted from ID #%d to #%d", id, (id + count));
396407
} else {
397408
LOG_ERR("Error deleting image 0x%X", rx_packet.buf[R502A_CC_IDX]);
398-
return -EIO;
409+
ret = -EIO;
399410
}
400-
401-
return 0;
411+
unlock:
412+
k_mutex_unlock(&drv_data->lock);
413+
return ret;
402414
}
403415

404416
static int fps_empty_db(const struct device *dev)
@@ -429,19 +441,19 @@ static int fps_empty_db(const struct device *dev)
429441
LOG_ERR("Error emptying fingerprint library 0x%X",
430442
rx_packet.buf[R502A_CC_IDX]);
431443
ret = -EIO;
432-
goto unlock;
433444
}
434445

435446
unlock:
436447
k_mutex_unlock(&drv_data->lock);
437448
return ret;
438449
}
439450

440-
static int fps_search(const struct device *dev, uint8_t char_buf_idx)
451+
static int fps_search(const struct device *dev, struct sensor_value *val)
441452
{
442453
struct grow_r502a_data *drv_data = dev->data;
443454
union r502a_packet rx_packet = {0};
444455
char const search_len = 6;
456+
int ret = 0;
445457

446458
struct led_params led_ctrl = {
447459
.ctrl_code = LED_CTRL_BREATHING,
@@ -452,111 +464,139 @@ static int fps_search(const struct device *dev, uint8_t char_buf_idx)
452464

453465
union r502a_packet tx_packet = {
454466
.pid = R502A_COMMAND_PACKET,
455-
.data = {R502A_SEARCH, char_buf_idx}
467+
.data = {R502A_SEARCH, R502A_CHAR_BUF_1}
456468
};
457469
sys_put_be16(R02A_LIBRARY_START_IDX, &tx_packet.data[2]);
458470
sys_put_be16(R502A_DEFAULT_CAPACITY, &tx_packet.data[4]);
459471

472+
k_mutex_lock(&drv_data->lock, K_FOREVER);
473+
460474
transceive_packet(dev, &tx_packet, &rx_packet, search_len);
461475

462476
if (rx_packet.pid != R502A_ACK_PACKET) {
463477
LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
464-
return -EIO;
478+
ret = -EIO;
479+
goto unlock;
465480
}
466481

467482
if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
468483
led_ctrl.ctrl_code = LED_CTRL_FLASHING;
469484
led_ctrl.color_idx = LED_COLOR_PURPLE;
470485
led_ctrl.cycle = 0x01;
471486
fps_led_control(dev, &led_ctrl);
472-
drv_data->finger_id = sys_get_be16(&rx_packet.data[1]);
473-
drv_data->matching_score = sys_get_be16(&rx_packet.data[3]);
474-
LOG_INF("Found a matching print! at ID #%d", drv_data->finger_id);
475-
} else if (rx_packet.buf[R502A_CC_IDX] == R502A_NOT_FOUND) {
487+
val->val1 = sys_get_be16(&rx_packet.data[1]);
488+
val->val2 = sys_get_be16(&rx_packet.data[3]);
489+
LOG_INF("Found a matching print! at ID #%d", val->val1);
490+
} else if (rx_packet.buf[R502A_CC_IDX] == R502A_NOT_FOUND_CC) {
476491
led_ctrl.ctrl_code = LED_CTRL_BREATHING;
477492
led_ctrl.color_idx = LED_COLOR_RED;
478493
led_ctrl.cycle = 0x02;
479494
fps_led_control(dev, &led_ctrl);
480495
LOG_ERR("Did not find a match");
481-
return -ENOENT;
496+
ret = -ENOENT;
482497
} else {
483498
led_ctrl.ctrl_code = LED_CTRL_ON_ALWAYS;
484499
led_ctrl.color_idx = LED_COLOR_RED;
485500
fps_led_control(dev, &led_ctrl);
486501
LOG_ERR("Error searching for image 0x%X", rx_packet.buf[R502A_CC_IDX]);
487-
return -EIO;
502+
ret = -EIO;
488503
}
489-
490-
return 0;
504+
unlock:
505+
k_mutex_unlock(&drv_data->lock);
506+
return ret;
491507
}
492508

493-
static int fps_enroll(const struct device *dev, const struct sensor_value *val)
509+
static int fps_load_template(const struct device *dev, uint16_t id)
494510
{
495511
struct grow_r502a_data *drv_data = dev->data;
496-
int ret = -1;
512+
union r502a_packet rx_packet = {0};
513+
char const load_tmp_len = 4;
514+
int ret = 0;
497515

498-
if (val->val1 < 0 || val->val1 > R502A_DEFAULT_CAPACITY) {
499-
LOG_ERR("Invalid ID number");
500-
return -EINVAL;
501-
}
516+
union r502a_packet tx_packet = {
517+
.pid = R502A_COMMAND_PACKET,
518+
.data = {R502A_LOAD, R502A_CHAR_BUF_1}
519+
};
520+
sys_put_be16(id, &tx_packet.data[2]);
502521

503522
k_mutex_lock(&drv_data->lock, K_FOREVER);
504523

505-
ret = fps_get_image(dev);
506-
if (ret != 0) {
507-
goto unlock;
508-
}
509-
510-
ret = fps_image_to_char(dev, R502A_CHAR_BUF_1);
511-
if (ret != 0) {
512-
goto unlock;
513-
}
514-
515-
ret = fps_get_image(dev);
516-
if (ret != 0) {
517-
goto unlock;
518-
}
524+
transceive_packet(dev, &tx_packet, &rx_packet, load_tmp_len);
519525

520-
ret = fps_image_to_char(dev, R502A_CHAR_BUF_2);
521-
if (ret != 0) {
526+
if (rx_packet.pid != R502A_ACK_PACKET) {
527+
LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
528+
ret = -EIO;
522529
goto unlock;
523530
}
524531

525-
ret = fps_create_model(dev);
526-
if (ret != 0) {
527-
goto unlock;
532+
if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
533+
LOG_DBG("Load template data from id #%d to Char_buffer2", id);
534+
} else {
535+
LOG_ERR("Error Loading template 0x%X",
536+
rx_packet.buf[R502A_CC_IDX]);
537+
ret = -EIO;
528538
}
529539

530-
ret = fps_store_model(dev, val->val1);
531-
532540
unlock:
533541
k_mutex_unlock(&drv_data->lock);
534542
return ret;
535543
}
536544

537-
static int fps_delete(const struct device *dev, const struct sensor_value *val)
545+
static int fps_match_templates(const struct device *dev, struct sensor_value *val)
538546
{
539547
struct grow_r502a_data *drv_data = dev->data;
540-
int ret = -1;
548+
union r502a_packet rx_packet = {0};
549+
char const match_templates_len = 1;
550+
int ret = 0;
551+
552+
struct led_params led_ctrl = {
553+
.ctrl_code = LED_CTRL_BREATHING,
554+
.color_idx = LED_COLOR_BLUE,
555+
.speed = LED_SPEED_HALF,
556+
.cycle = 0x01,
557+
};
558+
559+
union r502a_packet tx_packet = {
560+
.pid = R502A_COMMAND_PACKET,
561+
.data = {R502A_MATCH}
562+
};
541563

542564
k_mutex_lock(&drv_data->lock, K_FOREVER);
543565

544-
ret = fps_delete_model(dev, val->val1, val->val2);
545-
if (ret != 0) {
566+
transceive_packet(dev, &tx_packet, &rx_packet, match_templates_len);
567+
568+
if (rx_packet.pid != R502A_ACK_PACKET) {
569+
LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
570+
ret = -EIO;
546571
goto unlock;
547572
}
548573

549-
ret = fps_get_template_count(dev);
550-
574+
if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
575+
fps_led_control(dev, &led_ctrl);
576+
val->val1 = R502A_FINGER_MATCH_FOUND;
577+
val->val2 = sys_get_be16(&rx_packet.data[1]);
578+
LOG_INF("Fingerprint matched with a score %d", val->val2);
579+
} else if (rx_packet.buf[R502A_CC_IDX] == R502A_NOT_MATCH_CC) {
580+
val->val1 = R502A_FINGER_MATCH_NOT_FOUND;
581+
LOG_ERR("Fingerprint not matched");
582+
ret = -ENOENT;
583+
} else {
584+
led_ctrl.ctrl_code = LED_CTRL_ON_ALWAYS;
585+
led_ctrl.color_idx = LED_COLOR_RED;
586+
fps_led_control(dev, &led_ctrl);
587+
LOG_ERR("Error Matching templates 0x%X",
588+
rx_packet.buf[R502A_CC_IDX]);
589+
ret = -EIO;
590+
}
551591
unlock:
552592
k_mutex_unlock(&drv_data->lock);
553593
return ret;
554594
}
555595

556-
static int fps_match(const struct device *dev, struct sensor_value *val)
596+
static int fps_capture(const struct device *dev)
557597
{
558598
struct grow_r502a_data *drv_data = dev->data;
559-
int ret = -1;
599+
int ret;
560600

561601
k_mutex_lock(&drv_data->lock, K_FOREVER);
562602

@@ -570,12 +610,13 @@ static int fps_match(const struct device *dev, struct sensor_value *val)
570610
goto unlock;
571611
}
572612

573-
ret = fps_search(dev, R502A_CHAR_BUF_1);
574-
if (ret == 0) {
575-
val->val1 = drv_data->finger_id;
576-
val->val2 = drv_data->matching_score;
613+
ret = fps_get_image(dev);
614+
if (ret != 0) {
615+
goto unlock;
577616
}
578617

618+
ret = fps_image_to_char(dev, R502A_CHAR_BUF_2);
619+
579620
unlock:
580621
k_mutex_unlock(&drv_data->lock);
581622
return ret;
@@ -643,12 +684,18 @@ static int grow_r502a_attr_set(const struct device *dev, enum sensor_channel cha
643684
}
644685

645686
switch ((enum sensor_attribute_grow_r502a)attr) {
687+
case SENSOR_ATTR_R502A_CAPTURE:
688+
return fps_capture(dev);
689+
case SENSOR_ATTR_R502A_TEMPLATE_CREATE:
690+
return fps_create_model(dev);
646691
case SENSOR_ATTR_R502A_RECORD_ADD:
647-
return fps_enroll(dev, val);
692+
return fps_store_model(dev, val->val1);
648693
case SENSOR_ATTR_R502A_RECORD_DEL:
649-
return fps_delete(dev, val);
694+
return fps_delete_model(dev, val->val1, val->val2);
650695
case SENSOR_ATTR_R502A_RECORD_EMPTY:
651696
return fps_empty_db(dev);
697+
case SENSOR_ATTR_R502A_RECORD_LOAD:
698+
return fps_load_template(dev, val->val1);
652699
default:
653700
LOG_ERR("Sensor attribute not supported");
654701
return -ENOTSUP;
@@ -660,7 +707,6 @@ static int grow_r502a_attr_get(const struct device *dev, enum sensor_channel cha
660707
enum sensor_attribute attr, struct sensor_value *val)
661708
{
662709
int ret;
663-
struct grow_r502a_data *drv_data = dev->data;
664710

665711
if ((enum sensor_channel_grow_r502a)chan != SENSOR_CHAN_FINGERPRINT) {
666712
LOG_ERR("Channel not supported");
@@ -669,11 +715,14 @@ static int grow_r502a_attr_get(const struct device *dev, enum sensor_channel cha
669715

670716
switch ((enum sensor_attribute_grow_r502a)attr) {
671717
case SENSOR_ATTR_R502A_RECORD_FIND:
672-
ret = fps_match(dev, val);
718+
ret = fps_search(dev, val);
673719
break;
674720
case SENSOR_ATTR_R502A_RECORD_FREE_IDX:
675721
ret = fps_read_template_table(dev, &val->val1);
676722
break;
723+
case SENSOR_ATTR_R502A_COMPARE:
724+
ret = fps_match_templates(dev, val);
725+
break;
677726
default:
678727
LOG_ERR("Sensor attribute not supported");
679728
ret = -ENOTSUP;

drivers/sensor/grow_r502a/grow_r502a.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
*/
6262

6363
#define R502A_OK 0x00 /*commad execution complete*/
64-
#define R502A_NOT_FOUND 0x09 /*fail to find the matching finger*/
6564

6665
/*Package Identifier's definition*/
6766
#define R502A_COMMAND_PACKET 0x1 /*Command packet*/
@@ -95,6 +94,11 @@
9594
#define R502A_HANDSHAKE 0x40 /*Handshake*/
9695
#define R502A_BADPACKET 0xFE /* Bad packet was sent*/
9796

97+
#define R502A_NOT_MATCH_CC 0x08 /* templates of two buffers not matching*/
98+
#define R502A_NOT_FOUND_CC 0x09 /*fail to find the matching finger*/
99+
#define R502A_FINGER_MATCH_NOT_FOUND 0
100+
#define R502A_FINGER_MATCH_FOUND 1
101+
98102
#define R502A_STARTCODE 0xEF01 /*Fixed value, High byte transferred first*/
99103
#define R502A_DEFAULT_PASSWORD 0x00000000
100104
#define R502A_DEFAULT_ADDRESS 0xFFFFFFFF
@@ -187,8 +191,6 @@ struct grow_r502a_data {
187191
struct k_mutex lock;
188192
struct k_sem uart_rx_sem;
189193

190-
uint16_t finger_id;
191-
uint16_t matching_score;
192194
uint16_t template_count;
193195
};
194196

0 commit comments

Comments
 (0)