Websocket++ websocket server class member function not recognised from ISR

194 Views Asked by At

I'm using an Odroid (Raspi-like ARM Board) to run a small SPI-based radio chip that sends data to, among other things, a Websocket. This is applied using Websocket++: https://github.com/zaphoyd/websocketpp. I've bastardised one of the simple examples, which sends a server message to all clients. The program has a count_server class which handles the websocket, but also has an ISR handled by WiringPi which calls nested functions to handle different operations.

The issue I've got is that in order to send this message, the sending function must be in the count_server class as far as I can tell, to access client addresses etc. This class method is not accesible from inside my ISR, which handles all data received from the radio, so when I try to send a websocket message from inside the ISR I get the error:

error: 'webSocketServer' was not declared in this scope webSocketServer.sendLiveData();

The webSocketServer is an instance of the class count_server, instantiated in main(). Why can't the ISR 'see' the webSocketServer class.

One workaround is to poll inside the count() function, but this blocks the CPU and I'd rather leave it ready to perform other tasks.]

Here's the simplest example I could produce. Requires Websocket++ and WiringPi for the attachInterrupt.

#include <cstdlib>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <string>
#include <RF24/RF24.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Websocket++ includes
#include <mutex>
#include <set>
#include <thread>

#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
//*/


//websocket++ declarations

typedef websocketpp::server<websocketpp::config::asio> server;

using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;

class count_server {
public:
    count_server() : m_count(0) {
        m_server.init_asio();

        m_server.set_open_handler(bind(&count_server::on_open,this,_1));
        m_server.set_close_handler(bind(&count_server::on_close,this,_1));
    }

    void on_open(connection_hdl hdl) {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_connections.insert(hdl);
    }

    void on_close(connection_hdl hdl) {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_connections.erase(hdl);
    }

        void sendLiveData(){
        std::stringstream ss;
        ss << "foobar";
        for (auto it : m_connections) {
                 m_server.send(it,ss.str(),websocketpp::frame::opcode::text);
            }

    }

    void count() {//simple loop thread, most likely not needed, but in working example so lef
t for time being
        while (1) {
            sleep(1000);
                }
        }
    void run(uint16_t port) {
        m_server.listen(port);
        m_server.start_accept();
        m_server.run();
    }
private:
    typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;

    int m_count;
    server m_server;
    con_list m_connections;
    std::mutex m_mutex;
};

/****************** Raspberry Pi ***********************/


int interruptPin = 6; // GPIO pin for interrupts - interrupts have been edited to be handled
//by wiringPi, so #6 is used, not #103, check RF24/utility/SPIDEV/interrupt.c for info
int i=0;
/**************************************************************/

void addLiveData(){ //Live data buffer handler + calls sending function when buffer hits max
webSocketServer.sendLiveData();
}

void intHandler(){//when radio chip IRQ goes Low, something happened, this handles it
                addLiveData();//recvd = 2;//flag that the data is live data
}

int main(){

    attachInterrupt(interruptPin, INT_EDGE_FALLING, &intHandler); //Attach interrupt to bcm p
in 23
    count_server webSocketServer;
    std::thread t(std::bind(&count_server::count,&webSocketServer));
    webSocketServer.sendLiveData();
    webSocketServer.run(8080);
}
'
0

There are 0 best solutions below