common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / otherservice / vehicle_parameter_library / library / src / VP_GetEnv.c
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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 /* ====================================================================== */
18 /**
19  * @file VP_GetEnv.c
20  * @brief API to get vehicle parameter environment variable
21  */
22 /* ====================================================================== */
23
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <limits.h>
29 #include <other_service/VP_GetEnv.h>
30 #include <native_service/ns_backup.h>
31 #include <native_service/ns_backup_id.h>
32
33 #include "VP_FuncCheck_CanRcv_private.h"
34
35 // copy from vpsvc.h
36 #define VPSVC_COUNTRY_MAX (0x10)          // Maximal number of country codes for 1 destination
37
38 // copy from VehicleSens_Vpupdate.cpp
39
40 #define VP_VEHICLEPARAMETERLIBRARY_DEST_LEN_MIN  5           // At least 5 characters for 1 destination(e.g. "I:104")
41 #define VP_VEHICLEPARAMETERLIBRARY_DEST_NUM_MAX  (VP_MAX_LENGTH/VP_VEHICLEPARAMETERLIBRARY_DEST_LEN_MIN)
42 #define DS_PACK2_LEN_MAX    2+1           // Max. digits of destination package 2(+\0)
43
44 typedef struct {
45   char dest_bdb[2];                       // Destination symbol
46   char ds_pack2[DS_PACK2_LEN_MAX];        // Destination package 2
47   uint8_t coutry_num;                     // Number of Country Codes
48   uint16_t country_no[VPSVC_COUNTRY_MAX]; // Country Code
49 } VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE_t;
50
51 VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE_t g_vp_vehicleparameterlibrary_dest[VP_VEHICLEPARAMETERLIBRARY_DEST_NUM_MAX]; // Destination information get from environment variables
52 static uint32_t g_vp_vehicleparameterlibrary_dest_num;   // Number of destination information get from environment variables
53
54 static void AnalyzeVpVehicleparameterlibraryDestEnv(void) {
55   char p_env_variable[VP_MAX_LENGTH] = {};
56   char dest_buf[VP_MAX_LENGTH] = { 0 }; // Destination data for environment variables
57   char *c_code_ascii;                   // Country Code(ASCII)
58   char *dest_bdb_buf;                   // Destination symbol
59   uint32_t dest_num = 0;                // Number of destinations
60   int country_num = 0;                  // Number of Country Codes per Destination
61   char *token1, *saveptr1;              // Destination break token
62   char *token2, *saveptr2;              // Delimiter tokens in each destination
63   VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE_t *p_dest;
64   uint16_t *p_country;
65
66   g_vp_vehicleparameterlibrary_dest_num = 0;
67   memset(&g_vp_vehicleparameterlibrary_dest, 0x00, sizeof(VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE_t));
68
69   // Get environment variables
70   VP_GetEnv(VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE, p_env_variable);
71   if (0 == strncmp(p_env_variable, "", VP_MAX_LENGTH)) {
72     DEBUG_PRINT("VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE is NULL");
73     return;
74   }
75
76   DEBUG_PRINTF("VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE=[%s]\n", p_env_variable);
77
78   // Begin analysis  First, separate with ","
79   token1 = strtok_r(p_env_variable, ",", &saveptr1);
80   while (token1 != NULL) {
81     snprintf(dest_buf, VP_MAX_LENGTH, "%s", token1);
82     DEBUG_PRINTF("dest_buf\t[%s]", dest_buf);
83     p_dest = &g_vp_vehicleparameterlibrary_dest[dest_num];
84
85     // Get the country code  000-999
86     country_num = 0;
87     c_code_ascii = strchr(dest_buf, (int32_t)(':'));
88     if (c_code_ascii != NULL) {
89       c_code_ascii += 1;
90       token2 = strtok_r(c_code_ascii, "/", &saveptr2);
91       while (token2 != NULL) {
92         p_country = &(p_dest->country_no[country_num]);
93         errno = 0;
94         long int val = strtol(token2, (char **)NULL, 10);
95         if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
96             || (errno != 0 && val == 0)) {
97           DEBUG_PRINTF("country No format error [%s]", token2);
98           return;
99         }
100         *p_country = (uint16_t)val;
101
102         if (country_num >= VPSVC_COUNTRY_MAX) {
103           DEBUG_PRINTF("country num over [%s]", token2);
104           return;
105         }
106         country_num++;
107         token2 = strtok_r(NULL, "/", &saveptr2);
108       }
109       p_dest->coutry_num = (uint8_t)(country_num);
110       DEBUG_PRINTF("country_num\t[%d]", country_num);
111     }
112
113     // Get destination symbol (may not be available)
114     dest_bdb_buf = strchr(dest_buf, (int32_t)('&'));
115     if (dest_bdb_buf != NULL) {
116       p_dest->dest_bdb[0] = *(dest_bdb_buf + 1);
117       DEBUG_PRINTF("dest_bdb\t[%s]", p_dest->dest_bdb);
118     } else {
119       DEBUG_PRINT("dest_bdb is noting");
120     }
121
122     // Get destination packages
123     token2 = strtok_r(dest_buf, "&:", &saveptr2);
124     snprintf(p_dest->ds_pack2, DS_PACK2_LEN_MAX, "%s", token2);
125     DEBUG_PRINTF("ds_pack2\t[%s]\n", p_dest->ds_pack2);
126
127     dest_num++;
128     token1 = strtok_r(NULL, ",", &saveptr1);
129   }
130
131   g_vp_vehicleparameterlibrary_dest_num = dest_num;
132 }
133
134 // based VP_DspackToCountryCode
135 // dest [in] Destination codes read from the bkup_manager
136 // country_code [out] Country code string, separated by ";" for multi-country codes
137 static BOOL VP_DspackToCountryCode(uint8_t* dest, char *country_code) {
138   BOOL ret = FALSE;
139   VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE_t *p;
140   typedef struct {
141     char dest_bdb[2];                // Destination symbol
142     char ds_pack2[DS_PACK2_LEN_MAX]; // Destination package 2
143   } VEHICLE_CAN_DEST_t;
144   VEHICLE_CAN_DEST_t can_dest = {};
145
146   AnalyzeVpVehicleparameterlibraryDestEnv();
147
148   // Setting CAN data
149   can_dest.dest_bdb[0] = (int8_t)dest[0]; // Get destination symbol
150
151   if (isalpha(dest[2]) != 0) { // Get destination packages
152     can_dest.ds_pack2[0] = (int8_t)dest[2];
153   } else {
154     DEBUG_PRINTF("ds_pack2 is neither Alpha character[%c]", (int32_t)dest[2]);
155     return ret;
156   }
157
158   // Comparison with vehicle parameter
159   for (uint32_t i = 0; i < g_vp_vehicleparameterlibrary_dest_num; i++) {
160     p = &g_vp_vehicleparameterlibrary_dest[i];
161     // Determination of destination packages
162     if (strncmp(p->ds_pack2, can_dest.ds_pack2, sizeof(can_dest.ds_pack2)) != 0) {
163       continue;
164     }
165
166     // Determination of destination symbol
167     if (p->dest_bdb[0] == 0) {
168       ret = TRUE; // When the destination code judgment is not required
169       break;
170     }
171
172     if (strncmp(p->dest_bdb, can_dest.dest_bdb, sizeof(can_dest.dest_bdb)) == 0) {
173       ret = TRUE;
174       break;
175     }
176   }
177
178   // Returned as a country code string
179   if (ret == TRUE) {
180     snprintf(country_code, VP_MAX_LENGTH, "%03d", p->country_no[0]);
181     for (uint32_t i = 1; i < p->coutry_num; i++) {
182       char buf[VP_MAX_LENGTH] = {};
183       snprintf(buf, VP_MAX_LENGTH, "%s;%03d", country_code, p->country_no[i]);
184       strncpy(country_code, buf, VP_MAX_LENGTH);
185     }
186   }
187   return ret;
188 }
189 // end of copy from VehicleSens_Vpupdate.cpp
190
191 // copy from vehicle unit
192 typedef struct {
193   uint8_t uc_hv;             /* hv */
194   uint8_t uc_hv_status;      /* hv status */
195   uint8_t uc_2wd4wd;         /* 2wd4wd */
196   uint8_t uc_2wd4wd_status;  /* 2wd4wd status */
197   uint8_t uc_dest[3];        /* Destination */
198   uint8_t uc_dest_status;    /* Destination status */
199   uint8_t uc_stwheel;        /* STEERING_WHEEL */
200   uint8_t uc_stwheel_status; /* STEERING_WHEEL status */
201   uint8_t uc_reserve[6];     /* Reserve */
202 } VEHICLESENS_NON_VOLATILE_DATA;
203
204 /* ====================================================================== */
205 /**
206  * @fn
207  * void VP_GetVp_CWORD31_Destination(char *pEnvBuff)
208  * @breaf Get environment variables
209  * @param[out] (pEnvBuff) Start address of the area to store the get environment variable value
210  * @return None
211  * @detail Read and analyze D_BK_ID_VEHICLE_STABLE_DATA from BackupManager.
212  *         The country code string is stored in the argument and returned.
213  */
214 /* ====================================================================== */
215 static void VP_GetVp_CWORD31_Destination(char *pEnvBuff)
216 {
217   int32_t ret_api = BKUP_RET_NORMAL;
218   VEHICLESENS_NON_VOLATILE_DATA pstback_up_data;
219   memset(&pstback_up_data, 0x00, sizeof(VEHICLESENS_NON_VOLATILE_DATA));
220   char env_string[VP_MAX_LENGTH];
221
222   VP_GetEnv(VP__CWORD31__TELEMATICS_FUNCTION, env_string);
223
224   if ('\0' == env_string[0]) {
225     *pEnvBuff = '\0';
226     return;
227   } else {
228     // read from backup
229     ret_api = Backup_DataRd(D_BK_ID_VEHICLE_STABLE_DATA,
230                             0,
231                             &pstback_up_data,
232                             sizeof(VEHICLESENS_NON_VOLATILE_DATA));
233     if (BKUP_RET_NORMAL != ret_api) {
234       *pEnvBuff = '\0';
235       return;
236     }
237   }
238
239   if (0 == strncmp(env_string, "country_ID", strlen("country_ID"))) {
240     uint32_t country_code = 0;
241     country_code += (pstback_up_data.uc_dest[1]); // countory_code 3rd and 2nd digit
242     country_code = (country_code << 4);           // shift
243     country_code += (pstback_up_data.uc_dest[2] >> 4); // countory_code 1st digit
244     snprintf(pEnvBuff, VP_MAX_LENGTH, "%x", country_code);
245   } else if(0 == strncmp(env_string, "CAN_judge", strlen("CAN_judge"))) {
246     VP_DspackToCountryCode(pstback_up_data.uc_dest, pEnvBuff);
247   } else {
248     *pEnvBuff = '\0';
249   }
250 }
251
252 /* ====================================================================== */
253 /**
254  * @fn
255  * void VP_GetEnv(char *pEnvStr, char *pEnvBuff )
256  * @breaf Get Environment Variable
257  * @param[in]  (pEnvStr) Pointer to the string of the environment variable name to be gotten
258  * @param[out] (pEnvBuff) Start address of the area to store the gotten value of the environment variable
259  * @return None
260  * @detail Read the value (string) of the environment variable specified by the argument and stores it in the specified address.
261  */
262 /* ====================================================================== */
263 void VP_GetEnv(const char *pEnvStr, char *pEnvBuff )
264 {
265   const char *env_string;
266   size_t length;
267
268   if ((pEnvStr != NULL) && (pEnvBuff != NULL)) {
269     if (0 == strncmp(pEnvStr, VP__CWORD31__DESTINATION, strlen(VP__CWORD31__DESTINATION))) {
270       VP_GetVp_CWORD31_Destination(pEnvBuff);
271     } else {
272       env_string = getenv(pEnvStr);
273
274       if (env_string == NULL) {
275         *pEnvBuff = '\0';
276       } else {
277         length = strlen( env_string );
278         if (length < ((size_t)VP_MAX_LENGTH)) {
279           (void)strcpy(pEnvBuff, env_string);
280         } else {
281           (void)strncpy(pEnvBuff, env_string, (VP_MAX_LENGTH - 1));
282           pEnvBuff[ VP_MAX_LENGTH - 1 ] = '\0';
283         }
284       }
285     }
286   } else {
287     if(pEnvBuff != NULL) {
288       *pEnvBuff = '\0';
289     }
290   }
291 }