Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions FSMappingKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
// Mapping item types
enum FSItemType {
EFSDirectory,
EFSBinary,
EFSAmend1,
EFSAmend2,
EFSAmend3,
Expand Down
1 change: 1 addition & 0 deletions FSSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ CFSSubsystem::CFSSubsystem(const std::string& strName, core::log::Logger& logger
{
// Provide mapping keys to upper layer
addContextMappingKey("Directory");
addContextMappingKey("Binary");
addContextMappingKey("Amend1");
addContextMappingKey("Amend2");
addContextMappingKey("Amend3");
Expand Down
73 changes: 51 additions & 22 deletions FSSubsystemObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ CFSSubsystemObject::CFSSubsystemObject(const string& mappingValue,
EFSAmend1,
(EFSAmendEnd - EFSAmend1 + 1),
context),
_wrongElementTypeErrorOccured(false), _stringFormat(false)
_wrongElementTypeErrorOccured(false), _stringFormat(false), _isBinary(context.iSet(EFSBinary))
{
// Get actual element type
const CParameterType* parameterType
= static_cast<const CParameterType*>(instanceConfigurableElement->getTypeElement());

// Retrieve sizes
_scalarSize = parameterType->getSize();
_arraySize = instanceConfigurableElement->getFootPrint() / _scalarSize;
if (instanceConfigurableElement->isScalar()) {
_arraySize = 1;
} else {
_arraySize = instanceConfigurableElement->getArrayLength();
}
_scalarSize = instanceConfigurableElement->getFootPrint() / _arraySize;

// Amend
_directoryPath = context.getItem(EFSDirectory) + "/";
Expand All @@ -82,6 +82,15 @@ CFSSubsystemObject::CFSSubsystemObject(const string& mappingValue,
case CInstanceConfigurableElement::EStringParameter:
_stringFormat = true;
break;
case CInstanceConfigurableElement::EBitParameterBlock:
// fallthrough
case CInstanceConfigurableElement::EParameterBlock:
// fallthrough
case CInstanceConfigurableElement::EComponent:
if (not _isBinary) {
_wrongElementTypeErrorOccured = true;
}
break;
default:
_wrongElementTypeErrorOccured = true;
break;
Expand All @@ -93,7 +102,13 @@ bool CFSSubsystemObject::accessHW(bool receive, string& error)
// Check parameter type is ok (deferred error, no exceptions available :-()
if (_wrongElementTypeErrorOccured) {

error = "Only Parameter and StringParameter types are supported";
if (_isBinary) {
error = "Only numerical Parameters, StringParameter, BitParameterBlock, ParameterBlocks"
" and Components are supported in binary mode.";
} else {
error = "Only Parameter and StringParameter types are supported in string mode (blocks"
" are not).";
}

return false;
}
Expand Down Expand Up @@ -147,8 +162,7 @@ bool CFSSubsystemObject::receiveFromHW(string& error)
bool CFSSubsystemObject::sendToFile(int fileDesc, string& error)
{
uint32_t index;
int nbBytes;
string formatedContent;
ssize_t expectedWriteSize, actualWriteSize;
void* blackboardContent = alloca(_scalarSize);
string filePath = _directoryPath + getFormattedMappingValue();

Expand All @@ -157,26 +171,31 @@ bool CFSSubsystemObject::sendToFile(int fileDesc, string& error)
// Read Value in BlackBoard
blackboardRead(blackboardContent, _scalarSize);

formatedContent = toString(blackboardContent, _scalarSize);

// WARNING: Current C++ STL implementation of fstream operator '<<' cannot
// write a string at once: it instead writes the first character and
// then the remainining part of the string.
// To support sysfs, we need to be able to write the string at once,
// thus we use 'write' API.
if (_isBinary) {
expectedWriteSize = _scalarSize;
actualWriteSize = write(fileDesc, blackboardContent, _scalarSize);
} else {
string formatedContent = toString(blackboardContent, _scalarSize);
expectedWriteSize = formatedContent.size();

// Try to write the entire string
actualWriteSize = write(fileDesc, (const void *)formatedContent.c_str(), formatedContent.size());
}

// Try to write the entire string
nbBytes = write(fileDesc, (const void *)formatedContent.c_str(), formatedContent.size());

if (nbBytes == -1) {
if (actualWriteSize == -1) {
// Error when writing
stringstream errorStream;
errorStream << "Unable to write element #" << index << "over a total of "
<< _arraySize << " elements to file " << filePath
<< " with error " << errno;
error = errorStream.str();
return false;
} else if (nbBytes != (int)formatedContent.size()) {
} else if (actualWriteSize != expectedWriteSize) {
// Did not write all characters at once
stringstream errorStream;
errorStream << "Unable to write element #" << index << "over a total of "
Expand All @@ -192,8 +211,7 @@ bool CFSSubsystemObject::sendToFile(int fileDesc, string& error)
bool CFSSubsystemObject::receiveFromFile(ifstream& inputFile, string& error)
{
uint32_t index;
char formatedContent[STR_FORMAT_LENGTH];
void* blackboardContent = alloca(_scalarSize);
char blackboardContent[_scalarSize];
string filePath = _directoryPath + getFormattedMappingValue();

for (index = 0 ; index < _arraySize ; index++) {
Expand All @@ -208,9 +226,20 @@ bool CFSSubsystemObject::receiveFromFile(ifstream& inputFile, string& error)
return false;
}

inputFile.getline(formatedContent, STR_FORMAT_LENGTH);

fromString(formatedContent, blackboardContent, _scalarSize);
if (_isBinary) {
auto read = inputFile.readsome(blackboardContent, _scalarSize);
Copy link
Contributor

@krocard krocard Sep 9, 2016

Choose a reason for hiding this comment

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

Open the file in binary form. Even if it is not strictly required on Linux as \n have no special meaning.

if (read != _scalarSize) {
stringstream errorStream;
errorStream << "Failed to read " << _scalarSize << " bytes from " << filePath;
error = errorStream.str();
return false;
}
} else {
char formatedContent[STR_FORMAT_LENGTH];
inputFile.getline(formatedContent, STR_FORMAT_LENGTH);

fromString(formatedContent, blackboardContent, _scalarSize);
}

// Write Value in Blackboard
blackboardWrite(blackboardContent, _scalarSize);
Expand Down
2 changes: 2 additions & 0 deletions FSSubsystemObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,6 @@ class CFSSubsystemObject : public CFormattedSubsystemObject
bool _wrongElementTypeErrorOccured;
// Format for reading
bool _stringFormat;
// Is the object to be written in binary format?
bool _isBinary;
};
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ This is a file system plugin for the
[parameter-framework](https://github.com/01org/parameter-framework)
handling file system operations such as `read()` and `write()`.

By default, the plugin reads/writes individual parameters (numerical and
strings) as strings (one parameter per file or one array per file). This can
be overriden using the `Binary` mapping key; when added to a parameter or a
block (ParameterBlock or Component), the plugin reads/writes them in binary
(in-memory representation). e.g.:

```xml
<ParameterBlock Name="foo" Mapping="Binary,File:bar">
<IntegerParameter .../>
<ParameterBlock ...>
</ParameterBlock>
...
</ParameterBlock>
```

## Compiling

Expand Down