PTErr_t TpSetImplicitAttr ( PTRefNum_t PTRefNum) { KpChar_t attribute[256]; PTErr_t errnum; fut_hdr_p futHdr; chan_hdr_p chan; KpInt32_t i1, i2, attributeTag, numOutVar, numInVar[FUT_NCHAN]; KpHandle_t PTHdr; errnum = PTSetAttribute (PTRefNum, KCM_TECH_TYPE, KCM_FUT_S); PTHdr = getPTHdr (PTRefNum); /* get the header info */ futHdr = (fut_hdr_p)lockBuffer (PTHdr); if (futHdr != NULL) { /* set the technology version attribute */ KpItoa (futHdr->version, attribute); errnum = PTSetAttribute (PTRefNum, KCM_TECH_VERSION, attribute); /* get the number of active output channels */ /* and the number of active inputs for each channel */ for (i1 = 0; i1 < FUT_NCHAN; i1++) { numInVar[i1] = 0; /* zero out the number of inputs array */ } numOutVar = 0; /* and # of outputs */ switch (futHdr->magic) { case PTTYPE_MFT1: case PTTYPE_MFT2: case PTTYPE_MB2A: case PTTYPE_MA2B: numOutVar = futHdr->icode[1]; /* copy from header */ for (i1 = 0; i1 < numOutVar; i1++) { numInVar[i1] = futHdr->icode[0]; } break; default: /* PTTYPE_FUTF */ for (i1 = 0, chan = futHdr->chan; i1 < FUT_NCHAN; i1++, chan++) { if ((chan->gcode & FUTIO_CODE) != FUTIO_NULL) { numOutVar++; for (i2 = 0; i2 < FUT_NCHAN; i2++) { if ((chan->icode[i2] & FUTIO_CODE) != FUTIO_NULL) { (numInVar[i1])++; } } } } break; } /* set the attribute for number of inputs for each channnel */ for (i1 = 0, attributeTag = KCM_NUM_IN_VAR_1; i1<FUT_NCHAN; i1++) { if (numInVar[i1] != 0) { KpItoa (numInVar[i1], attribute); PTSetAttribute (PTRefNum, attributeTag, attribute); attributeTag++; } } /* set the number of output channnels attribute */ KpItoa (numOutVar, attribute); errnum = PTSetAttribute (PTRefNum, KCM_NUM_OUT_VAR, attribute); if ( ! unlockBuffer (PTHdr)) { errnum = KCP_MEM_UNLOCK_ERR; } } return errnum; }
/* PTGetSizeF calculates the size of a PT in any format. */ PTErr_t PTGetSizeF (PTRefNum_t PTRefNum, PTType_t format, KpInt32_p mBlkSize) { PTErr_t errnum, errnum1; KpInt32_t extSize, intSize; KpHandle_t PTHdr, PTData; PTRefNum_t matrixPTRefNum; KpUInt32_t lutConfig; #if !defined KCP_ICC_ONLY KpHandle_t PTAttr; #endif #if !defined KCMS_NO_CRC KpChar_t strCRCmade[KCM_MAX_ATTRIB_VALUE_LENGTH+1]; KpInt32_t crc32; #endif errnum = getPTStatus (PTRefNum); if ((errnum == KCP_PT_ACTIVE) || (errnum == KCP_PT_INACTIVE) || (errnum == KCP_SERIAL_PT)) { if (mBlkSize == NULL) return (KCP_BAD_PTR); switch (format) { #if !defined KCP_ICC_ONLY case PTTYPE_FUTF: extSize = KCP_PT_HEADER_SIZE; /* size of external header */ break; #endif case PTTYPE_MFT1: case PTTYPE_MFT2: case PTTYPE_MFT2_VER_0: extSize = (2 * sizeof (KpInt32_t)) + (4 * sizeof (KpUInt8_t)) + (MF_MATRIX_DIM * MF_MATRIX_DIM * sizeof (KpInt32_t)); break; case PTTYPE_MAB1: case PTTYPE_MAB2: case PTTYPE_MBA1: case PTTYPE_MBA2: extSize = (7 * sizeof (KpInt32_t)) + (2 * sizeof (KpUInt8_t)) + sizeof (KpUInt16_t); errnum1 = getMatrixPTRefNum (PTRefNum, &matrixPTRefNum, &lutConfig); if (KCP_SUCCESS == errnum1) { extSize += ((MF_MATRIX_DIM * MF_MATRIX_DIM + MF_MATRIX_DIM) * sizeof (KpInt32_t)); } break; default: return (KCP_INVAL_PTTYPE); } if ((errnum == KCP_PT_ACTIVE) || (errnum == KCP_SERIAL_PT)) { /* when active, add size of external PT data block */ PTHdr = getPTHdr (PTRefNum); PTData = getPTData (PTRefNum); intSize = TpGetDataSize (PTHdr, PTData, format); if (intSize == 0) { PTRefNum_t resizePTRefNum; /* TpGetDataSize will return 0 if the grid table dimensions are not valid for the format specified. PTGetPTF will attempt to resize the grid table of the PT. Check if that resizing is possible */ errnum = gridDimValid (format, PTRefNum, &resizePTRefNum); if (errnum != KCP_SUCCESS) { return errnum; } /* Determine the size of the resized PT */ PTHdr = getPTHdr (resizePTRefNum); PTData = getPTData (resizePTRefNum); intSize = TpGetDataSize (PTHdr, PTData, format); PTCheckOut (resizePTRefNum); if (intSize == 0) { return KCP_INCON_PT; } } extSize += intSize; /* add size of data */ #if !defined KCMS_NO_CRC switch (format) { case PTTYPE_FUTF: errnum = TpCalCrc (PTHdr, PTData, &crc32); if (errnum == KCP_SUCCESS) { KpItoa(crc32, strCRCmade); PTSetAttribute(PTRefNum, KCM_CRC, strCRCmade); } break; default: break; } #endif } /* add size of attributes. Must be done after CRC calculation */ #if !defined KCP_ICC_ONLY switch (format) { case PTTYPE_FUTF: PTAttr = getPTAttr (PTRefNum); extSize += getAttrSize(PTAttr); /* plus size of attributes */ break; default: break; } #endif *mBlkSize = extSize; /* return external size of PT */ errnum = KCP_SUCCESS; } return (errnum); }
/* PTGetPTF writes a PT to external memory in a variety of formats. */ PTErr_t PTGetPTF ( PTRefNum_t PTRefNum, PTType_t format, KpInt32_t mBlkSize, PTAddr_t PTAddr) { PTErr_t errnum, PTstatus; KpHandle_t PTHdr, PTAttr, PTData; KpFd_t fd; KpInt32_t attrSize, resultSize, nBytes; PTRefNum_t resizePTRefNum = 0, thePTRefNum; KpChar_p memData; errnum = getPTStatus (PTRefNum); if ((errnum == KCP_PT_ACTIVE) || (errnum == KCP_PT_INACTIVE) || (errnum == KCP_SERIAL_PT)) { #if defined KCP_DIAG_LOG {KpChar_t string[256]; sprintf (string, "\nPTGetPTF\n PTRefNum %x, format %x, mBlkSize %d, PTAddr %x\n", PTRefNum, format, mBlkSize, PTAddr); kcpDiagLog (string);} #endif PTstatus = errnum; /* verify the dimensions of the grid table are valid for the specified format. */ errnum = gridDimValid (format, PTRefNum, &resizePTRefNum); if (errnum != KCP_SUCCESS) { goto ErrOut1; } if (resizePTRefNum != 0) { thePTRefNum = resizePTRefNum; /* resized PT made */ } else { thePTRefNum = PTRefNum; /* use original PT */ } /* determine the size the resized PT */ errnum = PTGetSizeF (thePTRefNum, format, &resultSize); if (errnum != KCP_SUCCESS) { goto ErrOut1; } if (resultSize > mBlkSize) { /* PT may not be larger than the buffer size */ errnum = KCP_PT_BLOCK_TOO_SMALL; goto ErrOut1; } PTAttr = getPTAttr (thePTRefNum); PTHdr = getPTHdr (thePTRefNum); PTData = getPTData (thePTRefNum); /* initialize memory file manager to write the PT */ if (KpOpen (NULL, "m", &fd, NULL, (KpGenericPtr_t)PTAddr, mBlkSize) != KCMS_IO_SUCCESS) { errnum = KCP_SYSERR_1; goto ErrOut1; } attrSize = getAttrSize (PTAttr); /* get size of attributes */ errnum = TpWriteHdr (&fd, format, PTHdr, attrSize); /* write the header info */ if (errnum != KCP_SUCCESS) { Kp_close (&fd); goto ErrOut1; } #if !defined KCP_ICC_ONLY switch (format) { case PTTYPE_FUTF: errnum = writeAttributes (&fd, PTAttr); /* write the attributes */ if (errnum != KCP_SUCCESS) { break; } default: break; } #endif /* if PT active, write data to external format */ if (((PTstatus == KCP_PT_ACTIVE) || (PTstatus == KCP_SERIAL_PT)) && (errnum == KCP_SUCCESS)) { errnum = TpWriteData (&fd, format, PTHdr, PTData); } (void) Kp_close (&fd); /* if the result PT size is smaller than the memory block size fill the end of the memory block with zeros */ nBytes = mBlkSize - resultSize; if (nBytes > 0) { memData = (KpChar_p)PTAddr + resultSize; while (nBytes--) { *memData++ = 0; } } } ErrOut1: if (resizePTRefNum != 0) { PTCheckOut (resizePTRefNum); } return (errnum); }
/* PTActivate reads the PT data from an external memory block and * sets up the technology specific PT memory structures. Before loading * the PT, check to make sure that it matches the "checked in" info. */ PTErr_t PTActivate( PTRefNum_t PTRefNum, KpInt32_t mBlkSize, PTAddr_t PTAddr) { PTErr_t errnum; KpFd_t fd; KpHandle_t PTHdr, PTData, PTHdr2; fut_hdr_p futp; #if !defined KCMS_NO_CRC KpChar_t strCRCmade[KCM_MAX_ATTRIB_VALUE_LENGTH+1]; KpChar_t strCRCfound[KCM_MAX_ATTRIB_VALUE_LENGTH+1]; KpInt32_t crcAttrSize, crc32; #endif KpInt32_t attrSize; PTType_t format; errnum = getPTStatus (PTRefNum); if (errnum != KCP_PT_INACTIVE) { goto ErrOut1; } /* initialize memory file manager */ if (KpOpen (NULL, "m", &fd, NULL, (KpGenericPtr_t)PTAddr, mBlkSize) != KCMS_IO_SUCCESS) { errnum = KCP_SYSERR_1; goto ErrOut1; } /* read in the encoded header, verify that its the same as the original, then discard */ errnum = TpReadHdr (&fd, &PTHdr2, &format); if (errnum != KCP_SUCCESS ) { goto ErrOut2; } #if defined KCP_DIAG_LOG {KpChar_t string[256]; sprintf (string, "\nPTActivate\n PTRefNum %x, mBlkSize %d, PTAddr %x, format %x\n", PTRefNum, mBlkSize, PTAddr, format); kcpDiagLog (string);} #endif /* get and save size of attributes */ futp = lockBuffer (PTHdr2); attrSize = futp->idstr_len; unlockBuffer (PTHdr2); PTHdr = getPTHdr (PTRefNum); /* get the original PT header */ /* make sure the PT header and checkin info match */ errnum = TpCompareHdr (PTHdr, PTHdr2); (void) TpFreeHdr (PTHdr2); /* free the header */ if (errnum != KCP_SUCCESS) { /* then check for an error in hdrVerify */ goto ErrOut2; } #if !defined KCP_ICC_ONLY switch (format) { case PTTYPE_FUTF: /* discard the attribute info */ if (Kp_skip (&fd, attrSize) != KCMS_IO_SUCCESS){ /* may have been setAttribute after checkin */ errnum = KCP_PTERR_3; goto ErrOut2; } break; default: break; } #endif errnum = TpReadData (&fd, format, PTRefNum, PTHdr, &PTData); /* get the PT data */ if (errnum == KCP_SUCCESS) { if (PTMemTest () == 0) { /* enough memory to continue operations? */ errnum = KCP_NO_ACTIVATE_MEM; goto ErrOut3; } #if !defined KCMS_NO_CRC errnum = TpCalCrc (PTHdr, PTData, &crc32); /* calculate the CRC */ if (errnum == KCP_SUCCESS) { KpItoa(crc32, strCRCmade); crcAttrSize = KCM_MAX_ATTRIB_VALUE_LENGTH; errnum = PTGetAttribute(PTRefNum, KCM_CRC, &crcAttrSize, strCRCfound); if (errnum == KCP_INVAL_PTA_TAG) { /* if not present, just set it */ PTSetAttribute(PTRefNum, KCM_CRC, strCRCmade); errnum = KCP_SUCCESS; } else { /* if ((errnum == KCP_SUCCESS)*/ /* if present, must match */ /* && (strcmp (strCRCmade, strCRCfound) != 0)) { */ /* errnum = KCP_INCON_PT; */ /* goto ErrOut3; */ /* } */ } } #endif } if (errnum == KCP_SUCCESS) { /* Everything's OK, now activate */ makeActive (PTRefNum, PTData); } ErrOut2: (void) Kp_close (&fd); ErrOut1: return (errnum); ErrOut3: (void) TpFreeData (PTData); /* Release the PT memory */ goto ErrOut2; }