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