<commit_before>
#include "../Commands/commands.h"
#include "ESP8266.h"

////////////////////////////////////////////////////////////////////////////////
// Buffers and global variables
////////////////////////////////////////////////////////////////////////////////

static char formattedString[64];

WifiServiceData DefaultWifiServiceData = {
    DefaultWifiWriteString
};

////////////////////////////////////////////////////////////////////////////////
// Wifi private api
////////////////////////////////////////////////////////////////////////////////

extern int sprintf(char *, const char *, ...);
extern int sscanf(const char *, const char *, ...);
extern char * strcpy(char *, const char *);

static char * strstr(const char * searchee, const char * lookfor)
{
    if (*searchee == 0)
    {
        if (*lookfor)
            return (char *) NULL;
        return (char *) searchee;
    }

    while (*searchee)
    {
        int i;
        i = 0;

        while (1)
        {
            if (lookfor[i] == 0)
            {
                return (char *) searchee;
            }

            if (lookfor[i] != searchee[i])
            {
                break;
            }
            i++;
        }
        searchee++;
    }

    return (char *) NULL;
}

static WifiServiceData* GetWifiServiceData(Service* service)
{
    return (WifiServiceData*)service->Data;
}

static void EmptyBuffer(WifiServiceData* data)
{
    data->WifiBufferCounter = 0;
    data->WifiBuffer[0] = '\0';
}

////////////////////////////////////////////////////////////////////////////////
// Wifi public api
////////////////////////////////////////////////////////////////////////////////

void PutcToWifiReceivedBuffer(const char rchar, Service* service)
{
    WifiServiceData* data = GetWifiServiceData(service);
    if (rchar == RETURN_ASCII && data->WifiBufferCounter < (RECEIVE_BUFFER_SIZE - 3))
    {
        data->WifiBuffer[data->WifiBufferCounter] = '\r';
        ++data->WifiBufferCounter;
        data->WifiBuffer[data->WifiBufferCounter] = '\n';
        ++data->WifiBufferCounter;
    }

    if (rchar > 31 && data->WifiBufferCounter < (RECEIVE_BUFFER_SIZE - 2) )
    {
        data->WifiBuffer[data->WifiBufferCounter] = rchar;
        ++data->WifiBufferCounter;
    }

    data->WifiBuffer[data->WifiBufferCounter] = '\0';
}

void SendRequest(TcpRequest * tcpRequest, Service* service)
{
    WifiServiceData* data = GetWifiServiceData(service);
    unsigned short i;

    for(i = 0; i < 5; ++i)
    {
        if (data->ActiveRequests[i] == NULL)
        {
            data->ActiveRequests[i] = tcpRequest;
            return;
        }
    }
}

void ConnectToAccessPoint(AccessPointConnection* accessPointConnection, Service* service)
{
    WifiServiceData* data = GetWifiServiceData(service);
    if (data->AccessPointConnection == NULL)
    {
        data->AccessPointConnection = accessPointConnection;
    }
}

void DisconnectAccessPoint(Service* service)
{
    WifiServiceData* data = GetWifiServiceData(service);
    if (data->AccessPointConnection != NULL)
    {
        data->AccessPointConnection = NULL;
        service->State = 0x04;
    }
}

void ResetWifiModule(Service* service)
{
    service->State = Starting;
}

////////////////////////////////////////////////////////////////////////////////
// Wifi service implementation
////////////////////////////////////////////////////////////////////////////////

byte ServiceWifiImplementation(byte state, void* data, struct CommandEngine* commandEngine)
{
    WifiServiceData* wifiServiceData = (WifiServiceData*)data;

    if (wifiServiceData->WifiWriteString == NULL)
    {
        return Stopped;
    }

    unsigned short i;

    switch(state)
    {
        case Starting:
            EmptyBuffer(wifiServiceData);
            // Turn on, make a reset<commit_msg>When soft restarting, empty the buffer<commit_after>
#include "../Commands/commands.h"
#include "ESP8266.h"

////////////////////////////////////////////////////////////////////////////////
// Buffers and global variables
////////////////////////////////////////////////////////////////////////////////

static char formattedString[64];

WifiServiceData DefaultWifiServiceData = {
    DefaultWifiWriteString
};

////////////////////////////////////////////////////////////////////////////////
// Wifi private api
////////////////////////////////////////////////////////////////////////////////

extern int sprintf(char *, const char *, ...);
extern int sscanf(const char *, const char *, ...);
extern char * strcpy(char *, const char *);

static char * strstr(const char * searchee, const char * lookfor)
{
    if (*searchee == 0)
    {
        if (*lookfor)
            return (char *) NULL;
        return (char *) searchee;
    }

    while (*searchee)
    {
        int i;
        i = 0;

        while (1)
        {
            if (lookfor[i] == 0)
            {
                return (char *) searchee;
            }

            if (lookfor[i] != searchee[i])
            {
                break;
            }
            i++;
        }
        searchee++;
    }

    return (char *) NULL;
}

static WifiServiceData* GetWifiServiceData(Service* service)
{
    return (WifiServiceData*)service->Data;
}

static void EmptyBuffer(WifiServiceData* data)
{
    data->WifiBufferCounter = 0;
    data->WifiBuffer[0] = '\0';
}

////////////////////////////////////////////////////////////////////////////////
// Wifi public api
////////////////////////////////////////////////////////////////////////////////

void PutcToWifiReceivedBuffer(const char rchar, Service* service)
{
    WifiServiceData* data = GetWifiServiceData(service);
    if (rchar == RETURN_ASCII && data->WifiBufferCounter < (RECEIVE_BUFFER_SIZE - 3))
    {
        data->WifiBuffer[data->WifiBufferCounter] = '\r';
        ++data->WifiBufferCounter;
        data->WifiBuffer[data->WifiBufferCounter] = '\n';
        ++data->WifiBufferCounter;
    }

    if (rchar > 31 && data->WifiBufferCounter < (RECEIVE_BUFFER_SIZE - 2) )
    {
        data->WifiBuffer[data->WifiBufferCounter] = rchar;
        ++data->WifiBufferCounter;
    }

    data->WifiBuffer[data->WifiBufferCounter] = '\0';
}

void SendRequest(TcpRequest * tcpRequest, Service* service)
{
    WifiServiceData* data = GetWifiServiceData(service);
    unsigned short i;

    for(i = 0; i < 5; ++i)
    {
        if (data->ActiveRequests[i] == NULL)
        {
            data->ActiveRequests[i] = tcpRequest;
            return;
        }
    }
}

void ConnectToAccessPoint(AccessPointConnection* accessPointConnection, Service* service)
{
    WifiServiceData* data = GetWifiServiceData(service);
    if (data->AccessPointConnection == NULL)
    {
        data->AccessPointConnection = accessPointConnection;
    }
}

void DisconnectAccessPoint(Service* service)
{
    WifiServiceData* data = GetWifiServiceData(service);
    if (data->AccessPointConnection != NULL)
    {
        data->AccessPointConnection = NULL;
        service->State = 0x04;
    }
}

void ResetWifiModule(Service* service)
{
    service->State = Starting;
    EmptyBuffer(GetWifiServiceData(service->Data));
}

////////////////////////////////////////////////////////////////////////////////
// Wifi service implementation
////////////////////////////////////////////////////////////////////////////////

byte ServiceWifiImplementation(byte state, void* data, struct CommandEngine* commandEngine)
{
    WifiServiceData* wifiServiceData = (WifiServiceData*)data;

    if (wifiServiceData->WifiWriteString == NULL)
    {
        return Stopped;
    }

    unsigned short i;

    switch(state)
    {
        case Starting:
            EmptyBuffer(wifiServiceData);
            // Turn on, make a reset
{'fraction_non_alphanumeric': 0.2660111269245698, 'fraction_numerical': 0.005951610816405745, 'mean_word_length': 1.4855305466237942, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 56, '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': '5334117', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2024, 'n_words': 423}