Skip to content

Commit

Permalink
add connection with Android NFC service
Browse files Browse the repository at this point in the history
  • Loading branch information
cinit committed Dec 21, 2021
1 parent 3586577 commit 6f654ef
Show file tree
Hide file tree
Showing 16 changed files with 852 additions and 6 deletions.
16 changes: 16 additions & 0 deletions app/src/main/cpp/libnciclient/NciHostDaemonProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ TypedLpcResult<int> NciHostDaemonProxy::deviceDriverWriteRawBuffer(const std::ve
TypedLpcResult<int> NciHostDaemonProxy::deviceDriverIoctl0(uint64_t request, uint64_t arg) {
return invokeRemoteProcedure<int, const uint64_t &, const uint64_t &>(Ids::deviceDriverIoctl0, request, arg);
}

TypedLpcResult<bool> NciHostDaemonProxy::isAndroidNfcServiceConnected() {
return invokeRemoteProcedure<bool>(Ids::isAndroidNfcServiceConnected);
}

TypedLpcResult<bool> NciHostDaemonProxy::connectToAndroidNfcService() {
return invokeRemoteProcedure<bool>(Ids::connectToAndroidNfcService);
}

TypedLpcResult<bool> NciHostDaemonProxy::isNfcDiscoverySoundDisabled() {
return invokeRemoteProcedure<bool>(Ids::isNfcDiscoverySoundDisabled);
}

TypedLpcResult<bool> NciHostDaemonProxy::setNfcDiscoverySoundDisabled(bool disable) {
return invokeRemoteProcedure<bool, const bool &>(Ids::setNfcDiscoverySoundDisabled, disable);
}
8 changes: 8 additions & 0 deletions app/src/main/cpp/libnciclient/NciHostDaemonProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ class NciHostDaemonProxy : public INciHostDaemon, public BaseIpcProxy {
TypedLpcResult<int> deviceDriverWriteRawBuffer(const std::vector<uint8_t> &buffer) override;

TypedLpcResult<int> deviceDriverIoctl0(uint64_t request, uint64_t arg) override;

TypedLpcResult<bool> isAndroidNfcServiceConnected() override;

TypedLpcResult<bool> connectToAndroidNfcService() override;

TypedLpcResult<bool> isNfcDiscoverySoundDisabled() override;

TypedLpcResult<bool> setNfcDiscoverySoundDisabled(bool disable) override;
};

}
Expand Down
128 changes: 124 additions & 4 deletions app/src/main/cpp/libnciclient/ipc_handle_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_ntGetHistoryI
* Method: clearHistoryIoEvents
* Signature: ()Z
*/
extern "C" [[maybe_unused]] JNIEXPORT jboolean JNICALL
extern "C" [[maybe_unused]] JNIEXPORT jboolean JNICALL
Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_clearHistoryIoEvents
(JNIEnv *env, jobject) {
IpcConnector &connector = IpcConnector::getInstance();
Expand Down Expand Up @@ -679,7 +679,7 @@ Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_clearHistoryI
* Method: getDaemonStatus
* Signature: ()Lcc/ioctl/nfcdevicehost/ipc/daemon/INciHostDaemon/DaemonStatus;
*/
extern "C" [[maybe_unused]] JNIEXPORT jobject JNICALL
extern "C" [[maybe_unused]] JNIEXPORT jobject JNICALL
Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_getDaemonStatus
(JNIEnv *env, jobject) {
IpcConnector &connector = IpcConnector::getInstance();
Expand Down Expand Up @@ -722,7 +722,7 @@ Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_getDaemonStat
* Method: deviceDriverWriteRaw
* Signature: ([B)I
*/
extern "C" [[maybe_unused]] JNIEXPORT jint JNICALL
extern "C" [[maybe_unused]] JNIEXPORT jint JNICALL
Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_deviceDriverWriteRaw
(JNIEnv *env, jobject, jbyteArray jdata) {
if (jdata == nullptr) {
Expand Down Expand Up @@ -760,7 +760,7 @@ Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_deviceDriverW
* Method: deviceDriverIoctl0
* Signature: (JJ)I
*/
extern "C" [[maybe_unused]] JNIEXPORT jint JNICALL
extern "C" [[maybe_unused]] JNIEXPORT jint JNICALL
Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_deviceDriverIoctl0
(JNIEnv *env, jobject, jlong request, jlong arg) {
IpcConnector &connector = IpcConnector::getInstance();
Expand All @@ -784,3 +784,123 @@ Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_deviceDriverI
return 0;
}
}

/*
* Class: cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy
* Method: isAndroidNfcServiceConnected
* Signature: ()Z
*/
extern "C" [[maybe_unused]] JNIEXPORT jboolean JNICALL
Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_isAndroidNfcServiceConnected
(JNIEnv *env, jobject) {
IpcConnector &connector = IpcConnector::getInstance();
INciHostDaemon *proxy = connector.getNciDaemon();
if (proxy == nullptr) {
env->ThrowNew(env->FindClass("java/lang/IllegalStateException"),
"attempt to transact while proxy object not available");
return 0;
} else {
if (auto lpcResult = proxy->isAndroidNfcServiceConnected();
!jniThrowLpcResultErrorOrException(env, lpcResult)) {
bool r;
if (lpcResult.getResult(&r)) {
return r;
} else {
env->ThrowNew(env->FindClass("java/lang/RuntimeException"),
"error while read data from LpcResult");
return 0;
}
}
return 0;
}
}

/*
* Class: cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy
* Method: connectToAndroidNfcService
* Signature: ()Z
*/
extern "C" [[maybe_unused]] JNIEXPORT jboolean JNICALL
Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_connectToAndroidNfcService
(JNIEnv *env, jobject) {
IpcConnector &connector = IpcConnector::getInstance();
INciHostDaemon *proxy = connector.getNciDaemon();
if (proxy == nullptr) {
env->ThrowNew(env->FindClass("java/lang/IllegalStateException"),
"attempt to transact while proxy object not available");
return 0;
} else {
if (auto lpcResult = proxy->connectToAndroidNfcService();
!jniThrowLpcResultErrorOrException(env, lpcResult)) {
bool r;
if (lpcResult.getResult(&r)) {
return r;
} else {
env->ThrowNew(env->FindClass("java/lang/RuntimeException"),
"error while read data from LpcResult");
return 0;
}
}
return 0;
}
}

/*
* Class: cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy
* Method: isNfcDiscoverySoundDisabled
* Signature: ()Z
*/
extern "C" [[maybe_unused]] JNIEXPORT jboolean JNICALL
Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_isNfcDiscoverySoundDisabled
(JNIEnv *env, jobject) {
IpcConnector &connector = IpcConnector::getInstance();
INciHostDaemon *proxy = connector.getNciDaemon();
if (proxy == nullptr) {
env->ThrowNew(env->FindClass("java/lang/IllegalStateException"),
"attempt to transact while proxy object not available");
return 0;
} else {
if (auto lpcResult = proxy->isNfcDiscoverySoundDisabled();
!jniThrowLpcResultErrorOrException(env, lpcResult)) {
bool r;
if (lpcResult.getResult(&r)) {
return r;
} else {
env->ThrowNew(env->FindClass("java/lang/RuntimeException"),
"error while read data from LpcResult");
return 0;
}
}
return 0;
}
}

/*
* Class: cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy
* Method: setNfcDiscoverySoundDisabled
* Signature: (Z)Z
*/
extern "C" [[maybe_unused]] JNIEXPORT jboolean JNICALL
Java_cc_ioctl_nfcdevicehost_ipc_daemon_internal_NciHostDaemonProxy_setNfcDiscoverySoundDisabled
(JNIEnv *env, jobject, jboolean jdisable) {
IpcConnector &connector = IpcConnector::getInstance();
INciHostDaemon *proxy = connector.getNciDaemon();
if (proxy == nullptr) {
env->ThrowNew(env->FindClass("java/lang/IllegalStateException"),
"attempt to transact while proxy object not available");
return 0;
} else {
if (auto lpcResult = proxy->setNfcDiscoverySoundDisabled(jdisable);
!jniThrowLpcResultErrorOrException(env, lpcResult)) {
bool r;
if (lpcResult.getResult(&r)) {
return r;
} else {
env->ThrowNew(env->FindClass("java/lang/RuntimeException"),
"error while read data from LpcResult");
return 0;
}
}
return 0;
}
}
32 changes: 32 additions & 0 deletions app/src/main/cpp/libnciclient/ipc_handle_jni.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/src/main/cpp/ncihostd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ add_executable(ncihostd startup/startup.c startup/daemon.cpp ipc/IpcStateControl
service/front/NciHostDaemonImpl.cpp service/front/NciClientProxy.cpp service/HwServiceStatus.cpp
elfsym/ElfView.cpp elfsym/ProcessView.cpp inject/Injector.cpp inject/arch/ptrace_inject_utils.cpp
inject/arch/ptrace_inject_arm_impl.cpp inject/arch/ptrace_inject_x86_impl.cpp inject/SysServicePatch.cpp
service/ServiceManager.cpp service/hw/BaseHwHalHandler.cpp service/hw/nxpnci/NxpHalHandler.cpp)
service/ServiceManager.cpp service/hw/BaseHwHalHandler.cpp service/hw/nxpnci/NxpHalHandler.cpp
service/xposed/BaseRemoteAndroidService.cpp service/xposed/AndroidNfcService.cpp)

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
SET(CLANG_CXX_EXTRA_OPT "-Werror=unknown-warning-option -Werror=format-invalid-specifier -Werror=call-to-pure-virtual-from-ctor-dtor")
Expand Down
47 changes: 47 additions & 0 deletions app/src/main/cpp/ncihostd/service/front/NciHostDaemonImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "ncihostd/service/hw/nxpnci/NxpHalHandler.h"
#include "ncihostd/inject/SysServicePatch.h"
#include "ncihostd/service/ServiceManager.h"
#include "ncihostd/service/xposed/AndroidNfcService.h"

#include "rpcprotocol/utils/TextUtils.h"
#include "rpcprotocol/utils/auto_close_fd.h"
#include "rpcprotocol/utils/SELinux.h"
Expand Down Expand Up @@ -118,6 +120,24 @@ bool NciHostDaemonImpl::dispatchLpcInvocation([[maybe_unused]] const IpcTransact
}));
return true;
}
case Ids::isAndroidNfcServiceConnected: {
result = R::invoke(this, args, R::is(+[](T *p) { return p->isAndroidNfcServiceConnected(); }));
return true;
}
case Ids::connectToAndroidNfcService: {
result = R::invoke(this, args, R::is(+[](T *p) { return p->connectToAndroidNfcService(); }));
return true;
}
case Ids::isNfcDiscoverySoundDisabled: {
result = R::invoke(this, args, R::is(+[](T *p) { return p->isNfcDiscoverySoundDisabled(); }));
return true;
}
case Ids::setNfcDiscoverySoundDisabled: {
result = R::invoke(this, args, R::is(+[](T *p, bool disabled) {
return p->setNfcDiscoverySoundDisabled(disabled);
}));
return true;
}
default:
return false;
}
Expand Down Expand Up @@ -228,6 +248,13 @@ TypedLpcResult<bool> NciHostDaemonImpl::initHwServiceConnection(const std::strin
LOGI("remote hook initialized");
}
}
{
auto &androidNfcSvc = androidsvc::AndroidNfcService::getInstance();
if (!androidNfcSvc.isConnected()) {
// we don't care about the result, the client will check the status later
(void) androidNfcSvc.tryConnect();
}
}
return true;
}

Expand Down Expand Up @@ -324,3 +351,23 @@ TypedLpcResult<int> NciHostDaemonImpl::deviceDriverIoctl0(uint64_t request, uint
return {-ENOSYS};
}
}

TypedLpcResult<bool> NciHostDaemonImpl::isAndroidNfcServiceConnected() {
const auto &androidNfcService = androidsvc::AndroidNfcService::getInstance();
return {androidNfcService.isConnected()};
}

TypedLpcResult<bool> NciHostDaemonImpl::connectToAndroidNfcService() {
auto &androidNfcService = androidsvc::AndroidNfcService::getInstance();
return {androidNfcService.tryConnect()};
}

TypedLpcResult<bool> NciHostDaemonImpl::isNfcDiscoverySoundDisabled() {
auto &androidNfcService = androidsvc::AndroidNfcService::getInstance();
return {androidNfcService.isNfcDiscoverySoundDisabled()};
}

TypedLpcResult<bool> NciHostDaemonImpl::setNfcDiscoverySoundDisabled(bool disable) {
auto &androidNfcService = androidsvc::AndroidNfcService::getInstance();
return {androidNfcService.setNfcDiscoverySoundDisabled(disable)};
}
8 changes: 8 additions & 0 deletions app/src/main/cpp/ncihostd/service/front/NciHostDaemonImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ class NciHostDaemonImpl : public INciHostDaemon, public BaseIpcObject {
TypedLpcResult<int> deviceDriverWriteRawBuffer(const std::vector<uint8_t> &buffer) override;

TypedLpcResult<int> deviceDriverIoctl0(uint64_t request, uint64_t arg) override;

TypedLpcResult<bool> isAndroidNfcServiceConnected() override;

TypedLpcResult<bool> connectToAndroidNfcService() override;

TypedLpcResult<bool> isNfcDiscoverySoundDisabled() override;

TypedLpcResult<bool> setNfcDiscoverySoundDisabled(bool disable) override;
};

}
Expand Down
Loading

0 comments on commit 6f654ef

Please sign in to comment.