// Copyright 2021 Google LLC
//
// 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 "util.hpp"

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/message.hpp>

#include <cmath>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>

namespace metric_blob
{

using phosphor::logging::log;
using level = phosphor::logging::level;

char controlCharsToSpace(char c)
{
    if (c < 32)
    {
        c = ' ';
    }
    return c;
}

long getTicksPerSec()
{
    return sysconf(_SC_CLK_TCK);
}

std::string readFileThenGrepIntoString(const std::string_view fileName,
                                       const std::string_view grepStr)
{
    std::stringstream ss;
    std::ifstream ifs(fileName.data());
    while (ifs.good())
    {
        std::string line;
        std::getline(ifs, line);
        if (line.find(grepStr) != std::string::npos)
        {
            ss << line;
        }
        if (ifs.good())
            ss << std::endl;
    }
    return ss.str();
}

bool isNumericPath(const std::string_view path, int& value)
{
    size_t p = path.rfind('/');
    if (p == std::string::npos)
    {
        return false;
    }
    int id = 0;
    for (size_t i = p + 1; i < path.size(); ++i)
    {
        const char ch = path[i];
        if (ch < '0' || ch > '9')
            return false;
        else
        {
            id = id * 10 + (ch - '0');
        }
    }
    value = id;
    return true;
}

// Trims all control characters at the end of a string.
std::string trimStringRight(std::string_view s)
{
    std::string ret(s.data());
    while (!ret.empty())
    {
        if (ret.back() <= 32)
            ret.pop_back();
        else
            break;
    }
    return ret;
}

std::string getCmdLine(const int pid)
{
    const std::string& cmdlinePath =
        "/proc/" + std::to_string(pid) + "/cmdline";

    std::string cmdline = readFileThenGrepIntoString(cmdlinePath);
    for (size_t i = 0; i < cmdline.size(); ++i)
    {
        cmdline[i] = controlCharsToSpace(cmdline[i]);
    }

    // Trim empty strings
    cmdline = trimStringRight(cmdline);

    return cmdline;
}

// strtok is used in this function in order to avoid usage of <sstream>.
// However, that would require us to create a temporary std::string.
TcommUtimeStime parseTcommUtimeStimeString(std::string_view content,
                                           const long ticksPerSec)
{
    TcommUtimeStime ret;
    ret.tcomm = "";
    ret.utime = ret.stime = 0;

    const float invTicksPerSec = 1.0f / static_cast<float>(ticksPerSec);

    // pCol now points to the first part in content after content is split by
    // space.
    // This is not ideal,
    std::string temp(content);
    char* pCol = strtok(temp.data(), " ");

    if (pCol != nullptr)
    {
        const int fields[] = {1, 13, 14}; // tcomm, utime, stime
        int fieldIdx = 0;
        for (int colIdx = 0; colIdx < 15; ++colIdx)
        {
            if (fieldIdx < 3 && colIdx == fields[fieldIdx])
            {
                switch (fieldIdx)
                {
                    case 0:
                    {
                        ret.tcomm = std::string(pCol);
                        break;
                    }
                    case 1:
                        [[fallthrough]];
                    case 2:
                    {
                        int ticks = std::atoi(pCol);
                        float t = static_cast<float>(ticks) * invTicksPerSec;

                        if (fieldIdx == 1)
                        {
                            ret.utime = t;
                        }
                        else if (fieldIdx == 2)
                        {
                            ret.stime = t;
                        }
                        break;
                    }
                }
                ++fieldIdx;
            }
            pCol = strtok(nullptr, " ");
        }
    }

    if (ticksPerSec <= 0)
    {
        log<level::ERR>("ticksPerSec is equal or less than zero");
    }

    return ret;
}

TcommUtimeStime getTcommUtimeStime(const int pid, const long ticksPerSec)
{
    const std::string& statPath = "/proc/" + std::to_string(pid) + "/stat";
    return parseTcommUtimeStimeString(readFileThenGrepIntoString(statPath),
                                      ticksPerSec);
}

// Returns true if successfully parsed and false otherwise. If parsing was
// successful, value is set accordingly.
// Input: "MemAvailable:      1234 kB"
// Returns true, value set to 1234
bool parseMeminfoValue(const std::string_view content,
                       const std::string_view keyword, int& value)
{
    size_t p = content.find(keyword);
    if (p != std::string::npos)
    {
        std::string_view v = content.substr(p + keyword.size());
        p = v.find("kB");
        if (p != std::string::npos)
        {
            v = v.substr(0, p);
            value = std::atoi(v.data());
            return true;
        }
    }
    return false;
}

bool parseProcUptime(const std::string_view content, double& uptime,
                     double& idleProcessTime)
{
    double t0, t1; // Attempts to parse uptime and idleProcessTime
    int ret = sscanf(content.data(), "%lf %lf", &t0, &t1);
    if (ret == 2 && std::isfinite(t0) && std::isfinite(t1))
    {
        uptime = t0;
        idleProcessTime = t1;
        return true;
    }
    return false;
}

bool readMem(const uint32_t target, uint32_t& memResult)
{
    int fd = open("/dev/mem", O_RDONLY | O_SYNC);
    if (fd < 0)
    {
        return false;
    }

    int pageSize = getpagesize();
    uint32_t pageOffset = target & ~static_cast<uint32_t>(pageSize - 1);
    uint32_t offsetInPage = target & static_cast<uint32_t>(pageSize - 1);

    void* mapBase =
        mmap(NULL, pageSize * 2, PROT_READ, MAP_SHARED, fd, pageOffset);
    if (mapBase == MAP_FAILED)
    {
        close(fd);
        return false;
    }

    char* virtAddr = reinterpret_cast<char*>(mapBase) + offsetInPage;
    memResult = *(reinterpret_cast<uint32_t*>(virtAddr));
    close(fd);
    munmap(mapBase, pageSize * 2);
    return true;
}

// clang-format off
/*
 *  power-on
 *  counter(start)                 uptime(start)
 *  firmware(Neg)    loader(Neg)   kernel(always 0)    initrd                 userspace              finish
 *  |----------------|-------------|-------------------|----------------------|----------------------|
 *  |----------------| <--- firmwareTime=firmware-loader
 *                   |-------------| <--- loaderTime=loader
 *  |------------------------------| <--- firmwareTime(Actually is firmware+loader)=counter-uptime \
 *                                        (in this case we can treat this as firmware time \
 *                                         since firmware consumes most of the time)
 *                                 |-------------------| <--- kernelTime=initrd (if initrd present)
 *                                 |------------------------------------------| <--- kernelTime=userspace (if no initrd)
 *                                                     |----------------------| <--- initrdTime=userspace-initrd (if initrd present)
 *                                                                            |----------------------| <--- userspaceTime=finish-userspace
 */
// clang-format on
bool getBootTimesMonotonic(BootTimesMonotonic& btm)
{
    // Timestamp name and its offset in the struct.
    std::vector<std::pair<std::string_view, size_t>> timeMap = {
        {"FirmwareTimestampMonotonic",
         offsetof(BootTimesMonotonic, firmwareTime)}, // negative value
        {"LoaderTimestampMonotonic",
         offsetof(BootTimesMonotonic, loaderTime)},   // negative value
        {"InitRDTimestampMonotonic", offsetof(BootTimesMonotonic, initrdTime)},
        {"UserspaceTimestampMonotonic",
         offsetof(BootTimesMonotonic, userspaceTime)},
        {"FinishTimestampMonotonic", offsetof(BootTimesMonotonic, finishTime)}};

    auto b = sdbusplus::bus::new_default_system();
    auto m = b.new_method_call("org.freedesktop.systemd1",
                               "/org/freedesktop/systemd1",
                               "org.freedesktop.DBus.Properties", "GetAll");
    m.append("");
    auto reply = b.call(m);
    std::vector<std::pair<std::string, std::variant<uint64_t>>> timestamps;
    reply.read(timestamps);

    // Parse timestamps from dbus result.
    auto btmPtr = reinterpret_cast<char*>(&btm);
    unsigned int recordCnt = 0;
    for (auto& t : timestamps)
    {
        for (auto& tm : timeMap)
        {
            if (tm.first.compare(t.first) == 0)
            {
                auto temp = std::get<uint64_t>(t.second);
                memcpy(btmPtr + tm.second, reinterpret_cast<char*>(&temp),
                       sizeof(temp));
                recordCnt++;
                break;
            }
        }
        if (recordCnt == timeMap.size())
        {
            break;
        }
    }
    if (recordCnt != timeMap.size())
    {
        log<level::ERR>("Didn't get desired timestamps");
        return false;
    }

    std::string cpuinfo =
        readFileThenGrepIntoString("/proc/cpuinfo", "Hardware");
    // Nuvoton NPCM7XX chip has a counter which starts from power-on.
    if (cpuinfo.find("NPCM7XX") != std::string::npos)
    {
        // Get elapsed seconds from SEC_CNT register
        const uint32_t SEC_CNT_ADDR = 0xf0801068;
        uint32_t memResult = 0;
        if (readMem(SEC_CNT_ADDR, memResult))
        {
            btm.powerOnSecCounterTime = static_cast<uint64_t>(memResult);
        }
        else
        {
            log<level::ERR>("Read memory SEC_CNT_ADDR(0xf0801068) failed");
            return false;
        }
    }

    return true;
}

bool getECCErrorCounts(EccCounts& eccCounts)
{
    std::vector<
        std::pair<std::string, std::variant<uint64_t, uint8_t, std::string>>>
        values;
    try
    {
        auto bus = sdbusplus::bus::new_default_system();
        auto m =
            bus.new_method_call("xyz.openbmc_project.memory.ECC",
                                "/xyz/openbmc_project/metrics/memory/BmcECC",
                                "org.freedesktop.DBus.Properties", "GetAll");
        m.append("xyz.openbmc_project.Memory.MemoryECC");
        auto reply = bus.call(m);
        reply.read(values);
    }
    catch (const sdbusplus::exception::SdBusError& ex)
    {
        return false;
    }
    bool hasCorrectable = false;
    bool hasUncorrectable = false;
    for (const auto& [key, value] : values)
    {
        if (key == "ceCount")
        {
            eccCounts.correctableErrCount =
                static_cast<int32_t>(std::get<uint64_t>(value));
            hasCorrectable = true;
            if (hasUncorrectable)
            {
                return true;
            }
        }
        if (key == "ueCount")
        {
            eccCounts.uncorrectableErrCount =
                static_cast<int32_t>(std::get<uint64_t>(value));
            hasUncorrectable = true;
            if (hasCorrectable)
            {
                return true;
            }
        }
    }
    return false;
}

} // namespace metric_blob
