sim: Add osim_card_{reset,close}() API

This is required to reset and close a card under software control
after opening it with osim_card_open()

Change-Id: Ie9ec66db4d54fdb1331f4ae05ca3ca4274912e9d
This commit is contained in:
Harald Welte 2021-06-01 20:11:19 +02:00
parent 6c7ac6abcd
commit 20199da02d
4 changed files with 56 additions and 0 deletions

View File

@ -12,3 +12,4 @@ libosmogsm gsm0808_old_bss_to_new_bss_info ABI break (struct changes size),
libosmosim osim_card_hdl ABI + API breakage due to new struct members
libosmocore osmo_tdef_fsm_inst_state_chg change default_timeout arg from unsigned long to long type (API breakage, not ABI)
libosmovty vty_read_config_filep New API
libosmosim osim_card_{reset,close} New API

View File

@ -375,6 +375,8 @@ struct osim_reader_ops {
const char *name;
struct osim_reader_hdl *(*reader_open)(int idx, const char *name, void *ctx);
struct osim_card_hdl *(*card_open)(struct osim_reader_hdl *rh, enum osim_proto proto);
int (*card_reset)(struct osim_card_hdl *card, bool cold_reset);
int (*card_close)(struct osim_card_hdl *card);
int (*transceive)(struct osim_reader_hdl *rh, struct msgb *msg);
};
@ -441,4 +443,6 @@ int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg);
struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver drv, int idx,
const char *name, void *ctx);
struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto);
int osim_card_reset(struct osim_card_hdl *card, bool cold_reset);
int osim_card_close(struct osim_card_hdl *card);
#endif /* _OSMOCOM_SIM_H */

View File

@ -277,3 +277,24 @@ struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto
return ch;
}
int osim_card_reset(struct osim_card_hdl *card, bool cold_reset)
{
struct osim_reader_hdl *rh = card->reader;
return rh->ops->card_reset(card, cold_reset);
}
int osim_card_close(struct osim_card_hdl *card)
{
struct osim_reader_hdl *rh = card->reader;
int rc;
rc = rh->ops->card_close(card);
card->reader = NULL;
talloc_free(card);
rh->card = NULL;
return rc;
}

View File

@ -156,6 +156,34 @@ end:
return NULL;
}
static int pcsc_card_reset(struct osim_card_hdl *card, bool cold_reset)
{
struct pcsc_reader_state *st = card->reader->priv;
LONG rc;
rc = SCardReconnect(st->hCard, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0,
cold_reset ? SCARD_UNPOWER_CARD : SCARD_RESET_CARD,
&st->dwActiveProtocol);
PCSC_ERROR(rc, "SCardReconnect");
return 0;
end:
return -EIO;
}
static int pcsc_card_close(struct osim_card_hdl *card)
{
struct pcsc_reader_state *st = card->reader->priv;
LONG rc;
rc = SCardDisconnect(st->hCard, SCARD_UNPOWER_CARD);
PCSC_ERROR(rc, "SCardDisconnect");
return 0;
end:
return -EIO;
}
static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg)
{
@ -179,6 +207,8 @@ const struct osim_reader_ops pcsc_reader_ops = {
.name = "PC/SC",
.reader_open = pcsc_reader_open,
.card_open = pcsc_card_open,
.card_reset = pcsc_card_reset,
.card_close = pcsc_card_close,
.transceive = pcsc_transceive,
};