forked from kmiit/FakeOmapi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReader.cpp
More file actions
89 lines (80 loc) · 3.23 KB
/
Reader.cpp
File metadata and controls
89 lines (80 loc) · 3.23 KB
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
84
85
86
87
88
89
#include "Reader.h"
#include "Session.h"
#include <algorithm>
namespace aidl::android::se {
using aidl::android::se::omapi::SecureElementSession;
SecureElementReader::SecureElementReader(std::shared_ptr<SecureElementService> service,
::android::sp<Terminal> terminal)
: mService(service),
mTerminal(terminal) {}
std::vector<uint8_t> SecureElementReader::getAtr() {
LOG(INFO) << __PRETTY_FUNCTION__;
return mTerminal->getAtr();
}
::ndk::ScopedAStatus SecureElementReader::isSecureElementPresent(bool* isTrue) {
LOG(INFO) << __PRETTY_FUNCTION__;
*isTrue = mTerminal->isSecureElementPresent();
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementReader::closeSessions() {
LOG(INFO) << __PRETTY_FUNCTION__;
// Snapshot + unlock before calling Session::close(): close() re-enters
// removeSession() which also takes mLock, so holding it here would
// self-deadlock on a non-recursive std::mutex.
std::vector<std::shared_ptr<SecureElementSession>> snapshot;
{
std::lock_guard<std::mutex> lock(mLock);
snapshot = std::move(mSessions);
mSessions.clear();
}
for (auto& cSession : snapshot) {
if (cSession) {
cSession->close();
}
}
return ::ndk::ScopedAStatus::ok();
}
void SecureElementReader::removeSession(SecureElementSession* session) {
LOG(INFO) << __PRETTY_FUNCTION__;
if (!session) {
LOG(ERROR) << "Session is empty, failed to remove";
return;
}
std::lock_guard<std::mutex> lock(mLock);
mSessions.erase(
std::remove_if(
mSessions.begin(),
mSessions.end(),
[&session](const std::shared_ptr<aidl::android::se::omapi::SecureElementSession>& ptr) {
return ptr.get() == session;
}
),
mSessions.end()
);
if (mSessions.empty()) {
mTerminal->mDefaultApplicationSelectedOnBasicChannel = true;
}
}
::ndk::ScopedAStatus SecureElementReader::openSession(std::shared_ptr<ISecureElementSession>* session) {
LOG(INFO) << __PRETTY_FUNCTION__;
if (!mTerminal->isSecureElementPresent()) {
LOG(ERROR) << "Secure Element is not present";
return ::ndk::ScopedAStatus::fromExceptionCodeWithMessage(
EX_ILLEGAL_STATE, "Secure Element is not present");
}
std::lock_guard<std::mutex> lock(mLock);
auto nSession = ndk::SharedRefBase::make<SecureElementSession>(this->ref<SecureElementReader>());
mSessions.push_back(nSession);
*session = std::static_pointer_cast<ISecureElementSession>(nSession);
return ::ndk::ScopedAStatus::ok();
}
::android::sp<Terminal> SecureElementReader::getTerminal() {
LOG(INFO) << __PRETTY_FUNCTION__;
return mTerminal;
}
::ndk::ScopedAStatus SecureElementReader::reset(bool* isReset) {
LOG(INFO) << __PRETTY_FUNCTION__;
*isReset = mTerminal->reset();
return ::ndk::ScopedAStatus::ok();
}
}