1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <aidl/android/hardware/threadnetwork/BnThreadChip.h>
20 #include <aidl/android/hardware/threadnetwork/IThreadChipCallback.h>
21 
22 #include "lib/spinel/spinel_interface.hpp"
23 #include "mainloop.hpp"
24 #include "radio_url.hpp"
25 
26 #include <android/binder_auto_utils.h>
27 #include <android/binder_ibinder.h>
28 #include <utils/Mutex.h>
29 
30 namespace aidl {
31 namespace android {
32 namespace hardware {
33 namespace threadnetwork {
34 
35 class ThreadChip : public BnThreadChip, ot::Posix::Mainloop::Source {
36   public:
37     ThreadChip(const char* url);
~ThreadChip()38     ~ThreadChip() {}
39 
40     ndk::ScopedAStatus open(const std::shared_ptr<IThreadChipCallback>& in_callback) override;
41     ndk::ScopedAStatus close() override;
42     ndk::ScopedAStatus sendSpinelFrame(const std::vector<uint8_t>& in_frame) override;
43     ndk::ScopedAStatus hardwareReset() override;
44     void Update(otSysMainloopContext& context) override;
45     void Process(const otSysMainloopContext& context) override;
46 
47   private:
48     static void onBinderDiedJump(void* context);
49     void onBinderDied(void);
50     static void onBinderUnlinkedJump(void* context);
51     void onBinderUnlinked(void);
52     static void handleReceivedFrameJump(void* context);
53     void handleReceivedFrame(void);
54     ndk::ScopedAStatus errorStatus(int32_t error, const char* message);
55     ndk::ScopedAStatus initChip(const std::shared_ptr<IThreadChipCallback>& in_callback);
56     ndk::ScopedAStatus deinitChip();
57 
58     ot::Posix::RadioUrl mUrl;
59     std::shared_ptr<ot::Spinel::SpinelInterface> mSpinelInterface;
60     ot::Spinel::SpinelInterface::RxFrameBuffer mRxFrameBuffer;
61     std::shared_ptr<IThreadChipCallback> mCallback;
62     ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
63 };
64 
65 }  // namespace threadnetwork
66 }  // namespace hardware
67 }  // namespace android
68 }  // namespace aidl
69