Exemplo n.º 1
0
int main(int argc, char *argv[])
{
  int i = 0;
  char *progname = argv[0];
  if (argc < 2)
  {
    print_help(progname);
    exit(EXIT_FAILURE);
  }
  int maxSamples = atoi(argv[1]);

  // check for seed
  int initSeed = 0;
  if(argc >= 3)
  {
    initSeed = atoi(argv[2]);
    debug_print(1, "Initial seed of %i provided!\n", initSeed);
  }
  else
  {
    initSeed = 1000 + (time(NULL) % 9000);
    debug_print(1, "Using time(NULL) to obtain 4-digit seed\n", "");
  }

  debug_print(1, "Going through %i samples of middle square\n", maxSamples);
  debug_print(1, "Using initial seed value: %i\n", initSeed);

  int x = initSeed;
  int newSeed;
  int *storageArray;
  storageArray = (int *) malloc(sizeof(int) * maxSamples);
  for( i = 0; i < maxSamples; i++)
  {
    // take previous seed or initial number
    newSeed = x * x;
    debug_print(2, "%i * %i = %i\n", x, x, newSeed);

    // select the middle four digits as output and next seed
    int oldSeed = newSeed;
    newSeed = (newSeed / 100) % 10000;
    debug_print(2, "(%i / 100) %% 10000 = %i\n", oldSeed, newSeed);

    // set the new seed as previous seed for next iteration
    x = newSeed;

    // store as output as well
    storageArray[i] = x;

    debug_print(1, "byte %i of %i: 0x%.2X (%i)\n", i+1, maxSamples, x, x);
  }
  for( i = 0; i < maxSamples; i++)
  {
    printf(BYTETOBINARYPATTERN, BYTETOBINARY(storageArray[i]));
  }

  free(storageArray); storageArray = NULL;
  exit(EXIT_SUCCESS);
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
  int i = 0;
  unsigned long initSeed = 0;
  char *progname = argv[0];
  if (argc < 2)
  {
    print_help(progname);
    exit(EXIT_FAILURE);
  }
  int maxSamples = atoi(argv[1]);

  struct timeval tv_init;
  struct timeval tv;
  gettimeofday(&tv_init, NULL);

  int numOfKeystrokes = 10;
  if (argc >= 3)
  {
    numOfKeystrokes = atoi(argv[2]);
  }

  debug_print(2, "UINT_MAX is %lu\n", (unsigned long) UINT_MAX);
  for (i = 0; i < numOfKeystrokes; ++i)
  {
    int tmp = getchar();
    gettimeofday(&tv, NULL);

    debug_print(1, "Got char %i at usec %i\n", tmp, tv.tv_usec);
    initSeed = ((initSeed ^ tv.tv_usec) * tmp) % UINT_MAX;
    debug_print(1, "Seed currently %lu at round %i/10\n", initSeed, i+1);
  }

  debug_print(1, "Going through %i samples of C-based rand()\n", maxSamples);
  debug_print(1, "Using initial seed value: %lu\n", initSeed);
  lab2_srand((unsigned int) initSeed);

  int *storageArray;
  storageArray = (int *) malloc(sizeof(int) * maxSamples);
  for( i = 0; i < maxSamples; i++)
  {
    int x = lab2_rand();
    debug_print(1, "byte %i of %i: 0x%.2X (%i)\n", i+1, maxSamples, x, x);
    storageArray[i] = x;
  }
  for( i = 0; i < maxSamples; i++)
  {
    printf(BYTETOBINARYPATTERN, BYTETOBINARY(storageArray[i]));
  }

  free(storageArray); storageArray = NULL;
  exit(EXIT_SUCCESS);
}
Exemplo n.º 3
0
__interrupt void  ML13_interrupt(void)  {
    char c = *interrupt_address & 0x3F;
    printf ("Leading text "BYTETOBINARYPATTERN, BYTETOBINARY(c));
    puts("\t-Call from interrupt");

    if(c & 0x08)    {
        interruptType = SENSOR;
    }else if(c & 0x01)  {
        interruptType = OPENED_DOOR;
    }
    //Clears interrupt register
    *interrupt_address = 0x01;
}
Exemplo n.º 4
0
Arquivo: cmd_hub.c Projeto: lirihe/arm
int cmd_hub_knife(struct command_context *ctx) {

	char * args = command_args(ctx);
	unsigned int knife;
	unsigned int channel;
	unsigned int delay;
	unsigned int duration;
	nanohub_knivesdata_t knivesdata;

	printf("Input knife, channel, delay and duration\r\n");
	if (sscanf(args, "%u %u %u %u", &knife,&channel,&delay,&duration) == 4)
	{
		if (!hub_knife((uint8_t) knife,(uint8_t) channel, (uint16_t) delay, (uint16_t) duration, &knivesdata)) {
			printf("Error do deploy\r\n");
			return CMD_ERROR_FAIL;
		};
		printf("Knivesdata:\r\n knife %d channel %d is set to deploy in %d with duration %d\r\n",knife, channel,delay,duration);
	}
	else
	{
		if (!hub_knife(99, 0, 0, 0, &knivesdata)) {
			printf("Error get deploy\r\n");
			return CMD_ERROR_FAIL;
		}
		printf("Knivesdata:\r\n");
	}

	  printf("Knife0 timer:  %"PRIu32" %"PRIu32"\r\n",knivesdata.knife[0].timer[0],knivesdata.knife[0].timer[1]);
	  printf("Knife0 mode:   %"PRIu8" %"PRIu8"\r\n",knivesdata.knife[0].mode[0],knivesdata.knife[0].mode[1]);
	  printf("Knife0 burns:  %"PRIu8" %"PRIu8"\r\n",knivesdata.knife[0].burns[0],knivesdata.knife[0].burns[1]);
	  printf("Knife1 timer:  %"PRIu32" %"PRIu32"\r\n",knivesdata.knife[1].timer[0],knivesdata.knife[1].timer[1]);
	  printf("Knife1 mode:   %"PRIu8" %"PRIu8"\r\n",knivesdata.knife[1].mode[0],knivesdata.knife[1].mode[1]);
	  printf("Knife1 burns:  %"PRIu8" %"PRIu8"\r\n",knivesdata.knife[1].burns[0],knivesdata.knife[1].burns[1]);
	  printf("Burn status(%X): "BYTETOBINARYPATTERN"\r\n",knivesdata.burn_status, BYTETOBINARY(knivesdata.burn_status));
	  printf("Sense status: "BYTETOBINARYPATTERN"\r\n", BYTETOBINARY(knivesdata.sense_status));


	return CMD_ERROR_NONE;
}
Exemplo n.º 5
0
int testread()
{
	char *receive;
	int rec_size = llread(app.fd, &receive);
	
	//catch info
	//char endchar = receive[rec_size - 1];
	//receive[rec_size - 1] = 0;
	printf("\n");
	//printf("%s%c - %d", receive, endchar, rec_size);test string
	int i=0;
	for(;i<rec_size;++i)
	printf(PRINTBYTETOBINARY " - " , BYTETOBINARY(receive[i]));
	printf("%d",rec_size);
	//gets(receive);
	if (rec_size>0)free(receive);

	//catch DISC
	rec_size = llread(app.fd, &receive);
	if (rec_size == -2) printf("\ngot disk");

	return 0;
}
Exemplo n.º 6
0
int main(int argc, char **argv){

	set_opt(argc, argv); // prepare parameters
	
	init_arrays(); // init counters and flags
	
	prepare_flags(); // randomize flags
	
	int n;
	
	fprintf(stderr, "Begin calculation\n");
	for (n = 0; n < count; n++){
	  uint16_t flag = flags[n];
	  /* in for loop calculation (very slow)
	    (flag & 0x8000 ? counters[0]++ : 0);
	    (flag & 0x4000 ? counters[1]++ : 0);
	    (flag & 0x2000 ? counters[2]++ : 0);
	    (flag & 0x1000 ? counters[3]++ : 0);
	    (flag & 0x0800 ? counters[4]++ : 0);
	    (flag & 0x0400 ? counters[5]++ : 0);
	    (flag & 0x0200 ? counters[6]++ : 0);
	    (flag & 0x0100 ? counters[7]++ : 0);
	    (flag & 0x0080 ? counters[8]++ : 0);
	    (flag & 0x0040 ? counters[9]++ : 0);
	    (flag & 0x0020 ? counters[10]++ : 0);
	    (flag & 0x0010 ? counters[11]++ : 0);
	    (flag & 0x0008 ? counters[12]++ : 0);
	    (flag & 0x0004 ? counters[13]++ : 0);
	    (flag & 0x0002 ? counters[14]++ : 0);
	    (flag & 0x0001 ? counters[15]++ : 0);
	    */
	  /* simple shift (fast)*/
	  counters[0] += (flag & 0x8000) >> 15;
	  counters[1] += (flag & 0x4000) >> 14;
	  counters[2] += (flag & 0x2000) >> 13;
	  counters[3] += (flag & 0x1000) >> 12;
	  counters[4] += (flag & 0x0800) >> 11;
	  counters[5] += (flag & 0x0400) >> 10;
	  counters[6] += (flag & 0x0200) >> 9;
	  counters[7] += (flag & 0x0100) >> 8;
	  counters[8] += (flag & 0x0080) >> 7;
	  counters[9] += (flag & 0x0040) >> 6;
	  counters[10] += (flag & 0x0020) >> 5;
	  counters[13] += (flag & 0x0010) >> 4;
	  counters[11] += (flag & 0x0008) >> 3;
	  counters[12] += (flag & 0x0004) >> 2;
	  counters[14] += (flag & 0x0002) >> 1;
	  counters[15] += (flag & 0x0001);
          
	  
	  /*   shift in loops (slow)
	  int k;
	  int shift = 1;
	  for (k = 0; k<16; k++){
	    counters[k] += (flag & (shift << (15-k))) >> (15-k) ;
	  }
	    */
  
	  DEBUG_TRACE(("DEBUG: FLAG[%d] = [%04X]\n", n, flag));
	}
	fprintf(stderr, "End calculation\n");
	
    #ifdef DEBUG
	int i;
	for (i = 0; i < 16; i++){
	  DEBUG_TRACE(("DEBUG: Counter[%d] = [%d]\n", i, counters[i]));
	}
	
	for (i = 0; i < count; i++){
	  DEBUG_TRACE(("DEBUG: FLAGS[%d] = (%04X)\t"BYTETOBINARYPATTERN"\n", i, flags[i], BYTETOBINARY(flags[i])));
	}
	  DEBUG_TRACE(("\n"));
	  DEBUG_TRACE(("DEBUG: COUNT[1..16] =\t\t"));
	  
	  int j;
	  for (j=0; j<16; j++){
	    ( j!=0 && j%4 == 0 ) ? DEBUG_TRACE((" %d", counters[j])) : DEBUG_TRACE(("%d", counters[j]));
	  }
	  DEBUG_TRACE(("\n\n"));
    #endif
	  
	int c;
	for (c=0; c<16; c++){
	  printf("Counter(%02d) = %08d\n", c+1, counters[c]);
	}
	free(flags);
	exit(EXIT_SUCCESS);
}
Exemplo n.º 7
0
/*
 * Reads a UTF-8 codepoint. The string must be null terminated.
 */
static inline int read_utf8_codepoint(char *str, uint32_t *codepoint_out) {
  unsigned char byte;
  uint32_t codepoint;
  int bytes, i;
  byte = *str;
  if (byte & 0x80) {// UTF-8 multibyte
    // determine the number of bytes in the multibyte
    if ((byte & 0xE0) == 0xC0) {
      bytes = 2;
      codepoint = ((uint32_t)(byte & 31)) << 6;
    } else if ((byte & 0xF0) == 0xE0) {
      bytes = 3;
      codepoint = ((uint32_t)(byte & 15)) << 12;
    } else if ((byte & 0xF8) == 0xF0) {
      bytes = 4;
      codepoint = ((uint32_t)(byte & 7)) << 18;
    } else if ((byte & 0xFC) == 0xF8) {
      bytes = 5;
      codepoint = ((uint32_t)(byte & 3)) << 24;
    } else if ((byte & 0xFE) == 0xFC) {
      bytes = 6;
      codepoint = ((uint32_t)(byte & 1)) << 30;
    } else { // bad byte!
      if ((byte & 0xC0) == 0x80) {
        fprintf(stderr, "The bit pattern 10xxxxxx is illegal for the first "
            "byte of a UTF-8 multibyte.\n");
      } else if (byte == 0xFE) {
        fprintf(stderr, "The byte 0xFE is illegal for UTF-8.\n");
      } else {
        fprintf(stderr, "The byte 0xFF is illegal for UTF-8.\n");
      } 
      exit(1);
      // stop compiler complaints
      bytes = 1;
      codepoint = 0;
    }
    // read the rest of the bytes
    for (i = 1; i < bytes; i++) {
      if (str[i] == '\0') {
        return i - bytes;
      }
      byte = str[i];
      if ((byte & 0xC0) != 0x80) {
        fprintf(stderr, "Expected the bit pattern 10xxxxxx for a following "
            "byte of a UTF-8 multibyte.\n");
        exit(1);
      }
      codepoint = codepoint | (((uint32_t)(byte & 0x3F)) << (6 * (bytes - i - 1)));
    }
    // check for overlong representations by seeing if we could have represented
    // the number with one less byte
    if (bytes > 1 && codepoint < (1 << (bytes == 2 ? 7 : (6 * (bytes - 2) + (8 - bytes))))) {
      fprintf(stderr, "The UTF-8 multibyte uses too many bytes (%d) for the "
          "codepoint (%zu) it represents.\n", bytes, codepoint);
      for (i = 0; i < bytes; i++) {
        fprintf(stderr, BYTETOBINARYPATTERN " ", BYTETOBINARY((unsigned char)str[i]));
      }
      fputs("\n", stderr);
      exit(1);
    }
  } else { // ASCII byte
    bytes = 1;
    codepoint = byte;
  }
  *codepoint_out = codepoint;
  return bytes;
}
Exemplo n.º 8
0
/*******************************************************************
* Function:			void map (void)
* Input Variables:	none
* Output Return:	none
* Overview:			Makes the robot map the world
********************************************************************/
void map (void)
{
	// Initialize State
	isMapping = 1;
	
	// Mapping Loop
	while(isMapping)
	{	
		//Sense
		checkIR();	
		checkWorld();
		
		//Record
		setGateways();
				
		//Plan using the Map
		planMap();
		
		//Act on the Map
		moveMap();
		
		//Shift the Map
		currentCellWorld = shiftMap(currentCellWorld, currentMove, currentOrientation);
		
		//Break?
		isMapping = !((currentCellWorldStart == currentCellWorld)&&(currentOrientationStart == currentOrientation));
		if(!isMapping){			
			break;
		}
		
		//Print Map
		LCD_clear();
		LCD_printf("      Move"BYTETOBINARYPATTERN"\n      Cell"BYTETOBINARYPATTERN"\n      Ornt"BYTETOBINARYPATTERN"\n\n",BYTETOBINARY(currentMove),BYTETOBINARY(currentCellWorld),BYTETOBINARY(currentOrientation));
		printMap(currentOrientation,currentCellWorld,RESET);
		TMRSRVC_delay(500);//wait 3 seconds
	}
	
}
Exemplo n.º 9
0
/*******************************************************************
* Function:			char getGateways(void)
* Input Variables:	void
* Output Return:	void
* Overview:		    Interpolates the list of gateways in the path by
					using the map and initial conditions 
********************************************************************/
void getGateways(void)
{
	// Get the start location of the robot
	// unsigned char curRow = (currentCellWorld>>2) & 0b1100;
	// unsigned char curCol = currentCellWorld & 0b0011;
	
	unsigned char curRow = currentCellWorld >> 2;
	unsigned char curCol = currentCellWorld & 0b0011;
	
	// Git the start orientation of the robot
	unsigned char curOrient = currentOrientation;
	
	// This will be the gatway the robot will look for
	unsigned char curCell;
		
	// This will be the move the robot will preform
	unsigned char curMove;
	
	// This is the index of the move we are looking at
	unsigned char j;
	
	for (j = 0; j<=MAX_MOVE_SIZE; j++)
	{	// Get the current move
		curMove = moveCommands[j];
		
		// Get the current cell
		curCell = ROBOT_WORLD[curRow][curCol];
		
		// Rotate the cell with reference to the robot
		curCell = rotateCell(curCell,curOrient,1);
		
		// Store the cell as a searchable gateway
		moveGateways[j] = curCell;
				
		// If we are moving forward
		// move to the next cell with respect to our orientation
		if (curMove == MOVE_FORWARD){
			switch(curOrient){
				case NORTH:
					curRow -= 1;
					break;
				case EAST:
					curCol += 1;
					break;
				case SOUTH:
					curRow += 1;					
					break;
				case WEST:
					curCol -= 1;					
					break;
				default:
					break;
			}			
		}
		//	If we are turning right
		// then rotate our map orientation appropriately
		else if (curMove == MOVE_RIGHT){
			curOrient++;
			curOrient = curOrient&0b11;
			// LCD_clear();
			// LCD_printf("Num:\n%i\curOrient:\n"BYTETOBINARYPATTERN,j,BYTETOBINARY(curOrient));
			// TMRSRVC_delay(500);//wait 1/2 seconds
		}
		//	If we are turning left
		// then rotate our map orientation appropriately
		else if (curMove == MOVE_LEFT){
			// if(curOrient == 0){
				// curOrient = 0b0011;
			// }
			curOrient--;
			curOrient = curOrient&0b11;
		}			
	}
	
	for (j = 0; j<=MAX_MOVE_SIZE; j++)
	{
		curCell = moveGateways[j];
		LCD_clear();
		LCD_printf("Num:\n%i\nCurCell:\n"BYTETOBINARYPATTERN,j,BYTETOBINARY(curCell));
		TMRSRVC_delay(1000);//wait 1 second
	}
}
Exemplo n.º 10
0
static void pmic_dump_registers(int dump_mask)
{
    int i, retval = 0, ret;
    uint8_t reg_val;
    uint16_t chk_reg_addr;
    uint16_t reg_addr_boot[] = {MSIC_BATT_RESETIRQ1_ADDR,
        MSIC_BATT_RESETIRQ2_ADDR, MSIC_BATT_CHR_LOWBATTDET_ADDR,
        MSIC_BATT_CHR_SPCHARGER_ADDR, MSIC_BATT_CHR_CHRTTIME_ADDR,
        MSIC_BATT_CHR_CHRCTRL1_ADDR, MSIC_BATT_CHR_CHRSTWDT_ADDR,
        MSIC_BATT_CHR_CHRSAFELMT_ADDR};
    char *reg_str_boot[] = {"rirq1", "rirq2", "lowdet",
                "spchr", "chrtime", "chrctrl1",
                "chrgwdt", "safelmt"};
    uint16_t reg_addr_int[] = {MSIC_BATT_CHR_PWRSRCINT_ADDR,
        MSIC_BATT_CHR_PWRSRCINT1_ADDR, MSIC_BATT_CHR_CHRINT_ADDR,
        MSIC_BATT_CHR_CHRINT1_ADDR, MSIC_BATT_CHR_PWRSRCLMT_ADDR};
    char *reg_str_int[] = {"pwrint", "pwrint1", "chrint",
                "chrint1", "pwrsrclmt"};
    uint16_t reg_addr_evt[] = {MSIC_BATT_CHR_CHRCTRL_ADDR,
        MSIC_BATT_CHR_CHRCVOLTAGE_ADDR, MSIC_BATT_CHR_CHRCCURRENT_ADDR,
        MSIC_BATT_CHR_SPWRSRCINT_ADDR, MSIC_BATT_CHR_SPWRSRCINT1_ADDR,
        CHR_STATUS_FAULT_REG};
    char *reg_str_evt[] = {"chrctrl", "chrcv", "chrcc",
                "spwrsrcint", "sprwsrcint1", "chrflt"};

    uint16_t reg_addr_others[] = {MSIC_BATT_CHR_MPWRSRCINT_ADDR,
        MSIC_BATT_CHR_MPWRSRCINT1_ADDR, MSIC_BATT_CHR_MCHRINT_ADDR,
        MSIC_BATT_CHR_MCHRINT1_ADDR, MSIC_BATT_CHR_VBUSDET_ADDR,
        MSIC_BATT_CHR_WDTWRITE_ADDR};
    char *reg_str_others[] = {"chrmpwrsrcint", "chrmpwrsrcint1", "chrmchrint",
                "chrmchrint1", "chrvbusdet", "chrwdtwrite"};

    if (dump_mask & MSIC_CHRG_REG_DUMP_BOOT) {
        for (i = 0; i < ARRAY_SIZE(reg_addr_boot); i++) {
            retval = intel_scu_ipc_ioread8(reg_addr_boot[i], &reg_val);
            if (retval) {
                chk_reg_addr = reg_addr_boot[i];
                goto ipcread_err;
            }
            BAT_DBG("PMIC(0x%03x)\tval: " BYTETOBINARYPATTERN "\t%s\n", reg_addr_boot[i],
                                BYTETOBINARY(reg_val), reg_str_boot[i]);
        }
    }
    if (dump_mask & MSIC_CHRG_REG_DUMP_INT) {
        for (i = 0; i < ARRAY_SIZE(reg_addr_int); i++) {
            retval = intel_scu_ipc_ioread8(reg_addr_int[i], &reg_val);
            if (retval) {
                chk_reg_addr = reg_addr_int[i];
                goto ipcread_err;
            }
            BAT_DBG("PMIC(0x%03x)\tval: " BYTETOBINARYPATTERN "\t%s\n", reg_addr_int[i],
                                BYTETOBINARY(reg_val), reg_str_int[i]);
        }
    }
    if (dump_mask & MSIC_CHRG_REG_DUMP_EVENT) {
        for (i = 0; i < ARRAY_SIZE(reg_addr_evt); i++) {
            retval = intel_scu_ipc_ioread8(reg_addr_evt[i], &reg_val);
            if (retval) {
                chk_reg_addr = reg_addr_evt[i];
                goto ipcread_err;
            }
            BAT_DBG("PMIC(0x%03x)\tval: " BYTETOBINARYPATTERN "\t%s\n", reg_addr_evt[i],
                                BYTETOBINARY(reg_val), reg_str_evt[i]);
        }
    }
    if (dump_mask & MSIC_CHRG_REG_DUMP_OTHERS) {
        for (i = 0; i < ARRAY_SIZE(reg_addr_others); i++) {
            retval = intel_scu_ipc_ioread8(reg_addr_others[i], &reg_val);
            if (retval) {
                chk_reg_addr = reg_addr_others[i];
                goto ipcread_err;
            }
            BAT_DBG("PMIC(0x%03x)\t val: " BYTETOBINARYPATTERN "\t%s\n", reg_addr_others[i],
                                BYTETOBINARY(reg_val), reg_str_others[i]);
        }
    }

	/*modify MSIC_BATT_CHR_LOWBATTDET_ADDR values [7:6]='11'*/
	intel_scu_ipc_ioread8(MSIC_BATT_CHR_LOWBATTDET_ADDR, &reg_val);
	reg_val |= (BIT(6)|BIT(7));
	ret = intel_scu_ipc_iowrite8(MSIC_BATT_CHR_LOWBATTDET_ADDR, reg_val);
	if (ret) {
		BAT_DBG_E("PMIC register MSIC_BATT_CHR_LOWBATTDET_ADDR Failed to write %d\n", ret);
	}else {
		intel_scu_ipc_ioread8(MSIC_BATT_CHR_LOWBATTDET_ADDR, &reg_val);
		BAT_DBG("PMIC register 0x%x Success to write 0x%x.\n", MSIC_BATT_CHR_LOWBATTDET_ADDR, reg_val);
	}

    return;

ipcread_err:
    BAT_DBG("ipcread_err: address: 0x%03x!!!", chk_reg_addr);
}
Exemplo n.º 11
0
int main(void)
{
    // C MUST BE PRIME to ensure that it is co-prime with other values
	const BYTE C = 0x07;
    
    // D MUST be assigned the same value as Y
	BYTE D = 0x00;

    // Variable for the X, Y, etc. values calculated by the recipient
    BYTE calculated_X = 0x00;
    BYTE calculated_Y = 0x00;
    BYTE calculated_Q = 0x00;
    BYTE calculated_R = 0x00;
    BYTE calculated_Z = 0x00;
    BYTE calculated_Zprime = 0x00;
	
    bool root_found = false;
    
    BYTE Q = 0x00;
    BYTE R = 0x00;
    
    BYTE Z1 = 0x00;
    BYTE Z2 = 0x00;
    
    BYTE sender_Z1prime = 0x00;
    BYTE sender_Z2prime = 0x00;
    
    BYTE recipient_Zprime = 0x00;
    
    // Iterate through every possible combination of the X and Y byte values to verify
    // that it is possible to recover X and Y from Z given ANY combinations of values
    for (BYTE X = 0x00; X < 0x10; ++X)
    {
        // For each value of X iterate for each value of Y incremented by 0x01
        for (BYTE Y = 0x00; Y < 0x10; ++Y)
        {
            /**
             * Simulate the sender compressing the 2 bytes into a single byte, Z and
             * then sending Z to the recipient
             */
            D = Y;  // D MUST be same value as Y, D is not known to recipient
            Q = calcQ(&X, &Y, &C);
            R = calcR(&X, &Y, &D);
            
            // Calculate Z using both equivalent methods, Z = R XOR C = Q XOR D
            Z1 = calcZ(&R, &C);
            Z2 = calcZ(&Q, &D);
            
            // Calcualte Z' using both equivalent methods, Z' = R XNOR C = Q XNOR D
            sender_Z1prime = calcZprime(&R, &C);
            sender_Z2prime = calcZprime(&Q, &D);
            
            // Verify that Z using both methods is equivalent (This is an AXIOM OF THE
            // COMPRESSION ALGORITHM) so if it's wrong we have a major flaw
            if (Z1 != Z2)
            {
                printf("Z1 NOT EQUIVALENT TO Z2!\n");
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("Z1:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
                printf("Z2:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z2)), 0xF0 ^ Z2);
            }
            
            // Verify that Z' using both methods is equivalent (This is an AXIOM OF THE
            // COMPRESSION ALGORITHM) so if it's wrong we have a major flaw           
            if (sender_Z1prime != sender_Z2prime)
            {
                printf("Z1 PRIME NOT EQUIVALENT TO Z2 PRIME!\n");
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("Z1 PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ sender_Z1prime)), 0xF0 ^ sender_Z1prime);
                printf("Z2 PRIME:\t\t"BYTETOBINARYPATTERN", %d\n\n", BYTETOBINARY((0xF0 ^ sender_Z2prime)), 0xF0 ^ sender_Z2prime);            
            }
            
               
            /**
             * Simulate the recipient receiving a single byte, Z and getting the original
             * bytes X and Y from Z, the recipient knows the constant value C, which is prime
             */
             // The user can get Z' which is ~Z, this is the UNIQUE property that makes it 
             // possible to recover X and Y by using the equations of for Z and Z' given the
             // two values Z and Z' = ~Z
             recipient_Zprime = ~(Z1);
             
             // Verify that the Zprime the recipient gets from ~Z is ACTUALLY, the same Zprime
             // that the sender calculated using the equations (This is an AXIOM OF THE
             // COMPRESSION ALGORITHM), so if it's wrong we have a major flaw
            if (recipient_Zprime != sender_Z1prime)
            {
                printf("RECIPIENT'S Z PRIME NOT EQUIVALENT TO SENDER'S Z PRIME!!!\n");
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("Q:\t\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("R:\t\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
                printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
            }

            // Now the user can calculate X very easily given a unique property when C is prime and D == Y
            // The user has RECEIVED Z and BOTH recipient and sender KNOW C, UNKNOWN is Y == D
            calculated_X = calcX(&Z1, &C);
            
            // Verify that the CALCULATED X IS ACTUALLY EQUAL to the Sender's X value the recipients' 
            // calculated X should ALWAYS be equivalent to the sender's X when Y == D (This is an AXIOM OF THE
            // COMPRESSION ALGORITHM), so if it's wrong we have a major flaw
            if (calculated_X != X)
            {
                printf("RECIPIENT'S CALCULATED X NOT EQUIVALENT TO SENDER'S X!!!\n");
                printf("Q:\t\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("R:\t\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("RECIPIENT X:\t"BYTETOBINARYPATTERN"\t\t: %d\n\n", BYTETOBINARY(calculated_X), calculated_X);
            }
            
            /**
            calculated_Y = 0x00;
            calculated_Q = 0x00;
            calculated_R = 0x00;
            calculated_Z = 0x00;
            calculated_Zprime = 0x00;
            root_found = false;
            
            calculated_Q = calcQ(&calculated_X, &Y, &C);
            calculated_R = calcR(&calculated_X, &Y, &Y);

            calculated_Z = calcZ(&calculated_R, &C);
            calculated_Zprime = calcZprime(&calculated_Q, &Y);
            
            calculated_Y = calcY(&calculated_Zprime, &calculated_Q);

            printf("SENDER Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
            printf("CALCULATED Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Q)), 0xF0 ^ calculated_Q);
            printf("SENDER R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
            printf("CALCULATED R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_R)), 0xF0 ^ calculated_R);
            printf("SENDER Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
            printf("CALCULATED Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Z)), 0xF0 ^ calculated_Z);
            printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
            printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
            printf("CALCULATED Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(calculated_Zprime), calculated_Zprime);
            printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
            printf("RECIPIENT X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n\n", BYTETOBINARY(calculated_X), 
                    BYTETOBINARY(calculated_Y), calculated_X, calculated_Y);
            */
            // Now solve for the unknown value Y, given the Z' value, X, and C, the only way to feasibly solve
            // for Y is to perform a bruteforce search given the known values and the equations for
            // Z' and Q. THERE MUST ONLY BE ONE ROOT, Y, GIVEN THE ROOT X , Z' and C (This is an AXIOM OF THE
            // COMPRESSION ALGORITHM), so if it's wrong we have a major flaw, in fact its IMPOSSIBLE to
            // reverse the compression unless Y == D is UNIQUE!!!!
            for (BYTE Y_TEST = 0x00; Y_TEST < 0x10; ++Y_TEST)
            {
                calculated_Q = calcQ(&calculated_X, &Y_TEST, &C);
                calculated_R = calcR(&calculated_X, &Y_TEST, &Y_TEST);
                
                calculated_Z = calcZ(&calculated_R, &C);
                calculated_Zprime = calcZprime(&calculated_Q, &Y_TEST);
                
                //printf("Calculated X: %d\n", calculated_X);
                //printf("Calculated Y: %d\n", Y_TEST);
                //printf("Calculated Q: %d\n", 0xF0 ^ calculated_Q);
                //printf("Calculated R: %d\n", 0xF0 ^ calculated_R);
                //printf("Calculated Z: %d\n",  0xF0 ^ calculated_Z);
                //printf("Calculated Z PRIME: %d\n\n", calculated_Zprime);
                
                /**
                printf("SENDER Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("CALCULATED Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Q)), 0xF0 ^ calculated_Q);
                printf("SENDER R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("CALCULATED R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_R)), 0xF0 ^ calculated_R);
                printf("SENDER Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
                printf("CALCULATED Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Z)), 0xF0 ^ calculated_Z);
                printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
                printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
                printf("CALCULATED Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(calculated_Zprime), calculated_Zprime);
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("RECIPIENT X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n\n", BYTETOBINARY(calculated_X), 
                    BYTETOBINARY(Y_TEST), calculated_X, Y_TEST);
                */
                
                // If the calculated Z' given the calculated root Y matches the known Z' which is 
                // Z' = ~Z, then the root Y satisfies the equations and IS THE ROOT. Remember that
                // for the sender as well as the recipient Y == D, this is the unique property that
                // makes it possible to avoid the 0000/1111 cancellations of XOR              
                if (calculated_X == calcX(&calculated_Z, &C))
                {
                    if (Y_TEST == calcY(&calculated_Zprime, &calculated_Q))
                    {
                        if (Z1 == calculated_Z && recipient_Zprime == calculated_Zprime)
                        {
                            // X XOR Y SHOULD NOT BE EQUIVALENT TO Z, ONLY THE UNIQUE ROOT IS NOT A COMBINATION
                            // OF X XOR Y = Z
                            //if (Y_TEST == Y)
                            if ((calculated_X ^ Y_TEST) != calculated_Z)
                            {
                                calculated_Y = Y_TEST;
                                root_found = true;
                                break;
                            }
                        }
                    }
                }
            }
            // If the correct root is found I will be DELIGHTED, the calculated Y MUST
            // be UNIQUE and equivalent to the initial Y value of the sender, if it does
            // not match we have a major flaw,
            if (calculated_Y == Y)
            {
                printf("RECIPIENT'S CALCULATED Y EQUIVALENT TO SENDER'S Y!!!!!!!!!!!!!!!!!!!!\n");
                printf("SENDER Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("CALCULATED Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Q)), 0xF0 ^ calculated_Q);
                printf("SENDER R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("CALCULATED R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_R)), 0xF0 ^ calculated_R);
                printf("SENDER Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
                printf("CALCULATED Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Z)), 0xF0 ^ calculated_Z);
                printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
                printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
                printf("CALCULATED Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(calculated_Zprime), calculated_Zprime);
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("RECIPIENT X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n", BYTETOBINARY(calculated_X), 
                    BYTETOBINARY(calculated_Y), calculated_X, calculated_Y);
            }
            // major flaw, back to the drawing board!
            else
            {
                printf("RECIPIENT'S CALCULATED Y NOT EQUIVALENT TO SENDER'S Y!!!!!!!!!!!!!!!!!!!!\n");
                printf("SENDER Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("CALCULATED Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Q)), 0xF0 ^ calculated_Q);
                printf("SENDER R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("CALCULATED R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_R)), 0xF0 ^ calculated_R);
                printf("SENDER Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
                printf("CALCULATED Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Z)), 0xF0 ^ calculated_Z);
                printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
                printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
                printf("CALCULATED Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(calculated_Zprime), calculated_Zprime);
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("RECIPIENT X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n", BYTETOBINARY(calculated_X), 
                    BYTETOBINARY(calculated_Y), calculated_X, calculated_Y);
            }
        }
            /**
            Zprime = calcZprime(&Q, &D);
            
            calculated_Y = calcY(&Zprime, &Q);

            if ( X == calculated_X && Y == calculated_Y)
            {
            printf("Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
            printf("R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
            printf("Z1:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
            printf("Z2:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z2)), 0xF0 ^ Z2);

            );
            //printf("Z':\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(Zprime), Zprime);

            printf("Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(Q), Q); 
            printf("R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(R), R);
            printf("Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(Z), Z);
            printf("Z':\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(Zprime), Zprime);

            printf("INITIAL X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
            printf("SOLVED X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n", 
            BYTETOBINARY(calculated_X), BYTETOBINARY(calculated_Y), calculated_X, calculated_Y);
            }*/
    }
	return 0;
}
Exemplo n.º 12
0
int main(){



	int fd = spi_open("/dev/spidev0.0");
	spi_mode(fd, SPI_MODE_1);
	// spi_mode(fd,  SPI_NO_CS | SPI_MODE_2);
	spi_speed(fd, MHZ_32);
	dumpstat("SPI device 0: ",fd);

	// uint16_t command = MCP3208_START_BIT | MCP3208_SINGLE_ENDED

	printf("size %d\n", sizeof(MCP_CHAN_7));

	// uint32_t foo = MCP_CHAN_7;
	// uint32_t mcp_tx = ((uint32_t)MCP_CHAN_7) << 14;
	// uint32_t mcp_rx = 0;

	uint8_t tx[] = {1,8 + 0 << 4,0,0};
	uint8_t rx[4] = {0}; 


	struct spi_ioc_transfer mcp_tr ={
		.tx_buf = (unsigned long)&tx,
		.rx_buf = (unsigned long)&rx,
		.len = 4,
		.delay_usecs = 0,
		.speed_hz = KHZ_500,
		.bits_per_word = 8,
	};


	while(1){
		spi_transfer(fd, &mcp_tr, 1);

		// printf ("B "BYTETOBINARYPATTERN" ", BYTETOBINARY(mcp_tx >> 24));
		// printf (""BYTETOBINARYPATTERN" ", BYTETOBINARY(mcp_tx >> 16));
		// printf (""BYTETOBINARYPATTERN" ", BYTETOBINARY(mcp_tx >> 8));
		// printf (""BYTETOBINARYPATTERN" ", BYTETOBINARY(mcp_tx));
		// printf("\tbuf_tx: %u\n", mcp_tx);

		// mcp_rx |= 0xff000000;
		// mcp_rx &= 0x00ffffff;
		// mcp_rx = mcp_rx >> 0;

		uint32_t ret = 0;
		ret |= (uint32_t)rx[3];
		ret |= (uint32_t)rx[2] << 8;
		ret |= (uint32_t)rx[1] << 16;
		// ret |= (uint32_t)rx[0] << 24;

		print_byte(rx[0]);
		print_byte(rx[1]);
		print_byte(rx[2]);
		print_byte(rx[3]);
		printf(" rx %d", ret >> 6);
		printf("\n");

		// printf("%d\n", rx);

		// printf ("\nA "BYTETOBINARYPATTERN" ", BYTETOBINARY(mcp_rx >> 24));
		// printf (""BYTETOBINARYPATTERN" ", BYTETOBINARY(mcp_rx >> 16));
		// printf (""BYTETOBINARYPATTERN" ", BYTETOBINARY(mcp_rx >> 8));
		// printf (""BYTETOBINARYPATTERN" ", BYTETOBINARY(mcp_rx));
		// printf("\tbuf_rx: %u\n", mcp_rx);

		nanosleep(&requested,&remaining);
	}

	// uint16_t command = get_mcp_channel_command(0);
	// printf ("Byte 00 "BYTETOBINARYPATTERN"\n", BYTETOBINARY((uint8_t)command));
	// printf ("Byte 01 "BYTETOBINARYPATTERN"\n", BYTETOBINARY((uint8_t)command >> 8));

	// printf ("Byte 00 "BYTETOBINARYPATTERN"\n", BYTETOBINARY((uint8_t)0x8));
	// printf ("Byte 01 "BYTETOBINARYPATTERN"\n", BYTETOBINARY((uint8_t)0x10));
	// printf("command %d %s\n", command, &buf);

	// dumpstat("SPI device 0: ",fd);

	spi_close(fd);

}

static void print_byte(uint8_t byte){
	printf (""BYTETOBINARYPATTERN" ", BYTETOBINARY(byte));
}