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 
read_a_line(const char * file_path,char * line,int line_size)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         len = len < line_size - 1? len : line_size - 1;
84         line[len] = '\0';
85         LOC_LOGD("cat %s: %s", file_path, line);
86         fclose(fp);
87     }
88     return result;
89 }
90 
91 /*The character array passed to this function should have length
92   of atleast PROPERTY_VALUE_MAX*/
loc_get_target_baseband(char * baseband,int array_length)93 void loc_get_target_baseband(char *baseband, int array_length)
94 {
95     if(baseband && (array_length >= PROPERTY_VALUE_MAX)) {
96         property_get("ro.baseband", baseband, "");
97         LOC_LOGD("%s:%d]: Baseband: %s\n", __func__, __LINE__, baseband);
98     }
99     else {
100         LOC_LOGE("%s:%d]: NULL parameter or array length less than PROPERTY_VALUE_MAX\n",
101                  __func__, __LINE__);
102     }
103 }
104 
105 /*The character array passed to this function should have length
106   of atleast PROPERTY_VALUE_MAX*/
loc_get_platform_name(char * platform_name,int array_length)107 void loc_get_platform_name(char *platform_name, int array_length)
108 {
109     if(platform_name && (array_length >= PROPERTY_VALUE_MAX)) {
110         property_get("ro.board.platform", platform_name, "");
111         LOC_LOGD("%s:%d]: Target name: %s\n", __func__, __LINE__, platform_name);
112     }
113     else {
114         LOC_LOGE("%s:%d]: Null parameter or array length less than PROPERTY_VALUE_MAX\n",
115                  __func__, __LINE__);
116     }
117 }
118 
119 /*The character array passed to this function should have length
120   of atleast PROPERTY_VALUE_MAX*/
loc_get_auto_platform_name(char * platform_name,int array_length)121 void loc_get_auto_platform_name(char *platform_name, int array_length)
122 {
123     if(platform_name && (array_length >= PROPERTY_VALUE_MAX)) {
124         property_get("ro.hardware.type", platform_name, "");
125         LOC_LOGD("%s:%d]: Autoplatform name: %s\n", __func__, __LINE__, platform_name);
126     }
127     else {
128         LOC_LOGE("%s:%d]: Null parameter or array length less than PROPERTY_VALUE_MAX\n",
129                  __func__, __LINE__);
130     }
131 }
132 
loc_get_target(void)133 unsigned int loc_get_target(void)
134 {
135     if (gTarget != (unsigned int)-1)
136         return gTarget;
137 
138     static const char hw_platform[]      = "/sys/devices/soc0/hw_platform";
139     static const char id[]               = "/sys/devices/soc0/soc_id";
140     static const char hw_platform_dep[]  =
141         "/sys/devices/system/soc/soc0/hw_platform";
142     static const char id_dep[]           = "/sys/devices/system/soc/soc0/id";
143     static const char mdm[]              = "/target"; // mdm target we are using
144 
145     char rd_hw_platform[LINE_LEN];
146     char rd_id[LINE_LEN];
147     char rd_mdm[LINE_LEN];
148     char baseband[LINE_LEN];
149     char rd_auto_platform[LINE_LEN];
150 
151     loc_get_target_baseband(baseband, sizeof(baseband));
152 
153     if (!access(hw_platform, F_OK)) {
154         read_a_line(hw_platform, rd_hw_platform, LINE_LEN);
155     } else {
156         read_a_line(hw_platform_dep, rd_hw_platform, LINE_LEN);
157     }
158     if (!access(id, F_OK)) {
159         read_a_line(id, rd_id, LINE_LEN);
160     } else {
161         read_a_line(id_dep, rd_id, LINE_LEN);
162     }
163 
164     /*check automotive platform*/
165     loc_get_auto_platform_name(rd_auto_platform, sizeof(rd_auto_platform));
166     if( !memcmp(rd_auto_platform, STR_AUTO, LENGTH(STR_AUTO)) )
167     {
168           gTarget = TARGET_AUTO;
169           goto detected;
170     }
171 
172     if( !memcmp(baseband, STR_APQ_NO_WGR, LENGTH(STR_APQ_NO_WGR)) ){
173 
174         gTarget = TARGET_NO_GNSS;
175         goto detected;
176     }
177 
178     if( !memcmp(baseband, STR_APQ, LENGTH(STR_APQ)) ||
179         !memcmp(baseband, STR_SDC, LENGTH(STR_SDC)) ||
180         !memcmp(baseband, STR_QCS, LENGTH(STR_QCS)) ) {
181 
182         if( !memcmp(rd_id, MPQ8064_ID_1, LENGTH(MPQ8064_ID_1))
183             && IS_STR_END(rd_id[LENGTH(MPQ8064_ID_1)]) )
184             gTarget = TARGET_NO_GNSS;
185         else
186             gTarget = TARGET_APQ_SA;
187     } else if (((!memcmp(rd_hw_platform, STR_LIQUID, LENGTH(STR_LIQUID))
188                  && IS_STR_END(rd_hw_platform[LENGTH(STR_LIQUID)])) ||
189                 (!memcmp(rd_hw_platform, STR_SURF,   LENGTH(STR_SURF))
190                  && IS_STR_END(rd_hw_platform[LENGTH(STR_SURF)])) ||
191                 (!memcmp(rd_hw_platform, STR_MTP,   LENGTH(STR_MTP))
192                  && IS_STR_END(rd_hw_platform[LENGTH(STR_MTP)]))) &&
193                !read_a_line( mdm, rd_mdm, LINE_LEN)) {
194         gTarget = TARGET_MDM;
195     } else if( (!memcmp(rd_id, MSM8930_ID_1, LENGTH(MSM8930_ID_1))
196                 && IS_STR_END(rd_id[LENGTH(MSM8930_ID_1)])) ||
197                (!memcmp(rd_id, MSM8930_ID_2, LENGTH(MSM8930_ID_2))
198                 && IS_STR_END(rd_id[LENGTH(MSM8930_ID_2)])) ) {
199         gTarget = TARGET_MSM_NO_SSC;
200     } else if ( !memcmp(baseband, STR_MSM, LENGTH(STR_MSM)) ||
201                 !memcmp(baseband, STR_SDM, LENGTH(STR_SDM)) ) {
202         gTarget = TARGET_DEFAULT;
203     } else {
204         gTarget = TARGET_UNKNOWN;
205     }
206 
207 detected:
208     LOC_LOGW("HAL: %s returned %d", __FUNCTION__, gTarget);
209     return gTarget;
210 }
211