Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
b2190e4d
Commit
b2190e4d
authored
Dec 26, 2015
by
dnc40085
Browse files
Added functions: wifi.sta.sethostname and wifi.sta.gethostname and
option to set hostname in user.config.h
parent
2f655dee
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/include/user_config.h
View file @
b2190e4d
...
@@ -83,6 +83,17 @@
...
@@ -83,6 +83,17 @@
#define ENDUSER_SETUP_AP_SSID "SetupGadget"
#define ENDUSER_SETUP_AP_SSID "SetupGadget"
/*
* A valid hostname only contains alphanumeric and hyphen(-) characters, with no hyphens at first or last char
* if WIFI_STA_HOSTNAME not defined: hostname will default to NODE-xxxxxx (xxxxxx being last 3 octets of MAC address)
* if WIFI_STA_HOSTNAME defined: hostname must only contain alphanumeric characters
* if WIFI_STA_HOSTNAME_APPEND_MAC not defined: Hostname MUST be 32 chars or less
* if WIFI_STA_HOSTNAME_APPEND_MAC defined: Hostname MUST be 26 chars or less, since last 3 octets of MAC address will be appended
* if defined hostname is invalid: hostname will default to NODE-xxxxxx (xxxxxx being last 3 octets of MAC address)
*/
//#define WIFI_STA_HOSTNAME "NodeMCU"
//#define WIFI_STA_HOSTNAME_APPEND_MAC
#define STRBUF_DEFAULT_INCREMENT 32
#define STRBUF_DEFAULT_INCREMENT 32
#endif
/* __USER_CONFIG_H__ */
#endif
/* __USER_CONFIG_H__ */
app/modules/wifi.c
View file @
b2190e4d
...
@@ -920,6 +920,65 @@ static int wifi_station_listap( lua_State* L )
...
@@ -920,6 +920,65 @@ static int wifi_station_listap( lua_State* L )
}
}
}
}
// Lua: wifi.sta.gethostname()
static
int
wifi_sta_gethostname
(
lua_State
*
L
)
{
char
*
hostname
=
wifi_station_get_hostname
();
lua_pushstring
(
L
,
hostname
);
return
1
;
}
static
uint8
wifi_sta_sethostname
(
const
char
*
hostname
,
size_t
len
)
{
const
char
*
charset
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-"
;
size_t
span
=
strspn
(
hostname
,
charset
);
if
(
hostname
[
span
]
==
'\0'
&&
hostname
[
span
-
1
]
!=
'-'
&&
hostname
[
0
]
!=
'-'
&&
(
len
>=
1
&&
len
<=
32
))
{
NODE_DBG
(
"
\n\t
string is:
\"
%s
\"\n\t
length is: %d
\n
"
,
hostname
,
len
);
if
(
wifi_station_set_hostname
((
char
*
)
hostname
))
{
NODE_DBG
(
"
\n\t
wifi_sta_sethostname returned: true
\n
"
);
return
1
;
//pass
}
else
{
NODE_DBG
(
"
\n\t
wifi_sta_sethostname returned: false
\n
"
);
return
0
;
//fail
}
}
else
{
NODE_DBG
(
"
\n\t
Invalid hostname!
\n
"
);
return
2
;
//Invalid hostname
}
}
// Lua: wifi.sta.sethostname()
static
int
wifi_sta_sethostname_lua
(
lua_State
*
L
)
{
size_t
len
;
const
char
*
hostname
=
luaL_checklstring
(
L
,
1
,
&
len
);
uint8
retval
=
wifi_sta_sethostname
(
hostname
,
len
);
NODE_DBG
(
"
\n\t
wifi_sta_sethostname returned: %d
\n
"
,
retval
);
if
(
retval
==
0
)
{
lua_pushboolean
(
L
,
0
);
return
1
;
}
else
if
(
retval
==
1
)
{
lua_pushboolean
(
L
,
1
);
return
1
;
}
else
if
(
retval
==
2
)
{
return
luaL_error
(
L
,
"Invalid hostname!"
);
}
}
// Lua: wifi.sta.status()
// Lua: wifi.sta.status()
static
int
wifi_station_status
(
lua_State
*
L
)
static
int
wifi_station_status
(
lua_State
*
L
)
{
{
...
@@ -1349,6 +1408,8 @@ static const LUA_REG_TYPE wifi_station_map[] = {
...
@@ -1349,6 +1408,8 @@ static const LUA_REG_TYPE wifi_station_map[] = {
{
LSTRKEY
(
"getmac"
),
LFUNCVAL
(
wifi_station_getmac
)
},
{
LSTRKEY
(
"getmac"
),
LFUNCVAL
(
wifi_station_getmac
)
},
{
LSTRKEY
(
"setmac"
),
LFUNCVAL
(
wifi_station_setmac
)
},
{
LSTRKEY
(
"setmac"
),
LFUNCVAL
(
wifi_station_setmac
)
},
{
LSTRKEY
(
"getap"
),
LFUNCVAL
(
wifi_station_listap
)
},
{
LSTRKEY
(
"getap"
),
LFUNCVAL
(
wifi_station_listap
)
},
{
LSTRKEY
(
"sethostname"
),
LFUNCVAL
(
wifi_sta_sethostname_lua
)
},
{
LSTRKEY
(
"gethostname"
),
LFUNCVAL
(
wifi_sta_gethostname
)
},
{
LSTRKEY
(
"status"
),
LFUNCVAL
(
wifi_station_status
)
},
{
LSTRKEY
(
"status"
),
LFUNCVAL
(
wifi_station_status
)
},
{
LSTRKEY
(
"eventMonReg"
),
LFUNCVAL
(
wifi_station_event_mon_reg
)
},
{
LSTRKEY
(
"eventMonReg"
),
LFUNCVAL
(
wifi_station_event_mon_reg
)
},
{
LSTRKEY
(
"eventMonStart"
),
LFUNCVAL
(
wifi_station_event_mon_start
)
},
{
LSTRKEY
(
"eventMonStart"
),
LFUNCVAL
(
wifi_station_event_mon_start
)
},
...
@@ -1421,4 +1482,40 @@ static const LUA_REG_TYPE wifi_map[] = {
...
@@ -1421,4 +1482,40 @@ static const LUA_REG_TYPE wifi_map[] = {
{
LNILKEY
,
LNILVAL
}
{
LNILKEY
,
LNILVAL
}
};
};
NODEMCU_MODULE
(
WIFI
,
"wifi"
,
wifi_map
,
NULL
);
int
luaopen_wifi
(
lua_State
*
L
)
{
#ifndef WIFI_STA_HOSTNAME
char
temp
[
32
];
uint8_t
mac
[
6
];
wifi_get_macaddr
(
STATION_IF
,
mac
);
c_sprintf
(
temp
,
"NODE-%X%X%X"
,
(
mac
)[
3
],
(
mac
)[
4
],
(
mac
)[
5
]);
wifi_sta_sethostname
((
const
char
*
)
temp
,
strlen
(
temp
));
#elif defined(WIFI_STA_HOSTNAME) && !defined(WIFI_STA_HOSTNAME_APPEND_MAC)
const
char
*
hostname
=
WIFI_STA_HOSTNAME
;
uint8
retval
=
wifi_sta_sethostname
(
hostname
,
strlen
(
hostname
));
if
(
retval
!=
1
)
{
char
temp
[
32
];
uint8_t
mac
[
6
];
wifi_get_macaddr
(
STATION_IF
,
mac
);
c_sprintf
(
temp
,
"NODE-%X%X%X"
,
(
mac
)[
3
],
(
mac
)[
4
],
(
mac
)[
5
]);
wifi_sta_sethostname
((
const
char
*
)
temp
,
strlen
(
temp
));
}
#elif defined(WIFI_STA_HOSTNAME) && defined(WIFI_STA_HOSTNAME_APPEND_MAC)
char
temp
[
32
];
uint8_t
mac
[
6
];
wifi_get_macaddr
(
STATION_IF
,
mac
);
c_sprintf
(
temp
,
"%s%X%X%X"
,
WIFI_STA_HOSTNAME
,
(
mac
)[
3
],
(
mac
)[
4
],
(
mac
)[
5
]);
uint8
retval
=
wifi_sta_sethostname
(
temp
,
strlen
(
temp
));
if
(
retval
!=
1
)
{
c_sprintf
(
temp
,
"NODE-%X%X%X"
,
(
mac
)[
3
],
(
mac
)[
4
],
(
mac
)[
5
]);
wifi_sta_sethostname
((
const
char
*
)
temp
,
strlen
(
temp
));
}
#endif
return
0
;
}
NODEMCU_MODULE
(
WIFI
,
"wifi"
,
wifi_map
,
luaopen_wifi
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment