diff --git a/software/raspberry/superviseur-robot/lib/comrobot.cpp b/software/raspberry/superviseur-robot/lib/comrobot.cpp index 2a9118c..d9227b4 100644 --- a/software/raspberry/superviseur-robot/lib/comrobot.cpp +++ b/software/raspberry/superviseur-robot/lib/comrobot.cpp @@ -31,7 +31,7 @@ #include #include int sock = 0; -const char* host = "127.0.0.1"; +string host = "127.0.0.1"; #define PORT 6699 #endif @@ -96,7 +96,7 @@ int ComRobot::Open(string usart) { serv_addr.sin_port = htons(PORT); // Convert IPv4 and IPv6 addresses from text to binary form - if (inet_pton(AF_INET, host, &serv_addr.sin_addr) <= 0) { + if (inet_pton(AF_INET, host.c_str(), &serv_addr.sin_addr) <= 0) { printf("\nInvalid address/ Address not supported \n"); return -1; } @@ -127,6 +127,36 @@ int ComRobot::Open(string usart) { #endif } +int ComRobot::Open(string shost, int nport) { +#ifdef __SIMULATION__ + + struct sockaddr_in serv_addr; + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + printf("\n Socket creation error \n"); + return -1; + } + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 200000; + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof tv); + + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(nport); + + if (inet_pton(AF_INET, shost.c_str(), &serv_addr.sin_addr) <= 0) { + cout << "Invalid address/ Address not supported" << endl; + return -1; + } + + if (connect(sock, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) { + return -2; + } + return 1; +#else + return -1 +#endif +} + /** * Close serial link * @return Success if above 0, failure if below 0 diff --git a/software/raspberry/superviseur-robot/lib/comrobot.h b/software/raspberry/superviseur-robot/lib/comrobot.h index d6bdd11..3b92089 100644 --- a/software/raspberry/superviseur-robot/lib/comrobot.h +++ b/software/raspberry/superviseur-robot/lib/comrobot.h @@ -59,6 +59,14 @@ public: */ int Open(string usart); + /** + * Open tcp client for the graphical simulator + * @param host Host address + * @param port Port number + * @return 1 if ok or -1 + */ + int Open(string shost, int nport); + /** * Close serial link * @return Success if above 0, failure if below 0 @@ -89,9 +97,9 @@ public: */ virtual void Write_Post() { } - + Message *SendCommand(Message* msg, MessageID answerID, int maxRetries); - + static Message *Ping() { return new Message(MESSAGE_ROBOT_PING); }