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 
19 #ifndef ANDROID_HARDWARE_UWB_UWBCHIP
20 #define ANDROID_HARDWARE_UWB_UWBCHIP
21 
22 #include <vector>
23 #include <android-base/logging.h>
24 
25 
26 #include <aidl/android/hardware/uwb/BnUwbChip.h>
27 #include <aidl/android/hardware/uwb/IUwbClientCallback.h>
28 
29 namespace android {
30 namespace hardware {
31 namespace uwb {
32 namespace impl {
33 using namespace ::aidl::android::hardware::uwb;
34 // Default implementation mean't to be used on simulator targets.
35 class UwbChip : public BnUwbChip {
36   public:
37     UwbChip(const std::string& name);
38     virtual ~UwbChip();
39 
40     ::ndk::ScopedAStatus getName(std::string* name) override;
41     ::ndk::ScopedAStatus open(const std::shared_ptr<IUwbClientCallback>& clientCallback) override;
42     ::ndk::ScopedAStatus close() override;
43     ::ndk::ScopedAStatus coreInit() override;
44     ::ndk::ScopedAStatus getSupportedAndroidUciVersion(int32_t* version) override;
45     ::ndk::ScopedAStatus sendUciMessage(const std::vector<uint8_t>& data,
46                                         int32_t* bytes_written) override;
47 
48     ::ndk::ScopedAStatus sessionInit(int32_t sessionId) override;
eventCallback(uint8_t event,uint8_t status)49   static void eventCallback(uint8_t event, uint8_t status) {
50   if (mClientCallback != nullptr) {
51       auto ret = mClientCallback->onHalEvent((UwbEvent)event,
52                                       (UwbStatus)status);
53       if (!ret.isOk()) {
54           LOG(ERROR) << "Failed to call back into uwb process";
55       }
56     }
57   }
dataCallback(uint16_t data_len,uint8_t * p_data)58   static void dataCallback(uint16_t data_len, uint8_t* p_data) {
59       std::vector<uint8_t> data;
60       data.assign(p_data, p_data + data_len);
61       if (mClientCallback != nullptr) {
62           auto ret = mClientCallback->onUciMessage(data);
63           if (!ret.isOk()) {
64               LOG(ERROR) << "Failed to data call back into uwb process";
65           }
66       }
67   }
68 
69 
70   private:
71     std::string name_;
72 
73   public:
74     static std::shared_ptr<IUwbClientCallback> mClientCallback;
75 };
76 }  // namespace impl
77 }  // namespace uwb
78 }  // namespace hardware
79 }  // namespace android
80 
81 #endif  // ANDROID_HARDWARE_UWB_UWBCHIP
82