Skip to content

Commit

Permalink
Refactores Error Translation, resolves #9
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-j-h committed May 8, 2017
1 parent 5f59ea8 commit c206bfa
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 625 deletions.
22 changes: 22 additions & 0 deletions libsweep/include/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef SWEEP_ERROR_1E0FA029CE94_HPP
#define SWEEP_ERROR_1E0FA029CE94_HPP

/*
* Internal base error sub-system errors inherit from.
* Implementation detail; not exported.
*/

#include <stdexcept>

namespace sweep {
namespace error {

struct error : std::runtime_error {
using base = std::runtime_error;
using base::base;
};

} // ns errors
} // ns sweep

#endif
37 changes: 18 additions & 19 deletions libsweep/include/protocol.h → libsweep/include/protocol.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#ifndef SWEEP_PROTOCOL_2EADE195E243_H
#define SWEEP_PROTOCOL_2EADE195E243_H
#ifndef SWEEP_PROTOCOL_2EADE195E243_HPP
#define SWEEP_PROTOCOL_2EADE195E243_HPP

/*
* Device communication protocol specifics.
* Implementation detail; not exported.
*/

#include <stdint.h>
#include "error.hpp"
#include "serial.hpp"

#include "serial.h"
#include "sweep.h"

#include <stdint.h>

namespace sweep {
namespace protocol {

typedef struct error* error_s;

const char* error_message(error_s error);
void error_destruct(error_s error);
struct error : sweep::error::error {
using base = sweep::error::error;
using base::base;
};

// Command Symbols

Expand Down Expand Up @@ -166,24 +168,21 @@ static_assert(sizeof(response_info_sample_rate_s) == 5, "response info sample ra

// Read and write specific packets

void write_command(sweep::serial::device_s serial, const uint8_t cmd[2], error_s* error);
void write_command(sweep::serial::device_s serial, const uint8_t cmd[2]);

void write_command_with_arguments(sweep::serial::device_s serial, const uint8_t cmd[2], const uint8_t arg[2], error_s* error);
void write_command_with_arguments(sweep::serial::device_s serial, const uint8_t cmd[2], const uint8_t arg[2]);

void read_response_header(sweep::serial::device_s serial, const uint8_t cmd[2], response_header_s* header, error_s* error);
void read_response_header(sweep::serial::device_s serial, const uint8_t cmd[2], response_header_s* header);

void read_response_param(sweep::serial::device_s serial, const uint8_t cmd[2], response_param_s* param, error_s* error);
void read_response_param(sweep::serial::device_s serial, const uint8_t cmd[2], response_param_s* param);

void read_response_scan(sweep::serial::device_s serial, response_scan_packet_s* scan, error_s* error);
void read_response_scan(sweep::serial::device_s serial, response_scan_packet_s* scan);

void read_response_info_motor_ready(sweep::serial::device_s serial, const uint8_t cmd[2], response_info_motor_ready_s* info,
error_s* error);
void read_response_info_motor_ready(sweep::serial::device_s serial, const uint8_t cmd[2], response_info_motor_ready_s* info);

void read_response_info_motor_speed(sweep::serial::device_s serial, const uint8_t cmd[2], response_info_motor_speed_s* info,
error_s* error);
void read_response_info_motor_speed(sweep::serial::device_s serial, const uint8_t cmd[2], response_info_motor_speed_s* info);

void read_response_info_sample_rate(sweep::serial::device_s serial, const uint8_t cmd[2], response_info_sample_rate_s* info,
error_s* error);
void read_response_info_sample_rate(sweep::serial::device_s serial, const uint8_t cmd[2], response_info_sample_rate_s* info);

// Some protocol conversion utilities
inline float u16_to_f32(uint16_t v) { return ((float)(v >> 4u)) + (v & 15u) / 16.0f; }
Expand Down
4 changes: 2 additions & 2 deletions libsweep/include/queue.h → libsweep/include/queue.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef SWEEP_QUEUE_62C8F42E8DD5_H
#define SWEEP_QUEUE_62C8F42E8DD5_H
#ifndef SWEEP_QUEUE_62C8F42E8DD5_HPP
#define SWEEP_QUEUE_62C8F42E8DD5_HPP

/*
* Thread-safe queue.
Expand Down
32 changes: 0 additions & 32 deletions libsweep/include/serial.h

This file was deleted.

35 changes: 35 additions & 0 deletions libsweep/include/serial.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef SWEEP_SERIAL_575F0FB571D1_HPP
#define SWEEP_SERIAL_575F0FB571D1_HPP

/*
* Communication with serial devices.
* Implementation detail; not exported.
*/

#include "error.hpp"

#include "sweep.h"

#include <stdint.h>

namespace sweep {
namespace serial {

struct error : sweep::error::error {
using base = sweep::error::error;
using base::base;
};

typedef struct device* device_s;

device_s device_construct(const char* port, int32_t bitrate);
void device_destruct(device_s serial);

void device_read(device_s serial, void* to, int32_t len);
void device_write(device_s serial, const void* from, int32_t len);
void device_flush(device_s serial);

} // ns serial
} // ns sweep

#endif
9 changes: 4 additions & 5 deletions libsweep/src/dummy.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "sweep.h"

#include <chrono>
#include <string>
#include <thread>

int32_t sweep_get_version(void) { return SWEEP_VERSION; }
bool sweep_is_abi_compatible(void) { return sweep_get_version() >> 16u == SWEEP_VERSION_MAJOR; }

typedef struct sweep_error {
const char* what; // always literal, do not deallocate
} sweep_error;
typedef struct sweep_error { std::string what; } sweep_error;

typedef struct sweep_device {
bool is_scanning;
Expand All @@ -25,7 +24,7 @@ typedef struct sweep_scan {
const char* sweep_error_message(sweep_error_s error) {
SWEEP_ASSERT(error);

return error->what;
return error->what.c_str();
}

void sweep_error_destruct(sweep_error_s error) {
Expand Down Expand Up @@ -251,4 +250,4 @@ void sweep_device_attempt_set_motor_speed(sweep_device_s device, int32_t hz, swe
(void)error;

device->motor_speed = hz;
}
}
Loading

0 comments on commit c206bfa

Please sign in to comment.