rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* Copyright Statement: |
| 2 | * |
| 3 | * This software/firmware and related documentation ("MediaTek Software") are |
| 4 | * protected under relevant copyright laws. The information contained herein |
| 5 | * is confidential and proprietary to MediaTek Inc. and/or its licensors. |
| 6 | * Without the prior written permission of MediaTek inc. and/or its licensors, |
| 7 | * any reproduction, modification, use or disclosure of MediaTek Software, |
| 8 | * and information contained herein, in whole or in part, shall be strictly prohibited. |
| 9 | * |
| 10 | * MediaTek Inc. (C) 2016. All rights reserved. |
| 11 | * |
| 12 | * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| 13 | * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| 14 | * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON |
| 15 | * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| 18 | * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| 19 | * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| 20 | * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH |
| 21 | * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES |
| 22 | * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES |
| 23 | * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK |
| 24 | * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR |
| 25 | * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND |
| 26 | * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| 27 | * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| 28 | * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO |
| 29 | * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| 30 | * |
| 31 | * The following software/firmware and/or related documentation ("MediaTek Software") |
| 32 | * have been modified by MediaTek Inc. All revisions are subject to any receiver's |
| 33 | * applicable license agreements with MediaTek Inc. |
| 34 | */ |
| 35 | |
| 36 | #include <log/log.h> |
| 37 | #include <cutils/properties.h> |
| 38 | |
| 39 | #include "RatConfiguration.h" |
| 40 | #include "utils.h" |
| 41 | |
| 42 | #define LOG_TAG "DEMO_RatConfig" |
| 43 | |
| 44 | const std::string RatConfiguration::PROPERTY_BUILD_RAT_CONFIG = |
| 45 | "ro.vendor.mtk_protocol1_rat_config"; |
| 46 | /* the system property of active rat */ |
| 47 | const std::string RatConfiguration::PROPERTY_RAT_CONFIG = "ro.boot.opt_ps1_rat"; |
| 48 | const std::string RatConfiguration::PROPERTY_IS_USING_DEFAULT_CONFIG = |
| 49 | "ro.boot.opt_using_default"; |
| 50 | /* the valid characters of the human-readable rat config */ |
| 51 | /* the defination must be sync with ratconfig.c */ |
| 52 | const std::string RatConfiguration::CDMA = "C"; |
| 53 | const std::string RatConfiguration::LteFdd = "Lf"; |
| 54 | const std::string RatConfiguration::LteTdd = "Lt"; |
| 55 | const std::string RatConfiguration::WCDMA = "W"; |
| 56 | const std::string RatConfiguration::TDSCDMA = "T"; |
| 57 | const std::string RatConfiguration::GSM = "G"; |
| 58 | const std::string RatConfiguration::DELIMITER = "/"; |
| 59 | |
| 60 | /* bitmask */ |
| 61 | /* the defination must be sync with ratconfig.c */ |
| 62 | const int RatConfiguration::MASK_CDMA = (1 << 5); |
| 63 | const int RatConfiguration::MASK_LteFdd = (1 << 4); |
| 64 | const int RatConfiguration::MASK_LteTdd = (1 << 3); |
| 65 | const int RatConfiguration::MASK_WCDMA = (1 << 2); |
| 66 | const int RatConfiguration::MASK_TDSCDMA = (1 << 1); |
| 67 | const int RatConfiguration::MASK_GSM = (1); |
| 68 | |
| 69 | const int RatConfiguration::MD_MODE_UNKNOWN = 0; |
| 70 | const int RatConfiguration::MD_MODE_LTG = 8; //uLTG |
| 71 | const int RatConfiguration::MD_MODE_LWG = 9; //uLWG |
| 72 | const int RatConfiguration::MD_MODE_LWTG = 10; //uLWTG |
| 73 | const int RatConfiguration::MD_MODE_LWCG = 11; //uLWCG |
| 74 | const int RatConfiguration::MD_MODE_LWCTG = 12; //uLWTCG(Auto mode) |
| 75 | const int RatConfiguration::MD_MODE_LTTG = 13; //LtTG |
| 76 | const int RatConfiguration::MD_MODE_LFWG = 14; //LfWG |
| 77 | const int RatConfiguration::MD_MODE_LFWCG = 15; //uLfWCG |
| 78 | const int RatConfiguration::MD_MODE_LCTG = 16; //uLCTG |
| 79 | const int RatConfiguration::MD_MODE_LTCTG = 17; //uLtCTG |
| 80 | |
| 81 | int RatConfiguration::max_rat = 0; |
| 82 | bool RatConfiguration::max_rat_initialized = false; |
| 83 | int RatConfiguration::actived_rat = 0; |
| 84 | bool RatConfiguration::is_default_config = true; |
| 85 | |
| 86 | RatConfiguration::RatConfiguration() { |
| 87 | // TODO Auto-generated constructor stub |
| 88 | |
| 89 | } |
| 90 | |
| 91 | RatConfiguration::~RatConfiguration() { |
| 92 | // TODO Auto-generated destructor stub |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * transfer rat format from "L/T/G" to bitmask |
| 97 | * @params String rat, the rat in format like "L/T/G" |
| 98 | * @return int, the rat in bitmask. |
| 99 | */ |
| 100 | int RatConfiguration::ratToBitmask(std::string rat) { |
| 101 | int iRat = 0; |
| 102 | if (rat.find(CDMA) != std::string::npos) { |
| 103 | iRat = iRat | MASK_CDMA; |
| 104 | } |
| 105 | if (rat.find(LteFdd) != std::string::npos) { |
| 106 | iRat = iRat | MASK_LteFdd; |
| 107 | } |
| 108 | if (rat.find(LteTdd) != std::string::npos) { |
| 109 | iRat = iRat | MASK_LteTdd; |
| 110 | } |
| 111 | if (rat.find(WCDMA) != std::string::npos) { |
| 112 | iRat = iRat | MASK_WCDMA; |
| 113 | } |
| 114 | if (rat.find(TDSCDMA) != std::string::npos) { |
| 115 | iRat = iRat | MASK_TDSCDMA; |
| 116 | } |
| 117 | if (rat.find(GSM) != std::string::npos) { |
| 118 | iRat = iRat | MASK_GSM; |
| 119 | } |
| 120 | return iRat; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * get the rat of project config |
| 125 | * @return int, the rat in bitmask. |
| 126 | */ |
| 127 | int RatConfiguration::getMaxRat() { |
| 128 | if (!max_rat_initialized) { |
| 129 | char rat[PROPERTY_VALUE_MAX] = {0}; |
| 130 | utils::mtk_property_get(PROPERTY_BUILD_RAT_CONFIG.c_str(), rat, ""); |
| 131 | std::string sMaxRat(rat); |
| 132 | max_rat = ratToBitmask(sMaxRat); |
| 133 | char def_rat[PROPERTY_VALUE_MAX] = {0}; |
| 134 | is_default_config = (utils::mtk_property_get_int32(PROPERTY_IS_USING_DEFAULT_CONFIG.c_str(), 1) != 0) ? true : false; |
| 135 | max_rat_initialized = true; |
| 136 | RLOGD("getMaxRat: initial %s %d ", sMaxRat.c_str(), max_rat); |
| 137 | } |
| 138 | return max_rat; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * check rat config is supported by project config |
| 143 | * @params int iRat, the rat to be checked. |
| 144 | * @return boolean, |
| 145 | * true, the rat is supported. |
| 146 | * false, the rat is not supported. |
| 147 | */ |
| 148 | bool RatConfiguration::checkRatConfig(int iRat) { |
| 149 | int maxrat = getMaxRat(); |
| 150 | if ((iRat | maxrat) == maxrat) { |
| 151 | return true; |
| 152 | } else { |
| 153 | RLOGD("checkRatConfig: FAIL with %d", iRat); |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * get the active rat in bitmask. |
| 160 | * @return int, the rat in bitmask. |
| 161 | */ |
| 162 | int RatConfiguration::getRatConfig() { |
| 163 | int default_rat_config = getMaxRat(); |
| 164 | if (default_rat_config == 0) { |
| 165 | actived_rat = 0; |
| 166 | return actived_rat; |
| 167 | } |
| 168 | if (is_default_config) { |
| 169 | actived_rat = default_rat_config; |
| 170 | return default_rat_config; |
| 171 | } |
| 172 | char rat_config[PROPERTY_VALUE_MAX] = {0}; |
| 173 | utils::mtk_property_get(PROPERTY_RAT_CONFIG.c_str(), rat_config, ""); |
| 174 | std::string rat(rat_config); |
| 175 | if (rat.length() > 0) { |
| 176 | actived_rat = ratToBitmask(rat); |
| 177 | if (checkRatConfig(actived_rat) == false) { |
| 178 | RLOGD("getRatConfig: invalid PROPERTY_RAT_CONFIG, set to max_rat"); |
| 179 | actived_rat = getMaxRat(); |
| 180 | } |
| 181 | } else { |
| 182 | RLOGD("getRatConfig: ger property PROPERTY_RAT_CONFIG fail, initialize"); |
| 183 | actived_rat = getMaxRat(); |
| 184 | } |
| 185 | return actived_rat; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * transfer the format from bitmask to "L/T/G". |
| 190 | * @params int iRat, rat in bitmask |
| 191 | * @return String, rat in format like "L/T/G". |
| 192 | */ |
| 193 | std::string RatConfiguration::ratToString(int iRat) { |
| 194 | std::string rat = ""; |
| 195 | if ((iRat & MASK_CDMA) == MASK_CDMA) { |
| 196 | rat += (DELIMITER + CDMA); |
| 197 | } |
| 198 | if ((iRat & MASK_LteFdd) == MASK_LteFdd) { |
| 199 | rat += (DELIMITER + LteFdd); |
| 200 | } |
| 201 | if ((iRat & MASK_LteTdd) == MASK_LteTdd) { |
| 202 | rat += (DELIMITER + LteTdd); |
| 203 | } |
| 204 | if ((iRat & MASK_WCDMA) == MASK_WCDMA) { |
| 205 | rat += (DELIMITER + WCDMA); |
| 206 | } |
| 207 | if ((iRat & MASK_TDSCDMA) == MASK_TDSCDMA) { |
| 208 | rat += (DELIMITER + TDSCDMA); |
| 209 | } |
| 210 | if ((iRat & MASK_GSM) == MASK_GSM) { |
| 211 | rat += (DELIMITER + GSM); |
| 212 | } |
| 213 | if (rat.length() > 0) { |
| 214 | // for remove the delimiter at rat[0] |
| 215 | rat = rat.substr(1); |
| 216 | } |
| 217 | return rat; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * check C2k suppport |
| 222 | * @return boolean, cases as following |
| 223 | * true, rat is active and project config supports it. |
| 224 | * false, rat is inactive no matter project config supports. |
| 225 | */ |
| 226 | bool RatConfiguration::isC2kSupported() { |
| 227 | return (getMaxRat() & getRatConfig() & MASK_CDMA) == MASK_CDMA ? |
| 228 | true : false; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * check LteFdd suppport |
| 233 | * @return boolean, cases as following |
| 234 | * true, rat is active and project config supports it. |
| 235 | * false, rat is inactive no matter project config supports. |
| 236 | */ |
| 237 | bool RatConfiguration::isLteFddSupported() { |
| 238 | return (getMaxRat() & getRatConfig() & MASK_LteFdd) == MASK_LteFdd ? |
| 239 | true : false; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * check LteTdd suppport |
| 244 | * @return boolean, cases as following |
| 245 | * true, rat is active and project config supports it. |
| 246 | * false, rat is inactive no matter project config supports. |
| 247 | */ |
| 248 | bool RatConfiguration::isLteTddSupported() { |
| 249 | return (getMaxRat() & getRatConfig() & MASK_LteTdd) == MASK_LteTdd ? |
| 250 | true : false; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * check Wcdma suppport |
| 255 | * @return boolean, cases as following |
| 256 | * true, rat is active and project config supports it. |
| 257 | * false, rat is inactive no matter project config supports. |
| 258 | */ |
| 259 | bool RatConfiguration::isWcdmaSupported() { |
| 260 | return (getMaxRat() & getRatConfig() & MASK_WCDMA) == MASK_WCDMA ? |
| 261 | true : false; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * check Tdscdma suppport |
| 266 | * @return boolean, cases as following |
| 267 | * true, rat is active and project config supports it. |
| 268 | * false, rat is inactive no matter project config supports. |
| 269 | */ |
| 270 | bool RatConfiguration::isTdscdmaSupported() { |
| 271 | return (getMaxRat() & getRatConfig() & MASK_TDSCDMA) == MASK_TDSCDMA ? |
| 272 | true : false; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * check GSM suppport |
| 277 | * @return boolean, cases as following |
| 278 | * true, rat is active and project config supports it. |
| 279 | * false, rat is inactive no matter project config supports. |
| 280 | */ |
| 281 | bool RatConfiguration::isGsmSupported() { |
| 282 | return (getMaxRat() & getRatConfig() & MASK_GSM) == MASK_GSM ? true : false; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * get the active rat |
| 287 | * @return String, the rat in format like C/Lf/Lt/T/W/G |
| 288 | */ |
| 289 | std::string RatConfiguration::getActiveRatConfig() { |
| 290 | std::string rat = ratToString(getRatConfig()); |
| 291 | RLOGD("getActiveRatConfig: %s", rat.c_str()); |
| 292 | return rat; |
| 293 | } |