Files
reHDD/src/printer.cpp
2025-12-10 20:40:48 +01:00

101 lines
3.2 KiB
C++

/**
* @file printer.cpp
* @brief Send drive data to printer service using ipc msg queue
* @author Hendrik Schutter
* @date 24.11.2022
*/
#include "../include/reHDD.h"
bool Printer::instanceFlag = false;
Printer *Printer::single = NULL;
/**
* \brief create new Printer instance
* \param path to log file
* \param struct with data
* \return instance of Printer
*/
Printer::Printer()
{
if (-1 == (this->msqid = msgget((key_t)IPC_MSG_QUEUE_KEY, IPC_CREAT | 0666)))
{
Logger::logThis()->error("Printer: Create mgs queue failed!");
}
}
/**
* \brief deconstructor
* \return void
*/
Printer::~Printer()
{
instanceFlag = false;
}
/**
* \brief send data to msg queue
* \return void
*/
void Printer::print(Drive *drive)
{
t_msgQueueData msgQueueData;
msgQueueData.msg_queue_type = 1;
snprintf(msgQueueData.driveData.caDriveIndex, STR_BUFFER_SIZE, "%i", drive->u16DriveIndex);
snprintf(msgQueueData.driveData.caDriveState, STR_BUFFER_SIZE, "shredded");
snprintf(msgQueueData.driveData.caDriveModelFamily, STR_BUFFER_SIZE, "%s", drive->getModelFamily().c_str());
snprintf(msgQueueData.driveData.caDriveModelName, STR_BUFFER_SIZE, "%s", drive->getModelName().c_str());
snprintf(msgQueueData.driveData.caDriveCapacity, STR_BUFFER_SIZE, "%li", drive->getCapacity());
snprintf(msgQueueData.driveData.caDriveSerialnumber, STR_BUFFER_SIZE, "%s", drive->getSerial().c_str());
snprintf(msgQueueData.driveData.caDriveHours, STR_BUFFER_SIZE, "%i", drive->getPowerOnHours());
snprintf(msgQueueData.driveData.caDriveCycles, STR_BUFFER_SIZE, "%i", drive->getPowerCycles());
snprintf(msgQueueData.driveData.caDriveErrors, STR_BUFFER_SIZE, "%i", drive->getErrorCount());
snprintf(msgQueueData.driveData.caDriveShredTimestamp, STR_BUFFER_SIZE, "%li", drive->getActionStartTimestamp());
snprintf(msgQueueData.driveData.caDriveShredDuration, STR_BUFFER_SIZE, "%li", drive->getTaskDuration());
switch (drive->connectionType)
{
case Drive::ConnectionType::USB:
strncpy(msgQueueData.driveData.caDriveConnectionType, "usb", STR_BUFFER_SIZE);
break;
case Drive::ConnectionType::SATA:
strncpy(msgQueueData.driveData.caDriveConnectionType, "sata", STR_BUFFER_SIZE);
break;
case Drive::ConnectionType::NVME:
strncpy(msgQueueData.driveData.caDriveConnectionType, "nvme", STR_BUFFER_SIZE);
break;
case Drive::ConnectionType::UNKNOWN:
default:
strncpy(msgQueueData.driveData.caDriveConnectionType, "na", STR_BUFFER_SIZE);
}
snprintf(msgQueueData.driveData.caDriveReHddVersion, STR_BUFFER_SIZE, "%s", REHDD_VERSION);
if (-1 == msgsnd(this->msqid, &msgQueueData, sizeof(t_msgQueueData) - sizeof(long), 0))
{
Logger::logThis()->error("Printer: Send mgs queue failed!");
}
else
{
Logger::logThis()->info("Printer: print triggered - Drive: " + drive->getSerial());
}
}
/**
* \brief return a instance of the printer
* \return printer obj
*/
Printer *Printer::getPrinter()
{
if (!instanceFlag)
{
single = new Printer(); // create new obj
instanceFlag = true;
return single;
}
else
{
return single; // return existing obj
}
}