Exemplo n.º 1
0
/*	Open connection.
*	Returns 1 if ESP8266 is responding, 0 otherwise.
*/
bool ArduEsp8266::init(){

	// init serial debugging interface
	if( useDebug() ){
		debug.begin(115200);
		debug.println("init esp8266 wifi module ...");
	}

	if( getEnablePin() >= 0 ){
		pinMode( getEnablePin(), OUTPUT );
		digitalWrite( getEnablePin(), HIGH );
		delay(2000);
	}
	
	// init serial port
	Serial.begin( getBaudrate() );

	if( isConnected() ){
		// try to disable echo
		setEcho(false);
		return true;
	}
	
	return false;
}
uint8_t MG2639_Cell::begin(unsigned long baud)
{
	unsigned long setBaud = 0;
	uint8_t tries = 0;
	
	initializePins(); // Set up power and UART pin direction
	initializeUART(baud); // Initialize UART to requested baud
	
	// Try turning echo off first (send ATE0 command). If it succeeds,
	// we're already at the target baud and the module's on.
	if (setEcho(0) <= 0)
	{	
		// If setEcho fails, we can't communicate with the shield. The baud
		// may be incorrect, or the shield may be off. First time in, we'll
		// assume the module is on.
		// tries should be at least > 2 - one time assuming module is on,
		// another time assuming module is off
		while (setBaud <= 0 && tries < 4)
		{
			setBaud = autoBaud(); // Try to find the baud rate
			
			if (setBaud <= 0)
			{
				// If autoBaud fails to find anything, the module must be off.
				powerPulse(); // Send a power pulse (hold PWRKEY for 3s)
				delay(MODULE_WARM_UP_TIME); // Delay ~3s for module to warm up
			}
			tries++; // Increment tries and go again
		}
		
		// If we still can't find the baud rate, or communicate with the shield
		// we give up. Return a fail.
		if (setBaud <= 0)
			return 0;
		
		// If autoBaud succeeds, the module is on, we just need to 
		// change the baud rate.
		if (setBaud != baud)
		{
			int baudRsp;
	#if (ARDUINO >= 10601) 
			// Software serial after 1.6.1 works much better, no need for brute force
			baudRsp = changeBaud(setBaud, baud);
	#else
			baudRsp = bruteForceBaudChange(setBaud, baud, 100);
	#endif
			// Look for any error _except_ ERROR_UNKOWN_RESPONSE -- a change from 
			// 115200 will result in that error, even though the change baud 
			// worked. (SoftwareSerial can't read reliably at that high rate.)
			if ((baudRsp == 0) || (baudRsp == ERROR_FAIL_RESPONSE) || 
				(baudRsp == ERROR_TIMEOUT))
			{
				return 0;
			}
		}
		delay(COMMAND_RESPONSE_TIME);
	}
	
	return 1;
}
unsigned long MG2639_Cell::autoBaud()
{
	int i;
	int echoResponse;
	for (i=0; i<BAUD_COUNT; i++)
	{
		initializeUART(baudRates[i]); // Set UART to baud rate

		printChar('\r'); // Print a '\r' to send any possible garbage command
		delay(10); // Wait for a possible "ERROR" response

		// Try turning echo off to see if the module responds with "OK"
		echoResponse = setEcho(0);
		// If setEcho is successful, we found the baud rate! Break this loop
		if (echoResponse > 0)
			break;
	}
	// If we found the baud rate, return the matching value
	if (echoResponse > 0)
		return baudRates[i];
	else
		return 0; // Otherwise we failed to find it, return 0
}
Exemplo n.º 4
0
int main(int argc, char** argv) {
   const char *auth_method = NULL;
   const char *service = NULL;
   char username[BUF_SIZE];
   char password[BUF_SIZE];
   int i = 0;
   int ret = 0;
   error_handler_t error_handler;
   int uid = 0;
   int gid = 0;
#ifdef WINDOWS
   char **ppszUID = NULL;
   char **ppszGID = NULL;
   char **ppszGroupNames = NULL;
   int  nGIDs;
#endif

   error_handler.error = print_error;
   
   if(argc < 2 ) {
      print_error(MSG_AUTUSER_INVAILD_ARG_COUNT);
      usage();
   }
   auth_method = argv[1];

#ifndef WINDOWS
#define SGE_SUPERUSER_UID 0
   /* only root can successfull execute this */
   if(geteuid() != SGE_SUPERUSER_UID && geteuid() != getuid()) {
      print_error(MSG_AUTHUSER_ONLY_ROOT_S, argv[0]);
      return 1;
   }
#endif

   if(strcmp(auth_method, "pam") == 0 ) {
      for(i=2; i < argc; i++) {
         if( strcmp(argv[i], "-s") == 0 ) {
            i++;
            if(i >= argc) {
               print_error(MSG_AUTUSER_MISSING_PAM_SERVICE);
               usage();
            }
            service = argv[i];
         } else {
            print_error(MSG_AUTUSER_UNKNOWN_PARAM_S, argv[i]);
            usage();
         }
      }
      if (service == NULL) {
         print_error(MSG_AUTUSER_MISSING_PAM_SERVICE);
         usage();
      }
   } else if (strcmp(auth_method, "system" ) == 0 ) {
   } else {
      print_error(MSG_AUTUSER_UNKNOWN_AUTH_METHOD_S, auth_method);
      usage();
   }

   fprintf(stdout, "username: "******"password: "******"\n");
   setEcho(1);
   
   if (strcmp(auth_method, "pam") == 0 ) {      
#ifndef WINDOWS
      ret = do_pam_authenticate(service, username, password, &error_handler);
      if(ret == JUTI_AUTH_SUCCESS) {
         struct passwd *pw = getpwnam(username);
         if(pw == NULL) {
            print_error(MSG_AUTHUSER_NO_PW_ENTRY_SS,
                        username, strerror(errno));
            return -1;
         }
         uid = pw->pw_uid;
         gid = pw->pw_gid;
      }
#else
      ret = JUTI_AUTH_ERROR;
      print_error(MSG_AUTHUSER_PAM_NOT_AVAILABLE);
#endif
   } else if(strcmp(auth_method, "system") == 0 ) {
#ifndef WINDOWS
      ret = do_system_authentication(username, password, &uid, &gid, &error_handler);
#else
      ret = do_windows_system_authentication(username, password, 
                                             &ppszUID, &ppszGID, &ppszGroupNames,
                                             &nGIDs, &error_handler); 
#endif
   } else {
      ret = -1;
   }
   if (ret==JUTI_AUTH_SUCCESS) {
      int group_count = 0;
      gid_t *groups = NULL;
      char  **group_names = NULL;
#ifndef WINDOWS      
      fprintf(stdout, "uid %d\n", uid);
      fprintf(stdout, "gid ");

      if(juti_getgrouplist(username, gid, &groups, &group_names, &group_count) == 0) {            
         for(i = 0; i < group_count; i++) {
            if(i>0) {
               fprintf(stdout, ",%s("gid_t_fmt")", group_names[i], groups[i]);
            } else {
               fprintf(stdout, "%s("gid_t_fmt")", group_names[i], groups[i]);
            }
            free(group_names[i]);
         }
         free(groups);
         free(group_names);
      }
#else
      fprintf(stdout, "uid %s\n", *ppszUID);
      LocalFree(ppszUID[0]);
      free(ppszUID);

      fprintf(stdout, "gid ");
      
      for(i=0; i<nGIDs; i++) {
         if(i>0) {
            fprintf(stdout, ",%s(%s)", ppszGroupNames[i], ppszGID[i]);
         } else {
            fprintf(stdout, "%s(%s)", ppszGroupNames[i], ppszGID[i]);
         }
         LocalFree(ppszGID[i]);
         free(ppszGroupNames[i]);
      }
      free(ppszGID);
      free(ppszGroupNames);
#endif
      fprintf(stdout, "\n");
   }
   return ret;
}