📄 script.c
字号:
/******************************************************************
* script.c: Application running on top of the web server
* Called from httpd.c as response to POST request
*
* Copyright (c) 2001 Atmel Corporation.
* All Rights Reserved.
*
* You are autorized to use, copy and distribute this software only at
* a single site (the term "site" meaning a single company location).
* This copyright notice must be included in any copy, modification
* or portion of this software merged into another program.
*
* This software is licenced solely for use with Atmel AVR micro
* controller family. The software may not be modified to execute on
* any other microcontroller architectures
*
* This software is provided "as is"; Without warranties either express
* or implied, including any warranty regarding merchantability,
* fitness for a particular purpose or noninfringement.
*
* In no event shall Atmel or its suppliers be liable for any special,
* indirect,incidential or concequential damages resulting from the
* use or inability to use this software.
*
* Revision history:
*
* January 17, 2001: Version 1.0 Created by JB
* July 13, 2001 Version 1.2 JB
* - Changed to IAR compiler V2.25
* - Renamed flash file functions to avoid conflict with
* standard file I/O names
* - Bug fixes in HTTP
* - Speed optimization in TCP
*
*
*******************************************************************/
#include "comp_a90.h"
#include "main.h"
#include "ffile.h"
#include "tcp.h"
#include "httpd.h"
#include "udp.h"
#include "time.h"
#include "script.h"
#include "stdio.h"
#include <pgmspace.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include <iom103.h>
unsigned int readAd(unsigned char channel);
void ewsScript(SOCKET * socket, char *httpBuff)
{
char *nameStr;
char tempBuf[20];
char responseBuffer[100];
char *tempPoint;
char *form,*name, *temperature, *acfan;
FFILE * file;
unsigned char cnt;
unsigned char led = 0; /*list of expected variables*/
unsigned char ad = 0;
unsigned int itemp;
unsigned int graph;
// unsigned char data0;
/*read values into variables, the received string will be formated as follows:
name1=value1&name2=value2&...*/
// responseBuffer[0] = '\0';
nameStr = strtok(httpBuff, "=");
while(nameStr != NULL)
{
tempPoint = strtok(NULL,"&");
if ( !strcmp(nameStr, "form")) form=tempPoint; // Temporary storage of form name
else if ( !strcmp(nameStr, "led1")) led |= LED1;
else if ( !strcmp(nameStr, "led2")) led |= LED2;
else if ( !strcmp(nameStr, "led3")) led |= LED3;
else if ( !strcmp(nameStr, "led4")) led |= LED4;
else if ( !strcmp(nameStr, "ad0")) ad |= (1<<AD0);
else if ( !strcmp(nameStr, "ad1")) ad |= (1<<AD1);
else if ( !strcmp(nameStr, "ad2")) ad |= (1<<AD2);
else if ( !strcmp(nameStr, "ad3")) ad |= (1<<AD3);
else if ( !strcmp(nameStr, "ad4")) ad |= (1<<AD4);
else if ( !strcmp(nameStr, "ad5")) ad |= (1<<AD5);
else if ( !strcmp(nameStr, "name")) name=tempPoint; // Temporary storage of name variable
else if ( !strcmp(nameStr, "temp")) temperature=tempPoint;
else if ( !strcmp(nameStr, "fan")) acfan=tempPoint;
nameStr=strtok(NULL, "=");
}
if (!strcmp(form, "io")) //if the posted form is the io form
{
/*turn on/off the leds*/
PORTB = ~led;
/*check/uncheck led checkboxes*/
cnt = 4;
do // Shift led to check if bit is set
{
if (led & LED1)
{
strcat(responseBuffer,SCRIPT_CHECKED);
}
else
{
strcat(responseBuffer,SCRIPT_NOT_CHECKED);
}
led = led >> 1;
} while (--cnt);
cnt = 0;
do
{
if(ad & (1<<AD0)) //if ad channel is enabled
{
itemp = readAd(cnt);
strcat(responseBuffer,SCRIPT_CHECKED); //enable ad channel checkbox
sprintf(tempBuf,"%d\r",itemp);
strcat(responseBuffer,tempBuf); //the ad result
}
else
{
strcat(responseBuffer,SCRIPT_NOT_CHECKED); //uncheck the ad checkbox
strcat(responseBuffer,SCRIPT_NOT_CHECKED); //do not display any ad result
}
ad = ad >> 1;
} while (cnt++ < 6);
file=ffopen("io.syn",'r');
generateResponse(socket,file,responseBuffer);
}
else if( !strcmp(form, "name") ) //if the posted form is the name form
{
/// sprintf(responseBuffer,"<H1>Hi %s, Welcome to the Atmel Embedded Internet Toolkit</H><br><H6>%s</H>\0",name,tempBuf);
sprintf(responseBuffer,"<H1>Hi %s, Welcome to the Atmel Embedded Internet Toolkit</H><br><H6></H>\0",name);
sendHtml("Hi\0", responseBuffer, socket, 1);
}
else if(!strcmp(form,"ac")) // Check for right POST form
{
// itemp = readAd(AD0); // Read the temperature with A/D converter
itemp = 26; // Debug
graph = (itemp - 10)*11; // Scale graph according to temperature
sprintf(tempBuf,"%d\r%d\r",itemp,graph); // Print first tokens seperated by \r
strcpy(responseBuffer,tempBuf); // Copy result to buffer
sprintf(tempBuf,"%s\r",temperature);// Print temperature variable to buffer
strcat(responseBuffer,tempBuf);
if(!strcmp(acfan,"low")) // Check if fan is low
{
PORTB = ~LED1; // Set fan to low
strcat(responseBuffer,SCRIPT_CHECKED); // Print result to buffer
}
else
{
strcat(responseBuffer,SCRIPT_NOT_CHECKED); // Do not print buffer
}
if(!strcmp(acfan,"medium"))
{
PORTB = ~LED2;
strcat(responseBuffer,SCRIPT_CHECKED);
}
else
{
strcat(responseBuffer,SCRIPT_NOT_CHECKED);
}
if(!strcmp(acfan,"high"))
{
PORTB = ~LED3;
strcat(responseBuffer,SCRIPT_CHECKED);
}
else
{
strcat(responseBuffer,SCRIPT_NOT_CHECKED);
}
file=ffopen("ac.syn",'r'); // Open sync file
generateResponse(socket,file,responseBuffer); // Generate response based on results above
}
}
/*generates response from a template file, will replace any occurenses of SCRIPT_GENERATE_TAG
with the next element in the list, the elements should be separeted by \r. example:
template file: An apple % day, keeps the % away
list: a'\r'doctor'\0'
result: An apple a day, keeps the doctor away
If two tags are found next to each other they are not replaced with elements from the list,
they are simply replaced with one tag. This way the tag may occur in the resulting text*/
void generateResponse(SOCKET * socket,FFILE * file,char * list)
{
char buffer[SCRIPT_GENERATE_BUFFER];
unsigned char read=0; //number of chars in buffer
unsigned char data;
char * token = strtok(list,"\r"); //token to be substituted for the tag
while(!ffeof(file))
{
data = ffget(file); //read one char from file
if(data == SCRIPT_GENERATE_TAG) //check if tag
{
data = ffget(file); // If data is tag, read next data
if(data != SCRIPT_GENERATE_TAG) // Skip if two tags are read
{ // Replace the tag with the next token
TCPsend(socket, read, buffer); // Send the buffer
TCPsend(socket, strlen(token), token); // Send the token
token = strtok(NULL,"\r"); // Extract the next token
read = 0; // Reset read
}
}
buffer[read++] = data; // Data read not tag, place in buffer
if(read == SCRIPT_GENERATE_BUFFER) //check if the buffer is full
{
TCPsend(socket, read, buffer); //send the buffer
read=0; //reset read
}
}//while
TCPsend(socket, read, buffer); //send the buffer
TCPclose(socket);
ffclose(file);
}
unsigned int readAd(unsigned char channel)
{
ADCSR = 0x14; //clear the ADIF bit, and use 16 for ADC Prescaler
ADMUX = channel; //select pin F1 as input to ADC
ADCSR |= 0xC0; //start the A/D conversion
while( !(ADCSR & 0x10)) //wait for the conversion to complete
{
}
return ADC;
}
/*
void acScript(SOCKET * socket, unsigned char *httpBuff)
{
unsigned char *nameStr;
unsigned char tempBuf[10];
unsigned char responseBuffer[150];
unsigned char *tempPoint;
FILE * file;
unsigned int itemp;
unsigned int graph;
// itemp = readAd(AD0); // Read the temperature with A/D converter
itemp = 26; // Debug
graph = (itemp - 10)*11; // Scale graph according to temperature
sprintf(tempBuf,"%d\r%d\r",itemp,graph); // Print first tokens seperated by \r
strcpy(responseBuffer,tempBuf); // Copy result to buffer
// read values into variables, the received string will be formated as follows:
// name1=value1&name2=value2&...
nameStr = strtok(httpBuff, "="); // Find first = sign
if(!strcmp(nameStr,"ac")) // Check for right POST form
{
do
{
tempPoint = strtok(NULL,"&"); // Extract token
if ( !strcmp(nameStr, "temp")) // Check if this is temperature variable
{
sprintf(tempBuf,"%s\r",tempPoint); // Yes, print token to buffer
strcat(responseBuffer,tempBuf);
}
else if ( !strcmp(nameStr, "fan")) // Check is this is fan varible
{
if(!strcmp(tempPoint,"low")) // Yes, check if fan is low
{
PORTB = ~LED1; // Set fan to low
strcat(responseBuffer,SCRIPT_CHECKED); // Print result to buffer
}
else
{
strcat(responseBuffer,SCRIPT_NOT_CHECKED); // Do not print buffer
}
if(!strcmp(tempPoint,"medium"))
{
PORTB = ~LED2;
strcat(responseBuffer,SCRIPT_CHECKED);
}
else
{
strcat(responseBuffer,SCRIPT_NOT_CHECKED);
}
if(!strcmp(tempPoint,"high"))
{
PORTB = ~LED3;
strcat(responseBuffer,SCRIPT_CHECKED);
}
else
{
strcat(responseBuffer,SCRIPT_NOT_CHECKED);
}
}
nameStr=strtok(NULL, "="); // Check for next variable
} while (nameStr != NULL); // Continue until all variables are read
file=ffopen("ac2.syn",'r'); // Open sync file
generateResponse(socket,file,responseBuffer); // Generate response based on results above
}
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -