@@ -335,8 +335,10 @@ static int fps_create_model(const struct device *dev)
335
335
336
336
static int fps_store_model (const struct device * dev , uint16_t id )
337
337
{
338
+ struct grow_r502a_data * drv_data = dev -> data ;
338
339
union r502a_packet rx_packet = {0 };
339
340
char const store_model_len = 4 ;
341
+ int ret = 0 ;
340
342
341
343
struct led_params led_ctrl = {
342
344
.ctrl_code = LED_CTRL_BREATHING ,
@@ -351,11 +353,14 @@ static int fps_store_model(const struct device *dev, uint16_t id)
351
353
};
352
354
sys_put_be16 (id , & tx_packet .data [2 ]);
353
355
356
+ k_mutex_lock (& drv_data -> lock , K_FOREVER );
357
+
354
358
transceive_packet (dev , & tx_packet , & rx_packet , store_model_len );
355
359
356
360
if (rx_packet .pid != R502A_ACK_PACKET ) {
357
361
LOG_ERR ("Error receiving ack packet 0x%X" , rx_packet .pid );
358
- return - EIO ;
362
+ ret = - EIO ;
363
+ goto unlock ;
359
364
}
360
365
361
366
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)
366
371
LOG_INF ("Fingerprint stored! at ID #%d" , id );
367
372
} else {
368
373
LOG_ERR ("Error storing model 0x%X" , rx_packet .buf [R502A_CC_IDX ]);
369
- return - EIO ;
374
+ ret = - EIO ;
370
375
}
371
-
372
- return 0 ;
376
+ unlock :
377
+ k_mutex_unlock (& drv_data -> lock );
378
+ return ret ;
373
379
}
374
380
375
381
static int fps_delete_model (const struct device * dev , uint16_t id , uint16_t count )
376
382
{
383
+ struct grow_r502a_data * drv_data = dev -> data ;
377
384
union r502a_packet rx_packet = {0 };
378
385
char const delete_model_len = 5 ;
386
+ int ret = 0 ;
379
387
380
388
union r502a_packet tx_packet = {
381
389
.pid = R502A_COMMAND_PACKET ,
@@ -384,21 +392,25 @@ static int fps_delete_model(const struct device *dev, uint16_t id, uint16_t coun
384
392
sys_put_be16 (id , & tx_packet .data [1 ]);
385
393
sys_put_be16 (count + R502A_DELETE_COUNT_OFFSET , & tx_packet .data [3 ]);
386
394
395
+ k_mutex_lock (& drv_data -> lock , K_FOREVER );
396
+
387
397
transceive_packet (dev , & tx_packet , & rx_packet , delete_model_len );
388
398
389
399
if (rx_packet .pid != R502A_ACK_PACKET ) {
390
400
LOG_ERR ("Error receiving ack packet 0x%X" , rx_packet .pid );
391
- return - EIO ;
401
+ ret = - EIO ;
402
+ goto unlock ;
392
403
}
393
404
394
405
if (rx_packet .buf [R502A_CC_IDX ] == R502A_OK ) {
395
406
LOG_INF ("Fingerprint Deleted from ID #%d to #%d" , id , (id + count ));
396
407
} else {
397
408
LOG_ERR ("Error deleting image 0x%X" , rx_packet .buf [R502A_CC_IDX ]);
398
- return - EIO ;
409
+ ret = - EIO ;
399
410
}
400
-
401
- return 0 ;
411
+ unlock :
412
+ k_mutex_unlock (& drv_data -> lock );
413
+ return ret ;
402
414
}
403
415
404
416
static int fps_empty_db (const struct device * dev )
@@ -429,19 +441,19 @@ static int fps_empty_db(const struct device *dev)
429
441
LOG_ERR ("Error emptying fingerprint library 0x%X" ,
430
442
rx_packet .buf [R502A_CC_IDX ]);
431
443
ret = - EIO ;
432
- goto unlock ;
433
444
}
434
445
435
446
unlock :
436
447
k_mutex_unlock (& drv_data -> lock );
437
448
return ret ;
438
449
}
439
450
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 )
441
452
{
442
453
struct grow_r502a_data * drv_data = dev -> data ;
443
454
union r502a_packet rx_packet = {0 };
444
455
char const search_len = 6 ;
456
+ int ret = 0 ;
445
457
446
458
struct led_params led_ctrl = {
447
459
.ctrl_code = LED_CTRL_BREATHING ,
@@ -452,111 +464,139 @@ static int fps_search(const struct device *dev, uint8_t char_buf_idx)
452
464
453
465
union r502a_packet tx_packet = {
454
466
.pid = R502A_COMMAND_PACKET ,
455
- .data = {R502A_SEARCH , char_buf_idx }
467
+ .data = {R502A_SEARCH , R502A_CHAR_BUF_1 }
456
468
};
457
469
sys_put_be16 (R02A_LIBRARY_START_IDX , & tx_packet .data [2 ]);
458
470
sys_put_be16 (R502A_DEFAULT_CAPACITY , & tx_packet .data [4 ]);
459
471
472
+ k_mutex_lock (& drv_data -> lock , K_FOREVER );
473
+
460
474
transceive_packet (dev , & tx_packet , & rx_packet , search_len );
461
475
462
476
if (rx_packet .pid != R502A_ACK_PACKET ) {
463
477
LOG_ERR ("Error receiving ack packet 0x%X" , rx_packet .pid );
464
- return - EIO ;
478
+ ret = - EIO ;
479
+ goto unlock ;
465
480
}
466
481
467
482
if (rx_packet .buf [R502A_CC_IDX ] == R502A_OK ) {
468
483
led_ctrl .ctrl_code = LED_CTRL_FLASHING ;
469
484
led_ctrl .color_idx = LED_COLOR_PURPLE ;
470
485
led_ctrl .cycle = 0x01 ;
471
486
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 ) {
476
491
led_ctrl .ctrl_code = LED_CTRL_BREATHING ;
477
492
led_ctrl .color_idx = LED_COLOR_RED ;
478
493
led_ctrl .cycle = 0x02 ;
479
494
fps_led_control (dev , & led_ctrl );
480
495
LOG_ERR ("Did not find a match" );
481
- return - ENOENT ;
496
+ ret = - ENOENT ;
482
497
} else {
483
498
led_ctrl .ctrl_code = LED_CTRL_ON_ALWAYS ;
484
499
led_ctrl .color_idx = LED_COLOR_RED ;
485
500
fps_led_control (dev , & led_ctrl );
486
501
LOG_ERR ("Error searching for image 0x%X" , rx_packet .buf [R502A_CC_IDX ]);
487
- return - EIO ;
502
+ ret = - EIO ;
488
503
}
489
-
490
- return 0 ;
504
+ unlock :
505
+ k_mutex_unlock (& drv_data -> lock );
506
+ return ret ;
491
507
}
492
508
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 )
494
510
{
495
511
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 ;
497
515
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 ]);
502
521
503
522
k_mutex_lock (& drv_data -> lock , K_FOREVER );
504
523
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 );
519
525
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 ;
522
529
goto unlock ;
523
530
}
524
531
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 ;
528
538
}
529
539
530
- ret = fps_store_model (dev , val -> val1 );
531
-
532
540
unlock :
533
541
k_mutex_unlock (& drv_data -> lock );
534
542
return ret ;
535
543
}
536
544
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 )
538
546
{
539
547
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
+ };
541
563
542
564
k_mutex_lock (& drv_data -> lock , K_FOREVER );
543
565
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 ;
546
571
goto unlock ;
547
572
}
548
573
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
+ }
551
591
unlock :
552
592
k_mutex_unlock (& drv_data -> lock );
553
593
return ret ;
554
594
}
555
595
556
- static int fps_match (const struct device * dev , struct sensor_value * val )
596
+ static int fps_capture (const struct device * dev )
557
597
{
558
598
struct grow_r502a_data * drv_data = dev -> data ;
559
- int ret = -1 ;
599
+ int ret ;
560
600
561
601
k_mutex_lock (& drv_data -> lock , K_FOREVER );
562
602
@@ -570,12 +610,13 @@ static int fps_match(const struct device *dev, struct sensor_value *val)
570
610
goto unlock ;
571
611
}
572
612
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 ;
577
616
}
578
617
618
+ ret = fps_image_to_char (dev , R502A_CHAR_BUF_2 );
619
+
579
620
unlock :
580
621
k_mutex_unlock (& drv_data -> lock );
581
622
return ret ;
@@ -643,12 +684,18 @@ static int grow_r502a_attr_set(const struct device *dev, enum sensor_channel cha
643
684
}
644
685
645
686
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 );
646
691
case SENSOR_ATTR_R502A_RECORD_ADD :
647
- return fps_enroll (dev , val );
692
+ return fps_store_model (dev , val -> val1 );
648
693
case SENSOR_ATTR_R502A_RECORD_DEL :
649
- return fps_delete (dev , val );
694
+ return fps_delete_model (dev , val -> val1 , val -> val2 );
650
695
case SENSOR_ATTR_R502A_RECORD_EMPTY :
651
696
return fps_empty_db (dev );
697
+ case SENSOR_ATTR_R502A_RECORD_LOAD :
698
+ return fps_load_template (dev , val -> val1 );
652
699
default :
653
700
LOG_ERR ("Sensor attribute not supported" );
654
701
return - ENOTSUP ;
@@ -660,7 +707,6 @@ static int grow_r502a_attr_get(const struct device *dev, enum sensor_channel cha
660
707
enum sensor_attribute attr , struct sensor_value * val )
661
708
{
662
709
int ret ;
663
- struct grow_r502a_data * drv_data = dev -> data ;
664
710
665
711
if ((enum sensor_channel_grow_r502a )chan != SENSOR_CHAN_FINGERPRINT ) {
666
712
LOG_ERR ("Channel not supported" );
@@ -669,11 +715,14 @@ static int grow_r502a_attr_get(const struct device *dev, enum sensor_channel cha
669
715
670
716
switch ((enum sensor_attribute_grow_r502a )attr ) {
671
717
case SENSOR_ATTR_R502A_RECORD_FIND :
672
- ret = fps_match (dev , val );
718
+ ret = fps_search (dev , val );
673
719
break ;
674
720
case SENSOR_ATTR_R502A_RECORD_FREE_IDX :
675
721
ret = fps_read_template_table (dev , & val -> val1 );
676
722
break ;
723
+ case SENSOR_ATTR_R502A_COMPARE :
724
+ ret = fps_match_templates (dev , val );
725
+ break ;
677
726
default :
678
727
LOG_ERR ("Sensor attribute not supported" );
679
728
ret = - ENOTSUP ;
0 commit comments