|
| 1 | +#include <pspkernel.h> |
| 2 | +#include <pspdebug.h> |
| 3 | +#include <pspctrl.h> |
| 4 | +#include <pspsdk.h> |
| 5 | +#include <pspnet_apctl.h> |
| 6 | +#include <psputility.h> |
| 7 | +#include <arpa/inet.h> |
| 8 | + |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#define MODULE_NAME "PSP-CONTROLLER" |
| 12 | +#define IP_ADDRESS "192.168.4.1" |
| 13 | +#define PORT 80 |
| 14 | + |
| 15 | +PSP_MODULE_INFO(MODULE_NAME, PSP_MODULE_USER, 0, 1); |
| 16 | +PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU); |
| 17 | + |
| 18 | +PSP_HEAP_THRESHOLD_SIZE_KB(4096); |
| 19 | +PSP_HEAP_SIZE_KB(-4096); |
| 20 | +PSP_MAIN_THREAD_STACK_SIZE_KB(4096); |
| 21 | + |
| 22 | +int exit_cb(int arg0, int arg1, void *common) { |
| 23 | + sceKernelExitGame(); |
| 24 | + return 0; |
| 25 | +} |
| 26 | + |
| 27 | +int thread_cb(SceSize args, void *argp) { |
| 28 | + int cbid = sceKernelCreateCallback("exit_cb", exit_cb, NULL); |
| 29 | + |
| 30 | + sceKernelRegisterExitCallback(cbid); |
| 31 | + sceKernelSleepThreadCB(); |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +int setup_cb(void) { |
| 36 | + SceUID thid = sceKernelCreateThread("setup_cb", thread_cb, 0x10, 0x1000, PSP_THREAD_ATTR_USER, NULL); |
| 37 | + sceKernelStartThread(thid, 0, NULL); |
| 38 | + return thid; |
| 39 | +} |
| 40 | + |
| 41 | +int connect_apctl(int config) { |
| 42 | + int state; |
| 43 | + int last_state = -1; |
| 44 | + int error; |
| 45 | + |
| 46 | + /* Connect to first wifi profile */ |
| 47 | + error = sceNetApctlConnect(config); |
| 48 | + if (error < 0) { |
| 49 | + pspDebugScreenPrintf(MODULE_NAME ": sceNetApctlConnect returns %08x\n", error); |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + pspDebugScreenPrintf(MODULE_NAME ": Connecting...\n"); |
| 54 | + while (1) { |
| 55 | + error = sceNetApctlGetState(&state); |
| 56 | + if (error < 0) { |
| 57 | + pspDebugScreenPrintf(MODULE_NAME ": sceNetApctlGetState returns $%08x\n", error); |
| 58 | + break; |
| 59 | + } |
| 60 | + |
| 61 | + if (state > last_state) { |
| 62 | + pspDebugScreenPrintf("connection state %d of 4\n", state); |
| 63 | + last_state = state; |
| 64 | + } |
| 65 | + |
| 66 | + if (state == PSP_NET_APCTL_STATE_GOT_IP) { break; } |
| 67 | + sceKernelDelayThread(50 * 1000); // 50ms |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +int main(void) { |
| 72 | + setup_cb(); |
| 73 | + sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON); |
| 74 | + sceUtilityLoadNetModule(PSP_NET_MODULE_INET); |
| 75 | + pspDebugScreenInit(); |
| 76 | + |
| 77 | + int sock; |
| 78 | + int error; |
| 79 | + SceCtrlData pad; |
| 80 | + struct sockaddr_in name; |
| 81 | + const char ON[64] = "GET /on HTTP/1.1\r\nHost: " IP_ADDRESS "\r\nConnection: close\r\n\r\n"; |
| 82 | + const char OFF[64] = "GET /off HTTP/1.1\r\nHost: " IP_ADDRESS "\r\nConnection: close\r\n\r\n"; |
| 83 | + |
| 84 | + memset(&name, 0, sizeof(name)); |
| 85 | + sceCtrlSetSamplingCycle(0); |
| 86 | + sceCtrlSetSamplingMode(PSP_CTRL_MODE_DIGITAL); |
| 87 | + |
| 88 | + do { |
| 89 | + error = pspSdkInetInit(); |
| 90 | + if (error) { |
| 91 | + pspDebugScreenPrintf(MODULE_NAME ": pspSdkInetInit returns %08x\n", error); |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + connect_apctl(1); |
| 96 | + pspDebugScreenClear(); |
| 97 | + pspDebugScreenPrintf("==================================\n"); |
| 98 | + pspDebugScreenPrintf("psp-controller-client\n"); |
| 99 | + pspDebugScreenPrintf("GitHub: https://github.com/diamant3/psp-controller\n"); |
| 100 | + pspDebugScreenPrintf("==================================\n"); |
| 101 | + while (1) { |
| 102 | + pspDebugScreenSetXY(0, 4); |
| 103 | + sceCtrlReadBufferPositive(&pad, 1); |
| 104 | + |
| 105 | + if (pad.Buttons != 0) { |
| 106 | + sock = socket(PF_INET, SOCK_STREAM, 0); |
| 107 | + if (sock < 0) { return -1; } |
| 108 | + |
| 109 | + name.sin_family = AF_INET; |
| 110 | + name.sin_port = htons(PORT); |
| 111 | + inet_pton(AF_INET, IP_ADDRESS, &(name.sin_addr)); |
| 112 | + |
| 113 | + connect(sock, (struct sockaddr *)&name, sizeof(name)); |
| 114 | + |
| 115 | + if (pad.Buttons & PSP_CTRL_CIRCLE) { |
| 116 | + pspDebugScreenPrintf("LED Status: On "); |
| 117 | + send(sock, &ON, strlen(ON), 0); |
| 118 | + } |
| 119 | + |
| 120 | + if (pad.Buttons & PSP_CTRL_CROSS) { |
| 121 | + pspDebugScreenPrintf("LED Status: Off"); |
| 122 | + send(sock, &OFF, strlen(OFF), 0); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + sceKernelDelayThread(50 * 1000); // 50ms |
| 127 | + } |
| 128 | + } while (0); |
| 129 | + |
| 130 | + pspSdkInetTerm(); |
| 131 | + sceUtilityUnloadNetModule(PSP_NET_MODULE_COMMON); |
| 132 | + sceUtilityUnloadNetModule(PSP_NET_MODULE_INET); |
| 133 | + return 0; |
| 134 | +} |
0 commit comments