ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
ps2ip.c File Reference
#include <stdio.h>
#include <string.h>
#include <netman.h>
#include <lwip/memp.h>
#include "lwip/sys.h"
#include "lwip/tcp.h"
#include "lwip/tcpip.h"
#include "lwip/netif.h"
#include "lwip/dhcp.h"
#include "lwip/prot/dhcp.h"
#include "lwip/inet.h"
#include "netif/etharp.h"
#include "ps2ip_internal.h"
+ Include dependency graph for ps2ip.c:

Go to the source code of this file.

Functions

int ps2ip_getconfig (char *pszName, t_ip_info *pInfo)
 
int ps2ip_setconfig (const t_ip_info *pInfo)
 
static void EnQTxPacket (struct pbuf *tx)
 
static err_t SMapLowLevelOutput (struct netif *pNetIF, struct pbuf *pOutput)
 
static void LinkStateUp (void)
 
static void LinkStateDown (void)
 
static void * AllocRxPacket (unsigned int size, void **payload)
 
static void ReallocRxPacket (void *packet, unsigned int size)
 
static void FreeRxPacket (void *packet)
 
static void EnQRxPacket (void *packet)
 
static int NextTxPacket (void **payload)
 
static void DeQTxPacket (void)
 
static int AfterTxPacket (void **payload)
 
static void InitDone (void *pvArg)
 
static err_t SMapIFInit (struct netif *pNetIF)
 
err_t ps2ip_input (PBuf *pInput, NetIF *pNetIF)
 
static void InitializeLWIP (void)
 
int ps2ipInit (struct ip4_addr *ip_address, struct ip4_addr *subnet_mask, struct ip4_addr *gateway)
 
void ps2ipDeinit (void)
 
void ps2ipSetHsyncTicksPerMSec (unsigned char ticks)
 

Variables

static struct netif NIF
 
static struct pbufTxHead
 
static struct pbufTxTail
 
unsigned short int hsyncTicksPerMSec = 16
 

Detailed Description

PS2 TCP/IP STACK FOR EE

Definition in file ps2ip.c.

Function Documentation

◆ AfterTxPacket()

static int AfterTxPacket ( void **  payload)
static

Definition at line 248 of file ps2ip.c.

249 {
250  int len;
251 
252  if(TxTail != NULL && TxTail->next != NULL)
253  {
254  *payload = TxTail->next->payload;
255  len = TxTail->next->len;
256  } else
257  len = 0;
258 
259  return len;
260 }
static struct pbuf * TxTail
Definition: ps2ip.c:37
struct pbuf * next
Definition: tcpip.h:167
void * payload
Definition: tcpip.h:170
u16 len
Definition: tcpip.h:182
#define NULL
Definition: tamtypes.h:91

References PBuf::len, PBuf::next, NULL, PBuf::payload, and TxTail.

Referenced by ps2ipInit().

◆ AllocRxPacket()

static void* AllocRxPacket ( unsigned int  size,
void **  payload 
)
static

Definition at line 181 of file ps2ip.c.

182 {
183  struct pbuf* pbuf;
184 
185  if((pbuf = pbuf_alloc(PBUF_RAW, size, PBUF_POOL)) != NULL)
186  *payload = pbuf->payload;
187 
188  return pbuf;
189 }
struct pbuf * pbuf_alloc(pbuf_layer l, u16 size, pbuf_type type)
Definition: tcpip.h:165
@ PBUF_POOL
Definition: tcpip.h:161
@ PBUF_RAW
Definition: tcpip.h:132

References NULL, PBuf::payload, pbuf_alloc(), PBUF_POOL, and PBUF_RAW.

Referenced by ps2ipInit().

◆ DeQTxPacket()

static void DeQTxPacket ( void  )
static

Definition at line 220 of file ps2ip.c.

221 {
222  struct pbuf *toFree;
223 
224  toFree = NULL;
225 
226  DI();
227  if(TxTail != NULL)
228  {
229  toFree = TxTail;
230 
231  if(TxTail == TxHead) {
232  //Last in queue.
233  TxTail = NULL;
234  TxHead = NULL;
235  } else {
236  TxTail = TxTail->next;
237  }
238  }
239  EI();
240 
241  if(toFree != NULL)
242  {
243  toFree->next = NULL;
244  pbuf_free(toFree);
245  }
246 }
#define DI
Definition: kernel.h:24
#define EI
Definition: kernel.h:25
u8 pbuf_free(struct pbuf *p)
static struct pbuf * TxHead
Definition: ps2ip.c:37

References DI, EI, PBuf::next, NULL, pbuf_free(), TxHead, and TxTail.

Referenced by ps2ipInit().

◆ EnQRxPacket()

static void EnQRxPacket ( void *  packet)
static

Definition at line 201 of file ps2ip.c.

202 {
204 }
packet_t packet
Definition: font.c:24
static struct netif NIF
Definition: ps2ip.c:36
err_t ps2ip_input(PBuf *pInput, NetIF *pNetIF)
Definition: ps2ip.c:290

References NIF, packet, and ps2ip_input().

Referenced by ps2ipInit().

◆ EnQTxPacket()

static void EnQTxPacket ( struct pbuf tx)
static

Definition at line 131 of file ps2ip.c.

132 {
133  DI();
134 
135  if(TxHead != NULL)
136  TxHead->next = tx;
137 
138  TxHead = tx;
139  tx->next = NULL;
140 
141  if(TxTail == NULL) //Queue empty
142  TxTail = tx;
143 
144  EI();
145 }

References DI, EI, PBuf::next, NULL, TxHead, and TxTail.

Referenced by SMapLowLevelOutput().

◆ FreeRxPacket()

static void FreeRxPacket ( void *  packet)
static

Definition at line 196 of file ps2ip.c.

197 {
198  pbuf_free(packet);
199 }

References packet, and pbuf_free().

Referenced by ps2ipInit().

◆ InitDone()

static void InitDone ( void *  pvArg)
static

Definition at line 262 of file ps2ip.c.

263 {
264  dbgprintf("InitDone: TCPIP initialized\n");
265  sys_sem_signal((sys_sem_t*)pvArg);
266 }
#define dbgprintf(args...)
void sys_sem_signal(sys_sem_t *Sema)
Definition: sys_arch.c:406
int sys_sem_t
Definition: sys_arch.h:25

References dbgprintf, and sys_sem_signal().

Referenced by InitializeLWIP().

◆ InitializeLWIP()

static void InitializeLWIP ( void  )
inlinestatic

Definition at line 300 of file ps2ip.c.

301 {
302  sys_sem_t Sema;
303 
304  dbgprintf("PS2IP: Module Loaded.\n");
305 
306  sys_sem_new(&Sema, 0);
307  dbgprintf("PS2IP: Calling tcpip_init\n");
308  tcpip_init(InitDone,&Sema);
309 
310  sys_arch_sem_wait(&Sema, 0);
311  sys_sem_free(&Sema);
312 
313  dbgprintf("PS2IP: System Initialised\n");
314 }
static void InitDone(void *pvArg)
Definition: ps2ip.c:262
void sys_sem_free(sys_sem_t *Sema)
Definition: sys_arch.c:411
u32_t sys_arch_sem_wait(sys_sem_t *sema, u32_t timeout)
Definition: sys_arch.c:373
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
Definition: sys_arch.c:352

References dbgprintf, InitDone(), sys_arch_sem_wait(), sys_sem_free(), and sys_sem_new().

Referenced by ps2ipInit().

◆ LinkStateDown()

static void LinkStateDown ( void  )
static

Definition at line 176 of file ps2ip.c.

177 {
178  tcpip_callback((void*)&netif_set_link_down, &NIF);
179 }

References NIF.

Referenced by ps2ipInit().

◆ LinkStateUp()

static void LinkStateUp ( void  )
static

Definition at line 171 of file ps2ip.c.

172 {
173  tcpip_callback((void*)&netif_set_link_up, &NIF);
174 }

References NIF.

Referenced by ps2ipInit().

◆ NextTxPacket()

static int NextTxPacket ( void **  payload)
static

Definition at line 206 of file ps2ip.c.

207 {
208  int len;
209 
210  if(TxTail != NULL)
211  {
212  *payload = TxTail->payload;
213  len = TxTail->len;
214  } else
215  len = 0;
216 
217  return len;
218 }

References PBuf::len, NULL, PBuf::payload, and TxTail.

Referenced by ps2ipInit().

◆ ps2ip_getconfig()

int ps2ip_getconfig ( char *  pszName,
t_ip_info pInfo 
)

Definition at line 41 of file ps2ip.c.

42 {
43  NetIF* pNetIF=netif_find(pszName);
44 
45  if (pNetIF==NULL)
46  {
47  //Net interface not found.
48  memset(pInfo,0,sizeof(*pInfo));
49  return 0;
50  }
51  strcpy(pInfo->netif_name,pszName);
52  pInfo->ipaddr.s_addr=pNetIF->ip_addr.addr;
53  pInfo->netmask.s_addr=pNetIF->netmask.addr;
54  pInfo->gw.s_addr=pNetIF->gw.addr;
55 
56  memcpy(pInfo->hw_addr,pNetIF->hwaddr,sizeof(pInfo->hw_addr));
57 
58 #if LWIP_DHCP
59  struct dhcp *dhcp = netif_dhcp_data(pNetIF);
60 
61  if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF))
62  {
63  pInfo->dhcp_enabled=1;
64  pInfo->dhcp_status=dhcp->state;
65  }
66  else
67  {
68  pInfo->dhcp_enabled=0;
70  }
71 
72 #else
73  pInfo->dhcp_enabled=0;
74 #endif
75 
76  return 1;
77 }
struct netif * netif_find(const char *name)
#define netif_dhcp_data(netif)
u32 s_addr
Definition: tcpip.h:1278
struct in_addr ipaddr
u32 dhcp_enabled
struct in_addr gw
u8 hw_addr[8]
struct in_addr netmask
char netif_name[4]
@ DHCP_STATE_OFF
Definition: tcpip.h:1260

Referenced by ethApplyIPConfig(), ethGetDHCPStatus(), and ethPrintIPConfig().

◆ ps2ip_input()

err_t ps2ip_input ( PBuf *  pInput,
NetIF *  pNetIF 
)

Definition at line 290 of file ps2ip.c.

291 {
292  err_t result;
293 
294  if((result = pNetIF->input(pInput, pNetIF)) != ERR_OK)
295  pbuf_free(pInput);
296 
297  return result;
298 }
s32 result
Definition: rpc_client.c:23
signed char err_t
Definition: tcpip.h:95

References pbuf_free(), and result.

Referenced by EnQRxPacket().

◆ ps2ip_setconfig()

int ps2ip_setconfig ( const t_ip_info pInfo)

Definition at line 79 of file ps2ip.c.

80 {
81  NetIF* pNetIF=netif_find(pInfo->netif_name);
82 
83  if (pNetIF==NULL)
84  {
85  return 0;
86  }
87 
88 #if LWIP_DHCP
89  struct dhcp *dhcp = netif_dhcp_data(pNetIF);
90 
91  //Enable dhcp here
92 
93  if (pInfo->dhcp_enabled)
94  {
95  if ((dhcp == NULL) || (dhcp->state == DHCP_STATE_OFF))
96  {
97  //Set initial IP address
98  netif_set_ipaddr(pNetIF,(IPAddr*)&pInfo->ipaddr);
99  netif_set_netmask(pNetIF,(IPAddr*)&pInfo->netmask);
100  netif_set_gw(pNetIF,(IPAddr*)&pInfo->gw);
101 
102  //Start DHCP client
103  dhcp_start(pNetIF);
104  }
105  }
106  else
107  {
108  if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF))
109  {
110  //Release DHCP lease
111  dhcp_release(pNetIF);
112 
113  //Stop DHCP client
114  dhcp_stop(pNetIF);
115  }
116 
117  netif_set_ipaddr(pNetIF,(IPAddr*)&pInfo->ipaddr);
118  netif_set_netmask(pNetIF,(IPAddr*)&pInfo->netmask);
119  netif_set_gw(pNetIF,(IPAddr*)&pInfo->gw);
120  }
121 
122 #else
123  netif_set_ipaddr(pNetIF,(IPAddr*)&pInfo->ipaddr);
124  netif_set_netmask(pNetIF,(IPAddr*)&pInfo->netmask);
125  netif_set_gw(pNetIF,(IPAddr*)&pInfo->gw);
126 #endif
127 
128  return 1;
129 }
void netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
void netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
void netif_set_gw(struct netif *netif, const ip4_addr_t *gw)

References t_ip_info::dhcp_enabled, DHCP_STATE_OFF, t_ip_info::gw, t_ip_info::ipaddr, netif_dhcp_data, netif_find(), t_ip_info::netif_name, netif_set_gw(), netif_set_ipaddr(), netif_set_netmask(), t_ip_info::netmask, and NULL.

Referenced by ethApplyIPConfig().

◆ ps2ipDeinit()

void ps2ipDeinit ( void  )

Definition at line 342 of file ps2ip.c.

342  {
344 }
void NetManUnregisterNetworkStack(void)
Definition: netman.c:92

References NetManUnregisterNetworkStack().

Referenced by main().

◆ ps2ipInit()

int ps2ipInit ( struct ip4_addr ip_address,
struct ip4_addr subnet_mask,
struct ip4_addr gateway 
)

Initializes PS2IP. Specify a dummy address like "169.254.0.1" if DHCP is to be used, before enabling DHCP via ps2ip_setconfig().

Definition at line 316 of file ps2ip.c.

316  {
317  static struct NetManNetProtStack stack={
318  &LinkStateUp,
319  &LinkStateDown,
320  &AllocRxPacket,
321  &FreeRxPacket,
322  &EnQRxPacket,
323  &NextTxPacket,
324  &DeQTxPacket,
325  &AfterTxPacket,
327  };
328 
329  NetManInit();
330 
331  InitializeLWIP();
332 
333  netif_add(&NIF, ip_address, subnet_mask, gateway, NULL, &SMapIFInit, tcpip_input);
335 
337  netif_set_up(&NIF);
338 
339  return 0;
340 }
int NetManRegisterNetworkStack(const struct NetManNetProtStack *stack)
Definition: netman.c:70
int NetManInit(void)
Definition: netman.c:43
struct netif * netif_add(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input)
void netif_set_default(struct netif *netif)
err_t tcpip_input(struct pbuf *p, struct netif *inp)
void netif_set_up(struct netif *netif)
static void FreeRxPacket(void *packet)
Definition: ps2ip.c:196
static void DeQTxPacket(void)
Definition: ps2ip.c:220
static void LinkStateUp(void)
Definition: ps2ip.c:171
static void LinkStateDown(void)
Definition: ps2ip.c:176
static int AfterTxPacket(void **payload)
Definition: ps2ip.c:248
static void * AllocRxPacket(unsigned int size, void **payload)
Definition: ps2ip.c:181
static int NextTxPacket(void **payload)
Definition: ps2ip.c:206
static void InitializeLWIP(void)
Definition: ps2ip.c:300
static void EnQRxPacket(void *packet)
Definition: ps2ip.c:201
static void ReallocRxPacket(void *packet, unsigned int size)
Definition: ps2ip.c:191
static err_t SMapIFInit(struct netif *pNetIF)
Definition: ps2ip.c:269

References AfterTxPacket(), AllocRxPacket(), DeQTxPacket(), EnQRxPacket(), FreeRxPacket(), InitializeLWIP(), LinkStateDown(), LinkStateUp(), netif_add(), netif_set_default(), netif_set_up(), NetManInit(), NetManRegisterNetworkStack(), NextTxPacket(), NIF, NULL, ReallocRxPacket(), SMapIFInit(), and tcpip_input().

Referenced by main().

◆ ps2ipSetHsyncTicksPerMSec()

void ps2ipSetHsyncTicksPerMSec ( unsigned char  ticks)

Use to specify the number of H-sync ticks per milisecond (Default: 16). Use this function to keep timings accurate, if a mode like 480P (~31KHz H-sync) is used instead of NTSC/PAL (~16KHz H-sync).

Definition at line 346 of file ps2ip.c.

346  {
347  hsyncTicksPerMSec = ticks;
348 }
unsigned short int hsyncTicksPerMSec
Definition: ps2ip.c:39

References hsyncTicksPerMSec.

◆ ReallocRxPacket()

static void ReallocRxPacket ( void *  packet,
unsigned int  size 
)
static

Definition at line 191 of file ps2ip.c.

192 {
193  pbuf_realloc((struct pbuf *)packet, size);
194 }
void pbuf_realloc(struct pbuf *p, u16 size)

References packet, and pbuf_realloc().

Referenced by ps2ipInit().

◆ SMapIFInit()

static err_t SMapIFInit ( struct netif pNetIF)
static

Should be called at the beginning of the program to set up the network interface.

Definition at line 269 of file ps2ip.c.

270 {
271  TxHead = NULL;
272  TxTail = NULL;
273 
274  pNetIF->name[0]='s';
275  pNetIF->name[1]='m';
276  pNetIF->output=&etharp_output; // For LWIP 1.3.0 and later.
279  pNetIF->flags|=(NETIF_FLAG_ETHARP|NETIF_FLAG_BROADCAST); // For LWIP v1.3.0 and later.
280  pNetIF->mtu=1500;
281 
282  //Get MAC address.
283  NetManIoctl(NETMAN_NETIF_IOCTL_ETH_GET_MAC, NULL, 0, pNetIF->hwaddr, sizeof(pNetIF->hwaddr));
284  dbgprintf("MAC address : %02d:%02d:%02d:%02d:%02d:%02d\n",pNetIF->hwaddr[0],pNetIF->hwaddr[1],pNetIF->hwaddr[2],
285  pNetIF->hwaddr[3],pNetIF->hwaddr[4],pNetIF->hwaddr[5]);
286 
287  return ERR_OK;
288 }
#define NETIF_FLAG_ETHARP
Definition: tcpip.h:931
#define NETIF_FLAG_BROADCAST
Definition: tcpip.h:921
int NetManIoctl(unsigned int command, void *arg, unsigned int arg_len, void *output, unsigned int length)
Definition: netman.c:109
@ NETMAN_NETIF_IOCTL_ETH_GET_MAC
Definition: netman.h:75
err_t etharp_output(struct netif *netif, struct pbuf *q, const ip_addr_t *ipaddr)
static err_t SMapLowLevelOutput(struct netif *pNetIF, struct pbuf *pOutput)
Definition: ps2ip.c:147
u8 flags
Definition: tcpip.h:1145
char name[2]
Definition: tcpip.h:1147
netif_output_fn output
Definition: tcpip.h:1090
netif_linkoutput_fn linkoutput
Definition: tcpip.h:1095
u8 hwaddr_len
Definition: tcpip.h:1141
u16 mtu
Definition: tcpip.h:1139
u8 hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: tcpip.h:1143
#define NETIF_MAX_HWADDR_LEN
Definition: tcpip.h:903

References dbgprintf, etharp_output(), NetIF::flags, NetIF::hwaddr, NetIF::hwaddr_len, NetIF::linkoutput, NetIF::mtu, NetIF::name, NETIF_FLAG_BROADCAST, NETIF_FLAG_ETHARP, NETIF_MAX_HWADDR_LEN, NETMAN_NETIF_IOCTL_ETH_GET_MAC, NetManIoctl(), NULL, NetIF::output, SMapLowLevelOutput(), TxHead, and TxTail.

Referenced by ps2ipInit().

◆ SMapLowLevelOutput()

static err_t SMapLowLevelOutput ( struct netif pNetIF,
struct pbuf pOutput 
)
static

Definition at line 147 of file ps2ip.c.

148 {
149  struct pbuf* pbuf;
150  err_t result;
151 
152  result = ERR_OK;
153  if(pOutput->tot_len > pOutput->len)
154  {
155  pbuf_ref(pOutput); //Increment reference count because LWIP must free the PBUF, not the driver!
156  if((pbuf = pbuf_coalesce(pOutput, PBUF_RAW)) != pOutput)
157  { //No need to increase reference count because pbuf_coalesce() does it.
158  EnQTxPacket(pbuf);
159  NetManNetIFXmit();
160  } else
161  result = ERR_MEM;
162  } else {
163  pbuf_ref(pOutput); //This will be freed later.
164  EnQTxPacket(pOutput);
165  NetManNetIFXmit();
166  }
167 
168  return result;
169 }
void NetManNetIFXmit(void)
Definition: netman.c:103
struct pbuf * pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
void pbuf_ref(struct pbuf *p)
static void EnQTxPacket(struct pbuf *tx)
Definition: ps2ip.c:131
u16 tot_len
Definition: tcpip.h:179

References EnQTxPacket(), PBuf::len, NetManNetIFXmit(), pbuf_coalesce(), PBUF_RAW, pbuf_ref(), result, and PBuf::tot_len.

Referenced by SMapIFInit().

Variable Documentation

◆ hsyncTicksPerMSec

unsigned short int hsyncTicksPerMSec = 16

Definition at line 39 of file ps2ip.c.

Referenced by mSec2HSyncTicks(), and ps2ipSetHsyncTicksPerMSec().

◆ NIF

struct netif NIF
static

Definition at line 1 of file ps2ip.c.

Referenced by EnQRxPacket(), LinkStateDown(), LinkStateUp(), and ps2ipInit().

◆ TxHead

struct pbuf* TxHead
static

Definition at line 37 of file ps2ip.c.

Referenced by DeQTxPacket(), EnQTxPacket(), and SMapIFInit().

◆ TxTail

struct pbuf * TxTail
static

Definition at line 37 of file ps2ip.c.

Referenced by AfterTxPacket(), DeQTxPacket(), EnQTxPacket(), NextTxPacket(), and SMapIFInit().