?? http.c
字號:
//********************************************************************************************
//
// File : http.c implement for Hyper Text transfer Protocol
//
//********************************************************************************************
//
// Copyright (C) 2007
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
// This program is distributed in the hope that it will be useful, but
//
// WITHOUT ANY WARRANTY;
//
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin St, Fifth Floor, Boston, MA 02110, USA
//
// http://www.gnu.de/gpl-ger.html
//
//********************************************************************************************
#include "includes.h"
//********************************************************************************************
//
// Global variable for http.c
//
//********************************************************************************************
prog_int8_t web_title[] = "AVRnet V0.9 by AVRportal.com";
prog_int8_t tag_br[] = "<br>";
prog_int8_t tag_hr[] = "<hr width=\"100%\" size=\"1\"><br>";
prog_int8_t tag_form[] = "<form action=\"./?\" method=\"get\">";
//********************************************************************************************
//
// Function : http_webserver_process
// Description : Initial connection to web server
//
//********************************************************************************************
void http_webserver_process ( BYTE *rxtx_buffer, BYTE *dest_mac, BYTE *dest_ip )
{
WORD dlength, dest_port;
BYTE count_time_temp[3];
BYTE generic_buf[64];
dest_port = (rxtx_buffer[TCP_SRC_PORT_H_P]<<8)|rxtx_buffer[TCP_SRC_PORT_L_P];
// tcp port 80 start for web server
if ( rxtx_buffer [ IP_PROTO_P ] == IP_PROTO_TCP_V && rxtx_buffer[ TCP_DST_PORT_H_P ] == 0 && rxtx_buffer[ TCP_DST_PORT_L_P ] == 80 )
{
// received packet with flags "SYN", let's send "SYNACK"
if ( (rxtx_buffer[ TCP_FLAGS_P ] & TCP_FLAG_SYN_V) )
{
// tcp_send_synack ( rxtx_buffer, dest_mac, dest_ip );
tcp_send_packet (
rxtx_buffer,
(WORD_BYTES){dest_port},
(WORD_BYTES){80}, // source port
TCP_FLAG_SYN_V|TCP_FLAG_ACK_V, // flag
1, // (bool)maximum segment size
0, // (bool)clear sequence ack number
1, // (bool)calculate new seq and seqack number
0, // tcp data length
dest_mac, // server mac address
dest_ip ); // server ip address
flag1.bits.syn_is_received = 1;
return;
}
if ( (rxtx_buffer [ TCP_FLAGS_P ] & TCP_FLAG_ACK_V) )
{
// get tcp data length
dlength = tcp_get_dlength( rxtx_buffer );
if ( dlength == 0 )
{
// finack, answer with ack
if ( (rxtx_buffer[TCP_FLAGS_P] & TCP_FLAG_FIN_V) )
{
// tcp_send_ack ( rxtx_buffer, dest_mac, dest_ip );
tcp_send_packet (
rxtx_buffer,
(WORD_BYTES){dest_port},
(WORD_BYTES){80}, // source port
TCP_FLAG_ACK_V, // flag
0, // (bool)maximum segment size
0, // (bool)clear sequence ack number
1, // (bool)calculate new seq and seqack number
0, // tcp data length
dest_mac, // server mac address
dest_ip ); // server ip address
}
return;
}
// get avr ip address from request and set to new avr ip address if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "aip" ), generic_buf ) )
{
if ( http_get_ip ( generic_buf, (BYTE*)&avr_ip ) == 4 )
eeprom_write_block ( &avr_ip, ee_avr_ip, 4 );
eeprom_read_block ( &avr_ip, ee_avr_ip, 4 );
}
// get server ip address from request and set to new server ip address
if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "sip" ), generic_buf ) )
{
if ( http_get_ip ( generic_buf, (BYTE*)&server_ip ) == 4 )
eeprom_write_block ( &server_ip, ee_server_ip, 4 );
eeprom_read_block ( &server_ip, ee_server_ip, 4 );
}
// get LED1 on/of command
if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "l1" ), generic_buf ) )
{
if ( generic_buf[0] == '0' )
LED_PORT |= _BV ( LED_PIN1 );
else
LED_PORT &= ~_BV ( LED_PIN1 );
}
// get LED1 on/of command
if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "l2" ), generic_buf ) )
{
if ( generic_buf[0] == '0' )
LED_PORT |= _BV ( LED_PIN2 );
else
LED_PORT &= ~_BV ( LED_PIN2 );
}
// get LCD string and show on first line
if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "lcd1" ), generic_buf ) )
{
urldecode ( generic_buf );
lcd_putc ( '\f' );
lcd_print ( generic_buf );
flag1.bits.lcd_busy = 1;
}
// get LCD string and show on second line
if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "lcd2" ), generic_buf ) )
{
urldecode ( generic_buf );
lcd_putc ( '\n' );
lcd_print ( generic_buf );
flag1.bits.lcd_busy = 1;
}
// get send temparature to server configuration
if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "tc" ), generic_buf ) )
{
// enable or disable send temparature
if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "en" ), generic_buf ) )
count_time_temp[0] = 1;
else
count_time_temp[0] = 0;
// get hour
if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "h" ), generic_buf ) )
{
count_time_temp[1] = (generic_buf[0] - '0') * 10;
count_time_temp[1] = count_time_temp[1] + (generic_buf[1] - '0');
}
// get minute
if ( http_get_variable ( rxtx_buffer, dlength, PSTR( "m" ), generic_buf ) )
{
count_time_temp[2] = (generic_buf[0] - '0') * 10;
count_time_temp[2] = count_time_temp[2] + (generic_buf[1] - '0');
}
// write config to eeprom
eeprom_write_block ( count_time_temp, ee_count_time, 3 );
eeprom_read_block ( count_time, ee_count_time, 3 );
count_time[3] = 0;
}
// print webpage
dlength = http_home( rxtx_buffer );
// send ack before send data
// tcp_send_ack ( rxtx_buffer, dest_mac, dest_ip );
tcp_send_packet (
rxtx_buffer,
(WORD_BYTES){dest_port},
(WORD_BYTES){80}, // source port
TCP_FLAG_ACK_V, // flag
0, // (bool)maximum segment size
0, // (bool)clear sequence ack number
1, // (bool)calculate new seq and seqack number
0, // tcp data length
dest_mac, // server mac address
dest_ip ); // server ip address
// send tcp data
// tcp_send_data ( rxtx_buffer, dest_mac, dest_ip, dlength );
tcp_send_packet (
rxtx_buffer,
(WORD_BYTES){dest_port},
(WORD_BYTES){80}, // source port
TCP_FLAG_ACK_V | TCP_FLAG_PSH_V | TCP_FLAG_FIN_V, // flag
0, // (bool)maximum segment size
0, // (bool)clear sequence ack number
0, // (bool)calculate new seq and seqack number
dlength, // tcp data length
dest_mac, // server mac address
dest_ip ); // server ip address
flag1.bits.syn_is_received = 0;
}
}
}
//********************************************************************************************
//
// Function : http_get_ip
// Description : Get IP address from buffer (stored after call http_get_variable function)
// example after call http_get_variable function ip address (ascii) has been stored in buffer
// 10.1.1.1 (ascii), http_get_ip function convert ip address in ascii to binary and stored
// in BYTE *dest
//
//********************************************************************************************
unsigned char http_get_ip ( unsigned char *buf, BYTE *dest )
{
unsigned char i, ch, digit, temp;
i = 0;
digit = 1;
temp = 0;
while ( 1 )
{
ch = *buf++;
if ( ch >= '0' && ch <= '9' )
{
ch = ch - '0';
temp = (temp * digit) + ch;
digit *= 10;
}
else if ( ch == '.' || ch == '\0' )
{
dest[ i ] = temp;
i++;
digit = 1;
temp = 0;
}
else
{
return 0;
}
if ( i == 4 )
{
return i;
}
}
}
//********************************************************************************************
//
// Function : http_get_variable
// Description : Get http variable from GET method, example http://10.1.1.1/?pwd=123456
// when you call http_get_variable with val_key="pwd", then function stored "123456"
// to dest buffer.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -