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