Microchip® Advanced Software Framework

cgi.c File Reference

CGI request function handlers.

Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.

#include <string.h>
#include <FreeRTOS.h>
#include <task.h>
#include "lwip/debug.h"
#include "lwip/tcp.h"
#include "afec.h"
#include "httpd.h"
#include "cgi.h"
#include "led.h"
#include "sysclk.h"

Data Structures

struct  archnames
 

Macros

#define CGI_LED_CMD_KEY   "set"
 
#define CGI_LED_ID_KEY   "n"
 
#define CGI_MATCH_EXT   2
 Select item in table if the extention match. More...
 
#define CGI_MATCH_NAME   3
 Select item in table if the string is content. More...
 
#define CGI_MATCH_NONE   0
 
#define CGI_MATCH_WORD   1
 Select item in table only if string match. More...
 
#define CGI_MSG_CMD_KEY   "msg"
 
#define CHIPID_ARCH
 
#define CHIPID_EPRCOC
 
#define CHIPID_NVPSIZ
 
#define CHIPID_NVTYP
 
#define CHIPID_SRAMSIZ
 
#define CLEAR_LED_STATUS(status, led_id)   ((status) &= ~(1 << (led_id)))
 
#define CONFIG_AFSK_TX_BUFLEN   1024
 FIFO tx buffer. More...
 
#define GET_LED_STATUS(status, led_id)   (((status) & (1 << (led_id))) >> (led_id))
 
#define IP_ADDR_TO_INT_TUPLE(addr)
 Macro to unpack the ip address from LwIP format into 4 integers. More...
 
#define SET_LED_STATUS(status, led_id)   ((status) |= (1 << (led_id)))
 

Functions

static int cgi_chipInfo (const char *name, char *recv_buf, size_t recv_len)
 Send the chip ID information. More...
 
static int cgi_displayMsg (const char *name, char *recv_buf, size_t recv_len)
 Read a message. More...
 
static int cgi_echo (const char *name, char *recv_buf, size_t recv_len)
 Function declarations. More...
 
static int cgi_error (const char *name, char *recv_buf, size_t recv_len)
 Used to handle unsupported CGI requests. More...
 
static int cgi_led (const char *name, char *recv_buf, size_t recv_len)
 Set the led status and send back the new status. More...
 
static int cgi_ledStatus (const char *name, char *recv_buf, size_t recv_len)
 Send the led status. More...
 
static int cgi_resistor (const char *name, char *recv_buf, size_t recv_len)
 Send the potentiometer voltage. More...
 
http_handler_t cgi_search (const char *name, HttpCGI *table)
 CGI request search engine. More...
 
static int cgi_status (const char *name, char *recv_buf, size_t recv_len)
 Send a JSON string representing the board status. More...
 
static int cgi_temp (const char *name, char *recv_buf, size_t recv_len)
 Send the internal core temperature in string format. More...
 
static int cgi_uptime (const char *name, char *recv_buf, size_t recv_len)
 Send system uptime. More...
 
static const char * chipid_archnames (unsigned value)
 Return the chip architecture name. More...
 
static const char * chipid_eproc_name (int idx)
 Return the chip ID name. More...
 
static const char * chipid_nvpsize (int idx)
 Return the non volatile programmable memory size. More...
 
static const char * chipid_nvptype (int idx)
 Return the chip volatile programmable memory type. More...
 
static const char * chipid_sramsize (int idx)
 Return the chip SRAM size. More...
 
static int sec_to_strDhms (u32_t sec_time, char *str, size_t len)
 convert seconds to day/hour/minute/sec. More...
 

Variables

static const char _unknown [] = "unknown"
 
HttpCGI cgi_table []
 CGI table where we associate one callback to one page. More...
 
static struct archnames chip_id_archnames []
 
static const char *const chip_id_eproc_names []
 
static const char *const chip_id_nvpsize []
 
static const char *const chip_id_nvptype []
 
static const char *const chip_id_sramsize []
 
static char key_value [80]
 HTTP buffer to store key value. More...
 
uint32_t mag_in_buffer_int []
 Magnitude buffer converted in float. More...
 
u8_t tx_buf [CONFIG_AFSK_TX_BUFLEN]
 

#define CGI_LED_CMD_KEY   "set"

Referenced by cgi_led().

#define CGI_LED_ID_KEY   "n"

Referenced by cgi_led().

#define CGI_MATCH_EXT   2

Select item in table if the extention match.

Referenced by cgi_search().

#define CGI_MATCH_NAME   3

Select item in table if the string is content.

Referenced by cgi_search().

#define CGI_MATCH_NONE   0
#define CGI_MATCH_WORD   1

Select item in table only if string match.

#define CGI_MSG_CMD_KEY   "msg"

Referenced by cgi_displayMsg().

#define CHIPID_ARCH
Value:
(((CHIPID->CHIPID_CIDR) & \
CHIPID_CIDR_ARCH_Msk) >> CHIPID_CIDR_ARCH_Pos)

Referenced by cgi_chipInfo().

#define CHIPID_EPRCOC
Value:
(((CHIPID->CHIPID_CIDR) & \
CHIPID_CIDR_EPROC_Msk) >> CHIPID_CIDR_EPROC_Pos)

Referenced by cgi_chipInfo().

#define CHIPID_NVPSIZ
Value:
(((CHIPID->CHIPID_CIDR) & \
CHIPID_CIDR_NVPSIZ_Msk) >> CHIPID_CIDR_NVPSIZ_Pos)

Referenced by cgi_chipInfo().

#define CHIPID_NVTYP
Value:
(((CHIPID->CHIPID_CIDR) & \
CHIPID_CIDR_NVPTYP_Msk) >> CHIPID_CIDR_NVPTYP_Pos)

Referenced by cgi_chipInfo().

#define CHIPID_SRAMSIZ
Value:
(((CHIPID->CHIPID_CIDR) & \
CHIPID_CIDR_SRAMSIZ_Msk) >> CHIPID_CIDR_SRAMSIZ_Pos)

Referenced by cgi_chipInfo().

#define CLEAR_LED_STATUS (   status,
  led_id 
)    ((status) &= ~(1 << (led_id)))

Referenced by cgi_led().

#define CONFIG_AFSK_TX_BUFLEN   1024

FIFO tx buffer.

#define GET_LED_STATUS (   status,
  led_id 
)    (((status) & (1 << (led_id))) >> (led_id))

Referenced by cgi_ledStatus(), and cgi_status().

#define IP_ADDR_TO_INT_TUPLE (   addr)
Value:
(int)((addr) >> 0 & 0xff), \
(int)((addr) >> 8 & 0xff), \
(int)((addr) >> 16 & 0xff), \
(int)((addr) >> 24 & 0xff)

Macro to unpack the ip address from LwIP format into 4 integers.

Referenced by cgi_status().

#define SET_LED_STATUS (   status,
  led_id 
)    ((status) |= (1 << (led_id)))

Referenced by cgi_led().

static int cgi_chipInfo ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Send the chip ID information.

Parameters
nameNot used.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
0.

References CHIPID_ARCH, chipid_archnames(), CHIPID_EPRCOC, chipid_eproc_name(), CHIPID_NVPSIZ, chipid_nvpsize(), chipid_nvptype(), CHIPID_NVTYP, CHIPID_SRAMSIZ, chipid_sramsize(), HTTP_CONTENT_JSON, http_sendOk(), http_write(), and UNUSED.

static int cgi_displayMsg ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Read a message.

Parameters
nameRequest containing the message.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
0.

References CGI_MSG_CMD_KEY, HTTP_CONTENT_JSON, http_getValue(), http_sendInternalErr(), http_sendOk(), http_tokenizeGetRequest(), and UNUSED.

static int cgi_echo ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Function declarations.

Send back the input string.

Parameters
nameNot used.
recv_bufReceive buffer to send back.
recv_lenReceive buffer length.
Returns
0.

References HTTP_CONTENT_PLAIN, http_sendOk(), http_write(), and UNUSED.

static int cgi_error ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Used to handle unsupported CGI requests.

Parameters
nameNot used.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
-1.

References UNUSED.

static int cgi_led ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Set the led status and send back the new status.

Parameters
nameString containing the led status request.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
0.

References CGI_LED_CMD_KEY, CGI_LED_ID_KEY, CLEAR_LED_STATUS, HTTP_CONTENT_JSON, http_getValue(), http_sendInternalErr(), http_sendOk(), http_tokenizeGetRequest(), http_write(), ioport_set_pin_level(), LED0_GPIO, LED1_GPIO, LED2_ACTIVE_LEVEL, LED2_GPIO, LED2_INACTIVE_LEVEL, LED3_GPIO, BoardStatus::led_status, SET_LED_STATUS, status, and UNUSED.

static int cgi_ledStatus ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Send the led status.

Parameters
nameNot used.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
0.

References GET_LED_STATUS, HTTP_CONTENT_JSON, http_sendOk(), http_write(), BoardStatus::led_status, status, and UNUSED.

static int cgi_resistor ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Send the potentiometer voltage.

Parameters
nameNot used.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
0.

References AFEC_CHANNEL_5, afec_channel_get_value(), HTTP_CONTENT_JSON, http_sendOk(), http_write(), and UNUSED.

http_handler_t cgi_search ( const char *  name,
HttpCGI table 
)

CGI request search engine.

Parameters
nameCGI request name.
tableCGI handler table.
Returns
A valid function handler for the specified CGI request, NULL otherwise.

References CGI_MATCH_EXT, CGI_MATCH_NAME, get_ext(), HttpCGI::handler, and NULL.

Referenced by http_recv().

static int cgi_status ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static
static int cgi_temp ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Send the internal core temperature in string format.

Parameters
nameNot used.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
0.

References HTTP_CONTENT_JSON, http_sendOk(), http_write(), BoardStatus::internal_temp, status, and UNUSED.

static int cgi_uptime ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Send system uptime.

Parameters
nameNot used.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
0.

References HTTP_CONTENT_JSON, http_sendOk(), http_write(), sec_to_strDhms(), status, UNUSED, and BoardStatus::up_time.

static const char* chipid_archnames ( unsigned  value)
static

Return the chip architecture name.

Parameters
idxIndex in the chip_id_archnames table.
Returns
The chip architecture name.

References _unknown, chip_id_archnames, and archnames::name.

Referenced by cgi_chipInfo().

static const char* chipid_eproc_name ( int  idx)
static

Return the chip ID name.

Parameters
idxIndex in the chip_id_eproc_names table.
Returns
The chip id name.

References _unknown, and chip_id_eproc_names.

Referenced by cgi_chipInfo().

static const char* chipid_nvpsize ( int  idx)
static

Return the non volatile programmable memory size.

Parameters
idxIndex in the chip_id_nvpsize table.
Returns
The chip non volatile programmable memory size.

References _unknown, and chip_id_nvpsize.

Referenced by cgi_chipInfo().

static const char* chipid_nvptype ( int  idx)
static

Return the chip volatile programmable memory type.

Parameters
idxIndex in the chip_id_nvptype table.
Returns
The chip non volatile programmable memory type.

References _unknown.

Referenced by cgi_chipInfo().

static const char* chipid_sramsize ( int  idx)
static

Return the chip SRAM size.

Parameters
idxIndex in the chip_id_sramsize table.
Returns
The chip SRAM size.

References _unknown, and chip_id_sramsize.

Referenced by cgi_chipInfo().

static int sec_to_strDhms ( u32_t  sec_time,
char *  str,
size_t  len 
)
static

convert seconds to day/hour/minute/sec.

Parameters
sec_timeTime value.
strBuffer to store date.
lenBuffer length.
Returns
0 on success, -1 otherwise.

References h.

Referenced by cgi_uptime().

const char _unknown[] = "unknown"
static
HttpCGI cgi_table[]
Initial value:
= {
{CGI_MATCH_NAME, "get_temperature", cgi_temp},
{CGI_MATCH_NAME, "get_uptime", cgi_uptime},
{CGI_MATCH_NAME, "get_resistor", cgi_resistor},
{CGI_MATCH_NAME, "set_led", cgi_led},
{CGI_MATCH_NAME, "get_ledStatus", cgi_ledStatus},
{CGI_MATCH_NAME, "status", cgi_status},
{CGI_MATCH_NAME, "get_chipinfo", cgi_chipInfo},
}
static int cgi_chipInfo(const char *name, char *recv_buf, size_t recv_len)
Send the chip ID information.
Definition: cgi.c:593
static int cgi_led(const char *name, char *recv_buf, size_t recv_len)
Set the led status and send back the new status.
Definition: cgi.c:343
static int cgi_error(const char *name, char *recv_buf, size_t recv_len)
Used to handle unsupported CGI requests.
Definition: cgi.c:435
static int cgi_displayMsg(const char *name, char *recv_buf, size_t recv_len)
Read a message.
Definition: cgi.c:622
#define NULL
Definition: def.h:47
static int cgi_resistor(const char *name, char *recv_buf, size_t recv_len)
Send the potentiometer voltage.
Definition: cgi.c:316
static int cgi_echo(const char *name, char *recv_buf, size_t recv_len)
Function declarations.
Definition: cgi.c:225
static int cgi_temp(const char *name, char *recv_buf, size_t recv_len)
Send the internal core temperature in string format.
Definition: cgi.c:243
#define CGI_MATCH_NAME
Select item in table if the string is content.
Definition: cgi.c:54
#define CGI_MATCH_NONE
Definition: cgi.c:48
static int cgi_ledStatus(const char *name, char *recv_buf, size_t recv_len)
Send the led status.
Definition: cgi.c:409
static int cgi_uptime(const char *name, char *recv_buf, size_t recv_len)
Send system uptime.
Definition: cgi.c:293
static int cgi_status(const char *name, char *recv_buf, size_t recv_len)
Send a JSON string representing the board status.
Definition: cgi.c:453

CGI table where we associate one callback to one page.

Referenced by http_recv().

struct archnames chip_id_archnames[]
static

Referenced by chipid_archnames().

const char* const chip_id_eproc_names[]
static
Initial value:
= {
"arm946es",
"arm7tdmi",
"cortex-m3",
"arm920t",
"arm926ejs",
"cortex-a5",
"cortex-m4"
}
static const char _unknown[]
Definition: cgi.c:85

Referenced by chipid_eproc_name().

const char* const chip_id_nvpsize[]
static
Initial value:
= {
"none",
"8K bytes",
"16K bytes",
"32K bytes",
"64K bytes",
"128K bytes",
"256K bytes",
"512K bytes",
"1024K bytes",
"2048K bytes",
}
static const char _unknown[]
Definition: cgi.c:85

Referenced by chipid_nvpsize().

const char* const chip_id_nvptype[]
static
Initial value:
= {
"rom",
"romless or onchip flash",
"embedded flash memory",
"rom(nvpsiz) + embedded flash (nvpsiz2)",
"sram emulating flash",
}
const char* const chip_id_sramsize[]
static
Initial value:
= {
"48K bytes",
"1K bytes",
"2K bytes",
"6K bytes",
"112K bytes",
"4K bytes",
"80K bytes",
"160K bytes",
"8K bytes",
"16K bytes",
"32K bytes",
"64K bytes",
"128K bytes",
"256K bytes",
"96K bytes",
"512K bytes",
}

Referenced by chipid_sramsize().

char key_value[80]
static

HTTP buffer to store key value.

uint32_t mag_in_buffer_int[]

Magnitude buffer converted in float.

Referenced by cgi_status(), dsp_task(), and gfx_refresh_mag().