Skip to content

Commit

Permalink
Merge pull request #3 from whitecatboard/the0ne-wifi-fix
Browse files Browse the repository at this point in the history
HTTP server, added a mechanism in Net driver for register a callback into the event loop + lua pages preprocessor
  • Loading branch information
the0ne committed Jun 6, 2017
2 parents 19789b9 + d19d45d commit 4cd1e55
Show file tree
Hide file tree
Showing 28 changed files with 663 additions and 267 deletions.
8 changes: 8 additions & 0 deletions Makefile
Expand Up @@ -52,4 +52,12 @@ restore-idf:
@cd $(IDF_PATH)/components/tcpip_adapter && git checkout tcpip_adapter_lwip.c
@cd $(IDF_PATH)/components/newlib/include/sys && git checkout syslimits.h

flash-args:
@echo $(subst --port $(ESPPORT),, \
$(subst python /components/esptool_py/esptool/esptool.py,, \
$(subst $(IDF_PATH),, $(ESPTOOLPY_WRITE_FLASH))\
)\
) \
$(subst /build/, , $(subst /build/bootloader/,, $(subst $(PROJECT_PATH), , $(ESPTOOL_ALL_FLASH_ARGS))))

include $(IDF_PATH)/make/project.mk
69 changes: 15 additions & 54 deletions components/http/httpsrv.c
Expand Up @@ -29,6 +29,8 @@

#if LUA_USE_HTTP

#include "preprocessor.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
Expand Down Expand Up @@ -198,6 +200,7 @@ int http_print(lua_State* L) {
void send_file(FILE *f, char *path, struct stat *statbuf, char *requestdata) {
int n, inLua, line;
char data[HTTP_BUFF_SIZE];

char *p1, *p2;
FILE *file = fopen(path, "r");

Expand All @@ -215,60 +218,19 @@ void send_file(FILE *f, char *path, struct stat *statbuf, char *requestdata) {
lua_pushlightuserdata(LL, (void*)f);
lua_setglobal(LL, "http_stream_handle");

file = fopen(path, "r");
inLua = false;
line = 0;
while (fgets(data, sizeof (data), file)) {
line++;
n = strlen(data);

for(p1 = data; p1-data < n; ) {

if(inLua) {

p2 = strcasestr(p1, "?>");
if(p2) {
*p2 = 0;
inLua = false;
p2+=2;
//remove newline after lua code
while(*p2=='\r' || *p2=='\n') p2++;
}
else {
p2 = data + n; //all of this line was done
}
if (*p1 != 0) {
if (luaL_dostring(LL, p1)) {
//printf("LUA Error at %s in %s on line %i", lua_tostring(LL, -1), path, line);
chunk(f, "LUA Error at %s in %s on line %i", lua_tostring(LL, -1), path, line);
}
}
p1 = p2;
}
else {

p2 = strcasestr(p1, "<?lua");
if(p2) {
*p2 = 0;
inLua = true;
p2+=5;
//ignore newline before lua code
while(*p2=='\r' || *p2=='\n') p2++;
}
else {
p2 = data + n; //all of this line was done
}
if (*p1 != 0) {
chunk(f, "%s", p1);
}
p1 = p2;
}

}
char ppath[PATH_MAX];

fflush(f);
strcpy(ppath, path);
if (strlen(ppath) < PATH_MAX - 1) {
strcat(ppath, "p");
}
fclose(file);

http_process_lua_page(path,ppath);

int rc = luaL_dofile(LL, ppath);
(void) rc;

fflush(f);

fprintf(f, "0\r\n\r\n");

Expand Down Expand Up @@ -451,7 +413,7 @@ int process(FILE *f) {

} // while
} // AP mode

if (!method || !path) return -1; //protocol may be omitted

syslog(LOG_DEBUG, "http: %s %s %s\r", method, path, protocol);
Expand Down Expand Up @@ -607,7 +569,6 @@ static void *http_thread(void *arg) {
if we'd properly close the socket we would need to bind() again
when restarting the httpsrv but bind() will succeed only after
several (~4) minutes: http://lwip.wikia.com/wiki/Netconn_bind
"Note that if you try to bind the same address and/or port you
might get an error (ERR_USE, address in use), even if you
delete the netconn. Only after some time (minutes) the
Expand Down
187 changes: 187 additions & 0 deletions components/http/preprocessor.c
@@ -0,0 +1,187 @@
/*
* Lua RTOS, http lua page preprocessor
*
* Copyright (C) 2015 - 2017
* IBEROXARXA SERVICIOS INTEGRALES, S.L. & CSS IBÉRICA, S.L.
*
* Author: Jaume Olivé (jolive@iberoxarxa.com / jolive@whitecatboard.org)
*
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and this
* permission notice and warranty disclaimer appear in supporting
* documentation, and that the name of the author not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* The author disclaim all warranties with regard to this
* software, including all implied warranties of merchantability
* and fitness. In no event shall the author be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether
* in an action of contract, negligence or other tortious action,
* arising out of or in connection with the use or performance of
* this software.
*/

#include <stdio.h>

int http_process_lua_page(const char *ipath, const char *opath) {
FILE *ifp; // Input file
FILE *ofp; // Output file

int c;
char string;
char lua = 0;
char delim;
char io_write = 0;
char add_cr = 0;
char buff[6];

const char *bt = "<?lua";
const char *et = "?>";

const char *cbt = bt;
const char *cet = et;
char *cbuff = buff;

// Open input file
ifp = fopen(ipath,"r");
if (!ifp) {
return -1;
}

// Open output file
ofp = fopen(opath,"w+");
if (!ofp) {
fclose(ifp);

return -1;
}

string = 0;
*cbuff = '\0';

fprintf(ofp, "do\n");
fprintf(ofp, "local print = net.service.http.print_chunk\n");

while((c = fgetc(ifp)) != EOF) {
if (c == '"') {
if (!string) {
delim = '\"';
string = 1;
} else {
if (c == delim) {
string = 0;
}
}
} else if (c == '\'') {
if (!string) {
delim = '\'';
string = 1;
} else {
if (c == delim) {
string = 0;
}
}
}

if ((c == *cbt) && (!string)) {
cbt++;
if (!*cbt) {
lua = 1;
add_cr = 1;
cbuff = buff;
} else {
*cbuff++ = c;
}

*cbuff = '\0';
continue;
} else if ((c == *cet) && (!string)) {
cet++;
if (!*cet) {
lua = 0;
add_cr = 1;
cbuff = buff;
} else {
*cbuff++ = c;
}

*cbuff = '\0';
continue;
} else {
cbt = bt;
cet = et;

cbuff = buff;

while (*cbuff) {
if (c == '\n') {
if (io_write) {
fprintf(ofp, "\")\n");
io_write = 0;
}
} else if (c == '\r') {
continue;
} else if (c == '\"') {
fprintf(ofp, "\\%c",c);
} else {
if (!io_write) {
if (add_cr) {
fprintf(ofp, "\n");
add_cr = 0;
}
fprintf(ofp, "print(\"");
io_write = 1;
}
fprintf(ofp, "%c",*cbuff);
}

cbuff++;
}

if (!lua) {
if (c == '\n') {
if (io_write) {
fprintf(ofp, "\")\n");
io_write = 0;
}
} else if (c == '\r') {
continue;
} else if (c == '\"') {
fprintf(ofp, "\\%c",c);
} else {
if (!io_write) {
if (add_cr) {
fprintf(ofp, "\n");
add_cr = 0;
}
fprintf(ofp, "print(\"");
io_write = 1;
}
fprintf(ofp, "%c",c);
}
} else {
fprintf(ofp, "%c",c);
}

cbuff = buff;
*cbuff = '\0';
}
}

if (io_write) {
fprintf(ofp, "\")\n");
}

fprintf(ofp, "end");

fclose(ifp);
fclose(ofp);

return 0;
}
35 changes: 35 additions & 0 deletions components/http/preprocessor.h
@@ -0,0 +1,35 @@
/*
* Lua RTOS, http lua page preprocessor
*
* Copyright (C) 2015 - 2017
* IBEROXARXA SERVICIOS INTEGRALES, S.L. & CSS IBÉRICA, S.L.
*
* Author: Jaume Olivé (jolive@iberoxarxa.com / jolive@whitecatboard.org)
*
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and this
* permission notice and warranty disclaimer appear in supporting
* documentation, and that the name of the author not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* The author disclaim all warranties with regard to this
* software, including all implied warranties of merchantability
* and fitness. In no event shall the author be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether
* in an action of contract, negligence or other tortious action,
* arising out of or in connection with the use or performance of
* this software.
*/

#ifndef HTTP_PREPROCESSOR_H_
#define HTTP_PREPROCESSOR_H_

int http_process_lua_page(const char *ipath, const char *opath);

#endif
2 changes: 1 addition & 1 deletion components/lora/gateway/src/loragw_spi.native.c
Expand Up @@ -80,7 +80,7 @@ int lgw_spi_open(void **spi_target_ptr) {
/* open SPI device */
driver_error_t *error;

if ((error = spi_setup(CONFIG_LUA_RTOS_LORA_GATEWAY_SPI, 1, CONFIG_LUA_RTOS_LORA_GATEWAY_CS, 0, CONFIG_LUA_RTOS_LORA_GATEWAY_SPEED, &dev))) {
if ((error = spi_setup(CONFIG_LUA_RTOS_LORA_GATEWAY_SPI, 1, CONFIG_LUA_RTOS_LORA_GATEWAY_CS, 0, CONFIG_LUA_RTOS_LORA_GATEWAY_SPEED, SPI_FLAG_WRITE | SPI_FLAG_READ, &dev))) {
DEBUG_PRINTF("ERROR: failed to open SPI device SPI%d\n", CONFIG_LUA_RTOS_LORA_GATEWAY_SPI);
return LGW_SPI_ERROR;
}
Expand Down
2 changes: 1 addition & 1 deletion components/lora/node/lmic/lmic_hal.c
Expand Up @@ -140,7 +140,7 @@ driver_error_t *hal_init (void) {
driver_error_t *error;

// Init SPI bus
if ((error = spi_setup(CONFIG_LUA_RTOS_LORA_NODE_SPI, 1, CONFIG_LUA_RTOS_LORA_NODE_CS, 0, LMIC_SPI_KHZ * 1000, &spi_device))) {
if ((error = spi_setup(CONFIG_LUA_RTOS_LORA_NODE_SPI, 1, CONFIG_LUA_RTOS_LORA_NODE_CS, 0, LMIC_SPI_KHZ * 1000, SPI_FLAG_WRITE | SPI_FLAG_READ, &spi_device))) {
syslog(LOG_ERR, "lmic cannot open spi%u", CONFIG_LUA_RTOS_LORA_NODE_SPI);
return error;
}
Expand Down
4 changes: 2 additions & 2 deletions components/lua_rtos/Lua/common/lrotable.c
Expand Up @@ -61,7 +61,7 @@ void luaA_pushobject(lua_State *L, const TValue *o) {

/* Find an entry in a rotable and return it */
static const IRAM_ATTR TValue *luaR_auxfind(const luaR_entry *pentry, const char *k, luaR_numkey nk, unsigned *ppos) {
const TValue *res = luaO_nilobject;
const TValue *res = NULL;
const luaR_entry *entry = pentry;
int i = 0;

Expand Down Expand Up @@ -119,7 +119,7 @@ static const IRAM_ATTR TValue *luaR_auxfind(const luaR_entry *pentry, const char
const IRAM_ATTR TValue *luaR_findglobal(const char *name) {
// Try to get from cache
#if CONFIG_LUA_RTOS_LUA_USE_ROTABLE_CACHE
const TValue *res;
const TValue *res = NULL;

res = rotable_cache_get(lua_rotable, name);
if (res) {
Expand Down
1 change: 1 addition & 0 deletions components/lua_rtos/Lua/modules/lua_adds.inc
Expand Up @@ -30,6 +30,7 @@
#include "pthread.h"
#include "linenoise.h"
#include "shell.h"
#include "cache.h"

#include <limits.h>

Expand Down

0 comments on commit 4cd1e55

Please sign in to comment.