2018-11-21 01:26:11 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2019-10-19 19:51:10 +00:00
|
|
|
#include <stdlib.h>
|
2018-11-21 01:26:11 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "board/sg-cmd.h"
|
|
|
|
#include "board/sg-nfc.h"
|
|
|
|
#include "board/sg-nfc-cmd.h"
|
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
#include "iccard/aime.h"
|
|
|
|
#include "iccard/felica.h"
|
|
|
|
|
2018-11-21 01:26:11 +00:00
|
|
|
#include "util/dprintf.h"
|
2019-10-19 19:51:10 +00:00
|
|
|
#include "util/dump.h"
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_dispatch(
|
|
|
|
void *ctx,
|
|
|
|
const void *v_req,
|
2019-10-26 22:25:04 +00:00
|
|
|
void *v_res);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_reset(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_res_header *res);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_get_fw_version(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_get_fw_version *res);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_get_hw_version(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_get_hw_version *res);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
static HRESULT sg_nfc_cmd_poll(
|
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_poll *res);
|
2019-10-19 19:51:10 +00:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_poll_aime(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
struct sg_nfc_poll_mifare *mifare);
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_poll_felica(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
struct sg_nfc_poll_felica *felica);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_mifare_read_block(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_nfc_req_mifare_read_block *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_mifare_read_block *res);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
static HRESULT sg_nfc_cmd_felica_encap(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
const struct sg_nfc_req_felica_encap *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_felica_encap *res);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_dummy(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_res_header *res);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2023-12-03 17:45:42 +00:00
|
|
|
static const char *hw_version[] = {
|
|
|
|
"TN32MSEC003S H/W Ver3.0",
|
|
|
|
"837-15286",
|
|
|
|
"837-15396"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *fw_version[] = {
|
|
|
|
"TN32MSEC003S F/W Ver1.2",
|
|
|
|
"\x94",
|
|
|
|
"\x94"
|
|
|
|
};
|
|
|
|
|
2018-11-21 01:26:11 +00:00
|
|
|
void sg_nfc_init(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
uint8_t addr,
|
|
|
|
const struct sg_nfc_ops *ops,
|
2023-12-03 17:45:42 +00:00
|
|
|
unsigned int gen,
|
2018-11-21 01:26:11 +00:00
|
|
|
void *ops_ctx)
|
|
|
|
{
|
|
|
|
assert(nfc != NULL);
|
|
|
|
assert(ops != NULL);
|
|
|
|
|
|
|
|
nfc->ops = ops;
|
|
|
|
nfc->ops_ctx = ops_ctx;
|
|
|
|
nfc->addr = addr;
|
2023-12-03 17:45:42 +00:00
|
|
|
nfc->gen = gen;
|
2018-11-21 01:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#define sg_nfc_dprintfv(nfc, fmt, ap)
|
|
|
|
#define sg_nfc_dprintf(nfc, fmt, ...)
|
|
|
|
#else
|
|
|
|
static void sg_nfc_dprintfv(
|
|
|
|
const struct sg_nfc *nfc,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
dprintf("NFC %02x: ", nfc->addr);
|
|
|
|
dprintfv(fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sg_nfc_dprintf(
|
|
|
|
const struct sg_nfc *nfc,
|
|
|
|
const char *fmt,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
sg_nfc_dprintfv(nfc, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void sg_nfc_transact(
|
|
|
|
struct sg_nfc *nfc,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct iobuf *res_frame,
|
2018-11-21 01:26:11 +00:00
|
|
|
const void *req_bytes,
|
|
|
|
size_t req_nbytes)
|
|
|
|
{
|
|
|
|
assert(nfc != NULL);
|
2019-10-26 22:25:04 +00:00
|
|
|
assert(res_frame != NULL);
|
2018-11-21 01:26:11 +00:00
|
|
|
assert(req_bytes != NULL);
|
|
|
|
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_req_transact(res_frame, req_bytes, req_nbytes, sg_nfc_dispatch, nfc);
|
2018-11-21 01:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_dispatch(
|
|
|
|
void *ctx,
|
|
|
|
const void *v_req,
|
2019-10-26 22:25:04 +00:00
|
|
|
void *v_res)
|
2018-11-21 01:26:11 +00:00
|
|
|
{
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc;
|
2018-11-21 01:26:11 +00:00
|
|
|
const union sg_nfc_req_any *req;
|
2019-10-26 22:25:04 +00:00
|
|
|
union sg_nfc_res_any *res;
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
nfc = ctx;
|
|
|
|
req = v_req;
|
2019-10-26 22:25:04 +00:00
|
|
|
res = v_res;
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
if (req->simple.hdr.addr != nfc->addr) {
|
|
|
|
/* Not addressed to us, don't send a response */
|
|
|
|
return S_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (req->simple.hdr.cmd) {
|
|
|
|
case SG_NFC_CMD_RESET:
|
2019-10-26 22:25:04 +00:00
|
|
|
return sg_nfc_cmd_reset(nfc, &req->simple, &res->simple);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
case SG_NFC_CMD_GET_FW_VERSION:
|
|
|
|
return sg_nfc_cmd_get_fw_version(
|
|
|
|
nfc,
|
|
|
|
&req->simple,
|
2019-10-26 22:25:04 +00:00
|
|
|
&res->get_fw_version);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
case SG_NFC_CMD_GET_HW_VERSION:
|
|
|
|
return sg_nfc_cmd_get_hw_version(
|
|
|
|
nfc,
|
|
|
|
&req->simple,
|
2019-10-26 22:25:04 +00:00
|
|
|
&res->get_hw_version);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
case SG_NFC_CMD_POLL:
|
|
|
|
return sg_nfc_cmd_poll(
|
2018-11-21 01:26:11 +00:00
|
|
|
nfc,
|
|
|
|
&req->simple,
|
2019-10-26 22:25:04 +00:00
|
|
|
&res->poll);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
case SG_NFC_CMD_MIFARE_READ_BLOCK:
|
|
|
|
return sg_nfc_cmd_mifare_read_block(
|
|
|
|
nfc,
|
|
|
|
&req->mifare_read_block,
|
2019-10-26 22:25:04 +00:00
|
|
|
&res->mifare_read_block);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
case SG_NFC_CMD_FELICA_ENCAP:
|
|
|
|
return sg_nfc_cmd_felica_encap(
|
|
|
|
nfc,
|
|
|
|
&req->felica_encap,
|
2019-10-26 22:25:04 +00:00
|
|
|
&res->felica_encap);
|
2019-10-19 19:51:10 +00:00
|
|
|
|
|
|
|
case SG_NFC_CMD_MIFARE_AUTHENTICATE:
|
2018-11-21 01:26:11 +00:00
|
|
|
case SG_NFC_CMD_MIFARE_SELECT_TAG:
|
2019-10-19 19:51:10 +00:00
|
|
|
case SG_NFC_CMD_MIFARE_SET_KEY_AIME:
|
|
|
|
case SG_NFC_CMD_MIFARE_SET_KEY_BANA:
|
|
|
|
case SG_NFC_CMD_RADIO_ON:
|
|
|
|
case SG_NFC_CMD_RADIO_OFF:
|
2023-12-03 17:45:42 +00:00
|
|
|
case SG_NFC_CMD_SEND_HEX_DATA: // TODO: implement?
|
2019-10-26 22:25:04 +00:00
|
|
|
return sg_nfc_cmd_dummy(nfc, &req->simple, &res->simple);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
sg_nfc_dprintf(nfc, "Unimpl command %02x\n", req->simple.hdr.cmd);
|
|
|
|
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_reset(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_res_header *res)
|
2018-11-21 01:26:11 +00:00
|
|
|
{
|
|
|
|
sg_nfc_dprintf(nfc, "Reset\n");
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_res_init(res, req, 0);
|
|
|
|
res->status = 3;
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_get_fw_version(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_get_fw_version *res)
|
2018-11-21 01:26:11 +00:00
|
|
|
{
|
2023-12-03 17:45:42 +00:00
|
|
|
unsigned int len = strlen(fw_version[nfc->gen - 1]);
|
|
|
|
|
2018-11-21 01:26:11 +00:00
|
|
|
/* Dest version is not NUL terminated, this is intentional */
|
2023-12-03 17:45:42 +00:00
|
|
|
sg_res_init(&res->res, req, len);
|
|
|
|
memcpy(res->version, fw_version[nfc->gen - 1], len);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_get_hw_version(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_get_hw_version *res)
|
2018-11-21 01:26:11 +00:00
|
|
|
{
|
2023-12-03 17:45:42 +00:00
|
|
|
unsigned int len = strlen(hw_version[nfc->gen - 1]);
|
|
|
|
|
2018-11-21 01:26:11 +00:00
|
|
|
/* Dest version is not NUL terminated, this is intentional */
|
2023-12-03 17:45:42 +00:00
|
|
|
sg_res_init(&res->res, req, len);
|
|
|
|
memcpy(res->version, hw_version[nfc->gen - 1], len);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
static HRESULT sg_nfc_cmd_poll(
|
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_poll *res)
|
2018-11-21 01:26:11 +00:00
|
|
|
{
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc_poll_mifare mifare;
|
|
|
|
struct sg_nfc_poll_felica felica;
|
2018-11-21 01:26:11 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
hr = nfc->ops->poll(nfc->ops_ctx);
|
2018-12-17 22:09:56 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
2018-12-17 22:09:56 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
hr = sg_nfc_poll_felica(nfc, &felica);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
if (SUCCEEDED(hr) && hr != S_FALSE) {
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_res_init(&res->res, req, 1 + sizeof(felica));
|
|
|
|
memcpy(res->payload, &felica, sizeof(felica));
|
|
|
|
res->count = 1;
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
return S_OK;
|
2019-10-19 19:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hr = sg_nfc_poll_aime(nfc, &mifare);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr) && hr != S_FALSE) {
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_res_init(&res->res, req, 1 + sizeof(mifare));
|
|
|
|
memcpy(res->payload, &mifare, sizeof(mifare));
|
|
|
|
res->count = 1;
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
return S_OK;
|
2019-10-19 19:51:10 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_res_init(&res->res, req, 1);
|
|
|
|
res->count = 0;
|
2019-10-19 19:51:10 +00:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_poll_aime(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
struct sg_nfc_poll_mifare *mifare)
|
|
|
|
{
|
|
|
|
uint8_t luid[10];
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
/* Call backend */
|
|
|
|
|
|
|
|
if (nfc->ops->get_aime_id != NULL) {
|
|
|
|
hr = nfc->ops->get_aime_id(nfc->ops_ctx, luid, sizeof(luid));
|
|
|
|
} else {
|
|
|
|
hr = S_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr) || hr == S_FALSE) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
sg_nfc_dprintf(nfc, "AiMe card is present\n");
|
|
|
|
|
|
|
|
/* Construct response (use an arbitrary UID) */
|
|
|
|
|
|
|
|
mifare->type = 0x10;
|
|
|
|
mifare->id_len = sizeof(mifare->uid);
|
|
|
|
mifare->uid = _byteswap_ulong(0x01020304);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
/* Initialize MIFARE IC emulator */
|
|
|
|
|
|
|
|
hr = aime_card_populate(&nfc->mifare, luid, sizeof(luid));
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2018-11-21 01:26:11 +00:00
|
|
|
return hr;
|
2019-10-19 19:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_poll_felica(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
struct sg_nfc_poll_felica *felica)
|
|
|
|
{
|
|
|
|
uint64_t IDm;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
/* Call backend */
|
|
|
|
|
|
|
|
if (nfc->ops->get_felica_id != NULL) {
|
|
|
|
hr = nfc->ops->get_felica_id(nfc->ops_ctx, &IDm);
|
2018-11-21 01:26:11 +00:00
|
|
|
} else {
|
2019-10-19 19:51:10 +00:00
|
|
|
hr = S_FALSE;
|
|
|
|
}
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
if (FAILED(hr) || hr == S_FALSE) {
|
|
|
|
return hr;
|
2018-11-21 01:26:11 +00:00
|
|
|
}
|
2019-10-19 19:51:10 +00:00
|
|
|
|
|
|
|
sg_nfc_dprintf(nfc, "FeliCa card is present\n");
|
|
|
|
|
|
|
|
/* Construct poll response */
|
|
|
|
|
|
|
|
felica->type = 0x20;
|
|
|
|
felica->id_len = sizeof(felica->IDm) + sizeof(felica->PMm);
|
|
|
|
felica->IDm = _byteswap_uint64(IDm);
|
|
|
|
felica->PMm = _byteswap_uint64(felica_get_generic_PMm());
|
|
|
|
|
|
|
|
/* Initialize FeliCa IC emulator */
|
|
|
|
|
|
|
|
nfc->felica.IDm = IDm;
|
|
|
|
nfc->felica.PMm = felica_get_generic_PMm();
|
|
|
|
nfc->felica.system_code = 0x0000;
|
|
|
|
|
|
|
|
return S_OK;
|
2018-11-21 01:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_mifare_read_block(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_nfc_req_mifare_read_block *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_mifare_read_block *res)
|
2018-11-21 01:26:11 +00:00
|
|
|
{
|
2019-02-25 22:23:04 +00:00
|
|
|
uint32_t uid;
|
|
|
|
|
2018-11-21 01:26:11 +00:00
|
|
|
if (req->req.payload_len != sizeof(req->payload)) {
|
|
|
|
sg_nfc_dprintf(nfc, "%s: Payload size is incorrect\n", __func__);
|
|
|
|
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
uid = _byteswap_ulong(req->payload.uid);
|
2019-02-25 22:23:04 +00:00
|
|
|
|
|
|
|
sg_nfc_dprintf(nfc, "Read uid %08x block %i\n", uid, req->payload.block_no);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
if (req->payload.block_no > 3) {
|
|
|
|
sg_nfc_dprintf(nfc, "MIFARE block number out of range\n");
|
|
|
|
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_res_init(&res->res, &req->req, sizeof(res->block));
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-26 22:25:04 +00:00
|
|
|
memcpy( res->block,
|
2019-10-19 19:51:10 +00:00
|
|
|
nfc->mifare.sectors[0].blocks[req->payload.block_no].bytes,
|
2019-10-26 22:25:04 +00:00
|
|
|
sizeof(res->block));
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
static HRESULT sg_nfc_cmd_felica_encap(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
const struct sg_nfc_req_felica_encap *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_nfc_res_felica_encap *res)
|
2019-10-19 19:51:10 +00:00
|
|
|
{
|
|
|
|
struct const_iobuf f_req;
|
|
|
|
struct iobuf f_res;
|
|
|
|
HRESULT hr;
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
/* First byte of encapsulated request and response is a length byte
|
|
|
|
(inclusive of itself). The FeliCa emulator expects its caller to handle
|
|
|
|
that length byte on its behalf (we adopt the convention that the length
|
|
|
|
byte is part of the FeliCa protocol's framing layer). */
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
if (req->req.payload_len != 8 + req->payload[0]) {
|
2018-11-21 01:26:11 +00:00
|
|
|
sg_nfc_dprintf(
|
|
|
|
nfc,
|
2019-10-19 19:51:10 +00:00
|
|
|
"FeliCa encap payload length mismatch: sg %i != felica %i + 8",
|
|
|
|
req->req.payload_len,
|
|
|
|
req->payload[0]);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
return E_FAIL;
|
2018-11-21 01:26:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
f_req.bytes = req->payload;
|
|
|
|
f_req.nbytes = req->payload[0];
|
|
|
|
f_req.pos = 1;
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-26 22:25:04 +00:00
|
|
|
f_res.bytes = res->payload;
|
|
|
|
f_res.nbytes = sizeof(res->payload);
|
2019-10-19 19:51:10 +00:00
|
|
|
f_res.pos = 1;
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
#if 0
|
|
|
|
dprintf("FELICA OUTBOUND:\n");
|
|
|
|
dump_const_iobuf(&f_req);
|
|
|
|
#endif
|
2018-11-21 01:26:11 +00:00
|
|
|
|
2019-10-19 19:51:10 +00:00
|
|
|
hr = felica_transact(&nfc->felica, &f_req, &f_res);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_res_init(&res->res, &req->req, f_res.pos);
|
|
|
|
res->payload[0] = f_res.pos;
|
2019-10-19 19:51:10 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
dprintf("FELICA INBOUND:\n");
|
|
|
|
dump_iobuf(&f_res);
|
|
|
|
#endif
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_dummy(
|
2019-10-19 19:51:10 +00:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 01:26:11 +00:00
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_res_header *res)
|
2018-11-21 01:26:11 +00:00
|
|
|
{
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_res_init(res, req, 0);
|
2018-11-21 01:26:11 +00:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|