<commit_before>#include "application.h"
#include "w5100.h"
#include "socket.h"

#define yield()

static uint16_t local_port;

/**
 * @brief	This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it.
 * @return 	1 for success else 0.
 */
uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
{
  if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE))
  {
    close(s);
    // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
    W5100.writeSnMR(s, protocol | flag);
    if (port != 0) {
      W5100.writeSnPORT(s, port);
    }
    else {
      local_port++; // if don't set the source port, set local_port number.
      W5100.writeSnPORT(s, local_port);
    }

    W5100.execCmdSn(s, Sock_OPEN);
    // SPI.endTransaction();
    return 1;
  }

  return 0;
}


uint8_t socketStatus(SOCKET s)
{
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  uint8_t status = W5100.readSnSR(s);
  // SPI.endTransaction();
  return status;
}


/**
 * @brief	This function close the socket and parameter is "s" which represent the socket number
 */
void close(SOCKET s)
{
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  W5100.execCmdSn(s, Sock_CLOSE);
  W5100.writeSnIR(s, 0xFF);
  // SPI.endTransaction();
}


/**
 * @brief	This function established  the connection for the channel in passive (server) mode. This function waits for the request from the peer.
 * @return	1 for success else 0.
 */
uint8_t listen(SOCKET s)
{
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  if (W5100.readSnSR(s) != SnSR::INIT) {
    // SPI.endTransaction();
    return 0;
  }
  W5100.execCmdSn(s, Sock_LISTEN);
  // SPI.endTransaction();
  return 1;
}


/**
 * @brief	This function established  the connection for the channel in Active (client) mode.
 * 		This function waits for the untill the connection is established.
 *
 * @return	1 for success else 0.
 */
uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
{
  if
    (
  ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
    ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
    (port == 0x00)
    )
    return 0;

  // set destination IP
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  W5100.writeSnDIPR(s, addr);
  W5100.writeSnDPORT(s, port);
  W5100.execCmdSn(s, Sock_CONNECT);
  // SPI.endTransaction();

  return 1;
}



/**
 * @brief	This function used for disconnect the socket and parameter is "s" which represent the socket number
 * @return	1 for success else 0.
 */
void disconnect(SOCKET s)
{
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);<commit_msg>Update socket.cpp<commit_after>#include "application.h"
#include "w5100.h"
#include "socket.h"

#define yield()

static uint16_t local_port;

/**
 * @brief	This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it.
 * @return 	1 for success else 0.
 */
uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
{
  if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE))
  {
    close(s);
    // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
    W5100.writeSnMR(s, protocol | flag);
    if (port != 0) {
      W5100.writeSnPORT(s, port);
    }
    else {
      local_port++; // if don't set the source port, set local_port number.
      W5100.writeSnPORT(s, local_port);
    }

    W5100.execCmdSn(s, Sock_OPEN);
    // SPI.endTransaction();
    return 1;
  }

  return 0;
}


uint8_t socketStatus(SOCKET s)
{
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  uint8_t status = W5100.readSnSR(s);
  // SPI.endTransaction();
  return status;
}


/**
 * @brief	This function close the socket and parameter is "s" which represent the socket number
 */
void close(SOCKET s)
{
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  W5100.execCmdSn(s, Sock_CLOSE);
  W5100.writeSnIR(s, 0xFF);
  // SPI.endTransaction();
}


/**
 * @brief	This function established  the connection for the channel in passive (server) mode. This function waits for the request from the peer.
 * @return	1 for success else 0.
 */
uint8_t listen(SOCKET s)
{
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  if (W5100.readSnSR(s) != SnSR::INIT) {
    // SPI.endTransaction();
    return 0;
  }
  W5100.execCmdSn(s, Sock_LISTEN);
  // SPI.endTransaction();
  return 1;
}


/**
 * @brief	This function established  the connection for the channel in Active (client) mode.
 * 		This function waits for the untill the connection is established.
 *
 * @return	1 for success else 0.
 */
uint8_t connecttcp(SOCKET s, uint8_t * addr, uint16_t port)
{
  if
    (
  ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
    ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
    (port == 0x00)
    )
    return 0;

  // set destination IP
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  W5100.writeSnDIPR(s, addr);
  W5100.writeSnDPORT(s, port);
  W5100.execCmdSn(s, Sock_CONNECT);
  // SPI.endTransaction();

  return 1;
}



/**
 * @brief	This function used for disconnect the socket and parameter is "s" which represent the socket number
 * @return	1 for success else 0.
 */
void disconnecttcp(SOCKET s)
{
  // SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
{'fraction_non_alphanumeric': 0.14413591523565947, 'fraction_numerical': 0.041286079649251003, 'mean_word_length': 3.5285359801488836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '765620', 'n_tokens_mistral': 2122, 'n_tokens_neox': 1991, 'n_words': 522}