1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-30 16:24:09 +03:00

Migrate from astyle to clang-format (#8464)

This commit is contained in:
Maxim Prokhorov
2022-02-20 19:23:33 +03:00
committed by Max Prokhorov
parent 46190b61f1
commit 19b7a29720
241 changed files with 15925 additions and 16197 deletions

View File

@ -39,168 +39,173 @@
#include <fcntl.h>
#include <errno.h>
int mockSockSetup (int sock)
int mockSockSetup(int sock)
{
if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
{
perror("socket fcntl(O_NONBLOCK)");
close(sock);
return -1;
}
if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
{
perror("socket fcntl(O_NONBLOCK)");
close(sock);
return -1;
}
#ifndef MSG_NOSIGNAL
int i = 1;
if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof i) == -1)
{
perror("sockopt(SO_NOSIGPIPE)(macOS)");
close(sock);
return -1;
}
int i = 1;
if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof i) == -1)
{
perror("sockopt(SO_NOSIGPIPE)(macOS)");
close(sock);
return -1;
}
#endif
return sock;
return sock;
}
int mockConnect (uint32_t ipv4, int& sock, int port)
int mockConnect(uint32_t ipv4, int& sock, int port)
{
struct sockaddr_in server;
if ((sock = ::socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror(MOCK "ClientContext:connect: ::socket()");
return 0;
}
server.sin_family = AF_INET;
server.sin_port = htons(port);
memcpy(&server.sin_addr, &ipv4, 4);
if (::connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1)
{
perror(MOCK "ClientContext::connect: ::connect()");
return 0;
}
struct sockaddr_in server;
if ((sock = ::socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror(MOCK "ClientContext:connect: ::socket()");
return 0;
}
server.sin_family = AF_INET;
server.sin_port = htons(port);
memcpy(&server.sin_addr, &ipv4, 4);
if (::connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1)
{
perror(MOCK "ClientContext::connect: ::connect()");
return 0;
}
return mockSockSetup(sock) == -1? 0: 1;
return mockSockSetup(sock) == -1 ? 0 : 1;
}
ssize_t mockFillInBuf (int sock, char* ccinbuf, size_t& ccinbufsize)
ssize_t mockFillInBuf(int sock, char* ccinbuf, size_t& ccinbufsize)
{
size_t maxread = CCBUFSIZE - ccinbufsize;
ssize_t ret = ::read(sock, ccinbuf + ccinbufsize, maxread);
size_t maxread = CCBUFSIZE - ccinbufsize;
ssize_t ret = ::read(sock, ccinbuf + ccinbufsize, maxread);
if (ret == 0)
{
// connection closed
// nothing is read
return 0;
}
if (ret == 0)
{
// connection closed
// nothing is read
return 0;
}
if (ret == -1)
{
if (errno != EAGAIN)
{
fprintf(stderr, MOCK "ClientContext::(read/peek fd=%i): filling buffer for %zd bytes: %s\n", sock, maxread, strerror(errno));
if (ret == -1)
{
if (errno != EAGAIN)
{
fprintf(stderr,
MOCK "ClientContext::(read/peek fd=%i): filling buffer for %zd bytes: %s\n",
sock, maxread, strerror(errno));
// error
return -1;
}
ret = 0;
}
return -1;
}
ret = 0;
}
ccinbufsize += ret;
return ret;
ccinbufsize += ret;
return ret;
}
ssize_t mockPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, char* ccinbuf, size_t& ccinbufsize)
ssize_t mockPeekBytes(int sock, char* dst, size_t usersize, int timeout_ms, char* ccinbuf,
size_t& ccinbufsize)
{
// usersize==0: peekAvailable()
if (usersize > CCBUFSIZE)
mockverbose("CCBUFSIZE(%d) should be increased by %zd bytes (-> %zd)\n", CCBUFSIZE, usersize - CCBUFSIZE, usersize);
if (usersize > CCBUFSIZE)
mockverbose("CCBUFSIZE(%d) should be increased by %zd bytes (-> %zd)\n", CCBUFSIZE,
usersize - CCBUFSIZE, usersize);
struct pollfd p;
size_t retsize = 0;
do
{
if (usersize && usersize <= ccinbufsize)
{
// data already buffered
retsize = usersize;
break;
}
// check incoming data data
if (mockFillInBuf(sock, ccinbuf, ccinbufsize) < 0)
{
return -1;
}
struct pollfd p;
size_t retsize = 0;
do
{
if (usersize && usersize <= ccinbufsize)
{
// data already buffered
retsize = usersize;
break;
}
// check incoming data data
if (mockFillInBuf(sock, ccinbuf, ccinbufsize) < 0)
{
return -1;
}
if (usersize == 0 && ccinbufsize)
// peekAvailable
return ccinbufsize;
if (usersize <= ccinbufsize)
{
// data just received
retsize = usersize;
break;
}
// wait for more data until timeout
p.fd = sock;
p.events = POLLIN;
} while (poll(&p, 1, timeout_ms) == 1);
if (usersize <= ccinbufsize)
{
// data just received
retsize = usersize;
break;
}
// wait for more data until timeout
p.fd = sock;
p.events = POLLIN;
} while (poll(&p, 1, timeout_ms) == 1);
if (dst)
{
memcpy(dst, ccinbuf, retsize);
}
return retsize;
return retsize;
}
ssize_t mockRead (int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, size_t& ccinbufsize)
ssize_t mockRead(int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf,
size_t& ccinbufsize)
{
ssize_t copied = mockPeekBytes(sock, dst, size, timeout_ms, ccinbuf, ccinbufsize);
if (copied < 0)
return -1;
// swallow (XXX use a circular buffer)
memmove(ccinbuf, ccinbuf + copied, ccinbufsize - copied);
ccinbufsize -= copied;
return copied;
ssize_t copied = mockPeekBytes(sock, dst, size, timeout_ms, ccinbuf, ccinbufsize);
if (copied < 0)
return -1;
// swallow (XXX use a circular buffer)
memmove(ccinbuf, ccinbuf + copied, ccinbufsize - copied);
ccinbufsize -= copied;
return copied;
}
ssize_t mockWrite (int sock, const uint8_t* data, size_t size, int timeout_ms)
{
size_t sent = 0;
while (sent < size)
{
struct pollfd p;
p.fd = sock;
p.events = POLLOUT;
int ret = poll(&p, 1, timeout_ms);
if (ret == -1)
{
fprintf(stderr, MOCK "ClientContext::write(%d): %s\n", sock, strerror(errno));
return -1;
}
if (ret)
{
ssize_t mockWrite(int sock, const uint8_t* data, size_t size, int timeout_ms)
{
size_t sent = 0;
while (sent < size)
{
struct pollfd p;
p.fd = sock;
p.events = POLLOUT;
int ret = poll(&p, 1, timeout_ms);
if (ret == -1)
{
fprintf(stderr, MOCK "ClientContext::write(%d): %s\n", sock, strerror(errno));
return -1;
}
if (ret)
{
#ifndef MSG_NOSIGNAL
ret = ::write(sock, data + sent, size - sent);
ret = ::write(sock, data + sent, size - sent);
#else
ret = ::send(sock, data + sent, size - sent, MSG_NOSIGNAL);
ret = ::send(sock, data + sent, size - sent, MSG_NOSIGNAL);
#endif
if (ret == -1)
{
fprintf(stderr, MOCK "ClientContext::write/send(%d): %s\n", sock, strerror(errno));
return -1;
}
sent += ret;
if (sent < size)
fprintf(stderr, MOCK "ClientContext::write: sent %d bytes (%zd / %zd)\n", ret, sent, size);
}
}
if (ret == -1)
{
fprintf(stderr, MOCK "ClientContext::write/send(%d): %s\n", sock, strerror(errno));
return -1;
}
sent += ret;
if (sent < size)
fprintf(stderr, MOCK "ClientContext::write: sent %d bytes (%zd / %zd)\n", ret, sent,
size);
}
}
#ifdef DEBUG_ESP_WIFI
mockverbose(MOCK "ClientContext::write: total sent %zd bytes\n", sent);
#endif
return sent;
return sent;
}