Index: /trunk/Platform/psp.mk
===================================================================
--- /trunk/Platform/psp.mk	(revision 182)
+++ /trunk/Platform/psp.mk	(revision 183)
@@ -1,9 +1,11 @@
 # $Id$
+
+PREFIX = ms0:/PSP/GAME/httpd
 
 CC = psp-gcc
 AR = psp-ar
 CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -I /usr/local/pspdev/psp/sdk/include -D_PSP_FW_VERSION=600
-LDFLAGS = -L /usr/local/pspdev/psp/sdk/lib
-LIBS = -lpspgum -lpspgu -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspnet -lpspnet_apctl
+LDFLAGS = -Wl,-zmax-page-size=128 -L /usr/local/pspdev/psp/sdk/lib
+LIBS = -lpspgum -lpspgu -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspnet -lpspnet_apctl -lcglue -lpspwlan
 EXEC = .elf
 LIB = .so
Index: /trunk/Server/Makefile
===================================================================
--- /trunk/Server/Makefile	(revision 182)
+++ /trunk/Server/Makefile	(revision 183)
@@ -17,5 +17,5 @@
 
 tewi.pbp: tewi_strip$(EXEC) param.sfo
-	pack-pbp $@ param.sfo NULL NULL NULL NULL NULL tewi_strip$(EXEC) NULL
+	pack-pbp $@ param.sfo ../Binary/psp.png NULL NULL NULL NULL tewi_strip$(EXEC) NULL
 
 param.sfo:
Index: /trunk/Server/main.c
===================================================================
--- /trunk/Server/main.c	(revision 182)
+++ /trunk/Server/main.c	(revision 183)
@@ -29,4 +29,9 @@
 #include <pspkernel.h>
 #include <pspdebug.h>
+#include <pspsdk.h>
+#include <psputility.h>
+#include <pspctrl.h>
+#include <pspnet_apctl.h>
+#include <pspwlan.h>
 
 PSP_MODULE_INFO("Tewi HTTPd", PSP_MODULE_USER, 1, 1);
@@ -34,4 +39,7 @@
 
 #define printf(...) pspDebugScreenPrintf(__VA_ARGS__)
+#define STDERR_LOG(...) pspDebugScreenPrintf(__VA_ARGS__)
+#else
+#define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
 #endif
 
@@ -87,4 +95,18 @@
 #endif
 
+int running = 1;
+#ifdef _PSP
+
+int psp_exit_callback(int arg1, int arg2, void* arg3) { running = 0; }
+
+int psp_callback_thread(SceSize args, void* argp) {
+	int cid;
+	cid = sceKernelCreateCallback("Exit Call Back", psp_exit_callback, NULL);
+	sceKernelRegisterExitCallback(cid);
+	sceKernelSleepThreadCB();
+	return 0;
+}
+#endif
+
 int main(int argc, char** argv) {
 	logfile = stderr;
@@ -96,8 +118,142 @@
 	pspDebugScreenInit();
 	pspDebugScreenSetXY(0, 0);
+	printf("PSP Bootstrap, Tewi/%s\n", tw_get_version());
+	int thid = sceKernelCreateThread("update_thread", psp_callback_thread, 0x11, 0xfa0, 0, NULL);
+	if(thid >= 0) {
+		sceKernelStartThread(thid, 0, NULL);
+	} else {
+		printf("Failed to start thread\n");
+		while(running) sceKernelDelayThread(50 * 1000);
+		sceKernelExitGame();
+	}
+	sceCtrlSetSamplingCycle(0);
+	sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
+	sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
+	sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
+	if(pspSdkInetInit()) {
+		printf("Could not init the network\n");
+		while(running) sceKernelDelayThread(50 * 1000);
+		sceKernelExitGame();
+	} else {
+		printf("Network initialization successful\n");
+	}
+	if(sceWlanGetSwitchState() != 1) {
+		printf("Turn the Wi-Fi switch on\n");
+		while(sceWlanGetSwitchState() != 1) {
+			sceKernelDelayThread(1000 * 1000);
+		}
+	} else {
+		printf("Wi-Fi is turned on\n");
+	}
+	int i;
+	int choice[100];
+	int incr = 0;
+	int last = 0;
+	int cur = 0;
+	for(i = 1; i < 100; i++) {
+		choice[i - 1] = 0;
+		netData name;
+		netData data;
+		if(sceUtilityCheckNetParam(i) != 0) continue;
+		choice[incr++] = i;
+		pspDebugScreenSetXY(0, 1 + 3 + incr - 1);
+		if(incr == 1) printf("> ");
+		pspDebugScreenSetXY(2, 1 + 3 + incr - 1);
+		sceUtilityGetNetParam(i, 0, &name);
+		sceUtilityGetNetParam(i, 1, &data);
+		printf("SSID=%s", data.asString);
+		sceUtilityGetNetParam(i, 4, &data);
+		if(data.asString[0]) {
+			sceUtilityGetNetParam(i, 5, &data);
+			printf(" IPADDR=%s\n", data.asString);
+		} else {
+			printf(" DHCP\n");
+		}
+	}
+	int press = 0;
+	while(1) {
+		if(!running) {
+			sceKernelExitGame();
+		}
+		SceCtrlData c;
+		sceCtrlReadBufferPositive(&c, 1);
+		press = 0;
+		if(c.Buttons & PSP_CTRL_DOWN) {
+			if(cur < incr - 1) {
+				cur++;
+			}
+			press = 1;
+		} else if(c.Buttons & PSP_CTRL_UP) {
+			if(cur > 0) {
+				cur--;
+			}
+			press = -1;
+		} else if(c.Buttons & PSP_CTRL_START) {
+			break;
+		}
+		if(last != cur) {
+			pspDebugScreenSetXY(0, 1 + 3 + last);
+			printf("  ");
+			pspDebugScreenSetXY(0, 1 + 3 + cur);
+			printf("> ");
+			last = cur;
+		}
+		if(press != 0) {
+			while(1) {
+				SceCtrlData c;
+				sceCtrlReadBufferPositive(&c, 1);
+				if(press == 1) {
+					if(!(c.Buttons & PSP_CTRL_DOWN)) break;
+				} else if(press == -1) {
+					if(!(c.Buttons & PSP_CTRL_UP)) break;
+				}
+			}
+		}
+	}
+	pspDebugScreenSetXY(0, 1 + 3 + incr + 1);
+	int err = sceNetApctlConnect(choice[cur]);
+	if(err != 0) {
+		printf("Apctl initialization failure\n");
+		while(running) sceKernelDelayThread(50 * 1000);
+		sceKernelExitGame();
+	} else {
+		printf("Apctl initialization successful\n");
+	}
+	printf("Apctl connecting\n");
+	while(1) {
+		int state;
+		err = sceNetApctlGetState(&state);
+		if(err != 0) {
+			printf("Apctl getting status failure\n");
+			while(running) sceKernelDelayThread(50 * 1000);
+			sceKernelExitGame();
+		}
+		if(state == 4) {
+			break;
+		}
+		sceKernelDelayThread(50 * 1000);
+	}
+	union SceNetApctlInfo info;
+	if(sceNetApctlGetInfo(8, &info) != 0) {
+		printf("Got an unknown IP\n");
+		while(running) sceKernelDelayThread(50 * 1000);
+		sceKernelExitGame();
+	}
+	printf("Connected, My IP is %s\n", info.ip);
 #endif
 	int st = startup(argc, argv);
-	if(st != -1) return st;
+	if(st != -1) {
+#ifdef _PSP
+		printf("Error code %d\n", st);
+		while(running) sceKernelDelayThread(50 * 1000);
+		sceKernelExitGame();
+#else
+		return st;
+#endif
+	}
 	tw_server_loop();
+#endif
+#ifdef _PSP
+	sceKernelExitGame();
 #endif
 	return 0;
@@ -124,5 +280,5 @@
 					i++;
 					if(argv[i] == NULL) {
-						fprintf(stderr, "Missing argument\n");
+						STDERR_LOG("Missing argument\n");
 						return 1;
 					}
@@ -132,5 +288,5 @@
 					i++;
 					if(argv[i] == NULL) {
-						fprintf(stderr, "Missing argument\n");
+						STDERR_LOG("Missing argument\n");
 						return 1;
 					}
@@ -140,5 +296,5 @@
 					logfile = fopen(argv[i], "a");
 					if(logfile == NULL) {
-						fprintf(stderr, "Failed to open logfile\n");
+						STDERR_LOG("Failed to open logfile\n");
 						return 1;
 					}
@@ -158,5 +314,5 @@
 					return 0;
 				} else {
-					fprintf(stderr, "Unknown option: %s\n", argv[i]);
+					STDERR_LOG("Unknown option: %s\n", argv[i]);
 					return 1;
 				}
@@ -166,9 +322,9 @@
 	tw_config_init();
 	if(tw_config_read(confpath) != 0) {
-		fprintf(stderr, "Could not read the config\n");
+		STDERR_LOG("Could not read the config\n");
 		return 1;
 	}
 	if(tw_server_init() != 0) {
-		fprintf(stderr, "Could not initialize the server\n");
+		STDERR_LOG("Could not initialize the server\n");
 		return 1;
 	}
Index: /trunk/Server/server.c
===================================================================
--- /trunk/Server/server.c	(revision 182)
+++ /trunk/Server/server.c	(revision 183)
@@ -431,6 +431,8 @@
 #elif defined(__HAIKU__)
 int32_t tw_server_pass(void* ptr) {
-#endif
-#if defined(__HAIKU__) || defined(__MINGW32__)
+#elif defined(_PSP)
+int tw_server_pass(void* ptr) {
+#endif
+#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP)
 	int sock = ((struct pass_entry*)ptr)->sock;
 	bool ssl = ((struct pass_entry*)ptr)->ssl;
@@ -855,4 +857,6 @@
 #endif
 
+extern int running;
+
 void tw_server_loop(void) {
 	int i;
@@ -873,5 +877,5 @@
 		struct timeval tv;
 #endif
-	while(1) {
+	while(running) {
 #ifdef USE_POLL
 		int ret = poll(pollfds, sockcount, 1000);
@@ -915,5 +919,5 @@
 					int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
 					cm_log("Server", "New connection accepted");
-#if defined(__MINGW32__) || defined(__HAIKU__)
+#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP)
 					struct pass_entry* e = malloc(sizeof(*e));
 					e->sock = sock;
@@ -941,27 +945,29 @@
 						}
 					}
+#elif defined(_PSP)
+						tw_server_pass(e);
 #elif defined(__HAIKU__)
-						int j;
-						for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
-							if(threads[j].used) {
-								thread_info info;
-								bool kill = false;
-								if(get_thread_info(threads[j].thread, &info) == B_OK) {
-								} else {
-									kill = true;
-								}
-								if(kill) {
-									threads[j].used = false;
-								}
+					int j;
+					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
+						if(threads[j].used) {
+							thread_info info;
+							bool kill = false;
+							if(get_thread_info(threads[j].thread, &info) == B_OK) {
+							} else {
+								kill = true;
+							}
+							if(kill) {
+								threads[j].used = false;
 							}
 						}
-						for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
-							if(!threads[j].used) {
-								threads[j].thread = spawn_thread(tw_server_pass, "Tewi HTTPd", 60, e);
-								threads[j].used = true;
-								resume_thread(threads[j].thread);
-								break;
-							}
+					}
+					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
+						if(!threads[j].used) {
+							threads[j].thread = spawn_thread(tw_server_pass, "Tewi HTTPd", 60, e);
+							threads[j].used = true;
+							resume_thread(threads[j].thread);
+							break;
 						}
+					}
 #else
 					pid_t pid = fork();
Index: /trunk/Server/version.c
===================================================================
--- /trunk/Server/version.c	(revision 182)
+++ /trunk/Server/version.c	(revision 183)
@@ -20,4 +20,6 @@
 #elif defined(__CYGWIN__)
     "Cygwin"
+#elif defined(_PSP)
+    "PSP"
 #else
     "Unix"
