1 /*
2 // Copyright (c) 2018 Intel 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 #pragma once
17 #include "utils.hpp"
18 
19 #include <sdbusplus/timer.hpp>
20 
21 namespace phosphor
22 {
23 namespace service
24 {
25 
26 static constexpr const char* serviceConfigSrvName =
27     "xyz.openbmc_project.Control.Service.Manager";
28 static constexpr const char* serviceConfigIntfName =
29     "xyz.openbmc_project.Control.Service.Attributes";
30 static constexpr const char* sockAttrIntfName =
31     "xyz.openbmc_project.Control.Service.SocketAttributes";
32 static constexpr const char* srcCfgMgrBasePath =
33     "/xyz/openbmc_project/control/service";
34 static constexpr const char* sockAttrPropPort = "Port";
35 static constexpr const char* srvCfgPropMasked = "Masked";
36 static constexpr const char* srvCfgPropEnabled = "Enabled";
37 static constexpr const char* srvCfgPropRunning = "Running";
38 
39 enum class UpdatedProp
40 {
41     port = 1,
42     maskedState,
43     enabledState,
44     runningState
45 };
46 
47 using VariantType =
48     std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
49                  int16_t, uint16_t, uint8_t, bool,
50                  std::vector<std::tuple<std::string, std::string>>>;
51 
52 class ServiceConfig
53 {
54   public:
55     ServiceConfig(sdbusplus::asio::object_server& srv_,
56                   std::shared_ptr<sdbusplus::asio::connection>& conn_,
57                   const std::string& objPath_, const std::string& baseUnitName,
58                   const std::string& instanceName,
59                   const std::string& serviceObjPath,
60                   const std::string& socketObjPath);
61     ~ServiceConfig() = default;
62 
63     std::shared_ptr<sdbusplus::asio::connection> conn;
64     uint8_t updatedFlag;
65 
66     void stopAndApplyUnitConfig(boost::asio::yield_context yield);
67     void restartUnitConfig(boost::asio::yield_context yield);
68     void startServiceRestartTimer();
69 
70 #ifdef USB_CODE_UPDATE
71     void saveUSBCodeUpdateStateToFile(const bool& maskedState,
72                                       const bool& enabledState);
73     void getUSBCodeUpdateStateFromFile();
74     void setUSBCodeUpdateState(const bool& state);
75 #endif
76 
77   private:
78     sdbusplus::asio::object_server& server;
79     std::shared_ptr<sdbusplus::asio::dbus_interface> srvCfgIface;
80     std::shared_ptr<sdbusplus::asio::dbus_interface> sockAttrIface;
81 
82     bool internalSet = false;
83     std::string objPath;
84     std::string baseUnitName;
85     std::string instanceName;
86     std::string instantiatedUnitName;
87     std::string serviceObjectPath;
88     std::string socketObjectPath;
89     std::string overrideConfDir;
90 
91     // Properties
92     std::string activeState;
93     std::string subState;
94     uint16_t portNum;
95     std::vector<std::string> channelList;
96     std::string protocol;
97     std::string stateValue;
98     bool unitMaskedState = false;
99     bool unitEnabledState = false;
100     bool unitRunningState = false;
101 
102     // dropbear is handled specially because it is a socket-activated service.
103     bool isDropBearService = false;
104     std::string subStateValue;
105 
106     bool isMaskedOut();
107     void registerProperties();
108     void queryAndUpdateProperties();
109     void createSocketOverrideConf();
110     void updateServiceProperties(
111         const boost::container::flat_map<std::string, VariantType>&
112             propertyMap);
113     void updateSocketProperties(
114         const boost::container::flat_map<std::string, VariantType>&
115             propertyMap);
116     std::string getSocketUnitName();
117     std::string getServiceUnitName();
118 };
119 
120 } // namespace service
121 } // namespace phosphor
122