Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
connect.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file connect.c
12 * @brief Implements the OP_PCSC_CONNECT opcode (conditional build).
13 *
14 * Connects to a smart card in the specified reader using an existing PC/SC
15 * context. On success, allocates/returns a card handle id from the internal
16 * registry. When PCSC support is disabled at build time, this opcode returns 0.
17 *
18 * OP_PCSC_CONNECT: (ctx_id:int, reader_name:any) -> int
19 *
20 * - Pops: reader_name (converted to string), then ctx_id.
21 * - Pushes: card handle id (>0) on success; 0 on error/invalid inputs or when
22 * FUN_WITH_PCSC is disabled.
23 * - Notes: Uses SCARD_SHARE_SHARED and negotiates T0/T1 protocols.
24 */
25
26/* PCSC connect */
28#ifdef FUN_WITH_PCSC
29 /* Stack: [..., ctx_id, reader_name] -> pops reader_name first, then ctx_id */
30 Value vreader = pop_value(vm);
32
33 int ctx_id = (int)vctx.i;
35 char *rname = value_to_string_alloc(&vreader);
36 free_value(vreader);
37
39 if (!e || !rname) {
40 if (rname) free(rname);
41 push_value(vm, make_int(0));
42 break;
43 }
44
46 if (!hslot) {
47 free(rname);
48 push_value(vm, make_int(0));
49 break;
50 }
52 DWORD dwActive = 0;
53 LONG rv = SCardConnect(e->ctx, rname, SCARD_SHARE_SHARED,
54 SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
55 &ce->h, &dwActive);
57 if (rv != SCARD_S_SUCCESS) {
58 ce->in_use = 0;
59 push_value(vm, make_int(0));
60 break;
61 }
62 ce->proto = dwActive;
64#else
65 Value vreader = pop_value(vm);
66 free_value(vreader);
67 Value vctx = pop_value(vm);
69 push_value(vm, make_int(0));
70#endif
71 break;
72}
@ OP_PCSC_CONNECT
Definition bytecode.h:187
int hslot
Definition connect.c:45
push_value(vm, make_int(hslot))
pcsc_card_entry * ce
Definition connect.c:51
char * rname
Definition connect.c:35
int ctx_id
Definition connect.c:33
LONG rv
Definition connect.c:53
DWORD dwActive
Definition connect.c:52
Value vctx
Definition connect.c:31
pcsc_ctx_entry * e
Definition connect.c:38
free_value(vctx)
free(rname)
static pcsc_card_entry * pcsc_get_card(int id)
Lookup a card slot by id.
Definition pcsc.c:102
static int pcsc_alloc_card_slot(void)
Allocate a free card slot in the PC/SC registry.
Definition pcsc.c:70
static pcsc_ctx_entry * pcsc_get_ctx(int id)
Lookup a context slot by id.
Definition pcsc.c:88
Tagged union representing a Fun value.
Definition value.h:68
Definition pcsc.c:40
Definition pcsc.c:35
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580