<commit_before>/**
 * Copyright 2017 Google Inc.
 *
 * 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 <chrono>
#include <cmath>
#include <mutex>

#include "dbuspassive.hpp"

DbusPassive::DbusPassive(
    sdbusplus::bus::bus& bus,
    const std::string& type,
    const std::string& id)
    : ReadInterface(),
      _bus(bus),
      _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this),
      _id(id)
{
    /* Need to get the scale and initial value */
    auto tempBus = sdbusplus::bus::new_default();
    /* service == busname */
    std::string path = GetSensorPath(type, id);
    std::string service = GetService(tempBus, sensorintf, path);

    struct SensorProperties settings;
    GetProperties(tempBus, service, path, &settings);

    _scale = settings.scale;
    _value = settings.value * pow(10, _scale);
    _updated = std::chrono::high_resolution_clock::now();
}

ReadReturn DbusPassive::read(void)
{
    std::lock_guard<std::mutex> guard(_lock);

    struct ReadReturn r = {
        _value,
        _updated
    };

    return r;
}

void DbusPassive::setValue(double value)
{
    std::lock_guard<std::mutex> guard(_lock);

    _value = value;
    _updated = std::chrono::high_resolution_clock::now();
}

int64_t DbusPassive::getScale(void)
{
    return _scale;
}

std::string DbusPassive::getId(void)
{
    return _id;
}

int DbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
{
    namespace sdm = sdbusplus::message;
    auto sdbpMsg = sdm::message(msg);
    DbusPassive* obj = static_cast<DbusPassive*>(usrData);

    std::string msgSensor;
    std::map<std::string, sdm::variant<int64_t>> msgData;
    sdbpMsg.read(msgSensor, msgData);

    if (msgSensor == "xyz.openbmc_project.Sensor.Value")
    {
        auto valPropMap = msgData.find("Value");
        if (valPropMap != msgData.end())
        {
            int64_t rawValue = sdm::variant_ns::get<int64_t>
                               (valPropMap->second);

            double value = rawValue * pow(10, obj->getScale());

#if 0
            std::cerr << "received update: " << value
                      << " for: " << obj->getId()
                      << std::endl;
#endif

            obj->setValue(value);
        }
    }

    return 0;
}<commit_msg>dbus: dbuspassive refactor<commit_after>/**
 * Copyright 2017 Google Inc.
 *
 * 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 <chrono>
#include <cmath>
#include <mutex>

#include "dbuspassive.hpp"

DbusPassive::DbusPassive(
    sdbusplus::bus::bus& bus,
    const std::string& type,
    const std::string& id)
    : ReadInterface(),
      _bus(bus),
      _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this),
      _id(id)
{
    /* Need to get the scale and initial value */
    auto tempBus = sdbusplus::bus::new_default();
    /* service == busname */
    std::string path = GetSensorPath(type, id);
    std::string service = GetService(tempBus, sensorintf, path);

    struct SensorProperties settings;
    GetProperties(tempBus, service, path, &settings);

    _scale = settings.scale;
    _value = settings.value * pow(10, _scale);
    _updated = std::chrono::high_resolution_clock::now();
}

ReadReturn DbusPassive::read(void)
{
    std::lock_guard<std::mutex> guard(_lock);

    struct ReadReturn r = {
        _value,
        _updated
    };

    return r;
}

void DbusPassive::setValue(double value)
{
    std::lock_guard<std::mutex> guard(_lock);

    _value = value;
    _updated = std::chrono::high_resolution_clock::now();
}

int64_t DbusPassive::getScale(void)
{
    return _scale;
}

std::string DbusPassive::getId(void)
{
    return _id;
}

int HandleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
{
    std::string msgSensor;
    std::map<std::string, sdbusplus::message::variant<int64_t>> msgData;

    msg.read(msgSensor, msgData);

    if (msgSensor == "xyz.openbmc_project.Sensor.Value")
    {
        auto valPropMap = msgData.find("Value");
        if (valPropMap != msgData.end())
        {
            int64_t rawValue = sdbusplus::message::variant_ns::get<int64_t>(
                valPropMap->second);

            double value = rawValue * std::pow(10, owner->getScale());

            owner->setValue(value);
        }
    }

    return 0;
}

int DbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
{
    auto sdbpMsg = sdbusplus::message::message(msg);
    DbusPassive* obj = static_cast<DbusPassive*>(usrData);

    return HandleSensorValue(sdbpMsg, obj);
}
{'fraction_non_alphanumeric': 0.12617328519855595, 'fraction_numerical': 0.00776173285198556, 'mean_word_length': 3.3154205607476634, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6232879', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1760, 'n_words': 451}