Skip to content

Commit

Permalink
Merge pull request #94 from marcomicera/watchdog
Browse files Browse the repository at this point in the history
Watchdog
  • Loading branch information
marcomicera committed Jan 13, 2020
2 parents f07eba3 + 1b5513a commit 223eff3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
1 change: 1 addition & 0 deletions board/followifier/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ idf_component_register(SRCS "followifier.c"
"util/misc.c"
"util/conf.h"
"components/sync.c"
"components/watchdog.c"
INCLUDE_DIRS ".")
25 changes: 18 additions & 7 deletions board/followifier/main/components/flusher.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <sys/socket.h>
#include <tcpip_adapter.h>
#include <esp_log.h>
#include <esp_wifi.h>
#include "gen/message.pb-c.h"
#include "flusher.h"
#include "util/misc.h"
Expand Down Expand Up @@ -123,20 +122,32 @@ void flush(void) {
"setsockopt failed\n",
reactivate_sniffer);

struct linger sl;
sl.l_onoff = 1; /* non-zero value enables linger option in kernel */
sl.l_linger = 3; /* timeout interval in seconds */
ESP_ERROR_CHECK_JUMP_LABEL (
setsockopt(tcp_socket, SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)) >= 0,
"setsockopt failed\n",
reactivate_sniffer);

ESP_LOGI(BOARD_TAG, "Socket created, connecting to %s:%d...", SERVER_ADDRESS, SERVER_PORT);

// Creating connection to the server
ESP_ERROR_CHECK_JUMP_LABEL(!connect(tcp_socket, (struct sockaddr *) &server_address, sizeof(server_address)),
"Error while connecting to the server: discarding local packets, re-enabling sniffing mode...",
closing_socket);

// Flushing the message buffer
ESP_LOGI(BOARD_TAG, "Sending batch");
ESP_ERROR_CHECK_JUMP_LABEL(send(tcp_socket, (char *) buffer, batch_length + sizeof("\n\r\n\r"), 0) >= 0,
"Error while sending batch: discarding local packets, re-enabling sniffing mode...",
closing_socket);
ESP_LOGI(BOARD_TAG, "Sending delimiter...");

// Flushing the message buffer in slices of equal length.
u_int32_t total_len = batch_length + sizeof(DELIMITER);

ESP_LOGI(BOARD_TAG, "Sending batch of %d bytes.", total_len);
int32_t sent_bytes = send(tcp_socket, (char *) buffer, total_len, 0);
ESP_ERROR_CHECK_JUMP_LABEL(sent_bytes >= 0,
"Error while sending batch: discarding local packets, re-enabling sniffing mode...",
closing_socket);
ESP_LOGI(BOARD_TAG, "Waiting 2 seconds before closing...");
vTaskDelay(2 * 10); // in deci-seconds (0.1 seconds)
// Skipping until here in case connection towards the server was unsuccessful
closing_socket:

Expand Down
3 changes: 2 additions & 1 deletion board/followifier/main/components/flusher.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
*/
// FIXME Implement timer + messages threshold flushing conditions (#43)
#define FLUSH_THRESHOLD 3 // number of messages to be saved before flushing

#define FLUSH_MODULUS 1440
#define DELIMITER "\n\r\n\r"
/**
* Stores a message and eventually flushes all stored ones.
*/
Expand Down
4 changes: 4 additions & 0 deletions board/followifier/main/components/sniffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pthread_t measurement_timer_thread_id;

#endif

uint64_t RECEIVED_WIFI_PACKETS;

typedef struct {
unsigned frame_ctrl:16;
unsigned duration_id:16;
Expand Down Expand Up @@ -198,6 +200,8 @@ void hexDump(char *desc, void *addr, int len) {
*/
void sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type) {

++RECEIVED_WIFI_PACKETS;

// Not necessary after calling `esp_wifi_set_promiscuous_filter()`
if (type != WIFI_PKT_MGMT)
return;
Expand Down
2 changes: 2 additions & 0 deletions board/followifier/main/components/sniffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ extern signed max_rrsi_in_measure_period;

extern const char *SNIFFER_TAG;

extern uint64_t RECEIVED_WIFI_PACKETS;

/**
* @brief Supported Sniffer Interface
*
Expand Down
57 changes: 57 additions & 0 deletions board/followifier/main/components/watchdog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdlib.h>
#include <tcpip_adapter.h>
#include <esp_log.h>
#include "util/misc.h"
#include "sniffer.h"

char* WATCHDOG_TAG = "watchdog";

void watchdog(void *pvParameters) {

// Waiting for the whole system to initialize
const signed short initial_seconds_sleep_time = 30;
vTaskDelay((initial_seconds_sleep_time * 1000) / portTICK_PERIOD_MS);

const signed short seconds_sleep_time = 30;

ESP_LOGI(WATCHDOG_TAG, "Watchdog started.");

for (;;) {

uint64_t TMP_RECEIVED_WIFI_PACKETS = RECEIVED_WIFI_PACKETS;
ESP_LOGI(WATCHDOG_TAG, "Watchdog received %llu Wi-Fi packets. Going to sleep...",
TMP_RECEIVED_WIFI_PACKETS);

vTaskDelay((seconds_sleep_time * 1000) / portTICK_PERIOD_MS);

// Main thread is dead
if (TMP_RECEIVED_WIFI_PACKETS == RECEIVED_WIFI_PACKETS) {
ESP_LOGE(WATCHDOG_TAG, "Main thread is dead: from %llu packets to %llu received in %u seconds.",
TMP_RECEIVED_WIFI_PACKETS, RECEIVED_WIFI_PACKETS, seconds_sleep_time);
ESP_LOGE(WATCHDOG_TAG, "Restarting main thread...");
esp_restart();
} else {
ESP_LOGI(WATCHDOG_TAG, "Main thread is not dead: from %llu packets to %llu received in %u seconds.",
TMP_RECEIVED_WIFI_PACKETS, RECEIVED_WIFI_PACKETS, seconds_sleep_time);
// RECEIVED_WIFI_PACKETS = 0;
}
}
}

void start_watchdog(void) {

TaskHandle_t xHandle = NULL;

// Create the task, storing the handle. Note that the passed parameter ucParameterToPass
// must exist for the lifetime of the task, so in this case is declared static. If it was just an
// an automatic stack variable it might no longer exist, or at least have been corrupted, by the time
// the new task attempts to access it.
xTaskCreate(watchdog, "watchdog", CONFIG_SNIFFER_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle);
ESP_LOGI(BOARD_TAG, "Watchdog's task has been created.");
configASSERT(xHandle);

// // Use the handle to delete the task.
// if (xHandle != NULL) {
// vTaskDelete(xHandle);
// }
}
4 changes: 4 additions & 0 deletions board/followifier/main/followifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "components/sync.h"
#include "util/misc.h"
#include "util/conf.h"
#include "components/watchdog.c"

/**
* Board event handler.
Expand Down Expand Up @@ -111,6 +112,9 @@ void app_main(void) {
// Initialize board
init_all();

// Start watchdog
start_watchdog();

// Turn the Wi-Fi on
ESP_ERROR_CHECK(start_wifi());
}

0 comments on commit 223eff3

Please sign in to comment.