Skip to content

Commit 0d55f47

Browse files
Add CDC support in SDK
1 parent d3cf494 commit 0d55f47

File tree

8 files changed

+91
-23
lines changed

8 files changed

+91
-23
lines changed

io/include/os_io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ typedef enum {
5454
OS_IO_PACKET_TYPE_NFC_MASK = 0x40,
5555
OS_IO_PACKET_TYPE_NFC_APDU = 0x40,
5656
OS_IO_PACKET_TYPE_NFC_APDU_RSP = 0x41,
57+
58+
OS_IO_PACKET_TYPE_AT_APDU = 0x42,
5759
} os_io_packet_type_t;
5860

5961
typedef enum {

io/src/os_io.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
#ifdef HAVE_IO_USB
1313
#include "usbd_ledger.h"
14-
#include "usbd_ledger_hid_u2f.h"
1514
#endif // HAVE_IO_USB
15+
#ifdef HAVE_IO_U2F
16+
#include "usbd_ledger_hid_u2f.h"
17+
#endif
1618

1719
#ifdef HAVE_BLE
1820
#include "ble_ledger.h"
@@ -351,6 +353,7 @@ int os_io_tx_cmd(uint8_t type,
351353
unsigned int *timeout_ms)
352354
{
353355
int status = 0;
356+
354357
switch (type) {
355358
#ifdef HAVE_IO_USB
356359
case OS_IO_PACKET_TYPE_USB_HID_APDU:
@@ -375,6 +378,12 @@ int os_io_tx_cmd(uint8_t type,
375378
USBD_LEDGER_send(USBD_LEDGER_CLASS_CCID_BULK, type, buffer, length, 0);
376379
break;
377380
#endif // HAVE_CCID_USB
381+
382+
#ifdef HAVE_CDCUSB
383+
case OS_IO_PACKET_TYPE_USB_CDC_RAW:
384+
USBD_LEDGER_send(USBD_LEDGER_CLASS_CDC_DATA, type, buffer, length, 0);
385+
break;
386+
#endif // HAVE_CDCUSB
378387
#endif // HAVE_IO_USB
379388

380389
#ifdef HAVE_BLE

io_legacy/include/os_io_legacy_types.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef enum {
3333
IO_APDU_MEDIA_USB_WEBUSB,
3434
IO_APDU_MEDIA_RAW,
3535
IO_APDU_MEDIA_U2F,
36+
IO_APDU_MEDIA_CDC,
3637
} io_apdu_media_t;
3738

3839
typedef enum {
@@ -72,7 +73,11 @@ typedef struct {
7273
unsigned short io_flags; // flags to be set when calling io_exchange
7374
io_apdu_media_t apdu_media;
7475
#ifdef HAVE_BLE
75-
unsigned int plane_mode;
76+
unsigned int plane_mode;
77+
unsigned char ble_ready;
78+
unsigned char name_changed;
79+
unsigned char enabling_advertising;
80+
unsigned char disabling_advertising;
7681
#endif // HAVE_BLE
7782
} io_seph_app_t;
7883

io_legacy/src/os_io_legacy.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ int io_legacy_apdu_rx(uint8_t handle_ux_events)
452452
#endif // HAVE_IO_U2F
453453
}
454454
break;
455+
case OS_IO_PACKET_TYPE_AT_APDU:
456+
// PRINTF("OS_IO_PACKET_TYPE_AT_APDU\n");
457+
break;
455458

456459
default:
457460
status = 0;

lib_blewbxx/include/ble_ledger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
/* Includes ------------------------------------------------------------------*/
2020
#include <stdint.h>
2121
#include "os_id.h"
22+
#include "os_io.h"
2223
#include "lcx_crc.h"
2324
#include "ble_types.h"
2425

lib_blewbxx/include/ble_ledger_types.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ typedef enum ble_profile_status_e {
2626
BLE_PROFILE_STATUS_OK,
2727
BLE_PROFILE_STATUS_OK_AND_SEND_PACKET,
2828
BLE_PROFILE_STATUS_OK_PROCEDURE_END,
29-
BLE_PROFILE_STATUS_BAD_PARAMETERS = 0xFF,
29+
BLE_PROFILE_STATUS_BAD_PARAMETERS = 0xFF,
3030
} ble_profile_status_t;
3131

3232
/* Exported defines --------------------------------------------------------*/
@@ -47,27 +47,38 @@ typedef struct ble_connection_s {
4747
uint16_t max_tx_time;
4848
uint16_t max_rx_octets;
4949
uint16_t max_rx_time;
50-
bool encrypted;
50+
bool encrypted;
51+
int8_t rssi_level;
52+
int8_t current_transmit_power_level;
53+
int8_t max_transmit_power_level;
54+
int8_t requested_tx_power;
55+
5156
} ble_connection_t;
5257

5358
typedef ble_profile_status_t (*ble_profile_init_t)(ble_cmd_data_t *cmd_data, void *cookie);
54-
typedef ble_profile_status_t (*ble_profile_create_db_t)(uint8_t *hci_buffer, uint16_t length, void *cookie);
59+
typedef ble_profile_status_t (*ble_profile_create_db_t)(uint8_t *hci_buffer,
60+
uint16_t length,
61+
void *cookie);
5562
typedef uint8_t (*ble_profile_handle_in_range_t)(uint16_t gatt_handle, void *cookie);
5663

5764
typedef void (*ble_profile_connection_evt_t)(ble_connection_t *connection, void *cookie);
5865
typedef void (*ble_profile_connection_update_evt_t)(ble_connection_t *connection, void *cookie);
5966
typedef void (*ble_profile_encryption_changed_t)(bool encrypted, void *cookie);
6067

61-
typedef ble_profile_status_t (*ble_profile_att_modified_t)(uint8_t *hci_buffer, uint16_t length, void *cookie);
68+
typedef ble_profile_status_t (*ble_profile_att_modified_t)(uint8_t *hci_buffer,
69+
uint16_t length,
70+
void *cookie);
6271
typedef ble_profile_status_t (*ble_profile_write_permit_req_t)(uint8_t *hci_buffer,
63-
uint16_t length,
64-
void *cookie);
72+
uint16_t length,
73+
void *cookie);
6574
typedef ble_profile_status_t (*ble_profile_mtu_changed_t)(uint16_t mtu, void *cookie);
6675

6776
typedef ble_profile_status_t (*ble_profile_write_rsp_ack_t)(void *cookie);
6877
typedef ble_profile_status_t (*ble_profile_update_char_value_ack_t)(void *cookie);
6978

70-
typedef ble_profile_status_t (*ble_profile_send_packet_t)(const uint8_t *packet, uint16_t length, void *cookie);
79+
typedef ble_profile_status_t (*ble_profile_send_packet_t)(const uint8_t *packet,
80+
uint16_t length,
81+
void *cookie);
7182
typedef bool (*ble_profile_is_busy_t)(void *cookie);
7283

7384
typedef int32_t (*ble_profile_data_ready_t)(uint8_t *buffer, uint16_t max_length, void *cookie);

lib_stusb/src/usbd_ledger.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
#include "usbd_ledger.h"
2222
#include "usbd_ledger_hid.h"
2323
#include "usbd_ledger_hid_kbd.h"
24+
#ifdef HAVE_IO_U2F
2425
#include "usbd_ledger_hid_u2f.h"
26+
#endif
2527
#include "usbd_ledger_webusb.h"
2628
#include "usbd_ledger_ccid.h"
2729
#include "usbd_ledger_cdc.h"

lib_stusb/src/usbd_ledger_cdc.c

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#ifdef HAVE_CDCUSB
2323

24-
2524
/* Private enumerations ------------------------------------------------------*/
2625

2726
/* Private defines------------------------------------------------------------*/
@@ -120,7 +119,7 @@ const usbd_class_info_t USBD_LEDGER_CDC_Data_class_info = {
120119

121120
.send_packet = USBD_LEDGER_CDC_send_packet,
122121

123-
.data_ready = NULL,
122+
.data_ready = USBD_LEDGER_CDC_data_ready,
124123

125124
.setting = NULL,
126125

@@ -306,7 +305,9 @@ USBD_StatusTypeDef USBD_LEDGER_CDC_de_init(USBD_HandleTypeDef *pdev, void *cooki
306305
return USBD_OK;
307306
}
308307

309-
USBD_StatusTypeDef USBD_LEDGER_CDC_setup(USBD_HandleTypeDef *pdev, void *cookie, USBD_SetupReqTypedef *req)
308+
USBD_StatusTypeDef USBD_LEDGER_CDC_setup(USBD_HandleTypeDef *pdev,
309+
void *cookie,
310+
USBD_SetupReqTypedef *req)
310311
{
311312
if (!pdev || !cookie || !req) {
312313
return USBD_FAIL;
@@ -412,18 +413,24 @@ USBD_StatusTypeDef USBD_LEDGER_CDC_data_in(USBD_HandleTypeDef *pdev, void *cooki
412413
}
413414

414415
USBD_StatusTypeDef USBD_LEDGER_CDC_data_out(USBD_HandleTypeDef *pdev,
415-
void *cookie,
416-
uint8_t ep_num,
417-
uint8_t *packet,
418-
uint16_t packet_length)
416+
void *cookie,
417+
uint8_t ep_num,
418+
uint8_t *packet,
419+
uint16_t packet_length)
419420
{
420421
if (!pdev) {
421422
return USBD_FAIL;
422423
}
423424

424425
UNUSED(cookie);
425-
UNUSED(packet);
426-
UNUSED(packet_length);
426+
427+
// Filter anything not starting with AT+ :
428+
if (packet_length > 3) {
429+
memset(G_io_apdu_buffer, 0, sizeof(G_io_apdu_buffer));
430+
memcpy(G_io_apdu_buffer, packet, packet_length);
431+
G_io_app.apdu_length = packet_length;
432+
G_io_app.apdu_media = IO_APDU_MEDIA_CDC;
433+
}
427434

428435
if (ep_num == LEDGER_CDC_DATA_EPOUT_ADDR) {
429436
USBD_LL_PrepareReceive(pdev, LEDGER_CDC_DATA_EPOUT_ADDR, NULL, LEDGER_CDC_DATA_EPOUT_SIZE);
@@ -433,11 +440,11 @@ USBD_StatusTypeDef USBD_LEDGER_CDC_data_out(USBD_HandleTypeDef *pdev,
433440
}
434441

435442
USBD_StatusTypeDef USBD_LEDGER_CDC_send_packet(USBD_HandleTypeDef *pdev,
436-
void *cookie,
437-
uint8_t packet_type,
438-
const uint8_t *packet,
439-
uint16_t packet_length,
440-
uint32_t timeout_ms)
443+
void *cookie,
444+
uint8_t packet_type,
445+
const uint8_t *packet,
446+
uint16_t packet_length,
447+
uint32_t timeout_ms)
441448
{
442449
if (!pdev || !cookie || !packet) {
443450
return USBD_FAIL;
@@ -461,4 +468,32 @@ USBD_StatusTypeDef USBD_LEDGER_CDC_send_packet(USBD_HandleTypeDef *pdev,
461468
return ret;
462469
}
463470

471+
int32_t USBD_LEDGER_CDC_data_ready(USBD_HandleTypeDef *pdev,
472+
void *cookie,
473+
uint8_t *buffer,
474+
uint16_t max_length)
475+
{
476+
int32_t status = 0;
477+
478+
UNUSED(pdev);
479+
UNUSED(max_length);
480+
481+
if (!cookie || !buffer) {
482+
return -1;
483+
}
484+
485+
ledger_cdc_handle_t *handle = (ledger_cdc_handle_t *) PIC(cookie);
486+
487+
// Filter anything not starting with AT+ :
488+
if (handle->tx_in_progress == 0 && G_io_app.apdu_length > 3) {
489+
// First update packet type in buff :
490+
buffer[0] = OS_IO_PACKET_TYPE_AT_APDU;
491+
// Then copy data to G_io_rx_buffer.
492+
memmove(buffer + 1, G_io_apdu_buffer, G_io_app.apdu_length);
493+
status = G_io_app.apdu_length + 1;
494+
}
495+
496+
return status;
497+
}
498+
464499
#endif // HAVE_CDCUSB

0 commit comments

Comments
 (0)