#ifdef _PSP
#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);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
#define printf(...) pspDebugScreenPrintf(__VA_ARGS__)
+#define STDERR_LOG(...) pspDebugScreenPrintf(__VA_ARGS__)
+#else
+#define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
#endif
extern bool cm_do_log;
}
#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;
#ifdef SERVICE
#ifdef _PSP
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;
}
} else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
i++;
if(argv[i] == NULL) {
- fprintf(stderr, "Missing argument\n");
+ STDERR_LOG("Missing argument\n");
return 1;
}
confpath = argv[i];
} else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
i++;
if(argv[i] == NULL) {
- fprintf(stderr, "Missing argument\n");
+ STDERR_LOG("Missing argument\n");
return 1;
}
if(logfile != NULL && logfile != stderr) {
}
logfile = fopen(argv[i], "a");
if(logfile == NULL) {
- fprintf(stderr, "Failed to open logfile\n");
+ STDERR_LOG("Failed to open logfile\n");
return 1;
}
#endif
printf("--version | -V : Version information\n");
return 0;
} else {
- fprintf(stderr, "Unknown option: %s\n", argv[i]);
+ STDERR_LOG("Unknown option: %s\n", argv[i]);
return 1;
}
}
}
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;
}
sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
unsigned int WINAPI tw_server_pass(void* ptr) {
#elif defined(__HAIKU__)
int32_t tw_server_pass(void* ptr) {
+#elif defined(_PSP)
+int tw_server_pass(void* ptr) {
#endif
-#if defined(__HAIKU__) || defined(__MINGW32__)
+#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP)
int sock = ((struct pass_entry*)ptr)->sock;
bool ssl = ((struct pass_entry*)ptr)->ssl;
int port = ((struct pass_entry*)ptr)->port;
};
#endif
+extern int running;
+
void tw_server_loop(void) {
int i;
#if defined(__MINGW32__) || defined(__HAIKU__)
fd_set fdset;
struct timeval tv;
#endif
- while(1) {
+ while(running) {
#ifdef USE_POLL
int ret = poll(pollfds, sockcount, 1000);
#else
socklen_t clen = sizeof(claddr);
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;
e->ssl = config.ports[i] & (1ULL << 32);
break;
}
}
+#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;
}
- }
- 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;
+ 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;
+ }
+ }
#else
pid_t pid = fork();
if(pid == 0) {