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 #include <aidl/android/hardware/threadnetwork/IThreadChip.h>
18 #include <android-base/logging.h>
19 #include <android/binder_manager.h>
20 #include <android/binder_process.h>
21 #include <netinet/in.h>
22 #include <net/if.h>
23 #include <utils/Log.h>
24 #include <cutils/properties.h>
25 #include <sys/stat.h>
26
27 #include "service.hpp"
28 #include "thread_chip.hpp"
29
30 using aidl::android::hardware::threadnetwork::IThreadChip;
31 using aidl::android::hardware::threadnetwork::ThreadChip;
32
33 #define THREADNETWORK_COPROCESSOR_SIMULATION_PATH "/apex/com.android.hardware.threadnetwork/bin/ot-rcp"
34
35 namespace {
addThreadChip(int id,const char * url)36 void addThreadChip(int id, const char* url) {
37 binder_status_t status;
38 const std::string serviceName(std::string() + IThreadChip::descriptor + "/chip" +
39 std::to_string(id));
40
41 ALOGI("ServiceName: %s, Url: %s", serviceName.c_str(), url);
42
43 auto threadChip = ndk::SharedRefBase::make<ThreadChip>(url);
44
45 CHECK_NE(threadChip, nullptr);
46
47 status = AServiceManager_addService(threadChip->asBinder().get(), serviceName.c_str());
48 CHECK_EQ(status, STATUS_OK);
49 }
50
addSimulatedThreadChip()51 void addSimulatedThreadChip() {
52 char local_interface[PROP_VALUE_MAX];
53
54 CHECK_GT(property_get("persist.vendor.otsim.local_interface",
55 local_interface, "eth1"), 0);
56
57 int node_id = property_get_int32("ro.boot.openthread_node_id", 0);
58 CHECK_GT(node_id,0);
59
60 std::string url = std::string("spinel+hdlc+forkpty://" \
61 THREADNETWORK_COPROCESSOR_SIMULATION_PATH "?forkpty-arg=-L") \
62 + local_interface + "&forkpty-arg=" + std::to_string(node_id);
63 addThreadChip(0, url.c_str());
64 }
65 }
66
main(int argc,char * argv[])67 int main(int argc, char* argv[]) {
68 aidl::android::hardware::threadnetwork::Service service;
69
70 if (argc > 1) {
71 for (int id = 0; id < argc - 1; id++) {
72 addThreadChip(id, argv[id + 1]);
73 }
74 } else {
75 struct stat sb;
76
77 CHECK_EQ(stat(THREADNETWORK_COPROCESSOR_SIMULATION_PATH, &sb), 0);
78 CHECK(sb.st_mode & S_IXUSR);
79 addSimulatedThreadChip();
80 }
81
82 ALOGI("Thread Network HAL is running");
83
84 service.startLoop();
85 return EXIT_FAILURE; // should not reach
86 }
87