Hi,
I was trying to encapsulate 4 different kinds of objects into one "master" object. But ended up with "Segmentation Fault" at hook (kernel code)
============================================ CODE
rmves.cpp
#include <iostream>
#include "rmves.h"
#include "rs232.h"
RS232_Controller *rs232;
// Attach an observer to the subject
// so that subsequent interrupts will trigger the db
void SubjectEx::Attach( Observer *o )
{
m_Observer->push_back(o);
}
// Detach an observer to the subject when
// it is no longer used
void SubjectEx:
etach( Observer *o )
{
m_Observer->remove(o);
}
// Signals all observers
void SubjectEx::Notify (void)
{
list<Observer *> i;
for (list<Observer *>::iterator iter = i.begin(); iter != i.end(); iter++)
{
i.front()->Update(this);
}
}
....
// Constructor for NOX
NOX::NOX(RS232_Controller *s)
{
m_oSubject = s;
m_oSubject->Attach(this); //<<<<<<<<<<<<<<<< Error!
}
// Destructor for NOX
NOX::~NOX()
{
m_oSubject->Detach(this);
}
void NOX::Update (SubjectEx *theChangedSubject)
{
}
....
int main (int argc, char *argv[])
{
bool res;
pthread_t tid;
rs232 = new RS232_Controller();
res = rs232->CreateThread();
rs232->Join(rs232->getThread(0));
return 0;
}
=================================================
rmves.h
#ifndef __RMVES__
#define __RMVES__
/******************************
Version 2.0
Purpose: define the most basic
type of objects
******************************/
#include <list>
//#include "rs232.h"
#define TRUE 1
#define FALSE 0
#define CARRIAGE_RETURN 13
#define NO_ITERATIONS 100
#define BLACK 0
#define WHITE 255
using namespace std;
class SubjectEx;
class RS232_Controller;
//=======================================
// Class: Observer
// Purpose: Updating Subject
// after receiving signals
// from the sensors
//=======================================
class Observer
{
public:
virtual ~Observer() { }
virtual void Update (SubjectEx *theChangedSubject) = 0;
protected:
Observer() { }
};
//=======================================
// Class: Subject
// Purpose: Monitoring the RS232
//=======================================
class SubjectEx {
public:
virtual ~SubjectEx() { }
virtual void Attach (Observer *);
virtual void Detach (Observer *);
virtual void Notify (void);
protected:
SubjectEx() { }
private:
list<Observer *>*m_Observer;
};
//=======================================
// Class: NOX
// Purpose: class that represent the NOX
// gas reading
//=======================================
class NOX : public Observer {
public:
NOX(RS232_Controller *);
~NOX();
virtual void Update (SubjectEx *);
// Get/Set functions
void SetNOX (double _NOX) { m_fNOXReading = _NOX; }
double GetNOX() { return m_fNOXReading; }
private:
double m_fNOXReading;
RS232_Controller *m_oSubject;
};
======================================================
rs232.h
#ifndef __RS232__
#define __RS232__
/**************************
Version 2.0
Purpose: Defines RS232
Controller
***************************/
#include "rmves.h"
#define MAX_THREADS 10
//=========================================
// Class: RS232_Controller
// Purpose: Controlling the RS232 interface
//=========================================
class RS232_Controller : public SubjectEx
{
public:
bool CreateThread(void);
// Join the thread pool
void Join (pthread_t pthread);
// Thunk that wraps C Thread Objects
void *PortMonitorThread(void*);
pthread_t getThread (int index) { return m_Threads[index]; }
public:
RS232_Controller() { m_oNOX = new NOX();
m_oHC = new HC();
m_CO = new CO();
m_CO2 = new CO2(); }
~RS232_Controller() { }
private:
NOX *m_oNOX; //(changed) <<<<<<<<<<<< Encapsulation
HC *m_oHC; // (changed)
CO *m_oCO; // (changed)
CO2 *m_oCO2; // (changed)
private:
pthread_t m_Threads[MAX_THREADS];
int m_iNumThreadsRunning;
};
#endif
===================================================
And this part...
void *RS232_Controller::PortMonitorThread (void *a)
{
struct termios oldtio, newtio;
struct sigaction saio;
unsigned char *buff;
size_t bytesread, totalbytesread;
WebCam *webcam1;
MyPacket *packet;
PlumeData *plumedata;
fileHandle = open (LINUX_SERIAL_DEVICE, O_RDWR | O_NOCTTY); // | O_NONBLOCKq);
if (fileHandle == 0)
{
fprintf (stderr, "COM1 is not connected\n");
exit(-1);
}
saio.sa_handler = signal_handler_IO;
//saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction (SIGIO, &saio, NULL);
fcntl ( fileHandle, F_SETOWN, getpid() );
// set to the file for nonblocking read
fcntl ( fileHandle, F_SETFL, FASYNC );
// clear attributes
bzero (&newtio, sizeof(newtio));
// save old attributes
tcgetattr ( fileHandle, &oldtio );
// control flags
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
// input Flag
newtio.c_lflag = IGNPAR | ICRNL;
// output flag
newtio.c_oflag = 0;
newtio.c_lflag = ISIG;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
tcflush ( fileHandle, TCIFLUSH );
tcsetattr ( fileHandle, TCSANOW, &newtio );
buff = (unsigned char *) malloc (65535);
memset ( buff, 0, 65535 );
webcam1 = new WebCam("192.168.0.124");
while (STOP == false)
{
packet = new MyPacket(16);
// chk for rece buff first
do {
bytesread = 0;
printf ("packet->GetPacketSize() = %d\n", packet->GetPacketSize());
bytesread = read ( fileHandle, buff, packet->GetPacketSize()); // <<<<<<<<<<<<<<<<<<<< Can't get thru this blocking read
printf ("bytesread = %d\n", bytesread);
totalbytesread += bytesread;
} while (totalbytesread != strlen ((const char *)buff));
===================================================
Thanks
Jack