forked from F0bes/gs4ps2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
189 lines (162 loc) · 4.5 KB
/
config.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "common.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <ps2ip.h>
const char* CFG_NAMES[COUNT_CFG] =
{
"dhcp",
"ip",
"nm",
"gw",
"priv-synch",
"priv-csr-aware",
"transfer-timeout",
"transfer-msg-timeout",
"udptty",
"net-dbg-msg",
"dump-frames"};
void* CFG_VALS[COUNT_CFG];
const char* CFG_DEFAULT =
R"(# Configuration generated by gs4ps2.
# Networking settings #
# Use DHCP
dhcp=0
# Static addresses to use when DHCP is disabled
ip=192.168.1.10
nm=255.255.255.0
gw=192.168.1.1
# GS settings.
# Optionally set SYNCHV, SYNCH1, SYNCH2, SRFSH priviledged registers
# Some dumps can set them to values incompatible with your television
priv-synch=1
# Match CSR:FIELD privileged register with hardware CSR:FIELD
priv-csr-aware=1
# GIF transfer timeout in (0 to disable, decimal)
transfer-timeout=300
# Transfer timeout message timeout
transfer-msg-timeout=1000
# Communication settings.
# Toggles 'udptty', might be slower but you can use ps2client to see logging
udptty=1
# Toggles networking messages, great speedup when off
net-dbg-msg=0
# Toggles dumping frames back over the network
dump-frames=1
)";
int cfg_parse_s32(u32 cfg_index, const char* str)
{
dprint("Parsing int: %s, index %d\n", str, cfg_index);
if (sscanf(str, "%d", (s32*)CFG_VALS[cfg_index]) != 1)
return -1;
return 0;
}
int cfg_parse_u32(u32 cfg_index, const char* str)
{
dprint("Parsing int: %s, index %d\n", str, cfg_index);
if (sscanf(str, "%u", (u32*)CFG_VALS[cfg_index]) != 1)
return -1;
return 0;
}
int cfg_parse_ip4_addr(u32 cfg_index, const char* str)
{
dprint("Parsing ip: %s\n", str);
u32 addr1, addr2, addr3, addr4;
if (sscanf(str, "%d.%d.%d.%d", &addr1, &addr2, &addr3, &addr4) != 4)
return -1;
IP4_ADDR((struct ip4_addr*)CFG_VALS[cfg_index], addr1, addr2, addr3, addr4);
return 0;
}
int (*CFG_PARSE[COUNT_CFG])(u32 cfg_index, const char* str) =
{
cfg_parse_s32,
cfg_parse_ip4_addr,
cfg_parse_ip4_addr,
cfg_parse_ip4_addr,
cfg_parse_s32,
cfg_parse_s32,
cfg_parse_u32,
cfg_parse_u32,
cfg_parse_s32,
cfg_parse_s32,
cfg_parse_s32,
};
FILE* g_cfgFile;
// Returns 0 on default options loaded
// Returns 1 on configuration file
u32 LoadDefaults(void)
{
// Wish I had templating, why did I choose C. xP
CFG_VALS[CFG_OPT_DHCP] = malloc(sizeof(s32));
*(s32*)CFG_VALS[CFG_OPT_DHCP] = 0;
CFG_VALS[CFG_OPT_IP] = malloc(sizeof(struct ip4_addr));
ip4_addr_set_zero((struct ip4_addr*)CFG_VALS[CFG_OPT_IP]);
CFG_VALS[CFG_OPT_NM] = malloc(sizeof(struct ip4_addr));
ip4_addr_set_zero((struct ip4_addr*)CFG_VALS[CFG_OPT_NM]);
CFG_VALS[CFG_OPT_GW] = malloc(sizeof(struct ip4_addr));
ip4_addr_set_zero((struct ip4_addr*)CFG_VALS[CFG_OPT_GW]);
CFG_VALS[CFG_OPT_SYNCH_PRIV] = malloc(sizeof(s32));
*(s32*)CFG_VALS[CFG_OPT_SYNCH_PRIV] = 1;
CFG_VALS[CFG_OPT_PRIV_CSR_AWARE] = malloc(sizeof(s32));
*(s32*)CFG_VALS[CFG_OPT_PRIV_CSR_AWARE] = 1;
CFG_VALS[CFG_OPT_GIF_TIMEOUT] = malloc(sizeof(u32));
*(u32*)CFG_VALS[CFG_OPT_GIF_TIMEOUT] = 300;
CFG_VALS[CFG_OPT_GIF_MSG_TIMEOUT] = malloc(sizeof(u32));
*(u32*)CFG_VALS[CFG_OPT_GIF_MSG_TIMEOUT] = 1000;
CFG_VALS[CFG_OPT_UDPTTY] = malloc(sizeof(u32));
*(s32*)CFG_VALS[CFG_OPT_UDPTTY] = 1;
CFG_VALS[CFG_OPT_NET_DBG_MSG] = malloc(sizeof(s32));
*(s32*)CFG_VALS[CFG_OPT_NET_DBG_MSG] = 0;
CFG_VALS[CFG_OPT_FRAME_DUMP] = malloc(sizeof(s32));
*(s32*)CFG_VALS[CFG_OPT_FRAME_DUMP] = 1;
g_cfgFile = fopen("host:config.txt", "r");
if (g_cfgFile == NULL)
{
// The configuration file does not exist, create one
dprint("No configuration file exists! Attempting to create one.\n");
g_cfgFile = fopen("host:config.txt", "w");
if (g_cfgFile != NULL)
{
// We have created a configuration file
fwrite(CFG_DEFAULT, strlen(CFG_DEFAULT), 1, g_cfgFile);
dprint("Created a configuration file with default values.\n");
fclose(g_cfgFile); // Possibly fflush would work better here?
g_cfgFile = fopen("host:config.txt", "r");
return 1;
}
else
{
dprint("Failed to generate a config.txt\n");
}
return 0;
}
return 1;
}
void LoadConfig(void)
{
memset(CFG_VALS, 0, sizeof(CFG_VALS));
if (LoadDefaults())
{
char line[256];
while (fgets(line, 256, g_cfgFile) != NULL)
{
char* equals = strchr(line, '=');
if (equals == NULL)
continue;
*equals = '\0';
equals++;
for (int i = 0; i < COUNT_CFG; i++)
{
if (strcmp(line, CFG_NAMES[i]) == 0)
{
if (CFG_PARSE[i](i, equals) != 0)
dprint("Failed to parse %s\n", line);
break;
}
}
}
fclose(g_cfgFile);
}
}