Change in osmo-ccid-firmware[master]: add debug command to communicate with card

2019-05-10 Thread Harald Welte
Harald Welte has submitted this change and it was merged. ( 
https://gerrit.osmocom.org/13943 )

Change subject: add debug command to communicate with card
..

add debug command to communicate with card

currently the sim-iccid command only selects MF.
this is a proof of concept for card communication.
the transmission/reception must be generalized to avoid copying
the code multiple times.

IMPORTANT: the sim-iccid commands only works after the sim-atr
command, which sets up the cards correctly

Change-Id: Icbcde783143694afce23af6e3f26bcd7a9a13b85
---
M sysmoOCTSIM/main.c
1 file changed, 145 insertions(+), 7 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/sysmoOCTSIM/main.c b/sysmoOCTSIM/main.c
index a895c14..11849c7 100644
--- a/sysmoOCTSIM/main.c
+++ b/sysmoOCTSIM/main.c
@@ -38,10 +38,28 @@
 // TODO for now SIM7 is not present because used for debug
 static struct usart_async_descriptor* SIM_peripheral_descriptors[] = {, 
, , , , , , NULL};

+/** number of bytes transmitted on the SIM peripheral */
+static volatile bool SIM_tx_count[8];
+
 static void SIM_rx_cb(const struct usart_async_descriptor *const io_descr)
 {
 }

+/** called when the transmission is complete
+ *  e.g. this is when the byte has been sent and there is no data to transmit 
anymore
+ */
+static void SIM_tx_cb(const struct usart_async_descriptor *const io_descr)
+{
+   // find slotnr for corresponding USART
+   uint8_t slotnr;
+   for (slotnr = 0; slotnr < ARRAY_SIZE(SIM_peripheral_descriptors) && 
SIM_peripheral_descriptors[slotnr] != io_descr; slotnr++);
+
+   // set flag
+   if (slotnr < ARRAY_SIZE(SIM_peripheral_descriptors)) {
+   SIM_tx_count[slotnr] = true;
+   }
+}
+
 /** possible clock sources for the SERCOM peripheral
  *  warning: the definition must match the GCLK configuration
  */
@@ -80,6 +98,7 @@
continue;
}
usart_async_register_callback(SIM_peripheral_descriptors[i], 
USART_ASYNC_RXC_CB, SIM_rx_cb); // required for RX to work, even if the 
callback does nothing
+   usart_async_register_callback(SIM_peripheral_descriptors[i], 
USART_ASYNC_TXC_CB, SIM_tx_cb); // to count the number of bytes transmitted 
since we are using it asynchronously
usart_async_enable(SIM_peripheral_descriptors[i]);
}
 }
@@ -360,7 +379,7 @@
// check if card is present (and read current settings)
ncn8025_get(slotnr, );
if (!settings.simpres) {
-   printf("no card present in slot %d, aborting\r\n", slotnr);
+   printf("(%d) error: no card present\r\n", slotnr);
return;
}

@@ -406,19 +425,19 @@
}
// verify if one byte has been received
if (!usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
-   printf("card in slot %d is not responding, aborting\r\n", 
slotnr);
+   printf("(%d) error: card not responsive\r\n", slotnr);
return;
}

// read ATR (just do it until there is no traffic anymore)
-   // TODO the ATR should be parsed to read the right number of bytes
+   // TODO the ATR should be parsed to read the right number of bytes, 
instead we just wait until to end of WT
printf("(%d) ATR: ", slotnr);
uint8_t atr_byte;
while (usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) 
{
if (1 == io_read(_peripheral_descriptors[slotnr]->io, 
_byte, 1)) {
printf("%02x ", atr_byte);
}
-   uint16_t wt = 9600; // waiting time in ETU
+   uint16_t wt = ISO7816_3_DEFAULT_WT; // waiting time in ETU
while (wt && 
!usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
delay_us(149); // wait for 1 ETU (372 / 1 / 2.5 MHz = 
148.8 us)
wt--;
@@ -426,9 +445,127 @@
}
printf("\r\n");

-   /* disable VCC and LED, re-enable RST */
-   settings.cmdvcc = false;
-   settings.rstin = true;
+   /* disable LED */
+   settings.led = false;
+   ncn8025_set(slotnr, );
+}
+
+DEFUN(sim_iccid, cmd_sim_iccid, "sim-iccid", "Read ICCID from SIM card")
+{
+   struct ncn8025_settings settings;
+   int slotnr = validate_slotnr(argc, argv, 1);
+
+   if (slotnr < 0 || slotnr >= ARRAY_SIZE(SIM_peripheral_descriptors) || 
NULL == SIM_peripheral_descriptors[slotnr]) {
+   return;
+   }
+
+   // read current settings and check if card is present and powered
+   ncn8025_get(slotnr, );
+   if (!settings.simpres) {
+   printf("(%d) error: no card present\r\n", slotnr);
+   return;
+   }
+   if (!settings.cmdvcc) {
+   printf("(%d) error: card not powered\r\n", 

Change in osmo-ccid-firmware[master]: add debug command to communicate with card

2019-05-10 Thread Harald Welte
Harald Welte has posted comments on this change. ( 
https://gerrit.osmocom.org/13943 )

Change subject: add debug command to communicate with card
..


Patch Set 3: Code-Review+2


--
To view, visit https://gerrit.osmocom.org/13943
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-ccid-firmware
Gerrit-Branch: master
Gerrit-MessageType: comment
Gerrit-Change-Id: Icbcde783143694afce23af6e3f26bcd7a9a13b85
Gerrit-Change-Number: 13943
Gerrit-PatchSet: 3
Gerrit-Owner: Kévin Redon 
Gerrit-Reviewer: Harald Welte 
Gerrit-Reviewer: Jenkins Builder (102)
Gerrit-Comment-Date: Fri, 10 May 2019 07:07:55 +
Gerrit-HasComments: No
Gerrit-HasLabels: Yes


Change in osmo-ccid-firmware[master]: add debug command to communicate with card

2019-05-09 Thread Kévin Redon
Hello Jenkins Builder,

I'd like you to reexamine a change. Please visit

https://gerrit.osmocom.org/13943

to look at the new patch set (#3).

Change subject: add debug command to communicate with card
..

add debug command to communicate with card

currently the sim-iccid command only selects MF.
this is a proof of concept for card communication.
the transmission/reception must be generalized to avoid copying
the code multiple times.

IMPORTANT: the sim-iccid commands only works after the sim-atr
command, which sets up the cards correctly

Change-Id: Icbcde783143694afce23af6e3f26bcd7a9a13b85
---
M sysmoOCTSIM/main.c
1 file changed, 145 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-ccid-firmware 
refs/changes/43/13943/3
--
To view, visit https://gerrit.osmocom.org/13943
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-ccid-firmware
Gerrit-Branch: master
Gerrit-MessageType: newpatchset
Gerrit-Change-Id: Icbcde783143694afce23af6e3f26bcd7a9a13b85
Gerrit-Change-Number: 13943
Gerrit-PatchSet: 3
Gerrit-Owner: Kévin Redon 
Gerrit-Reviewer: Jenkins Builder (102)


Change in osmo-ccid-firmware[master]: add debug command to communicate with card

2019-05-09 Thread Kévin Redon
Kévin Redon has uploaded this change for review. ( 
https://gerrit.osmocom.org/13943


Change subject: add debug command to communicate with card
..

add debug command to communicate with card

currently the read-iccid command only selects MF.
this is a proof of concept for card communication.
the transmission/reception must be generalized to avoid copying
the code multiple times.

Change-Id: Icbcde783143694afce23af6e3f26bcd7a9a13b85
---
M sysmoOCTSIM/main.c
1 file changed, 145 insertions(+), 7 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-ccid-firmware 
refs/changes/43/13943/1

diff --git a/sysmoOCTSIM/main.c b/sysmoOCTSIM/main.c
index a895c14..11849c7 100644
--- a/sysmoOCTSIM/main.c
+++ b/sysmoOCTSIM/main.c
@@ -38,10 +38,28 @@
 // TODO for now SIM7 is not present because used for debug
 static struct usart_async_descriptor* SIM_peripheral_descriptors[] = {, 
, , , , , , NULL};

+/** number of bytes transmitted on the SIM peripheral */
+static volatile bool SIM_tx_count[8];
+
 static void SIM_rx_cb(const struct usart_async_descriptor *const io_descr)
 {
 }

+/** called when the transmission is complete
+ *  e.g. this is when the byte has been sent and there is no data to transmit 
anymore
+ */
+static void SIM_tx_cb(const struct usart_async_descriptor *const io_descr)
+{
+   // find slotnr for corresponding USART
+   uint8_t slotnr;
+   for (slotnr = 0; slotnr < ARRAY_SIZE(SIM_peripheral_descriptors) && 
SIM_peripheral_descriptors[slotnr] != io_descr; slotnr++);
+
+   // set flag
+   if (slotnr < ARRAY_SIZE(SIM_peripheral_descriptors)) {
+   SIM_tx_count[slotnr] = true;
+   }
+}
+
 /** possible clock sources for the SERCOM peripheral
  *  warning: the definition must match the GCLK configuration
  */
@@ -80,6 +98,7 @@
continue;
}
usart_async_register_callback(SIM_peripheral_descriptors[i], 
USART_ASYNC_RXC_CB, SIM_rx_cb); // required for RX to work, even if the 
callback does nothing
+   usart_async_register_callback(SIM_peripheral_descriptors[i], 
USART_ASYNC_TXC_CB, SIM_tx_cb); // to count the number of bytes transmitted 
since we are using it asynchronously
usart_async_enable(SIM_peripheral_descriptors[i]);
}
 }
@@ -360,7 +379,7 @@
// check if card is present (and read current settings)
ncn8025_get(slotnr, );
if (!settings.simpres) {
-   printf("no card present in slot %d, aborting\r\n", slotnr);
+   printf("(%d) error: no card present\r\n", slotnr);
return;
}

@@ -406,19 +425,19 @@
}
// verify if one byte has been received
if (!usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
-   printf("card in slot %d is not responding, aborting\r\n", 
slotnr);
+   printf("(%d) error: card not responsive\r\n", slotnr);
return;
}

// read ATR (just do it until there is no traffic anymore)
-   // TODO the ATR should be parsed to read the right number of bytes
+   // TODO the ATR should be parsed to read the right number of bytes, 
instead we just wait until to end of WT
printf("(%d) ATR: ", slotnr);
uint8_t atr_byte;
while (usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) 
{
if (1 == io_read(_peripheral_descriptors[slotnr]->io, 
_byte, 1)) {
printf("%02x ", atr_byte);
}
-   uint16_t wt = 9600; // waiting time in ETU
+   uint16_t wt = ISO7816_3_DEFAULT_WT; // waiting time in ETU
while (wt && 
!usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
delay_us(149); // wait for 1 ETU (372 / 1 / 2.5 MHz = 
148.8 us)
wt--;
@@ -426,9 +445,127 @@
}
printf("\r\n");

-   /* disable VCC and LED, re-enable RST */
-   settings.cmdvcc = false;
-   settings.rstin = true;
+   /* disable LED */
+   settings.led = false;
+   ncn8025_set(slotnr, );
+}
+
+DEFUN(sim_iccid, cmd_sim_iccid, "sim-iccid", "Read ICCID from SIM card")
+{
+   struct ncn8025_settings settings;
+   int slotnr = validate_slotnr(argc, argv, 1);
+
+   if (slotnr < 0 || slotnr >= ARRAY_SIZE(SIM_peripheral_descriptors) || 
NULL == SIM_peripheral_descriptors[slotnr]) {
+   return;
+   }
+
+   // read current settings and check if card is present and powered
+   ncn8025_get(slotnr, );
+   if (!settings.simpres) {
+   printf("(%d) error: no card present\r\n", slotnr);
+   return;
+   }
+   if (!settings.cmdvcc) {
+   printf("(%d) error: card not powered\r\n", slotnr);
+   return;
+   }
+   if (settings.rstin) {
+   printf("(%d) error: card