Helpful C macros for checking validity of OM values

From Cassandra

Jump to: navigation, search

The following C macros are courtesy of Alex Johnson of Invensys and make checking the validity of an OM object much easier. Simply include this header file in your source listing and use the macros to check the status of the OM value.

### om_macros.h ###

#ifndef OMMACROS_H

#define OMMACROS_H

#define BOOLEAN 5
#define LEGAL_OM_NAME_CHARS \
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_:."
#define isLegalChars(x) (!(strcspn(x, LEGAL_OM_NAME_CHARS)))

/* Define the characters used in an OM Name to delimit parts of it*/
#define OM_DELIMS ":."


/* Define the length of the longest expected OM string */
#define OM_STR_LENGTH 256


#define OM_SCANNING 0x0020
#define OM_BAD      0x0100 /* Bit  8 */
#define OM_SECURED  0x0200 /* Bit  9 */
#define OM_ACK      0x0400 /* Bit 10 */
#define OM_OOS      0x0800 /* Bit 11 */
#define OM_SHADOW   0x1000 /* Bit 12 */
#define OM_LLO      0x2000 /* Bit 13 */
#define OM_LHI      0x4000 /* Bit 14 */
#define OM_ERROR    0x8000 /* Bit 15 */

/* This macro gets the object's type */
#define getType(x)         (x & 0x001f)

/* These macros check the status word returned by the OM via omread
*/
#define isCHARACTER(x)    ((x & 0x001f) == CHARACTER)
#define isINTEGER(x)      ((x & 0x001f) == INTEGER)
#define isFLOAT(x)        ((x & 0x001f) == FLOAT)
#define isSTRING(x)       ((x & 0x001f) == STRING)
#define isBOOLEAN(x)      ((x & 0x001f) == BOOLEAN)
#define isLONGINT(x)      ((x & 0x001f) == OM_LNG_INT)
#define isSHORTINT(x)     ((x & 0x001f) == CIO_SHORT)
#define isPKBOOL(x)       ((x & 0x001f) == OM_S_PKBOL)
#define isPKLONG(x)       ((x & 0x001f) == OM_L_PKBOL)
#define isBadType(x)      (!isCHARACTER(x) && !isINTEGER(x) && \
                           !isFLOAT(x)     && !isSTRING(x)  && \
                           !isLONGINT(x)   && !isBOOLEAN(x) && \
                           !isSHORTINT(x)  && !isPKBOOL(x)  && \
                           !isPKLONG(x) \
                          )
#define isNoResp(x)       (((x & 0x00e0) >> 5) == 0)
#define isBeingScanned(x) (((x & 0x00e0) >> 5) == 1)
#define isDisconnected(x) (((x & 0x00e0) >> 5) == 2)
#define isDeleted(x)      (((x & 0x00e0) >> 5) == 3)
#define isBadConnType(x)  (((x & 0x00e0) >> 5) == 4)
#define isNotSent(x)      (((x & 0x00e0) >> 5) == 7)
#define isBad(x)          ((x & OM_BAD)        == OM_BAD)
#define isSecured(x)      ((x & OM_SECURED)    == OM_SECURED)
#define isAck(x)          ((x & OM_ACK)        == OM_ACK)
#define isOOS(x)          ((x & OM_OOS)        == OM_OOS)
#define isShadow(x)       ((x & OM_SHADOW)     == OM_SHADOW)
#define isLLO(x)          ((x & OM_LLO)        == OM_LLO)
#define isLHI(x)          ((x & OM_LHI)        == OM_LHI)
#define isError(x)        ((x & OM_ERROR)      == OM_ERROR)


#define isInvalidCBP(x)      (isBadType(x) || \
                           isNoResp(x) ||  \
                           isDisconnected(x) || \
                           isDeleted(x) || \
                           isBadConnType(x) || \
                           isNotSent(x) || \
                           isBad(x) || \
                           isOOS(x))
#define isValidCBP(x)      (!isInvalidCBP(x))

#define isInvalidSV(x)    (isBadType(x) || \
                           isNoResp(x) ||  \
                           isDisconnected(x) || \
                           isDeleted(x) || \
                           isBadConnType(x) || \
                           isNotSent(x) || \
                           isBad(x) || \
                           isOOS(x))
#define isValidSV(x)       (!isInvalidSV(x))

#define isValid(cpb, x) (strchr(cpb, ':') ? isValidCBP(x) :
isValidSV(x))
#define isInvalid(cpb, x) \
   (strchr(cpb, ':') ? isInvalidCBP(x) : isInvalidSInv(x))

#define setBad(x)        (x |= OM_BAD)
#define setAck(x)        (x |= OM_ACK)
#define setOOS(x)        (x |= OM_OOS)
#define setSecured(x)    (x |= OM_SECURED)
#define setError(x)      (x |= OM_ERROR)

#define resetBad(x)      (x &= ~OM_BAD)
#define resetAck(x)      (x &= ~OM_ACK)
#define resetOOS(x)      (x &= ~OM_OOS)
#define resetSecured(x)  (x &= ~OM_SECURED)

static int sDataTypeSizes[]=
{
     /*  0              */ 0
   , /*  1 CHARACTER    */ sizeof(char)
   , /*  2 INTEGER      */ sizeof(int)
   , /*  3 FLOAT        */ sizeof(float)
   , /*  4 STRING       */ OM_STR_LENGTH
   , /*  5 OM_BOOL      */ sizeof(char)
   , /*  6 OM_LNG_INT   */ sizeof(long)
   , /*  7              */ 0
   , /*  8 OM_SHORT_INT */ sizeof(char)
   , /*  9 OM_S_PKBOL   */ sizeof(int)
   , /* 10 OM_L_PKBOL   */ sizeof(long)
};

#endif


Sun Studio 11 Notes

When migrating to the Sun Studio 11 C compiler, ran into a problem with the Foxboro header file om_udat.h (typically found in /usr/include/fox). Specifically, the structure called value seems to be a reserved word.

Edit the om_udat.h file and rename this structure, remembering to update the function prototypes that also use this structure. Also, any C programs referencing this structure will have to be updated with the new name as well.

### om_udat.h ###
struct fox_value {
     int             index;
     unsigned int    status;
     union type_val  uval;
};

extern int dqchange   (int, int, int *, int, struct fox_value *, int *);
extern int dqlist     (int, int, int, int, struct fox_value *, int *);
extern int omread     (int, int, struct fox_value *);
extern int omwrite    (int, int, struct fox_value *);
Personal tools