From ead759007999573bf37d1ea01bf19ae76af79e01 Mon Sep 17 00:00:00 2001
From: roly <Rolyli.Li@luxshare-ict.com>
Date: Wed, 20 Nov 2024 15:16:53 +0800
Subject: [PATCH] Support SOL non-volatile bitrate and volatile bitrate

---
 console-dbus.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++----
 meson.build    |  2 +-
 2 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/console-dbus.c b/console-dbus.c
index dd53908..e0c0011 100644
--- a/console-dbus.c
+++ b/console-dbus.c
@@ -19,6 +19,7 @@
 #include <err.h>
 #include <string.h>
 #include <sys/socket.h>
+#include <stdlib.h>
 
 #include "console-server.h"
 
@@ -53,7 +54,7 @@ static void tty_change_baudrate(struct console *console)
 	}
 }
 
-static int set_baud_handler(sd_bus *bus, const char *path,
+static int set_non_volatile_baud_handler(sd_bus *bus, const char *path,
 			    const char *interface, const char *property,
 			    sd_bus_message *msg, void *userdata,
 			    sd_bus_error *err __attribute__((unused)))
@@ -61,6 +62,8 @@ static int set_baud_handler(sd_bus *bus, const char *path,
 	struct console *console = userdata;
 	uint64_t baudrate;
 	speed_t speed;
+	char buf[64] = {0};
+	int encodedBitRate = 0;
 	int r;
 
 	if (!console) {
@@ -84,6 +87,63 @@ static int set_baud_handler(sd_bus *bus, const char *path,
 
 	sd_bus_emit_properties_changed(bus, path, interface, property, NULL);
 
+    switch(baudrate)
+    {
+        case 9600:
+            encodedBitRate = 0x06;
+            break;
+        case 19200:
+            encodedBitRate = 0x07;
+            break;
+        case 38400:
+            encodedBitRate = 0x08;
+            break;
+        case 57600:
+            encodedBitRate = 0x09;
+            break;
+        case 115200:
+            encodedBitRate = 0xa;
+            break;
+        default:
+            encodedBitRate = 0xa;
+            break;
+    }
+    snprintf(buf, sizeof(buf), "fw_setenv hostserialcfg %d", encodedBitRate);
+    system(buf);
+    system("/usr/bin/sol-configure.sh setup");
+	return 1;
+}
+
+static int set_volatile_baud_handler(sd_bus *bus, const char *path,
+			    const char *interface, const char *property,
+			    sd_bus_message *msg, void *userdata,
+			    sd_bus_error *err __attribute__((unused)))
+{
+	struct console *console = userdata;
+	uint64_t baudrate;
+	speed_t speed;
+	int r;
+
+	if (!console) {
+		return -ENOENT;
+	}
+
+	r = sd_bus_message_read(msg, "t", &baudrate);
+	if (r < 0 || baudrate > UINT32_MAX) {
+		return -EINVAL;
+	}
+
+	speed = parse_int_to_baud((uint32_t)baudrate);
+	if (!speed) {
+		warnx("Invalid baud rate: '%" PRIu64 "'", baudrate);
+		return -EINVAL;
+	}
+
+	assert(console->tty.type == TTY_DEVICE_UART);
+	console->tty.uart.baud = speed;
+	tty_change_baudrate(console);
+
+	sd_bus_emit_properties_changed(bus, path, interface, property, NULL);
 	return 1;
 }
 
@@ -142,10 +202,14 @@ static int method_connect(sd_bus_message *msg, void *userdata,
 
 static const sd_bus_vtable console_uart_vtable[] = {
 	SD_BUS_VTABLE_START(0),
-	SD_BUS_WRITABLE_PROPERTY("Baud", "t", get_baud_handler,
-				 set_baud_handler, 0,
-				 SD_BUS_VTABLE_UNPRIVILEGED |
-					 SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+    SD_BUS_WRITABLE_PROPERTY("NonVbitrate", "t", get_baud_handler,
+                set_non_volatile_baud_handler, 0,
+                SD_BUS_VTABLE_UNPRIVILEGED |
+                    SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+    SD_BUS_WRITABLE_PROPERTY("Vbitrate", "t", get_baud_handler,
+                set_volatile_baud_handler, 0,
+                SD_BUS_VTABLE_UNPRIVILEGED |
+                    SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
 	SD_BUS_VTABLE_END,
 };
 
diff --git a/meson.build b/meson.build
index 8a570a1..c4e8cf0 100644
--- a/meson.build
+++ b/meson.build
@@ -10,7 +10,7 @@ project('obmc-console', 'c',
     meson_version: '>=0.63.0',
 )
 
-add_project_arguments('-D_GNU_SOURCE', language: 'c')
+add_project_arguments('-D_GNU_SOURCE', '-Wno-unused-result', language: 'c')
 
 systemdsystemunitdir = dependency('systemd').get_variable('systemdsystemunitdir')
 install_data('conf/obmc-console@.service.in',
-- 
2.25.1

