upm  0.2.0
Sensor/Actuator repository for libmraa (v0.6.1)
zfm20.h
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 <termios.h>
39 #include <sys/time.h>
40 #include <sys/select.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 
44 #include <mraa/uart.h>
45 
46 #define ZFM20_DEFAULT_UART 0
47 
48 // protocol start codes
49 #define ZFM20_START1 0xef
50 #define ZFM20_START2 0x01
51 
52 #define ZFM20_MAX_PKT_LEN 256
53 
54 #define ZFM20_TIMEOUT 5000 // in ms
55 
56 #define ZFM20_DEFAULT_PASSWORD 0x00000000
57 #define ZFM20_DEFAULT_ADDRESS 0xffffffff
58 
59 
60 namespace upm {
88  class ZFM20 {
89  public:
90 
91  // commands
92  typedef enum {
93  CMD_GEN_IMAGE = 0x01,
94  CMD_IMG2TZ = 0x02,
95  CMD_MATCH = 0x03,
96  CMD_SEARCH = 0x04,
97  CMD_REGMODEL = 0x05,
98  CMD_STORE = 0x06,
99  CMD_LOAD_TMPL = 0x07,
100  CMD_UPLOAD_TMPL = 0x08,
101  CMD_DOWNLOAD_TMPL = 0x09,
102  CMD_UPLOAD_IMAGE = 0x0a,
103  CMD_DOWNLOAD_IMAGE = 0x0b,
104  CMD_DELETE_TMPL = 0x0c,
105  CMD_EMPTYDB = 0x0d,
106  CMD_SET_SYSPARAMS = 0x0e,
107  CMD_GET_SYSPARAMS = 0x0f,
108  CMD_SET_PASSWORD = 0x12,
109  CMD_VERIFY_PASSWORD = 0x13,
110  CMD_GET_RANDOM_NUMBER = 0x14,
111  CMD_SET_ADDRESS = 0x15,
112  CMD_GET_TMPL_COUNT = 0x1d,
113  CMD_GET_INDEX_TABLE = 0x1f
114  } ZFM20_COMMAND_T;
115 
116  // Error response codes
117  typedef enum {
118  ERR_OK = 0x00,
119  ERR_PACKET_RX_ERROR = 0x01,
120  ERR_NO_FINGER = 0x02,
121  ERR_FP_IMAGE_FAILED = 0x03,
122  ERR_FP_TOO_MESSY = 0x06,
123  ERR_FP_IMAGE_FEW_FEATURES = 0x07,
124  ERR_FP_NOMATCH = 0x08,
125  ERR_FP_NOTFOUND = 0x09,
126  ERR_FP_ENROLLMISMATCH = 0x0a,
127  ERR_BAD_LOCATION = 0x0b,
128  ERR_DB_ERROR = 0x0c,
129  ERR_UPLOAD_FEAT_FAILED = 0x0d,
130  ERR_NO_MORE_PACKETS = 0x0e,
131  ERR_UPLOAD_IMG_FAILED = 0x0f,
132  ERR_RM_TMPL_FAILED = 0x10,
133  ERR_EMPTY_DB_FAILED = 0x11,
134  ERR_INVALID_PWD = 0x13,
135  ERR_INVALID_IMAGE = 0x15,
136  ERR_RW_FLASH_ERROR = 0x18,
137  ERR_INVALID_REG = 0x1a,
138  ERR_INVALID_ADDR = 0x20,
139  ERR_NEEDS_PWD = 0x21,
140  // end of module specific errors
141  ERR_INTERNAL_ERR = 0xff // API internal error
142  } ZFM20_ERRORS_T;
143 
144  typedef enum {
145  PKT_COMMAND = 0x01,
146  PKT_DATA = 0x02,
147  PKT_ACK = 0x07,
148  PKT_END_DATA = 0x08
149  } ZFM20_PKTCODES_T;
150 
156  ZFM20(int uart);
157 
161  ~ZFM20();
162 
169  bool dataAvailable(unsigned int millis);
170 
181  int readData(char *buffer, size_t len);
182 
190  int writeData(char *buffer, size_t len);
191 
199  bool setupTty(speed_t baud=B57600);
200 
208  int writeCmdPacket(unsigned char *pkt, int len);
209 
216  bool verifyPacket(unsigned char *pkt);
217 
224  uint32_t getMillis();
225 
230  void initClock();
231 
237  void setAddress(uint32_t addr) { m_address = addr; };
238 
244  void setPassword(uint32_t pw) { m_password = pw; };
245 
254  bool getResponse(unsigned char *pkt, int len);
255 
262  bool verifyPassword();
263 
270  int getNumTemplates();
271 
280  bool setNewPassword(uint32_t pwd);
281 
290  bool setNewAddress(uint32_t addr);
291 
297  uint8_t generateImage();
298 
307  uint8_t image2Tz(int slot);
308 
316  uint8_t createModel();
317 
327  uint8_t storeModel(int slot, uint16_t id);
328 
335  uint8_t deleteModel(uint16_t id);
336 
342  uint8_t deleteDB();
343 
353  uint8_t search(int slot, uint16_t *id, uint16_t *score);
354 
362  uint8_t match(uint16_t *score);
363 
364 
365  protected:
366  int ttyFd() { return m_ttyFd; };
367  int setTtyFd(int fd) { m_ttyFd = fd; };
368 
369  private:
370  mraa_uart_context m_uart;
371  int m_ttyFd;
372  uint32_t m_password;
373  uint32_t m_address;
374  struct timeval m_startTime;
375  };
376 }
377 
378 
int writeCmdPacket(unsigned char *pkt, int len)
Definition: zfm20.cxx:165
int writeData(char *buffer, size_t len)
Definition: zfm20.cxx:116
uint8_t search(int slot, uint16_t *id, uint16_t *score)
Definition: zfm20.cxx:597
uint8_t match(uint16_t *score)
Definition: zfm20.cxx:643
bool getResponse(unsigned char *pkt, int len)
Definition: zfm20.cxx:252
uint8_t storeModel(int slot, uint16_t id)
Definition: zfm20.cxx:512
bool setNewPassword(uint32_t pwd)
Definition: zfm20.cxx:357
C++ API for the ZFM-20 fingerprint sensor module.
Definition: zfm20.h:88
void setPassword(uint32_t pw)
Definition: zfm20.h:244
uint8_t deleteDB()
Definition: zfm20.cxx:573
uint8_t generateImage()
Definition: zfm20.cxx:433
bool verifyPassword()
Definition: zfm20.cxx:297
ZFM20(int uart)
Definition: zfm20.cxx:34
uint8_t createModel()
Definition: zfm20.cxx:488
Definition: a110x.h:29
void setAddress(uint32_t addr)
Definition: zfm20.h:237
~ZFM20()
Definition: zfm20.cxx:68
bool verifyPacket(unsigned char *pkt)
Definition: zfm20.cxx:232
uint32_t getMillis()
Definition: zfm20.cxx:204
bool setupTty(speed_t baud=B57600)
Definition: zfm20.cxx:137
int getNumTemplates()
Definition: zfm20.cxx:325
uint8_t image2Tz(int slot)
Definition: zfm20.cxx:457
uint8_t deleteModel(uint16_t id)
Definition: zfm20.cxx:545
bool setNewAddress(uint32_t addr)
Definition: zfm20.cxx:395
void initClock()
Definition: zfm20.cxx:199
int readData(char *buffer, size_t len)
Definition: zfm20.cxx:100
bool dataAvailable(unsigned int millis)
Definition: zfm20.cxx:76