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.android.server.wifi;
18 
19 import android.annotation.NonNull;
20 
21 import com.android.server.wifi.hal.WifiChip;
22 
23 import org.json.JSONArray;
24 import org.json.JSONException;
25 import org.json.JSONObject;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * Utility methods for HalDeviceManager.
32  */
33 public class HalDeviceManagerUtil {
34     static class StaticChipInfo {
35         private int mChipId;
36         private long mChipCapabilities;
37         private @NonNull ArrayList<WifiChip.ChipMode> mAvailableModes = new ArrayList<>();
38 
StaticChipInfo( int chipId, long chipCapabilities, @NonNull ArrayList<WifiChip.ChipMode> availableModes)39         StaticChipInfo(
40                 int chipId,
41                 long chipCapabilities,
42                 @NonNull ArrayList<WifiChip.ChipMode> availableModes) {
43             mChipId = chipId;
44             mChipCapabilities = chipCapabilities;
45             if (availableModes != null) {
46                 mAvailableModes = availableModes;
47             }
48         }
49 
getChipId()50         int getChipId() {
51             return mChipId;
52         }
53 
getChipCapabilities()54         long getChipCapabilities() {
55             return mChipCapabilities;
56         }
57 
getAvailableModes()58         ArrayList<WifiChip.ChipMode> getAvailableModes() {
59             return mAvailableModes;
60         }
61     }
62 
63     private static final String KEY_CHIP_ID = "chipId";
64     private static final String KEY_CHIP_CAPABILITIES = "chipCapabilities";
65     private static final String KEY_AVAILABLE_MODES = "availableModes";
66 
staticChipInfoToJson(@onNull StaticChipInfo staticChipInfo)67     static JSONObject staticChipInfoToJson(@NonNull StaticChipInfo staticChipInfo)
68             throws JSONException {
69         JSONObject jsonObject = new JSONObject();
70         jsonObject.put(KEY_CHIP_ID, staticChipInfo.getChipId());
71         jsonObject.put(KEY_CHIP_CAPABILITIES, staticChipInfo.getChipCapabilities());
72         JSONArray availableModesJson = new JSONArray();
73         for (WifiChip.ChipMode mode : staticChipInfo.getAvailableModes()) {
74             availableModesJson.put(chipModeToJson(mode));
75         }
76         jsonObject.put(KEY_AVAILABLE_MODES, availableModesJson);
77         return jsonObject;
78     }
79 
jsonToStaticChipInfo(JSONObject jsonObject)80     static StaticChipInfo jsonToStaticChipInfo(JSONObject jsonObject) throws JSONException {
81         ArrayList<WifiChip.ChipMode> availableModes = new ArrayList<>();
82         int chipId = jsonObject.getInt(KEY_CHIP_ID);
83         long chipCapabilities = jsonObject.getLong(KEY_CHIP_CAPABILITIES);
84         JSONArray modesJson = jsonObject.getJSONArray(KEY_AVAILABLE_MODES);
85         for (int i = 0; i < modesJson.length(); i++) {
86             availableModes.add(jsonToChipMode(modesJson.getJSONObject(i)));
87         }
88         return new StaticChipInfo(chipId, chipCapabilities, availableModes);
89     }
90 
91     private static final String KEY_ID = "id";
92     private static final String KEY_AVAILABLE_COMBINATIONS = "availableCombinations";
93 
chipModeToJson(WifiChip.ChipMode chipMode)94     private static JSONObject chipModeToJson(WifiChip.ChipMode chipMode)
95             throws JSONException {
96         JSONObject jsonObject = new JSONObject();
97         jsonObject.put(KEY_ID, chipMode.id);
98         JSONArray availableCombinationsJson = new JSONArray();
99         for (WifiChip.ChipConcurrencyCombination combo : chipMode.availableCombinations) {
100             availableCombinationsJson.put(chipConcurrencyCombinationToJson(combo));
101         }
102         jsonObject.put(KEY_AVAILABLE_COMBINATIONS, availableCombinationsJson);
103         return jsonObject;
104     }
105 
jsonToChipMode(JSONObject jsonObject)106     private static WifiChip.ChipMode jsonToChipMode(JSONObject jsonObject) throws JSONException {
107         List<WifiChip.ChipConcurrencyCombination> availableCombinations = new ArrayList<>();
108         JSONArray availableCombinationsJson =
109                 jsonObject.getJSONArray(KEY_AVAILABLE_COMBINATIONS);
110         for (int i = 0; i < availableCombinationsJson.length(); i++) {
111             availableCombinations.add(jsonToChipConcurrencyCombination(
112                     availableCombinationsJson.getJSONObject(i)));
113         }
114         return new WifiChip.ChipMode(jsonObject.getInt(KEY_ID), availableCombinations);
115     }
116 
117     private static final String KEY_CONCURRENCY_LIMITS = "limits";
118 
chipConcurrencyCombinationToJson( WifiChip.ChipConcurrencyCombination combo)119     private static JSONObject chipConcurrencyCombinationToJson(
120             WifiChip.ChipConcurrencyCombination combo) throws JSONException {
121         JSONObject jsonObject = new JSONObject();
122         JSONArray limitsJson = new JSONArray();
123         for (WifiChip.ChipConcurrencyCombinationLimit limit : combo.limits) {
124             limitsJson.put(chipConcurrencyCombinationLimitToJson(limit));
125         }
126         jsonObject.put(KEY_CONCURRENCY_LIMITS, limitsJson);
127         return jsonObject;
128     }
129 
jsonToChipConcurrencyCombination( JSONObject jsonObject)130     private static WifiChip.ChipConcurrencyCombination jsonToChipConcurrencyCombination(
131             JSONObject jsonObject) throws JSONException {
132         List<WifiChip.ChipConcurrencyCombinationLimit> limits = new ArrayList<>();
133         JSONArray limitsJson = jsonObject.getJSONArray(KEY_CONCURRENCY_LIMITS);
134         for (int i = 0; i < limitsJson.length(); i++) {
135             limits.add(
136                     jsonToChipConcurrencyCombinationLimit(limitsJson.getJSONObject(i)));
137         }
138         return new WifiChip.ChipConcurrencyCombination(limits);
139     }
140 
141     private static final String KEY_MAX_IFACES = "maxIfaces";
142     private static final String KEY_TYPES = "types";
143 
chipConcurrencyCombinationLimitToJson( WifiChip.ChipConcurrencyCombinationLimit limit)144     private static JSONObject chipConcurrencyCombinationLimitToJson(
145             WifiChip.ChipConcurrencyCombinationLimit limit) throws JSONException {
146         JSONObject jsonObject = new JSONObject();
147         jsonObject.put(KEY_MAX_IFACES, limit.maxIfaces);
148         jsonObject.put(KEY_TYPES, new JSONArray(limit.types));
149         return jsonObject;
150     }
151 
jsonToChipConcurrencyCombinationLimit( JSONObject jsonObject)152     private static WifiChip.ChipConcurrencyCombinationLimit jsonToChipConcurrencyCombinationLimit(
153             JSONObject jsonObject) throws JSONException {
154         List<Integer> types = new ArrayList<>();
155         JSONArray limitsJson = jsonObject.getJSONArray(KEY_TYPES);
156         for (int i = 0; i < limitsJson.length(); i++) {
157             types.add(limitsJson.getInt(i));
158         }
159         return new WifiChip.ChipConcurrencyCombinationLimit(
160                 jsonObject.getInt(KEY_MAX_IFACES), types);
161     }
162 }
163