RASCSI/src/raspberrypi/rasutil.cpp
uweseimet af6e311e6e
protobuf-based rasctl/rascsi command interface (#129)
* Initial protobuf definition

* protobuf result message draft

* Merge with develop branch

* Makefile generates protobuf-based source files

* Interface update

* Fixed typo

* Fixed typo

* Updated returning status

* Serialize return data

* Use correct descriptor

* Made interface fields required

* Deserialize result

* Serialization update

* Updated serialization

* Serialization update

* Updated deserialization

* status handling update

* Evaluate status

* Revert "Evaluate status"

This reverts commit 3d8f2c3252.

* Completed sense_key enum

* Renaming

* Added protobuf Command

* Updated command evaluation

* Interface update

* Interface update

* Added DeviceType enum

* Improved type-safety

* Fixed typo

* Type-safety update

* Fixed typo

* Error handling update

* Updated list handling

* Error handling update

* Use more C++ strings

* protobuf enums can provide their names

* Fixed listing devices

* Updated logging

* Enum usage cleanup

* More enum cleanup

* Fixed command check

* Updated type check

* Updated enums

* Removed unused variable

* Removed goto, added exception

* Socket handling cleanup

* Code locality cleanup

* Added helper method

* Extracted code

* Updated socket/file handling

* Use C++ I/O

* Use tolower()

* Renaming

* Simplified has_suffix

* Fixed typo

* Use spdlog namespace

* Simplified formatting (endl) of error messages

* Added -s option for changing the runtime log level to rasctl

* Renaming

* Renaming

* Updated error reporting

* Fixed log string formatting

* String conversion cleanup

* Fixed typo

* Log mmap error (happens on 64 bit)

* Improved proto3 compatibility, updated error handling

* CHanges based on review

* Fixed comment

* Comment update

* Updated ListDevice to not directly write to the stream

* Use size_t

* Renaming

* Buffering update

* MapController should not use fp

* Use fd, not fp

* rasctl has to display *all* results

* rasctl has to display *all* results

* Error handling update

* Updated to proto3 protocol

* Optimization by using protobuf-lite

* RaspBian outdated protoc does not support _Name

* Added protobuf-compiler to easyinstall.sh

Co-authored-by: akuker <34318535+akuker@users.noreply.github.com>
2021-07-18 17:15:13 -05:00

66 lines
1.6 KiB
C++

//---------------------------------------------------------------------------
//
// SCSI Target Emulator RaSCSI (*^..^*)
// for Raspberry Pi
//
// Powered by XM6 TypeG Technology.
// Copyright (C) 2016-2020 GIMONS
// Copyright (C) 2020 akuker
//
//---------------------------------------------------------------------------
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include "exceptions.h"
#include "rasutil.h"
using namespace std;
//---------------------------------------------------------------------------
//
// Serialize/Deserialize protobuf data: Length followed by the actual data
//
//---------------------------------------------------------------------------
void SerializeProtobufData(FILE *fp, const string& data)
{
// Write the size of the protobuf data as a header
size_t size = data.length();
fwrite(&size, sizeof(size), 1, fp);
// Write the actual protobuf data
void *buf = malloc(size);
memcpy(buf, data.data(), size);
fwrite(buf, size, 1, fp);
fflush(fp);
free(buf);
}
string DeserializeProtobufData(int fd)
{
// First read the header with the size of the protobuf data
size_t size;
size_t res = read(fd, &size, sizeof(int));
if (res != sizeof(int)) {
// No more data
return "";
}
// Read the actual protobuf data
void *buf = malloc(size);
res = read(fd, buf, size);
if (res != size) {
free(buf);
throw ioexception("Missing protobuf data");
}
// Read protobuf data into a string, to be converted into a protobuf data structure by the caller
string data((const char *)buf, size);
free(buf);
return data;
}