From 31c70ef771e5954dffc5629700e28a89e8ecd493 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sat, 24 May 2025 15:15:09 +0800 Subject: Update FPM driver and enroll if there are no finger prints. --- lock/client.c | 3 + lock/fpm.c | 214 +++++++++++++++++++++++++++++++++++++++------------------- lock/fpm.h | 22 ++++-- 3 files changed, 164 insertions(+), 75 deletions(-) (limited to 'lock') diff --git a/lock/client.c b/lock/client.c index c5e8729..2173987 100644 --- a/lock/client.c +++ b/lock/client.c @@ -67,7 +67,10 @@ int main(void) wdt_off(); led_init(); + fpm_init(); + if (fpm_get_count() == 0) + fpm_enroll(); init_rx(); init_btns(); diff --git a/lock/fpm.c b/lock/fpm.c index 056d0ac..57e9ac0 100644 --- a/lock/fpm.c +++ b/lock/fpm.c @@ -4,13 +4,15 @@ #include "fpm.h" -#define MAXPDLEN 64 -#define RST_DELAY_MS 500 +#define MAXPDLEN 64 +#define RST_DELAY_MS 500 -#define OK 0x00 +#define HEADER_HO 0xEF +#define HEADER_LO 0x01 +#define ADDR 0xFFFFFFFF -static uint8_t start_code[] = { 0xEF, 0x01 }; -static uint8_t addr[] = { 0xFF, 0xFF, 0xFF, 0xFF }; +#define OK 0x00 +#define PACKID 0x01 static inline uint8_t read(void) { @@ -26,28 +28,26 @@ static inline void write(uint8_t c) UDR0 = c; } -static inline void write_bulk(uint8_t *data, uint16_t n) +static inline void send(uint8_t *data, uint8_t n) { int i; + uint16_t pktlen, sum; - for (i = 0; i < n; i++) - write(data[i]); -} + write(HEADER_HO); + write(HEADER_LO); -static inline void send(uint8_t pktid, uint8_t *data, uint8_t n) -{ - int i; - uint16_t pktlen, sum; + write((uint8_t)(ADDR >> 24)); + write((uint8_t)(ADDR >> 16)); + write((uint8_t)(ADDR >> 8)); + write((uint8_t)(ADDR & 0xFF)); - write_bulk(start_code, 2); - write_bulk(addr, 4); - write(pktid); + write(PACKID); pktlen = n + 2; write((uint8_t)(pktlen >> 8)); write((uint8_t)pktlen); - sum = (pktlen >> 8) + (pktlen & 0xFF) + pktid; + sum = (pktlen >> 8) + (pktlen & 0xFF) + PACKID; for (i = 0; i < n; i++) { write(data[i]); sum += data[i]; @@ -57,7 +57,7 @@ static inline void send(uint8_t pktid, uint8_t *data, uint8_t n) write((uint8_t)sum); } -static inline void recv(uint8_t buf[MAXPDLEN], uint16_t *n) +static inline uint16_t recv(uint8_t buf[MAXPDLEN]) { int i; uint16_t len; @@ -69,21 +69,20 @@ static inline void recv(uint8_t buf[MAXPDLEN], uint16_t *n) byte = read(); switch (i) { case 0: - if (byte != start_code[0]) + if (byte != HEADER_HO) continue; break; case 1: - if (byte != start_code[1]) - goto bad_pkt; - break; + if (byte != HEADER_LO) + return 0; case 2: case 3: case 4: case 5: - // toss the address + // toss address break; case 6: - // toss the packet id + // toss packet id break; case 7: len = (uint16_t)byte << 8; @@ -94,26 +93,33 @@ static inline void recv(uint8_t buf[MAXPDLEN], uint16_t *n) default: if ((i - 9) < MAXPDLEN) { buf[i - 9] = byte; - if ((i - 8) == len) { - *n = len; - return; - } - } else { - goto bad_pkt; - } + if ((i - 8) == len) + return len; + } else + return 0; break; } i++; } + return 0; +} -bad_pkt: - *n = 0; - return; +static inline void led_ctrl(uint8_t mode, COLOR color) +{ + uint8_t buf[MAXPDLEN]; + + buf[0] = 0x35; + buf[1] = mode; + buf[2] = 0x60; + buf[3] = color; + buf[4] = 0x00; + + send(buf, 5); + recv(buf); } static inline uint8_t check_pwd(void) { - unsigned int n; uint8_t buf[MAXPDLEN]; buf[0] = 0x13; @@ -122,13 +128,54 @@ static inline uint8_t check_pwd(void) buf[3] = (uint8_t)((uint32_t)FPM_PWD >> 8); buf[4] = (uint8_t)((uint32_t)FPM_PWD & 0xFF); - send(0x01, buf, 5); + send(buf, 5); + recv(buf); + return buf[0] == OK; +} + +static inline uint8_t scan(void) +{ + uint16_t retries; + uint8_t buf[MAXPDLEN]; + + retries = 0; + led_ctrl(0x01, PURPLE); - n = 0; - recv(buf, &n); + do { + buf[0] = 0x28; + send(buf, 1); + recv(buf); + if (buf[0] != OK) { + retries++; + _delay_ms(100); + } + } while(buf[0] != OK && retries < 100); + + led_ctrl(0x06, PURPLE); return buf[0] == OK; } +static inline uint8_t img2tz(uint8_t bufid) +{ + uint8_t buf[MAXPDLEN]; + + buf[0] = 0x02; + buf[1] = bufid; + send(buf, 2); + recv(buf); + return buf[0] == OK; +} + +void fpm_led_on(COLOR color) +{ + led_ctrl(0x03, color); +} + +void fpm_led_off(void) +{ + led_ctrl(0x04, 0x00); +} + uint8_t fpm_init(void) { UBRR0H = UBRRH_VALUE; @@ -145,14 +192,14 @@ uint8_t fpm_init(void) return check_pwd(); } -uint8_t fpm_getcfg(struct fpm_cfg *cfg) +uint8_t fpm_get_cfg(struct fpm_cfg *cfg) { uint16_t n; uint8_t buf[MAXPDLEN]; buf[0] = 0x0F; - send(0x01, buf, 1); - recv(buf, &n); + send(buf, 1); + n = recv(buf); if (buf[0] == OK && n >= 17) { cfg->status = ((uint16_t)buf[1] << 8) | buf[2]; @@ -172,9 +219,8 @@ uint8_t fpm_getcfg(struct fpm_cfg *cfg) return 0; } -uint8_t fpm_setpwd(uint32_t pwd) +uint8_t fpm_set_pwd(uint32_t pwd) { - uint16_t n; uint8_t buf[MAXPDLEN]; buf[0] = 0x12; @@ -183,19 +229,19 @@ uint8_t fpm_setpwd(uint32_t pwd) buf[3] = (uint8_t)(pwd >> 8); buf[4] = (uint8_t)(pwd & 0xFF); - send(0x01, buf, 5); - recv(buf, &n); + send(buf, 5); + recv(buf); return buf[0] == OK; } -uint16_t fpm_getcount(void) +uint16_t fpm_get_count(void) { uint16_t n, count; uint8_t buf[MAXPDLEN]; buf[0] = 0x1D; - send(0x01, buf, 1); - recv(buf, &n); + send(buf, 1); + n = recv(buf); count = 0; if (buf[0] == OK && n >= 2) { @@ -208,37 +254,67 @@ uint16_t fpm_getcount(void) uint8_t fpm_enroll(void) { - uint16_t n, retries; + struct fpm_cfg cfg; + uint16_t n; uint8_t buf[MAXPDLEN]; - - retries = 0; - do { - _delay_ms(100); - buf[0] = 0x10; - send(0x01, buf, 1); - recv(buf, &n); - retries++; - } while (buf[0] != OK && retries < 50); + fpm_get_cfg(&cfg); + n = fpm_get_count() + 1; + if (n >= cfg.cap) + return 0; + + if (!scan()) + return 0; + + if (!img2tz(1)) + return 0; + _delay_ms(2000); + + if (!scan()) + return 0; + + if (!img2tz(2)) + return 0; + + buf[0] = 0x05; + send(buf, 1); + recv(buf); + if (buf[0] != OK) + return 0; + + buf[0] = 0x06; + buf[1] = 1; + buf[2] = (uint8_t)(n >> 8); + buf[3] = (uint8_t)(n & 0xFF); + send(buf, 4); + recv(buf); + return buf[0] == OK; } uint8_t fpm_match(void) { - uint16_t n, retries; + struct fpm_cfg cfg; uint8_t buf[MAXPDLEN]; - retries = 0; + if (!fpm_get_cfg(&cfg)) + return 0; - do { - _delay_ms(100); - buf[0] = 0x11; - send(0x01, buf, 1); - recv(buf, &n); - retries++; - } while (buf[0] != OK && retries < 50); - + if (!scan()) + return 0; + + if (!img2tz(1)) + return 0; + + buf[0] = 0x04; + buf[1] = 1; + buf[2] = 0x00; + buf[3] = 0x00; + buf[4] = (uint8_t)(cfg.cap >> 8); + buf[5] = (uint8_t)(cfg.cap & 0xFF); + + send(buf, 6); + recv(buf); return buf[0] == OK; } - diff --git a/lock/fpm.h b/lock/fpm.h index b3fcd91..1deca46 100644 --- a/lock/fpm.h +++ b/lock/fpm.h @@ -1,5 +1,5 @@ -#ifndef FPM_H -#define FPM_H +#ifndef FPM_R503_H +#define FPM_R503_H #include @@ -13,16 +13,26 @@ struct fpm_cfg { uint16_t baud; }; +typedef enum { + RED = 0x01, + BLUE = 0x02, + PURPLE = 0x03 +} COLOR; + uint8_t fpm_init(void); -uint8_t fpm_getcfg(struct fpm_cfg *cfg); +uint8_t fpm_get_cfg(struct fpm_cfg *cfg); + +uint8_t fpm_set_pwd(uint32_t pwd); + +void fpm_led_on(COLOR color); -uint8_t fpm_setpwd(uint32_t pwd); +void fpm_led_off(void); -uint16_t fpm_getcount(void); +uint16_t fpm_get_count(void); uint8_t fpm_enroll(void); uint8_t fpm_match(void); -#endif /* FPM_H */ +#endif /* FPM_R50_H */ -- cgit v1.2.3