+#ifdef _WIN32
+ HANDLE *handle_ptr = result_ptr;
+ *handle_ptr = port->hdl;
+#else
+ int *fd_ptr = result_ptr;
+ *fd_ptr = port->fd;
+#endif
+
+ RETURN_OK();
+}
+
+enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
+{
+ TRACE("%p, %p", port, copy_ptr);
+
+ if (!copy_ptr)
+ RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
+
+ *copy_ptr = NULL;
+
+ if (!port)
+ RETURN_ERROR(SP_ERR_ARG, "Null port");
+
+ if (!port->name)
+ RETURN_ERROR(SP_ERR_ARG, "Null port name");
+
+ DEBUG("Copying port structure");
+
+ RETURN_VALUE("%p", sp_get_port_by_name(port->name, copy_ptr));
+}
+
+void sp_free_port(struct sp_port *port)
+{
+ TRACE("%p", port);
+
+ if (!port)
+ {
+ DEBUG("Null port");
+ RETURN();
+ }
+
+ DEBUG("Freeing port structure");
+
+ if (port->name)
+ free(port->name);
+
+ free(port);
+
+ RETURN();
+}
+
+static struct sp_port **list_append(struct sp_port **list, const char *portname)
+{
+ void *tmp;
+ unsigned int count;
+
+ for (count = 0; list[count]; count++);
+ if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
+ goto fail;
+ list = tmp;
+ if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
+ goto fail;
+ list[count + 1] = NULL;
+ return list;
+
+fail:
+ sp_free_port_list(list);
+ return NULL;
+}
+
+enum sp_return sp_list_ports(struct sp_port ***list_ptr)
+{
+ struct sp_port **list;
+ int ret = SP_ERR_SUPP;
+
+ TRACE("%p", list_ptr);
+
+ if (!list_ptr)
+ RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
+
+ DEBUG("Enumerating ports");
+
+ if (!(list = malloc(sizeof(struct sp_port **))))
+ RETURN_ERROR(SP_ERR_MEM, "Port list malloc failed");
+
+ list[0] = NULL;
+
+#ifdef _WIN32
+ HKEY key;
+ TCHAR *value, *data;
+ DWORD max_value_len, max_data_size, max_data_len;
+ DWORD value_len, data_size, data_len;
+ DWORD type, index = 0;
+ char *name;
+ int name_len;
+
+ ret = SP_OK;
+
+ DEBUG("Opening registry key");
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
+ 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
+ SET_FAIL(ret, "RegOpenKeyEx() failed");
+ goto out_done;
+ }
+ DEBUG("Querying registry key value and data sizes");
+ if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+ &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
+ SET_FAIL(ret, "RegQueryInfoKey() failed");
+ goto out_close;
+ }
+ max_data_len = max_data_size / sizeof(TCHAR);
+ if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
+ SET_ERROR(ret, SP_ERR_MEM, "registry value malloc failed");
+ goto out_close;
+ }
+ if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
+ SET_ERROR(ret, SP_ERR_MEM, "registry data malloc failed");
+ goto out_free_value;
+ }
+ DEBUG("Iterating over values");
+ while (
+ value_len = max_value_len + 1,
+ data_size = max_data_size,
+ RegEnumValue(key, index, value, &value_len,
+ NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
+ {
+ data_len = data_size / sizeof(TCHAR);
+ data[data_len] = '\0';
+#ifdef UNICODE
+ name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
+#else
+ name_len = data_len + 1;
+#endif
+ if (!(name = malloc(name_len))) {
+ SET_ERROR(ret, SP_ERR_MEM, "registry port name malloc failed");
+ goto out;
+ }
+#ifdef UNICODE
+ WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
+#else
+ strcpy(name, data);
+#endif
+ if (type == REG_SZ) {
+ DEBUG("Found port %s", name);
+ if (!(list = list_append(list, name))) {
+ SET_ERROR(ret, SP_ERR_MEM, "list append failed");
+ goto out;
+ }
+ }
+ index++;
+ }
+out:
+ free(data);
+out_free_value:
+ free(value);
+out_close:
+ RegCloseKey(key);
+out_done:
+#endif
+#ifdef __APPLE__
+ mach_port_t master;
+ CFMutableDictionaryRef classes;
+ io_iterator_t iter;
+ char *path;
+ io_object_t port;
+ CFTypeRef cf_path;
+ Boolean result;
+
+ ret = SP_OK;
+
+ DEBUG("Getting IOKit master port");
+ if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
+ SET_FAIL(ret, "IOMasterPort() failed");
+ goto out_done;
+ }
+
+ DEBUG("Creating matching dictionary");
+ if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
+ SET_FAIL(ret, "IOServiceMatching() failed");
+ goto out_done;
+ }
+
+ CFDictionarySetValue(classes,
+ CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
+
+ DEBUG("Getting matching services");
+ if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
+ SET_FAIL(ret, "IOServiceGetMatchingServices() failed");
+ goto out_done;
+ }
+
+ if (!(path = malloc(PATH_MAX))) {
+ SET_ERROR(ret, SP_ERR_MEM, "device path malloc failed");
+ goto out_release;
+ }
+
+ DEBUG("Iterating over results");
+ while ((port = IOIteratorNext(iter))) {
+ cf_path = IORegistryEntryCreateCFProperty(port,
+ CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
+ if (cf_path) {
+ result = CFStringGetCString(cf_path,
+ path, PATH_MAX, kCFStringEncodingASCII);
+ CFRelease(cf_path);
+ if (result) {
+ DEBUG("Found port %s", path);
+ if (!(list = list_append(list, path))) {
+ SET_ERROR(ret, SP_ERR_MEM, "list append failed");
+ IOObjectRelease(port);
+ goto out;
+ }
+ }
+ }
+ IOObjectRelease(port);
+ }
+out:
+ free(path);
+out_release:
+ IOObjectRelease(iter);
+out_done:
+#endif
+#ifdef __linux__
+ struct udev *ud;
+ struct udev_enumerate *ud_enumerate;
+ struct udev_list_entry *ud_list;
+ struct udev_list_entry *ud_entry;
+ const char *path;
+ struct udev_device *ud_dev, *ud_parent;
+ const char *name;
+ const char *driver;
+ int fd, ioctl_result;
+ struct serial_struct serial_info;
+
+ ret = SP_OK;
+
+ DEBUG("Enumerating tty devices");
+ ud = udev_new();
+ ud_enumerate = udev_enumerate_new(ud);
+ udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
+ udev_enumerate_scan_devices(ud_enumerate);
+ ud_list = udev_enumerate_get_list_entry(ud_enumerate);
+ DEBUG("Iterating over results");
+ udev_list_entry_foreach(ud_entry, ud_list) {
+ path = udev_list_entry_get_name(ud_entry);
+ DEBUG("Found device %s", path);
+ ud_dev = udev_device_new_from_syspath(ud, path);
+ /* If there is no parent device, this is a virtual tty. */
+ ud_parent = udev_device_get_parent(ud_dev);
+ if (ud_parent == NULL) {
+ DEBUG("No parent device, assuming virtual tty");
+ udev_device_unref(ud_dev);
+ continue;
+ }
+ name = udev_device_get_devnode(ud_dev);
+ /* The serial8250 driver has a hardcoded number of ports.
+ * The only way to tell which actually exist on a given system
+ * is to try to open them and make an ioctl call. */
+ driver = udev_device_get_driver(ud_parent);
+ if (driver && !strcmp(driver, "serial8250")) {
+ DEBUG("serial8250 device, attempting to open");
+ if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
+ DEBUG("open failed, skipping");
+ goto skip;
+ }
+ ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
+ close(fd);
+ if (ioctl_result != 0) {
+ DEBUG("ioctl failed, skipping");
+ goto skip;
+ }
+ if (serial_info.type == PORT_UNKNOWN) {
+ DEBUG("port type is unknown, skipping");
+ goto skip;
+ }
+ }
+ DEBUG("Found port %s", name);
+ list = list_append(list, name);
+skip:
+ udev_device_unref(ud_dev);
+ if (!list) {
+ SET_ERROR(ret, SP_ERR_MEM, "list append failed");
+ goto out;
+ }
+ }
+out:
+ udev_enumerate_unref(ud_enumerate);
+ udev_unref(ud);
+#endif
+
+ switch (ret) {
+ case SP_OK:
+ *list_ptr = list;
+ RETURN_OK();
+ case SP_ERR_SUPP:
+ DEBUG_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform.");
+ default:
+ if (list)
+ sp_free_port_list(list);
+ *list_ptr = NULL;
+ return ret;
+ }
+}
+
+void sp_free_port_list(struct sp_port **list)
+{
+ unsigned int i;
+
+ TRACE("%p", list);
+
+ if (!list) {
+ DEBUG("Null list");
+ RETURN();
+ }
+
+ DEBUG("Freeing port list");
+
+ for (i = 0; list[i]; i++)
+ sp_free_port(list[i]);
+ free(list);
+
+ RETURN();
+}
+
+#define CHECK_PORT() do { \
+ if (port == NULL) \
+ RETURN_ERROR(SP_ERR_ARG, "Null port"); \
+ if (port->name == NULL) \
+ RETURN_ERROR(SP_ERR_ARG, "Null port name"); \
+} while (0)
+#ifdef _WIN32
+#define CHECK_PORT_HANDLE() do { \
+ if (port->hdl == INVALID_HANDLE_VALUE) \
+ RETURN_ERROR(SP_ERR_ARG, "Invalid port handle"); \
+} while (0)
+#else
+#define CHECK_PORT_HANDLE() do { \
+ if (port->fd < 0) \
+ RETURN_ERROR(SP_ERR_ARG, "Invalid port fd"); \
+} while (0)
+#endif
+#define CHECK_OPEN_PORT() do { \
+ CHECK_PORT(); \
+ CHECK_PORT_HANDLE(); \
+} while (0)
+
+enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
+{
+ TRACE("%p, %x", port, flags);
+
+ CHECK_PORT();
+
+ if (flags > (SP_MODE_READ | SP_MODE_WRITE | SP_MODE_NONBLOCK))
+ RETURN_ERROR(SP_ERR_ARG, "Invalid flags");