Exemplo n.º 1
0
// Wraper around original SCardTransmit that does the actuall logging
LONG WINAPI HookedSCardTransmit(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, LPSCARD_IO_REQUEST pioRecvPci, LPBYTE pbRecvBuffer,LPDWORD pcbRecvLength){
	LONG result = NULL;
	char *pcSendBufferHex = (char *)malloc(cbSendLength*3+1);
	char *pcRecvBufferHex;
	DWORD dwCount;
	HANDLE hLogFile;
	//log send buffer
	byte2hex(pbSendBuffer,cbSendLength,pcSendBufferHex);
	hLogFile = CreateFile(pathToLog, GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if(hLogFile != INVALID_HANDLE_VALUE){
		SetFilePointer(hLogFile, 0, 0, FILE_END);
		WriteFile(hLogFile, "Winscard!SCardTransmit:\n", 24, &dwCount,NULL);
		WriteFile(hLogFile, ">>> ", 4, &dwCount,NULL);
		WriteFile(hLogFile, pcSendBufferHex,strlen(pcSendBufferHex) , &dwCount,NULL);
		WriteFile(hLogFile, "\r\n",2 , &dwCount,NULL);
	}
	free(pcSendBufferHex);
	//invoke original
	if(OrigSCardTransmit != NULL){
		result = (*OrigSCardTransmit)(hCard,pioSendPci,pbSendBuffer,cbSendLength,pioRecvPci,pbRecvBuffer,pcbRecvLength);
	}
	//log receive buffer
	pcRecvBufferHex = (char *)malloc(*pcbRecvLength*3+1);
	byte2hex(pbRecvBuffer,*pcbRecvLength,pcRecvBufferHex);
	if(hLogFile != INVALID_HANDLE_VALUE){
		WriteFile(hLogFile, "<<< ", 4, &dwCount,NULL);
		WriteFile(hLogFile, pcRecvBufferHex,strlen(pcRecvBufferHex) , &dwCount,NULL);
		WriteFile(hLogFile, "\r\n",2 , &dwCount,NULL);
		CloseHandle(hLogFile);
	}
	free(pcRecvBufferHex);
	return result;
}
Exemplo n.º 2
0
char *bin2hex(unsigned char *bin, char *hex, int len) {
	int i;	
	for(i = 0; i < len; i++) {
		unsigned char byte = *bin++;
		unsigned char hbyte = (byte & 0xF0) >> 4;
		unsigned char lbyte = byte & 0xF;
		*hex++ = byte2hex(hbyte);	
		*hex++ = byte2hex(lbyte);	
	}

	*hex++ = 0;

	return hex;
}
Exemplo n.º 3
0
//
// Update keyboard status and issue events on it if necessary.
// This function doesn't block, but is normally only called when
// there is known to be some data waiting to be read from the keyboard.
//
BOOL GsCheckKeyboardEvent(void)
{
	MWKEY		ch;		// latest character
	MWKEYMOD	modifiers;	// latest modifiers 
	MWSCANCODE	scancode;	// latest modifiers 
	int		keystatus;	// latest keyboard status 

	// Read the latest keyboard status: 
display_puts("key read\n");
	keystatus = GdReadKeyboard(&ch, &modifiers, &scancode);
	if(keystatus < 0) {
		if(keystatus == -2)	// special case for ESC pressed
			GsTerminate();
		display_puts("Kbd error\n");
		return FALSE;
	} else if(keystatus) { // Deliver events as appropriate: 
		byte2hex(modifiers,s);
		display_puts("kbd '");
		display_putc(ch);
		display_puts("',");
		display_puts(s);
		display_puts("\n");
		return TRUE;
	}
	return FALSE;
}
Exemplo n.º 4
0
void main(int argc, char**argv)
{
  int ch,i,j;
  FILE *fp1;
  char *pathname;
  char hexnum[]  = { "        " };
  char hexdata[]  = { "[ 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F]" };
  char chardata[] = { "[0123456789ABCDEF]" };

  if(MAXINTx2 < 2*sizeof(int)) {
    printf("MAXINTx2 is not large enough!");
    exit(1);
  }

  if(argc>2) {
    printf("Too many parameters!\n");
    exit(1);
  }
  else if(argc==2)
    pathname = argv[1];
  else {
    pathname = calloc(MAXPATH, sizeof(char));
    printf("Input filename: ");
    gets(pathname);
  }
  printf("File: %s\n",pathname);
  if( *pathname == '\0' ) {
    printf("You did not give a file name!\n");
    exit(1);
  }
  fp1 = fopen(pathname, "r");
  if (fp1 == NULL) {
    printf("cannot open %s\n", pathname);
    exit(1);
  }

  printf(" %s  %s %s\n\n",hexnum,hexdata,chardata);
  for (i=0; ( (ch=fgetc(fp1)) != EOF) ; i++) {    
    byte2hex(ch,&hexdata[3*(i%16)+1]);
    chardata[i%16+1] = pablechar(ch);
    if ( (i%16)==15 )
      printf(" %s  %s %s\n",int2hex((i/16)*16,hexnum),hexdata,chardata);
  }
  if( ((i-1)%16)!=15 ) {
    for(j=(i%16);j<16;j++) {
      chardata[ j+1] = ' ';
      hexdata[3*j+1] = ' ';
      hexdata[3*j+2] = ' ';
    }
    printf(" %s  %s %s\n",int2hex((i/16)*16,hexnum),hexdata,chardata);
  }

  fclose(fp1);
  exit(0);

}
Exemplo n.º 5
0
static void
put_hex(char *hex, unsigned char *bp, int nbytes)
{
	int nhex = nbytes * 2;

	byte2hex(hex, bp, nbytes);
	hex[nhex] = '\n';
	_write(1, hex, nhex + 1);
	_exit(0);
}
Exemplo n.º 6
0
char *int2hex(int n, char *string)
{
  int i,temp;
  char *ch;

  temp = n;
  ch   = string + 2*sizeof(int);
  *ch  = '\0';
  ch   = ch-2;
  for (i=0; i<sizeof(int); i++, ch=ch-2) {
    byte2hex(temp,ch);
    temp = temp/256;
  }
  return(string);
}
Exemplo n.º 7
0
char ir_tmr_isr(void)
{
	char ok;

	// capture received frame in separate variables (maybe new IR bits would flow in)
	final_ircode = ircode;
	final_nbits = nbits;
	final_protocol = protocol;
	ok = final_protocol != IR_error && final_protocol != IR_unknown && nbits_ok;

	if (ok)
	{
		byte2hex( usb_tmr_msg+3, (byte)(ircode>>8));
		byte2hex( usb_tmr_msg+5, (byte)(ircode&0x00ff));
	} else
	{
Exemplo n.º 8
0
static void dump(char *addr)
{
  int i,j;
  for(j=0;j<2;j++) {
    long2hex((unsigned long)addr,s);
    display_puts(s);
    for(i=0;i<16;i++,addr++) {
      byte2hex(*addr,s);
      display_puts(" ");
      display_puts(s);
    }
    display_puts("\n");
    syscall_wait(50);
  }

}
Exemplo n.º 9
0
int start(int argc, char *argv[])
{
  struct kmem_queue *plist;
  int i,num;
  char strbuf[16];

  plist = malloc(sizeof(struct kmem_queue)*PLIST_SIZE);
  if(plist==0) {
    display_puts("error=malloc\n");
    return 1;
  }

  num = syscall_que_list(0,PLIST_SIZE,plist);

  display_puts("ID   IN   OUT  NAME ST WAIT\n");
  for(i=0;i<num;i++) {
    word2hex(plist[i].id,strbuf);
    display_puts(strbuf);
    display_putc(' ');

    word2hex(plist[i].in,strbuf);
    display_puts(strbuf);
    display_putc(' ');

    word2hex(plist[i].out,strbuf);
    display_puts(strbuf);
    display_putc(' ');

    word2hex(plist[i].name,strbuf);
    display_puts(strbuf);
    display_putc(' ');

    byte2hex(plist[i].status,strbuf);
    display_puts(strbuf);
    display_putc(' ');

    word2hex(plist[i].numwait,strbuf);
    display_puts(strbuf);
    display_putc('\n');

  }

  mfree(plist);

  return 0;
}
Exemplo n.º 10
0
static void *pci_scan_bios(void)
{
  unsigned char *bios_addr;
  for(bios_addr = (void*)CFG_MEM_BIOSSTART;
      (unsigned long)bios_addr < CFG_MEM_BIOSMAX-sizeof(struct pci_bios) ;
      bios_addr++  )
  {
    if(memcmp(bios_addr,"_32_",4)!=0)
      continue;
    struct pci_bios *info=(void*)bios_addr;
    if(info->revision!=0)
      continue;
    if(info->length!=(unsigned char)1)
      continue;
    unsigned char i,checksum=0;
    for(i=0;i<sizeof(struct pci_bios);i++) {
      checksum += bios_addr[i];
    }
    if(checksum!=info->checksum)
      continue;

display_puts("entry=");
long2hex(info->entry,s);
display_puts(s);
display_puts(",rev=");
int2dec(info->revision,s);
display_puts(s);
display_puts(",len=");
int2dec(info->length,s);
display_puts(s);
display_puts(",chk=");
byte2hex(info->checksum,s);
display_puts(s);
display_puts("\n");

    return (void*)info->entry;
    //display_puts(".");
    //syscall_wait(100);
  }
  display_puts("PCI BIOS is not found\n");
  dump((void*)CFG_MEM_BIOSSTART);
  return 0;
}
Exemplo n.º 11
0
static void test_helper_hex_single(const char* cs) {
	CU_ASSERT_EQUAL(strlen(cs), 2);
	
	char c1 = *cs;
	char c2 = *(cs+1);
	
	char val = hex2byte(c1, c2);
	
	char cookie = 42;
	char[3] buf;
	*(buf+2) = cookie;
	
	byte2hex(val, bufs);
	
	CU_ASSERT_EQUAL(toupper(*buf), toupper(c1));
	CU_ASSERT_EQUAL(toupper(*(buf+1)), toupper(c2));
	char val2 = hex2byte(*buf, *(buf+1));
	CU_ASSERT_EQUAL(val, val2);
	
	CU_ASSERT_EQUAL(*(buf+2), cookie);
}
Exemplo n.º 12
0
// 服务器端组装发给客户端的协商包
char* pkg_talk_rtn(const packet_parser_t *pkg)
{
	char *r;
	unsigned char *encrypted_key;// 使用公钥加密过的临时密钥
	char *output;
	iks *x, *tmp;
	int dest_len;

	x = iks_new ("connection");
	if(NULL == x) return NULL;

	iks_insert_attrib(x, "xmlns", TALK_XMLNS);
	iks_insert_attrib(x, "type", "result");
	tmp = iks_insert(x, "encryption");
	iks_insert_attrib(tmp, "type", pkg->curr_ert.transfer_ert_type);
	// 使用公钥对临时密钥进行加密
	if (pkg->asym_encrypt_hook == NULL)
	{
		encrypted_key = rsa_encrypt((unsigned char *)get_transfer_crt_key(pkg), strlen(get_transfer_crt_key(pkg)), &dest_len, (char *)(pkg->curr_ert.ert_keys[0]), CRYPT_TYPE_ENCRYPT);
	}
	else
	{
		encrypted_key = pkg->asym_encrypt_hook((unsigned char *)get_transfer_crt_key(pkg), strlen(get_transfer_crt_key(pkg)), &dest_len, (char *)(pkg->curr_ert.ert_keys[0]), CRYPT_TYPE_ENCRYPT);
	}
	output = (char *)calloc(dest_len*2+1, 1);
	byte2hex(encrypted_key, dest_len, output);
	iks_insert_cdata(tmp, output, 0);
	free(output);
	free(encrypted_key);

	iks_insert_cdata(iks_insert(x, "compression"), pkg->cps_type, 0);
	tmp = iks_insert(x, "heartbeat");
	// TODO:在初始化服务器端包解析器的时候应该设置心跳参数
	iks_insert_attrib(tmp, "sponsor", "server");
	iks_insert_attrib(tmp, "seconds", "60");

	r = iks_string(NULL, x);
	iks_delete(x);
	return r;
}
Exemplo n.º 13
0
void scamper_icmp_resp_print(const scamper_icmp_resp_t *ir)
{
  char *t = NULL, tbuf[64];
  char *c = NULL, cbuf[64];
  char addr[64];
  char ip[256];
  char icmp[256];
  char inner_ip[256];
  char inner_transport[256];
  char ext[256];
  int  i, j;
  size_t off;

  assert(ir->ir_af == AF_INET || ir->ir_af == AF_INET6);

  if(ir->ir_af == AF_INET)
    {
      addr_tostr(AF_INET, &ir->ir_ip_src.v4, addr, sizeof(addr));

      off = 0;
      string_concat(ip, sizeof(ip), &off,
		    "from %s size %d ttl %d tos 0x%02x ipid 0x%04x",
		    addr, ir->ir_ip_size, ir->ir_ip_ttl, ir->ir_ip_tos,
		    ir->ir_ip_id);

      if(ir->ir_ipopt_rrc > 0)
	string_concat(ip, sizeof(ip), &off, " rr %d", ir->ir_ipopt_rrc);

      switch(ir->ir_icmp_type)
        {
        case ICMP_UNREACH:
          t = "unreach";
          switch(ir->ir_icmp_code)
            {
            case ICMP_UNREACH_NET:           c = "net";           break;
            case ICMP_UNREACH_HOST:          c = "host";          break;
            case ICMP_UNREACH_PROTOCOL:      c = "protocol";      break;
            case ICMP_UNREACH_PORT:          c = "port";          break;
            case ICMP_UNREACH_SRCFAIL:       c = "src-rt failed"; break;
            case ICMP_UNREACH_NET_UNKNOWN:   c = "net unknown";   break;
            case ICMP_UNREACH_HOST_UNKNOWN:  c = "host unknown";  break;
            case ICMP_UNREACH_ISOLATED:      c = "isolated";      break;
            case ICMP_UNREACH_NET_PROHIB:    c = "net prohib";    break;
            case ICMP_UNREACH_HOST_PROHIB:   c = "host prohib";   break;
            case ICMP_UNREACH_TOSNET:        c = "tos net";       break;
            case ICMP_UNREACH_TOSHOST:       c = "tos host";      break;
            case ICMP_UNREACH_FILTER_PROHIB: c = "admin prohib";  break;
            case ICMP_UNREACH_NEEDFRAG:
	      /*
	       * use the type buf to be consistent with the ICMP6
	       * fragmentation required message
	       */
	      snprintf(tbuf, sizeof(tbuf), "need frag %d", ir->ir_icmp_nhmtu);
	      t = tbuf;
	      break;

            default:
	      snprintf(cbuf, sizeof(cbuf), "code %d", ir->ir_icmp_code);
	      c = cbuf;
	      break;
            }
          break;

        case ICMP_TIMXCEED:
          t = "time exceeded";
          switch(ir->ir_icmp_code)
            {
            case ICMP_TIMXCEED_INTRANS: c = "in trans"; break;
            case ICMP_TIMXCEED_REASS:   c = "in reass"; break;
            default:
	      snprintf(cbuf, sizeof(cbuf), "code %d", ir->ir_icmp_code);
	      c = cbuf;
	      break;
            }
          break;

	case ICMP_ECHOREPLY:
	  t = "echo reply";
	  snprintf(cbuf, sizeof(cbuf), "id %d seq %d",
		   ir->ir_icmp_id, ir->ir_icmp_seq);
	  c = cbuf;
	  break;

	case ICMP_TSTAMPREPLY:
	  t = "ts reply";
	  snprintf(cbuf, sizeof(cbuf), "id %d seq %d",
		   ir->ir_icmp_id, ir->ir_icmp_seq);
	  c = cbuf;
	  break;
        }
    }
  else /* if(ir->ir_af == AF_INET6) */
    {
      addr_tostr(AF_INET6, &ir->ir_ip_src.v6, addr, sizeof(addr));

      snprintf(ip, sizeof(ip), "from %s size %d hlim %d", addr,
	       ir->ir_ip_size, ir->ir_ip_hlim);

      switch(ir->ir_icmp_type)
        {
        case ICMP6_DST_UNREACH:
          t = "unreach";
          switch(ir->ir_icmp_code)
            {
            case ICMP6_DST_UNREACH_NOROUTE:     c = "no route";     break;
            case ICMP6_DST_UNREACH_ADMIN:       c = "admin prohib"; break;
            case ICMP6_DST_UNREACH_BEYONDSCOPE: c = "beyond scope"; break;
            case ICMP6_DST_UNREACH_ADDR:        c = "addr";         break;
            case ICMP6_DST_UNREACH_NOPORT:      c = "port";         break;

            default:
	      snprintf(cbuf, sizeof(cbuf), "code %d", ir->ir_icmp_code);
	      c = cbuf;
	      break;
            }
          break;

        case ICMP6_TIME_EXCEEDED:
          t = "time exceeded";
          switch(ir->ir_icmp_code)
            {
            case ICMP6_TIME_EXCEED_TRANSIT:    c = "in trans"; break;
            case ICMP6_TIME_EXCEED_REASSEMBLY: c = "in reass"; break;

            default:
	      snprintf(cbuf, sizeof(cbuf), "code %d", ir->ir_icmp_code);
	      c = cbuf;
	      break;
            }
          break;

	case ICMP6_PACKET_TOO_BIG:
	  snprintf(tbuf, sizeof(tbuf), "need frag %d", ir->ir_icmp_nhmtu);
	  t = tbuf;
	  break;

	case ICMP6_ECHO_REPLY:
	  t = "echo reply";
	  snprintf(cbuf, sizeof(cbuf), "id %d seq %d",
		   ir->ir_icmp_id, ir->ir_icmp_seq);
	  c = cbuf;
	  break;
        }
    }

  if(t == NULL)
    {
      snprintf(icmp, sizeof(icmp), "icmp %d code %d",
	       ir->ir_icmp_type, ir->ir_icmp_code);
    }
  else if(c == NULL)
    {
      snprintf(icmp, sizeof(icmp), "icmp %s", t);
    }
  else
    {
      snprintf(icmp, sizeof(icmp), "icmp %s %s", t, c);
    }

  if(ir->ir_flags & SCAMPER_ICMP_RESP_FLAG_INNER_IP)
    {
      if(ir->ir_af == AF_INET)
	{
	  addr_tostr(AF_INET, &ir->ir_inner_ip_dst.v4, addr, sizeof(addr));

	  off = 0;
	  string_concat(inner_ip, sizeof(inner_ip), &off,
			" to %s size %d ttl %d tos 0x%02x ipid 0x%04x",
			addr, ir->ir_inner_ip_size, ir->ir_inner_ip_ttl,
			ir->ir_inner_ip_tos, ir->ir_inner_ip_id);
	  if(ir->ir_inner_ipopt_rrc > 0)
	    string_concat(inner_ip, sizeof(inner_ip), &off, " rr %d",
			  ir->ir_inner_ipopt_rrc);
	}
      else /* if(ir->ir_af == AF_INET6) */
	{
	  addr_tostr(AF_INET6, &ir->ir_inner_ip_dst.v6, addr, sizeof(addr));
	  snprintf(inner_ip, sizeof(inner_ip),
		   " to %s size %d hlim %d flow 0x%05x", addr,
		   ir->ir_inner_ip_size, ir->ir_inner_ip_hlim,
		   ir->ir_inner_ip_flow);
	}

      switch(ir->ir_inner_ip_proto)
	{
	case IPPROTO_UDP:
	  snprintf(inner_transport, sizeof(inner_transport),
		   " proto UDP sport %d dport %d sum 0x%04x",
		   ir->ir_inner_udp_sport, ir->ir_inner_udp_dport,
		   ntohs(ir->ir_inner_udp_sum));
	  break;

	case IPPROTO_ICMP:
	case IPPROTO_ICMPV6:
	  snprintf(inner_transport, sizeof(inner_transport),
		   " proto ICMP type %d code %d id %04x seq %d sum %04x",
		   ir->ir_inner_icmp_type, ir->ir_inner_icmp_code,
		   ir->ir_inner_icmp_id, ir->ir_inner_icmp_seq,
		   ntohs(ir->ir_inner_icmp_sum));
	  break;

	case IPPROTO_TCP:
	  snprintf(inner_transport, sizeof(inner_transport),
		   " proto TCP sport %d dport %d seq %08x",
		   ir->ir_inner_tcp_sport, ir->ir_inner_tcp_dport,
		   ir->ir_inner_tcp_seq);
	  break;

	default:
	  inner_transport[0] = '\0';
	  break;
	}
    }
  else
    {
      inner_ip[0] = '\0';
      inner_transport[0] = '\0';
    }

  if(ir->ir_ext != NULL)
    {
      snprintf(ext, sizeof(ext), " icmp-ext");
      j = 9;
      for(i=0; i<ir->ir_extlen; i++)
	{
	  if(i % 4 == 0)
	    {
	      if(sizeof(ext)-j < 4)
		break;
	      ext[j++] = ' ';
	    }
	  else if(sizeof(ext)-j < 3)
	    break;
	  byte2hex(ir->ir_ext[i], ext + j);
	  j += 2;
	}
      ext[j] = '\0';
    }
  else
    {
      ext[0] = '\0';
    }

  scamper_debug(NULL, "%s %s%s%s%s", ip, icmp, inner_ip, inner_transport, ext);
  return;
}
Exemplo n.º 14
0
int
main(int argc, char **argv)
{
	unsigned char bufA[PIPE_MAX], bufB[PIPE_MAX], *bp, *bp1, *bp2;
	int tail_nr;
	int nbuf = 0;
	char hex[65];

#if PIPE_MAX < 32
	"this compilation error means that PIPE_MAX < 32 bytes"
#endif

	if (argc != 1)
		die(EXIT_BAD_ARGC, "wrong number of command line arguments");
	(void)argv;

	//  read exactly PIPE_MAX bytes or read the tail of the blob,
	//  alternating between bufA and bufB.

	bp = bufA;
	while (fill(bp, &tail_nr)) {
		nbuf++;
		if (bp == bufA)
			bp = bufB;
		else
			bp = bufA;
	}

	bp = (bp == bufA ? bufB : bufA);

	//  final read() of blob got at least 32 bytes

	if (tail_nr >= 32)
		put_hex(hex, bp + tail_nr - 32, 32);
	
	//  zero length blob

	if (nbuf == 0)
		_exit(0);

	//  total blob size < 32 bytes

	if (nbuf == 1)
		put_hex(hex, bp, tail_nr);

	//  we now know that the suffix will be exactly 32 bytes.

	//  is the suffix entirely in the previous block

	if (tail_nr == PIPE_MAX)
		put_hex(hex, bp + PIPE_MAX - 32, 32);

	//  tail block was read less than PIPE_MAX bytes, so 32 byte suffix
	//  spans final two chunks, where the length of tail chunk is 0 <&&< 32
	//  bytes and that the length of previous chunk is exactly PIPE_MAX
	//  bytes.

	if (bp == bufA) {
		bp1 = bufB;
		bp2 = bufA;
	} else {
		bp1 = bufA;
		bp2 = bufB;
	}

	byte2hex(hex, bp1 + (PIPE_MAX - (32 - tail_nr)), 32 - tail_nr);
	byte2hex(hex + (64 - tail_nr * 2), bp2, tail_nr);
	hex[64] = '\n';
	_write(1, hex, 65);
	_exit(0);
}
Exemplo n.º 15
0
void main(void) {
    uint16_t i;
    char usb_char, err;
    const char init_msg[] = {'I', 'N', 'I', 'T'};
    //extern FILE *stdout = _H_USER; // redirect stdout to USB

    init();
    storage_init();

    display_cnt = 0;
    volume_tick = 0;
    chan_tick = 0;
    usb_tick = 0;
    ir_tick = 0;
    ir_speedup = 20;
    dac_lock_tick = 0;

    display_set_alt(0x00, 0x00, 0x00);
    display_set(0x00, 0x00, 1);
    ir_receiver_init();


    err = relay_boards_init(); // as side-effect: determine board Type and Id
    amp_state_init();
    set_relays(0x00, 0x00, 0x00, 0x00);

    display_oled_init();
    if (has_oled_display) {
        display_oled_chars( 0, 0, 5, "hello");
        display_set_alt(DIGIT_D, 0x00, 3);
    }
    else if (err)
        display_set_alt(DIGIT_E, 0x01, 3);

    // Globally enable interrupts
#ifdef UseIPEN
    INTCONbits.GIEH = 1;
    INTCONbits.GIEL = 1;
#else
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
#endif

    // (re-)launch USB activity
    prev_usb_bus_sense = 0;
    usb_write(init_msg, (uint8_t) 4);

    // The above 'set_relays' enabled the power relay for the analog supply.
#ifdef __DEBUG
    power_tick = 0;
#else
    power_tick = 120;
#endif

    // Set a timer to later undo the mute and activate last volume setting.
    // wait some time for stabilization before enabling all other interrupts
    while (power_tick > 0)
        ; // gets decreased on timer interrupts, 183Hz

    

    // power==0 now, from amp_state_init().
    // incr power now quickly to 1, and later to 2.
    power_incr = 1;

    INTCON3bits.INT1IF = 0;
    INTCON3bits.INT1IE = 1;
    INTCON3bits.INT2IF = 0;
    INTCON3bits.INT2IE = 1;
    INTCON3bits.INT3IF = 0;
    INTCON3bits.INT3IE = 1;

    // Check if a DAC is present in this Relaixed, if so initialize.
    // This check was delayed to allow DAC power-up, otherwise its i2c interface stays in reset
    dac_init();

    while (1) {
        if (volume_incr)
            volume_update();

        if (balance_incr > 1 || balance_incr < -1)
            // suppress a single tick, might have been by accident
            balance_update();

        if (channel_incr)
            channel_update();

        if (power_incr) {
            if (flash_tick != 0 && power_incr < 0) {
                // quickly save recent volume/balance update
                flash_tick = 0;
                flash_volume_channel();
            } else if (power_incr > 0 && power_state() == 0) {
                // if we move power_state from 0 to 1, we surely want to go later to 2
                // For RelaixedPassive: wait somewhat longer for its soft-switch main power
                power_tick = (isRelaixedXLR) ? 500 : 700;
            }

            if (power_incr > 0)
                dac_init(); // check (again) for presence of DAC: it needs time to get out of reset
            power_update();
        }

        if (ir_received_ok) {
            ir_received_ok = 0;
            ir_handle_code();
            if (volume_incr) {
                vol_usb_msg[0] = 'V';
                // when 'volume' keeps pressed, the volume-tick-speed goes up
                if (ir_speedup <= 49)
                    ir_speedup += 4;
            } else {
                vol_usb_msg[0] = 'v';
                ir_speedup = 20;
            }
            byte2hex(vol_usb_msg + 1, ir_tick);
            byte2hex(vol_usb_msg + 3, ir_speedup);

            if (power_incr)
                ir_tick = 100; // increase the default 20 to 100 on power on/off
            else
                flash_tick = 400;

            usb_write(vol_usb_msg, 5);
        }

        if (flash_tick == 1) {
            flash_tick = 0;
            flash_volume_channel();
        }

        if (dac_status() >= DAC_NOLOCK && dac_lock_tick == 0) {
            dac_check_lock();
            dac_lock_tick = 45; // check lock 4x per secnd
        }

        /* some I/O to check repeatedly, for absence of interrupt-on-change */
        check_usb_power(err);
    }
}