aboutsummaryrefslogtreecommitdiff
path: root/src/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/io')
-rw-r--r--src/io/CMakeLists.txt15
-rw-r--r--src/io/be16.c16
-rw-r--r--src/io/be32.c17
-rw-r--r--src/io/read.c32
-rw-r--r--src/io/read_c.c28
-rw-r--r--src/io/tobe16.c17
-rw-r--r--src/io/tobe32.c19
7 files changed, 144 insertions, 0 deletions
diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt
new file mode 100644
index 0000000..b989afd
--- /dev/null
+++ b/src/io/CMakeLists.txt
@@ -0,0 +1,15 @@
+# wip, a small TCP/IP stack.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ be16.c
+ be32.c
+ read.c
+ read_c.c
+ tobe16.c
+ tobe32.c
+)
diff --git a/src/io/be16.c b/src/io/be16.c
new file mode 100644
index 0000000..d8b995a
--- /dev/null
+++ b/src/io/be16.c
@@ -0,0 +1,16 @@
+/*
+ * wip, a small TCP/IP stack.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <wip/wip.h>
+#include <wip/prv/io.h>
+
+unsigned short wip_be16(const struct wip_be16 *const v)
+{
+ return ((unsigned long)v->v[0] << 8) | v->v[1];
+}
diff --git a/src/io/be32.c b/src/io/be32.c
new file mode 100644
index 0000000..9defd06
--- /dev/null
+++ b/src/io/be32.c
@@ -0,0 +1,17 @@
+/*
+ * wip, a small TCP/IP stack.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <wip/wip.h>
+#include <wip/prv/io.h>
+
+unsigned long wip_be32(const struct wip_be32 *const v)
+{
+ return ((unsigned long)v->v[0] << 24) | ((unsigned long)v->v[1] << 16)
+ | (v->v[2] << 8) | v->v[3];
+}
diff --git a/src/io/read.c b/src/io/read.c
new file mode 100644
index 0000000..f8ee1b2
--- /dev/null
+++ b/src/io/read.c
@@ -0,0 +1,32 @@
+/*
+ * wip, a small TCP/IP stack.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <wip/wip.h>
+#include <wip/prv/io.h>
+
+enum wip_state wip_io_read(struct wip *const w, struct wip_sm_io *const io)
+{
+ void *const buf = (unsigned char *)io->buf + io->read;
+ const struct wip_cfg *const cfg = &w->cfg;
+ const size_t rem = io->n - io->read;
+ const int n = cfg->read(buf, rem, cfg->user);
+
+ if (n < 0)
+ {
+ w->exception = "read error";
+ return WIP_FATAL;
+ }
+ else if ((io->read += n) >= io->n)
+ {
+ io->read = 0;
+ return WIP_OK;
+ }
+
+ return WIP_AGAIN;
+}
diff --git a/src/io/read_c.c b/src/io/read_c.c
new file mode 100644
index 0000000..4433a9f
--- /dev/null
+++ b/src/io/read_c.c
@@ -0,0 +1,28 @@
+/*
+ * wip, a small TCP/IP stack.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <wip/wip.h>
+#include <wip/prv/io.h>
+
+enum wip_state wip_io_read_c(struct wip_cfg *const cfg, struct wip_sm_io *const io)
+{
+ void *const buf = (unsigned char *)io->buf + io->read;
+ const size_t rem = io->n - io->read;
+ const int n = cfg->read(buf, rem, cfg->user);
+
+ if (n < 0)
+ return WIP_FATAL;
+ else if ((io->read += n) >= io->n)
+ {
+ io->read = 0;
+ return WIP_OK;
+ }
+
+ return WIP_AGAIN;
+}
diff --git a/src/io/tobe16.c b/src/io/tobe16.c
new file mode 100644
index 0000000..3ac7992
--- /dev/null
+++ b/src/io/tobe16.c
@@ -0,0 +1,17 @@
+/*
+ * wip, a small TCP/IP stack.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <wip/wip.h>
+#include <wip/prv/io.h>
+
+void wip_tobe16(const unsigned long v, struct wip_be16 *const r)
+{
+ r->v[0] = v >> 8;
+ r->v[1] = v;
+}
diff --git a/src/io/tobe32.c b/src/io/tobe32.c
new file mode 100644
index 0000000..251a0f9
--- /dev/null
+++ b/src/io/tobe32.c
@@ -0,0 +1,19 @@
+/*
+ * wip, a small TCP/IP stack.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <wip/wip.h>
+#include <wip/prv/io.h>
+
+void wip_tobe32(const unsigned long v, struct wip_be32 *const r)
+{
+ r->v[0] = v >> 24;
+ r->v[1] = v >> 16;
+ r->v[2] = v >> 8;
+ r->v[3] = v;
+}