int Copy2Mem43(int portnum, uchar *SerialNum) { uchar rt=FALSE; uchar read_data; owSerialNum(portnum, SerialNum, FALSE); if(owAccess(portnum)) { if (!owWriteBytePower(portnum, COPY_SCRATCH_CMD)) return FALSE; owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write LSB of target addr owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write MSB of target addr owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x1f); //write E/S msDelay(500); owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); if (read_data == 0xaa) rt=TRUE; } return rt; }
//---------------------------------------------------------------------- // Search for devices // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'FamilySN' - an array of all the serial numbers with the matching // family code // 'family_code' - the family code of the devices to search for on the // 1-Wire Net // 'MAXDEVICES' - the maximum number of devices to look for with the // family code passed. // // Returns: TRUE(1) success, device type found // FALSE(0) device not found // SMALLINT FindDevices(int portnum, uchar FamilySN[][8], SMALLINT family_code, int MAXDEVICES) { int NumDevices=0; // find the devices // set the search to first find that family code owFamilySearchSetup(portnum,family_code); // loop to find all of the devices up to MAXDEVICES NumDevices = 0; do { // perform the search if (!owNext(portnum,TRUE, FALSE)) break; owSerialNum(portnum,FamilySN[NumDevices], TRUE); if ((FamilySN[NumDevices][0] & 0x7F) == (family_code & 0x7F)) { NumDevices++; } } while (NumDevices < (MAXDEVICES - 1)); // check if not at least 1 device return NumDevices; }
//------------------------------------------------------------------------- // Creates a random challenge using the SHA engine of the given // coprocessor. // // 'copr' - Structure for holding coprocessor information. // 'pagenum' - pagenumber to use for calculation. // 'chlg' - 3-byte return buffer for challenge. // 'offset' - offset into resulting MAC to pull challenge data from. // // Return: TRUE - create challenge succeeded. // FALSE - an error occurred. // SMALLINT CreateChallenge(SHACopr* copr, SMALLINT pageNum, uchar* chlg, SMALLINT offset) { uchar scratchpad[32]; int addr = pageNum << 5; uchar es = 0x1F; int start = 8; if(offset>0 && offset<17) start += offset; //set the serial number owSerialNum(copr->portnum, copr->devAN, FALSE); OWASSERT( EraseScratchpadSHA18(copr->portnum, addr, FALSE), OWERROR_ERASE_SCRATCHPAD_FAILED, FALSE ); OWASSERT( SHAFunction18(copr->portnum, SHA_COMPUTE_CHALLENGE, addr, TRUE), OWERROR_SHA_FUNCTION_FAILED, FALSE ); OWASSERT( ReadScratchpadSHA18(copr->portnum, &addr, &es, scratchpad, TRUE), OWERROR_READ_SCRATCHPAD_FAILED, FALSE ); memcpy(chlg,&scratchpad[start],3); return TRUE; }
//-------------------------------------------------------------------------- // The 'MissionThermo' starts a new Thermochron mission on 'SerialNum' // from the state information provided in 'ThermoState'. Progress output // is printed to the specified file 'fp'. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'SerialNum' - Device serial number to download // 'ThermoState' - pointer to a structure type that holds the raw and // translated Thermochron data. // 'fp' - file pointer to print status information to // // Returns: TRUE (1) : Thermochron missioned // FALSE (0): not missioned. Abort due to repeated errors // or user keypress. // int MissionThermo(int portnum, uchar *SerialNum, ThermoStateType *ThermoState, FILE *fp) { // set the serial num owSerialNum(portnum, SerialNum, FALSE); // run the script and mission thermochron return RunThermoScript(portnum,ThermoState,Mission,fp); }
//---------------------------------------------------------------------- // Read the counter on a specified page of a DS2423. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'SerialNum' - Serial Number of DS2423 that contains the counter // to be read // 'CounterPage' - page number that the counter is associated with // 'Count' - pointer to variable where that count will be returned // // Returns: TRUE(1) counter has been read and verified // FALSE(0) could not read the counter, perhaps device is not // in contact // SMALLINT ReadCounter(int portnum, uchar SerialNum[8], int CounterPage, ulong *Count) { uchar rt=FALSE; uchar send_block[30]; uchar send_cnt=0, i; int address; ushort lastcrc16; setcrc16(portnum,0); // set the device serial number to the counter device owSerialNum(portnum,SerialNum,FALSE); // access the device if (owAccess(portnum)) { // create a block to send that reads the counter // read memory and counter command send_block[send_cnt++] = 0xA5; lastcrc16 = docrc16(portnum,0xA5); // address of last data byte before counter address = (CounterPage << 5) + 31; // (1.02) send_block[send_cnt++] = (uchar)(address & 0xFF); lastcrc16 = docrc16(portnum,(ushort)(address & 0xFF)); send_block[send_cnt++] = (uchar)(address >> 8); lastcrc16 = docrc16(portnum,(ushort)(address >> 8)); // now add the read bytes for data byte,counter,zero bits, crc16 for (i = 0; i < 11; i++) send_block[send_cnt++] = 0xFF; // now send the block if (owBlock(portnum,FALSE,send_block,send_cnt)) { // perform the CRC16 on the last 11 bytes of packet for (i = send_cnt - 11; i < send_cnt; i++) lastcrc16 = docrc16(portnum,send_block[i]); // verify CRC16 is correct if (lastcrc16 == 0xB001) { // success rt = TRUE; // extract the counter value *Count = 0; for (i = send_cnt - 7; i >= send_cnt - 10; i--) { *Count <<= 8; *Count |= send_block[i]; } } } } // return the result flag rt return rt; }
//-------------------------------------------------------------------------- // The 'DownloadThermo' downloads the specified Thermochron in 'SerialNum' // and puts the data in the state variable 'ThermoState'. Progress output // is printed to the specified file 'fp'. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'SerialNum' - Device serial number to download // 'ThermoState' - pointer to a structure type that holds the raw and // translated Thermochron data. // 'fp' - file pointer to print status information to // // Returns: TRUE (1) : Thermochron download with raw data in ThermoState // FALSE (0): not downloaded. Abort due to repeated errors // or user keypress. // int DownloadThermo(int portnum, uchar *SerialNum, ThermoStateType *ThermoState, FILE *fp) { // set the serial num owSerialNum(portnum, SerialNum, FALSE); // run the script and download thermochron return RunThermoScript(portnum,ThermoState,Download,fp); }
//-------------------------------------------------------------------------- // The 'ReadThermoStatus' reads the Thermochron status in 'SerialNum' // and puts the data in the state variable 'ThermoState'. Progress output // is printed to the specified file 'fp'. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'SerialNum' - Device serial number to download // 'ThermoState' - pointer to a structure type that holds the raw and // translated Thermochron data. // 'fp' - file pointer to print status information to // // Returns: TRUE (1) : Thermochron status read with raw data in ThermoState // FALSE (0): status not read. Abort due to repeated errors // or user keypress. // int ReadThermoStatus(int portnum, uchar *SerialNum, ThermoStateType *ThermoState, FILE *fp) { // set the serial num owSerialNum(portnum, SerialNum, FALSE); // run the script and read status of thermochron return RunThermoScript(portnum,ThermoState,GetStatus,fp); }
int Temp_DoRead(int iSensor) { uchar send_block[30], lastcrc8; int send_cnt, tsht = 0, i, loop = 0; // LED_Set(6); if(iSensor >= NumDevices) return 0; // LED_Set(7); owSerialNum(PORTNUM, TempSensorSN[iSensor], FALSE); for (loop = 0; loop < 2; loop++) { // access the device if (owAccess(PORTNUM)) { // send the convert command and if nesessary start power delivery if (!owWriteByte(PORTNUM, 0x44)) return 0; // access the device if (owAccess(PORTNUM)) { // create a block to send that reads the temperature // read scratchpad command send_cnt = 0; send_block[send_cnt++] = 0xBE; // now add the read bytes for data bytes and crc8 for (i = 0; i < 9; i++) send_block[send_cnt++] = 0xFF; // now send the block if (owBlock(PORTNUM, FALSE, send_block, send_cnt)) { // initialize the CRC8 setcrc8(PORTNUM, 0); // perform the CRC8 on the last 8 bytes of packet for (i = send_cnt - 9; i < send_cnt; i++) lastcrc8 = docrc8(PORTNUM, send_block[i]); // verify CRC8 is correct if (lastcrc8 == 0x00) { // calculate the high-res temperature tsht = send_block[2] << 8; tsht = tsht | send_block[1]; if (tsht & 0x00001000) tsht = tsht | 0xffff0000; if(!(tsht == 1360 && LastTemperature[iSensor] == 0)){ LastTemperature[iSensor] = tsht; }else{ tsht = 0; } // success break; } } } } } return tsht; }
//---------------------------------------------------------------------- // Read and print the Serial Number. // void DisplaySerialNum(int portnum) { uchar serial_num[8]; int i; owSerialNum(portnum,serial_num,TRUE); for (i = 7; i >= 0; i--) printf("%02X",(int)serial_num[i]); printf("\r\n"); }
//---------------------------------------------------------------------- // Read and print the Serial Number. // void PrintSerialNum(int portnum) { uchar TempSerialNumber[8]; int i; owSerialNum(portnum,TempSerialNumber,TRUE); for (i = 7; i >= 0; i--) printf("%02X",TempSerialNumber[i]); printf("\n"); }
// routine for reading the scratchpad of a DS28EC20P EEPROM // 80 pages of 32byte // 32byte scratchpad // expects 32 byte deep buffer int ReadScratch43(int portnum, uchar *SerialNum, uchar *page_buffer) { uchar rt=FALSE; ushort lastcrc16; int i; ushort target_addr = 0; uchar read_data; owSerialNum(portnum, SerialNum, FALSE); if(owAccess(portnum)) { mprintf(" Reading Scratchpad...\n"); if (!owWriteBytePower(portnum, READ_SCRATCH_CMD)) return FALSE; setcrc16(portnum, 0); //init crc docrc16(portnum,(ushort)READ_SCRATCH_CMD); owLevel(portnum,MODE_NORMAL); //read 2 byte address and 1 byte status read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); target_addr = read_data; owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); target_addr |= read_data << 8; owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); mprintf("E/S: 0x%x\n", read_data); for(i = 0; i < 32; i++) { owLevel(portnum,MODE_NORMAL); page_buffer[i] = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, page_buffer[i]); } for(i = 0; i < 2; i++) { owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); } if (lastcrc16 == 0xb001) rt=TRUE; } return rt; }
int Write43(int portnum, uchar *SerialNum, uchar *page_buffer) { uchar rt=FALSE; ushort lastcrc16; int i; owSerialNum(portnum, SerialNum, FALSE); if(owAccess(portnum)) { mprintf(" Writing Scratchpad...\n"); if (!owWriteBytePower(portnum, WRITE_SCRATCH_CMD)) return FALSE; setcrc16(portnum, 0); docrc16(portnum,(ushort)WRITE_SCRATCH_CMD); owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write LSB of target addr docrc16(portnum,(ushort)0x00); owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write MSB of target addr docrc16(portnum,(ushort)0x00); for(i = 0; i < 32; i++) //write 32 data bytes to scratchpad { owLevel(portnum,MODE_NORMAL); owWriteBytePower(portnum, i); lastcrc16 = docrc16(portnum,i); } for(i = 0; i < 2; i++) //read two bytes CRC16 { owLevel(portnum,MODE_NORMAL); lastcrc16 = docrc16(portnum,(ushort)owReadBytePower(portnum)); } mprintf(" CRC16: %x\n", lastcrc16); if(lastcrc16 == 0xb001) { //copy to mem owLevel(portnum, MODE_NORMAL); if(Copy2Mem43(portnum, SerialNum)) rt=TRUE; } } return rt; }
int main(void) { uchar owAddrBytes[8]; uchar writeBuffer[] = {0x64, 0xEE, 0x00, 0x00, 0x00, 0x02, 0x03}; portNum = 0; strcpy(portName,"DS2490-1"); strcpy(serial, "FA00000001743763"); portNum = owAcquireEx(portName); if(portNum <= 0 ) { printf("Error acquiring 1-wire USB device\n"); return 1; } // Convert string-based serial to owAddress if(!checkSerial(serial, owAddrBytes)) { owRelease(portNum); return 1; } // DO STUFF HERE owSerialNum(portNum, owAddrBytes, FALSE); // 1) Test Maxq Read status testMaxQReadStatus(portNum, owAddrBytes); // 2) See what's in the command buffer initially testReadCmdBuffer(portNum, owAddrBytes, 7); // 3) Write something to the command buffer testWriteCmdBufferExStart(portNum, owAddrBytes, writeBuffer, 7); // 4) Test Maxq Read status testMaxQReadStatus(portNum, owAddrBytes); // 5) Read back what we wrote testReadCmdBuffer(portNum, owAddrBytes, 7); // Notice that the write succeeds but the release sequence response fails. owRelease(portNum); printf("Finished Successfully\n"); return 0; }
//---------------------------------------------------------------------- // SUBROUTINE = FindBranchDevices // // This routine will find all the branches on a certain DS2409 device and // will return the serial numbers down that branch // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'Branch[8]' - The serial number of the branch to look down // 'BranchSN[][8]' - The list of serial numbers on the branch // 'MAXDEVICES' - The Max devices on the branch // 'MainBr' - True to search down the main branch, False for Aux. // // Returns: Returns the number of devices found on the branch // int FindBranchDevice(int portnum, uchar Branch[8], uchar BranchSN[][8], int MAXDEVICES, int MainBr) { int NumDevices = 0; short result; result = (short)owBranchFirst(portnum, &Branch[0], FALSE, MainBr); while(result) { owSerialNum(portnum,BranchSN[NumDevices], TRUE); NumDevices++; result = (short)owBranchNext(portnum, &Branch[0], FALSE, MainBr); } return NumDevices; }
/* ds2338mem_rd Reading DS2438 memory returns one page of memory input parameters portnum the port number of the port being used for the 1-Wire Network. SNum the serial number for the part that the read is to be done on. pageno the page number of memory to be read */ int ds2438mem_rd(int portnum, uchar *SNum, uchar *pagemem, uchar pageno, char *device) { int block_cnt; int i; ushort lastcrc8; owSerialNum(portnum,SNum,FALSE); block_cnt = 0; // Recall the Status/Configuration page // Recall command pagemem[block_cnt++] = 0xB8; // Page to Recall pagemem[block_cnt++] = pageno; owAccess(portnum); owBlock(portnum,FALSE,pagemem,block_cnt); syslog(LOG_DEBUG, "ds2438mem_rd: recall memory (B8h %xh): %s\n", pageno, ppagemem(pagemem)); block_cnt = 0; // Read the Status/Configuration byte // Read scratchpad command pagemem[block_cnt++] = 0xBE; // Page for the Status/Configuration byte pagemem[block_cnt++] = pageno; for(i=0;i<9;i++) pagemem[block_cnt++] = 0xFF; owAccess(portnum); owBlock(portnum,FALSE,pagemem,block_cnt); syslog(LOG_DEBUG,"ds2438mem_rd: read scratchpad (BEh %xh): %s \n", pageno, ppagemem( pagemem)); setcrc8(portnum,0); for(i=2;i<block_cnt;i++) { lastcrc8 = docrc8(portnum,pagemem[i]); } if(lastcrc8 != 0x00) { syslog(LOG_ALERT, "ds2438mem_rd: CRC error "); bitprint( lastcrc8, "lastcrc8"); return 1; } return 0; }
//------------------------------------------------------------------------- // Answers a random challenge by performing an authenticated read of the // user's account information. // // 'user' - Structure for holding user token information. // 'chlg' - 3-byte buffer of challenge data. // // Return: the value of the write cycle counter for the account page. // or -1 if there is an error // int AnswerChallenge(SHAUser* user, uchar* chlg) { int addr = user->accountPageNumber << 5; user->writeCycleCounter = -1; memcpy(&user->accountFile[20], chlg, 3); //set the serial number owSerialNum(user->portnum, user->devAN, FALSE); if(user->devAN[0]==0x18) { // for the DS1963S OWASSERT( EraseScratchpadSHA18(user->portnum, addr, FALSE), OWERROR_ERASE_SCRATCHPAD_FAILED, -1 ); OWASSERT( WriteScratchpadSHA18(user->portnum, addr, user->accountFile, 32, TRUE), OWERROR_WRITE_SCRATCHPAD_FAILED, -1 ); user->writeCycleCounter = ReadAuthPageSHA18(user->portnum, user->accountPageNumber, user->accountFile, user->responseMAC, TRUE); } else if(user->devAN[0]==0x33||user->devAN[0]==0xB3) { // for the DS1961S OWASSERT( WriteScratchpadSHA33(user->portnum, addr, &user->accountFile[16], FALSE), OWERROR_WRITE_SCRATCHPAD_FAILED, -1 ); user->writeCycleCounter = ReadAuthPageSHA33(user->portnum, user->accountPageNumber, user->accountFile, user->responseMAC, TRUE); } return user->writeCycleCounter; }
// routine for reading a memory page of a DS28EC20P EEPROM // expects 32 byte deep buffer int ReadMem43(int portnum, uchar *SerialNum, uchar *page_buffer) { uchar rt=FALSE; ushort lastcrc16; int i; uchar read_data; owSerialNum(portnum, SerialNum, FALSE); if(owAccess(portnum)) { if (!owWriteBytePower(portnum, E_READ_MEM_CMD)) return FALSE; setcrc16(portnum, 0); //init crc docrc16(portnum,(ushort)E_READ_MEM_CMD); owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write LSB of target addr docrc16(portnum,(ushort)0x00); owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write MSB of target addr docrc16(portnum,(ushort)0x00); for(i = 0; i < 32; i++) { owLevel(portnum,MODE_NORMAL); page_buffer[i] = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, page_buffer[i]); } for(i = 0; i < 2; i++) { owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); } if (lastcrc16 == 0xb001) rt=TRUE; } return rt; }
//------------------------------------------------------------------------- // Creates a data signature for the given data buffer using the hardware // coprocessor. // // 'copr' - Structure for holding coprocessor information. // 'data' - data written to the data page to sign // 'scratchpad' - data written to the scratchpad to sign // 'signature' - data buffer which is either holding the signature that // must match exactly what is generated on the coprocessor // -or- will hold the resulting signature created by the // coprocessor. // 'readSignature' - implies whether or not the signature buffer // receives the contents of the scratchpad or is used to // match the contents of the scratchpad. If true, // scratchpad contents are read into the signature buffer. // // Return: If TRUE, the user's authentication response matched exactly the // signature generated by the coprocessor or the signature was // successfully copied into the return buffer. // If FALSE, an error occurred or the signature did not match. // SMALLINT CreateDataSignature(SHACopr* copr, uchar* data, uchar* scratchpad, uchar* signature, SMALLINT readSignature) { int addr = copr->signPageNumber << 5; // set the serial number to that of the coprocessor owSerialNum(copr->portnum, copr->devAN, FALSE); OWASSERT( WriteDataPageSHA18(copr->portnum, copr->signPageNumber, data, FALSE), OWERROR_WRITE_DATA_PAGE_FAILED, FALSE ); OWASSERT( WriteScratchpadSHA18(copr->portnum, addr, scratchpad, 32, TRUE), OWERROR_WRITE_SCRATCHPAD_FAILED, FALSE ); OWASSERT( SHAFunction18(copr->portnum, SHA_SIGN_DATA_PAGE, addr, TRUE), OWERROR_SHA_FUNCTION_FAILED, FALSE ); if(readSignature) { OWASSERT( ReadScratchpadSHA18(copr->portnum, 0, 0, scratchpad, TRUE), OWERROR_READ_SCRATCHPAD_FAILED, FALSE ); } else { OWASSERT( MatchScratchpadSHA18(copr->portnum, signature, TRUE), OWERROR_MATCH_SCRATCHPAD_FAILED, FALSE ); } memcpy(signature, &scratchpad[8], 20); return TRUE; }
//---------------------------------------------------------------------- // Read the temperature of a DS18B20 // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'SerialNum' - Serial Number of DS1920/DS1820 to read temperature from // 'Temp ' - pointer to variable where that temperature will be // returned // // Returns: TRUE(1) temperature has been read and verified // FALSE(0) could not read the temperature, perhaps device is not // in contact // int ReadTemperature28(int portnum, uchar *SerialNum, float *Temp) { uchar rt=FALSE; uchar send_block[30],lastcrc8; int send_cnt, tsht, i, loop=0; float tmp,cr,cpc; // set the device serial number to the counter device owSerialNum(portnum,SerialNum,FALSE); for (loop = 0; loop < 2; loop ++) { // access the device if (owAccess(portnum)) { // send the convert command and start power delivery if (!owWriteBytePower(portnum,0x44)) return FALSE; // sleep for 1 second msDelay(1000); // turn off the 1-Wire Net strong pull-up if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL) return FALSE; // access the device if (owAccess(portnum)) { // create a block to send that reads the temperature // read scratchpad command send_cnt = 0; send_block[send_cnt++] = 0xBE; // now add the read bytes for data bytes and crc8 for (i = 0; i < 9; i++) send_block[send_cnt++] = 0xFF; // now send the block if (owBlock(portnum,FALSE,send_block,send_cnt)) { // initialize the CRC8 setcrc8(portnum,0); // perform the CRC8 on the last 8 bytes of packet for (i = send_cnt - 9; i < send_cnt; i++) lastcrc8 = docrc8(portnum,send_block[i]); // verify CRC8 is correct if (lastcrc8 == 0x00) { // calculate the high-res temperature tsht = (send_block[1] >> 4) + ((send_block[2] & 0x07) << 4); if (send_block[2] & 0x80) tsht |= -128; tmp = (float)(tsht); //cr = send_block[7]; //cpc = send_block[8]; //if (((cpc - cr) == 1) && (loop == 0)) // continue; //if (cpc == 0) // return FALSE; //else // tmp = tmp - (float)0.25 + (cpc - cr)/cpc; // TODO: Check config register if(send_block[1] & 0x01) { tmp += 0.0625f; } else if(send_block[1] & 0x02) { tmp += 0.125f; } else if(send_block[1] & 0x04) { tmp += 0.25f; } else if(send_block[1] & 0x08) { tmp += 0.5f; } *Temp = tmp; // success rt = TRUE; break; } } } } }
/* SetupVsens setup DS2438 to read Vsens voltage difference enable IAD, CA and EE of status configuration register ( page <0h> byte <0h>) Vsens A/D conversion occurs with a frequency of 36.41 measurements/sec once IAD is enabled ( set to '1'). No special command necessary. input parameters portnum port number SNum serial number of DS2438 device device (USB DS2490 or serial DS9097U) */ int SetupVsens(int portnum, uchar *SNum, char *device) { uchar datablock[50]; uchar conf_reg = 0x00; int send_cnt = 0; int i; ushort lastcrc8; int busybyte; double ti, tf; struct timezone tz; struct timeval tv; gettimeofday( &tv, &tz); ti = tv.tv_sec+1.0e-6*tv.tv_usec; /* enable IAD, CA and EE of configuration byte */ conf_reg |= IAD | CA | EE; owSerialNum(portnum,SNum,FALSE); // Recall the Status/Configuration page // Recall command datablock[send_cnt++] = 0xB8; // Page to Recall datablock[send_cnt++] = 0x00; if(!owBlock(portnum,FALSE,datablock,send_cnt)) return FALSE; send_cnt = 0; if(owAccess(portnum)) { // Read the Status/Configuration byte // Read scratchpad command datablock[send_cnt++] = 0xBE; // Page for the Status/Configuration byte datablock[send_cnt++] = 0x00; for(i=0;i<9;i++) datablock[send_cnt++] = 0xFF; if(owBlock(portnum,FALSE,datablock,send_cnt)) { setcrc8(portnum,0); for(i=2;i<send_cnt;i++) lastcrc8 = docrc8(portnum,datablock[i]); if(lastcrc8 != 0x00) return FALSE; }//Block else return FALSE; if ( datablock[2] & conf_reg ) { syslog(LOG_DEBUG, "SetupVsens: IAD, CA and EE are set: return!\n"); gettimeofday( &tv, &tz); tf = tv.tv_sec+1.0e-6*tv.tv_usec; syslog(LOG_DEBUG, "SetupVsens: elapsed time: %f\n", tf -ti); return TRUE; } else { syslog(LOG_DEBUG, "SetupVsens: IAD, CA and EE are not set. Continue to setup\n"); } }//Access if(owAccess(portnum)) { send_cnt = 0; // Write the Status/Configuration byte // Write scratchpad command datablock[send_cnt++] = 0x4E; // Write page datablock[send_cnt++] = 0x00; // IAD, CA and EE set to "1" datablock[send_cnt++] |= conf_reg; // do not change the rest for(i=0;i<7;i++) datablock[send_cnt++] = datablock[i+3]; if(owBlock(portnum,FALSE,datablock,send_cnt)) { send_cnt = 0; if(owAccess(portnum)) { // Copy the Status/Configuration byte // Copy scratchpad command datablock[send_cnt++] = 0x48; // Copy page datablock[send_cnt++] = 0x00; if(owBlock(portnum,FALSE,datablock,send_cnt)) { busybyte = owReadByte(portnum); while(busybyte == 0) busybyte = owReadByte(portnum); gettimeofday( &tv, &tz); tf = tv.tv_sec+1.0e-6*tv.tv_usec; syslog(LOG_DEBUG, "SetupVsens: elapsed time: %f\n", tf -ti); return TRUE; }//Block }//Access }//Block }//Access return FALSE; }
//---------------------------------------------------------------------- // Read the temperature of a DS18B20 (family code 0x28) // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'SerialNum' - Serial Number of DS18B20 to read temperature from // 'Temp ' - pointer to variable where that temperature will be // returned // // Returns: TRUE(1) temperature has been read and verified // FALSE(0) could not read the temperature, perhaps device is not // in contact // int ReadTemperature28(int portnum, uchar *SerialNum, float *Temp) { uchar rt=FALSE; uchar send_block[30],lastcrc8; int send_cnt, tsht, i, loop=0; int power; // set the device serial number to the counter device owSerialNum(portnum,SerialNum,FALSE); for (loop = 0; loop < 2; loop ++) { // check if the chip is connected to VDD if (owAccess(portnum)) { owWriteByte(portnum,0xB4); power = owReadByte(portnum); } // access the device if (owAccess(portnum)) { // send the convert command and if nesessary start power delivery if (power) { if (!owWriteBytePower(portnum,0x44)) return FALSE; } else { if (!owWriteByte(portnum,0x44)) return FALSE; } // sleep for 1 second msDelay(1000); // turn off the 1-Wire Net strong pull-up if (power) { if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL) return FALSE; } // access the device if (owAccess(portnum)) { // create a block to send that reads the temperature // read scratchpad command send_cnt = 0; send_block[send_cnt++] = 0xBE; // now add the read bytes for data bytes and crc8 for (i = 0; i < 9; i++) send_block[send_cnt++] = 0xFF; // now send the block if (owBlock(portnum,FALSE,send_block,send_cnt)) { // initialize the CRC8 setcrc8(portnum,0); // perform the CRC8 on the last 8 bytes of packet for (i = send_cnt - 9; i < send_cnt; i++) lastcrc8 = docrc8(portnum,send_block[i]); // verify CRC8 is correct if (lastcrc8 == 0x00) { // calculate the high-res temperature tsht = send_block[2] << 8; tsht = tsht | send_block[1]; if (tsht & 0x00001000) tsht = tsht | 0xffff0000; *Temp = ((float) tsht)/16; // success rt = TRUE; break; } } } } } // return the result flag rt return rt; }
//---------------------------------------------------------------------- // Main Test // int main() //short argc, char **argv) { int PortNum=1,rslt,i,j,testcnt=0,length; uchar TempSerialNum[8]; uchar tran_buffer[2000], filename[10]; char return_msg[128]; int portnum=0; // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"1\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if (!owAcquire(portnum, argv[1], return_msg)) { printf("%s",return_msg); exit(1); } // success printf("%s",return_msg); //---------------------------------------- // Introduction printf("\n/---------------------------------------------\n"); printf(" The following is a test excersize of the\n" " 1-Wire Net public domain library Version 2.00.\n\n" " This test was run using with 2 DS1920's (DS1820),\n" " 1 DS1971 (DS2430), and 1 DS1996.\n\n"); //---------------------------------------- // First the devices on the 1-Wire Net printf("\n/---------------------------------------------\n"); printf("TEST%d: Searching for devices on 1-Wire Net\n",testcnt++); // find the first device (all devices not just alarming) rslt = owFirst(portnum,TRUE, FALSE); while (rslt) { // print the Serial Number of the device just found PrintSerialNum(portnum); // find the next device rslt = owNext(portnum,TRUE, FALSE); } //---------------------------------------- // now search for the part with a 0x0C family code (DS1996) printf("\n/---------------------------------------------\n"); printf("TEST%d: Set to find first device with 0x0C family code\n",testcnt++); owFamilySearchSetup(portnum,0x0C); // find the first 0x0c device TempSerialNum[0]=0; while (TempSerialNum[0]!=0x0c && owNext(portnum,TRUE,FALSE)) { owSerialNum(portnum,TempSerialNum,TRUE); } printf("search result %d\n",TempSerialNum[0]==0x0c); // print the Serial Number of the device just found PrintSerialNum(portnum); //---------------------------------------- // Access a device and read ram printf("\n/---------------------------------------------\n"); printf("TEST%d: Access the current device and read ram\n",testcnt++); printf("owAccess %d\n",owAccess(portnum)); printf("Read Ram 0xF0: %02X\n",owTouchByte(portnum,0xF0)); printf("Address0 0x00: %02X\n",owTouchByte(portnum,0x00)); printf("Address1 0x00: %02X\n",owTouchByte(portnum,0x00)); printf("Page 0: "); for (i = 0; i < 32; i++) printf("%02X ",owTouchByte(portnum,0xFF)); printf("\n"); //---------------------------------------- // Read ram with owBlock printf("\n/---------------------------------------------\n"); printf("TEST%d: Read ram with owBlock\n",testcnt++); for (i = 0; i < 32; i++) tran_buffer[i] = 0xFF; printf("owBlock %d\n",owBlock(portnum,FALSE,tran_buffer,32)); printf("Page 1: "); for (i = 0; i < 32; i++) printf("%02X ",tran_buffer[i]); printf("\n"); //---------------------------------------- // Write a packet in each page of DS1996 printf("\n/---------------------------------------------\n"); printf("TEST%d: Place the DS1996 into overdrive\n",testcnt++); printf("owOverdriveAccess %d\n",owOverdriveAccess(portnum)); //---------------------------------------- // Write 4 packets with owWritePacketStd printf("\n/---------------------------------------------\n"); printf("TEST%d: Write 4 packets with owWritePacketStd\n",testcnt++); for (j = 0; j < 4; j++) { for (i = 0; i < 29; i++) tran_buffer[i] = (uchar)i + j; printf("Write page %d: %d\n",j,owWritePacketStd(portnum,j,tran_buffer,29,FALSE,FALSE)); for (i = 0; i < 29; i++) tran_buffer[i] = 0; length = owReadPacketStd(portnum,TRUE,j,tran_buffer); printf("Read page %d: %d\n",j,length); for (i = 0; i < length; i++) printf("%02X",tran_buffer[i]); printf("\n"); } //---------------------------------------- // Write a file to DS1996 printf("\n/---------------------------------------------\n"); printf("TEST%d: Format and write a file (in overdrive)\n",testcnt++); sprintf(filename,"DEMO"); // set the data to write for (i = 0; i < 2000; i++) tran_buffer[i] = i % 255; printf("Format and write file DEMO.000 %d\n", owFormatWriteFile(portnum,filename,2000,tran_buffer)); // clear the buffer for (i = 0; i < 2000; i++) tran_buffer[i] = 0x55; printf("Read file DEMO.000 %d\n",owReadFile(portnum,filename,tran_buffer)); // print the data result for (i = 0; i < 2000; i++) { if ((i % 0x20) == 0) printf("\n%03X ",i); printf("%02X",tran_buffer[i]); } printf("\n"); //---------------------------------------- // Turn off overdrive printf("\n/---------------------------------------------\n"); printf("TEST%d: Turn off overdrive\n",testcnt++); printf("Set 1-Wire Net speed to normal %d\n",owSpeed(portnum,MODE_NORMAL)); //---------------------------------------- // Verify a device printf("\n/---------------------------------------------\n"); printf("TEST%d: Verify the current device\n",testcnt++); printf("owVerify (normal) %d\n",owVerify(portnum,FALSE)); printf("owVerify (alarm) %d\n",owVerify(portnum,TRUE)); //---------------------------------------- // Skip the first family code found printf("\n/---------------------------------------------\n"); printf("TEST%d: Skip the first family code found\n",testcnt++); // find the next device printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE)); // print the Serial Number of the device just found PrintSerialNum(portnum); // skip the first family type found owSkipFamily(portnum); printf("owSkipFamily called\n"); // find the next device printf("search result of owNext %d\n",owNext(portnum,TRUE, FALSE)); // print the Serial Number of the device just found PrintSerialNum(portnum); //---------------------------------------- // Find first family code (DS1920) and read temperature printf("\n/---------------------------------------------\n"); printf("TEST%d: Find first family code (DS1920) and read temperature\n",testcnt++); // find the next device printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE)); // print the Serial Number of the device just found PrintSerialNum(portnum); // send the convert temperature command printf("Convert temperature command %02X\n",owTouchByte(portnum,0x44)); // set the 1-Wire Net to strong pull-up printf("Set power delivery %d\n",owLevel(portnum,MODE_STRONG5)); // sleep for 1 second msDelay(1000); // turn off the 1-Wire Net strong pull-up printf("Disable power delivery %d\n",owLevel(portnum,MODE_NORMAL)); // read the DS1920 temperature value printf("Access the DS1920 %d\n",owAccess(portnum)); tran_buffer[0] = 0xBE; tran_buffer[1] = 0xFF; tran_buffer[2] = 0xFF; printf("Block to read temperature %d\n",owBlock(portnum,FALSE,tran_buffer,3)); // interpret the result printf("result: DS1920 temperature read: %d C\n", (tran_buffer[1] | ((int)tran_buffer[2] << 8)) / 2); //---------------------------------------- // Verify the current device, could also be alarming printf("\n/---------------------------------------------\n"); printf("TEST%d: Verify the current device, could also be alarming\n",testcnt++); printf("owVerify (normal) %d\n",owVerify(portnum,FALSE)); printf("owVerify (alarm) %d\n",owVerify(portnum,TRUE)); //---------------------------------------- // Test setting the Serial Number with owSerialNum printf("\n/---------------------------------------------\n"); printf("TEST%d: Test setting the Serial Number with owSerialNum\n",testcnt++); // set the Serial Num to 0 to 7 for (i = 0; i < 8; i++) TempSerialNum[i] = (uchar)i; owSerialNum(portnum,TempSerialNum,FALSE); // read back the Serial Number PrintSerialNum(portnum); //---------------------------------------- // Verify the current device (should fail, no such device) printf("\n/---------------------------------------------\n"); printf("TEST%d: Verify the current device (should fail, no such device)\n",testcnt++); printf("owVerify (normal) %d\n",owVerify(portnum,FALSE)); printf("owVerify (alarm) %d\n",owVerify(portnum,TRUE)); // release the 1-Wire Net owRelease(portnum,return_msg); printf("%s",return_msg); exit(0); return 0; }
//------------------------------------------------------------------------- // Installs new service data on a user token. // // 'copr' - Structure for holding coprocessor information. // 'user' - Structure for holding user token information. // 'secret' - the authentication secret to install on user token. // // Return: If TRUE, new service installation succeeded. // If FALSE, an error occurred. // SMALLINT InstallServiceData(SHACopr* copr, SHAUser* user, uchar* secret, int secret_length, int initialBalance) { short handle; int maxwrite; FileEntry fe; uchar fullBindCode[15]; //make sure user has a file directory structure memcpy(fe.Name, copr->serviceFilename, 4); fe.Ext = copr->serviceFilename[4]; // install master authentication secret if(user->devAN[0]==0x18) { //need to format the device if(!owFormat(user->portnum, user->devAN)) return FALSE; //and create an empty stub for his account information if(!owCreateFile(user->portnum, user->devAN, &maxwrite, &handle, &fe)) return FALSE; //need to know what page the stub is on user->accountPageNumber = fe.Spage; // set the serial number to that of the user owSerialNum(user->portnum, user->devAN, FALSE); OWASSERT( InstallSystemSecret18(user->portnum, user->accountPageNumber, user->accountPageNumber&7, secret, secret_length, FALSE), OWERROR_INSTALL_SECRET_FAILED, FALSE ); } else if((user->devAN[0]&0x7F)==0x33) { // set the serial number to that of the user owSerialNum(user->portnum, user->devAN, FALSE); //because of copy-authorization, we need to install the //secret first on the DS1961S and _then_ format the system OWASSERT( InstallSystemSecret33(user->portnum, 0, 0, secret, secret_length, FALSE), OWERROR_INSTALL_SECRET_FAILED, FALSE ); //need to format the device if(!owFormat(user->portnum, user->devAN)) return FALSE; //and create an empty stub for his account information if(!owCreateFile(user->portnum, user->devAN, &maxwrite, &handle, &fe)) return FALSE; //need to know what page the stub is on user->accountPageNumber = fe.Spage; } else { return FALSE; } // format the bind code properly // first four bytes of bind code memcpy(fullBindCode, copr->bindCode, 4); // followed by the pagenumber fullBindCode[4] = (uchar)user->accountPageNumber; // and 7 bytes of the address of current device memcpy(&fullBindCode[5], user->devAN, 7); // followed by the last 3 bytes of bind code memcpy(&fullBindCode[12], &(copr->bindCode[4]), 3); // create a unique secret for iButton if(user->devAN[0]==0x18) { DebitFile* accountFile = (DebitFile*)user->accountFile; OWASSERT( BindSecretToiButton18(user->portnum, user->accountPageNumber, user->accountPageNumber&7, copr->bindData, fullBindCode, TRUE), OWERROR_BIND_SECRET_FAILED, FALSE ); // do a read just to get value of writecycle counter user->writeCycleCounter = ReadAuthPageSHA18(user->portnum, user->accountPageNumber, user->accountFile, NULL, TRUE); //setup user account file with initial balance IntToBytes(accountFile->balanceBytes,3,initialBalance); // set transaction ID accountFile->transID[0] = 0; accountFile->transID[0] = 0; } else if((user->devAN[0]&0x7F)==0x33) { DebitFile33* accountFile33 = (DebitFile33*)user->accountFile; OWASSERT( BindSecretToiButton33(user->portnum, user->accountPageNumber, 0, copr->bindData, fullBindCode, TRUE), OWERROR_BIND_SECRET_FAILED, FALSE ); // Call VerifyUser just to get the user's secret in wspc if(!VerifyUser(copr, user, TRUE)) return FALSE; //Record A //setup user account file with initial balance IntToBytes(accountFile33->balanceBytes_A,3,initialBalance); // set transaction ID accountFile33->transID_A[0] = 0; accountFile33->transID_A[0] = 0; //Record B //setup user account file with initial balance IntToBytes(accountFile33->balanceBytes_B,3,initialBalance); // set transaction ID accountFile33->transID_B[0] = 0; accountFile33->transID_B[0] = 0; } //sign the data with coprocessor and write it out return UpdateServiceData(copr, user); }
//--------------------------------------------------------------------- // Finds new SHA iButtons on the given port. Uses 'triple-buffer' // technique to insure it only finds 'new' buttons. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // 'devAN' - pointer to buffer for device address // 'resetList' - if TRUE, final buffer is cleared. // // Returns: TRUE, found a new SHA iButton. // FALSE, no new buttons are present. // SMALLINT FindNewSHA(int portnum, uchar* devAN, SMALLINT resetList) { uchar ROM[8]; uchar tempList[MAX_SHA_IBUTTONS] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; int tempIndex = 0, i; SMALLINT hasDevices = TRUE, completeList = FALSE; // force back to standard speed if(MODE_NORMAL != owSpeed(portnum,MODE_NORMAL)) { OWERROR(OWERROR_LEVEL_FAILED); return FALSE; } in_overdrive[portnum&0x0FF] = FALSE; // get first device in list with the specified family if(needsFirst || resetList) { hasDevices = owFirst(portnum, TRUE, FALSE); completeList = TRUE; if(resetList) listIndex = 0; } else { hasDevices = owNext(portnum, TRUE, FALSE); } if(hasDevices) { do { // verify correct device type owSerialNum(portnum, ROM, TRUE); tempList[tempIndex++] = ROM[7]; // compare crc to complete list of ROMs for(i=0; i<listIndex; i++) { if(ROM[7] == ListOfKnownSHA[portnum&0x0FF][i]) break; } // found no match; if(i==listIndex) { // check if correct type and not copr_rom if ((SHA_FAMILY_CODE == (ROM[0] & 0x7F)) || (SHA33_FAMILY_CODE == (ROM[0] & 0x7F))) { // save the ROM to the return buffer owSerialNum(portnum,devAN,TRUE); ListOfKnownSHA[portnum&0x0FF][listIndex++] = devAN[7]; return TRUE; } } } while(owNext(portnum, TRUE, FALSE)); } // depleted the list, start over from beginning next time needsFirst = TRUE; if(completeList) { // known list is triple-buffered, listBuffer is intermediate // buffer. tempList is the immediate buffer, while // ListOfKnownSHA is the final buffer. if( (memcmp(tempList, listBuffer[portnum&0x0FF], MAX_SHA_IBUTTONS)==0) && tempIndex == indexBuffer ) { memcpy(ListOfKnownSHA[portnum&0x0FF], tempList, MAX_SHA_IBUTTONS); listIndex = tempIndex; } else { memcpy(listBuffer[portnum&0x0FF], tempList, MAX_SHA_IBUTTONS); indexBuffer = tempIndex; } } return FALSE; }
//------------------------------------------------------------------------- // Select the current device and attempt overdrive if possible. Usable // for both DS1963S and DS1961S. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // // Return: TRUE - device selected // FALSE - device not select // SMALLINT SelectSHA(int portnum) { int rt,cnt=0; #ifdef __MC68K__ // Used in when overdrive isn't...Used owVerify for overdrive rt = owAccess(portnum); #else //\\//\\//\\//\\//\\//\\//\\//\\//\\// #ifdef DEBUG_DUMP uchar ROM[8]; int i, mcnt; char msg[255]; owSerialNum(portnum,ROM, TRUE); mcnt = sprintf(msg,"\n Device select "); for (i = 0; i < 8; i++) mcnt += sprintf(msg + mcnt, "%02X",ROM[i]); mcnt += sprintf(msg + mcnt,"\n"); printf("%s",msg); #endif //\\//\\//\\//\\//\\//\\//\\//\\//\\// // loop to access the device and optionally do overdrive do { rt = owVerify(portnum,FALSE); // check not present if (rt != 1) { // if in overdrive, drop back if (in_overdrive[portnum&0x0FF]) { // set to normal speed if(MODE_NORMAL == owSpeed(portnum,MODE_NORMAL)) in_overdrive[portnum&0x0FF] = FALSE; } } // present but not in overdrive else if (!in_overdrive[portnum&0x0FF]) { // put all devices in overdrive if (owTouchReset(portnum)) { if (owWriteByte(portnum,0x3C)) { // set to overdrive speed if(MODE_OVERDRIVE == owSpeed(portnum,MODE_OVERDRIVE)) in_overdrive[portnum&0x0FF] = TRUE; } } rt = 0; } else break; } while ((rt != 1) && (cnt++ < 3)); #endif return rt; }
//--------------------------------------------------------------------------- // The main program that performs the operations on switches // int main(int argc, char **argv) { short test; //info byte data short clear=0; //used to clear the button short done; //to tell when the user is done SwitchProps sw; //used to set Channel A and B uchar SwitchSN[MAXDEVICES][8]; //the serial number for the devices int num; //for the number of devices present int i,j,n,count,result; //loop counters and indexes char out[140]; //used for output of the info byte data int portnum=0; long select; //inputed number from user //---------------------------------------- // Introduction header printf("\n/---------------------------------------------\n"); printf(" Switch - V3.00\n" " The following is a test to excersize the \n" " setting of the state in a DS2406.\n"); printf(" Press any CTRL-C to stop this program.\n\n"); // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"1\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if ((portnum = owAcquireEx(argv[1])) < 0) { OWERROR_DUMP(stdout); exit(1); } // success printf("Port opened: %s\n",argv[1]); // this is to get the number of the devices and the serial numbers num = FindDevices(portnum, &SwitchSN[0], SWITCH_FAMILY, MAXDEVICES); // setting up the first print out for the frist device owSerialNum(portnum, SwitchSN[0], FALSE); printf("\n"); n=0; if(owAccess(portnum)) { // loop while not done do { test = ReadSwitch12(portnum, clear); for(i=7; i>=0; i--) printf("%02X", SwitchSN[n][i]); printf("\n"); count = SwitchStateToString12(test, out); printf("%s", out); // print menu select = 1; if (!EnterNum("\n\n(1) Display the switch Info\n" "(2) Clear activity Latches\n" "(3) Set Flip Flop(s) on switch\n" "(4) Select different device\n" "(5) Quit\n" "Select a Number", 1, &select, 1, 5)) break; printf("\n\n"); // do something from the menu selection clear = FALSE; switch(select) { case 1: // Display the switch Info done = FALSE; break; case 2: // Clear activity Latches clear = TRUE; done = FALSE; break; case 3: // Set Flip Flop(s) on switch select = 0; if (EnterNum("Channel A Flip Flop (1 set, 0 clear)", 1, &select, 0, 1)) { sw.Chan_A = (uchar)select; if(test & 0x40) { if (EnterNum("Channel B Flip Flop (1 set, 0 clear)", 1, &select, 0, 1)) { sw.Chan_B = (uchar)select; done = FALSE; } } else { sw.Chan_B = 0; done = FALSE; } printf("\n"); } // proceed to setting switch state if not done if (!done) { // loop to attempt to set the switch (try up to 5 times) count = 0; do { result = SetSwitch12(portnum, SwitchSN[n], sw); // if failed then delay to let things settle if (!result) msDelay(50); } while ((count++ < 5) && (result != TRUE)); // if could not set then show error if (!result) printf("Could not set Switch!\n"); } break; case 4: // Switch Devices // print the device list for(j=0; j < num; j++) { printf("%d ", j+1); for(i=7; i>=0; i--) { printf("%02X", SwitchSN[j][i]); } printf("\n"); } printf("\n"); // select the device select = 0; if (EnterNum("Select Device",1, &select, 1, num)) { n = (int)(select - 1); owSerialNum(portnum, SwitchSN[n], FALSE); done = FALSE; } break; case 5: // Done done = TRUE; break; default: break; } } while (!done); } //One Wire Access owRelease(portnum); printf("Closing port %s.\n", argv[1]); exit(0); return 0; }
//-------------------------------------------------------------------------- // This is the begining of the program that tests the different Channels int main() //short argc, char **argv) { char return_msg[128]; //returned message from 1-wire operations int i,j,k,n; //loop counters short test=0; //info byte data short clear=0; //used to clear the button SwitchProps sw; //used to set Channel A and B uchar SwitchSN[MAXDEVICES][8]; //the serial numbers for the devices int num; //for the number of devices present int ch; //inputed character from user char out[140]; //used for output of the info byte data int portnum=0; //---------------------------------------- // Introduction header printf("\n/---------------------------------------------\n"); printf(" swtest - V2.00\n" " The following is a test to excersize the\n" " different channels on the DS2406.\n"); printf(" Press any CTRL-C to stop this program.\n\n"); // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"1\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if (!owAcquire(portnum, argv[1], return_msg)) { printf("%s",return_msg); exit(1); } // success printf("%s",return_msg); // this is to get the number of the devices and the serial numbers num = FindDevices(portnum, &SwitchSN[0], SWITCH_FAMILY, MAXDEVICES); // setting up the first print out for the frist device owSerialNum(portnum, SwitchSN[0], FALSE); j=1; n=0; do { // This is for after the different combinations of channels // have been tested to reset to a different device to be tested. if( ((test & 0x40) && (j==5)) || ((!(test & 0x40)) && (j==3)) ) { printf("\n\n"); for(k=0; k < num; k++) { printf("%d ", k+1); for(i=7; i>=0; i--) { printf("%02X", SwitchSN[k][i]); } printf("\n"); } printf("%d To quit or any other key.\n", k+1); printf("\n"); printf("Pick a device\n"); ch = getkeystroke(); n = 0; n = (10*n + (ch - '0')) - 1; if( (n>num-1) || (n<0) ) { n = 0; //used to finish off the loop break; } owSerialNum(portnum, SwitchSN[n], FALSE); j = 1; } printf("\n"); test = ReadSwitch12(portnum,clear); // This looks at the info byte to determine if it is a // two or one channel device. if(test & 0x40) { switch(j) { case 1: sw.Chan_A = 0; sw.Chan_B = 0; break; case 2: sw.Chan_A = 0; sw.Chan_B = 1; break; case 3: sw.Chan_A = 1; sw.Chan_B = 0; break; case 4: sw.Chan_A = 1; sw.Chan_B = 1; break; default: sw.Chan_A = 1; sw.Chan_B = 1; j=0; break; } } else { switch(j) { case 1: sw.Chan_B = 0; sw.Chan_A = 0; break; case 2: sw.Chan_B = 0; sw.Chan_A = 1; break; default: sw.Chan_B = 0; sw.Chan_A = 1; j = 0; break; } } if(!SetSwitch12(portnum, SwitchSN[n], &sw)) { msDelay(50); if(SetSwitch12(portnum, SwitchSN[n], &sw)) msDelay(50); else printf("Switch not set\n"); } test = ReadSwitch12(portnum,clear); printf("\n"); for(i=7; i>=0; i--) { printf("%02X", SwitchSN[n][i]); } printf("\n"); SwitchStateToString12(test, out); printf("%s", out); j++; } while(1); // release the 1-Wire Net owRelease(portnum,return_msg); printf("%s",return_msg); exit(0); return 0; }
//------------------------------------------------------------------------- // Verifies the authentication response of a user token. // // 'copr' - Structure for holding coprocessor information. // 'user' - Structure for holding user token information. // 'chlg' - 3-byte buffer of challenge data. // 'doBind' - if true, the user's unique secret is recreated on the // coprocessor. If this function is called multiple times, // it is acceptable to skip the bind for all calls after // the first on the same user token. // // Return: If TRUE, the user's authentication response matched exactly the // signature generated by the coprocessor. // If FALSE, an error occurred or the signature did not match. // SMALLINT VerifyAuthResponse(SHACopr* copr, SHAUser* user, uchar* chlg, SMALLINT doBind) { int addr = copr->wspcPageNumber << 5; int wcc = user->writeCycleCounter; uchar sign_cmd; uchar scratchpad[32]; uchar fullBindCode[15]; memset(scratchpad, 0x00, 32); memset(fullBindCode, 0x0FF, 15); // format the bind code properly if(user->devAN[0]==0x18) { // Format for DS1963S memcpy(fullBindCode, copr->bindCode, 4); memcpy(&fullBindCode[12], &(copr->bindCode[4]), 3); // use ValidateDataPage command sign_cmd = SHA_VALIDATE_DATA_PAGE; // copy wcc LSB first if(!IntToBytes(&scratchpad[8], 4, wcc)) OWERROR(OWERROR_NO_ERROR_SET); } else if(user->devAN[0]==0x33||user->devAN[0]==0xB3) { // Leave bindCode FF for DS1961S // Use AuthenticateHost command sign_cmd = SHA_AUTHENTICATE_HOST; // the user doesn't have a write cycle counter memset(&scratchpad[8], 0x0FF, 4); } else { OWERROR(OWERROR_WRONG_TYPE); return FALSE; } // the pagenumber fullBindCode[4] = (uchar)user->accountPageNumber; // and 7 bytes of the address of current device memcpy(&fullBindCode[5], user->devAN, 7); // get the user address and page num from fullBindCode memcpy(&scratchpad[12], &fullBindCode[4], 8); // set the same challenge bytes memcpy(&scratchpad[20], chlg, 3); // set the serial number to that of the coprocessor owSerialNum(copr->portnum, copr->devAN, FALSE); // install user's unique secret on the wspc secret if(doBind) { OWASSERT( BindSecretToiButton18(copr->portnum, copr->authPageNumber, copr->wspcPageNumber&7, copr->bindData, fullBindCode, FALSE), OWERROR_BIND_SECRET_FAILED, FALSE ); if(user->devAN[0]==0x33||user->devAN[0]==0xB3) { // also copy the resulting secret into secret location 0, to // replace the signing secret. Necessary for producing the // DS1961S's write-authorization MAC. OWASSERT( CopySecretSHA18(copr->portnum, 0), OWERROR_COPY_SECRET_FAILED, FALSE); } } // recreate the signature and verify OWASSERT( WriteDataPageSHA18(copr->portnum, copr->wspcPageNumber, user->accountFile, doBind), OWERROR_WRITE_DATA_PAGE_FAILED, FALSE ); OWASSERT( WriteScratchpadSHA18(copr->portnum, addr, scratchpad, 32, TRUE), OWERROR_WRITE_SCRATCHPAD_FAILED, FALSE ); OWASSERT( SHAFunction18(copr->portnum, sign_cmd, addr, TRUE), OWERROR_SHA_FUNCTION_FAILED, FALSE ); OWASSERT( MatchScratchpadSHA18(copr->portnum, user->responseMAC, TRUE), OWERROR_MATCH_SCRATCHPAD_FAILED, FALSE ); return TRUE; }
//---------------------------------------------------------------------- // Read the temperature of a DS1920/DS1820 // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'SerialNum' - Serial Number of DS1920/DS1820 to read temperature from // 'Temp ' - pointer to variable where that temperature will be // returned // // Returns: TRUE(1) temperature has been read and verified // FALSE(0) could not read the temperature, perhaps device is not // in contact // int ReadTemperature(int portnum, uchar *SerialNum, float *Temp) { int rt=FALSE; uchar send_block[30],lastcrc8; int send_cnt=0, tsht, i, loop=0; float tmp,cr,cpc; setcrc8(portnum,0); // set the device serial number to the counter device owSerialNum(portnum,SerialNum,FALSE); for (loop = 0; rt==FALSE && loop < 2; loop ++) { // access the device if (owAccess(portnum)) { // send the convert temperature command owTouchByte(portnum,0x44); // set the 1-Wire Net to strong pull-up if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5) return FALSE; // sleep for 1 second msDelay(1000); // turn off the 1-Wire Net strong pull-up if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL) return FALSE; // access the device if (owAccess(portnum)) { // create a block to send that reads the temperature // read scratchpad command send_block[send_cnt++] = 0xBE; // now add the read bytes for data bytes and crc8 for (i = 0; i < 9; i++) send_block[send_cnt++] = 0xFF; // now send the block if (owBlock(portnum,FALSE,send_block,send_cnt)) { // perform the CRC8 on the last 8 bytes of packet for (i = send_cnt - 9; i < send_cnt; i++) lastcrc8 = docrc8(portnum,send_block[i]); // verify CRC8 is correct if (lastcrc8 == 0x00) { // calculate the high-res temperature tsht = send_block[1]/2; if (send_block[2] & 0x01) tsht |= -128; tmp = (float)(tsht); cr = send_block[7]; cpc = send_block[8]; if (((cpc - cr) == 1) && (loop == 0)) continue; if (cpc == 0) return FALSE; else tmp = tmp - (float)0.25 + (cpc - cr)/cpc; *Temp = tmp; // success rt = TRUE; } } } } } // return the result flag rt return rt; }
//---------------------------------------------------------------------- // Main for tstfind // int main(int argc, char **argv) { int rslt,cnt; int portnum=0; uchar SNum[8]; // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"{1,5}\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if((portnum = owAcquireEx(argv[1])) < 0) { OWERROR_DUMP(stdout); exit(1); } // success printf("Port opened: %s\n",argv[1]); //---------------------------------------- // Introduction printf("\n/---------------------------------------------\n"); printf(" Loop to find all iButton on 1-Wire Net.\n\n"); do { printf("-------------------- Start of search\n"); cnt = 0; // find the first device (all devices not just alarming) rslt = owFirst(portnum, TRUE, FALSE); while (rslt) { // print the device number cnt++; printf("(%d) ",cnt); // print the Serial Number of the device just found owSerialNum(portnum,&SNum[0],TRUE); PrintSerialNum(&SNum[0]); printf("\n"); // find the next device rslt = owNext(portnum, TRUE, FALSE); } printf("-------------------- End of search\n\n"); } while (!key_abort()); // release the 1-Wire Net owRelease(portnum); printf("Closing port %s.\n", argv[1]); exit(0); return 0; }