diff --git a/README.md b/README.md index 155583c..ce36bf0 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ thin Win32 shell over one small C library, libdmodemctl. - **HyperTerminal items**: file transfers (ZMODEM, YMODEM, XMODEM and Kermit), copy and capture, and the ASCII Setup, Terminal Settings and Call menu of HyperTerminal. Refer to [Features](docs/features.md) - **Settings**: each modem setting as a native control, from AT!M - **Call Stats**: the rate, modulation, error control and line figures of the last call (AT#UD, ATI11) -- **Line settings**: a SIP account, a USB modem, or a modem that already runs (dmodemd, or a hardware modem such as the MultiTech MT9234MU) +- **Line settings**: a SIP account, a USB modem, or a modem that already runs (dmodemd, or a hardware modem such as the MultiTech MT9234MU). **Look for a modem** finds the modem and its port | | Windows | macOS | Linux | | --- | --- | --- | --- | diff --git a/docs/design.md b/docs/design.md index 624270a..0a69098 100644 --- a/docs/design.md +++ b/docs/design.md @@ -331,8 +331,9 @@ The app gives the SIP account to dmodemd in its environment (`SIP_LOGIN`, USB modem and the debug level as flags. The app can also use a modem that already runs: a TCP address or a COM -port. On Linux and macOS, the layer changes a COM port name to a device -(refer to "The Windows app on Linux and macOS", below). +port. On Linux and macOS, it can also be a device path (`/dev/ttyUSB0`, +`/dev/cu.usbmodem1`), or a COM port name that the layer changes to a +device (refer to "The Windows app on Linux and macOS", below). The app does not start dmodemd by default. It runs the modem in a thread (`libdmodem_embed`), and its DTE is a socket. Refer to @@ -364,11 +365,27 @@ it. This repository includes the file, with a notice copy it to `/lib/firmware` (for the MT9234ZBA-USB, as `mts_mt9234zba.fw`), then disconnect the modem and connect it again. -Select the port of the modem in the Line Settings dialog, with hardware -flow control (RTS/CTS). On Linux, give the port a COM name with -`WINPORT_COMn` (for example `WINPORT_COM5=/dev/ttyUSB0`, refer to "The -Windows app on Linux and macOS", below). The modem uses `&K3` from -the factory. +### Look for a modem + +The Line Settings dialog finds the modem (`windows/src/ports.cpp`). When +the dialog opens, it searches the ports. If it finds a modem on a port +that is not selected, it shows the modem below the settings of the serial +port. Click **Look for a modem** to search again. If the app finds a +modem, it selects "A modem on a serial port", the port of the modem and +hardware flow control (RTS/CTS). The modem uses `&K3` from the factory. +The Port box lists all the ports that the app found, with the modems +first. When you select a port, the dialog shows what is on it. + +| | Where the app looks | What is a modem | +| --- | --- | --- | +| Windows | The modems that Windows knows: the Modem class in the registry (`AttachedTo`, and `FriendlyName` or `DriverDesc`), for Windows 95, 98 and ME and for NT. The MultiTech modems in `Enum\USB` (NT). The ports of `HARDWARE\DEVICEMAP\SERIALCOMM`. If that key has no ports, the app tries `COM1` to `COM16` | An installed modem, or a MultiTech MT9234MU or MT9234ZBA-USB | +| Linux | `/sys/class/tty`: each `ttyUSB` and `ttyACM`, with the maker and the product of its USB device | A MultiTech MT9234MU or MT9234ZBA-USB, or a USB device whose name has "modem" in it | +| macOS | Each `/dev/cu.*` device, but not the Bluetooth devices | A `cu.usbmodem` device (CDC-ACM, for example the MT9234MU-CDC) | + +On Linux, if a MultiTech modem is connected but has no port, the dialog +tells you why. With no firmware (one USB configuration, the boot ROM of +the TUSB3410), it shows the command that copies the file. If the +firmware is there, it tells you to load the `ti_usb_3410_5052` driver. Set the country of the line with `AT%T19,0,` and `AT&F&W` in the "Commands when the modem opens" field one time, or in the Terminal page. diff --git a/docs/features.md b/docs/features.md index a600462..23d704b 100644 --- a/docs/features.md +++ b/docs/features.md @@ -14,4 +14,4 @@ This page gives the details of the pages and menus of the app. The - **Copy and capture**: select text with the mouse (Shift and the mouse when the remote side uses the mouse), then copy it. Paste to Host, Select All, Capture Text (to a file), Capture to Printer, and Print (the screen or the selection). Text that the remote side sends to its printer port goes to the printer. Use the Edit and Transfer menus, or click the right mouse button on the terminal - **ASCII Setup** (Modem menu): the same items as HyperTerminal. For the data that you send: Enter sends CR LF, the local echo, and the line delay and character delay of a text file. For the data that you receive: an LF after each CR, 7-bit ASCII, and the wrap of long lines. The settings apply to the modem terminal and to the Remote terminal - **Terminal Settings** (Modem menu): the same items as HyperTerminal, for the modem terminal and for the Remote terminal. They are what Backspace sends (Ctrl+H, Del, or Ctrl+H, Space, Ctrl+H), the function, arrow and Ctrl keys as terminal keys or Windows keys, the backscroll buffer lines, the font, the cursor, the Telnet terminal ID, and Exit program upon disconnecting. The status bar shows the connect timer during a call -- **Call menu**: Call (sessions with a name and a number, as the connection files of HyperTerminal), Wait for a Call (ATS0=1), Disconnect, and Send Break (Ctrl+Break: a COM port or a Telnet connection). Line Settings has the COM port settings: bits per second, data bits, parity, stop bits and flow control +- **Call menu**: Call (sessions with a name and a number, as the connection files of HyperTerminal), Wait for a Call (ATS0=1), Disconnect, and Send Break (Ctrl+Break: a COM port or a Telnet connection). Line Settings has the COM port settings: bits per second, data bits, parity, stop bits and flow control. **Look for a modem** finds the modem and selects its port (refer to "Look for a modem" in [Design](design.md)) diff --git a/windows/CMakeLists.txt b/windows/CMakeLists.txt index 346faa0..b3dfe4e 100644 --- a/windows/CMakeLists.txt +++ b/windows/CMakeLists.txt @@ -8,7 +8,7 @@ include(sources.cmake) add_executable(dmodem-app WIN32 ${DMODEM_WIN_SOURCES} res/app.rc) set_target_properties(dmodem-app PROPERTIES OUTPUT_NAME dmodem) target_include_directories(dmodem-app PRIVATE src) -target_link_libraries(dmodem-app PRIVATE dmodemctl comctl32 wsock32 comdlg32 gdi32 winmm msacm32 shell32) +target_link_libraries(dmodem-app PRIVATE dmodemctl comctl32 wsock32 comdlg32 gdi32 winmm msacm32 shell32 advapi32) # the Remote page's SSH: libdmodemssh (libssh, built in) if(TARGET dmodemssh) target_link_libraries(dmodem-app PRIVATE dmodemssh) diff --git a/windows/res/app.rc b/windows/res/app.rc index 2ab43f2..36ad46a 100644 --- a/windows/res/app.rc +++ b/windows/res/app.rc @@ -64,7 +64,7 @@ BEGIN END END -IDD_LINE DIALOG 0, 0, 300, 318 +IDD_LINE DIALOG 0, 0, 300, 332 STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Line Settings" FONT 8, "MS Shell Dlg" @@ -106,10 +106,12 @@ BEGIN COMBOBOX IDC_L_STOP, 248, 228, 44, 80, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP LTEXT "&Flow control:", -1, 20, 246, 60, 8 COMBOBOX IDC_L_FLOW, 84, 244, 80, 80, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP - LTEXT "Commands when the modem &opens:", -1, 8, 266, 140, 8 - EDITTEXT IDC_L_INIT, 8, 276, 284, 12, ES_AUTOHSCROLL - DEFPUSHBUTTON "OK", IDOK, 186, 298, 50, 14 - PUSHBUTTON "Cancel", IDCANCEL, 242, 298, 50, 14 + PUSHBUTTON "&Look for a modem", IDC_L_FIND, 212, 243, 80, 14 + LTEXT "", IDC_L_FOUND, 20, 260, 272, 16 + LTEXT "Commands when the modem &opens:", -1, 8, 280, 140, 8 + EDITTEXT IDC_L_INIT, 8, 290, 284, 12, ES_AUTOHSCROLL + DEFPUSHBUTTON "OK", IDOK, 186, 312, 50, 14 + PUSHBUTTON "Cancel", IDCANCEL, 242, 312, 50, 14 END IDD_SETTING DIALOG 0, 0, 240, 110 diff --git a/windows/sources.cmake b/windows/sources.cmake index 1de1b98..bc76c6d 100644 --- a/windows/sources.cmake +++ b/windows/sources.cmake @@ -16,5 +16,6 @@ set(DMODEM_WIN_SOURCES capture.cpp ascii.cpp termset.cpp - calls.cpp) + calls.cpp + ports.cpp) list(TRANSFORM DMODEM_WIN_SOURCES PREPEND "${CMAKE_CURRENT_LIST_DIR}/src/") diff --git a/windows/src/app.h b/windows/src/app.h index f9ae490..3bfbe5d 100644 --- a/windows/src/app.h +++ b/windows/src/app.h @@ -85,7 +85,7 @@ struct Config { dmodemd.exe started ([line] builtin=0) */ int builtin; char tcp[256]; /* host:port */ - char com[32]; /* COM1 */ + char com[64]; /* COM1, or /dev/ttyUSB0 (winport) */ /* the COM port's settings, as HyperTerminal's Configure: bits per second, data bits (5 to 8), parity (0 none, 1 odd, 2 even, 3 mark, 4 space), stop bits (0 one, 1 one and a half, 2 two), flow control @@ -109,6 +109,19 @@ bool config_sip_ready(const Config *c); /* the Line Settings dialog: true if changed */ bool line_dialog(HWND owner, Config *c); +/* ports.cpp: a serial port of this computer, and what is on it */ +struct PortInfo { + char name[64]; /* "COM3", or "/dev/ttyUSB0" (winport) */ + char desc[128]; /* "MultiTech MT9234MU", or "" */ + int modem; /* a modem is on this port */ +}; +/* the serial ports of this computer, the modems first: their number + (max at most) */ +int ports_list(PortInfo *out, int max); +/* a USB modem that has no port yet and what to do (Linux: the firmware + of the MT9234MU), or "" */ +void ports_problem(char *out, int n); + /* a command's response */ typedef void (*ResponseFn)(const dmc_event *ev, void *data); diff --git a/windows/src/config.cpp b/windows/src/config.cpp index fbee311..5287953 100644 --- a/windows/src/config.cpp +++ b/windows/src/config.cpp @@ -285,6 +285,94 @@ static void enable(HWND d) EnableWindow(GetDlgItem(d, com_ids[i]), com); } +/* the ports that the last search found (ports.cpp) */ +static PortInfo found[32]; +static int n_found; + +/* Searches the ports again, and puts their names in the Port box. + With no ports found on Windows, the box has COM1 to COM16. The text + of the box stays current. */ +static void fill_ports(HWND d, const char *current) +{ + n_found = ports_list(found, (int)(sizeof(found) / sizeof(found[0]))); + HWND port = GetDlgItem(d, IDC_L_PORT); + SendMessage(port, CB_RESETCONTENT, 0, 0); + for (int i = 0; i < n_found; i++) + SendMessage(port, CB_ADDSTRING, 0, (LPARAM)found[i].name); +#ifndef DMODEM_WINPORT + for (int i = 1; n_found == 0 && i <= 16; i++) { + char n[8]; + _snprintf(n, sizeof(n), "COM%d", i); + n[sizeof(n) - 1] = '\0'; + SendMessage(port, CB_ADDSTRING, 0, (LPARAM)n); + } +#endif + SetWindowText(port, current); +} + +/* Shows what is on port name (from the last search) below the serial + port settings, or nothing when the app does not know. */ +static void show_port(HWND d, const char *name) +{ + char t[200] = ""; + for (int i = 0; i < n_found; i++) { + if (_stricmp(found[i].name, name) || !found[i].desc[0]) + continue; + _snprintf(t, sizeof(t), "%s: %s", found[i].name, found[i].desc); + t[sizeof(t) - 1] = '\0'; + } + SetDlgItemText(d, IDC_L_FOUND, t); +} + +/* Searches for a modem (the Look for a modem button, and when the + dialog opens: quiet). When it finds one and quiet is false, it + selects the serial port line, the port and hardware flow control. + It shows the result below the serial port settings. */ +static void look(HWND d, bool quiet) +{ + char current[64]; + GetDlgItemText(d, IDC_L_PORT, current, sizeof(current)); + fill_ports(d, current); + char t[256]; + const PortInfo *m = n_found > 0 && found[0].modem ? &found[0] : NULL; + if (m && quiet) { + if (!_stricmp(m->name, current)) { + show_port(d, current); + return; + } + _snprintf(t, sizeof(t), "Found %s on %s. Click Look for a " + "modem to use it.", m->desc[0] ? m->desc : "a modem", + m->name); + } else if (m) { + CheckRadioButton(d, IDC_L_DMODEMD, IDC_L_COM, IDC_L_COM); + SetDlgItemText(d, IDC_L_PORT, m->name); + SendDlgItemMessage(d, IDC_L_FLOW, CB_SETCURSEL, 2, 0); + enable(d); + _snprintf(t, sizeof(t), "Found %s on %s.", + m->desc[0] ? m->desc : "a modem", m->name); + } else { + ports_problem(t, sizeof(t)); + if (!t[0] && quiet) { + show_port(d, current); + return; + } + if (!t[0] && n_found == 0) { + _snprintf(t, sizeof(t), "No modem found. This computer " + "has no serial ports that the app can see."); + } else if (!t[0]) { + int o = _snprintf(t, sizeof(t), "No modem found. The " + "ports:"); + for (int i = 0; i < n_found && o > 0 && + o < (int)sizeof(t) - 2; i++) + o += _snprintf(t + o, sizeof(t) - (size_t)o, + "%s %s", i ? "," : "", + found[i].name); + } + } + t[sizeof(t) - 1] = '\0'; + SetDlgItemText(d, IDC_L_FOUND, t); +} + static void check(HWND d, int id, int on) { CheckDlgButton(d, id, on ? BST_CHECKED : BST_UNCHECKED); @@ -332,14 +420,7 @@ static BOOL CALLBACK line_proc(HWND d, UINT m, WPARAM wp, LPARAM lp) SetDlgItemText(d, IDC_L_USB, c->usb); SetDlgItemText(d, IDC_L_PATH, c->dmodemd); SetDlgItemText(d, IDC_L_ADDRESS, c->tcp); - HWND port = GetDlgItem(d, IDC_L_PORT); - for (int i = 1; i <= 16; i++) { - char n[8]; - _snprintf(n, sizeof(n), "COM%d", i); - n[sizeof(n) - 1] = '\0'; - SendMessage(port, CB_ADDSTRING, 0, (LPARAM)n); - } - SetWindowText(port, c->com); + SetDlgItemText(d, IDC_L_PORT, c->com); HWND bps = GetDlgItem(d, IDC_L_BPS); for (int i = 0; bps_list[i]; i++) { char n[16]; @@ -355,6 +436,7 @@ static BOOL CALLBACK line_proc(HWND d, UINT m, WPARAM wp, LPARAM lp) fill(d, IDC_L_FLOW, flow_names, c->com_flow); SetDlgItemText(d, IDC_L_INIT, c->init); enable(d); + look(d, true); return TRUE; } case WM_COMMAND: @@ -368,6 +450,23 @@ static BOOL CALLBACK line_proc(HWND d, UINT m, WPARAM wp, LPARAM lp) case IDC_L_BROWSE: browse(d); return TRUE; + case IDC_L_FIND: + look(d, false); + return TRUE; + case IDC_L_PORT: + if (HIWORD(wp) == CBN_SELCHANGE) { + char n[64] = ""; + int i = (int)SendDlgItemMessage(d, IDC_L_PORT, + CB_GETCURSEL, 0, 0); + if (i >= 0 && SendDlgItemMessage(d, IDC_L_PORT, + CB_GETLBTEXTLEN, (WPARAM)i, 0) < + (LRESULT)sizeof(n)) + SendDlgItemMessage(d, IDC_L_PORT, + CB_GETLBTEXT, (WPARAM)i, + (LPARAM)n); + show_port(d, n); + } + return TRUE; case IDOK: c->line = checked(d, IDC_L_TCP) ? LINE_TCP : checked(d, IDC_L_COM) ? LINE_COM : LINE_DMODEMD; diff --git a/windows/src/link.cpp b/windows/src/link.cpp index 2717bee..db75e50 100644 --- a/windows/src/link.cpp +++ b/windows/src/link.cpp @@ -168,8 +168,12 @@ static bool open_tcp(const char *spec) static bool open_com(const char *name) { - char path[64]; - _snprintf(path, sizeof(path), "\\\\.\\%s", name); + /* (a device path on Linux and macOS, winport/: as it is) */ + char path[96]; + if (name[0] == '/') + _snprintf(path, sizeof(path), "%s", name); + else + _snprintf(path, sizeof(path), "\\\\.\\%s", name); path[sizeof(path) - 1] = '\0'; com = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); diff --git a/windows/src/ports.cpp b/windows/src/ports.cpp new file mode 100644 index 0000000..1b46f58 --- /dev/null +++ b/windows/src/ports.cpp @@ -0,0 +1,474 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (C) 2026 D-Modem Authors + * + * ports.cpp - the serial ports of this computer and the modems on them, + * for the Line Settings dialog ("Look for a modem"). + * + * - Windows: the modems that Windows knows (the Modem class in the + * registry, AttachedTo), the MultiTech USB modems (Enum\USB, NT), + * and the ports of HARDWARE\DEVICEMAP\SERIALCOMM. If the registry + * has no ports, the app tries COM1 to COM16. + * - Linux (winport/): /sys/class/tty (ttyUSB and ttyACM), with the + * USB maker and product of each port. A MultiTech MT9234MU with no + * firmware has no port: ports_problem() tells you what to do. + * - macOS (winport/): the /dev/cu.* devices. + */ +#include "app.h" + +#include + +#ifdef DMODEM_WINPORT +#include +#include +#include +#include +#endif + +/* The MultiTech modems with a TI TUSB3410 USB UART (vendor 06E0) */ +static const struct { + const char *product, *name; +} multitech[] = { + { "f114", "MultiTech MT9234MU" }, + { "f115", "MultiTech MT9234ZBA-USB" }, + { "0319", "MultiTech MT9234ZBA-USB" }, +}; +#define N_MULTITECH (int)(sizeof(multitech) / sizeof(multitech[0])) + +/* Copies at most n - 1 characters of src to dst, and ends dst with a + NUL. */ +static void copy(char *dst, const char *src, int n) +{ + if (n <= 0) + return; + strncpy(dst, src ? src : "", (size_t)n - 1); + dst[n - 1] = '\0'; +} + +/* Gives the index of the port with this name in list, or -1. */ +static int find(const PortInfo *list, int count, const char *name) +{ + for (int i = 0; i < count; i++) + if (!_stricmp(list[i].name, name)) + return i; + return -1; +} + +/* Adds a port to list (or changes the port with the same name). A + description replaces an empty one. modem marks the port as a modem. + Gives the new count. */ +static int add(PortInfo *list, int count, int max, const char *name, + const char *desc, int modem) +{ + if (!name || !*name) + return count; + int i = find(list, count, name); + if (i < 0) { + if (count >= max) + return count; + i = count++; + memset(&list[i], 0, sizeof(list[i])); + copy(list[i].name, name, sizeof(list[i].name)); + } + if (desc && *desc && (!list[i].desc[0] || modem)) + copy(list[i].desc, desc, sizeof(list[i].desc)); + if (modem) + list[i].modem = 1; + return count; +} + +/* Puts the modems first, and keeps the order of the other ports. */ +static void modems_first(PortInfo *list, int count) +{ + for (int i = 1; i < count; i++) { + if (!list[i].modem) + continue; + PortInfo p = list[i]; + int j = i; + while (j > 0 && !list[j - 1].modem) { + list[j] = list[j - 1]; + j--; + } + list[j] = p; + } +} + +#ifndef DMODEM_WINPORT + +/* Reads the string value name of key k into out (n bytes). Gives true + if the value is there. */ +static bool reg_string(HKEY k, const char *name, char *out, int n) +{ + DWORD type = 0, size = (DWORD)n; + out[0] = '\0'; + if (RegQueryValueEx(k, name, NULL, &type, (BYTE *)out, &size) != + ERROR_SUCCESS || type != REG_SZ) + return false; + out[n - 1] = '\0'; + return true; +} + +/* Adds the modems of the Modem class under root\path: each subkey that + has AttachedTo (the port) and FriendlyName or DriverDesc (the + name). Windows 95, 98 and ME keep the class in + System\CurrentControlSet\Services\Class\Modem, and NT in + SYSTEM\CurrentControlSet\Control\Class\{4D36E96D-...}. */ +static int class_modems(PortInfo *list, int count, int max, const char *path) +{ + HKEY cls; + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &cls) != + ERROR_SUCCESS) + return count; + char sub[64]; + for (DWORD i = 0;; i++) { + DWORD len = sizeof(sub); + if (RegEnumKeyEx(cls, i, sub, &len, NULL, NULL, NULL, NULL) != + ERROR_SUCCESS) + break; + HKEY k; + if (RegOpenKeyEx(cls, sub, 0, KEY_READ, &k) != ERROR_SUCCESS) + continue; + char port[64], name[128]; + if (reg_string(k, "AttachedTo", port, sizeof(port)) && + !strncmp(port, "COM", 3)) { + if (!reg_string(k, "FriendlyName", name, sizeof(name))) + reg_string(k, "DriverDesc", name, sizeof(name)); + count = add(list, count, max, port, name, 1); + } + RegCloseKey(k); + } + RegCloseKey(cls); + return count; +} + +/* Adds the MultiTech USB modems that NT knows: + Enum\USB\VID_06E0&PID_xxxx\\Device Parameters, PortName. */ +static int usb_modems(PortInfo *list, int count, int max) +{ + for (int m = 0; m < N_MULTITECH; m++) { + char path[160]; + _snprintf(path, sizeof(path), + "SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_06E0&PID_%s", + multitech[m].product); + path[sizeof(path) - 1] = '\0'; + HKEY dev; + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &dev) != + ERROR_SUCCESS) + continue; + char inst[128]; + for (DWORD i = 0;; i++) { + DWORD len = sizeof(inst); + if (RegEnumKeyEx(dev, i, inst, &len, NULL, NULL, NULL, + NULL) != ERROR_SUCCESS) + break; + char sub[200]; + _snprintf(sub, sizeof(sub), "%s\\Device Parameters", inst); + sub[sizeof(sub) - 1] = '\0'; + HKEY k; + if (RegOpenKeyEx(dev, sub, 0, KEY_READ, &k) != ERROR_SUCCESS) + continue; + char port[64]; + if (reg_string(k, "PortName", port, sizeof(port))) + count = add(list, count, max, port, + multitech[m].name, 1); + RegCloseKey(k); + } + RegCloseKey(dev); + } + return count; +} + +/* Adds the ports of HARDWARE\DEVICEMAP\SERIALCOMM (the value data is + the port name). Gives the new count. */ +static int devicemap_ports(PortInfo *list, int count, int max) +{ + HKEY k; + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", + 0, KEY_READ, &k) != ERROR_SUCCESS) + return count; + for (DWORD i = 0;; i++) { + char name[128], data[64]; + DWORD nlen = sizeof(name), dlen = sizeof(data), type = 0; + if (RegEnumValue(k, i, name, &nlen, NULL, &type, (BYTE *)data, + &dlen) != ERROR_SUCCESS) + break; + if (type != REG_SZ) + continue; + data[sizeof(data) - 1] = '\0'; + count = add(list, count, max, data, NULL, 0); + } + RegCloseKey(k); + return count; +} + +/* Adds COM1 to COM16 when they open, or when another program has them + open (access denied). For a Windows with no SERIALCOMM key. */ +static int probe_ports(PortInfo *list, int count, int max) +{ + for (int i = 1; i <= 16; i++) { + char name[8], path[16]; + _snprintf(name, sizeof(name), "COM%d", i); + name[sizeof(name) - 1] = '\0'; + _snprintf(path, sizeof(path), "\\\\.\\%s", name); + path[sizeof(path) - 1] = '\0'; + HANDLE h = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, + NULL, OPEN_EXISTING, 0, NULL); + if (h != INVALID_HANDLE_VALUE) { + CloseHandle(h); + count = add(list, count, max, name, NULL, 0); + } else if (GetLastError() == ERROR_ACCESS_DENIED) { + count = add(list, count, max, name, "in use", 0); + } + } + return count; +} + +int ports_list(PortInfo *out, int max) +{ + int count = 0; + count = class_modems(out, count, max, + "SYSTEM\\CurrentControlSet\\Control\\Class\\" + "{4D36E96D-E325-11CE-BFC1-08002BE10318}"); + count = class_modems(out, count, max, + "System\\CurrentControlSet\\Services\\Class\\Modem"); + count = usb_modems(out, count, max); + int known = count; + count = devicemap_ports(out, count, max); + if (count == known) + count = probe_ports(out, count, max); + modems_first(out, count); + return count; +} + +void ports_problem(char *out, int n) +{ + if (n > 0) + out[0] = '\0'; +} + +#elif defined(__APPLE__) + +int ports_list(PortInfo *out, int max) +{ + int count = 0; + DIR *d = opendir("/dev"); + if (!d) + return 0; + struct dirent *e; + while ((e = readdir(d)) != NULL) { + const char *n = e->d_name; + if (strncmp(n, "cu.", 3) || strstr(n, "Bluetooth") || + strstr(n, "debug-console")) + continue; + char path[64]; + _snprintf(path, sizeof(path), "/dev/%s", n); + path[sizeof(path) - 1] = '\0'; + /* (CDC-ACM modems are cu.usbmodem*: the MT9234MU-CDC + among them) */ + int modem = !strncmp(n, "cu.usbmodem", 11); + count = add(out, count, max, path, + modem ? "USB modem" : + !strncmp(n, "cu.usbserial", 12) ? "USB serial port" : + NULL, modem); + } + closedir(d); + modems_first(out, count); + return count; +} + +void ports_problem(char *out, int n) +{ + if (n > 0) + out[0] = '\0'; +} + +#else /* Linux */ + +/* the root of sysfs (a test can give another one) */ +#ifndef PORTS_SYSFS +#define PORTS_SYSFS "/sys" +#endif + +/* Reads the first line of the sysfs file dir/name into out (n bytes), + with no space at the end. Gives true if the file is there. */ +static bool attr(const char *dir, const char *name, char *out, int n) +{ + char path[PATH_MAX]; + _snprintf(path, sizeof(path), "%s/%s", dir, name); + path[sizeof(path) - 1] = '\0'; + out[0] = '\0'; + FILE *f = fopen(path, "r"); + if (!f) + return false; + if (!fgets(out, n, f)) + out[0] = '\0'; + fclose(f); + size_t l = strlen(out); + while (l > 0 && (unsigned char)out[l - 1] <= ' ') + out[--l] = '\0'; + return true; +} + +/* Gives the name of the MultiTech model for USB product pid (vendor + 06E0), or NULL. */ +static const char *multitech_name(const char *vid, const char *pid) +{ + if (_stricmp(vid, "06e0")) + return NULL; + for (int m = 0; m < N_MULTITECH; m++) + if (!_stricmp(pid, multitech[m].product)) + return multitech[m].name; + return NULL; +} + +/* Tells if the text has "modem" in it, in upper or lower case. */ +static bool says_modem(const char *s) +{ + for (; *s; s++) + if (!_strnicmp(s, "modem", 5)) + return true; + return false; +} + +int ports_list(PortInfo *out, int max) +{ + int count = 0; + DIR *d = opendir(PORTS_SYSFS "/class/tty"); + if (!d) + return 0; + struct dirent *e; + while ((e = readdir(d)) != NULL) { + const char *n = e->d_name; + if (strncmp(n, "ttyUSB", 6) && strncmp(n, "ttyACM", 6)) + continue; + char link[PATH_MAX], itf[PATH_MAX], usb[PATH_MAX]; + _snprintf(link, sizeof(link), PORTS_SYSFS "/class/tty/%s/device", n); + link[sizeof(link) - 1] = '\0'; + char desc[128] = ""; + int modem = 0; + /* (device is the USB interface; its parent is the USB + device, with idVendor, idProduct, manufacturer and + product) */ + if (realpath(link, itf)) { + _snprintf(usb, sizeof(usb), "%s/..", itf); + usb[sizeof(usb) - 1] = '\0'; + char vid[16], pid[16], maker[64], product[64]; + attr(usb, "idVendor", vid, sizeof(vid)); + attr(usb, "idProduct", pid, sizeof(pid)); + attr(usb, "manufacturer", maker, sizeof(maker)); + attr(usb, "product", product, sizeof(product)); + const char *mt = multitech_name(vid, pid); + if (mt) { + copy(desc, mt, sizeof(desc)); + modem = 1; + } else if (maker[0] || product[0]) { + _snprintf(desc, sizeof(desc), "%s%s%s", maker, + maker[0] && product[0] ? " " : "", + product); + desc[sizeof(desc) - 1] = '\0'; + modem = says_modem(desc); + } + } + char path[64]; + _snprintf(path, sizeof(path), "/dev/%s", n); + path[sizeof(path) - 1] = '\0'; + count = add(out, count, max, path, desc, modem); + } + closedir(d); + modems_first(out, count); + return count; +} + +/* The places where Linux looks for the firmware of the MT9234MU */ +static const char *const firmware[] = { + "/lib/firmware/updates/mts_mt9234mu.fw", + "/lib/firmware/mts_mt9234mu.fw", + "/usr/lib/firmware/updates/mts_mt9234mu.fw", + "/usr/lib/firmware/mts_mt9234mu.fw", + "/lib/firmware/mts_mt9234zba.fw", + "/usr/lib/firmware/mts_mt9234zba.fw", + NULL +}; + +/* Tells if Linux has the firmware of the MT9234MU. */ +static bool firmware_present() +{ + struct stat st; + for (int i = 0; firmware[i]; i++) + if (stat(firmware[i], &st) == 0) + return true; + return false; +} + +/* Gives true if USB device dir has a tty in one of its interfaces + ("1-2:1.0/ttyUSB0", or "1-2:1.0/tty/ttyACM0"). */ +static bool has_tty(const char *dir) +{ + DIR *d = opendir(dir); + if (!d) + return false; + bool found = false; + struct dirent *e; + while (!found && (e = readdir(d)) != NULL) { + if (!strchr(e->d_name, ':')) + continue; + char itf[PATH_MAX]; + _snprintf(itf, sizeof(itf), "%s/%s", dir, e->d_name); + itf[sizeof(itf) - 1] = '\0'; + DIR *i = opendir(itf); + struct dirent *t; + while (i && !found && (t = readdir(i)) != NULL) + if (!strncmp(t->d_name, "tty", 3)) + found = true; + if (i) + closedir(i); + } + closedir(d); + return found; +} + +void ports_problem(char *out, int n) +{ + if (n <= 0) + return; + out[0] = '\0'; + DIR *d = opendir(PORTS_SYSFS "/bus/usb/devices"); + if (!d) + return; + struct dirent *e; + while ((e = readdir(d)) != NULL) { + char dir[PATH_MAX], vid[16], pid[16], nc[8]; + _snprintf(dir, sizeof(dir), PORTS_SYSFS "/bus/usb/devices/%s", e->d_name); + dir[sizeof(dir) - 1] = '\0'; + if (!attr(dir, "idVendor", vid, sizeof(vid)) || + !attr(dir, "idProduct", pid, sizeof(pid))) + continue; + const char *mt = multitech_name(vid, pid); + if (!mt || has_tty(dir)) + continue; + attr(dir, "bNumConfigurations", nc, sizeof(nc)); + /* (one configuration: the boot ROM of the TUSB3410, with no + firmware loaded) */ + bool no_fw = !strcmp(nc, "1"); + const char *to = strstr(mt, "ZBA") ? + "/lib/firmware/mts_mt9234zba.fw" : "/lib/firmware/"; + if (no_fw && !firmware_present()) + _snprintf(out, (size_t)n, + "%s: no firmware. Copy mts_mt9234mu.fw " + "(share/dmodem-gui/firmware) to %s, then " + "connect the modem again.", mt, to); + else if (no_fw) + _snprintf(out, (size_t)n, + "%s: no firmware. Load the ti_usb_3410_5052 " + "driver, then connect the modem again.", mt); + else + _snprintf(out, (size_t)n, + "%s: no serial port. Load the " + "ti_usb_3410_5052 driver.", mt); + out[n - 1] = '\0'; + break; + } + closedir(d); +} + +#endif diff --git a/windows/src/resource.h b/windows/src/resource.h index 80aaf38..48ccc81 100644 --- a/windows/src/resource.h +++ b/windows/src/resource.h @@ -148,6 +148,8 @@ #define IDC_L_PARITY 921 #define IDC_L_STOP 922 #define IDC_L_FLOW 923 +#define IDC_L_FIND 924 +#define IDC_L_FOUND 925 /* the Change Setting dialog */ #define IDD_SETTING 950