Split USB_Component from USB_Emulation and add CCID_Emulation
This commit is contained in:
parent
10ce350e47
commit
af4236db1f
|
@ -4,253 +4,88 @@ import from USB_Types all;
|
||||||
import from USB_Templates all;
|
import from USB_Templates all;
|
||||||
import from USB_PortType all;
|
import from USB_PortType all;
|
||||||
import from USB_PortTypes all;
|
import from USB_PortTypes all;
|
||||||
|
import from USB_Component all;
|
||||||
|
|
||||||
type component USB_CT {
|
type component Demo_CT extends USB_CT {
|
||||||
port USB_PT USB;
|
|
||||||
var integer g_dev_hdl := 42;
|
|
||||||
var integer g_next_req_hdl := 23;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
modulepar {
|
modulepar {
|
||||||
hexstring mp_vendor_id := '138a'H;
|
hexstring mp_vendor_id := '1d50'H;
|
||||||
hexstring mp_product_id := '0017'H;
|
hexstring mp_product_id := '6141'H;
|
||||||
}
|
}
|
||||||
|
|
||||||
function f_init_usb() runs on USB_CT {
|
function f_init_usb() runs on USB_CT {
|
||||||
map(self:USB, system:USB);
|
f_usb_init_vid_pid(mp_vendor_id, mp_product_id);
|
||||||
|
|
||||||
var USB_result res;
|
|
||||||
var integer req_hdl := f_usb_get_req_hdl();
|
|
||||||
USB.send(ts_USB_open_vid_pid(hex2int(mp_vendor_id), hex2int(mp_product_id),
|
|
||||||
device_hdl := g_dev_hdl, req_hdl := req_hdl));
|
|
||||||
alt {
|
|
||||||
[] USB.receive(tr_USB_result(req_hdl := req_hdl, device_hdl := g_dev_hdl)) -> value res {
|
|
||||||
log("Received ", res);
|
|
||||||
}
|
|
||||||
[] USB.receive {
|
|
||||||
testcase.stop("Couldn't open requested USB device");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template (value) USB_transfer ts_UsbXfer_DevReq(template (value) USB_DeviceRequest req,
|
|
||||||
integer device_hdl, integer xfer_hdl) := {
|
|
||||||
device_hdl := device_hdl,
|
|
||||||
transfer_hdl := xfer_hdl,
|
|
||||||
endpoint := 0,
|
|
||||||
ttype := USB_TRANSFER_TYPE_CONTROL,
|
|
||||||
data := enc_USB_DeviceRequest(valueof(req)),
|
|
||||||
timeout_msec := 1000
|
|
||||||
}
|
|
||||||
|
|
||||||
template USB_transfer_compl tr_UsbXfer_compl(template USB_endpoint ep := ?,
|
testcase TC_get_desc_device() runs on Demo_CT {
|
||||||
template USB_transfer_type ttype := ?,
|
|
||||||
template USB_transfer_status sts := ?,
|
|
||||||
template integer device_hdl := ?,
|
|
||||||
template integer xfer_hdl := ?) := {
|
|
||||||
device_hdl := device_hdl,
|
|
||||||
transfer_hdl := xfer_hdl,
|
|
||||||
endpoint := ep,
|
|
||||||
ttype := ttype,
|
|
||||||
data := ?,
|
|
||||||
status := sts
|
|
||||||
}
|
|
||||||
|
|
||||||
function f_usb_get_req_hdl() runs on USB_CT return integer
|
|
||||||
{
|
|
||||||
var integer i := g_next_req_hdl;
|
|
||||||
g_next_req_hdl := g_next_req_hdl + 1;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
function f_usb_claim_interface(integer dev_hdl, integer bInterface)
|
|
||||||
runs on USB_CT {
|
|
||||||
var USB_result res;
|
|
||||||
var integer req_hdl := f_usb_get_req_hdl();
|
|
||||||
USB.send(ts_USB_claim_interface(dev_hdl, bInterface, req_hdl));
|
|
||||||
USB.receive(USB_result:{req_hdl, dev_hdl, ?}) -> value res { log("Received ", res); }
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Send a USB Device Request and wait for its completion to arrive */
|
|
||||||
private function f_xceive_DevReq(template (value) USB_DeviceRequest req)
|
|
||||||
runs on USB_CT return USB_transfer_compl
|
|
||||||
{
|
|
||||||
var USB_transfer_compl tc;
|
|
||||||
var integer req_hdl := f_usb_get_req_hdl();
|
|
||||||
USB.send(ts_UsbXfer_DevReq(req, g_dev_hdl, req_hdl));
|
|
||||||
alt {
|
|
||||||
[] USB.receive(tr_UsbXfer_compl(0, ?, ?, g_dev_hdl, req_hdl)) -> value tc { }
|
|
||||||
[] USB.receive {
|
|
||||||
testcase.stop("Received unexpected primitive from USB");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Send a USB Device Request and expect it to fail with a certain transfer status */
|
|
||||||
private function f_DevReq_exp_fail(template (value) USB_DeviceRequest req,
|
|
||||||
template USB_transfer_status sts := USB_TRANSFER_STALL)
|
|
||||||
runs on USB_CT
|
|
||||||
{
|
|
||||||
var USB_transfer_compl tc := f_xceive_DevReq(req);
|
|
||||||
if (match(tc.status, sts)) {
|
|
||||||
setverdict(pass);
|
|
||||||
} else {
|
|
||||||
setverdict(fail, "Unexpected USB transfer status ", tc.status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function f_get_desc(USB_DescriptorType dtype, integer idx, integer len)
|
|
||||||
runs on USB_CT return octetstring
|
|
||||||
{
|
|
||||||
var USB_transfer_compl tc;
|
|
||||||
var integer req_hdl := f_usb_get_req_hdl();
|
|
||||||
USB.send(ts_UsbXfer_DevReq(ts_DevReq_GET_DESCRIPTOR(dtype, idx, len), g_dev_hdl, req_hdl));
|
|
||||||
alt {
|
|
||||||
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, USB_TRANSFER_COMPLETED, g_dev_hdl, req_hdl)) -> value tc {
|
|
||||||
var octetstring raw_desc := substr(tc.data, 8, lengthof(tc.data)-8);
|
|
||||||
return raw_desc;
|
|
||||||
}
|
|
||||||
[] USB.receive(tr_UsbXfer_compl(0, ?, ?, g_dev_hdl, req_hdl)) -> value tc{
|
|
||||||
setverdict(fail, "Transfer completed unsuccessfully: ", tc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ''O;
|
|
||||||
}
|
|
||||||
|
|
||||||
function f_usb_get_desc_std(USB_DescriptorType dtype, integer idx, integer len)
|
|
||||||
runs on USB_CT return USB_StandardDescriptors
|
|
||||||
{
|
|
||||||
var octetstring raw := f_get_desc(dtype, idx, len);
|
|
||||||
return dec_USB_StandardDescriptors(raw);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function f_get_desc_exp_fail(USB_DescriptorType dtype, integer idx, integer len,
|
|
||||||
template USB_transfer_status exp_status := USB_TRANSFER_STALL)
|
|
||||||
runs on USB_CT
|
|
||||||
{
|
|
||||||
f_DevReq_exp_fail(ts_DevReq_GET_DESCRIPTOR(dtype, idx, len), exp_status);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function f_get_config()
|
|
||||||
runs on USB_CT return integer {
|
|
||||||
var USB_transfer_compl tc;
|
|
||||||
var integer req_hdl := f_usb_get_req_hdl();
|
|
||||||
USB.send(ts_UsbXfer_DevReq(ts_DevReq_GET_CONFIGURATION, g_dev_hdl, req_hdl));
|
|
||||||
alt {
|
|
||||||
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, USB_TRANSFER_COMPLETED, g_dev_hdl, req_hdl)) -> value tc {
|
|
||||||
var octetstring raw_desc := substr(tc.data, 8, lengthof(tc.data)-8);
|
|
||||||
return oct2int(raw_desc[0]);
|
|
||||||
}
|
|
||||||
[] USB.receive(tr_UsbXfer_compl(0, ?, ?, g_dev_hdl, req_hdl)) -> value tc{
|
|
||||||
setverdict(fail, "Transfer completed unsuccessfully: ", tc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
};
|
|
||||||
|
|
||||||
private function f_get_interface(integer intf)
|
|
||||||
runs on USB_CT return integer {
|
|
||||||
var USB_transfer_compl tc;
|
|
||||||
var integer req_hdl := f_usb_get_req_hdl();
|
|
||||||
USB.send(ts_UsbXfer_DevReq(ts_DevReq_GET_INTERFACE(intf), g_dev_hdl, req_hdl));
|
|
||||||
alt {
|
|
||||||
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, USB_TRANSFER_COMPLETED, g_dev_hdl, req_hdl)) -> value tc {
|
|
||||||
var octetstring raw_desc := substr(tc.data, 8, lengthof(tc.data)-8);
|
|
||||||
return oct2int(raw_desc[0]);
|
|
||||||
}
|
|
||||||
[] USB.receive(tr_UsbXfer_compl(0, ?, ?, g_dev_hdl, req_hdl)) -> value tc{
|
|
||||||
setverdict(fail, "Transfer completed unsuccessfully: ", tc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
};
|
|
||||||
|
|
||||||
private function f_get_status(USB_RequestType_Recipient recipient, u16le_t wIndex)
|
|
||||||
runs on USB_CT return integer
|
|
||||||
{
|
|
||||||
var USB_transfer_compl tc;
|
|
||||||
var integer req_hdl := f_usb_get_req_hdl();
|
|
||||||
USB.send(ts_UsbXfer_DevReq(ts_DevReq_GET_STATUS(recipient, wIndex), g_dev_hdl, req_hdl));
|
|
||||||
alt {
|
|
||||||
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, USB_TRANSFER_COMPLETED, g_dev_hdl, req_hdl)) -> value tc {
|
|
||||||
var octetstring raw_desc := substr(tc.data, 8, lengthof(tc.data)-8);
|
|
||||||
return oct2int(raw_desc);
|
|
||||||
}
|
|
||||||
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, ?, g_dev_hdl, req_hdl)) -> value tc{
|
|
||||||
setverdict(fail, "Transfer completed unsuccessfully: ", tc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
testcase TC_get_desc_device() runs on USB_CT {
|
|
||||||
f_init_usb();
|
f_init_usb();
|
||||||
var USB_StandardDescriptors descs;
|
var USB_StandardDescriptors descs;
|
||||||
descs := f_usb_get_desc_std(USB_DescriptorType_DEVICE, 0, 256);
|
descs := f_usb_get_desc_std(USB_DescriptorType_DEVICE, 0, 256);
|
||||||
/* FIXME: do some verifications and set veridct */
|
/* FIXME: do some verifications and set veridct */
|
||||||
setverdict(pass);
|
setverdict(pass);
|
||||||
}
|
}
|
||||||
testcase TC_get_desc_device_illegal_idx() runs on USB_CT {
|
testcase TC_get_desc_device_illegal_idx() runs on Demo_CT {
|
||||||
f_init_usb();
|
f_init_usb();
|
||||||
f_get_desc_exp_fail(USB_DescriptorType_DEVICE, 23, 256);
|
f_usb_get_desc_exp_fail(USB_DescriptorType_DEVICE, 23, 256);
|
||||||
}
|
}
|
||||||
testcase TC_get_desc_config() runs on USB_CT {
|
testcase TC_get_desc_config() runs on Demo_CT {
|
||||||
f_init_usb();
|
f_init_usb();
|
||||||
var USB_StandardDescriptors descs;
|
var USB_StandardDescriptors descs;
|
||||||
descs := f_usb_get_desc_std(USB_DescriptorType_CONFIGURATION, 0, 256);
|
descs := f_usb_get_desc_std(USB_DescriptorType_CONFIGURATION, 0, 256);
|
||||||
/* FIXME: do some verifications and set veridct */
|
/* FIXME: do some verifications and set veridct */
|
||||||
setverdict(pass);
|
setverdict(pass);
|
||||||
}
|
}
|
||||||
testcase TC_get_desc_config_illegal_idx() runs on USB_CT {
|
testcase TC_get_desc_config_illegal_idx() runs on Demo_CT {
|
||||||
f_init_usb();
|
f_init_usb();
|
||||||
f_get_desc_exp_fail(USB_DescriptorType_CONFIGURATION, 23, 256);
|
f_usb_get_desc_exp_fail(USB_DescriptorType_CONFIGURATION, 23, 256);
|
||||||
}
|
}
|
||||||
testcase TC_get_config() runs on USB_CT {
|
testcase TC_get_config() runs on Demo_CT {
|
||||||
f_init_usb();
|
f_init_usb();
|
||||||
var integer cfg_num := f_get_config();
|
var integer cfg_num := f_usb_get_config();
|
||||||
if (cfg_num == 0) {
|
if (cfg_num == 0) {
|
||||||
setverdict(pass);
|
setverdict(pass);
|
||||||
} else {
|
} else {
|
||||||
setverdict(fail, "unexpected configuration: ", cfg_num);
|
setverdict(fail, "unexpected configuration: ", cfg_num);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
testcase TC_get_interface() runs on USB_CT {
|
testcase TC_get_interface() runs on Demo_CT {
|
||||||
f_init_usb();
|
f_init_usb();
|
||||||
var integer altsett_num := f_get_interface(0);
|
var integer altsett_num := f_usb_get_interface(0);
|
||||||
if (altsett_num == 0) {
|
if (altsett_num == 0) {
|
||||||
setverdict(pass);
|
setverdict(pass);
|
||||||
} else {
|
} else {
|
||||||
setverdict(fail, "unexpected interface altsetting: ", altsett_num);
|
setverdict(fail, "unexpected interface altsetting: ", altsett_num);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
testcase TC_get_status() runs on USB_CT {
|
testcase TC_get_status() runs on Demo_CT {
|
||||||
f_init_usb();
|
f_init_usb();
|
||||||
|
|
||||||
/* valid get_status on device */
|
/* valid get_status on device */
|
||||||
f_get_status(RequestRecipient_Device, 0);
|
f_usb_get_status(RequestRecipient_Device, 0);
|
||||||
|
|
||||||
/* valid get_status on interface */
|
/* valid get_status on interface */
|
||||||
f_get_status(RequestRecipient_Interface, 0);
|
f_usb_get_status(RequestRecipient_Interface, 0);
|
||||||
|
|
||||||
/* valid get_status on endpoint */
|
/* valid get_status on endpoint */
|
||||||
f_get_status(RequestRecipient_Endpoint, 1);
|
f_usb_get_status(RequestRecipient_Endpoint, 1);
|
||||||
}
|
}
|
||||||
testcase TC_get_status_invalid() runs on USB_CT {
|
testcase TC_get_status_invalid() runs on Demo_CT {
|
||||||
f_init_usb();
|
f_init_usb();
|
||||||
|
|
||||||
f_DevReq_exp_fail(ts_DevReq_GET_STATUS(RequestRecipient_Device, 1));
|
f_usb_dev_req_exp_fail(ts_DevReq_GET_STATUS(RequestRecipient_Device, 1));
|
||||||
|
|
||||||
/* Gets blocked by kernel USB? */
|
/* Gets blocked by kernel USB? */
|
||||||
//f_DevReq_exp_fail(ts_DevReq_GET_STATUS(RequestRecipient_Interface, 100));
|
//f_usb_dev_req_exp_fail(ts_DevReq_GET_STATUS(RequestRecipient_Interface, 100));
|
||||||
//f_DevReq_exp_fail(ts_DevReq_GET_STATUS(RequestRecipient_Endpoint, 100));
|
//f_usb_dev_req_exp_fail(ts_DevReq_GET_STATUS(RequestRecipient_Endpoint, 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
testcase TC_foobar() runs on USB_CT {
|
testcase TC_foobar() runs on Demo_CT {
|
||||||
var USB_result res;
|
var USB_result res;
|
||||||
|
|
||||||
f_init_usb();
|
f_init_usb();
|
||||||
|
|
|
@ -0,0 +1,163 @@
|
||||||
|
module CCID_Emulation {
|
||||||
|
|
||||||
|
/* This module implements multiplexing/demultiplexing of USB CCID, enabling different
|
||||||
|
* TTCN-3 test components to talk to different CCID slots
|
||||||
|
*
|
||||||
|
* (C) 2019 by Harald Welte <laforge@gnumonks.org>
|
||||||
|
* All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
import from General_Types all;
|
||||||
|
import from Osmocom_Types all;
|
||||||
|
|
||||||
|
import from USB_Types all;
|
||||||
|
import from USB_Templates all;
|
||||||
|
import from USB_PortType all;
|
||||||
|
import from USB_PortTypes all;
|
||||||
|
import from USB_Component all;
|
||||||
|
|
||||||
|
import from CCID_Types all;
|
||||||
|
import from CCID_Templates all;
|
||||||
|
|
||||||
|
type enumerated CCID_Emulation_Event_UpDown {
|
||||||
|
CCID_EVENT_UP,
|
||||||
|
CCID_EVENT_DOWN
|
||||||
|
};
|
||||||
|
|
||||||
|
type union CCID_Emulation_Event {
|
||||||
|
CCID_Emulation_Event_UpDown up_down
|
||||||
|
};
|
||||||
|
|
||||||
|
type port CCID_SLOT_PT message {
|
||||||
|
inout CCID_PDU, CCID_Emulation_Event;
|
||||||
|
} with { extension "internal" }
|
||||||
|
|
||||||
|
type component CCID_Emulation_CT extends USB_CT {
|
||||||
|
var integer g_interface;
|
||||||
|
var integer g_ep_in;
|
||||||
|
var integer g_ep_out;
|
||||||
|
var integer g_ep_irq;
|
||||||
|
|
||||||
|
var integer g_next_bseq := 0;
|
||||||
|
|
||||||
|
/* ports to the test components; one for each theoretically possible slot */
|
||||||
|
port CCID_SLOT_PT SLOT[256];
|
||||||
|
}
|
||||||
|
|
||||||
|
private const octetstring c_oct261 := '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'O;
|
||||||
|
|
||||||
|
private function f_usb_submit_xfer(USB_endpoint ep, octetstring data := c_oct261,
|
||||||
|
USB_transfer_type ttype := USB_TRANSFER_TYPE_BULK,
|
||||||
|
integer tout_ms := 30000) runs on CCID_Emulation_CT
|
||||||
|
{
|
||||||
|
var integer req_hdl := f_usb_get_req_hdl();
|
||||||
|
var USB_transfer xfer := {
|
||||||
|
device_hdl := g_dev_hdl,
|
||||||
|
transfer_hdl := req_hdl,
|
||||||
|
endpoint := ep,
|
||||||
|
ttype := ttype,
|
||||||
|
data := data,
|
||||||
|
timeout_msec := tout_ms
|
||||||
|
};
|
||||||
|
USB.send(xfer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function main() runs on CCID_Emulation_CT {
|
||||||
|
var USB_transfer_compl tc;
|
||||||
|
var CCID_PDU ccid_in, ccid_out;
|
||||||
|
var integer i, v_i;
|
||||||
|
|
||||||
|
f_usb_init_vid_pid('1d50'H, '6141'H);
|
||||||
|
|
||||||
|
/* FIXME: get device descriptor; find CCID interface; find endpoints */
|
||||||
|
g_interface := 0;
|
||||||
|
g_ep_out := hex2int('02'H);
|
||||||
|
g_ep_in := hex2int('83'H);
|
||||||
|
g_ep_irq := hex2int('84'H);
|
||||||
|
|
||||||
|
f_usb_claim_interface(g_dev_hdl, g_interface);
|
||||||
|
|
||||||
|
/* submit xfer fro IN and IRQ endpoints */
|
||||||
|
f_usb_submit_xfer(g_ep_in);
|
||||||
|
f_usb_submit_xfer(g_ep_irq, ttype := USB_TRANSFER_TYPE_INTERRUPT);
|
||||||
|
|
||||||
|
/* let everyone know we're alive and kicking */
|
||||||
|
for (i := 0; i < 256; i := i+1) {
|
||||||
|
if (SLOT[i].checkstate("Connected")) {
|
||||||
|
SLOT[i].send(CCID_Emulation_Event:{up_down:=CCID_EVENT_UP});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
alt {
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(g_ep_out, USB_TRANSFER_TYPE_BULK, USB_TRANSFER_COMPLETED,
|
||||||
|
g_dev_hdl, ?)) -> value tc {
|
||||||
|
/* do nothing; normal completion of OUT transfer */
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(g_ep_in, USB_TRANSFER_TYPE_BULK, USB_TRANSFER_COMPLETED,
|
||||||
|
g_dev_hdl, ?)) -> value tc {
|
||||||
|
/* Submit another IN transfer */
|
||||||
|
f_usb_submit_xfer(g_ep_in);
|
||||||
|
/* forward to slot-specific port */
|
||||||
|
ccid_in := dec_CCID_PDU(tc.data);
|
||||||
|
SLOT[ccid_in.hdr.bSlot].send(ccid_in);
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(g_ep_irq, USB_TRANSFER_TYPE_INTERRUPT,
|
||||||
|
USB_TRANSFER_COMPLETED, g_dev_hdl, ?)) -> value tc {
|
||||||
|
/* Submit another IRQ transfer */
|
||||||
|
f_usb_submit_xfer(g_ep_irq);
|
||||||
|
/* forward to all slot-specific ports with connected components */
|
||||||
|
/*
|
||||||
|
ccid_in := dec_CCID_PDU(tc.data);
|
||||||
|
for (i := 0; i < 256; i := i+1) {
|
||||||
|
if (SLOT[i].checkstate("Connected")) {
|
||||||
|
SLOT[i].send(ccid_in);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(?, ?, USB_TRANSFER_ERROR, g_dev_hdl, ?)) -> value tc {
|
||||||
|
setverdict(fail, "Unexpected USB_TRANSFER_ERROR on EP ", int2hex(tc.endpoint, 2));
|
||||||
|
mtc.stop;
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(?, ?, USB_TRANSFER_TIMED_OUT, g_dev_hdl, ?)) -> value tc {
|
||||||
|
setverdict(fail, "Unexpected USB_TRANSFER_TIMED_OUT on EP ", int2hex(tc.endpoint, 2));
|
||||||
|
mtc.stop;
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(?, ?, USB_TRANSFER_OVERFLOW, g_dev_hdl, ?)) -> value tc {
|
||||||
|
setverdict(fail, "Unexpected USB_TRANSFER_OVERFLOW on EP ", int2hex(tc.endpoint, 2));
|
||||||
|
mtc.stop;
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(?, ?, ?, g_dev_hdl, ?)) -> value tc {
|
||||||
|
setverdict(fail, "Unexpected USB Endpoint ", int2hex(tc.endpoint, 2));
|
||||||
|
mtc.stop;
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(?, ?, ?, ?, ?)) -> value tc {
|
||||||
|
setverdict(fail, "Unexpected USB Device ", tc.device_hdl);
|
||||||
|
mtc.stop;
|
||||||
|
}
|
||||||
|
[] USB.receive {
|
||||||
|
setverdict(fail, "Unexpected Message from USB");
|
||||||
|
mtc.stop;
|
||||||
|
}
|
||||||
|
|
||||||
|
[] any from SLOT.receive(CCID_PDU:?) -> value ccid_out @index value v_i {
|
||||||
|
var octetstring bin;
|
||||||
|
ccid_out.hdr.bSlot := v_i;
|
||||||
|
ccid_out.hdr.bSeq := g_next_bseq;
|
||||||
|
g_next_bseq := (g_next_bseq + 1) mod 256;
|
||||||
|
bin := enc_CCID_PDU(ccid_out);
|
||||||
|
f_usb_submit_xfer(g_ep_out, bin, tout_ms := 3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} /* alt */
|
||||||
|
} /* while (true) */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,185 @@
|
||||||
|
module USB_Component {
|
||||||
|
|
||||||
|
import from General_Types all;
|
||||||
|
|
||||||
|
import from USB_Types all;
|
||||||
|
import from USB_Templates all;
|
||||||
|
import from USB_PortType all;
|
||||||
|
import from USB_PortTypes all;
|
||||||
|
|
||||||
|
type component USB_CT {
|
||||||
|
port USB_PT USB;
|
||||||
|
var integer g_dev_hdl := 42;
|
||||||
|
var integer g_next_req_hdl := 13;
|
||||||
|
}
|
||||||
|
|
||||||
|
function f_usb_init_vid_pid(HEX4n vendor_id, HEX4n product_id) runs on USB_CT {
|
||||||
|
map(self:USB, system:USB);
|
||||||
|
|
||||||
|
var USB_result res;
|
||||||
|
var integer req_hdl := f_usb_get_req_hdl();
|
||||||
|
USB.send(ts_USB_open_vid_pid(hex2int(vendor_id), hex2int(product_id),
|
||||||
|
device_hdl := g_dev_hdl, req_hdl := req_hdl));
|
||||||
|
alt {
|
||||||
|
[] USB.receive(tr_USB_result(req_hdl := req_hdl, device_hdl := g_dev_hdl)) -> value res {
|
||||||
|
log("Received ", res);
|
||||||
|
}
|
||||||
|
[] USB.receive {
|
||||||
|
testcase.stop("Couldn't open requested USB device");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template (value) USB_transfer ts_UsbXfer_DevReq(template (value) USB_DeviceRequest req,
|
||||||
|
integer device_hdl, integer xfer_hdl) := {
|
||||||
|
device_hdl := device_hdl,
|
||||||
|
transfer_hdl := xfer_hdl,
|
||||||
|
endpoint := 0,
|
||||||
|
ttype := USB_TRANSFER_TYPE_CONTROL,
|
||||||
|
data := enc_USB_DeviceRequest(valueof(req)),
|
||||||
|
timeout_msec := 1000
|
||||||
|
}
|
||||||
|
|
||||||
|
template USB_transfer_compl tr_UsbXfer_compl(template USB_endpoint ep := ?,
|
||||||
|
template USB_transfer_type ttype := ?,
|
||||||
|
template USB_transfer_status sts := ?,
|
||||||
|
template integer device_hdl := ?,
|
||||||
|
template integer xfer_hdl := ?) := {
|
||||||
|
device_hdl := device_hdl,
|
||||||
|
transfer_hdl := xfer_hdl,
|
||||||
|
endpoint := ep,
|
||||||
|
ttype := ttype,
|
||||||
|
data := ?,
|
||||||
|
status := sts
|
||||||
|
}
|
||||||
|
|
||||||
|
function f_usb_get_req_hdl() runs on USB_CT return integer
|
||||||
|
{
|
||||||
|
var integer i := g_next_req_hdl;
|
||||||
|
g_next_req_hdl := g_next_req_hdl + 1;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
function f_usb_claim_interface(integer dev_hdl, integer bInterface)
|
||||||
|
runs on USB_CT {
|
||||||
|
var USB_result res;
|
||||||
|
var integer req_hdl := f_usb_get_req_hdl();
|
||||||
|
USB.send(ts_USB_claim_interface(dev_hdl, bInterface, req_hdl));
|
||||||
|
USB.receive(USB_result:{req_hdl, dev_hdl, ?}) -> value res { log("Received ", res); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Send a USB Device Request and wait for its completion to arrive */
|
||||||
|
function f_usb_dev_req(template (value) USB_DeviceRequest req)
|
||||||
|
runs on USB_CT return USB_transfer_compl
|
||||||
|
{
|
||||||
|
var USB_transfer_compl tc;
|
||||||
|
var integer req_hdl := f_usb_get_req_hdl();
|
||||||
|
USB.send(ts_UsbXfer_DevReq(req, g_dev_hdl, req_hdl));
|
||||||
|
alt {
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(0, ?, ?, g_dev_hdl, req_hdl)) -> value tc { }
|
||||||
|
[] USB.receive {
|
||||||
|
testcase.stop("Received unexpected primitive from USB");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Send a USB Device Request and expect it to fail with a certain transfer status */
|
||||||
|
function f_usb_dev_req_exp_fail(template (value) USB_DeviceRequest req,
|
||||||
|
template USB_transfer_status sts := USB_TRANSFER_STALL)
|
||||||
|
runs on USB_CT
|
||||||
|
{
|
||||||
|
var USB_transfer_compl tc := f_usb_dev_req(req);
|
||||||
|
if (match(tc.status, sts)) {
|
||||||
|
setverdict(pass);
|
||||||
|
} else {
|
||||||
|
setverdict(fail, "Unexpected USB transfer status ", tc.status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function f_usb_get_desc(USB_DescriptorType dtype, integer idx, integer len)
|
||||||
|
runs on USB_CT return octetstring
|
||||||
|
{
|
||||||
|
var USB_transfer_compl tc;
|
||||||
|
var integer req_hdl := f_usb_get_req_hdl();
|
||||||
|
USB.send(ts_UsbXfer_DevReq(ts_DevReq_GET_DESCRIPTOR(dtype, idx, len), g_dev_hdl, req_hdl));
|
||||||
|
alt {
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, USB_TRANSFER_COMPLETED, g_dev_hdl, req_hdl)) -> value tc {
|
||||||
|
var octetstring raw_desc := substr(tc.data, 8, lengthof(tc.data)-8);
|
||||||
|
return raw_desc;
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(0, ?, ?, g_dev_hdl, req_hdl)) -> value tc{
|
||||||
|
setverdict(fail, "Transfer completed unsuccessfully: ", tc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ''O;
|
||||||
|
}
|
||||||
|
|
||||||
|
function f_usb_get_desc_std(USB_DescriptorType dtype, integer idx, integer len)
|
||||||
|
runs on USB_CT return USB_StandardDescriptors
|
||||||
|
{
|
||||||
|
var octetstring raw := f_usb_get_desc(dtype, idx, len);
|
||||||
|
return dec_USB_StandardDescriptors(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
function f_usb_get_desc_exp_fail(USB_DescriptorType dtype, integer idx, integer len,
|
||||||
|
template USB_transfer_status exp_status := USB_TRANSFER_STALL)
|
||||||
|
runs on USB_CT
|
||||||
|
{
|
||||||
|
f_usb_dev_req_exp_fail(ts_DevReq_GET_DESCRIPTOR(dtype, idx, len), exp_status);
|
||||||
|
}
|
||||||
|
|
||||||
|
function f_usb_get_config()
|
||||||
|
runs on USB_CT return integer {
|
||||||
|
var USB_transfer_compl tc;
|
||||||
|
var integer req_hdl := f_usb_get_req_hdl();
|
||||||
|
USB.send(ts_UsbXfer_DevReq(ts_DevReq_GET_CONFIGURATION, g_dev_hdl, req_hdl));
|
||||||
|
alt {
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, USB_TRANSFER_COMPLETED, g_dev_hdl, req_hdl)) -> value tc {
|
||||||
|
var octetstring raw_desc := substr(tc.data, 8, lengthof(tc.data)-8);
|
||||||
|
return oct2int(raw_desc[0]);
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(0, ?, ?, g_dev_hdl, req_hdl)) -> value tc{
|
||||||
|
setverdict(fail, "Transfer completed unsuccessfully: ", tc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
function f_usb_get_interface(integer intf)
|
||||||
|
runs on USB_CT return integer {
|
||||||
|
var USB_transfer_compl tc;
|
||||||
|
var integer req_hdl := f_usb_get_req_hdl();
|
||||||
|
USB.send(ts_UsbXfer_DevReq(ts_DevReq_GET_INTERFACE(intf), g_dev_hdl, req_hdl));
|
||||||
|
alt {
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, USB_TRANSFER_COMPLETED, g_dev_hdl, req_hdl)) -> value tc {
|
||||||
|
var octetstring raw_desc := substr(tc.data, 8, lengthof(tc.data)-8);
|
||||||
|
return oct2int(raw_desc[0]);
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(0, ?, ?, g_dev_hdl, req_hdl)) -> value tc{
|
||||||
|
setverdict(fail, "Transfer completed unsuccessfully: ", tc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
function f_usb_get_status(USB_RequestType_Recipient recipient, u16le_t wIndex)
|
||||||
|
runs on USB_CT return integer
|
||||||
|
{
|
||||||
|
var USB_transfer_compl tc;
|
||||||
|
var integer req_hdl := f_usb_get_req_hdl();
|
||||||
|
USB.send(ts_UsbXfer_DevReq(ts_DevReq_GET_STATUS(recipient, wIndex), g_dev_hdl, req_hdl));
|
||||||
|
alt {
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, USB_TRANSFER_COMPLETED, g_dev_hdl, req_hdl)) -> value tc {
|
||||||
|
var octetstring raw_desc := substr(tc.data, 8, lengthof(tc.data)-8);
|
||||||
|
return oct2int(raw_desc);
|
||||||
|
}
|
||||||
|
[] USB.receive(tr_UsbXfer_compl(0, USB_TRANSFER_TYPE_CONTROL, ?, g_dev_hdl, req_hdl)) -> value tc{
|
||||||
|
setverdict(fail, "Transfer completed unsuccessfully: ", tc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue