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 ESED_LIBESE_H_
18 #define ESED_LIBESE_H_
19 
20 #include <vector>
21 
22 #include <android-base/logging.h>
23 
24 #include <ese/ese.h>
25 
26 namespace android {
27 namespace esed {
28 
29 /**
30  * Mockable wrapper for libese's C API.
31  *
32  * `init` and `open` must be implemented to select the appropriate HW implementation.
33  */
34 class EseInterface {
35 public:
EseInterface()36     EseInterface() : mEse(nullptr) {}
37     virtual ~EseInterface() = default;
38 
39     virtual void init() = 0;
40     virtual int open() = 0;
41     virtual void close() = 0;
42 
name()43     virtual const char* name() { return ese_name(mEse); }
44 
45     /**
46      * Doesn't match the libese interface perfectly as this uses vectors instead
47      * of raw C-style arrays but it makes it much easier to mock/test.
48      */
transceive(const std::vector<uint8_t> & tx,std::vector<uint8_t> & rx)49     virtual int transceive(const std::vector<uint8_t>& tx, std::vector<uint8_t>& rx) {
50         return ese_transceive(mEse,
51                               tx.data(), static_cast<uint32_t>(tx.size()),
52                               rx.data(), static_cast<uint32_t>(rx.size()));
53     }
54 
error()55     virtual bool error() { return ese_error(mEse); }
error_message()56     virtual const char* error_message() { return ese_error_message(mEse); }
error_code()57     virtual int error_code() { return ese_error_code(mEse); }
58 
59 protected:
60     ::EseInterface* mEse;
61 };
62 
63 } // namespace esed
64 } // namespace android
65 
66 #endif // ESED_LIBESE_H_
67