From a42c5d2e7f1fda6539122928ab42ba4690af86df Mon Sep 17 00:00:00 2001 From: lefranc Date: Fri, 4 Jan 2008 09:55:19 +0000 Subject: - added socket implementation for maximus to station communication - you need to define "STATION_SOCK" into host/config.h to enable socket communication git-svn-id: svn+ssh://pessac/svn/cesar/trunk@1211 017c9cb6-072f-447c-8318-d5b54f68fe89 --- .../hal/maximus/arch/current/include/hal_syscall.h | 4 +- .../hal/maximus/arch/current/src/maximus_diag.c | 2 +- hal/phy/maximus/test/src/test_bridgedma.c | 29 ++- hal/phy/maximus/test/src/test_phy_access.c | 120 +++++++++-- hal/phy/maximus/test/src/test_phy_ctrl.c | 161 +++++++++++++- host/Module | 2 +- host/config.h | 2 +- host/socket.h | 67 ++++++ host/src/sci.c | 33 ++- host/src/socketcalls.c | 237 +++++++++++++++++++++ host/src/station.c | 125 ++++++++++- host/station.h | 17 +- host/syscall.h | 16 +- host/test/src/test_fcall.c | 17 +- host/test/src/test_netclock.c | 30 ++- host/test/src/test_sci.c | 8 + host/test/src/test_station.c | 42 +++- maximus/sci/src/SciServer.cpp | 12 +- maximus/system/inc/Station.h | 13 +- maximus/system/src/Station.cpp | 114 ++++++++-- 20 files changed, 970 insertions(+), 81 deletions(-) create mode 100644 host/socket.h create mode 100644 host/src/socketcalls.c diff --git a/ecos/packages/hal/maximus/arch/current/include/hal_syscall.h b/ecos/packages/hal/maximus/arch/current/include/hal_syscall.h index 52e5c66699..557838e252 100644 --- a/ecos/packages/hal/maximus/arch/current/include/hal_syscall.h +++ b/ecos/packages/hal/maximus/arch/current/include/hal_syscall.h @@ -306,8 +306,8 @@ struct cyg_hal_sys_stat // applications which want to access the host OS. externC int cyg_hal_sys_mknod(const char *, unsigned int, short); -externC int/*unsigned long*/ cyg_hal_sys_write(int, const void*, long); -externC int/*unsigned long*/ cyg_hal_sys_read(int, void*, long); +externC unsigned long cyg_hal_sys_write(int, const void*, long); +externC unsigned long cyg_hal_sys_read(int, void*, long); externC int cyg_hal_sys_lseek(int, int, int); externC int cyg_hal_sys_open(const char *,int,int); externC int cyg_hal_sys_fdatasync(int); diff --git a/ecos/packages/hal/maximus/arch/current/src/maximus_diag.c b/ecos/packages/hal/maximus/arch/current/src/maximus_diag.c index d124d95cb7..b63f5c1e61 100644 --- a/ecos/packages/hal/maximus/arch/current/src/maximus_diag.c +++ b/ecos/packages/hal/maximus/arch/current/src/maximus_diag.c @@ -92,7 +92,7 @@ void hal_diag_write_char(char c) #ifdef DIAG_WITH_STDIN cyg_hal_sys_fdatasync(1); #else - cyg_hal_sys_fdatasync(my_station.pipe_out_fd); + cyg_hal_sys_fdatasync(my_station.pipe_log_fd); #endif } } diff --git a/hal/phy/maximus/test/src/test_bridgedma.c b/hal/phy/maximus/test/src/test_bridgedma.c index 8491c79f0f..146d2a06d7 100644 --- a/hal/phy/maximus/test/src/test_bridgedma.c +++ b/hal/phy/maximus/test/src/test_bridgedma.c @@ -164,8 +164,13 @@ void bridgedma_segmentation_test_case(test_t t) bridgedma_ctx = phy_bridgedma_init(&bridgedma_data, _bridgedma_segmentation_cb, _bridgedma_deffered_cb); pb_first = blk_alloc_desc_range(5, &pb_last); +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; + fd_out = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(bridgedma_ctx->station->pipe_out_name, O_RDONLY); fd_out = open(bridgedma_ctx->station->pipe_in_name, O_WRONLY); +#endif /* STATION_SOCK */ maximus_pending_isrs = 0; _buffer_random_fill(frame_buffer, sizeof(frame_buffer)); crc_ctx.width = 32; @@ -555,7 +560,11 @@ void bridgedma_segmentation_test_case(test_t t) } test_end; blk_release_desc_range(pb_first, pb_last); - + +#ifndef STATION_SOCK + close(fd_in); + close(fd_out); +#endif /* !STATION_SOCK */ return; } @@ -589,8 +598,13 @@ void bridgedma_reassembly_test_case(test_t t) bridgedma_ctx = phy_bridgedma_init(&bridgedma_data, _bridgedma_reassembly_cb, _bridgedma_deffered_cb); pb_first = blk_alloc_desc_range(4, &pb_last); +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; + fd_out = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(bridgedma_ctx->station->pipe_out_name, O_RDONLY); fd_out = open(bridgedma_ctx->station->pipe_in_name, O_WRONLY); +#endif /* STATION_SOCK */ maximus_pending_isrs = 0; _buffer_random_fill(frame_buffer, sizeof(frame_buffer)); crc_ctx.width = 32; @@ -970,6 +984,10 @@ void bridgedma_reassembly_test_case(test_t t) blk_release_desc_range(pb_first, pb_last); +#ifndef STATION_SOCK + close(fd_in); + close(fd_out); +#endif /* !STATION_SOCK */ return; } @@ -1132,8 +1150,13 @@ void bridgedma_ticket_3(test_t t) test_case_begin(t, "ticket #3"); bridgedma_ctx = phy_bridgedma_init(&bridgedma_data, _bridgedma_reassembly_cb, _bridgedma_deffered_cb); +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; + fd_out = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(bridgedma_ctx->station->pipe_out_name, O_RDONLY); fd_out = open(bridgedma_ctx->station->pipe_in_name, O_WRONLY); +#endif /* STATION_SOCK */ pb_first = blk_alloc_desc_range(3, &pb_last); @@ -1206,6 +1229,10 @@ void bridgedma_ticket_3(test_t t) blk_release_desc_range(pb_first, pb_last); +#ifndef STATION_SOCK + close(fd_in); + close(fd_out); +#endif /* !STATION_SOCK */ return; } diff --git a/hal/phy/maximus/test/src/test_phy_access.c b/hal/phy/maximus/test/src/test_phy_access.c index 5c3b80830b..17d8140799 100644 --- a/hal/phy/maximus/test/src/test_phy_access.c +++ b/hal/phy/maximus/test/src/test_phy_access.c @@ -58,8 +58,12 @@ void phy_access_backoff_start_test_case(test_t t) set_node_t *found_node; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -73,7 +77,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless (date == ntohl(netclock_hdr->tick_low)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ // get new network tick msg_tick_tck = (((unsigned long long)(ntohl(netclock_hdr->tick_high))) << 32) @@ -114,8 +120,12 @@ void phy_access_backoff_start_test_case(test_t t) set_node_t *found_node; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -129,7 +139,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless ((my_station.current_tick_tck & 0x00000000FFFFFFFF) + (unsigned long)MAC_SLOT_TCK == ntohl(netclock_hdr->tick_low)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ /* Remove netclock callback. */ @@ -156,8 +168,12 @@ void phy_access_backoff_start_test_case(test_t t) set_node_t *found_node; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -171,7 +187,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless ((my_station.current_tick_tck & 0x00000000FFFFFFFF) + (unsigned long)MAXIMUS_PHY_PRS_DETECTION_DELAY_TCK == ntohl(netclock_hdr->tick_low)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ // get new network tick msg_tick_tck = (((unsigned long long)(ntohl(netclock_hdr->tick_high))) << 32) @@ -212,8 +230,12 @@ void phy_access_backoff_start_test_case(test_t t) uint8_t prs0 = 1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and phy headers memset(data, '\0', SCI_MSG_MAX_SIZE); test_fail_unless ((-1 != fd_in) @@ -233,7 +255,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless (0 == memcmp(&prs0, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), sizeof(u8))); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -248,8 +272,12 @@ void phy_access_backoff_start_test_case(test_t t) set_node_t *found_node; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -264,7 +292,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless ((my_station.current_tick_tck & 0x00000000FFFFFFFF) + (unsigned long)MAC_SLOT_TCK - (unsigned long)MAXIMUS_PHY_PRS_DETECTION_DELAY_TCK == ntohl(netclock_hdr->tick_low)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ // get new network tick msg_tick_tck = (((unsigned long long)(ntohl(sci_hdr->netclock_high))) << 32) @@ -307,8 +337,12 @@ void phy_access_backoff_start_test_case(test_t t) set_node_t *found_node; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -322,7 +356,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless ((my_station.current_tick_tck & 0x00000000FFFFFFFF) + (unsigned long)MAC_SLOT_TCK == ntohl(netclock_hdr->tick_low)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ /* Remove netclock callback. */ @@ -362,8 +398,12 @@ void phy_access_backoff_start_test_case(test_t t) set_node_t *found_node; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -378,7 +418,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless ((my_station.current_tick_tck & 0x00000000FFFFFFFF) + (unsigned long)MAXIMUS_PHY_PRS_DETECTION_DELAY_TCK == ntohl(netclock_hdr->tick_low)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ // get new network tick msg_tick_tck = (((unsigned long long)(ntohl(sci_hdr->netclock_high))) << 32) @@ -419,8 +461,12 @@ void phy_access_backoff_start_test_case(test_t t) uint8_t prs1 = 1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and phy headers memset(data, '\0', SCI_MSG_MAX_SIZE); test_fail_unless ((-1 != fd_in) @@ -440,7 +486,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless (0 == memcmp(&prs1, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), sizeof(u8))); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -452,8 +500,12 @@ void phy_access_backoff_start_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -468,7 +520,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless ((my_station.current_tick_tck & 0x00000000FFFFFFFF) + (unsigned long)MAC_SLOT_TCK - (unsigned long)MAXIMUS_PHY_PRS_DETECTION_DELAY_TCK == ntohl(netclock_hdr->tick_low)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ // get new network tick msg_tick_tck = (((unsigned long long)(ntohl(sci_hdr->netclock_high))) << 32) @@ -496,8 +550,12 @@ void phy_access_backoff_start_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -511,7 +569,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -522,8 +582,12 @@ void phy_access_backoff_start_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -540,7 +604,9 @@ void phy_access_backoff_start_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == ctx->access.backoff_start_netclock_id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // test the prp result callback @@ -627,8 +693,12 @@ void phy_access_timer_program_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -645,7 +715,9 @@ void phy_access_timer_program_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == ctx->access.timer_program_netclock_id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; id = ctx->access.timer_program_netclock_id; @@ -659,8 +731,12 @@ void phy_access_timer_program_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -674,7 +750,9 @@ void phy_access_timer_program_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -688,8 +766,12 @@ void phy_access_timer_program_test_case(test_t t) set_node_t *found_node; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -706,7 +788,9 @@ void phy_access_timer_program_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == ctx->access.timer_program_netclock_id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ /* Remove netclock callback. */ @@ -765,8 +849,12 @@ void phy_access_timer_cancel_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -780,7 +868,9 @@ void phy_access_timer_cancel_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; return; diff --git a/hal/phy/maximus/test/src/test_phy_ctrl.c b/hal/phy/maximus/test/src/test_phy_ctrl.c index e9238bc1c0..0b63b69595 100644 --- a/hal/phy/maximus/test/src/test_phy_ctrl.c +++ b/hal/phy/maximus/test/src/test_phy_ctrl.c @@ -888,8 +888,12 @@ void phy_uninit_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); test_fail_unless ((-1 != fd_in) @@ -906,7 +910,9 @@ void phy_uninit_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == ctx->control.extra_timer_program_netclock_id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; id = ctx->control.extra_timer_program_netclock_id; @@ -930,7 +936,11 @@ void phy_uninit_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and netclock headers memset(data, '\0', 256); @@ -945,7 +955,9 @@ void phy_uninit_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; maximus_pending_isrs = 0; @@ -1158,7 +1170,11 @@ void phy_tx_frame_test_case(test_t t) set_node_t *found_node; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci header test_fail_unless ((-1 != fd_in) @@ -1178,7 +1194,9 @@ void phy_tx_frame_test_case(test_t t) my_station.current_tick_tck = msg_tick_tck; // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ /* Remove netclock callback. */ @@ -1242,7 +1260,11 @@ void phy_tx_frame_test_case(test_t t) date += duration_in_ticks; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci header test_fail_unless ((-1 != fd_in) @@ -1262,7 +1284,9 @@ void phy_tx_frame_test_case(test_t t) my_station.current_tick_tck = msg_tick_tck; // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ /* Remove netclock callback. */ @@ -1294,7 +1318,11 @@ void phy_tx_frame_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci header test_fail_unless ((-1 != fd_in) @@ -1307,7 +1335,9 @@ void phy_tx_frame_test_case(test_t t) test_fail_unless (date2 == ntohl(netclock_hdr.tick_low)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // test the second callback @@ -1326,7 +1356,11 @@ void phy_tx_frame_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and phy headers memset(data, '\0', SCI_MSG_MAX_SIZE); @@ -1342,7 +1376,9 @@ void phy_tx_frame_test_case(test_t t) test_fail_unless (PHY_TYPE_PREAMBLE == phy_hdr->type); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -1376,7 +1412,11 @@ void phy_tx_frame_test_case(test_t t) date += duration_in_ticks; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci header test_fail_unless ((-1 != fd_in) @@ -1396,7 +1436,9 @@ void phy_tx_frame_test_case(test_t t) my_station.current_tick_tck = msg_tick_tck; // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ /* Remove netclock callback. */ @@ -1428,7 +1470,11 @@ void phy_tx_frame_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and phy headers memset(data, '\0', SCI_MSG_MAX_SIZE); @@ -1451,7 +1497,9 @@ void phy_tx_frame_test_case(test_t t) test_fail_unless (0 == memcmp(&ctx->control.current_tx_param.fc_av, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t)+sizeof(u32), 4*sizeof(u32))); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -1482,7 +1530,11 @@ void phy_tx_frame_test_case(test_t t) date += duration_in_ticks; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci header test_fail_unless ((-1 != fd_in) @@ -1502,7 +1554,9 @@ void phy_tx_frame_test_case(test_t t) my_station.current_tick_tck = msg_tick_tck; // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ /* Remove netclock callback. */ @@ -1570,7 +1624,11 @@ void phy_tx_frame_test_case(test_t t) phy_pb_tx_t *current_pb = &ctx->pbdma.first_pb->pb_tx; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and phy headers memset(data, '\0', SCI_MSG_MAX_SIZE); @@ -1606,7 +1664,9 @@ void phy_tx_frame_test_case(test_t t) my_station.current_tick_tck = msg_tick_tck; // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; test_begin(t, "short ppdu") @@ -1626,7 +1686,11 @@ void phy_tx_frame_test_case(test_t t) // check that the correct phy message has been sent to Maximus // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and phy headers memset(data, '\0', SCI_MSG_MAX_SIZE); @@ -1649,7 +1713,9 @@ void phy_tx_frame_test_case(test_t t) test_fail_unless (0 == memcmp(&ctx->control.current_tx_param.fc_av, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t)+sizeof(u32), 4*sizeof(u32))); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; id = ctx->control.next_tx_param.tx_frame_netclock_id; @@ -1670,7 +1736,11 @@ void phy_tx_frame_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and netclock headers memset(data, '\0', 256); @@ -1685,7 +1755,9 @@ void phy_tx_frame_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -1699,7 +1771,11 @@ void phy_tx_frame_test_case(test_t t) set_node_t *found_node; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci header test_fail_unless ((-1 != fd_in) @@ -1712,7 +1788,9 @@ void phy_tx_frame_test_case(test_t t) test_fail_unless (date3 == ntohl(netclock_hdr.tick_low)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ /* Remove netclock callback. */ @@ -1833,7 +1911,11 @@ void maximus_phy_send_test_case(test_t t) } // open pipe - fd_in = open(my_station.pipe_out_name, O_RDONLY); +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ + fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and phy headers memset(data, '\0', SCI_MSG_MAX_SIZE); @@ -1861,7 +1943,9 @@ void maximus_phy_send_test_case(test_t t) } // close pipe - close(fd_in); +#ifndef STATION_SOCK + close(fd_in); +#endif /* !STATION_SOCK */ } } test_end; @@ -1885,7 +1969,11 @@ void maximus_phy_send_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and phy headers memset(data, '\0', SCI_MSG_MAX_SIZE); @@ -1909,7 +1997,9 @@ void maximus_phy_send_test_case(test_t t) test_fail_unless (0 == memcmp(tonemap1_data, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t)+(PHY_CARRIER_NB+1)/2-MAC_PB520_BYTES, MAC_PB520_BYTES)); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // test send RX @@ -1929,7 +2019,11 @@ void maximus_phy_send_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and phy headers memset(data, '\0', SCI_MSG_MAX_SIZE); @@ -1952,7 +2046,9 @@ void maximus_phy_send_test_case(test_t t) test_fail_unless (0 == memcmp(data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), &ctx->control.rx_param.src_station_id, sizeof(uint16_t))); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; } @@ -2013,7 +2109,11 @@ void phy_rx_activate_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci header and netclock header memset(data, '\0', 256); @@ -2031,7 +2131,9 @@ void phy_rx_activate_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == ctx->control.rx_param.rx_activate_netclock_id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; phy_access_backoff_start (ctx, date+10, cap); @@ -2044,7 +2146,11 @@ void phy_rx_activate_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and netclock headers memset(data, '\0', 256); @@ -2062,7 +2168,9 @@ void phy_rx_activate_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == ctx->access.backoff_start_netclock_id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; pre_detection = false; @@ -2083,7 +2191,11 @@ void phy_rx_activate_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and netclock headers memset(data, '\0', 256); @@ -2098,7 +2210,9 @@ void phy_rx_activate_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == id1); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -2109,7 +2223,11 @@ void phy_rx_activate_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and netclock headers memset(data, '\0', 256); @@ -2124,7 +2242,9 @@ void phy_rx_activate_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == id2); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -2135,7 +2255,11 @@ void phy_rx_activate_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and netclock headers memset(data, '\0', 256); @@ -2153,7 +2277,9 @@ void phy_rx_activate_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == ctx->control.rx_param.rx_activate_netclock_id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // test the callback @@ -2247,8 +2373,8 @@ void phy_rx_prepare_test_case(test_t t) // check phy data test_fail_unless (0 == memcmp(data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), &ctx->control.rx_param.src_station_id, sizeof(uint16_t))); - // close pipe - close(fd_in); + // close pipe + close(fd_in); } test_end; test_begin(t, "rx prepare with ROBO modulation") @@ -2424,7 +2550,12 @@ void phy_extra_timer_program_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ + // read sci and netclock headers memset(data, '\0', 256); @@ -2442,7 +2573,9 @@ void phy_extra_timer_program_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == ctx->control.extra_timer_program_netclock_id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // test the callback @@ -2472,7 +2605,11 @@ void phy_extra_timer_program_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and netclock headers memset(data, '\0', 256); @@ -2487,7 +2624,9 @@ void phy_extra_timer_program_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; // check that the correct netclock message has been sent to Maximus @@ -2498,7 +2637,11 @@ void phy_extra_timer_program_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and netclock headers memset(data, '\0', 256); @@ -2516,7 +2659,9 @@ void phy_extra_timer_program_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == ctx->control.extra_timer_program_netclock_id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; return; @@ -2545,7 +2690,11 @@ void phy_extra_timer_cancel_test_case(test_t t) int fd_in = -1; // open pipe +#ifdef STATION_SOCK + fd_in = my_station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(my_station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ // read sci and netclock headers memset(data, '\0', 256); @@ -2560,7 +2709,9 @@ void phy_extra_timer_cancel_test_case(test_t t) test_fail_unless (ntohs(netclock_hdr->id) == id); // close pipe +#ifndef STATION_SOCK close(fd_in); +#endif /* !STATION_SOCK */ } test_end; return; diff --git a/host/Module b/host/Module index ad42989004..158e32e697 100644 --- a/host/Module +++ b/host/Module @@ -1 +1 @@ -SOURCES := fcall.c fcall_param.c probe.c netclock.c sci.c sci_msg.c station.c # phy_hal.c +SOURCES := fcall.c fcall_param.c probe.c netclock.c sci.c sci_msg.c station.c socketcalls.c # phy_hal.c diff --git a/host/config.h b/host/config.h index 4915bbd1df..7051d97de8 100644 --- a/host/config.h +++ b/host/config.h @@ -19,6 +19,6 @@ * \todo */ -//#define STATION_SOCK /** enable socket communication */ +#undef STATION_SOCK /** enable socket communication */ #endif /* CONFIG_H_ */ diff --git a/host/socket.h b/host/socket.h new file mode 100644 index 0000000000..d73a0d5afc --- /dev/null +++ b/host/socket.h @@ -0,0 +1,67 @@ +#ifndef SOCKET_H_ +#define SOCKET_H_ + +/* Cesar project {{{ + * + * Copyright (C) 2007 Spidcom + * + * <<>> + * + * }}} */ + + /** + * \file socket.h + * \brief The socket adaptation layer for host. + * \ingroup host + * + * This file implements the socket layer for host + * + * \todo + */ + +#include + +#define HOST_PF_UNIX 1 +#define HOST_AF_UNIX HOST_PF_UNIX + +#define HOST_SOCK_STREAM 1 +#define HOST_SOL_SOCKET 1 +#define HOST_SO_SNDBUF 7 +#define HOST_SO_RCVBUF 8 + +struct host_sockaddr +{ + unsigned short int sa_family; + char sa_data[14]; +}; + +#define HOST_UNIX_PATH_MAX 108 + +struct host_sockaddr_un +{ + unsigned short int sun_family; /* AF_UNIX */ + char sun_path[HOST_UNIX_PATH_MAX]; /* pathname */ +}; + +extern int host_socket (int __domain, int __type, int __protocol); +extern int host_socketpair (int __domain, int __type, int __protocol, int __fds[2]); +extern int host_bind (int __fd, const struct host_sockaddr *__addr, unsigned int __len); +extern int host_getsockname (int __fd, const struct host_sockaddr *__addr, unsigned int *__len); +extern int host_connect (int __fd, const struct host_sockaddr *__addr, unsigned int __len); +extern int host_getpeername (int __fd, const struct host_sockaddr *__addr, unsigned int *__len); +extern ssize_t host_send (int __fd, const void *__buf, size_t __n, int __flags); +extern ssize_t host_recv (int __fd, void *__buf, size_t __n, int __flags); +extern ssize_t host_sendto (int __fd, const void *__buf, size_t __n, int __flags, + const struct host_sockaddr *addr, unsigned int __addr_len); +extern ssize_t host_recvfrom (int __fd, void *__buf, size_t __n, int __flags, + const struct host_sockaddr *__addr, unsigned int *__addr_len); +//extern ssize_t host_sendmsg (int __fd, const struct msghdr *__message, int __flags); +//extern ssize_t host_recvmsg (int __fd, struct msghdr *__message, int __flags); +extern int host_getsockopt (int __fd, int __level, int __optname, void *__optval, unsigned int *__optlen); +extern int host_setsockopt (int __fd, int __level, int __optname, const void *__optval, + unsigned int __optlen); +extern int host_listen (int __fd, int __n); +extern int host_accept (int __fd, const struct host_sockaddr *__addr, unsigned int *__addr_len); +extern int host_shutdown (int __fd, int __how); + +#endif /*SOCKET_H_*/ diff --git a/host/src/sci.c b/host/src/sci.c index e1edd848c3..344c07bc7f 100644 --- a/host/src/sci.c +++ b/host/src/sci.c @@ -167,7 +167,6 @@ int sci_fill_hdr( int sci_send(sci_ctx_t *sci, sci_msg_t *msg) { int len, total_length; - struct timespec req, rem; DBG_ASSERT(sci); DBG_ASSERT(msg); @@ -181,7 +180,16 @@ int sci_send(sci_ctx_t *sci, sci_msg_t *msg) total_length = 0; while(total_length < msg->length) { - len = write(sci->station->pipe_out_fd, msg->data_begin + total_length, msg->length); +#ifdef STATION_SOCK + len = write(sci->station->sock_fd, msg->data_begin + total_length, msg->length); + //station_log(sci->station, STATION_LOG_DEBUG, STATION_LOGTYPE_SCI, "%s: write %d byte", __FUNCTION__, ntohs(msg->length)); + if(len < 0) + { + station_log(sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_SCI, "%s: write %d failed (errno=%d)", __FUNCTION__, ntohs(msg->sci_hdr->msg_id), errno); + return -1; + } +#else /* STATION_SOCK */ + len = write(sci->station->pipe_out_fd, msg->data_begin + total_length, msg->length); if(len < 0) { if(errno != EAGAIN) @@ -192,11 +200,9 @@ int sci_send(sci_ctx_t *sci, sci_msg_t *msg) else { len = 0; - req.tv_sec = 0; - req.tv_nsec = 5000000; - //nanosleep(&req, &rem); } } +#endif /* STATION_SOCK */ total_length += len; } @@ -229,7 +235,12 @@ int sci_recv(sci_ctx_t *sci) sci_msg_init(&msg, sci_buffer, SCI_MSG_MAX_SIZE); /* at first, read msg header */ +#ifdef STATION_SOCK + len = read(sci->station->sock_fd, &hdr, sizeof(sci_msg_hdr_t)); + //station_log(sci->station, STATION_LOG_DEBUG, STATION_LOGTYPE_SCI, "%s: read %d bytes", __FUNCTION__, len); +#else /* STATION_SOCK */ len = read(sci->station->pipe_in_fd, &hdr, sizeof(sci_msg_hdr_t)); +#endif /* STATION_SOCK */ if(len < 0) { station_log(sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_SCI, "%s: read msg_hdr failed (errno=%d)", __FUNCTION__, errno); @@ -279,7 +290,11 @@ int sci_recv(sci_ctx_t *sci) /* read the remaining part of message */ sci_msg_push(&msg, ntohs(hdr.length)); +#ifdef STATION_SOCK + len = read(sci->station->sock_fd, msg.data_begin, msg.length); +#else /* STATION_SOCK */ len = read(sci->station->pipe_in_fd, msg.data_begin, msg.length); +#endif /* STATION_SOCK */ if(len < 0) { station_log(sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_SCI, "%s: read msg_body failed (errno=%d)", __FUNCTION__, errno); @@ -309,11 +324,19 @@ int sci_recv(sci_ctx_t *sci) } fd_flush: +#ifdef STATION_SOCK + read(sci->station->sock_fd, sci_buffer, sizeof(sci_buffer)); +#else /* STATION_SOCK */ read(sci->station->pipe_in_fd, sci_buffer, sizeof(sci_buffer)); +#endif /* STATIOn_SOCK */ return -1; read_end: +#ifdef STATION_SOCK + read(sci->station->sock_fd, sci_buffer, hdr.length); +#else /* STATION_SOCK */ read(sci->station->pipe_in_fd, sci_buffer, hdr.length); +#endif /* STATION_SOCK */ return -1; } diff --git a/host/src/socketcalls.c b/host/src/socketcalls.c new file mode 100644 index 0000000000..dce3f163b4 --- /dev/null +++ b/host/src/socketcalls.c @@ -0,0 +1,237 @@ +/* Cesar project {{{ + * + * Copyright (C) 2007 Spidcom + * + * <<>> + * + * }}} */ + +/** + * \file socket.c + * \brief The socket layer for ecos synthetic + * \ingroup host + * + * This file provide the socket functions for eCos synthetic + * + * \todo + */ + +#include "common/std.h" +#ifndef UNIT_TEST +#include "host/syscall.h" +#endif +#include "host/socket.h" +#include + +#ifdef UNIT_TEST +int socketcall(int call, unsigned long *args) +{ + return 0; +} +#else /* UNIT_TEST */ +extern int socketcall(int call, unsigned long *args); +#endif /* UNIT_TEST */ + +/* Various socketcall numbers */ +#define SYS_SOCKET 1 +#define SYS_BIND 2 +#define SYS_CONNECT 3 +#define SYS_LISTEN 4 +#define SYS_ACCEPT 5 +#define SYS_GETSOCKNAME 6 +#define SYS_GETPEERNAME 7 +#define SYS_SOCKETPAIR 8 +#define SYS_SEND 9 +#define SYS_RECV 10 +#define SYS_SENDTO 11 +#define SYS_RECVFROM 12 +#define SYS_SHUTDOWN 13 +#define SYS_SETSOCKOPT 14 +#define SYS_GETSOCKOPT 15 +#define SYS_SENDMSG 16 +#define SYS_RECVMSG 17 + +int host_accept(int s, const struct host_sockaddr *addr, unsigned int * addrlen) +{ + unsigned long args[3]; + + args[0] = s; + args[1] = (unsigned long) addr; + args[2] = (unsigned long) addrlen; + return socketcall(SYS_ACCEPT, args); +} + +int host_bind(int sockfd, const struct host_sockaddr *myaddr, unsigned int addrlen) +{ + unsigned long args[3]; + + args[0] = sockfd; + args[1] = (unsigned long) myaddr; + args[2] = addrlen; + return socketcall(SYS_BIND, args); +} + +int host_connect(int sockfd, const struct host_sockaddr *saddr, unsigned int addrlen) +{ + unsigned long args[3]; + + args[0] = sockfd; + args[1] = (unsigned long) saddr; + args[2] = addrlen; + return socketcall(SYS_CONNECT, args); +} + +int host_getpeername(int sockfd, const struct host_sockaddr *addr, unsigned int * paddrlen) +{ + unsigned long args[3]; + + args[0] = sockfd; + args[1] = (unsigned long) addr; + args[2] = (unsigned long) paddrlen; + return socketcall(SYS_GETPEERNAME, args); +} + +int host_getsockname(int sockfd, const struct host_sockaddr *addr, unsigned int * paddrlen) +{ + unsigned long args[3]; + + args[0] = sockfd; + args[1] = (unsigned long) addr; + args[2] = (unsigned long) paddrlen; + return socketcall(SYS_GETSOCKNAME, args); +} + +int host_getsockopt(int fd, int level, int optname, void * optval, + unsigned int * optlen) +{ + unsigned long args[5]; + + args[0] = fd; + args[1] = level; + args[2] = optname; + args[3] = (unsigned long) optval; + args[4] = (unsigned long) optlen; + return (socketcall(SYS_GETSOCKOPT, args)); +} + +int host_listen(int sockfd, int backlog) +{ + unsigned long args[2]; + + args[0] = sockfd; + args[1] = backlog; + return socketcall(SYS_LISTEN, args); +} + +ssize_t host_recv(int sockfd, void * buffer, size_t len, int flags) +{ + unsigned long args[4]; + + args[0] = sockfd; + args[1] = (unsigned long) buffer; + args[2] = len; + args[3] = flags; + return (socketcall(SYS_RECV, args)); +} + +ssize_t host_recvfrom(int sockfd, void * buffer, size_t len, int flags, + const struct host_sockaddr *to, unsigned int * tolen) +{ + unsigned long args[6]; + + args[0] = sockfd; + args[1] = (unsigned long) buffer; + args[2] = len; + args[3] = flags; + args[4] = (unsigned long) to; + args[5] = (unsigned long) tolen; + return (socketcall(SYS_RECVFROM, args)); +} +/*ssize_t host_recvmsg(int sockfd, struct msghdr *msg, int flags) +{ + unsigned long args[3]; + + args[0] = sockfd; + args[1] = (unsigned long) msg; + args[2] = flags; + return (socketcall(SYS_RECVMSG, args)); +}*/ + +ssize_t host_send(int sockfd, const void *buffer, size_t len, int flags) +{ + unsigned long args[4]; + + args[0] = sockfd; + args[1] = (unsigned long) buffer; + args[2] = len; + args[3] = flags; + return (socketcall(SYS_SEND, args)); +} + +/*ssize_t host_sendmsg(int sockfd, const struct msghdr *msg, int flags) +{ + unsigned long args[3]; + + args[0] = sockfd; + args[1] = (unsigned long) msg; + args[2] = flags; + return (socketcall(SYS_SENDMSG, args)); +}*/ + +ssize_t host_sendto(int sockfd, const void *buffer, size_t len, int flags, + const struct host_sockaddr *to, unsigned int tolen) +{ + unsigned long args[6]; + + args[0] = sockfd; + args[1] = (unsigned long) buffer; + args[2] = len; + args[3] = flags; + args[4] = (unsigned long) to; + args[5] = tolen; + return (socketcall(SYS_SENDTO, args)); +} + +int host_setsockopt(int fd, int level, int optname, const void *optval, + unsigned int optlen) +{ + unsigned long args[5]; + + args[0] = fd; + args[1] = level; + args[2] = optname; + args[3] = (unsigned long) optval; + args[4] = optlen; + return (socketcall(SYS_SETSOCKOPT, args)); +} + +int host_shutdown(int sockfd, int how) +{ + unsigned long args[2]; + + args[0] = sockfd; + args[1] = how; + return (socketcall(SYS_SHUTDOWN, args)); +} + +int host_socket(int family, int type, int protocol) +{ + unsigned long args[3]; + + args[0] = family; + args[1] = type; + args[2] = (unsigned long) protocol; + return socketcall(SYS_SOCKET, args); +} + +int host_socketpair(int family, int type, int protocol, int sockvec[2]) +{ + unsigned long args[4]; + + args[0] = family; + args[1] = type; + args[2] = protocol; + args[3] = (unsigned long) sockvec; + return socketcall(SYS_SOCKETPAIR, args); +} + diff --git a/host/src/station.c b/host/src/station.c index 30501aa61b..55fe3522a3 100644 --- a/host/src/station.c +++ b/host/src/station.c @@ -26,11 +26,14 @@ #include "host/sci.h" #include "host/station.h" #ifndef UNIT_TEST +#include "host/socket.h" #include "host/syscall.h" #else #include #include #include +#include +#include #endif /* awfull, but needed */ @@ -64,6 +67,10 @@ static netclock_callback_t _ecos_tick_cb; */ int station_init(station_ctx_t *station) { +#ifdef STATION_SOCK + int sock_server_fd; +#endif /* STATION_SOCK */ + DBG_ASSERT(station); if(station == NULL) { @@ -72,8 +79,14 @@ int station_init(station_ctx_t *station) } memset(station, '\0', sizeof(station_ctx_t)); +#ifdef STATION_SOCK + station->sock_fd = -1; + station->sock_pair_fd = -1; + sock_server_fd = -1; +#else /* STATION_SOCK */ station->pipe_in_fd = -1; station->pipe_out_fd = -1; +#endif station->pipe_log_fd = -1; /* get station id */ @@ -85,17 +98,66 @@ int station_init(station_ctx_t *station) /* build pipe names */ sprintf(station->pipe_log_name, "%s/%s_log_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, station->id); unlink(station->pipe_log_name); +#ifndef STATION_SOCK sprintf(station->pipe_in_name, "%s/%s_in_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, station->id); unlink(station->pipe_in_name); sprintf(station->pipe_out_name, "%s/%s_out_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, station->id); unlink(station->pipe_out_name); - +#endif + /* open log */ if(mknod(station->pipe_log_name, 0770 | S_IFIFO, 0) < 0) goto failed; if((station->pipe_log_fd = open(station->pipe_log_name, O_RDWR | O_NONBLOCK, S_IRWXU | S_IRWXG)) < 0) goto failed; +#ifdef STATION_SOCK + /* build sock name */ + sprintf(station->sock_name, "%s/%s_sock_%d", STATION_SOCK_PATH, STATION_SOCK_PREFIX, station->id); + unlink(station->sock_name); + + /* create socket */ +#ifdef UNIT_TEST + int sock_fd[2]; + socketpair(PF_UNIX, SOCK_STREAM, 0, sock_fd); + station->sock_fd = sock_fd[0]; + station->sock_pair_fd = sock_fd[1]; +#else /* UNIT_TEST */ + if((sock_server_fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0) + { + station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION, + "%s: failed to create socket (errno=%d)", + __FUNCTION__, errno); + goto failed; + } + + /* extend send buffer size */ + { + int bufsize = STATION_MAX_SOCK_BUFFER_SIZE; + if(setsockopt (sock_server_fd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize)) < 0) + { + station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION, + "%s: failed to set buffer size to %d bytes (errno=%d)", + __FUNCTION__, bufsize, errno); + goto failed; + } + } + + /* bind the socket */ + { + struct sockaddr_un sockaddr; + sockaddr.sun_family = AF_UNIX; + strcpy (sockaddr.sun_path, station->sock_name); + if((bind (sock_server_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) < 0) + { + station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION, + "%s: bind to '%s' failed (errno=%d)", + __FUNCTION__, station->sock_name, errno); + goto failed; + } + } +#endif /* UNIT_TEST */ +#else /* STATION_SOCK */ /* open in pipe */ if(mknod(station->pipe_in_name, 0770 | S_IFIFO, 0) < 0) { @@ -127,6 +189,8 @@ int station_init(station_ctx_t *station) __FUNCTION__, station->pipe_out_name, errno); goto failed; } + +#endif /* STATION_SOCK */ /* init all contexts */ station->sci = &_my_sci; @@ -143,6 +207,19 @@ int station_init(station_ctx_t *station) /* init the random generator */ srand(station->id); +#if (defined STATION_SOCK) && !defined(UNIT_TEST) + /* listen and accept connection */ + listen(sock_server_fd, 1); + if((station->sock_fd = accept(sock_server_fd, NULL, NULL)) < 0) + { + station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION, + "%s: accept failed (errno=%d)", + __FUNCTION__, errno); + goto failed; + } + close(sock_server_fd); +#endif /* STATION_SOCK && !UNIT_TEST */ + #ifndef UNIT_TEST /* first call for tick */ station_ecos_set_itimer(station, TICK_HZ / 100); @@ -151,18 +228,30 @@ int station_init(station_ctx_t *station) return 0; failed: - if(station->pipe_out_fd >= 0) + if(station->pipe_log_fd >= 0) + close(station->pipe_log_fd); + unlink(station->pipe_log_name); + station->pipe_log_fd = -1; +#ifdef STATION_SOCK + if(station->sock_fd >= 0) + close(station->sock_fd); + if(station->sock_pair_fd >= 0) + close(station->sock_pair_fd); + if(sock_server_fd >= 0) + close(sock_server_fd); + unlink(station->sock_name); + station->sock_fd = -1; + station->sock_pair_fd = -1; +#else /* STATION_SOCK */ + if(station->pipe_out_fd >= 0) close(station->pipe_out_fd); if(station->pipe_in_fd >= 0) close(station->pipe_in_fd); - if(station->pipe_log_fd >= 0) - close(station->pipe_log_fd); unlink(station->pipe_out_name); unlink(station->pipe_in_name); - unlink(station->pipe_log_name); station->pipe_out_fd = -1; station->pipe_in_fd = -1; - station->pipe_log_fd = -1; +#endif /* STATION_SOCK */ station->status = STATION_STATUS_ERROR; return -1; } @@ -176,18 +265,28 @@ void station_down(station_ctx_t *station) if((station == NULL) || (station->status == STATION_STATUS_INIT)) return; - + +#ifdef STATION_SOCK + if(station->sock_fd >= 0) + close(station->sock_fd); + if(station->sock_pair_fd >= 0) + close(station->sock_pair_fd); + unlink(station->sock_name); + station->sock_fd = -1; + station->sock_pair_fd = -1; +#else /* STATION_SOCK */ if(station->pipe_out_fd >= 0) close(station->pipe_out_fd); if(station->pipe_in_fd >= 0) close(station->pipe_in_fd); - if(station->pipe_log_fd >= 0) - close(station->pipe_log_fd); unlink(station->pipe_out_name); unlink(station->pipe_in_name); - unlink(station->pipe_log_name); station->pipe_out_fd = -1; station->pipe_in_fd = -1; +#endif /* STATION_SOCK */ + if(station->pipe_log_fd >= 0) + close(station->pipe_log_fd); + unlink(station->pipe_log_name); station->pipe_log_fd = -1; return; } @@ -247,10 +346,14 @@ int station_idle(station_ctx_t *station) /* vait for next message with 1 second timeout */ FD_ZERO(&read_fds); +#ifdef STATION_SOCK + FD_SET(station->sock_fd, &read_fds); +#else /* STATION_SOCK */ FD_SET(station->pipe_in_fd, &read_fds); +#endif /* STATION_SOCK */ timeout.tv_sec = 1; timeout.tv_usec = 0; - sel = select(station->pipe_in_fd + 1, &read_fds, NULL, NULL, &timeout); + sel = select(STATION_MAX_FD + 1, &read_fds, NULL, NULL, &timeout); if(sel < 0) { station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION, "%s select failed (errno=%d)", __FUNCTION__, errno); diff --git a/host/station.h b/host/station.h index b7199fa75e..3564545cf8 100644 --- a/host/station.h +++ b/host/station.h @@ -21,6 +21,7 @@ #include #include "maximus/common/types/system_types.h" +#include "host/config.h" #include "host/fwd.h" #include "host/sci.h" #include "host/netclock.h" @@ -28,7 +29,11 @@ #define STATION_PIPE_PATH "/tmp" #define STATION_PIPE_PREFIX "station" +#define STATION_SOCK_PATH STATION_PIPE_PATH +#define STATION_SOCK_PREFIX STATION_PIPE_PREFIX #define STATION_MAX_LOG_SIZE 4096 +#define STATION_MAX_SOCK_BUFFER_SIZE (256*1024) +#define STATION_MAX_FD 16 #define TICK_PERIOD_NS 40 /** period of a tick in nanosecond */ #define TICK_HZ 25000000 /** frequence of tick (tick number per second) */ @@ -82,16 +87,22 @@ struct station_ctx struct fcall_ctx *fcall; struct probe_ctx *probe; tick_t current_tick_tck; /** current global clock tick got from sci msg */ +#ifdef STATION_SOCK + int sock_fd; /** file descriptor for messaging input/output */ + int sock_pair_fd; /** file descriptor for socket pair, used by unit test */ + char sock_name[MAXPATHLEN]; /** filename of messaging input/output */ +#else /* STATION_SOCK */ int pipe_in_fd; /** file descriptor for messaging input pipe */ char pipe_in_name[MAXPATHLEN]; /** filename of messaging input file */ int pipe_out_fd; /** file descriptor for messaging output pipe */ char pipe_out_name[MAXPATHLEN]; /** filename of messaging output pipe */ +#endif /* STATION_SOCK */ int pipe_log_fd; /** output pipe file desc to send debug data */ char pipe_log_name[MAXPATHLEN]; /** output pipe filename to send debug data */ station_log_level_t log_level; unsigned long log_mask; struct netclock_callback *ecos_tick_cb; - int is_unit_test; + //int is_unit_test; }; //BEGIN_DECLS @@ -146,7 +157,11 @@ int station_ecos_set_itimer(station_ctx_t *station, tick_t tick); static inline int station_is_initialized(station_ctx_t *station) { +#ifdef STATION_SOCK + return (station->sock_fd >= 0); +#else /* STATION_SOCK */ return ((station->pipe_in_fd >= 0) && (station->pipe_out_fd >= 0)); +#endif /* STATION_SOCK */ } /** diff --git a/host/syscall.h b/host/syscall.h index f413cb9f89..a212e66cda 100644 --- a/host/syscall.h +++ b/host/syscall.h @@ -75,6 +75,17 @@ #define FD_CLR CYG_HAL_SYS_FD_CLR /** functions redefinition */ +/*#define system_adapt(function) { \ + int result; \ + if((result = function(##args)) < 0) \ + errno = -result; \ + result = -1; \ + } \ + return result; \ +} + +#define open(args...) system_adapt(cyg_hal_sys_open)*/ + static inline int open(const char *pathname, int flags, mode_t mode) { int result; @@ -152,7 +163,8 @@ static inline int unlink(const char *pathname) return result; } -static inline pid_t getpid(void) +#define getpid cyg_hal_sys_getpid +/*static inline pid_t getpid(void) { int result; if((result = cyg_hal_sys_getpid()) < 0) @@ -161,7 +173,7 @@ static inline pid_t getpid(void) result = -1; } return result; -} +}*/ /** socket redefinition */ #define AF_UNIX HOST_AF_UNIX diff --git a/host/test/src/test_fcall.c b/host/test/src/test_fcall.c index cfd7f5c241..c3e21b861a 100644 --- a/host/test/src/test_fcall.c +++ b/host/test/src/test_fcall.c @@ -325,9 +325,12 @@ void fcall_return_test_case(test_t t) ); } test_end; - +#ifdef STATION_SOCK + fd_in = station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + test_begin(t, "fcall_return") { test_fail_unless( @@ -374,7 +377,9 @@ void fcall_return_test_case(test_t t) } test_end; station_down(&station); +#ifndef STATION_SOCK close(fd_in); +#endif } int _test_recv_result; @@ -447,8 +452,12 @@ void fcall_recv_test_case(test_t t) ); } test_end; +#ifdef STATION_SOCK + fd_in = station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(station.pipe_out_name, O_RDONLY); - +#endif /* STATION_SOCK */ + test_begin(t, "check recv") { sci_msg_init(&msg, buffer, 256); @@ -539,7 +548,9 @@ void fcall_recv_test_case(test_t t) } test_end; station_down(&station); +#ifndef STATION_SOCK close(fd_in); +#endif /* STATION_SOCK */ return; } diff --git a/host/test/src/test_netclock.c b/host/test/src/test_netclock.c index 2a4cec85e5..df23add6ea 100644 --- a/host/test/src/test_netclock.c +++ b/host/test/src/test_netclock.c @@ -254,7 +254,11 @@ void netclock_schedule_test_case(test_t t) ); } test_end +#ifdef STATION_SOCK + fd_in = station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ test_begin(t, "recv netclock msg") { @@ -321,6 +325,9 @@ void netclock_schedule_test_case(test_t t) // } test_end; station_down(&station); +#ifndef STATION_SOCK + close(fd_in); +#endif /* STATION_SOCK */ return; } @@ -370,7 +377,11 @@ void netclock_unschedule_test_case(test_t t) && (netclock.callback_nb == 1) ); +#ifdef STATION_SOCK + fd_in = station.sock_pair_fd; +#else /* STATION_SOCK */ fd_in = open(station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ test_fail_if((len = read(fd_in, buffer, sizeof(sci_msg_hdr_t))) != sizeof(sci_msg_hdr_t)); hdr = (sci_msg_hdr_t *)buffer; @@ -396,9 +407,11 @@ void netclock_unschedule_test_case(test_t t) ); } test_end; - close(fd_in); station_down(&station); - +#ifndef STATION_SOCK + close(fd_in); +#endif /* STATION_SOCK */ + return; } @@ -423,7 +436,7 @@ void netclock_recv_test_case(test_t t) netclock_id_t id; unsigned char buffer[256]; char data_buffer[32]; - int fd_in, fd_out; + int fd_in; test_case_begin(t, "recv"); @@ -448,8 +461,12 @@ void netclock_recv_test_case(test_t t) ); } test_end; - fd_in = open(station.pipe_out_name, O_RDONLY); - fd_out = open(station.pipe_in_name, O_WRONLY); +#ifdef STATION_SOCK + fd_in = station.sock_pair_fd; +#else /* STATION_SOCK */ + fd_in = open(station.pipe_out_name, O_RDONLY); +#endif /* STATION_SOCK */ + memset(data_buffer, '\0', 32); test_begin(t, "recv") @@ -473,8 +490,9 @@ void netclock_recv_test_case(test_t t) ); } test_end; +#ifndef STATION_SOCK close(fd_in); - close(fd_out); +#endif /* STATION_SOCK */ station_down(&station); return; diff --git a/host/test/src/test_sci.c b/host/test/src/test_sci.c index c116856e3a..81b724d30d 100644 --- a/host/test/src/test_sci.c +++ b/host/test/src/test_sci.c @@ -266,7 +266,11 @@ void sci_send_test_case(test_t t) test_begin(t, "check send") { +#ifdef STATION_SOCK + fd_out = station.sock_pair_fd; +#else fd_out = open(station.pipe_out_name, O_RDONLY); +#endif sci_msg_push(&msg, strlen(TEST_DATA_STR)); memcpy(msg.data_begin, TEST_DATA_STR, msg.length); sci_fill_hdr(&sci, &msg, SCI_MSG_TYPE_FUNCTION_CALL, 0); @@ -334,7 +338,11 @@ void sci_recv_test_case(test_t t) msg.hdr.station = (station_msg_hdr_t *)msg.data_begin; msg.hdr.station->type = SYSTEM_TYPE_IDLE; sci_fill_hdr(&sci, &msg, SCI_MSG_TYPE_SYSTEM, 0); +#ifdef STATION_SOCK + fd_in = station.sock_pair_fd; +#else fd_in = open(station.pipe_in_name, O_WRONLY); +#endif test_begin(t, "bad magic") { ((char *)&msg.sci_hdr->magic_id)[2] = magic_id[2] + 1; diff --git a/host/test/src/test_station.c b/host/test/src/test_station.c index 4c09fa7dca..f3f2295a78 100644 --- a/host/test/src/test_station.c +++ b/host/test/src/test_station.c @@ -66,7 +66,9 @@ void station_init_test_case(test_t t) { station_ctx_t station; char buffer[256]; +#ifndef STATION_SOCK struct stat my_stat; +#endif test_case_begin(t, "init"); station_init(&station); @@ -75,8 +77,13 @@ void station_init_test_case(test_t t) { test_fail_unless( (station.id == getpid()) +#ifdef STATION_SOCK + && (station.sock_fd >= 0) + && (station.sock_pair_fd >= 0) +#else && (station.pipe_in_fd >= 0) && (station.pipe_out_fd >= 0) +#endif && (station.pipe_log_fd >= 0) && (station.sci != NULL) && (station.log_level == STATION_LOG_WARNING) @@ -90,14 +97,20 @@ void station_init_test_case(test_t t) test_begin(t, "check pipe names") { /* check pipe names */ - sprintf(buffer, "%s/%s_in_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getpid()); +#ifdef STATION_SOCK + sprintf(buffer, "%s/%s_sock_%d", STATION_SOCK_PATH, STATION_SOCK_PREFIX, getpid()); + test_fail_if(strcmp(buffer, station.sock_name)); +#else + sprintf(buffer, "%s/%s_in_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getpid()); test_fail_if(strcmp(buffer, station.pipe_in_name)); sprintf(buffer, "%s/%s_out_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getpid()); test_fail_if(strcmp(buffer, station.pipe_out_name)); +#endif /*sprintf(buffer, "%s/%s_debug_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getpid()); test_fail_if(strcmp(buffer, station->debug_out_name));*/ } test_end; +#ifndef STATION_SOCK test_begin(t, "check if pipes are created and open") { test_fail_unless( @@ -110,7 +123,8 @@ void station_init_test_case(test_t t) && (fstat(station.pipe_out_fd, &my_stat) >= 0) && S_ISFIFO(my_stat.st_mode)); } test_end; - +#endif + station_down(&station); } @@ -118,7 +132,9 @@ void station_down_test_case(test_t t) { station_ctx_t station; int fd_in, fd_out; +#ifndef STATION_SOCK char name_in[256], name_out[256]; +#endif struct stat my_stat; test_case_begin(t, "down"); @@ -130,12 +146,25 @@ void station_down_test_case(test_t t) } test_end; station_init(&station); +#ifdef STATION_SOCK + fd_in = station.sock_fd; + fd_out = station.sock_pair_fd; +#else fd_in = station.pipe_in_fd; fd_out = station.pipe_out_fd; strcpy(name_in, station.pipe_in_name); strcpy(name_out, station.pipe_out_name); +#endif station_down(&station); - + +#ifdef STATION_SOCK + test_begin(t, "socket is closed") + { + test_fail_unless( + (fstat(fd_in, &my_stat) < 0) + && (fstat(fd_out, &my_stat) < 0)); + } test_end; +#else test_begin(t, "pipes are closed") { test_fail_unless( @@ -149,6 +178,7 @@ void station_down_test_case(test_t t) (stat(name_in, &my_stat) < 0) && (stat(name_out, &my_stat) < 0)); } test_end; +#endif } void station_idle_test_case(test_t t) @@ -173,9 +203,15 @@ void station_idle_test_case(test_t t) test_begin(t, "recv idle msg") { +#ifdef STATION_SOCK + test_fail_unless( + ((fd_in = station.sock_pair_fd) >= 0) + ); +#else test_fail_if( ((fd_in = open(station.pipe_out_name, O_RDONLY)) < 0) ); +#endif test_fail_unless( (station_idle(&station) >= 0) && ((len = read(fd_in, buffer, sizeof(sci_msg_hdr_t))) == sizeof(sci_msg_hdr_t)) diff --git a/maximus/sci/src/SciServer.cpp b/maximus/sci/src/SciServer.cpp index 88071fb54f..b1737754d4 100644 --- a/maximus/sci/src/SciServer.cpp +++ b/maximus/sci/src/SciServer.cpp @@ -184,12 +184,11 @@ bool SciServer::process ( ) header_len = sizeof(struct Sci_Msg_Header); if((len = read(fd_index, &header, sizeof(struct Sci_Msg_Header))) < header_len) { - clog << logger(LOG_ERROR) << "header read error: len = " << dec << len << ", errno = " << errno << endl; - if (0 == len) - { - throw Error(__PRETTY_FUNCTION__, "Header read error: len = 0"); - } - continue; + if(len < 0) + clog << logger(LOG_ERROR) << "header read error: len = " << dec << len << ", errno = " << errno << endl; + else + clog << logger(LOG_ERROR) << "header read error: len = " << dec << len << endl; + throw Error(__PRETTY_FUNCTION__, "Header read error: len < header_len"); } else { @@ -492,6 +491,7 @@ bool SciServer::sendSciMsgToAllActiveStations ( SciMsg & sci_msg_to_send ) const clog << logger(LOG_INFO) << "--- send " << displaySciMsgType(sci_msg_to_send.getSciMsgHeader()->type) \ << " SCI message to station " << dec << (*it)->getStationId() \ << " (0x" << setfill('0') << setw(4) << uppercase << hex << (*it)->getStationId() << ")" << dec << endl; + clog << logger(LOG_COM) << "socket ID = " << (*it)->getOutputFileDescriptor() << endl; } } } diff --git a/maximus/system/inc/Station.h b/maximus/system/inc/Station.h index 0a328dd0d1..154f7b4b8f 100644 --- a/maximus/system/inc/Station.h +++ b/maximus/system/inc/Station.h @@ -31,6 +31,7 @@ The original location of this file is /home/buret/eclipse/maximus/system/inc/Sta #ifndef STATION_H #define STATION_H +#include "host/config.h" #include "system_types.h" #include "sci_types.h" @@ -40,8 +41,11 @@ The original location of this file is /home/buret/eclipse/maximus/system/inc/Sta #define STATION_PIPE_PATH "/tmp" #define STATION_PIPE_PREFIX "station" +#define STATION_SOCK_PATH STATION_PIPE_PATH +#define STATION_SOCK_PREFIX STATION_PIPE_PREFIX #define STATION_WAIT_LOOP_NB 10 #define STATION_WAIT_TIMEOUT_MS 100 +#define STATION_MAX_SOCK_BUFFER_SIZE (256*1024) class StationConfiguration; @@ -63,8 +67,13 @@ private: // private attributes // - File_Descriptor mInputPipe, mOutputPipe, mLogPipe; - pid_t mPid; +#ifdef STATION_SOCK + File_Descriptor mSocket; +#else /* STATION_SOCK */ + File_Descriptor mInputPipe, mOutputPipe; +#endif /* STATION_SOCK */ + File_Descriptor mLogPipe; + pid_t mPid; Station_Status mStationStatus; unsigned short int mStationIdleCounter; StationConfiguration * mpStationConfiguration; diff --git a/maximus/system/src/Station.cpp b/maximus/system/src/Station.cpp index 3d6c9d9e4e..d035f6bc05 100644 --- a/maximus/system/src/Station.cpp +++ b/maximus/system/src/Station.cpp @@ -40,6 +40,8 @@ The original location of this file is /home/buret/eclipse/maximus/system/src/Sta #include #include // for 'exec()' and 'system()' #include +#include +#include #include #include // for 'setfill()' and 'setw()' using namespace std; @@ -53,8 +55,12 @@ extern string stationTest; Station::Station ( string station_executable ): +#ifdef STATION_SOCK +mSocket(-1), +#else /* STATION_SOCK */ mInputPipe(-1), mOutputPipe(-1), +#endif /* STATION_SOCK */ mLogPipe(-1), mPid(0), mStationStatus(MAXIMUS_STATION_STATUS_NONE), @@ -153,7 +159,7 @@ void Station::displayStation ( ) const { logFunction(); - clog << logger(LOG_INFO) << "\t[input file descriptor = " << dec << getInputFileDescriptor() << ", output file descriptor = " << getOutputFileDescriptor() << ", log file descriptor = " << getLogFileDescriptor() \ + clog << logger(LOG_INFO) << "\t[socket descriptor = " << dec << getInputFileDescriptor() << ", log file descriptor = " << getLogFileDescriptor() \ << ", process id = " << getStationId() << " (0x" << setfill('0') << setw(4) << uppercase << hex << getStationId() << ")]" << dec << endl; } @@ -223,16 +229,22 @@ const pid_t Station::getPid ( ) const bool Station::setInputFileDescriptor ( const File_Descriptor input_file_descriptor ) { +#ifdef STATION_SOCK + mSocket = input_file_descriptor; +#else /* STATION_SOCK */ mInputPipe = input_file_descriptor; - +#endif /* STATION_SOCK */ return true; } bool Station::setOutputFileDescriptor ( const File_Descriptor output_file_descriptor ) { +#ifdef STATION_SOCK + mSocket = output_file_descriptor; +#else /* STATION_SOCK */ mOutputPipe = output_file_descriptor; - +#endif /* STATION_SOCK */ return true; } @@ -247,13 +259,21 @@ bool Station::setLogFileDescriptor ( const File_Descriptor log_file_descriptor ) const File_Descriptor Station::getInputFileDescriptor ( ) const { +#ifdef STATION_SOCK + return mSocket; +#else /* STATION_SOCK */ return mInputPipe; +#endif /* STATION_SOCK */ } const File_Descriptor Station::getOutputFileDescriptor ( ) const { +#ifdef STATION_SOCK + return mSocket; +#else /* STATION_SOCK */ return mOutputPipe; +#endif /* STATION_SOCK */ } @@ -356,6 +376,7 @@ void Station::startProcess ( const string station_executable ) char stationExecutable[station_executable.size()+1]; size_t stringLength = station_executable.copy(stationExecutable, station_executable.size()); stationExecutable[stringLength] = '\0'; + if (-1 == execlp(stationExecutable, stationExecutable, NULL)) { throw Error(__PRETTY_FUNCTION__, "Cannot launch station executable", errno); @@ -366,33 +387,83 @@ void Station::startProcess ( const string station_executable ) // Loop until pipe creation: max = STATION_WAIT_LOOP_NB * STATION_WAIT_TIMEOUT_MS ms // int loop; + int fd_status; char nameBuffer[256]; - sprintf(nameBuffer, "%s/%s_in_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getPid()); + +#ifdef STATION_SOCK + struct sockaddr_un sockaddr; + int bufsize = STATION_MAX_SOCK_BUFFER_SIZE; + sprintf(nameBuffer, "%s/%s_sock_%d", STATION_SOCK_PATH, STATION_SOCK_PREFIX, getPid()); + // open the socket + if((mSocket = socket (PF_UNIX, SOCK_STREAM, 0)) < 0) + { + throw Error(__FUNCTION__ , "Cannot create socket", errno); + } + + // avoid other stations to inherit the socket + fd_status = fcntl(mSocket, F_GETFD); + fcntl(mSocket, F_SETFD, fd_status | FD_CLOEXEC); + + // extend send buffer size + if(setsockopt (mSocket, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize)) < 0) + { + throw Error(__FUNCTION__ , "Cannot increase buffer size", errno); + } + sockaddr.sun_family = AF_UNIX; + strcpy (sockaddr.sun_path, nameBuffer); + for(loop = 0; loop < STATION_WAIT_LOOP_NB; loop++) { - // Open my output pipe + // Connect to server // - if((mOutputPipe = open(nameBuffer, O_WRONLY)) >= 0) // my output pipe is the station in pipe - { + if((connect(mSocket, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) >= 0) break; - } usleep(STATION_WAIT_TIMEOUT_MS * 1000); } + if(loop >= STATION_WAIT_LOOP_NB) { - stopProcess(); - throw Error(__FUNCTION__ , "Cannot open output pipe", errno); + Error e(__FUNCTION__ , "Cannot connect to station", errno); + e.display(); + stopProcess(); + throw e; } - // Open my input pipe - // +#else /* STATION_SOCK */ + // Open input pipe sprintf(nameBuffer, "%s/%s_out_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getPid()); - if((mInputPipe = open(nameBuffer, O_RDONLY)) < 0) // my input pipe is the station out pipe + for(loop = 0; loop < STATION_WAIT_LOOP_NB; loop++) + { + // Connect to server + // + if((mInputPipe = open(nameBuffer, O_RDONLY | O_NONBLOCK)) >= 0) + break; + usleep(STATION_WAIT_TIMEOUT_MS * 1000); + } + if(loop >= STATION_WAIT_LOOP_NB) + { + Error e(__FUNCTION__ , "Cannot connect to station", errno); + e.display(); + stopProcess(); + throw e; + } + + // avoid other stations to inherit the pipe + fd_status = fcntl(mInputPipe, F_GETFD); + fcntl(mInputPipe, F_SETFD, fd_status | FD_CLOEXEC); + + // Open output pipe + sprintf(nameBuffer, "%s/%s_in_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getPid()); + if((mOutputPipe = open(nameBuffer, O_WRONLY | O_NONBLOCK)) < 0) { stopProcess(); - throw Error(__PRETTY_FUNCTION__, "Cannot open input pipe", errno); + throw Error(__PRETTY_FUNCTION__, "Cannot open output pipe", errno); } - + // avoid other stations to inherit the pipe + fd_status = fcntl(mOutputPipe, F_GETFD); + fcntl(mOutputPipe, F_SETFD, fd_status | FD_CLOEXEC); +#endif /* STATION_SOCK */ + // Open log pipe // sprintf(nameBuffer, "%s/%s_log_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getPid()); @@ -401,13 +472,23 @@ void Station::startProcess ( const string station_executable ) stopProcess(); throw Error(__PRETTY_FUNCTION__, "Cannot open log pipe", errno); } + + // avoid other stations to inherit the pipe + fd_status = fcntl(mLogPipe, F_GETFD); + fcntl(mLogPipe, F_SETFD, fd_status | FD_CLOEXEC); + } void Station::stopProcess ( ) { logFunction(); - +#ifdef STATION_SOCK + if(mSocket >= 0) + { + close(mSocket); + } +#else /* STATION_SOCK */ if(mInputPipe >= 0) { close(mInputPipe); @@ -416,6 +497,7 @@ void Station::stopProcess ( ) { close(mOutputPipe); } +#endif /* STATION_SOCK */ if(mLogPipe >= 0) { close(mLogPipe); -- cgit v1.2.3