531b7012bcbc44ddb49dd24bfaedd1fcd6082809
[staging/basesystem.git] / service / system / task_manager / client / libtskmcfg / src / tskm_xml_data.cpp
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 #include "system_service/tskm_xml_data.h"
18
19 #include <boost/algorithm/string.hpp>
20 #include <fstream>
21 #include <iostream>
22 #include <string>
23 #include <list>
24
25 #include "system_service/tskm_svc.h"
26
27 #include "system_service/tskm_svcid.h"
28 #include "tskm_debug.h"
29 #include "tskm_comm.h"
30 #include "tskm_auto_build.h"  // Generated data from XML
31
32 // This size depends on the size of the TM area of CL_Monitor
33 #define TSKM_SVC_ID_MAX_SIZE 1024
34
35 /***********************************************************************
36  *  tskm_initServiceList
37  ***********************************************************************/
38 int tskm_initServiceList(TSKM_SVCS_CTX_t* p_svcs, int iFd) {
39   uint32_t ii;
40
41   p_svcs->svcNum = sizeof(serviceList) / sizeof(TSKM_SVC_CTX_t);
42   p_svcs->svcList = serviceList;
43
44   // It is initialized with the Inotify floppy disk.
45   for (ii = 0; ii < p_svcs->svcNum; ii++) {
46     serviceList[ii].attr = &serviceAttr[ii];
47     serviceList[ii].iFd = iFd;
48     if (serviceList[ii].attr->svcId >= TSKM_SVC_ID_MAX_SIZE) {
49       TSKM_ASSERT(0);
50       return -1;
51     }
52   }
53
54   const char* nfsenv = getenv("AGL_NFS");
55   bool isNfs = (nfsenv && strcmp(nfsenv, "y") == 0) ? true : false;
56
57   // For NFS environments, replace the PATH with CAP with /tmp
58   if (isNfs) {
59     std::list<std::string> capFiles;
60     const std::string capPath("/usr/debug/share/target/cap.lst");
61     std::ifstream fin(capPath.c_str());
62     std::string line;
63     while (fin && std::getline(fin, line)) {
64       std::list<std::string> strList;
65       try {  // throw exception by boost::split
66         boost::split(strList, line, boost::is_any_of("|"));
67         if (!strList.empty()) {
68           if (strList.front()[0] == '/') {  // Only character strings beginning with '/' are considered PATH
69             capFiles.push_back(strList.front().c_str());
70           }
71         }
72       } catch (...) {
73         TSKM_ASSERT(0);
74       }
75     }
76
77     for (ii = 0; ii < p_svcs->svcNum; ii++) {
78       std::string binPath(serviceList[ii].attr->path);
79
80       for (std::list<std::string>::iterator ite = capFiles.begin();
81            ite != capFiles.end(); ite++) {
82         if (binPath == *ite) {
83           std::list<std::string> nodes;
84           try {  // throw exception by boost::split
85             boost::split(nodes, binPath, boost::is_any_of("/"));
86             std::string *p_newPath = new std::string("/tmp/");  // Intent not to free up memory
87             *p_newPath += nodes.back();
88             TSKM_PRINTF(TSKM_LOG_STATE, "EXCHG %s", p_newPath->c_str());
89             serviceList[ii].attr->path = p_newPath->c_str();
90             break;
91           } catch (...) {
92             TSKM_ASSERT(0);
93           }
94         }
95       }
96     }
97   }
98
99   // If there is no socket resource equal to the number of services + 1 (internal control connection), a compilation error occurs
100   TSKM_STATIC_ASSERT(TSKM_COMM_CONNECT_MAX >= (TSKM_SVC_NUM+1));
101
102   return 0;
103 }
104
105 /***********************************************************************
106  *  tskm_initWakeupCtx
107  ***********************************************************************/
108 void tskm_initWakeupCtx(TSKM_GSTEP_CTX_t* p_wakeup, BOOL isVupMode) {
109   memset(p_wakeup, 0, sizeof(*p_wakeup));
110
111   if (isVupMode) {
112     p_wakeup->gstepNum = sizeof(wakeupGstepVup) / sizeof(TSKM_GSTEP_t);
113     p_wakeup->gstep = wakeupGstepVup;
114   } else {
115     p_wakeup->gstepNum = sizeof(wakeupGstep) / sizeof(TSKM_GSTEP_t);
116     p_wakeup->gstep = wakeupGstep;
117   }
118
119   TSKM_PRINTF(TSKM_LOG_STATE, "gstep(wakeup):%d", p_wakeup->gstepNum);
120 }
121
122 /***********************************************************************
123  *  tskm_initDownCtx
124  ***********************************************************************/
125 void tskm_initDownCtx(TSKM_GSTEP_CTX_t* p_down, BOOL isVupMode) {
126   memset(p_down, 0, sizeof(*p_down));
127
128   if (isVupMode) {
129     p_down->gstepNum = sizeof(downGstepVup) / sizeof(TSKM_GSTEP_t);
130     p_down->gstep = downGstepVup;
131   } else {
132     p_down->gstepNum = sizeof(downGstep) / sizeof(TSKM_GSTEP_t);
133     p_down->gstep = downGstep;
134   }
135
136   TSKM_PRINTF(TSKM_LOG_STATE, "gstep(down):%d", p_down->gstepNum);
137 }
138