Format cleanup

This commit is contained in:
Maurizio Porrato 2017-02-19 14:48:44 +00:00
parent 6e65a264df
commit 853266b151
1 changed files with 42 additions and 50 deletions

View File

@ -15,30 +15,28 @@ enum {
SSI_LED_STATE
};
int32_t ssi_handler(int32_t iIndex, char *pcInsert, int32_t iInsertLen)
{
int32_t ssi_handler(int32_t iIndex, char *pcInsert, int32_t iInsertLen) {
switch (iIndex) {
case SSI_UPTIME:
snprintf(pcInsert, iInsertLen, "%d",
xTaskGetTickCount() * portTICK_PERIOD_MS / 1000);
break;
case SSI_FREE_HEAP:
snprintf(pcInsert, iInsertLen, "%d", (int) xPortGetFreeHeapSize());
break;
case SSI_LED_STATE:
snprintf(pcInsert, iInsertLen, (GPIO.OUT & BIT(LED_PIN)) ? "Off" : "On");
break;
default:
snprintf(pcInsert, iInsertLen, "N/A");
break;
case SSI_UPTIME:
snprintf(pcInsert, iInsertLen, "%d",
xTaskGetTickCount() * portTICK_PERIOD_MS / 1000);
break;
case SSI_FREE_HEAP:
snprintf(pcInsert, iInsertLen, "%d", (int) xPortGetFreeHeapSize());
break;
case SSI_LED_STATE:
snprintf(pcInsert, iInsertLen, (GPIO.OUT & BIT(LED_PIN)) ? "Off" : "On");
break;
default:
snprintf(pcInsert, iInsertLen, "N/A");
break;
}
/* Tell the server how many characters to insert */
return (strlen(pcInsert));
}
char *gpio_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
char *gpio_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) {
for (int i = 0; i < iNumParams; i++) {
if (strcmp(pcParam[i], "on") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
@ -57,18 +55,15 @@ char *gpio_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValu
return "/index.ssi";
}
char *about_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
char *about_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) {
return "/about.html";
}
char *websocket_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
char *websocket_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) {
return "/websockets.html";
}
void websocket_task(void *pvParameter)
{
void websocket_task(void *pvParameter) {
struct tcp_pcb *pcb = (struct tcp_pcb *) pvParameter;
printf("Websocket process started\n");
@ -85,9 +80,9 @@ void websocket_task(void *pvParameter)
/* Generate response in JSON format */
char response[64];
int len = snprintf(response, sizeof (response),
"{\"uptime\" : \"%d\","
" \"heap\" : \"%d\","
" \"led\" : \"%d\"}", uptime, heap, led);
"{\"uptime\" : \"%d\","
" \"heap\" : \"%d\","
" \"led\" : \"%d\"}", uptime, heap, led);
if (len < sizeof (response))
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
@ -104,30 +99,29 @@ void websocket_task(void *pvParameter)
* Note: this function is executed on TCP thread and should return as soon
* as possible.
*/
void websocket_cb(struct tcp_pcb *pcb, uint8_t *data, u16_t data_len, uint8_t mode)
{
void websocket_cb(struct tcp_pcb *pcb, uint8_t *data, u16_t data_len, uint8_t mode) {
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char*) data);
uint8_t response[2];
uint16_t val;
switch (data[0]) {
case 'A': // ADC
/* This should be done on a separate thread in 'real' applications */
val = sdk_system_adc_read();
break;
case 'D': // Disable LED
gpio_write(LED_PIN, true);
val = 0xDEAD;
break;
case 'E': // Enable LED
gpio_write(LED_PIN, false);
val = 0xBEEF;
break;
default:
printf("Unknown command\n");
val = 0;
break;
case 'A': // ADC
/* This should be done on a separate thread in 'real' applications */
val = sdk_system_adc_read();
break;
case 'D': // Disable LED
gpio_write(LED_PIN, true);
val = 0xDEAD;
break;
case 'E': // Enable LED
gpio_write(LED_PIN, false);
val = 0xBEEF;
break;
default:
printf("Unknown command\n");
val = 0;
break;
}
response[1] = (uint8_t) val;
@ -140,8 +134,7 @@ void websocket_cb(struct tcp_pcb *pcb, uint8_t *data, u16_t data_len, uint8_t mo
* This function is called when new websocket is open and
* creates a new websocket_task if requested URI equals '/stream'.
*/
void websocket_open_cb(struct tcp_pcb *pcb, const char *uri)
{
void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) {
printf("WS URI: %s\n", uri);
if (!strcmp(uri, "/stream")) {
printf("request for streaming\n");
@ -149,8 +142,7 @@ void websocket_open_cb(struct tcp_pcb *pcb, const char *uri)
}
}
void httpd_task(void *pvParameters)
{
void httpd_task(void *pvParameters) {
tCGI pCGIs[] = {
{"/gpio", (tCGIHandler) gpio_cgi_handler},
{"/about", (tCGIHandler) about_cgi_handler},
@ -168,9 +160,9 @@ void httpd_task(void *pvParameters)
/* register handlers and start the server */
http_set_cgi_handlers(pCGIs, sizeof (pCGIs) / sizeof (pCGIs[0]));
http_set_ssi_handler((tSSIHandler) ssi_handler, pcConfigSSITags,
sizeof (pcConfigSSITags) / sizeof (pcConfigSSITags[0]));
sizeof (pcConfigSSITags) / sizeof (pcConfigSSITags[0]));
websocket_register_callbacks((tWsOpenHandler) websocket_open_cb,
(tWsHandler) websocket_cb);
(tWsHandler) websocket_cb);
printf("Entering httpd main loop\n");
httpd_init();
printf("httpd exiting\n");