downloading files?? from internet.....How?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
FaderX
Posts: 28
Joined: Sat Mar 01, 2008 12:38 pm

downloading files?? from internet.....How?

Post by FaderX »

Hey all im new here, lol

SO i have a homebrew which accesses the internet,.....How do you download a file do you use?

Code: Select all

copy_file("http://website.com/file.extension, "ms0:/file.extension");
I've tried that & it currently doesn't work?? & im connected to the internet.

im fairly sure its not going to work, but i could be wrong.

i know Dark_AleX /moonlight does it in the 3.90 M33 Unpacker/Downloader thingy

If someone knows what it is please let me know, or if there's a place that shows it already can you point me in that direction please,

Thanks all,

FaderX

p.s. I've checked usin the search option as usual in forums, & Google, and I've ended up with no results in what I'm looking for.
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

except access point connection and adress resolving which is available from samples in the sdk everything else it's the same as with the pc, so search for a good network connection in c/c++ tutorial with google
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

As welltall said, you could use whatever network code and create a very basic http client, as for downloading a file you don't need much complicated things.

Anyways, the M33 updater uses the firmware libhttp which has not been added yet to the sdk, and uses it like it has been seen in the sony xmb network update.

Basically, after loading the other network modules, load http ones:

Code: Select all

if &#40;sceUtilityLoadNetModule&#40;PSP_NET_MODULE_PARSEURI&#41; < 0&#41;
	// error here

if &#40;sceUtilityLoadNetModule&#40;PSP_NET_MODULE_PARSEHTTP&#41; < 0&#41;
	// error here

if &#40;sceUtilityLoadNetModule&#40;PSP_NET_MODULE_HTTP&#41; < 0&#41;
   // error here
(for unload, unload them in reverse order).

And usage of library:

Code: Select all

char *the_url = "http&#58;//www.someweb.com/file.bin";
u8 buf&#91;100*1024&#93; __attribute__&#40;&#40;aligned&#40;64&#41;&#41;&#41;; // this should go as global var

u32 filesize;
int nbytes;

if &#40;sceHttpInit&#40;20000&#41; < 0&#41;
   // error here

int template = sceHttpCreateTemplate&#40;"MyUserAgent/0.0.1 libhttp/1.0.0", 1, 1&#41;;

if &#40;template < 0&#41;
    // error here

if &#40;sceHttpSetResolveTimeOut&#40;template, 3000000&#41; < 0&#41;
   // error here

if &#40;sceHttpSetRecvTimeOut&#40;template, 60000000&#41; < 0&#41;
   // error here

if &#40;sceHttpSetSendTimeOut&#40;template, 60000000&#41; < 0&#41;
   // error here
int connection = sceHttpCreateConnectionWithURL&#40;template, the_url, 0&#41;;

if &#40;connection < 0&#41;
   // error here

int request = sceHttpCreateRequestWithURL&#40;connection, 0, the_url, 0&#41;;

if &#40;request < 0&#41;
   // error here

if &#40;sceHttpSendRequest&#40;request, 0, 0&#41; < 0&#41;
   // error here

int status = sceHttpGetStatusCode&#40;request, &status&#41;;

if &#40;status != 200&#41;
   // error here

if &#40;sceHttpGetContentLength&#40;request, &filesize&#41; < 0&#41;
   // error here

printf&#40;"File size = %d\n" filesize&#41;;

while &#40;&#40;nbytes = sceHttpReadData&#40;request, buf, sizeof&#40;buf&#41;&#41;&#41; != 0&#41;
&#123;
   if &#40;nbytes < 0&#41;
      // error here, abort loop

   // Write nbytes to a file or whatever
&#125;
Of course, you have to create the stub or the library for libhttp

sceHttp.S:

Code: Select all

.set noreorder

#include "pspstub.s"

	STUB_START	"sceHttp",0x00090011,0x00530005
	STUB_FUNC	0x0282A3BD,sceHttpGetContentLength
	STUB_FUNC	0x1F0FC3E3,sceHttpSetRecvTimeOut
	STUB_FUNC	0x47940436,sceHttpSetResolveTimeOut
	STUB_FUNC	0x4CC7D78F,sceHttpGetStatusCode
	STUB_FUNC	0x9988172D,sceHttpSetSendTimeOut
	STUB_FUNC	0x9B1F1F36,sceHttpCreateTemplate
	STUB_FUNC	0xAB1ABE07,sceHttpInit
	STUB_FUNC	0xB509B09E,sceHttpCreateRequestWithURL
	STUB_FUNC	0xBB70706F,sceHttpSendRequest
	STUB_FUNC	0xCDF8ECB9,sceHttpCreateConnectionWithURL
	STUB_END
FaderX
Posts: 28
Joined: Sat Mar 01, 2008 12:38 pm

Post by FaderX »

thanks weltall & moonlight, i'll have a look in the morning, its about 10:00 & im tired coz i just got back from a engagement party.

i appreciate it the help,

FaderX
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

I'll be adding libhttp to the SDK soon, I'm working on it currently.

It's just a wrapper for the socket functions anyway,
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

There's also curl in the repo. Getting files across the net with curl is pretty simple.
FaderX
Posts: 28
Joined: Sat Mar 01, 2008 12:38 pm

Post by FaderX »

This is my main.c

Code: Select all

//Headers are here

PSP_MODULE_INFO&#40;"DL_File", 0x0800, 1, 0&#41;;

PSP_MAIN_THREAD_ATTR&#40;PSP_THREAD_ATTR_VSH&#41;;

#define printf pspDebugScreenPrintf

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
   sceKernelExitGame&#40;&#41;;
   return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
   int cbid;

   cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
   sceKernelRegisterExitCallback&#40;cbid&#41;;
   sceKernelSleepThreadCB&#40;&#41;;

   return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
   int thid = 0;

   thid = sceKernelCreateThread&#40;"update_thread", CallbackThread,
                 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0&#41;;
   if&#40;thid >= 0&#41;
   &#123;
      sceKernelStartThread&#40;thid, 0, 0&#41;;
   &#125;

   return thid;
&#125;


int connect_to_apctl&#40;int config&#41;
&#123;
  int sceWlanGetSwitchState&#40;void&#41;;  
  int err;
  int stateLast = -1;

  if &#40;sceWlanGetSwitchState&#40;&#41; != 1&#41;
    pspDebugScreenClear&#40;&#41;;

  while &#40;sceWlanGetSwitchState&#40;&#41; != 1&#41;
  &#123;
    pspDebugScreenSetXY&#40;0, 0&#41;;
    printf&#40;"enable WLAN to continue.\n"&#41;;
    sceKernelDelayThread&#40;1000 * 1000&#41;;
  &#125;

  err = sceNetApctlConnect&#40;config&#41;;
  if &#40;err != 0&#41;
  &#123;
    printf&#40;"sceNetApctlConnect returns %08X\n", err&#41;;
    return 0;
  &#125;

  printf&#40;"Connecting...\n"&#41;;
  while &#40;1&#41;
  &#123;
    int state;
    err = sceNetApctlGetState&#40;&state&#41;;
    if &#40;err != 0&#41;
    &#123;
      printf&#40;"sceNetApctlGetState returns $%x\n", err&#41;;
      break;
    &#125;
    if &#40;state != stateLast&#41;
    &#123;
      printf&#40;" Connection state %d of 4.\n", state&#41;;
      stateLast = state;
    &#125;
    if &#40;state == 4&#41;
    &#123;
      break;
    &#125;
    sceKernelDelayThread&#40;50 * 1000&#41;;
  &#125;
  printf&#40;"connected!\n"&#41;;
  sceKernelDelayThread&#40;3000 * 1000&#41;;

  if &#40;err != 0&#41;
  &#123;
    return 0;
  &#125;

  return 1;
&#125;

char *getconfname&#40;int confnum&#41;
&#123;
     static char confname&#91;128&#93;;
     sceUtilityGetNetParam&#40;confnum, PSP_NETPARAM_NAME, &#40;netData *&#41;confname&#41;;
     return confname;
&#125;

int net_thread&#40;SceSize args, void *argp&#41;
&#123;
  int selComponent = 1;
 
  printf&#40;"Connection %d &#40;%s&#41; to connect...\n", selComponent, getconfname&#40;selComponent&#41;&#41;;

  if &#40;connect_to_apctl&#40;selComponent&#41;&#41;
  &#123;
    char szMyIPAddr&#91;32&#93;;
    if &#40;sceNetApctlGetInfo&#40;8, szMyIPAddr&#41; != 0&#41;
    &#123;
           strcpy&#40;szMyIPAddr, "unknown IP address"&#41;;
           printf&#40;"ip&#58; %s\n", szMyIPAddr&#41;;
           sceKernelSleepThread&#40;&#41;;
    &#125;
  &#125;  
  return 0;
&#125;

int InitialiseNetwork&#40;void&#41;
&#123;
  int err;
  
  //Load the Network modules
  printf&#40;"load network modules... "&#41;;
  
  err = sceUtilityLoadNetModule&#40;PSP_NET_MODULE_COMMON&#41;;  
  if &#40;err != 0&#41;
  &#123;
    printf&#40;"Error, could not load PSP_NET_MODULE_COMMON %08X\n", err&#41;;
    return 1;
  &#125;
  
  err = sceUtilityLoadNetModule&#40;PSP_NET_MODULE_INET&#41;;  
  if &#40;err != 0&#41;
  &#123;
    printf&#40;"Error, could not load PSP_NET_MODULE_INET %08X\n", err&#41;;
    return 1;
  &#125;
  
  printf&#40;"Done\n"&#41;;

  err = pspSdkInetInit&#40;&#41;;  
  if &#40;err != 0&#41;
  &#123;
    printf&#40;"Error, could not initialize the network %08X\n", err&#41;;
    return 1;
  &#125;
  
   //Now the http modules
   if &#40;sceUtilityLoadNetModule&#40;PSP_NET_MODULE_PARSEURI&#41; < 0&#41;
   &#123;
         // error here
         printf&#40;"Error, Could Not initialize the Net module PARSEURI\n"&#41;;
    
    return 1;
   &#125;

  if &#40;sceUtilityLoadNetModule&#40;PSP_NET_MODULE_PARSEHTTP&#41; < 0&#41;
  &#123;
         // error here
         printf&#40;"Error, Could Not initialize the Net module PARSEHTTP\n"&#41;;
    
    return 1;
  &#125;

  if &#40;sceUtilityLoadNetModule&#40;PSP_NET_MODULE_HTTP&#41; < 0&#41;
  &#123;
         // error here
         printf&#40;"Error, Could Not initialize the Net module HTTP\n"&#41;;
    
    return 1;
  &#125; 
  
  return 0;
&#125;

/* Simple thread */
int main&#40;int argc, char **argv&#41;
&#123;
   SceUID thid;

   SetupCallbacks&#40;&#41;;

   pspDebugScreenInit&#40;&#41;;
   
   pspDebugScreenClear&#40;&#41;;
   
   if &#40;InitialiseNetwork&#40;&#41; != 0&#41;
   &#123;
      printf&#40;"Error! Network Couldn't be Initialised\n"&#41;;
      sceKernelSleepThread&#40;&#41;;
   &#125;

  thid = sceKernelCreateThread&#40;"net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL&#41;;
  if &#40;thid < 0&#41;
  &#123;
    printf&#40;"thread couldnt be created!\n"&#41;;
    sceKernelSleepThread&#40;&#41;;
  &#125;

  sceKernelStartThread&#40;thid, 0, NULL&#41;;
	
	pspDebugScreenSetTextColor&#40;0xFFFFF1&#41;;
	printf&#40;"Some text crap\n"&#41;;

	while &#40;1&#41;
	&#123;
          SceCtrlData pad;
          
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;

		if &#40;pad.Buttons & PSP_CTRL_CROSS&#41;
		&#123;                       
			break;
		&#125;

		else if &#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41;
		&#123;
			sceKernelExitGame&#40;&#41;;
		&#125;

		sceKernelDelayThread&#40;10000&#41;;
	&#125;
	
    char *the_url = "http&#58;//someweb.com/file.bin";
    
    u8 buf&#91;100*1024&#93; __attribute__&#40;&#40;aligned&#40;64&#41;&#41;&#41;; // this should go as global var
    
    u32 filesize;
    int nbytes;
    
    if &#40;sceHttpInit&#40;20000&#41; < 0&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
    
    &#125; 

    int template = sceHttpCreateTemplate&#40;"MyUserAgent/0.0.1 libhttp/1.0.0", 1, 1&#41;;

    if &#40;template < 0&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
         
    &#125; 

    if &#40;sceHttpSetResolveTimeOut&#40;template, 3000000&#41; < 0&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
         
    &#125; 

    if &#40;sceHttpSetRecvTimeOut&#40;template, 60000000&#41; < 0&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
         
    &#125; 

    if &#40;sceHttpSetSendTimeOut&#40;template, 60000000&#41; < 0&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
         
    &#125; 
  
    int connection = sceHttpCreateConnectionWithURL&#40;template, the_url, 0&#41;;

    if &#40;connection < 0&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
         
    &#125; 

    int request = sceHttpCreateRequestWithURL&#40;connection, 0, the_url, 0&#41;;

    if &#40;request < 0&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
         
    &#125; 

    if &#40;sceHttpSendRequest&#40;request, 0, 0&#41; < 0&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
         
    &#125; 

    int status = sceHttpGetStatusCode&#40;request, &status&#41;;

    if &#40;status != 200&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
         
    &#125; 
  
    if &#40;sceHttpGetContentLength&#40;request, &filesize&#41; < 0&#41;
    &#123;
         // error here
         printf&#40;"Error\n"&#41;;
         
    &#125; 

    printf&#40;"File size = %d\n", filesize&#41;;

    while &#40;&#40;nbytes = sceHttpReadData&#40;request, buf, sizeof&#40;buf&#41;&#41;&#41; != 0&#41;
    &#123;
       if &#40;nbytes < 0&#41;
       &#123;
         // error here, abort loop
         printf&#40;"Error\n"&#41;;
         
       &#125; 

       // Write nbytes to a file or whatever
    &#125;    

   return 0;
&#125; 
 

Now my makefile, i sorta adapt this makefile to everything so it may be wrong as usual, im using the kernels 3.XX or 1.50 either one. atm im using the 3.XX

Code: Select all

TARGET = DL_File
OBJS = main.o HttpRequest.o HttpPacket.o HttpResponse.o

INCDIR = 
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBDIR =
LIBS = sceHttp.S -lpsppower -lpspgu -lz -lm -lpspwlan sceWlanDrv_lib.S sceWlanDrv.S
LDFLAGS =

PSP_FW_VERSION = 340
BUILD_PRX = 1
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = DL_File

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
I know im going wrong, just can't correct it, It compiles for me, but doesnt load now? it gets past the gameboot then....Black Screen. Idea's anyone.

i also tried in user,

Code: Select all

PSP_MODULE_INFO&#40;"DL_File", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;PSP_THREAD_ATTR_USER&#41;; 
Do i need to add more stuff??, take stuff out, lol.
gambiting
Posts: 154
Joined: Thu Aug 17, 2006 5:39 pm

Post by gambiting »

You have to define Heap size:

Code: Select all

PSP_HEAP_SIZE_MAX&#40;&#41;;
put it after psp module info.Should help.
FaderX
Posts: 28
Joined: Sat Mar 01, 2008 12:38 pm

Post by FaderX »

gambiting wrote:You have to define Heap size:

Code: Select all

PSP_HEAP_SIZE_MAX&#40;&#41;;
put it after psp module info.Should help.
thanks, but it still doesnt work, any more idea's??
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Well, as i put in the comments, the 100 KB global var would go better as global variable.
FaderX
Posts: 28
Joined: Sat Mar 01, 2008 12:38 pm

Post by FaderX »

i also tried using a prx that loads, it fails aswell, i used the Cfe 2.2 ftpd.prx btw. any other idea's to get this working??
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

I've commited libhttp to the SDK, revision 2376.
FaderX
Posts: 28
Joined: Sat Mar 01, 2008 12:38 pm

Post by FaderX »

Insert_witty_name wrote:I've commited libhttp to the SDK, revision 2376.
Thanks man!!
PiCkDaT
Posts: 69
Joined: Thu Oct 04, 2007 9:49 pm

Post by PiCkDaT »

can you use a TCP/IP Socket to get files from the internet instead of a UDP(since it loses data). Just wondering because I have seen examples on how to get HTML data from a webserver but it was through UDP...
Enlighten me, Reveal my fate -- Follow - Breaking Benjamin
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Where I can find the lastest build of PSP SDK? In the home there is a old build of PSP SDK!
PiCkDaT
Posts: 69
Joined: Thu Oct 04, 2007 9:49 pm

Post by PiCkDaT »

in cygwin type

svn co svn://svn.ps2dev.org/psp/trunk/pspsdk/
cd pspsdk
make
make install
Enlighten me, Reveal my fate -- Follow - Breaking Benjamin
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

I'm not use the Cygwin, i use the Heimdall win32 like toolchain!!
PiCkDaT
Posts: 69
Joined: Thu Oct 04, 2007 9:49 pm

Post by PiCkDaT »

hmm... well you should try to go with Cygwin if you want easy updates.. Cygwin lets you install libs and update the SDK easily.. downside to installing Cygwin and toolchain is it takes forever.. but after that it works fine.
Enlighten me, Reveal my fate -- Follow - Breaking Benjamin
Gh0st-UPMS
Posts: 23
Joined: Sun Feb 17, 2008 12:32 am

Post by Gh0st-UPMS »

PiCkDaT wrote:in cygwin type

svn co svn://svn.ps2dev.org/psp/trunk/pspsdk/
cd pspsdk
make
make install
If you get errors saying no make file then add ./bootstrap then type the above commands.

It's in the Read Me that comes with the sdk.
homemister
Posts: 25
Joined: Mon Mar 24, 2008 12:16 pm

Post by homemister »

does anyony know how to use the Socket Functions eg socket:recv() socket:send(). to download files from the web?.
Regards
Homemister
pegasus2000
Posts: 160
Joined: Wed Jul 12, 2006 7:09 am

Post by pegasus2000 »

homemister wrote:does anyony know how to use the Socket Functions eg socket:recv() socket:send(). to download files from the web?.
Regards
Homemister

Install Nanodesktop. In 3 rows of code you can download what you
want....
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

homemister wrote:does anyony know how to use the Socket Functions eg socket:recv() socket:send(). to download files from the web?
You use them the same way you would if you weren't on a PSP.

Seriously, folks, some of y'all need some refresher courses on programming, and we don't do that here.
wagon
Posts: 8
Joined: Tue Mar 18, 2008 8:08 pm

headfiles

Post by wagon »

src/GameApp.cpp: In function 'int net_thread(SceSize, void*)':
src/GameApp.cpp:197: error: 'sceHttpInit' was not declared in this scope
src/GameApp.cpp:204: error: expected unqualified-id before 'template'
src/GameApp.cpp:206: error: expected primary-expression before 'template'
src/GameApp.cpp:206: error: expected `)' before 'template'
src/GameApp.cpp:211: error: expected primary-expression before 'template'
src/GameApp.cpp:211: error: 'sceHttpSetResolveTimeOut' was not declared in this scope
src/GameApp.cpp:216: error: expected primary-expression before 'template'
src/GameApp.cpp:216: error: 'sceHttpSetRecvTimeOut' was not declared in this scope
src/GameApp.cpp:221: error: expected primary-expression before 'template'
src/GameApp.cpp:221: error: 'sceHttpSetSendTimeOut' was not declared in this scope
src/GameApp.cpp:226: error: expected primary-expression before 'template'
src/GameApp.cpp:226: error: 'sceHttpCreateConnectionWithURL' was not declared in this s
src/GameApp.cpp:232: error: 'sceHttpCreateRequestWithURL' was not declared in this scop
src/GameApp.cpp:238: error: 'sceHttpSendRequest' was not declared in this scope
src/GameApp.cpp:243: error: 'sceHttpGetStatusCode' was not declared in this scope
src/GameApp.cpp:250: error: 'sceHttpGetContentLength' was not declared in this scope
src/GameApp.cpp:256: warning: format '%d' expects type 'int', but argument 2 has type '
src/GameApp.cpp:258: error: 'sceHttpReadData' was not declared in this scope


code without headfiles

the head files? where? any one tell me?
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Insert_witty_name wrote:I've commited libhttp to the SDK, revision 2376.
Use header and library from the SDK. psphttp.h and -lpsphttp
wagon
Posts: 8
Joined: Tue Mar 18, 2008 8:08 pm

Post by wagon »

Insert_witty_name wrote:
Insert_witty_name wrote:I've commited libhttp to the SDK, revision 2376.
Use header and library from the SDK. psphttp.h and -lpsphttp

i'm bad in english. i still don't know what you mean.
there is no psphttp.h in my pspsdk include dir.
may be it's not the latest one.
where can i find your psphttp.h and the lib.
thank you very much.
DeathCradle
Posts: 6
Joined: Sat Jan 19, 2008 10:56 pm
Contact:

Post by DeathCradle »

Read PiCkDaT's post in this thread, he told ne0h how to.
PiCkDaT wrote:in cygwin type

svn co svn://svn.ps2dev.org/psp/trunk/pspsdk/
cd pspsdk
make
make install
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

@FaderX: What exactly was going on when you started up your application? Did anything go wrong, or did nothing work?
FaderX
Posts: 28
Joined: Sat Mar 01, 2008 12:38 pm

Post by FaderX »

Vincent_M wrote:@FaderX: What exactly was going on when you started up your application? Did anything go wrong, or did nothing work?
it loaded until i added libhttp functions. i also tried a prx which worked until it reached the http functions, moonlight has to be right about the global variable. im guna try it again when i get a working app.
Post Reply