Skip to content

Commit

Permalink
Tidied up SD card and flash ACL read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Oct 5, 2023
1 parent f41b687 commit e619784
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

### Added
1. Keypad emulation
2. _clear-acl_ command

### Updated
1. Fixed bug in SD card _store-acl_ logic.


## [0.8.6](https://github.com/uhppoted/uhppoted-wiegand/releases/tag/v0.8.6) - 2023-08-30
Expand Down
8 changes: 2 additions & 6 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
- (?) Make -Wimplicit-function-declaration an error

- [x] Weird card after ACL revoking a card in the middle of a list
- [x] clear ACL

- [ ] Emulate keypad (cf. https://github.com/uhppoted/uhppoted-wiegand/issues/4)
- [x] 4-bit burst mode write
- [x] 4-bit burst mode read
- [x] controller: execute
- [x] unlock on valid passcode
- [x] master passcode
- [x] set passcodes
- [x] include passcodes in CRC
- [x] clear ACL
- [x] controller passcodes
- [ ] 8-bit burst mode write
- [ ] 8-bit burst mode read
- [ ] card + PIN writer
Expand Down
4 changes: 2 additions & 2 deletions pico/core/include/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

#include "wiegand.h"

extern void flash_read_acl(CARD[], int *, uint32_t[4]);
extern void flash_write_acl(CARD[], int, uint32_t[4]);
extern void flash_read_acl(CARD[], int *, uint32_t[4], int);
extern void flash_write_acl(CARD[], int, uint32_t[4], int);
1 change: 1 addition & 0 deletions pico/core/include/wiegand.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pico/util/queue.h"

#define CARD_NAME_SIZE 48
#define MAX_CARDS 32

extern const uint UART0_RX;
extern const uint UART0_TX;
Expand Down
8 changes: 4 additions & 4 deletions pico/core/src/acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <sdcard.h>
#include <wiegand.h>

CARD ACL[32];
CARD ACL[MAX_CARDS];
uint32_t PASSCODES[4] = {0, 0, 0, 0};
const uint32_t OVERRIDE = MASTER_PASSCODE;
const int ACL_SIZE = sizeof(ACL) / sizeof(CARD);
Expand All @@ -33,7 +33,7 @@ int acl_load() {
int N = ACL_SIZE;
char s[64];

flash_read_acl(ACL, &N, PASSCODES);
flash_read_acl(ACL, &N, PASSCODES, PASSCODES_SIZE);
snprintf(s, sizeof(s), "ACL LOADED %d CARDS FROM FLASH", N);
logd_log(s);

Expand Down Expand Up @@ -88,7 +88,7 @@ int acl_save() {
}

// ... save to flash
flash_write_acl(ACL, ACL_SIZE, PASSCODES);
flash_write_acl(ACL, ACL_SIZE, PASSCODES, PASSCODES_SIZE);
snprintf(s, sizeof(s), "ACL STORED %d CARDS TO FLASH", N);
logd_log(s);

Expand Down Expand Up @@ -239,7 +239,7 @@ bool acl_set_passcodes(uint32_t passcode1, uint32_t passcode2, uint32_t passcode
PASSCODES[3] = passcode4 > 0 && passcode4 < 1000000 ? passcode4 : 0;

// ... save to flash
flash_write_acl(ACL, ACL_SIZE, PASSCODES);
flash_write_acl(ACL, ACL_SIZE, PASSCODES, PASSCODES_SIZE);
logd_log("ACL UPDATED PASSCODES IN FLASH");

return true;
Expand Down
18 changes: 8 additions & 10 deletions pico/core/src/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ uint32_t crc32(const char *, size_t);
/* Reads the ACL from onboard flash.
*
*/
void flash_read_acl(CARD cards[], int *N, uint32_t passcodes[4]) {
void flash_read_acl(CARD cards[], int *N, uint32_t passcodes[4], int max_passcodes) {
uint32_t page = flash_get_current_page();

if (page != -1 && page < PAGES) {
Expand All @@ -53,10 +53,9 @@ void flash_read_acl(CARD cards[], int *N, uint32_t passcodes[4]) {
uint32_t *r = (uint32_t *)(addr);

// ... get passcodes
passcodes[0] = *q++;
passcodes[1] = *q++;
passcodes[2] = *q++;
passcodes[3] = *q++;
for (int i = 0; i < max_passcodes; i++) {
passcodes[i] = *q++;
}

// ... get cards
for (uint32_t i = 0; i < size && ix < *N; i++) {
Expand Down Expand Up @@ -90,7 +89,7 @@ void flash_read_acl(CARD cards[], int *N, uint32_t passcodes[4]) {
/* Writes the ACL to onboard flash.
*
*/
void flash_write_acl(CARD cards[], int N, uint32_t passcodes[4]) {
void flash_write_acl(CARD cards[], int N, uint32_t passcodes[4], int max_passcodes) {
uint32_t page = flash_get_current_page();
uint32_t version = flash_get_version(page);

Expand All @@ -109,10 +108,9 @@ void flash_write_acl(CARD cards[], int N, uint32_t passcodes[4]) {
memset(buffer, 0, sizeof(buffer));

// ... set passcodes
buffer[32] = passcodes[0];
buffer[32 + 1] = passcodes[1];
buffer[32 + 2] = passcodes[2];
buffer[32 + 3] = passcodes[3];
for (int i = 0; i < max_passcodes; i++) {
buffer[32 + i] = passcodes[i];
}

// .. fill cards buffer
uint32_t *p = (uint32_t *)(&buffer[64]);
Expand Down
6 changes: 2 additions & 4 deletions pico/core/src/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,9 @@ int sdcard_read_acl(CARD cards[], int *N) {
while (f_gets(buffer, sizeof(buffer), &file) && ix < *N) {
buffer[strcspn(buffer, "\n")] = 0;

printf(">> READ %s\n", buffer);

char *p = strtok(buffer, " ");

if (p && ((card_number = strtol(p, NULL, 10)) != 0)) {
if (p && ((card_number = strtol(p, NULL, 10)) != 0) && card_number != 0xffffffff) {
cards[ix].card_number = card_number;
cards[ix].start = string2date("2000-01-01");
cards[ix].end = string2date("2000-12-31");
Expand Down Expand Up @@ -239,7 +237,7 @@ int sdcard_write_acl(CARD cards[], int N) {
}

int count = 0;
for (int i = 0; i < 32 && count < N; i++) {
for (int i = 0; i < MAX_CARDS && count < N; i++) {
CARD card = cards[i];
char record[64];

Expand Down

0 comments on commit e619784

Please sign in to comment.