RASCSI/cpp/monitor/sm_json_report.cpp

93 lines
2.9 KiB
C++
Raw Normal View History

//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Powered by XM6 TypeG Technology.
// Copyright (C) 2016-2020 GIMONS
// Copyright (C) 2021-2022 akuker
//
//---------------------------------------------------------------------------
#include "hal/data_sample_raspberry.h"
#include "shared/log.h"
#include "sm_reports.h"
#include "string.h"
#include <fstream>
#include <iostream>
using namespace std;
const string timestamp_label = "\"timestamp\":\"0x";
const string data_label = "\"data\":\"0x";
uint32_t scsimon_read_json(const string &json_filename, vector<shared_ptr<DataSample>> &data_capture_array)
{
std::ifstream json_file(json_filename);
uint32_t sample_count = 0;
while (json_file) {
string str_buf;
std::getline(json_file, str_buf);
string timestamp;
string data;
uint64_t timestamp_uint;
uint32_t data_uint;
char *ptr;
size_t timestamp_pos = str_buf.find(timestamp_label);
if (timestamp_pos == string::npos)
continue;
timestamp = str_buf.substr(timestamp_pos + timestamp_label.length(), 16);
timestamp_uint = strtoull(timestamp.c_str(), &ptr, 16);
size_t data_pos = str_buf.find(data_label);
if (data_pos == string::npos)
continue;
data = str_buf.substr(data_pos + data_label.length(), 8);
data_uint = static_cast<uint32_t>(strtoul(data.c_str(), &ptr, 16));
// For reading in JSON files, we'll just assume raspberry pi data types
data_capture_array.push_back(make_unique<DataSample_Raspberry>(data_uint, timestamp_uint));
sample_count++;
if (sample_count == UINT32_MAX) {
LOGWARN("Maximum number of samples read. Some data may not be included.")
break;
}
}
Fix simple SonarCloud issues (#834) * Fixing SonarCloud issues, first round * Fixing SonarCLoud issues, next round * Fixing SonarQube issues, next round * Fixed warning * Replaced empty constructors/destructors with = default; * Fixed warning * Replaced new * Use constants instead of macros * Use structured binding declarations * Use init statements for if * Use string views * Use enum class, use using instead of typedef * Fixed more SonarCloud warnings * Replaced redundant/duplicate types with auto * Devlared methods const * Memory management update * Fixed warning * Added missing const * Improved RaScsiResponse memory management * Improved memory management * Improved memory management * Replaced macros by constants, removed unused constants * Made member private * Fixed warning * Added comment * Fixed shadowing warnings * Cleanup * Cleanup * Cleanup * Fixed shadowing warning * Removed unused code * Fixed more warnings * Removed obsolete casts * Fixed warnings * Removed unused field * Removed library not needed by rasctl * Include cleanup * Updated platform check for better compatibility * Improved check for invalid option. This prevents rasctl to break on macos. * Updated option check * Fixed typo * Added TODO * Removed macro * Scope update * Replaced macro * Added TODO, update memory management * Fixed typo * Replaced NULL by nullptr * Use more structured bindings * Added TODO * Use calloc instead of mallco to not need memset * Fixed warnings * Fixed SonarQube initialization issues * Fixed warning * Cleaned up override/virtual/final * Fixed warnings * Constructor update * Fixed tests * Improved memory management * Added missing const * Added const * Fixed two bugs reported by SonarCloud * Fix SonarCloud hotspot * Fixed memory management * Memory management update * Addressing hotspot by using strncpy * Fixed SonarCloud issues * Fixed SonarQube issues * Added missing const * Added const * Added const * Suppress false positive * Added SonarQube suppressions for false positives * Added suppresoin * Fixed code smells * Reverted changes that is a SonarQube issue, but caused problems with -O3 * Removed TODO based on review
2022-09-07 14:38:42 +00:00
json_file.close();
return sample_count;
}
//---------------------------------------------------------------------------
//
// Generate JSON Output File
//
//---------------------------------------------------------------------------
void scsimon_generate_json(const string &filename, const vector<shared_ptr<DataSample>> &data_capture_array)
{
LOGTRACE("Creating JSON file (%s)", filename.c_str())
ofstream json_ofstream;
json_ofstream.open(filename.c_str(), ios::out);
json_ofstream << "[" << endl;
size_t i = 0;
size_t capture_count = data_capture_array.size();
for (auto data : data_capture_array) {
json_ofstream << fmt::format("{{\"id\": \"{0:d}\", \"timestamp\":\"{1:#016x}\", \"data\":\"{2:#08x}\"}}", i,
data->GetTimestamp(), data->GetRawCapture());
if (i != (capture_count - 1)) {
json_ofstream << ",";
}
json_ofstream << endl;
i++;
}
json_ofstream << "]" << endl;
json_ofstream.close();
}