From 6666229d83111f8878ce5134f288115afb47f1c3 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 6 May 2013 13:25:49 -0700 Subject: [PATCH 1/4] close the serial file descriptor in destructor --- interface/src/SerialInterface.cpp | 40 +++++++++++++++---------------- interface/src/SerialInterface.h | 11 +++++++-- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/interface/src/SerialInterface.cpp b/interface/src/SerialInterface.cpp index 5878ab672e..87e79d8f5a 100644 --- a/interface/src/SerialInterface.cpp +++ b/interface/src/SerialInterface.cpp @@ -22,7 +22,6 @@ #include #endif -int serialFd; const int MAX_BUFFER = 255; char serialBuffer[MAX_BUFFER]; int serialBufferPos = 0; @@ -34,6 +33,10 @@ const int GRAVITY_SAMPLES = 200; // Use the first samples to const bool USING_INVENSENSE_MPU9150 = 1; +SerialInterface::~SerialInterface() { + close(_serialDescriptor); +} + void SerialInterface::pair() { #ifdef __APPLE__ @@ -65,18 +68,19 @@ void SerialInterface::pair() { } // connect to the serial port -int SerialInterface::initializePort(char* portname, int baud) { +void SerialInterface::initializePort(char* portname, int baud) { #ifdef __APPLE__ - serialFd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY); + _serialDescriptor = open(portname, O_RDWR | O_NOCTTY | O_NDELAY); printLog("Opening SerialUSB %s: ", portname); - if (serialFd == -1) { + if (_serialDescriptor == -1) { printLog("Failed.\n"); - return -1; // Failed to open port + _failedOpenAttempts++; + return; } struct termios options; - tcgetattr(serialFd,&options); + tcgetattr(_serialDescriptor, &options); switch(baud) { case 9600: cfsetispeed(&options,B9600); @@ -101,26 +105,26 @@ int SerialInterface::initializePort(char* portname, int baud) { options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; - tcsetattr(serialFd,TCSANOW,&options); + tcsetattr(_serialDescriptor, TCSANOW, &options); if (USING_INVENSENSE_MPU9150) { // block on invensense reads until there is data to read - int currentFlags = fcntl(serialFd, F_GETFL); - fcntl(serialFd, F_SETFL, currentFlags & ~O_NONBLOCK); + int currentFlags = fcntl(_serialDescriptor, F_GETFL); + fcntl(_serialDescriptor, F_SETFL, currentFlags & ~O_NONBLOCK); // there are extra commands to send to the invensense when it fires up // this takes it out of SLEEP - write(serialFd, "WR686B01\n", 9); + write(_serialDescriptor, "WR686B01\n", 9); // delay after the wakeup usleep(10000); // this disables streaming so there's no garbage data on reads - write(serialFd, "SD\n", 3); + write(_serialDescriptor, "SD\n", 3); // flush whatever was produced by the last two commands - tcflush(serialFd, TCIOFLUSH); + tcflush(_serialDescriptor, TCIOFLUSH); } printLog("Connected.\n"); @@ -128,8 +132,6 @@ int SerialInterface::initializePort(char* portname, int baud) { active = true; #endif - - return 0; } // Reset Trailing averages to the current measurement @@ -143,9 +145,7 @@ void SerialInterface::renderLevels(int width, int height) { int disp_x = 10; const int GAP = 16; char val[10]; - for(i = 0; i < NUM_CHANNELS; i++) - { - + for(i = 0; i < NUM_CHANNELS; i++) { // Actual value glLineWidth(2.0); glColor4f(1, 1, 1, 1); @@ -202,8 +202,8 @@ void SerialInterface::readData() { unsigned char gyroBuffer[20]; // ask the invensense for raw gyro data - write(serialFd, "RD684306\n", 9); - read(serialFd, gyroBuffer, 20); + write(_serialDescriptor, "RD684306\n", 9); + read(_serialDescriptor, gyroBuffer, 20); convertHexToInt(gyroBuffer + 6, _lastYaw); convertHexToInt(gyroBuffer + 10, _lastRoll); @@ -216,7 +216,7 @@ void SerialInterface::readData() { const float AVG_RATE[] = {0.002, 0.002, 0.002, 0.002, 0.002, 0.002}; char bufchar[1]; - while (read(serialFd, &bufchar, 1) > 0) { + while (read(_serialDescriptor, &bufchar, 1) > 0) { serialBuffer[serialBufferPos] = bufchar[0]; serialBufferPos++; // Have we reached end of a line of input? diff --git a/interface/src/SerialInterface.h b/interface/src/SerialInterface.h index 9558a1601f..05148c1420 100644 --- a/interface/src/SerialInterface.h +++ b/interface/src/SerialInterface.h @@ -36,7 +36,11 @@ extern const bool USING_INVENSENSE_MPU9150; class SerialInterface { public: - SerialInterface() { active = false; }; + + SerialInterface() : active(false), + _failedOpenAttempts(0) {} + ~SerialInterface(); + void pair(); void readData(); @@ -56,8 +60,10 @@ public: glm::vec3 getGravity() {return gravity;}; private: - int initializePort(char * portname, int baud); + void initializePort(char* portname, int baud); void resetSerial(); + + int _serialDescriptor; int lastMeasured[NUM_CHANNELS]; float trailingAverage[NUM_CHANNELS]; int samplesAveraged; @@ -68,6 +74,7 @@ private: int _lastYaw; int _lastPitch; int _lastRoll; + int _failedOpenAttempts; }; #endif From 969bf7645a7c6b71884257079ef39009e0a970ee Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 6 May 2013 13:30:10 -0700 Subject: [PATCH 2/4] remove an accidentally added g --- domain-server/src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/domain-server/src/main.cpp b/domain-server/src/main.cpp index 2ded57b9ae..705bc317b2 100644 --- a/domain-server/src/main.cpp +++ b/domain-server/src/main.cpp @@ -100,7 +100,7 @@ int main(int argc, const char * argv[]) std::map newestSoloAgents; agentType = packetData[1]; - unpackSocket(&packetData[2], (sockaddr*) g&agentLocalAddress); + unpackSocket(&packetData[2], (sockaddr*) &agentLocalAddress); // check the agent public address // if it matches our local address we're on the same box From ab6a9285ad511ecccfcc838ec763040652f7944b Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 6 May 2013 13:33:15 -0700 Subject: [PATCH 3/4] have eve reference new Agent timestamp variables --- eve/src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eve/src/main.cpp b/eve/src/main.cpp index 30de50c284..ad0081fc3b 100644 --- a/eve/src/main.cpp +++ b/eve/src/main.cpp @@ -49,7 +49,7 @@ void *receiveAgentData(void *args) { avatarMixer = agentList->soloAgentOfType(AGENT_TYPE_AVATAR_MIXER); if (avatarMixer != NULL) { - avatarMixer->setLastRecvTimeUsecs(usecTimestampNow()); + avatarMixer->setLastHeardMicrostamp(usecTimestampNow()); } break; From 784fa5882a9e48828f3ed4c2cb08cec9ec0814bf Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 6 May 2013 13:36:58 -0700 Subject: [PATCH 4/4] closing of serial interface should be conditional on __APPLE__ --- interface/src/SerialInterface.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interface/src/SerialInterface.cpp b/interface/src/SerialInterface.cpp index c7d539935f..cca18f76cc 100644 --- a/interface/src/SerialInterface.cpp +++ b/interface/src/SerialInterface.cpp @@ -34,7 +34,9 @@ const int GRAVITY_SAMPLES = 200; // Use the first samples to const bool USING_INVENSENSE_MPU9150 = 1; SerialInterface::~SerialInterface() { +#ifdef __APPLE__ close(_serialDescriptor); +#endif } void SerialInterface::pair() {