loc_target.cpp 11 KB

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