📄 init_ether_and_plugs.c
字号:
/***************************************************************************
;
; 2002 Altera Corporation. All rights reserved. Altera products are protected
; under numerous U.S. and foreign patents, maskwork rights, copyrights and other
; intellectual property laws. This reference design file, and your use thereof,
; is subject to and governed by the terms and conditions of the applicable
; Altera Reference Design License Agreement (found at www.altera.com). By using
; this reference design file, you indicate your acceptance of such terms and
; conditions between you and Altera Corporation. In the event that you do not
; agree with such terms and conditions, you may not use the reference design file
; and please promptly destroy any copies you have made. This reference design
; file being provided on an "as-is" basis and as an accommodation and therefore
; all warranties, representations or guarantees of any kind (whether express,
; implied or statutory) including, without limitation, warranties of
; merchantability, non-infringement, or fitness for a particular purpose, are
; specifically disclaimed. By making this reference design file available, Altera
; expressly does not recommend, suggest or require that this reference design file
; be used in combination with any other product not provided by Altera.
;
;***************************************************************************
;
; Filename: init_ether_and_plugs.c
;
; This code calls routines that initalize ethernet mac with it's ip settings.
; Any changes to the ethernet mac settings should be made here in the
; init_ethernet_and_plugs_lib routine. There are also calls to functions that
; initializes the plugs library.
;
;***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "nios_compatibility.h"
#include "plugs.h"
#include "ahbeth.h"
#include "init_ether_and_plugs.h"
#include "webserver.h"
#include "string.h"
#include "stripe.h"
#define k_http_port 80
#define k_tcp_timeout 500000 // milliseconds
ip_control_struct tcp_control = {0};
//Local Prototypes
static int r_tcp_proc(int plug_handle,
void *context,
ns_plugs_packet *p,
void *payload,
int payload_length);
static int r_listen_proc(int plug_handle,
void *context,
host_32 remote_ip_address,
host_16 remote_port);
int init_ethernet_and_plugs_lib(void)
{
int result;
ns_plugs_network_settings settings = {0};
unsigned int *ahb_eth_add = (unsigned int *)EXC_PLD_BLOCK0_BASE;
settings.ethernet_address.u32=0x00DE7000; //Altera allocated Ethernet Address sector
settings.ethernet_address.l16=0x9740; //Arbitray selection
settings.nameserver_ip_address = nm_ip2n(137,57,64,200);
settings.subnet_mask = nm_ip2n(255,255,255,0);
settings.gateway_ip_address = nm_ip2n(137,57,193,254);
settings.ip_address = nm_ip2n(137,57,193,168);
nr_plugs_terminate(); //Terminate exiting plug
result = nr_plugs_initialize(0,&settings,ahb_eth_add,&ng_ahbeth);
if(result)
goto error_exit;
result = nr_plugs_create
(
&tcp_control.tcp_plug_handle, // | Plug to create
ne_plugs_tcp, // | Kind of plug (tcp connection)
k_http_port, // | Port to listen on
r_tcp_proc, // | To call with incoming data
0, // | Context for plug callback
0 // | Flags for plug (none)
);
if(result)
goto error_exit;
// |
// | Accept incoming connections
// |
result = r_start_listening();
if(result)
goto error_exit;
error_exit:
nr_plugs_print_error_message("[init_ethernet_and_plugs_lib]", result);
return result;
}
// +----------------------------------
// | r_start_listening
// |
// | This routine closes the current tcp
// | connection (if any) and starts listening
// | again.
// |
int r_start_listening(void)
{
int result;
printf("\nlistening on port %d at t = %8d\n",k_http_port,nr_timer_milliseconds() );
result = nr_plugs_listen(tcp_control.tcp_plug_handle,r_listen_proc,0);
tcp_control.tcp_opened_time = 0; // so it looks like we're "closed"
return result;
}
static int r_listen_proc(int plug_handle,
void *context,
host_32 remote_ip_address,
host_16 remote_port)
{
tcp_control.tcp_opened_time = nr_timer_milliseconds();
printf("Accepted connection from ");
nr_plugs_print_ip_address(nr_h2n32(remote_ip_address));
printf(" port %d at t = %8ld\n",remote_port,tcp_control.tcp_opened_time);
return 0;
}
static int r_tcp_proc(int plug_handle, void *context, ns_plugs_packet *p, void *payload, int payload_length)
{
if(payload_length)
{
if(tcp_control.still_receiving_http_request)
{
get_entity_data(payload,payload_length, NULL, plug_handle);
}
else
{
r_handle_http_request(plug_handle,payload, payload_length);
}
}
else
{
tcp_control.still_receiving_http_request = 0;
}
return 0;
}
void check_for_timeout(void)
{ int result;
if( tcp_control.tcp_opened_time
&& ((nr_timer_milliseconds() - tcp_control.tcp_opened_time) > k_tcp_timeout) )
{
char *s = "nedk_web_server: Connection timing out. Bye.\n";
result = nr_plugs_send(tcp_control.tcp_plug_handle,s,strlen(s),0);
result = r_start_listening();
tcp_control.still_receiving_http_request = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -