2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* ====================================================================== */
20 * @brief API to get vehicle parameter environment variable
22 /* ====================================================================== */
29 #include <other_service/VP_GetEnv.h>
30 #include <native_service/ns_backup.h>
31 #include <native_service/ns_backup_id.h>
33 #include "VP_FuncCheck_CanRcv_private.h"
36 #define VPSVC_COUNTRY_MAX (0x10) // Maximal number of country codes for 1 destination
38 // copy from VehicleSens_Vpupdate.cpp
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)
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;
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
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;
66 g_vp_vehicleparameterlibrary_dest_num = 0;
67 memset(&g_vp_vehicleparameterlibrary_dest, 0x00, sizeof(VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE_t));
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");
76 DEBUG_PRINTF("VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE=[%s]\n", p_env_variable);
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];
85 // Get the country code 000-999
87 c_code_ascii = strchr(dest_buf, (int32_t)(':'));
88 if (c_code_ascii != NULL) {
90 token2 = strtok_r(c_code_ascii, "/", &saveptr2);
91 while (token2 != NULL) {
92 p_country = &(p_dest->country_no[country_num]);
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);
100 *p_country = (uint16_t)val;
102 if (country_num >= VPSVC_COUNTRY_MAX) {
103 DEBUG_PRINTF("country num over [%s]", token2);
107 token2 = strtok_r(NULL, "/", &saveptr2);
109 p_dest->coutry_num = (uint8_t)(country_num);
110 DEBUG_PRINTF("country_num\t[%d]", country_num);
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);
119 DEBUG_PRINT("dest_bdb is noting");
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);
128 token1 = strtok_r(NULL, ",", &saveptr1);
131 g_vp_vehicleparameterlibrary_dest_num = dest_num;
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) {
139 VP_VEHICLEPARAMETERLIBRARY_DEST_C_CODE_t *p;
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 = {};
146 AnalyzeVpVehicleparameterlibraryDestEnv();
149 can_dest.dest_bdb[0] = (int8_t)dest[0]; // Get destination symbol
151 if (isalpha(dest[2]) != 0) { // Get destination packages
152 can_dest.ds_pack2[0] = (int8_t)dest[2];
154 DEBUG_PRINTF("ds_pack2 is neither Alpha character[%c]", (int32_t)dest[2]);
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) {
166 // Determination of destination symbol
167 if (p->dest_bdb[0] == 0) {
168 ret = TRUE; // When the destination code judgment is not required
172 if (strncmp(p->dest_bdb, can_dest.dest_bdb, sizeof(can_dest.dest_bdb)) == 0) {
178 // Returned as a country code string
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);
189 // end of copy from VehicleSens_Vpupdate.cpp
191 // copy from vehicle unit
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;
204 /* ====================================================================== */
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
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.
214 /* ====================================================================== */
215 static void VP_GetVp_CWORD31_Destination(char *pEnvBuff)
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];
222 VP_GetEnv(VP__CWORD31__TELEMATICS_FUNCTION, env_string);
224 if ('\0' == env_string[0]) {
229 ret_api = Backup_DataRd(D_BK_ID_VEHICLE_STABLE_DATA,
232 sizeof(VEHICLESENS_NON_VOLATILE_DATA));
233 if (BKUP_RET_NORMAL != ret_api) {
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);
252 /* ====================================================================== */
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
260 * @detail Read the value (string) of the environment variable specified by the argument and stores it in the specified address.
262 /* ====================================================================== */
263 void VP_GetEnv(const char *pEnvStr, char *pEnvBuff )
265 const char *env_string;
268 if ((pEnvStr != NULL) && (pEnvBuff != NULL)) {
269 if (0 == strncmp(pEnvStr, VP__CWORD31__DESTINATION, strlen(VP__CWORD31__DESTINATION))) {
270 VP_GetVp_CWORD31_Destination(pEnvBuff);
272 env_string = getenv(pEnvStr);
274 if (env_string == NULL) {
277 length = strlen( env_string );
278 if (length < ((size_t)VP_MAX_LENGTH)) {
279 (void)strcpy(pEnvBuff, env_string);
281 (void)strncpy(pEnvBuff, env_string, (VP_MAX_LENGTH - 1));
282 pEnvBuff[ VP_MAX_LENGTH - 1 ] = '\0';
287 if(pEnvBuff != NULL) {