void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
{
    connected = false;
   
    // make sure a cable is connected before starting to connect
    while (!linkStatus()) 
    {
        wait(1.0f);
        WARN("Ethernet link not present. Check cable connection\n");
    }
        
    while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) 
    {    
        if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)
            return; // don't reattempt to connect if credentials are wrong
            
        Thread red_thread(flashing_red);

        int timeout = getConnTimeout(++retryAttempt);
        WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
        
        // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
        //  or maybe just add the proper members to do this disconnect and call attemptConnect(...)
        
        // this works - reset the system when the retry count gets to a threshold
        if (retryAttempt == 5)
            NVIC_SystemReset();
        else
            wait(timeout);
    }
}
Ejemplo n.º 2
0
bool linkProgram(GLuint handle, GLuint vshader, GLuint fshader) {
	glAttachShader(handle, vshader);
	glAttachShader(handle, fshader);
	glLinkProgram(handle);
	if (!linkStatus(handle)) {
	    char buff[2048];
	    int nwritten;
		glGetProgramInfoLog(handle, 2048, &nwritten, buff);
		printf("Program link error:\n%s\n", buff);
		return false;
	}
	return true;
}
Ejemplo n.º 3
0
int main( const int argc, const char* argv[]) {
  struct ifreq  ifr;
  int           sd,resultCode = 0;
  int           devLen,devLenMax = sizeof(ifr.ifr_name) - 1;
  int           argn, opt;
  int           opt_usage = 0;
  int           opt_link = 0;
  int           opt_hwaddr = 0;
  int           opt_quiet = 0;
  char*         device;
  char          hwAddr[19];
  int           arg_count = argc;


  /* Process any cli options: */
  while ( (opt = getopt(argc, argv, "lahq")) != -1 ) {
    switch ( opt ) {

      case 'h':
        opt_usage++;
        arg_count--;
        break;

      case 'q':
        opt_quiet++;
        arg_count--;
        break;

      case 'l':
        opt_link++;
        arg_count--;
        break;

      case 'a':
        opt_hwaddr++;
        arg_count--;
        break;

    }
  }

  if ( opt_usage || arg_count <= 0 ) {
    usage(argv[0]);
    exit(0);
  }

  devLen = strlen(argv[argc-1]);

  /* Make sure the device name wasn't too long */
  if ( devLen > devLenMax ) {
    printf("ERROR: Invalid device name (too many characters to be a device!)\n");
    return 1;
  }
  
  if ( opt_link ) {
     if ( ! opt_quiet ) {
        printf("%s: ", argv[argc-1]);
        fflush(stdout);
      }

     /* Setup the ifreq structure */
     memset(&ifr, 0, sizeof(ifr));
     strncpy(ifr.ifr_name, argv[argc-1], devLenMax);
  
     /* Create a socket so we can grab link status from it: */
     if ( (sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
       perror("Cannot get control socket");
       resultCode = 1;
     } else {
       /* Get link status and react accordingly: */
       switch ( linkStatus(sd, &ifr) ) {
      
         case 0:
           printf("ERROR: Something truly weird happened in linkStatus\n");
           resultCode = -1;
           break;
        
         case 1:
           printf("Link detected\n");
           break;
    
         case 2:
           printf("WARNING: no link\n");
           resultCode = 1;
           break;
    
         case 3:
           resultCode = 2;
           break;
      
       }
     }
  }
  if ( opt_hwaddr ) {

  /* Get the node ID: */
  hwAddr[18] = '\0';
  if ( getHWAddrForInterface(argv[argc-1],hwAddr,18) != 0 ) {
    printf("ERROR: Could not determine hardware address of device '%s'\n", argv[argc-1]);
    exit(1);
  }

  if ( opt_quiet ) {
     printf("%s\n", hwAddr);
  } else {
     printf("%s: %s\n", argv[argc-1], hwAddr);
  }

  }
  return resultCode;
}