<commit_before>
#include "gcs.h"

#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>

#include <smavlink/channel.h>
#include <smavlink/system.h>
#include <smavlink/messages.h>
#include <smavlink/send.h>

#include <AP_HAL.h>
extern const AP_HAL::HAL& hal;

static xTaskHandle gcs_task_handle;
static xSemaphoreHandle gcs_mutex;
static struct gcs_state gcs_shared_state;

static smavlink_out_channel smav_ch;
static smavlink_system smav_sys;

/* Control thread, shares state */
static void gcs_task(void* args);
static void gcs_get(struct gcs_state *state);

/* Manage smavlink objects */
static void gcs_setup();
static void gcs_send_heartbeat(const struct gcs_state *state);

/* smavlink write delegation */
static size_t gcs_write(void* delegate, const uint8_t *data, size_t len);
static bool   gcs_begin_atomic(void*, size_t);
static void   gcs_end_atomic(void*);

void gcs_init(void) {
    gcs_mutex = xSemaphoreCreateMutex();
    xTaskCreate(gcs_task, (signed char *)"gcs", 1024, NULL, 0,
                &gcs_task_handle);
}

static void gcs_task(void* args) {

    struct gcs_state state = {0};

    gcs_setup();

    portTickType last_wake_time = xTaskGetTickCount();

    for(;;) {
        vTaskDelayUntil(&last_wake_time, 2000);
        gcs_get(&state);
        gcs_send_heartbeat(&state);
    }
}

/* gcs_get: for internal thread to grab the shared state */
static void gcs_get(struct gcs_state *state) {
    if (xSemaphoreTake(gcs_mutex, 1)) {
        memcpy(state, &gcs_shared_state,
                sizeof(struct gcs_state));
        xSemaphoreGive(gcs_mutex);
    } else {
        hal.scheduler->panic("PANIC: gcs_get took too long to grab "
                "memory barrier (should never happen)");
    }
}

/* gcs_set: for external threads to update the shared state */
void gcs_set(const struct gcs_state *out) {
    if (xSemaphoreTake(gcs_mutex, 1)) {<commit_msg>stabilize gcs: reduce heartbeat period to 1s<commit_after>
#include "gcs.h"

#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>

#include <smavlink/channel.h>
#include <smavlink/system.h>
#include <smavlink/messages.h>
#include <smavlink/send.h>

#include <AP_HAL.h>
extern const AP_HAL::HAL& hal;

static xTaskHandle gcs_task_handle;
static xSemaphoreHandle gcs_mutex;
static struct gcs_state gcs_shared_state;

static smavlink_out_channel smav_ch;
static smavlink_system smav_sys;

/* Control thread, shares state */
static void gcs_task(void* args);
static void gcs_get(struct gcs_state *state);

/* Manage smavlink objects */
static void gcs_setup();
static void gcs_send_heartbeat(const struct gcs_state *state);

/* smavlink write delegation */
static size_t gcs_write(void* delegate, const uint8_t *data, size_t len);
static bool   gcs_begin_atomic(void*, size_t);
static void   gcs_end_atomic(void*);

void gcs_init(void) {
    gcs_mutex = xSemaphoreCreateMutex();
    xTaskCreate(gcs_task, (signed char *)"gcs", 1024, NULL, 0,
                &gcs_task_handle);
}

static void gcs_task(void* args) {

    struct gcs_state state = {0};

    gcs_setup();

    portTickType last_wake_time = xTaskGetTickCount();

    for(;;) {
        vTaskDelayUntil(&last_wake_time, 1000);
        gcs_get(&state);
        gcs_send_heartbeat(&state);
    }
}

/* gcs_get: for internal thread to grab the shared state */
static void gcs_get(struct gcs_state *state) {
    if (xSemaphoreTake(gcs_mutex, 1)) {
        memcpy(state, &gcs_shared_state,
                sizeof(struct gcs_state));
        xSemaphoreGive(gcs_mutex);
    } else {
        hal.scheduler->panic("PANIC: gcs_get took too long to grab "
                "memory barrier (should never happen)");
    }
}

/* gcs_set: for external threads to update the shared state */
void gcs_set(const struct gcs_state *out) {
    if (xSemaphoreTake(gcs_mutex, 1)) {
{'fraction_non_alphanumeric': 0.11105276975584143, 'fraction_numerical': 0.007088474665266474, 'mean_word_length': 4.019762845849803, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3286458', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1357, 'n_words': 334}