diff options
| author | kutayboz <kutayboz@gmail.com> | 2023-06-05 05:30:20 +0300 |
|---|---|---|
| committer | kutayboz <kutayboz@gmail.com> | 2023-06-05 05:30:20 +0300 |
| commit | addad212bbf05f6599fea747ebae945cb16e5d96 (patch) | |
| tree | cb7f42f36f8603dd1d4fbfba1b1f60971cab18d0 /src | |
| parent | 97a92eaadb6806a3e7f898bafec7b0105b9829a5 (diff) | |
| download | celnetmon-addad212bbf05f6599fea747ebae945cb16e5d96.tar.gz | |
Added device power status pin functionality
Diffstat (limited to 'src')
| -rw-r--r-- | src/gpio.c | 36 | ||||
| -rw-r--r-- | src/main.c | 46 |
2 files changed, 61 insertions, 21 deletions
@@ -42,3 +42,39 @@ int modemPowerToggle(int pinNum) { gpiod_chip_close(chip); return 0; } + +int checkPinVal(int pinNum) { + int pinVal = -1; + struct gpiod_chip *chip; + struct gpiod_line *line; + + chip = gpiod_chip_open("/dev/gpiochip0"); + if (!chip) { + perror("Error opening GPIO chip"); + return -1; + } + + line = gpiod_chip_get_line( + chip, pinNum); // Replace with the desired GPIO pin number + if (!line) { + perror("Error getting GPIO line"); + gpiod_chip_close(chip); + return -1; + } + + if (gpiod_line_request_input(line, "cellular modem power status") < 0) { + perror("Error setting GPIO line as input"); + gpiod_chip_close(chip); + return -1; + } + + pinVal = gpiod_line_get_value(line); + if (pinVal < 0) { + perror("Error getting GPIO line value"); + gpiod_chip_close(chip); + return -1; + } + + gpiod_chip_close(chip); + return pinVal; +} @@ -7,33 +7,43 @@ int main() { - char *commandList[] = {"AT", "ATI", "AT+COPS?"/* , - "AT+COPS=?" */}, + char *commandList[] = {"AT", "ATI", "AT+COPS?", "AT+COPS=?"}, serialFilePath[] = "/dev/serial0", *output = malloc(1); - int serialPort; + int serialPort, moduleStatus; unsigned long idx; + time_t pwrKeyTime; - serialPort = openSerialPort(serialFilePath); - if (serialPort < 0) { - printf("Error openSerialPort()\n"); - free(output); - return 1; - } - - /* if (0 != querySerialPort(&output, serialPort, "AT")) { - printf("Error querySerialPort()\n"); + moduleStatus = checkPinVal(27); + if (0 > moduleStatus) { + printf("Error checkPinVal()\n"); close(serialPort); free(output); return 1; - } - if (NULL == strstr(output, "OK\r\n")) { + } else if (moduleStatus == 0) { + printf("Module is off\nPowering on\n"); if (0 != modemPowerToggle(17)) { printf("Error toggling modem power\n"); free(output); return 1; } - } */ + pwrKeyTime = time(NULL); + while (!checkPinVal(27)) { + sleep(1); + if (time(NULL) - pwrKeyTime > 10) { + printf("Modem power on takes longer than 10s\nAborting\n"); + free(output); + return 1; + } + } + } + + serialPort = openSerialPort(serialFilePath); + if (serialPort < 0) { + printf("Error openSerialPort()\n"); + free(output); + return 1; + } for (idx = 0; idx < sizeof(commandList) / sizeof(*commandList); idx++) { if (0 != querySerialPort(&output, serialPort, commandList[idx])) { @@ -47,12 +57,6 @@ int main() { close(serialPort); - /* if (0 != modemPowerToggle(17)) { - printf("Error toggling modem power\n"); - free(output); - return 1; - } */ - free(output); return 0; } |
