This is almost a release except for it doesnt work... so close!
I want my program to download a file chosen by a person in advance, so like torrents, but you can create it on notepad but it has an actual URL link...
There is a filebrowser to choose what file to get...
The layout of that file is
URL =
NameandPath =
Comments =
(this is the config file from harleyg).
Now source code:
main.c
// INCLUDES
#include <pspnet_apctl.h>
#include <pspnet_inet.h>
#include <pspwlan.h>
#include <pspctrl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <pspnet.h>
#include <pspsdk.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <psploadexec.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspkernel.h>
#include <pspiofilemgr.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pspsdk.h>
#include <psppower.h>
#include "wifi.h"
#include <pspnet_apctl.h>
// MODULE INFO
PSP_MODULE_INFO("Wifi Testing", 0x1000, 1, 1);
PSP_MAIN_THREAD_ATTR(0);
// DEFINES // // These make lots of stuff easier, like typing
#define printf pspDebugScreenPrintf
#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))
#define IS_ALPHA(color) (((color)&0xff000000)==0xff000000?0:1)
#define FRAMEBUFFER_SIZE (PSP_LINE_SIZE*SCREEN_HEIGHT*4)
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#define FALSE 0
#define TRUE !FALSE
#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)
#define PIXEL_SIZE (4) // change this if you change to another screenmode
#define FRAME_SIZE (BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE)
#define ZBUF_SIZE (BUF_WIDTH * SCR_HEIGHT * 2)
char write_buffer[128*1024]; // Defines a buffer to use
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
sceNetApctlDisconnect();
pspSdkInetTerm();
sceKernelExitGame();
return 0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
char *getconfig(const char *location, const char searchstr[]) {
FILE *file;
char *w[100], string[100], line[100], c;
int z[100], x[100], i=0, v;
file = fopen (location, "r");
if (file == NULL) return NULL;
else {
while(fgets(string , 100 , file) != NULL) {
x[i] = strlen(string)-1;
if(string[0] == '#') continue;
w[i] = strchr(string,'=')-1;
*w[i] = 0;
if(!strcmp(searchstr, string)) {
char *crlf = strpbrk(w[i]+3, "\r\n");
if(crlf)
*crlf = 0;
return w[i]+3;
}
i++;
}
fclose(file);
return NULL;
}
}
int FileSize( const char * szFileName )
{
struct stat fileStat;
int err = stat( szFileName, &fileStat );
if (0 != err) return 0;
return fileStat.st_size;
}
#define MAX_ENTRIES 200
typedef struct
{
char name[255];
int isdirectory;
int filesize;
} dir_entry;
int get_dirs(dir_entry *entries)
{
int __index = 0;
struct dirent *namelist;
struct stat buf;
DIR *dir;
dir = opendir(".");
while ((namelist = readdir(dir)) && __index < MAX_ENTRIES)
{
strcpy( entries[__index].name, namelist->d_name );
if (stat(namelist->d_name, &buf)==0)
{
entries[__index].filesize = buf.st_size;
entries[__index].isdirectory = S_ISDIR(buf.st_mode);
}
else
{
entries[__index].filesize = 0;
entries[__index].isdirectory = FIO_SO_ISDIR(namelist->d_stat.st_mode);
}
if (strcmp(entries[__index].name,"..") == 0)
entries[__index].isdirectory = 1;
__index++;
}
closedir(dir);
return __index;
}
// START MAIN
int user_main(SceSize argc, void* argv)
{
pspDebugScreenInit();
pspSdkLoadInetModules();
SetupCallbacks();
pspDebugScreenInit();
dir_entry entries[MAX_ENTRIES];
chdir("ms0:/Downloads/");
int sel_index = 0;
int tot_index = 0;
int __button_pressed = 0;
int __button_released = 0;
SceCtrlData pad;
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
while (1)
{
sceCtrlReadBufferPositive(&pad, 1);
if (!tot_index) goto first_time;
__button_released = (__button_pressed^pad.Buttons)&(__button_pressed^0xFFFFFFFF);
__button_pressed = pad.Buttons;
if (!__button_released) continue;
if (__button_released & PSP_CTRL_UP)
sel_index = sel_index-1 < 0 ? 0 : sel_index-1;
if (__button_released & PSP_CTRL_DOWN)
sel_index = sel_index+1 == tot_index ? tot_index-1 : sel_index+1;
if (__button_released & PSP_CTRL_CROSS && entries[sel_index].isdirectory)
{ chdir(entries[sel_index].name); sel_index = 0;}
if (__button_released & PSP_CTRL_CROSS && !entries[sel_index].isdirectory)
{
pspDebugScreenClear();
char* URL = getconfig(entries[sel_index].name, "URL");
char* path = getconfig(entries[sel_index].name, "NameandPath");
pspDebugScreenPrintf("About to download %s", getconfig(entries[sel_index].name, "URL"));
DownloadFile(URL, path);
pspDebugScreenPrintf("Downloading %s, to %s", getconfig(entries[sel_index].name, "URL"), getconfig(entries[sel_index].name, "NameandPath"));
pspDebugScreenPrintf("\nInfo about file: \n\n%s", getconfig(entries[sel_index].name, "Comments"));
}
first_time:
if (!sel_index)
tot_index = get_dirs(entries);
pspDebugScreenClear();
pspDebugScreenSetTextColor(0x0000FF);
char buff[255];
char *p = getcwd(buff,255);
int x;
for ( x = (sel_index > 30 ? sel_index - 30 : 0) ;
x < tot_index && x < 31+(sel_index > 30 ? sel_index - 30 : 0); x++)
if (entries[x].isdirectory)
{
printf("%c %s\n", x == sel_index ? '>' : ' ', entries[x].name);
}
else
{
printf("%c %s\n", x == sel_index ? '>' : ' ', entries[x].name);
}
}
sceNetApctlDisconnect();
pspSdkInetTerm();
return 0;
}
int main()
{
pspDebugScreenInit();
int err = pspSdkLoadInetModules();
if (err != 0) {
pspDebugScreenPrintf("pspSdkLoadInetModules failed with %x\n", err);
sceKernelDelayThread(5*1000000); // 5 sec to read error
return 1;
}
// create user thread, tweek stack size here if necessary
SceUID thid = sceKernelCreateThread("User Mode Thread", user_main,
8, // default priority
256 * 1024, // stack size (256KB is regular default)
PSP_THREAD_ATTR_USER, NULL);
// start user thread, then wait for it to do everything else
sceKernelStartThread(thid, 0, NULL);
sceKernelWaitThreadEnd(thid, NULL);
return 0;
}
wlan.c
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <stdlib.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <psputility.h>
#include <pspnet_apctl.h>
#include <pspnet_inet.h>
#include <pspwlan.h>
#include <pspctrl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <pspnet_resolver.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <pspnet.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#define TRUE 1
#define FALSE 0
int isConnected = 0;
struct FtpFile {
char *filename;
FILE *stream;
};
int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream) {
/* open file for writing */
out->stream=fopen(out->filename, "wb");
if(!out->stream)
return -1; /* failure, can't open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}
int NetConnect(int config)
{
SceCtrlData pad;
while (sceWlanGetSwitchState() != 1)
{
pspDebugScreenPrintf("Turn the wifi switch on!\n");
sceKernelDelayThread(1000*1000);
}
// init wlan
int err = pspSdkInetInit();
if (err != 0) {
pspDebugScreenPrintf("pspSdkInetInit failed: %i", err);
return 1;
}
connectWifi:
err = sceNetApctlConnect(config);
if (err != 0)
{
pspDebugScreenPrintf("sceNetApctlConnect returns $%x\n", err);
sceKernelDelayThread(4*1000000); // 4sec to read before exit
return 1;
}
// Report status while waiting for connection to access point
int stateLast = -1;
pspDebugScreenPrintf("Connecting To Wifi...\n");
while (1)
{
sceCtrlPeekBufferPositive(&pad, 1); //READ CONTROLS
if (pad.Buttons & PSP_CTRL_CIRCLE)
return 1;
int state;
err = sceNetApctlGetState(&state);
if (err != 0)
{
pspDebugScreenPrintf("sceNetApctlGetState returns $%x\n", err);
sceKernelDelayThread(10*1000000); // 10sec to read before exit
return 1;
}
if (state != stateLast)
{
if (stateLast == 2 && state == 0)
{
pspDebugScreenPrintf(" Connecting to wifi Failed, Retrying...\n");
sceKernelDelayThread(500*1000); // 500ms
goto connectWifi;
}
pspDebugScreenPrintf(" connection state %d of 4\n", state);
stateLast = state;
}
if (state == 4)
break; // connected
// wait a little before polling again
sceKernelDelayThread(50*1000); // 50ms
}
sceKernelDelayThread(2*1000000); // 2sec to read any final status
return 0;
}
int DownloadFile(char *url, char *filename)
{
pspDebugScreenInit();
NetConnect(1);
CURL *curl;
CURLcode res;
struct FtpFile ftpfile={
filename, /* name to store the file as if succesful */
NULL
};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,
url);
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);
//curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
if(CURLE_OK != res) {
/* we failed */
pspDebugScreenPrintf("Error: curl told us %d\n", res);
}
}
if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
curl_global_cleanup();
return 0;
}
wlan.h
#ifndef __WIFI__
#define __WIFI__
void NetConnect(int config);
int DownloadFile(char *url, char *filename);
#endif