Skip to content

Commit 7b4e60d

Browse files
committed
Implement changes suggested by @RebornedBrain
1 parent 4388524 commit 7b4e60d

File tree

2 files changed

+37
-48
lines changed

2 files changed

+37
-48
lines changed

lib/nfc/protocols/felica/felica_listener.c

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static FelicaError felica_listener_process_request(
164164

165165
static void felica_listener_populate_polling_response_header(
166166
FelicaListener* instance,
167-
FelicaListenerPollingResponse* resp) {
167+
FelicaListenerPollingResponseHeader* resp) {
168168
resp->idm = instance->data->idm;
169169
resp->pmm = instance->data->pmm;
170170
resp->response_code = FELICA_LISTENER_RESPONSE_POLLING;
@@ -179,10 +179,9 @@ static bool felica_listener_check_system_code(
179179
generic_request->polling.system_code == (code | 0xFF00U));
180180
}
181181

182-
static FelicaError felica_listener_process_system_code(
182+
static uint16_t felica_listener_get_response_system_code(
183183
FelicaListener* instance,
184184
const FelicaListenerGenericRequest* const generic_request) {
185-
// It should respond to 12FC, 12FF, FFFC, 88B4, 88FF and FFB4 according to the Lite-S manual.
186185
uint16_t resp_system_code = FELICA_SYSTEM_CODE_CODE;
187186
if(felica_listener_check_system_code(generic_request, FELICA_LISTENER_SYSTEM_CODE_NDEF) &&
188187
instance->data->data.fs.mc.data[FELICA_MC_SYS_OP] == 1) {
@@ -193,44 +192,38 @@ static FelicaError felica_listener_process_system_code(
193192
// Lite-S
194193
resp_system_code = FELICA_LISTENER_SYSTEM_CODE_LITES;
195194
}
195+
return resp_system_code;
196+
}
196197

197-
if(resp_system_code != FELICA_SYSTEM_CODE_CODE) {
198-
switch(generic_request->polling.request_code) {
199-
case FELICA_LISTENER_REQUEST_SYSTEM_CODE:
200-
case FELICA_LISTENER_REQUEST_PERFORMANCE: {
201-
FelicaListenerPollingResponseWithRequest* resp =
202-
malloc(sizeof(FelicaListenerPollingResponseWithRequest));
203-
resp->base.length = sizeof(FelicaListenerPollingResponseWithRequest);
204-
felica_listener_populate_polling_response_header(instance, &resp->base);
205-
206-
if(generic_request->polling.request_code == FELICA_LISTENER_REQUEST_SYSTEM_CODE) {
207-
resp->request_data = resp_system_code;
208-
} else {
209-
resp->request_data = FELICA_LISTENER_PERFORMANCE_VALUE;
210-
}
211-
212-
bit_buffer_reset(instance->tx_buffer);
213-
bit_buffer_append_bytes(instance->tx_buffer, (uint8_t*)resp, resp->base.length);
214-
free(resp);
215-
break;
216-
}
217-
case FELICA_LISTENER_REQUEST_NONE:
218-
default: {
219-
FelicaListenerPollingResponse* resp = malloc(sizeof(FelicaListenerPollingResponse));
220-
resp->length = sizeof(FelicaListenerPollingResponse);
221-
felica_listener_populate_polling_response_header(instance, resp);
222-
223-
bit_buffer_reset(instance->tx_buffer);
224-
bit_buffer_append_bytes(instance->tx_buffer, (uint8_t*)resp, resp->length);
225-
free(resp);
226-
break;
227-
}
198+
static FelicaError felica_listener_process_system_code(
199+
FelicaListener* instance,
200+
const FelicaListenerGenericRequest* const generic_request) {
201+
FelicaError result = FelicaErrorFeatureUnsupported;
202+
do {
203+
uint16_t resp_system_code =
204+
felica_listener_get_response_system_code(instance, generic_request);
205+
if(resp_system_code == FELICA_SYSTEM_CODE_CODE) break;
206+
207+
FelicaListenerPollingResponse* resp = malloc(sizeof(FelicaListenerPollingResponse));
208+
felica_listener_populate_polling_response_header(instance, &resp->header);
209+
210+
resp->header.length = sizeof(FelicaListenerPollingResponse);
211+
if(generic_request->polling.request_code == FELICA_LISTENER_REQUEST_SYSTEM_CODE) {
212+
resp->optional_request_data = resp_system_code;
213+
} else if(generic_request->polling.request_code == FELICA_LISTENER_REQUEST_PERFORMANCE) {
214+
resp->optional_request_data = FELICA_LISTENER_PERFORMANCE_VALUE;
215+
} else {
216+
resp->header.length = sizeof(FelicaListenerPollingResponseHeader);
228217
}
229-
return felica_listener_frame_exchange(instance, instance->tx_buffer);
230-
}
231218

232-
// Card does not support this System Code
233-
return FelicaErrorFeatureUnsupported;
219+
bit_buffer_reset(instance->tx_buffer);
220+
bit_buffer_append_bytes(instance->tx_buffer, (uint8_t*)resp, resp->header.length);
221+
free(resp);
222+
223+
result = felica_listener_frame_exchange(instance, instance->tx_buffer);
224+
} while(false);
225+
226+
return result;
234227
}
235228

236229
NfcCommand felica_listener_run(NfcGenericEvent event, void* context) {

lib/nfc/protocols/felica/felica_listener_i.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,24 @@ typedef enum {
2525
Felica_ListenerStateActivated,
2626
} FelicaListenerState;
2727

28-
#pragma pack(push, 1)
29-
typedef struct {
28+
typedef struct FURI_PACKED {
3029
uint8_t code;
3130
uint16_t system_code;
3231
uint8_t request_code;
3332
uint8_t time_slot;
3433
} FelicaListenerPollingHeader;
35-
#pragma pack(pop)
3634

3735
typedef struct {
3836
uint8_t length;
3937
uint8_t response_code;
4038
FelicaIDm idm;
4139
FelicaPMm pmm;
42-
} FelicaListenerPollingResponse;
40+
} FelicaListenerPollingResponseHeader;
4341

44-
#pragma pack(push, 1)
45-
typedef struct {
46-
FelicaListenerPollingResponse base;
47-
uint16_t request_data;
48-
} FelicaListenerPollingResponseWithRequest;
49-
#pragma pack(pop)
42+
typedef struct FURI_PACKED {
43+
FelicaListenerPollingResponseHeader header;
44+
uint16_t optional_request_data;
45+
} FelicaListenerPollingResponse;
5046

5147
/** Generic Felica request same for both read and write operations. */
5248
typedef struct {

0 commit comments

Comments
 (0)