[源码]调用psp系统的联网对话框连接wifi



掌叔
2010-01-17 09:27:28

[b]nettest.c[/b]
[code=cpp]
#include
#include
#include
#include
#include
#include

#include
#include
#include

#include

#include
#include
#include
#include
#include
#include
#include

PSP_MODULE_INFO("nettest", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);

PSP_HEAP_SIZE_KB(-4096);

/* Define the main thread's attribute value (optional) */
//PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);


/* Define printf, just to make typing easier */
//#define printf pspDebugScreenPrintf
//#define printf(x)

int connected=0;
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
return 0;
}


/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
int cbid;

// Exit callback
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;
}

static unsigned int __attribute__((aligned(16))) _list[262144];
unsigned int * _gu_list;
void* _gu_fb;

#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)

static void setup_gu() {

void* fbp0 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
void* fbp1 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
void* zbp = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444);

sceGuInit();
_gu_list = _list;
sceGuStart(GU_DIRECT,_gu_list);
sceGuDrawBuffer(GU_PSM_8888,fbp0,BUF_WIDTH);
sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH);
sceGuDepthBuffer(zbp,BUF_WIDTH);
sceGuOffset(2048 - (SCR_WIDTH/2),2048 - (SCR_HEIGHT/2));
sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT);
//sceGuDepthRange(65535,0);
sceGuDepthRange(0xc350,0x2710);
sceGuScissor(0,0,SCR_WIDTH,SCR_HEIGHT);
sceGuEnable(GU_SCISSOR_TEST);
sceGuFrontFace(GU_CW);
sceGuEnable(GU_TEXTURE_2D);

sceGuDepthFunc(GU_GEQUAL);
sceGuEnable(GU_DEPTH_TEST);

sceGuEnable(GU_CULL_FACE);
sceGuEnable(GU_CLIP_PLANES);


sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
sceGuFinish();
sceGuSync(0,0);
_gu_fb = fbp0;

sceDisplayWaitVblankStart();

sceKernelDcacheWritebackAll();
}

int network_init_start(void) {
int err;
int start = 0;



if((err = sceNetInit(384*1024, 42, 4*1024, 42, 4*1024))) {
printf(": Error, could not sceNetInit the network %08X
", err);
return -1;
}
if((err = sceNetInetInit())) {
printf(": Error, could not sceNetInetInit the network %08X
", err);
return -1;
}
if((err = sceNetResolverInit())) {
printf(": Error, could not sceNetResolverInit the network %08X
", err);
return -1;
}
if((err = sceNetApctlInit(0x8000, 48))) {
printf(": Error, could not sceNetApctlInit the network %08X
", err);
return -1;
}

return 0;
}


int netDialog(int status)
{
if(network_init_start()!=0)
return -1;
int state;
int buttonSwap = PSP_UTILITY_ACCEPT_CIRCLE;
char* res;
int running = 1;

if (status == 0) {
sceNetApctlGetState(&state);
if (state > 0) {
status = 1;
}
}

pspUtilityNetconfData data;

memset(&data, 0, sizeof(data));
data.base.size = sizeof(data);
data.base.language = PSP_SYSTEMPARAM_LANGUAGE_ENGLISH;
data.base.buttonSwap = buttonSwap;
data.base.graphicsThread = 17;
data.base.accessThread = 19;
data.base.fontThread = 18;
data.base.soundThread = 16;
data.action = status?PSP_NETCONF_ACTION_DISPLAYSTATUS:PSP_NETCONF_ACTION_CONNECTAP;
data.hotspot = 0;

sceUtilityNetconfInitStart(&data);

while(running)
{
switch(sceUtilityNetconfGetStatus())
{
case PSP_UTILITY_DIALOG_NONE:
running = 0;
break;

case PSP_UTILITY_DIALOG_VISIBLE:
sceUtilityNetconfUpdate(1);
sceDisplayWaitVblankStart();
sceGuSwapBuffers();
break;

case PSP_UTILITY_DIALOG_FINISHED:
break;

case PSP_UTILITY_DIALOG_QUIT:
sceUtilityNetconfShutdownStart();
break;

default:
break;
}
}
sceKernelDelayThread(500000);
sceNetApctlGetState(&state);
sceKernelDelayThread(500000);

return state==4?1:0;
}

int net_thread(SceSize args, void *argp)
{
int err;

do
{
if(1==connected)
{
break;
printf("connnected
");
}
else
{
printf("before: %d
", connected);
connected=netDialog(connected);
printf("after: %d
", connected);
}
}
while(0);

return 0;
}


int main(void)
{
SceUID thid;
int res = 0;
int ret_val = 0;

pspDebugScreenInit();
test_mem("Before setup GU");
printf("Setup GU
");
setup_gu();
test_mem("After setup GU");


printf("Loading network modules
");
if ((ret_val = sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON)) < 0) {
printf("Error, could not load inet modules %d
", PSP_NET_MODULE_COMMON);
goto fail;
}

if ((ret_val = sceUtilityLoadNetModule(PSP_NET_MODULE_INET)) < 0) {
printf("Error, could not load inet modules %d
", PSP_NET_MODULE_INET);
goto fail;
}


printf("Network module loaded
");


ret_val = 0;

/* create user thread */
thid = sceKernelCreateThread("net_thread",net_thread,
0x2f, // default priority
512 * 1024, // stack size
PSP_THREAD_ATTR_USER | PSP_THREAD_ATTR_VFPU, NULL); //# user mode

// start user thread, then wait for it to do everything else
sceKernelStartThread(thid, 0, 0);
test_mem("nettest thread started");
printf("sceKernelWaitThreadEnd...
");
sceKernelWaitThreadEnd(thid, NULL);
printf("sceKernelWaitThreadEnd OK
");

fail:
printf("sceKernelExitGame...
");
sceKernelExitGame();
printf("sceKernelExitGame OK
");

return ret_val;

}

void test_mem(const char* msg) {
int ii=64*1024*1024;
char* tmpp=NULL;
printf("Wait..
");
printf("Sys memory free:%d(total), %d(max)
", sceKernelTotalFreeMemSize(), sceKernelMaxFreeMemSize());
while(ii-=1024*1024) {
if ((tmpp=malloc(ii))!=NULL) {

free(tmpp);
break;
}
}
printf("[%s]MAX HEAP:%d
",msg, ii);
}
[/code]

[b]vram.h[/b]
[code=cpp]
#ifndef common_vram_h
#define common_vram_h

#ifdef __cplusplus
extern "C" {
#endif

/* make a static allocation of vram memory and return pointer relative to vram start */
void* getStaticVramBuffer(unsigned int width, unsigned int height, unsigned int psm);
/* make a static allocation of vram memory and return absolute pointer */
void* getStaticVramTexture(unsigned int width, unsigned int height, unsigned int psm);

#ifdef __cplusplus
}
#endif

#endif
[/code]

[b]vram.c[/b]
[code=cpp]
#include "vram.h"

#include
#include

static unsigned int staticOffset = 0;

static unsigned int getMemorySize(unsigned int width, unsigned int height, unsigned int psm)
{
switch (psm)
{
case GU_PSM_T4:
return (width * height) >> 1;

case GU_PSM_T8:
return width * height;

case GU_PSM_5650:
case GU_PSM_5551:
case GU_PSM_4444:
case GU_PSM_T16:
return 2 * width * height;

case GU_PSM_8888:
case GU_PSM_T32:
return 4 * width * height;

default:
return 0;
}
}

void* getStaticVramBuffer(unsigned int width, unsigned int height, unsigned int psm)
{
unsigned int memSize = getMemorySize(width,height,psm);
void* result = (void*)staticOffset;
staticOffset += memSize;

return result;
}

void* getStaticVramTexture(unsigned int width, unsigned int height, unsigned int psm)
{
void* result = getStaticVramBuffer(width,height,psm);
return (void*)(((unsigned int)result) + ((unsigned int)sceGeEdramGetAddr()));
}
[/code]
by:winston521


掌叔
2010-01-17 09:29:40

makefile
ps::请在psptoolchain下编译
TARGET = nettest
OBJS = nettest.o vram.o

INCDIR =
CFLAGS = -O0 -G0 -Wall -g
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
BUILD_PRX=1

LIBDIR =
LIBS +=-lpsphprm -lpspwlan -lpspgu -lpsprtc

EXTRA_TARGETS = EBOOT.PBP

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak


winston521
2010-01-19 11:03:56

咦???????