Skip to content

Commit

Permalink
Merge pull request #97 from es-ude/fix_http_lib
Browse files Browse the repository at this point in the history
Fix HTTP GET
  • Loading branch information
DavidFederl authored Aug 1, 2023
2 parents 74ee4a1 + 8d58e0e commit 4e47d55
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 107 deletions.
5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions bitfile_scripts/webserver_dummy.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
from io import BytesIO

from flask import Flask, send_file
from flask import Flask, send_file, make_response

app = Flask(__name__)


def read_slice(position: int, filename: str) -> bytes:
with open(filename, "rb") as file:
file.seek(position *1024)
file.seek(position * 1024)
chunk: bytes = file.read(1024)
return chunk


@app.route('/getfile/<position>')
def get_file(position: str):
@app.route('/getfast/<position>')
def get_file_fast(position: str):
"""
Using positional arguments as parameter did not work!
"""
buffer = BytesIO()
buffer.write(read_slice(int(position), "bitfile_scripts/bitfiles/env5_bitfiles/id_0x11/env5_top_reconfig.bin"))
buffer.write(read_slice(int(position), "bitfile_scripts/bitfiles/env5_bitfiles/blink_fast/led_test.bin"))
buffer.seek(0)
return send_file(
buffer,
as_attachment=True,
download_name='bitfile.bin',
mimetype='application/octet-stream')


@app.route('/getslow/<position>')
def get_file_slow(position: str):
"""
Using positional arguments as parameter did not work!
"""
buffer = BytesIO()
buffer.write(read_slice(int(position), "bitfile_scripts/bitfiles/env5_bitfiles/blink_slow/led_test.bin"))
buffer.seek(0)
return send_file(
buffer,
Expand All @@ -29,7 +44,7 @@ def get_file(position: str):

@app.route('/check')
def check_server_available():
return "AVAILABLE"
return "AVAILABLE\0"


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions src/esp/Esp.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ espErrorCode_t espSendCommand(char *cmd, char *expectedResponse, int timeoutMs)
PRINT_DEBUG("Correct response received!")
break;
}
PRINT_DEBUG("No Response Received")
freeRtosTaskWrapperTaskSleep(REFRESH_RESPOND_IN_MS);
}
// free command buffer of UART
Expand Down
15 changes: 8 additions & 7 deletions src/http/HTTP.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ void HTTPReceive(char *httpResponse) {
HTTPStatus HTTPGet(const char *url, HttpResponse_t **data) {
if (espStatus.ChipStatus == ESP_CHIP_NOT_OK || espStatus.WIFIStatus == NOT_CONNECTED) {
PRINT_DEBUG("HTTP ERROR - No connection")
return HTTP_CONNECTION_FAILED;
return HTTP_HARDWARE_NOT_READY;
}

if (strlen(url) > 256) {
PRINT_DEBUG("HTTP ERROR - URL to long")
return HTTP_CONNECTION_FAILED;
return HTTP_URL_TO_LONG;
}

size_t lengthOfString = AT_HTTP_GET_LENGTH + strlen(url);
Expand All @@ -40,8 +40,9 @@ HTTPStatus HTTPGet(const char *url, HttpResponse_t **data) {

if (espSendCommand(httpGet, AT_HTTP_GET_RESPONSE, 10000) == ESP_WRONG_ANSWER_RECEIVED) {
if (HTTPResponse != NULL) {
HTTPCleanResponseBuffer(HTTPResponse);
HTTPCleanResponseBuffer(&HTTPResponse);
}
free(httpGet);
return HTTP_CONNECTION_FAILED;
}

Expand All @@ -51,10 +52,10 @@ HTTPStatus HTTPGet(const char *url, HttpResponse_t **data) {
return HTTP_SUCCESS;
}

void HTTPCleanResponseBuffer(HttpResponse_t *response) {
free(response->response);
free(response);
response = NULL;
void HTTPCleanResponseBuffer(HttpResponse_t **response) {
free((*response)->response);
free(*response);
*response = NULL;
}

void HTTPSetReceiverFunction(void) {
Expand Down
9 changes: 7 additions & 2 deletions src/http/HTTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

#include <stdint.h>

typedef enum HTTPStatus { HTTP_SUCCESS = 1, HTTP_CONNECTION_FAILED = 2 } HTTPStatus;
typedef enum HTTPStatus {
HTTP_SUCCESS = 0x00,
HTTP_HARDWARE_NOT_READY,
HTTP_URL_TO_LONG,
HTTP_CONNECTION_FAILED,
} HTTPStatus;

struct httpResponse {
uint32_t length;
Expand All @@ -28,6 +33,6 @@ void HTTPSetReceiverFunction(void);
*
* @param response buffer to be removed
*/
void HTTPCleanResponseBuffer(HttpResponse_t *response);
void HTTPCleanResponseBuffer(HttpResponse_t **response);

#endif // ENV5_HTTP_H
14 changes: 10 additions & 4 deletions src/network/Network.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,24 @@ void networkDisconnectFromNetwork(void) {
}
}

void networkcheckConnection(void) {
networkErrorCode_t networkcheckConnection(void) {
if (espStatus.ChipStatus == ESP_CHIP_NOT_OK) {
PRINT("Chip not working!")
return;
return NETWORK_ESP_CHIP_FAILED;
}
if (espStatus.WIFIStatus == NOT_CONNECTED) {
PRINT("No connection to disconnect from!")
return;
return NETWORK_ESTABLISH_CONNECTION_FAILED;
}

char *checkConnection = malloc(AT_CHECK_CONNECTION_LENGTH);
strcpy(checkConnection, AT_CHECK_CONNECTION);
espSendCommand(checkConnection, AT_CHECK_CONNCETION_RESPONSE, 5000);
espErrorCode_t espErrorCode =
espSendCommand(checkConnection, AT_CHECK_CONNCETION_RESPONSE, 5000);
free(checkConnection);

if (espErrorCode == ESP_NO_ERROR) {
return NETWORK_NO_ERROR;
}
return NETWORK_ESTABLISH_CONNECTION_FAILED;
}
2 changes: 1 addition & 1 deletion src/network/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ networkErrorCode_t networkConnectToNetwork(networkCredentials_t credentials);

void networkDisconnectFromNetwork(void);

void networkcheckConnection(void);
networkErrorCode_t networkcheckConnection(void);

#endif /* ENV5_NETWORK_HEADER */
6 changes: 6 additions & 0 deletions src/time/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_library(time_interface INTERFACE)
target_sources(time_interface INTERFACE ${CMAKE_CURRENT_LIST_DIR}/Time.c)
target_include_directories(time_interface INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
target_link_libraries(time_interface INTERFACE
pico_time
pico_stdlib)
6 changes: 5 additions & 1 deletion src/uart/Uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ uartErrorCode_t uartSendCommand(char *command, char *expectedResponse) {
}

// send command over uart
PRINT_DEBUG("COMMAND: \"%s\"", command)
PRINT_DEBUG("Sending command: \"%s\"", command)
uart_puts((uart_inst_t *)uartDevice->uartInstance, command);
// send \r\n because AT command always ends with CR-LF
uart_puts((uart_inst_t *)uartDevice->uartInstance, "\r\n");
PRINT_DEBUG("command send")

return UART_NO_ERROR;
}
Expand Down Expand Up @@ -139,6 +140,9 @@ void uartInternalHandleNewLine(void) {
if (uartHTTPReceive != NULL) {
uartHTTPReceive(uartDevice->receiveBuffer);
}
} else if (strncmp("+CWJAP", uartDevice->receiveBuffer, 6) == 0) {
PRINT_DEBUG("Message: %s", uartDevice->receiveBuffer);
uartCorrectResponseReceived = true;
}
if (strncmp(uartExpectedResponseFromEsp, uartDevice->receiveBuffer,
strlen(uartExpectedResponseFromEsp)) == 0) {
Expand Down
9 changes: 9 additions & 0 deletions test/hardware/TestNetworking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@ target_link_libraries(hardware-test_Durability
hardware-testHelper)
target_compile_definitions(hardware-test_Durability PRIVATE DEBUG)
make_to_output_file(hardware-test_Durability)

################## test_networkStrength ##################
add_executable(hardware-test_checknetwork HardwaretestCheckNetwork.c)
target_link_libraries(hardware-test_checknetwork
pico_stdlib
freeRtosUtils
hardware-testHelper)
target_compile_definitions(hardware-test_checknetwork PRIVATE DEBUG)
make_to_output_file(hardware-test_checknetwork)
32 changes: 32 additions & 0 deletions test/hardware/TestNetworking/HardwaretestCheckNetwork.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define SOURCE_FILE "NETWORK-CHECK-TEST"

#include "Common.h"
#include "FreeRtosTaskWrapper.h"
#include "HardwaretestHelper.h"
#include "Network.h"

/*!
* Tries to connect to the network which is specified in NetworkSettings.h. When successful the
* connection is closed and the process repeats.
*/

extern networkCredentials_t credentials;

_Noreturn void networkTask() {
connectToNetwork();

PRINT("=== STARTING TEST ===")

while (1) {
networkcheckConnection();
freeRtosTaskWrapperTaskSleep(3000);
}
}

int main() {
initHardwareTest();

freeRtosTaskWrapperRegisterTask(enterBootModeTaskHardwareTest, "enterBootModeTask");
freeRtosTaskWrapperRegisterTask(networkTask, "networkTask");
freeRtosTaskWrapperStartScheduler();
}
23 changes: 13 additions & 10 deletions test/hardware/TestNetworking/HardwaretestHTTP.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
#include "HardwaretestHelper.h"

void _Noreturn httpTask(void) {
PRINT("=== STARTING TEST ===")

connectToNetwork();

HttpResponse_t *response;
uint8_t code = HTTPGet("http://192.168.203.51:5000/getfile/0", &response);
PRINT("=== STARTING TEST ===")

HttpResponse_t *response = NULL;

PRINT("HTTP Get returns with %u", code);
PRINT("Response Length: %li", response->length);
PRINT("Response: %s", response->response)
while (1) {
uint8_t code = HTTPGet("http://192.168.178.24:5000/check", &response);

HTTPCleanResponseBuffer(response);
PRINT("done")
PRINT("HTTP Get returns with %u", code);
if (code == HTTP_SUCCESS) {
PRINT("Response Length: %li", response->length);
PRINT("Response: %s", response->response)
HTTPCleanResponseBuffer(&response);
}

while (1) {}
freeRtosTaskWrapperTaskSleep(3000);
}
}

int main() {
Expand Down
11 changes: 6 additions & 5 deletions test/hardware/TestNetworking/HardwaretestHelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "MqttBroker.h"
#include "Network.h"
#include "NetworkConfiguration.h"

#include "hardware/watchdog.h"
#include "pico/bootrom.h"
#include "pico/stdlib.h"
Expand All @@ -24,20 +25,20 @@ void initHardwareTest(void) {
if (watchdog_enable_caused_reboot()) {
reset_usb_boot(0, 0);
}
// init usb, queue and watchdog
// init stdio and esp
stdio_init_all();
while ((!stdio_usb_connected())) {}
espInit();
freeRtosQueueWrapperCreate();
watchdog_enable(2000, 1);
}

void _Noreturn enterBootModeTaskHardwareTest(void) {
watchdog_enable(5000000, 1); // max timeout ~8.3s

while (1) {
if (getchar_timeout_us(10) == 'r' || !stdio_usb_connected()) {
if (getchar_timeout_us(0) == 'r' || !stdio_usb_connected()) {
reset_usb_boot(0, 0);
}
watchdog_update();
freeRtosTaskWrapperTaskSleep(1000);
freeRtosTaskWrapperTaskSleep(500);
}
}
Loading

0 comments on commit 4e47d55

Please sign in to comment.