xref: /aosp_15_r20/external/parameter-framework/upstream/test/test-subsystem/TESTSubsystem.cpp (revision c33452fb792a5495ec310a9626f2638b053af5dd)
1*c33452fbSAndroid Build Coastguard Worker /*
2*c33452fbSAndroid Build Coastguard Worker * Copyright (c) 2011-2015, Intel Corporation
3*c33452fbSAndroid Build Coastguard Worker * All rights reserved.
4*c33452fbSAndroid Build Coastguard Worker *
5*c33452fbSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without modification,
6*c33452fbSAndroid Build Coastguard Worker * are permitted provided that the following conditions are met:
7*c33452fbSAndroid Build Coastguard Worker *
8*c33452fbSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright notice, this
9*c33452fbSAndroid Build Coastguard Worker * list of conditions and the following disclaimer.
10*c33452fbSAndroid Build Coastguard Worker *
11*c33452fbSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright notice,
12*c33452fbSAndroid Build Coastguard Worker * this list of conditions and the following disclaimer in the documentation and/or
13*c33452fbSAndroid Build Coastguard Worker * other materials provided with the distribution.
14*c33452fbSAndroid Build Coastguard Worker *
15*c33452fbSAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the names of its contributors
16*c33452fbSAndroid Build Coastguard Worker * may be used to endorse or promote products derived from this software without
17*c33452fbSAndroid Build Coastguard Worker * specific prior written permission.
18*c33452fbSAndroid Build Coastguard Worker *
19*c33452fbSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20*c33452fbSAndroid Build Coastguard Worker * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21*c33452fbSAndroid Build Coastguard Worker * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22*c33452fbSAndroid Build Coastguard Worker * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23*c33452fbSAndroid Build Coastguard Worker * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*c33452fbSAndroid Build Coastguard Worker * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*c33452fbSAndroid Build Coastguard Worker * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26*c33452fbSAndroid Build Coastguard Worker * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*c33452fbSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28*c33452fbSAndroid Build Coastguard Worker * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*c33452fbSAndroid Build Coastguard Worker */
30*c33452fbSAndroid Build Coastguard Worker #include <fstream>
31*c33452fbSAndroid Build Coastguard Worker #include <assert.h>
32*c33452fbSAndroid Build Coastguard Worker #include "TESTSubsystem.h"
33*c33452fbSAndroid Build Coastguard Worker #include "TESTSubsystemBinary.h"
34*c33452fbSAndroid Build Coastguard Worker #include "TESTSubsystemString.h"
35*c33452fbSAndroid Build Coastguard Worker #include "TESTMappingKeys.h"
36*c33452fbSAndroid Build Coastguard Worker #include "SubsystemObjectFactory.h"
37*c33452fbSAndroid Build Coastguard Worker #include <stdlib.h>
38*c33452fbSAndroid Build Coastguard Worker #include <stdio.h>
39*c33452fbSAndroid Build Coastguard Worker 
40*c33452fbSAndroid Build Coastguard Worker #define base CSubsystem
41*c33452fbSAndroid Build Coastguard Worker 
42*c33452fbSAndroid Build Coastguard Worker // Directory for isAlive and NeedResync files
43*c33452fbSAndroid Build Coastguard Worker const char *gacFwNamePropName = getenv("PFW_RESULT");
44*c33452fbSAndroid Build Coastguard Worker 
45*c33452fbSAndroid Build Coastguard Worker // Implementation
CTESTSubsystem(const std::string & strName,core::log::Logger & logger)46*c33452fbSAndroid Build Coastguard Worker CTESTSubsystem::CTESTSubsystem(const std::string &strName, core::log::Logger &logger)
47*c33452fbSAndroid Build Coastguard Worker     : base(strName, logger)
48*c33452fbSAndroid Build Coastguard Worker {
49*c33452fbSAndroid Build Coastguard Worker     // Provide mapping keys to upper layer
50*c33452fbSAndroid Build Coastguard Worker     addContextMappingKey("Directory");
51*c33452fbSAndroid Build Coastguard Worker     addContextMappingKey("Log");
52*c33452fbSAndroid Build Coastguard Worker 
53*c33452fbSAndroid Build Coastguard Worker     // Provide creators to upper layer
54*c33452fbSAndroid Build Coastguard Worker     addSubsystemObjectFactory(
55*c33452fbSAndroid Build Coastguard Worker         new TSubsystemObjectFactory<CTESTSubsystemBinary>("Binary", 1 << ETESTDirectory));
56*c33452fbSAndroid Build Coastguard Worker     addSubsystemObjectFactory(
57*c33452fbSAndroid Build Coastguard Worker         new TSubsystemObjectFactory<CTESTSubsystemString>("String", 1 << ETESTDirectory));
58*c33452fbSAndroid Build Coastguard Worker }
59*c33452fbSAndroid Build Coastguard Worker 
60*c33452fbSAndroid Build Coastguard Worker // Susbsystem sanity health
isAlive() const61*c33452fbSAndroid Build Coastguard Worker bool CTESTSubsystem::isAlive() const
62*c33452fbSAndroid Build Coastguard Worker {
63*c33452fbSAndroid Build Coastguard Worker     assert(gacFwNamePropName != nullptr);
64*c33452fbSAndroid Build Coastguard Worker     return read(std::string(gacFwNamePropName) + "/isAlive") == "true";
65*c33452fbSAndroid Build Coastguard Worker }
66*c33452fbSAndroid Build Coastguard Worker 
67*c33452fbSAndroid Build Coastguard Worker // Resynchronization after subsystem restart needed
needResync(bool bClear)68*c33452fbSAndroid Build Coastguard Worker bool CTESTSubsystem::needResync(bool bClear)
69*c33452fbSAndroid Build Coastguard Worker {
70*c33452fbSAndroid Build Coastguard Worker     assert(gacFwNamePropName != nullptr);
71*c33452fbSAndroid Build Coastguard Worker     std::string strNeedResyncFile = std::string(gacFwNamePropName) + "/needResync";
72*c33452fbSAndroid Build Coastguard Worker     bool bNeedResync;
73*c33452fbSAndroid Build Coastguard Worker 
74*c33452fbSAndroid Build Coastguard Worker     bNeedResync = read(strNeedResyncFile) == "true";
75*c33452fbSAndroid Build Coastguard Worker 
76*c33452fbSAndroid Build Coastguard Worker     if (!bNeedResync) {
77*c33452fbSAndroid Build Coastguard Worker 
78*c33452fbSAndroid Build Coastguard Worker         // subsystem does not need resync
79*c33452fbSAndroid Build Coastguard Worker         return false;
80*c33452fbSAndroid Build Coastguard Worker     } else {
81*c33452fbSAndroid Build Coastguard Worker         // subsystem needs resync
82*c33452fbSAndroid Build Coastguard Worker         // If indicated, clear need resync state
83*c33452fbSAndroid Build Coastguard Worker         if (bClear) {
84*c33452fbSAndroid Build Coastguard Worker 
85*c33452fbSAndroid Build Coastguard Worker             write(strNeedResyncFile, "false");
86*c33452fbSAndroid Build Coastguard Worker         }
87*c33452fbSAndroid Build Coastguard Worker 
88*c33452fbSAndroid Build Coastguard Worker         return true;
89*c33452fbSAndroid Build Coastguard Worker     }
90*c33452fbSAndroid Build Coastguard Worker }
91*c33452fbSAndroid Build Coastguard Worker 
92*c33452fbSAndroid Build Coastguard Worker // Read boolean from file
read(const std::string & strFileName)93*c33452fbSAndroid Build Coastguard Worker std::string CTESTSubsystem::read(const std::string &strFileName)
94*c33452fbSAndroid Build Coastguard Worker {
95*c33452fbSAndroid Build Coastguard Worker     std::ifstream file;
96*c33452fbSAndroid Build Coastguard Worker     std::string strContent;
97*c33452fbSAndroid Build Coastguard Worker 
98*c33452fbSAndroid Build Coastguard Worker     file.open(strFileName.c_str());
99*c33452fbSAndroid Build Coastguard Worker 
100*c33452fbSAndroid Build Coastguard Worker     file >> strContent;
101*c33452fbSAndroid Build Coastguard Worker 
102*c33452fbSAndroid Build Coastguard Worker     return strContent;
103*c33452fbSAndroid Build Coastguard Worker }
104*c33452fbSAndroid Build Coastguard Worker 
105*c33452fbSAndroid Build Coastguard Worker // Write boolean to file
write(const std::string & strFileName,const std::string & strContent)106*c33452fbSAndroid Build Coastguard Worker void CTESTSubsystem::write(const std::string &strFileName, const std::string &strContent)
107*c33452fbSAndroid Build Coastguard Worker {
108*c33452fbSAndroid Build Coastguard Worker     std::ofstream file;
109*c33452fbSAndroid Build Coastguard Worker 
110*c33452fbSAndroid Build Coastguard Worker     file.open(strFileName.c_str());
111*c33452fbSAndroid Build Coastguard Worker 
112*c33452fbSAndroid Build Coastguard Worker     assert(file.is_open());
113*c33452fbSAndroid Build Coastguard Worker 
114*c33452fbSAndroid Build Coastguard Worker     file << strContent;
115*c33452fbSAndroid Build Coastguard Worker }
116