first step of channel parsing
This commit is contained in:
3
Makefile
3
Makefile
@@ -2,6 +2,7 @@ target = r
|
||||
CXX = g++
|
||||
CXX_FLAGS := -std=c++14 -Iloguru
|
||||
LD_FLAGS := -lpthread -ldl
|
||||
# LOGURU_FLAG := -DLOGURU_SCOPE_TIME_PRECISION=9
|
||||
BUILD := ./build
|
||||
OBJS := $(BUILD)/client.o $(BUILD)/loguru.o
|
||||
|
||||
@@ -13,7 +14,7 @@ $(BUILD)/%.o: %.cc
|
||||
|
||||
$(BUILD)/loguru.o:
|
||||
@mkdir -p $(BUILD)
|
||||
g++ -c loguru/loguru.cpp -Iloguru -o $@ $(CXX_FLAGS)
|
||||
g++ -c loguru/loguru.cpp -Iloguru -o $@ $(CXX_FLAGS) $(LOGURU_FLAG)
|
||||
|
||||
r: $(OBJS)
|
||||
g++ -o $@ $^ $(LD_FLAGS)
|
||||
|
||||
82
client.cc
82
client.cc
@@ -19,17 +19,59 @@ const char * HOST = "192.168.30.89";
|
||||
const uint32_t PORT = 9999;
|
||||
const size_t MAX_EVENT_SIZE = 33484; // in bytes
|
||||
const size_t MIN_EVENT_SIZE = 204; // in bytes
|
||||
const bool verbose = true;
|
||||
const size_t MAX_CHANNEL_SIZE = 4184;
|
||||
const size_t MIN_CHANNEL_SIZE = 24;
|
||||
const uint8_t EVENT_MARKER = 0xA5;
|
||||
const uint8_t CHANNEL_MARKER = 0x5A;
|
||||
|
||||
int sock = 0;
|
||||
bool running = true;
|
||||
int open_socket();
|
||||
void signalHandler( int signum );
|
||||
|
||||
bool bad_data = true;
|
||||
|
||||
int main(int argc, char const* argv[])
|
||||
int open_socket();
|
||||
void signalHandler( int signum );
|
||||
void setup_logger();
|
||||
size_t process_channel(char const * ptr, size_t len);
|
||||
size_t process_channel(char const * ptr, size_t len){
|
||||
if ((len < MIN_CHANNEL_SIZE) || (len > MAX_CHANNEL_SIZE)) {
|
||||
LOG_F(WARNING, "channel length must be between %u and %u.",
|
||||
MIN_CHANNEL_SIZE, MAX_CHANNEL_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t bytes_processed = 0;
|
||||
size_t bytes_left = len;
|
||||
bool valid_channel = false;
|
||||
uint32_t * wordData = (uint32_t *)ptr;
|
||||
|
||||
while ((bytes_left > 0) && !valid_channel) {
|
||||
if ((ptr[0] & 0xFF) != CHANNEL_MARKER){
|
||||
LOG_F(WARNING, "bad_data, expecting %02X, received %02X",
|
||||
static_cast<uint32_t>(CHANNEL_MARKER), static_cast<uint32_t>(ptr[0]));
|
||||
bytes_left -= 4;
|
||||
} else {
|
||||
valid_channel = true;
|
||||
|
||||
size_t channel_size = ((wordData[0] & 0xFFFFFF00) >> 8) * sizeof(uint32_t);
|
||||
if ((channel_size > MAX_CHANNEL_SIZE) || (channel_size < MIN_CHANNEL_SIZE)) {
|
||||
bad_data = true;
|
||||
LOG_F(WARNING, "Invalid channel size: %u", channel_size);
|
||||
}
|
||||
|
||||
LOG_F(INFO, "channel size %u", channel_size);
|
||||
bytes_left -= (channel_size - 4);
|
||||
}
|
||||
}
|
||||
|
||||
// return number of bytes cosumed of the channel
|
||||
return len - bytes_left;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
setup_logger();
|
||||
loguru::init(argc, argv);
|
||||
signal(SIGINT, signalHandler);
|
||||
int ret = open_socket();
|
||||
if (ret != 0)
|
||||
@@ -56,7 +98,7 @@ int main(int argc, char const* argv[])
|
||||
else{
|
||||
// read in first 4 bytes
|
||||
nread = recv(sock, char_buffer, 4, 0);
|
||||
LOG_IF_F(INFO, verbose, "read in %u bytes: 0x%08X", nread, wordData[0]);
|
||||
LOG_F(INFO, "read in %u bytes: 0x%08X", nread, wordData[0]);
|
||||
// if not A5 marker, go to next iteration (read in 4 more bytes)
|
||||
if ((char_buffer[0] & 0xFF) != 0xA5){
|
||||
bad_data = true;
|
||||
@@ -73,13 +115,14 @@ int main(int argc, char const* argv[])
|
||||
}
|
||||
else {
|
||||
// ok, this seems to be good data
|
||||
LOG_IF_F(INFO, verbose, "next %u bytes (expecting %u): 0x%08X",
|
||||
nread, frameSize - 4, wordData[0]);
|
||||
LOG_F(INFO, "next %u bytes (expecting %u): %08X %08X %08X %08X",
|
||||
nread, frameSize - 4, wordData[0], wordData[1], wordData[2], wordData[3]);
|
||||
LOG_F(INFO, "%08X %08X %08X %08X", wordData[4], wordData[5], wordData[6], wordData[7]);
|
||||
uint8_t board_id = (wordData[0] >> 24) & 0xFF;
|
||||
uint8_t version = wordData[0] & 0xFF;
|
||||
uint16_t spill_id = (wordData[1] & 0x00FFFF00) >> 8;
|
||||
uint8_t channel_count = (wordData[1]) & 0x000000FF;
|
||||
LOG_IF_F(INFO, verbose, "board %u, version %u, spill %u, channel_count %u",
|
||||
LOG_F(INFO, "board %u, version %u, spill %u, channel_count %u",
|
||||
static_cast<uint32_t>(board_id),
|
||||
static_cast<uint32_t>(version),
|
||||
static_cast<uint32_t>(spill_id),
|
||||
@@ -90,7 +133,12 @@ int main(int argc, char const* argv[])
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
|
||||
size_t bytes_left = frameSize - 3;
|
||||
size_t bytes_processed = 0;
|
||||
while (bytes_left >= MIN_CHANNEL_SIZE) {
|
||||
bytes_processed = process_channel(char_buffer + 2 * 4, bytes_left);
|
||||
bytes_left -= bytes_processed;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -103,7 +151,7 @@ int main(int argc, char const* argv[])
|
||||
}
|
||||
|
||||
void signalHandler( int signum ) {
|
||||
LOG_F(WARNING, "Interrupt signal (%d) received, exiting...", signum);
|
||||
RAW_LOG_F(WARNING, "Interrupt signal (%d) received, exiting...", signum);
|
||||
running = false;
|
||||
}
|
||||
|
||||
@@ -130,3 +178,15 @@ int open_socket(){
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void setup_logger(){
|
||||
loguru::g_stderr_verbosity = loguru::Verbosity_INFO;
|
||||
// loguru::g_stderr_verbosity = loguru::Verbosity_WARNING;
|
||||
loguru::g_preamble_date = true; // The date field
|
||||
loguru::g_preamble_time = true; // The time of the current day
|
||||
loguru::g_preamble_uptime = true; // The time since init call
|
||||
loguru::g_preamble_thread = false; // The logging thread
|
||||
loguru::g_preamble_file = false; // The file from which the log originates from
|
||||
loguru::g_preamble_verbose = false; // The verbosity field
|
||||
loguru::g_preamble_pipe = false; // The pipe symbol right before the message
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user