summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDongwoo Lee <dwoo08.lee@samsung.com>2020-03-17 13:42:38 +0900
committerDongwoo Lee <dwoo08.lee@samsung.com>2020-03-18 17:32:58 +0900
commitbfa980fadb2273fb752ea4b45f4177cd012065e2 (patch)
treeab38516390355325d2e17354a3b324948557ed68
parent5df0e1d2947d87e15492f3fe103dcd4d68992beb (diff)
downloadinitrd-flash-bfa980fadb2273fb752ea4b45f4177cd012065e2.tar.gz
initrd-flash-bfa980fadb2273fb752ea4b45f4177cd012065e2.tar.bz2
initrd-flash-bfa980fadb2273fb752ea4b45f4177cd012065e2.zip
interface: net: Apply interface f/w into network transfer
Since this, network connection is processed upon interface f/w. Change-Id: I87df71a923f010bb013e0b4166c50949bb19a153 Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
-rw-r--r--src/main.c37
-rw-r--r--src/net.c74
-rw-r--r--src/net.h27
-rw-r--r--src/tfm.h18
-rw-r--r--src/thor.c34
5 files changed, 83 insertions, 107 deletions
diff --git a/src/main.c b/src/main.c
index 376d8be..141f165 100644
--- a/src/main.c
+++ b/src/main.c
@@ -20,18 +20,24 @@
#include <unistd.h>
#include <sys/wait.h>
+#include "tfm.h"
#include "dfu.h"
-#include "net.h"
+#include "interface.h"
int _main(int argc, char *argv[])
{
- struct tfm_context tfm;
+ struct tfm_interface_context *supported_interfaces;
struct dfu_context *dfu;
+ struct tfm_interface *intf;
const char *part_table = "/usr/share/partition.info";
char *opt_table = NULL;
int ret, opt;
- memset(&tfm, 0, sizeof(tfm));
+ supported_interfaces = tfm_interface_init();
+ if (!supported_interfaces) {
+ ret = -1;
+ goto out;
+ }
while ((opt = getopt(argc, argv, "p:i:")) != -1) {
switch (opt) {
@@ -45,13 +51,13 @@ int _main(int argc, char *argv[])
} else {
fprintf(stderr, "Out of memory\n");
ret = -1;
- goto out;
+ goto out_intfexit;
}
} else {
fprintf(stderr,
"path should be specified with '-i'\n");
ret = -1;
- goto out;
+ goto out_intfexit;
}
break;
case 'p':
@@ -66,7 +72,8 @@ int _main(int argc, char *argv[])
ret = -1;
goto out_optfree;
}
- tfm.port = (int)val;
+ tfm_interface_set_private(supported_interfaces,
+ "net", (void *)val);
break;
}
default:
@@ -87,26 +94,26 @@ int _main(int argc, char *argv[])
goto out_dfuexit;
}
- ret = net_connect(&tfm);
- if (ret < 0) {
+ intf = tfm_interface_connect(supported_interfaces);
+ if (!intf) {
ret = -1;
goto out_dfuexit;
}
- ret = thor_setup(&tfm);
+ ret = thor_setup(intf);
if (ret < 0) {
ret = -1;
- goto out_netdisconn;
+ goto out_disconn;
}
- ret = thor_process(&tfm, dfu);
+ ret = thor_process(intf, dfu);
if (ret < 0) {
ret = -1;
- goto out_netdisconn;
+ goto out_disconn;
}
-out_netdisconn:
- net_disconnect(&tfm);
+out_disconn:
+ tfm_interface_disconnect(intf);
out_dfuexit:
dfu_free_context(dfu);
@@ -114,6 +121,8 @@ out_dfuexit:
out_optfree:
if (opt_table)
free(opt_table);
+out_intfexit:
+ tfm_interface_exit(supported_interfaces);
out:
exit(ret);
diff --git a/src/net.c b/src/net.c
index b946fa4..9de6741 100644
--- a/src/net.c
+++ b/src/net.c
@@ -23,80 +23,86 @@
#include <sys/wait.h>
#include <arpa/inet.h>
-#include "net.h"
+#include "tfm.h"
+#include "interface.h"
-static ssize_t net_rx_data(int sock, void *buf, ssize_t len)
+#define DEFAULT_PORT 23456
+
+static ssize_t net_rx_data(int fd, void *buf, ssize_t len)
{
- return recv(sock, buf, len, MSG_WAITALL);
+ return recv(fd, buf, len, MSG_WAITALL);
}
-static ssize_t net_tx_data(int sock, void *buf, ssize_t len)
+static ssize_t net_tx_data(int fd, void *buf, ssize_t len)
{
- return send(sock, buf, len, 0);
+ return send(fd, buf, len, 0);
}
-int net_connect(struct tfm_context *ctx)
+static int net_connect(struct tfm_interface *intf)
{
struct sockaddr_in servaddr;
- struct tfm_connect *conn;
- int listener, sock;
- int ret = 0;
-
- conn = malloc(sizeof(*conn));
- if (!conn)
- return -ENOMEM;
+ int listener, ret = 0;
+ int port;
listener = socket(AF_INET, SOCK_STREAM, 0);
if (listener < 0) {
fprintf(stderr, "Failed to create socket\n");
- ret = -EINVAL;
- goto err_free;
+ return -EINVAL;
}
+ port = (unsigned long) intf->priv;
+ if (port <= 1024)
+ port = DEFAULT_PORT;
+
+ fprintf(stdout, "%s using port: %d\n", intf->name, port);
+
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons((ctx->port ? ctx->port : DEFAULT_PORT));
+ servaddr.sin_port = htons(port);
ret = bind(listener, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret < 0) {
fprintf(stderr, "Failed to bind socket\n");
- goto err_socket;
+ goto err;
}
ret = listen(listener, 1024);
if (ret < 0) {
fprintf(stderr, "Failed to call listen\n");
- goto err_socket;
+ goto err;
}
- sock = accept(listener, NULL, NULL);
- if (sock < 0) {
+ ret = accept(listener, NULL, NULL);
+ if (ret < 0) {
fprintf(stderr, "Failed to accept connection\n");
- ret = sock;
- goto err_socket;
+ goto err;
}
- conn->fd = sock;
- conn->rx_data = net_rx_data;
- conn->tx_data = net_tx_data;
- ctx->connect = conn;
+ intf->rxd = intf->txd = ret;
close(listener);
return 0;
-err_socket:
+err:
close(listener);
-err_free:
- free(conn);
return ret;
}
-void net_disconnect(struct tfm_context *ctx)
+static int net_disconnect(struct tfm_interface *intf)
{
- close(ctx->connect->fd);
-
- free(ctx->connect);
- ctx->connect = NULL;
+ /* since rxd == txd, we close only rxd */
+ return close(intf->rxd);
}
+
+static struct tfm_interface_driver network = {
+ .name = "net",
+ .ops = {
+ .connect = net_connect,
+ .disconnect = net_disconnect,
+ .rx_data = net_rx_data,
+ .tx_data = net_tx_data,
+ },
+};
+INTERFACE_REGISTER(network)
diff --git a/src/net.h b/src/net.h
deleted file mode 100644
index dc96b4d..0000000
--- a/src/net.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * flash-manager - Tizen kernel-level image flashing solution
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __NET_H
-#define __NET_H
-
-#include "tfm.h"
-
-#define DEFAULT_PORT 23456
-
-int net_connect(struct tfm_context *ctx);
-void net_disconnect(struct tfm_context *ctx);
-
-#endif
diff --git a/src/tfm.h b/src/tfm.h
index 8760e35..7d18f4e 100644
--- a/src/tfm.h
+++ b/src/tfm.h
@@ -16,25 +16,13 @@
#ifndef __TFM_H
#define __TFM_H
-
#include <string.h>
#include <pthread.h>
#include <sys/queue.h>
#include "dfu.h"
+#include "interface.h"
-struct tfm_connect {
- int fd;
- ssize_t (*rx_data)(int fd, void *buf, ssize_t len);
- ssize_t (*tx_data)(int fd, void *buf, ssize_t len);
-};
-
-struct tfm_context {
- struct tfm_connect *connect;
- int port;
-};
-
-int thor_setup(struct tfm_context *tfm);
-int thor_process(struct tfm_context *tfm, struct dfu_context *dfu);
-
+int thor_setup(struct tfm_interface *intf);
+int thor_process(struct tfm_interface *intf, struct dfu_context *dfu);
#endif
diff --git a/src/thor.c b/src/thor.c
index 1c255d2..426dd40 100644
--- a/src/thor.c
+++ b/src/thor.c
@@ -28,11 +28,12 @@
#include "tfm.h"
#include "dfu.h"
+#include "interface.h"
#include "thor-proto.h"
struct thor_context {
struct rqt_pkt *rqt;
- struct tfm_connect *intf;
+ struct tfm_interface *intf;
struct dfu_context *dfu;
struct dfu_entry *dfu_entry;
@@ -52,10 +53,10 @@ static unsigned int _checkboard(void)
static int thor_send_rsp(struct thor_context *tctx, struct res_pkt *rsp)
{
- struct tfm_connect *intf = tctx->intf;
+ struct tfm_interface *intf = tctx->intf;
ssize_t n;
- n = intf->tx_data(intf->fd, (void *)rsp, RES_PKT_SIZE);
+ n = tfm_interface_send(intf, (void *)rsp, RES_PKT_SIZE);
if (n < sizeof(*rsp))
return -EIO;
@@ -137,19 +138,19 @@ static int thor_process_rqt_cmd(struct thor_context *tctx)
static void thor_send_data_rsp(struct thor_context *tctx, int ack, int count)
{
- struct tfm_connect *intf = tctx->intf;
+ struct tfm_interface *intf = tctx->intf;
struct data_res_pkt rsp;
rsp.ack = ack;
rsp.cnt = count;
- intf->tx_data(intf->fd, &rsp, DATA_RES_PKT_SIZE);
+ tfm_interface_send(intf, &rsp, DATA_RES_PKT_SIZE);
}
static int
thor_download_head(struct thor_context *tctx, unsigned int packet_size)
{
- struct tfm_connect *intf = tctx->intf;
+ struct tfm_interface *intf = tctx->intf;
struct dfu_entry *e = tctx->dfu_entry;
uint64_t recv = 0;
uint64_t total = tctx->file_size;
@@ -160,7 +161,7 @@ thor_download_head(struct thor_context *tctx, unsigned int packet_size)
tctx->buffer = buf = dfu_get_buffer(FLASH_UNIT_SIZE);
while (total - recv >= packet_size) {
- n = intf->rx_data(intf->fd, buf, packet_size);
+ n = tfm_interface_recv(intf, buf, packet_size);
if (n < packet_size)
return -EIO;
@@ -181,7 +182,7 @@ thor_download_head(struct thor_context *tctx, unsigned int packet_size)
tctx->remain = (total - recv) + (uint64_t)(buf - tctx->buffer);
if ((total - recv) > 0) {
- n = intf->rx_data(intf->fd, buf, packet_size);
+ n = tfm_interface_recv(intf, buf, packet_size);
if (n < packet_size)
return -EIO;
recv += n;
@@ -319,17 +320,17 @@ static int thor_do_request(struct thor_context *tctx)
return ret;
}
-static int thor_handshake(struct tfm_connect *intf)
+static int thor_handshake(struct tfm_interface *intf)
{
char buf[5];
ssize_t n;
- n = intf->rx_data(intf->fd, buf, 4/* strlen("THOR") */);
+ n = tfm_interface_recv(intf, buf, 4/* strlen("THOR") */);
if (n < 4)
return -EIO;
if (!strncmp(buf, "THOR", 4)) {
- n = intf->tx_data(intf->fd, "ROHT", 4);
+ n = tfm_interface_send(intf, "ROHT", 4);
if (n < 4)
return -EIO;
} else {
@@ -340,9 +341,8 @@ static int thor_handshake(struct tfm_connect *intf)
return 0;
}
-int thor_process(struct tfm_context *tfm, struct dfu_context *dfu)
+int thor_process(struct tfm_interface *intf, struct dfu_context *dfu)
{
- struct tfm_connect *intf = tfm->connect;
struct thor_context tctx;
struct rqt_pkt rqt;
ssize_t n;
@@ -354,7 +354,7 @@ int thor_process(struct tfm_context *tfm, struct dfu_context *dfu)
tctx.dfu = dfu;
for (;;) {
- n = intf->rx_data(intf->fd, &rqt, RQT_PKT_SIZE);
+ n = tfm_interface_recv(intf, &rqt, RQT_PKT_SIZE);
if (n < sizeof(rqt)) {
fprintf(stderr,
"Failed to receive data from the host(%zd:%zu)",
@@ -372,12 +372,12 @@ int thor_process(struct tfm_context *tfm, struct dfu_context *dfu)
return 0;
}
-int thor_setup(struct tfm_context *tfm)
+int thor_setup(struct tfm_interface *intf)
{
- if (!tfm->connect) {
+ if (!intf) {
fprintf(stderr, "Invalid connection\n");
return -EINVAL;
}
- return thor_handshake(tfm->connect);
+ return thor_handshake(intf);
}