📄 mac.c
字号:
/*
** DESCRIPTION
** This program is used to generate an image file (.img) with an Ethernet Address.
** The following algorithm is applied:
** 1. Ethernet.dat file is checked for a matching serial number.
** If a match is found, then the asscociated ethernet addr is rewritten
** into the device.
**
** 2. If ethernet.dat does not contain the serial number, the following occurs:
** 2.1 The serial number is used to search the database.
** 2.2 If the serial number is found - then the EPA is extracted
** from the database record and reused.
** 2.3 If the serial number is NOT found - then the last EPA used is
** extracted from the last record of the database. The last EPA
** is incremented by one and used for the EPA for the new circuit
** pack. The serial number, EPA and date are appended to the
** database. A copy of the database is created for backup purposes.
**
** If a new directory must be setup to handle a different block of ethernet addresses,
** there are 2 things that must be done:
** a) Append the new block's beginning address at the end of ethernet.dat file.
** b) Set the new block's final address test condition
** Search for "Set final address here".
**
** NOTE: ethernet.dat file contains all SERIALNUMBERS and their associated
** assigned ethernet address.
**
**
** HISTORY
** When Who What
** Unknown Unknown Initial Release compiled with Visual C.
** 12/11/00 R.K. Michael Converted to LabWindows CVI code. Removed dependency of
** 4096 records. The ethernet.dat file can grow as large as
** the filesystem will allow.
** 10/19/01 W.C. McManus Modified the routine to use Celestica's SFDM system to send the serial
** number and receive the MAC Address back. A new subroutine
** 'getMacAddressFromSFDM' was added to replace the 'getMacAddress' routine
** which was the OKC method using the ethernet.dat log files.
**
** 02/27/02 W. C. McManus Modified the program for circuit packs that require two (2) MAC Addresses per tracking
number. The SFMI.dll will call the SFDM system to return the MAC Addresses in the following
format string: "00601DNNNNNN 00601DNNNNNN" which will be read into the Spectrum variable
MacAddress. The string will be parsed into two variables that will be passed to the
Spectrum variables MacAddress1 and MacAdress2. These will be used to create the two image
files mac1.img and mac2.img. This was done specifically for the LPA901/911 test.
** @(#) SCCSID %W% %G% %U%
*/
#include <ansi_c.h>
#include <cvirte.h>
#include <userint.h>
#include "clib.l"
#define MAC_FILENAME "Ethernet.dat"
#define IMG_FILENAME1 "mac1.img"
#define IMG_FILENAME2 "mac2.img"
#define SET 1
#define CLEAR 0
#define SUCCESS 0
#define MATCH 0
#define JOBPATH_LENGTH 80
#define ERROR_STRING_LENGTH 80
#define LINE_BUFFER_LENGTH 80
//#define TWO_MAC_ADDRESS_LENGTH 25
#define MAC_ADDRESS_LENGTH 12
#define SERIAL_NUMBER_LENGTH 12
#define PRODUCT_CODE_LENGTH 25
#define DATE_STAMP_LENGTH 25
#define MIN_BOARDNAME_LENGTH 3
#define MIN_JOBPATH_LENGTH 3
// FILE *fopen(); Defined in stdio.h Will be ignored in CVI 5.5.1 amd 6.0 however -wcm 02/28/2002
int getSerialNumber(void),
getJobPath(void),
getBoardName(void),
getLastAddress(void),
getMacAddress(void),
writeImgFile1(void),
writeImgFile2(void),
writeEthernetDatFile(void),
ASC2HEX(char *),
incrementMACaddress(void);
struct MACrecord {
char jobPath[JOBPATH_LENGTH+1];
char MACaddress[MAC_ADDRESS_LENGTH];
char serialNumber[SERIAL_NUMBER_LENGTH+1];
char productCode[PRODUCT_CODE_LENGTH+1];
char dateStamp[DATE_STAMP_LENGTH+1];
char lastMACaddress[MAC_ADDRESS_LENGTH];
char MACaddress1[MAC_ADDRESS_LENGTH];
char MACaddress2[MAC_ADDRESS_LENGTH];
};
struct MACrecord userString;
struct MACrecord table;
//Added for LPA901 and LPA911
struct LPA901_911_MACrecord {
char MACaddress1[MAC_ADDRESS_LENGTH];
char MACaddress2[MAC_ADDRESS_LENGTH];
};
struct LPA901_911_MACrecord table2;
static char macFilename[JOBPATH_LENGTH+25],
imageFilename1[JOBPATH_LENGTH+25],
imageFilename2[JOBPATH_LENGTH+25];
int MACaddress[MAC_ADDRESS_LENGTH] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0},
MACaddress1[MAC_ADDRESS_LENGTH] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0},
MACaddress2[MAC_ADDRESS_LENGTH] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0},
lastMACaddress[MAC_ADDRESS_LENGTH] = {0x0,0x0,0x6,0x0,0x1,0xD,0x2,0x6,0x0,0x3,0x5,0x0};
int preExistFlag = CLEAR,
failFlag = CLEAR;
time_t now;
//
// Main Exported function
//
__declspec(dllexport) getMacAddressFromFile(char **cmd)
{
failFlag = CLEAR;
if(getSerialNumber() != SUCCESS) {
failFlag = SET;
goto mainend;
}
if(getJobPath() != SUCCESS) {
failFlag = SET;
goto mainend;
}
if(getBoardName() != SUCCESS) {
failFlag = SET;
goto mainend;
}
if(getLastAddress() != SUCCESS) {
failFlag = SET;
goto mainend;
}
// This was the OKC method using ethernet.dat
if(getMacAddress() != SUCCESS) {
goto mainend;
}
/* This is the SFDM method in Monterrey.
if(getMacAddressFromSFDM() != SUCCESS) {
goto mainend;
}
*/
mainend:
return(failFlag);
}
//
// Get Job Path
//
int getJobPath()
{
int i,
pathLength;
char errorString[ERROR_STRING_LENGTH];
//
// Init array
//
for(i = 0; i < JOBPATH_LENGTH; i++)
userString.jobPath[i] = '\0';
//
// Get Job Path
//
SPECTRUM_GetUserString("JOBPATH", userString.jobPath, 0, JOBPATH_LENGTH);
//
// Check the length of job path
//
pathLength = strlen(userString.jobPath);
if (pathLength < MIN_JOBPATH_LENGTH) {
sprintf(errorString,"%s\nPath Length Too Short",userString.jobPath);
MessagePopup ("getJobPath()", errorString);
failFlag = SET;
return(failFlag);
}
//
// Replace '-' with '\'
//
for(i = 0; i < pathLength; i++) {
if(userString.jobPath[i] == '-')
userString.jobPath[i] = '\\';
}
strcat(userString.jobPath,"\\ildp\\mac\\");
//
// Define MAC data file
//
sprintf(macFilename,"%s%s\0",userString.jobPath,MAC_FILENAME);
//
// Define MAC image files
//
sprintf(imageFilename1,"%s%s\0",userString.jobPath,IMG_FILENAME1);
sprintf(imageFilename2,"%s%s\0",userString.jobPath,IMG_FILENAME2);
return(SUCCESS);
}
//
// Get UUT Serial Number
//
int getSerialNumber()
{
int i;
char errorString[ERROR_STRING_LENGTH];
//
// Init array
//
for(i = 0; i < SERIAL_NUMBER_LENGTH; i++)
userString.serialNumber[i] = '\0';
//
// Get UUT Serial Number
//
SPECTRUM_GetSystemString(CLIB_SERIALNUMBER,userString.serialNumber,SERIAL_NUMBER_LENGTH);
if(strlen(userString.serialNumber) !=SERIAL_NUMBER_LENGTH)
{
sprintf(errorString,"S/N:%s\nSerialNumber's length is not %d",userString.serialNumber,SERIAL_NUMBER_LENGTH);
MessagePopup ("SerialNumber", errorString);
failFlag = SET;
return(failFlag);
}
return(SUCCESS);
}
//
// Get UUT Product Code Name
//
int getBoardName()
{
int i;
char errorString[ERROR_STRING_LENGTH];
//
// Init array
//
for(i = 0; i < PRODUCT_CODE_LENGTH; i++)
userString.productCode[i] = '\0';
//
// Get Board Name
//
SPECTRUM_GetUserString("BOARDNAME",userString.productCode, 0, PRODUCT_CODE_LENGTH);
//
// Validate Board Name
//
if (strlen(userString.productCode) < MIN_BOARDNAME_LENGTH) {
sprintf(errorString,"%s\n%s",userString.productCode, "NOT A VALID BOARDNAME");
MessagePopup ("getBoardName()", errorString);
failFlag = SET;
return(failFlag);
}
return(SUCCESS);
}
//
// Get Last permissable MAC Address
//
int getLastAddress()
{
int i = 0;
int lastNibble[MAC_ADDRESS_LENGTH] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
char errorString[ERROR_STRING_LENGTH];
//
// Init arrays
//
for(i = 0; i < MAC_ADDRESS_LENGTH; i++) {
userString.lastMACaddress[i] = '\0';
lastMACaddress[i] = '\0';
}
//
// Get max range of MAC assignment
//
SPECTRUM_GetUserString("LASTADDR",userString.lastMACaddress, 0, MAC_ADDRESS_LENGTH);
//
// Validate last MAC address
//
if (strlen(userString.lastMACaddress) != MAC_ADDRESS_LENGTH) {
sprintf(errorString,"%s\n%s"
,userString.lastMACaddress
,"NOT USING A VALID ETHERNET LAST ADDRESS");
MessagePopup ("getLastAddress()", errorString);
failFlag = SET;
return(failFlag);
}
for (i=0 ; i < MAC_ADDRESS_LENGTH ; i++) {
if (userString.lastMACaddress[i] == '0') lastNibble[i]=0x0;
if (userString.lastMACaddress[i] == '1') lastNibble[i]=0x1;
if (userString.lastMACaddress[i] == '2') lastNibble[i]=0x2;
if (userString.lastMACaddress[i] == '3') lastNibble[i]=0x3;
if (userString.lastMACaddress[i] == '4') lastNibble[i]=0x4;
if (userString.lastMACaddress[i] == '5') lastNibble[i]=0x5;
if (userString.lastMACaddress[i] == '6') lastNibble[i]=0x6;
if (userString.lastMACaddress[i] == '7') lastNibble[i]=0x7;
if (userString.lastMACaddress[i] == '8') lastNibble[i]=0x8;
if (userString.lastMACaddress[i] == '9') lastNibble[i]=0x9;
if (userString.lastMACaddress[i] == 'a') lastNibble[i]=0xA;
if (userString.lastMACaddress[i] == 'A') lastNibble[i]=0xA;
if (userString.lastMACaddress[i] == 'b') lastNibble[i]=0xB;
if (userString.lastMACaddress[i] == 'B') lastNibble[i]=0xB;
if (userString.lastMACaddress[i] == 'c') lastNibble[i]=0xC;
if (userString.lastMACaddress[i] == 'C') lastNibble[i]=0xC;
if (userString.lastMACaddress[i] == 'd') lastNibble[i]=0xD;
if (userString.lastMACaddress[i] == 'D') lastNibble[i]=0xD;
if (userString.lastMACaddress[i] == 'e') lastNibble[i]=0xE;
if (userString.lastMACaddress[i] == 'E') lastNibble[i]=0xE;
if (userString.lastMACaddress[i] == 'f') lastNibble[i]=0xF;
if (userString.lastMACaddress[i] == 'F') lastNibble[i]=0xF;
lastMACaddress[i]=lastNibble[i];
}
return(SUCCESS);
}
//
// Get a new or previously assigned MAC address based on the UUT's serial number
//
int getMacAddress()
{
FILE *filePtr;
int index,
itemsFormatted;
char errorString[ERROR_STRING_LENGTH],
lineBuffer[LINE_BUFFER_LENGTH];
//
// Open Ethernet File
//
if ((filePtr = fopen(macFilename, "r")) == (FILE *) NULL) {
sprintf(errorString,"Could not open file:\n'%s'", macFilename);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -