// Lua: wifi.ap.config(table) static int wifi_ap_config( lua_State* L ) { if (!lua_istable(L, 1)) return luaL_error( L, "wrong arg type" ); struct softap_config config; wifi_softap_get_config(&config); size_t len; lua_getfield(L, 1, "ssid"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { const char *ssid = luaL_checklstring( L, -1, &len ); if(len<1 || len>32 || ssid == NULL) return luaL_error( L, "ssid:1~32" ); c_memset(config.ssid, 0, 32); c_memcpy(config.ssid, ssid, len); NODE_DBG(config.ssid); NODE_DBG("\n"); config.ssid_len = len; config.ssid_hidden = 0; } else return luaL_error( L, "wrong arg type" ); } else return luaL_error( L, "ssid required" ); lua_getfield(L, 1, "pwd"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the password string { const char *pwd = luaL_checklstring( L, -1, &len ); if(len<8 || len>64 || pwd == NULL) return luaL_error( L, "pwd:8~64" ); c_memset(config.password, 0, 64); c_memcpy(config.password, pwd, len); NODE_DBG(config.password); NODE_DBG("\n"); config.authmode = AUTH_WPA_WPA2_PSK; } else return luaL_error( L, "wrong arg type" ); } else{ config.authmode = AUTH_OPEN; } lua_getfield(L, 1, "auth"); if (!lua_isnil(L, -1)) { config.authmode = (uint8_t)luaL_checkinteger(L, -1); NODE_DBG("%d\n", config.authmode); } else { // keep whatever value resulted from "pwd" logic above } lua_getfield(L, 1, "channel"); if (!lua_isnil(L, -1)) { unsigned channel = luaL_checkinteger(L, -1); if (channel < 1 || channel > 13) return luaL_error( L, "channel:1~13" ); config.channel = (uint8_t)channel; NODE_DBG("%d\n", config.channel); } else { config.channel = 6; } lua_getfield(L, 1, "hidden"); if (!lua_isnil(L, -1)) { config.ssid_hidden = (uint8_t)luaL_checkinteger(L, -1); NODE_DBG("%d\n", config.ssid_hidden); NODE_DBG("\n"); } else { config.ssid_hidden = 0; } lua_getfield(L, 1, "max"); if (!lua_isnil(L, -1)) { unsigned max = luaL_checkinteger(L, -1); if (max < 1 || max > 4) return luaL_error( L, "max:1~4" ); config.max_connection = (uint8_t)max; NODE_DBG("%d\n", config.max_connection); } else { config.max_connection = 4; } lua_getfield(L, 1, "beacon"); if (!lua_isnil(L, -1)) { unsigned beacon = luaL_checkinteger(L, -1); if (beacon < 100 || beacon > 60000) return luaL_error( L, "beacon:100~60000" ); config.beacon_interval = (uint16_t)beacon; NODE_DBG("%d\n", config.beacon_interval); } else { config.beacon_interval = 100; } wifi_softap_set_config(&config); // system_restart(); return 0; }
/** * wifi.sta.listap() * Description: * scan and get ap list as a lua table into callback function. * Syntax: * wifi.sta.getap(function(table)) * wifi.sta.getap(format, function(table)) * wifi.sta.getap(cfg, function(table)) * wifi.sta.getap(cfg, format, function(table)) * Parameters: * cfg: table that contains scan configuration * Format:Select output table format. * 0 for the old format (SSID : Authmode, RSSI, BSSID, Channel) (Default) * 1 for the new format (BSSID : SSID, RSSI, Authmode, Channel) * function(table): a callback function to receive ap table when scan is done this function receive a table, the key is the ssid, value is other info in format: authmode,rssi,bssid,channel * Returns: * nil * * Example: --original function left intact to preserve backward compatibility wifi.sta.getap(function(T) for k,v in pairs(T) do print(k..":"..v) end end) --if no scan configuration is desired cfg can be set to nil or previous example can be used wifi.sta.getap(nil, function(T) for k,v in pairs(T) do print(k..":"..v) end end) --scan configuration scan_cfg={} scan_cfg.ssid="myssid" --if set to nil, ssid is not filtered scan_cfg.bssid="AA:AA:AA:AA:AA:AA" --if set to nil, MAC address is not filtered scan_cfg.channel=0 --if set to nil, channel will default to 0(scans all channels), if set scan will be faster scan_cfg.show_hidden=1 --if set to nil, show_hidden will default to 0 wifi.sta.getap(scan_cfg, function(T) for k,v in pairs(T) do print(k..":"..v) end end) */ static int wifi_station_listap( lua_State* L ) { if(wifi_get_opmode() == SOFTAP_MODE) { return luaL_error( L, "Can't list ap in SOFTAP mode" ); } gL = L; struct scan_config scan_cfg; getap_output_format=0; if (lua_type(L, 1)==LUA_TTABLE) { char ssid[32]; char bssid[6]; uint8 channel=0; uint8 show_hidden=0; size_t len; lua_getfield(L, 1, "ssid"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { const char *ssidstr = luaL_checklstring( L, -1, &len ); if(len>32) return luaL_error( L, "ssid:<32" ); c_memset(ssid, 0, 32); c_memcpy(ssid, ssidstr, len); scan_cfg.ssid=ssid; NODE_DBG(scan_cfg.ssid); NODE_DBG("\n"); } else return luaL_error( L, "wrong arg type" ); } else scan_cfg.ssid=NULL; lua_getfield(L, 1, "bssid"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { const char *macaddr = luaL_checklstring( L, -1, &len ); if(len!=17) return luaL_error( L, "bssid: FF:FF:FF:FF:FF:FF" ); c_memset(bssid, 0, 6); os_str2macaddr(bssid, macaddr); scan_cfg.bssid=bssid; NODE_DBG(MACSTR, MAC2STR(scan_cfg.bssid)); NODE_DBG("\n"); } else return luaL_error( L, "wrong arg type" ); } else scan_cfg.bssid=NULL; lua_getfield(L, 1, "channel"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isnumber(L, -1) ) // deal with the ssid string { channel = luaL_checknumber( L, -1); if(!(channel>=0 && channel<=13)) return luaL_error( L, "channel: 0 or 1-13" ); scan_cfg.channel=channel; NODE_DBG("%d\n", scan_cfg.channel); } else return luaL_error( L, "wrong arg type" ); } else scan_cfg.channel=0; lua_getfield(L, 1, "show_hidden"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isnumber(L, -1) ) // deal with the ssid string { show_hidden = luaL_checknumber( L, -1); if(show_hidden!=0 && show_hidden!=1) return luaL_error( L, "show_hidden: 0 or 1" ); scan_cfg.show_hidden=show_hidden; NODE_DBG("%d\n", scan_cfg.show_hidden); } else return luaL_error( L, "wrong arg type" ); } else scan_cfg.show_hidden=0; if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) { lua_pushnil(L); lua_insert(L, 2); } lua_pop(L, -4); } else if (lua_type(L, 1) == LUA_TNUMBER) { lua_pushnil(L); lua_insert(L, 1); } else if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION) { lua_pushnil(L); lua_insert(L, 1); lua_pushnil(L); lua_insert(L, 1); } else if(lua_isnil(L, 1)) { if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) { lua_pushnil(L); lua_insert(L, 2); } } else { return luaL_error( L, "wrong arg type" ); } if (lua_type(L, 2) == LUA_TNUMBER) //this section changes the output format { getap_output_format=luaL_checkinteger( L, 2 ); if ( getap_output_format != 0 && getap_output_format != 1) return luaL_error( L, "wrong arg type" ); } NODE_DBG("Use alternate output format: %d\n", getap_output_format); if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) { lua_pushvalue(L, 3); // copy argument (func) to the top of stack if(wifi_scan_succeed != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed); wifi_scan_succeed = luaL_ref(L, LUA_REGISTRYINDEX); if (lua_type(L, 1)==LUA_TTABLE) { wifi_station_scan(&scan_cfg,wifi_scan_done); } else { wifi_station_scan(NULL,wifi_scan_done); } } else { if(wifi_scan_succeed != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed); wifi_scan_succeed = LUA_NOREF; } }
/** * wifi.sta.config() * Description: * Set current Station configuration. * Note: If there are multiple APs with the same ssid, you can connect to a specific one by entering it's MAC address into the "bssid" field. * Syntax: * wifi.sta.getconfig(ssid, password) --Set STATION configuration, Auto-connect by default, Connects to any BSSID * wifi.sta.getconfig(ssid, password, Auto_connect) --Set STATION configuration, Auto-connect(0 or 1), Connects to any BSSID * wifi.sta.getconfig(ssid, password, bssid) --Set STATION configuration, Auto-connect by default, Connects to specific BSSID * wifi.sta.getconfig(ssid, password, Auto_connect, bssid) --Set STATION configuration, Auto-connect(0 or 1), Connects to specific BSSID * Parameters: * ssid: string which is less than 32 bytes. * Password: string which is less than 64 bytes. * Auto_connect: 0 (disable Auto-connect) or 1 (to enable Auto-connect). * bssid: MAC address of Access Point you would like to connect to. * Returns: * Nothing. * * Example: --Connect to Access Point automatically when in range wifi.sta.getconfig("myssid", "password") --Connect to Access Point, User decides when to connect/disconnect to/from AP wifi.sta.getconfig("myssid", "mypassword", 0) wifi.sta.connect() --do some wifi stuff wifi.sta.disconnect() --Connect to specific Access Point automatically when in range wifi.sta.getconfig("myssid", "mypassword", "12:34:56:78:90:12") --Connect to specific Access Point, User decides when to connect/disconnect to/from AP wifi.sta.getconfig("myssid", "mypassword", 0) wifi.sta.connect() --do some wifi stuff wifi.sta.disconnect() * */ static int wifi_station_config( lua_State* L ) { size_t sl, pl, ml; struct station_config sta_conf; int auto_connect=0; const char *ssid = luaL_checklstring( L, 1, &sl ); if (sl>32 || ssid == NULL) return luaL_error( L, "ssid:<32" ); const char *password = luaL_checklstring( L, 2, &pl ); if (pl!=0 && (pl<8 || pl>64) || password == NULL) return luaL_error( L, "pwd:0,8~64" ); if(lua_isnumber(L, 3)) { auto_connect=luaL_checkinteger( L, 3 );; if ( auto_connect != 0 && auto_connect != 1) return luaL_error( L, "wrong arg type" ); } else if (lua_isstring(L, 3)&& !(lua_isnumber(L, 3))) { lua_pushnil(L); lua_insert(L, 3); auto_connect=1; } else { if(lua_isnil(L, 3)) return luaL_error( L, "wrong arg type" ); auto_connect=1; } if(lua_isnumber(L, 4)) { sta_conf.bssid_set = 0; c_memset(sta_conf.bssid, 0, 6); } else { if (lua_isstring(L, 4)) { const char *macaddr = luaL_checklstring( L, 4, &ml ); if (ml!=17) return luaL_error( L, "MAC:FF:FF:FF:FF:FF:FF" ); c_memset(sta_conf.bssid, 0, 6); os_str2macaddr(sta_conf.bssid, macaddr); sta_conf.bssid_set = 1; } else { sta_conf.bssid_set = 0; c_memset(sta_conf.bssid, 0, 6); } } c_memset(sta_conf.ssid, 0, 32); c_memset(sta_conf.password, 0, 64); c_memcpy(sta_conf.ssid, ssid, sl); c_memcpy(sta_conf.password, password, pl); NODE_DBG(sta_conf.ssid); NODE_DBG(" %d\n", sl); NODE_DBG(sta_conf.password); NODE_DBG(" %d\n", pl); NODE_DBG(" %d\n", sta_conf.bssid_set); NODE_DBG( MACSTR, MAC2STR(sta_conf.bssid)); NODE_DBG("\n"); wifi_station_set_config(&sta_conf); wifi_station_disconnect(); if(auto_connect==0) { wifi_station_set_auto_connect(false); } else { wifi_station_set_auto_connect(true); wifi_station_connect(); } // station_check_connect(0); return 0; }
static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) { NODE_DBG("net_dns_found is called.\n"); struct espconn *pesp_conn = arg; if(pesp_conn == NULL){ NODE_DBG("pesp_conn null.\n"); return; } lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse; if(nud == NULL){ NODE_DBG("nud null.\n"); return; } if(nud->cb_dns_found_ref == LUA_NOREF){ NODE_DBG("cb_dns_found_ref null.\n"); return; } if(nud->self_ref == LUA_NOREF){ NODE_DBG("self_ref null.\n"); return; } /* original if(ipaddr == NULL) { NODE_ERR( "DNS Fail!\n" ); goto end; } // ipaddr->addr is a uint32_t ip char ip_str[20]; c_memset(ip_str, 0, sizeof(ip_str)); if(ipaddr->addr != 0) { c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr))); } lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function //lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua lua_pushstring(gL, ip_str); // the ip para */ // "enhanced" lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua if(ipaddr == NULL) { NODE_DBG( "DNS Fail!\n" ); lua_pushnil(gL); }else{ // ipaddr->addr is a uint32_t ip char ip_str[20]; c_memset(ip_str, 0, sizeof(ip_str)); if(ipaddr->addr != 0) { c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr))); } lua_pushstring(gL, ip_str); // the ip para } // "enhanced" end lua_call(gL, 2, 0); end: if((pesp_conn->type == ESPCONN_TCP && pesp_conn->proto.tcp->remote_port == 0) || (pesp_conn->type == ESPCONN_UDP && pesp_conn->proto.udp->remote_port == 0) ){ lua_gc(gL, LUA_GCSTOP, 0); if(nud->self_ref != LUA_NOREF){ luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref); nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self } lua_gc(gL, LUA_GCRESTART, 0); } }
// Lua: mqtt.Client(clientid, keepalive, user, pass) static int mqtt_socket_client( lua_State* L ) { NODE_DBG("enter mqtt_socket_client.\n"); lmqtt_userdata *mud; char tempid[20] = {0}; c_sprintf(tempid, "%s%x", "NodeMCU_", system_get_chip_id() ); NODE_DBG_(tempid); NODE_DBG("\n"); const char *clientId = tempid, *username = NULL, *password = NULL; size_t idl = c_strlen(tempid); size_t unl = 0, pwl = 0; int keepalive = 0; int stack = 1; unsigned secure = 0; int top = lua_gettop(L); // create a object mud = (lmqtt_userdata *)lua_newuserdata(L, sizeof(lmqtt_userdata)); // pre-initialize it, in case of errors mud->L = NULL; mud->self_ref = LUA_NOREF; mud->cb_connect_ref = LUA_NOREF; mud->cb_disconnect_ref = LUA_NOREF; mud->cb_message_ref = LUA_NOREF; mud->cb_suback_ref = LUA_NOREF; mud->cb_puback_ref = LUA_NOREF; mud->pesp_conn = NULL; mud->secure = 0; mud->keep_alive_tick = 0; mud->event_timeout = 0; mud->connState = MQTT_INIT; mud->connected = false; c_memset(&mud->mqttTimer, 0, sizeof(ETSTimer)); c_memset(&mud->mqtt_state, 0, sizeof(mqtt_state_t)); c_memset(&mud->connect_info, 0, sizeof(mqtt_connect_info_t)); // set its metatable luaL_getmetatable(L, "mqtt.socket"); lua_setmetatable(L, -2); mud->L = L; // L for mqtt module. if( lua_isstring(L,stack) ) // deal with the clientid string { clientId = luaL_checklstring( L, stack, &idl ); stack++; } if(lua_isnumber( L, stack )) { keepalive = luaL_checkinteger( L, stack); stack++; } if(keepalive == 0){ keepalive = MQTT_DEFAULT_KEEPALIVE; } if(lua_isstring( L, stack )){ username = luaL_checklstring( L, stack, &unl ); stack++; } if(username == NULL) unl = 0; NODE_DBG("lengh username: %d\r\n", unl); if(lua_isstring( L, stack )){ password = luaL_checklstring( L, stack, &pwl ); stack++; } if(password == NULL) pwl = 0; NODE_DBG("lengh password: %d\r\n", pwl); // TODO: check the zalloc result. mud->connect_info.client_id = (uint8_t *)c_zalloc(idl+1); mud->connect_info.username = (uint8_t *)c_zalloc(unl + 1); mud->connect_info.password = (uint8_t *)c_zalloc(pwl + 1); if(!mud->connect_info.client_id || !mud->connect_info.username || !mud->connect_info.password){ if(mud->connect_info.client_id) { c_free(mud->connect_info.client_id); mud->connect_info.client_id = NULL; } if(mud->connect_info.username) { c_free(mud->connect_info.username); mud->connect_info.username = NULL; } if(mud->connect_info.password) { c_free(mud->connect_info.password); mud->connect_info.password = NULL; } return luaL_error(L, "not enough memory"); } c_memcpy(mud->connect_info.client_id, clientId, idl); mud->connect_info.client_id[idl] = 0; c_memcpy(mud->connect_info.username, username, unl); mud->connect_info.username[unl] = 0; c_memcpy(mud->connect_info.password, password, pwl); mud->connect_info.password[pwl] = 0; NODE_DBG("MQTT: Init info: %s, %s, %s\r\n", mud->connect_info.client_id, mud->connect_info.username, mud->connect_info.password); mud->connect_info.clean_session = 1; mud->connect_info.will_qos = 0; mud->connect_info.will_retain = 0; mud->connect_info.keepalive = keepalive; mud->mqtt_state.pending_msg_q = NULL; mud->mqtt_state.auto_reconnect = 0; mud->mqtt_state.port = 1883; mud->mqtt_state.connect_info = &mud->connect_info; NODE_DBG("leave mqtt_socket_client.\n"); return 1; }
static int gprs_start( lua_State* L ) { uint32_t len; int timeout = luaL_checkinteger(L, 1); const char *APN[10]; char *APN_USED; char Param[BUFFER_SZ]; uint8_t i = 0; uint8_t j = 5; uint8_t result = 1; unsigned char cont = lua_gettop(L) - 1; APN[0] = lua_tolstring( L, 2, &len ); APN[1] = lua_tolstring( L, 3, &len ); APN[2] = lua_tolstring( L, 4, &len ); APN[3] = lua_tolstring( L, 5, &len ); APN[4] = lua_tolstring( L, 6, &len ); APN[5] = lua_tolstring( L, 7, &len ); APN[6] = lua_tolstring( L, 8, &len ); APN[7] = lua_tolstring( L, 9, &len ); APN[8] = lua_tolstring( L, 10, &len ); APN[9] = lua_tolstring( L, 11, &len ); c_memset(Param, 0 , BUFFER_SZ); if( APN[0] != NULL && APN[1] != NULL && APN[2] != NULL && APN[3] != NULL ) { result = 1; gprs_clear(L); milisec(1500); tcpip_getIpStatus(L); len = gprs_Attach(L); if( len == 1 ) { result = 2; for(i = 0; i < 5; i++) { gprs_clear(L); //tcpip_getIpStatus(L); if( APN[9] != NULL ) { APN_USED = (char*)APN[9]; c_sprintf(Param, CGDCONT, APN[9]); for(j = 0; j < 5; j++) { len = gprs_Activate(L,Param); if( len == 1 ) goto end_act; milisec(1000); } } if( j>= 5 ) { for(j = 0; j < cont; j++) { if( APN[j] != NULL ) { APN_USED = (char*)APN[j]; c_sprintf(Param, CGDCONT, APN[j]); len = gprs_Activate(L,Param); if( len == 1 ) goto end_act; milisec(1000); } } } } end_act: if( tcpip_getIpStatus(L) == 1 ) { tcpip_getIp(L); tcpip_getIpStatus(L); result = 0; lua_pushinteger(L, result); lua_pushstring(L,APN_USED); return 2; } } } lua_pushinteger(L, result); return 1; }