source: Main/trunk/Server/main.c@ 193

Last change on this file since 193 was 193, checked in by Nishi, on Sep 29, 2024 at 2:55:18 PM

wip but should work

  • Property svn:keywords set to Id
File size: 15.3 KB
RevLine 
[2]1/* $Id: main.c 193 2024-09-29 05:55:18Z nishi $ */
2
[16]3#define SOURCE
4
[43]5#include "../config.h"
6
[189]7#include <unistd.h>
[3]8#include <stdio.h>
9#include <stdbool.h>
10#include <string.h>
[16]11#include <signal.h>
[116]12#include <stdlib.h>
[3]13
[43]14#ifndef NO_SSL
[6]15#include <openssl/opensslv.h>
[43]16#endif
[6]17
[3]18#include <cm_log.h>
[62]19#include <cm_string.h>
[3]20
[4]21#include "tw_config.h"
[8]22#include "tw_server.h"
[3]23#include "tw_version.h"
24
[51]25#ifdef __MINGW32__
26#include <windows.h>
27#endif
28
[182]29#ifdef _PSP
30#include <pspkernel.h>
31#include <pspdebug.h>
[183]32#include <pspsdk.h>
33#include <psputility.h>
34#include <pspctrl.h>
35#include <pspnet_apctl.h>
36#include <pspwlan.h>
[182]37
38PSP_MODULE_INFO("Tewi HTTPd", PSP_MODULE_USER, 1, 1);
39PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
40
41#define printf(...) pspDebugScreenPrintf(__VA_ARGS__)
[183]42#define STDERR_LOG(...) pspDebugScreenPrintf(__VA_ARGS__)
[189]43#elif defined(__ps2sdk__)
44#include <debug.h>
45#include <sifrpc.h>
46
47#define printf(...) scr_printf(__VA_ARGS__)
48#define STDERR_LOG(...) scr_printf(__VA_ARGS__)
[191]49#elif defined(__PPU__)
50#include <rsx/gcm_sys.h>
51#include <rsx/rsx.h>
52#include <sysutil/video.h>
53#include <malloc.h>
54#include <sys/thread.h>
55#include <stdarg.h>
56
57#define printf(...) tt_printf(__VA_ARGS__)
58#define STDERR_LOG(...) tt_printf(__VA_ARGS__)
[183]59#else
60#define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
[182]61#endif
62
[3]63extern bool cm_do_log;
[18]64extern struct tw_config config;
[62]65extern FILE* logfile;
[3]66
[18]67char tw_server[2048];
68
[62]69int startup(int argc, char** argv);
70
71#ifdef SERVICE
72SERVICE_STATUS status;
73SERVICE_STATUS_HANDLE status_handle;
74
[70]75void WINAPI servhandler(DWORD control) {
76 switch(control) {
77 case SERVICE_CONTROL_STOP:
78 case SERVICE_CONTROL_SHUTDOWN:
79 status.dwCurrentState = SERVICE_STOP_PENDING;
80 break;
[62]81 }
82 SetServiceStatus(status_handle, &status);
83}
84
[70]85void WINAPI servmain(DWORD argc, LPSTR* argv) {
[62]86 logfile = fopen(PREFIX "/logs/tewi.log", "a");
87 if(logfile == NULL) logfile = stderr;
88 status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
89 status.dwCurrentState = SERVICE_START_PENDING;
90 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
91 status.dwWin32ExitCode = NO_ERROR;
92 status.dwServiceSpecificExitCode = 0;
93 status.dwCheckPoint = 0;
94 status.dwWaitHint = 0;
95 status_handle = RegisterServiceCtrlHandler("Tewi HTTPd", servhandler);
96 if(status_handle == NULL) return;
97 if(SetServiceStatus(status_handle, &status) == 0) return;
98 int st = startup(argc, argv);
[70]99 if(st != -1) {
[62]100 status.dwWin32ExitCode = NO_ERROR;
101 status.dwServiceSpecificExitCode = st;
102 status.dwCurrentState = SERVICE_STOPPED;
103 SetServiceStatus(status_handle, &status);
104 return;
105 }
106 status.dwCurrentState = SERVICE_RUNNING;
107 SetServiceStatus(status_handle, &status);
108 tw_server_loop();
109 status.dwCurrentState = SERVICE_STOPPED;
110 SetServiceStatus(status_handle, &status);
111}
112#endif
113
[183]114int running = 1;
115#ifdef _PSP
116
117int psp_exit_callback(int arg1, int arg2, void* arg3) { running = 0; }
118
119int psp_callback_thread(SceSize args, void* argp) {
120 int cid;
121 cid = sceKernelCreateCallback("Exit Call Back", psp_exit_callback, NULL);
122 sceKernelRegisterExitCallback(cid);
123 sceKernelSleepThreadCB();
124 return 0;
125}
126#endif
127
[191]128#ifdef __PPU__
129uint32_t depth_pitch;
130uint32_t depth_offset;
131uint32_t* depth_buffer;
132
133#define CB_SIZE 0x100000
134#define HOST_SIZE (32 * 1024 * 1024)
135
136struct rsx_buffer {
137 int width, height, id;
138 uint32_t* ptr;
139 uint32_t offset;
140};
141
142void wait_rsx(gcmContextData* ctx, uint32_t label) {
143 rsxSetWriteBackendLabel(ctx, GCM_INDEX_TYPE_32B, label);
144
145 rsxFlushBuffer(ctx);
146
147 while(*(uint32_t*)gcmGetLabelAddress(GCM_INDEX_TYPE_32B) != label) usleep(50);
148
149 label++;
150}
151
152void wait_rsx_until_idle(gcmContextData* ctx) {
153 uint32_t label = 1;
154 rsxSetWriteBackendLabel(ctx, GCM_INDEX_TYPE_32B, label);
155 rsxSetWaitLabel(ctx, GCM_INDEX_TYPE_32B, label);
156 label++;
157 wait_rsx(ctx, label);
158}
159
160void get_resolution(int* width, int* height) {
161 videoState state;
162 videoResolution res;
163 if(videoGetState(0, 0, &state) != 0) {
164 return;
165 }
166
167 if(state.state != 0) {
168 return;
169 }
170
171 if(videoGetResolution(state.displayMode.resolution, &res) != 0) {
172 return;
173 }
174 *width = res.width;
175 *height = res.height;
176}
177
178void make_buffer(struct rsx_buffer* buffer, int id) {
179 int w, h;
180 get_resolution(&w, &h);
181
182 buffer->ptr = (uint32_t*)rsxMemalign(64, 4 * w * h);
183 if(buffer->ptr == NULL) return;
184
185 if(rsxAddressToOffset(buffer->ptr, &buffer->offset) != 0) return;
186
187 if(gcmSetDisplayBuffer(id, buffer->offset, 4 * w, w, h) != 0) return;
188 buffer->width = w;
189 buffer->height = h;
190 buffer->id = id;
191}
192
193gcmContextData* init_screen(void) {
194 void* host = memalign(1024 * 1024, HOST_SIZE);
195 gcmContextData* ctx = NULL;
196 videoState state;
197 videoConfiguration vconfig;
198 videoResolution res;
199 rsxInit(&ctx, CB_SIZE, HOST_SIZE, host);
200 if(ctx == NULL) {
201 free(host);
202 return NULL;
203 }
204
205 if(videoGetState(0, 0, &state) != 0) {
206 rsxFinish(ctx, 0);
207 free(host);
208 return NULL;
209 }
210
211 if(state.state != 0) {
212 rsxFinish(ctx, 0);
213 free(host);
214 return NULL;
215 }
216
217 if(videoGetResolution(state.displayMode.resolution, &res) != 0) {
218 rsxFinish(ctx, 0);
219 free(host);
220 return NULL;
221 }
222
223 memset(&vconfig, 0, sizeof(vconfig));
224 vconfig.resolution = state.displayMode.resolution;
225 vconfig.format = VIDEO_BUFFER_FORMAT_XRGB;
226 vconfig.pitch = res.width * 4;
227 vconfig.aspect = state.displayMode.aspect;
228
229 wait_rsx_until_idle(ctx);
230
231 if(videoConfigure(0, &vconfig, NULL, 0) != 0) {
232 rsxFinish(ctx, 0);
233 free(host);
234 return NULL;
235 }
236
237 if(videoGetState(0, 0, &state) != 0) {
238 rsxFinish(ctx, 0);
239 free(host);
240 return NULL;
241 }
242 gcmSetFlipMode(GCM_FLIP_VSYNC);
243
244 depth_pitch = res.width * 4;
245 depth_buffer = (uint32_t*)rsxMemalign(64, (res.height * depth_pitch) * 2);
246 rsxAddressToOffset(depth_buffer, &depth_offset);
247
248 gcmResetFlipStatus();
249
250 return ctx;
251}
252
253void set_render_target(gcmContextData* context, struct rsx_buffer* buffer) {
254 gcmSurface sf;
255
256 sf.colorFormat = GCM_SURFACE_X8R8G8B8;
257 sf.colorTarget = GCM_SURFACE_TARGET_0;
258 sf.colorLocation[0] = GCM_LOCATION_RSX;
259 sf.colorOffset[0] = buffer->offset;
260 sf.colorPitch[0] = depth_pitch;
261
262 sf.colorLocation[1] = GCM_LOCATION_RSX;
263 sf.colorLocation[2] = GCM_LOCATION_RSX;
264 sf.colorLocation[3] = GCM_LOCATION_RSX;
265 sf.colorOffset[1] = 0;
266 sf.colorOffset[2] = 0;
267 sf.colorOffset[3] = 0;
268 sf.colorPitch[1] = 64;
269 sf.colorPitch[2] = 64;
270 sf.colorPitch[3] = 64;
271
272 sf.depthFormat = GCM_SURFACE_ZETA_Z16;
273 sf.depthLocation = GCM_LOCATION_RSX;
274 sf.depthOffset = depth_offset;
275 sf.depthPitch = depth_pitch;
276
277 sf.type = GCM_TEXTURE_LINEAR;
278 sf.antiAlias = GCM_SURFACE_CENTER_1;
279
280 sf.width = buffer->width;
281 sf.height = buffer->height;
282 sf.x = 0;
283 sf.y = 0;
284
285 rsxSetSurface(context, &sf);
286}
287
288void wait_flip(void) {
289 while(gcmGetFlipStatus() != 0) usleep(200);
290 gcmResetFlipStatus();
291}
292
293void flip(gcmContextData* ctx, uint32_t buffer) {
294 if(gcmSetFlip(ctx, buffer) == 0) {
295 rsxFlushBuffer(ctx);
296 gcmSetWaitFlip(ctx);
297 }
298}
299
300uint8_t* tvram;
301
302extern uint8_t font[];
303
304int tt_x = 0;
305int tt_y = 0;
306int tt_width;
307int tt_height;
308
309void tt_putstr(const char* str) {
310 int i;
[193]311 for(i = 0; str[i] != 0; i++) {
[191]312 tvram[tt_y * tt_width + tt_x] = str[i];
[193]313 if(str[i] == '\n') {
[191]314 tt_x = 0;
315 tt_y++;
[193]316 } else {
[191]317 tt_x++;
[193]318 if(tt_x == tt_width) {
[191]319 tt_x = 0;
320 tt_y++;
321 }
322 }
[193]323 if(tt_y == tt_height) {
[191]324 tt_y--;
325 int x, y;
[193]326 for(y = 0; y < tt_height - 1; y++) {
327 for(x = 0; x < tt_width; x++) {
[191]328 tvram[y * tt_width + x] = tvram[(y + 1) * tt_width + x];
329 }
330 }
[193]331 for(x = 0; x < tt_width; x++) {
[191]332 tvram[(tt_height - 1) * tt_width + x] = 0;
333 }
334 }
335 }
336}
337
[193]338void tt_putchar(struct rsx_buffer* buffer, int x, int y, uint8_t c) {
[191]339 int i, j;
340 if(c < 0x20) c = 0x20;
341 if(c >= 0x7f) c = 0x20;
[193]342 for(i = 0; i < 7; i++) {
[191]343 uint8_t l = font[(c - 0x20) * 8 + i];
[193]344 for(j = 0; j < 5; j++) {
[191]345 uint32_t c = 0;
[193]346 if(l & (1 << 7)) {
[191]347 c = 0xffffff;
348 }
349 l = l << 1;
350 buffer->ptr[(y * 8 + i) * buffer->width + x * 6 + j] = c;
351 }
352 }
353}
354
355void draw(struct rsx_buffer* buffer, int current) {
356 int i, j, c;
357 for(i = 0; i < buffer->height / 8; i++) {
358 for(j = 0; j < buffer->width / 6; j++) {
359 uint8_t c = tvram[i * (buffer->width / 6) + j];
360 tt_putchar(buffer, j, i, c);
361 }
362 }
363}
364
365#define BUFFERS 2
366gcmContextData* ctx;
367struct rsx_buffer buffers[BUFFERS];
368
369void text_thread(void* arg) {
370 int current = 0;
371 while(1) {
372 wait_flip();
373 draw(&buffers[current], current);
374 flip(ctx, buffers[current].id);
375 current++;
376 if(current >= BUFFERS) current = 0;
377 }
378}
379
380void tt_printf(const char* tmpl, ...) {
381 va_list va;
382 va_start(va, tmpl);
383 int i;
384 char cbuf[2];
385 cbuf[1] = 0;
386 char* log = cm_strdup("");
387 for(i = 0; tmpl[i] != 0; i++) {
388 if(tmpl[i] == '%') {
389 i++;
[193]390 if(tmpl[i] == 's') {
[191]391 char* tmp = log;
392 log = cm_strcat(tmp, va_arg(va, char*));
393 free(tmp);
[193]394 } else if(tmpl[i] == 'd') {
[191]395 char buf[513];
396 sprintf(buf, "%d", va_arg(va, int));
397 char* tmp = log;
398 log = cm_strcat(tmp, buf);
399 free(tmp);
[193]400 } else if(tmpl[i] == '%') {
[191]401 char* tmp = log;
402 log = cm_strcat(tmp, "%");
403 free(tmp);
404 }
405 } else {
406 cbuf[0] = tmpl[i];
407 char* tmp = log;
408 log = cm_strcat(tmp, cbuf);
409 free(tmp);
410 }
411 }
412 va_end(va);
413 tt_putstr(log);
414}
415
416#endif
417
[3]418int main(int argc, char** argv) {
[62]419 logfile = stderr;
420#ifdef SERVICE
421 SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
422 StartServiceCtrlDispatcher(table);
423#else
[182]424#ifdef _PSP
425 pspDebugScreenInit();
426 pspDebugScreenSetXY(0, 0);
[183]427 printf("PSP Bootstrap, Tewi/%s\n", tw_get_version());
428 int thid = sceKernelCreateThread("update_thread", psp_callback_thread, 0x11, 0xfa0, 0, NULL);
429 if(thid >= 0) {
430 sceKernelStartThread(thid, 0, NULL);
431 } else {
432 printf("Failed to start thread\n");
433 while(running) sceKernelDelayThread(50 * 1000);
434 sceKernelExitGame();
435 }
436 sceCtrlSetSamplingCycle(0);
437 sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
438 sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
439 sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
440 if(pspSdkInetInit()) {
441 printf("Could not init the network\n");
442 while(running) sceKernelDelayThread(50 * 1000);
443 sceKernelExitGame();
444 } else {
445 printf("Network initialization successful\n");
446 }
447 if(sceWlanGetSwitchState() != 1) {
448 printf("Turn the Wi-Fi switch on\n");
449 while(sceWlanGetSwitchState() != 1) {
450 sceKernelDelayThread(1000 * 1000);
451 }
452 } else {
453 printf("Wi-Fi is turned on\n");
454 }
455 int i;
456 int choice[100];
457 int incr = 0;
458 int last = 0;
459 int cur = 0;
460 for(i = 1; i < 100; i++) {
461 choice[i - 1] = 0;
462 netData name;
463 netData data;
464 if(sceUtilityCheckNetParam(i) != 0) continue;
465 choice[incr++] = i;
466 pspDebugScreenSetXY(0, 1 + 3 + incr - 1);
467 if(incr == 1) printf("> ");
468 pspDebugScreenSetXY(2, 1 + 3 + incr - 1);
469 sceUtilityGetNetParam(i, 0, &name);
470 sceUtilityGetNetParam(i, 1, &data);
471 printf("SSID=%s", data.asString);
472 sceUtilityGetNetParam(i, 4, &data);
473 if(data.asString[0]) {
474 sceUtilityGetNetParam(i, 5, &data);
475 printf(" IPADDR=%s\n", data.asString);
476 } else {
477 printf(" DHCP\n");
478 }
479 }
480 int press = 0;
481 while(1) {
482 if(!running) {
483 sceKernelExitGame();
484 }
485 SceCtrlData c;
486 sceCtrlReadBufferPositive(&c, 1);
487 press = 0;
488 if(c.Buttons & PSP_CTRL_DOWN) {
489 if(cur < incr - 1) {
490 cur++;
491 }
492 press = 1;
493 } else if(c.Buttons & PSP_CTRL_UP) {
494 if(cur > 0) {
495 cur--;
496 }
497 press = -1;
498 } else if(c.Buttons & PSP_CTRL_START) {
499 break;
500 }
501 if(last != cur) {
502 pspDebugScreenSetXY(0, 1 + 3 + last);
503 printf(" ");
504 pspDebugScreenSetXY(0, 1 + 3 + cur);
505 printf("> ");
506 last = cur;
507 }
508 if(press != 0) {
509 while(1) {
510 SceCtrlData c;
511 sceCtrlReadBufferPositive(&c, 1);
512 if(press == 1) {
513 if(!(c.Buttons & PSP_CTRL_DOWN)) break;
514 } else if(press == -1) {
515 if(!(c.Buttons & PSP_CTRL_UP)) break;
516 }
517 }
518 }
519 }
520 pspDebugScreenSetXY(0, 1 + 3 + incr + 1);
521 int err = sceNetApctlConnect(choice[cur]);
522 if(err != 0) {
523 printf("Apctl initialization failure\n");
524 while(running) sceKernelDelayThread(50 * 1000);
525 sceKernelExitGame();
526 } else {
527 printf("Apctl initialization successful\n");
528 }
529 printf("Apctl connecting\n");
530 while(1) {
531 int state;
532 err = sceNetApctlGetState(&state);
533 if(err != 0) {
534 printf("Apctl getting status failure\n");
535 while(running) sceKernelDelayThread(50 * 1000);
536 sceKernelExitGame();
537 }
538 if(state == 4) {
539 break;
540 }
541 sceKernelDelayThread(50 * 1000);
542 }
543 union SceNetApctlInfo info;
544 if(sceNetApctlGetInfo(8, &info) != 0) {
545 printf("Got an unknown IP\n");
546 while(running) sceKernelDelayThread(50 * 1000);
547 sceKernelExitGame();
548 }
549 printf("Connected, My IP is %s\n", info.ip);
[187]550#elif defined(__PPU__)
[191]551 int i;
552 ctx = init_screen();
553 int w, h;
554 get_resolution(&w, &h);
555 tt_width = w / 6;
556 tt_height = h / 8;
557 tvram = malloc((w / 6) * (h / 8));
558 for(i = 0; i < BUFFERS; i++) make_buffer(&buffers[i], i);
559 flip(ctx, BUFFERS - 1);
560 sys_ppu_thread_t id;
561 sysThreadCreate(&id, text_thread, NULL, 1500, 0x1000, THREAD_JOINABLE, "TextThread");
[187]562 printf("PS3 Bootstrap, Tewi/%s\n", tw_get_version());
563 netInitialize();
[189]564#elif defined(__ps2sdk__)
565 SifInitRpc(0);
566 init_scr();
567 scr_printf("PS2 Bootstrap, Tewi/%s\n", tw_get_version());
568 while(1)
569 ;
[182]570#endif
[62]571 int st = startup(argc, argv);
[183]572 if(st != -1) {
573#ifdef _PSP
574 printf("Error code %d\n", st);
575 while(running) sceKernelDelayThread(50 * 1000);
576 sceKernelExitGame();
577#else
[191]578#ifdef __PPU__
579 printf("Error code %d\n", st);
580 while(1)
581 ;
582#endif
[183]583 return st;
584#endif
585 }
[62]586 tw_server_loop();
587#endif
[183]588#ifdef _PSP
589 sceKernelExitGame();
590#endif
[168]591 return 0;
[62]592}
593
[70]594int startup(int argc, char** argv) {
[3]595 int i;
[18]596 const char* confpath = PREFIX "/etc/tewi.conf";
[70]597 if(argv != NULL) {
[62]598 for(i = 1; i < argc; i++) {
599 if(argv[i][0] == '-') {
600 if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
601 if(!cm_do_log) {
602 cm_do_log = true;
[70]603#ifndef NO_SSL
[62]604 cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
[70]605#else
[62]606 cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
[70]607#endif
[62]608 } else {
609 cm_do_log = true;
610 }
611 } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
612 i++;
613 if(argv[i] == NULL) {
[183]614 STDERR_LOG("Missing argument\n");
[62]615 return 1;
616 }
617 confpath = argv[i];
[182]618#ifndef _PSP
[117]619 } else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
620 i++;
621 if(argv[i] == NULL) {
[183]622 STDERR_LOG("Missing argument\n");
[117]623 return 1;
624 }
625 if(logfile != NULL && logfile != stderr) {
626 fclose(logfile);
627 }
628 logfile = fopen(argv[i], "a");
629 if(logfile == NULL) {
[183]630 STDERR_LOG("Failed to open logfile\n");
[117]631 return 1;
632 }
[182]633#endif
[62]634 } else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
635 printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
636 printf("Under public domain.\n");
637 printf("Original by 2024 Nishi\n");
638 printf("\n");
639 printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
[119]640 printf("--config | -C config : Specify config\n");
[182]641#ifndef _PSP
[119]642 printf("--logfile | -l logfile : Specify logfile\n");
[182]643#endif
[119]644 printf("--verbose | -v : Verbose mode\n");
645 printf("--version | -V : Version information\n");
[62]646 return 0;
[3]647 } else {
[183]648 STDERR_LOG("Unknown option: %s\n", argv[i]);
[4]649 return 1;
650 }
[3]651 }
652 }
653 }
[6]654 tw_config_init();
[18]655 if(tw_config_read(confpath) != 0) {
[183]656 STDERR_LOG("Could not read the config\n");
[4]657 return 1;
658 }
[8]659 if(tw_server_init() != 0) {
[183]660 STDERR_LOG("Could not initialize the server\n");
[8]661 return 1;
662 }
[18]663 sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
[62]664 char* r = cm_strcat(tw_server, " running...");
665 cm_force_log(r);
666 free(r);
[16]667#ifndef __MINGW32__
668 signal(SIGCHLD, SIG_IGN);
[90]669 signal(SIGPIPE, SIG_IGN);
[50]670#else
671 SetConsoleTitle(tw_server);
[16]672#endif
[62]673 return -1;
[3]674}
Note: See TracBrowser for help on using the repository browser.