ftp: Move net init/exit/dialog into a separate source file and add more error checking

This commit is contained in:
Joel16 2021-12-10 13:52:07 -05:00
parent bb824a3071
commit 48995f634f
11 changed files with 298 additions and 240 deletions

9
app/include/net.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _CMFILEMANAGER_NET_H_
#define _CMFILEMANAGER_NET_H_
namespace Net {
bool InitFTP(char *string);
void ExitFTP(void);
}
#endif

View File

@ -16,8 +16,10 @@
#include "fs.h"
#include "ftppsp.h"
#include "log.h"
#include "kernel_functions.h"
#include "mutex.h"
#include "utils.h"
#define UNUSED(x) (void)(x)
@ -296,7 +298,8 @@ static void send_LIST(ftppsp_client_info_t *client, const char *path) {
#else
dir = pspIoOpenDir(get_psp_path(path));
#endif
if (dir < 0) {
if (R_FAILED(dir)) {
Log::Error("sceIoDopen() failed: 0x%08x\n", dir);
client_send_ctrl_msg(client, "550 Invalid directory.\r\n");
return;
}
@ -427,7 +430,8 @@ static void cmd_CWD_func(ftppsp_client_info_t *client) {
pd = pspIoOpenDir(get_psp_path(tmp_path));
#endif
if (pd < 0) {
if (R_FAILED(pd)) {
Log::Error("sceIoDopen() failed: 0x%08x\n", pd);
client_send_ctrl_msg(client, "550 Invalid directory.\r\n");
return;
}
@ -686,6 +690,7 @@ static void cmd_RNFR_func(ftppsp_client_info_t *client) {
}
static void cmd_RNTO_func(ftppsp_client_info_t *client) {
int ret = 0;
char path_dst[512] = {0};
const char *psp_path_dst;
@ -696,16 +701,20 @@ static void cmd_RNTO_func(ftppsp_client_info_t *client) {
DEBUG("Renaming: %s to %s\n", client->rename_path, psp_path_dst);
#ifdef FS_DEBUG
if (sceIoRename(client->rename_path, psp_path_dst) < 0)
if (R_FAILED(ret = sceIoRename(client->rename_path, psp_path_dst))) {
Log::Error("sceIoRename() failed: 0x%08x\n", ret);
#else
if (pspIoRename(client->rename_path, psp_path_dst) < 0)
if (R_FAILED(ret = pspIoRename(client->rename_path, psp_path_dst))) {
Log::Error("pspIoRename() failed: 0x%08x\n", ret);
#endif
client_send_ctrl_msg(client, "550 Error renaming the file.\r\n");
}
client_send_ctrl_msg(client, "226 Rename completed.\r\n");
}
static void cmd_SIZE_func(ftppsp_client_info_t *client) {
int ret = 0;
SceIoStat stat;
char path[512] = {0};
char cmd[64] = {0};
@ -714,9 +723,11 @@ static void cmd_SIZE_func(ftppsp_client_info_t *client) {
/* Check if the file exists */
#ifdef FS_DEBUG
if (sceIoGetstat(get_psp_path(path), &stat) < 0) {
if (R_FAILED(ret = sceIoGetstat(get_psp_path(path), &stat))) {
Log::Error("sceIoGetStat() failed: 0x%08x\n", ret);
#else
if (pspIoGetstat(get_psp_path(path), &stat) < 0) {
if (R_FAILED(ret = pspIoGetstat(get_psp_path(path), &stat))) {
Log::Error("pspIoGetStat() failed: 0x%08x\n", ret);
#endif
client_send_ctrl_msg(client, "550 The file doesn't exist.\r\n");
return;
@ -1023,23 +1034,28 @@ static int server_thread(SceSize args, void *argp) {
}
int ftppsp_init(char *psp_ip, unsigned short int *psp_port) {
int ret = 0;
if (ftp_initialized)
return -1;
union SceNetApctlInfo info;
if (sceNetApctlGetInfo(8, &info) < 0)
return -1;
if (R_FAILED(ret = sceNetApctlGetInfo(8, &info))) {
Log::Error("sceNetApctlGetInfo() failed: 0x%08x\n", ret);
return ret;
}
else
std::strcpy(psp_ip, info.ip);
*psp_port = FTP_PORT;
/* Save the IP of PSP to a global variable */
if (sceNetInetInetPton(AF_INET, info.ip, &psp_addr) < 0)
return -1;
if (R_FAILED(ret = sceNetInetInetPton(AF_INET, info.ip, &psp_addr))) {
Log::Error("sceNetApctlGetInfo() failed: 0x%08x\n", ret);
return ret;
}
/* Create server thread */
server_thid = sceKernelCreateThread("FTPpsp_server_thread", server_thread, 0x20, 0x6000, PSP_THREAD_ATTR_USBWLAN, nullptr);
server_thid = sceKernelCreateThread("ftppsp_server_thread", server_thread, 0x20, 0x6000, PSP_THREAD_ATTR_USBWLAN, nullptr);
DEBUG("Server thread UID: 0x%08X\n", server_thid);
/* Create the client list mutex */
@ -1054,7 +1070,11 @@ int ftppsp_init(char *psp_ip, unsigned short int *psp_port) {
custom_command_dispatchers[i].valid = 0;
/* Start the server thread */
sceKernelStartThread(server_thid, 0, nullptr);
if (R_FAILED(ret = sceKernelStartThread(server_thid, 0, nullptr))) {
Log::Error("sceKernelStartThread(server_thid) failed: 0x%08x\n", ret);
return ret;
}
ftp_initialized = 1;
return 0;
}

View File

@ -2,8 +2,8 @@
#include <sys/time.h>
#include <psprtc.h>
#include "config.h"
#include "colours.h"
#include "config.h"
#include "fs.h"
#include "g2d.h"
#include "gui.h"

View File

@ -2,8 +2,8 @@
#include <cstdio>
#include "audio.h"
#include "config.h"
#include "colours.h"
#include "config.h"
#include "fs.h"
#include "g2d.h"
#include "gui.h"

View File

@ -1,6 +1,6 @@
#include "colours.h"
#include "config.h"
#include "fs.h"
#include "colours.h"
#include "g2d.h"
#include "gui.h"
#include "textures.h"

View File

@ -3,9 +3,9 @@
#include "archive_helper.h"
#include "audioplayer.h"
#include "colours.h"
#include "config.h"
#include "fs.h"
#include "colours.h"
#include "g2d.h"
#include "game_launcher.h"
#include "gui.h"

View File

@ -1,7 +1,7 @@
#include <pspumd.h>
#include "config.h"
#include "colours.h"
#include "config.h"
#include "fs.h"
#include "g2d.h"
#include "gui.h"

View File

@ -1,6 +1,6 @@
#include "colours.h"
#include "config.h"
#include "fs.h"
#include "colours.h"
#include "g2d.h"
#include "gui.h"
#include "log.h"

View File

@ -1,229 +1,12 @@
#include <cstring>
#include <pspdisplay.h>
#include <pspgu.h>
#include <pspkernel.h>
#include <pspnet.h>
#include <pspnet_apctl.h>
#include <pspnet_inet.h>
#include <psppower.h>
#include <psputility.h>
#include "config.h"
#include "colours.h"
#include "config.h"
#include "fs.h"
#include "ftppsp.h"
#include "g2d.h"
#include "log.h"
#include "gui.h"
#include "net.h"
#include "textures.h"
#include "utils.h"
namespace FTP {
static int DisplayNetDialog(void) {
int ret = 0;
bool done = false;
pspUtilityNetconfData data;
std::memset(&data, 0, sizeof(data));
data.base.size = sizeof(data);
data.base.language = PSP_SYSTEMPARAM_LANGUAGE_ENGLISH;
data.base.buttonSwap = PSP_UTILITY_ACCEPT_CROSS;
data.base.graphicsThread = 17;
data.base.accessThread = 19;
data.base.fontThread = 18;
data.base.soundThread = 16;
data.action = PSP_NETCONF_ACTION_CONNECTAP;
struct pspUtilityNetconfAdhoc adhocparam;
std::memset(&adhocparam, 0, sizeof(adhocparam));
data.adhocparam = &adhocparam;
if (R_FAILED(ret = sceUtilityNetconfInitStart(&data))) {
Log::Error("sceUtilityNetconfInitStart() failed: 0x%08x\n", ret);
return ret;
}
while(!done) {
g2dClear(G2D_RGBA(39, 50, 56, 255));
sceGuFinish();
sceGuSync(0, 0);
switch(sceUtilityNetconfGetStatus()) {
case PSP_UTILITY_DIALOG_INIT:
break;
case PSP_UTILITY_DIALOG_VISIBLE:
if (R_FAILED(ret = sceUtilityNetconfUpdate(1))) {
Log::Error("sceUtilityNetconfUpdate(1) failed: 0x%08x\n", ret);
done = true;
}
break;
case PSP_UTILITY_DIALOG_QUIT:
if (R_FAILED(ret = sceUtilityNetconfShutdownStart())) {
Log::Error("sceUtilityNetconfShutdownStart() failed: 0x%08x\n", ret);
done = true;
}
break;
case PSP_UTILITY_DIALOG_FINISHED:
break;
case PSP_UTILITY_DIALOG_NONE:
done = true;
default:
break;
}
g2dFlip(G2D_VSYNC);
}
return 0;
}
static int InitNet(void) {
int ret = 0;
if (R_FAILED(ret = sceNetInit(128 * 1024, 42, 4 * 1024, 42, 4 * 1024))) {
Log::Error("sceNetInit() failed: 0x%08x\n", ret);
return ret;
}
if (R_FAILED(ret = sceNetInetInit())) {
Log::Error("sceNetInetInit() failed: 0x%08x\n", ret);
return ret;
}
if (R_FAILED(ret = sceNetApctlInit(0x8000, 48))) {
Log::Error("sceNetApctlInit() failed: 0x%08x\n", ret);
return ret;
}
return 0;
}
static void ExitNet(void) {
sceNetApctlTerm();
sceNetInetTerm();
sceNetTerm();
}
static void InitFlash(void) {
unsigned int ret = 0;
if ((R_FAILED(ret = sceIoUnassign("flash0:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash0) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash0:", "lflash0:0,0", "flashfat0:", IOASSIGN_RDWR, nullptr, 0)))
Log::Error("sceIoAssign(flash0) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash1:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash1) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash1:", "lflash0:0,1", "flashfat1:", IOASSIGN_RDWR, nullptr, 0)))
Log::Error("sceIoAssign(flash1) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash2:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash2) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash2:", "lflash0:0,2", "flashfat2:", IOASSIGN_RDWR, nullptr, 0)))
Log::Error("sceIoAssign(flash2) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash3:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash3) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash3:", "lflash0:0,3", "flashfat3:", IOASSIGN_RDWR, nullptr, 0)))
Log::Error("sceIoAssign(flash3) failed: 0x%x\n", ret);
}
static void ExitFlash(void) {
unsigned int ret = 0;
if ((R_FAILED(ret = sceIoUnassign("flash0:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash0) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash0:", "lflash0:0,0", "flashfat0:", IOASSIGN_RDONLY, nullptr, 0)))
Log::Error("sceIoAssign(flash0) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash1:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash1) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash1:", "lflash0:0,1", "flashfat1:", IOASSIGN_RDONLY, nullptr, 0)))
Log::Error("sceIoAssign(flash1) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash2:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash2) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash2:", "lflash0:0,2", "flashfat2:", IOASSIGN_RDONLY, nullptr, 0)))
Log::Error("sceIoAssign(flash2) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash3:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash3) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash3:", "lflash0:0,3", "flashfat3:", IOASSIGN_RDONLY, nullptr, 0)))
Log::Error("sceIoAssign(flash3) failed: 0x%x\n", ret);
}
bool Init(char *string) {
int ret = 0;
char psp_ip[16] = {0};
unsigned short int psp_port = 0;
scePowerLock(0);
sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
if (R_FAILED(ret = FTP::InitNet())) {
std::sprintf(string, "Net initialization Failed.");
return false;
}
FTP::DisplayNetDialog();
ret = ftppsp_init(psp_ip, &psp_port);
if (is_psp_go) {
if (is_ms_inserted) {
ftppsp_add_device("ms0:");
ftppsp_add_device("ef0:");
}
else
ftppsp_add_device("ef0:");
}
else
ftppsp_add_device("ms0:");
if (cfg.dev_options) {
FTP::InitFlash();
ftppsp_add_device("flash0:");
ftppsp_add_device("flash1:");
ftppsp_add_device("flash2:");
ftppsp_add_device("flash3:");
}
if (ret < 0)
std::sprintf(string, "Connection Failed.");
else
std::sprintf(string, "FTP Connected %s:%i", psp_ip, psp_port);
return true;
}
void Exit(void) {
ftppsp_fini();
if (cfg.dev_options)
FTP::ExitFlash();
FTP::ExitNet();
sceUtilityUnloadNetModule(PSP_NET_MODULE_INET);
sceUtilityUnloadNetModule(PSP_NET_MODULE_COMMON);
scePowerUnlock(0);
}
}
namespace GUI {
enum SETTINGS_STATE {
GENERAL_SETTINGS,
@ -254,7 +37,7 @@ namespace GUI {
static void ControlFTPSettings(void) {
if (Utils::IsButtonPressed(PSP_CTRL_CANCEL)) {
FTP::Exit();
Net::ExitFTP();
settings_state = GENERAL_SETTINGS;
}
@ -360,7 +143,7 @@ namespace GUI {
if (Utils::IsButtonPressed(PSP_CTRL_ENTER)) {
switch(selection) {
case 0:
FTP::Init(ftp_text);
Net::InitFTP(ftp_text);
settings_state = FTP_SETTINGS;
break;

View File

@ -4,8 +4,8 @@
#include <pspiofilemgr.h>
#include <pspthreadman.h>
#include "config.h"
#include "colours.h"
#include "config.h"
#include "fs.h"
#include "g2d.h"
#include "gui.h"

246
app/source/net.cpp Normal file
View File

@ -0,0 +1,246 @@
#include <cstring>
#include <pspdisplay.h>
#include <pspgu.h>
#include <pspiofilemgr.h>
#include <pspnet.h>
#include <pspnet_apctl.h>
#include <pspnet_inet.h>
#include <psppower.h>
#include <psputility.h>
#include "config.h"
#include "ftppsp.h"
#include "g2d.h"
#include "log.h"
#include "utils.h"
namespace Flash {
void Init(void) {
unsigned int ret = 0;
if ((R_FAILED(ret = sceIoUnassign("flash0:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash0) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash0:", "lflash0:0,0", "flashfat0:", IOASSIGN_RDWR, nullptr, 0)))
Log::Error("sceIoAssign(flash0) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash1:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash1) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash1:", "lflash0:0,1", "flashfat1:", IOASSIGN_RDWR, nullptr, 0)))
Log::Error("sceIoAssign(flash1) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash2:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash2) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash2:", "lflash0:0,2", "flashfat2:", IOASSIGN_RDWR, nullptr, 0)))
Log::Error("sceIoAssign(flash2) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash3:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash3) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash3:", "lflash0:0,3", "flashfat3:", IOASSIGN_RDWR, nullptr, 0)))
Log::Error("sceIoAssign(flash3) failed: 0x%x\n", ret);
}
void Exit(void) {
unsigned int ret = 0;
if ((R_FAILED(ret = sceIoUnassign("flash0:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash0) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash0:", "lflash0:0,0", "flashfat0:", IOASSIGN_RDONLY, nullptr, 0)))
Log::Error("sceIoAssign(flash0) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash1:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash1) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash1:", "lflash0:0,1", "flashfat1:", IOASSIGN_RDONLY, nullptr, 0)))
Log::Error("sceIoAssign(flash1) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash2:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash2) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash2:", "lflash0:0,2", "flashfat2:", IOASSIGN_RDONLY, nullptr, 0)))
Log::Error("sceIoAssign(flash2) failed: 0x%x\n", ret);
if ((R_FAILED(ret = sceIoUnassign("flash3:"))) && (ret != 0x80020321))
Log::Error("sceIoUnassign(flash3) failed: 0x%x\n", ret);
if (R_FAILED(ret = sceIoAssign("flash3:", "lflash0:0,3", "flashfat3:", IOASSIGN_RDONLY, nullptr, 0)))
Log::Error("sceIoAssign(flash3) failed: 0x%x\n", ret);
}
}
namespace Net {
static int DisplayNetDialog(void) {
int ret = 0;
bool done = false;
pspUtilityNetconfData data;
std::memset(&data, 0, sizeof(data));
data.base.size = sizeof(data);
data.base.language = PSP_SYSTEMPARAM_LANGUAGE_ENGLISH;
data.base.buttonSwap = PSP_UTILITY_ACCEPT_CROSS;
data.base.graphicsThread = 17;
data.base.accessThread = 19;
data.base.fontThread = 18;
data.base.soundThread = 16;
data.action = PSP_NETCONF_ACTION_CONNECTAP;
struct pspUtilityNetconfAdhoc adhocparam;
std::memset(&adhocparam, 0, sizeof(adhocparam));
data.adhocparam = &adhocparam;
if (R_FAILED(ret = sceUtilityNetconfInitStart(&data))) {
Log::Error("sceUtilityNetconfInitStart() failed: 0x%08x\n", ret);
return ret;
}
while(!done) {
g2dClear(G2D_RGBA(39, 50, 56, 255));
sceGuFinish();
sceGuSync(0, 0);
switch(sceUtilityNetconfGetStatus()) {
case PSP_UTILITY_DIALOG_INIT:
break;
case PSP_UTILITY_DIALOG_VISIBLE:
if (R_FAILED(ret = sceUtilityNetconfUpdate(1))) {
Log::Error("sceUtilityNetconfUpdate(1) failed: 0x%08x\n", ret);
done = true;
}
break;
case PSP_UTILITY_DIALOG_QUIT:
if (R_FAILED(ret = sceUtilityNetconfShutdownStart())) {
Log::Error("sceUtilityNetconfShutdownStart() failed: 0x%08x\n", ret);
done = true;
}
break;
case PSP_UTILITY_DIALOG_FINISHED:
break;
case PSP_UTILITY_DIALOG_NONE:
done = true;
default:
break;
}
g2dFlip(G2D_VSYNC);
}
return 0;
}
static int Init(void) {
int ret = 0;
if (R_FAILED(ret = sceNetInit(128 * 1024, 42, 4 * 1024, 42, 4 * 1024))) {
Log::Error("sceNetInit() failed: 0x%08x\n", ret);
return ret;
}
if (R_FAILED(ret = sceNetInetInit())) {
Log::Error("sceNetInetInit() failed: 0x%08x\n", ret);
return ret;
}
if (R_FAILED(ret = sceNetApctlInit(0x8000, 48))) {
Log::Error("sceNetApctlInit() failed: 0x%08x\n", ret);
return ret;
}
return 0;
}
static void Exit(void) {
sceNetApctlTerm();
sceNetInetTerm();
sceNetTerm();
}
bool InitFTP(char *string) {
int ret = 0;
char psp_ip[16] = {0};
unsigned short int psp_port = 0;
scePowerLock(0);
sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
if (R_FAILED(ret = Net::Init())) {
std::sprintf(string, "Net initialization Failed.");
return false;
}
Net::DisplayNetDialog();
if (R_FAILED(ret = ftppsp_init(psp_ip, &psp_port))) {
std::sprintf(string, "FTP initialization Failed.");
return false;
}
if (is_psp_go) {
if (is_ms_inserted) {
ftppsp_add_device("ms0:");
ftppsp_add_device("ef0:");
}
else
ftppsp_add_device("ef0:");
}
else
ftppsp_add_device("ms0:");
if (cfg.dev_options) {
Flash::Init();
ftppsp_add_device("flash0:");
ftppsp_add_device("flash1:");
ftppsp_add_device("flash2:");
ftppsp_add_device("flash3:");
}
if (ret < 0) {
std::sprintf(string, "Connection Failed.");
}
else
std::sprintf(string, "FTP Connected %s:%i", psp_ip, psp_port);
return true;
}
void ExitFTP(void) {
if (is_psp_go) {
if (is_ms_inserted) {
ftppsp_del_device("ms0:");
ftppsp_del_device("ef0:");
}
else
ftppsp_del_device("ef0:");
}
else
ftppsp_del_device("ms0:");
if (cfg.dev_options) {
Flash::Init();
ftppsp_del_device("flash0:");
ftppsp_del_device("flash1:");
ftppsp_del_device("flash2:");
ftppsp_del_device("flash3:");
}
ftppsp_fini();
if (cfg.dev_options)
Flash::Exit();
Net::Exit();
sceUtilityUnloadNetModule(PSP_NET_MODULE_INET);
sceUtilityUnloadNetModule(PSP_NET_MODULE_COMMON);
scePowerUnlock(0);
}
}