[OpenWrt-Devel] [PATCH] mbim-proxy support

Bjørn Mork bjorn at mork.no
Fri Feb 15 08:24:57 EST 2019


This adds support for the libmbim-glib "mbim-proxy", allowing umbim to access
a device under ModemManager control. The feature is mostly useful for debugging
and development purposes, and is therefore disabled by default.

Signed-off-by: Bjørn Mork <bjorn at mork.no>
---
Probably not very useful for most users, but it's a feature I've been
missing every time I have been testing or developing umbim.  I often
use the built-in modem in my laptop for quick MBIM testing. But this
modem is normally under ModemManager control and connected.  Disconnecting
and stopping ModemManager is unnecessary hassle just to send a few simple
MBIM requests using umbim.

The patch makes it possible for umbim to co-exist peacefully with
ModemManager, mixing and matching mmcli, mbimcli and umbim requests:

root at miraculix:/tmp# mmcli -b 3
  --------------------------------
  General            |  dbus path: /org/freedesktop/ModemManager1/Bearer/3
                     |       type: default
  --------------------------------
  Status             |  connected: yes
                     |  suspended: no
                     |  interface: wwan0
                     | ip timeout: 20
  --------------------------------
  Properties         |        apn: telenor.smart
                     |    roaming: allowed
                     |    ip type: ipv4v6
  --------------------------------
  IPv4 configuration |     method: static
                     |    address: 10.180.17.174
                     |     prefix: 30
                     |    gateway: 10.180.17.173
                     |        dns: 193.213.112.4, 130.67.15.198
                     |        mtu: 1500
  --------------------------------
  IPv6 configuration |     method: static
                     |    address: 2a02:2121:2c1:c222:4d24:9fd0:a5d8:9f8
                     |     prefix: 64
                     |    gateway: 2a02:2121:2c1:c222:2070:97f2:61dc:914f
                     |        dns: 2001:4600:4:fff::52, 2001:4600:4:1fff::52
                     |        mtu: 1500
  --------------------------------
  Statistics         |   duration: 18270


root at miraculix:/tmp# umbim -p -d /dev/cdc-wdm0 config
  ipv4address: 10.180.17.174/30
  ipv4gateway: 10.180.17.173
  ipv4mtu: 1500
  ipv4dnsserver: 193.213.112.4
  ipv4dnsserver: 130.67.15.198
  ipv6address: 2a02:2121:2c1:c222:65c0:d142:7f38:1546/64
  ipv6gateway: 2a02:2121:2c1:c222:2070:97f2:61dc:914f
  ipv6mtu: 1500
  ipv6dnsserver: 2001:4600:4:fff:0:0:0:52
  ipv6dnsserver: 2001:4600:4:1fff:0:0:0:52


root at miraculix:/tmp# mbimcli -p -d /dev/cdc-wdm0 --query-ip-configuration

[/dev/cdc-wdm0] IPv4 configuration available: 'address, gateway, dns, mtu'
     IP [0]: '10.180.17.174/30'
    Gateway: '10.180.17.173'
    DNS [0]: '193.213.112.4'
    DNS [1]: '130.67.15.198'
        MTU: '1500'

[/dev/cdc-wdm0] IPv6 configuration available: 'address, gateway, dns, mtu'
     IP [0]: '2a02:2121:2c1:c222:fc7e:3717:fb01:98e3/64'
    Gateway: '2a02:2121:2c1:c222:2070:97f2:61dc:914f'
    DNS [0]: '2001:4600:4:fff::52'
    DNS [1]: '2001:4600:4:1fff::52'
        MTU: '1500'


 CMakeLists.txt                    |  7 ++++++
 cli.c                             | 18 +++++++++++++-
 data/mbim-service-proxy-control.h |  5 ++++
 mbim-dev.c                        | 49 +++++++++++++++++++++++++++++++++++++++
 mbim-dev.h                        |  3 +++
 5 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c210d6f828fb..bc18c1f6d0d7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,9 @@
 cmake_minimum_required(VERSION 2.6)
 
 PROJECT(umbim C)
+
+OPTION(PROXY OFF)
+
 ADD_DEFINITIONS(-Os -ggdb -Wall -Werror --std=gnu99 -Wmissing-declarations)
 
 SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
@@ -13,6 +16,10 @@ IF(DEBUG)
   ADD_DEFINITIONS(-DDEBUG -g3)
 ENDIF()
 
+IF(PROXY)
+  ADD_DEFINITIONS(-DLIBQMI_MBIM_PROXY)
+ENDIF()
+
 ADD_EXECUTABLE(umbim ${SOURCES})
 
 TARGET_LINK_LIBRARIES(umbim ${LIBS})
diff --git a/cli.c b/cli.c
index 1dd6330b8113..44c063513cfd 100644
--- a/cli.c
+++ b/cli.c
@@ -484,6 +484,9 @@ usage(void)
 {
 	fprintf(stderr, "Usage: umbim <caps|pinstate|unlock|registration|subscriber|attach|detach|connect|disconnect|config|radio> [options]\n"
 		"Options:\n"
+#ifdef LIBQMI_MBIM_PROXY
+		"    -p			use mbim-proxy\n"
+#endif
 		"    -d <device>	the device (/dev/cdc-wdmX)\n"
 		"    -t <transaction>	the transaction id\n"
 		"    -n 		no close\n\n"
@@ -496,8 +499,11 @@ main(int argc, char **argv)
 {
 	char *cmd, *device = NULL;
 	int no_open = 0, ch, i;
+#ifdef LIBQMI_MBIM_PROXY
+	int proxy = 0;
+#endif
 
-	while ((ch = getopt(argc, argv, "nvd:t:")) != -1) {
+	while ((ch = getopt(argc, argv, "pnvd:t:")) != -1) {
 		switch (ch) {
 		case 'v':
 			verbose = 1;
@@ -512,6 +518,11 @@ main(int argc, char **argv)
 			no_open = 1;
 			transaction_id = atoi(optarg);
 			break;
+#ifdef LIBQMI_MBIM_PROXY
+		case 'p':
+			proxy = 1;
+			break;
+#endif
 		default:
 			return usage();
 		}
@@ -535,6 +546,11 @@ main(int argc, char **argv)
 
 	uloop_init();
 
+#ifdef LIBQMI_MBIM_PROXY
+	if (proxy)
+		mbim_proxy_open(device);
+	else
+#endif
 	mbim_open(device);
 	if (!no_open)
 		mbim_send_open_msg();
diff --git a/data/mbim-service-proxy-control.h b/data/mbim-service-proxy-control.h
index 7d81e7d2daf6..227b6253a512 100644
--- a/data/mbim-service-proxy-control.h
+++ b/data/mbim-service-proxy-control.h
@@ -5,3 +5,8 @@
 
 #define MBIM_CMD_PROXY_CONTROL_CONFIGURATION	1
 
+struct mbim_proxy_control_configuration_s {
+	struct mbim_string devicepath;
+	uint32_t timeout;
+} __attribute__((packed));
+
diff --git a/mbim-dev.c b/mbim-dev.c
index dd1110daeb52..170de2dde198 100644
--- a/mbim-dev.c
+++ b/mbim-dev.c
@@ -27,6 +27,15 @@
 
 #include "mbim.h"
 
+
+#ifdef LIBQMI_MBIM_PROXY
+#include <sys/socket.h>
+#include <sys/un.h>
+#include "data/mbim-service-proxy-control.h"
+
+uint8_t proxy_control[16] = { 0x83, 0x8c, 0xf7, 0xfb, 0x8d, 0x0d, 0x4d, 0x7f, 0x87, 0x1e, 0xd7, 0x1d, 0xbe, 0xfb, 0xb3, 0x9b };
+#endif
+
 size_t mbim_bufsize = 0;
 uint8_t *mbim_buffer = NULL;
 static struct uloop_fd mbim_fd;
@@ -114,6 +123,10 @@ mbim_recv(struct uloop_fd *u, unsigned int events)
 		}
 		if (msg->status_code && !msg->buffer_length)
 			return_code = -le32toh(msg->status_code);
+#ifdef LIBQMI_MBIM_PROXY
+		else if (le32toh(msg->command_id) == MBIM_CMD_PROXY_CONTROL_CONFIGURATION && !memcmp(msg->service_id, proxy_control, 16))
+			break;
+#endif
 		else
 			return_code = current_handler->response(msg->buffer, le32toh(msg->buffer_length));
 		if (return_code < 0)
@@ -152,6 +165,42 @@ mbim_open(const char *path)
 	uloop_fd_add(&mbim_fd, ULOOP_READ);
 }
 
+#ifdef LIBQMI_MBIM_PROXY
+static int
+mbim_send_proxy_msg(const char *path)
+{
+	struct mbim_proxy_control_configuration_s *p =
+		(struct mbim_proxy_control_configuration_s *) mbim_setup_command_msg(proxy_control,
+			MBIM_MESSAGE_COMMAND_TYPE_SET, MBIM_CMD_PROXY_CONTROL_CONFIGURATION,
+			sizeof(struct mbim_proxy_control_configuration_s));
+	mbim_encode_string(&p->devicepath, (char *)path);
+	p->timeout = htole32(30); // FIXME: hard coded timeout
+	return mbim_send_command_msg();
+}
+
+void
+mbim_proxy_open(const char *path)
+{
+	struct sockaddr_un addr = { .sun_family = AF_UNIX, .sun_path = "\0mbim-proxy" };
+
+	mbim_fd.cb = mbim_recv;
+	mbim_fd.fd = socket(PF_UNIX, SOCK_STREAM, 0);
+	if (mbim_fd.fd < 1) {
+		perror("socket failed: ");
+		exit(-1);
+	}
+	if (connect(mbim_fd.fd, (struct sockaddr *)&addr, 13)) {
+		perror("failed to connect to mbim-proxy: ");
+		exit(-1);
+	}
+	mbim_bufsize = 512; // FIXME
+	mbim_buffer = malloc(mbim_bufsize);
+	uloop_fd_add(&mbim_fd, ULOOP_READ);
+	no_close = 1;
+	mbim_send_proxy_msg(path);
+}
+#endif
+
 void
 mbim_end(void)
 {
diff --git a/mbim-dev.h b/mbim-dev.h
index b7a253ca0bef..501bbf2b32d7 100644
--- a/mbim-dev.h
+++ b/mbim-dev.h
@@ -21,6 +21,9 @@ extern int no_close;
 
 int mbim_send(void);
 void mbim_open(const char *path);
+#ifdef LIBQMI_MBIM_PROXY
+void mbim_proxy_open(const char *path);
+#endif
 void mbim_end(void);
 
 #endif
-- 
2.11.0


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list