satdump/src-core/common/utils.cpp

331 lines
10 KiB
C++
Raw Permalink Normal View History

2021-03-31 12:16:38 +02:00
#include "utils.h"
#include <cmath>
#include <sstream>
2022-12-27 21:37:31 +01:00
#include "resources.h"
2023-01-12 16:36:27 +01:00
#include <locale>
#include <codecvt>
2021-03-31 12:16:38 +02:00
#include "core/config.h"
#include "common/dsp_source_sink/format_notated.h"
2024-05-24 20:59:18 +02:00
#include <curl/curl.h>
void signed_soft_to_unsigned(int8_t *in, uint8_t *out, int nsamples)
{
for (int i = 0; i < nsamples; i++)
{
out[i] = in[i] + 127;
if (out[i] == 128) // 128 is for erased syms
out[i] = 127;
}
}
std::vector<std::string> splitString(std::string input, char del)
{
std::stringstream stcStream(input);
std::string seg;
std::vector<std::string> segs;
while (std::getline(stcStream, seg, del))
segs.push_back(seg);
return segs;
2022-02-24 19:33:58 +01:00
}
// Return filesize
uint64_t getFilesize(std::string filepath)
2022-02-24 19:33:58 +01:00
{
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
uint64_t fileSize = file.tellg();
2022-02-24 19:33:58 +01:00
file.close();
return fileSize;
2022-03-10 19:56:37 +01:00
}
bool isStringPresent(std::string searched, std::string keyword)
{
std::transform(searched.begin(), searched.end(), searched.begin(), tolower);
std::transform(keyword.begin(), keyword.end(), keyword.begin(), tolower);
auto found_it = searched.find(keyword, 0);
return found_it != std::string::npos;
2022-04-16 08:10:25 +02:00
}
#include "logger.h"
2022-09-19 21:40:41 +02:00
#include "satdump_vars.h"
2024-05-24 20:59:18 +02:00
size_t curl_write_std_string(void *contents, size_t size, size_t nmemb, std::string *s)
2022-04-16 08:10:25 +02:00
{
2024-05-24 20:59:18 +02:00
size_t newLength = size * nmemb;
try
2023-07-17 23:11:13 +02:00
{
2024-05-24 20:59:18 +02:00
s->append((char *)contents, newLength);
2023-07-17 23:11:13 +02:00
}
catch (std::bad_alloc &)
2024-05-24 20:59:18 +02:00
{
return 0;
}
return newLength;
}
2022-04-16 08:10:25 +02:00
2024-05-24 20:59:18 +02:00
int perform_http_request(std::string url_str, std::string &result)
{
CURL *curl;
CURLcode res;
2024-08-24 23:33:49 -04:00
bool ret = 1;
2024-08-24 17:02:49 -04:00
char error_buffer[CURL_ERROR_SIZE] = { 0 };
2022-04-16 08:10:25 +02:00
2024-08-24 23:33:49 -04:00
curl_global_init(CURL_GLOBAL_ALL);
2024-05-24 20:59:18 +02:00
curl = curl_easy_init();
if (curl)
{
2024-08-24 17:02:49 -04:00
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
2024-05-24 20:59:18 +02:00
curl_easy_setopt(curl, CURLOPT_USERAGENT, std::string((std::string) "SatDump/v" + SATDUMP_VERSION).c_str());
curl_easy_setopt(curl, CURLOPT_URL, url_str.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_std_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 100);
2024-08-24 17:02:49 -04:00
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
2024-05-24 20:59:18 +02:00
res = curl_easy_perform(curl);
2022-04-16 08:10:25 +02:00
2024-05-24 20:59:18 +02:00
if (res != CURLE_OK)
2024-08-24 17:02:49 -04:00
{
if(strlen(error_buffer))
logger->error("curl_easy_perform() failed: %s", error_buffer);
else
logger->error("curl_easy_perform() failed: %s", curl_easy_strerror(res));
}
2022-04-16 08:10:25 +02:00
2024-05-24 20:59:18 +02:00
curl_easy_cleanup(curl);
2024-08-24 23:33:49 -04:00
ret = 0;
2022-04-16 08:10:25 +02:00
}
2024-05-24 20:59:18 +02:00
curl_global_cleanup();
return ret;
}
2022-04-16 08:10:25 +02:00
2024-05-24 20:59:18 +02:00
int perform_http_request_post(std::string url_str, std::string &result, std::string post_req)
{
CURL *curl;
CURLcode res;
2024-08-24 23:33:49 -04:00
bool ret = 1;
2024-08-24 17:02:49 -04:00
char error_buffer[CURL_ERROR_SIZE] = { 0 };
2022-04-16 08:10:25 +02:00
2024-05-24 20:59:18 +02:00
curl_global_init(CURL_GLOBAL_ALL);
2023-07-17 23:11:13 +02:00
2024-05-24 20:59:18 +02:00
curl = curl_easy_init();
if (curl)
{
2024-08-24 17:18:30 -04:00
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
2024-05-24 20:59:18 +02:00
curl_easy_setopt(curl, CURLOPT_USERAGENT, std::string((std::string) "SatDump/v" + SATDUMP_VERSION).c_str());
curl_easy_setopt(curl, CURLOPT_URL, url_str.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_req.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_std_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
2024-08-24 17:02:49 -04:00
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
2022-04-16 08:10:25 +02:00
2024-05-24 20:59:18 +02:00
res = curl_easy_perform(curl);
2022-04-16 08:10:25 +02:00
2024-05-24 20:59:18 +02:00
if (res != CURLE_OK)
2024-08-24 17:02:49 -04:00
{
if(strlen(error_buffer))
logger->error("curl_easy_perform() failed: %s", error_buffer);
else
logger->error("curl_easy_perform() failed: %s", curl_easy_strerror(res));
}
2022-09-19 21:40:41 +02:00
2024-05-24 20:59:18 +02:00
curl_easy_cleanup(curl);
ret = 0;
}
curl_global_cleanup();
return ret;
2022-04-16 08:10:25 +02:00
}
2024-07-29 11:40:19 -04:00
std::string timestamp_to_string(double timestamp, bool local)
2022-04-16 08:10:25 +02:00
{
if (timestamp < 0)
timestamp = 0;
2024-07-29 11:40:19 -04:00
2022-04-16 08:10:25 +02:00
time_t tttime = timestamp;
2024-07-29 11:40:19 -04:00
std::tm *timeReadable = (local ? localtime(&tttime) : gmtime(&tttime));
std::stringstream timestamp_string;
std::string timezone_string = "";
if (local)
{
#ifdef _WIN32
size_t tznameSize = 0;
char* tznameBuffer = NULL;
timezone_string = " ";
_get_tzname(&tznameSize, NULL, 0, timeReadable->tm_isdst);
if (tznameSize > 0)
{
if (nullptr != (tznameBuffer = (char*)(malloc(tznameSize))))
{
if (_get_tzname(&tznameSize, tznameBuffer, tznameSize, timeReadable->tm_isdst) == 0)
for (size_t i = 0; i < tznameSize; i++)
if (std::isupper(tznameBuffer[i]))
timezone_string += tznameBuffer[i];
free(tznameBuffer);
}
}
if (timezone_string == " ")
timezone_string = " Local";
#else
timezone_string = " " + std::string(timeReadable->tm_zone);
#endif
}
timestamp_string << std::setfill('0')
<< timeReadable->tm_year + 1900 << "/"
<< std::setw(2) << timeReadable->tm_mon + 1 << "/"
<< std::setw(2) << timeReadable->tm_mday << " "
<< std::setw(2) << timeReadable->tm_hour << ":"
<< std::setw(2) << timeReadable->tm_min << ":"
<< std::setw(2) << timeReadable->tm_sec
<< timezone_string;
return timestamp_string.str();
2022-05-23 01:11:11 +02:00
}
double get_median(std::vector<double> values)
{
2022-07-30 19:53:17 +02:00
if (values.size() == 0)
return 0;
2022-05-23 01:11:11 +02:00
std::sort(values.begin(), values.end());
size_t middle = values.size() / 2;
return values[middle];
}
2022-12-27 21:02:36 +01:00
std::string loadFileToString(std::string path)
{
2022-12-28 00:41:17 +01:00
std::ifstream f(path);
2022-12-27 21:02:36 +01:00
std::string str = std::string(std::istreambuf_iterator<char>{f}, {});
f.close();
return str;
2023-01-13 23:15:55 +01:00
}
2023-01-12 16:36:27 +01:00
std::string ws2s(const std::wstring &wstr)
{
using convert_typeX = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.to_bytes(wstr);
}
std::wstring s2ws(const std::string &str)
{
using convert_typeX = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.from_bytes(str);
}
std::string prepareAutomatedPipelineFolder(time_t timevalue, double frequency, std::string pipeline_name, std::string folder)
{
std::tm *timeReadable = gmtime(&timevalue);
if (folder == "")
2024-02-17 15:27:06 -05:00
{
folder = satdump::config::main_cfg["satdump_directories"]["live_processing_path"]["value"].get<std::string>();
#ifdef __ANDROID__
if (folder == "./live_output")
2024-02-17 15:27:06 -05:00
folder = "/storage/emulated/0/live_output";
#endif
}
std::string timestamp = std::to_string(timeReadable->tm_year + 1900) + "-" +
(timeReadable->tm_mon + 1 > 9 ? std::to_string(timeReadable->tm_mon + 1) : "0" + std::to_string(timeReadable->tm_mon + 1)) + "-" +
(timeReadable->tm_mday > 9 ? std::to_string(timeReadable->tm_mday) : "0" + std::to_string(timeReadable->tm_mday)) + "_" +
(timeReadable->tm_hour > 9 ? std::to_string(timeReadable->tm_hour) : "0" + std::to_string(timeReadable->tm_hour)) + "-" +
(timeReadable->tm_min > 9 ? std::to_string(timeReadable->tm_min) : "0" + std::to_string(timeReadable->tm_min));
2024-02-17 15:27:06 -05:00
std::string output_dir = folder + "/" + timestamp + "_" + pipeline_name + "_" + format_notated(frequency, "Hz");
std::filesystem::create_directories(output_dir);
logger->info("Generated folder name : " + output_dir);
return output_dir;
2024-01-21 21:24:35 +01:00
}
std::string prepareBasebandFileName(double timeValue_precise, uint64_t samplerate, uint64_t frequency)
{
const time_t timevalue = timeValue_precise;
std::tm *timeReadable = gmtime(&timevalue);
std::string timestamp = std::to_string(timeReadable->tm_year + 1900) + "-" +
(timeReadable->tm_mon + 1 > 9 ? std::to_string(timeReadable->tm_mon + 1) : "0" + std::to_string(timeReadable->tm_mon + 1)) + "-" +
(timeReadable->tm_mday > 9 ? std::to_string(timeReadable->tm_mday) : "0" + std::to_string(timeReadable->tm_mday)) + "_" +
(timeReadable->tm_hour > 9 ? std::to_string(timeReadable->tm_hour) : "0" + std::to_string(timeReadable->tm_hour)) + "-" +
(timeReadable->tm_min > 9 ? std::to_string(timeReadable->tm_min) : "0" + std::to_string(timeReadable->tm_min)) + "-" +
(timeReadable->tm_sec > 9 ? std::to_string(timeReadable->tm_sec) : "0" + std::to_string(timeReadable->tm_sec));
if (satdump::config::main_cfg["user_interface"]["recorder_baseband_filename_millis_precision"]["value"].get<bool>())
{
std::ostringstream ss;
double ms_val = fmod(timeValue_precise, 1.0) * 1e3;
ss << "-" << std::fixed << std::setprecision(0) << std::setw(3) << std::setfill('0') << ms_val;
timestamp += ss.str();
}
return timestamp + "_" + std::to_string(samplerate) + "SPS_" + std::to_string(frequency) + "Hz";
2024-01-23 20:25:27 +01:00
}
void hsv_to_rgb(float h, float s, float v, uint8_t *rgb)
{
float out_r, out_g, out_b;
if (s == 0.0f)
{
// gray
out_r = out_g = out_b = v;
rgb[0] = out_r * 255;
rgb[1] = out_g * 255;
rgb[2] = out_b * 255;
return;
}
h = fmod(h, 1.0f) / (60.0f / 360.0f);
int i = (int)h;
float f = h - (float)i;
float p = v * (1.0f - s);
float q = v * (1.0f - s * f);
float t = v * (1.0f - s * (1.0f - f));
switch (i)
{
case 0:
out_r = v;
out_g = t;
out_b = p;
break;
case 1:
out_r = q;
out_g = v;
out_b = p;
break;
case 2:
out_r = p;
out_g = v;
out_b = t;
break;
case 3:
out_r = p;
out_g = q;
out_b = v;
break;
case 4:
out_r = t;
out_g = p;
out_b = v;
break;
case 5:
default:
out_r = v;
out_g = p;
out_b = q;
break;
}
rgb[0] = out_r * 255;
rgb[1] = out_g * 255;
rgb[2] = out_b * 255;
2024-02-17 15:27:06 -05:00
}