summaryrefslogtreecommitdiff
path: root/agent/sila/powerstate.cpp
blob: d82ad2358d09dd43b484bf62c56fe1c00afd1d2d (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
/**
 * @brief SILA host power state implementation.
 *
 * This file is part of sila-snmp project.
 *
 * Copyright (c) 2022 SILA
 *
 * 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 "data/scalar.hpp"
#include "sila/sila_oid.hpp"
#include "tracing.hpp"

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

#include "snmptrap.hpp"

namespace sila
{
namespace host
{
namespace power
{
namespace state
{
using OID = phosphor::snmp::agent::OID;

static const OID state_oid = SILA_OID(1, 1);
static const OID notify_oid = SILA_OID(0, 1);

// Values specified in the MIB file.
constexpr int UNKNOWN = -1;
constexpr int OFF = 0;
constexpr int ON = 1;

struct State : public phosphor::snmp::data::Scalar<std::string>
{
    static constexpr auto IFACE = "xyz.openbmc_project.State.Host";
    static constexpr auto PATH = "/xyz/openbmc_project/state/host0";

    State() :
        phosphor::snmp::data::Scalar<std::string>(PATH, IFACE,
                                                  "CurrentHostState", "")
    {
    }

    void setValue(value_t& var)
    {
        auto prev = getValue();
        phosphor::snmp::data::Scalar<std::string>::setValue(var);

        auto curr = getValue();
        if (prev != curr)
        {
            DEBUGMSGTL(("sila:powerstate",
                        "Host power state changed: '%s' -> '%s'\n",
                        prev.c_str(), curr.c_str()));

            phosphor::snmp::agent::Trap trap(notify_oid);
            trap.add_field(state_oid, toSNMPValue());
            trap.send();
        }
    }

    int toSNMPValue() const
    {
        auto value = getValue();
        if (value == "xyz.openbmc_project.State.Host.HostState.Running")
        {
            return ON;
        }
        if (value == "xyz.openbmc_project.State.Host.HostState.Off")
        {
            return OFF;
        }

        return UNKNOWN;
    }
};

static State state;

/** @brief Handler for snmp requests */
static int State_snmp_handler(netsnmp_mib_handler* /*handler*/,
                              netsnmp_handler_registration* /*reginfo*/,
                              netsnmp_agent_request_info* reqinfo,
                              netsnmp_request_info* requests)
{
    DEBUGMSGTL(("sila:handle",
                "Processing request (%d) for silaHostPowerState\n",
                reqinfo->mode));

    switch (reqinfo->mode)
    {
        case MODE_GET:
            for (netsnmp_request_info* request = requests; request;
                 request = request->next)
            {
                phosphor::snmp::agent::VariableList::set(request->requestvb,
                                                         state.toSNMPValue());
            }
            break;
    }

    return SNMP_ERR_NOERROR;
}

void init()
{
    DEBUGMSGTL(("sila:init", "Initialize silaHostPowerState\n"));

    state.update();

    netsnmp_register_read_only_instance(netsnmp_create_handler_registration(
        "silaHostPowerState", State_snmp_handler, state_oid.data(),
        state_oid.size(), HANDLER_CAN_RONLY));
}
void destroy()
{
    DEBUGMSGTL(("sila:shutdown", "destroy silaHostPowerState\n"));
    unregister_mib(const_cast<oid*>(state_oid.data()), state_oid.size());
}

} // namespace state
} // namespace power
} // namespace host
} // namespace sila