• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "IvnAndroidDeviceService.h"
18 
19 #include <aidl/android/hardware/automotive/ivn/ConnectProtocol.h>
20 #include <aidl/android/hardware/automotive/ivn/HardwareIdentifiers.h>
21 #include <aidl/android/hardware/automotive/ivn/OccupantType.h>
22 #include <android-base/logging.h>
23 #include <android/binder_status.h>
24 #include <json/json.h>
25 
26 #include <fstream>
27 #include <string>
28 
29 namespace android {
30 namespace hardware {
31 namespace automotive {
32 namespace ivn {
33 
34 namespace {
35 
36 using ::aidl::android::hardware::automotive::ivn::ConnectProtocol;
37 using ::aidl::android::hardware::automotive::ivn::EndpointInfo;
38 using ::aidl::android::hardware::automotive::ivn::HardwareIdentifiers;
39 using ::aidl::android::hardware::automotive::ivn::OccupantType;
40 using ::aidl::android::hardware::automotive::ivn::OccupantZoneInfo;
41 using ::ndk::ScopedAStatus;
42 
43 constexpr int IVN_ERROR_GENERIC = -1;
44 
45 }  // namespace
46 
IvnAndroidDeviceService(std::string_view configPath)47 IvnAndroidDeviceService::IvnAndroidDeviceService(std::string_view configPath) {
48     mConfigPath = configPath;
49 }
50 
init()51 bool IvnAndroidDeviceService::init() {
52     std::string configPathStr(mConfigPath);
53     std::ifstream configStream(configPathStr);
54     if (!configStream) {
55         LOG(ERROR) << "couldn't open " << mConfigPath << " for parsing.";
56         return false;
57     }
58     Json::CharReaderBuilder builder;
59     std::string errs;
60     if (!Json::parseFromStream(builder, configStream, &mConfigRootNode, &errs)) {
61         LOG(ERROR) << "Failed to parse config JSON stream, error: " << errs;
62         return false;
63     }
64     if (!mConfigRootNode.isObject()) {
65         LOG(ERROR) << "Root must be an object";
66         return false;
67     }
68     if (!mConfigRootNode.isMember("MyDeviceId")) {
69         LOG(ERROR) << "Must contain 'MyDeviceId' field";
70         return false;
71     }
72     mMyDeviceId = mConfigRootNode["MyDeviceId"].asInt();
73     if (!mConfigRootNode.isMember("Devices") || !mConfigRootNode["Devices"].isArray()) {
74         LOG(ERROR) << "Must contain 'Devices' field as array";
75         return false;
76     }
77     Json::Value& devices = mConfigRootNode["Devices"];
78     for (unsigned int i = 0; i < devices.size(); i++) {
79         Json::Value& device = devices[i];
80         int deviceId = device["DeviceId"].asInt();
81         DeviceInfo deviceInfo = {};
82         Json::Value& occupantZones = device["OccupantZones"];
83         for (unsigned int j = 0; j < occupantZones.size(); j++) {
84             Json::Value& occupantZone = occupantZones[j];
85             int zoneId = occupantZone["ZoneId"].asInt();
86             std::string occupantTypeStr = occupantZone["OccupantType"].asString();
87             int seat = occupantZone["Seat"].asInt();
88             OccupantType occupantType;
89             if (occupantTypeStr == "DRIVER") {
90                 occupantType = OccupantType::DRIVER;
91             } else if (occupantTypeStr == "FRONT_PASSENGER") {
92                 occupantType = OccupantType::FRONT_PASSENGER;
93             } else if (occupantTypeStr == "REAR_PASSENGER") {
94                 occupantType = OccupantType::REAR_PASSENGER;
95             } else {
96                 LOG(ERROR) << "Unknown occupant type: " << occupantTypeStr;
97                 return false;
98             }
99             OccupantZoneInfo occupantZoneInfo = {
100                     .zoneId = zoneId, .occupantType = occupantType, .seat = seat};
101             deviceInfo.occupantZones.push_back(std::move(occupantZoneInfo));
102         }
103         Json::Value& ep = device["EndpointInfo"];
104         EndpointInfo endpointInfo = {};
105         endpointInfo.connectProtocol = ConnectProtocol::TCP_IP;
106         endpointInfo.ipAddress = ep["IpAddress"].asString();
107         endpointInfo.portNumber = ep["PortNumber"].asInt();
108         HardwareIdentifiers hardwareId = {};
109         if (ep.isMember("BrandName")) {
110             hardwareId.brandName = ep["BrandName"].asString();
111         }
112         if (ep.isMember("DeviceName")) {
113             hardwareId.deviceName = ep["DeviceName"].asString();
114         }
115         if (ep.isMember("ProductName")) {
116             hardwareId.productName = ep["ProductName"].asString();
117         }
118         if (ep.isMember("ManufacturerName")) {
119             hardwareId.manufacturerName = ep["ManufacturerName"].asString();
120         }
121         if (ep.isMember("ModelName")) {
122             hardwareId.modelName = ep["ModelName"].asString();
123         }
124         if (ep.isMember("SerialNumber")) {
125             hardwareId.serialNumber = ep["SerialNumber"].asString();
126         }
127         endpointInfo.hardwareId = hardwareId;
128         deviceInfo.endpointInfo = endpointInfo;
129         mDeviceInfoById[deviceId] = deviceInfo;
130     }
131     if (mDeviceInfoById.find(mMyDeviceId) == mDeviceInfoById.end()) {
132         LOG(ERROR) << "My device ID is not in the device info list";
133         return false;
134     }
135     return true;
136 }
137 
getMyDeviceId(int * deviceId)138 ScopedAStatus IvnAndroidDeviceService::getMyDeviceId(int* deviceId) {
139     *deviceId = mMyDeviceId;
140     return ScopedAStatus::ok();
141 }
142 
getOtherDeviceIds(std::vector<int> * deviceIds)143 ScopedAStatus IvnAndroidDeviceService::getOtherDeviceIds(std::vector<int>* deviceIds) {
144     deviceIds->clear();
145     for (const auto& [deviceId, _] : mDeviceInfoById) {
146         if (deviceId == mMyDeviceId) {
147             continue;
148         }
149         deviceIds->push_back(deviceId);
150     }
151     return ScopedAStatus::ok();
152 }
153 
getDeviceIdForOccupantZone(int zoneId,int * outDeviceId)154 ScopedAStatus IvnAndroidDeviceService::getDeviceIdForOccupantZone(int zoneId, int* outDeviceId) {
155     for (const auto& [deviceId, deviceInfo] : mDeviceInfoById) {
156         for (const auto& occupantZoneInfo : deviceInfo.occupantZones) {
157             if (occupantZoneInfo.zoneId == zoneId) {
158                 *outDeviceId = deviceId;
159                 return ScopedAStatus::ok();
160             }
161         }
162     }
163     return ScopedAStatus::fromServiceSpecificErrorWithMessage(IVN_ERROR_GENERIC,
164                                                               "Occupant zone not found");
165 }
166 
getOccupantZonesForDevice(int androidDeviceId,std::vector<OccupantZoneInfo> * occupantZones)167 ScopedAStatus IvnAndroidDeviceService::getOccupantZonesForDevice(
168         int androidDeviceId, std::vector<OccupantZoneInfo>* occupantZones) {
169     if (mDeviceInfoById.find(androidDeviceId) == mDeviceInfoById.end()) {
170         return ScopedAStatus::fromServiceSpecificErrorWithMessage(IVN_ERROR_GENERIC,
171                                                                   "Android device ID not found");
172     }
173     for (const auto& occupantZoneInfo : mDeviceInfoById[androidDeviceId].occupantZones) {
174         occupantZones->push_back(occupantZoneInfo);
175     }
176     return ScopedAStatus::ok();
177 }
178 
getMyEndpointInfo(EndpointInfo * endpointInfo)179 ScopedAStatus IvnAndroidDeviceService::getMyEndpointInfo(EndpointInfo* endpointInfo) {
180     *endpointInfo = mDeviceInfoById[mMyDeviceId].endpointInfo;
181     return ScopedAStatus::ok();
182 }
183 
getEndpointInfoForDevice(int androidDeviceId,EndpointInfo * endpointInfo)184 ScopedAStatus IvnAndroidDeviceService::getEndpointInfoForDevice(int androidDeviceId,
185                                                                 EndpointInfo* endpointInfo) {
186     if (mDeviceInfoById.find(androidDeviceId) == mDeviceInfoById.end()) {
187         return ScopedAStatus::fromServiceSpecificErrorWithMessage(IVN_ERROR_GENERIC,
188                                                                   "Android device ID not found");
189     }
190     *endpointInfo = mDeviceInfoById[androidDeviceId].endpointInfo;
191     return ScopedAStatus::ok();
192 }
193 
dump(int fd,const char ** args,uint32_t numArgs)194 binder_status_t IvnAndroidDeviceService::dump(int fd, [[maybe_unused]] const char** args,
195                                               [[maybe_unused]] uint32_t numArgs) {
196     dprintf(fd, "IVN Android Device debug interface, Config: \n%s\n",
197             mConfigRootNode.toStyledString().c_str());
198     return STATUS_OK;
199 }
200 
201 }  // namespace ivn
202 }  // namespace automotive
203 }  // namespace hardware
204 }  // namespace android
205