Remove FTP, and fix Download speed crash...?

This commit is contained in:
StackZ
2020-06-29 01:37:52 +02:00
parent 15602637cc
commit 1e2afdaf96
10 changed files with 60 additions and 4552 deletions
+1 -1
View File
@@ -30,7 +30,7 @@
#include "common.hpp"
#define APP_TITLE "Universal-Updater"
#define VERSION_STRING "2.5.0"
#define VERSION_STRING "2.5.1"
// The Release Fetch struct.
struct ReleaseFetch {
-47
View File
@@ -1,47 +0,0 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#ifndef _UNIVERSAL_UPDATER_FTP_SCREEN_HPP
#define _UNIVERSAL_UPDATER_FTP_SCREEN_HPP
#include "common.hpp"
#include "structs.hpp"
#include <vector>
class FTPScreen : public Screen {
public:
void Draw(void) const override;
void Logic(u32 hDown, u32 hHeld, touchPosition touch) override;
private:
int ftpEnabled = 1;
const std::vector<Structs::ButtonPos> arrowPos = {
{0, 215, 25, 25} // Back Arrow.
};
};
#endif
-39
View File
@@ -1,39 +0,0 @@
#pragma once
#ifdef _3DS
#include <3ds.h>
#define ESC(x) "\x1b[" #x
#define RESET ESC(0m)
#define BLACK ESC(30m)
#define RED ESC(31;1m)
#define GREEN ESC(32;1m)
#define YELLOW ESC(33;1m)
#define BLUE ESC(34;1m)
#define MAGENTA ESC(35;1m)
#define CYAN ESC(36;1m)
#define WHITE ESC(37;1m)
#else
#define ESC(x)
#define RESET
#define BLACK
#define RED
#define GREEN
#define YELLOW
#define BLUE
#define MAGENTA
#define CYAN
#define WHITE
#endif
void console_init(void);
__attribute__((format(printf,1,2)))
void console_set_status(const char *fmt, ...);
__attribute__((format(printf,1,2)))
void console_print(const char *fmt, ...);
__attribute__((format(printf,1,2)))
void debug_print(const char *fmt, ...);
void console_render(void);
-18
View File
@@ -1,18 +0,0 @@
#pragma once
#include <3ds.h>
/*! Loop status */
typedef enum
{
LOOP_CONTINUE, /*!< Continue looping */
LOOP_RESTART, /*!< Reinitialize */
LOOP_EXIT, /*!< Terminate looping */
} loop_status_t;
extern bool isTransfering;
extern char ftp_accepted_connection[50], ftp_file_transfer[100];
int ftp_init(void);
loop_status_t ftp_loop(void);
void ftp_exit(void);
+41 -29
View File
@@ -69,9 +69,10 @@ extern u32 selected;
extern u32 unselected;
CURL *hnd; // Needed to display download speed properly?
CURLcode curlResult;
curl_off_t downloadTotal = 1; //Dont initialize with 0 to avoid division by zero later
curl_off_t downloadNow = 0;
curl_off_t downloadSpeed = 0;
static FILE *downfile = NULL;
static size_t file_buffer_pos = 0;
@@ -152,12 +153,9 @@ static size_t file_handle_data(char *ptr, size_t size, size_t nmemb, void *userd
}
Result downloadToFile(std::string url, std::string path) {
Result retcode = 0;
downloadTotal = 1;
downloadNow = 0;
int res;
CURLcode cres;
printf("Downloading from:\n%s\nto:\n%s\n", url.c_str(), path.c_str());
const char* filepath = path.c_str();
@@ -197,12 +195,11 @@ Result downloadToFile(std::string url, std::string path) {
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_STDERR, stdout);
cres = curl_easy_perform(hnd);
downloadSpeed = 0;
curlResult = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
if (cres != CURLE_OK) {
retcode = -cres;
if (curlResult != CURLE_OK) {
retcode = -curlResult;
goto exit;
}
@@ -216,6 +213,7 @@ Result downloadToFile(std::string url, std::string path) {
retcode = -3;
goto exit;
}
fflush(downfile);
exit:
@@ -232,23 +230,26 @@ exit:
if (socubuf) {
free(socubuf);
}
if (downfile) {
fclose(downfile);
downfile = NULL;
}
if (g_buffers[0]) {
free(g_buffers[0]);
g_buffers[0] = NULL;
}
if (g_buffers[1]) {
free(g_buffers[1]);
g_buffers[1] = NULL;
}
g_index = 0;
file_buffer_pos = 0;
file_toCommit_size = 0;
writeError = false;
return retcode;
}
@@ -456,16 +457,16 @@ int SelectRelease(std::vector<ReleaseFetch> bruh) {
if (hDown & KEY_TOUCH) {
if (config->viewMode() == 0) {
for(int i=0;i<ENTRIES_PER_SCREEN && i<(int)bruh.size(); i++) {
if(touch.py > 40+(i*57) && touch.py < 40+(i*57)+45) {
for(int i = 0; i < ENTRIES_PER_SCREEN && i < (int)bruh.size(); i++) {
if (touch.py > 40+(i*57) && touch.py < 40+(i*57)+45) {
if (bruh.size() != 0) {
return screenPos + i;
}
}
}
} else if (config->viewMode() == 1) {
for(int i=0;i<ENTRIES_PER_LIST && i<(int)bruh.size(); i++) {
if(touch.py > (i+1)*27 && touch.py < (i+2)*27) {
for(int i = 0; i < ENTRIES_PER_LIST && i < (int)bruh.size(); i++) {
if (touch.py > (i+1)*27 && touch.py < (i+2)*27) {
if (bruh.size() != 0) {
return screenPosList + i;
}
@@ -475,13 +476,13 @@ int SelectRelease(std::vector<ReleaseFetch> bruh) {
}
if (config->viewMode() == 0) {
if(selectedRelease < screenPos) {
if (selectedRelease < screenPos) {
screenPos = selectedRelease;
} else if (selectedRelease > screenPos + ENTRIES_PER_SCREEN - 1) {
screenPos = selectedRelease - ENTRIES_PER_SCREEN + 1;
}
} else if (config->viewMode() == 1) {
if(selectedRelease < screenPosList) {
if (selectedRelease < screenPosList) {
screenPosList = selectedRelease;
} else if (selectedRelease > screenPosList + ENTRIES_PER_LIST - 1) {
screenPosList = selectedRelease - ENTRIES_PER_LIST + 1;
@@ -606,6 +607,7 @@ Result downloadFromRelease(std::string url, std::string asset, std::string path,
result_buf = NULL;
result_sz = 0;
result_written = 0;
if (assetUrl.empty()) {
ret = DL_ERROR_GIT;
} else {
@@ -839,22 +841,32 @@ std::string getLatestCommit(std::string repo, std::string array, std::string ite
void displayProgressBar() {
char str[256];
while(showProgressBar) {
if (downloadTotal < 1.0f) {
downloadTotal = 1.0f;
}
if (downloadTotal < downloadNow) {
downloadTotal = downloadNow;
}
if (progressbarType == ProgressBar::Downloading) curl_easy_getinfo(hnd, CURLINFO_SPEED_DOWNLOAD_T, &downloadSpeed); // Get download speed.
switch(progressbarType) {
case ProgressBar::Downloading:
snprintf(str, sizeof(str), "%s / %s (%.2f%%) \n\n\n\n\n %s %lld %s",
formatBytes(downloadNow).c_str(),
formatBytes(downloadTotal).c_str(),
((float)downloadNow/(float)downloadTotal) * 100.0f,
Lang::get("DOWNLOAD_SPEED").c_str(), (downloadSpeed / 1000), Lang::get("KB_PER_SECOND").c_str());
if (downloadTotal < 1.0f) {
downloadTotal = 1.0f;
}
if (downloadTotal < downloadNow) {
downloadTotal = downloadNow;
}
if (!curlResult) {
curl_off_t speed;
curlResult = curl_easy_getinfo(hnd, CURLINFO_SPEED_DOWNLOAD_T, &speed);
if (!curlResult) {
snprintf(str, sizeof(str), "%s / %s (%.2f%%) \n\n\n\n\n %s %lld %s",
formatBytes(downloadNow).c_str(),
formatBytes(downloadTotal).c_str(),
((float)downloadNow/(float)downloadTotal) * 100.0f,
Lang::get("DOWNLOAD_SPEED").c_str(), (speed / 1000), Lang::get("KB_PER_SECOND").c_str());
} else {
snprintf(str, sizeof(str), "%s / %s (%.2f%%) \n\n\n\n\n %s",
formatBytes(downloadNow).c_str(),
formatBytes(downloadTotal).c_str(),
((float)downloadNow/(float)downloadTotal) * 100.0f);
}
}
break;
case ProgressBar::Extracting:
snprintf(str, sizeof(str), "%s / %s (%.2f%%)",
-97
View File
@@ -1,97 +0,0 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 Universal-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "ftpScreen.hpp"
#include <algorithm>
#include <fstream>
#include <unistd.h>
extern "C" {
#include "ftp.h"
}
extern std::unique_ptr<Config> config;
extern bool touching(touchPosition touch, Structs::ButtonPos button);
extern touchPosition touch;
void FTPScreen::Draw(void) const {
ftp_init();
Result ret = 0;
char buf[137], hostname[128];
u32 wifiStatus = 0;
ret = gethostname(hostname, sizeof(hostname));
while(ftpEnabled == 1) {
ftp_loop();
Gui::clearTextBufs();
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
GFX::DrawTop();
Gui::DrawStringCentered(0, config->useBars() ? 0 : 2, 0.7f, config->textColor(), Lang::get("FTP_MODE"), 400);
if (fadealpha > 0) Gui::Draw_Rect(0, 0, 400, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha)); // Fade in/out effect
GFX::DrawBottom();
GFX::DrawArrow(0, 218, 0, 1);
ret = ACU_GetWifiStatus(&wifiStatus);
if ((wifiStatus != 0) && R_SUCCEEDED(ret)) {
Gui::DrawStringCentered(0, 40, 0.48f, config->textColor(), Lang::get("FTP_INITIALIZED"), 320);
snprintf(buf, 137, "IP: %s:5000", R_FAILED(ret)? Lang::get("FAILED_GET_IP").c_str() : hostname);
if (strlen(ftp_accepted_connection) != 0) {
Gui::DrawStringCentered(0, 80, 0.45f, config->textColor(), ftp_accepted_connection, 320);
}
if (strlen(ftp_file_transfer) != 0) {
Gui::DrawStringCentered(0, 150, 0.45f, config->textColor(), ftp_file_transfer, 320);
}
} else {
Gui::DrawStringCentered(0, 40, 0.48f, config->textColor(), Lang::get("FAILED_INITIALIZE_FTP"), 320);
snprintf(buf, 18, Lang::get("WIFI_NOT_ENABLED").c_str());
}
Gui::DrawStringCentered(0, 60, 0.48, config->textColor(), buf, 320);
Gui::DrawStringCentered(0, 222, 0.48f, config->textColor(), Lang::get("B_FTP_EXIT"), 320);
if (fadealpha > 0) Gui::Draw_Rect(0, 0, 320, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha)); // Fade in/out effect
Gui::clearTextBufs();
C3D_FrameEnd(0);
hidScanInput();
hidTouchRead(&touch);
u32 hDown = hidKeysDown();
if ((hDown & KEY_B) || (hDown & KEY_TOUCH && touching(touch, arrowPos[0])))
break;
}
memset(ftp_accepted_connection, 0, 20); // Empty accepted connection address.
memset(ftp_file_transfer, 0, 50); // Empty transfer status.
ftp_exit();
Gui::screenBack(false);
return;
}
// Needed here, otherwise it won't compile.
void FTPScreen::Logic(u32 hDown, u32 hHeld, touchPosition touch) { }
+4 -4
View File
@@ -25,7 +25,7 @@
*/
#include "config.hpp"
#include "ftpScreen.hpp"
#include "credits.hpp"
#include "mainMenu.hpp"
#include "scriptHelper.hpp"
#include "scriptlist.hpp"
@@ -51,7 +51,7 @@ void MainMenu::Draw(void) const {
GFX::DrawButton(mainButtons[0].x, mainButtons[0].y, "UniStore");
GFX::DrawButton(mainButtons[1].x, mainButtons[1].y, Lang::get("SCRIPTS"));
GFX::DrawButton(mainButtons[2].x, mainButtons[2].y, Lang::get("SETTINGS"));
GFX::DrawButton(mainButtons[3].x, mainButtons[3].y, "FTP");
GFX::DrawButton(mainButtons[3].x, mainButtons[3].y, Lang::get("CREDITS"));
// Selector.
Animation::Button(mainButtons[Selection].x, mainButtons[Selection].y, .060);
@@ -90,7 +90,7 @@ void MainMenu::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
Gui::setScreen(std::make_unique<Settings>(), config->screenFade(), true);
break;
case 3:
Gui::setScreen(std::make_unique<FTPScreen>(), false, true);
Gui::setScreen(std::make_unique<Credits>(), config->screenFade(), true);
break;
}
}
@@ -103,7 +103,7 @@ void MainMenu::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
} else if (touching(touch, mainButtons[2])) {
Gui::setScreen(std::make_unique<Settings>(), config->screenFade(), true);
} else if (touching(touch, mainButtons[3])) {
Gui::setScreen(std::make_unique<FTPScreen>(), false, true);
Gui::setScreen(std::make_unique<Credits>(), config->screenFade(), true);
}
}
}
+14 -34
View File
@@ -24,7 +24,6 @@
* reasonable ways as different from the original version.
*/
#include "credits.hpp"
#include "keyboard.hpp"
#include "settings.hpp"
@@ -59,7 +58,7 @@ void Settings::DrawSubMenu(void) const {
GFX::DrawButton(mainButtons[0].x, mainButtons[0].y, Lang::get("LANGUAGE"));
GFX::DrawButton(mainButtons[1].x, mainButtons[1].y, Lang::get("COLORS"));
GFX::DrawButton(mainButtons[2].x, mainButtons[2].y, Lang::get("CREDITS"));
GFX::DrawButton(mainButtons[2].x, mainButtons[2].y, Lang::get("CHANGE_BAR_STYLE"));
// Selector.
Animation::Button(mainButtons[Selection].x, mainButtons[Selection].y, .060);
if (fadealpha > 0) Gui::Draw_Rect(0, 0, 320, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha)); // Fade in/out effect
@@ -97,6 +96,7 @@ void Settings::DrawLanguageSelection(void) const {
Gui::DrawStringCentered(0, 50+(i*57), 0.7f, config->textColor(), line1, 320);
}
if (fadealpha > 0) Gui::Draw_Rect(0, 0, 320, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha)); // Fade in/out effect
}
@@ -215,6 +215,7 @@ void Settings::DrawColorChanging(void) const {
GFX::DrawButton(mainButtons[2].x, mainButtons[2].y, ColorHelper::getColorName(config->buttonColor(), 0).c_str(), C2D_Color32(0, 0, 255, 255));
}
}
if (fadealpha > 0) Gui::Draw_Rect(0, 0, 320, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha)); // Fade in/out effect
}
@@ -226,9 +227,8 @@ void Settings::DrawMiscSettings(void) const {
GFX::DrawArrow(0, 218, 0, 1);
GFX::DrawButton(mainButtons2[0].x, mainButtons2[0].y, Lang::get("CHANGE_MUSICFILE"));
GFX::DrawButton(mainButtons2[1].x, mainButtons2[1].y, Lang::get("CHANGE_BAR_STYLE"));
GFX::DrawButton(mainButtons2[2].x, mainButtons2[2].y, Lang::get("CHANGE_KEY_DELAY"));
GFX::DrawButton(mainButtons2[3].x, mainButtons2[3].y, Lang::get("TOGGLE_FADE"));
GFX::DrawButton(mainButtons2[1].x, mainButtons2[1].y, Lang::get("CHANGE_KEY_DELAY"));
GFX::DrawButton(mainButtons2[2].x, mainButtons2[2].y, Lang::get("TOGGLE_FADE"));
// Selector.
Animation::Button(mainButtons2[Selection].x, mainButtons2[Selection].y, .060);
@@ -241,27 +241,19 @@ void Settings::MiscSettingsLogic(u32 hDown, u32 hHeld, touchPosition touch) {
std::string tempMusic = selectFilePath(Lang::get("SELECT_MUSIC_FILE"), "sdmc:/", {"wav"}, 2);
if (tempMusic != "") {
config->musicPath(tempMusic);
changesMade = true;
}
} else if (Selection == 1) {
if (config->useBars() == true) config->useBars(false);
else if (config->useBars() == false) config->useBars(true);
changesMade = true;
} else if (Selection == 2) {
config->keyDelay(Input::getUint(255, Lang::get("ENTER_KEY_DELAY")));
changesMade = true;
} else if (Selection == 3) {
} else if (Selection == 2) {
if (config->screenFade()) {
if (Msg::promptMsg(Lang::get("TOGGLE_FADE_DISABLE"))) {
config->screenFade(false);
Msg::DisplayWarnMsg(Lang::get("DISABLED"));
changesMade = true;
}
} else {
if (Msg::promptMsg(Lang::get("TOGGLE_FADE_ENABLE"))) {
config->screenFade(true);
Msg::DisplayWarnMsg(Lang::get("ENABLED"));
changesMade = true;
}
}
}
@@ -272,27 +264,19 @@ void Settings::MiscSettingsLogic(u32 hDown, u32 hHeld, touchPosition touch) {
std::string tempMusic = selectFilePath(Lang::get("SELECT_MUSIC_FILE"), "sdmc:/", {"wav"}, 2);
if (tempMusic != "") {
config->musicPath(tempMusic);
changesMade = true;
}
} else if (touching(touch, mainButtons2[1])) {
if (config->useBars() == true) config->useBars(false);
else if (config->useBars() == false) config->useBars(true);
changesMade = true;
} else if (touching(touch, mainButtons2[2])) {
config->keyDelay(Input::getUint(255, Lang::get("ENTER_KEY_DELAY")));
changesMade = true;
} else if (touching(touch, mainButtons2[3])) {
} else if (touching(touch, mainButtons2[2])) {
if (config->screenFade()) {
if (Msg::promptMsg(Lang::get("TOGGLE_FADE_DISABLE"))) {
config->screenFade(false);
Msg::DisplayWarnMsg(Lang::get("DISABLED"));
changesMade = true;
}
} else {
if (Msg::promptMsg(Lang::get("TOGGLE_FADE_ENABLE"))) {
config->screenFade(true);
Msg::DisplayWarnMsg(Lang::get("ENABLED"));
changesMade = true;
}
}
}
@@ -307,15 +291,13 @@ void Settings::MiscSettingsLogic(u32 hDown, u32 hHeld, touchPosition touch) {
if (hDown & KEY_SELECT) {
if (config->progressDisplay()) {
if (Msg::promptMsg(Lang::get("PROGRESS_BAR_DISABLE"))) {
config->progressDisplay(false);
Msg::DisplayWarnMsg(Lang::get("DISABLED"));
changesMade = true;
config->progressDisplay(false);
Msg::DisplayWarnMsg(Lang::get("DISABLED"));
}
} else {
if (Msg::promptMsg(Lang::get("PROGRESS_BAR_ENABLE"))) {
config->progressDisplay(true);
Msg::DisplayWarnMsg(Lang::get("ENABLED"));
changesMade = true;
}
}
}
@@ -354,7 +336,8 @@ void Settings::SubMenuLogic(u32 hDown, u32 hHeld, touchPosition touch) {
mode = 2;
break;
case 2:
Gui::setScreen(std::make_unique<Credits>(), config->screenFade(), true);
if (config->useBars()) config->useBars(false);
else config->useBars(true);
break;
}
}
@@ -368,7 +351,8 @@ void Settings::SubMenuLogic(u32 hDown, u32 hHeld, touchPosition touch) {
screenPos = 0;
mode = 2;
} else if (touching(touch, mainButtons[2])) {
Gui::setScreen(std::make_unique<Credits>(), config->screenFade(), true);
if (config->useBars()) config->useBars(false);
else config->useBars(true);
}
}
@@ -410,7 +394,6 @@ void Settings::LanguageSelection(u32 hDown, u32 hHeld, touchPosition touch) {
if (hDown & KEY_A) {
config->language(langsTemp[selectedLang]);
Lang::load(config->language());
changesMade = true;
mode = 0;
}
@@ -459,7 +442,6 @@ void Settings::colorChanging(u32 hDown, u32 hHeld, touchPosition touch) {
}
} else {
if ((hDown & KEY_SELECT) || (hDown & KEY_TOUCH && touching(touch, arrowPos[3]))) {
colorSelection = colorMode;
dropDownMenu = true;
@@ -507,7 +489,6 @@ void Settings::colorChanging(u32 hDown, u32 hHeld, touchPosition touch) {
} else if (colorMode == 11) {
config->buttonColor(RGBA8(red, ColorHelper::getColorValue(config->buttonColor(), 1), ColorHelper::getColorValue(config->buttonColor(), 0), 255));
}
changesMade = true;
}
} else if (touching(touch, mainButtons[1])) {
int temp = Input::getUint(255, Lang::get("ENTER_GREEN_RGB"));
@@ -538,7 +519,6 @@ void Settings::colorChanging(u32 hDown, u32 hHeld, touchPosition touch) {
} else if (colorMode == 11) {
config->buttonColor(RGBA8(ColorHelper::getColorValue(config->buttonColor(), 2), green, ColorHelper::getColorValue(config->buttonColor(), 0), 255));
}
changesMade = true;
}
} else if (touching(touch, mainButtons[2])) {
int temp = Input::getUint(255, Lang::get("ENTER_BLUE_RGB"));
@@ -569,11 +549,11 @@ void Settings::colorChanging(u32 hDown, u32 hHeld, touchPosition touch) {
} else if (colorMode == 11) {
config->buttonColor(RGBA8(ColorHelper::getColorValue(config->buttonColor(), 2), ColorHelper::getColorValue(config->buttonColor(), 1), blue, 255));
}
changesMade = true;
}
}
}
}
if (colorSelection < screenPos) {
screenPos = colorSelection;
} else if (colorSelection > screenPos + ENTRIES_PER_SCREEN - 1) {
-237
View File
@@ -1,237 +0,0 @@
#include "console.h"
#include <arpa/inet.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _3DS
#include <3ds.h>
static PrintConsole status_console;
static PrintConsole main_console;
static PrintConsole tcp_console;
#if ENABLE_LOGGING
static bool disable_logging = false;
#endif
/*! initialize console subsystem */
void
console_init(void)
{
consoleInit(GFX_TOP, &status_console);
consoleSetWindow(&status_console, 0, 0, 50, 1);
consoleInit(GFX_TOP, &main_console);
consoleSetWindow(&main_console, 0, 1, 50, 29);
consoleInit(GFX_BOTTOM, &tcp_console);
consoleSelect(&main_console);
}
/*! set status bar contents
*
* @param[in] fmt format string
* @param[in] ... format arguments
*/
void
console_set_status(const char *fmt, ...)
{
va_list ap;
consoleSelect(&status_console);
va_start(ap, fmt);
vprintf(fmt, ap);
#ifdef ENABLE_LOGGING
vfprintf(stderr, fmt, ap);
#endif
va_end(ap);
consoleSelect(&main_console);
}
/*! add text to the console
*
* @param[in] fmt format string
* @param[in] ... format arguments
*/
void
console_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
#ifdef ENABLE_LOGGING
if (!disable_logging)
vfprintf(stderr, fmt, ap);
#endif
va_end(ap);
}
/*! print debug message
*
* @param[in] fmt format string
* @param[in] ... format arguments
*/
void
debug_print(const char *fmt, ...)
{
#ifdef ENABLE_LOGGING
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
#endif
}
/*! print tcp tables */
static void
print_tcp_table(void)
{
static SOCU_TCPTableEntry tcp_entries[32];
socklen_t optlen;
size_t i;
int rc, lines = 0;
#ifdef ENABLE_LOGGING
disable_logging = true;
#endif
consoleSelect(&tcp_console);
console_print("\x1b[0;0H\x1b[K");
optlen = sizeof(tcp_entries);
rc = SOCU_GetNetworkOpt(SOL_CONFIG, NETOPT_TCP_TABLE, tcp_entries, &optlen);
if(rc != 0 && errno != ENODEV)
console_print(RED "tcp table: %d %s\n\x1b[J\n" RESET, errno, strerror(errno));
else if(rc == 0)
{
for(i = 0; lines < 30 && i < optlen / sizeof(SOCU_TCPTableEntry); ++i)
{
SOCU_TCPTableEntry *entry = &tcp_entries[i];
struct sockaddr_in *local = (struct sockaddr_in*)&entry->local;
struct sockaddr_in *remote = (struct sockaddr_in*)&entry->remote;
console_print(GREEN "%stcp[%zu]: ", i == 0 ? "" : "\n", i);
switch(entry->state)
{
case TCP_STATE_CLOSED:
console_print("CLOSED\x1b[K");
local = remote = NULL;
break;
case TCP_STATE_LISTEN:
console_print("LISTEN\x1b[K");
remote = NULL;
break;
case TCP_STATE_ESTABLISHED:
console_print("ESTABLISHED\x1b[K");
break;
case TCP_STATE_FINWAIT1:
console_print("FINWAIT1\x1b[K");
break;
case TCP_STATE_FINWAIT2:
console_print("FINWAIT2\x1b[K");
break;
case TCP_STATE_CLOSE_WAIT:
console_print("CLOSE_WAIT\x1b[K");
break;
case TCP_STATE_LAST_ACK:
console_print("LAST_ACK\x1b[K");
break;
case TCP_STATE_TIME_WAIT:
console_print("TIME_WAIT\x1b[K");
break;
default:
console_print("State %lu\x1b[K", entry->state);
break;
}
++lines;
if(local && (lines++ < 30))
console_print("\n Local %s:%u\x1b[K", inet_ntoa(local->sin_addr),
ntohs(local->sin_port));
if(remote && (lines++ < 30))
console_print("\n Peer %s:%u\x1b[K", inet_ntoa(remote->sin_addr),
ntohs(remote->sin_port));
}
console_print(RESET "\x1b[J");
}
else
console_print("\x1b[2J");
consoleSelect(&main_console);
#ifdef ENABLE_LOGGING
disable_logging = false;
#endif
}
/*! draw console to screen */
void
console_render(void)
{
/* print tcp table */
print_tcp_table();
/* flush framebuffer */
gfxFlushBuffers();
gspWaitForVBlank();
gfxSwapBuffers();
}
#else
/* this is a lot easier when you have a real console */
void
console_init(void)
{
}
void
console_set_status(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
fputc('\n', stdout);
}
void
console_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
void
debug_print(const char *fmt, ...)
{
#ifdef ENABLE_LOGGING
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
#endif
}
void console_render(void)
{
}
#endif
-4046
View File
File diff suppressed because it is too large Load Diff