eive-obsw/linux/obc/Ptme.cpp

73 lines
2.2 KiB
C++
Raw Normal View History

2021-09-19 12:27:48 +02:00
#include <sys/mman.h>
#include <fcntl.h>
#include <linux/obc/Ptme.h>
#include "fsfw/serviceinterface/ServiceInterface.h"
2021-09-26 08:29:30 +02:00
#include "PtmeConfig.h"
2021-09-19 12:27:48 +02:00
2021-10-11 19:34:34 +02:00
Ptme::Ptme(object_id_t objectId): SystemObject(objectId) {
2021-09-19 12:27:48 +02:00
}
Ptme::~Ptme() {
}
ReturnValue_t Ptme::initialize() {
2021-09-26 08:29:30 +02:00
int fd = open(PtmeConfig::UIO_DEVICE_FILE, O_RDWR);
2021-09-19 12:27:48 +02:00
if (fd < 1) {
sif::warning << "Ptme::initialize: Invalid UIO device file" << std::endl;
return RETURN_FAILED;
}
/**
* Map uio device in virtual address space
* PROT_WRITE: Map uio device in writable only mode
*/
ptmeBaseAddress = static_cast<uint32_t*>(mmap(NULL, MAP_SIZE, PROT_WRITE,
MAP_SHARED, fd, 0));
if (ptmeBaseAddress == MAP_FAILED) {
sif::error << "Ptme::initialize: Failed to map uio address" << std::endl;
return RETURN_FAILED;
}
2021-09-26 08:29:30 +02:00
VcInterfaceMapIter iter;
for (iter = vcInterfaceMap.begin(); iter != vcInterfaceMap.end(); iter++) {
iter->second->setRegisterAddress(ptmeBaseAddress);
}
return RETURN_OK;
2021-09-19 12:27:48 +02:00
}
2021-09-26 08:29:30 +02:00
ReturnValue_t Ptme::writeToVc(uint8_t vcId, const uint8_t * data, size_t size) {
2021-09-19 12:27:48 +02:00
ReturnValue_t result = RETURN_OK;
VcInterfaceMapIter vcInterfaceMapIter = vcInterfaceMap.find(vcId);
if (vcInterfaceMapIter == vcInterfaceMap.end()) {
sif::warning << "Ptme::writeToVc: No virtual channel interface found for the virtual "
"channel with id " << static_cast<unsigned int>(vcId) << std::endl;
return UNKNOWN_VC_ID;
}
2021-09-26 08:29:30 +02:00
result = vcInterfaceMapIter->second->write(data, size);
2021-09-19 12:27:48 +02:00
return result;
}
void Ptme::addVcInterface(VcId_t vcId, VcInterfaceIF* vc) {
2021-10-11 19:34:34 +02:00
if (vcId > common::NUMBER_OF_VIRTUAL_CHANNELS) {
2021-09-19 12:27:48 +02:00
sif::warning << "Ptme::addVcInterface: Invalid virtual channel ID" << std::endl;
return;
}
if (vc == nullptr) {
sif::warning << "Ptme::addVcInterface: Invalid virtual channel interface" << std::endl;
return;
}
auto status = vcInterfaceMap.emplace(vcId, vc);
if (status.second == false) {
sif::warning << "Ptme::addVcInterface: Failed to add virtual channel interface to "
"virtual channel map" << std::endl;
return;
}
}