Пример #1
0
//---------------------------------------------------------------------
// Uses File I/O API to find the user token with a specific
// service file name.  Usually 'DSLM.102'.
//
// 'user'       - Structure for holding user token information
// 'fe'         - pointer to file entry structure, with proper
//                service filename.
// 'doBlocking' - if TRUE, method blocks until a user token is found.
//
// Returns: TRUE, found a valid user token
//          FALSE, no user token is present
//
SMALLINT FindUserSA(SHAUser* user, FileEntry* fe,
                     SMALLINT doBlocking)
{
   SMALLINT FoundUser = FALSE;

   if(FindNewSHA(user->portnum, user->devAN, TRUE))
   {
      short handle;

      if(owOpenFile(user->portnum, user->devAN, fe, &handle))
      {
         user->accountPageNumber = fe->Spage;
         FoundUser = TRUE;
         owCloseFile(user->portnum, user->devAN, handle);
      }
      else
      {
         PrintSerialNum(user->devAN);
         printf(" is not a SHA User.");
      }
   }

   for(;;)
   {
      // now get all the SHA iButton parts until we find
      // one that has the right file on it.
      while(!FoundUser && FindNewSHA(user->portnum, user->devAN, FALSE))
      {
         short handle;

         if(owOpenFile(user->portnum, user->devAN, fe, &handle))
         {
            user->accountPageNumber = fe->Spage;
            FoundUser = TRUE;
            owCloseFile(user->portnum, user->devAN, handle);
         }
         else
         {
            PrintSerialNum(user->devAN);
            printf(" is not a SHA User.");
         }
      }

      if(FoundUser)
         return TRUE;
      else if(!doBlocking)
         return FALSE;
   }
}
Пример #2
0
//----------------------------------------------------------------------
//  Main Test for the DS2450 - 1-Wire Quad A/D Converter
//
int main(int argc, char **argv)
{
   char msg[45];
   int NumDevices = 0;
   int i = 0;
   int start_address = 0x8;
   int end_address = 0x11;
   float prslt[4];
   uchar ctrl[16];
   int try_overdrive=0;
   int portnum=0;

   //------------------------------------------------------
   // Introduction header
   printf("\n/---------------------------------------------\n");
   printf("  Channels A to D Application - V2.00\n"
          "  The following is a test to excersize a\n"
          "  DS2450 - 1-Wire Quad A/D Converter \n\n");

   printf("  Press any CTRL-C to stop this program.\n\n");
   printf("  Output   [Serial Number(s) ... Channel 'A' Value ... Channel 'B' Value ... \n"
                         "                             ... Channel 'C' Value ... Channel 'D' Value] \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,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]);

   // Find the device(s)
   NumDevices = FindDevices(portnum, &FamilySN[0], 0x20, MAXDEVICES);
   if (NumDevices>0)
   {
      printf("\n");
      printf("Device(s) Found: \n");

      for (i = 0; i < NumDevices; i++)
      {

         PrintSerialNum(FamilySN[i]);
         printf("\n");

         if (SetupAtoDControl(portnum, FamilySN[i], &ctrl[0], &msg[0]))
         {
            printf("A/D settings found\n %s\n", msg);
         }
         else
            printf("\n\n\n ERROR, device set up unsuccessful!\n");


         if (WriteAtoD(portnum, try_overdrive, FamilySN[i], &ctrl[0], start_address, end_address))
         {
            printf("\nA/D settings written");

         }
         else
            printf("\n\n\n ERROR, device not found!\n");
      }
   }

   // (stops on CTRL-C)
   do
   {
      // read the current channels
      for (i = 0; i < NumDevices; i++)
      {
         printf("\n\n");
         PrintSerialNum(FamilySN[i]);

         if (!DoAtoDConversion(portnum, try_overdrive, FamilySN[i]))
         {
            printf("\nError doing conversion, verify device present: %d\n",
            (int)owVerify(portnum,FALSE));
         }
         if (ReadAtoDResults(portnum, try_overdrive, FamilySN[i], &prslt[0], &ctrl[0]))
         {
            int  c = 0;
            for (c = 0; c < 4; c++)
            {
               printf("  %1.3f ", prslt[c]);
            }
         }
         else
         {
            printf("\nError reading channel, verify device present: %d\n",
            (int)owVerify(portnum,FALSE));
         }

      }
   }
   while (!key_abort());

   // release the 1-Wire Net
   owRelease(portnum);
   printf("Closing port %s.\n", argv[1]);
   exit(0);

   return 0;
}
Пример #3
0
int main(int argc, char** argv)
{
   int i = 0;
   SHACopr copr;
   SHAUser user;
   FileEntry fe = {"COPR",0};
   uchar *authSecret;
   int authlen;
   char test[2] = {'y',0};
   long balance;

   copr.portnum = 0;
   user.portnum = 0;

   puts("\nStarting SHA initrov Application\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,5}\" (Win32 TMEX)\n");
      exit(1);
   }

   if((user.portnum = copr.portnum = owAcquireEx(argv[1])) < 0)
   {
      printf("Failed to acquire port.\n");
      exit(2);
   }

#ifdef COPRVM
   if(!GetCoprVM(&copr, &fe))
      exit(1);
#else
   puts("\nPlease place coprocessor token on the 1-Wire bus.\n");

   while(!FindCoprSHA(&copr, &fe))
   {
      if(owHasErrors())
         msDelay(10);
   }

   printf("Found device: ");
   PrintSerialNum(copr.devAN);
   puts("\n");
#endif

   authlen = GetSecret("System Authentication Secret", &authSecret);

   EnterString("Reformat the secret for DS1961S compatibility", test, 1, 1);
   if(test[0] == 'y')
   {
      ReformatSecretFor1961S(authSecret, authlen);
      PrintHex(authSecret, authlen);
      printf("\n");
      copr.ds1961Scompatible = 0x55;
   }

   puts("\nPlease place user token on the 1-Wire bus.\n");
   do
   {
      while(!FindNewSHA(user.portnum, user.devAN, (i==0)))
      {
         if(owHasErrors())
         {  
            OWERROR_DUMP(stdout);
            msDelay(10);
         }
      }
      i++;
   }
   while(user.devAN[7]==copr.devAN[7]);
   // just check the crc of the two devices

   balance = 100;
   EnterNum("\nInitial Balance in Cents?\n", 5, &balance, 0, 0xFFFFF);

   printf("Installing Service Data on device: ");
   PrintSerialNum(user.devAN);
   puts("\n");
   if(InstallServiceData(&copr, &user, authSecret, authlen, (int)balance))
   {
      puts("User token successfully set up");
   }
   else
   {
      puts("User token setup failed");
      OWERROR_DUMP(stdout);
   }

   // and we're done
   owRelease(copr.portnum);

   // program is about to exit, but we may as well free these
   // up anyways...
   free(authSecret);

   return 0;
}
Пример #4
0
//----------------------------------------------------------------------
//  Main Test for DS1920/DS1820 temperature measurement
//
int main(int argc, char **argv)
{
   float current_temp;
   int i = 0;
   int NumDevices=0;
   int portnum = 0;

   //----------------------------------------
   // Introduction header
   printf("\n/---------------------------------------------\n");
//   printf("  Temperature application DS1920/DS1820 - Version 1.00 \n"
//          "  The following is a test to excersize a DS1920/DS1820.\n"
//          "  Temperature Find and Read from a: \n"
//          "  DS1920/DS1820 (at least 1)\n\n");

   printf("  Press any CTRL-C to stop this program.\n\n");
   printf("  Output [Serial Number(s) ........ Temp1(F)] \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,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]);

   // Find the device(s)
   NumDevices = FindDevices(portnum, &FamilySN[0], 0x10, MAXDEVICES);
   if (NumDevices>0)
   {
      printf("\n");
      printf("Device(s) Found: \n");
      for (i = 0; i < NumDevices; i++)
      {
         PrintSerialNum(FamilySN[i]);
         printf("\n");
      }
      printf("\n\n");

      // (stops on CTRL-C)
      do
      {
         // read the temperature and print serial number and temperature
         for (i = 0; i < NumDevices; i++)
         {

            if (ReadTemperature(portnum, FamilySN[i],&current_temp))
            {
               PrintSerialNum(FamilySN[i]);
//               printf("     %5.1f \n", current_temp * 9 / 5 + 32);
                 printf("     %5.1f \n", current_temp );
              // converting temperature from Celsius to Fahrenheit
            }
            else
               printf("     Error reading temperature, verify device present:%d\n",
                       (int)owVerify(portnum, FALSE));
         }
         printf("\n");
      }
      while (!key_abort());
   }
   else
      printf("\n\n\nERROR, device DS1920/DS1820 not found!\n");

   // release the 1-Wire Net
   owRelease(portnum);
   printf("Closing port %s.\n", argv[1]);
   exit(0);

   return 0;
}
Пример #5
0
//----------------------------------------------------------------------
//  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;
}
Пример #6
0
//----------------------------------------------------------------------
//  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;
}
Пример #7
0
int main(int argc, char** argv)
{
   FileEntry fe;
   int len;
   int i;
   uchar authSecret[47];
   int authlen = 21;
   uchar signSecret[11] = {(uchar)'s',(uchar)'i',(uchar)'g',(uchar)'n',
                           (uchar)' ',(uchar)'s',(uchar)'e',(uchar)'c',
                           (uchar)'r',(uchar)'e',(uchar)'t'};
   int signlen = 11;
   int ischars;


   SHACopr copr;
   SHAUser user;

   //could be different ports.
   copr.portnum = 0;
   user.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,5}\" (Win32 TMEX)\n");
      exit(1);
   }

   if((copr.portnum = owAcquireEx(argv[1])) < 0)
   {
      printf("Failed to acquire port.\n");
      exit(1);
   }

   user.portnum = copr.portnum;

   // MANUALLY SETTING DATA FOR COPROCESSOR
   memcpy(copr.serviceFilename, "DLSM", 4);
   copr.serviceFilename[4] = (uchar) 102;
   copr.signPageNumber     = (uchar) 8;
   copr.authPageNumber     = (uchar) 7;
   copr.wspcPageNumber     = (uchar) 9;
   copr.versionNumber      = (uchar) 1;
   memcpy(copr.bindCode, "bindcde", 7);
   for(i=0;i<8;i++)
   {
      memcpy(&copr.bindData[i*4], "bind", 4);
   }
   copr.encCode           = 0x01;
   copr.ds1961Scompatible = 0x01;

   // Prompt for password
   printf("Enter up to 47 bytes of the Authentication Secret.\n");
   printf("  Data Entry Mode\n");
   printf("  (0) Text (single line)\n");
   printf("  (1) Hex (XX XX XX XX ...)\n");
   len = getData(authSecret,47,getNumber(0,1));
   // padd the data with spaces or 0's depending on type
   if(len < 47)
   {
      for(i = len; i < 47; i++)
         authSecret[i] = 0x00;
   }

   ReformatSecretFor1961S(authSecret, authlen);
   copr.ds1961Scompatible = 0x55;
   InstallAuthSecretVM(&copr, authSecret, authlen);
   InstallSignSecretVM(&copr, signSecret, signlen);

   puts("\nStarting SHA Software Authentication\n");
   puts("\nPlease place token on the 1-Wire bus.\n");

   memcpy(fe.Name, "DLSM", 4);
   fe.Ext = 102;

   for(;;)
   {
      if(FindUserSA(&user,&fe,FALSE))
      {
         if(VerifyUser(&copr, &user, TRUE))
         {
            PrintSerialNum(&user.devAN[0]);
            printf(", user data = ");
            // print the user data
            ischars = TRUE;
            for(i=0;i<7;i++)
            {
               if ((user.accountFile[i+22] < 0x20) ||
                   (user.accountFile[i+22] > 0x7E))
                   ischars = FALSE;
            }
            for(i=0;i<7;i++)
            {
               if (ischars)
                  printf("%c", user.accountFile[i+22]);
               else
                  printf("%02X ", user.accountFile[i+22]);
            }
            printf(", VALID\n");
            FindNewSHA(user.portnum, &user.devAN[0], TRUE);
         }
         else
         {
            PrintSerialNum(&user.devAN[0]);
            printf(", invalid\n");
            FindNewSHA(user.portnum, &user.devAN[0], TRUE);
         }
      }
      else
      {
         printf("NO DEVICE, invalid\n");
         FindNewSHA(user.portnum, &user.devAN[0], TRUE);
      }
   }

   owRelease(copr.portnum);

   return TRUE;
}