Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend node.info #2830

Merged
merged 21 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local/
user_config.h
server-ca.crt
luac.cross
luac.cross.int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might belong in a separate PR, but I'm not going to complain too much.

uz_unzip
uz_zip
tools/toolchains/
Expand All @@ -15,3 +16,6 @@ tools/toolchains/
.project
.settings/
.vscode

#ignore temp file for build infos
buildinfo.h
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ endif # TARGET
#

ifndef TARGET
all: toolchain sdk_pruned pre_build .subdirs
all: toolchain sdk_pruned pre_build buildinfo .subdirs
else
all: .subdirs $(OBJS) $(OLIBS) $(OIMAGES) $(OBINS) $(SPECIAL_MKTARGETS)
endif
Expand Down Expand Up @@ -412,6 +412,11 @@ pre_build:
@-rm -f $(APP_DIR)/modules/server-ca.crt.h
endif

.PHONY: buildinfo

buildinfo:
tools/update_buildinfo.sh

ifdef TARGET
$(OBJODIR)/%.o: %.c
@mkdir -p $(dir $@);
Expand Down
5 changes: 4 additions & 1 deletion app/include/user_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __USER_VERSION_H__

#include "version.h" /* ESP firmware header */
#include <buildinfo.h>

#define NODE_VERSION_MAJOR ESP_SDK_VERSION_MAJOR
#define NODE_VERSION_MINOR ESP_SDK_VERSION_MINOR
Expand All @@ -11,7 +12,9 @@
#define NODE_VERSION_STR(x) #x
#define NODE_VERSION_XSTR(x) NODE_VERSION_STR(x)

#define NODE_VERSION "NodeMCU " ESP_SDK_VERSION_STRING "." NODE_VERSION_XSTR(NODE_VERSION_INTERNAL)
# define NODE_VERSION "NodeMCU " ESP_SDK_VERSION_STRING "." NODE_VERSION_XSTR(NODE_VERSION_INTERNAL) " " NODE_VERSION_LONG
marcelstoer marked this conversation as resolved.
Show resolved Hide resolved
// Leave the space after # in the line above. It busts replacement of NODE_VERSION in the docker build which is not needed anymore with this PR.
// Can be removed when the script is adapted

#ifndef BUILD_DATE
#define BUILD_DATE "unspecified"
Expand Down
74 changes: 65 additions & 9 deletions app/modules/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,71 @@ static int node_sleep( lua_State* L )
#endif //PMSLEEP_ENABLE
static int node_info( lua_State* L )
{
lua_pushinteger(L, NODE_VERSION_MAJOR);
lua_pushinteger(L, NODE_VERSION_MINOR);
lua_pushinteger(L, NODE_VERSION_REVISION);
lua_pushinteger(L, system_get_chip_id()); // chip id
lua_pushinteger(L, spi_flash_get_id()); // flash id
lua_pushinteger(L, flash_rom_get_size_byte() / 1024); // flash size in KB
lua_pushinteger(L, flash_rom_get_mode());
lua_pushinteger(L, flash_rom_get_speed());
return 8;
const char* options[] = {"hw", "sw_version", "build_config", "legacy", NULL};
int option = luaL_checkoption (L, 1, options[3], options);

switch (option) {
case 0: { // hw
lua_createtable (L, 0, 5);
int table_index = lua_gettop(L);
lua_pushinteger(L, system_get_chip_id()); // chip id
lua_setfield(L, table_index, "chip_id");
lua_pushinteger(L, spi_flash_get_id()); // flash id
lua_setfield(L, table_index, "flash_id");
lua_pushinteger(L, flash_rom_get_size_byte() / 1024); // flash size in KB
lua_setfield(L, table_index, "flash_size");
lua_pushinteger(L, flash_rom_get_mode());
lua_setfield(L, table_index, "flash_mode");
lua_pushinteger(L, flash_rom_get_speed());
lua_setfield(L, table_index, "flash_speed");
return 1;
}
case 1: { // sw_version
lua_createtable (L, 0, 7);
int table_index = lua_gettop(L);
lua_pushinteger(L, NODE_VERSION_MAJOR);
lua_setfield(L, table_index, "node_version_major");
lua_pushinteger(L, NODE_VERSION_MINOR);
lua_setfield(L, table_index, "node_version_minor");
lua_pushinteger(L, NODE_VERSION_REVISION);
lua_setfield(L, table_index, "node_version_revision");
lua_pushstring(L, BUILDINFO_BRANCH);
lua_setfield(L, table_index, "git_branch");
lua_pushstring(L, BUILDINFO_COMMIT_ID);
lua_setfield(L, table_index, "git_commit_id");
lua_pushstring(L, BUILDINFO_RELEASE);
lua_setfield(L, table_index, "git_release");
lua_pushstring(L, BUILDINFO_RELEASE_DTS);
lua_setfield(L, table_index, "git_commit_dts");
return 1;
}
case 2: { // build_config
lua_createtable (L, 0, 4);
int table_index = lua_gettop(L);
lua_pushboolean(L, BUILDINFO_SSL);
lua_setfield(L, table_index, "ssl");
lua_pushnumber(L, BUILDINFO_LFS);
lua_setfield(L, table_index, "lfs_size");
lua_pushstring(L, BUILDINFO_MODULES);
lua_setfield(L, table_index, "modules");
lua_pushstring(L, BUILDINFO_BUILD_TYPE);
lua_setfield(L, table_index, "number_type");
return 1;
}
default:
{
platform_print_deprecation_note("node.info() without parameter", "in the next version");
lua_pushinteger(L, NODE_VERSION_MAJOR);
lua_pushinteger(L, NODE_VERSION_MINOR);
lua_pushinteger(L, NODE_VERSION_REVISION);
lua_pushinteger(L, system_get_chip_id()); // chip id
lua_pushinteger(L, spi_flash_get_id()); // flash id
lua_pushinteger(L, flash_rom_get_size_byte() / 1024); // flash size in KB
lua_pushinteger(L, flash_rom_get_mode());
lua_pushinteger(L, flash_rom_get_speed());
return 8;
}
}
}

// Lua: chipid()
Expand Down
2 changes: 1 addition & 1 deletion app/user/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ extern void _ResetHandler(void);
* the use of a partition table (PT) to control flash allocation. The NodeMCU uses
* this PT for overall allocation of its flash resources. The non_OS SDK calls the
* user_pre_init() entry to do all of this startup configuration. Note that this
* runs with Icache enabled -- that is the IROM0 partition is already mapped the
* runs with Icache enabled -- that is the IROM0 partition is already mapped to the
* address space at 0x40210000 and so that most SDK services are available, such
* as system_get_flash_size_map() which returns the valid flash size (including the
* 8Mb and 16Mb variants).
Expand Down
8 changes: 5 additions & 3 deletions docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ make EXTRA_CCFLAGS="-DLUA_NUMBER_INTEGRAL ....
```

### Tag Your Build
Identify your firmware builds by editing `app/include/user_version.h`
Identify your firmware builds by setting the environment variable `USER_PROLOG`.
You may also edit `app/include/user_version.h`. The variable `USER_PROLOG` will be included in `NODE_VERSION_LONG`.

```c
#define NODE_VERSION "NodeMCU " ESP_SDK_VERSION_STRING "." NODE_VERSION_XSTR(NODE_VERSION_INTERNAL)
#define NODE_VERSION "NodeMCU " ESP_SDK_VERSION_STRING "." NODE_VERSION_XSTR(NODE_VERSION_INTERNAL) " " NODE_VERSION_LONG

#ifndef BUILD_DATE
#define BUILD_DATE "YYYYMMDD"
#define BUILD_DATE "unspecified"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I only now realized that update_buildinfo.sh doesn't set the build date. Did you omit this on purpose? Wouldn't it be useful to have BUILD_DATE="$(date "+%Y-%m-%d %H:%M")" in there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HHHartmann However, we'd have to invert that order of precedence.

If we do

BUILD_DATE="$(date "+%Y-%m-%d %H:%M")"
#define BUILD_DATE "$BUILD_DATE"

in the script the #ifndef BUILD_DATE would guarantee that any date the user sets explicitly is overwritten. It should be the other way around.

#endif
```

Expand Down
72 changes: 59 additions & 13 deletions docs/modules/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,17 @@ Get the current LFS and SPIFFS partition information.
none

#### Returns
An array containing entries for `lfs_addr`, `lfs_size`, `spiffs_addr` and `spiffs_size`. The address values are offsets relative to the startof the Flash memory.
An array containing entries for `lfs_addr`, `lfs_size`, `spiffs_addr` and `spiffs_size`. The address values are offsets relative to the start of the Flash memory.

#### Example
```lua
print("The LFS size is " .. node.getpartitiontable().lfs_size)
```

#### See also
[`node.setpartitiontable()`](#nodesetpartitiontable)


## node.heap()

Returns the current available heap size in bytes. Note that due to fragmentation, actual allocations of this size may not be possible.
Expand All @@ -272,30 +276,68 @@ system heap size left in bytes (number)

## node.info()

Returns NodeMCU version, chipid, flashid, flash size, flash mode, flash speed, and Lua File Store (LFS) usage statics.
Returns information about hardware, software version and build configuration.


#### Syntax
`node.info()`
`node.info([group])`

#### Parameters
none
`group` group of information (optional, if ommited return legacy information). May be one of `"hw"`, `"sw_version"`, `"build_config"`.

#### Returns
- `majorVer` (number)
- `minorVer` (number)
- `devVer` (number)
- `chipid` (number)
- `flashid` (number)
- `flashsize` (number)
- `flashmode` (number)
- `flashspeed` (number)
if a `group` is given the return value will be a table containing the following elements:
- for `group` = `"hw"`
- `chip_id` (number)
- `flash_id` (number)
- `flash_size` (number)
- `flash_mode` (number) QIO = 0, QOUT = 1, DIO = 2, DOUT = 15.
- `flash_speed` (number)
- for `group` = `"sw_version"`
- `git_branch` (string)
- `git_commit_id` (string)
- `git_release` (string) Release name +additional commits e.g. "2.0.0-master_20170202 +403"
- `git_commit_dts` (string) in an ordering format. e.g. "201908111200"
- `node_verion_major` (number)
- `node_verion_minor` (number)
- `node_verion_revision` (number)
- for `group` = `"build_config"`
- `ssl` (boolean)
- `lfs_size` (number) as defined at build time
- `modules` (string) comma separated list
- `number_type` (string) `integer` or `float`

!!! attention

This interface is deprecated and will be removed in one of the next releases. Use the above calls instead.

- for no `group` given: --deprecated
- `majorVer` (number)
- `minorVer` (number)
- `devVer` (number)
- `chipid` (number)
- `flashid` (number)
- `flashsize` (number)
- `flashmode` (number)
- `flashspeed` (number)

#### Example
```lua
majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info()
print("NodeMCU "..majorVer.."."..minorVer.."."..devVer)
```

```lua
for k,v in pairs(node.info("build_config")) do
print (k,v)
end
```

```lua
print(node.info("sw_version").git_release)
```


## node.input()

Submits a string to the Lua interpreter. Similar to `pcall(loadstring(str))`, but without the single-line limitation.
Expand Down Expand Up @@ -436,7 +478,7 @@ Sets the current LFS and / or SPIFFS partition information.
This function is typically only used once during initial provisioning after first flashing the firmware. It does some consistency checks to validate the specified parameters, and it then reboots the ESP module to load the new partition table. If the LFS or SPIFFS regions have changed then you will need to reload LFS, reformat the SPIFSS and reload its contents.

#### Parameters
An array containing one or more of the following enties. The address values are byte offsets relative to the startof the Flash memory. The size values are in bytes. Note that these parameters must be a multiple of 8Kb to align to Flash page boundaries.
An array containing one or more of the following enties. The address values are byte offsets relative to the start of the Flash memory. The size values are in bytes. Note that these parameters must be a multiple of 8Kb to align to Flash page boundaries.
- `lfs_addr`. The base address of the LFS region.
- `lfs_size`. The size of the LFS region.
- `spiffs_addr`. The base address of the SPIFFS region.
Expand All @@ -450,6 +492,10 @@ Not applicable. The ESP module will be rebooted for a valid new set, or a Lua e
node.setpartitiontable{lfs_size = 0x20000, spiffs_addr = 0x120000, spiffs_size = 0x20000}
```

#### See also
[`node.getpartitiontable()`](#nodegetpartitiontable)



## node.sleep()

Expand Down
66 changes: 66 additions & 0 deletions tools/update_buildinfo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash
HHHartmann marked this conversation as resolved.
Show resolved Hide resolved

USER_MODULES_H=app/include/user_modules.h

COMMIT_ID="$(git rev-parse HEAD)"
BRANCH="$(git rev-parse --abbrev-ref HEAD | sed -r 's/[\/\\]+/_/g')"
HHHartmann marked this conversation as resolved.
Show resolved Hide resolved
RELEASE="$(git describe --tags --long | sed -r 's/(.*)-(.*)-.*/\1 +\2/g' | sed 's/ +0$//')"
RELEASE_DTS=$(TZ=UTC git show --quiet --date=format-local:"%Y%m%d%H%M" --format="%cd" HEAD)

MODULES=$(awk '/^[ \t]*#define LUA_USE_MODULES/{modules=modules sep tolower(substr($2,17));sep=","}END{if(length(modules)==0)modules="-";print modules}' $USER_MODULES_H | tr -d '\r')

# create temp buildinfo
TEMPFILE=/tmp/buildinfo.h
cat > $TEMPFILE << EndOfMessage
#ifndef __BUILDINFO_H__
#define __BUILDINFO_H__

#include "user_config.h"

#define BUILDINFO_STR_HELPER(x) #x
#define BUILDINFO_TO_STR(x) BUILDINFO_STR_HELPER(x)

#ifdef LUA_FLASH_STORE
#define BUILDINFO_LFS LUA_FLASH_STORE
#else
#define BUILDINFO_LFS 0
#endif

#ifdef CLIENT_SSL_ENABLE
#define BUILDINFO_SSL true
#define BUILDINFO_SSL_STR "true"
#else
#define BUILDINFO_SSL false
#define BUILDINFO_SSL_STR "false"
#endif

#ifdef LUA_NUMBER_INTEGRAL
#define BUILDINFO_BUILD_TYPE "integer"
#else
#define BUILDINFO_BUILD_TYPE "float"
#endif

#define USER_PROLOG "$USER_PROLOG"
#define BUILDINFO_BRANCH "$BRANCH"
#define BUILDINFO_COMMIT_ID "$COMMIT_ID"
#define BUILDINFO_RELEASE "$RELEASE"
#define BUILDINFO_RELEASE_DTS "$RELEASE_DTS"
#define BUILDINFO_MODULES "$MODULES"

#define NODE_VERSION_LONG \\
USER_PROLOG "\n" \\
"\tbranch: " BUILDINFO_BRANCH "\n" \\
"\tcommit: " BUILDINFO_COMMIT_ID "\n" \\
"\trelease: " BUILDINFO_RELEASE "\n" \\
"\trelease DTS: " BUILDINFO_RELEASE_DTS "\n" \\
"\tSSL: " BUILDINFO_SSL_STR "\n" \\
"\tBuild type: " BUILDINFO_BUILD_TYPE "\n" \\
HHHartmann marked this conversation as resolved.
Show resolved Hide resolved
"\tLFS: " BUILDINFO_TO_STR(BUILDINFO_LFS) "\n" \\
"\tmodules: " BUILDINFO_MODULES "\n"

EndOfMessage

echo "#endif /* __BUILDINFO_H__ */" >> $TEMPFILE

diff -q $TEMPFILE app/include/buildinfo.h || cp $TEMPFILE app/include/buildinfo.h
rm $TEMPFILE