?? udldcfg.c
字號:
/* $Header: "%n Ver=%v %f LastEdit=%w Locker=%l" */
/* "UDLDCFG.C Ver=3 20-Nov-97,17:41:02 LastEdit=JIMV Locker=***_NOBODY_***" */
/***********************************************************************\
* *
* Copyright Wonderware Software Development Corp. 1992-1997 *
* *
* ThisFileName="L:\ww\dde_serv\src\udsample\udldcfg.c" *
* LastEditDate="1997 Nov 20 17:41:00" *
* *
\***********************************************************************/
/*
* Functions that build and manipulate data structures required by the
* driver.
*/
#define LINT_ARGS
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>
#include <stdlib.h>
#include "ntconv.h"
#include "hmemcpy.h"
#include "udprot.h"
#include "uddefs.h"
#include "protocol.h"
#include "debug.h"
#include "wwassert.h"
#include "udgetstr.h"
#include "wwcomdlg.h"
#include "ntsrvr.h"
#include "strval.h"
#include "Utitech.h"
#include "CheckItem.h"
USES_ASSERT
#define MAX_MSG_SIZE 100
#define MAX_PTS 22
#define FOLDMIN 2
extern CHAIN PortList;
extern HWND hWndParent;
extern BOOL bBldSendMsgInBinary;
/***********************************************************************/
static unsigned long WINAPI FindNextActive(LPSTAT lpTopic, SYMPTR lpSymEnt, LPUDMSG lpMsg);
static unsigned long WINAPI FindPrevActive(LPSTAT lpTopic, SYMPTR lpSymEnt, LPUDMSG lpMsg);
LPHVOID FAR AllocateHeapArray (LPEXTARRAY array);
LPHVOID FAR ExtendHeapArray (LPEXTARRAY array);
BOOL FAR DeleteHeapArray (LPEXTARRAY array);
/***********************************************************************/
/** add a port to the list of ports **/
void
WINAPI
UdprotLinkPORT (LPPORT lpNewPort)
{
if (lpNewPort != (LPPORT) NULL)
AppendItemAtTail (&PortList, (LPCHAINLINK) lpNewPort);
} /* UdprotLinkPORT */
/***********************************************************************/
/** Set up port
This function is called one time per port, by ProtAllocateLogicalDevice()
to allocate and initialize the hardware resources.
Returns pointer if successful, NULL otherwise **/
LPPORT
WINAPI
UdprotSetupPort(LPSTNPARAM lpStnParam)
{
LPPORT lpPort;
/* attempt to allocate space for port structure */
lpPort = (LPPORT) wwHeap_AllocPtr(hHeap,
GMEM_MOVEABLE | GMEM_ZEROINIT,
(DWORD) sizeof(PORT));
if (lpPort == (LPPORT) NULL) {
MessageBox(GetFocus(),
GetString(STRUSER + 65) /* "Can't allocate port struct" */ ,
GetAppName(), MB_OK);
return (LPPORT) NULL;
}
/* set port name */
ExtractPortName (lpStnParam, lpPort->mbPortName, sizeof(lpPort->mbPortName));
/* get configuration info for port */
SetPortConfigInfo (lpPort, lpStnParam);
/* attempt to start up port */
if (!OpenPort(lpPort)) {
/* start up failed, display error code, free port structure */
UdprotFreePort(lpPort);
return (LPPORT) NULL;
}
/* Initialize port state, chain it in */
lpPort->mbState = PROT_IDLE;
UdprotLinkPORT(lpPort);
return lpPort;
} /* UdprotSetupPort */
/***********************************************************************/
/* check whether port name matches indicated string */
BOOL FAR PortNameMatch (LPCHAINLINK chain_link, void FAR *comparisonValue)
{
BOOL status;
LPSTR st;
LPPORT lpPort;
/* initialize return value */
status = FALSE;
/* get pointers and perform string comparison */
lpPort = (LPPORT) chain_link;
st = (LPSTR) comparisonValue;
if (lstrcmp (lpPort->mbPortName, st) == 0)
status = TRUE;
/* indicate whether match was found */
return (status);
} /* PortNameMatch */
/***********************************************************************/
/** find port with indicated name
returns pointer if found, NULL otherwise */
LPPORT
WINAPI
UdprotFindPort(LPSTNPARAM lpStnParam)
{
LPPORT lpPort;
char portName[33];
CHAINSCANNER port_scanner;
ExtractPortName (lpStnParam, portName, sizeof(portName));
/* search for port with indicated name */
lpPort = (LPPORT) FindFirstItem (&PortList,
SCAN_FROM_HEAD, /* search forward */
PortNameMatch, /* check if name matches */
(LPSTR) portName, /* comparison value */
&port_scanner); /* used for FindNextItem */
/* return pointer to port or NULL if not found */
return (lpPort);
} /* UdprotFindPort */
/***********************************************************************/
/* check whether station ID matches indicated value */
BOOL FAR StationIDMatch (LPCHAINLINK chain_link, void FAR *comparisonValue)
{
BOOL status;
IDLDEV id;
LPSTAT lpTopic;
/* initialize return value */
status = FALSE;
/* get pointer, value and perform comparison */
lpTopic = (LPSTAT) chain_link;
id = *((IDLDEV FAR *) comparisonValue);
if (lpTopic->statIdLogDev == id)
status = TRUE;
/* indicate whether match was found */
return (status);
} /* StationIDMatch */
/***********************************************************************/
/** find station on indicated port with indicated logical device ID
returns pointer if found, NULL otherwise **/
LPSTAT
WINAPI
UdprotFindTopic(LPPORT lpPort, IDLDEV id)
{
LPSTAT lpTopic;
CHAINSCANNER topic_scanner;
/* search for station with indicated ID */
lpTopic = (LPSTAT) FindFirstItem (&lpPort->mbTopicList,
SCAN_FROM_HEAD, /* search forward */
StationIDMatch, /* check if ID matches */
(IDLDEV FAR *) &id, /* comparison value */
&topic_scanner); /* used for FindNextItem */
/* return pointer to station or NULL if not found */
return (lpTopic);
} /* UdprotFindTopic */
/***********************************************************************/
/** allocate a new station on indicated port
return pointer if successful, NULL otherwise */
LPSTAT
WINAPI
UdprotAllocateTopic(LPPORT lpPort)
{
LPSTAT lpTopic;
/* attempt to allocate space for station structure */
lpTopic = (LPSTAT) wwHeap_AllocPtr(hHeap,
GMEM_MOVEABLE | GMEM_ZEROINIT,
(DWORD) sizeof(STAT));
if (lpTopic != (LPSTAT) NULL) {
/* initialize station parameters */
lpTopic->statPort = lpPort;
lpTopic->statHdbStatus = (HDB)NULL;
lpTopic->statStatusActive = FALSE;
lpTopic->statStatusDue = FALSE;
lpTopic->statFailed = FALSE;
lpTopic->statRetries = TOPIC_CONSEC_FAILURE_LIMIT;
lpTopic->statPortRetries = TOPIC_NORMAL_RETRIES;
lpTopic->statDelay = 0L;
/* clear the link with the list of stations */
ClearChainLink (&lpTopic->statChainLink);
/* initialize the symbol table extensible array */
InitializeExtArray (&lpTopic->statSymTab,
SYMTABQUANTUM, /* number to start with */
sizeof (SYMENT), /* size of a symbol entry */
SYMTABQUANTUM, /* number to extend each time */
AllocateHeapArray, ExtendHeapArray, DeleteHeapArray);
/* initialize the used symbol index extensible array */
InitializeExtArray (&lpTopic->statSymInd,
SYMTABQUANTUM, /* number to start with */
sizeof (unsigned long), /* size of a symbol index */
SYMTABQUANTUM, /* number to extend each time */
AllocateHeapArray, ExtendHeapArray, DeleteHeapArray);
/* clear the symbol chains to empty */
InitializeChain (&lpTopic->statSymUsed);
InitializeChain (&lpTopic->statSymUnused);
/* append new station to list of stations on this port */
AppendItemAtTail (&lpPort->mbTopicList, (LPCHAINLINK) lpTopic);
/* if this is the only station so far, use as current station */
if (lpPort->mbTopicList.item_count == 1)
lpPort->mbCurTopic = lpTopic;
}
/* return pointer or NULL if unable to allocate new station */
return (lpTopic);
} /* UdprotAllocateTopic */
/***********************************************************************/
/** set up station on indicated port with indicated ID **/
LPSTAT
WINAPI
UdprotSetupTopic(LPPORT lpPort,
LPSTNPARAM lpTopicParam,
IDLDEV idLogDev,
LPSTR lpszTopic)
{
LPSTAT lpTopic;
/* find station on this port with indicated ID, if any */
lpTopic = UdprotFindTopic(lpPort, idLogDev);
if (lpTopic == (LPSTAT) NULL) {
/* no such station found, attempt to create a new one */
lpTopic = UdprotAllocateTopic(lpPort);
}
if (lpTopic == (LPSTAT) NULL) {
/* unable to find or create topic, return NULL and exit */
return (LPSTAT) NULL;
}
/* initialize station parameters */
lpTopic->statTopic = lpTopicParam->spTopicID;
lpTopic->statUpdatePeriod = lpTopicParam->spUpdatePeriod;
lpTopic->statReplyTimeout = lpTopicParam->spReplyTimeout * 1000;
lpTopic->statIdLogDev = idLogDev;
lpTopic->statRetries = TOPIC_CONSEC_FAILURE_LIMIT;
lpTopic->statPortRetries = TOPIC_NORMAL_RETRIES;
lpTopic->statDelay = 0L;
lstrcpy (lpTopic->statTopicName, lpszTopic);
/************************************************************\
Any driver dependent fields should be initialized here as
required.
\************************************************************/
lpTopic->statCoilReadSize = (WORD) lpTopicParam->spCoilReadSize;
lpTopic->statRegReadSize = (WORD) lpTopicParam->spRegReadSize;
/* return pointer to station */
return lpTopic;
} /* UdprotSetupTopic */
/***********************************************************************/
/** perform logical address comparison between two symbol table entries
returns -1, 0, +1 for <, =, > **/
int
WINAPI
LogicalAddrCmp(SYMPTR a, SYMPTR b)
{
/* compare device data types */
if (a->msPlcDataType < b->msPlcDataType)
return -1;
if (a->msPlcDataType > b->msPlcDataType)
return 1;
/* data types same, compare addresses */
if (a->msAddr1 < b->msAddr1)
return -1;
if (a->msAddr1 > b->msAddr1)
return 1;
/* addresses same, compare bit positions */
if (a->msBitPos < b->msBitPos)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -