[OpenWrt-Devel] [PATCH 1/4] dynamically allocate buffer

Bjørn Mork bjorn at mork.no
Wed May 11 07:24:01 EDT 2016


Signed-off-by: Bjørn Mork <bjorn at mork.no>
---
 mbim-dev.c | 24 +++++++++++++++++++-----
 mbim-dev.h |  4 +++-
 mbim-msg.c | 14 +++++++++-----
 mbim.h     |  2 --
 4 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/mbim-dev.c b/mbim-dev.c
index d986cbe775b0..34bb2c228eb0 100644
--- a/mbim-dev.c
+++ b/mbim-dev.c
@@ -25,7 +25,8 @@
 
 #include "mbim.h"
 
-uint8_t mbim_buffer[MBIM_BUFFER_SIZE];
+size_t mbim_bufsize = 0;
+uint8_t *mbim_buffer = NULL;
 static struct uloop_fd mbim_fd;
 static uint32_t expected;
 int no_close;
@@ -33,7 +34,7 @@ int no_close;
 static void mbim_msg_tout_cb(struct uloop_timeout *t)
 {
 	fprintf(stderr, "ERROR: mbim message timeout\n");
-	uloop_end();
+	mbim_end();
 }
 
 static struct uloop_timeout tout = {
@@ -46,7 +47,7 @@ mbim_send(void)
 	struct mbim_message_header *hdr = (struct mbim_message_header *) mbim_buffer;
 	int ret = 0;
 
-	if (le32toh(hdr->length) > MBIM_BUFFER_SIZE) {
+	if (le32toh(hdr->length) > mbim_bufsize) {
 		fprintf(stderr, "message too big %d\n", le32toh(hdr->length));
 		return -1;
 	}
@@ -74,7 +75,7 @@ mbim_send(void)
 static void
 mbim_recv(struct uloop_fd *u, unsigned int events)
 {
-	ssize_t cnt = read(u->fd, mbim_buffer, MBIM_BUFFER_SIZE);
+	ssize_t cnt = read(u->fd, mbim_buffer, mbim_bufsize);
 	struct mbim_message_header *hdr = (struct mbim_message_header *) mbim_buffer;
 	struct command_done_message *msg = (struct command_done_message *) (hdr + 1);
 	int i;
@@ -118,7 +119,7 @@ mbim_recv(struct uloop_fd *u, unsigned int events)
 		mbim_send_close_msg();
 		break;
 	case MBIM_MESSAGE_TYPE_CLOSE_DONE:
-		uloop_end();
+		mbim_end();
 		break;
 	case MBIM_MESSAGE_TYPE_FUNCTION_ERROR:
 		no_close = 0;
@@ -137,5 +138,18 @@ mbim_open(const char *path)
 		perror("open failed: ");
 		exit(-1);
 	}
+	mbim_bufsize = MBIM_BUFFER_SIZE;
+	mbim_buffer = malloc(mbim_bufsize);
 	uloop_fd_add(&mbim_fd, ULOOP_READ);
 }
+
+void
+mbim_end(void)
+{
+	if (mbim_buffer) {
+		free(mbim_buffer);
+		mbim_bufsize = 0;
+		mbim_buffer = NULL;
+	}
+	uloop_end();
+}
diff --git a/mbim-dev.h b/mbim-dev.h
index 1499630e20be..b7a253ca0bef 100644
--- a/mbim-dev.h
+++ b/mbim-dev.h
@@ -15,10 +15,12 @@
 #ifndef _MBIM_DEV_H__
 #define _MBIM_DEV_H__
 
-extern uint8_t mbim_buffer[MBIM_BUFFER_SIZE];
+extern size_t mbim_bufsize;
+extern uint8_t *mbim_buffer;
 extern int no_close;
 
 int mbim_send(void);
 void mbim_open(const char *path);
+void mbim_end(void);
 
 #endif
diff --git a/mbim-msg.c b/mbim-msg.c
index ad5a2d5dcd9a..3413f5dea32a 100644
--- a/mbim-msg.c
+++ b/mbim-msg.c
@@ -143,7 +143,9 @@ mbim_setup_command_msg(uint8_t *uuid, uint32_t type, uint32_t command_id, int le
 {
 	struct command_message *cmd = (struct command_message *) mbim_buffer;
 
-	memset(mbim_buffer, 0, MBIM_BUFFER_SIZE);
+	if (!mbim_buffer)
+		return NULL;
+	memset(mbim_buffer, 0, mbim_bufsize);
 
 	cmd->fragment_header.total = htole32(1);
 	cmd->fragment_header.current = htole32(0);
@@ -153,7 +155,7 @@ mbim_setup_command_msg(uint8_t *uuid, uint32_t type, uint32_t command_id, int le
 	cmd->buffer_length = htole32(len);
 
 	payload_offset = len;
-	payload_free = MBIM_BUFFER_SIZE - (sizeof(*cmd) + len);
+	payload_free = mbim_bufsize - (sizeof(*cmd) + len);
 	payload_len = 0;
 	payload_buffer = cmd->buffer;
 
@@ -165,6 +167,8 @@ mbim_send_command_msg(void)
 {
 	struct command_message *cmd = (struct command_message *) mbim_buffer;
 
+	if (!mbim_buffer)
+		return 0;
 	if (payload_len & 0x3) {
 		payload_len &= ~0x3;
 		payload_len += 4;
@@ -182,7 +186,7 @@ mbim_send_open_msg(void)
 	struct mbim_open_message *msg = (struct mbim_open_message *) mbim_buffer;
 
 	mbim_setup_header(&msg->header, MBIM_MESSAGE_TYPE_OPEN, sizeof(*msg));
-	msg->max_control_transfer = htole32(MBIM_BUFFER_SIZE);
+	msg->max_control_transfer = htole32(mbim_bufsize);
 
 	return mbim_send();
 }
@@ -192,8 +196,8 @@ mbim_send_close_msg(void)
 {
 	struct mbim_message_header *hdr = (struct mbim_message_header *) mbim_buffer;
 
-	if (no_close) {
-		uloop_end();
+	if (no_close || !mbim_buffer) {
+		mbim_end();
 		return 0;
 	}
 	mbim_setup_header(hdr, MBIM_MESSAGE_TYPE_CLOSE, sizeof(*hdr));
diff --git a/mbim.h b/mbim.h
index 6e7e8b4d9e5e..746257eecda2 100644
--- a/mbim.h
+++ b/mbim.h
@@ -18,8 +18,6 @@
 #include <stdint.h>
 #include <sys/types.h>
 
-#define MBIM_BUFFER_SIZE	1024
-
 extern int return_code;
 extern int verbose;
 
-- 
2.1.4
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list