void testing(void) { printf("current log level: %d\n", rblog_level); rblog(RBLOG_ERROR, "error msg"); rblog(RBLOG_WARN, "warn msg"); rblog(RBLOG_INFO, "info msg"); rblog(RBLOG_DEBUG, "info msg"); }
// Debug message void REALBOT_PRINT(cBot * pBot, char *Function, char *msg) { // Message format: // Function name - [BOT NAME, BOT TEAM]: Message char cMessage[256]; char team[9]; char name[32]; memset(team, 0, sizeof(team)); // clear memset(name, 0, sizeof(name)); // clear strcpy(team, "TERROR"); // t strcpy(name, "FUNCTION"); if (pBot) { memset(name, 0, sizeof(name)); // clear strcpy(name, pBot->name); // copy name if (pBot->iTeam == 2) strcpy(team, "COUNTER"); } else { strcpy(team, "NONE"); } sprintf(cMessage, "RBPRINT->[%s '%s']-[Team %s] : %s\n", name, Function, team, msg); // print in console only when on debug print if (Game.bDebug) SERVER_PRINT(cMessage); // print this realbot message also in the LOG file. rblog(cMessage); } // REALBOT_PRINT()
int open_port(char *port) { char logmsg[255]; snprintf(logmsg, 255, "Opening port ..."); rblog(RBLOG_DEBUG, logmsg); int fd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK); snprintf(logmsg, 255, "opened port, fd is: %d", fd); rblog(RBLOG_DEBUG, logmsg); // TODO: use errno? if (fd == -1) { // if open is unsucessful snprintf(logmsg, 255, "Unable to open serial port, exiting"); rblog(RBLOG_ERROR, logmsg); exit(1); } else { configure_port(fd, B38400); fcntl(fd, F_SETFL, O_NONBLOCK); } return(fd); }
int configure_port(int fd, speed_t st) { char logmsg[255]; struct termios port_settings; // structure to store the port settings in int i; int rc; rc = tcgetattr(fd, &port_settings); if (rc < 0) { snprintf(logmsg, 255, "Failed to get port settings, trying anyway"); rblog(RBLOG_WARN, logmsg); } cfsetispeed(&port_settings, st); // set baud rates cfsetospeed(&port_settings, st); port_settings.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS | HUPCL); port_settings.c_cflag |= (CS8 | CLOCAL | CREAD); // Need to see which flags should be unset. // This is not the "correct" way to fix this, as it just turns all flags off // rather than specifying the specific flag setting. port_settings.c_iflag = 0; port_settings.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | IEXTEN); port_settings.c_lflag &= ~(INPCK | IXON | IXOFF | IXANY | ICRNL); port_settings.c_oflag &= ~(OPOST | ONLCR); for (i = 0; i < sizeof(port_settings.c_cc); i++) { port_settings.c_cc[i] = _POSIX_VDISABLE; } port_settings.c_cc[VTIME] = 0; port_settings.c_cc[VMIN] = 1; tcsetattr(fd, TCSAFLUSH, &port_settings); // apply the settings to the port return(fd); }
int send_cmd_with_delay(char *portname, uint8_t *write_bytes, int writelen, uint8_t *read_bytes, int delay) { // initialize read_bytes to 0 read_bytes[0] = '\0'; char errmsg[255]; char bytecheck[8]; snprintf(errmsg, 255, "Port: %s", portname); rblog(RBLOG_DEBUG, errmsg); int fd = open_port(portname); uint8_t tmpbuf[64]; size_t bufout_idx = 1; /* first element will be length, data starts 2nd element */ //Needed to use select fd_set input; int max_fd; int s_ret; FD_ZERO(&input); FD_SET(fd, &input); struct timeval timeout; max_fd = fd + 1; timeout.tv_sec = 1; timeout.tv_usec = 0; // Give port some time to open // Might be able to be removed usleep(delay); //debug errmsg[0] = '\0'; bytecheck[0] = '\0'; if (rbloggable(RBLOG_DEBUG)) { snprintf(errmsg, 255, "Sending bytes: "); size_t idx2 = 0; while (idx2 < writelen) { snprintf(bytecheck, 8, "%02x", (int)write_bytes[idx2]); strncat(errmsg, bytecheck, 8); idx2++; } rblog(RBLOG_DEBUG, errmsg); } // end debug /* writing writelen write_bytes to the serial port */ int bytes_written = write(fd, write_bytes, writelen); snprintf(errmsg, 255, "Wrote %d bytes to port", bytes_written); rblog(RBLOG_DEBUG, errmsg); // Don't need delay with select s_ret = select(max_fd, &input, NULL, NULL, &timeout); if (s_ret < 0) { snprintf(errmsg, 255, "Selecting port failed"); rblog(RBLOG_ERROR, errmsg); return -4; } int exit_code; if (FD_ISSET(fd, &input)) { snprintf(errmsg, 255, "Input on port"); rblog(RBLOG_DEBUG, errmsg); // -- Process input size_t len = read(fd, tmpbuf, sizeof(tmpbuf)); size_t retry_count = 0; size_t eagain_max_retry = 3; while ((EAGAIN == errno) && (retry_count < eagain_max_retry)) { snprintf(errmsg, 255, "Recieved: '%s (%d)', retrying(%zu)", strerror(errno), errno, retry_count); rblog(RBLOG_WARN, errmsg); errno = 0; len = read(fd, tmpbuf, sizeof(tmpbuf)); retry_count++; } if (len == -1) { snprintf(errmsg, 255, "Error reading from port: %s", strerror(errno)); rblog(RBLOG_ERROR, errmsg); exit_code = -1; } else if (len == 0) { snprintf(errmsg, 255, "No data from port"); rblog(RBLOG_ERROR, errmsg); exit_code = -2; } else if ((len > 0) && (len <= MAX_READ_LEN)) { /* Success Condition */ errmsg[0] = '\0'; bytecheck[0] = '\0'; snprintf(errmsg, 255, "Bytes from serial port:"); read_bytes[0] = len; while (bufout_idx < len + 1) { snprintf(bytecheck, 8, "%02x", (int)tmpbuf[bufout_idx - 1]); strncat(errmsg, bytecheck, 8); read_bytes[bufout_idx] = tmpbuf[bufout_idx - 1]; bufout_idx++; } rblog(RBLOG_DEBUG, errmsg); exit_code = 0; } else { snprintf(errmsg, 255, "Error in data length"); rblog(RBLOG_ERROR, errmsg); exit_code = -3; } } else { // attemptint to process input but no input available snprintf(errmsg, 255, "Input on port"); rblog(RBLOG_DEBUG, errmsg); exit_code = -6; } snprintf(errmsg, 255, "closing port: %d", fd); rblog(RBLOG_DEBUG, errmsg); close(fd); snprintf(errmsg, 255, "closed port: %d", fd); rblog(RBLOG_DEBUG, errmsg); snprintf(errmsg, 255, "exit code: %d", exit_code); rblog(RBLOG_DEBUG, errmsg); return exit_code; }
// Parse IAD file: // Important Area Definition file void INI_PARSE_IAD() { char dirname[256]; char filename[256]; FILE *stream; int section = INI_NONE; int wordtype = WORD_NONE; // Set Directory name strcpy(dirname, "data/cstrike/ini/"); strcat(dirname, STRING(gpGlobals->mapname)); strcat(dirname, ".ini"); // nodes file // writes whole path into "filename", Linux compatible UTIL_BuildFileNameRB(dirname, filename); SERVER_PRINT(filename); SERVER_PRINT("\n"); float AreaX, AreaY, AreaZ; AreaX = AreaY = AreaZ = 9999; if ((stream = fopen(filename, "r+t")) != NULL) { char linefeed[80]; char lineword[25]; char linesection[30]; // infinite loop baby while (!feof(stream)) { INI_Sentence(stream, linefeed); // Linefeed contains a string of 1 sentence. Whenever the first character is a commentary // character (which is "//", ";" or "#"), or an empty line, then skip it if (linefeed[0] == ';' || linefeed[0] == '#' || (linefeed[0] == '/' && linefeed[1] == '/') || linefeed[0] == '\n' || linefeed[0] == '\0') continue; // Skip wordtype = WORD_NONE; // Every line is checked for a new section. INI_Section(linefeed, linesection); if (linesection[0] != '\0' && strlen(linesection) > 1) { section = INI_SectionType(linesection, section); continue; // next line } // Check word only when in a section if (section != INI_NONE) { INI_Word(linefeed, lineword); wordtype = INI_WordType(lineword, section); if (section == INI_AREA) { if (wordtype == WORD_AREAX) AreaX = INI_WordValueINT(linefeed); if (wordtype == WORD_AREAY) AreaY = INI_WordValueINT(linefeed); if (wordtype == WORD_AREAZ) AreaZ = INI_WordValueINT(linefeed); if (AreaX != 9999 && AreaY != 9999 && AreaZ != 9999) { // add this to goal rblog("IAD: Adding an important area/goal\n"); NodeMachine.goal_add(NULL, GOAL_IMPORTANT, Vector(AreaX, AreaY, AreaZ)); AreaX = AreaY = AreaZ = 9999; } } } } // while fclose(stream); } }