Go to the source code of this file.
Defines | |
#define | BUF_LEN 256 |
Functions | |
int | get_modem_signal_strength () |
Copyright (C) 2007 iRex Technologies BV
Definition in file modemUtils.h.
#define BUF_LEN 256 |
Definition at line 30 of file modemUtils.h.
int get_modem_signal_strength | ( | ) |
Definition at line 37 of file modemUtils.c.
00038 { 00039 CN_LOGPRINTF("entry"); 00040 00041 int fd = open(DEVICE_NAME, O_RDWR | O_NOCTTY); 00042 00043 if (fd < 0) 00044 { 00045 CN_ERRORPRINTF("Can't open %s", DEVICE_NAME); 00046 return 0; 00047 } 00048 00049 struct termios new_opt, old_opt; 00050 00051 // Save original settings 00052 tcgetattr(fd, &old_opt); 00053 00054 // Set terminal attribute 00055 new_opt.c_cflag = BAUD_RATE | CS8 | CLOCAL | CREAD; 00056 new_opt.c_iflag = IGNPAR; 00057 new_opt.c_oflag = 0; 00058 new_opt.c_lflag = 0; 00059 00060 // Blocking read until 1 char arrives 00061 new_opt.c_cc[VMIN] = 1; 00062 new_opt.c_cc[VTIME] = 0; 00063 00064 // Now clean the modem line and activate the settings for modem 00065 tcflush(fd, TCIFLUSH); 00066 tcsetattr(fd, TCSANOW, &new_opt); 00067 00068 char buf[BUF_LEN] = "AT+CSQ?\r"; 00069 int len = strlen(buf); 00070 if (len != write(fd, buf, len)) 00071 { 00072 CN_ERRORPRINTF("Can't write data to %s", DEVICE_NAME); 00073 return 0; 00074 } 00075 00076 len = read(fd, buf, BUF_LEN); 00077 00078 if (strstr(buf, "\r\nOK\r\n") == 0) 00079 { 00080 // Modem returned error 00081 CN_ERRORPRINTF("Modem returned error for command AT+CSQ?"); 00082 return 0; 00083 } 00084 00085 int rssi = 0, fer = 0; 00086 sscanf(buf, "\r\n+CSQ: %d, %d\r\n", &rssi, &fer); 00087 00088 // Restore original settings 00089 tcsetattr(fd, TCSANOW, &old_opt); 00090 close(fd); 00091 return rssi; 00092 }