upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
zfm20.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Thanks to Adafruit for supplying a google translated version of the
6  * Chinese datasheet and some clues in their code.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #pragma once
28 
29 #include <string>
30 #include <iostream>
31 
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <sys/select.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 
42 #include "mraa.hpp"
43 #include "upm_utilities.h"
44 
45 #define ZFM20_DEFAULT_UART 0
46 
47 // protocol start codes
48 #define ZFM20_START1 0xef
49 #define ZFM20_START2 0x01
50 
51 #define ZFM20_MAX_PKT_LEN 256
52 
53 #define ZFM20_TIMEOUT 5000 // in ms
54 
55 #define ZFM20_DEFAULT_PASSWORD 0x00000000
56 #define ZFM20_DEFAULT_ADDRESS 0xffffffff
57 
58 
59 namespace upm {
89  class ZFM20 {
90  public:
91 
92  // commands
93  typedef enum {
94  CMD_GEN_IMAGE = 0x01,
95  CMD_IMG2TZ = 0x02,
96  CMD_MATCH = 0x03,
97  CMD_SEARCH = 0x04,
98  CMD_REGMODEL = 0x05,
99  CMD_STORE = 0x06,
100  CMD_LOAD_TMPL = 0x07,
101  CMD_UPLOAD_TMPL = 0x08,
102  CMD_DOWNLOAD_TMPL = 0x09,
103  CMD_UPLOAD_IMAGE = 0x0a,
104  CMD_DOWNLOAD_IMAGE = 0x0b,
105  CMD_DELETE_TMPL = 0x0c,
106  CMD_EMPTYDB = 0x0d,
107  CMD_SET_SYSPARAMS = 0x0e,
108  CMD_GET_SYSPARAMS = 0x0f,
109  CMD_SET_PASSWORD = 0x12,
110  CMD_VERIFY_PASSWORD = 0x13,
111  CMD_GET_RANDOM_NUMBER = 0x14,
112  CMD_SET_ADDRESS = 0x15,
113  CMD_GET_TMPL_COUNT = 0x1d,
114  CMD_GET_INDEX_TABLE = 0x1f
115  } ZFM20_COMMAND_T;
116 
117  // Error response codes
118  typedef enum {
119  ERR_OK = 0x00,
120  ERR_PACKET_RX_ERROR = 0x01,
121  ERR_NO_FINGER = 0x02,
122  ERR_FP_IMAGE_FAILED = 0x03,
123  ERR_FP_TOO_MESSY = 0x06,
124  ERR_FP_IMAGE_FEW_FEATURES = 0x07,
125  ERR_FP_NOMATCH = 0x08,
126  ERR_FP_NOTFOUND = 0x09,
127  ERR_FP_ENROLLMISMATCH = 0x0a,
128  ERR_BAD_LOCATION = 0x0b,
129  ERR_DB_ERROR = 0x0c,
130  ERR_UPLOAD_FEAT_FAILED = 0x0d,
131  ERR_NO_MORE_PACKETS = 0x0e,
132  ERR_UPLOAD_IMG_FAILED = 0x0f,
133  ERR_RM_TMPL_FAILED = 0x10,
134  ERR_EMPTY_DB_FAILED = 0x11,
135  ERR_INVALID_PWD = 0x13,
136  ERR_INVALID_IMAGE = 0x15,
137  ERR_RW_FLASH_ERROR = 0x18,
138  ERR_INVALID_REG = 0x1a,
139  ERR_INVALID_ADDR = 0x20,
140  ERR_NEEDS_PWD = 0x21,
141  // end of module-specific errors
142  ERR_INTERNAL_ERR = 0xff // API internal error
143  } ZFM20_ERRORS_T;
144 
145  typedef enum {
146  PKT_COMMAND = 0x01,
147  PKT_DATA = 0x02,
148  PKT_ACK = 0x07,
149  PKT_END_DATA = 0x08
150  } ZFM20_PKTCODES_T;
151 
158  ZFM20(int uart, int baud = 57600);
159 
166  ZFM20(std::string uart_raw, int baud = 57600);
167 
171  virtual ~ZFM20() {}
172 
183  int readData(char *buffer, int len);
184 
192  int writeData(char *buffer, int len);
193 
201  bool setupTty(uint32_t baud = 57600);
202 
210  int writeCmdPacket(uint8_t *pkt, int len);
211 
219  bool verifyPacket(uint8_t *pkt, int len);
220 
227  uint32_t getMillis();
228 
233  void initClock();
234 
240  void setAddress(uint32_t addr) { m_address = addr; };
241 
247  void setPassword(uint32_t pw) { m_password = pw; };
248 
257  bool getResponse(uint8_t *pkt, int len);
258 
265  bool verifyPassword();
266 
273  int getNumTemplates();
274 
283  bool setNewPassword(uint32_t pwd);
284 
293  bool setNewAddress(uint32_t addr);
294 
300  uint8_t generateImage();
301 
310  uint8_t image2Tz(int slot);
311 
319  uint8_t createModel();
320 
330  uint8_t storeModel(int slot, uint16_t id);
331 
338  uint8_t deleteModel(uint16_t id);
339 
345  uint8_t deleteDB();
346 
356  uint8_t search(int slot, uint16_t &id, uint16_t &score);
357 
365  uint8_t match(uint16_t &score);
366 
367  private:
368  mraa::Uart m_uart;
369  uint32_t m_password;
370  uint32_t m_address;
371  upm_clock_t m_clock;
372  };
373 }
uint8_t search(int slot, uint16_t &id, uint16_t &score)
Definition: zfm20.cxx:404
bool getResponse(uint8_t *pkt, int len)
Definition: zfm20.cxx:157
int writeData(char *buffer, int len)
Definition: zfm20.cxx:77
uint8_t storeModel(int slot, uint16_t id)
Definition: zfm20.cxx:345
bool setNewPassword(uint32_t pwd)
Definition: zfm20.cxx:238
bool setupTty(uint32_t baud=57600)
Definition: zfm20.cxx:93
API for the ZFM-20 Fingerprint Sensor Module.
Definition: zfm20.hpp:89
void setPassword(uint32_t pw)
Definition: zfm20.hpp:247
uint8_t deleteDB()
Definition: zfm20.cxx:388
uint8_t generateImage()
Definition: zfm20.cxx:292
bool verifyPassword()
Definition: zfm20.cxx:197
bool verifyPacket(uint8_t *pkt, int len)
Definition: zfm20.cxx:142
ZFM20(int uart, int baud=57600)
Definition: zfm20.cxx:36
uint8_t createModel()
Definition: zfm20.cxx:329
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
void setAddress(uint32_t addr)
Definition: zfm20.hpp:240
uint32_t getMillis()
Definition: zfm20.cxx:137
int readData(char *buffer, int len)
Definition: zfm20.cxx:63
virtual ~ZFM20()
Definition: zfm20.hpp:171
int getNumTemplates()
Definition: zfm20.cxx:218
uint8_t image2Tz(int slot)
Definition: zfm20.cxx:308
uint8_t deleteModel(uint16_t id)
Definition: zfm20.cxx:368
int writeCmdPacket(uint8_t *pkt, int len)
Definition: zfm20.cxx:98
bool setNewAddress(uint32_t addr)
Definition: zfm20.cxx:265
uint8_t match(uint16_t &score)
Definition: zfm20.cxx:440
void initClock()
Definition: zfm20.cxx:132