-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,537 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#if defined(_WIN32) | ||
#include "win32_dirent.h" | ||
#else | ||
#include <dirent.h> | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef COMPAT_ESP_H | ||
#define COMPAT_ESP_H | ||
|
||
#ifndef ESP_PLATFORM | ||
|
||
#ifndef IRAM_ATTR | ||
#define IRAM_ATTR | ||
#endif | ||
|
||
#endif /* !ESP_PLATFORM */ | ||
|
||
#endif /* COMPAT_ESP_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#ifndef ESP_PLATFORM | ||
|
||
#include <sys/time.h> | ||
|
||
// precise gettimeofday on Windows | ||
#if defined(_WIN32) | ||
|
||
|
||
// a) use GetSystemTimePreciseAsFileTime | ||
#include <windows.h> | ||
int compat_gettimeofday(struct timeval *tv, struct timezone* tz) | ||
{ | ||
FILETIME ft; | ||
unsigned __int64 tmpres = 0; | ||
|
||
if (tv != NULL) | ||
{ | ||
#if !defined(_UCRT) | ||
GetSystemTimeAsFileTime(&ft); // low precision (16ms) | ||
#else | ||
GetSystemTimePreciseAsFileTime(&ft); // high precision (1<ms) | ||
#endif | ||
tmpres |= ft.dwHighDateTime; | ||
tmpres <<= 32; | ||
tmpres |= ft.dwLowDateTime; | ||
tmpres /= 10; // convert into microseconds | ||
tmpres -= (__int64) 11644473600000000; | ||
tv->tv_sec = (long) (tmpres / 1000000UL); | ||
tv->tv_usec = (long) (tmpres % 1000000UL); | ||
} | ||
(void) tz; | ||
return 0; | ||
} | ||
|
||
|
||
// // b) use C++ chrono (portable, slower) (rename file to .cpp) | ||
// #include <chrono> | ||
// int compat_gettimeofday(struct timeval* tp, struct timezone* tzp) | ||
// { | ||
// namespace sc = std::chrono; | ||
// if (tv != NULL) | ||
// { | ||
// sc::system_clock::duration d = sc::system_clock::now().time_since_epoch(); | ||
// sc::seconds s = sc::duration_cast<sc::seconds>(d); | ||
// tp->tv_sec = s.count(); | ||
// tp->tv_usec = sc::duration_cast<sc::microseconds>(d - s).count(); | ||
// } | ||
// (void) tz; | ||
// return 0; | ||
// } | ||
|
||
#else | ||
// Linux and macOS | ||
|
||
int compat_gettimeofday(struct timeval* tp, struct timezone* tzp) | ||
{ | ||
return gettimeofday(tp, tzp); | ||
} | ||
|
||
#endif | ||
|
||
|
||
// Test compat_gettimeofday | ||
// example: | ||
// test_gettimeofday(100000); | ||
// test_gettimeofday(10000000); | ||
|
||
// #include <stdint.h> | ||
// #include <stdio.h> | ||
// #include "compat_gettimeofday.h" | ||
|
||
// void test_gettimeofday(int iter) | ||
// { | ||
// struct timeval tv; | ||
// printf("test_gettimeofday(%d) started\n", iter); | ||
// compat_gettimeofday(&tv, NULL); | ||
// uint64_t t1 = (uint64_t)(tv.tv_sec*1000ULL+tv.tv_usec/1000ULL); | ||
// for(int i = 0; i < iter; i++) | ||
// { | ||
// compat_gettimeofday(&tv, NULL); | ||
// } | ||
// uint64_t t2 = (uint64_t)(tv.tv_sec*1000ULL+tv.tv_usec/1000ULL); | ||
// printf("test_gettimeofday ended in %lu ms\n", (unsigned long)(t2-t1)); | ||
// } | ||
|
||
#endif // !ESP_PLATFORM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef COMPAT_GETTIMEOFDAY_H | ||
#define COMPAT_GETTIMEOFDAY_H | ||
|
||
#ifndef ESP_PLATFORM | ||
|
||
#include <time.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
int compat_gettimeofday(struct timeval* tp, struct timezone* tzp); | ||
|
||
// void test_gettimeofday(int iter); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // !ESP_PLATFORM | ||
|
||
#endif // COMPAT_GETTIMEOFDAY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <errno.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
|
||
#include "compat_inet.h" | ||
|
||
char *compat_inet_ntoa(in_addr_t in) | ||
{ | ||
struct in_addr sin; | ||
sin.s_addr = in; | ||
return inet_ntoa(sin); | ||
} | ||
|
||
int compat_getsockerr() | ||
{ | ||
#if defined(_WIN32) | ||
return WSAGetLastError(); | ||
#else | ||
return errno; | ||
#endif | ||
} | ||
|
||
void compat_setsockerr(int err) | ||
{ | ||
#if defined(_WIN32) | ||
WSASetLastError(err); | ||
#else | ||
errno = err; | ||
#endif | ||
} | ||
|
||
// NOTE: This is not thread safe function | ||
// TODO: Thread safe variant - compat_sockstrerror_r | ||
const char *compat_sockstrerror(int err) | ||
{ | ||
#if defined(_WIN32) | ||
static char msgbuf[256]; // for a message up to 255 bytes. | ||
msgbuf [0] = '\0'; // Microsoft doesn't guarantee this on man page. | ||
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags | ||
NULL, // lpsource | ||
err, // message id | ||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // languageid | ||
msgbuf, // output buffer | ||
sizeof(msgbuf), // size of msgbuf, bytes | ||
NULL); // va_list of arguments | ||
// strip new line | ||
char *c = strrchr(msgbuf, '\r'); | ||
if (c) *c = '\0'; | ||
// provide error # if no string available | ||
if (! *msgbuf) | ||
sprintf (msgbuf, "%d", err); | ||
return msgbuf; | ||
#else | ||
return strerror(err); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef COMPAT_INET_H | ||
#define COMPAT_INET_H | ||
|
||
#if defined(_WIN32) | ||
|
||
#include <stdint.h> | ||
#include <winsock2.h> | ||
#include <ws2tcpip.h> | ||
|
||
typedef uint32_t in_addr_t; | ||
|
||
#else | ||
|
||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
|
||
#include <netdb.h> | ||
#include <sys/socket.h> | ||
|
||
#ifndef closesocket | ||
#include <unistd.h> | ||
#define closesocket(x) ::close(x) | ||
#endif | ||
|
||
#endif | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/* takes in_addr_t as argument */ | ||
char *compat_inet_ntoa(in_addr_t in); | ||
|
||
int compat_getsockerr(); | ||
void compat_setsockerr(int err); | ||
const char *compat_sockstrerror(int err); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
|
||
#endif // COMPAT_INET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef COMPAT_STRING_H | ||
#define COMPAT_STRING_H | ||
|
||
#include <string.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#if defined(__linux__) || defined(_WIN32) && !defined(ESP_PLATFORM) | ||
#include <sys/types.h> | ||
size_t strlcpy(char *dst, const char *src, size_t dsize); | ||
size_t strlcat(char *dst, const char *src, size_t dsize); | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // COMPAT_STRING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef COMPAT_UNAME_H | ||
#define COMPAT_UNAME_H | ||
|
||
#if defined(_WIN32) | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#define UTSNAME_MAXLENGTH 65 | ||
|
||
struct utsname { | ||
char sysname [UTSNAME_MAXLENGTH]; // name of this implementation of the operating system | ||
char nodename[UTSNAME_MAXLENGTH]; // name of this node within an implementation - dependent communications network | ||
char release [UTSNAME_MAXLENGTH]; // current release level of this implementation | ||
char version [UTSNAME_MAXLENGTH]; // current version level of this release | ||
char machine [UTSNAME_MAXLENGTH]; // name of the hardware type on which the system is running | ||
}; | ||
|
||
int uname(struct utsname *name); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#else | ||
|
||
#include <sys/utsname.h> | ||
|
||
#endif | ||
|
||
#endif // COMPAT_UNAME_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#ifndef TERMIOS2_H | ||
#define TERMIOS2_H | ||
|
||
/* | ||
Termios2.h - defines for struct termios2 missing in glibc | ||
Copyright (C) 2018-2019 Matthias Reichl <[email protected]> | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
*/ | ||
|
||
#include <sys/ioctl.h> | ||
#include <termios.h> | ||
|
||
#ifndef BOTHER | ||
|
||
#define BOTHER CBAUDEX | ||
#define LINUX_NCCS 19 | ||
#define LINUX_IBSHIFT 16 | ||
|
||
struct termios2 { | ||
tcflag_t c_iflag; /* input mode flags */ | ||
tcflag_t c_oflag; /* output mode flags */ | ||
tcflag_t c_cflag; /* control mode flags */ | ||
tcflag_t c_lflag; /* local mode flags */ | ||
cc_t c_line; /* line discipline */ | ||
cc_t c_cc[LINUX_NCCS]; /* control characters */ | ||
speed_t c_ispeed; /* input speed */ | ||
speed_t c_ospeed; /* output speed */ | ||
}; | ||
|
||
#endif | ||
|
||
#ifndef TCGETS2 | ||
#define TCGETS2 0x2A | ||
#endif | ||
|
||
#ifndef TCSETS2 | ||
#define TCSETS2 0x2B | ||
#endif | ||
|
||
#ifndef TCSETSW2 | ||
#define TCSETSW2 0x2C | ||
#endif | ||
|
||
#ifndef TCSETSF2 | ||
#define TCSETSF2 0x2D | ||
#endif | ||
|
||
#endif | ||
|
Oops, something went wrong.