Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 /*
vithyat 0:977e87915078 2 * mbed Microcontroller Library
vithyat 0:977e87915078 3 * Copyright (c) 2006-2018 ARM Limited
vithyat 0:977e87915078 4 *
vithyat 0:977e87915078 5 * SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 6 *
vithyat 0:977e87915078 7 * Licensed under the Apache License, Version 2.0 (the "License");
vithyat 0:977e87915078 8 * you may not use this file except in compliance with the License.
vithyat 0:977e87915078 9 * You may obtain a copy of the License at
vithyat 0:977e87915078 10 *
vithyat 0:977e87915078 11 * http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 12 *
vithyat 0:977e87915078 13 * Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 14 * distributed under the License is distributed on an "AS IS" BASIS,
vithyat 0:977e87915078 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 16 * See the License for the specific language governing permissions and
vithyat 0:977e87915078 17 * limitations under the License.
vithyat 0:977e87915078 18 */
vithyat 0:977e87915078 19
vithyat 0:977e87915078 20 /*
vithyat 0:977e87915078 21 * Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test
vithyat 0:977e87915078 22 */
vithyat 0:977e87915078 23
vithyat 0:977e87915078 24 #include "mbed.h"
vithyat 0:977e87915078 25 #include "unity/unity.h"
vithyat 0:977e87915078 26 #include "greentea-client/test_env.h"
vithyat 0:977e87915078 27 #include <string>
vithyat 0:977e87915078 28
vithyat 0:977e87915078 29 #define MAX_THREADS 5
vithyat 0:977e87915078 30
vithyat 0:977e87915078 31 #define MAX_RETRIES 3
vithyat 0:977e87915078 32
vithyat 0:977e87915078 33 #ifdef MBED_CONF_DOWNLOAD_TEST_URL_HOST
vithyat 0:977e87915078 34 const char dl_host[] = MBED_CONF_DOWNLOAD_TEST_URL_HOST;
vithyat 0:977e87915078 35 #else
vithyat 0:977e87915078 36 const char dl_host[] = "armmbed.github.io";
vithyat 0:977e87915078 37 #endif
vithyat 0:977e87915078 38 #ifdef MBED_CONF_DOWNLOAD_TEST_URL_PATH
vithyat 0:977e87915078 39 const char dl_path[] = MBED_CONF_DOWNLOAD_TEST_URL_PATH;
vithyat 0:977e87915078 40 #else
vithyat 0:977e87915078 41 const char dl_path[] = "/mbed-test-files/alice.txt";
vithyat 0:977e87915078 42 #endif
vithyat 0:977e87915078 43
vithyat 0:977e87915078 44 const char req_template[] = "GET %s HTTP/1.1\nHost: %s\n\n";
vithyat 0:977e87915078 45
vithyat 0:977e87915078 46 #define REQ_BUF_SIZE 256
vithyat 0:977e87915078 47 #define RECV_BUF_SIZE 1024
vithyat 0:977e87915078 48
vithyat 0:977e87915078 49 static char g_request_buffer[MAX_THREADS][REQ_BUF_SIZE]; // the default request is 65bytes long.
vithyat 0:977e87915078 50 static char g_receive_buffer[MAX_THREADS * RECV_BUF_SIZE];
vithyat 0:977e87915078 51
vithyat 0:977e87915078 52 static volatile bool event_fired[MAX_THREADS] = { };
vithyat 0:977e87915078 53
vithyat 0:977e87915078 54 static void socket_event_0(void) { event_fired[0] = true; }
vithyat 0:977e87915078 55 static void socket_event_1(void) { event_fired[1] = true; }
vithyat 0:977e87915078 56 static void socket_event_2(void) { event_fired[2] = true; }
vithyat 0:977e87915078 57 static void socket_event_3(void) { event_fired[3] = true; }
vithyat 0:977e87915078 58 static void socket_event_4(void) { event_fired[4] = true; }
vithyat 0:977e87915078 59
vithyat 0:977e87915078 60
vithyat 0:977e87915078 61 size_t download_test(NetworkInterface* interface, const unsigned char* data, size_t data_length, size_t buff_size, uint32_t thread_id) {
vithyat 0:977e87915078 62 int result = -1;
vithyat 0:977e87915078 63
vithyat 0:977e87915078 64 TEST_ASSERT_MESSAGE(MAX_THREADS > thread_id, "Unsupported thread ID");
vithyat 0:977e87915078 65 TEST_ASSERT_MESSAGE((MAX_THREADS * RECV_BUF_SIZE) >= buff_size, "Cannot test with the requested buffer size");
vithyat 0:977e87915078 66
vithyat 0:977e87915078 67 /* setup TCP socket */
vithyat 0:977e87915078 68 TCPSocket tcpsocket(interface);
vithyat 0:977e87915078 69
vithyat 0:977e87915078 70 for (int tries = 0; tries < MAX_RETRIES; tries++) {
vithyat 0:977e87915078 71 result = tcpsocket.connect(dl_host, 80);
vithyat 0:977e87915078 72 TEST_ASSERT_MESSAGE(result != NSAPI_ERROR_NO_SOCKET, "out of sockets");
vithyat 0:977e87915078 73
vithyat 0:977e87915078 74 if (result == 0) {
vithyat 0:977e87915078 75 break;
vithyat 0:977e87915078 76 }
vithyat 0:977e87915078 77 ThisThread::sleep_for(1000);
vithyat 0:977e87915078 78 printf("[NET-%d] Connection failed. Retry %d of %d\r\n", thread_id, tries, MAX_RETRIES);
vithyat 0:977e87915078 79 }
vithyat 0:977e87915078 80 TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "failed to connect");
vithyat 0:977e87915078 81
vithyat 0:977e87915078 82 if (thread_id == 0) {
vithyat 0:977e87915078 83 // technically this is non-threaded mode
vithyat 0:977e87915078 84 tcpsocket.sigio(socket_event_0);
vithyat 0:977e87915078 85 } else if (thread_id == 1) {
vithyat 0:977e87915078 86 tcpsocket.sigio(socket_event_1);
vithyat 0:977e87915078 87 } else if (thread_id == 2) {
vithyat 0:977e87915078 88 tcpsocket.sigio(socket_event_2);
vithyat 0:977e87915078 89 } else if (thread_id == 3) {
vithyat 0:977e87915078 90 tcpsocket.sigio(socket_event_3);
vithyat 0:977e87915078 91 } else if (thread_id == 4) {
vithyat 0:977e87915078 92 tcpsocket.sigio(socket_event_4);
vithyat 0:977e87915078 93 } else {
vithyat 0:977e87915078 94 TEST_ASSERT_MESSAGE(0, "wrong thread id");
vithyat 0:977e87915078 95 }
vithyat 0:977e87915078 96 printf("[NET-%d] Registered socket callback function\r\n", thread_id);
vithyat 0:977e87915078 97 event_fired[thread_id] = false;
vithyat 0:977e87915078 98
vithyat 0:977e87915078 99 /* setup request */
vithyat 0:977e87915078 100 char *request = g_request_buffer[thread_id];
vithyat 0:977e87915078 101
vithyat 0:977e87915078 102 /* construct request */
vithyat 0:977e87915078 103 size_t req_len = snprintf(request, REQ_BUF_SIZE-1, req_template, dl_path, dl_host);
vithyat 0:977e87915078 104 request[req_len] = 0;
vithyat 0:977e87915078 105 printf("[NET-%d] Request header (%u): %s\r\n", thread_id, req_len, request);
vithyat 0:977e87915078 106
vithyat 0:977e87915078 107 /* send request to server */
vithyat 0:977e87915078 108 result = tcpsocket.send(request, req_len);
vithyat 0:977e87915078 109 TEST_ASSERT_EQUAL_INT_MESSAGE(req_len, result, "failed to send");
vithyat 0:977e87915078 110
vithyat 0:977e87915078 111 /* read response */
vithyat 0:977e87915078 112 char* receive_buffer = &g_receive_buffer[thread_id * RECV_BUF_SIZE];
vithyat 0:977e87915078 113
vithyat 0:977e87915078 114 tcpsocket.set_blocking(false);
vithyat 0:977e87915078 115 printf("[NET-%d] Non-blocking socket mode set\r\n", thread_id);
vithyat 0:977e87915078 116
vithyat 0:977e87915078 117 size_t received_bytes = 0;
vithyat 0:977e87915078 118 int body_index = -1;
vithyat 0:977e87915078 119
vithyat 0:977e87915078 120 float speed;
vithyat 0:977e87915078 121 float percent;
vithyat 0:977e87915078 122 uint32_t time_left;
vithyat 0:977e87915078 123 Timer timer;
vithyat 0:977e87915078 124 timer.start();
vithyat 0:977e87915078 125
vithyat 0:977e87915078 126 /* loop until all expected bytes have been received */
vithyat 0:977e87915078 127 while (received_bytes < data_length) {
vithyat 0:977e87915078 128 /* wait for async event */
vithyat 0:977e87915078 129 while(!event_fired[thread_id]) {
vithyat 0:977e87915078 130 if (thread_id > 0) {
vithyat 0:977e87915078 131 ThisThread::yield();
vithyat 0:977e87915078 132 }
vithyat 0:977e87915078 133 }
vithyat 0:977e87915078 134 event_fired[thread_id] = false;
vithyat 0:977e87915078 135
vithyat 0:977e87915078 136 /* loop until all data has been read from socket */
vithyat 0:977e87915078 137 do {
vithyat 0:977e87915078 138 result = tcpsocket.recv(receive_buffer, buff_size);
vithyat 0:977e87915078 139 TEST_ASSERT_MESSAGE((result == NSAPI_ERROR_WOULD_BLOCK) || (result >= 0),
vithyat 0:977e87915078 140 "failed to read socket");
vithyat 0:977e87915078 141
vithyat 0:977e87915078 142 if (result > 0) {
vithyat 0:977e87915078 143 /* skip HTTP header */
vithyat 0:977e87915078 144 if (body_index < 0) {
vithyat 0:977e87915078 145 /* note that there are no required Response headers and their length may greatly vary */
vithyat 0:977e87915078 146 std::string header(receive_buffer, result);
vithyat 0:977e87915078 147 body_index = header.find("\r\n\r\n");
vithyat 0:977e87915078 148 if (body_index < 0) {
vithyat 0:977e87915078 149 continue;
vithyat 0:977e87915078 150 } else {
vithyat 0:977e87915078 151 printf("[NET-%d] Found body index: %d\r\n", thread_id, body_index);
vithyat 0:977e87915078 152
vithyat 0:977e87915078 153 /* remove header before comparison */
vithyat 0:977e87915078 154 memmove(receive_buffer, &receive_buffer[body_index + 4], result - body_index - 4);
vithyat 0:977e87915078 155
vithyat 0:977e87915078 156 TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(data, receive_buffer, result - body_index - 4,
vithyat 0:977e87915078 157 "character mismatch in header");
vithyat 0:977e87915078 158
vithyat 0:977e87915078 159 received_bytes += (result - body_index - 4);
vithyat 0:977e87915078 160 }
vithyat 0:977e87915078 161 } else {
vithyat 0:977e87915078 162 TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(&data[received_bytes], receive_buffer, result,
vithyat 0:977e87915078 163 "character mismatch in body");
vithyat 0:977e87915078 164
vithyat 0:977e87915078 165 received_bytes += result;
vithyat 0:977e87915078 166 }
vithyat 0:977e87915078 167
vithyat 0:977e87915078 168 speed = float(received_bytes) / timer.read();
vithyat 0:977e87915078 169 percent = float(received_bytes) * 100 / float(data_length);
vithyat 0:977e87915078 170 time_left = (data_length - received_bytes) / speed;
vithyat 0:977e87915078 171 printf("[NET-%d] Received bytes: %u, (%.2f%%, %.2fKB/s, ETA: %02d:%02d:%02d)\r\n",
vithyat 0:977e87915078 172 thread_id, received_bytes, percent, speed / 1024,
vithyat 0:977e87915078 173 time_left / 3600, (time_left / 60) % 60, time_left % 60);
vithyat 0:977e87915078 174 }
vithyat 0:977e87915078 175 }
vithyat 0:977e87915078 176 while ((result > 0) && (received_bytes < data_length));
vithyat 0:977e87915078 177 }
vithyat 0:977e87915078 178
vithyat 0:977e87915078 179 TEST_ASSERT_MESSAGE(body_index >= 0, "failed to find body");
vithyat 0:977e87915078 180
vithyat 0:977e87915078 181 timer.stop();
vithyat 0:977e87915078 182 float f_received_bytes = float(received_bytes);
vithyat 0:977e87915078 183 printf("[NET-%d] Downloaded: %.2fKB (%.2fKB/s, %.2f secs)\r\n", thread_id,
vithyat 0:977e87915078 184 f_received_bytes / 1024.,
vithyat 0:977e87915078 185 f_received_bytes / (timer.read() * 1024.),
vithyat 0:977e87915078 186 timer.read());
vithyat 0:977e87915078 187
vithyat 0:977e87915078 188 return received_bytes;
vithyat 0:977e87915078 189 }
vithyat 0:977e87915078 190