serial: Replace mctp_write_all with a function
Brace grouped expressions are a GNU extension so don't work with
-Wpedantic
Change-Id: I9ec2b0542251c59856be0e7470e117c76e210b95
Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
diff --git a/serial.c b/serial.c
index e2a2df2..681dc57 100644
--- a/serial.c
+++ b/serial.c
@@ -25,37 +25,6 @@
#define pr_fmt(x) "serial: " x
-/*
- * @fn: A function that will copy data from the buffer at src into the dst object
- * @dst: An opaque object to pass as state to fn
- * @src: A pointer to the buffer of data to copy to dst
- * @len: The length of the data pointed to by src
- * @return: 0 on succes, negative error code on failure
- *
- * Pre-condition: fn returns a write count or a negative error code
- * Post-condition: All bytes written or an error has occurred
- */
-#define mctp_write_all(fn, dst, src, len) \
- ({ \
- typeof(src) __src = src; \
- ssize_t wrote; \
- while (len) { \
- wrote = fn(dst, __src, len); \
- if (wrote < 0) \
- break; \
- __src += wrote; \
- len -= wrote; \
- } \
- len ? wrote : 0; \
- })
-
-static ssize_t mctp_serial_write(int fildes, const void *buf, size_t nbyte)
-{
- ssize_t wrote;
-
- return ((wrote = write(fildes, buf, nbyte)) < 0) ? -errno : wrote;
-}
-
#include "libmctp.h"
#include "libmctp-alloc.h"
#include "libmctp-log.h"
@@ -110,6 +79,40 @@
uint8_t flag;
};
+/*
+ * @fn: A function that will copy data from the buffer at src into the dst object
+ * @dst: An opaque object to pass as state to fn
+ * @src: A pointer to the buffer of data to copy to dst
+ * @len: The length of the data pointed to by src
+ * @return: 0 on succes, negative error code on failure
+ *
+ * Pre-condition: fn returns a write count or a negative error code
+ * Post-condition: All bytes written or an error has occurred
+ */
+static ssize_t mctp_write_all(mctp_serial_tx_fn fn, void *dst, uint8_t *src,
+ size_t len)
+{
+ uint8_t *__src = src;
+ ssize_t wrote;
+ while (len) {
+ wrote = fn(dst, __src, len);
+ if (wrote < 0) {
+ break;
+ }
+ __src += wrote;
+ len -= wrote;
+ }
+ return len ? wrote : 0;
+}
+
+static int mctp_serial_write(void *fildesp, void *buf, size_t nbyte)
+{
+ ssize_t wrote;
+ int fildes = *((int *)fildesp);
+
+ return ((wrote = write(fildes, buf, nbyte)) < 0) ? -errno : wrote;
+}
+
static size_t mctp_serial_pkt_escape(struct mctp_pktbuf *pkt, uint8_t *buf)
{
uint8_t total_len;
@@ -176,7 +179,7 @@
len += sizeof(*hdr) + sizeof(*tlr);
if (!serial->tx_fn)
- return mctp_write_all(mctp_serial_write, serial->fd,
+ return mctp_write_all(mctp_serial_write, &serial->fd,
&serial->txbuf[0], len);
return mctp_write_all(serial->tx_fn, serial->tx_fn_data,