1
0
Fork 0
forked from fun/fun

Some arg Fun in Rust. (0.38.1)

This commit is contained in:
Johannes Findeisen 2026-01-29 00:53:34 +01:00
commit 543483041b
16 changed files with 167 additions and 66 deletions

View file

@ -17,11 +17,9 @@
include <hex.fun>
/*
// Unified PCSC class API
class PCSC()
// ---- hex helpers moved to lib/hex.fun ----
// ---- PCSC wrappers ----
// ---- Context management ----
fun establish(this)
// returns ctx id (>0) or 0
return pcsc_establish()
@ -36,6 +34,16 @@ class PCSC()
return []
return readers
// Convenience: get readers without managing ctx externally
fun get_readers(this)
ctx = pcsc_establish()
readers = pcsc_list_readers(ctx)
_ = pcsc_release(ctx)
if readers == nil
return []
return readers
// ---- Connection management ----
fun connect(this, ctx, reader_name)
// returns handle id (>0) or 0
return pcsc_connect(ctx, to_string(reader_name))
@ -44,6 +52,7 @@ class PCSC()
// returns 1 on success or 0
return pcsc_disconnect(handle)
// ---- Transmit helpers ----
// Transmit raw APDU bytes (array of numbers 0..255). Returns map {"data":[], "sw1":n, "sw2":n, "code":n}
fun transmit_bytes(this, handle, bytes)
res = pcsc_transmit(handle, bytes)
@ -77,22 +86,12 @@ class PCSC()
m["sw2"] = res["sw2"]
m["code"] = res["code"]
return m
*/
// This class is in a very early stage of development.
class PCSC()
fun get_readers(this)
ctx = pcsc_establish()
readers = pcsc_list_readers(ctx)
_ = pcsc_release(ctx)
return readers
// Transmit a raw hex APDU to the first available reader and print the result map
// Convenience: Transmit a raw hex APDU to the first available reader and return the result map
fun transmit(this, hex_apdu)
ctx = pcsc_establish()
readers = pcsc_list_readers(ctx)
if (readers == nil)
print([])
_ = pcsc_release(ctx)
// return a default result map instead of 0, so callers can index fields safely
m = {}
@ -102,7 +101,6 @@ class PCSC()
m["code"] = -2
return m
if (len(readers) == 0)
print([])
_ = pcsc_release(ctx)
m = {}
m["data"] = []
@ -110,50 +108,19 @@ class PCSC()
m["sw2"] = -1
m["code"] = -2
return m
// Actually selecting the second reader hardcoded.
// Select the second reader (index 1) as previously used in examples
handle = pcsc_connect(ctx, readers[1])
apdu = this.hex_to_bytes(hex_apdu)
res = pcsc_transmit(handle, apdu)
apdu_bytes = hex_to_bytes(hex_apdu)
res = pcsc_transmit(handle, apdu_bytes)
_ = pcsc_disconnect(handle)
_ = pcsc_release(ctx)
if res == nil
m = {}
m["data"] = []
m["sw1"] = -1
m["sw2"] = -1
m["code"] = -2
return m
if res["data"] == nil
res["data"] = []
return res
fun hex_digit(this, ch)
m = {}
m["0"] = 0
m["1"] = 1
m["2"] = 2
m["3"] = 3
m["4"] = 4
m["5"] = 5
m["6"] = 6
m["7"] = 7
m["8"] = 8
m["9"] = 9
m["a"] = 10
m["A"] = 10
m["b"] = 11
m["B"] = 11
m["c"] = 12
m["C"] = 12
m["d"] = 13
m["D"] = 13
m["e"] = 14
m["E"] = 14
m["f"] = 15
m["F"] = 15
v = m[ch]
if v == nil
return 0
return v
fun hex_to_bytes(this, hex)
s = to_string(hex)
out = []
number i = 0
number n = len(s)
while i + 1 < n
number b = this.hex_digit(substr(s, i, 1)) * 16 + this.hex_digit(substr(s, i + 1, 1))
push(out, b)
i = i + 2
return out