1/*
2 * Author: Jon Trulson <jtrulson@ics.com>
3 * Copyright (c) 2015 Intel Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <unistd.h>
26#include <iostream>
27#include <signal.h>
28#include "zfm20.h"
29
30using namespace std;
31using namespace upm;
32
33int main (int argc, char **argv)
34{
35//! [Interesting]
36  // Instantiate a ZFM20 Fingerprint reader on UART 0
37
38  upm::ZFM20* fp = new upm::ZFM20(0);
39
40  // make sure port is initialized properly.  57600 baud is the default.
41  if (!fp->setupTty(B57600))
42    {
43      cerr << "Failed to setup tty port parameters" << endl;
44      return 1;
45    }
46
47  // This example demonstrates registering a fingerprint on the zfm20
48  // module.  The procedure is as follows:
49  //
50  // 1. get an image, store it in characteristics buffer 1
51  // 2. get another image, store it in characteristics buffer 2
52  // 3. store the image, assuming the two fingerprints match
53
54  // first, we need to register our address and password
55
56  fp->setPassword(ZFM20_DEFAULT_PASSWORD);
57  fp->setAddress(ZFM20_DEFAULT_ADDRESS);
58
59  // now verify the password.  If this fails, any other commands
60  // will be ignored, so we just bail.
61  if (fp->verifyPassword())
62    {
63      cout << "Password verified." << endl;
64    }
65  else
66    {
67      cerr << "Password verification failed." << endl;
68      return 1;
69    }
70
71  cout << endl;
72
73  uint8_t rv;
74  // get the first image
75
76  cout << "Place a finger on the sensor." << endl;
77  while (fp->generateImage() != ZFM20::ERR_OK)
78    ;
79
80  // in theory, we have an image
81  cout << "Image captured, converting..." << endl;
82
83  if ((rv = fp->image2Tz(1)) != ZFM20::ERR_OK)
84    {
85      cerr << "Image conversion failed with error code " << int(rv) <<endl;
86      return 1;
87    }
88
89  cout << "Image conversion succeeded, remove finger." << endl;
90  sleep(1);
91
92  while (fp->generateImage() != ZFM20::ERR_NO_FINGER)
93    ;
94
95  cout << endl;
96  cout << "Now place the same finger on the sensor." << endl;
97
98  while (fp->generateImage() != ZFM20::ERR_OK)
99    ;
100
101  cout << "Image captured, converting..." << endl;
102
103  // save this one in slot 2
104  if ((rv = fp->image2Tz(2)) != ZFM20::ERR_OK)
105    {
106      cerr << "Image conversion failed with error code " << int(rv) <<endl;
107      return 1;
108    }
109
110  cout << "Image conversion succeeded, remove finger." << endl;
111  cout << endl;
112
113  cout << "Storing fingerprint at id 1" << endl;
114
115  // create the model
116  if ((rv = fp->createModel()) != ZFM20::ERR_OK)
117    {
118      if (rv == ZFM20::ERR_FP_ENROLLMISMATCH)
119        cerr << "Fingerprints did not match." << endl;
120      else
121        cerr << "createModel failed with error code " << int(rv) <<endl;
122
123      return 1;
124    }
125
126  // now store it, we hard code the id (second arg) to 1 here
127  if ((rv = fp->storeModel(1, 1)) != ZFM20::ERR_OK)
128    {
129      cerr << "storeModel failed with error code " << int(rv) <<endl;
130      return 1;
131    }
132
133  cout << endl;
134  cout << "Fingerprint stored at id 1." << endl;
135
136//! [Interesting]
137
138  delete fp;
139  return 0;
140}
141