Skip to content

Commit 11cf421

Browse files
JianyuWang0623xiaoxiang781216
authored andcommitted
system/fastboot: enable usb and tcp the same time
Add support for enabling USB and TCP transport at the same time. Signed-off-by: wangjianyu3 <[email protected]>
1 parent abf9fbe commit 11cf421

File tree

1 file changed

+143
-100
lines changed

1 file changed

+143
-100
lines changed

system/fastboot/fastboot.c

Lines changed: 143 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,11 @@ static ssize_t fastboot_usbdev_read(FAR struct fastboot_ctx_s *ctx,
231231
FAR void *buf, size_t len);
232232
static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
233233
FAR const void *buf, size_t len);
234-
#elif defined(CONFIG_NET_TCP)
234+
#endif
235235

236236
/* TCP transport */
237237

238+
#ifdef CONFIG_NET_TCP
238239
static int fastboot_tcp_initialize(FAR struct fastboot_ctx_s *ctx);
239240
static void fastboot_tcp_deinit(FAR struct fastboot_ctx_s *ctx);
240241
static ssize_t fastboot_tcp_read(FAR struct fastboot_ctx_s *ctx,
@@ -275,23 +276,25 @@ static const struct memory_region_s g_memory_region[] =
275276
};
276277
#endif
277278

278-
#ifdef CONFIG_USBFASTBOOT
279-
static const struct fastboot_transport_ops_s g_tran_ops_usb =
279+
static const struct fastboot_transport_ops_s g_tran_ops[] =
280280
{
281-
.init = fastboot_usbdev_initialize,
282-
.deinit = fastboot_usbdev_deinit,
283-
.read = fastboot_usbdev_read,
284-
.write = fastboot_usbdev_write,
285-
};
286-
#elif defined(CONFIG_NET_TCP)
287-
static const struct fastboot_transport_ops_s g_tran_ops_tcp =
288-
{
289-
.init = fastboot_tcp_initialize,
290-
.deinit = fastboot_tcp_deinit,
291-
.read = fastboot_tcp_read,
292-
.write = fastboot_tcp_write,
293-
};
281+
#ifdef CONFIG_USBFASTBOOT
282+
{
283+
.init = fastboot_usbdev_initialize,
284+
.deinit = fastboot_usbdev_deinit,
285+
.read = fastboot_usbdev_read,
286+
.write = fastboot_usbdev_write,
287+
},
288+
#endif
289+
#ifdef CONFIG_NET_TCP
290+
{
291+
.init = fastboot_tcp_initialize,
292+
.deinit = fastboot_tcp_deinit,
293+
.read = fastboot_tcp_read,
294+
.write = fastboot_tcp_write,
295+
},
294296
#endif
297+
};
295298

296299
/****************************************************************************
297300
* Private Functions
@@ -660,6 +663,11 @@ static void fastboot_download(FAR struct fastboot_ctx_s *ctx,
660663
ssize_t r = ctx->ops->read(ctx, download, len);
661664
if (r < 0)
662665
{
666+
if (errno == EAGAIN)
667+
{
668+
continue;
669+
}
670+
663671
ctx->download_size = 0;
664672
fb_err("fastboot_download usb read error\n");
665673
return;
@@ -978,28 +986,34 @@ static void fastboot_oem(FAR struct fastboot_ctx_s *ctx, FAR const char *arg)
978986
}
979987
}
980988

981-
static void fastboot_command_loop(FAR struct fastboot_ctx_s *ctx)
989+
static void fastboot_command_loop(FAR struct fastboot_ctx_s *ctx,
990+
size_t nctx)
982991
{
983-
struct epoll_event ev[1];
992+
FAR struct fastboot_ctx_s *c;
993+
struct epoll_event ev[nctx];
984994
int epfd;
995+
int n;
985996

986-
if (ctx->left > 0)
997+
epfd = epoll_create(1);
998+
if (epfd < 0)
987999
{
988-
epfd = epoll_create(1);
989-
if (epfd < 0)
990-
{
991-
fb_err("open epoll failed %d", errno);
992-
return;
993-
}
1000+
fb_err("open epoll failed %d", errno);
1001+
return;
1002+
}
9941003

995-
ev[0].events = EPOLLIN;
996-
ev[0].data.ptr = ctx;
997-
if (epoll_ctl(epfd, EPOLL_CTL_ADD, ctx->tran_fd[0], &ev[0]) < 0)
1004+
for (c = ctx, n = nctx; n-- > 0; c++)
1005+
{
1006+
ev[n].events = EPOLLIN;
1007+
ev[n].data.ptr = c;
1008+
if (epoll_ctl(epfd, EPOLL_CTL_ADD, c->tran_fd[0], &ev[n]) < 0)
9981009
{
999-
fb_err("err add poll %d", ctx->tran_fd[0]);
1010+
fb_err("err add poll %d", c->tran_fd[0]);
10001011
return;
10011012
}
1013+
}
10021014

1015+
if (ctx->left > 0)
1016+
{
10031017
if (epoll_wait(epfd, ev, nitems(ev), ctx->left) <= 0)
10041018
{
10051019
return;
@@ -1008,35 +1022,43 @@ static void fastboot_command_loop(FAR struct fastboot_ctx_s *ctx)
10081022

10091023
/* Reinitialize for storing TCP transport remaining data size. */
10101024

1011-
ctx->left = 0;
1025+
for (c = ctx, n = nctx; n-- > 0; c++)
1026+
{
1027+
c->left = 0;
1028+
}
10121029

10131030
while (1)
10141031
{
10151032
char buffer[FASTBOOT_MSG_LEN + 1];
10161033
size_t ncmds = nitems(g_fast_cmd);
10171034
size_t index;
10181035

1019-
ssize_t r = ctx->ops->read(ctx, buffer, FASTBOOT_MSG_LEN);
1020-
if (r < 0)
1036+
n = epoll_wait(epfd, ev, nitems(ev), -1);
1037+
for (n--; n >= 0; )
10211038
{
1022-
fb_err("fastboot transport read() %zd\n", r);
1023-
continue;
1024-
}
1039+
c = (FAR struct fastboot_ctx_s *)ev[n].data.ptr;
1040+
ssize_t r = c->ops->read(c, buffer, FASTBOOT_MSG_LEN);
1041+
if (r <= 0)
1042+
{
1043+
n--;
1044+
continue;
1045+
}
10251046

1026-
buffer[r] = '\0';
1027-
for (index = 0; index < ncmds; index++)
1028-
{
1029-
size_t len = strlen(g_fast_cmd[index].prefix);
1030-
if (memcmp(buffer, g_fast_cmd[index].prefix, len) == 0)
1047+
buffer[r] = '\0';
1048+
for (index = 0; index < ncmds; index++)
10311049
{
1032-
g_fast_cmd[index].handle(ctx, buffer + len);
1033-
break;
1050+
size_t len = strlen(g_fast_cmd[index].prefix);
1051+
if (memcmp(buffer, g_fast_cmd[index].prefix, len) == 0)
1052+
{
1053+
g_fast_cmd[index].handle(c, buffer + len);
1054+
break;
1055+
}
10341056
}
1035-
}
10361057

1037-
if (index == ncmds)
1038-
{
1039-
fastboot_fail(ctx, "Unknown command");
1058+
if (index == ncmds)
1059+
{
1060+
fastboot_fail(c, "Unknown command");
1061+
}
10401062
}
10411063
}
10421064
}
@@ -1062,25 +1084,33 @@ static void fastboot_publish(FAR struct fastboot_ctx_s *ctx,
10621084
ctx->varlist = var;
10631085
}
10641086

1065-
static void fastboot_create_publish(FAR struct fastboot_ctx_s *ctx)
1087+
static void fastboot_create_publish(FAR struct fastboot_ctx_s *ctx,
1088+
size_t nctx)
10661089
{
1067-
fastboot_publish(ctx, "product", "NuttX", 0);
1068-
fastboot_publish(ctx, "kernel", "NuttX", 0);
1069-
fastboot_publish(ctx, "version", CONFIG_VERSION_STRING, 0);
1070-
fastboot_publish(ctx, "slot-count", "1", 0);
1071-
fastboot_publish(ctx, "max-download-size", NULL,
1072-
CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX);
1090+
for (; nctx-- > 0; ctx++)
1091+
{
1092+
fastboot_publish(ctx, "product", "NuttX", 0);
1093+
fastboot_publish(ctx, "kernel", "NuttX", 0);
1094+
fastboot_publish(ctx, "version", CONFIG_VERSION_STRING, 0);
1095+
fastboot_publish(ctx, "slot-count", "1", 0);
1096+
fastboot_publish(ctx, "max-download-size", NULL,
1097+
CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX);
1098+
}
10731099
}
10741100

1075-
static void fastboot_free_publish(FAR struct fastboot_ctx_s *ctx)
1101+
static void fastboot_free_publish(FAR struct fastboot_ctx_s *ctx,
1102+
size_t nctx)
10761103
{
10771104
FAR struct fastboot_var_s *next;
10781105

1079-
while (ctx->varlist)
1106+
for (; nctx-- > 0; ctx++)
10801107
{
1081-
next = ctx->varlist->next;
1082-
free(ctx->varlist);
1083-
ctx->varlist = next;
1108+
while (ctx->varlist)
1109+
{
1110+
next = ctx->varlist->next;
1111+
free(ctx->varlist);
1112+
ctx->varlist = next;
1113+
}
10841114
}
10851115
}
10861116

@@ -1149,15 +1179,15 @@ static int fastboot_usbdev_initialize(FAR struct fastboot_ctx_s *ctx)
11491179
}
11501180
#endif /* SYSTEM_FASTBOOTD_USB_BOARDCTL */
11511181

1152-
ctx->tran_fd[0] =
1153-
fastboot_open_usb(FASTBOOT_EP_BULKOUT_IDX, O_RDONLY | O_CLOEXEC);
1182+
ctx->tran_fd[0] = fastboot_open_usb(FASTBOOT_EP_BULKOUT_IDX,
1183+
O_RDONLY | O_CLOEXEC | O_NONBLOCK);
11541184
if (ctx->tran_fd[0] < 0)
11551185
{
11561186
return ctx->tran_fd[0];
11571187
}
11581188

1159-
ctx->tran_fd[1] =
1160-
fastboot_open_usb(FASTBOOT_EP_BULKIN_IDX, O_WRONLY | O_CLOEXEC);
1189+
ctx->tran_fd[1] = fastboot_open_usb(FASTBOOT_EP_BULKIN_IDX,
1190+
O_WRONLY | O_CLOEXEC | O_NONBLOCK);
11611191
if (ctx->tran_fd[1] < 0)
11621192
{
11631193
close(ctx->tran_fd[0]);
@@ -1190,13 +1220,15 @@ static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
11901220
{
11911221
return fastboot_write(ctx->tran_fd[1], buf, len);
11921222
}
1223+
#endif
11931224

1194-
#elif defined(CONFIG_NET_TCP)
1225+
#ifdef CONFIG_NET_TCP
11951226
static int fastboot_tcp_initialize(FAR struct fastboot_ctx_s *ctx)
11961227
{
11971228
struct sockaddr_in addr;
11981229

1199-
ctx->tran_fd[0] = socket(AF_INET, SOCK_STREAM, 0);
1230+
ctx->tran_fd[0] = socket(AF_INET, SOCK_STREAM,
1231+
SOCK_CLOEXEC | SOCK_NONBLOCK);
12001232
if (ctx->tran_fd[0] < 0)
12011233
{
12021234
fb_err("create socket failed %d", errno);
@@ -1357,36 +1389,54 @@ static int fastboot_tcp_write(FAR struct fastboot_ctx_s *ctx,
13571389
}
13581390
#endif
13591391

1360-
static int fastboot_context_initialize(FAR struct fastboot_ctx_s *ctx)
1392+
static int fastboot_context_initialize(FAR struct fastboot_ctx_s *ctx,
1393+
size_t nctx)
13611394
{
1362-
ctx->download_max = CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX;
1363-
ctx->download_offset = 0;
1364-
ctx->download_size = 0;
1365-
ctx->flash_fd = -1;
1366-
ctx->total_imgsize = 0;
1367-
ctx->varlist = NULL;
1368-
ctx->left = 0;
1369-
#ifdef CONFIG_USBFASTBOOT
1370-
ctx->ops = &g_tran_ops_usb;
1371-
#elif defined(CONFIG_NET_TCP)
1372-
ctx->ops = &g_tran_ops_tcp;
1373-
#endif
1374-
ctx->tran_fd[0] = -1;
1375-
ctx->tran_fd[1] = -1;
1395+
int ret;
13761396

1377-
ctx->download_buffer = malloc(CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX);
1378-
if (ctx->download_buffer == NULL)
1379-
{
1380-
fb_err("ERROR: Could not allocate the memory.\n");
1381-
return -errno;
1397+
for (; nctx-- > 0; ctx++)
1398+
{
1399+
ctx->download_max = CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX;
1400+
ctx->download_offset = 0;
1401+
ctx->download_size = 0;
1402+
ctx->flash_fd = -1;
1403+
ctx->total_imgsize = 0;
1404+
ctx->varlist = NULL;
1405+
ctx->left = ctx[0].left;
1406+
ctx->ops = &g_tran_ops[nctx];
1407+
ctx->tran_fd[0] = -1;
1408+
ctx->tran_fd[1] = -1;
1409+
1410+
ctx->download_buffer = malloc(CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX);
1411+
if (ctx->download_buffer == NULL)
1412+
{
1413+
fb_err("ERROR: Could not allocate the memory.\n");
1414+
continue;
1415+
}
1416+
1417+
ret = ctx->ops->init(ctx);
1418+
if (ret < 0)
1419+
{
1420+
free(ctx->download_buffer);
1421+
ctx->download_buffer = NULL;
1422+
ctx->ops->deinit(ctx);
1423+
}
13821424
}
13831425

13841426
return 0;
13851427
}
13861428

1387-
static void fastboot_context_deinit(FAR struct fastboot_ctx_s *ctx)
1429+
static void fastboot_context_deinit(FAR struct fastboot_ctx_s *ctx,
1430+
size_t nctx)
13881431
{
1389-
free(ctx->download_buffer);
1432+
for (; nctx-- > 0; ctx++)
1433+
{
1434+
if (ctx->download_buffer)
1435+
{
1436+
ctx->ops->deinit(ctx);
1437+
free(ctx->download_buffer);
1438+
}
1439+
}
13901440
}
13911441

13921442
/****************************************************************************
@@ -1395,15 +1445,9 @@ static void fastboot_context_deinit(FAR struct fastboot_ctx_s *ctx)
13951445

13961446
int main(int argc, FAR char **argv)
13971447
{
1398-
struct fastboot_ctx_s context;
1448+
struct fastboot_ctx_s context[nitems(g_tran_ops)];
13991449
int ret;
14001450

1401-
ret = fastboot_context_initialize(&context);
1402-
if (ret < 0)
1403-
{
1404-
return ret;
1405-
}
1406-
14071451
if (argc > 1)
14081452
{
14091453
if (strcmp(argv[1], "-h") == 0)
@@ -1414,23 +1458,22 @@ int main(int argc, FAR char **argv)
14141458
return 0;
14151459
}
14161460

1417-
if (sscanf(argv[1], "%" SCNu64 , &context.left) != 1)
1461+
if (sscanf(argv[1], "%" SCNu64 , &context[0].left) != 1)
14181462
{
14191463
return -EINVAL;
14201464
}
14211465
}
14221466

1423-
ret = context.ops->init(&context);
1467+
ret = fastboot_context_initialize(context, nitems(context));
14241468
if (ret < 0)
14251469
{
14261470
return ret;
14271471
}
14281472

1429-
fastboot_create_publish(&context);
1430-
fastboot_command_loop(&context);
1431-
fastboot_free_publish(&context);
1432-
context.ops->deinit(&context);
1433-
fastboot_context_deinit(&context);
1473+
fastboot_create_publish(context, nitems(context));
1474+
fastboot_command_loop(context, nitems(context));
1475+
fastboot_free_publish(context, nitems(context));
1476+
fastboot_context_deinit(context, nitems(context));
14341477

14351478
return ret;
14361479
}

0 commit comments

Comments
 (0)