Skip to content

Commit a851df6

Browse files
author
Deepika
committed
Code/bug after testing
1. Fixing astyle and docs 2. Extra mutex lock was removed 3. Bytes are updated when send/recv > 0 and not in case of -ve error 4. Review comments 5. Guard statistics implementation in test with MBED_NW_STATS_ENABLED
1 parent eec54a0 commit a851df6

13 files changed

+54
-47
lines changed

TESTS/netsocket/tcp/main.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,12 @@ int split2half_rmng_tcp_test_time()
119119
return (tcp_global::TESTS_TIMEOUT - tc_bucket.read()) / 2;
120120
}
121121

122+
#if defined(MBED_NW_STATS_ENABLED)
122123
int fetch_stats()
123124
{
124-
#if defined(MBED_NW_STATS_ENABLED)
125125
return SocketStats::mbed_stats_socket_get_each(&tcp_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
126-
#else
127-
return 0;
128-
#endif
129126
}
127+
#endif
130128

131129
// Test setup
132130
utest::v1::status_t greentea_setup(const size_t number_of_cases)

TESTS/netsocket/tcp/tcp_tests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ void drop_bad_packets(TCPSocket &sock, int orig_timeout);
2323
void fill_tx_buffer_ascii(char *buff, size_t len);
2424
nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket &sock);
2525
nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket &sock);
26-
int fetch_stats(void);
2726

2827
#if defined(MBED_NW_STATS_ENABLED)
2928
extern mbed_stats_socket_t tcp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
29+
int fetch_stats(void);
3030
#endif
3131

3232
/**

TESTS/netsocket/tcp/tcpsocket_echotest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)
114114

115115
void TCPSOCKET_ECHOTEST_NONBLOCK()
116116
{
117+
#if defined(MBED_NW_STATS_ENABLED)
117118
int j = 0;
118119
int count = fetch_stats();
119120
for (; j < count; j++) {
120121
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
121122
}
122-
123+
#endif
123124
tc_exec_time.start();
124125
time_allotted = split2half_rmng_tcp_test_time(); // [s]
125126

@@ -166,14 +167,15 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
166167
bytes2send -= sent;
167168
}
168169
printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s);
170+
#if defined(MBED_NW_STATS_ENABLED)
169171
count = fetch_stats();
170172
for (j = 0; j < count; j++) {
171173
if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
172174
break;
173175
}
174176
}
175177
TEST_ASSERT_EQUAL(bytes2send, tcp_stats[j].sent_bytes);
176-
178+
#endif
177179
tx_sem.wait(split2half_rmng_tcp_test_time());
178180
thread->join();
179181
delete thread;

TESTS/netsocket/tcp/tcpsocket_open_close_repeat.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ using namespace utest::v1;
2626

2727
void TCPSOCKET_OPEN_CLOSE_REPEAT()
2828
{
29+
#if defined(MBED_NW_STATS_ENABLED)
2930
int count = fetch_stats();
3031
for (int j = 0; j < count; j++) {
3132
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
3233
}
33-
34+
#endif
3435
TCPSocket *sock = new TCPSocket;
3536
if (!sock) {
3637
TEST_FAIL();
@@ -41,9 +42,10 @@ void TCPSOCKET_OPEN_CLOSE_REPEAT()
4142
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
4243
}
4344
delete sock;
44-
45+
#if defined(MBED_NW_STATS_ENABLED)
4546
count = fetch_stats();
4647
for (int j = 0; j < count; j++) {
4748
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
4849
}
50+
#endif
4951
}

TESTS/netsocket/tcp/tcpsocket_open_limit.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void TCPSOCKET_OPEN_LIMIT()
7070
break;
7171
}
7272

73+
#if defined(MBED_NW_STATS_ENABLED)
7374
int count = fetch_stats();
7475
int open_count = 0;
7576
for (int j = 0; j < count; j++) {
@@ -78,6 +79,7 @@ void TCPSOCKET_OPEN_LIMIT()
7879
}
7980
}
8081
TEST_ASSERT(open_count >= 4);
82+
#endif
8183

8284
TCPSocketItem *tmp;
8385
for (TCPSocketItem *it = socket_list_head; it;) {

TESTS/netsocket/udp/main.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,12 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
8080
}
8181
}
8282

83+
#if defined(MBED_NW_STATS_ENABLED)
8384
int fetch_stats()
8485
{
85-
#if defined(MBED_NW_STATS_ENABLED)
8686
return SocketStats::mbed_stats_socket_get_each(&udp_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
87-
#else
88-
return 0;
89-
#endif
9087
}
88+
#endif
9189

9290
// Test setup
9391
utest::v1::status_t greentea_setup(const size_t number_of_cases)

TESTS/netsocket/udp/udp_tests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
NetworkInterface *get_interface();
2222
void drop_bad_packets(UDPSocket &sock, int orig_timeout);
2323
void fill_tx_buffer_ascii(char *buff, size_t len);
24-
int fetch_stats(void);
2524

2625
#if defined(MBED_NW_STATS_ENABLED)
2726
extern mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
27+
int fetch_stats(void);
2828
#endif
2929

3030
/*

TESTS/netsocket/udp/udpsocket_open_close_repeat.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ using namespace utest::v1;
2626

2727
void UDPSOCKET_OPEN_CLOSE_REPEAT()
2828
{
29+
#if defined(MBED_NW_STATS_ENABLED)
2930
int count = fetch_stats();
3031
for (int j = 0; j < count; j++) {
3132
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
3233
}
33-
34+
#endif
3435
UDPSocket *sock = new UDPSocket;
3536
if (!sock) {
3637
TEST_FAIL();
@@ -41,9 +42,10 @@ void UDPSOCKET_OPEN_CLOSE_REPEAT()
4142
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
4243
}
4344
delete sock;
44-
45+
#if defined(MBED_NW_STATS_ENABLED)
4546
count = fetch_stats();
4647
for (int j = 0; j < count; j++) {
4748
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
4849
}
50+
#endif
4951
}

TESTS/netsocket/udp/udpsocket_open_limit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void UDPSOCKET_OPEN_LIMIT()
7070
if (!socket_list_head) {
7171
break;
7272
}
73-
73+
#if defined(MBED_NW_STATS_ENABLED)
7474
int count = fetch_stats();
7575
int open_count = 0;
7676
for (int j = 0; j < count; j++) {
@@ -79,7 +79,7 @@ void UDPSOCKET_OPEN_LIMIT()
7979
}
8080
}
8181
TEST_ASSERT(open_count >= 3);
82-
82+
#endif
8383
UDPSocketItem *tmp;
8484
for (UDPSocketItem *it = socket_list_head; it;) {
8585
++open_sockets[i];

features/netsocket/SocketStats.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ uint32_t SocketStats::_size = 0;
3131

3232
int SocketStats::get_entry_position(const Socket *const reference_id)
3333
{
34-
_mutex->lock();
3534
for (uint32_t j = 0; j < _size; j++) {
3635
if (_stats[j].reference_id == reference_id) {
3736
return j;
@@ -70,7 +69,7 @@ void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
7069
if (get_entry_position(reference_id) >= 0) {
7170
// Duplicate entry
7271
MBED_WARNING1(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_INVALID_INDEX), "Duplicate socket Reference ID ", reference_id);
73-
} else if (_size <= MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT) {
72+
} else if (_size < MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT) {
7473
// Add new entry
7574
_stats[_size].reference_id = (Socket *)reference_id;
7675
_size++;
@@ -87,7 +86,7 @@ void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
8786
}
8887
}
8988
if (-1 == position) {
90-
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_OUT_OF_RESOURCES), "List full with all open sockets");
89+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_OUT_OF_RESOURCES), "List full with all open sockets");
9190
}
9291
memset(&_stats[position], 0, sizeof(mbed_stats_socket_t));
9392
_stats[position].reference_id = (Socket *)reference_id;
@@ -117,10 +116,8 @@ void SocketStats::stats_update_peer(const Socket *const reference_id, const Sock
117116
#if defined(MBED_NW_STATS_ENABLED)
118117
_mutex->lock();
119118
int position = get_entry_position(reference_id);
120-
if (position >= 0) {
121-
if (!_stats[position].peer) {
122-
_stats[position].peer = peer;
123-
}
119+
if ((position >= 0) && (!_stats[position].peer)) {
120+
_stats[position].peer = peer;
124121
}
125122
_mutex->unlock();
126123
#endif
@@ -143,7 +140,7 @@ void SocketStats::stats_update_sent_bytes(const Socket *const reference_id, size
143140
#if defined(MBED_NW_STATS_ENABLED)
144141
_mutex->lock();
145142
int position = get_entry_position(reference_id);
146-
if (position >= 0) {
143+
if ((position >= 0) && (sent_bytes > 0)) {
147144
_stats[position].sent_bytes += sent_bytes;
148145
}
149146
_mutex->unlock();
@@ -155,7 +152,7 @@ void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size
155152
#if defined(MBED_NW_STATS_ENABLED)
156153
_mutex->lock();
157154
int position = get_entry_position(reference_id);
158-
if (position >= 0) {
155+
if ((position >= 0) && (recv_bytes > 0)) {
159156
_stats[position].recv_bytes += recv_bytes;
160157
}
161158
_mutex->unlock();

0 commit comments

Comments
 (0)