Problems (#1) - cwiid_open within thread = Socket connect error (#53) - Message List

cwiid_open within thread = Socket connect error
 unsolved

I have a simple C++ wrapper class for libcwiid which works great. At this point, I'm trying to encapsulate the cwiid_open within a thread to avoid the block on connection. In the code below, if I call connect() it works fine (but blocks of course), but if I call open() it dies on a socket error:

Put Wiimote in discoverable mode now (press 1+2)... Socket connect error (control channel) Segmentation fault (core dumped)

So my question is am doing something wrong in trying to connect within a thread? Mabey its better just to encapsulate the entire wiimote instance in a thread. My initial idea is just to listen to each wiimote in a main program listening loop and call the cwiid_open functions within thread so as not to block the loop.

/* open first available wiimote

returns 0 on success and -1 on failure */

int Wiimote::open() {

// create thread if(pthread_create(&connect_thread, &attr, Wiimote::connectThread, this) < 0) {

cout << "Wiimote connect thread failed" << endl; return -1; // thread creation failed

}

//return connect(); return 0;

}

// connect int Wiimote::connect() {

cout << "Put Wiimote in discoverable mode now (press 1+2)..." << endl;

if(!(wiimote = cwiid_open(&bdaddr, CWIID_FLAG_REPEAT_BTN))) {

cerr << "Unable to connect to wiimote" << endl; return -1;

}

// set wiimote report mode toggle_bit(rpt_mode, CWIID_RPT_IR);

cwiid_set_rpt_mode(wiimote, rpt_mode);

toggle_bit(rpt_mode, CWIID_RPT_BTN); cwiid_set_rpt_mode(wiimote, rpt_mode);

toggle_bit(rpt_mode, CWIID_RPT_ACC); cwiid_set_rpt_mode(wiimote, rpt_mode);

// get acc calibration cwiid_get_acc_cal(wiimote, CWIID_EXT_NONE, &wm_cal);

connected = true; // ready

cout << "Wiimote connected" << endl;

return 0;

}

// connect thread void *Wiimote::connectThread(void *arg) {

// void pointer cast Wiimote *wii = (Wiimote*) wii;

// connect wii->connect();

return 0;

}

Attachments

No attachments created.