dirs refactoring

This commit is contained in:
2025-02-16 12:38:13 +01:00
parent 054165ed84
commit a61fe778e6
60 changed files with 67 additions and 53 deletions

View File

@@ -0,0 +1,5 @@
set(SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
file(GLOB_RECURSE ALL_SOURCES "*.c")
add_library(os_interface ${ALL_SOURCES})

595
modules/os_interface/fs.c Normal file
View File

@@ -0,0 +1,595 @@
/*
* JTAG Core library
* Copyright (c) 2008 - 2024 Viveris Technologies
*
* JTAG Core library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* JTAG Core library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with JTAG Core library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file fs.c
* @brief Basic/generic OS file system functions wrapper.
* @author Jean-Fran<61>ois DEL NERO <Jean-Francois.DELNERO@viveris.fr>
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include "stdint.h"
#ifdef WIN32
# include <windows.h>
# include <io.h>
# include <fcntl.h>
# include <direct.h>
#else
# include <unistd.h>
# include <dirent.h>
#ifdef OSX
# include <mach-o/dyld.h>
#endif
#endif
#include "os_interface.h"
#ifdef WIN32
int convertpath (const char * path, wchar_t * wpath)
{
if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
{
errno = ENOENT;
return -1;
}
wpath[MAX_PATH] = L'\0';
return 0;
}
#else
#endif
int genos_open (const char *filename, int flags, ...)
{
#ifdef WIN32
wchar_t wpath[MAX_PATH+1];
#else
const char *local_name;
#endif
unsigned int mode = 0;
va_list ap;
#if defined (DEBUG)
printf("genos_open : filename=%s flags=%x\n",filename,flags);
#endif
va_start (ap, flags);
if (flags & O_CREAT)
mode = va_arg (ap, unsigned int);
va_end (ap);
#ifdef WIN32
memset(wpath,0,(MAX_PATH+1)*sizeof(wchar_t));
if(convertpath(filename, wpath)<0)
return -1;
return _wopen (wpath, flags, mode);
#else
local_name = filename;
//local_name = (const char *)ToLocale (filename);
if (local_name == NULL)
{
errno = ENOENT;
return -1;
}
int fd = open (local_name, flags, mode);
//LocaleFree (local_name);
return fd;
#endif
}
FILE *genos_fopen (const char *filename, const char *mode)
{
int rwflags, oflags;
int append;
int fd;
const char *ptr;
FILE *stream;
#if defined (DEBUG)
printf("genos_fopen : filename=%s mode=%s\n",filename,mode);
#endif
// Try the classic way (ascii path)
stream = fopen(filename,mode);
if( ( stream == NULL ) && (errno == ENOENT) )
{
// Try the utf8 way
rwflags = 0;
oflags = 0;
append = 0;
for(ptr = mode;*ptr;ptr++)
{
switch (*ptr)
{
case 'r':
rwflags = O_RDONLY;
break;
#ifdef O_BINARY
case 'b':
oflags |= O_BINARY;
break;
#endif
case 'a':
rwflags = O_WRONLY;
oflags |= O_CREAT;
append = 1;
break;
case 'w':
rwflags = O_WRONLY;
oflags |= O_CREAT | O_TRUNC;
break;
case '+':
rwflags = O_RDWR;
break;
#ifdef O_TEXT
case 't':
oflags |= O_TEXT;
break;
#endif
}
}
fd = genos_open (filename, rwflags|oflags, (unsigned int)(0666));
if(fd==-1)
return NULL;
if(append)
{
#if defined (WIN32)
if(_lseek(fd,0,SEEK_END) == -1)
{
_close (fd);
return NULL;
}
#else
if(lseek(fd,0,SEEK_END) == -1)
{
close (fd);
return NULL;
}
#endif
}
else
{
#if defined (WIN32)
_lseek(fd,0,SEEK_SET);
#else
lseek(fd,0,SEEK_SET);
#endif
}
#if defined (WIN32)
stream = _fdopen(fd,mode);
if(stream == NULL)
{
_close (fd);
}
#else
stream = fdopen(fd,mode);
if(stream == NULL)
{
close (fd);
}
#endif
}
return stream;
}
int genos_fread(void * ptr, size_t size, FILE *f)
{
#if defined (DEBUG)
printf("genos_fread : ptr=%p size=%zu file:%p\n",ptr,size,f);
#endif
if( fread(ptr,size,1,f) != 1 )
{
return 1; // Error
}
else
{
return 0; // No Error
}
}
char * genos_fgets(char * str, int num, FILE *f)
{
return fgets( str, num, f );
}
int genos_fclose(FILE * f)
{
#if defined (DEBUG)
printf("genos_fclose : file:%p\n",f);
#endif
return fclose(f);
}
int genos_statex( const char *filename, struct stat *buf)
{
#if defined (DEBUG)
printf("genos_statex : filename=%s\n",filename);
#endif
#if defined (WIN32)
wchar_t wpath[MAX_PATH+1];
if( convertpath(filename, wpath) < 0 )
return -1;
return _wstat (wpath,(struct _stat *)buf);
#else
int res;
const char *local_name;
local_name = filename;//ToLocale( filename );
if( local_name != NULL )
{
res = lstat( local_name, buf );
// LocaleFree( local_name );
return res;
}
errno = ENOENT;
return -1;
#endif
}
int genos_stat( const char *filename, struct stat *buf)
{
return genos_statex(filename,buf);
}
void * genos_find_first_file(char *folder, char *file, filefoundinfo* fileinfo)
{
#if defined (DEBUG)
printf("genos_find_first_file : folder=%s file=%s\n",folder,file);
#endif
#if defined (WIN32)
HANDLE hfindfile;
char *folderstr;
WIN32_FIND_DATAW FindFileData;
wchar_t wpath[MAX_PATH+1];
if(file)
{
folderstr=(char *) malloc(strlen(folder)+strlen(file)+2);
sprintf((char *)folderstr,"%s\\%s",folder,file);
}
else
{
folderstr = (char *) malloc(strlen(folder)+1);
sprintf((char *)folderstr,"%s",folder);
}
convertpath (folderstr, wpath);
hfindfile = FindFirstFileW(wpath, &FindFileData);
if(hfindfile!=INVALID_HANDLE_VALUE)
{
WideCharToMultiByte(CP_UTF8,0,FindFileData.cFileName,-1,fileinfo->filename,sizeof(fileinfo->filename),NULL,NULL);
//sprintf(fileinfo->filename,"%s",FindFileData.cFileName);
fileinfo->isdirectory = 0;
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
fileinfo->isdirectory = 1;
}
fileinfo->size = FindFileData.nFileSizeLow;
free(folderstr);
return (void*)hfindfile;
}
else
{
free(folderstr);
return 0;
}
#else
struct dirent *d;
DIR * dir;
struct stat fileStat;
char * tmpstr;
if(!file)
{
// File mode
memset(&fileStat,0,sizeof(struct stat));
if(!lstat (folder, &fileStat))
{
if ( !S_ISDIR ( fileStat.st_mode ) )
{
fileinfo->isdirectory = 0;
fileinfo->size = fileStat.st_size;
strncpy(fileinfo->filename,genos_getfilenamebase(folder,NULL,SYS_PATH_TYPE),FILEFOUND_NAMESIZE - 1);
fileinfo->filename[FILEFOUND_NAMESIZE - 1] = 0;
return (void*)-1;
}
}
}
dir = opendir (folder);
if(dir)
{
d = readdir (dir);
if(d)
{
tmpstr = malloc (strlen(folder) + strlen(d->d_name) + 4 );
if(tmpstr)
{
strcpy(tmpstr,folder);
strcat(tmpstr,"/");
strcat(tmpstr,d->d_name);
memset(&fileStat,0,sizeof(struct stat));
if(!lstat (tmpstr, &fileStat))
{
if ( S_ISDIR ( fileStat.st_mode ) )
fileinfo->isdirectory=1;
else
fileinfo->isdirectory=0;
fileinfo->size=fileStat.st_size;
strncpy(fileinfo->filename,d->d_name,FILEFOUND_NAMESIZE - 1);
fileinfo->filename[FILEFOUND_NAMESIZE - 1] = '\0';
free(tmpstr);
return (void*)dir;
}
free(tmpstr);
}
closedir (dir);
dir=0;
return (void*)dir;
}
closedir (dir);
dir=0;
}
return (void*)dir;
#endif
return 0;
}
int genos_find_next_file(void* handleff, char *folder, char *file, filefoundinfo* fileinfo)
{
int ret;
#if defined (DEBUG)
printf("genos_find_next_file : handleff:%p folder=%s file=%s\n",handleff,folder,file);
#endif
#if defined (WIN32)
WIN32_FIND_DATAW FindFileData;
ret=FindNextFileW((HANDLE)handleff,&FindFileData);
if(ret)
{
WideCharToMultiByte(CP_UTF8,0,FindFileData.cFileName,-1,fileinfo->filename,sizeof(fileinfo->filename),NULL,NULL);
//sprintf(fileinfo->filename,"%s",FindFileData.cFileName);
fileinfo->isdirectory=0;
if( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
fileinfo->isdirectory = 1;
}
fileinfo->size = FindFileData.nFileSizeLow;
}
#else
struct dirent *d;
DIR * dir;
struct stat fileStat;
char * tmpstr;
if((long)(handleff) == -1) // File mode
return 0;
dir = (DIR*) handleff;
d = readdir (dir);
ret = 0;
if(d)
{
tmpstr = malloc (strlen(folder) + strlen(d->d_name) + 4 );
if(tmpstr)
{
strcpy(tmpstr,folder);
strcat(tmpstr,"/");
strcat(tmpstr,d->d_name);
if(!lstat (tmpstr, &fileStat))
{
if ( S_ISDIR ( fileStat.st_mode ) )
fileinfo->isdirectory=1;
else
fileinfo->isdirectory=0;
fileinfo->size=fileStat.st_size;
strncpy(fileinfo->filename,d->d_name,FILEFOUND_NAMESIZE - 1);
fileinfo->filename[FILEFOUND_NAMESIZE - 1] = '\0';
ret = 1;
free(tmpstr);
return ret;
}
free(tmpstr);
}
}
#endif
return ret;
}
int genos_find_close(void* handle)
{
#if defined (DEBUG)
printf("genos_find_close : handle:%p\n",handle);
#endif
#if defined (WIN32)
if(handle)
FindClose((void*)handle);
#else
if((long)(handle) == -1) // File mode
return 0;
if(handle)
closedir((DIR*) handle);
#endif
return 0;
}
char * genos_getcurrentdirectory(char *currentdirectory,int buffersize)
{
memset(currentdirectory,0,buffersize);
#if defined (WIN32)
if(GetModuleFileName(GetModuleHandle(NULL),currentdirectory,buffersize))
{
if(strrchr(currentdirectory,'\\'))
{
*((char*)strrchr(currentdirectory,'\\')) = 0;
return currentdirectory;
}
}
#else
#if defined (OSX)
if (_NSGetExecutablePath(currentdirectory, &buffersize) == 0)
{
if(strrchr(currentdirectory,'/'))
{
*((char*)strrchr(currentdirectory,'/')) = 0;
return currentdirectory;
}
}
#else
strncpy(currentdirectory,"./",buffersize - 1);
currentdirectory[buffersize-1] = '\0';
return currentdirectory;
#endif
#endif
return 0;
}
int genos_mkdir(char * folder)
{
#if defined (DEBUG)
printf("genos_mkdir : folder:%s\n",folder);
#endif
#ifdef WIN32
_mkdir(folder);
#else
mkdir(folder,0777);
#endif
return 0;
}
int genos_fgetsize( FILE * f )
{
int cur_pos,filesize;
filesize = 0;
if( f )
{
cur_pos = ftell(f);
if( cur_pos >= 0 )
{
if(fseek (f , 0 , SEEK_END))
return 0;
filesize = ftell(f);
if( filesize < 0 )
filesize = 0;
fseek (f , cur_pos , SEEK_SET);
}
return filesize;
}
return 0;
}

View File

@@ -0,0 +1,202 @@
/*
* JTAG Core library
* Copyright (c) 2008 - 2024 Viveris Technologies
*
* JTAG Core library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* JTAG Core library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with JTAG Core library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file network.c
* @brief Basic/generic network functions wrapper.
* @author Jean-François DEL NERO <Jean-Francois.DELNERO@viveris.fr>
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <stdint.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <commctrl.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close (s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#include <unistd.h>
#include <dirent.h>
#ifdef OSX
# include <mach-o/dyld.h>
#endif
#endif
#include "network.h"
#include "os_interface.h"
void * network_connect(char * address,unsigned short port)
{
genos_tcp_stat * tcp_stat;
#ifdef WIN32
int iResult;
#endif
tcp_stat = malloc(sizeof(genos_tcp_stat));
if(tcp_stat)
{
memset(tcp_stat,0,sizeof(genos_tcp_stat));
#ifdef WIN32
iResult = WSAStartup(MAKEWORD(2,2), &tcp_stat->wsaData);
if (iResult != NO_ERROR)
{
free(tcp_stat);
return (void*)NULL;
}
#endif
tcp_stat->m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (tcp_stat->m_socket == INVALID_SOCKET)
{
#ifdef WIN32
WSACleanup();
#endif
free(tcp_stat);
return (void*)NULL;
}
tcp_stat->clientService.sin_family = AF_INET;
tcp_stat->clientService.sin_addr.s_addr = inet_addr(address);
tcp_stat->clientService.sin_port = htons(port);
if (connect(tcp_stat->m_socket, (SOCKADDR*)&tcp_stat->clientService, sizeof(tcp_stat->clientService)) == SOCKET_ERROR)
{
#ifdef WIN32
WSACleanup();
#else
close(tcp_stat->m_socket);
#endif
free(tcp_stat);
return (void*)NULL;
}
return (void*)tcp_stat;
}
return (void*)NULL;
}
int network_read(void * network_connection, unsigned char * buffer, int size,int timeout)
{
int bytesRecv;
int offset;
genos_tcp_stat * tcp_stat;
tcp_stat = (genos_tcp_stat *)network_connection;
offset=0;
while(offset < size)
{
bytesRecv = recv(tcp_stat->m_socket, (char*)&buffer[offset], size - offset, 0);
if(bytesRecv == SOCKET_ERROR)
{
return -1;
}
else
{
offset += bytesRecv;
}
}
return size;
}
int network_read2(void * network_connection, unsigned char * buffer, int size,int timeout)
{
int bytesRecv;
int offset;
genos_tcp_stat * tcp_stat;
tcp_stat = (genos_tcp_stat *)network_connection;
offset=0;
bytesRecv = recv(tcp_stat->m_socket, (char*)&buffer[offset], size - offset, 0);
return bytesRecv;
}
int network_write(void * network_connection, unsigned char * buffer, int size,int timeout)
{
int bytesSent;
int offset;
genos_tcp_stat * tcp_stat;
tcp_stat = (genos_tcp_stat *)network_connection;
offset = 0;
while(offset < size)
{
bytesSent = send(tcp_stat->m_socket, (char*)&buffer[offset], size - offset, 0);
if(bytesSent == SOCKET_ERROR)
{
return -1;
}
else
{
offset += bytesSent;
}
}
return size;
}
int network_close(void * network_connection)
{
genos_tcp_stat * tcp_stat;
tcp_stat = (genos_tcp_stat *)network_connection;
closesocket (tcp_stat->m_socket);
#ifdef WIN32
WSACleanup();
#endif
free(tcp_stat);
return 0;
}

View File

@@ -0,0 +1,33 @@
/*
* JTAG Boundary Scanner
* Copyright (c) 2008 - 2024 Viveris Technologies
*
* JTAG Boundary Scanner 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 3.0 of the License, or (at your option) any later version.
*
* JTAG Boundary Scanner 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 version 3 for more details.
*
* You should have received a copy of the GNU General Public License
* along with JTAG Boundary Scanners; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file network.h
* @brief Basic/generic network functions wrapper header.
* @author Jean-François DEL NERO <Jean-Francois.DELNERO@viveris.fr>
*/
typedef struct genos_tcp_stat_
{
#ifdef WIN32
WSADATA wsaData;
#endif
struct sockaddr_in clientService;
SOCKET m_socket;
}genos_tcp_stat;

View File

@@ -0,0 +1,514 @@
/*
* JTAG Core library
* Copyright (c) 2008 - 2024 Viveris Technologies
*
* JTAG Core library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* JTAG Core library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with JTAG Core library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file os_interface.c
* @brief Basic/generic OS functions wrapper.
* @author Jean-Fran<61>ois DEL NERO <Jean-Francois.DELNERO@viveris.fr>
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/time.h>
#include <pthread.h>
#include <sched.h>
#include <errno.h>
#include <stdint.h>
#include "os_interface.h"
typedef struct _EVENT_HANDLE{
pthread_cond_t eCondVar;
pthread_mutex_t eMutex;
int iVar;
} EVENT_HANDLE;
EVENT_HANDLE * eventtab[256];
pthread_mutex_t criticalsectiontab[256];
#if 0
void * ThreadProc( void *lpParameter)
{
threadinit *threadinitptr;
THREADFUNCTION thread;
jtag_core* jtag_ctx;
void * hw_context;
threadinitptr=(threadinit*)lpParameter;
if( threadinitptr )
{
thread=threadinitptr->thread;
jtag_ctx = threadinitptr->jtag_ctx;
hw_context=threadinitptr->hwcontext;
thread(jtag_ctx,hw_context);
free(threadinitptr);
}
return 0;
}
#endif
int genos_setevent(unsigned char id)
{
pthread_mutex_lock(&eventtab[id]->eMutex);
pthread_cond_signal(&eventtab[id]->eCondVar);
pthread_mutex_unlock(&eventtab[id]->eMutex);
return 0;
}
uintptr_t genos_createevent(unsigned char id)
{
eventtab[id]=(EVENT_HANDLE*)malloc(sizeof(EVENT_HANDLE));
pthread_mutex_init(&eventtab[id]->eMutex, NULL);
pthread_cond_init(&eventtab[id]->eCondVar, NULL);
return (uintptr_t)eventtab[id];
}
int genos_waitevent(int id,int timeout)
{
struct timeval now;
struct timespec timeoutstr;
int retcode;
pthread_mutex_lock(&eventtab[id]->eMutex);
gettimeofday(&now,0);
timeoutstr.tv_sec = now.tv_sec + (timeout/1000);
timeoutstr.tv_nsec = (now.tv_usec * 1000) ;//+(timeout*1000000);
retcode = 0;
retcode = pthread_cond_timedwait(&eventtab[id]->eCondVar, &eventtab[id]->eMutex, &timeoutstr);
if (retcode == ETIMEDOUT)
{
pthread_mutex_unlock(&eventtab[id]->eMutex);
return 1;
}
else
{
pthread_mutex_unlock(&eventtab[id]->eMutex);
return 0;
}
}
uintptr_t genos_createcriticalsection(unsigned char id)
{
//create mutex attribute variable
pthread_mutexattr_t mAttr;
pthread_mutexattr_init(&mAttr);
#if defined(__APPLE__)
// setup recursive mutex for mutex attribute
pthread_mutexattr_settype(&mAttr, PTHREAD_MUTEX_RECURSIVE);
#else
pthread_mutexattr_settype(&mAttr, PTHREAD_MUTEX_RECURSIVE_NP);
#endif
// Use the mutex attribute to create the mutex
pthread_mutex_init(&criticalsectiontab[id], &mAttr);
// Mutex attribute can be destroy after initializing the mutex variable
pthread_mutexattr_destroy(&mAttr);
return (uintptr_t)&criticalsectiontab[id];
}
void genos_entercriticalsection(unsigned char id)
{
#ifdef WIN32
EnterCriticalSection( &criticalsectiontab[id] );
#else
pthread_mutex_lock( &criticalsectiontab[id] );
#endif
}
void genos_leavecriticalsection(unsigned char id)
{
#ifdef WIN32
LeaveCriticalSection( &criticalsectiontab[id] );
#else
pthread_mutex_unlock( &criticalsectiontab[id] );
#endif
}
void genos_destroycriticalsection(unsigned char id)
{
#ifdef WIN32
DeleteCriticalSection(&criticalsectiontab[id]);
#else
pthread_mutex_destroy (&criticalsectiontab[id]);
#endif
}
void genos_msleep (unsigned int ms) {
int microsecs;
struct timeval tv;
microsecs = ms * 1000;
tv.tv_sec = microsecs / 1000000;
tv.tv_usec = microsecs % 1000000;
select (0, NULL, NULL, NULL, &tv);
}
void genos_pause(int ms)
{
genos_msleep(ms);
}
#if 0
int genos_createthread(void* hwcontext,THREADFUNCTION thread,int priority)
{
unsigned long sit;
int ret;
pthread_t threadid;
pthread_attr_t threadattrib;
threadinit *threadinitptr;
struct sched_param param;
JTAGCORE_PRINT_FUNC print_callback;
sit = 0;
pthread_attr_init(&threadattrib);
pthread_attr_setinheritsched(&threadattrib, PTHREAD_EXPLICIT_SCHED);
if(priority)
{
pthread_attr_setschedpolicy(&threadattrib,SCHED_FIFO);
param.sched_priority = sched_get_priority_max(SCHED_FIFO);
}
else
{
pthread_attr_setschedpolicy(&threadattrib,SCHED_OTHER);
param.sched_priority = sched_get_priority_max(SCHED_OTHER);
}
/* set the new scheduling param */
pthread_attr_setschedparam (&threadattrib, &param);
print_callback = jtag_ctx->jtagcore_print_callback;
threadinitptr = (threadinit *)malloc(sizeof(threadinit));
if( threadinitptr )
{
threadinitptr->thread = thread;
threadinitptr->jtag_ctx = jtag_ctx;
//threadinitptr->hwcontext=hwcontext;
ret = pthread_create(&threadid, &threadattrib,ThreadProc, threadinitptr);
if(ret)
{
print_callback(jtag_ctx,"genos_createthread : pthread_create failed !");
free( threadinitptr );
}
}
else
{
print_callback(jtag_ctx,"genos_createthread : memory allocation failed !");
}
return sit;
}
#endif
char * genos_strupper(char * str)
{
int i;
i=0;
while(str[i])
{
if(str[i]>='a' && str[i]<='z')
{
str[i]=str[i]+('A'-'a');
}
i++;
}
return str;
}
char * genos_strlower(char * str)
{
int i;
i=0;
while(str[i])
{
if(str[i]>='A' && str[i]<='Z')
{
str[i]=str[i]+('a'-'A');
}
i++;
}
return str;
}
char * genos_getfilenamebase(char * fullpath,char * filenamebase, int type)
{
int len,i;
char separator;
if(fullpath)
{
len=strlen(fullpath);
separator = DIR_SEPARATOR_CHAR; // system type by default
switch(type)
{
case SYS_PATH_TYPE: // System based
separator = DIR_SEPARATOR_CHAR;
break;
case UNIX_PATH_TYPE: // Unix style
separator = '/';
break;
case WINDOWS_PATH_TYPE: // Windows style
separator = '\\';
break;
}
i=0;
if(len)
{
i=len-1;
while(i && ( fullpath[i] != separator && fullpath[i]!=':') )
{
i--;
}
if( fullpath[i] == separator || fullpath[i]==':' )
{
i++;
}
if(i>len)
{
i=len;
}
}
if(filenamebase)
{
strcpy(filenamebase,&fullpath[i]);
}
return &fullpath[i];
}
return 0;
}
char * genos_getfilenameext(char * fullpath,char * filenameext, int type )
{
char * filename;
int len,i;
filename = genos_getfilenamebase(fullpath,0,type);
if(filename)
{
len=strlen(filename);
i=0;
if(len)
{
i=len-1;
while(i && ( filename[i] != '.' ) )
{
i--;
}
if( filename[i] == '.' )
{
i++;
}
else
{
i=len;
}
if(i>len)
{
i=len;
}
}
if(filenameext)
{
strcpy(filenameext,&filename[i]);
}
return &filename[i];
}
return 0;
}
int genos_getfilenamewext(char * fullpath,char * filenamewext, int type)
{
char * filename;
char * ext;
int len;
len = 0;
if(fullpath)
{
filename = genos_getfilenamebase(fullpath,0,type);
ext = genos_getfilenameext(fullpath,0,type);
len = ext-filename;
if(len && filename[len-1]=='.')
{
len--;
}
if(filenamewext)
{
memcpy(filenamewext,filename,len);
filenamewext[len]=0;
}
}
return len;
}
int genos_getpathfolder(char * fullpath,char * folder,int type)
{
int len;
char * filenameptr;
len = 0;
if(fullpath)
{
filenameptr = genos_getfilenamebase(fullpath,0,type);
len = filenameptr-fullpath;
if(folder)
{
memcpy(folder,fullpath,len);
folder[len]=0;
}
}
return len;
}
int genos_checkfileext(char * path,char *ext,int type)
{
char pathext[16];
char srcext[16];
char * ptr;
if(path && ext)
{
pathext[0] = '\0';
srcext[0] = ' ';
srcext[1] = '\0';
ptr = genos_getfilenameext(path,0,type);
if(!ptr)
return 0;
if( ( strlen(ptr) < 16 ) && ( strlen(ext) < 16 ))
{
genos_getfilenameext(path,(char*)&pathext,type);
genos_strlower(pathext);
strcpy((char*)srcext,ext);
genos_strlower(srcext);
if(!strcmp(pathext,srcext))
{
return 1;
}
}
}
return 0;
}
int genos_getfilesize(char * path)
{
int filesize;
FILE * f;
filesize=-1;
if(path)
{
f=genos_fopen(path,"rb");
if(f)
{
fseek (f , 0 , SEEK_END);
filesize=ftell(f);
fclose(f);
}
}
return filesize;
}
char * genos_strndstcat( char *dest, const char *src, size_t maxdestsize )
{
int i,j;
i = 0;
while( ( i < maxdestsize ) && dest[i] )
{
i++;
}
if( !dest[i] )
{
j = 0;
while( ( i < maxdestsize ) && src[j] )
{
dest[i] = src[j];
i++;
j++;
}
if( i < maxdestsize )
{
dest[i] = '\0';
}
}
return dest;
}

View File

@@ -0,0 +1,118 @@
/*
* JTAG Boundary Scanner
* Copyright (c) 2008 - 2024 Viveris Technologies
*
* JTAG Boundary Scanner 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 3.0 of the License, or (at your option) any later version.
*
* JTAG Boundary Scanner 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 version 3 for more details.
*
* You should have received a copy of the GNU General Public License
* along with JTAG Boundary Scanners; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file os_interface.h
* @brief Basic/generic OS functions wrapper header.
* @author Jean-François DEL NERO <Jean-Francois.DELNERO@viveris.fr>
*/
#ifdef __cplusplus
extern "C" {
#endif
#define DIR_SEPARATOR "/"
#define DIR_SEPARATOR_CHAR '/'
#define FILEFOUND_NAMESIZE 256
/////////////// Thread functions ////////////////
#if 0
typedef int (*THREADFUNCTION) (void* jtag_ctx,void* hw_ctx);
typedef struct threadinit_
{
THREADFUNCTION thread;
jtag_core * jtag_ctx;
void * hwcontext;
}threadinit;
#endif
typedef struct filefoundinfo_
{
int isdirectory;
char filename[FILEFOUND_NAMESIZE];
int size;
}filefoundinfo;
int genos_setevent(unsigned char id );
uintptr_t genos_createevent(unsigned char id );
int genos_waitevent(int id, int timeout );
void genos_pause( int ms );
#if 0
int genos_createthread(void* hwcontext, THREADFUNCTION thread, int priority );
#endif
uintptr_t genos_createcriticalsection(unsigned char id );
void genos_entercriticalsection(unsigned char id );
void genos_leavecriticalsection(unsigned char id );
void genos_destroycriticalsection(unsigned char id );
/////////////// String functions ///////////////
char * genos_strupper( char * str );
char * genos_strlower( char * str );
char * genos_strndstcat( char *dest, const char *src, size_t maxdestsize );
/////////////// File functions ////////////////
int genos_open ( const char *filename, int flags, ... );
FILE *genos_fopen ( const char *filename, const char *mode );
int genos_fread( void * ptr, size_t size, FILE *f );
char * genos_fgets( char * str, int num, FILE *f );
int genos_fclose( FILE * f );
int genos_fgetsize( FILE * f );
#ifndef stat
#include <sys/stat.h>
#endif
int genos_stat( const char *filename, struct stat *buf );
void* genos_find_first_file( char *folder, char *file, filefoundinfo* fileinfo );
int genos_find_next_file( void* handleff, char *folder, char *file, filefoundinfo* fileinfo );
int genos_find_close( void* handle );
int genos_mkdir( char * folder );
char * genos_getcurrentdirectory( char *currentdirectory, int buffersize );
enum
{
SYS_PATH_TYPE = 0,
UNIX_PATH_TYPE,
WINDOWS_PATH_TYPE,
};
char * genos_getfilenamebase( char * fullpath, char * filenamebase, int type );
char * genos_getfilenameext( char * fullpath, char * filenameext, int type );
int genos_getfilenamewext( char * fullpath, char * filenamewext, int type );
int genos_getpathfolder( char * fullpath, char * folder, int type );
int genos_checkfileext( char * path, char *ext, int type );
int genos_getfilesize( char * path );
/////////////// Network functions ////////////////
void * network_connect(char * address,unsigned short port);
int network_read(void * network_connection, unsigned char * buffer, int size,int timeout);
int network_read2(void * network_connection, unsigned char * buffer, int size,int timeout);
int network_write(void * network_connection, unsigned char * buffer, int size,int timeout);
int network_close(void * network_connection);
#ifdef __cplusplus
}
#endif