Added Linux function of ShmLib.

This commit is contained in:
sunwen
2022-06-10 10:44:35 +08:00
parent bca57b11cc
commit 4607454395

View File

@@ -2,8 +2,17 @@
#include <stdlib.h> #include <stdlib.h>
#include "ShimLib.h" #include "ShimLib.h"
#ifdef _WIN32
#include <Windows.h> #include <Windows.h>
#include <process.h> #include <process.h>
#else
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#endif
#include <time.h> #include <time.h>
typedef void(*error_cb)(const char* msg); typedef void(*error_cb)(const char* msg);
@@ -11,15 +20,30 @@ typedef void(*error_cb)(const char* msg);
int statusCountFlag = 0; int statusCountFlag = 0;
error_cb innerCallback = NULL; error_cb innerCallback = NULL;
#ifdef _WIN32
void ThreadFunc(void*); void ThreadFunc(void*);
HANDLE th; HANDLE th;
HANDLE e1,e2; HANDLE e1,e2;
int InitLib(error_cb cb) { #else
void* ThreadFunc(void*);
pthread_t th;
sem_t e1;
sem_t e2;
#endif
int InitLib(error_cb cb)
{
innerCallback = cb; innerCallback = cb;
innerCallback("11111"); innerCallback("11111");
#ifdef _WIN32
e1 = CreateEvent(NULL, FALSE, FALSE, "e1"); e1 = CreateEvent(NULL, FALSE, FALSE, "e1");
e2 = CreateEvent(NULL, FALSE, FALSE, "e2"); e2 = CreateEvent(NULL, FALSE, FALSE, "e2");
th = _beginthread(ThreadFunc,0, NULL); th = _beginthread(ThreadFunc, 0, NULL);
#else
sem_init(&e1, 0, 0);
sem_init(&e2, 0, 0);
th = pthread_create(&th, NULL, ThreadFunc, (void*)"");
#endif
return 0; return 0;
} }
@@ -30,14 +54,28 @@ volatile int stop_flag = 0;
volatile int empty = 0; volatile int empty = 0;
char output_path[256] = {0}; char output_path[256] = {0};
void ThreadFunc(void* args){ #ifdef _WIN32
while (running){ void ThreadFunc(void* args)
{
#else
void* ThreadFunc(__attribute__((unused))void* args)
{
#endif
while (running)
{
stop_flag = 0; stop_flag = 0;
progress = 0; progress = 0;
#ifdef _WIN32
WaitForSingleObject(e1, INFINITE); WaitForSingleObject(e1, INFINITE);
#else
sem_wait(&e1);
#endif
status = SCANNING; status = SCANNING;
#ifdef _WIN32
Sleep(300); Sleep(300);
#else
usleep(300000);
#endif
if (empty) if (empty)
{ {
GetPreviewData(); GetPreviewData();
@@ -49,7 +87,11 @@ void ThreadFunc(void* args){
} }
if (i == 20){ if (i == 20){
progress = 139; progress = 139;
#ifdef _WIN32
WaitForSingleObject(e2, INFINITE); WaitForSingleObject(e2, INFINITE);
#else
sem_wait(&e2);
#endif
} }
else if ( i > 20 && i<35){ else if ( i > 20 && i<35){
progress = i * 2 + 100; progress = i * 2 + 100;
@@ -64,7 +106,11 @@ void ThreadFunc(void* args){
stop_flag = 0; stop_flag = 0;
break; break;
} }
#ifdef _WIN32
Sleep(300); Sleep(300);
#else
usleep(300000);
#endif
} }
status = READY; status = READY;
} }
@@ -76,9 +122,17 @@ int ScanControl(ScanAction actionType) {
case SCAN: case SCAN:
printf("Do Scan!\r\n"); printf("Do Scan!\r\n");
statusCountFlag = 2; statusCountFlag = 2;
#ifdef _WIN32
SYSTEMTIME st = {0}; SYSTEMTIME st = {0};
GetLocalTime(&st); GetLocalTime(&st);
#else
time_t now;
struct tm* st;
time(&now);
st = localtime(&now);
#endif
sprintf(output_path, "%d%02d%02dT%02d%02d%02d", sprintf(output_path, "%d%02d%02dT%02d%02d%02d",
#ifdef _WIN32
st.wYear, st.wYear,
st.wMonth, st.wMonth,
st.wDay, st.wDay,
@@ -86,6 +140,15 @@ int ScanControl(ScanAction actionType) {
st.wMinute, st.wMinute,
st.wSecond); st.wSecond);
SetEvent(e1); SetEvent(e1);
#else
st->tm_year+1900,
st->tm_mon+1,
st->tm_mday,
st->tm_hour,
st->tm_min,
st->tm_sec);
sem_post(&e1);
#endif
break; break;
case PREVIEW_SCAN: case PREVIEW_SCAN:
statusCountFlag = 1; statusCountFlag = 1;
@@ -97,11 +160,19 @@ int ScanControl(ScanAction actionType) {
stop_flag = 1; stop_flag = 1;
progress = 0; progress = 0;
status = READY; status = READY;
#ifdef _WIN32
SetEvent(e2); SetEvent(e2);
#else
sem_post(&e2);
#endif
printf("Stop everything!\r\n"); printf("Stop everything!\r\n");
break; break;
case SCAN_CONTINUE: case SCAN_CONTINUE:
#ifdef _WIN32
SetEvent(e2); SetEvent(e2);
#else
sem_post(&e2);
#endif
break; break;
} }
return 0; return 0;
@@ -117,7 +188,11 @@ StatusInfo GetStatus() {
} }
//result, 0 success, other false //result, 0 success, other false
#ifdef _WIN32
int SetScanInfo(const char* jsonString, int e) { int SetScanInfo(const char* jsonString, int e) {
#else
int SetScanInfo(const __attribute__((unused))char* jsonString, __attribute__((unused))int e){
#endif
empty = e; empty = e;
return 0; return 0;
} }
@@ -137,7 +212,7 @@ const char* GetPreviewData() {
previewCount++; previewCount++;
if (previewCount>3){ if (previewCount>3){
status = READY; status = READY;
innerCallback("22222"); innerCallback("Preview Device Error");
return NULL; return NULL;
} }
FILE* file; FILE* file;