Exemplo n.º 1
0
/*
 *  Print out current value of push buttons, if board is moving or tiled, 
 *  the value of the potentiometer, and the elapsed time since bootup
 */
int_32  Shell_status(int_32 argc, char_ptr argv[] )
{
   boolean           print_usage, shorthelp = FALSE;
   int_32            return_code = SHELL_EXIT_SUCCESS;
   int pot;
        
    TIME_STRUCT time;
    DATE_STRUCT date;
    
   print_usage = Shell_check_help_request(argc, argv, &shorthelp );

   if (!print_usage)  
   {
      if (argc > 1) 
      {
         printf("Error, invalid number of parameters\n");
         return_code = SHELL_EXIT_ERROR;
         print_usage=TRUE;
      } 
      else 
      {
        /* Print out 'Door' status */
        printf("Door (SW2):                      ");
        if(SEC_GetDoorStatus()==OPENED)
            printf("Open\n");
        else
            printf("Closed\n");
      
        /* Print out 'Window' status */ 
            printf("Window (SW3):                    ");
        if(SEC_GetWindowStatus()==OPENED)
            printf("Open\n");
        else
            printf("Closed\n");
        
        /* Print out movement/tilt status */
            printf("Motion Detection:                ");
        if(SEC_GetMovementStatus()==MOVING)
            printf("Movement\n");
        else
            printf("No Movement\n");
        
        /* Print out potentiometer percentage in 10% increments */
        pot = (SEC_Params.GarageStatus / 409) * 10;
        printf("Garage Door (Potentiometer R2):  %d%% Open\n",pot);

        /* Print out elapsed time since bootup */
            _time_get(&time);
            _time_to_date(&time,&date);   
            printf("Time Since Bootup:               %02d:%02d:%02d\n\n",date.HOUR,date.MINUTE,date.SECOND);    
        
      }
   }
   
   if (print_usage)  
   {
      print_usage_simple (shorthelp, argv[0], "Display security system status");
   }
   return return_code;
} 
Exemplo n.º 2
0
/*FUNCTION**********************************************************************
 *
 * Function Name    : print_current_time
 * Returned Value   : none
 * Comments         :
 *    Get current time & printf it in "date" format to stdout
 *
 *END**************************************************************************/
static void print_rtc_time
(
    uint32_t rtc_time
)
{ /* Body*/
    TIME_STRUCT  ts;
    DATE_STRUCT  tm;

    ts.SECONDS = rtc_time;
    ts.MILLISECONDS = 0;
    /* Convert rtc_time to date format */
    if (_time_to_date(&ts, &tm) == FALSE ) {
        printf("\n Cannot convert rtc_time to date format");
        _task_block();
    }

    printf(" (RTC) Current time is: %s %s %3d %.2d:%.2d:%.2d %d\n",
            _days_abbrev[tm.WDAY],
            _months_abbrev[tm.MONTH - 1],
            tm.DAY,
            tm.HOUR,
            tm.MINUTE,
            tm.SECOND,
            (tm.YEAR));

} /* Endboy*/
Exemplo n.º 3
0
_mqx_int _rtc_sync_with_mqx
(
    /* [IN] whether to update MQX time (source is RTC) or RTC time (source is MQX) */
    boolean update_mqx
)
{
    RTC_TIME_STRUCT rtc_time;
    DATE_STRUCT     mqx_date;
    TIME_STRUCT     mqx_time;

    if (update_mqx == TRUE)
    {
        _rtc_get_time(&rtc_time);
        _rtc_time_to_mqx_date(&rtc_time, &mqx_date);
        _time_from_date(&mqx_date, &mqx_time);
        _time_set(&mqx_time);
    }
    else
    {
        _time_get(&mqx_time);
        _time_to_date(&mqx_time, &mqx_date);
        _rtc_time_from_mqx_date (&mqx_date, &rtc_time);
        _rtc_set_time (&rtc_time);
    }
    return MQX_OK;
}
Exemplo n.º 4
0
/*
 *  Display the last MAX_QUEUE events that occured with a timestamp
 *  Button pushes and movement changes logged. Potentiometer changes are not logged.
 */
int_32  Shell_displaylog(int_32 argc, char_ptr argv[] )
{
   boolean           print_usage, shorthelp = FALSE;
   int_32            return_code = SHELL_EXIT_SUCCESS;
        
    TIME_STRUCT time;
    DATE_STRUCT date;
    SEC_HIST_PTR hist;
    int i;

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

   if (!print_usage)  
   {
      if (argc > 1) 
      {
         printf("Error, invalid number of parameters\n");
         return_code = SHELL_EXIT_ERROR;
         print_usage=TRUE;
      } 
      else 
      {

            _time_get_elapsed(&time);
            _time_to_date(&time,&date);   
            printf("Time Since Bootup: %02d:%02d:%02d\n",date.HOUR,date.MINUTE,date.SECOND);    
        
            /* Print out last MAX_QUEUE events */
            for(i = (MAX_QUEUE - 1);i >= 0;i--)
            {
           hist = &(SEC_Params.History[(i+SEC_Params.HistoryIndex)%MAX_QUEUE]);
               if(NULL != hist->eventptr) {
              _time_to_date(&hist->time,&date);
              printf("%02d:%02d:%02d %s\n", date.HOUR,date.MINUTE,date.SECOND,hist->eventptr);
               }
            }       
        
      }
   }
   
   if (print_usage)  
   {
      print_usage_simple (shorthelp, argv[0], "Display event log");
   }
   return return_code;
} 
Exemplo n.º 5
0
void lcd_task
(
        uint32_t initial_data
)
{
    DATE_STRUCT      time_rtc;
    uint32_t         time;
    TIME_STRUCT      ts;
    eLCD_Symbols     spec_sym;
    char             time[5];
    char             state = 0;

    puts("\n\n");
    if (_lcd_init() == IO_ERROR)
    {
        puts("_lcd_init() function returned IO_ERROR\n");
        puts("lcd_task blocked\n\n");
        _task_block();
    }

    puts("TWRPI-SLCD display is connected and lcd_task is running\n");

    _lcd_segments( FALSE );
    while(1)
    {
        _rtc_get_time(&time);
        ts.SECONDS = time;
        ts.MILLISECONDS = 0;
        _time_to_date(&ts, &time_rtc);

        /* post meridiem */
        if( time_rtc.HOUR > 12 )
        {
            time_rtc.HOUR -= 12;
            spec_sym = LCD_AM;
            _lcd_symbol( spec_sym, FALSE );
            spec_sym = LCD_PM;
            _lcd_symbol( spec_sym, TRUE);
        }
        else
        {
            spec_sym = LCD_PM;
            _lcd_symbol( spec_sym, FALSE);
            spec_sym = LCD_AM;
            _lcd_symbol( spec_sym, TRUE);
        }
        sprintf( time, "%2d%2d", time_rtc.HOUR, time_rtc.MINUTE);
        if( time_rtc.MINUTE < 10 )
        {
            time[2] = '0';
        }
        _lcd_puts( time );
        spec_sym = LCD_COL1;
        state = (state + 1) & 1;
        _lcd_symbol( spec_sym, (bool)state );
        _time_delay(1000);
    } /* Endwhile */
} /* Endbody */
Exemplo n.º 6
0
void _rtc_get_alarm_mqxd (DATE_STRUCT_PTR time)
{
    TIME_STRUCT         rtc_time;

    rtc_time.SECONDS = alarm_time.seconds;
    rtc_time.MILLISECONDS=0;

    _time_to_date( &rtc_time, time );
}
Exemplo n.º 7
0
void _rtc_get_time_mqxd (DATE_STRUCT_PTR time)
{
    RTC_MemMapPtr rtc = RTC_BASE_PTR;
    TIME_STRUCT         rtc_time;

    rtc_time.SECONDS = rtc->TSR;
    rtc_time.MILLISECONDS=0;

    _time_to_date( &rtc_time, time );
}
Exemplo n.º 8
0
/*FUNCTION**********************************************************************
 *
 * Function Name    : install_alarm
 * Returned Value   :
 * Comments         :
 *
 *END**************************************************************************/
static void install_alarm(void)
{
    DATE_STRUCT     time_rtc;
    TIME_STRUCT     time_mqx;

    _rtc_get_time_mqxd (&time_rtc);
    _time_from_date (&time_rtc, &time_mqx);
    time_mqx.SECONDS += 10;
    _time_to_date (&time_mqx, &time_rtc);
    _rtc_set_alarm_mqxd (&time_rtc);
    printf ("\nSetting alarm to 10 seconds to %02d.%02d.%04d %02d:%02d:%02d ... OK\n", time_rtc.DAY, time_rtc.MONTH, time_rtc.YEAR, time_rtc.HOUR, time_rtc.MINUTE, time_rtc.SECOND);
    printf ("Wasting time ... \n");
}
Exemplo n.º 9
0
static void set_rtc_alarm
    (
        uint_32 seconds
    )
{
    DATE_STRUCT     time_rtc;
    TIME_STRUCT     time_mqx;

    _rtc_get_time_mqxd (&time_rtc);
    _time_from_date (&time_rtc, &time_mqx);
    time_mqx.SECONDS += seconds;
    _time_to_date (&time_mqx, &time_rtc);
    _rtc_set_alarm_mqxd (&time_rtc);
    printf ("\nRTC alarm set to +%d seconds.\n", seconds);
}
Exemplo n.º 10
0
void _rtc_time_to_mqx_date
(
    /* [IN] RTC time representation */
    RTC_TIME_STRUCT_PTR rtc_time,
    /* [OUT] MQX date representation */
    DATE_STRUCT_PTR     mqx_date
)
{
    TIME_STRUCT         time;

    time.SECONDS = rtc_time->seconds;
    time.MILLISECONDS=0;

    _time_to_date( &time, mqx_date );

}
Exemplo n.º 11
0
boolean SEC_GetTime()
{
    boolean res = FALSE;
    
#if DEMOCFG_ENABLE_SNTP
   _ip_address  ipaddr;
   TIME_STRUCT time;
   DATE_STRUCT date;
   char tries = 0;

   /* Try three times to get time */
   while(tries<3)
   {
     _time_delay(1000);
      printf("\nGetting time from time server ... ");

      if (RTCS_resolve_ip_address(SNTP_SERVER,&ipaddr,NULL,0)) {
         /* Contact SNTP server and update time */
         if(SNTP_oneshot(ipaddr,1000)==RTCS_OK)
         {
            printf("Succeeded\n");
            res = TRUE;
            break;
         }
         else
         {
            printf("Failed\n");
         }

      }
      else
      {
         printf("Failed - address not resolved\n");
         break;
      }
    tries++;
   }
   /* Get current time */
   _time_get(&time);
   _time_to_date(&time,&date);
   printf("\nCurrent Time: %02d/%02d/%02d %02d:%02d:%02d\n",
      date.YEAR,date.MONTH,date.DAY,date.HOUR,date.MINUTE,date.SECOND);
#endif

   return res;
}
Exemplo n.º 12
0
void setup_clock(void)
{
    uint32_t ret = 0, i = 0;
    uint32_t sntp_connected = 0;
    uint32_t sntp_max_tries = 3;
    TIME_STRUCT time_s;
    DATE_STRUCT date_s;

    /* NTP server: nist1-lnk.binary.net */
    _ip_address ipaddr = IPADDR(216,229,0,179);

    for (i = 0; i < sntp_max_tries; i++) {

        printf("Getting time from NTP server [ attempt %d of %d ]...\n",
                i+1, sntp_max_tries);

        /* update time from NTP server */
        ret = SNTP_oneshot(ipaddr, 5000);

        if (ret == RTCS_OK) {
            sntp_connected = 1;
            printf("SNTP successfully updated device time\n");
            break;
        } else if (ret == RTCSERR_TIMEOUT) {
            printf("SNTP attempt timed out.\n");
        }

        _time_delay(1000);
    }

    if (sntp_connected == 0) {
        err_sys("SNTP failed to update device time");
    }

    /* print device time, for debug purposes */
    _time_get(&time_s);
    _time_to_date(&time_s, &date_s);
    printf("Current time: %02d/%02d/%02d %02d:%02d:%02d\n",
            date_s.YEAR, date_s.MONTH, date_s.DAY, date_s.HOUR, date_s.MINUTE,
            date_s.SECOND);

    return;
}
Exemplo n.º 13
0
int32_t  MFS_Close_file
    (
        MFS_HANDLE_PTR handle,
        MFS_DRIVE_STRUCT_PTR drive_ptr
    )
{
    TIME_STRUCT             time;
    DATE_STRUCT             clk_time;
    _mfs_error              error_code;

    error_code = MFS_lock_dos_disk( drive_ptr );
    if ( error_code != MFS_NO_ERROR )
    {
        return error_code;
    }

#if !MFSCFG_READ_ONLY
#if MFSCFG_READ_ONLY_CHECK
    if (MFS_is_read_only (drive_ptr))
    {
        error_code = MFS_DISK_IS_WRITE_PROTECTED;
    }
#endif    
    if ((handle->TOUCHED) && (error_code == MFS_NO_ERROR))
    {
        _time_get(&time);
        _time_to_date(&time, &clk_time);
        NORMALIZE_DATE(&clk_time);
        mqx_htods(handle->DIR_ENTRY.TIME, PACK_TIME(clk_time));
        mqx_htods(handle->DIR_ENTRY.DATE, PACK_DATE(clk_time));
        error_code = MFS_Update_entry(drive_ptr, handle);
    }
#endif

    MFS_Free_handle(drive_ptr, handle);
    MFS_unlock(drive_ptr,TRUE);

    return(int32_t) error_code;
}  
Exemplo n.º 14
0
void  *MFS_Create_file
    (
        MFS_DRIVE_STRUCT_PTR drive_ptr,
        unsigned char       attr,       /*[IN] attribute for created file*/
        char                *pathname,   /*[IN] directory and filename of the file to be created */
        _mfs_error_ptr      error_ptr   /*[IN/OUT] error code is written to this address */
    )
{
    MFS_HANDLE_PTR          handle;
    MFS_HANDLE_PTR          next_handle;
    MFS_DIR_ENTRY_PTR       dir_entry_ptr;
    TIME_STRUCT             time;
    DATE_STRUCT             clk_time;
    uint32_t                 dir_cluster;
    uint32_t                 dir_index;
    char                    access;
    _mfs_error              error_code, saved_code = 0;

    if ( (pathname == NULL) || (*pathname == '\0') )
    {
        *error_ptr = MFS_INVALID_PARAMETER;
        return( NULL );
    }

#if MFSCFG_READ_ONLY_CHECK
    if (MFS_is_read_only (drive_ptr))
    {
        *error_ptr = MFS_DISK_IS_WRITE_PROTECTED;
        return NULL;
    }
#endif

    error_code = MFS_lock_dos_disk( drive_ptr );
    if ( error_code != MFS_NO_ERROR )
    {
        *error_ptr = error_code;
        return( NULL );
    }

    handle = NULL;

    attr &= (MFS_ATTR_READ_ONLY | MFS_ATTR_HIDDEN_FILE | MFS_ATTR_SYSTEM_FILE | MFS_ATTR_ARCHIVE | MFS_ATTR_VOLUME_NAME);
    attr |= MFS_ATTR_ARCHIVE;
    access = (attr & MFS_ATTR_READ_ONLY) ? MFS_ACCESS_READ_ONLY : MFS_ACCESS_READ_WRITE;
    dir_entry_ptr =  MFS_Create_entry_slave(drive_ptr, attr, pathname, &dir_cluster, &dir_index, &error_code, TRUE);

    if ( (dir_entry_ptr != NULL) && !(attr & MFS_ATTR_VOLUME_NAME) )
    {
        handle = MFS_Get_handle(drive_ptr,dir_entry_ptr);
        if ( handle != NULL )
        {
            handle->DIR_CLUSTER = dir_cluster;
            handle->DIR_INDEX = dir_index;
            handle->ACCESS = access;
            handle->CURRENT_CLUSTER = 0;
            handle->PREVIOUS_CLUSTER = 0;

            /*
            ** If file exists, overwrite and set size to 0
            */
            if ( error_code == MFS_FILE_EXISTS )
            {
                _time_get(&time);
                _time_to_date(&time, &clk_time);
                NORMALIZE_DATE(&clk_time);
                saved_code = MFS_Release_chain(drive_ptr, clustoh(handle->DIR_ENTRY.HFIRST_CLUSTER, handle->DIR_ENTRY.LFIRST_CLUSTER));
                if ( saved_code == MFS_NO_ERROR || saved_code == MFS_LOST_CHAIN )
                {
                    clustod(handle->DIR_ENTRY.HFIRST_CLUSTER, handle->DIR_ENTRY.LFIRST_CLUSTER, 0);
                    mqx_htodl(handle->DIR_ENTRY.FILE_SIZE, 0L);
                    mqx_htodc(handle->DIR_ENTRY.ATTRIBUTE, attr);
                    mqx_htods(handle->DIR_ENTRY.TIME, PACK_TIME(clk_time));
                    mqx_htods(handle->DIR_ENTRY.DATE, PACK_DATE(clk_time));
                    error_code = MFS_Update_entry(drive_ptr, handle);

                    /*
                    ** If the same file is already open, mark it as 'freshly
                    ** truncated' so reads and writes don't clobber any data.
                    */
                    if ( error_code == MFS_NO_ERROR )
                    {
                        next_handle =  (MFS_HANDLE_PTR) _queue_head(&drive_ptr->HANDLE_LIST);
                        while ( next_handle )
                        {
                            if ( next_handle->DIR_CLUSTER == dir_cluster && next_handle->DIR_INDEX == dir_index )
                            {
                                next_handle->CURRENT_CLUSTER = 0;
                                next_handle->PREVIOUS_CLUSTER = 0;
                            }
                            next_handle =  (MFS_HANDLE_PTR) _queue_next(&drive_ptr->HANDLE_LIST, (QUEUE_ELEMENT_STRUCT_PTR) next_handle);
                        }  
                    }
                }
            }

            /*
            ** No need to update the disk image if we didn't change anything.
            */
            if ( (mqx_dtohc(handle->DIR_ENTRY.ATTRIBUTE) != attr) && (error_code == MFS_NO_ERROR) )
            {
                mqx_htodc(handle->DIR_ENTRY.ATTRIBUTE, attr);
                error_code = MFS_Update_entry(drive_ptr, handle);
            }

        }
        else
        {
            error_code = MFS_INSUFFICIENT_MEMORY;
        }  
    }

    MFS_unlock(drive_ptr,FALSE);

    if ( error_code == MFS_NO_ERROR && saved_code == MFS_LOST_CHAIN )
    {
        *error_ptr = saved_code;
    }
    else
    {
        *error_ptr = error_code; 
    }  

    return((void *)handle);
}  
Exemplo n.º 15
0
int32_t Shell_smtp (int32_t argc, char *argv[])
{
    struct addrinfo            hints;
    struct addrinfo           *result, *rp;
    int32_t                    retval = 0;
    uint32_t                   sfd = 0;
    int32_t                    err_code = 0;
    bool                       print_help;
    bool                       shorthelp = FALSE;
    SMTP_PARAM_STRUCT          params;
    char                      *errstr = NULL;
    char                      *server = NULL;
    char                      *optstring = ":f:t:s:u:p:m:h";
    int32_t                    next_option;
    char                      *email_text = NULL;
    uint32_t                   email_size = 0;
    TIME_STRUCT                time;
    DATE_STRUCT                date;
    char                       date_str[DATE_LENGTH];
    SHELL_GETOPT_CONTEXT       gopt_context;

    params.login = NULL;
    params.pass = NULL;

    print_help = Shell_check_help_request(argc, argv, &shorthelp);
    
    if (print_help)
    {
        if (!shorthelp)
        {
            fprintf(stdout,"Usage:"); 
        }
        fprintf(stdout, "%s", argv[0]);
        print_usage(stdout, shorthelp);
        err_code = SHELL_EXIT_SUCCESS;
        return(err_code);
    }
    
    /* Parse command line options */
    Shell_getopt_init(&gopt_context);
    do
    {
        next_option = Shell_getopt(argc, argv, optstring, &gopt_context);
        switch (next_option)
        {
            case 'f':
                params.envelope.from = gopt_context.optarg;
                break;
            case 't':
                params.envelope.to = gopt_context.optarg;
                break;
            case 'm':
                params.text = gopt_context.optarg;
                break;
            case 's':
                server = gopt_context.optarg;
                break;
            case 'u':
                params.login = gopt_context.optarg;
                break;
            case 'p':
                params.pass = gopt_context.optarg;
                break;
            case 'h':
                print_usage (stdout, FALSE);
                return(SHELL_EXIT_SUCCESS);
            case '?': /* User specified an invalid option. */
                print_usage(stderr, TRUE);
                return(SHELL_EXIT_ERROR);
            case ':': /* Option has a missing parameter. */
                printf("Option -%c requires a parameter.\n", next_option);
                return(SHELL_EXIT_ERROR);
            case -1: /* Done with options. */
                break;
            default:
                break;
        }
    }while(next_option != -1);
    
    
    _time_get(&time);
    _time_to_date(&time,&date);

    snprintf(date_str, DATE_LENGTH, "%s, %d %s %d %02d:%02d:%02d -0000",
        wday[date.WDAY],date.DAY, months[date.MONTH-1],date.YEAR, date.HOUR, date.MINUTE, date.SECOND);
    /* Evaluate email size */
    email_size = strlen(params.envelope.from) + 
                 strlen(params.envelope.to) +
                 strlen(params.text) +
                 strlen(date_str) +
                 strlen("From: <>\r\n") +
                 strlen("To: <>\r\n") + 
                 strlen("Subject: Freescale Tower Email\r\n") +
                 strlen("Date: \r\n\r\n") +   
                 strlen("\r\n") + 1;
    /* Allocate space */
    email_text = (char *) _mem_alloc_zero(email_size);
    if (email_text == NULL)
    {
        fprintf(stderr, "  Unable to allocate memory for email message.\n");
        err_code = SHELL_EXIT_ERROR;
        return(err_code);
    }
    /* Prepare email message */
    snprintf(email_text, email_size, "From: <%s>\r\n"
                                     "To: <%s>\r\n"
                                     "Subject: Freescale Tower Email\r\n"
                                     "Date: %s\r\n\r\n"
                                     "%s\r\n",
                                     params.envelope.from,
                                     params.envelope.to,
                                     date_str,
                                     params.text);
    params.text = email_text;
    
    _mem_zero(&hints, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM; /* TCP socket */
    hints.ai_flags = AI_PASSIVE;     /* For wildcard IP address */
    hints.ai_protocol = 0;           /* Any protocol */
    hints.ai_canonname = NULL;
    hints.ai_addr = NULL;
    hints.ai_next = NULL;

    retval = getaddrinfo(server, NULL, &hints, &result);
    if (retval != 0)
    {
        fprintf(stderr, "  getaddrinfo failed. Error: 0x%X\n", retval);
        err_code = SHELL_EXIT_ERROR;
        return(err_code);
    }
    /* Allocate buffer for error message */
    errstr = (char *) _mem_alloc_system_zero(ERR_MSG_BUFF_SIZE);
    /* Try to send email using one of addresses. If it fails try another one. */
    for (rp = result; rp != NULL; rp = rp->ai_next)
    {
        _mem_copy(rp->ai_addr, &params.server, sizeof(params.server));
        /* Try to send email */
        retval = SMTP_send_email(&params, errstr, ERR_MSG_BUFF_SIZE);
        /* If connection failed try another address */
        if (retval != SMTP_ERR_CONN_FAILED)
        {
            break;
        }
    }
    /* No address succeeded */
    if (rp == NULL)
    {
        fprintf(stderr, "  Unable to connect to %s.\n", server);
        err_code = SHELL_EXIT_ERROR;
    }

    if (retval != SMTP_OK)
    {
        printf("  Email sending failed%s %s\n", (strlen(errstr) > 0) ? ":":".", errstr);
        err_code = SHELL_EXIT_ERROR;
    }
    else
    {
        printf("  Email send. Server response: %s", errstr);
    }
    /* Cleanup */
    freeaddrinfo(result);
    _mem_free(errstr);
    _mem_free(email_text);
    return(err_code);
} 
Exemplo n.º 16
0
MFS_DIR_ENTRY_PTR MFS_Create_directory_entry
    (
    MFS_DRIVE_STRUCT_PTR    drive_ptr,  /*[IN] the drive on which to operate */
    char_ptr        filename_ptr,       /*[IN] pointer to the file's name */
    char            attribute,          /*[IN] attribute to be assigned to the file */
    uint_32_ptr     dir_cluster_ptr,    /*[IN/OUT] indicates cluster in which the entry was put.*/
    uint_32_ptr     dir_index_ptr,      /*[IN/OUT] index of the location of new file within the directory */
    uint_32_ptr     error_ptr           /*[IN/OUT] error_return_address */
    )
{
    MFS_DIR_ENTRY_PTR     dir_entry_ptr, dir_entry_ptr1;
    MFS_DIR_ENTRY_PTR     temp_entry_ptr;
    TIME_STRUCT           time;
    DATE_STRUCT           clk_time;
    uint_32               dir_index, saved_index, temp_index;
    uint_32               entry_cluster, saved_cluster, temp_cluster;
    uint_32               needed_entries, i;
    int_32                length;
    boolean               found_all = FALSE;
    char                  temp_name[SFILENAME_SIZE+1], short_name[SFILENAME_SIZE+1];  
    uchar                 checksum = 0;
    uint_32               prev_cluster= CLUSTER_INVALID, saved_prev_cluster, temp_prev_cluster; 

    dir_entry_ptr = NULL;
    entry_cluster = *dir_cluster_ptr;
    dir_index = 0;

    /*
    ** Check for duplicate file
    */
    if ( filename_ptr == NULL )
    {
        *error_ptr = MFS_INVALID_PARAMETER;
    }
    else if ( *filename_ptr == '\0' )
    {
        *error_ptr = MFS_FILE_NOT_FOUND;
    }
#if MFSCFG_READ_ONLY_CHECK
    else if (MFS_is_read_only (NULL, drive_ptr))
    {
        *error_ptr = MFS_DISK_IS_WRITE_PROTECTED;
    }
#endif
    else if ( (dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, filename_ptr,
        &entry_cluster, &dir_index, &prev_cluster, MFS_ATTR_ANY, error_ptr)) != NULL )
    {
        *error_ptr = MFS_FILE_EXISTS;
    }
    else
    {
        /*
        ** Search for an empty slot
        */

        dir_index = 0;
        dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, attribute, error_ptr); 


        if ( MFS_Dirname_valid(filename_ptr) )
        {
            found_all = TRUE;
        }

        if ( dir_entry_ptr ==  NULL && !*error_ptr )
        {
            found_all = TRUE;
        }

        /* 
        ** Save it now because we might not go into the while loop
        ** If we don't go into while, we lose original values when these 
        ** variables are restored after the while loop
        */
        saved_cluster = entry_cluster;
        saved_index = dir_index;

        while ( !found_all )
        {
            /* Calculate the amount of extra entries needed for LFN's */
            saved_cluster = entry_cluster;
            saved_index = dir_index;

            for ( needed_entries = (strlen(filename_ptr) + 12) / 13;  needed_entries >= 1 && !found_all; needed_entries-- )
            {
                *error_ptr = MFS_Increment_dir_index(drive_ptr, &entry_cluster, &dir_index, NULL);  
                if ( entry_cluster == CLUSTER_EOF && !*error_ptr )
                {
                    /* This means the LFN will span a cluster. */
                    found_all = TRUE;
                    break;
                }
                temp_index = dir_index;
                temp_cluster = entry_cluster;
                temp_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, MFS_ATTR_ANY,error_ptr); 
                if ( *error_ptr )
                {
                    return NULL;
                }
                if ( !temp_entry_ptr )
                {
                    found_all = TRUE;
                    dir_entry_ptr = NULL;
                    break;
                }
                else if ( dir_index == temp_index && temp_cluster == entry_cluster )
                {
                    continue;
                }
                else
                {
                    break;
                }        
            }  

            if ( needed_entries == 0 )
            {
                found_all = TRUE;
            }

            if ( found_all )
            {
                dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &saved_cluster, &saved_index, &saved_prev_cluster, MFS_ATTR_ANY,error_ptr); 
            }
        }  

        entry_cluster = saved_cluster;
        dir_index = saved_index;


        if ( dir_entry_ptr ==  NULL && !*error_ptr )
        {
            /*
            ** Empty spot not found... get a new cluster and use the first entry.
            */
            dir_index = 0;
            if ( entry_cluster == 0 )
            {
                *error_ptr = MFS_ROOT_DIR_FULL;
                return(NULL);
            }
            else
            {
                *error_ptr = MFS_Add_cluster_to_chain(drive_ptr, entry_cluster, &entry_cluster);
                if ( *error_ptr == MFS_NO_ERROR )
                {
                    *error_ptr = MFS_Clear_cluster(drive_ptr, entry_cluster);
                    if ( *error_ptr )
                    {
                        return(NULL);
                    }
                    dir_entry_ptr = (pointer) drive_ptr->DIR_SECTOR_PTR;
                    *error_ptr = MFS_Write_back_fat(drive_ptr);
                    if ( *error_ptr )
                    {
                        return(NULL);
                    }
                }
            }  
        }
    }  

    if ( *error_ptr == MFS_NO_ERROR )
    {
        /* At this point we have one of the following cases 
        **
        ** 1. We have a normal 8.3 filename and an empty slot for it
        ** 2. We have a LFN, and a slot which has enough consecutive free slots
        **    to store the LFN, followed by the actual entry
        */

        /* If the file is a normal 8.3 file */
        if ( MFS_Dirname_valid(filename_ptr) )
        {
            _mem_zero(dir_entry_ptr, sizeof (MFS_DIR_ENTRY));            
            htodc(dir_entry_ptr->ATTRIBUTE, attribute);
            MFS_Expand_dotfile(filename_ptr, dir_entry_ptr->NAME);

            _time_get(&time);
            _time_to_date(&time, &clk_time);
            NORMALIZE_DATE(&clk_time);
            htods(dir_entry_ptr->TIME, PACK_TIME(clk_time));
            htods(dir_entry_ptr->DATE, PACK_DATE(clk_time));   
            drive_ptr->DIR_SECTOR_DIRTY = TRUE;
        }
        else
        {
            /* Case where the file is a LFN */   
            length = strlen(filename_ptr);

            /* Get the 8.3 name and calculate the checksum */
            temp_index = 0;
            temp_cluster = *dir_cluster_ptr;
            *error_ptr = MFS_lfn_to_sfn(filename_ptr, temp_name);

            if ( !*error_ptr )
            {
                do
                {
                    dir_entry_ptr1 = MFS_Find_directory_entry(drive_ptr, temp_name, &temp_cluster, &temp_index, &temp_prev_cluster, MFS_ATTR_ANY, error_ptr); 
                    if ( !*error_ptr && dir_entry_ptr1 != NULL )
                    {
                        MFS_increment_lfn(temp_name);
                        temp_index = 0;       
                    }
                } while ( !*error_ptr && dir_entry_ptr1 != NULL );
                dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, attribute, error_ptr); 

                /*
                ** The memory should be cleared after finding the directory entry.
                */
                _mem_zero(dir_entry_ptr, sizeof (MFS_DIR_ENTRY));

                /* Setup the final slot */
                ((MFS_LNAME_ENTRY_PTR) dir_entry_ptr)->ID |= MFS_LFN_END;
                drive_ptr->DIR_SECTOR_DIRTY = TRUE;

                MFS_Expand_dotfile(temp_name,short_name);
                checksum = MFS_lfn_checksum(short_name);
            }

            while ( length && !*error_ptr )
            {
                i = length - ((length -1) % 13 + 1);
                *error_ptr = MFS_lfn_name_to_entry(filename_ptr + i, (MFS_LNAME_ENTRY_PTR) dir_entry_ptr);

                /* Set the entry number, the checksum value and the attribute */
                ((MFS_LNAME_ENTRY_PTR) dir_entry_ptr)->ID |= (length + 12) / 13;
                ((MFS_LNAME_ENTRY_PTR) dir_entry_ptr)->ALIAS_CHECKSUM= checksum;
                ((MFS_LNAME_ENTRY_PTR) dir_entry_ptr)->ATTR = MFS_ATTR_LFN;
                drive_ptr->DIR_SECTOR_DIRTY = TRUE;

                length -= (length - i);
                if ( length < 0 )
                {
                    length = 0;
                }

                if ( length && !*error_ptr )
                {

                    dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, attribute, error_ptr); 
                    if ( dir_entry_ptr ==  NULL && !*error_ptr )
                    {
                        /* No empty spots... We need to get a new cluster */
                        dir_index = 0; 
                        if ( entry_cluster == 0 )
                        {
                            *error_ptr = MFS_ROOT_DIR_FULL; 
                            return(NULL); 
                        }
                        else
                        {
                            *error_ptr = MFS_Add_cluster_to_chain( drive_ptr, entry_cluster, &entry_cluster); 
                            if ( *error_ptr == MFS_NO_ERROR )
                            {
                                *error_ptr = MFS_Clear_cluster(drive_ptr, entry_cluster); 
                                if ( *error_ptr )
                                {
                                    return(NULL); 
                                }
                                dir_entry_ptr = (pointer) drive_ptr->DIR_SECTOR_PTR; 
                                *error_ptr = MFS_Write_back_fat(drive_ptr); 
                                if ( *error_ptr )
                                {
                                    return(NULL); 
                                }
                            }
                        }   
                    }
                    _mem_zero(dir_entry_ptr, sizeof (MFS_DIR_ENTRY));
                    drive_ptr->DIR_SECTOR_DIRTY = TRUE;
                }
            }  

            /* LFN is written, so write the actual entry */
            if ( !*error_ptr )
            {
                dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, attribute, error_ptr); 
                if ( dir_entry_ptr == NULL && !*error_ptr )
                {
                    dir_index = 0; 
                    if ( entry_cluster == 0 )
                    {
                        *error_ptr = MFS_ROOT_DIR_FULL; 
                        return(NULL); 
                    }
                    else
                    {
                        *error_ptr = MFS_Add_cluster_to_chain( drive_ptr, entry_cluster, &entry_cluster); 
                        if ( *error_ptr == MFS_NO_ERROR )
                        {
                            *error_ptr =  MFS_Clear_cluster(drive_ptr, entry_cluster); 
                            if ( *error_ptr )
                            {
                                return(NULL); 
                            }
                            dir_entry_ptr = (pointer) drive_ptr->DIR_SECTOR_PTR; 
                            *error_ptr = MFS_Write_back_fat(drive_ptr); 
                            if ( *error_ptr )
                            {
                                return(NULL); 
                            }
                        }
                    }   
                }

                _mem_zero(dir_entry_ptr, sizeof (MFS_DIR_ENTRY));
                htodc(dir_entry_ptr->ATTRIBUTE, attribute);
                MFS_Expand_dotfile(temp_name, dir_entry_ptr->NAME);

                _time_get(&time);
                _time_to_date(&time, &clk_time);
                NORMALIZE_DATE(&clk_time);
                htods(dir_entry_ptr->TIME, PACK_TIME(clk_time));
                htods(dir_entry_ptr->DATE, PACK_DATE(clk_time));   
                drive_ptr->DIR_SECTOR_DIRTY = TRUE;
            }
        }  

        if ( *error_ptr )
        {
            return(NULL);
        }
    }

    *dir_cluster_ptr = entry_cluster;
    *dir_index_ptr = dir_index;
    return(dir_entry_ptr);
}  
Exemplo n.º 17
0
void main_task
(
        uint_32 initial_data
)
{
    DATE_STRUCT     time_rtc;
    TIME_STRUCT     time_mqx;

    if (_lwevent_create(&lwevent,0) != MQX_OK)
    {
        printf("\nMake event failed");
        _task_block();
    }

    printf ("\fStart time (MQX synchronized to RTC time during bsp init):\n\n");


    /* initialize time */
    time_rtc.YEAR     = 2010;
    time_rtc.MONTH    = 10;
    time_rtc.DAY      = 15;
    time_rtc.HOUR     = 10;
    time_rtc.MINUTE   = 8;
    time_rtc.SECOND   = 0;
    time_rtc.MILLISEC = 0;

    _time_from_date (&time_rtc, &time_mqx);

    _time_set( &time_mqx);
    if( _rtc_sync_with_mqx(FALSE) != MQX_OK )
    {
        printf("\nError synchronize time!\n");
        _task_block();
    }
    _time_get (&time_mqx);

    _time_to_date (&time_mqx, &time_rtc);
    print_mqx_time(&time_rtc, &time_mqx);
    print_current_time();

    /* except MPC5125 */
#ifndef BSP_TWRMPC5125
    install_interrupt();

    /* enable stopwatch */
    install_stopwatch();

    /* enable alarm */
    install_alarm();

    _lwevent_wait_ticks(&lwevent,LWE_ALARM,FALSE,0);
    _lwevent_clear(&lwevent,LWE_ALARM);

    printf ("\nALARM!\n");
    print_current_time();
    /* end of alarm */

    printf ("Continue wasting time (2 minutes max) ...\n");
    _lwevent_wait_ticks(&lwevent,LWE_STOPWATCH,FALSE,0);
    _lwevent_clear(&lwevent,LWE_STOPWATCH);

    printf ("\nSTOPWATCH!\n");
    print_current_time();

    printf ("\nClearing RTC:\n");
    _rtc_init (RTC_INIT_FLAG_CLEAR | RTC_INIT_FLAG_ENABLE);

    print_current_time();

    install_alarm();
    _lwevent_wait_ticks(&lwevent,LWE_ALARM,FALSE,0);
    _lwevent_clear(&lwevent,LWE_ALARM);

    printf ("ALARM!\n");
    print_current_time();

#else /* BSP_TWRMPC5125 */
    printf ("Waste 10 seconds here\n");
    _time_delay(10000);
    _rtc_get_time_mqxd (&time_rtc);
    print_rtc_time(&time_rtc, &time_mqx);
#endif

    printf ("Synchronize RTC to MQX time again:\n");
    _rtc_sync_with_mqx (FALSE);
    _rtc_get_time_mqxd (&time_rtc);
    _time_from_date (&time_rtc, &time_mqx);
    print_rtc_time(&time_rtc, &time_mqx);

#if PSP_HAS_IRTC == 1
    irtc_test();
#endif /* PSP_HAS_IRTC == 1 */

    /* Test tamper event functionality on MCF51EMxx device */
#if PSP_MQX_CPU_IS_MCF51EM
    test_tamper();
#else
    printf ("Finish, press/hold reset to repeat.\n");
    _task_block() ;
#endif
}