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