1 /*
2  * Copyright (C) 2021 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 "structs.h"
18 
19 #include "commonStructs.h"
20 
21 #include "collections.h"
22 
23 #include <android-base/logging.h>
24 
25 namespace android::hardware::radio::compat {
26 
27 namespace aidl = ::aidl::android::hardware::radio::data;
28 
toHidl(const aidl::DataProfileInfo & info)29 V1_5::DataProfileInfo toHidl(const aidl::DataProfileInfo& info) {
30     return {
31             .profileId = static_cast<V1_0::DataProfileId>(info.profileId),
32             .apn = info.apn,
33             .protocol = static_cast<V1_4::PdpProtocolType>(info.protocol),
34             .roamingProtocol = static_cast<V1_4::PdpProtocolType>(info.roamingProtocol),
35             .authType = static_cast<V1_0::ApnAuthType>(info.authType),
36             .user = info.user,
37             .password = info.password,
38             .type = static_cast<V1_0::DataProfileInfoType>(info.type),
39             .maxConnsTime = info.maxConnsTime,
40             .maxConns = info.maxConns,
41             .waitTime = info.waitTime,
42             .enabled = info.enabled,
43             .supportedApnTypesBitmap = toHidlBitfield<V1_5::ApnTypes>(info.supportedApnTypesBitmap),
44             .bearerBitmap = toHidlBitfield<V1_4::RadioAccessFamily>(info.bearerBitmap),
45             .mtuV4 = info.mtuV4,
46             .mtuV6 = info.mtuV6,
47             .preferred = info.preferred,
48             .persistent = info.persistent,
49     };
50 }
51 
toHidl(const aidl::LinkAddress & addr)52 V1_5::LinkAddress toHidl(const aidl::LinkAddress& addr) {
53     return {
54             .address = addr.address,
55             .properties = addr.addressProperties,
56             .deprecationTime = static_cast<uint64_t>(addr.deprecationTime),
57             .expirationTime = static_cast<uint64_t>(addr.expirationTime),
58     };
59 }
60 
toAidl(const V1_6::SliceInfo & info)61 aidl::SliceInfo toAidl(const V1_6::SliceInfo& info) {
62     return {
63             .sliceServiceType = static_cast<int8_t>(info.sst),
64             .sliceDifferentiator = info.sliceDifferentiator,
65             .mappedHplmnSst = static_cast<int8_t>(info.mappedHplmnSst),
66             .mappedHplmnSd = info.mappedHplmnSD,
67             .status = static_cast<int8_t>(info.status),
68     };
69 }
70 
toHidl(const aidl::SliceInfo & info)71 V1_6::SliceInfo toHidl(const aidl::SliceInfo& info) {
72     return {
73             .sst = static_cast<V1_6::SliceServiceType>(info.sliceServiceType),
74             .sliceDifferentiator = info.sliceDifferentiator,
75             .mappedHplmnSst = static_cast<V1_6::SliceServiceType>(info.mappedHplmnSst),
76             .mappedHplmnSD = info.mappedHplmnSd,
77             .status = static_cast<V1_6::SliceStatus>(info.status),
78     };
79 }
80 
toAidl(const V1_6::TrafficDescriptor & descr)81 aidl::TrafficDescriptor toAidl(const V1_6::TrafficDescriptor& descr) {
82     return {
83             .dnn = toAidl(descr.dnn),
84             .osAppId = toAidl(descr.osAppId),
85     };
86 }
87 
toHidl(const aidl::TrafficDescriptor & descr)88 V1_6::TrafficDescriptor toHidl(const aidl::TrafficDescriptor& descr) {
89     return {
90             .dnn = toHidl<V1_6::OptionalDnn>(descr.dnn),
91             .osAppId = toHidl<V1_6::OptionalOsAppId>(descr.osAppId),
92     };
93 }
94 
toAidl(const V1_6::OsAppId & appId)95 aidl::OsAppId toAidl(const V1_6::OsAppId& appId) {
96     return {
97             .osAppId = appId.osAppId,
98     };
99 }
100 
toHidl(const aidl::OsAppId & appId)101 V1_6::OsAppId toHidl(const aidl::OsAppId& appId) {
102     return {
103             .osAppId = appId.osAppId,
104     };
105 }
106 
toHidl(const aidl::KeepaliveRequest & keep)107 V1_1::KeepaliveRequest toHidl(const aidl::KeepaliveRequest& keep) {
108     return {
109             .type = static_cast<V1_1::KeepaliveType>(keep.type),
110             .sourceAddress = keep.sourceAddress,
111             .sourcePort = keep.sourcePort,
112             .destinationAddress = keep.destinationAddress,
113             .destinationPort = keep.destinationPort,
114             .maxKeepaliveIntervalMillis = keep.maxKeepaliveIntervalMillis,
115             .cid = keep.cid,
116     };
117 }
118 
toAidl(const V1_6::QosBandwidth & bw)119 static aidl::QosBandwidth toAidl(const V1_6::QosBandwidth& bw) {
120     return {
121             .maxBitrateKbps = static_cast<int32_t>(bw.maxBitrateKbps),
122             .guaranteedBitrateKbps = static_cast<int32_t>(bw.guaranteedBitrateKbps),
123     };
124 }
125 
toAidl(const V1_6::EpsQos & qos)126 static aidl::EpsQos toAidl(const V1_6::EpsQos& qos) {
127     return {
128             .qci = qos.qci,
129             .downlink = toAidl(qos.downlink),
130             .uplink = toAidl(qos.uplink),
131     };
132 }
133 
toAidl(const V1_6::NrQos & qos)134 static aidl::NrQos toAidl(const V1_6::NrQos& qos) {
135     return {
136             .fiveQi = qos.fiveQi,
137             .downlink = toAidl(qos.downlink),
138             .uplink = toAidl(qos.uplink),
139             .qfi = static_cast<int8_t>(qos.qfi),
140             .averagingWindowMillis = qos.averagingWindowMs,
141     };
142 }
143 
toAidl(const V1_6::Qos & qos)144 static std::variant<bool, aidl::EpsQos, aidl::NrQos> toAidl(const V1_6::Qos& qos) {
145     if (qos.getDiscriminator() == V1_6::Qos::hidl_discriminator::eps) return toAidl(qos.eps());
146     if (qos.getDiscriminator() == V1_6::Qos::hidl_discriminator::nr) return toAidl(qos.nr());
147     return false;
148 }
149 
toAidl(const V1_5::SetupDataCallResult & res)150 aidl::SetupDataCallResult toAidl(const V1_5::SetupDataCallResult& res) {
151     return {
152             .cause = aidl::DataCallFailCause(res.cause),
153             .suggestedRetryTime = res.suggestedRetryTime,
154             .cid = res.cid,
155             .active = static_cast<int32_t>(res.active),
156             .type = aidl::PdpProtocolType(res.type),
157             .ifname = res.ifname,
158             .addresses = toAidl(res.addresses),
159             .dnses = toAidl(res.dnses),
160             .gateways = toAidl(res.gateways),
161             .pcscf = toAidl(res.pcscf),
162             .mtuV4 = res.mtuV4,
163             .mtuV6 = res.mtuV6,
164     };
165 }
166 
toAidl(const V1_6::SetupDataCallResult & res)167 aidl::SetupDataCallResult toAidl(const V1_6::SetupDataCallResult& res) {
168     return {
169             .cause = aidl::DataCallFailCause(res.cause),
170             .suggestedRetryTime = res.suggestedRetryTime,
171             .cid = res.cid,
172             .active = static_cast<int32_t>(res.active),
173             .type = aidl::PdpProtocolType(res.type),
174             .ifname = res.ifname,
175             .addresses = toAidl(res.addresses),
176             .dnses = toAidl(res.dnses),
177             .gateways = toAidl(res.gateways),
178             .pcscf = toAidl(res.pcscf),
179             .mtuV4 = res.mtuV4,
180             .mtuV6 = res.mtuV6,
181             .defaultQos = toAidl(res.defaultQos),
182             .qosSessions = toAidl(res.qosSessions),
183             .handoverFailureMode = static_cast<int8_t>(res.handoverFailureMode),
184             .pduSessionId = res.pduSessionId,
185             .sliceInfo = toAidl(res.sliceInfo),
186             .trafficDescriptors = toAidl(res.trafficDescriptors),
187     };
188 }
189 
toAidl(const V1_5::LinkAddress & addr)190 aidl::LinkAddress toAidl(const V1_5::LinkAddress& addr) {
191     return {
192             .address = addr.address,
193             .addressProperties = addr.properties,
194             .deprecationTime = static_cast<int64_t>(addr.deprecationTime),
195             .expirationTime = static_cast<int64_t>(addr.expirationTime),
196     };
197 }
198 
toAidl(const V1_6::QosSession & sess)199 aidl::QosSession toAidl(const V1_6::QosSession& sess) {
200     return {
201             .qosSessionId = sess.qosSessionId,
202             .qos = toAidl(sess.qos),
203             .qosFilters = toAidl(sess.qosFilters),
204     };
205 }
206 
toAidl(const V1_6::PortRange & range)207 static aidl::PortRange toAidl(const V1_6::PortRange& range) {
208     return {
209             .start = range.start,
210             .end = range.end,
211     };
212 }
213 
toAidl(const V1_6::MaybePort & opt)214 static std::optional<aidl::PortRange> toAidl(const V1_6::MaybePort& opt) {
215     if (opt.getDiscriminator() == V1_6::MaybePort::hidl_discriminator::noinit) return std::nullopt;
216     return toAidl(opt.range());  // can't use MaybeX template - this field is not named "value"
217 }
218 
toAidl(const V1_6::QosFilter & filter)219 aidl::QosFilter toAidl(const V1_6::QosFilter& filter) {
220     return {
221             .localAddresses = toAidl(filter.localAddresses),
222             .remoteAddresses = toAidl(filter.remoteAddresses),
223             .localPort = toAidl(filter.localPort),
224             .remotePort = toAidl(filter.remotePort),
225             .protocol = static_cast<int8_t>(filter.protocol),
226             .tos = toAidlVariant(filter.tos),
227             .flowLabel = toAidlVariant(filter.flowLabel),
228             .spi = toAidlVariant(filter.spi),
229             .direction = static_cast<int8_t>(filter.direction),
230             .precedence = filter.precedence,
231     };
232 }
233 
toAidl(const V1_1::KeepaliveStatus & status)234 aidl::KeepaliveStatus toAidl(const V1_1::KeepaliveStatus& status) {
235     return {
236             .sessionHandle = status.sessionHandle,
237             .code = static_cast<int32_t>(status.code),
238     };
239 }
240 
toAidl(const V1_0::PcoDataInfo & info)241 aidl::PcoDataInfo toAidl(const V1_0::PcoDataInfo& info) {
242     return {
243             .cid = info.cid,
244             .bearerProto = info.bearerProto,
245             .pcoId = info.pcoId,
246             .contents = info.contents,
247     };
248 }
249 
toAidl(const V1_6::SlicingConfig & cfg)250 aidl::SlicingConfig toAidl(const V1_6::SlicingConfig& cfg) {
251     return {
252             .urspRules = toAidl(cfg.urspRules),
253             .sliceInfo = toAidl(cfg.sliceInfo),
254     };
255 }
256 
toAidl(const V1_6::UrspRule & rule)257 aidl::UrspRule toAidl(const V1_6::UrspRule& rule) {
258     return {
259             .precedence = rule.precedence,
260             .trafficDescriptors = toAidl(rule.trafficDescriptors),
261             .routeSelectionDescriptor = toAidl(rule.routeSelectionDescriptor),
262     };
263 }
264 
toAidl(const V1_6::OptionalSscMode & opt)265 static int8_t toAidl(const V1_6::OptionalSscMode& opt) {
266     if (opt.getDiscriminator() == V1_6::OptionalSscMode::hidl_discriminator::noinit) {
267         return aidl::RouteSelectionDescriptor::SSC_MODE_UNKNOWN;
268     }
269     return static_cast<int8_t>(opt.value());
270 }
271 
toAidl(const V1_6::OptionalPdpProtocolType & opt)272 static aidl::PdpProtocolType toAidl(const V1_6::OptionalPdpProtocolType& opt) {
273     using discriminator = V1_6::OptionalPdpProtocolType::hidl_discriminator;
274     if (opt.getDiscriminator() == discriminator::noinit) return aidl::PdpProtocolType::UNKNOWN;
275     return aidl::PdpProtocolType(opt.value());
276 }
277 
toAidl(const V1_6::RouteSelectionDescriptor & descr)278 aidl::RouteSelectionDescriptor toAidl(const V1_6::RouteSelectionDescriptor& descr) {
279     return {
280             .precedence = static_cast<int8_t>(descr.precedence),
281             .sessionType = toAidl(descr.sessionType),
282             .sscMode = toAidl(descr.sscMode),
283             .sliceInfo = toAidl(descr.sliceInfo),
284             .dnn = toAidl(descr.dnn),
285     };
286 }
287 
288 }  // namespace android::hardware::radio::compat
289