Skip to content

Commit

Permalink
[PC] add compatibility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
a8jan committed Nov 8, 2023
1 parent 48dfa99 commit 021f8ae
Show file tree
Hide file tree
Showing 13 changed files with 1,537 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/compat/compat_dirent.h
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
12 changes: 12 additions & 0 deletions lib/compat/compat_esp.h
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 */
86 changes: 86 additions & 0 deletions lib/compat/compat_gettimeofday.c
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
22 changes: 22 additions & 0 deletions lib/compat/compat_gettimeofday.h
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
56 changes: 56 additions & 0 deletions lib/compat/compat_inet.c
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
}
43 changes: 43 additions & 0 deletions lib/compat/compat_inet.h
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
20 changes: 20 additions & 0 deletions lib/compat/compat_string.h
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
32 changes: 32 additions & 0 deletions lib/compat/compat_uname.h
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
63 changes: 63 additions & 0 deletions lib/compat/linux_termios2.h
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

Loading

0 comments on commit 021f8ae

Please sign in to comment.