Skip to content

Commit

Permalink
Add wifi and net module, connected to server successfully!
Browse files Browse the repository at this point in the history
Increace the useable code flash space(irom0)
  • Loading branch information
liangchen-harold committed Nov 28, 2014
1 parent 0559451 commit 8e09d2c
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 54 deletions.
6 changes: 3 additions & 3 deletions port_esp/app/Makefile
Expand Up @@ -12,8 +12,8 @@
# a generated lib/image xxx.a ()
#
TARGET = eagle
#FLAVOR = release
FLAVOR = debug
FLAVOR = release
#FLAVOR = debug

#EXTRA_CCFLAGS += -u

Expand Down Expand Up @@ -141,7 +141,7 @@ sinclude $(PDIR)Makefile

install: $(IMAGEODIR)/$(GEN_IMAGES)
./gen_misc.sh
../tools/esptool.py write_flash 0x00000 ../bin/eagle.app.v6.flash.bin 0x40000 ../bin/eagle.app.v6.irom0text.bin
../tools/esptool.py write_flash 0x00000 ../bin/eagle.app.v6.flash.bin 0x10000 ../bin/eagle.app.v6.irom0text.bin

#########################################################################
#
Expand Down
2 changes: 1 addition & 1 deletion port_esp/app/gen_misc.sh
Expand Up @@ -3,7 +3,7 @@ make
if [ $? == 0 ];then
rm ../bin/eagle.app.v6.flash.bin ../bin/eagle.app.v6.irom0text.bin ../bin/eagle.app.v6.dump ../bin/eagle.app.v6.S

cd .output/eagle/debug/image
cd .output/eagle/release/image

xt-objdump -x -s eagle.app.v6.out > ../../../../../bin/eagle.app.v6.dump
xt-objdump -S eagle.app.v6.out > ../../../../../bin/eagle.app.v6.S
Expand Down
4 changes: 4 additions & 0 deletions port_esp/app/include/lua/luaconf.h
Expand Up @@ -545,7 +545,11 @@
#define luai_numsub(a,b) ((a)-(b))
#define luai_nummul(a,b) ((a)*(b))
#define luai_numdiv(a,b) ((a)/(b))
#ifdef ESP_LUA_USE_FLOAT
#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
#else
#define luai_nummod(a,b) ((int)(a) % (int)(b))
#endif
#define luai_numpow(a,b) (pow(a,b))
#define luai_numunm(a) (-(a))
#define luai_numeq(a,b) ((a)==(b))
Expand Down
6 changes: 6 additions & 0 deletions port_esp/app/include/lua/lualib.h
Expand Up @@ -21,6 +21,12 @@ LUALIB_API int (luaopen_lnode_node) (lua_State *L);
#define LUA_LNODE_GPIO_NAME "gpio"
LUALIB_API int (luaopen_lnode_gpio) (lua_State *L);

#define LUA_LNODE_WIFI_NAME "wifi"
LUALIB_API int (luaopen_lnode_wifi) (lua_State *L);

#define LUA_LNODE_NET_NAME "net"
LUALIB_API int (luaopen_lnode_net) (lua_State *L);

#define LUA_COLIBNAME "coroutine"
LUALIB_API int (luaopen_base) (lua_State *L);

Expand Down
2 changes: 2 additions & 0 deletions port_esp/app/lua/linit.c
Expand Up @@ -25,6 +25,8 @@ static const luaL_Reg lualibs[] = {
//{LUA_DBLIBNAME, luaopen_debug},
{LUA_LNODE_NODE_NAME, luaopen_lnode_node},
{LUA_LNODE_GPIO_NAME, luaopen_lnode_gpio},
{LUA_LNODE_WIFI_NAME, luaopen_lnode_wifi},
{LUA_LNODE_NET_NAME, luaopen_lnode_net},
{NULL, NULL}
};

Expand Down
13 changes: 11 additions & 2 deletions port_esp/app/lua/lnode_gpio.c
Expand Up @@ -32,7 +32,7 @@ struct Pin
uint32_t mux;
uint8_t func;
};
static struct Pin pins[] = {
static const struct Pin pins[] = {
{PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0},
{PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1}, //TXD
{PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2},
Expand Down Expand Up @@ -120,5 +120,14 @@ for i = 1,10000 do print(i .. ":" .. node.free()) node.wdt() collectgarbage() en
for i = 1,1000000 do end
t={{0,0}, {2,0}, {4,0}, {5,0}, {12,0}, {13,0}, {14,0}, {15,1}}
table.foreach(t, function(i, v) gpio.mode(v[1],gpio.OUTPUT) gpio.write(v[1], v[2]) end)
table.foreach(t, function(j, v) gpio.mode(v[1],gpio.OUTPUT) gpio.write(v[1], v[2]) end)
table.foreach(socket, function(i, v) print(i .. ":") print(v) end)
a=net.createConnection(net.TCP, "aaaa", 80)
a:connect()
t={{0,0}, {2,0}, {4,0}, {5,0}, {12,0}, {13,0}, {14,0}, {15,1}}
for i = 1,10000 do print(i .. ":" .. node.free()) node.wdt() collectgarbage()
table.foreach(t, function(j, v) gpio.mode(v[1],gpio.OUTPUT) gpio.write(v[1], v[2]) end)
end
*/
262 changes: 262 additions & 0 deletions port_esp/app/lua/lnode_net.c
@@ -0,0 +1,262 @@
/*
** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
** Standard Operating System library
** See Copyright Notice in lua.h
*/


#include <os_type.h>
#include <ets_sys.h>
#include <ip_addr.h>
#include <mem.h>
#include <osapi.h>
#include <espconn.h>

#define loslib_c
#define LUA_LIB

#include "lua/lua.h"
#include "lua/portesp.h"

#include "lua/lauxlib.h"
#include "lua/lualib.h"

enum Protocol {
TCP = 0,
UDP,
};

typedef struct {
lua_State* L;
int r;
// uv_getaddrinfo_t handle;
} luv_dns_ref_t;

typedef struct {
struct espconn *pConn;
} Socket;

/* Utility for storing the callback in the dns_req token */
static luv_dns_ref_t* ICACHE_FLASH_ATTR luv_dns_store_callback(lua_State* L, int index) {
luv_dns_ref_t* ref;

ref = (luv_dns_ref_t*)os_malloc(sizeof(luv_dns_ref_t));
ref->L = L;
if (lua_isfunction(L, index)) {
lua_pushvalue(L, index); /* Store the callback */
ref->r = luaL_ref(L, LUA_REGISTRYINDEX);
}
return ref;
}

static void ICACHE_FLASH_ATTR luv_dns_ref_cleanup(luv_dns_ref_t *ref)
{
if (ref != NULL)
{
os_free(ref);
}
}

static void ICACHE_FLASH_ATTR luv_dns_get_callback(luv_dns_ref_t *ref)
{
lua_State *L = ref->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, ref->r);
luaL_unref(L, LUA_REGISTRYINDEX, ref->r);
}

/******************************************************************************
* FunctionName : on_dns_found
* Description : dns found callback
* Parameters : name -- pointer to the name that was looked up.
* ipaddr -- pointer to an ip_addr_t containing the IP address of
* the hostname, or NULL if the name could not be found (or on any
* other error).
* callback_arg -- a user-specified callback argument passed to
* dns_gethostbyname
* Returns : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
on_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
{
struct espconn *pespconn = (struct espconn *)arg;

luv_dns_ref_t *ref = (luv_dns_ref_t *)pespconn->reverse;

if (ipaddr != NULL)
{
char buf[16];
char *p = (char*)&(ipaddr->addr);
os_sprintf(buf, IPSTR, p[0], p[1], p[2], p[3]);

luv_dns_get_callback(ref);

lua_pushnil(ref->L);
lua_pushstring(ref->L, buf);
}
else
{
lua_pushstring(ref->L, "error!");
lua_pushnil(ref->L);
}
lua_pcall(ref->L, 2, 0, 0);

luv_dns_ref_cleanup(ref);
os_free(pespconn);
}

ip_addr_t dummy_ip;
static int ICACHE_FLASH_ATTR lnode_net_dns (lua_State *L)
{
const char* host = luaL_checkstring(L, 1);
luv_dns_ref_t *ref = luv_dns_store_callback(L, 2);

struct espconn *pCon = (struct espconn *)os_zalloc(sizeof(struct espconn));
pCon->type = ESPCONN_TCP;
pCon->state = ESPCONN_NONE;
pCon->reverse = ref;

espconn_gethostbyname(pCon, host, &dummy_ip, on_dns_found);

return 0;
}

static void ICACHE_FLASH_ATTR tcpclient_connect_cb(void *arg);

static int ICACHE_FLASH_ATTR lnode_net_createConnection (lua_State *L)
{
int protocol = luaL_checkinteger(L, 1);
const char *host = luaL_checkstring(L, 2);
int port = luaL_checkinteger(L, 3);

/* return Socket.new()
*
*/
Socket *s = lua_newuserdata(L,sizeof(Socket));
luaL_getmetatable(L, "Socket");
lua_setmetatable(L, -2);

s->pConn = (struct espconn *)os_zalloc(sizeof(struct espconn));
if (protocol == TCP)
{
s->pConn->type = ESPCONN_TCP;
s->pConn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
//os_memcpy(s->pConn->proto.tcp->remote_ip, host, 4);
*((uint8 *) &(s->pConn->proto.tcp->remote_ip)) = 115;
*((uint8 *) &(s->pConn->proto.tcp->remote_ip)+1) = 28;
*((uint8 *) &(s->pConn->proto.tcp->remote_ip)+2) = 78;
*((uint8 *) &(s->pConn->proto.tcp->remote_ip)+3) = 47;

s->pConn->proto.tcp->local_port = espconn_port();
s->pConn->proto.tcp->remote_port = port;

espconn_regist_connectcb(s->pConn, tcpclient_connect_cb);
//espconn_regist_reconcb(s->pConn, tcpclient_recon_cb);
}
else if (protocol == UDP)
{
s->pConn->type = ESPCONN_UDP;
s->pConn->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
os_memcpy(s->pConn->proto.udp->remote_ip, host, 4);
s->pConn->proto.udp->local_port = espconn_port();
s->pConn->proto.udp->remote_port = port;
}
__printf("At: 0x%08X\n", s);

return 1;
}

/* ==socket====================================================== */
/**
* @brief Tcp client connect success callback function.
* @param arg: contain the ip link information
* @retval None
*/
static void ICACHE_FLASH_ATTR tcpclient_connect_cb(void *arg)
{
struct espconn *pConn = (struct espconn *)arg;

__printf("tcp client connect\n");

// espconn_regist_disconcb(pespconn, at_tcpclient_discon_cb);
// espconn_regist_recvcb(pespconn, at_tcpclient_recv);////////
// espconn_regist_sentcb(pespconn, at_tcpclient_sent_cb);///////
}

int ICACHE_FLASH_ATTR lnode_net_socket_gc(lua_State* L) {
__printf("## __gc\n");

Socket *s = (Socket *)luaL_checkudata(L, 1, "Socket");
if (s->pConn->proto.tcp != NULL)
{
os_free(s->pConn->proto.tcp);
}
if (s->pConn->proto.udp != NULL)
{
os_free(s->pConn->proto.udp);
}
os_free(s->pConn);
return 0;
}
// 只能通过createConnection创建socket!
// int ICACHE_FLASH_ATTR lnode_net_socket_new(lua_State* L) {
// __printf("## new\n");
//
// void *p = lua_newuserdata(L,sizeof(Socket));
// luaL_getmetatable(L, "Socket");
// lua_setmetatable(L, -2);
// return 1;
// }

static int ICACHE_FLASH_ATTR lnode_net_socket_connect (lua_State *L)
{
Socket *s = (Socket *)luaL_checkudata(L, 1, "Socket");

espconn_connect(s->pConn);

return 0;
}

static const luaL_Reg lnode_net_socket_methods[] = {
{"connect", lnode_net_socket_connect},
{ NULL, NULL }
};

/* ====================================================== */

static const luaL_Reg lnode_net_lib[] = {
{"dns", lnode_net_dns},
{"createConnection", lnode_net_createConnection},
{NULL, NULL}
};

LUALIB_API int luaopen_lnode_net (lua_State *L) {
luaL_register(L, LUA_LNODE_NET_NAME, lnode_net_lib);
lua_pushnumber(L, TCP);
lua_setfield(L, -2, "TCP");
lua_pushnumber(L, UDP);
lua_setfield(L, -2, "UDP");

// lua_createtable(L, 0, 1);
//
// lua_pushstring(L, "connect");
// lua_pushcclosure(L, lnode_net_socket_connect, 0);
// lua_settable(L, -3); /* 3rd element from the stack top */
//
// lua_setfield(L, -2, "socket");
luaL_newmetatable(L, "Socket"); //leaves new metatable on the stack
lua_pushvalue(L, -1); // there are two 'copies' of the metatable on the stack
lua_setfield(L, -2, "__index"); // pop one of those copies and assign it to
// __index field od the 1st metatable
// lua_pushcfunction(L, lnode_net_socket_new);
// lua_setfield(L,-2, "new");
lua_pushcfunction(L, lnode_net_socket_gc);
lua_setfield(L,-2, "__gc");

luaL_register(L, NULL, lnode_net_socket_methods); // register functions in the metatable
//luaL_register(L, "classname", lnode_net_socket_meta);

return 1;
}

/* Testing code:
*/

0 comments on commit 8e09d2c

Please sign in to comment.