text/plain
•
2.02 KB
•
96 lines
#include <curl/curl.h>
#include "../src/moon.h"
#include "../src/network.h"
#include "../src/storage.h"
#include "../src/ui.h"
static size_t write_cb(void* ptr, size_t size, size_t nmemb, void* userdata) {
storage_file_t f = (storage_file_t)userdata;
return storage_write(f, ptr, size * nmemb);
}
void network_init(void) {}
bool network_set_enabled(bool enabled) {
wifi_enabled = enabled;
return true;
}
bool network_download_file(const char* url, const char* destination_path) {
if (!url || !destination_path) {
return false;
}
ESP_LOGI("NET", "Downloading %s -> %s\n", url, destination_path);
storage_file_t f = storage_open(destination_path, "w");
if (!f) {
return false;
}
CURL* curl = curl_easy_init();
if (!curl) {
storage_close(f);
return false;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
storage_close(f);
if (res != CURLE_OK) {
ESP_LOGI("NET", "Download failed: %s\n", curl_easy_strerror(res));
storage_remove(destination_path);
return false;
}
return true;
}
bool network_is_connected(void) {
return wifi_enabled;
}
int network_get_rssi(void) {
return 0;
}
static size_t discard_cb(void* ptr, size_t size, size_t nmemb, void* userdata) {
(void)ptr;
(void)userdata;
return size * nmemb;
}
bool network_test_url(const char* url) {
if (!url) {
return false;
}
CURL* curl = curl_easy_init();
if (!curl) {
return false;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, discard_cb);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return (res == CURLE_OK);
}
int network_scan(char ssids[][33], int max_results) {
(void)ssids;
(void)max_results;
return 0;
}