📄 webserver.c
字号:
*EBI1_write = value_to_send; //Write Value to Flash
*EBI1_write = 0x0070; //Switch to Read Status Mode
check = *EBI1_write;
while ((check & prog_done_mask) == 0) //Check to see if programming is done
check = *EBI1_write;
*EBI1_write = 0x0050; //Clear Read Status Register
EBI1_write++;
index = index + 2;
}
printf("\r\nFinished writing new image to EBI1\n");
printf("Checking that the new image is valid\n");
for(i=0; i < length; i++)
{
if((*EBI1_check) != cond_bin_array[i])
{
printf("Bad data at location: %d\n", i);
printf("Resetting board\n");
*watch_dog = (*watch_dog_count) + 10000; //reset device
while(1); //wait for boad to reset
}
else
{
EBI1_check++;
}
if((i & end_of_para_block_mask) == 0x0000) //just use this to print to the screen so the
{
printf("."); //user knows something is happening
}
}
printf("\r\nNew image is valid\n");
printf("Resetting the board and will run new image\n");
write_flash(EBI1_magic, 0x1);
*watch_dog = (*watch_dog_count) + 10000; //reset device
}
void cond_bin_data(int length)
{
unsigned char cond_data;
int i;
int index = 0;
for(i=0; i <= length; (i = i + 2))
{ if(bin_data[i] <= '9')
{
cond_data = (bin_data[i] - '0');
}
else
{
cond_data = (bin_data[i] - '7');
}
cond_data = cond_data << 4;
if(bin_data[i + 1] <= '9')
{
cond_data = cond_data + (bin_data[i + 1] - '0');
}
else
{
cond_data = cond_data + (bin_data[i + 1] - '7');
}
cond_bin_array[index] = cond_data;
index++;
}
program_flash(index);
}
void convert_hex_data_to_bin(void)
{
char record_length[3];
long data_length;
char offset[5];
char record_type[3];
unsigned char *record;
int record_index = 0;
char address[10];
int bin_data_address = 0;
int i;
printf("Converting hex image to binary\n");
while(strcmp(record_type, end_of_file_record))
{
record = get_record(record_index);
record_index++;
record_length[0] = *(record + 1);
record_length[1] = *(record + 2);
record_length[2] = '\0';
offset[0] = *(record + 3);
offset[1] = *(record + 4);
offset[2] = *(record + 5);
offset[3] = *(record + 6);
offset[4] = '\0';
record_type[0] = *(record + 7);
record_type[1] = *(record + 8);
record_type[2] = '\0';
if(!strcmp(record_type, extended_linear_address_record))
{
address[0] = *(record + 9);
address[1] = *(record + 10);
address[2] = *(record + 11);
address[3] = *(record + 12);
address[9] = '\0';
printf("*");
}
else if (!strcmp(record_type, data_record))
{
address[4] = offset[0];
address[5] = offset[1];
address[6] = offset[2];
address[7] = offset[3];
address[8] = '\0';
bin_data_address = 2 * (r_hex_char_to_value(address, 8));
data_length = 2 * (r_hex_char_to_value(record_length, 2));
for(i=0; i < data_length; i++)
{
bin_data[bin_data_address + i] = *(record + 9 + i);
}
i = 0;
}
else if(!strcmp(record_type, end_of_file_record))
{
bin_data[bin_data_address + data_length] = '\0';
printf("\n\rFinished Image conversion\n");
}
else
{
printf("Received unexpected record type at address: %x\n", (int)record);
}
}
cond_bin_data((bin_data_address + data_length));
}
int get_content_length(void)
{
char tag[15];
int content_length;
int index = 0;
int i = 0;
unsigned char *line_to_check ;
char line_to_check_array[500];
strcpy(tag, "Content-Length");
http_request.http_message_index = http_request.http_message_request_head;
http_request.http_message_line_index = http_request.http_message_request_head;
while(http_request.http_message_line_index < http_request.http_message_end)
{
line_to_check = get_next_line_in_http_request();
index = 0;
while(*line_to_check != '\n')
{
line_to_check_array[index] = *line_to_check;
index++;
line_to_check++;
}
line_to_check_array[index] = *line_to_check; //add the newline char
while(line_to_check_array[i] != ':' )
{
i++;
if(i == index)
{
break;
}
}
line_to_check_array[i] = '\0';
i = 0;
if(!strcmp(line_to_check_array, tag))
{
content_length = strtol(((const char *)&line_to_check_array[16]), NULL, 10);
printf("Receiveing a new PLD image which is %d bytes long\n", content_length);
}
}
return content_length;
}
void get_entity_data(unsigned char *payload, int payload_length, long content_length, int plug_handle)
{
unsigned char *temp;
static int hex_data_length;
static int hex_data_index;
static int payload_index;
temp = payload;
if(tcp_control.still_receiving_http_request == 0)
{
hex_data_index = 0;
payload_index = 0;
hex_data_length = content_length;
tcp_control.still_receiving_http_request = 1;
}
else
if(hex_data_index <= hex_data_length)
{
while(temp != (payload + payload_length))
{
hex_data[payload_index] = *temp;
temp++;
payload_index++;
hex_data_index++;
}
if(hex_data_index == hex_data_length )
{ send_http_response(plug_handle);
hex_data[payload_index] = '\0';
payload_index = 0;
tcp_control.still_receiving_http_request = 0;
printf("Finished receiving the new PLD image\n");
convert_hex_data_to_bin();
r_start_listening();
}
}
}
void get_method(int plug_handle)
{
//http_message_response_type http_response;
int result;
http_response.html_response_length = 0;
// |
// | Default to 200 OK status, and check last char of URL for content type
// |
http_response.http_result_code_and_message = "200 OK";
http_response.http_content_type = http_request.url[strlen(http_request.url) - 1] == 'f'
? "image/gif" : "text/html";
http_response.html_response[0] = 0;
// |
// | Try to get the file from wosfs
// |
result = r_get_html_from_wosfs(http_request.url,http_response.html_response,&http_response.html_response_length);
// |
// | If not wosfs, try dynamic
// |
if(result)
result = r_get_html_dynamically(http_request.url,http_request.url_and_args,http_response.html_response);
// |
// | And if even that fails, call our
// | error generation routine.
// |
if(result)
{
http_response.http_result_code_and_message = "404 File Not Found";
http_response.http_content_type = "text/html";
r_get_html_404(http_request.url,http_response.html_response);
result = 0; // above routine not allowed to fail
}
// |
// | If nobody filled out the html_response_length,
// | get it as a string length.
// |
if(http_response.html_response_length == 0)
http_response.html_response_length = strlen(http_response.html_response);
send_http_response(plug_handle);
}
void send_http_response(int plug_handle)
// |
// | Lastly, send the HTTP response.
// | The plugs library will not automatically
// | break up the packet into ethernet-legal
// | chunks, so we do it here.
// |
{
int this_packet;
int size_left = http_response.html_response_length;
char *w = http_response.html_response;
int mtu = 512;
char http_intro[k_string_length];
// |
// | Send the HTTP stuff
// |
sprintf(http_intro,"HTTP/1.1 %s\r\nContent-type: %s\r\n\r\n",
http_response.http_result_code_and_message,
http_response.http_content_type
);
printf("Sending HTTP response\n");
nr_plugs_send(plug_handle,http_intro,strlen(http_intro),0);
// |
// | And send the HTML, broken up into MTU sized bits
// |
while(size_left)
{
this_packet = size_left;
if(this_packet > mtu)
this_packet = mtu;
nr_plugs_send(plug_handle,w,this_packet,0);
size_left -= this_packet;
w += this_packet;
}
}
static unsigned char *get_next_line_in_http_request(void)
{
unsigned char newline[1000];
int newline_position = 0;
while(*(http_request.http_message_index) != '\n')
{
newline[newline_position] = *(http_request.http_message_index);
http_request.http_message_index++;
newline_position++;
}
http_request.http_message_index++;
http_request.http_message_line_index = http_request.http_message_index;
newline[newline_position] = '\n';
newline_position++;
newline[newline_position] = '\0';
return &newline[0];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -