diff mbox series

[U-Boot,RFC,v2,05/20] fastboot: Introduce fastboot_response and refactor fastboot_okay/fail

Message ID 1525077174-6211-6-git-send-email-alex.kiernan@gmail.com
State RFC
Delegated to: Lukasz Majewski
Headers show
Series Add fastboot UDP support | expand

Commit Message

Alex Kiernan April 30, 2018, 8:32 a.m. UTC
Introduce fastboot_response which takes varargs parameters so we can
use it to generate formatted response strings. Refactor fastboot_okay/fail
to use it.

Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
---

Changes in v2: None

 drivers/fastboot/fb_common.c | 28 ++++++++++++++++++++++++----
 include/fastboot.h           |  4 ++++
 2 files changed, 28 insertions(+), 4 deletions(-)

Comments

Joe Hershberger May 3, 2018, 6:28 p.m. UTC | #1
On Mon, Apr 30, 2018 at 3:32 AM, Alex Kiernan <alex.kiernan@gmail.com> wrote:
> Introduce fastboot_response which takes varargs parameters so we can
> use it to generate formatted response strings. Refactor fastboot_okay/fail
> to use it.
>
> Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>

Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>
diff mbox series

Patch

diff --git a/drivers/fastboot/fb_common.c b/drivers/fastboot/fb_common.c
index 53cffe3..fe58803 100644
--- a/drivers/fastboot/fb_common.c
+++ b/drivers/fastboot/fb_common.c
@@ -13,14 +13,34 @@ 
 #include <common.h>
 #include <fastboot.h>
 
+/**
+ * Writes a response to response buffer of the form "$tag$reason".
+ *
+ * @param tag		The first part of the response
+ * @param response	Pointer to fastboot response buffer
+ * @param format	printf style format string
+ */
+void fastboot_response(const char *tag, char *response,
+		       const char *format, ...)
+{
+	va_list args;
+
+	strlcpy(response, tag, FASTBOOT_RESPONSE_LEN);
+	if (format) {
+		va_start(args, format);
+		vsnprintf(response + strlen(response),
+			  FASTBOOT_RESPONSE_LEN - strlen(response) - 1,
+			  format, args);
+		va_end(args);
+	}
+}
+
 void fastboot_fail(const char *reason, char *response)
 {
-	strncpy(response, "FAIL\0", 5);
-	strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
+	fastboot_response("FAIL", response, "%s", reason);
 }
 
 void fastboot_okay(const char *reason, char *response)
 {
-	strncpy(response, "OKAY\0", 5);
-	strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
+	fastboot_response("OKAY", response, "%s", reason);
 }
diff --git a/include/fastboot.h b/include/fastboot.h
index f22080a..2140c94 100644
--- a/include/fastboot.h
+++ b/include/fastboot.h
@@ -16,6 +16,10 @@ 
 /* The 64 defined bytes plus \0 */
 #define FASTBOOT_RESPONSE_LEN	(64 + 1)
 
+void fastboot_response(const char *tag, char *response,
+		       const char *format, ...)
+	__attribute__ ((format (__printf__, 3, 4)));
+
 void fastboot_fail(const char *reason, char *response);
 void fastboot_okay(const char *reason, char *response);