Ejemplo n.º 1
0
int32_t  Shell_exec(int32_t argc, char *argv[] )
{ /* Body */
   _ip_address          hostaddr;
   char                 hostname[MAX_HOSTNAMESIZE];
   char                 *host_ptr, *file_ptr, *format_ptr;

   bool              print_usage, shorthelp = FALSE;
   int32_t               error = 0, return_code = SHELL_EXIT_SUCCESS;

   print_usage = Shell_check_help_request(argc, argv, &shorthelp );

   if (!print_usage)  {
      if (argc > 4 )  {
         printf("Error, %s invoked with incorrect number of arguments\n", argv[0]);
         print_usage = TRUE;
         return_code = SHELL_EXIT_ERROR;
      } else  {
         format_ptr = "srec";
         if (argc > 1)  {
            file_ptr = argv[1];
            if (argc > 2)  {
               host_ptr = argv[2];
               if (argc>3)  {
                  format_ptr = argv[3];
               }
            } else  {
                  printf("Error, no TFTP Server specified.\n", argv[0]);
                  print_usage = TRUE;
                  return_code = SHELL_EXIT_ERROR;
            }
         } else  {
               printf("Error, no  bootfile specified.\n", argv[0]);
               print_usage = TRUE;
               return_code = SHELL_EXIT_ERROR;
         }
      }
      
      if (return_code != SHELL_EXIT_ERROR)  {
         RTCS_resolve_ip_address( host_ptr, &hostaddr, hostname, MAX_HOSTNAMESIZE ); 

         if (hostaddr)  {
            printf("Loading %s from %s [%ld.%ld.%ld.%ld]:\n", file_ptr, hostname, IPBYTES(hostaddr));
            if (strcmp(format_ptr, "srec") == 0)  {
               error = RTCS_exec_TFTP_SREC(hostaddr, file_ptr);
            } else if (strcmp(format_ptr, "coff") == 0)  {
               error = RTCS_exec_TFTP_COFF(hostaddr, file_ptr);
            } else {
               printf("Error, invalid format %s\n", format_ptr);
               print_usage = TRUE;
               return_code = SHELL_EXIT_ERROR;
            }
            if (error)  {
               printf("RTCS_exec_TFTP_%s returned with error code = %x\n",format_ptr, error);
               return_code = SHELL_EXIT_ERROR;
            }
         } else  {
            printf("Unable to resolve host\n");
            return_code = SHELL_EXIT_ERROR;
         }
      }         
   }

   if (print_usage)  {
      if (shorthelp)  {
         printf("%s <file> <host> <format>\n", argv[0]);
      } else  {
         printf("Usage: %s <file> <host> <format>\n", argv[0]);
         printf("   <file>   = filename\n");
         printf("   <host>   = TFTP Server ip address or name\n");
         printf("   <format> = file format, one of coff or srec\n");
      }
   }
   return return_code;
} /* Endbody */
Ejemplo n.º 2
0
Archivo: Mail.c Proyecto: gxliu/MqxSrc
void SEC_EmailAlert()
{
   MQX_TICK_STRUCT      ticks;
   MQX_XDATE_STRUCT     xdate;
   sockaddr             addr;
   _ip_address          ipaddr;
   uint_32              sock;
   uint_32              error;
   uint_32              option;
   char_ptr             event_ptr;
   char                 temp[150];
   char                 domain[30];
   char                 user[30];
    
#if DEMOCFG_AUTH_REQUIRED    
   char                 temp_encoded[40];
#endif
    

  
   if (!RTCS_resolve_ip_address(EMAIL_SERVER,&ipaddr,NULL,0)) {
      printf("Error resolving IP address for %s using DNS Server at %d.%d.%d.%d\n", EMAIL_SERVER, IPBYTES(ipcfg_get_dns_ip(BSP_DEFAULT_ENET_DEVICE, 0)));
      return;
   }
  
    /* Create socket */        
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == RTCS_SOCKET_ERROR) 
    {
        printf("Error creating socket\n");
        return;
    } 

   /* Reduce buffer size of socket to save memory */
   option = SMTP_BUFFER_SIZE;   
   error = setsockopt(sock, SOL_TCP, OPT_TBSIZE, &option, sizeof(option));
   option = SMTP_BUFFER_SIZE;   
   error = setsockopt(sock, SOL_TCP, OPT_RBSIZE, &option, sizeof(option));
   option = 1000;   
   error = setsockopt(sock, SOL_TCP, OPT_TIMEWAIT_TIMEOUT, &option, sizeof(option));


    /* Allow binding to any address */
   
    #if RTCSCFG_ENABLE_IP4  
    ((sockaddr_in*)(&addr))->sin_family      = AF_INET;
    ((sockaddr_in*)(&addr))->sin_port        = 0;
    ((sockaddr_in*)(&addr))->sin_addr.s_addr = INADDR_ANY;
 
    #elif RTCSCFG_ENABLE_IP6
        printf("\nThis application don't support IPv6 only.\n");
        _task_block();
    #endif

    error = bind(sock, &addr, sizeof(addr));
    if (error != RTCS_OK) {
        printf("Error in binding socket %08x\n",error);
        shutdown(sock, FLAG_ABORT_CONNECTION);
        return;
    } 

    /* Connect to SMTP server */
    #if RTCSCFG_ENABLE_IP4
        ((sockaddr_in*)(&addr))->sin_port        = IPPORT_SMTP;
        ((sockaddr_in*)(&addr))->sin_addr.s_addr = ipaddr;
    #elif RTCSCFG_ENABLE_IP6
        printf("\nThis application does not support IPv6 only.\n");
        _task_block();
    #endif


    error = connect(sock, &addr, sizeof(addr));
    if (error != RTCS_OK) {
        printf("Error in connection %08x\n",error);
        shutdown(sock, FLAG_ABORT_CONNECTION);
        return;
    }
  
    printf("\nConnecting to SMTP server %s...\n",EMAIL_SERVER);
    printf("SMTP Server Response:\n");
    SMTP_Receive_string(sock, temp, sizeof(temp));


    /* Follow SMTP protocol to send email */   

    /* Parse domain name from the FROM address, 
     for use with the EHLO or HELO commands */
    sscanf(EMAIL_FROM,"%30s@%30s",user,domain);
  
#if DEMOCFG_AUTH_REQUIRED 
  
    /* If Authentification is required, use EHLO command */
    SMTP_Send_command(sock, "EHLO", domain, temp, sizeof(temp));
    SMTP_Send_command(sock,"AUTH LOGIN",NULL,temp,sizeof(temp));

    /* Find base64 econding of login name and send to SMTP server */
    sprintf(temp,"%s\r\n",base64_encode(AUTH_USERNAME,temp_encoded));
    SMTP_Send_string(sock,temp);
    SMTP_Receive_string(sock,temp,sizeof(temp)-1);

    /* Find base64 econding of password and send to SMTP server */
    sprintf(temp,"%s\r\n",base64_encode(AUTH_PASSWORD,temp_encoded));
    SMTP_Send_string(sock,temp);
    SMTP_Receive_string(sock,temp, sizeof(temp)-1);
#else
    /* If Authentification is not required, use HELO command as not all servers recognize EHLO */
    SMTP_Send_command(sock, "HELO", domain, temp, sizeof(temp));
#endif


   /* Transfer sender email address to server */     
   SMTP_Send_command(sock,"MAIL FROM:","<" EMAIL_FROM ">" ,temp,sizeof(temp));
   /* Transfer recipient email address to server */            
   SMTP_Send_command(sock,"RCPT TO:","<" EMAIL_TO ">" ,temp,sizeof(temp));

   /* Start DATA section */
   SMTP_Send_command(sock,"DATA",NULL,temp,sizeof(temp));

   printf("Sent:\n");
   /* Send Subject, To, and From fields */
   SMTP_Send_string(sock,(pointer)Email_header);


   /* Determine what to send as body of message */
   _time_get_ticks(&ticks);
   _time_ticks_to_xdate(&ticks,&xdate);    

   sprintf(temp, "Date: %s, %d %s %d %02d:%02d:%02d -0000\r\n\r\n",
      wday[xdate.WDAY],xdate.MDAY, months[xdate.MONTH-1],xdate.YEAR, xdate.HOUR, xdate.MIN, xdate.SEC  );
   SMTP_Send_string(sock,temp);


   /* If button pushed was the "Door"... */
   if(SEC_Params.Status==SEC_DOOR_OPEN_STATUS)
   {
      event_ptr = "Door was opened ";
   } else 
   /* If button pushed was the "Window"... */
   if(SEC_Params.Status==SEC_WINDOW_OPEN_STATUS)
   {
      event_ptr = "Window was opened ";
   }
   /* If not sure what caused stop mode to exit... */  
   else 
   {
      event_ptr = "Unknown event occurred ";
   }    
   SMTP_Send_string(sock,event_ptr);

   /* If SNTP enabled, then send exact date and time button press happened */
   #if DEMOCFG_ENABLE_SNTP
      sprintf(temp,"on %d/%d/%d at %02d:%02d:%02d GMT. \r\n",xdate.MONTH,xdate.MDAY,xdate.YEAR,xdate.HOUR,xdate.MIN,xdate.SEC);
   /* If SNTP not enabled, then give elapsed time instead */
   #else
      sprintf(temp,"%02d:%02d:%02d after system was started. \r\n",xdate.HOUR,xdate.MIN,xdate.SEC);
   #endif
   SMTP_Send_string(sock,temp);  

   if (last[0]) {
      /* Send out how long ago the last event was */
      SMTP_Send_string(sock,"Previous event was ");
      SMTP_Send_string(sock,last);
   }
   strcpy(last,temp); 
   
   /* Send Freescale signature and disconnect from server with QUIT command */
   SMTP_Send_string(sock,"\r\n-Freescale Semiconductor\r\n.\r\n");
   
   SMTP_Send_command(sock,"QUIT",NULL,temp,sizeof(temp));

  /* Close socket */
    shutdown(sock, FLAG_CLOSE_TX);  
}