<commit_before>#include "ets_sys.h"
#include "osapi.h"
#include <c_types.h>
#include <string.h>
#include "coap.h"
#include "ws2812.h"
#include <stdio.h>

static char light = '1';
static uint8_t buffer[24];

const uint16_t rsplen = 1500;
static char rsp[1500] = "";
void build_rsp(void);

void endpoint_setup(void)
{
    build_rsp();
}

static const coap_endpoint_path_t path_well_known_core = {2, {".well-known", "core"}};
static int handle_get_well_known_core(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
    return coap_make_response(scratch, outpkt, (const uint8_t *)rsp, strlen(rsp), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT);
}

static const coap_endpoint_path_t path_light = {1, {"light"}};
static int handle_get_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
    return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
}

static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
    int c = 0;
    memset(&buffer[0], 0, sizeof(buffer));
    if (inpkt->payload.len == 0)
        return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);

//    os_printf("LEDS payload len: %d\n", inpkt->payload.len);
    for (c = 0; c < inpkt->payload.len; c++) {
        buffer[c] = (uint8_t)inpkt->payload.p[c];
//        os_printf(" %2x", buffer[c]);
    }
//    os_printf("\n");

    WS2812OutBuffer( buffer, inpkt->payload.len );
    return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
}

const coap_endpoint_t endpoints[] =
{
    {COAP_METHOD_GET, handle_get_well_known_core, &path_well_known_core, "ct=40"},
    {COAP_METHOD_GET, handle_get_light, &path_light, "ct=0"},
    {COAP_METHOD_PUT, handle_put_light, &path_light, NULL},
    {(coap_method_t)0, NULL, NULL, NULL}
};

void ICACHE_FLASH_ATTR build_rsp(void)
{
    uint16_t len = rsplen;
    const coap_endpoint_t *ep = endpoints;
    int i;

    len--; // Null-terminated string

    while(NULL != ep->handler)
    {<commit_msg>Updated to add some comments and constants.<commit_after>#include "ets_sys.h"
#include "osapi.h"
#include <c_types.h>
#include <string.h>
#include "coap.h"
#include "ws2812.h"
#include <stdio.h>

// we are controlling n leds with 3 colors
#define LED_BUFFER_SIZE 24

static char light = '1';
static uint8_t buffer[LED_BUFFER_SIZE];

const uint16_t rsplen = 1500;
static char rsp[1500] = "";
void build_rsp(void);

void endpoint_setup(void)
{
    build_rsp();
}

static const coap_endpoint_path_t path_well_known_core = {2, {".well-known", "core"}};
static int handle_get_well_known_core(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
    return coap_make_response(scratch, outpkt, (const uint8_t *)rsp, strlen(rsp), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT);
}

static const coap_endpoint_path_t path_light = {1, {"light"}};
static int handle_get_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
    return coap_make_response(scratch, outpkt, (const uint8_t *)&buffer[0], LED_BUFFER_SIZE, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_OCTET_STREAM);
}

static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
    int c = 0;

    // clear the buffer between writes
    memset(&buffer[0], 0, sizeof(buffer));

    // as we require 24 bytes of information to update the lights
    if (inpkt->payload.len != LED_BUFFER_SIZE)
        return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_OCTET_STREAM);

    // convert the payload to the types required in the WS2812 code
    // TODO: may be better to make these match
    for (c = 0; c < inpkt->payload.len; c++) {
        buffer[c] = (uint8_t)inpkt->payload.p[c];
    }

    WS2812OutBuffer( buffer, inpkt->payload.len );
    return coap_make_response(scratch, outpkt, (const uint8_t *)&buffer[0], LED_BUFFER_SIZE, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_OCTET_STREAM);
}

const coap_endpoint_t endpoints[] =
{
    {COAP_METHOD_GET, handle_get_well_known_core, &path_well_known_core, "ct=40"},
    {COAP_METHOD_GET, handle_get_light, &path_light, "ct=0"},
    {COAP_METHOD_PUT, handle_put_light, &path_light, NULL},
    {(coap_method_t)0, NULL, NULL, NULL}
};

void ICACHE_FLASH_ATTR build_rsp(void)
{
    uint16_t len = rsplen;
    const coap_endpoint_t *ep = endpoints;
    int i;

    len--; // Null-terminated string

    while(NULL != ep->handler)
    {
{'fraction_non_alphanumeric': 0.1249018067556952, 'fraction_numerical': 0.020227808326787118, 'mean_word_length': 5.158403869407497, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, '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': '7510781', 'n_tokens_mistral': 2069, 'n_tokens_neox': 2041, 'n_words': 435}