-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #526 from npoltorapavlo/RDK-30538
RDK-30538 : Create UsbAccess plugin
- Loading branch information
Showing
8 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <plugins/plugins.h> | ||
#include <tracing/tracing.h> | ||
|
||
#undef EXTERNAL | ||
#define EXTERNAL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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":{"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":{"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 | ||
``` | ||
None | ||
``` | ||
|
||
## Full Reference | ||
https://etwiki.sys.comcast.net/pages/viewpage.action?spaceKey=RDKV&title=UsbAccess |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
set (autostart false) | ||
set (preconditions Platform) | ||
set (callsign "org.rdk.UsbAccess") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
#include "UsbAccess.h" | ||
|
||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <dirent.h> | ||
#include <unistd.h> | ||
|
||
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::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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#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 | ||
//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 |