summaryrefslogtreecommitdiffstats
path: root/systemservice/config/library/system_manager_config/src/ss_system_manager_conf.cpp
blob: 16ec02a4c2882b2a978d9843aa626af5130956ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <fstream>
#include <string.h>
#include <agl_thread.h>
#include <other_service/VP_GetEnv.h>

#include <system_service/ss_templates.h>
#include <system_service/ss_sm_ram_access.h>
#include <system_service/ss_ver.h>

#include "system_service/ss_package.h"
#include "system_service/ss_access_if_private.h"
#include "system_service/ss_system_manager_conf.h"

#ifndef ZONE_WARN
#define ZONE_WARN     ZONEMASK(30)
#endif
#ifndef ZONE_INFO
#define ZONE_INFO     ZONEMASK(29)
#endif

/* Environment variable name */
const CHAR LaunchConfigFileNameEnvVariable[] = "APPS_CONFIG_FILENAME";
const CHAR AreaTypeEnvVariable[] = "VEHICLEPARAMETERLIBRARY_AREA";
const CHAR BrandTypeEnvVariable[] = "VEHICLEPARAMETERLIBRARY_BRAND";
const CHAR GradeEnvVariable[] = "VP_VEHICLEPARAMETERLIBRARY_grade";

/* Environment variable value */
const CHAR EnvValueYes[] = "y";
const CHAR EnvValueNo[] = "n";

const CHAR EnvValueAvailable[]   = "available";
const CHAR EnvValueUnavailable[] = "unavailable";

/*
 *  Note.
 *  Define areas, brands, and grades by the vendor.
 *  For example, an area is a destination of a product.
 *  Examples of areas : JP(Japan), EU, etc.
 */
const CHAR EnvValueArea1[] = "Area1";
const CHAR EnvValueArea2[] = "Area2";
const CHAR EnvValueArea3[] = "Area3";
const CHAR EnvValueArea4[] = "Area4";
const CHAR EnvValueBrand1[] = "Brand1";
const CHAR EnvValueBrand2[] = "Brand2";
const CHAR EnvValueGrade1[] = "Grade1";
const CHAR EnvValueGrade2[] = "Grade2";
const CHAR EnvValueGrade3[] = "Grade3";

/* Package information */
const CHAR PkgInfoGen2[] = "Gen2";


typedef struct T_SS_SM_CONF_CTX {
  EFrameworkunifiedStatus (*cbRebootNoticeFunc)(HANDLE hApp);
  T_SS_SM_CONF_CTX()
    : cbRebootNoticeFunc(NULL) {
  }
} T_SS_SM_CONF_CTX_INFO;

static T_SS_SM_CONF_CTX_INFO s_confCtx;

/***********************************************************************
 *  registProtectSvcs
 ***********************************************************************/
static void registProtectSvcs(std::vector<std::string> & protectedSvcs) {
  protectedSvcs.push_back(MN_NS_SHAREDMEM);
  protectedSvcs.push_back(MN_NS_NPPSERVICE);
  protectedSvcs.push_back(MN_NS_BACKUPMGR);
  protectedSvcs.push_back(MN_SS_LOGGERSRV);
  protectedSvcs.push_back(MN_COMMUNICATION);
  protectedSvcs.push_back(MN_PS_PSMSHADOW);
  protectedSvcs.push_back(MN_SS_POWERSERVICE);
}

/***********************************************************************
 *  registGroupRelaunchSvcs
 ***********************************************************************/
static void registGroupRelaunchSvcs(
  std::vector<std::string> & groupRelaunchSvcs) {
  groupRelaunchSvcs.push_back(MN_ENFORMSERVICE);
  groupRelaunchSvcs.push_back(MN_NAVIPROXY);
  groupRelaunchSvcs.push_back(MN_AWBPROXY);
  groupRelaunchSvcs.push_back(MN_TFFPROXY);
  groupRelaunchSvcs.push_back(MN_AWNPRIMARY);
  groupRelaunchSvcs.push_back(MN_AWMPRIMARY);
  groupRelaunchSvcs.push_back(MN_AWBPRIMARY);
  groupRelaunchSvcs.push_back(MN_AWTPRIMARY);
}

/***********************************************************************
 *  setEvnVariableForLaunchConfigFile
 ***********************************************************************/
static EFrameworkunifiedStatus setEvnVariableForLaunchConfigFile(std::string f_sEnvFile,
    std::string f_sEnvBrand,
    std::string f_sEnvGrade,
    BOOL f_bIsVup) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  const CHAR VupLaunchConfigFileName[] = "sm_launch_vup.cfg";
  const CHAR LaunchConfigFileName1[] = "sm_launch.cfg";
  const CHAR LaunchConfigFileName2[] = "sm_launch.cfg";

  // If an environment variable is set, that value is used, so set only when it is not set
  if (f_sEnvFile.empty()) {
    const CHAR *l_pLaunchConfigFile = NULL;

    if (f_bIsVup) {
      l_pLaunchConfigFile = VupLaunchConfigFileName;
    } else {
      // Change the file read by Brand and Grade
      if ((f_sEnvBrand == EnvValueBrand2) && (f_sEnvGrade != EnvValueGrade1)) {
        l_pLaunchConfigFile = LaunchConfigFileName1;
      } else {
        l_pLaunchConfigFile = LaunchConfigFileName2;
      }

      std::string cnfPath = "/usr/agl/conf/BS/ss/system_manager/rwdata/";
      cnfPath += l_pLaunchConfigFile;
      if (access(cnfPath.c_str(), F_OK) != 0) {
        l_pLaunchConfigFile = LaunchConfigFileName1;
      }
    }

    if (0 != setenv(LaunchConfigFileNameEnvVariable, l_pLaunchConfigFile, 1)) {
      SS_ASERT_ERRNO(0);
      l_eStatus = eFrameworkunifiedStatusFail;
    }
  }

  return l_eStatus;
}

/***********************************************************************
 *  setEvnVariableForLaunchCus
 ***********************************************************************/
static EFrameworkunifiedStatus setEvnVariableForLaunchCus(std::string f_sEnvArea) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  const CHAR LaunchCusEnvVariable[] = "SM_CONF_LAUNCH_CUS";

  const CHAR *l_pSetValue =
    (EnvValueArea1 == f_sEnvArea || EnvValueArea2 == f_sEnvArea) ?
    EnvValueYes : EnvValueNo;

  if (0 != setenv(LaunchCusEnvVariable, l_pSetValue, 1)) {
    SS_ASERT_ERRNO(0);
    l_eStatus = eFrameworkunifiedStatusFail;
  }

  return l_eStatus;
}

/***********************************************************************
 *  setEvnVariableForLaunchTFF
 ***********************************************************************/
static EFrameworkunifiedStatus setEvnVariableForLaunchTFF(std::string f_sEnvArea) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  const CHAR LaunchTFFEnvVariable[] = "SM_CONF_LAUNCH_TFF";

  const CHAR *l_pSetValue = EnvValueNo;

  BOOL l_bArea1 = (f_sEnvArea == EnvValueArea1) ? TRUE : FALSE;
  BOOL l_bArea2 = (f_sEnvArea == EnvValueArea2) ? TRUE : FALSE;

  if (l_bArea1 || l_bArea2) {
    l_pSetValue = EnvValueYes;
  } else {
    l_pSetValue = EnvValueNo;
  }

  if (0 != setenv(LaunchTFFEnvVariable, l_pSetValue, 1)) {
    SS_ASERT_ERRNO(0);
    l_eStatus = eFrameworkunifiedStatusFail;
  }

  return l_eStatus;
}

/***********************************************************************
 *  getEnvValiavleWithDefault
 ***********************************************************************/
static std::string getEnvValiavleWithDefault(const CHAR *f_pEnvName,
    const CHAR *f_pDefault) {
  std::string l_sReturnValue;
  char buf[VP_MAX_LENGTH];

  VP_GetEnv(f_pEnvName, buf);
  if (buf[0] == '\0') {
    l_sReturnValue = (NULL != f_pDefault) ? f_pDefault : "";
  } else {
    l_sReturnValue = buf;
  }
  return l_sReturnValue;
}


/***********************************************************************
 *  getPkgInfoWithDefault
 ***********************************************************************/
static std::string
getPkgInfoWithDefault(const CHAR *f_pPkgName, const CHAR *f_pDefault){
  std::string l_sReturnValue = (NULL != f_pDefault) ? f_pDefault : "";

  try {
    SSVER_PkgInfo pkgInfo = {0};
    CSSVer cVersion;

    if(!cVersion.empty()){
        EFrameworkunifiedStatus ret = cVersion.getPkgInfo(f_pPkgName, &pkgInfo);
        if((ret == eFrameworkunifiedStatusOK) && ('\0' != pkgInfo.version[0])){
            l_sReturnValue = pkgInfo.version;
        }
    }
  } catch(EFrameworkunifiedStatus ee){
    SS_ASERT(0);
  }

  return l_sReturnValue;
}

/***********************************************************************
 *  setEvnVariableForPkgSeriesInfo
 ***********************************************************************/
static EFrameworkunifiedStatus
setEvnVariableForPkgSeriesInfo(std::string f_sPkgSeriesInfo){
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  const CHAR PkgSeriesEnvVariable[] = "SM_CONF_PKG_SERIES";

  if (f_sPkgSeriesInfo.empty()) {
    SS_ASERT(0);
  }

  if (0 != setenv(PkgSeriesEnvVariable, f_sPkgSeriesInfo.c_str(), 1)) {
    SS_ASERT_ERRNO(0);
    l_eStatus = eFrameworkunifiedStatusFail;
  }

  return l_eStatus;
}

/***********************************************************************
 *  setVersionInfo subfunction(version,date settings)
 ***********************************************************************/
static EFrameworkunifiedStatus setVerInfo_version_date(const char* VerPath) {
  EFrameworkunifiedStatus configRet = eFrameworkunifiedStatusOK;
  CSSVer ver;
  SSVER_PkgInfo pkgInfo;

  bzero(&pkgInfo, sizeof(pkgInfo));
  std::string line;
  std::ifstream verFin(VerPath);
  if (verFin) {
    if (std::getline(verFin, line)) {
      snprintf(pkgInfo.version, sizeof(pkgInfo.version), "%s",
               line.c_str());
    }

    if (std::getline(verFin, line)) {
      snprintf(pkgInfo.date, sizeof(pkgInfo.date), "%s", line.c_str());
    }

    configRet = ver.setPkgInfo(SS_PKG_MAIN_LPF_SI, pkgInfo);
    SS_ASERT(configRet == eFrameworkunifiedStatusOK);
  } else {
    SS_ASERT_ERRNO(0);
  }
  return configRet;
}

/***********************************************************************
 *  Set version information
 ***********************************************************************/
static EFrameworkunifiedStatus setVersionInfo(void) {
  EFrameworkunifiedStatus configRet = eFrameworkunifiedStatusOK;

  try {
    CSSVer ver;
    SSVER_PkgInfo pkgInfo;
    const char targetEnv[] = "TARGET_BOARD";

    // HARDWARE
    snprintf(pkgInfo.version, sizeof(pkgInfo.version), "%s",
             getEnvValiavleWithDefault(targetEnv, NULL).c_str());
    pkgInfo.date[0] = '\0';
    configRet = ver.setPkgInfo(SS_PKG_TARGETBOARD, pkgInfo);
    SS_ASERT(configRet == eFrameworkunifiedStatusOK);

    // Set PRODUCT version information
    const char prdVersion[] = "/usr/agl/share/BS/ss/system_manager/rodata/version.txt";
    {
      std::string line;
      std::ifstream prdFin(prdVersion);
      if (prdFin) {
        bzero(&pkgInfo, sizeof(pkgInfo));
        if (std::getline(prdFin, line)) {
          snprintf(pkgInfo.version, sizeof(pkgInfo.version), "%s",
                   line.c_str());
        }
        configRet = ver.setPkgInfo(SS_PKG_MAIN_EMMC, pkgInfo);
        SS_ASERT(configRet == eFrameworkunifiedStatusOK);

        bzero(&pkgInfo, sizeof(pkgInfo));
        if (std::getline(prdFin, line)) {
          snprintf(pkgInfo.version, sizeof(pkgInfo.version), "%s",
                   line.c_str());
        }
        configRet = ver.setPkgInfo(SS_PKG_MAIN_PRODUCT_SI, pkgInfo);
        SS_ASERT(configRet == eFrameworkunifiedStatusOK);
      } else {
        SS_ASERT_ERRNO(0);
      }
    }

    // PHASE_INFO
    const char phaseVersion[] = "/usr/agl/share/BS/ss/system_manager/rodata/PhaseInfo.txt";
    {
      configRet = setVerInfo_version_date(phaseVersion);
    }

  } catch (EFrameworkunifiedStatus ee) {
    SS_ASERT(0);
    configRet = ee;
  }
  return configRet;
}

// If _CWORD83_ common header is added, replace with century definition
#define NTFY_PSMShadowService_Availability MN_PS_PSMSHADOW"/Availability"

EFrameworkunifiedStatus cbPsmNotificationFunc(HANDLE hApp) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (MSG_PSM_REBOOT_NOTIFY != FrameworkunifiedGetMsgProtocol(hApp)) {
    SS_ASERT(0);
    goto ERROR;
  }

  if (NULL != s_confCtx.cbRebootNoticeFunc) {
    l_eStatus = s_confCtx.cbRebootNoticeFunc(hApp);
    if (l_eStatus != eFrameworkunifiedStatusOK) {
      SS_ASERT(0);
      goto ERROR;
    }
  } else {
    SS_ASERT(0);
    goto ERROR;
  }

ERROR:
  return l_eStatus;
}

EFrameworkunifiedStatus cbServiceNotificationFunc(HANDLE hApp) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  PCSTR l_availabilityName;
  BOOL l_serviceAvailability;

  l_availabilityName = FrameworkunifiedGetLastNotification(hApp);
  if (l_availabilityName == NULL) {
    SS_ASERT(0);
    goto ERROR;
  }
  l_serviceAvailability = FrameworkunifiedIsServiceAvailable(hApp);

  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "%s : %s", l_availabilityName,
         l_serviceAvailability ? "TRUE" : "FALSE");

  // Since it is necessary to guarantee SYNC of RAMD at _CWORD71_
  // within 200msec after receiving reboot notice from SYS mcu,
  // do UMOUNT RAMD immediately after receiving reboot notice by SystemManager.
  if ((strcmp(NTFY_PSMShadowService_Availability, l_availabilityName) == 0)
      && (l_serviceAvailability)) {
    l_eStatus = FrameworkunifiedAttachCallbackToDispatcher(hApp, FRAMEWORKUNIFIED_ANY_SOURCE,
                MSG_PSM_REBOOT_NOTIFY,
                cbPsmNotificationFunc);
    if (eFrameworkunifiedStatusOK != l_eStatus) {
      SS_ASERT(0);
      goto ERROR;
    }
  }

ERROR:
  return l_eStatus;
}

/***********************************************************************
 *  ss_sm_initHook
 ***********************************************************************/
EFrameworkunifiedStatus ss_sm_initHook(HANDLE hApp, const T_SS_SM_INIT_HOOK_IN_PARAM *inPrm,
                          T_SS_SM_INIT_HOOK_OUT_PARAM *outPrm) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  std::string l_sEnvFile;
  std::string l_sEnvArea;
  std::string l_sEnvBrand;
  std::string l_sEnvGrade;

  std::string l_sPkgSeriesInfo;

  // Register non-recoverable services for OOM Killer
  registProtectSvcs(outPrm->protectedSvcs);

  // Register services for Group Relaunch
  registGroupRelaunchSvcs(outPrm->groupRelaunchSvcs);

  if (NULL == inPrm->cbRebootNoticeFunc) {
    SS_ASERT(0);
  } else {
    s_confCtx.cbRebootNoticeFunc = inPrm->cbRebootNoticeFunc;
  }

  /** Set Version information **/
  if (eFrameworkunifiedStatusOK != setVersionInfo()) {
    SS_ASERT(0);
  }

  /** Get environment variable **/

  // APPS_CONFIG_FILENAME (not set by default)
  l_sEnvFile = getEnvValiavleWithDefault(LaunchConfigFileNameEnvVariable, NULL);

  // VEHICLEPARAMETERLIBRARY_AREA
  l_sEnvArea = getEnvValiavleWithDefault(AreaTypeEnvVariable, EnvValueArea1);

  // VEHICLEPARAMETERLIBRARY_BRAND
  l_sEnvBrand = getEnvValiavleWithDefault(BrandTypeEnvVariable, NULL);

  // VEHICLEPARAMETERLIBRARY_grade
  l_sEnvGrade = getEnvValiavleWithDefault(GradeEnvVariable, NULL);


  /** Get Package information **/

  // SERIES_INFO
  l_sPkgSeriesInfo = getPkgInfoWithDefault(SS_PKG_SERIES_INFO, NULL);


  /** Set environment variable **/
  // APPS_CONFIG_FILENAME
  if (eFrameworkunifiedStatusOK
      != setEvnVariableForLaunchConfigFile(l_sEnvFile, l_sEnvBrand,
          l_sEnvGrade, inPrm->bIsVupMode)) {
    SS_ASERT(0);
  }

  // SM_CONF_PKG_SERIES
  if (eFrameworkunifiedStatusOK != setEvnVariableForPkgSeriesInfo(l_sPkgSeriesInfo)) {
    SS_ASERT(0);
  }


  // SM_CONF_LAUNCH_CUS
  if (eFrameworkunifiedStatusOK != setEvnVariableForLaunchCus(l_sEnvArea)) {
    SS_ASERT(0);
  }

  // SM_CONF_LAUNCH_TFF
  if (eFrameworkunifiedStatusOK
      != setEvnVariableForLaunchTFF(l_sEnvArea)) {
    SS_ASERT(0);
  }

  if (eFrameworkunifiedStatusOK
      != (l_eStatus = FrameworkunifiedSubscribeNotificationWithCallback(
                        hApp, NTFY_PSMShadowService_Availability, cbServiceNotificationFunc))) {
    FRAMEWORKUNIFIEDLOG(
      ZONE_ERR,
      __FUNCTION__,
      " Error: FrameworkunifiedSubscribeNotificationWithCallback(" "hApp, %s) errored: %d/'%s'",
      NTFY_PSMShadowService_Availability, l_eStatus, GetStr(l_eStatus).c_str());
  }

  return l_eStatus;
}