Skip to content

Commit

Permalink
feat: almost complete device.c, there are some enroll process left to do
Browse files Browse the repository at this point in the history
  • Loading branch information
eiguike committed Mar 14, 2018
1 parent 40f0bf6 commit 8f30e7e
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 8 deletions.
6 changes: 6 additions & 0 deletions include/device.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef DEVICE_H
#define DEVICE_H

#include <libfprint/fprint.h>
#include "fingerprint.h"

typedef enum {
Expand All @@ -20,6 +21,11 @@ typedef struct FINGERPRINT_DEVICE {

// Releases DEVICE data structure
void (*Dispose) (struct FINGERPRINT_DEVICE* This);

// Boolean to check if this DEVICE should update its fingerprints
int Outdated;

struct fp_dev* Device;
} DEVICE;

DEVICE* Device_Init(PROCESS_TYPE Type);
Expand Down
213 changes: 205 additions & 8 deletions library/device.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,162 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include "device.h"
#include "fingerprint.h"

extern char* gUrl;
extern char* gPassword;

int
Device_Update (
DEVICE* This
Device_SentRequest_Callback (
char *ptr,
size_t size,
size_t nmemb,
void *userdata
)
{
printf("Device_Update called\n");
return 0;
printf("Device_SentRequest_Callback Begin\n");
char* Aux = NULL;
DEVICE* This = NULL;

if (userdata == NULL) {
goto FINISH;
}

Aux = ptr;
This = (DEVICE*)userdata;

printf("ptr %s\n", Aux);

if(strstr(Aux, "true") != NULL){
printf("OUTDATED!!\n");
This->Outdated = 1;
}else{
printf("UP TO DATE!\n");
This->Outdated = 0;
}

FINISH:
return size*nmemb;
}

int
Device_SentRequest (
DEVICE* This,
int UserId
)
{
if (This == NULL) {
goto FINISH;
}

CURL* Curl = NULL;
CURLcode Resource = 0;
char* LocalUrl = NULL;
char* Body = NULL;
char* AccessBody = "embedded_password=%s&user_id=%d";
char* AccessUrl = "/api/fingerprint/access";

if (UserId != -1) {
UserId = This->Fingerprint->UserIdList[UserId];
}

// log on server that the door was opened
printf("❮ ⬆ ❯ uploading log...\n");

Curl = curl_easy_init();
if (Curl == NULL){
printf("❮ ⚠ ❯ Couldn't get a Curl handler!\n");
} else {
Body = realloc(Body, strlen(AccessBody) + strlen(gPassword) + 4);
sprintf(Body, AccessBody, gPassword, UserId);

/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
LocalUrl = realloc(LocalUrl, strlen(gUrl) + strlen(AccessUrl) + 1);
sprintf(LocalUrl, "%s%s", gUrl, AccessUrl);

curl_easy_setopt(Curl, CURLOPT_URL, LocalUrl);

/* Now specify the POST data */
curl_easy_setopt(Curl, CURLOPT_POSTFIELDS, Body);
curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, Device_SentRequest_Callback);
curl_easy_setopt(Curl, CURLOPT_WRITEDATA, This);

/* Perform the request, Resource will get the return code */
Resource = curl_easy_perform(Curl);

/* Check for errors */
if (Resource != CURLE_OK) {
printf("❮ ⚠ ❯ %s\n", gUrl);
printf("❮ ⚠ ❯ Could not save log on the server! (%s)\n", curl_easy_strerror(Resource));
}else{
printf("❮ ✔ ❯ Log saved to the server\n");
}

/* always cleanup */
curl_easy_cleanup(Curl);
}

FINISH:
if (Body != NULL) {
free(Body);
}
if (LocalUrl != NULL) {
free(LocalUrl);
}
return Resource;
}

int
Device_Verify (
DEVICE* This
)
{
printf("Device_Verify called\n");
return 0;
printf("Device_Verify Begin\n");

if (This == NULL) {
goto FINISH;
}

size_t CacheMatchPos = 0;
int ResultCode = 0;

ResultCode = fp_identify_finger(This->Device, This->Fingerprint->FingerprintList, &CacheMatchPos);
if (ResultCode < 0) {
printf("ERROR MATCHING FINGERPRINT!!\n");
}

switch (ResultCode){
case FP_VERIFY_NO_MATCH:
printf("❮ ☝ ✖ ❯ Fingerprint does not match any database entry\n");
Device_SentRequest(This, -1);
break;

case FP_VERIFY_RETRY:
case FP_VERIFY_RETRY_TOO_SHORT:
case FP_VERIFY_RETRY_CENTER_FINGER:
case FP_VERIFY_RETRY_REMOVE_FINGER:
printf("❮ ☝ ↻ ❯ Failed to read fingerprint, retrying...\n");
break;
case FP_VERIFY_MATCH:
printf("❮ ☝ ✔ ❯ Fingerprint match user ID:\n");
#ifdef WIRINGPILIB
// open the door
digitalWrite(DOOR, HIGH);
delay(500);
digitalWrite(DOOR, LOW);
#endif
Device_SentRequest(This, CacheMatchPos);
break;
}

FINISH:
printf("Device_Verify End\n");
return ResultCode;
}

int
Expand All @@ -41,15 +178,71 @@ Device_Dispose (
This->Fingerprint->Dispose(This->Fingerprint);
This->Fingerprint = NULL;
}
if (This->Device != NULL) {
free(This->Device);
This->Device = NULL;
}
free(This);
}
}

// TODO: REFACTOR THIS FUNCTION
void
Device_InitLibFP (
DEVICE* This
)
{
printf("Device_InitLibFP Begin %d\n", __LINE__);

struct fp_dscv_dev** DevicesFound = NULL;
struct fp_dscv_dev* DiscoverDevice = NULL;
struct fp_driver* Driver = NULL;
struct fp_dev* Device = NULL;

// if the lib couldn't be initialized
if (fp_init() < 0) {
printf("Failed to initialize libfprint!");
}

// find and open fingerprint reader
DevicesFound = fp_discover_devs();

// if no device found, exit the program
if (DevicesFound == NULL || DevicesFound[0] == NULL) {
printf("No reader found!");
}

// list devices found
//cout << "Chosen reader: " << endl;
for (int i = 0; DevicesFound[i] != NULL; i++) {
DiscoverDevice = DevicesFound[i];
Driver = fp_dscv_dev_get_driver(DiscoverDevice);
printf("%s\n", fp_driver_get_full_name(Driver));
}

// choose the first reader to use, and open it
Device = fp_dev_open(DevicesFound[0]);

// check if the reader could be opened
if (Device == NULL) {
printf("Couldn't open selected reader for use!");
} else {
printf("❮ ✔ ❯ Reader ready\n");
}

// free list of devices from memory
fp_dscv_devs_free(DevicesFound);

This->Device = Device;
printf("Device_InitLibFP End %d\n", __LINE__);
}

DEVICE*
Device_Init (
PROCESS_TYPE Type
)
{
printf("Device_Init Begin %d\n", __LINE__);
DEVICE* Device = NULL;

Device = calloc(1, sizeof(DEVICE));
Expand All @@ -62,21 +255,25 @@ Device_Init (
Device->Enroll = Device_Enroll;
Device->Verify = Device_Verify;
Device->Dispose = Device_Dispose;
Device->Update = Device_Update;

switch(Type) {
case ENROLL_PROCESS:
case VERIFY_PROCESS:
Device->Fingerprint = Fingerprint_Init(Type);
if (Device->Fingerprint == NULL) {
goto GENERAL_ERROR;
goto GENERAL_ERROR;
}
break;
default:
printf("Unknown process, exiting...\n");
goto GENERAL_ERROR;
}

Device_InitLibFP(Device);
if(Device->Device == NULL) {
goto GENERAL_ERROR;
}

goto FINISH;

GENERAL_ERROR:
Expand Down

0 comments on commit 8f30e7e

Please sign in to comment.