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.internal.telephony.uicc;
18 
19 import android.annotation.NonNull;
20 
21 import com.android.internal.telephony.uicc.IccSlotStatus.MultipleEnabledProfilesMode;
22 
23 /**
24  * Various methods, useful for dealing with port.
25  */
26 public class PortUtils {
27 
28     /**
29      * Converts the port index to compatible with the HAL.
30      *
31      * @param mepMode   supported MultipleEnabledProfilesMode
32      * @param portIndex port index
33      * @return target index according to the MultipleEnabledProfilesMode
34      */
convertToHalPortIndex(@onNull MultipleEnabledProfilesMode mepMode, int portIndex)35     public static int convertToHalPortIndex(@NonNull MultipleEnabledProfilesMode mepMode,
36             int portIndex) {
37         // In case of MEP-A1 and MEP-A2, profiles are selected on eSIM Ports 1 and higher, hence
38         // HAL expects the ports are indexed with 1, 2... etc.
39         // So inorder to compatible with HAL, shift the port index.
40         return mepMode.isMepAMode() ? ++portIndex : portIndex;
41     }
42 
43     /**
44      * Converts the port index to compatible with the HAL.
45      *
46      * @param slotIndex   physical slot index corresponding to the portIndex
47      * @param portIndex port index
48      * @return target port index according to the MultipleEnabledProfilesMode
49      */
convertToHalPortIndex(int slotIndex, int portIndex)50     public static int convertToHalPortIndex(int slotIndex, int portIndex) {
51         return convertToHalPortIndex(UiccController.getInstance().getSupportedMepMode(slotIndex),
52                 portIndex);
53     }
54 
55     /**
56      * Converts the port index to compatible with the platform.
57      *
58      * @param slotIndex physical slot index corresponding to the portIndex
59      * @param portIndex target port index
60      * @param cardState cardState
61      * @param supportedMepMode supported MEP mode
62      * @return shifted port index according to the MultipleEnabledProfilesMode
63      */
convertFromHalPortIndex(int slotIndex, int portIndex, IccCardStatus.CardState cardState, MultipleEnabledProfilesMode supportedMepMode)64     public static int convertFromHalPortIndex(int slotIndex, int portIndex,
65             IccCardStatus.CardState cardState, MultipleEnabledProfilesMode supportedMepMode) {
66         // In case of MEP-A1 and MEP-A2, profiles are selected on eSIM Ports 1 and higher.
67         // But inorder to platform code MEP mode agnostic, platform always expects the ports
68         // are indexed with 0, 1... etc. Hence shift the target port index to be compatible
69         // with platform.
70 
71         // When the SIM_STATUS is related to CARDSTATE_ABSENT, CardStatus will not contain proper
72         // MEP mode info, fallback onto to the supportedMepMode data available in UiccSlot.
73         MultipleEnabledProfilesMode mepMode = cardState.isCardPresent() ? supportedMepMode
74                 : UiccController.getInstance().getSupportedMepMode(slotIndex);
75         return mepMode.isMepAMode() ? --portIndex : portIndex;
76     }
77 }
78