示例#1
0
static int GetSecret(char* name, uchar** secret)
{
   uchar inputBuffer[255];
   long lvalue=1, length;

   printf("How would you like to enter the %s?\n", name);
   EnterNum("\n   1) Hex\n   2) Text\n", 1, &lvalue, 1, 2);

   lvalue = getData(inputBuffer, 255, (lvalue==1));

   if(lvalue%47!=0)
      length = ((lvalue/47) + 1)*47;
   else
      length = lvalue;
   *secret = malloc(length);
   memset(*secret, 0x00, length);
   memcpy(*secret, inputBuffer, lvalue);

   printf("length=%ld\n",length);
   PrintHex(*secret, length);
   printf("\n");

   return length;
}
示例#2
0
文件: swtoper.c 项目: AriZuu/OneWire
//---------------------------------------------------------------------------
// 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;
}
示例#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
//--------------------------------------------------------------------------
//  Prints a message, and the current driver versions.
//
int InputMissionType(ThermoStateType *ThermoState, int ConvertToF)
{
   long num;
   float temp;

   // prompt to erase current mission
   num = 1;
   if (!EnterNum("Erase current mission\n  (0) yes\n  (1) no\nAnswer:",1, &num, 0, 1))
      return FALSE;

   // check for no erase
   if (num == 1)
      return FALSE;

   // prompt for start delay
   num = 0;
   if (!EnterNum("\nEnter start delay in minutes\n"
                 "Answer (0 to 65535):",5, &num, 0, 65535))
      return FALSE;

   // set in mission status structure
   ThermoState->MissStat.start_delay = (ushort)num;

   // prompt for sample rate
   num = 5;
   if (!EnterNum("\nEnter sample rate in minutes\n"
                 "Answer (1 to 255):",3, &num, 1, 255))
      return FALSE;

   // set in mission status structure
   ThermoState->MissStat.sample_rate = (uchar)num;

   // prompt to erase current mission
   num = 0;
   if (!EnterNum("Enable roll-over\n  (0) yes\n  (1) no\nAnswer:",1, &num, 0, 1))
      return FALSE;

   // rollover enabled?
   ThermoState->MissStat.rollover_enable = (num == 0);

   // prompt for high trip
   if (ConvertToF)
   {
      num = 80;
      if (!EnterNum("\nEnter high temperature threshold in Fahrenheit\n"
                    "Answer (-40 to 158):",3, &num, -40, 158))
         return FALSE;
      temp = (float)((num - 32.0) * 5.0 / 9.0);
   }
   else
   {
      num = 35;
      if (!EnterNum("\nEnter high temperature threshold in Celsius\n"
                    "Answer (-40 to 70):",3, &num, -40, 70))
         return FALSE;
      temp = (float)num;
   }

   // set in mission status structure
   ThermoState->MissStat.high_threshold = (uchar)(2 * (temp + 40));

   // prompt for low trip
   if (ConvertToF)
   {
      num = 32;
      if (!EnterNum("\nEnter low temperature threshold in Fahrenheit\n"
                    "Answer (-40 to 158):",3, &num, -40, 158))
         return FALSE;
      temp = (float)((num - 32.0) * 5.0 / 9.0);
   }
   else
   {
      num = 0;
      if (!EnterNum("\nEnter low temperature threshold in Celsius\n"
                    "Answer (-40 to 70):",3, &num, -40, 70))
         return FALSE;
      temp = (float)num;
   }

   // set in mission status structure
   ThermoState->MissStat.low_threshold = (uchar)(2 * (temp + 40));

   return TRUE;
}