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 package com.google.uwb.support.generic;
18 
19 import android.os.PersistableBundle;
20 import android.uwb.UwbManager;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 
25 import com.google.uwb.support.aliro.AliroParams;
26 import com.google.uwb.support.aliro.AliroSpecificationParams;
27 import com.google.uwb.support.ccc.CccParams;
28 import com.google.uwb.support.ccc.CccSpecificationParams;
29 import com.google.uwb.support.fira.FiraParams;
30 import com.google.uwb.support.fira.FiraSpecificationParams;
31 import com.google.uwb.support.radar.RadarParams;
32 import com.google.uwb.support.radar.RadarSpecificationParams;
33 
34 import java.util.Objects;
35 
36 /**
37  * Defines parameters for generic capability.
38  *
39  * <p>This is returned as a bundle from the service API {@link UwbManager#getSpecificationInfo}.
40  */
41 public class GenericSpecificationParams extends GenericParams {
42     private static final int BUNDLE_VERSION_1 = 1;
43     private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1;
44 
45     private FiraSpecificationParams mFiraSpecificationParams;
46     private final CccSpecificationParams mCccSpecificationParams;
47     private final AliroSpecificationParams mAliroSpecificationParams;
48     private final RadarSpecificationParams mRadarSpecificationParams;
49     private final boolean mHasPowerStatsSupport;
50 
51     private static final String KEY_FIRA_SPECIFICATION_PARAMS = FiraParams.PROTOCOL_NAME;
52     private static final String KEY_ALIRO_SPECIFICATION_PARAMS = AliroParams.PROTOCOL_NAME;
53     private static final String KEY_CCC_SPECIFICATION_PARAMS = CccParams.PROTOCOL_NAME;
54     private static final String KEY_RADAR_SPECIFICATION_PARAMS = RadarParams.PROTOCOL_NAME;
55     private static final String KEY_POWER_STATS_QUERY_SUPPORT = "power_stats_query";
56 
GenericSpecificationParams( FiraSpecificationParams firaSpecificationParams, CccSpecificationParams cccSpecificationParams, AliroSpecificationParams aliroSpecificationParams, RadarSpecificationParams radarSpecificationParams, boolean hasPowerStatsSupport)57     private GenericSpecificationParams(
58             FiraSpecificationParams firaSpecificationParams,
59             CccSpecificationParams cccSpecificationParams,
60             AliroSpecificationParams aliroSpecificationParams,
61             RadarSpecificationParams radarSpecificationParams,
62             boolean hasPowerStatsSupport) {
63         mFiraSpecificationParams = firaSpecificationParams;
64         mCccSpecificationParams = cccSpecificationParams;
65         mAliroSpecificationParams = aliroSpecificationParams;
66         mRadarSpecificationParams = radarSpecificationParams;
67         mHasPowerStatsSupport = hasPowerStatsSupport;
68     }
69 
70     @Override
getBundleVersion()71     protected int getBundleVersion() {
72         return BUNDLE_VERSION_CURRENT;
73     }
74 
75     @Nullable
getFiraSpecificationParams()76     public FiraSpecificationParams getFiraSpecificationParams() {
77         return mFiraSpecificationParams;
78     }
79 
80     @Nullable
getCccSpecificationParams()81     public CccSpecificationParams getCccSpecificationParams() {
82         return mCccSpecificationParams;
83     }
84 
85     @Nullable
getAliroSpecificationParams()86     public AliroSpecificationParams getAliroSpecificationParams() {
87         return mAliroSpecificationParams;
88     }
89 
90     @Nullable
getRadarSpecificationParams()91     public RadarSpecificationParams getRadarSpecificationParams() {
92         return mRadarSpecificationParams;
93     }
94 
95     /**
96      * @return if the power stats is supported
97      */
hasPowerStatsSupport()98     public boolean hasPowerStatsSupport() {
99         return mHasPowerStatsSupport;
100     }
101 
setFiraSpecificationParams(FiraSpecificationParams params)102     public void setFiraSpecificationParams(FiraSpecificationParams params) {
103         mFiraSpecificationParams = params;
104     }
105 
106     @Override
toBundle()107     public PersistableBundle toBundle() {
108         PersistableBundle bundle = super.toBundle();
109         bundle.putPersistableBundle(KEY_FIRA_SPECIFICATION_PARAMS,
110                 mFiraSpecificationParams.toBundle());
111         if (mAliroSpecificationParams != null) {
112             bundle.putPersistableBundle(KEY_ALIRO_SPECIFICATION_PARAMS,
113                     mAliroSpecificationParams.toBundle());
114         }
115         if (mCccSpecificationParams != null) {
116             bundle.putPersistableBundle(KEY_CCC_SPECIFICATION_PARAMS,
117                     mCccSpecificationParams.toBundle());
118         }
119         if (mRadarSpecificationParams != null) {
120             bundle.putPersistableBundle(KEY_RADAR_SPECIFICATION_PARAMS,
121                     mRadarSpecificationParams.toBundle());
122         }
123         bundle.putBoolean(KEY_POWER_STATS_QUERY_SUPPORT, mHasPowerStatsSupport);
124         return bundle;
125     }
126 
fromBundle(PersistableBundle bundle)127     public static GenericSpecificationParams fromBundle(PersistableBundle bundle) {
128         switch (getBundleVersion(bundle)) {
129             case BUNDLE_VERSION_1:
130                 return parseVersion1(bundle);
131 
132             default:
133                 throw new IllegalArgumentException("Invalid bundle version");
134         }
135     }
136 
parseVersion1(PersistableBundle bundle)137     private static GenericSpecificationParams parseVersion1(PersistableBundle bundle) {
138         GenericSpecificationParams.Builder builder = new GenericSpecificationParams.Builder();
139         builder = builder.setFiraSpecificationParams(
140                 FiraSpecificationParams.fromBundle(
141                         bundle.getPersistableBundle(KEY_FIRA_SPECIFICATION_PARAMS)))
142                 .hasPowerStatsSupport(bundle.getBoolean(KEY_POWER_STATS_QUERY_SUPPORT));
143         PersistableBundle cccBundle = bundle.getPersistableBundle(KEY_CCC_SPECIFICATION_PARAMS);
144         if (cccBundle != null) {
145             builder = builder.setCccSpecificationParams(
146                     CccSpecificationParams.fromBundle(cccBundle));
147         }
148 
149         PersistableBundle aliroBundle = bundle.getPersistableBundle(KEY_ALIRO_SPECIFICATION_PARAMS);
150         if (aliroBundle != null) {
151             builder = builder.setAliroSpecificationParams(
152                     AliroSpecificationParams.fromBundle(aliroBundle));
153         }
154 
155         PersistableBundle radarBundle = bundle.getPersistableBundle(
156                 KEY_RADAR_SPECIFICATION_PARAMS);
157         if (radarBundle != null) {
158             builder = builder.setRadarSpecificationParams(
159                     RadarSpecificationParams.fromBundle(radarBundle));
160         }
161         return builder.build();
162     }
163 
164     /** Builder */
165     public static class Builder {
166         private FiraSpecificationParams mFiraSpecificationParams = null;
167         private CccSpecificationParams mCccSpecificationParams = null;
168         private AliroSpecificationParams mAliroSpecificationParams = null;
169         private RadarSpecificationParams mRadarSpecificationParams = null;
170         private boolean mHasPowerStatsSupport = false;
171 
172         /**
173          * Set FIRA specification params
174          */
setFiraSpecificationParams( @onNull FiraSpecificationParams firaSpecificationParams)175         public Builder setFiraSpecificationParams(
176                 @NonNull FiraSpecificationParams firaSpecificationParams) {
177             mFiraSpecificationParams = Objects.requireNonNull(firaSpecificationParams);
178             return this;
179         }
180 
181         /**
182          * Set CCC specification params
183          */
setCccSpecificationParams( @onNull CccSpecificationParams cccSpecificationParams)184         public Builder setCccSpecificationParams(
185                 @NonNull CccSpecificationParams cccSpecificationParams) {
186             mCccSpecificationParams = Objects.requireNonNull(cccSpecificationParams);
187             return this;
188         }
189 
190         /**
191          * Set ALIRO specification params.
192          */
setAliroSpecificationParams( @onNull AliroSpecificationParams aliroSpecificationParams)193         public Builder setAliroSpecificationParams(
194                 @NonNull AliroSpecificationParams aliroSpecificationParams) {
195             mAliroSpecificationParams = Objects.requireNonNull(aliroSpecificationParams);
196             return this;
197         }
198 
199         /**
200          * Set RADAR specification params
201          */
setRadarSpecificationParams( @onNull RadarSpecificationParams radarSpecificationParams)202         public Builder setRadarSpecificationParams(
203                 @NonNull RadarSpecificationParams radarSpecificationParams) {
204             mRadarSpecificationParams = Objects.requireNonNull(radarSpecificationParams);
205             return this;
206         }
207 
208         /**
209          * Sets if the power stats is supported
210          */
hasPowerStatsSupport(boolean value)211         public Builder hasPowerStatsSupport(boolean value) {
212             mHasPowerStatsSupport = value;
213             return this;
214         }
215 
216         /**
217          * Build {@link GenericSpecificationParams}
218          */
build()219         public GenericSpecificationParams build() {
220             return new GenericSpecificationParams(
221                     mFiraSpecificationParams,
222                     mCccSpecificationParams,
223                     mAliroSpecificationParams,
224                     mRadarSpecificationParams,
225                     mHasPowerStatsSupport);
226         }
227     }
228 }
229