Skip to content

Commit

Permalink
Event function ported to Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
CurlyMoo committed Sep 10, 2017
1 parent 4bb13ed commit ad73974
Show file tree
Hide file tree
Showing 25 changed files with 1,497 additions and 1,285 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ script:

after_success:
- gem install coveralls-lcov
- lcov -c -d CMakeFiles/pilight.dir/libs/pilight/hardware/ -d CMakeFiles/pilight.dir/libs/pilight/events/ -d CMakeFiles/pilight.dir/libs/pilight/core/ -d CMakeFiles/pilight.dir/libs/pilight/psutil/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/API/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/core/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/generic/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/network/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/GPIO -o coverage.info
- lcov -c -d CMakeFiles/pilight.dir/libs/pilight/lua -d CMakeFiles/pilight.dir/libs/pilight/hardware/ -d CMakeFiles/pilight.dir/libs/pilight/events/ -d CMakeFiles/pilight.dir/libs/pilight/core/ -d CMakeFiles/pilight.dir/libs/pilight/psutil/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/API/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/core/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/generic/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/network/ -d CMakeFiles/pilight.dir/libs/pilight/protocols/GPIO -o coverage.info
- lcov -r coverage.info '*433nano.c' '*IRgpio.c' '*none.c' '*zwave.cpp' '*adhoc.c' '*firmware.c' '*relay.c' '*pushover.c' '*pushbullet.c' '*dht11.c' '*dht22.c' -o coverage-filtered.info
- lcov --list coverage-filtered.info
- if [[ "$TRAVIS_EVENT_TYPE" != "pull_request" ]]; then coveralls-lcov --repo-token ${COVERALLS_TOKEN} coverage-filtered.info; fi
212 changes: 113 additions & 99 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ if(NOT WIN32)
set(CMAKE_EXE_LINKER_FLAGS " -Wl,-rpath=.,-rpath=/usr/local/lib/,-rpath=/usr/lib/,-rpath=/lib/")
set(CMAKE_SHARED_LINKER_FLAGS " -Wl,-rpath=.,-rpath=/usr/local/lib/,-rpath=/usr/lib/,-rpath=/lib/")
set(CMAKE_MODULE_LINKER_FLAGS " -Wl,-rpath=.,-rpath=/usr/local/lib/,-rpath=/usr/lib/,-rpath=/lib/")
else()
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc")
endif()


if(NOT WIN32)
# Start uninstaller generator
function(WRITE_UNINSTALL_TARGET_SCRIPT)
Expand Down Expand Up @@ -206,108 +209,108 @@ if(NOT WIN32)
else()
message(STATUS "Looking for libwiringx - found (${CMAKE_WIRINGX_LIBS_INIT})")
endif()
endif()

find_library(CMAKE_MBEDTLS_LIBS_INIT
NAME mbedtls
PATHS
${CROSS_COMPILE_LIBS}
/usr/lib
/usr/lib32
/usr/lib64
/usr/lib/i386-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/local/lib
/usr/local/lib32
/usr/local/lib64
/usr/lib/arm-linux-gnueabi
/usr/lib/arm-linux-gnueabihf
/usr/lib/aarch64-linux-gnu
NO_DEFAULT_PATH)

if(${CMAKE_MBEDTLS_LIBS_INIT} MATCHES "CMAKE_MBEDTLS_LIBS_INIT-NOTFOUND")
message(FATAL_ERROR "Looking for libmbedtls - not found")
else()
message(STATUS "Looking for libmbedtls - found (${CMAKE_MBEDTLS_LIBS_INIT})")
endif()

find_library(CMAKE_MBEDCRYPTO_LIBS_INIT
NAME mbedcrypto
PATHS
${CROSS_COMPILE_LIBS}
/usr/lib
/usr/lib32
/usr/lib64
/usr/lib/i386-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/local/lib
/usr/local/lib32
/usr/local/lib64
/usr/lib/arm-linux-gnueabi
/usr/lib/arm-linux-gnueabihf
/usr/lib/aarch64-linux-gnu
NO_DEFAULT_PATH)

if(${CMAKE_MBEDCRYPTO_LIBS_INIT} MATCHES "CMAKE_MBEDCRYPTO_LIBS_INIT-NOTFOUND")
message(FATAL_ERROR "Looking for libmbedcrypto - not found")
else()
message(STATUS "Looking for libmbedcrypto - found (${CMAKE_MBEDCRYPTO_LIBS_INIT})")
endif()

find_library(CMAKE_MBEDX509_LIBS_INIT
NAME mbedx509
PATHS
${CROSS_COMPILE_LIBS}
/usr/lib
/usr/lib32
/usr/lib64
/usr/lib/i386-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/local/lib
/usr/local/lib32
/usr/local/lib64
/usr/lib/arm-linux-gnueabi
/usr/lib/arm-linux-gnueabihf
/usr/lib/aarch64-linux-gnu
NO_DEFAULT_PATH)
find_library(CMAKE_MBEDTLS_LIBS_INIT
NAME mbedtls
PATHS
${CROSS_COMPILE_LIBS}
/usr/lib
/usr/lib32
/usr/lib64
/usr/lib/i386-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/local/lib
/usr/local/lib32
/usr/local/lib64
/usr/lib/arm-linux-gnueabi
/usr/lib/arm-linux-gnueabihf
/usr/lib/aarch64-linux-gnu
NO_DEFAULT_PATH)

if(${CMAKE_MBEDTLS_LIBS_INIT} MATCHES "CMAKE_MBEDTLS_LIBS_INIT-NOTFOUND")
message(FATAL_ERROR "Looking for libmbedtls - not found")
else()
message(STATUS "Looking for libmbedtls - found (${CMAKE_MBEDTLS_LIBS_INIT})")
endif()

if(${CMAKE_MBEDX509_LIBS_INIT} MATCHES "CMAKE_MBEDX509_LIBS_INIT-NOTFOUND")
message(FATAL_ERROR "Looking for libmbedx509 - not found")
else()
message(STATUS "Looking for libmbedx509 - found (${CMAKE_MBEDX509_LIBS_INIT})")
endif()
find_library(CMAKE_MBEDCRYPTO_LIBS_INIT
NAME mbedcrypto
PATHS
${CROSS_COMPILE_LIBS}
/usr/lib
/usr/lib32
/usr/lib64
/usr/lib/i386-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/local/lib
/usr/local/lib32
/usr/local/lib64
/usr/lib/arm-linux-gnueabi
/usr/lib/arm-linux-gnueabihf
/usr/lib/aarch64-linux-gnu
NO_DEFAULT_PATH)

if(${CMAKE_MBEDCRYPTO_LIBS_INIT} MATCHES "CMAKE_MBEDCRYPTO_LIBS_INIT-NOTFOUND")
message(FATAL_ERROR "Looking for libmbedcrypto - not found")
else()
message(STATUS "Looking for libmbedcrypto - found (${CMAKE_MBEDCRYPTO_LIBS_INIT})")
endif()

find_library(CMAKE_LUA_LIBS_INIT
NAME lua lua5 lua5.1 lua5.2 lua5.3
PATHS
${CROSS_COMPILE_LIBS}
/usr/lib
/usr/lib32
/usr/lib64
/usr/lib/i386-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/local/lib
/usr/local/lib32
/usr/local/lib64
/usr/lib/arm-linux-gnueabi
/usr/lib/arm-linux-gnueabihf
/usr/lib/aarch64-linux-gnu
NO_DEFAULT_PATH)
find_library(CMAKE_MBEDX509_LIBS_INIT
NAME mbedx509
PATHS
${CROSS_COMPILE_LIBS}
/usr/lib
/usr/lib32
/usr/lib64
/usr/lib/i386-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/local/lib
/usr/local/lib32
/usr/local/lib64
/usr/lib/arm-linux-gnueabi
/usr/lib/arm-linux-gnueabihf
/usr/lib/aarch64-linux-gnu
NO_DEFAULT_PATH)

if(${CMAKE_MBEDX509_LIBS_INIT} MATCHES "CMAKE_MBEDX509_LIBS_INIT-NOTFOUND")
message(FATAL_ERROR "Looking for libmbedx509 - not found")
else()
message(STATUS "Looking for libmbedx509 - found (${CMAKE_MBEDX509_LIBS_INIT})")
endif()

if(${CMAKE_LUA_LIBS_INIT} MATCHES "CMAKE_LUA_LIBS_INIT-NOTFOUND")
message(FATAL_ERROR "Looking for liblua - not found")
else()
if(${CMAKE_LUA_LIBS_INIT} MATCHES ".*5.1.*")
set(CMAKE_LUA_LIBS_VERSION "5.1")
elseif(${CMAKE_LUA_LIBS_INIT} MATCHES ".*5.2.*")
set(CMAKE_LUA_LIBS_VERSION "5.2")
elseif(${CMAKE_LUA_LIBS_INIT} MATCHES ".*5.3.*")
set(CMAKE_LUA_LIBS_VERSION "5.3")
elseif(${CMAKE_LUA_LIBS_INIT} MATCHES ".*5.*")
set(CMAKE_LUA_LIBS_VERSION "5")
endif()
find_path(CMAKE_LUA_INCLUDE_DIR "lua${CMAKE_LUA_LIBS_VERSION}/lua.h")
message(STATUS "Looking for liblua - found (${CMAKE_LUA_LIBS_INIT})")
endif()
find_library(CMAKE_LUA_LIBS_INIT
NAME lua lua5 lua5.1 lua5.2 lua5.3
PATHS
${CROSS_COMPILE_LIBS}
/usr/lib
/usr/lib32
/usr/lib64
/usr/lib/i386-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/local/lib
/usr/local/lib32
/usr/local/lib64
/usr/lib/arm-linux-gnueabi
/usr/lib/arm-linux-gnueabihf
/usr/lib/aarch64-linux-gnu
NO_DEFAULT_PATH)

if(${CMAKE_LUA_LIBS_INIT} MATCHES "CMAKE_LUA_LIBS_INIT-NOTFOUND")
message(FATAL_ERROR "Looking for liblua - not found")
else()
if(${CMAKE_LUA_LIBS_INIT} MATCHES ".*5.1.*")
set(CMAKE_LUA_LIBS_VERSION "5.1")
elseif(${CMAKE_LUA_LIBS_INIT} MATCHES ".*5.2.*")
set(CMAKE_LUA_LIBS_VERSION "5.2")
elseif(${CMAKE_LUA_LIBS_INIT} MATCHES ".*5.3.*")
set(CMAKE_LUA_LIBS_VERSION "5.3")
elseif(${CMAKE_LUA_LIBS_INIT} MATCHES ".*5.*")
set(CMAKE_LUA_LIBS_VERSION "5")
endif()
find_path(CMAKE_LUA_INCLUDE_DIR "lua${CMAKE_LUA_LIBS_VERSION}/lua.h")
message(STATUS "Looking for liblua - found (${CMAKE_LUA_LIBS_INIT})")
endif()

include_directories(${PROJECT_SOURCE_DIR}/inc)
Expand Down Expand Up @@ -364,7 +367,11 @@ if(${MODULESPACK} MATCHES "OFF" OR WIN32)
endif()

add_library(${PROJECT_NAME}_static STATIC $<TARGET_OBJECTS:${PROJECT_NAME}>)
add_library(${PROJECT_NAME}_shared SHARED $<TARGET_OBJECTS:${PROJECT_NAME}>)
if(WIN32)
add_library(${PROJECT_NAME}_shared STATIC $<TARGET_OBJECTS:${PROJECT_NAME}>)
else()
add_library(${PROJECT_NAME}_shared SHARED $<TARGET_OBJECTS:${PROJECT_NAME}>)
endif()

if(${PILIGHT_UNITTEST})
add_library(dso_shared SHARED $<TARGET_OBJECTS:DSO>)
Expand All @@ -386,11 +393,15 @@ if(${MODULESPACK} MATCHES "OFF" OR WIN32)
endif()
if(WIN32)
target_link_libraries(${PROJECT_NAME}_shared ws2_32)
target_link_libraries(${PROJECT_NAME}_shared user32)
target_link_libraries(${PROJECT_NAME}_shared userenv)
target_link_libraries(${PROJECT_NAME}_shared wsock32)
target_link_libraries(${PROJECT_NAME}_shared iphlpapi)
target_link_libraries(${PROJECT_NAME}_shared psapi)
target_link_libraries(${PROJECT_NAME}_shared userenv)
target_link_libraries(${PROJECT_NAME}_static ws2_32)
target_link_libraries(${PROJECT_NAME}_static user32)
target_link_libraries(${PROJECT_NAME}_static userenv)
target_link_libraries(${PROJECT_NAME}_static wsock32)
target_link_libraries(${PROJECT_NAME}_static iphlpapi)
target_link_libraries(${PROJECT_NAME}_static psapi)
Expand Down Expand Up @@ -493,6 +504,9 @@ if(${MODULESPACK} MATCHES "OFF" OR WIN32)
if(EXISTS "/usr/local/${PROJECT_NAME}/operators")
install(CODE "execute_process(COMMAND find /usr/local/${PROJECT_NAME}/operators/ -type f -exec rm {} \\;)")
endif()
if(EXISTS "/usr/local/${PROJECT_NAME}/functions")
install(CODE "execute_process(COMMAND find /usr/local/${PROJECT_NAME}/functions/ -type f -exec rm {} \\;)")
endif()

if(EXISTS "/etc/${PROJECT_NAME}/config.json")
install(CODE "execute_process(COMMAND mv /etc/${PROJECT_NAME}/config.json /etc/${PROJECT_NAME}/config.json.bak)")
Expand Down
1 change: 0 additions & 1 deletion libs/pilight/events/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ file(GLOB ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/*.h")

set(${PROJECT_NAME}_files
${pilight_event_actions_files}
${pilight_event_functions_files}
${${PROJECT_NAME}_sources}
${${PROJECT_NAME}_headers}
CACHE INTERNAL "")
63 changes: 31 additions & 32 deletions libs/pilight/events/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,7 @@ static int event_remove_hooks(char **rule, struct rules_t *obj, int depth) {
}

static int event_parse_function(char **rule, struct rules_t *obj, unsigned short validate, enum origin_t origin) {
struct JsonNode *arguments = NULL;
struct event_functions_t *tmp_function = event_functions;
struct event_function_args_t *args = NULL;
struct varcont_t v;
char *tmp = *rule, *ca = NULL;
char *ohook = strstr(tmp, "("), *chook = strstr(tmp, ")");
Expand Down Expand Up @@ -583,7 +582,6 @@ static int event_parse_function(char **rule, struct rules_t *obj, unsigned short
strncpy(name, &tmp[pos1], nl);
name[nl] = '\0';

arguments = json_mkarray();
pos4 = pos3+1;
for(i=pos3+1;i<(pos3+(fl-nl));i++) {
if(tmp[i] == '\'') {
Expand All @@ -605,13 +603,19 @@ static int event_parse_function(char **rule, struct rules_t *obj, unsigned short
ret = event_lookup_variable(&tmp[pos4+had_quote], obj, &v, validate, origin);
if(ret == 0) {
if(v.type_ == JSON_STRING) {
json_append_element(arguments, json_mkstring(v.string_));
args = event_function_add_argument(&v, args);
}
if(v.type_ == JSON_NUMBER) {
json_append_element(arguments, json_mknumber(v.number_, v.decimals_));
args = event_function_add_argument(&v, args);
}
} else {
json_append_element(arguments, json_mkstring(&tmp[pos4+had_quote]));
memset(&v, 0, sizeof(struct varcont_t));
if((v.string_ = STRDUP(&tmp[pos4+had_quote])) == NULL) {
OUT_OF_MEMORY
}
v.type_ = JSON_STRING;
args = event_function_add_argument(&v, args);
FREE(v.string_);
}
if(had_quote == 1) {
tmp[i-had_quote] = '\'';
Expand Down Expand Up @@ -659,6 +663,7 @@ static int event_parse_function(char **rule, struct rules_t *obj, unsigned short
}
}
if(has_quote[0] == 1 || has_quote[1] == 1) {
event_function_free_argument(args);
logprintf(LOG_ERR, "rule #%d invalid: unterminated quote", obj->nr, name);
return -1;
}
Expand All @@ -669,45 +674,40 @@ static int event_parse_function(char **rule, struct rules_t *obj, unsigned short
ret = event_lookup_variable(&tmp[pos4], obj, &v, validate, origin);
if(ret == 0) {
if(v.type_ == JSON_STRING) {
json_append_element(arguments, json_mkstring(v.string_));
args = event_function_add_argument(&v, args);
}
if(v.type_ == JSON_NUMBER) {
json_append_element(arguments, json_mknumber(v.number_, v.decimals_));
args = event_function_add_argument(&v, args);
}
} else {
json_append_element(arguments, json_mkstring(&tmp[pos4]));
memset(&v, 0, sizeof(struct varcont_t));
if((v.string_ = STRDUP(&tmp[pos4])) == NULL) {
OUT_OF_MEMORY
}
v.type_ = JSON_STRING;
args = event_function_add_argument(&v, args);
FREE(v.string_);
}
} else {
json_append_element(arguments, json_mkstring(&tmp[pos4]));
memset(&v, 0, sizeof(struct varcont_t));
if((v.string_ = STRDUP(&tmp[pos4])) == NULL) {
OUT_OF_MEMORY
}
v.type_ = JSON_STRING;
args = event_function_add_argument(&v, args);
FREE(v.string_);
}
tmp[i-1] = t;

if((output = MALLOC(BUFFER_SIZE)) == NULL) {
OUT_OF_MEMORY /*LCOV_EXCL_LINE*/
}
memset(output, '\0', BUFFER_SIZE);

int match = 0;
while(tmp_function) {
if(error == 0) {
if(strcmp(name, tmp_function->name) == 0) {
if(tmp_function->run != NULL) {
error = tmp_function->run(obj, arguments, &output, origin);
match = 1;
break;
}
}
} else {
goto close;
}
tmp_function = tmp_function->next;
}
if(match == 0) {
if(event_function_exists(name) != 0) {
event_function_free_argument(args);
logprintf(LOG_ERR, "rule #%d invalid: function \"%s\" does not exist", obj->nr, name);
error = -1;
goto close;
}

event_function_callback(name, args, &output);

if(strlen(output) > 0) {
if(pilight.debuglevel >= 2) {
fprintf(stderr, "replace %s with %s in %s\n", function, output, *rule);
Expand All @@ -716,7 +716,6 @@ static int event_parse_function(char **rule, struct rules_t *obj, unsigned short
}

close:
json_delete(arguments);
FREE(output);
FREE(name);
FREE(function);
Expand Down

0 comments on commit ad73974

Please sign in to comment.