|
| 1 | +/** |
| 2 | + * @file fingerprint.c |
| 3 | + * |
| 4 | + * @author Matthew Matz |
| 5 | + * |
| 6 | + * @version 0.50 |
| 7 | + * |
| 8 | + * @copyright |
| 9 | + * Copyright (C) Parallax, Inc. 2017. All Rights MIT Licensed. |
| 10 | + * |
| 11 | + * @brief Simplifies reading the WaveShare fingerprint scanner module. |
| 12 | + */ |
| 13 | + |
| 14 | + |
| 15 | +#include "fingerprint.h" |
| 16 | + |
| 17 | + |
| 18 | +fpScanner *fingerprint_open(int pin_rx, int pin_tx) |
| 19 | +{ |
| 20 | + int mode = 0; |
| 21 | + int baudrate = 19200; |
| 22 | + int txpin = pin_tx; |
| 23 | + int rxpin = pin_rx; |
| 24 | + |
| 25 | + extern int binary_pst_dat_start[]; |
| 26 | + |
| 27 | + fpScanner_t *fpsptr; |
| 28 | + |
| 29 | + /* can't use array instead of malloc because it would go out of scope. */ |
| 30 | + char* bufptr = (char*) malloc(2*(FDSERIAL_BUFF_MASK+1)); |
| 31 | + fpScanner* term = (fpScanner*) malloc(sizeof(fpScanner)); |
| 32 | + memset(term, 0, sizeof(fpScanner)); |
| 33 | + |
| 34 | + fpsptr = (void*) malloc(sizeof(fpScanner_t)); |
| 35 | + term->devst = fpsptr; |
| 36 | + memset((char*)fpsptr, 0, sizeof(fpScanner_t)); |
| 37 | + |
| 38 | + if(rxpin == 31 && txpin == 30) { |
| 39 | + simpleterm_close(); |
| 40 | + } |
| 41 | + |
| 42 | + /* required for terminal to work */ |
| 43 | + term->txChar = fdserial_txChar; |
| 44 | + term->rxChar = fdserial_rxChar; |
| 45 | + |
| 46 | + fpsptr->rx_pin = rxpin; /* recieve pin */ |
| 47 | + fpsptr->tx_pin = txpin; /* transmit pin */ |
| 48 | + fpsptr->mode = mode; /* interface mode */ |
| 49 | + //fpsptr->en = enablePin; /* interface mode */ |
| 50 | + |
| 51 | + /* baud from clkfreq (cpu clock typically 80000000 for 5M*pll16x) */ |
| 52 | + fpsptr->ticks = CLKFREQ/baudrate; |
| 53 | + fpsptr->buffptr = bufptr; /* receive and transmit buffer */ |
| 54 | + |
| 55 | + term->cogid[0] = setStopCOGID(cognew((void*)binary_pst_dat_start, (void*)fpsptr)); |
| 56 | + |
| 57 | + waitcnt(CLKFREQ/10+CNT); // give cog chance to load |
| 58 | + |
| 59 | + return term; |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +void fingerprint_close(fpScanner *device) |
| 65 | +{ |
| 66 | + int id = device->cogid[0]; |
| 67 | + fpScanner_t *fpsptr = (fpScanner_t*) device->devst; |
| 68 | + |
| 69 | + while(fdserial_rxCheck(device) >= 0); // clear out queue by receiving all available |
| 70 | + fdserial_txFlush(device); |
| 71 | + |
| 72 | + if(id > 0) cogstop(getStopCOGID(id)); |
| 73 | + |
| 74 | + free((void*)fpsptr->buffptr); |
| 75 | + free((void*)fpsptr); |
| 76 | + free(device); |
| 77 | + device = 0; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +void fingerprint_sendCommand(fpScanner *device, char __fpCmd, char __fpParam1, char __fpParam2, char __fpParam3) |
| 83 | +{ |
| 84 | + char __fpChk = __fpCmd; |
| 85 | + __fpChk ^= __fpParam1; |
| 86 | + __fpChk ^= __fpParam2; |
| 87 | + __fpChk ^= __fpParam3; |
| 88 | + __fpChk ^= 0; |
| 89 | + |
| 90 | + fdserial_txChar(device, 0xF5); |
| 91 | + fdserial_txChar(device, __fpCmd); |
| 92 | + fdserial_txChar(device, __fpParam1); |
| 93 | + fdserial_txChar(device, __fpParam2); |
| 94 | + fdserial_txChar(device, __fpParam3); |
| 95 | + fdserial_txChar(device, 0); |
| 96 | + fdserial_txChar(device, __fpChk); |
| 97 | + fdserial_txChar(device, 0xF5); |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +void fingerprint_readResponse(fpScanner *device, char *__fpResponse) |
| 104 | +{ |
| 105 | + char __read_fp[8]; |
| 106 | + char __fpChk = 0; |
| 107 | + for(char idx = 0; idx < 8; idx++) |
| 108 | + { |
| 109 | + __read_fp[idx] = fdserial_rxChar(device); |
| 110 | + if(idx > 0 && idx < 6) __fpChk ^= __read_fp[idx]; |
| 111 | + if(idx > 1 && idx < 5) __fpResponse[idx-2] = __read_fp[idx]; |
| 112 | + } |
| 113 | + __fpResponse[4] = 0; |
| 114 | + if(__read_fp[0] == 0xF5 && __read_fp[7] == 0xF5 && __read_fp[6] == __fpChk) __fpResponse[5] = 0xF5; |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +int fingerprint_allowOverwrite(fpScanner *device, char b) |
| 120 | +{ |
| 121 | + char __fpBuf[8]; |
| 122 | + char __allowOverwrite = 0; |
| 123 | + if(b == 0) __allowOverwrite = 1; |
| 124 | + fingerprint_sendCommand(device, CMD_SET_MODE, 0, __allowOverwrite, 0); |
| 125 | + fingerprint_readResponse(device, __fpBuf); |
| 126 | + return (int) __fpBuf[2]; |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +int fingerprint_add(fpScanner *device, int userId, char userLevel, int scanNumber) |
| 132 | +{ |
| 133 | + userLevel = 0x3 & userLevel; |
| 134 | + char __u2 = userId & 0xFF; |
| 135 | + char __u1 = (userId >> 8) & 0xFF; |
| 136 | + char __fpBuf[8]; |
| 137 | + |
| 138 | + if(userId < 1) return ACK_NOUSER; |
| 139 | + else { |
| 140 | + if(scanNumber < 1 || scanNumber > 3) { |
| 141 | + fingerprint_sendCommand(device, CMD_ADD_FINGERPRINT_1, __u1, __u2, userLevel); |
| 142 | + fingerprint_readResponse(device, __fpBuf); |
| 143 | + if(__fpBuf[2] != ACK_SUCCESS) return (int) __fpBuf[2]; |
| 144 | + pause(1000); |
| 145 | + fingerprint_sendCommand(device, CMD_ADD_FINGERPRINT_2, __u1, __u2, userLevel); |
| 146 | + fingerprint_readResponse(device, __fpBuf); |
| 147 | + if(__fpBuf[2] != ACK_SUCCESS) return (int) __fpBuf[2]; |
| 148 | + pause(1000); |
| 149 | + fingerprint_sendCommand(device, CMD_ADD_FINGERPRINT_3, __u1, __u2, userLevel); |
| 150 | + fingerprint_readResponse(device, __fpBuf); |
| 151 | + } else if(scanNumber == 1) { |
| 152 | + fingerprint_sendCommand(device, CMD_ADD_FINGERPRINT_1, __u1, __u2, userLevel); |
| 153 | + fingerprint_readResponse(device, __fpBuf); |
| 154 | + } else if(scanNumber == 2) { |
| 155 | + fingerprint_sendCommand(device, CMD_ADD_FINGERPRINT_2, __u1, __u2, userLevel); |
| 156 | + fingerprint_readResponse(device, __fpBuf); |
| 157 | + } else if(scanNumber == 3) { |
| 158 | + fingerprint_sendCommand(device, CMD_ADD_FINGERPRINT_3, __u1, __u2, userLevel); |
| 159 | + fingerprint_readResponse(device, __fpBuf); |
| 160 | + } |
| 161 | + |
| 162 | + return (int) __fpBuf[4]; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +int fingerprint_deleteUser(fpScanner *device, int userId) |
| 169 | +{ |
| 170 | + char __u2 = userId & 0xFF; |
| 171 | + char __u1 = (userId >> 8) & 0xFF; |
| 172 | + char __fpBuf[8]; |
| 173 | + |
| 174 | + if(userId < 1) { |
| 175 | + fingerprint_sendCommand(device, CMD_DELETE_ALL_USERS, 0, 0, 0); |
| 176 | + fingerprint_readResponse(device, __fpBuf); |
| 177 | + high(26); |
| 178 | + } else { |
| 179 | + fingerprint_sendCommand(device, CMD_DELETE_USER, __u1, __u2, 0); |
| 180 | + fingerprint_readResponse(device, __fpBuf); |
| 181 | + } |
| 182 | + |
| 183 | + return (int) __fpBuf[4]; |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | +int fingerprint_countUsers(fpScanner *device) |
| 189 | +{ |
| 190 | + char __fpBuf[8]; |
| 191 | + |
| 192 | + fingerprint_sendCommand(device, CMD_GET_USERS_COUNT, 0, 0, 0); |
| 193 | + fingerprint_readResponse(device, __fpBuf); |
| 194 | + |
| 195 | + if (__fpBuf[2] == 0x00) return ((((int) __fpBuf[0]) << 8) | ((int) __fpBuf[1])); |
| 196 | + else return -1; |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | +int fingerprint_scan(fpScanner *device, int userId, int *uid) |
| 202 | +{ |
| 203 | + char __u2 = userId & 0xFF; |
| 204 | + char __u1 = (userId >> 8) & 0xFF; |
| 205 | + char __fpBuf[8]; |
| 206 | + int idTemp = 0; |
| 207 | + *uid = 0; |
| 208 | + |
| 209 | + if(userId < 1) { |
| 210 | + fingerprint_sendCommand(device, CMD_SCAN_COMPARE_1_TO_N, 0, 0, 0); |
| 211 | + fingerprint_readResponse(device, __fpBuf); |
| 212 | + *uid = ((((int) __fpBuf[0]) << 8) | ((int) __fpBuf[1])); |
| 213 | + } else { |
| 214 | + fingerprint_sendCommand(device, CMD_SCAN_COMPARE_1_TO_1, __u1, __u2, 0); |
| 215 | + fingerprint_readResponse(device, __fpBuf); |
| 216 | + if(__fpBuf[2] == ACK_SUCCESS) { |
| 217 | + *uid = userId; |
| 218 | + fingerprint_sendCommand(device, CMD_READ_USER_PRIVLAGE, __u1, __u2, 0); |
| 219 | + fingerprint_readResponse(device, __fpBuf); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return (int) __fpBuf[2]; |
| 224 | +} |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | +int fingerprint_lookupUserPrivlage(fpScanner *device, int userId) |
| 229 | +{ |
| 230 | + char __u2 = userId & 0xFF; |
| 231 | + char __u1 = (userId >> 8) & 0xFF; |
| 232 | + char __fpBuf[8]; |
| 233 | + |
| 234 | + if(userId > 0) { |
| 235 | + fingerprint_sendCommand(device, CMD_READ_USER_PRIVLAGE, __u1, __u2, 0); |
| 236 | + fingerprint_readResponse(device, __fpBuf); |
| 237 | + } |
| 238 | + |
| 239 | + return (int) __fpBuf[2]; |
| 240 | +} |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | +int fingerprint_setTimeout(fpScanner *device, int timeout) |
| 245 | +{ |
| 246 | + char __fpBuf[8]; |
| 247 | + unsigned char tto = (unsigned char) abs(timeout); // convert roughly to milliseconds |
| 248 | + |
| 249 | + fingerprint_sendCommand(device, CMD_SET_SCAN_TIMEOUT, 0, tto, 0); |
| 250 | + fingerprint_readResponse(device, __fpBuf); |
| 251 | + |
| 252 | + return (int) __fpBuf[2]; |
| 253 | +} |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | +int fingerprint_setStrictness(fpScanner *device, char s_level) |
| 258 | +{ |
| 259 | + char __fpBuf[8]; |
| 260 | + if(s_level > 9) s_level = 9; |
| 261 | + if(s_level < 0) s_level = 0; |
| 262 | + |
| 263 | + fingerprint_sendCommand(device, CMD_SENSITIVITY, 0, s_level, 0); |
| 264 | + fingerprint_readResponse(device, __fpBuf); |
| 265 | + |
| 266 | + return (int) __fpBuf[2]; |
| 267 | +} |
| 268 | + |
| 269 | + |
| 270 | +/** |
| 271 | + * TERMS OF USE: MIT License |
| 272 | + * |
| 273 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 274 | + * copy of this software and associated documentation files (the "Software"), |
| 275 | + * to deal in the Software without restriction, including without limitation |
| 276 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 277 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 278 | + * Software is furnished to do so, subject to the following conditions: |
| 279 | + * |
| 280 | + * The above copyright notice and this permission notice shall be included in |
| 281 | + * all copies or substantial portions of the Software. |
| 282 | + * |
| 283 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 284 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 285 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 286 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 287 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 288 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 289 | + * DEALINGS IN THE SOFTWARE. |
| 290 | + */ |
0 commit comments