1804 lines
49 KiB
C++
1804 lines
49 KiB
C++
#include "process.h"
|
|
#include "soe.h"
|
|
#include "yklog.h"
|
|
#include "ytlog.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
|
|
static void* main_run_process(void* param)
|
|
{
|
|
CProcess* proc = (CProcess *)param;
|
|
DWORD old_ticks = 0;
|
|
|
|
while (TRUE) {
|
|
if (!proc->m_bRunFlag) break;
|
|
|
|
proc->Run();
|
|
if (old_ticks != system32.ticks) {
|
|
old_ticks = system32.ticks;
|
|
proc->OnTimer(); //every 10ms
|
|
}
|
|
//usleep(5000);
|
|
usleep(DELAY_RUN);
|
|
}
|
|
|
|
pthread_exit(0);
|
|
|
|
return ((void*)0);
|
|
}
|
|
|
|
CProcessItem::CProcessItem()
|
|
{
|
|
m_uid = -1;
|
|
}
|
|
|
|
CProcessItem::~CProcessItem()
|
|
{
|
|
m_uid = -1;
|
|
}
|
|
|
|
void CProcessItem::Attach(int uid, int /* sock */, DWORD /* peer_addr */, WORD /* peer_port */)
|
|
{
|
|
m_uid = uid;
|
|
}
|
|
|
|
void CProcessItem::Release(void)
|
|
{
|
|
// m_uid = -1;
|
|
}
|
|
|
|
const BYTE crc8table[256] = {
|
|
0x00,0x07,0x0E,0x09,0x1C,0x1B,0x12,0x15,0x38,0x3F,0x36,0x31,0x24,0x23,0x2A,0x2D,
|
|
0x70,0x77,0x7E,0x79,0x6C,0x6B,0x62,0x65,0x48,0x4F,0x46,0x41,0x54,0x53,0x5A,0x5D,
|
|
0xE0,0xE7,0xEE,0xE9,0xFC,0xFB,0xF2,0xF5,0xD8,0xDF,0xD6,0xD1,0xC4,0xC3,0xCA,0xCD,
|
|
0x90,0x97,0x9E,0x99,0x8C,0x8B,0x82,0x85,0xA8,0xAF,0xA6,0xA1,0xB4,0xB3,0xBA,0xBD,
|
|
0xC7,0xC0,0xC9,0xCE,0xDB,0xDC,0xD5,0xD2,0xFF,0xF8,0xF1,0xF6,0xE3,0xE4,0xED,0xEA,
|
|
0xB7,0xB0,0xB9,0xBE,0xAB,0xAC,0xA5,0xA2,0x8F,0x88,0x81,0x86,0x93,0x94,0x9D,0x9A,
|
|
0x27,0x20,0x29,0x2E,0x3B,0x3C,0x35,0x32,0x1F,0x18,0x11,0x16,0x03,0x04,0x0D,0x0A,
|
|
0x57,0x50,0x59,0x5E,0x4B,0x4C,0x45,0x42,0x6F,0x68,0x61,0x66,0x73,0x74,0x7D,0x7A,
|
|
0x89,0x8E,0x87,0x80,0x95,0x92,0x9B,0x9C,0xB1,0xB6,0xBF,0xB8,0xAD,0xAA,0xA3,0xA4,
|
|
0xF9,0xFE,0xF7,0xF0,0xE5,0xE2,0xEB,0xEC,0xC1,0xC6,0xCF,0xC8,0xDD,0xDA,0xD3,0xD4,
|
|
0x69,0x6E,0x67,0x60,0x75,0x72,0x7B,0x7C,0x51,0x56,0x5F,0x58,0x4D,0x4A,0x43,0x44,
|
|
0x19,0x1E,0x17,0x10,0x05,0x02,0x0B,0x0C,0x21,0x26,0x2F,0x28,0x3D,0x3A,0x33,0x34,
|
|
0x4E,0x49,0x40,0x47,0x52,0x55,0x5C,0x5B,0x76,0x71,0x78,0x7F,0x6A,0x6D,0x64,0x63,
|
|
0x3E,0x39,0x30,0x37,0x22,0x25,0x2C,0x2B,0x06,0x01,0x08,0x0F,0x1A,0x1D,0x14,0x13,
|
|
0xAE,0xA9,0xA0,0xA7,0xB2,0xB5,0xBC,0xBB,0x96,0x91,0x98,0x9F,0x8A,0x8D,0x84,0x83,
|
|
0xDE,0xD9,0xD0,0xD7,0xC2,0xC5,0xCC,0xCB,0xE6,0xE1,0xE8,0xEF,0xFA,0xFD,0xF4,0xF3
|
|
} ;
|
|
|
|
const BYTE crc16tableHi[] = {
|
|
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
|
|
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
|
|
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
|
|
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
|
|
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
|
|
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
|
|
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
|
|
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
|
|
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
|
|
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
|
|
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
|
|
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
|
|
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
|
|
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
|
|
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
|
|
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40
|
|
} ;
|
|
|
|
const BYTE crc16tableLo[] = {
|
|
0x00,0xC0,0xC1,0x01,0xC3,0x03,0x02,0xC2,0xC6,0x06,0x07,0xC7,0x05,0xC5,0xC4,0x04,
|
|
0xCC,0x0C,0x0D,0xCD,0x0F,0xCF,0xCE,0x0E,0x0A,0xCA,0xCB,0x0B,0xC9,0x09,0x08,0xC8,
|
|
0xD8,0x18,0x19,0xD9,0x1B,0xDB,0xDA,0x1A,0x1E,0xDE,0xDF,0x1F,0xDD,0x1D,0x1C,0xDC,
|
|
0x14,0xD4,0xD5,0x15,0xD7,0x17,0x16,0xD6,0xD2,0x12,0x13,0xD3,0x11,0xD1,0xD0,0x10,
|
|
0xF0,0x30,0x31,0xF1,0x33,0xF3,0xF2,0x32,0x36,0xF6,0xF7,0x37,0xF5,0x35,0x34,0xF4,
|
|
0x3C,0xFC,0xFD,0x3D,0xFF,0x3F,0x3E,0xFE,0xFA,0x3A,0x3B,0xFB,0x39,0xF9,0xF8,0x38,
|
|
0x28,0xE8,0xE9,0x29,0xEB,0x2B,0x2A,0xEA,0xEE,0x2E,0x2F,0xEF,0x2D,0xED,0xEC,0x2C,
|
|
0xE4,0x24,0x25,0xE5,0x27,0xE7,0xE6,0x26,0x22,0xE2,0xE3,0x23,0xE1,0x21,0x20,0xE0,
|
|
0xA0,0x60,0x61,0xA1,0x63,0xA3,0xA2,0x62,0x66,0xA6,0xA7,0x67,0xA5,0x65,0x64,0xA4,
|
|
0x6C,0xAC,0xAD,0x6D,0xAF,0x6F,0x6E,0xAE,0xAA,0x6A,0x6B,0xAB,0x69,0xA9,0xA8,0x68,
|
|
0x78,0xB8,0xB9,0x79,0xBB,0x7B,0x7A,0xBA,0xBE,0x7E,0x7F,0xBF,0x7D,0xBD,0xBC,0x7C,
|
|
0xB4,0x74,0x75,0xB5,0x77,0xB7,0xB6,0x76,0x72,0xB2,0xB3,0x73,0xB1,0x71,0x70,0xB0,
|
|
0x50,0x90,0x91,0x51,0x93,0x53,0x52,0x92,0x96,0x56,0x57,0x97,0x55,0x95,0x94,0x54,
|
|
0x9C,0x5C,0x5D,0x9D,0x5F,0x9F,0x9E,0x5E,0x5A,0x9A,0x9B,0x5B,0x99,0x59,0x58,0x98,
|
|
0x88,0x48,0x49,0x89,0x4B,0x8B,0x8A,0x4A,0x4E,0x8E,0x8F,0x4F,0x8D,0x4D,0x4C,0x8C,
|
|
0x44,0x84,0x85,0x45,0x87,0x47,0x46,0x86,0x82,0x42,0x43,0x83,0x41,0x81,0x80,0x40
|
|
} ;
|
|
|
|
CProcess::CProcess()
|
|
{
|
|
int i;
|
|
#ifdef WIN32
|
|
memset(&m_pid, NULL, sizeof(m_pid));
|
|
#else
|
|
m_pid = 0;
|
|
#endif
|
|
m_nProcess = -1;
|
|
m_nCurUnit = 0;
|
|
m_bUnitLoop = FALSE;
|
|
m_bRunFlag = FALSE;
|
|
memset(m_pItems, 0, sizeof(m_pItems));
|
|
for (i = 0; i < PROCESS_UNIT_NUM; i++)
|
|
{
|
|
m_pItems[i] = NULL;
|
|
}
|
|
}
|
|
|
|
CProcess::~CProcess()
|
|
{
|
|
}
|
|
|
|
BYTE CProcess::GetCRC8(BYTE* pBuf, int count)
|
|
{
|
|
int i;
|
|
BYTE *pData;
|
|
BYTE result = 0;
|
|
|
|
pData = pBuf;
|
|
for (i = 0; i < count; i++)
|
|
{
|
|
result = crc8table[(*pData) ^ result];
|
|
pData++;
|
|
}
|
|
result = ~result;
|
|
return result;
|
|
}
|
|
|
|
WORD CProcess::GetCRC16(BYTE* pBuf, int count)
|
|
{
|
|
int i;
|
|
BYTE uIndex;
|
|
WORD result;
|
|
BYTE uchCRCHi = 0xff;
|
|
BYTE uchCRCLo = 0xff;
|
|
BYTE* pData = pBuf;
|
|
|
|
for (i = 0; i < count; i++)
|
|
{
|
|
uIndex = uchCRCHi ^ *pData;
|
|
pData++;
|
|
uchCRCHi = uchCRCLo ^ crc16tableHi[uIndex];
|
|
uchCRCLo = crc16tableLo[uIndex];
|
|
}
|
|
|
|
result = uchCRCHi; result <<= 8;
|
|
result |= uchCRCLo;
|
|
return result;
|
|
}
|
|
|
|
BYTE CProcess::GetCheckSum8(BYTE* pData, int count)
|
|
{
|
|
int i;
|
|
BYTE result = 0;
|
|
BYTE* pBuf = pData;
|
|
|
|
for (i = 0; i < count; i++)
|
|
{
|
|
result += *pBuf;
|
|
pBuf++;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
WORD CProcess::GetCheckSum16(BYTE* pData, int count)
|
|
{
|
|
int i;
|
|
WORD result = 0;
|
|
BYTE* pBuf = pData;
|
|
|
|
for (i = 0; i < count; i++)
|
|
{
|
|
result += *pBuf;
|
|
pBuf++;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
BYTE CProcess::GetCheckLRC(BYTE* pBuf, int count)
|
|
{
|
|
int i;
|
|
BYTE uchLRC = 0;
|
|
for (i = 0; i < count; i++)
|
|
{
|
|
uchLRC += *pBuf;
|
|
pBuf++;
|
|
}
|
|
return ((BYTE)(-((char)uchLRC)));
|
|
}
|
|
|
|
|
|
BYTE CProcess::GetLRC1(const BYTE* pBuf, int count)
|
|
{
|
|
int i;
|
|
BYTE uchLRC = 0;
|
|
BYTE* pData = (BYTE *)pBuf;
|
|
for (i = 0; i < count; ++i)
|
|
{
|
|
uchLRC ^= *pData;
|
|
pData++;
|
|
}
|
|
uchLRC = ~uchLRC;
|
|
return uchLRC;
|
|
}
|
|
|
|
void CProcess::DisplayRxData(BYTE* pBuf, int count, BOOLEAN bCheck, int uid, BOOLEAN bPackage)
|
|
{
|
|
if (NULL == channelBuffer.buf) return;
|
|
if (!channelBuffer.enabled) return;
|
|
|
|
if (channelBuffer.mon_port >= 0 && m_nProcess != channelBuffer.mon_port) return;
|
|
if (channelBuffer.mon_unit >= 0 && uid != channelBuffer.mon_unit) return;
|
|
|
|
channelBuffer.rx_count += count;
|
|
|
|
int i;
|
|
BYTE* pData;
|
|
char tmp[16];
|
|
//string msg("t");
|
|
std::string text("r");
|
|
|
|
//t,0000,xxxxx
|
|
//"r,length,data";
|
|
pData = pBuf;
|
|
snprintf(tmp, sizeof(tmp), "%04d", (count << 1) + 1);
|
|
text += tmp;
|
|
for (i = 0; i < count; i++) {
|
|
snprintf(tmp, sizeof(tmp), "%02X", (int)(*pData));
|
|
text += tmp;
|
|
pData++;
|
|
}
|
|
if (bCheck) text += "+";
|
|
else text += "x";
|
|
if (bPackage) {
|
|
//text += 0x0d;
|
|
channelBuffer.rf_count++;
|
|
}
|
|
//text += "z";
|
|
DWORD l;
|
|
int len = text.length();
|
|
len = wMin(len, (int)(MAX_DISPLAY_BUFFER_SIZE - channelBuffer.save + channelBuffer.load));
|
|
/* 第一部分的拷贝:从环形缓冲区写入数据直至缓冲区最后一个地址 */
|
|
l = wMin(len, (int)(MAX_DISPLAY_BUFFER_SIZE - (channelBuffer.save & (MAX_DISPLAY_BUFFER_SIZE - 1))));
|
|
memcpy(channelBuffer.buf + (channelBuffer.save & (MAX_DISPLAY_BUFFER_SIZE - 1)), text.c_str(), l);
|
|
/* 如果溢出则在缓冲区头写入剩余的部分如果没溢出这句代码相当于无效 */
|
|
memcpy(channelBuffer.buf, text.c_str() + l, len - l);
|
|
channelBuffer.save += len;
|
|
}
|
|
|
|
void CProcess::DisplayTxData(BYTE* pBuf, int count, BOOLEAN bCheck, int uid, BOOLEAN bPackage)
|
|
{
|
|
if (NULL == channelBuffer.buf) return;
|
|
if (!channelBuffer.enabled) return;
|
|
|
|
if (channelBuffer.mon_port >= 0 && m_nProcess != channelBuffer.mon_port) return;
|
|
if (channelBuffer.mon_unit >= 0 && uid != channelBuffer.mon_unit) return;
|
|
|
|
channelBuffer.tx_count += count;
|
|
|
|
int i;
|
|
BYTE* pData;
|
|
char tmp[16];
|
|
std::string text("t");
|
|
|
|
//"t,length,data";
|
|
pData = pBuf;
|
|
snprintf(tmp, sizeof(tmp), "%04d", (count << 1) + 1);
|
|
text += tmp;
|
|
for (i = 0; i < count; i++) {
|
|
snprintf(tmp, sizeof(tmp), "%02X", (int)(*pData));
|
|
text += tmp;
|
|
pData++;
|
|
}
|
|
if (bCheck) text += "+";
|
|
else text += "x";
|
|
if (bPackage) {
|
|
//text += 0x0d;
|
|
channelBuffer.tf_count++;
|
|
}
|
|
//text += "z";
|
|
DWORD l;
|
|
int len = text.length();
|
|
len = wMin(len, (int)(MAX_DISPLAY_BUFFER_SIZE - channelBuffer.save + channelBuffer.load));
|
|
/* 第一部分的拷贝:从环形缓冲区写入数据直至缓冲区最后一个地址 */
|
|
l = wMin(len, (int)(MAX_DISPLAY_BUFFER_SIZE - (channelBuffer.save & (MAX_DISPLAY_BUFFER_SIZE - 1))));
|
|
memcpy(channelBuffer.buf + (channelBuffer.save & (MAX_DISPLAY_BUFFER_SIZE - 1)), text.c_str(), l);
|
|
/* 如果溢出则在缓冲区头写入剩余的部分如果没溢出这句代码相当于无效 */
|
|
memcpy(channelBuffer.buf, text.c_str() + l, len - l);
|
|
channelBuffer.save += len;
|
|
}
|
|
|
|
BOOLEAN CProcess::GetOption(void *pBuf, int size) const
|
|
{
|
|
if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return FALSE;
|
|
if (size < 0 || size > MAX_PROC_OPTION_SIZE)
|
|
{
|
|
memcpy(pBuf, &config.processes[m_nProcess].option, MAX_PROC_OPTION_SIZE);
|
|
}
|
|
else
|
|
{
|
|
memcpy(pBuf, &config.processes[m_nProcess].option, size);
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
BOOLEAN CProcess::GetUnitOption(int uid, void *pBuf, int size) const
|
|
{
|
|
int len;
|
|
struUnit* pUnit;
|
|
|
|
if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return FALSE;
|
|
if (uid < 0 || uid >= UNIT_NUM || size <= 0) return FALSE;
|
|
pUnit = &config.units[uid];
|
|
len = (size > MAX_UNIT_PARAM_SIZE) ? MAX_UNIT_PARAM_SIZE : size;
|
|
memcpy(pBuf, pUnit->m_param, len);
|
|
return TRUE;
|
|
}
|
|
|
|
int CProcess::GetOrderByUnitID(int uid) const
|
|
{
|
|
int i;
|
|
if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return -1;
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
for (i = 0; i < PROCESS_UNIT_NUM; i++)
|
|
{
|
|
if (config.processes[m_nProcess].units[i] == uid)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
struUnit* CProcess::GetNextUnit(void)
|
|
{
|
|
int i;
|
|
int uid;
|
|
int last;
|
|
|
|
if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return NULL;
|
|
if (m_nCurUnit >= PROCESS_UNIT_NUM) return NULL;
|
|
if (m_nCurUnit < 0) m_nCurUnit = -1;
|
|
|
|
for (i = 0; i < PROCESS_UNIT_NUM; i++)
|
|
{
|
|
uid = config.processes[m_nProcess].units[i];
|
|
if (uid < 0 || uid >= UNIT_NUM)
|
|
{
|
|
break;
|
|
}
|
|
if ((config.units[uid].state & 0x01) == TRUE)
|
|
{
|
|
if (GetUnitYK(uid) || GetUnitYT(uid))
|
|
{
|
|
m_nCurUnit = i;
|
|
return &config.units[uid];
|
|
}
|
|
}
|
|
}
|
|
last = m_nCurUnit;
|
|
for (m_nCurUnit++; m_nCurUnit < PROCESS_UNIT_NUM; m_nCurUnit++)
|
|
{
|
|
uid = config.processes[m_nProcess].units[m_nCurUnit];
|
|
if (uid < 0 || uid >= UNIT_NUM)
|
|
{
|
|
break;
|
|
}
|
|
if ((config.units[uid].state & 0x01) == TRUE)
|
|
{
|
|
return &config.units[uid];
|
|
}
|
|
}
|
|
m_bUnitLoop = TRUE;
|
|
for (m_nCurUnit = 0; m_nCurUnit <= last; m_nCurUnit++)
|
|
{
|
|
uid = config.processes[m_nProcess].units[m_nCurUnit];
|
|
if (uid < 0 || uid >= UNIT_NUM)
|
|
{
|
|
break;
|
|
}
|
|
if ((config.units[uid].state & 0x01) == TRUE)
|
|
{
|
|
return &config.units[uid];
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
BOOLEAN CProcess::GetUnitYK(int uid, BOOLEAN bAsSlave)
|
|
{
|
|
int i;
|
|
int udb;
|
|
BYTE act, result;
|
|
BOOLEAN bSlave;
|
|
BOOLEAN bMaster;
|
|
struUnit* pUnit;
|
|
struYK* pYK;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
|
bSlave = IsSlave(); if (IsDuplex() && bAsSlave) bSlave = TRUE;
|
|
bMaster = IsMaster(); if (IsDuplex() && !bAsSlave) bMaster = TRUE;
|
|
if (!bSlave && !bMaster) return FALSE;
|
|
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
|
for (i = 0; i < pUnit->ykcount; i++) {
|
|
udb = pUnit->yks[i].order;
|
|
if (udb < 0 || udb >= DATABASE_YK_NUM) continue;
|
|
pYK = &database.yks[udb];
|
|
act = (BYTE)pYK->state;
|
|
result = (BYTE)pYK->result;
|
|
if (bSlave) { //slave mode
|
|
if (pYK->op_unit != uid) continue;
|
|
switch (act) {
|
|
case YKS_SELED:
|
|
if (result == YKR_SUCC || result == YKR_FAIL) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_EXEED:
|
|
if (result == YKR_SUCC || result == YKR_FAIL) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_ABRED:
|
|
return TRUE;
|
|
case YKS_EXEING:
|
|
if (result == YKR_OVER) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_SELREQ:
|
|
if (result == YKR_OVER) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_SELING:
|
|
if (result == YKR_OVER) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_ABRREQ:
|
|
case YKS_EXEREQ:
|
|
case YKS_IDLE:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
else { //master mode
|
|
switch (act) {
|
|
case YKS_SELREQ:
|
|
if (pYK->result == YKR_IDLE) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_ABRREQ:
|
|
return TRUE;
|
|
case YKS_EXEREQ:
|
|
if (pYK->result == YKR_IDLE) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_SELING:
|
|
case YKS_SELED:
|
|
if (pYK->result == YKR_OVER) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_IDLE:
|
|
case YKS_EXEING:
|
|
case YKS_EXEED:
|
|
case YKS_ABRED:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
int CProcess::GetUnitYXPointByParam(int uid, BYTE* pParam, int count) const
|
|
{
|
|
int i;
|
|
int len;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
len = (count > MAX_UNIT_POINT_PARAM_SIZE) ? MAX_UNIT_POINT_PARAM_SIZE : count;
|
|
if (pUnit->yxcount <= 0) return -1;
|
|
|
|
for (i = 0; i < pUnit->yxcount; i++)
|
|
{
|
|
if (memcmp(pUnit->yxs[i].m_param, pParam, len) == 0)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYCPointByParam(int uid, BYTE* pParam, int count) const
|
|
{
|
|
int i;
|
|
int len;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
len = (count > MAX_UNIT_POINT_PARAM_SIZE) ? MAX_UNIT_POINT_PARAM_SIZE : count;
|
|
if (pUnit->yccount <= 0) return -1;
|
|
for (i = 0; i < pUnit->yccount; i++)
|
|
{
|
|
if (memcmp(pUnit->ycs[i].m_param, pParam, len) == 0)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYMPointByParam(int uid, BYTE* pParam, int count) const
|
|
{
|
|
int i;
|
|
int len;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
len = (count > MAX_UNIT_POINT_PARAM_SIZE) ? MAX_UNIT_POINT_PARAM_SIZE : count;
|
|
if (pUnit->ymcount <= 0) return -1;
|
|
for (i = 0; i < pUnit->ymcount; i++)
|
|
{
|
|
if (memcmp(pUnit->yms[i].m_param, pParam, len) == 0)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYKPointByParam(int uid, BYTE* pParam, int count) const
|
|
{
|
|
int i;
|
|
int len;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
len = (count > MAX_UNIT_POINT_PARAM_SIZE) ? MAX_UNIT_POINT_PARAM_SIZE : count;
|
|
if (pUnit->ykcount <= 0) return -1;
|
|
for (i = 0; i < pUnit->ykcount; i++)
|
|
{
|
|
if (memcmp(pUnit->yks[i].m_param, pParam, len) == 0)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYTPointByParam(int uid, BYTE* pParam, int count) const
|
|
{
|
|
int i;
|
|
int len;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
len = (count > MAX_UNIT_POINT_PARAM_SIZE) ? MAX_UNIT_POINT_PARAM_SIZE : count;
|
|
if (pUnit->ytcount <= 0) return -1;
|
|
for (i = 0; i < pUnit->ytcount; i++)
|
|
{
|
|
if (memcmp(pUnit->yts[i].m_param, pParam, len) == 0)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetProcessIDByIRN(QLONG IRN) const
|
|
{
|
|
int i;
|
|
for (i = 0; i < PROCESSES_NUM; i++)
|
|
{
|
|
if (config.processes[i].irn == IRN)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYXPointByIRN(int uid, QLONG IRN) const
|
|
{
|
|
int i;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
if (pUnit->yxcount <= 0) return -1;
|
|
|
|
for (i = 0; i < pUnit->yxcount; i++)
|
|
{
|
|
if (pUnit->yxs[i].irn == IRN)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYCPointByIRN(int uid, QLONG IRN) const
|
|
{
|
|
int i;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
if (pUnit->yccount <= 0) return -1;
|
|
|
|
for (i = 0; i < pUnit->yccount; i++)
|
|
{
|
|
if (pUnit->ycs[i].irn == IRN)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYMPointByIRN(int uid, QLONG IRN) const
|
|
{
|
|
int i;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
if (pUnit->ymcount <= 0) return -1;
|
|
|
|
for (i = 0; i < pUnit->ymcount; i++)
|
|
{
|
|
if (pUnit->yms[i].irn == IRN)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYKPointByIRN(int uid, QLONG IRN) const
|
|
{
|
|
int i;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
if (pUnit->ykcount <= 0) return -1;
|
|
|
|
for (i = 0; i < pUnit->ykcount; i++)
|
|
{
|
|
if (pUnit->yks[i].irn == IRN)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYTPointByIRN(int uid, QLONG IRN) const
|
|
{
|
|
int i;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
if (pUnit->ytcount <= 0) return -1;
|
|
|
|
for (i = 0; i < pUnit->ytcount; i++)
|
|
{
|
|
if (pUnit->yts[i].irn == IRN)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitByAddr(BYTE* pAddr, int count) const
|
|
{
|
|
int i;
|
|
int uid;
|
|
int len;
|
|
struUnit* pUnit;
|
|
|
|
if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return -1;
|
|
len = count; if (len < 0) return -1;
|
|
if (len > MAX_UNIT_ADDR_SIZE) len = MAX_UNIT_ADDR_SIZE;
|
|
for (i = 0; i < PROCESS_UNIT_NUM; i++)
|
|
{
|
|
uid = config.processes[m_nProcess].units[i];
|
|
if (uid < 0 || uid >= UNIT_NUM) break;
|
|
pUnit = &config.units[uid];
|
|
if (memcmp(pUnit->addr, pAddr, len) == 0)
|
|
{
|
|
return uid;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
BOOLEAN CProcess::GetUnitAddr(int uid, BYTE* pAddr, int count) const
|
|
{
|
|
int len;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM || count <= 0) return FALSE;
|
|
pUnit = &config.units[uid];
|
|
len = (count > MAX_UNIT_ADDR_SIZE) ? MAX_UNIT_ADDR_SIZE : count;
|
|
memcpy(pAddr, pUnit->addr, len);
|
|
return TRUE;
|
|
}
|
|
|
|
void CProcess::SetUnitYXBW(int uid, int point, BYTE value, unionCP56Time& st, int type)
|
|
{
|
|
int udb;
|
|
BYTE qds;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return;
|
|
if (point < 0 || point >= pUnit->yxcount) return;
|
|
udb = pUnit->yxs[point].order;
|
|
qds = GetUnitYXQDS(uid, point);
|
|
if (pUnit->yxs[point].invert) {
|
|
value = !value;
|
|
}
|
|
yxbw.PushYXBW(st, udb, value, qds, uid, point, type);
|
|
}
|
|
|
|
int CProcess::GetUnitYXBW(int uid, BOOLEAN& value, BYTE& qds) const
|
|
{
|
|
int i;
|
|
int order;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return -1;
|
|
|
|
while (yxbw.GetYXBW(pUnit->yxbwload, order, value, qds))
|
|
{
|
|
pUnit->yxbwload++;
|
|
for (i = 0; i < pUnit->yxcount; i++)
|
|
{
|
|
if (pUnit->yxs[i].order == order)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CProcess::GetUnitYXBW(int uid, BOOLEAN& value, BYTE& qds, int& type, unionCP56Time& st) const
|
|
{
|
|
int i;
|
|
int order;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return -1;
|
|
|
|
while (yxbw.GetYXBW(pUnit->yxbwload, st, order, value, qds, type))
|
|
{
|
|
pUnit->yxbwload++;
|
|
for (i = 0; i < pUnit->yxcount; i++)
|
|
{
|
|
if (pUnit->yxs[i].order == order)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void CProcess::ClearUnitYXBW(int uid)
|
|
{
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return;
|
|
|
|
pUnit->yxbwload = yxbw.GetSavePos();
|
|
}
|
|
|
|
void CProcess::SetUnitSOE(int uid, int point, BOOLEAN value, unionCP56Time& st)
|
|
{
|
|
int udb;
|
|
BYTE qds;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return;
|
|
if (point < 0 || point >= pUnit->yxcount) return;
|
|
udb = pUnit->yxs[point].order;
|
|
qds = GetUnitYXQDS(uid, point);
|
|
soe.PushSOE(st, udb, value, qds, uid, point);
|
|
}
|
|
|
|
int CProcess::GetUnitSOE(int uid, BOOLEAN& value, BYTE& qds, unionCP56Time& st) const
|
|
{
|
|
int i;
|
|
int order;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return -1;
|
|
while (soe.GetSOE(pUnit->soeload, st, order, value, qds))
|
|
{
|
|
pUnit->soeload++;
|
|
for (i = 0; i < pUnit->yxcount; i++) {
|
|
if (pUnit->yxs[i].order == order)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
float CProcess::GetUnitYCRealFromValue(int uid, int order, long value) const
|
|
{
|
|
int udb;
|
|
float coef;
|
|
float base;
|
|
struUnit* pUnit;
|
|
struUnitYC* pYC;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return 0;
|
|
if (order < 0 || order >= pUnit->yccount) return 0;
|
|
pYC = &pUnit->ycs[order];
|
|
udb = pYC->order;
|
|
if (udb < 0 || udb >= DATABASE_YC_NUM) {
|
|
coef = 1.0f;
|
|
base = 0.0f;
|
|
}
|
|
else {
|
|
// coef = database.ycs[udb].coef;
|
|
// base = database.ycs[udb].base;
|
|
coef = pYC->coef;
|
|
base = pYC->base;
|
|
}
|
|
return (float)(value * coef + base);
|
|
}
|
|
|
|
|
|
float CProcess::GetUnitYCCoef(int uid, int order) const
|
|
{
|
|
int udb;
|
|
float coef = 1.0f;
|
|
struUnit* pUnit;
|
|
struUnitYC* pYC;
|
|
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return 0;
|
|
if (order < 0 || order >= pUnit->yccount) return 0;
|
|
pYC = &pUnit->ycs[order];
|
|
udb = pYC->order;
|
|
if (udb < 0 || udb >= DATABASE_YC_NUM)
|
|
{
|
|
coef = 1.0f;
|
|
}
|
|
else
|
|
{
|
|
// coef = database.ycs[udb].coef;
|
|
coef = pYC->coef;
|
|
}
|
|
|
|
//此处防止系数位0
|
|
if (fabsf(coef) <= 1E-8) coef = 1.0f;
|
|
return coef;
|
|
}
|
|
|
|
float CProcess::GetUnitYCBase(int uid, int order) const
|
|
{
|
|
int udb;
|
|
float base = 0.0f;
|
|
struUnit* pUnit;
|
|
struUnitYC* pYC;
|
|
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return 0;
|
|
if (order < 0 || order >= pUnit->yccount) return 0;
|
|
pYC = &pUnit->ycs[order];
|
|
udb = pYC->order;
|
|
if (udb < 0 || udb >= DATABASE_YC_NUM)
|
|
{
|
|
base = 0.0f;
|
|
}
|
|
else
|
|
{
|
|
// base = database.ycs[udb].base;
|
|
base = pYC->base;
|
|
}
|
|
|
|
//此处防止系数位0
|
|
if (fabsf(base) < 1E-8) base = 0.0f;
|
|
return base;
|
|
}
|
|
|
|
int CProcess::GetUnitYCBW(int uid, LONG& value, BYTE& qds, int& type, unionCP56Time& st) const
|
|
{
|
|
int i;
|
|
int order;
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return -1;
|
|
|
|
while (ycbw.GetYCBW(pUnit->ycbwload, st, order, value, qds, type))
|
|
{
|
|
pUnit->ycbwload++;
|
|
for (i = 0; i < pUnit->yccount; i++)
|
|
{
|
|
if (pUnit->ycs[i].order == order)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void CProcess::ClearUnitYCBW(int uid)
|
|
{
|
|
struUnit* pUnit;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return;
|
|
|
|
pUnit->ycbwload = ycbw.GetSavePos();
|
|
}
|
|
|
|
float CProcess::GetUnitYMRealFromValue(int uid, int order, DWORD value) const
|
|
{
|
|
int udb;
|
|
float coef;
|
|
float base;
|
|
struUnit* pUnit;
|
|
struUnitYM* pYM;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return 0;
|
|
if (order < 0 || order >= pUnit->yccount) return 0;
|
|
pYM = &pUnit->yms[order];
|
|
udb = pYM->order;
|
|
if (udb < 0 || udb >= DATABASE_YM_NUM)
|
|
{
|
|
coef = 1.0f;
|
|
base = 0.0f;
|
|
}
|
|
else
|
|
{
|
|
coef = pYM->coef;
|
|
base = pYM->base;
|
|
}
|
|
return (float)((long)value * coef + base);
|
|
}
|
|
|
|
float CProcess::GetUnitYMCoef(int uid, int order) const
|
|
{
|
|
int udb;
|
|
float coef = 1.0f;
|
|
struUnit* pUnit;
|
|
struUnitYM* pYM;
|
|
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return 0;
|
|
if (order < 0 || order >= pUnit->yccount) return 0;
|
|
pYM = &pUnit->yms[order];
|
|
udb = pYM->order;
|
|
if (udb < 0 || udb >= DATABASE_YM_NUM)
|
|
{
|
|
coef = 1.0f;
|
|
}
|
|
else
|
|
{
|
|
// coef = database.yms[udb].coef;
|
|
coef = pYM->coef;
|
|
}
|
|
|
|
//此处防止系数位0
|
|
if (fabsf(coef) < 1E-8) coef = 1.0f;
|
|
return coef;
|
|
}
|
|
|
|
float CProcess::GetUnitYMBase(int uid, int order) const
|
|
{
|
|
int udb;
|
|
float base = 1.0f;
|
|
struUnit* pUnit;
|
|
struUnitYM* pYM;
|
|
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return 0;
|
|
if (order < 0 || order >= pUnit->yccount) return 0;
|
|
pYM = &pUnit->yms[order];
|
|
udb = pYM->order;
|
|
if (udb < 0 || udb >= DATABASE_YM_NUM)
|
|
{
|
|
base = 0.0f;
|
|
}
|
|
else
|
|
{
|
|
// base = database.yms[udb].base;
|
|
base = pYM->base;
|
|
}
|
|
|
|
//此处防止系数位0
|
|
if (fabsf(base) < 1E-8) base = 0.0f;
|
|
return base;
|
|
}
|
|
|
|
BOOLEAN CProcess::GetUnitYK(int uid, int& order, BYTE& value, BYTE& act, BYTE& result, BOOLEAN bAsSlave)
|
|
{
|
|
int i;
|
|
int udb;
|
|
BOOLEAN bSlave;
|
|
BOOLEAN bMaster;
|
|
struUnit* pUnit;
|
|
struYK* pYK;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
|
bSlave = IsSlave();
|
|
if (IsDuplex() && bAsSlave) bSlave = TRUE;
|
|
bMaster = IsMaster();
|
|
if (IsDuplex() && !bAsSlave) bMaster = TRUE;
|
|
if (!bSlave && !bMaster) return FALSE;
|
|
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
|
for (i = 0; i < pUnit->ykcount; i++) {
|
|
udb = pUnit->yks[i].order;
|
|
if (udb < 0 || udb >= DATABASE_YK_NUM) continue;
|
|
pYK = &database.yks[udb];
|
|
order = i;
|
|
value = pYK->value;
|
|
act = (BYTE)pYK->state;
|
|
result = (BYTE)pYK->result;
|
|
if (bSlave) { //slave mode
|
|
if (pYK->op_unit != uid) continue;
|
|
switch (act) {
|
|
case YKS_SELED:
|
|
if (result == YKR_SUCC) {
|
|
pYK->result = YKR_OPER;
|
|
yklog.PushYKLog(system32.now, udb, value, YKT_SELRET, YKS_PROC, uid);
|
|
return TRUE;
|
|
}
|
|
else if (result == YKR_FAIL) {
|
|
pYK->state = YKS_IDLE;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->op_unit = -1;
|
|
return TRUE;
|
|
}
|
|
else if (result == YKR_IDLE) {
|
|
pYK->state = YKS_IDLE;
|
|
pYK->op_unit = -1;
|
|
}
|
|
break;
|
|
case YKS_EXEED:
|
|
pYK->state = YKS_IDLE;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->op_unit = -1;
|
|
if (result == YKR_SUCC || result == YKR_FAIL) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_ABRED:
|
|
pYK->state = YKS_IDLE;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->op_unit = -1;
|
|
return TRUE;
|
|
case YKS_EXEING:
|
|
if (result == YKR_OVER) {
|
|
pYK->state = YKS_IDLE;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->op_unit = -1;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_SELREQ:
|
|
if (result == YKR_OVER) {
|
|
pYK->state = YKS_IDLE;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->op_unit = -1;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_SELING:
|
|
if (result == YKR_OVER) {
|
|
pYK->state = YKS_IDLE;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->op_unit = -1;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_ABRREQ:
|
|
case YKS_EXEREQ:
|
|
case YKS_IDLE:
|
|
break;
|
|
default:
|
|
pYK->state = YKS_IDLE;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->op_unit = -1;
|
|
break;
|
|
}
|
|
}
|
|
else { //master mode
|
|
switch (act) {
|
|
case YKS_SELREQ:
|
|
if (pYK->result == YKR_IDLE) {
|
|
pYK->state = YKS_SELING;
|
|
pYK->result = YKR_IDLE;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_ABRREQ:
|
|
pYK->state = YKS_IDLE;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->op_unit = -1;
|
|
return TRUE;
|
|
case YKS_EXEREQ:
|
|
if (pYK->result == YKR_IDLE) {
|
|
pYK->state = YKS_EXEING;
|
|
pYK->result = YKR_IDLE;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_SELING:
|
|
case YKS_SELED:
|
|
if (pYK->result == YKR_OVER) {
|
|
pYK->state = YKS_ABRED;
|
|
pYK->result = YKR_OVER;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YKS_IDLE:
|
|
case YKS_EXEING:
|
|
case YKS_EXEED:
|
|
case YKS_ABRED:
|
|
break;
|
|
default:
|
|
pYK->state = YKS_IDLE;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->op_unit = -1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
void CProcess::SetUnitYK(int uid, int order, BYTE value, BYTE act, BYTE result, BOOLEAN bAsSlave)
|
|
{
|
|
int udb;
|
|
BOOLEAN bSlave;
|
|
BOOLEAN bMaster;
|
|
struUnit* pUnit;
|
|
struYK* pYK;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return;
|
|
bSlave = IsSlave(); if (IsDuplex() && bAsSlave) bSlave = TRUE;
|
|
bMaster = IsMaster(); if (IsDuplex() && !bAsSlave) bMaster = TRUE;
|
|
if (!bSlave && !bMaster) return;
|
|
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return;
|
|
if (order < 0 || order >= pUnit->ykcount) return;
|
|
udb = pUnit->yks[order].order;
|
|
if (udb < 0 || udb >= DATABASE_YK_NUM) return;
|
|
pYK = &database.yks[udb];
|
|
if (bSlave) {
|
|
switch (act) {
|
|
case YKS_SELREQ:
|
|
if (pYK->state == YKS_IDLE /*&& pYK->op_unit == -1*/) {
|
|
pYK->state = act;
|
|
pYK->result = YKR_IDLE;
|
|
pYK->value = value;
|
|
pYK->op_time = system32.timers;
|
|
pYK->op_unit = uid;
|
|
yklog.PushYKLog(system32.now, udb, value, YKT_SELREQ, YKS_PROC, uid);
|
|
}
|
|
break;
|
|
case YKS_ABRREQ:
|
|
if (pYK->op_unit != uid) break;
|
|
if (pYK->value != value) break;
|
|
if (pYK->state != YKS_IDLE && pYK->state != YKS_EXEED) {
|
|
pYK->state = act;
|
|
pYK->result = YKR_IDLE;
|
|
yklog.PushYKLog(system32.now, udb, value, YKT_ABRREQ, YKS_PROC, uid);
|
|
}
|
|
break;
|
|
case YKS_EXEREQ:
|
|
if (pYK->op_unit != uid) break;
|
|
if (pYK->value != value) break;
|
|
if (pYK->state == YKS_SELED && pYK->result == YKR_OPER) {
|
|
pYK->state = act;
|
|
pYK->result = YKR_IDLE;
|
|
yklog.PushYKLog(system32.now, udb, value, YKT_EXEREQ, YKS_PROC, uid);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
else {
|
|
if (pYK->value != value) return;
|
|
switch (pYK->state) {
|
|
case YKS_SELING:
|
|
if (act == YKS_SELED) {
|
|
pYK->state = act;
|
|
pYK->result = result;
|
|
return;
|
|
}
|
|
break;
|
|
case YKS_EXEING:
|
|
if (act == YKS_EXEED) {
|
|
pYK->state = act;
|
|
pYK->result = result;
|
|
return;
|
|
}
|
|
break;
|
|
case YKS_IDLE:
|
|
case YKS_SELED:
|
|
case YKS_ABRREQ:
|
|
case YKS_EXEREQ:
|
|
case YKS_EXEED:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// yt start //
|
|
//////////////////////////////////////////////////////////////////////////
|
|
BOOLEAN CProcess::GetUnitYT(int uid, BOOLEAN bAsSlave)
|
|
{
|
|
int i;
|
|
int udb;
|
|
BYTE act, result;
|
|
BOOLEAN bSlave;
|
|
BOOLEAN bMaster;
|
|
struUnit* pUnit;
|
|
struYT* pYT;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
|
bSlave = IsSlave(); if (IsDuplex() && bAsSlave) bSlave = TRUE;
|
|
bMaster = IsMaster(); if (IsDuplex() && !bAsSlave) bMaster = TRUE;
|
|
if (!bSlave && !bMaster) return FALSE;
|
|
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
|
|
|
for (i = 0; i < pUnit->ytcount; i++) {
|
|
udb = pUnit->yts[i].order;
|
|
if (udb < 0 || udb >= DATABASE_YT_NUM) continue;
|
|
pYT = &database.yts[udb];
|
|
act = (BYTE)pYT->state;
|
|
result = (BYTE)pYT->result;
|
|
if (bSlave) { //slave mode
|
|
if (pYT->op_unit != uid) continue;
|
|
switch (act) {
|
|
case YTS_SELED:
|
|
if (result == YTR_SUCC || result == YTR_FAIL) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_EXEED:
|
|
if (result == YTR_SUCC || result == YTR_FAIL) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_ABRED:
|
|
return TRUE;
|
|
case YTS_EXEING:
|
|
if (result == YTR_OVER) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_SELREQ:
|
|
if (result == YTR_OVER) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_SELING:
|
|
if (result == YTR_OVER) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_ABRREQ:
|
|
case YTS_EXEREQ:
|
|
case YTS_IDLE:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
else { //master mode
|
|
switch (act) {
|
|
case YTS_SELREQ:
|
|
if (pYT->result == YTR_IDLE) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_ABRREQ:
|
|
return TRUE;
|
|
case YTS_EXEREQ:
|
|
if (pYT->result == YTR_IDLE) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_SELING:
|
|
case YTS_SELED:
|
|
if (pYT->result == YTR_OVER) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_IDLE:
|
|
case YTS_EXEING:
|
|
case YTS_EXEED:
|
|
case YTS_ABRED:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
BOOLEAN CProcess::GetUnitYT(int uid, int& order, DWORD& value, BYTE& act, BYTE& result, BOOLEAN bAsSlave)
|
|
{
|
|
int i;
|
|
int udb;
|
|
BOOLEAN bSlave;
|
|
BOOLEAN bMaster;
|
|
struUnit* pUnit;
|
|
struYT* pYT;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
|
bSlave = IsSlave(); if (IsDuplex() && bAsSlave) bSlave = TRUE;
|
|
bMaster = IsMaster(); if (IsDuplex() && !bAsSlave) bMaster = TRUE;
|
|
if (!bSlave && !bMaster) return FALSE;
|
|
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
|
for (i = 0; i < pUnit->ytcount; i++) {
|
|
udb = pUnit->yts[i].order;
|
|
if (udb < 0 || udb >= DATABASE_YT_NUM) continue;
|
|
pYT = &database.yts[udb];
|
|
order = i;
|
|
value = (DWORD)pYT->value;
|
|
act = (BYTE)pYT->state;
|
|
result = (BYTE)pYT->result;
|
|
if (bSlave) { //slave mode
|
|
if (pYT->op_unit != uid) continue;
|
|
switch (act) {
|
|
case YTS_SELED:
|
|
if (result == YTR_SUCC) {
|
|
pYT->result = YTR_OPER;
|
|
ytlog.PushYTLog(system32.now, udb, value, YTT_SELRET, YTS_PROC, uid);
|
|
return TRUE;
|
|
}
|
|
else if (result == YTR_FAIL) {
|
|
pYT->state = YTS_IDLE;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->op_unit = -1;
|
|
return TRUE;
|
|
}
|
|
else if (result == YTR_IDLE) {
|
|
pYT->state = YTS_IDLE;
|
|
pYT->op_unit = -1;
|
|
}
|
|
break;
|
|
case YTS_EXEED:
|
|
pYT->state = YTS_IDLE;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->op_unit = -1;
|
|
if (result == YTR_SUCC || result == YTR_FAIL) {
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_ABRED:
|
|
pYT->state = YTS_IDLE;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->op_unit = -1;
|
|
return TRUE;
|
|
case YTS_EXEING:
|
|
if (result == YTR_OVER) {
|
|
pYT->state = YTS_IDLE;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->op_unit = -1;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_SELREQ:
|
|
if (result == YTR_OVER) {
|
|
pYT->state = YTS_IDLE;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->op_unit = -1;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_SELING:
|
|
if (result == YTR_OVER) {
|
|
pYT->state = YTS_IDLE;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->op_unit = -1;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_ABRREQ:
|
|
case YTS_EXEREQ:
|
|
case YTS_IDLE:
|
|
break;
|
|
default:
|
|
pYT->state = YTS_IDLE;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->op_unit = -1;
|
|
break;
|
|
}
|
|
}
|
|
else { //master mode
|
|
switch (act) {
|
|
case YTS_SELREQ:
|
|
if (pYT->result == YTR_IDLE) {
|
|
pYT->state = YTS_SELING;
|
|
pYT->result = YTR_IDLE;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_ABRREQ:
|
|
pYT->state = YTS_IDLE;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->op_unit = -1;
|
|
return TRUE;
|
|
case YTS_EXEREQ:
|
|
if (pYT->result == YTR_IDLE) {
|
|
pYT->state = YTS_EXEING;
|
|
pYT->result = YTR_IDLE;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_SELING:
|
|
case YTS_SELED:
|
|
if (pYT->result == YTR_OVER) {
|
|
pYT->state = YTS_ABRED;
|
|
pYT->result = YTR_OVER;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
case YTS_IDLE:
|
|
case YTS_EXEING:
|
|
case YTS_EXEED:
|
|
case YTS_ABRED:
|
|
break;
|
|
default:
|
|
pYT->state = YTS_IDLE;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->op_unit = -1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
void CProcess::SetUnitYT(int uid, int order, DWORD value, BYTE act, BYTE result, BOOLEAN bAsSlave)
|
|
{
|
|
int udb;
|
|
BOOLEAN bSlave;
|
|
BOOLEAN bMaster;
|
|
struUnit* pUnit;
|
|
struYT* pYT;
|
|
|
|
if (uid < 0 || uid >= UNIT_NUM) return;
|
|
bSlave = IsSlave(); if (IsDuplex() && bAsSlave) bSlave = TRUE;
|
|
bMaster = IsMaster(); if (IsDuplex() && !bAsSlave) bMaster = TRUE;
|
|
if (!bSlave && !bMaster) return;
|
|
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return;
|
|
if (order < 0 || order >= pUnit->ytcount) return;
|
|
udb = pUnit->yts[order].order;
|
|
if (udb < 0 || udb >= DATABASE_YT_NUM) return;
|
|
pYT = &database.yts[udb];
|
|
if (bSlave) {
|
|
switch (act) {
|
|
case YTS_SELREQ:
|
|
if (pYT->state == YTS_IDLE /*&& pYT->op_unit == -1*/) {
|
|
pYT->state = act;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->value = value;
|
|
pYT->op_time = system32.timers;
|
|
pYT->op_unit = uid;
|
|
ytlog.PushYTLog(system32.now, udb, value, YTT_SELREQ, YTS_PROC, uid);
|
|
}
|
|
break;
|
|
case YTS_ABRREQ:
|
|
if (pYT->op_unit != uid) break;
|
|
if (pYT->value != (long)value) break;
|
|
if (pYT->state != YTS_IDLE && pYT->state != YTS_EXEED) {
|
|
pYT->state = act;
|
|
pYT->result = YTR_IDLE;
|
|
ytlog.PushYTLog(system32.now, udb, value, YTT_ABRREQ, YTS_PROC, uid);
|
|
}
|
|
break;
|
|
case YTS_EXEREQ:
|
|
if ((pYT->state == YTS_SELED || pYT->state == YTS_IDLE)) {
|
|
pYT->state = act;
|
|
pYT->result = YTR_IDLE;
|
|
pYT->value = value;
|
|
pYT->op_time = system32.timers;
|
|
pYT->op_unit = uid;
|
|
ytlog.PushYTLog(system32.now, udb, value, YTT_EXEREQ, YTS_PROC, uid);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
else {
|
|
if (pYT->value != (long)value) return;
|
|
switch (pYT->state) {
|
|
case YTS_SELING:
|
|
if (act == YTS_SELED) {
|
|
pYT->state = act;
|
|
pYT->result = result;
|
|
return;
|
|
}
|
|
break;
|
|
case YTS_EXEING:
|
|
if (act == YTS_EXEED) {
|
|
pYT->state = act;
|
|
pYT->result = result;
|
|
return;
|
|
}
|
|
break;
|
|
case YTS_IDLE:
|
|
case YTS_SELED:
|
|
case YTS_ABRREQ:
|
|
case YTS_EXEREQ:
|
|
case YTS_EXEED:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// yt end //
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
BOOLEAN CProcess::FetchUnitYX(int order)
|
|
{
|
|
int i;
|
|
int uid;
|
|
int oid;
|
|
struUnit* pUnit;
|
|
|
|
if (order < 0 || order >= PROCESS_UNIT_NUM) return FALSE;
|
|
uid = config.processes[m_nProcess].units[order];
|
|
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
|
for (i = 0; i < pUnit->yxcount; i++)
|
|
{
|
|
oid = pUnit->yxs[i].order;
|
|
if (oid < 0 || oid >= DATABASE_YX_NUM) continue;
|
|
pUnit->yxs[i].qds = database.yxs[oid].qds;
|
|
if (pUnit->yxs[i].value != database.yxs[oid].value)
|
|
{
|
|
pUnit->yxs[i].value = database.yxs[oid].value;
|
|
pUnit->yxs[i].update_time = database.yxs[oid].update_time;
|
|
pUnit->yxs[i].yxbw = TRUE;
|
|
}
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOLEAN CProcess::FetchUnitYC(int order)
|
|
{
|
|
int i;
|
|
int uid;
|
|
int oid;
|
|
struUnit* pUnit;
|
|
|
|
if (order < 0 || order >= PROCESS_UNIT_NUM) return FALSE;
|
|
uid = config.processes[m_nProcess].units[order];
|
|
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
|
for (i = 0; i < pUnit->yccount; i++)
|
|
{
|
|
oid = pUnit->ycs[i].order;
|
|
if (oid < 0 || oid >= DATABASE_YC_NUM) continue;
|
|
pUnit->ycs[i].qds = database.ycs[oid].qds;
|
|
if (pUnit->ycs[i].value != database.ycs[oid].value)
|
|
{
|
|
if (pUnit->ycs[i].change_pos >= 0)
|
|
{
|
|
if (abs(pUnit->ycs[i].value - database.ycs[oid].value) >= pUnit->ycs[i].change_pos)
|
|
{ //40码值变化量认为是遥测变位
|
|
pUnit->ycs[i].value = database.ycs[oid].value;
|
|
pUnit->ycs[i].update_time = database.ycs[oid].update_time;
|
|
pUnit->ycs[i].ycbw = TRUE;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOLEAN CProcess::FetchUnitYM(int order)
|
|
{
|
|
int i;
|
|
int uid;
|
|
int oid;
|
|
struUnit* pUnit;
|
|
|
|
if (order < 0 || order >= PROCESS_UNIT_NUM) return FALSE;
|
|
uid = config.processes[m_nProcess].units[order];
|
|
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
|
pUnit = &config.units[uid];
|
|
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
|
for (i = 0; i < pUnit->ymcount; i++)
|
|
{
|
|
oid = pUnit->yms[i].order;
|
|
if (oid < 0 || oid >= DATABASE_YM_NUM) continue;
|
|
if (pUnit->yms[i].value != database.yms[oid].value)
|
|
{
|
|
pUnit->yms[i].value = database.yms[oid].value;
|
|
pUnit->yms[i].update_time = database.yms[oid].update_time;
|
|
}
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOLEAN CProcess::OnPreCreate(int id)
|
|
{
|
|
int i;
|
|
int uid;
|
|
|
|
if (id < 0 || id >= PROCESSES_NUM)
|
|
{
|
|
vLog(LOG_ERROR, "OnPreCreate error! and id = %d\n", id);
|
|
return FALSE;
|
|
}
|
|
m_nProcess = id;
|
|
|
|
for (i = 0; i < PROCESS_UNIT_NUM; i++)
|
|
{
|
|
uid = GetUnitID(i);
|
|
if (uid < 0 || uid >= UNIT_NUM) continue;
|
|
if (NULL == m_pItems[i])
|
|
{
|
|
m_pItems[i] = CreateItem(i);
|
|
}
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
BOOLEAN CProcess::Create(int id)
|
|
{
|
|
if (!OnPreCreate(id))
|
|
{
|
|
return FALSE;
|
|
}
|
|
m_nCurUnit = -1;
|
|
GetNextUnit();
|
|
|
|
if (!OnCreated(id))
|
|
{
|
|
return FALSE;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
BOOLEAN CProcess::OnCreated(int id)
|
|
{
|
|
m_bRunFlag = TRUE;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_attr_init(&attr);
|
|
pthread_attr_setstacksize(&attr, MEMERY_1M);
|
|
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
|
|
//pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
if (pthread_create(&m_pid, &attr, main_run_process, this) < 0)
|
|
{
|
|
vLog(LOG_ERROR, "create main_run_process error(%d,%s).\n", errno, strerror(errno));
|
|
return FALSE;
|
|
}
|
|
#if 1
|
|
char name[16];
|
|
snprintf(name, 16, "%s", config.processes[id].name);
|
|
pthread_setname_np(m_pid, name);
|
|
#endif
|
|
pthread_attr_destroy(&attr);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void CProcess::Destroy(void)
|
|
{
|
|
m_bRunFlag = FALSE;
|
|
|
|
vLog(LOG_DEBUG, "waiting for protocol thread %d destroy.\n", m_nProcess);
|
|
pthread_join(m_pid, NULL);
|
|
|
|
int i = 0;
|
|
for (i = 0; i < PROCESS_UNIT_NUM; i++)
|
|
{
|
|
DestroyItem(i, FALSE);
|
|
}
|
|
|
|
vLog(LOG_DEBUG, "current protocol thread [%d] is destroyed.\n", m_nProcess);
|
|
}
|
|
|
|
BOOLEAN CProcess::Run(void)
|
|
{
|
|
if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return FALSE;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOLEAN CProcess::OnTimer(void)
|
|
{
|
|
return TRUE;
|
|
}
|
|
|
|
int CProcess::OnPackageReceived(BYTE* pBuf, int count, int uid)
|
|
{
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
//CProcessItem类实现
|
|
CProcessItem *CProcess::CreateItem(int ord)
|
|
{
|
|
return (new CProcessItem);
|
|
}
|
|
|
|
void CProcess::DestroyItem(int ord, BOOLEAN bDeleted)
|
|
{
|
|
if (ord < 0 || ord >= PROCESS_UNIT_NUM) {
|
|
return;
|
|
}
|
|
if (!bDeleted) {
|
|
CProcessItem* pItem = m_pItems[ord];
|
|
if (pItem) {
|
|
delete pItem;
|
|
}
|
|
}
|
|
m_pItems[ord] = NULL;
|
|
}
|
|
|