2021-03-31 12:16:38 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
#include <cmath>
|
2021-06-28 02:07:42 +02:00
|
|
|
#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
|
|
|
|
2021-12-27 19:18:02 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 02:07:42 +02:00
|
|
|
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
|
2023-11-15 16:08:17 -05:00
|
|
|
uint64_t getFilesize(std::string filepath)
|
2022-02-24 19:33:58 +01:00
|
|
|
{
|
|
|
|
|
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
2023-11-15 16:08:17 -05:00
|
|
|
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 <nng/nng.h>
|
|
|
|
|
#include <nng/supplemental/http/http.h>
|
|
|
|
|
#include "logger.h"
|
2022-09-19 21:40:41 +02:00
|
|
|
#include "satdump_vars.h"
|
|
|
|
|
|
|
|
|
|
#if defined(NNG_OPT_TLS_CONFIG)
|
|
|
|
|
#include <nng/supplemental/tls/tls.h>
|
|
|
|
|
#endif
|
2022-04-16 08:10:25 +02:00
|
|
|
|
|
|
|
|
int perform_http_request(std::string url_str, std::string &result)
|
|
|
|
|
{
|
|
|
|
|
nng_http_client *client;
|
|
|
|
|
nng_url *url;
|
|
|
|
|
nng_aio *aio;
|
|
|
|
|
nng_http_req *req;
|
|
|
|
|
nng_http_res *res;
|
|
|
|
|
int rv;
|
|
|
|
|
|
2023-08-02 14:37:27 +02:00
|
|
|
int return_val = 0;
|
|
|
|
|
|
2023-07-17 23:11:13 +02:00
|
|
|
if (((rv = nng_url_parse(&url, url_str.c_str())) != 0) ||
|
|
|
|
|
((rv = nng_http_client_alloc(&client, url)) != 0))
|
|
|
|
|
{
|
|
|
|
|
if (rv == NNG_ENOTSUP)
|
2023-08-13 12:31:33 +02:00
|
|
|
logger->trace("Protocol not supported!");
|
2022-04-16 08:10:25 +02:00
|
|
|
return 1;
|
2023-07-17 23:11:13 +02:00
|
|
|
}
|
2022-04-16 08:10:25 +02:00
|
|
|
|
2022-09-19 21:40:41 +02:00
|
|
|
// HTTPS
|
|
|
|
|
#if defined(NNG_OPT_TLS_CONFIG)
|
|
|
|
|
nng_tls_config *tls_config;
|
|
|
|
|
nng_tls_config_alloc(&tls_config, NNG_TLS_MODE_CLIENT);
|
|
|
|
|
nng_tls_config_auth_mode(tls_config, NNG_TLS_AUTH_MODE_NONE);
|
|
|
|
|
nng_http_client_set_tls(client, tls_config);
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-07-17 23:11:13 +02:00
|
|
|
if (((rv = nng_http_req_alloc(&req, url)) != 0) ||
|
|
|
|
|
((rv = nng_http_res_alloc(&res)) != 0) ||
|
|
|
|
|
((rv = nng_aio_alloc(&aio, NULL, NULL)) != 0))
|
2022-04-16 08:10:25 +02:00
|
|
|
return 1;
|
|
|
|
|
|
2023-08-02 14:37:27 +02:00
|
|
|
nng_aio_set_timeout(aio, 30000);
|
2023-08-01 19:59:26 +02:00
|
|
|
|
2023-07-17 23:11:13 +02:00
|
|
|
nng_http_req_add_header(req, "User-Agent", std::string("SatDump/v" + (std::string)SATDUMP_VERSION).c_str());
|
2022-04-16 08:10:25 +02:00
|
|
|
|
2023-07-17 23:11:13 +02:00
|
|
|
// Start operation
|
|
|
|
|
nng_http_client_transact(client, req, res, aio);
|
2022-04-16 08:10:25 +02:00
|
|
|
|
|
|
|
|
if (nng_http_res_get_status(res) != NNG_HTTP_STATUS_OK)
|
|
|
|
|
{
|
2023-08-13 12:31:33 +02:00
|
|
|
logger->trace("HTTP Server Responded: %d %s", nng_http_res_get_status(res), nng_http_res_get_reason(res));
|
2022-04-16 08:10:25 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for it to complete.
|
|
|
|
|
nng_aio_wait(aio);
|
|
|
|
|
|
|
|
|
|
if ((rv = nng_aio_result(aio)) != 0)
|
2023-08-01 19:59:26 +02:00
|
|
|
{
|
2023-08-13 12:31:33 +02:00
|
|
|
logger->trace("HTTP Request Error!");
|
2023-08-02 14:37:27 +02:00
|
|
|
return_val = 1;
|
2023-08-01 19:59:26 +02:00
|
|
|
}
|
2022-04-16 08:10:25 +02:00
|
|
|
|
2023-07-17 23:11:13 +02:00
|
|
|
// Load result
|
|
|
|
|
char *data_ptr;
|
|
|
|
|
size_t data_sz = 0;
|
|
|
|
|
nng_http_res_get_data(res, (void **)&data_ptr, &data_sz);
|
|
|
|
|
|
|
|
|
|
result = std::string(data_ptr, &data_ptr[data_sz]);
|
2022-04-16 08:10:25 +02:00
|
|
|
|
2023-07-17 23:11:13 +02:00
|
|
|
// Free everything
|
2022-04-16 08:10:25 +02:00
|
|
|
nng_http_client_free(client);
|
|
|
|
|
nng_aio_free(aio);
|
|
|
|
|
nng_http_res_free(res);
|
|
|
|
|
nng_http_req_free(req);
|
|
|
|
|
|
2022-09-19 21:40:41 +02:00
|
|
|
#if defined(NNG_OPT_TLS_CONFIG)
|
|
|
|
|
nng_tls_config_free(tls_config);
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-08-02 14:37:27 +02:00
|
|
|
return return_val;
|
2022-04-16 08:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string timestamp_to_string(double timestamp)
|
|
|
|
|
{
|
2023-01-11 22:05:42 +01:00
|
|
|
if (timestamp < 0)
|
|
|
|
|
timestamp = 0;
|
2022-04-16 08:10:25 +02:00
|
|
|
time_t tttime = timestamp;
|
|
|
|
|
std::tm *timeReadable = gmtime(&tttime);
|
|
|
|
|
return 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));
|
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);
|
2022-12-27 21:02:36 +01:00
|
|
|
}
|