1111#include <psp2/net/net.h>
1212#include <psp2/net/netctl.h>
1313#include <psp2/types.h>
14+ #include <psp2/kernel/clib.h>
1415#include "debugnet.h"
1516
16-
17-
18-
19- static int debugnet_initialized = 0 ;
20- int SocketFD = -1 ;
17+ int debugnet_external_conf = 0 ;
18+ debugNetConfiguration * dconfig = NULL ;
2119static void * net_memory = NULL ;
2220static SceNetInAddr vita_addr ;
23- int sceClibVsnprintf (char * , SceSize , const char * , va_list );
24- int logLevel = INFO ;
2521
2622/**
2723 * UDP printf for debugnet library
@@ -38,7 +34,7 @@ void debugNetUDPPrintf(const char* fmt, ...)
3834 va_start (arg , fmt );
3935 sceClibVsnprintf (buffer , sizeof (buffer ), fmt , arg );
4036 va_end (arg );
41- sceNetSend (SocketFD , buffer , strlen (buffer ), 0 );
37+ sceNetSend (dconfig -> SocketFD , buffer , strlen (buffer ), 0 );
4238}
4339/**
4440 * Log Level printf for debugnet library
@@ -55,7 +51,7 @@ void debugNetPrintf(int level, char* format, ...)
5551 char msgbuf [0x800 ];
5652 va_list args ;
5753
58- if (level > logLevel )
54+ if (level > dconfig -> logLevel )
5955 return ;
6056
6157 va_start (args , format );
@@ -92,7 +88,10 @@ void debugNetPrintf(int level, char* format, ...)
9288 */
9389void debugNetSetLogLevel (int level )
9490{
95- logLevel = level ;
91+ if (dconfig )
92+ {
93+ dconfig -> logLevel = level ;
94+ }
9695}
9796/**
9897 * Init debugnet library
@@ -114,10 +113,15 @@ int debugNetInit(char *serverIp, int port, int level)
114113 SceNetInitParam initparam ;
115114 SceNetCtlInfo info ;
116115 struct SceNetSockaddrIn stSockAddr ;
116+
117+ if (debugNetCreateConf ())
118+ {
119+ return dconfig -> debugnet_initialized ;
120+ }
121+
117122 debugNetSetLogLevel (level );
118- if (debugnet_initialized ) {
119- return debugnet_initialized ;
120- }
123+
124+
121125 /*net initialazation code from xerpi at https://github.com/xerpi/FTPVita/blob/master/ftp.c*/
122126 /* Init Net */
123127 if (sceNetShowNetstat () == SCE_NET_ERROR_ENOTINIT ) {
@@ -147,7 +151,7 @@ int debugNetInit(char *serverIp, int port, int level)
147151 sceNetInetPton (SCE_NET_AF_INET , info .ip_address , & vita_addr );
148152
149153 /* Create datagram udp socket*/
150- SocketFD = sceNetSocket ("debugnet_socket" ,
154+ dconfig -> SocketFD = sceNetSocket ("debugnet_socket" ,
151155 SCE_NET_AF_INET , SCE_NET_SOCK_DGRAM , SCE_NET_IPPROTO_UDP );
152156
153157 memset (& stSockAddr , 0 , sizeof stSockAddr );
@@ -158,7 +162,7 @@ int debugNetInit(char *serverIp, int port, int level)
158162 sceNetInetPton (SCE_NET_AF_INET , serverIp , & stSockAddr .sin_addr );
159163
160164 /*Connect socket to server*/
161- sceNetConnect (SocketFD , (struct SceNetSockaddr * )& stSockAddr , sizeof stSockAddr );
165+ sceNetConnect (dconfig -> SocketFD , (struct SceNetSockaddr * )& stSockAddr , sizeof stSockAddr );
162166
163167 /*Show log on pc/mac side*/
164168 debugNetUDPPrintf ("debugnet initialized\n" );
@@ -169,11 +173,67 @@ int debugNetInit(char *serverIp, int port, int level)
169173 debugNetUDPPrintf ("ready to have a lot of fun...\n" );
170174
171175 /*library debugnet initialized*/
172- debugnet_initialized = 1 ;
176+ dconfig -> debugnet_initialized = 1 ;
173177
174- return debugnet_initialized ;
178+ return dconfig -> debugnet_initialized ;
175179}
176180
181+ debugNetConfiguration * debugNetGetConf ()
182+ {
183+ if (dconfig )
184+ {
185+ return dconfig ;
186+ }
187+
188+ return NULL ;
189+ }
190+ int debugNetSetConf (debugNetConfiguration * conf )
191+ {
192+ if (conf )
193+ {
194+ dconfig = conf ;
195+ debugnet_external_conf = 1 ;
196+ return dconfig -> debugnet_initialized ;
197+ }
198+
199+ return 0 ;
200+ }
201+ int debugNetInitWithConf (debugNetConfiguration * conf )
202+ {
203+ int ret ;
204+ ret = debugNetSetConf (conf );
205+ if (ret )
206+ {
207+ debugNetPrintf (INFO ,"debugnet already initialized using configuration from ps4link\n" );
208+ debugNetPrintf (INFO ,"debugnet_initialized=%d SocketFD=%d logLevel=%d\n" ,dconfig -> debugnet_initialized ,dconfig -> SocketFD ,dconfig -> logLevel );
209+ debugNetPrintf (INFO ,"ready to have a lot of fun...\n" );
210+ return dconfig -> debugnet_initialized ;
211+ }
212+ else
213+ {
214+ return 0 ;
215+ }
216+
217+ }
218+ int debugNetCreateConf ()
219+ {
220+ if (!dconfig )
221+ {
222+ dconfig = malloc (sizeof (debugNetConfiguration ));
223+ dconfig -> debugnet_initialized = 0 ;
224+ dconfig -> SocketFD = -1 ;
225+ dconfig -> logLevel = INFO ;
226+ return 0 ;
227+ }
228+
229+ if (dconfig -> debugnet_initialized )
230+ {
231+ return 1 ;
232+ }
233+ return 0 ;
234+ }
235+
236+
177237/**
178238 * Finish debugnet library
179239 *
@@ -184,16 +244,18 @@ int debugNetInit(char *serverIp, int port, int level)
184244 */
185245void debugNetFinish ()
186246{
187- if (debugnet_initialized ) {
188-
189- sceNetCtlTerm ();
190- sceNetTerm ();
191-
192- if (net_memory ) {
193- free (net_memory );
194- net_memory = NULL ;
195- }
196-
197- debugnet_initialized = 0 ;
198- }
247+
248+ if (!debugnet_external_conf )
249+ {
250+ if (dconfig -> debugnet_initialized ) {
251+ dconfig -> debugnet_initialized = 0 ;
252+ dconfig -> SocketFD = -1 ;
253+
254+ if (net_memory ) {
255+ free (net_memory );
256+ net_memory = NULL ;
257+ }
258+ }
259+ }
260+
199261}
0 commit comments