From f719007bf2e6af91858c2b42d9c1fc46d024a7bb Mon Sep 17 00:00:00 2001 From: Nikita Poltorapavlo Date: Wed, 2 Dec 2020 15:17:32 +0200 Subject: [PATCH 1/2] RDK-30538 : Create UsbAccess plugin Reason for change: Create UsbAccess plugin Test Procedure: See doc Risks: Low Signed-off-by: Nikita Poltorapavlo --- CMakeLists.txt | 4 + UsbAccess/CMakeLists.txt | 24 ++++++ UsbAccess/Module.cpp | 22 +++++ UsbAccess/Module.h | 29 +++++++ UsbAccess/README.md | 27 ++++++ UsbAccess/UsbAccess.config | 3 + UsbAccess/UsbAccess.cpp | 168 +++++++++++++++++++++++++++++++++++++ UsbAccess/UsbAccess.h | 49 +++++++++++ 8 files changed, 326 insertions(+) create mode 100644 UsbAccess/CMakeLists.txt create mode 100644 UsbAccess/Module.cpp create mode 100644 UsbAccess/Module.h create mode 100644 UsbAccess/README.md create mode 100644 UsbAccess/UsbAccess.config create mode 100644 UsbAccess/UsbAccess.cpp create mode 100644 UsbAccess/UsbAccess.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e8077a585..b7f9242122 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -287,6 +287,10 @@ if(PLUGIN_PERSISTENT_STORE) add_subdirectory(PersistentStore) endif() +if(PLUGIN_USBACCESS) + add_subdirectory(UsbAccess) +endif() + if(WPEFRAMEWORK_CREATE_IPKG_TARGETS) set(CPACK_GENERATOR "DEB") set(CPACK_DEB_COMPONENT_INSTALL ON) diff --git a/UsbAccess/CMakeLists.txt b/UsbAccess/CMakeLists.txt new file mode 100644 index 0000000000..a7ab7c1683 --- /dev/null +++ b/UsbAccess/CMakeLists.txt @@ -0,0 +1,24 @@ +set(PLUGIN_NAME UsbAccess) +set(MODULE_NAME ${NAMESPACE}${PLUGIN_NAME}) + +find_package(${NAMESPACE}Plugins REQUIRED) + +find_package(PkgConfig) + +add_library(${MODULE_NAME} SHARED + UsbAccess.cpp + Module.cpp +) + +set_target_properties(${MODULE_NAME} PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES) + +target_include_directories(${MODULE_NAME} PRIVATE ../helpers) + +target_link_libraries(${MODULE_NAME} PRIVATE ${NAMESPACE}Plugins::${NAMESPACE}Plugins) + +install(TARGETS ${MODULE_NAME} + DESTINATION lib/${STORAGE_DIRECTORY}/plugins) + +write_config(${PLUGIN_NAME}) diff --git a/UsbAccess/Module.cpp b/UsbAccess/Module.cpp new file mode 100644 index 0000000000..ce759b615f --- /dev/null +++ b/UsbAccess/Module.cpp @@ -0,0 +1,22 @@ +/** +* If not stated otherwise in this file or this component's LICENSE +* file the following copyright and licenses apply: +* +* Copyright 2019 RDK Management +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +**/ + +#include "Module.h" + +MODULE_NAME_DECLARATION(BUILD_REFERENCE) diff --git a/UsbAccess/Module.h b/UsbAccess/Module.h new file mode 100644 index 0000000000..cbd4d9fc73 --- /dev/null +++ b/UsbAccess/Module.h @@ -0,0 +1,29 @@ +/** +* If not stated otherwise in this file or this component's LICENSE +* file the following copyright and licenses apply: +* +* Copyright 2019 RDK Management +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +**/ + +#pragma once +#ifndef MODULE_NAME +#define MODULE_NAME UsbAccess +#endif + +#include +#include + +#undef EXTERNAL +#define EXTERNAL diff --git a/UsbAccess/README.md b/UsbAccess/README.md new file mode 100644 index 0000000000..7ba3ab1c3c --- /dev/null +++ b/UsbAccess/README.md @@ -0,0 +1,27 @@ +----------------- +# UsbAccess + +## Versions +`org.rdk.UsbAccess.1` + +## Methods: +``` +curl -d '{"jsonrpc":"2.0","id":"3","method":"org.rdk.UsbAccess.1.getFileList","params":{}}' http://127.0.0.1:9998/jsonrpc +curl -d '{"jsonrpc":"2.0","id":"3","method":"org.rdk.UsbAccess.1.createLink","params":{}}' http://127.0.0.1:9998/jsonrpc +curl -d '{"jsonrpc":"2.0","id":"3","method":"org.rdk.UsbAccess.1.clearLink","params":{}}' http://127.0.0.1:9998/jsonrpc +``` + +## Responses +``` +{"jsonrpc":"2.0","id":3,"result":{"success":true}} +{"jsonrpc":"2.0","id":3,"result":{"success":true}} +{"jsonrpc":"2.0","id":3,"result":{"success":true}} +``` + +## Events +``` +onUSBMountChanged +``` + +## Full Reference +https://etwiki.sys.comcast.net/pages/viewpage.action?spaceKey=RDKV&title=UsbAccess \ No newline at end of file diff --git a/UsbAccess/UsbAccess.config b/UsbAccess/UsbAccess.config new file mode 100644 index 0000000000..8800440990 --- /dev/null +++ b/UsbAccess/UsbAccess.config @@ -0,0 +1,3 @@ +set (autostart false) +set (preconditions Platform) +set (callsign "org.rdk.UsbAccess") diff --git a/UsbAccess/UsbAccess.cpp b/UsbAccess/UsbAccess.cpp new file mode 100644 index 0000000000..0325359ee5 --- /dev/null +++ b/UsbAccess/UsbAccess.cpp @@ -0,0 +1,168 @@ +#include "UsbAccess.h" + +#include +#include +#include +#include + +const short WPEFramework::Plugin::UsbAccess::API_VERSION_NUMBER_MAJOR = 1; +const short WPEFramework::Plugin::UsbAccess::API_VERSION_NUMBER_MINOR = 0; +const string WPEFramework::Plugin::UsbAccess::SERVICE_NAME = "org.rdk.UsbAccess"; +const string WPEFramework::Plugin::UsbAccess::METHOD_GET_FILE_LIST = "getFileList"; +const string WPEFramework::Plugin::UsbAccess::METHOD_CREATE_LINK = "createLink"; +const string WPEFramework::Plugin::UsbAccess::METHOD_CLEAR_LINK = "clearLink"; +const string WPEFramework::Plugin::UsbAccess::EVT_ON_USB_MOUNT_CHANGED = "onUSBMountChanged"; +const string WPEFramework::Plugin::UsbAccess::USB_MOUNT_PATH = "/run/media/sda"; // platco +const string WPEFramework::Plugin::UsbAccess::LINK_PATH = "/opt/www/usbdrive"; +const string WPEFramework::Plugin::UsbAccess::LINK_URL_HTTP = "http://localhost:50050/usbdrive"; + +using namespace std; + +namespace WPEFramework { + namespace Plugin { + + SERVICE_REGISTRATION(UsbAccess, UsbAccess::API_VERSION_NUMBER_MAJOR, UsbAccess::API_VERSION_NUMBER_MINOR); + + UsbAccess* UsbAccess::_instance = nullptr; + + UsbAccess::UsbAccess() + : AbstractPlugin() + { + LOGINFO(); + UsbAccess::_instance = this; + registerMethod(METHOD_GET_FILE_LIST, &UsbAccess::getFileListWrapper, this); + registerMethod(METHOD_CREATE_LINK, &UsbAccess::createLinkWrapper, this); + registerMethod(METHOD_CLEAR_LINK, &UsbAccess::clearLinkWrapper, this); + } + + UsbAccess::~UsbAccess() + { + LOGINFO(); + UsbAccess::_instance = nullptr; + } + + const string UsbAccess::Initialize(PluginHost::IShell* /* service */) + { + LOGINFO(); + return ""; + } + + void UsbAccess::Deinitialize(PluginHost::IShell* /* service */) + { + LOGINFO(); + } + + string UsbAccess::Information() const + { + return(string("{\"service\": \"") + SERVICE_NAME + string("\"}")); + } + + // Registered methods (wrappers) begin + uint32_t UsbAccess::getFileListWrapper(const JsonObject& parameters, JsonObject& response) + { + LOGINFOMETHOD(); + + bool success = false; + + string dir = USB_MOUNT_PATH; + if (parameters.HasLabel("path")) + { + string path = parameters["path"].String(); + if (!path.empty()) + { + if (path[0] != '/') + dir += "/"; + dir += path; + } + } + + struct stat statbuf; + if (stat(dir.c_str(), &statbuf) != 0) + { + LOGERR("path '%s' not found", dir.c_str()); + response["error"] = "not found"; + } + else if (!S_ISDIR(statbuf.st_mode)) + { + LOGERR("path '%s' isn't dir", dir.c_str()); + response["error"] = "isn't dir"; + } + else + { + LOGINFO("path '%s' found and is dir", dir.c_str()); + + DIR* dirp = opendir(dir.c_str()); + if (dirp == nullptr) + { + LOGERR("could not open"); + response["error"] = "could not open"; + } + else + { + JsonArray contents; + struct dirent * dp; + while ((dp = readdir(dirp)) != nullptr) + { + const char* name = dp->d_name; + const char* t = dp->d_type == DT_DIR ? "d" : "f"; + + LOGINFO("%s : %s", name, t); + + JsonObject ent; + ent["name"] = name; + ent["t"] = t; + contents.Add(ent); + } + closedir(dirp); + response["contents"] = contents; + success = true; + } + } + + returnResponse(success); + } + + uint32_t UsbAccess::createLinkWrapper(const JsonObject& parameters, JsonObject& response) + { + LOGINFOMETHOD(); + + bool success = false; + int rc = symlink(USB_MOUNT_PATH.c_str(), LINK_PATH.c_str()); + + if (0 == rc) + { + LOGINFO("symlink %s created", LINK_PATH.c_str()); + response["baseURL"] = LINK_URL_HTTP; + success = true; + } + else + { + LOGERR("error %d", rc); + response["error"] = "could not create symlink"; + } + + returnResponse(success); + } + + uint32_t UsbAccess::clearLinkWrapper(const JsonObject& parameters, JsonObject& response) + { + LOGINFOMETHOD(); + + bool success = false; + int rc = remove(LINK_PATH.c_str()); + + if (0 == rc) + { + LOGINFO("symlink %s removed", LINK_PATH.c_str()); + success = true; + } + else + { + LOGERR("error %d", rc); + response["error"] = "could not remove symlink"; + } + + returnResponse(success); + } + } // namespace Plugin +} // namespace WPEFramework diff --git a/UsbAccess/UsbAccess.h b/UsbAccess/UsbAccess.h new file mode 100644 index 0000000000..5c2af80878 --- /dev/null +++ b/UsbAccess/UsbAccess.h @@ -0,0 +1,49 @@ +#pragma once + +#include "Module.h" +#include "utils.h" +#include "AbstractPlugin.h" + +namespace WPEFramework { + + namespace Plugin { + + class UsbAccess : public AbstractPlugin { + public: + UsbAccess(); + virtual ~UsbAccess(); + virtual const string Initialize(PluginHost::IShell* service) override; + virtual void Deinitialize(PluginHost::IShell* service) override; + virtual string Information() const override; + + public/*members*/: + static UsbAccess* _instance; + + public /*constants*/: + static const short API_VERSION_NUMBER_MAJOR; + static const short API_VERSION_NUMBER_MINOR; + static const string SERVICE_NAME; + //methods + static const string METHOD_GET_FILE_LIST; + static const string METHOD_CREATE_LINK; + static const string METHOD_CLEAR_LINK; + //events + static const string EVT_ON_USB_MOUNT_CHANGED; + //other + static const string USB_MOUNT_PATH; + static const string LINK_PATH; + static const string LINK_URL_HTTP; + + private/*registered methods (wrappers)*/: + + //methods ("parameters" here is "params" from the curl request) + uint32_t getFileListWrapper(const JsonObject& parameters, JsonObject& response); + uint32_t createLinkWrapper(const JsonObject& parameters, JsonObject& response); + uint32_t clearLinkWrapper(const JsonObject& parameters, JsonObject& response); + + private/*internal methods*/: + UsbAccess(const UsbAccess&) = delete; + UsbAccess& operator=(const UsbAccess&) = delete; + }; + } // namespace Plugin +} // namespace WPEFramework From 502790bd977a499836c9987b5e3b6c93a4733813 Mon Sep 17 00:00:00 2001 From: Nikita Poltorapavlo Date: Wed, 2 Dec 2020 18:20:47 +0200 Subject: [PATCH 2/2] remove unused event, updated doc --- UsbAccess/README.md | 8 ++++---- UsbAccess/UsbAccess.cpp | 1 - UsbAccess/UsbAccess.h | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/UsbAccess/README.md b/UsbAccess/README.md index 7ba3ab1c3c..71b928fc1b 100644 --- a/UsbAccess/README.md +++ b/UsbAccess/README.md @@ -6,21 +6,21 @@ ## Methods: ``` -curl -d '{"jsonrpc":"2.0","id":"3","method":"org.rdk.UsbAccess.1.getFileList","params":{}}' http://127.0.0.1:9998/jsonrpc +curl -d '{"jsonrpc":"2.0","id":"3","method":"org.rdk.UsbAccess.1.getFileList","params":{"path":"www"}}' http://127.0.0.1:9998/jsonrpc curl -d '{"jsonrpc":"2.0","id":"3","method":"org.rdk.UsbAccess.1.createLink","params":{}}' http://127.0.0.1:9998/jsonrpc curl -d '{"jsonrpc":"2.0","id":"3","method":"org.rdk.UsbAccess.1.clearLink","params":{}}' http://127.0.0.1:9998/jsonrpc ``` ## Responses ``` -{"jsonrpc":"2.0","id":3,"result":{"success":true}} -{"jsonrpc":"2.0","id":3,"result":{"success":true}} +{"jsonrpc":"2.0","id":3,"result":{"contents":[{"name":"var","t":"f"},{"name":"..","t":"d"},{"name":"pages","t":"d"},{"name":"logs","t":"f"},{"name":".","t":"d"}],"success":true}} +{"jsonrpc":"2.0","id":3,"result":{"baseURL":"http://localhost:50050/usbdrive","success":true}} {"jsonrpc":"2.0","id":3,"result":{"success":true}} ``` ## Events ``` -onUSBMountChanged +None ``` ## Full Reference diff --git a/UsbAccess/UsbAccess.cpp b/UsbAccess/UsbAccess.cpp index 0325359ee5..8abf1d188d 100644 --- a/UsbAccess/UsbAccess.cpp +++ b/UsbAccess/UsbAccess.cpp @@ -11,7 +11,6 @@ const string WPEFramework::Plugin::UsbAccess::SERVICE_NAME = "org.rdk.UsbAccess" const string WPEFramework::Plugin::UsbAccess::METHOD_GET_FILE_LIST = "getFileList"; const string WPEFramework::Plugin::UsbAccess::METHOD_CREATE_LINK = "createLink"; const string WPEFramework::Plugin::UsbAccess::METHOD_CLEAR_LINK = "clearLink"; -const string WPEFramework::Plugin::UsbAccess::EVT_ON_USB_MOUNT_CHANGED = "onUSBMountChanged"; const string WPEFramework::Plugin::UsbAccess::USB_MOUNT_PATH = "/run/media/sda"; // platco const string WPEFramework::Plugin::UsbAccess::LINK_PATH = "/opt/www/usbdrive"; const string WPEFramework::Plugin::UsbAccess::LINK_URL_HTTP = "http://localhost:50050/usbdrive"; diff --git a/UsbAccess/UsbAccess.h b/UsbAccess/UsbAccess.h index 5c2af80878..4e5a355fb4 100644 --- a/UsbAccess/UsbAccess.h +++ b/UsbAccess/UsbAccess.h @@ -28,7 +28,6 @@ namespace WPEFramework { static const string METHOD_CREATE_LINK; static const string METHOD_CLEAR_LINK; //events - static const string EVT_ON_USB_MOUNT_CHANGED; //other static const string USB_MOUNT_PATH; static const string LINK_PATH;