#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "modem_comm.h"

int verbose = 0;

int disp_battery_status(int fd)
{
        char reply[81];
		int dc_powered, percentage_left;
        int ret;

		ret = modem_issue(fd, "AT+CBC", reply, sizeof(reply), verbose);
        if (ret >= 0) {
          if (sscanf(reply, "+CBC: %d,%d", &dc_powered, &percentage_left) == 2) {
            printf("Battery Status: %s, remaining%% %d\n", dc_powered == 13 ? "DC power" : "No DC Power", percentage_left );
          }
          else {
            errno = EBADMSG;
            return -1;
          }
        }

	    return ret;
}

int disp_modem_type(int fd)
{  
		/* According to the 3GPP TS 27.007 std this cannot exceed 2048 bytes */
		char reply[2049];
        char modem_type[51];
        int ret;

		ret = modem_issue(fd, "AT+CGMM", reply, sizeof(reply), verbose);
        if (ret >= 0) {
                printf ("Modem Type: %s\n", reply+7);
            }
            else
            {
				errno = EBADMSG;
				return -1;
			}

		return ret;
}

int disp_serial_number(int fd)
{		/* According to the 3GPP TS 27.007 std this cannot exceed 2048 bytes 
		   In the 3GPP std this is normally the IMEI number - this is true for IPW */
		/* BTW: IMEI == International Mobile station Equipment Identity */
        char reply[2049];	
		char imei[31];
        int ret;

        ret = modem_issue(fd, "AT+CGSN", reply, sizeof(reply), verbose);
        if (ret >= 0) {
          printf("reply=%s\n", reply);  // ###
			if (sscanf(reply, "+CGSN: %s", imei) == 1) {
				printf("Serial Number (IMEI): %s\n", imei);
			}
			else {
				errno = EBADMSG;
				return -1;
			}
        }

		return ret;
}

int disp_signal_quality(int fd)
{
        char reply[128];
		int percentage, always_99, rscp, iscp, sir; 
        int ret;

        ret = modem_issue(fd, "AT+CSQ", reply, sizeof(reply), verbose);

        if (ret >= 0) {
          if (sscanf(reply, "+CSQ: %d,%d,%d,%d,%d", &percentage, &always_99, &rscp, &iscp, &sir) == 5) {
            printf("Signal Quality: Signal%% %d, RSCP %d dBm, ISCP %d dBm, SIR? %d\n", percentage, rscp, iscp, sir);
          }
          else {
            errno = EBADMSG;
            return -1;
          }
        }

	return ret;
}

int disp_last_status_msg(int fd)
{		/* According to the 3GPP TS 27.007 std this cannot exceed 2041 bytes
		   EER == Extended error report */
		   
        char reply[2042];
		char msg[2042];
        int ret;
		
        ret = modem_issue(fd, "AT+CEER", reply, sizeof(reply), verbose);

        if (ret >= 0) {
            printf("Last Status Message: %s\n", reply+7);
          }
          else {
            errno = EBADMSG;
            return -1;
          }

	return ret;
}

int find_device(char* device_name)
{
  FILE* f = fopen("/proc/ipwireless_usb", "r");
  char *p, buf[256];
  int got_device = 0;
  int is_vendor = 0;
  char version[40];

  if (f == NULL)
    return 1;
  strcpy(version, "unknown");
  while (p = fgets(buf, sizeof(buf), f)) {
    char* lf = strchr(p, '\n');
    if (lf != NULL)
      *lf = '\0';
    if (strcmp(p, "type: vendor") == 0)
      is_vendor = 1;
    if (strncmp(p, "version: ", 9) == 0)
      strcpy(version, p+9);
    if (strncmp(p, "port: ", 6) == 0 && !got_device) {
      char device_type[40];
      if (sscanf(p+6, "%s %s", device_name, device_type) == 2) {
        if (strcmp(device_type, "modem") == 0)
          got_device = 1;
      }
    }
  }
  fclose(f);
  printf("using %s driver version %s\n",
    is_vendor?"vendor":"customer", version);
  return got_device ? 0 : 2;
}

int main(int argc, char* argv[])
{
  int fd;
  char reply[80];
  int ret;

  char device_name[40];

  int got_device = find_device(device_name);
  if (got_device != 0) {
    if (got_device == 1)
      printf("ipwireless_usb driver is not installed\n");
    else
      printf("modem is not plugged in\n");
    return 1;
  }

  fd = modem_init(device_name, 10);

  if (fd < 0) {
    printf("Failed to connect: %s\n", strerror(errno));
    return 1;
  }
  
  /* Do a test command... */
  ret = modem_issue(fd, "ATE1Q0", reply, sizeof(reply), verbose);
  if (ret >= 0)
    disp_serial_number(fd);
  if (ret >= 0)
    ret = disp_battery_status(fd);
  if (ret >= 0)
    disp_modem_type(fd);
  if (ret >= 0)
    disp_signal_quality(fd);
  if (ret >= 0)
    disp_last_status_msg(fd);

  if (ret < 0) {
    if (errno == EAGAIN)
      printf("timed out!\n");
    else
      printf("failed: %s\n", strerror(errno));
  }

  modem_close(fd);
}





/* 

AT+CEER Extended error report
AT+CGATT? PS Attach or DEtach
*/
