liyujie
2025-08-28 867b8b7b729282c7e14e200ca277435329ebe747
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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef CPP_ESE_H_
#define CPP_ESE_H_
 
#include <vector>
 
#include <ese/ese.h>
 
namespace android {
 
/**
 * Mockable wrapper for libese's C API.
 *
 * `init` and `open` must be implemented to select the appropriate HW implementation.
 */
class EseInterface {
public:
    EseInterface() : mEse(nullptr) {}
    virtual ~EseInterface() = default;
 
    virtual void init() = 0;
    virtual int open() = 0;
    virtual void close() = 0;
 
    virtual const char* name() { return ese_name(mEse); }
 
    /**
     * Doesn't match the libese interface perfectly as this uses vectors instead
     * of raw C-style arrays but it makes it much easier to mock/test.
     */
    virtual int transceive(const std::vector<uint8_t>& tx, std::vector<uint8_t>& rx) {
        return ese_transceive(mEse,
                              tx.data(), static_cast<uint32_t>(tx.size()),
                              rx.data(), static_cast<uint32_t>(rx.size()));
    }
 
    // TODO: overload transceive for ese_transceive_sg
 
    virtual bool error() { return ese_error(mEse); }
    virtual const char* error_message() { return ese_error_message(mEse); }
    virtual int error_code() { return ese_error_code(mEse); }
 
    /**
     * This is needed in order to call the applet libraries written in C. We
     * should work out a better way to do this.
     */
    ::EseInterface* ese_interface() { return mEse; }
 
protected:
    ::EseInterface* mEse;
};
 
} // namespace android
 
#endif // CPP_ESE_H_