From 8eec887bad2b821079e15d653fbbe6d6ccaceaf2 Mon Sep 17 00:00:00 2001 From: kutayboz Date: Fri, 2 Jun 2023 02:45:07 +0300 Subject: Added cmake config. Code refactoring. --- .gitignore | 4 ++- CMakeLists.txt | 11 ++++++ src/main.c | 111 ++++++++++++++++++++++++++++++--------------------------- 3 files changed, 73 insertions(+), 53 deletions(-) create mode 100644 CMakeLists.txt diff --git a/.gitignore b/.gitignore index e1f5cb3..ad6e780 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .vscode -bin/ \ No newline at end of file +.cache +bin/ +build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a9a7334 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.0.0) +set(CMAKE_C_STANDARD 90) +set(CMAKE_C_STANDARD_REQUIRED True) + +project(network_monitor VERSION 0.1.0) +set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -pedantic-errors") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -O0") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O2") +add_executable(network_monitor ./src/main.c) + +install(TARGETS network_monitor DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/bin/) diff --git a/src/main.c b/src/main.c index c041563..b8b7639 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,3 @@ -#include #include /* strerror() */ #include /* O_RDWR */ #include @@ -8,7 +7,9 @@ #include #include /* write(), read(), close() */ -int openSerialPort(char pathToPort[], int baudRate) { +#define BUFFER_SIZE 256 + +int openSerialPort(char pathToPort[]) { static int serialPort; struct termios serialTerminal; @@ -21,6 +22,7 @@ int openSerialPort(char pathToPort[], int baudRate) { if (tcgetattr(serialPort, &serialTerminal) != 0) { printf("Error tcgetattr(): %s\n", strerror(errno)); + close(serialPort); return -1; } @@ -43,8 +45,8 @@ int openSerialPort(char pathToPort[], int baudRate) { serialTerminal.c_cc[VTIME] = 0; serialTerminal.c_cc[VMIN] = 0; - cfsetispeed(&serialTerminal, baudRate); - cfsetospeed(&serialTerminal, baudRate); + cfsetispeed(&serialTerminal, B115200); + cfsetospeed(&serialTerminal, B115200); if (tcsetattr(serialPort, TCSANOW, &serialTerminal) != 0) { printf("Error tcsetattr(): %s\n", strerror(errno)); @@ -55,86 +57,91 @@ int openSerialPort(char pathToPort[], int baudRate) { return serialPort; } -int readSerialPort(int serialPort) { +int querySerialPort(int serialPort, char *input) { - time_t start; - int bytesRead, bufferFree, idx, returnCode, isEnd; - int bufferSize = sizeof(char) * 256; /* initial size in bytes */ - char *buffer = calloc(256, sizeof(char)); + time_t timeStart; + unsigned long idx; + int bytesRead, bufferFree, returnCode, isEnd, + bufferSize = sizeof(char) * BUFFER_SIZE; /* initial size in bytes */ + char *txBuffer, *buffer = calloc(BUFFER_SIZE, sizeof(char)); const char *responseCodes[8] = {"\r\nOK\r\n", "\r\nCONNECT\r\n", "\r\nRING\r\n", "\r\nNO CARRIER\r\n", "\r\nERROR\r\n", "\r\nNO DIALTONE\r\n", "\r\nBUSY\r\n", "\r\nNO ANSWER\r\n"}; + txBuffer = malloc(sizeof(char) * (strlen(input) + 2)); + strncpy(txBuffer, input, sizeof(char) * (strlen(input) + 1)); + strncat(txBuffer, "\r", 2 * sizeof(char)); + if (write(serialPort, txBuffer, sizeof(char) * (strlen(txBuffer))) < 0) { + printf("Error write(): %s\n", strerror(errno)); + free(txBuffer); + free(buffer); + return 1; + } + free(txBuffer); + bytesRead = bufferFree = bufferSize; returnCode = isEnd = 0; - start = time(NULL); - + timeStart = time(NULL); while (!isEnd) { - if (bufferFree < 1) { - bufferSize += sizeof(char) * 256; - buffer = realloc(buffer, bufferSize); - if (buffer == NULL) { - printf("Error realloc(): %s\n", strerror(errno)); - returnCode = 1; - break; - } - } - bytesRead = read(serialPort, buffer + bufferSize - bufferFree, bufferFree); + bytesRead = + read(serialPort, buffer + bufferSize - bufferFree, bufferFree - 1); if (bytesRead < 0) { - printf("Error read(): %s\n", strerror(errno)); - returnCode = 1; - break; - } else if (bytesRead > 0) { - bufferFree -= bytesRead; - start = time(NULL); - } else if (bytesRead == 0 && difftime(time(NULL), start) > 180) { - printf("Response timeout\n"); + printf("Error reading serial port\n"); returnCode = 1; break; - } else { - for (idx = 0; (long unsigned)idx < - sizeof(responseCodes) / sizeof(responseCodes[1]); + } + + else if (bytesRead > 0) { + buffer[bytesRead] = '\0'; + bufferFree -= (bytesRead + 1); + + if (bufferFree == 0) { + bufferSize += bufferFree = BUFFER_SIZE; + buffer = realloc(buffer, bufferSize); + } + + for (idx = 0; idx < sizeof(responseCodes) / sizeof(*responseCodes); idx++) { - if (strstr(buffer, responseCodes[idx]) != NULL) { + if (strstr(buffer, responseCodes[idx])) { isEnd = 1; break; } } + } else if (bytesRead == 0 && time(NULL) - timeStart > 180) { + printf("Response timeout after 180 seconds\n"); + returnCode = 1; + break; } } - printf("%s", buffer); + printf("%s\n", buffer); free(buffer); return returnCode; } int main() { - int serialPort; - - serialPort = openSerialPort("/dev/serial0", 115200); + char *commandList[] = {"AT", "ATI", "AT+COPS?"/* , + "AT+COPS=?" */}, + serialFilePath[] = "/dev/serial0"; - if (write(serialPort, "AT\r", sizeof("AT\r")) < 0) { - printf("Error write(): %s\n", strerror(errno)); - close(serialPort); - return 1; - } - readSerialPort(serialPort); + int serialPort; + unsigned long idx; - if (write(serialPort, "ATI\r", sizeof("ATI\r")) < 0) { - printf("Error write(): %s\n", strerror(errno)); - close(serialPort); + serialPort = openSerialPort(serialFilePath); + if (serialPort < 0) { + printf("Error openSerialPort()\n"); return 1; } - readSerialPort(serialPort); - if (write(serialPort, "AT+COPS?\r", sizeof("AT+COPS?\r")) < 0) { - printf("Error write(): %s\n", strerror(errno)); - close(serialPort); - return 1; + for (idx = 0; idx < sizeof(commandList) / sizeof(*commandList); idx++) { + if (0 != querySerialPort(serialPort, commandList[idx])) { + printf("Error querySerialPort()\n"); + close(serialPort); + return 1; + } } - readSerialPort(serialPort); close(serialPort); -- cgit v1.3