Dependencies:   ros_lib_kinetic

HLComms.cpp

Committer:
WD40andTape
Date:
2018-08-29
Revision:
13:a373dfc57b89
Parent:
12:595ed862e52f
Child:
14:54c3759e76ed

File content as of revision 13:a373dfc57b89:

// HLComms.cpp

// Error codes: https://github.com/ARMmbed/mbed-os/blob/master/features/netsocket/nsapi_types.h
// http://man7.org/linux/man-pages/man3/errno.3.html

#include "HLComms.h"

HLComms::HLComms(short int port) { // Constructor
    _port = port;
}

int HLComms::setup_server(void) {
    int error_code;
    if(IS_PRINT_OUTPUT) printf("Starting TCP server...\n\r");
    if( !IS_DHCP ) {
        const char* ip = "192.168.1.125"; //"10.101.81.135"; //"10.101.79.125"; // Get first 3 parts from "ifconfig" on PC
        const char* mask = "255.255.255.0"; // Get from "ifconfig" on PC
        const char* gateway =  "10.101.81.1"; //"10.101.79.1"; // Get from "route -n" on PC
        error_code = interfaces.eth.set_network(ip, mask, gateway); // Dont use DHCP
        if( error_code < 0 ) {
            if(IS_PRINT_OUTPUT) printf("Error %i. Could not network interface to use a static IP address. "
                "Perhaps the network is already connected?\n\r", error_code);
            return -1;
        }
    }
    error_code = interfaces.eth.connect();
    if( error_code < 0 ) {
        if(IS_PRINT_OUTPUT) printf("Error %i. Could not start the Ethernet interface.\n\r", error_code);
        return -1;
    }
    const char* server_ip = interfaces.eth.get_ip_address();
    if( server_ip == NULL ) {
       error_code = -1;
        if(IS_PRINT_OUTPUT) printf("Error %i. Ethernet interface is not yet connected.\n\r", error_code);
        return -1;
    }
    if(IS_PRINT_OUTPUT) printf("The target IP address is '%s'\n\r", server_ip);
    error_code = interfaces.srv.open(&interfaces.eth); // Open the server on ethernet stack
    if( error_code < 0 ) {
        if(IS_PRINT_OUTPUT) printf("Error %i. Could not open server socket.\n\r", error_code);
        return -1;
    }
    error_code = interfaces.srv.bind(interfaces.eth.get_ip_address(), _port); // Bind the HTTP port (TCP 80) to the server
    if( error_code < 0 ) {
        if(IS_PRINT_OUTPUT) printf("Error %i. Could not bind server socket to port '%d'.\n\r", error_code, _port);
        return -1;
    }
    error_code = interfaces.srv.listen(1); // Listen for 1 simultaneous connection
    if( error_code < 0 ) {
        if(IS_PRINT_OUTPUT) printf("Error %i. Could not put server socket into listening mode.\n\r", error_code);
        return -1;
    }
    return 0;
}


int HLComms::accept_connection(void) {
    if(IS_PRINT_OUTPUT) printf("Waiting to accept a connection on the TCP socket.\n\r");
    int error_code;
    error_code = interfaces.srv.accept(&interfaces.clt_sock, &interfaces.clt_addr); // Blocks until data is sent
    if( error_code < 0 ) {
        if(IS_PRINT_OUTPUT) printf("Error %i. Could not create a network socket using the specified socket instance. "
            "Perhaps the socket is set to non-blocking or timed out?\n\r", error_code);
        return -1;
    }
    if(IS_PRINT_OUTPUT) printf("Accepted %s:%d\n\r", interfaces.clt_addr.get_ip_address(), interfaces.clt_addr.get_port());
    return 0;
}


void HLComms::close_server(void) {
    if(IS_PRINT_OUTPUT) printf("Closing server...\n\r");
    interfaces.clt_sock.close();
    interfaces.srv.close();
    interfaces.eth.disconnect();
    return;
}


int HLComms::receive_message(void) {
    memset(recv_buffer, 0, sizeof(recv_buffer));
    int error_code = interfaces.clt_sock.recv(recv_buffer, 1024); // Blocks until data is received
    if(IS_PRINT_OUTPUT) printf("Message received.\r\n");
    return error_code;
}


msg_format HLComms::process_message(void) { //HLComms::msg_format
    unsigned char * msg = recv_buffer;
    // Break message string into chunks at each comma
    vector<string> tokens;
    char * delim = ",";
    char * token = strtok((char *)msg, delim);
    while(token != NULL) {
        tokens.push_back(string(token));
        //if(IS_PRINT_OUTPUT) printf("%s\n\r",token);
        token = strtok(NULL, delim);
    }
    // Cast into doubles and assign to variables
    struct msg_format input;
    stringstream(tokens[0]) >> input.psi[0][0];
    stringstream(tokens[1]) >> input.psi[0][1];
    stringstream(tokens[2]) >> input.psi[0][2];
    stringstream(tokens[3]) >> input.psi[1][0];
    stringstream(tokens[4]) >> input.psi[1][1];
    stringstream(tokens[5]) >> input.psi[1][2];
    stringstream(tokens[6]) >> input.psi[2][0];
    stringstream(tokens[7]) >> input.psi[2][1];
    stringstream(tokens[8]) >> input.psi[2][2];
    stringstream(tokens[9]) >> input.duration;
    
    if(input.psi[0][0] < 0.0) input.psi[0][0] = 0.0;
    if(input.psi[0][1] < 0.0) input.psi[0][1] = 0.0;
    if(input.psi[0][2] < 0.0) input.psi[0][2] = 0.0;
    if(input.psi[1][0] < 0.0) input.psi[1][0] = 0.0;
    if(input.psi[1][1] < 0.0) input.psi[1][1] = 0.0;
    if(input.psi[1][2] < 0.0) input.psi[1][2] = 0.0;
    if(input.psi[2][0] < 0.0) input.psi[2][0] = 0.0;
    if(input.psi[2][1] < 0.0) input.psi[2][1] = 0.0;
    if(input.psi[2][2] < 0.0) input.psi[2][2] = 0.0;
    if(input.duration < 0.0) input.duration = 0.0;
    /*if( input.psi[2][0] > 0.14 ) {
        if(IS_PRINT_OUTPUT) printf("Hi\n\r");
    }*/
    //throw std::invalid_argument( "received negative value" );
    //return 0;
    return input;
} // End of consume_message()

void HLComms::make_message(double *dblTime) {
    memset(send_buffer, 0, sizeof(send_buffer));
    _snprintf(send_buffer,64,"%f",*dblTime);
}

int HLComms::send_message(void) {
    if(IS_PRINT_OUTPUT) printf("Sending message...\r\n");
    char * msg = send_buffer;
    int error_code = interfaces.clt_sock.send(msg, strlen(msg));
    if(IS_PRINT_OUTPUT) printf("Message sent.\r\n");
    return error_code;
}