Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 1 | /* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <unistd.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <errno.h> |
| 38 | #include <log_util.h> |
| 39 | #include "loc_target.h" |
| 40 | #include "loc_log.h" |
| 41 | #include <loc_pla.h> |
| 42 | |
| 43 | #define APQ8064_ID_1 "109" |
| 44 | #define APQ8064_ID_2 "153" |
| 45 | #define MPQ8064_ID_1 "130" |
| 46 | #define MSM8930_ID_1 "142" |
| 47 | #define MSM8930_ID_2 "116" |
| 48 | #define APQ8030_ID_1 "157" |
| 49 | #define APQ8074_ID_1 "184" |
| 50 | |
| 51 | #define LINE_LEN 100 |
| 52 | #define STR_LIQUID "Liquid" |
| 53 | #define STR_SURF "Surf" |
| 54 | #define STR_MTP "MTP" |
| 55 | #define STR_APQ "apq" |
| 56 | #define STR_SDC "sdc" // alternative string for APQ targets |
| 57 | #define STR_QCS "qcs" // string for Gen9 APQ targets |
| 58 | #define STR_MSM "msm" |
| 59 | #define STR_SDM "sdm" // alternative string for MSM targets |
| 60 | #define STR_APQ_NO_WGR "baseband_apq_nowgr" |
| 61 | #define STR_AUTO "auto" |
| 62 | #define IS_STR_END(c) ((c) == '\0' || (c) == '\n' || (c) == '\r') |
| 63 | #define LENGTH(s) (sizeof(s) - 1) |
| 64 | #define GPS_CHECK_NO_ERROR 0 |
| 65 | #define GPS_CHECK_NO_GPS_HW 1 |
| 66 | |
| 67 | static unsigned int gTarget = (unsigned int)-1; |
| 68 | |
| 69 | static int read_a_line(const char * file_path, char * line, int line_size) |
| 70 | { |
| 71 | FILE *fp; |
| 72 | int result = 0; |
| 73 | |
| 74 | * line = '\0'; |
| 75 | fp = fopen(file_path, "r" ); |
| 76 | if( fp == NULL ) { |
| 77 | LOC_LOGE("open failed: %s: %s\n", file_path, strerror(errno)); |
| 78 | result = -1; |
| 79 | } else { |
| 80 | int len; |
| 81 | fgets(line, line_size, fp); |
| 82 | len = strlen(line); |
| 83 | while ('\n' == line[len-1]) { |
| 84 | // If there is a new line at end of string, replace it with NULL |
| 85 | line[len-1] = '\0'; |
| 86 | len--; |
| 87 | } |
| 88 | len = len < line_size - 1? len : line_size - 1; |
| 89 | line[len] = '\0'; |
| 90 | LOC_LOGD("cat %s: %s", file_path, line); |
| 91 | fclose(fp); |
| 92 | } |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | /*The character array passed to this function should have length |
| 97 | of atleast PROPERTY_VALUE_MAX*/ |
| 98 | void loc_get_target_baseband(char *baseband, int array_length) |
| 99 | { |
| 100 | if(baseband && (array_length >= PROPERTY_VALUE_MAX)) { |
| 101 | property_get("ro.baseband", baseband, ""); |
| 102 | LOC_LOGD("%s:%d]: Baseband: %s\n", __func__, __LINE__, baseband); |
| 103 | } |
| 104 | else { |
| 105 | LOC_LOGE("%s:%d]: NULL parameter or array length less than PROPERTY_VALUE_MAX\n", |
| 106 | __func__, __LINE__); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /*The character array passed to this function should have length |
| 111 | of atleast PROPERTY_VALUE_MAX*/ |
| 112 | void loc_get_platform_name(char *platform_name, int array_length) |
| 113 | { |
| 114 | if(platform_name && (array_length >= PROPERTY_VALUE_MAX)) { |
| 115 | property_get("ro.board.platform", platform_name, ""); |
| 116 | LOC_LOGD("%s:%d]: Target name: %s\n", __func__, __LINE__, platform_name); |
| 117 | } |
| 118 | else { |
| 119 | LOC_LOGE("%s:%d]: Null parameter or array length less than PROPERTY_VALUE_MAX\n", |
| 120 | __func__, __LINE__); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /*The character array passed to this function should have length |
| 125 | of atleast PROPERTY_VALUE_MAX*/ |
| 126 | void loc_get_auto_platform_name(char *platform_name, int array_length) |
| 127 | { |
| 128 | if(platform_name && (array_length >= PROPERTY_VALUE_MAX)) { |
| 129 | property_get("ro.hardware.type", platform_name, ""); |
| 130 | LOC_LOGD("%s:%d]: Autoplatform name: %s\n", __func__, __LINE__, platform_name); |
| 131 | } |
| 132 | else { |
| 133 | LOC_LOGE("%s:%d]: Null parameter or array length less than PROPERTY_VALUE_MAX\n", |
| 134 | __func__, __LINE__); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /*Reads the property ro.config.low_ram to identify if this is a low ram target |
| 139 | Returns: |
| 140 | 0 if not a low ram target |
| 141 | 1 if this is a low ram target |
| 142 | */ |
| 143 | int loc_identify_low_ram_target() |
| 144 | { |
| 145 | int ret = 0; |
| 146 | char low_ram_target[PROPERTY_VALUE_MAX]; |
| 147 | property_get("ro.config.low_ram", low_ram_target, ""); |
| 148 | LOC_LOGd("low ram target: %s\n", low_ram_target); |
| 149 | return !(strncmp(low_ram_target, "true", PROPERTY_VALUE_MAX)); |
| 150 | } |
| 151 | |
| 152 | /*The character array passed to this function should have length |
| 153 | of atleast PROPERTY_VALUE_MAX*/ |
| 154 | /* Reads the soc_id node and return the soc_id value */ |
| 155 | void loc_get_device_soc_id(char *soc_id_value, int array_length) |
| 156 | { |
| 157 | static const char soc_id[] = "/sys/devices/soc0/soc_id"; |
| 158 | static const char soc_id_dep[] = "/sys/devices/system/soc/soc0/id"; |
| 159 | int return_val = 0; |
| 160 | |
| 161 | if (soc_id_value && (array_length >= PROPERTY_VALUE_MAX)) { |
| 162 | if (!access(soc_id, F_OK)) { |
| 163 | return_val = read_a_line(soc_id, soc_id_value, array_length); |
| 164 | } else { |
| 165 | return_val = read_a_line(soc_id_dep, soc_id_value, array_length); |
| 166 | } |
| 167 | if (0 == return_val) { |
| 168 | LOC_LOGd("SOC Id value: %s\n", soc_id_value); |
| 169 | } else { |
| 170 | LOC_LOGe("Unable to read the soc_id value\n"); |
| 171 | } |
| 172 | } else { |
| 173 | LOC_LOGe("Null parameter or array length less than PROPERTY_VALUE_MAX\n"); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | unsigned int loc_get_target(void) |
| 178 | { |
| 179 | if (gTarget != (unsigned int)-1) |
| 180 | return gTarget; |
| 181 | |
| 182 | static const char hw_platform[] = "/sys/devices/soc0/hw_platform"; |
| 183 | static const char hw_platform_dep[] = |
| 184 | "/sys/devices/system/soc/soc0/hw_platform"; |
| 185 | static const char mdm[] = "/target"; // mdm target we are using |
| 186 | |
| 187 | char rd_hw_platform[LINE_LEN]; |
| 188 | char rd_id[LINE_LEN]; |
| 189 | char rd_mdm[LINE_LEN]; |
| 190 | char baseband[LINE_LEN]; |
| 191 | char rd_auto_platform[LINE_LEN]; |
| 192 | |
| 193 | loc_get_target_baseband(baseband, sizeof(baseband)); |
| 194 | |
| 195 | if (!access(hw_platform, F_OK)) { |
| 196 | read_a_line(hw_platform, rd_hw_platform, LINE_LEN); |
| 197 | } else { |
| 198 | read_a_line(hw_platform_dep, rd_hw_platform, LINE_LEN); |
| 199 | } |
| 200 | // Get the soc-id for this device. |
| 201 | loc_get_device_soc_id(rd_id, sizeof(rd_id)); |
| 202 | |
| 203 | /*check automotive platform*/ |
| 204 | loc_get_auto_platform_name(rd_auto_platform, sizeof(rd_auto_platform)); |
| 205 | if( !memcmp(rd_auto_platform, STR_AUTO, LENGTH(STR_AUTO)) ) |
| 206 | { |
| 207 | gTarget = TARGET_AUTO; |
| 208 | goto detected; |
| 209 | } |
| 210 | |
| 211 | if( !memcmp(baseband, STR_APQ_NO_WGR, LENGTH(STR_APQ_NO_WGR)) ){ |
| 212 | |
| 213 | gTarget = TARGET_NO_GNSS; |
| 214 | goto detected; |
| 215 | } |
| 216 | |
| 217 | if( !memcmp(baseband, STR_APQ, LENGTH(STR_APQ)) || |
| 218 | !memcmp(baseband, STR_SDC, LENGTH(STR_SDC)) || |
| 219 | !memcmp(baseband, STR_QCS, LENGTH(STR_QCS)) ) { |
| 220 | |
| 221 | if( !memcmp(rd_id, MPQ8064_ID_1, LENGTH(MPQ8064_ID_1)) |
| 222 | && IS_STR_END(rd_id[LENGTH(MPQ8064_ID_1)]) ) |
| 223 | gTarget = TARGET_NO_GNSS; |
| 224 | else |
| 225 | gTarget = TARGET_APQ_SA; |
| 226 | } else if (((!memcmp(rd_hw_platform, STR_LIQUID, LENGTH(STR_LIQUID)) |
| 227 | && IS_STR_END(rd_hw_platform[LENGTH(STR_LIQUID)])) || |
| 228 | (!memcmp(rd_hw_platform, STR_SURF, LENGTH(STR_SURF)) |
| 229 | && IS_STR_END(rd_hw_platform[LENGTH(STR_SURF)])) || |
| 230 | (!memcmp(rd_hw_platform, STR_MTP, LENGTH(STR_MTP)) |
| 231 | && IS_STR_END(rd_hw_platform[LENGTH(STR_MTP)]))) && |
| 232 | !read_a_line( mdm, rd_mdm, LINE_LEN)) { |
| 233 | gTarget = TARGET_MDM; |
| 234 | } else if( (!memcmp(rd_id, MSM8930_ID_1, LENGTH(MSM8930_ID_1)) |
| 235 | && IS_STR_END(rd_id[LENGTH(MSM8930_ID_1)])) || |
| 236 | (!memcmp(rd_id, MSM8930_ID_2, LENGTH(MSM8930_ID_2)) |
| 237 | && IS_STR_END(rd_id[LENGTH(MSM8930_ID_2)])) ) { |
| 238 | gTarget = TARGET_MSM_NO_SSC; |
| 239 | } else if ( !memcmp(baseband, STR_MSM, LENGTH(STR_MSM)) || |
| 240 | !memcmp(baseband, STR_SDM, LENGTH(STR_SDM)) ) { |
| 241 | gTarget = TARGET_DEFAULT; |
| 242 | } else { |
| 243 | gTarget = TARGET_UNKNOWN; |
| 244 | } |
| 245 | |
| 246 | detected: |
| 247 | LOC_LOGW("HAL: %s returned %d", __FUNCTION__, gTarget); |
| 248 | return gTarget; |
| 249 | } |