From 12423649e1698af298b4caecb14f2a2245a43ba6 Mon Sep 17 00:00:00 2001 From: Wvader <34067397+wvader@users.noreply.github.com> Date: Wed, 2 Nov 2022 15:25:09 +0000 Subject: [PATCH] Initial commit --- .gitignore | 80 ++++++++++++++++++++ CMakeLists.txt | 12 +++ main.cpp | 197 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed52359 --- /dev/null +++ b/.gitignore @@ -0,0 +1,80 @@ + +.idea/ +*.spv + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake + +cmake-build-debug +cmake-build-release +# Xcode +.DS_Store +*/build/* +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 +xcuserdata +profile +*.moved-aside +DerivedData +.idea/ +*.hmap +*.xccheckout + +#CocoaPods +Pods + +*.xcodeproj + +/xproj.screenflow +/xproj.mp4 +/xproj-narc.psd +/xproj-narc.gif +/xproj-arc.psd +/xproj-arc.gif +build/* +cmake-build-* +RoastEngine.build +Debug/ +deps/glad +.version.toml \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..664664f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.21) +project(TestAugeas) +include_directories(/opt/homebrew/opt/augeas/include/) # Headers para auto-complete? +link_directories(/opt/homebrew/opt/augeas/lib) +# Os binarios com as libraries, como é mac .dylib, linux: .SO, etc +# tive que usar HOMEBREW_NO_INSTALL_CLEANUP=1 "brew install augeas --build-from-source" +# para ele armazenar o codigo compilado. + +set(CMAKE_CXX_STANDARD 14) + +add_executable(TestAugeas main.cpp) +target_link_libraries(TestAugeas augeas) # Se não ele não sabe que é para compilar para ARM64 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..50756fe --- /dev/null +++ b/main.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +// set wifi client data; DHCP, IP, ... +int setWifiParameter(int DHCP, std::string sIP, std::string sSubnet, std::string sRouter){ + std::stringstream buffer; + buffer << "/etc/network/interfaces" << std::flush; // +++ IMPORTANT: no SLASH at the end of path, otherwise it will not work but there won't be an error message + std::string sPathExec = buffer.str(); + std::cout << "Execution path is: " << sPathExec << std::endl; + + augeas *myAug = NULL; + std::string sPath; + int ret = 0; + int iMatches = 0; + unsigned int flags = AUG_NO_MODL_AUTOLOAD; // AUG_NO_MODL_AUTOLOAD: really do not load // AUG_NO_LOAD: do not load/scan files // 0 + const char *root = NULL; //sRoot.c_str(); + const char *path = NULL; + char **matches = NULL; + const char *value = NULL; + char **pathsErrors = NULL; + + // ================== + // Initialization of augeas + // ================== + + std::cout << std::endl << "Here we start the initialization." << std::endl; + + // Terminal: augtool --noautoload + // The flag is set to --noautoload in order to speed up the initialization. + // If the flag is not set, the standard config files are loaded which takes a while. + // The commands after aug_init() then set the specific files which should be loaded. + myAug = aug_init(root, "/usr/share/augeas/lenses", flags); + + // Set lens which should be loaded + // Terminal: augtool> set /augeas/load/Interfaces/lens "Interfaces.lns" + ret = aug_set(myAug, "/augeas/load/Interfaces/lens", "Interfaces.lns"); + std::cout << "Set lens: " << ret << std::endl; + + // Set incl(ude) path which should be parsed + // Terminal: augtool> set /augeas/load/Interfaces/incl "/etc/network/interfaces" + path = sPathExec.c_str(); + ret = aug_set(myAug, "/augeas/load/Interfaces/incl", path); + std::cout << "Set incl: " << ret << std::endl; + + // Load/parse config files + // Terminal: augtool> load + ret = aug_load(myAug); + std::cout << "Load augeas: " << ret << std::endl; + + // Reading and showing error messages + iMatches = aug_match(myAug, "/augeas//error/*", &pathsErrors); + std::cout << "Number of errors: " << std::to_string(iMatches) << std::endl; + for (int j = 0; j < iMatches; j++) { + ret = aug_get(myAug, pathsErrors[j], &value); + std::cout << pathsErrors[j] << std::endl; + if (value!=NULL){ + std::cout << value << std::endl; + }else{ + std::cout << "Null" << std::endl; + } + } + + // ================== + // Checking for specific nodes + // ================== + + std::cout << std::endl << "Here we start searching for a specific node." << std::endl; + + // Check for matches + sPath= "/files" + sPathExec + "/iface"; + path = sPath.c_str(); + iMatches = aug_match(myAug, path, &matches); + std::cout << "Number of matches: " << iMatches << std::endl; + + // Check which network interface is the wifi interface (wlan0) + // todo: simplify by using XPath + int iWlanInterface = -1; + + std::cout << "The following paths have been found:" << std::endl; + + for (int i=1; i<=iMatches; i++){ + sPath = "/files" + sPathExec + "/iface[" + std::to_string(i) + "]"; + std::cout << sPath << std::endl; + path = sPath.c_str(); + const char *value; + ret = aug_get(myAug, path, &value); // todo: ret has to be 1, otherwise value is NULL + if (ret==1){ + buffer.str(std::string()); // clear stream "buffer" + buffer << value << std::flush; + if (buffer.str()=="wlan0"){ + iWlanInterface = i; + std::cout << "Interface number of wlan0: " << std::to_string(iWlanInterface) << std::endl; + } + }else{ + std::cout << "no value" << std::endl; + } + } + + // ================== + // Changing value of nodes + // ================== + + std::cout << std::endl << "Here we start changing the value of the node." < set /files/home/pi/AugeasDemoC/network/interfaces/iface[3]/method dhcp + sPath= "/files" + sPathExec + "/iface[" + std::to_string(iWlanInterface) + "]/method"; + path = sPath.c_str(); + ret = aug_set(myAug, path, "dhcp"); + std::cout << "Set value: " << ret << std::endl; + + // Reading version of augeas + ret = aug_get(myAug, "/augeas/version", &value); + std::cout << "Augeas version: " << value << std::endl; + + // Reading values; just for debugging + sPath= "/files" + sPathExec + "/iface[" + std::to_string(iWlanInterface) + "]/method"; + path = sPath.c_str(); + ret = aug_get(myAug, path, &value); + std::cout << "The value read from node after changing: " << value << std::endl; + + // ================== + // Saving changes back to file + // ================== + + std::cout << std::endl << "Here we start saving changes back to files." << std::endl; + + // Set the save mode: overwrite/backup/newfile/noop + ret = aug_set(myAug, "/augeas/save", "overwrite"); + std::cout << "Save mode set: " << ret << std::endl; + + // Reading and showing saved files + // Terminal: augtool> print /augeas/events/saved + char **pathSaved= NULL; + ret = aug_match(myAug, "/augeas/events/saved", &pathSaved); + std::cout << "Number of changed files: " << std::to_string(ret) << std::endl; + for (int j = 0; j < ret; j++) { + aug_get(myAug, pathSaved[j], &value); + std::cout << pathSaved[j] << std::endl; + } + + // Save the changed config files + // Terminal: augtool > save + ret = aug_save(myAug); + std::cout << "File saved: " << ret << std::endl; + + // Reading and showing saved files + // Terminal: augtool> print /augeas/events/saved + ret = aug_match(myAug, "/augeas/events/saved", &pathSaved); + std::cout << "Number of changed files: " << std::to_string(ret) << std::endl; + for (int j = 0; j < ret; j++) { + aug_get(myAug, pathSaved[j], &value); + std::cout << pathSaved[j] << std::endl; + } + + // Reading and showing error messages + iMatches = aug_match(myAug, "/augeas//error/*", &pathsErrors); + std::cout << "Number of errors: " << std::to_string(iMatches) << std::endl; + for (int j = 0; j < iMatches; j++) { + ret = aug_get(myAug, pathsErrors[j], &value); + std::cout << pathsErrors[j] << std::endl; + if (value!=NULL){ + std::cout << value << std::endl; + }else{ + std::cout << "Null" << std::endl; + } + } + + aug_close(myAug); + + return 0; +} + +/** + * This is the main method which creates and sets consumer instance. + * @Looping is essential for this MQTT library to work. + * @Exceptions on connection and subscription error. + */ +int main(int argc, char *argv[]) { + + // write config files related to wifi + int ret = setWifiParameter(1, "192.168.178.123", "255.255.255.0", "192.168.178.1"); + std::cout << "Return: " << ret << std::endl; + + return 0; +} \ No newline at end of file