You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
241 lines
7.3 KiB
241 lines
7.3 KiB
#include "MEKOROT_Kernel.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "KRNL", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X )
|
|
|
|
bool getconchar(KEY_EVENT_RECORD& krec) {
|
|
DWORD cc, NOF;
|
|
INPUT_RECORD irec;
|
|
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
if (h == NULL) {
|
|
return false; // console not found
|
|
};
|
|
if (!GetNumberOfConsoleInputEvents(h, &NOF)) return false;
|
|
if( NOF ) for(int i=0;i<NOF; i++)
|
|
{
|
|
ReadConsoleInput(h, &irec, 1, &cc);
|
|
if (irec.EventType == KEY_EVENT
|
|
&& ((KEY_EVENT_RECORD&)irec.Event).bKeyDown
|
|
)//&& ! ((KEY_EVENT_RECORD&)irec.Event).wRepeatCount )
|
|
{
|
|
krec = (KEY_EVENT_RECORD&)irec.Event;
|
|
return true;
|
|
}
|
|
}
|
|
return false; //future ????
|
|
};
|
|
|
|
|
|
mekorotKernel::mekorotKernel(config_setting_t* ptr) : protocolLGS_CLIENT(), basicThread(), basicEngine() {
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(ptr, (CSTR)"KERNEL");
|
|
m_bStage = MEKOROT_KRNL_STG_INIT;
|
|
m_pSerial = NULL;
|
|
m_pSerialDevice[0] = (char)0;
|
|
m_nSerialSpeed = 19200;
|
|
m_bDeviceID[0] = m_bDeviceID[1] = m_bDeviceID[2] = -1;
|
|
m_bDeviceFlags[0] = m_bDeviceFlags[1] = m_bDeviceFlags[2] = false;
|
|
m_bDeviceTO[0] = m_bDeviceTO[1] = m_bDeviceTO[2] = 0;
|
|
m_bDeviceAlarms[0] = m_bDeviceAlarms[1] = m_bDeviceAlarms[2] = false;
|
|
};
|
|
|
|
mekorotKernel::~mekorotKernel() {
|
|
if (m_pSerial != NULL) delete( m_pSerial );
|
|
};
|
|
|
|
bool mekorotKernel::init(void){
|
|
config_setting_t *pSetting;
|
|
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_SERIAL_LINE_TAG);
|
|
if (pSetting != NULL) {
|
|
sprintf(m_pSerialDevice, "%s", config_setting_get_string(pSetting));
|
|
} else {
|
|
sprintf(m_pSerialDevice, "\\\\.\\COM2");
|
|
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_SERIAL_LINE_TAG, CONFIG_TYPE_STRING);
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
if (pSetting == NULL) {
|
|
LOG_ERROR("Failure while adding setting!");
|
|
return false;
|
|
};
|
|
config_setting_set_string(pSetting, m_pSerialDevice);
|
|
};
|
|
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_SERIAL_SPEED_TAG);
|
|
if (pSetting != NULL) {
|
|
m_nSerialSpeed = config_setting_get_int(pSetting);
|
|
} else {
|
|
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_SERIAL_SPEED_TAG, CONFIG_TYPE_INT);
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
if (pSetting == NULL) {
|
|
LOG_ERROR("Failure while adding setting!");
|
|
return false;
|
|
};
|
|
config_setting_set_int(pSetting, m_nSerialSpeed );
|
|
};
|
|
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_DEVICE_0_ID_TAG);
|
|
if (pSetting != NULL) {
|
|
m_bDeviceID[0] = config_setting_get_int(pSetting);
|
|
} else {
|
|
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_DEVICE_0_ID_TAG, CONFIG_TYPE_INT);
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
if (pSetting == NULL) {
|
|
LOG_ERROR("Failure while adding setting!");
|
|
return false;
|
|
};
|
|
config_setting_set_int(pSetting, m_bDeviceID[0]);
|
|
};
|
|
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_DEVICE_1_ID_TAG);
|
|
if (pSetting != NULL) {
|
|
m_bDeviceID[1] = config_setting_get_int(pSetting);
|
|
} else {
|
|
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_DEVICE_1_ID_TAG, CONFIG_TYPE_INT);
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
if (pSetting == NULL) {
|
|
LOG_ERROR("Failure while adding setting!");
|
|
return false;
|
|
};
|
|
config_setting_set_int(pSetting, m_bDeviceID[1]);
|
|
};
|
|
pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_DEVICE_2_ID_TAG);
|
|
if (pSetting != NULL) {
|
|
m_bDeviceID[2] = config_setting_get_int(pSetting);
|
|
} else {
|
|
pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_DEVICE_2_ID_TAG, CONFIG_TYPE_INT);
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
if (pSetting == NULL) {
|
|
LOG_ERROR("Failure while adding setting!");
|
|
return false;
|
|
};
|
|
config_setting_set_int(pSetting, m_bDeviceID[2]);
|
|
};
|
|
|
|
basicSettingsProvider::getInstance()->validate();
|
|
m_bStage = MEKOROT_KRNL_STG_CONNECT;
|
|
return basicThread::init();
|
|
};
|
|
|
|
void mekorotKernel::m_fThreadProc(void){
|
|
m_bThreadStatus = TH_WORKING;
|
|
while (m_bThreadStatus < TH_ENDED) {
|
|
KEY_EVENT_RECORD krec;
|
|
if( getconchar(krec) ){
|
|
//Parse KeyBoard Input
|
|
char tBuffer[128];
|
|
sprintf(tBuffer, "Key Stroke Code: %d", krec.wVirtualKeyCode);
|
|
LOG(tBuffer);
|
|
if (krec.wVirtualKeyCode == 81 || krec.wVirtualKeyCode == 75) { // q Or k
|
|
LOG("Exit by Operator Request!");
|
|
m_bThreadStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
};
|
|
switch( m_bStage ) {
|
|
case MEKOROT_KRNL_STG_CONNECT:{
|
|
if (m_pSerial == NULL) {
|
|
m_pSerial = new serialPort(m_pSerialDevice, m_nSerialSpeed);
|
|
if (m_pSerial != NULL) {
|
|
setTransport( m_pSerial );
|
|
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
|
|
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case MEKOROT_KRNL_STG_PING_DEVICES: {
|
|
bool LB = true;
|
|
for (uint8_t index = 0; index < 3; index++) {
|
|
if (m_bDeviceID[index] != -1 && !m_bDeviceFlags[index]){
|
|
LB = false;
|
|
if (setDeviceID(m_bDeviceID[index])) {
|
|
if (!getState()) {
|
|
LOG_ERROR("Get STATE FAILURE!");
|
|
};
|
|
} else {
|
|
LOG_ERROR("Set DevID FAILURE!");
|
|
};
|
|
m_bDeviceTO[index] = getMilliseconds();
|
|
};
|
|
};
|
|
if (LB) {
|
|
m_bStage = MEKOROT_KRNL_STG_WORKING;
|
|
} else {
|
|
SLEEP(KERNEL_SLOW_SLEEP_TIME); // SLOW wait responce
|
|
};
|
|
break;
|
|
};
|
|
case MEKOROT_KRNL_STG_WORKING: {
|
|
unsigned long long cTS;
|
|
cTS = getMilliseconds();
|
|
bool AllOffLine = true;
|
|
for (uint8_t index = 0; index < 3; index++) {
|
|
if (!m_bDeviceFlags[index]) continue;
|
|
AllOffLine = false;
|
|
if ((cTS - m_bDeviceTO[index]) > 3000) {
|
|
LOG_ERROR("Device TO Detected!");
|
|
m_bDeviceFlags[index] = false;
|
|
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
|
|
continue;
|
|
};
|
|
if (setDeviceID(m_bDeviceID[index])) {
|
|
if (!getState2()) {
|
|
LOG_ERROR("Get STATE2 FAILURE!");
|
|
m_bDeviceFlags[index] = false;
|
|
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
|
|
};
|
|
} else {
|
|
LOG_ERROR("Set DevID FAILURE!");
|
|
m_bDeviceFlags[index] = false;
|
|
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
|
|
continue;
|
|
};
|
|
};
|
|
if (AllOffLine) {
|
|
m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
|
|
continue;
|
|
} else {
|
|
SLEEP(KERNEL_SLOW_SLEEP_TIME);
|
|
};
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
bool mekorotKernel::packetHandler(protocolPackage* Package) {
|
|
uint8_t cDeviceID;
|
|
uint8_t cDeviceIDX = 255;
|
|
if( Package == NULL ) return true;
|
|
if( Package->m_nSize < 4 ) return true;
|
|
cDeviceID = Package->m_pData[2];
|
|
for (uint8_t idx = 0; idx < 3; idx++) {
|
|
if (cDeviceID == m_bDeviceID[idx]) cDeviceIDX = idx;
|
|
};
|
|
if (cDeviceIDX == 255) {
|
|
LOG_ERROR("Package from not registered device!");
|
|
return true;
|
|
};
|
|
m_bDeviceTO[cDeviceIDX] = getMilliseconds();
|
|
if (!m_bDeviceFlags[cDeviceIDX]) {
|
|
char tBuff[64];
|
|
sprintf(tBuff, "Device %d Became OnLine", cDeviceIDX);
|
|
LOG(tBuff);
|
|
m_bDeviceFlags[cDeviceIDX] = true;
|
|
};
|
|
switch (cDeviceIDX) {
|
|
case 0: {
|
|
LOG("Left Vertical");
|
|
break;
|
|
};
|
|
case 1: {
|
|
LOG("Center Horisontal");
|
|
break;
|
|
};
|
|
case 2: {
|
|
LOG("Right Vertical");
|
|
break;
|
|
};
|
|
};
|
|
return true;// to erase data
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|