1 /*
2 * Copyright 2021, The Android Open Source Project
3 *
4 * Copyright 2023 NXP
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 #include "hal_nxpuwb.h"
19 #include "phNxpUciHal_Adaptation.h"
20 #include "uwb.h"
21
22 namespace {
23 constexpr static int32_t kAndroidUciVersion = 1;
24 }
25
26 namespace android {
27 namespace hardware {
28 namespace uwb {
29 namespace impl {
30 using namespace ::aidl::android::hardware::uwb;
31
UwbChip(const std::string & name)32 UwbChip::UwbChip(const std::string& name) : name_(name){};
33 std::shared_ptr<IUwbClientCallback> UwbChip::mClientCallback = nullptr;
34 AIBinder_DeathRecipient *mClientDeathRecipient = nullptr;
~UwbChip()35 UwbChip::~UwbChip() {}
36
onServiceDied(void * cookie)37 void onServiceDied(void *cookie) {
38 if (UwbChip::mClientCallback != nullptr &&
39 !AIBinder_isAlive(UwbChip::mClientCallback->asBinder().get())) {
40 LOG(INFO)
41 << "UWB framework service died. Hence closing the UwbChip connection.";
42 UwbChip *uwbChip = static_cast<UwbChip *>(cookie);
43 uwbChip->close();
44 }
45 }
46
getName(std::string * name)47 ::ndk::ScopedAStatus UwbChip::getName(std::string* name) {
48 *name = name_;
49 return ndk::ScopedAStatus::ok();
50 }
51
open(const std::shared_ptr<IUwbClientCallback> & clientCallback)52 ::ndk::ScopedAStatus UwbChip::open(const std::shared_ptr<IUwbClientCallback>& clientCallback) {
53 mClientCallback = clientCallback;
54 LOG(INFO) << "AIDL-open Enter";
55
56 if (mClientCallback == nullptr) {
57 LOG(ERROR) << "AIDL-HAL open clientCallback is null......";
58 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
59 }
60 mClientDeathRecipient = AIBinder_DeathRecipient_new(onServiceDied);
61 auto linkRet =
62 AIBinder_linkToDeath(clientCallback->asBinder().get(),
63 mClientDeathRecipient, this /* cookie */);
64 if (linkRet == STATUS_OK) {
65 LOG(INFO) << "AIDL-linkToDeath succeed: " << linkRet;
66 } else {
67 LOG(ERROR) << "AIDL-linkToDeath failed: " << linkRet;
68 }
69 int status = phNxpUciHal_open(eventCallback, dataCallback);
70 LOG(INFO) << "AIDL-open Exit" << status;
71 return ndk::ScopedAStatus::ok();
72 }
73
close()74 ::ndk::ScopedAStatus UwbChip::close() {
75 LOG(INFO) << "AIDL-Close Enter";
76 if (mClientCallback == nullptr) {
77 LOG(ERROR) << "AIDL-HAL close mCallback is null......";
78 return ndk::ScopedAStatus(
79 AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
80 }
81 phNxpUciHal_close();
82 if (mClientCallback != nullptr) {
83 mClientCallback = nullptr;
84 }
85 AIBinder_DeathRecipient_delete(mClientDeathRecipient);
86 mClientDeathRecipient = nullptr;
87 return ndk::ScopedAStatus::ok();
88 }
89
coreInit()90 ::ndk::ScopedAStatus UwbChip::coreInit() {
91 LOG(INFO) << "AIDL-coreInit Enter";
92 phNxpUciHal_coreInitialization();
93 return ndk::ScopedAStatus::ok();
94 }
95
getSupportedAndroidUciVersion(int32_t * version)96 ::ndk::ScopedAStatus UwbChip::getSupportedAndroidUciVersion(int32_t* version) {
97 *version = kAndroidUciVersion;
98 return ndk::ScopedAStatus::ok();
99 }
100
sendUciMessage(const std::vector<uint8_t> & data,int32_t * _aidl_return)101 ::ndk::ScopedAStatus UwbChip::sendUciMessage(const std::vector<uint8_t>& data,
102 int32_t* _aidl_return /* bytes_written */) {
103 // TODO(b/195992658): Need emulator support for UCI stack.
104 std::vector<uint8_t> copy = data;
105 LOG(INFO) << "AIDL-Write Enter";
106 int32_t ret = phNxpUciHal_write(data.size(), ©[0]);
107 *_aidl_return = ret;
108 return ndk::ScopedAStatus::ok();
109 }
110
sessionInit(int32_t sessionId)111 ::ndk::ScopedAStatus UwbChip::sessionInit(int32_t sessionId) {
112 LOG(INFO) << "AIDL-SessionInitialization Enter";
113 phNxpUciHal_sessionInitialization(sessionId);
114 return ndk::ScopedAStatus::ok();
115 }
116 } // namespace impl
117 } // namespace uwb
118 } // namespace hardware
119 } // namespace android
120