2022-01-07 18:17:44 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-01-07 18:17:44 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Powered by XM6 TypeG Technology.
|
|
|
|
// Copyright (C) 2016-2020 GIMONS
|
2022-12-03 04:20:27 +00:00
|
|
|
// Copyright (C) 2021-2022 akuker
|
2022-01-07 18:17:44 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
#include "hal/data_sample_raspberry.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "hal/log.h"
|
2022-01-07 18:17:44 +00:00
|
|
|
#include "sm_reports.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include <fstream>
|
2022-12-03 04:20:27 +00:00
|
|
|
#include <iostream>
|
2022-11-10 06:44:06 +00:00
|
|
|
|
2022-01-07 18:17:44 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
const string timestamp_label = "\"timestamp\":\"0x";
|
|
|
|
const string data_label = "\"data\":\"0x";
|
2022-01-07 18:17:44 +00:00
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
uint32_t scsimon_read_json(const string &json_filename, vector<shared_ptr<DataSample>> &data_capture_array)
|
2022-01-07 18:17:44 +00:00
|
|
|
{
|
2022-12-03 04:20:27 +00:00
|
|
|
std::ifstream json_file(json_filename);
|
2022-10-08 17:26:04 +00:00
|
|
|
uint32_t sample_count = 0;
|
2022-01-07 18:17:44 +00:00
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
while (json_file) {
|
|
|
|
string str_buf;
|
|
|
|
std::getline(json_file, str_buf);
|
|
|
|
string timestamp;
|
|
|
|
string data;
|
2022-01-07 18:17:44 +00:00
|
|
|
uint64_t timestamp_uint;
|
|
|
|
uint32_t data_uint;
|
|
|
|
char *ptr;
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
size_t timestamp_pos = str_buf.find(timestamp_label);
|
|
|
|
if (timestamp_pos == string::npos)
|
2022-01-07 18:17:44 +00:00
|
|
|
continue;
|
2022-12-03 04:20:27 +00:00
|
|
|
timestamp = str_buf.substr(timestamp_pos + timestamp_label.length(), 16);
|
|
|
|
timestamp_uint = strtoull(timestamp.c_str(), &ptr, 16);
|
2022-01-07 18:17:44 +00:00
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
size_t data_pos = str_buf.find(data_label);
|
|
|
|
if (data_pos == string::npos)
|
2022-01-07 18:17:44 +00:00
|
|
|
continue;
|
2022-12-03 04:20:27 +00:00
|
|
|
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));
|
2022-01-07 18:17:44 +00:00
|
|
|
|
|
|
|
sample_count++;
|
2022-12-03 04:20:27 +00:00
|
|
|
if (sample_count == UINT32_MAX) {
|
2023-10-15 06:38:15 +00:00
|
|
|
spdlog::warn("Maximum number of samples read. Some data may not be included.");
|
2022-01-07 18:17:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 14:38:42 +00:00
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
json_file.close();
|
2022-01-07 18:17:44 +00:00
|
|
|
|
|
|
|
return sample_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Generate JSON Output File
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
2022-12-03 04:20:27 +00:00
|
|
|
void scsimon_generate_json(const string &filename, const vector<shared_ptr<DataSample>> &data_capture_array)
|
2022-01-07 18:17:44 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
spdlog::trace("Creating JSON file (" + filename + ")");
|
2022-01-07 18:17:44 +00:00
|
|
|
ofstream json_ofstream;
|
2022-12-03 04:20:27 +00:00
|
|
|
json_ofstream.open(filename.c_str(), ios::out);
|
2022-01-07 18:17:44 +00:00
|
|
|
|
|
|
|
json_ofstream << "[" << endl;
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
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());
|
2022-01-07 18:17:44 +00:00
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
if (i != (capture_count - 1)) {
|
2022-01-07 18:17:44 +00:00
|
|
|
json_ofstream << ",";
|
|
|
|
}
|
|
|
|
json_ofstream << endl;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
json_ofstream << "]" << endl;
|
|
|
|
json_ofstream.close();
|
|
|
|
}
|