Skip to content

Commit

Permalink
Replace exit code defines with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
mintar committed Apr 14, 2016
1 parent 11ca91c commit 4356913
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 68 deletions.
7 changes: 7 additions & 0 deletions include/sick_tim/abstract_parser.h
Expand Up @@ -41,6 +41,13 @@
namespace sick_tim
{

enum ExitCode
{
ExitSuccess = 0
, ExitError = 1 // non-fatal error, retry
, ExitFatal = 2 // fatal error, exit
};

class AbstractParser
{
public:
Expand Down
2 changes: 0 additions & 2 deletions include/sick_tim/sick_tim_common.h
Expand Up @@ -55,8 +55,6 @@
#include <sick_tim/SickTimConfig.h>
#include <sick_tim/abstract_parser.h>

#define EXIT_FATAL 2

namespace sick_tim
{

Expand Down
6 changes: 3 additions & 3 deletions src/sick_tim310.cpp
Expand Up @@ -52,7 +52,7 @@ int main(int argc, char **argv)

sick_tim::SickTimCommon* s = NULL;

int result = EXIT_FAILURE;
int result = sick_tim::ExitError;
while (ros::ok())
{
// Atempt to connect/reconnect
Expand All @@ -63,12 +63,12 @@ int main(int argc, char **argv)
s = new sick_tim::SickTimCommonUsb(parser);
result = s->init();

while(ros::ok() && (result == EXIT_SUCCESS)){
while(ros::ok() && (result == sick_tim::ExitSuccess)){
ros::spinOnce();
result = s->loopOnce();
}

if (result == EXIT_FATAL)
if (result == sick_tim::ExitFatal)
return result;

if (ros::ok() && !subscribe_datagram)
Expand Down
6 changes: 3 additions & 3 deletions src/sick_tim310_1130000m01.cpp
Expand Up @@ -52,7 +52,7 @@ int main(int argc, char **argv)

sick_tim::SickTimCommon* s = NULL;

int result = EXIT_FAILURE;
int result = sick_tim::ExitError;
while (ros::ok())
{
// Atempt to connect/reconnect
Expand All @@ -63,12 +63,12 @@ int main(int argc, char **argv)
s = new sick_tim::SickTimCommonUsb(parser);
result = s->init();

while(ros::ok() && (result == EXIT_SUCCESS)){
while(ros::ok() && (result == sick_tim::ExitSuccess)){
ros::spinOnce();
result = s->loopOnce();
}

if (result == EXIT_FATAL)
if (result == sick_tim::ExitFatal)
return result;

if (ros::ok() && !subscribe_datagram)
Expand Down
6 changes: 3 additions & 3 deletions src/sick_tim310_1130000m01_parser.cpp
Expand Up @@ -83,14 +83,14 @@ int SickTim3101130000M01Parser::parse_datagram(char* datagram, size_t datagram_l
"received less fields than expected fields (actual: %zu, expected: %zu), ignoring scan", count, NUM_FIELDS);
ROS_WARN("are you using the correct node? (124 --> sick_tim310_1130000m01, 306 --> sick_tim551_2050001, 580 --> sick_tim310s01, 592 --> sick_tim310)");
// ROS_DEBUG("received message was: %s", datagram_copy);
return EXIT_FAILURE;
return ExitError;
}
else if (count > NUM_FIELDS)
{
ROS_WARN("received more fields than expected (actual: %zu, expected: %zu), ignoring scan", count, NUM_FIELDS);
ROS_WARN("are you using the correct node? (124 --> sick_tim310_1130000m01, 306 --> sick_tim551_2050001, 580 --> sick_tim310s01, 592 --> sick_tim310)");
// ROS_DEBUG("received message was: %s", datagram_copy);
return EXIT_FAILURE;
return ExitError;
}

// ----- read fields into msg
Expand Down Expand Up @@ -220,7 +220,7 @@ int SickTim3101130000M01Parser::parse_datagram(char* datagram, size_t datagram_l
// - add time offset (to account for USB latency etc.)
msg.header.stamp += ros::Duration().fromSec(config.time_offset);

return EXIT_SUCCESS;
return ExitSuccess;
}

} /* namespace sick_tim */
6 changes: 3 additions & 3 deletions src/sick_tim310_parser.cpp
Expand Up @@ -83,14 +83,14 @@ int SickTim310Parser::parse_datagram(char* datagram, size_t datagram_length, Sic
"received less fields than expected fields (actual: %zu, expected: %zu), ignoring scan", count, NUM_FIELDS);
ROS_WARN("are you using the correct node? (124 --> sick_tim310_1130000m01, 306 --> sick_tim551_2050001, 580 --> sick_tim310s01, 592 --> sick_tim310)");
// ROS_DEBUG("received message was: %s", datagram_copy);
return EXIT_FAILURE;
return ExitError;
}
else if (count > NUM_FIELDS)
{
ROS_WARN("received more fields than expected (actual: %zu, expected: %zu), ignoring scan", count, NUM_FIELDS);
ROS_WARN("are you using the correct node? (124 --> sick_tim310_1130000m01, 306 --> sick_tim551_2050001, 580 --> sick_tim310s01, 592 --> sick_tim310)");
// ROS_DEBUG("received message was: %s", datagram_copy);
return EXIT_FAILURE;
return ExitError;
}

// ----- read fields into msg
Expand Down Expand Up @@ -220,7 +220,7 @@ int SickTim310Parser::parse_datagram(char* datagram, size_t datagram_length, Sic
// - add time offset (to account for USB latency etc.)
msg.header.stamp += ros::Duration().fromSec(config.time_offset);

return EXIT_SUCCESS;
return ExitSuccess;
}

} /* namespace sick_tim */
6 changes: 3 additions & 3 deletions src/sick_tim310s01.cpp
Expand Up @@ -52,7 +52,7 @@ int main(int argc, char **argv)

sick_tim::SickTimCommon* s = NULL;

int result = EXIT_FAILURE;
int result = sick_tim::ExitError;
while (ros::ok())
{
// Atempt to connect/reconnect
Expand All @@ -63,12 +63,12 @@ int main(int argc, char **argv)
s = new sick_tim::SickTimCommonUsb(parser);
result = s->init();

while(ros::ok() && (result == EXIT_SUCCESS)){
while(ros::ok() && (result == sick_tim::ExitSuccess)){
ros::spinOnce();
result = s->loopOnce();
}

if (result == EXIT_FATAL)
if (result == sick_tim::ExitFatal)
return result;

if (ros::ok() && !subscribe_datagram)
Expand Down
6 changes: 3 additions & 3 deletions src/sick_tim310s01_parser.cpp
Expand Up @@ -83,14 +83,14 @@ int SickTim310S01Parser::parse_datagram(char* datagram, size_t datagram_length,
"received less fields than expected fields (actual: %zu, expected: %zu), ignoring scan", count, NUM_FIELDS);
ROS_WARN("are you using the correct node? (124 --> sick_tim310_1130000m01, 306 --> sick_tim551_2050001, 580 --> sick_tim310s01, 592 --> sick_tim310)");
// ROS_DEBUG("received message was: %s", datagram_copy);
return EXIT_FAILURE;
return ExitError;
}
else if (count > NUM_FIELDS)
{
ROS_WARN("received more fields than expected (actual: %zu, expected: %zu), ignoring scan", count, NUM_FIELDS);
ROS_WARN("are you using the correct node? (124 --> sick_tim310_1130000m01, 306 --> sick_tim551_2050001, 580 --> sick_tim310s01, 592 --> sick_tim310)");
// ROS_DEBUG("received message was: %s", datagram_copy);
return EXIT_FAILURE;
return ExitError;
}

// ----- read fields into msg
Expand Down Expand Up @@ -220,7 +220,7 @@ int SickTim310S01Parser::parse_datagram(char* datagram, size_t datagram_length,
// - add time offset (to account for USB latency etc.)
msg.header.stamp += ros::Duration().fromSec(config.time_offset);

return EXIT_SUCCESS;
return ExitSuccess;
}

} /* namespace sick_tim */
6 changes: 3 additions & 3 deletions src/sick_tim551_2050001.cpp
Expand Up @@ -76,7 +76,7 @@ int main(int argc, char **argv)

sick_tim::SickTimCommon* s = NULL;

int result = EXIT_FAILURE;
int result = sick_tim::ExitError;
while (ros::ok())
{
// Atempt to connect/reconnect
Expand All @@ -89,12 +89,12 @@ int main(int argc, char **argv)
s = new sick_tim::SickTimCommonUsb(parser);
result = s->init();

while(ros::ok() && (result == EXIT_SUCCESS)){
while(ros::ok() && (result == sick_tim::ExitSuccess)){
ros::spinOnce();
result = s->loopOnce();
}

if (result == EXIT_FATAL)
if (result == sick_tim::ExitFatal)
return result;

if (ros::ok() && !subscribe_datagram && !useTCP)
Expand Down
16 changes: 8 additions & 8 deletions src/sick_tim551_2050001_parser.cpp
Expand Up @@ -89,17 +89,17 @@ int SickTim5512050001Parser::parse_datagram(char* datagram, size_t datagram_leng
"received less fields than minimum fields (actual: %zu, minimum: %zu), ignoring scan", count, HEADER_FIELDS);
ROS_WARN("are you using the correct node? (124 --> sick_tim310_1130000m01, > 33 --> sick_tim551_2050001, 580 --> sick_tim310s01, 592 --> sick_tim310)");
// ROS_DEBUG("received message was: %s", datagram_copy);
return EXIT_FAILURE;
return ExitError;
}
if (strcmp(fields[15], "0"))
{
ROS_WARN("Field 15 of received data is not equal to 0 (%s). Unexpected data, ignoring scan", fields[15]);
return EXIT_FAILURE;
return ExitError;
}
if (strcmp(fields[20], "DIST1"))
{
ROS_WARN("Field 20 of received data is not equal to DIST1i (%s). Unexpected data, ignoring scan", fields[20]);
return EXIT_FAILURE;
return ExitError;
}

// More in depth checks: check data length and RSSI availability
Expand All @@ -110,12 +110,12 @@ int SickTim5512050001Parser::parse_datagram(char* datagram, size_t datagram_leng
if (number_of_data < 1 || number_of_data > 811)
{
ROS_WARN("Data length is outside acceptable range 1-811 (%d). Ignoring scan", number_of_data);
return EXIT_FAILURE;
return ExitError;
}
if (count < HEADER_FIELDS + number_of_data)
{
ROS_WARN("Less fields than expected for %d data points (%zu). Ignoring scan", number_of_data, count);
return EXIT_FAILURE;
return ExitError;
}
ROS_DEBUG("Number of data: %d", number_of_data);

Expand All @@ -133,15 +133,15 @@ int SickTim5512050001Parser::parse_datagram(char* datagram, size_t datagram_leng
if (number_of_rssi_data != number_of_data)
{
ROS_WARN("Number of RSSI data is lower than number of range data (%d vs %d", number_of_data, number_of_rssi_data);
return EXIT_FAILURE;
return ExitError;
}

// Check if the total length is still appropriate.
// RSSI data size = number of RSSI readings + 6 fields describing the data
if (count < HEADER_FIELDS + number_of_data + number_of_rssi_data + 6)
{
ROS_WARN("Less fields than expected for %d data points (%zu). Ignoring scan", number_of_data, count);
return EXIT_FAILURE;
return ExitError;
}

if (strcmp(fields[rssi_idx + 1], "RSSI1"))
Expand Down Expand Up @@ -304,7 +304,7 @@ int SickTim5512050001Parser::parse_datagram(char* datagram, size_t datagram_leng
expected_time_increment, msg.time_increment);
}

return EXIT_SUCCESS;
return ExitSuccess;
}

void SickTim5512050001Parser::set_range_min(float min)
Expand Down
16 changes: 8 additions & 8 deletions src/sick_tim_common.cpp
Expand Up @@ -152,7 +152,7 @@ int SickTimCommon::init_scanner()
diagnostics_.setHardwareID(identStr + " " + serialStr);

if (!isCompatibleDevice(identStr))
return EXIT_FATAL;
return ExitFatal;

/*
* Read the SOPAS variable 'FirmwareVersion' by name.
Expand All @@ -174,10 +174,10 @@ int SickTimCommon::init_scanner()
{
ROS_ERROR("SOPAS - Error starting to stream 'LMDscandata'.");
diagnostics_.broadcast(diagnostic_msgs::DiagnosticStatus::ERROR, "SOPAS - Error starting to stream 'LMDscandata'.");
return EXIT_FAILURE;
return ExitError;
}

return EXIT_SUCCESS;
return ExitSuccess;
}

bool sick_tim::SickTimCommon::isCompatibleDevice(const std::string identStr) const
Expand Down Expand Up @@ -213,14 +213,14 @@ int SickTimCommon::loopOnce()
{
ROS_ERROR("Read Error when getting datagram: %i.", result);
diagnostics_.broadcast(diagnostic_msgs::DiagnosticStatus::ERROR, "Read Error when getting datagram.");
return EXIT_FAILURE; // return failure to exit node
return ExitError; // return failure to exit node
}
if(actual_length <= 0)
return EXIT_SUCCESS; // return success to continue looping
return ExitSuccess; // return success to continue looping

// ----- if requested, skip frames
if (iteration_count++ % (config_.skip + 1) != 0)
return EXIT_SUCCESS;
return ExitSuccess;

if (publish_datagram_)
{
Expand All @@ -242,12 +242,12 @@ int SickTimCommon::loopOnce()
*dend = '\0';
dstart++;
int success = parser_->parse_datagram(dstart, dlength, config_, msg);
if (success == EXIT_SUCCESS)
if (success == ExitSuccess)
diagnosticPub_->publish(msg);
buffer_pos = dend + 1;
}

return EXIT_SUCCESS; // return success to continue looping
return ExitSuccess; // return success to continue looping
}

void SickTimCommon::check_angle_range(SickTimConfig &conf)
Expand Down
12 changes: 6 additions & 6 deletions src/sick_tim_common_mockup.cpp
Expand Up @@ -59,7 +59,7 @@ int SickTimCommonMockup::close_device()
int SickTimCommonMockup::sendSOPASCommand(const char* request, std::vector<unsigned char> * reply)
{
ROS_ERROR("Mockup - sendSOPASCommand(), this should never be called");
return EXIT_FAILURE;
return ExitError;
}

/*
Expand All @@ -68,7 +68,7 @@ int SickTimCommonMockup::sendSOPASCommand(const char* request, std::vector<unsig
int SickTimCommonMockup::init_device()
{
ROS_INFO("Mockup - init_device()");
return EXIT_SUCCESS;
return ExitSuccess;
}

/*
Expand All @@ -77,7 +77,7 @@ int SickTimCommonMockup::init_device()
int SickTimCommonMockup::init_scanner()
{
ROS_INFO("Mockup - init_scanner()");
return EXIT_SUCCESS;
return ExitSuccess;
}

int SickTimCommonMockup::get_datagram(unsigned char* receiveBuffer, int bufferSize, int* actual_length)
Expand All @@ -88,7 +88,7 @@ int SickTimCommonMockup::get_datagram(unsigned char* receiveBuffer, int bufferSi
while(!datagram_msg_)
{
if (!ros::ok())
return EXIT_FAILURE;
return ExitError;

ros::Duration(0.01).sleep();
ros::spinOnce();
Expand All @@ -103,12 +103,12 @@ int SickTimCommonMockup::get_datagram(unsigned char* receiveBuffer, int bufferSi
if (bufferSize < *actual_length + 1)
{
ROS_ERROR("Mockup - Buffer too small!");
return EXIT_FAILURE;
return ExitError;
}

strncpy(reinterpret_cast<char *>(receiveBuffer), &str[0], *actual_length + 1);

return EXIT_SUCCESS;
return ExitSuccess;
}

void SickTimCommonMockup::datagramCB(const std_msgs::String::ConstPtr &msg)
Expand Down

0 comments on commit 4356913

Please sign in to comment.