Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into release-candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
skotopes committed Jun 13, 2024
2 parents 6007ee1 + ca8517a commit 0d42b64
Show file tree
Hide file tree
Showing 280 changed files with 6,777 additions and 2,827 deletions.
17 changes: 17 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CompileFlags:
Add:
- -Wno-unknown-warning-option
- -Wno-format
Remove:
- -mword-relocations

Diagnostics:
ClangTidy:
FastCheckFilter: None

---

If:
PathMatch: .*\.h
Diagnostics:
UnusedIncludes: None
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ jobs:
python3 scripts/testing/units.py ${{steps.device.outputs.flipper}}
- name: 'Check GDB output'
if: failure()
if: failure() && steps.flashing.outcome == 'success'
run: |
./fbt gdb_trace_all SWD_TRANSPORT_SERIAL=2A0906016415303030303032 LIB_DEBUG=1 FIRMWARE_APP_SET=unit_tests FORCE=1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ compile_commands.json
# JetBrains IDEs
.idea/

# Sublime Text
.sublime-project.sublime-workspace

# Python VirtEnvironments
.env
.venv
Expand Down
21 changes: 21 additions & 0 deletions .sublime-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"folders":
[
{
"path": ".",
}
],
"settings": {
"LSP": {
"clangd": {
"initializationOptions": {
"clangd.compile-commands-dir": "build/latest",
"clangd.header-insertion": "never",
"clangd.query-driver": "**",
"clangd.clang-tidy": true,
},
"enabled": true,
},
},
},
}
4 changes: 3 additions & 1 deletion .vscode/example/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"clangd.arguments": [
// We might be able to tighten this a bit more to only include the correct toolchain.
"--query-driver=**",
"--compile-commands-dir=${workspaceFolder}/build/latest"
"--compile-commands-dir=${workspaceFolder}/build/latest",
"--clang-tidy",
"--header-insertion=never"
]
}
4 changes: 4 additions & 0 deletions applications/debug/accessor/accessor_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ void AccessorApp::run(void) {
AccessorApp::AccessorApp()
: text_store{0} {
notification = static_cast<NotificationApp*>(furi_record_open(RECORD_NOTIFICATION));
expansion = static_cast<Expansion*>(furi_record_open(RECORD_EXPANSION));
onewire_host = onewire_host_alloc(&gpio_ibutton);
expansion_disable(expansion);
furi_hal_power_enable_otg();
}

AccessorApp::~AccessorApp() {
furi_hal_power_disable_otg();
expansion_enable(expansion);
furi_record_close(RECORD_EXPANSION);
furi_record_close(RECORD_NOTIFICATION);
onewire_host_free(onewire_host);
}
Expand Down
2 changes: 2 additions & 0 deletions applications/debug/accessor/accessor_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "helpers/wiegand.h"
#include <one_wire/one_wire_host.h>
#include <notification/notification_messages.h>
#include <expansion/expansion.h>

class AccessorApp {
public:
Expand Down Expand Up @@ -51,4 +52,5 @@ class AccessorApp {
OneWireHost* onewire_host;

NotificationApp* notification;
Expansion* expansion;
};
1 change: 0 additions & 1 deletion applications/debug/accessor/scene/accessor_scene_start.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "../accessor_app.h"
#include "../accessor_view_manager.h"
#include "../accessor_event.h"
#include "callback_connector.h"
#include "accessor_scene_start.h"

void AccessorSceneStart::on_enter(AccessorApp* app) {
Expand Down
2 changes: 1 addition & 1 deletion applications/debug/battery_test_app/views/battery_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void draw_battery(Canvas* canvas, BatteryInfoModel* data, int x, int y) {
drain_current > HIGH_DRAIN_CURRENT_THRESHOLD ? "mA!" : "mA");
} else if(drain_current != 0) {
snprintf(header, 20, "...");
} else if(data->charging_voltage < 4.2) {
} else if(data->charging_voltage < 4.2f) {
// Non-default battery charging limit, mention it
snprintf(emote, sizeof(emote), "Charged!");
snprintf(header, sizeof(header), "Limited to");
Expand Down
1 change: 0 additions & 1 deletion applications/debug/blink_test/blink_test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <core/common_defines.h>
#include <furi.h>
#include <furi_hal.h>

Expand Down
128 changes: 90 additions & 38 deletions applications/debug/ccid_test/ccid_test_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#include <gui/view_dispatcher.h>
#include <gui/modules/submenu.h>
#include <gui/gui.h>

#include "iso7816_callbacks.h"
#include "iso7816_t0_apdu.h"
#include "iso7816_atr.h"

typedef enum {
EventTypeInput,
Expand All @@ -33,38 +34,6 @@ typedef enum {
CcidTestSubmenuIndexInsertSmartcardReader
} SubmenuIndex;

void icc_power_on_callback(uint8_t* atrBuffer, uint32_t* atrlen, void* context) {
UNUSED(context);

iso7816_answer_to_reset(atrBuffer, atrlen);
}

//dataBlock points to the buffer
//dataBlockLen tells reader how nany bytes should be read
void xfr_datablock_callback(
const uint8_t* dataBlock,
uint32_t dataBlockLen,
uint8_t* responseDataBlock,
uint32_t* responseDataBlockLen,
void* context) {
UNUSED(context);

struct ISO7816_Command_APDU commandAPDU;
iso7816_read_command_apdu(&commandAPDU, dataBlock, dataBlockLen);

struct ISO7816_Response_APDU responseAPDU;
//class not supported
responseAPDU.SW1 = 0x6E;
responseAPDU.SW2 = 0x00;

iso7816_write_response_apdu(&responseAPDU, responseDataBlock, responseDataBlockLen);
}

static const CcidCallbacks ccid_cb = {
icc_power_on_callback,
xfr_datablock_callback,
};

static void ccid_test_app_render_callback(Canvas* canvas, void* ctx) {
UNUSED(ctx);
canvas_clear(canvas);
Expand Down Expand Up @@ -103,7 +72,6 @@ CcidTestApp* ccid_test_app_alloc(void) {

//message queue
app->event_queue = furi_message_queue_alloc(8, sizeof(CcidTestAppEvent));
furi_check(app->event_queue);
view_port_input_callback_set(app->view_port, ccid_test_app_input_callback, app->event_queue);

return app;
Expand All @@ -127,6 +95,86 @@ void ccid_test_app_free(CcidTestApp* app) {
free(app);
}

void ccid_icc_power_on_callback(uint8_t* atrBuffer, uint32_t* atrlen, void* context) {
UNUSED(context);

iso7816_icc_power_on_callback(atrBuffer, atrlen);
}

void ccid_xfr_datablock_callback(
const uint8_t* pcToReaderDataBlock,
uint32_t pcToReaderDataBlockLen,
uint8_t* readerToPcDataBlock,
uint32_t* readerToPcDataBlockLen,
void* context) {
UNUSED(context);

iso7816_xfr_datablock_callback(
pcToReaderDataBlock, pcToReaderDataBlockLen, readerToPcDataBlock, readerToPcDataBlockLen);
}

static const CcidCallbacks ccid_cb = {
ccid_icc_power_on_callback,
ccid_xfr_datablock_callback,
};

void iso7816_answer_to_reset(Iso7816Atr* atr) {
//minimum valid ATR: https://smartcard-atr.apdu.fr/parse?ATR=3B+00
atr->TS = 0x3B;
atr->T0 = 0x00;
}

void iso7816_process_command(
const struct ISO7816_Command_APDU* commandAPDU,
struct ISO7816_Response_APDU* responseAPDU,
const uint8_t* commandApduDataBuffer,
uint8_t commandApduDataBufferLen,
uint8_t* responseApduDataBuffer,
uint8_t* responseApduDataBufferLen) {
//example 1: sends a command with no body, receives a response with no body
//sends APDU 0x01:0x02:0x00:0x00
//receives SW1=0x90, SW2=0x00
if(commandAPDU->CLA == 0x01 && commandAPDU->INS == 0x01) {
responseAPDU->SW1 = 0x90;
responseAPDU->SW2 = 0x00;
}
//example 2: sends a command with no body, receives a response with a body with two bytes
//sends APDU 0x01:0x02:0x00:0x00
//receives 'bc' (0x62, 0x63) SW1=0x80, SW2=0x10
else if(commandAPDU->CLA == 0x01 && commandAPDU->INS == 0x02) {
responseApduDataBuffer[0] = 0x62;
responseApduDataBuffer[1] = 0x63;

*responseApduDataBufferLen = 2;

responseAPDU->SW1 = 0x90;
responseAPDU->SW2 = 0x00;
}
//example 3: ends a command with a body with two bytes, receives a response with a body with two bytes
//sends APDU 0x01:0x03:0x00:0x00:0x02:CA:FE
//receives (0xCA, 0xFE) SW1=0x90, SW2=0x02
else if(
commandAPDU->CLA == 0x01 && commandAPDU->INS == 0x03 && commandApduDataBufferLen == 2 &&
commandAPDU->Lc == 2) {
//echo command body to response body
responseApduDataBuffer[0] = commandApduDataBuffer[0];
responseApduDataBuffer[1] = commandApduDataBuffer[1];

*responseApduDataBufferLen = 2;

responseAPDU->SW1 = 0x90;
responseAPDU->SW2 = 0x00;
} else {
responseAPDU->SW1 = 0x6A;
responseAPDU->SW2 = 0x00;
}
}

static const Iso7816Callbacks iso87816_cb = {
iso7816_answer_to_reset,
iso7816_process_command,
};

int32_t ccid_test_app(void* p) {
UNUSED(p);

Expand All @@ -135,14 +183,16 @@ int32_t ccid_test_app(void* p) {

//setup CCID USB
// On linux: set VID PID using: /usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Info.plist
app->ccid_cfg.vid = 0x1234;
app->ccid_cfg.pid = 0x5678;
app->ccid_cfg.vid = 0x076B;
app->ccid_cfg.pid = 0x3A21;

FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
furi_hal_usb_unlock();
furi_hal_ccid_set_callbacks((CcidCallbacks*)&ccid_cb);
furi_hal_ccid_set_callbacks((CcidCallbacks*)&ccid_cb, NULL);
furi_check(furi_hal_usb_set_config(&usb_ccid, &app->ccid_cfg) == true);

iso7816_set_callbacks((Iso7816Callbacks*)&iso87816_cb);

//handle button events
CcidTestAppEvent event;
while(1) {
Expand All @@ -161,7 +211,9 @@ int32_t ccid_test_app(void* p) {

//tear down USB
furi_hal_usb_set_config(usb_mode_prev, NULL);
furi_hal_ccid_set_callbacks(NULL);
furi_hal_ccid_set_callbacks(NULL, NULL);

iso7816_set_callbacks(NULL);

//teardown view
ccid_test_app_free(app);
Expand Down
9 changes: 9 additions & 0 deletions applications/debug/ccid_test/iso7816_atr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _ISO7816_ATR_H_
#define _ISO7816_ATR_H_

typedef struct {
uint8_t TS;
uint8_t T0;
} Iso7816Atr;

#endif //_ISO7816_ATR_H_
76 changes: 76 additions & 0 deletions applications/debug/ccid_test/iso7816_callbacks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// transforms low level calls such as XFRCallback or ICC Power on to a structured one
// an application can register these calls and listen for the callbacks defined in Iso7816Callbacks

#include "iso7816_t0_apdu.h"
#include "iso7816_atr.h"
#include "iso7816_callbacks.h"
#include <stdint.h>
#include <stddef.h>
#include <furi.h>

#define ISO7816_RESPONSE_BUFFER_SIZE 255

static Iso7816Callbacks* callbacks = NULL;

void iso7816_set_callbacks(Iso7816Callbacks* cb) {
callbacks = cb;
}

void iso7816_icc_power_on_callback(uint8_t* atrBuffer, uint32_t* atrlen) {
Iso7816Atr atr;
callbacks->iso7816_answer_to_reset(&atr);

furi_assert(atr.T0 == 0x00);

uint8_t AtrBuffer[2] = {atr.TS, atr.T0};

*atrlen = 2;

memcpy(atrBuffer, AtrBuffer, sizeof(uint8_t) * (*atrlen));
}

//dataBlock points to the buffer
//dataBlockLen tells reader how nany bytes should be read
void iso7816_xfr_datablock_callback(
const uint8_t* pcToReaderDataBlock,
uint32_t pcToReaderDataBlockLen,
uint8_t* readerToPcDataBlock,
uint32_t* readerToPcDataBlockLen) {
struct ISO7816_Response_APDU responseAPDU;
uint8_t responseApduDataBuffer[ISO7816_RESPONSE_BUFFER_SIZE];
uint8_t responseApduDataBufferLen = 0;

if(callbacks != NULL) {
struct ISO7816_Command_APDU commandAPDU;

const uint8_t* commandApduDataBuffer = NULL;
uint8_t commandApduDataBufferLen = 0;

iso7816_read_command_apdu(&commandAPDU, pcToReaderDataBlock, pcToReaderDataBlockLen);

if(commandAPDU.Lc > 0) {
commandApduDataBufferLen = commandAPDU.Lc;
commandApduDataBuffer = &pcToReaderDataBlock[5];
}

callbacks->iso7816_process_command(
&commandAPDU,
&responseAPDU,
commandApduDataBuffer,
commandApduDataBufferLen,
responseApduDataBuffer,
&responseApduDataBufferLen);

} else {
//class not supported
responseAPDU.SW1 = 0x6E;
responseAPDU.SW2 = 0x00;
}

iso7816_write_response_apdu(
&responseAPDU,
readerToPcDataBlock,
readerToPcDataBlockLen,
responseApduDataBuffer,
responseApduDataBufferLen);
}
Loading

0 comments on commit 0d42b64

Please sign in to comment.