got everything compiling (should still work too) with aos
diff --git a/gyro_board/src/libusb-driver/Makefile b/gyro_board/src/libusb-driver/Makefile
deleted file mode 100644
index 707fa0c..0000000
--- a/gyro_board/src/libusb-driver/Makefile
+++ /dev/null
@@ -1,15 +0,0 @@
-SRCS = get.cc libusb_wrap.cc signal_handler.cc thread.cc
-CFLAGS = -I./ -I/usr/include/libusb-1.0 -g -std=gnu++0x
-LDFLAGS = -g -lgflags -lglog -lusb-1.0 -lboost_thread-mt -lrt
-
-get: $(SRCS:.cc=.o)
- g++ $^ -o $@ $(LDFLAGS)
-
-%.o: %.cc
- g++ -MMD -MP -c $(CFLAGS) $< -o $@
-
-clean:
- $(RM) $(SRCS:.cc=.o) $(SRCS:.cc=.d) get
-
-.PHONY: clean
--include $(SRCS:.cc=.d)
diff --git a/gyro_board/src/libusb-driver/get.cc b/gyro_board/src/libusb-driver/get.cc
index 5922958..f2ad2f8 100644
--- a/gyro_board/src/libusb-driver/get.cc
+++ b/gyro_board/src/libusb-driver/get.cc
@@ -1,15 +1,17 @@
#include "get.h"
-#include <google/gflags.h>
-#include <glog/logging.h>
-#include <iostream>
-#include <signal.h>
#include <stdio.h>
-#include <queue>
#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+
+#include <queue>
+#include <iostream>
#include <map>
-#include "signal_handler.h"
+#include <google/gflags.h>
+#include "aos/common/mutex.h"
+#include "aos/common/logging/logging_impl.h"
DEFINE_int32(vid, 0x1424, "Vendor ID of the device.");
DEFINE_int32(pid, 0xd243, "Product ID of the device.");
@@ -65,7 +67,7 @@
}
}
-class GyroDriver::PacketReceiver : public Thread {
+class GyroDriver::PacketReceiver : public ::aos::util::Thread {
public:
// refusing to send any more frames.
static const int kMaxRXFrames = 128;
@@ -74,7 +76,7 @@
: Thread(), dev_handle_(dev_handle), rx_queue_(new can_priority_queue) {}
// Serve.
- void operator()();
+ virtual void Run();
bool GetCanFrame(struct can_frame *msg);
@@ -84,16 +86,13 @@
can_frame_priority_comparator> can_priority_queue;
LibUSBDeviceHandle *dev_handle_;
- boost::mutex rx_mutex_;
+ ::aos::Mutex rx_mutex_;
std::unique_ptr<can_priority_queue> rx_queue_;
- //boost::condition_variable rx_cond_;
};
GyroDriver::GyroDriver(LibUSBDeviceHandle *dev_handle)
: dev_handle_(dev_handle), dbg_(new DbgReader(this)),
- debug_thread_(new boost::thread(boost::ref(*dbg_))),
- rx_(new GyroDriver::PacketReceiver(dev_handle)),
- rx_thread_(new boost::thread(boost::ref(*rx_))) { }
+ rx_(new GyroDriver::PacketReceiver(dev_handle)) {}
std::string GyroDriver::GetDebugData() {
@@ -106,15 +105,9 @@
return std::string(data, transferred);
}
-void GyroDriver::Terminate() {
- rx_->Terminate();
-}
-
GyroDriver::~GyroDriver() {
- rx_->Terminate();
- rx_thread_->join();
- dbg_->Terminate();
- debug_thread_->join();
+ rx_->Join();
+ dbg_->Join();
}
//bool GyroDriver::GetCanFrame(struct can_frame *msg) {
@@ -151,24 +144,29 @@
int8_t shooter_angle_rise_count;
} __attribute__((__packed__));
-void GyroDriver::PacketReceiver::operator()() {
- int r;
- int actual;
- uint8_t data[64];
+void GyroDriver::PacketReceiver::Run() {
+ int r;
+ int actual;
+ uint8_t data[64];
DataStruct *real_data;
- real_data = (DataStruct *)data;
+ static_assert(sizeof(*real_data) <= sizeof(data), "it doesn't fit");
+
+ uint8_t *data_pointer = data;
+ memcpy(&real_data, &data_pointer, sizeof(data_pointer));
- while (should_run()) {
+ while (should_continue()) {
r = dev_handle_->interrupt_transfer(
0x81, data, sizeof(data), &actual, 1000);
printf("size: %d\n",sizeof(DataStruct));
- CHECK_GT(actual, 0);
-
- if (r != 0) {
- LOG(ERROR) << "Read Error. Read " << actual;
+ if (actual <= 0) {
+ LOG(FATAL, "didn't get any data\n");
}
- printf("angle: %d\n", real_data->gyro_angle);
+ if (r != 0) {
+ LOG(ERROR, "Read Error. Read %d\n", actual);
+ }
+
+ printf("angle: %"PRId64"\n", real_data->gyro_angle);
printf("drivel: %d\n", real_data->left_drive);
printf("driver: %d\n", real_data->right_drive);
printf("shooter: %d\n", real_data->shooter);
@@ -197,33 +195,33 @@
//}
}
//rx_cond_.notify_all();
- LOG(INFO) << "Receive thread down.";
+ LOG(INFO, "Receive thread down.");
}
DbgReader::DbgReader(GyroDriver *gyro)
: Thread(), gyro_(gyro) {}
-void DbgReader::operator()() {
- unsigned char data[64];
- LOG(INFO) << "Running debug dump thread.";
- while (should_run()) {
+void DbgReader::Run() {
+ LOG(INFO, "Running debug dump thread.");
+ while (should_continue()) {
printf("%s", gyro_->GetDebugData().c_str());
}
- LOG(INFO) << "Exiting debug dump thread.";
+ LOG(INFO, "Exiting debug dump thread.");
}
int main(int argc, char ** argv) {
- int r;
- google::InitGoogleLogging(argv[0]);
google::ParseCommandLineFlags(&argc, &argv, true);
- google::InstallFailureSignalHandler();
+ ::aos::logging::Init();
LibUSB libusb;
{
std::unique_ptr<LibUSBDeviceHandle> dev_handle(
- CHECK_NOTNULL(libusb.FindDeviceWithVIDPID(FLAGS_vid, FLAGS_pid)));
+ libusb.FindDeviceWithVIDPID(FLAGS_vid, FLAGS_pid));
+ if (!dev_handle) {
+ LOG(FATAL, "couldn't find device\n");
+ }
GyroDriver gyro(dev_handle.release());
diff --git a/gyro_board/src/libusb-driver/get.h b/gyro_board/src/libusb-driver/get.h
index c0ed6a6..62462d3 100644
--- a/gyro_board/src/libusb-driver/get.h
+++ b/gyro_board/src/libusb-driver/get.h
@@ -3,11 +3,11 @@
#include <sys/socket.h>
#include <linux/can.h>
-#include <memory>
-#include <boost/thread/locks.hpp>
-#include <boost/thread.hpp>
-#include "thread.h"
+#include <memory>
+
+#include "aos/common/util/thread.h"
+
#include "libusb_wrap.h"
// Use a packed version of can_frame.
@@ -61,8 +61,6 @@
// Handle for the object run in the debug thread which prints out debug
// information.
std::unique_ptr<DbgReader> dbg_;
- // Thread handle for the debug thread.
- std::unique_ptr<boost::thread> debug_thread_;
// Handle for the transmit object which runs in the transmit thread and also
// handles priority queueing of packets.
@@ -73,16 +71,14 @@
// Handle for the rx object which runs in the rx thread and also
// handles priority queueing of packets.
std::unique_ptr<PacketReceiver> rx_;
- // Thread handle for the receive thread.
- std::unique_ptr<boost::thread> rx_thread_;
};
-class DbgReader : public Thread {
+class DbgReader : public ::aos::util::Thread {
public:
explicit DbgReader(GyroDriver *dev_handle);
// Serve.
- void operator()();
+ virtual void Run();
private:
GyroDriver *gyro_;
diff --git a/gyro_board/src/libusb-driver/libusb-driver.gyp b/gyro_board/src/libusb-driver/libusb-driver.gyp
new file mode 100644
index 0000000..1c50633
--- /dev/null
+++ b/gyro_board/src/libusb-driver/libusb-driver.gyp
@@ -0,0 +1,31 @@
+{
+ 'targets': [
+ {
+ 'target_name': 'get',
+ 'type': 'executable',
+ 'sources': [
+ 'get.cc',
+ ],
+ 'dependencies': [
+ '<(EXTERNALS):gflags',
+ '<(AOS)/common/util/util.gyp:thread',
+ 'libusb_wrap',
+ '<(AOS)/build/aos.gyp:logging',
+ ],
+ },
+ {
+ 'target_name': 'libusb_wrap',
+ 'type': 'static_library',
+ 'sources': [
+ 'libusb_wrap.cc',
+ ],
+ 'dependencies': [
+ '<(EXTERNALS):libusb',
+ '<(AOS)/build/aos.gyp:logging',
+ ],
+ 'export_dependent_settings': [
+ '<(EXTERNALS):libusb',
+ ],
+ },
+ ],
+}
diff --git a/gyro_board/src/libusb-driver/libusb_wrap.cc b/gyro_board/src/libusb-driver/libusb_wrap.cc
index 1f54998..d8161ab 100644
--- a/gyro_board/src/libusb-driver/libusb_wrap.cc
+++ b/gyro_board/src/libusb-driver/libusb_wrap.cc
@@ -1,12 +1,13 @@
-#include <libusb.h>
-#include <google/gflags.h>
-#include <glog/logging.h>
-#include <iostream>
-
#include "libusb_wrap.h"
+#include <iostream>
+
+#include "aos/common/logging/logging.h"
+
LibUSB::LibUSB() {
- CHECK_GE(libusb_init(&ctx_), 0) << ": LibUSB failed to initialize.";
+ if (libusb_init(&ctx_) < 0) {
+ LOG(FATAL, "libusb_init(%p) failed\n", &ctx_);
+ }
libusb_set_debug(ctx_, 3);
}
@@ -17,26 +18,30 @@
ssize_t cnt;
cnt = libusb_get_device_list(ctx_, &devs);
- if(cnt < 0) {
- LOG(ERROR) << "Get Device Error";
+ if (cnt < 0) {
+ LOG(ERROR, "Get Device Error\n");
return NULL;
}
- LOG(INFO) << cnt << " Devices in list.";
+ LOG(INFO, "%d Devices in list.\n", cnt);
bool found = false;
for (int i = 0; i < cnt; ++i) {
struct libusb_device_descriptor desc;
r = libusb_get_device_descriptor(devs[i], &desc);
- CHECK_GE(r, 0) << ": Couldn't get device descriptor, error " << r;
+ if (r < 0) {
+ LOG(FATAL, "lib_usb_get_device_descriptor(%p, %p) failed\n",
+ devs[i], &desc);
+ }
if (desc.idVendor == vid && desc.idProduct == pid) {
- LOG(INFO) << "Device " << (int)libusb_get_bus_number(devs[i]) << ":"
- << (int)libusb_get_device_address(devs[i]) << " matches";
+ LOG(INFO, "Device %d:%d matches\n",
+ (int)libusb_get_bus_number(devs[i]),
+ (int)libusb_get_device_address(devs[i]));
r = libusb_open(devs[i], &dev_handle);
if (libusb_kernel_driver_active(dev_handle, 0) == 1) {
- LOG(INFO) << "Device already in use, trying next one.";
+ LOG(INFO, "Device already in use, trying next one.");
continue;
}
if (r < 0) {
- LOG(WARNING) << "Failed to open device.";
+ LOG(WARNING, "Failed to open device.");
} else {
found = true;
break;
@@ -45,26 +50,26 @@
}
libusb_free_device_list(devs, 1);
if (!found) {
- LOG(ERROR) << "Couldn't open device.";
+ LOG(ERROR, "Couldn't open device.");
return NULL;
}
if (libusb_kernel_driver_active(dev_handle, 0) == 1) {
- LOG(INFO) << "Kernel Driver Active";
+ LOG(INFO, "Kernel Driver Active");
if (libusb_detach_kernel_driver(dev_handle, 0) == 0) {
- LOG(INFO) << "Kernel Driver Detached!";
+ LOG(INFO, "Kernel Driver Detached!");
} else {
- LOG(ERROR) << "Couldn't detach kernel driver.";
+ LOG(ERROR, "Couldn't detach kernel driver.");
return NULL;
}
}
r = libusb_claim_interface(dev_handle, 0);
if (r < 0) {
- LOG(ERROR) << "Cannot Claim Interface";
+ LOG(ERROR, "Cannot Claim Interface");
return 0;
}
- LOG(INFO) << "Claimed Interface";
+ LOG(INFO, "Claimed Interface");
return new LibUSBDeviceHandle(dev_handle);
}
@@ -79,9 +84,9 @@
int r;
r = libusb_release_interface(dev_handle_, 0);
if (r != 0) {
- LOG(FATAL) << "Cannot Release Interface";
+ LOG(FATAL, "Cannot Release Interface");
}
- LOG(INFO) << "Released Interface";
+ LOG(INFO, "Released Interface");
libusb_close(dev_handle_);
}
diff --git a/gyro_board/src/libusb-driver/libusb_wrap.h b/gyro_board/src/libusb-driver/libusb_wrap.h
index f87f235..074408d 100644
--- a/gyro_board/src/libusb-driver/libusb_wrap.h
+++ b/gyro_board/src/libusb-driver/libusb_wrap.h
@@ -1,7 +1,7 @@
#ifndef LIBUSB_H_
#define LIBUSB_H_
-#include <libusb.h>
+#include <libusb-1.0/libusb.h>
class LibUSBDeviceHandle;
diff --git a/gyro_board/src/libusb-driver/signal_handler.cc b/gyro_board/src/libusb-driver/signal_handler.cc
deleted file mode 100644
index ffb1b99..0000000
--- a/gyro_board/src/libusb-driver/signal_handler.cc
+++ /dev/null
@@ -1,87 +0,0 @@
-#include "signal_handler.h"
-
-#include <boost/thread/locks.hpp>
-#include <boost/thread.hpp>
-#include <boost/bind.hpp>
-#include <boost/function.hpp>
-#include <google/gflags.h>
-#include <glog/logging.h>
-#include <iostream>
-#include <signal.h>
-#include <stdio.h>
-#include <sys/socket.h>
-#include <linux/can.h>
-#include <memory>
-#include <queue>
-#include <unistd.h>
-
-#include "thread.h"
-
-// Boolean signaling if the handler has been set up or not.
-static bool pipe_has_been_initialized = false;
-
-// Pipe that the signal handler writes the signal number to
-static int handler_write_pipe;
-// Pipe that the handler thread reads the signal number from.
-static int thread_read_pipe;
-// Mutex to lock the signal handler dictionary.
-static boost::mutex signal_dict_lock;
-// Signal handler dictionary.
-static std::unique_ptr<std::map<int, boost::function<void(int)> > > signal_dict;
-
-// Thread which reads signal numbers from the pipe and handles them.
-class SignalHandlerThread: public Thread {
- public:
- void operator()() {
- while (true) {
- int signal_number;
- LOG(INFO) << "Back to waiting.";
- int size = read(thread_read_pipe, &signal_number, sizeof(int));
- if (size == -1) {
- continue;
- }
- CHECK_EQ(size, sizeof(int))
- << ": Read the wrong number of bytes when receiving a signal.";
- LOG(INFO) << "Got signal " << signal_number;
-
- boost::function<void(int)> function;
- if (GetFromMap(*signal_dict, signal_number, &function)) {
- function(signal_number);
- }
- }
- }
-};
-
-static std::unique_ptr<SignalHandlerThread> signal_handler_thread;
-static std::unique_ptr<boost::thread> signal_handler_thread_;
-
-// Simple signal handler which writes the signal number to the pipe.
-static void signal_handler(int signal_number) {
- CHECK_EQ(write(handler_write_pipe, &signal_number, sizeof(int)),
- sizeof(int));
-}
-
-void RegisterSignalHandler(int signal_number,
- boost::function<void(int)> function){
- if (!pipe_has_been_initialized) {
- int pipefd[2];
- CHECK(!pipe2(pipefd, 0))
- << ": Failed to create pipes for signal handler.";
- thread_read_pipe = pipefd[0];
- handler_write_pipe = pipefd[1];
- signal_handler_thread.reset(new SignalHandlerThread());
- signal_handler_thread_.reset(
- new boost::thread(boost::ref(*signal_handler_thread)));
- signal_dict.reset(new std::map<int, boost::function<void(int)> >());
- }
- struct sigaction new_action, old_action;
-
- new_action.sa_handler = &signal_handler;
- sigemptyset(&new_action.sa_mask);
-
- boost::lock_guard<boost::mutex> lockguard(signal_dict_lock);
-
- InsertIntoMap(signal_dict.get(), signal_number, function);
-
- sigaction(signal_number, &new_action, NULL);
-}
diff --git a/gyro_board/src/libusb-driver/signal_handler.h b/gyro_board/src/libusb-driver/signal_handler.h
deleted file mode 100644
index fbc8a37..0000000
--- a/gyro_board/src/libusb-driver/signal_handler.h
+++ /dev/null
@@ -1,39 +0,0 @@
-#ifndef SIGNAL_HANDLER_H_
-#define SIGNAL_HANDLER_H_
-
-#include <boost/function.hpp>
-#include <map>
-
-// TODO(aschuh): Template std::map as well.
-// Maps the key to the value, inserting it if it isn't there, or replacing it if
-// it is. Returns true if the key was added and false if it was replaced.
-template <typename K, typename V>
-bool InsertIntoMap(std::map<K, V> *my_map, const K &key, const V &new_value) {
- std::pair<typename std::map<K, V>::iterator, bool> element;
- element = my_map->insert(std::pair<K,V>(key, new_value));
- if (element.second == false) {
- element.first->second = new_value;
- }
- return element.second;
-}
-
-// Gets the value for the key from the map.
-// Returns true if the key was found and then populates value with the value.
-// Otherwise, leaves value alone and returns false.
-template <typename K, typename V>
-bool GetFromMap(const std::map<K, V> &my_map, const K &key, V *value) {
- typename std::map<K, V>::const_iterator element = my_map.find(key);
- if (element != my_map.end()) {
- *value = element->second;
- return true;
- }
- return false;
-}
-
-
-// Registers a signal handler to be run when we get the specified signal.
-// The handler will run in a thread, rather than in the signal's context.
-void RegisterSignalHandler(int signal_number,
- boost::function<void(int)> function);
-
-#endif // SIGNAL_HANDLER_H_
diff --git a/gyro_board/src/libusb-driver/thread.cc b/gyro_board/src/libusb-driver/thread.cc
deleted file mode 100644
index 0121f35..0000000
--- a/gyro_board/src/libusb-driver/thread.cc
+++ /dev/null
@@ -1,14 +0,0 @@
-#include "thread.h"
-
-#include <boost/bind.hpp>
-
-
-void Thread::Terminate() {
- boost::lock_guard<boost::mutex> lockguard(terminate_mutex_);
- should_terminate_ = true;
-}
-
-bool Thread::should_run() {
- boost::lock_guard<boost::mutex> lockguard(terminate_mutex_);
- return !should_terminate_;
-}
diff --git a/gyro_board/src/libusb-driver/thread.h b/gyro_board/src/libusb-driver/thread.h
deleted file mode 100644
index 1e94777..0000000
--- a/gyro_board/src/libusb-driver/thread.h
+++ /dev/null
@@ -1,29 +0,0 @@
-#ifndef THREAD_H_
-#define THREAD_H_
-
-#include <sys/socket.h>
-#include <linux/can.h>
-#include <boost/thread/locks.hpp>
-#include <boost/thread.hpp>
-
-class Thread {
- public:
- // Constructer initializes terminate to false.
- Thread() : should_terminate_(false) {}
-
- // Terminate the thread soon.
- void Terminate();
-
- protected:
- // Helper method to atomically read the should_terminate_ boolean.
- bool should_run();
-
- private:
- // Stores whether or not the thread has been asked to quit.
- // TODO(aschuh): This really should be a Notification...
- bool should_terminate_;
- // Mutex to protect should_terminate.
- boost::mutex terminate_mutex_;
-};
-
-#endif // THREAD_H_
diff --git a/gyro_board/src/usb/Makefile b/gyro_board/src/usb/Makefile
index 7b20a72..b91b6a5 100644
--- a/gyro_board/src/usb/Makefile
+++ b/gyro_board/src/usb/Makefile
@@ -58,7 +58,7 @@
@cd ../../bin; python serial_looper.py
assm.S: $(NAME).elf
- $(OBJDUMP) -D -S $(NAME).elf > assm.S
+ $(OBJDUMP) -D -S $(NAME).elf > $@
%.hex: %.elf Makefile
$(OBJCOPY) -O ihex $< $@