• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CPP_ESE_H_
18 #define CPP_ESE_H_
19 
20 #include <vector>
21 
22 #include <ese/ese.h>
23 
24 namespace android {
25 
26 /**
27  * Mockable wrapper for libese's C API.
28  *
29  * `init` and `open` must be implemented to select the appropriate HW implementation.
30  */
31 class EseInterface {
32 public:
EseInterface()33     EseInterface() : mEse(nullptr) {}
34     virtual ~EseInterface() = default;
35 
36     virtual void init() = 0;
37     virtual int open() = 0;
38     virtual void close() = 0;
39 
name()40     virtual const char* name() { return ese_name(mEse); }
41 
42     /**
43      * Doesn't match the libese interface perfectly as this uses vectors instead
44      * of raw C-style arrays but it makes it much easier to mock/test.
45      */
transceive(const std::vector<uint8_t> & tx,std::vector<uint8_t> & rx)46     virtual int transceive(const std::vector<uint8_t>& tx, std::vector<uint8_t>& rx) {
47         return ese_transceive(mEse,
48                               tx.data(), static_cast<uint32_t>(tx.size()),
49                               rx.data(), static_cast<uint32_t>(rx.size()));
50     }
51 
52     // TODO: overload transceive for ese_transceive_sg
53 
error()54     virtual bool error() { return ese_error(mEse); }
error_message()55     virtual const char* error_message() { return ese_error_message(mEse); }
error_code()56     virtual int error_code() { return ese_error_code(mEse); }
57 
58     /**
59      * This is needed in order to call the applet libraries written in C. We
60      * should work out a better way to do this.
61      */
ese_interface()62     ::EseInterface* ese_interface() { return mEse; }
63 
64 protected:
65     ::EseInterface* mEse;
66 };
67 
68 } // namespace android
69 
70 #endif // CPP_ESE_H_
71