Dependencies: ros_lib_kinetic
Diff: HLComms.cpp
- Revision:
- 7:5b6a2cefbf3b
- Child:
- 9:cd3607ba5643
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HLComms.cpp Fri Aug 03 14:58:36 2018 +0000 @@ -0,0 +1,118 @@ +// 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; + printf("Starting TCP server...\n\r"); + if( !IS_DHCP ) { + const char* ip = "10.101.79.135"; //"10.101.81.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.79.1"; //"10.101.81.1"; // Get from "route -n" on PC + error_code = interfaces.eth.set_network(ip, mask, gateway); // Dont use DHCP + if( error_code < 0 ) { + 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 ) { + 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; + printf("Error %i. Ethernet interface is not yet connected.\n\r", error_code); + return -1; + } + 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 ) { + 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 ) { + 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 ) { + printf("Error %i. Could not put server socket into listening mode.\n\r", error_code); + return -1; + } + return 0; +} + + +int HLComms::accept_connection(void) { + 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 ) { + 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; + } + printf("Accepted %s:%d\n\r", interfaces.clt_addr.get_ip_address(), interfaces.clt_addr.get_port()); + return 0; +} + + +void HLComms::close_server(void) { + interfaces.clt_sock.close(); + interfaces.srv.close(); + interfaces.eth.disconnect(); + return; +} + + +msg_format HLComms::consume_message( unsigned char * msg ) { //HLComms::msg_format + // 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)); + //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 ) { + printf("Hi\n\r"); + }*/ + //throw std::invalid_argument( "received negative value" ); + //return 0; + return input; +} // End of consume_message() \ No newline at end of file