diff --git a/hald/linux/probing/Makefile.am b/hald/linux/probing/Makefile.am index 274b870..9e0c7ec 100644 --- a/hald/linux/probing/Makefile.am +++ b/hald/linux/probing/Makefile.am @@ -20,7 +20,8 @@ libexec_PROGRAMS = \ hald-probe-serial \ hald-probe-ieee1394-unit \ hald-probe-net-bluetooth \ - hald-probe-video4linux + hald-probe-video4linux \ + hald-probe-modem endif hald_probe_smbios_SOURCES = probe-smbios.c ../../logger.c @@ -39,6 +40,9 @@ hald_probe_hiddev_LDADD = $(top_builddir)/libhal/libhal.la hald_probe_serial_SOURCES = probe-serial.c ../../logger.c hald_probe_serial_LDADD = $(top_builddir)/libhal/libhal.la +hald_probe_modem_SOURCES = probe-modem.c ../../logger.c +hald_probe_modem_LDADD = $(top_builddir)/libhal/libhal.la + hald_probe_storage_SOURCES = probe-storage.c linux_dvd_rw_utils.c linux_dvd_rw_utils.h ../../logger.c hald_probe_storage_LDADD = $(top_builddir)/libhal/libhal.la $(top_builddir)/partutil/libpartutil.la @GLIB_LIBS@ @VOLUME_ID_LIBS@ diff --git a/hald/linux/probing/probe-modem.c b/hald/linux/probing/probe-modem.c new file mode 100644 index 0000000..43300e4 --- /dev/null +++ b/hald/linux/probing/probe-modem.c @@ -0,0 +1,167 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include +#include + +#include +#include +#include + +#include "libhal/libhal.h" +#include "../../logger.h" + +#define MODEM_CAP_GSM 0x0001 /* GSM commands */ +#define MODEM_CAP_IS707_A 0x0002 /* CDMA circuit switched data commands */ +#define MODEM_CAP_DS 0x0004 /* data compression */ +#define MODEM_CAP_ES 0x0008 /* error control */ +#define MODEM_CAP_FCLASS 0x0010 /* Fax commands */ +#define MODEM_CAP_MS 0x0020 /* Modulation control commands */ +#define MODEM_CAP_W 0x0040 /* Wireless commands */ + +struct modem_caps { + char *name; + int bits; +}; + +static struct modem_caps modem_caps[] = { + {"+CGSM", MODEM_CAP_GSM}, + /* TODO: are they the same? */ + {"+CIS707-A", MODEM_CAP_IS707_A}, + {"+CIS707", MODEM_CAP_IS707_A}, + {"+CIS707P", MODEM_CAP_IS707_A}, + {NULL} +} ; + +#define AT_CAPS_PROBE "AT+GCAP\r\n" + +static int modem_probe_caps(int fd) +{ + char buf[200]; + char *ptr, *field = NULL; + int err, ret = 0; + + err = write(fd, AT_CAPS_PROBE, sizeof(AT_CAPS_PROBE) - 1); + + if (err != sizeof(AT_CAPS_PROBE) - 1) + return -1; + + /* 100ms is enough for modem to send all the data */ + usleep(100000); + + err = read(fd, buf, sizeof(buf) - 1); + if (err <= 0) + return -1; + buf[err] = 0; + + /* check okay reply */ + ptr = strstr(buf, "\r\nOK\r\n"); + if (!ptr) + return -1; + *ptr = 0; + + /* find +GCAP: string */ + ptr = strstr(buf, "\r\n+GCAP:"); + + if (ptr == NULL) + return -1; + ptr += 8; + + /* and parse it */ + do { + err = *ptr == '\0' || *ptr == '\r' || *ptr == '\n'; + if (*ptr == ' ' || *ptr == ',' || err) { + *ptr = 0; + if (field) { + struct modem_caps *cap = modem_caps; + + while (cap->name) { + if (!strcmp(cap->name, field)) + ret |= cap->bits; + cap++; + } + } + field = NULL; + } else if (NULL == field) { + field = ptr; + } + ptr++; + } while (!err); + + return ret; +} + +int main(int argc, char *argv[]) +{ + struct termios orig, attrs; + DBusError error; + LibHalContext *ctx; + char *udi; + char *device; + int fd, caps; + + setup_logger(); + + if ((udi = getenv("UDI")) == NULL) { + HAL_ERROR(("UDI is not set")); + return -1; + } + + if ((device = getenv("HAL_PROP_SERIAL_DEVICE")) == NULL) { + HAL_ERROR(("HAL_PROP_SERIAL_DEVICE is not set")); + return -1; + } + + fd = open(device, O_RDWR|O_NDELAY); + + if (-1 == fd) { + HAL_ERROR(("open(%s): %s", device, strerror(errno))); + return -1; + } + + if (tcgetattr(fd, &orig)) + return -1; + + memcpy(&attrs, &orig, sizeof(attrs)); + attrs.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR); + attrs.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET); + attrs.c_lflag &= ~(ICANON | XCASE | ECHO | ECHOE | ECHONL); + attrs.c_lflag &= ~(ECHO | ECHOE); + attrs.c_cc[VMIN] = 1; + attrs.c_cc[VTIME] = 0; + attrs.c_cc[VEOF] = 1; + + tcsetattr(fd, TCSANOW, &attrs); + caps = modem_probe_caps(fd); + tcsetattr(fd, TCSANOW, &orig); + + if (caps < 0) { + HAL_ERROR(("Couldn't get caps")); + return -1; + } + + dbus_error_init (&error); + + if ((ctx = libhal_ctx_init_direct(&error)) == NULL) { + HAL_ERROR(("ctx init failed")); + return -1; + } + + if (caps & MODEM_CAP_GSM) { + HAL_DEBUG(("Found GSM modem")); + libhal_device_property_strlist_append(ctx, udi, "modem.command_sets", "GSM-07.07", NULL); + libhal_device_property_strlist_append(ctx, udi, "modem.command_sets", "GSM-07.05", NULL); + } + + if (caps & MODEM_CAP_IS707_A) { + HAL_DEBUG(("Found CDMA modem")); + libhal_device_property_strlist_append(ctx, udi, "modem.command_sets", "IS-707-A", NULL); + } + + return 0; +}