Skip to content

Commit 82eb8e6

Browse files
committed
add readonly flag
1 parent 8794d50 commit 82eb8e6

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

fw/application/src/app/amiidb/api/amiidb_api_slot.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ int32_t amiidb_api_slot_read(uint8_t slot, ntag_t *p_ntag) {
3030
strcpy(p_ntag->notes, meta.notes);
3131
}
3232

33+
if (meta.has_flags && meta.flags & VFS_OBJ_FLAG_READONLY) {
34+
p_ntag->readonly = true;
35+
}
36+
3337
return 0;
3438
}
3539

@@ -51,6 +55,8 @@ int32_t amiidb_api_slot_write(uint8_t slot, ntag_t *p_ntag) {
5155
meta.amiibo_head = to_little_endian_int32(&p_ntag->data[84]);
5256
meta.amiibo_tail = to_little_endian_int32(&p_ntag->data[88]);
5357

58+
// TODO write only flag???
59+
5460
vfs_meta_encode(meta_encoded, sizeof(meta_encoded), &meta);
5561
res = p_vfs_driver->update_file_meta(path, meta_encoded, sizeof(meta_encoded));
5662
if (res < 0) {
@@ -90,13 +96,13 @@ int32_t amiidb_api_slot_info(uint8_t slot, amiidb_slot_info_t *p_info) {
9096

9197
p_info->slot = slot;
9298
p_info->is_empty = false;
99+
p_info->is_readonly = meta.has_flags && meta.flags & VFS_OBJ_FLAG_READONLY;
93100
p_info->amiibo_head = meta.amiibo_head;
94101
p_info->amiibo_tail = meta.amiibo_tail;
95102

96103
return 0;
97104
}
98105

99-
100106
int32_t amiidb_api_slot_list(amiibo_slot_info_cb_t cb, void *ctx) {
101107
vfs_dir_t dir;
102108
vfs_obj_t obj;
@@ -108,6 +114,7 @@ int32_t amiidb_api_slot_list(amiibo_slot_info_cb_t cb, void *ctx) {
108114
for (uint8_t i = 0; i < MAX_SLOT_COUNT; i++) {
109115
slots[i].slot = i;
110116
slots[i].is_empty = true;
117+
slots[i].is_readonly = false;
111118
slots[i].amiibo_head = 0;
112119
slots[i].amiibo_tail = 0;
113120
}
@@ -123,20 +130,47 @@ int32_t amiidb_api_slot_list(amiibo_slot_info_cb_t cb, void *ctx) {
123130
if (sscanf(obj.name, "%02d.bin", &index) == 1 && index >= 0 && index < max_slot_num) {
124131
memset(&meta, 0, sizeof(vfs_meta_t));
125132
vfs_meta_decode(obj.meta, sizeof(obj.meta), &meta);
126-
if(meta.has_amiibo_id) {
133+
if (meta.has_amiibo_id) {
127134
slots[index].is_empty = false;
128135
slots[index].amiibo_head = meta.amiibo_head;
129136
slots[index].amiibo_tail = meta.amiibo_tail;
130137
}
138+
if (meta.has_flags && meta.flags & VFS_OBJ_FLAG_READONLY) {
139+
slots[index].is_readonly = true;
140+
}
131141
}
132142
}
133143
}
134144
p_vfs_driver->close_dir(&dir);
135145

136-
137146
for (uint8_t i = 0; i < max_slot_num; i++) {
138147
cb(&slots[i], ctx);
139148
}
140149

141150
return 0;
151+
}
152+
153+
int32_t amiidb_api_slot_set_readonly(uint8_t slot, bool readonly) {
154+
char path[VFS_MAX_PATH_LEN];
155+
vfs_meta_t meta;
156+
vfs_obj_t obj;
157+
158+
sprintf(path, "/amiibo/data/%02d.bin", slot);
159+
vfs_driver_t *p_vfs_driver = vfs_get_driver(VFS_DRIVE_EXT);
160+
int32_t res = p_vfs_driver->stat_file(path, &obj);
161+
if (res < 0) {
162+
return -1;
163+
}
164+
165+
memset(&meta, 0, sizeof(vfs_meta_t));
166+
vfs_meta_decode(obj.meta, sizeof(obj.meta), &meta);
167+
168+
meta.has_flags = true;
169+
if (readonly) {
170+
meta.flags |= VFS_OBJ_FLAG_READONLY;
171+
} else {
172+
meta.flags &= ~VFS_OBJ_FLAG_READONLY;
173+
}
174+
175+
return p_vfs_driver->update_file_meta(path, obj.meta, sizeof(obj.meta));
142176
}

fw/application/src/app/amiidb/api/amiidb_api_slot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
typedef struct {
1515
uint8_t slot;
1616
uint8_t is_empty;
17+
uint8_t is_readonly;
1718
uint32_t amiibo_head;
1819
uint32_t amiibo_tail;
1920
} amiidb_slot_info_t;
@@ -28,4 +29,6 @@ int32_t amiidb_api_slot_remove(uint8_t slot);
2829
int32_t amiidb_api_slot_info(uint8_t slot, amiidb_slot_info_t * p_info);
2930
int32_t amiidb_api_slot_list(amiibo_slot_info_cb_t cb, void* ctx);
3031

32+
int32_t amiidb_api_slot_set_readonly(uint8_t slot, bool readonly);
33+
3134
#endif // FW_AMIIDB_API_SLOT_H

fw/application/src/mod/vfs/vfs_meta.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
typedef enum {
1212
VFS_OBJ_FLAG_HIDDEN = 1 << 0,
1313
VFS_OBJ_FLAG_SYSTEM = 1 << 1,
14+
VFS_OBJ_FLAG_READONLY = 1 << 2,
1415
} vfs_obj_flags_t;
1516

1617
typedef enum {

fw/application/src/ntag/ntag_def.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ typedef enum
2525
typedef struct {
2626
uint8_t data[NTAG_DATA_SIZE];
2727
uint8_t notes[128];
28+
bool readonly;
2829
} ntag_t;
2930

3031

fw/application/src/ntag/ntag_emu_v2.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ static void nfc_received_process(const uint8_t *p_data, size_t data_length, uint
108108
p_data = p_data + 1;
109109
case NFC_CMD_WRITE:
110110
NRF_LOG_INFO("NFC Write Block %d", block_num);
111+
if (ntag_emu.ntag.readonly) {
112+
NRF_LOG_INFO("NFC Read Only");
113+
hal_send_ack_nack(0x0);
114+
return;
115+
}
111116
if (data_length == 6) {
112117
ntag_emu.dirty = true;
113118
if (block_num == 133 || block_num == 134) {

0 commit comments

Comments
 (0)