-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibeuicc.go
83 lines (74 loc) · 1.68 KB
/
libeuicc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package libeuicc
/*
#include <stdlib.h>
#include <string.h>
#include "euicc.h"
*/
import "C"
import (
"errors"
"unsafe"
)
const APDUMTU = 240
type Libeuicc struct {
euiccCtx *C.struct_euicc_ctx
driver *driver
}
type driver struct {
apdu APDU
}
var (
ErrEuiccInitFailed = errors.New("euicc_init failed")
ErrNotEnoughMemory = errors.New("not enough memory")
)
// New creates a new Libeuicc instance.
func New(apdu APDU, customLogger Logger) (*Libeuicc, error) {
if customLogger != nil {
logger = customLogger
}
euiccCtx := (*C.struct_euicc_ctx)(C.malloc(C.sizeof_struct_euicc_ctx))
if euiccCtx == nil {
return nil, ErrNotEnoughMemory
}
C.memset(unsafe.Pointer(euiccCtx), 0, C.sizeof_struct_euicc_ctx)
euiccCtx.es10x_mss = APDUMTU
libeuicc := &Libeuicc{
euiccCtx: euiccCtx,
driver: &driver{
apdu: apdu,
},
}
if err := libeuicc.initAPDU(); err != nil {
libeuicc.Close()
return nil, err
}
if err := libeuicc.initHttp(); err != nil {
libeuicc.Close()
return nil, err
}
if C.euicc_init(libeuicc.euiccCtx) == CError {
return nil, ErrEuiccInitFailed
}
return libeuicc, nil
}
// Close closes the Libeuicc instance. It must be called when the instance is no longer needed.
func (e *Libeuicc) Close() {
if e.euiccCtx != nil {
C.euicc_fini(e.euiccCtx)
defer func() {
C.free(unsafe.Pointer(e.euiccCtx))
e.euiccCtx = nil
}()
}
if e.euiccCtx.http._interface != nil {
C.free(unsafe.Pointer(e.euiccCtx.http._interface))
e.euiccCtx.http._interface = nil
}
if e.euiccCtx.apdu._interface != nil {
C.free(unsafe.Pointer(e.euiccCtx.apdu._interface))
e.euiccCtx.apdu._interface = nil
}
}
func (e *Libeuicc) cleanupHttp() {
C.euicc_http_cleanup(e.euiccCtx)
}