Since this, network connection is processed upon interface f/w.
Change-Id: I87df71a923f010bb013e0b4166c50949bb19a153
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
#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) {
} 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':
ret = -1;
goto out_optfree;
}
- tfm.port = (int)val;
+ tfm_interface_set_private(supported_interfaces,
+ "net", (void *)val);
break;
}
default:
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);
out_optfree:
if (opt_table)
free(opt_table);
+out_intfexit:
+ tfm_interface_exit(supported_interfaces);
out:
exit(ret);
#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)
+++ /dev/null
-/*
- * 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
#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
#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;
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;
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;
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;
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;
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 {
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;
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)",
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);
}