cptime.c File Reference


Detailed Description

Control Panel local time module.

The local time module is in charge of the Control Panel local time management.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file cptime.c.

#include <stdio.h>
#include <string.h>
#include <time.h>
#include "FreeRTOS.h"
#include "task.h"
#include "shell.h"

Go to the source code of this file.

Data Structures

struct  st_scheduled_cmd

Defines

#define CPTIME_MAX_NB_SCHED_CMDS   10

Typedefs

typedef struct st_scheduled_cmd Scheduled_Cmd

Functions

eExecStatus e_cptime_cmd_get_config (eModId xModId, signed short FsNavId, int ac, signed portCHAR *av[], signed portCHAR **ppcStringReply)
 The get time config command: get the config fields value of the time module Takes no parameter.
eExecStatus e_cptime_cmd_set_config (eModId xModId, signed short FsNavId, int ac, signed portCHAR *av[], signed portCHAR **ppcStringReply)
 The set time config command: set each config field value of the time module. Takes one parameter : field=value.
eExecStatus e_cptime_RecordScheduledCmd (char *pcDate, int CmdId, void(*pfScheduledCmd)(int, void *), void *pvCmdParams, signed portCHAR **ppcStringReply)
 Record a scheduled command.
static void prv_v_ShiftScheduledCmdsArray (int SrcIdx, int DstIdx)
 Shift the Scheduled Cmds array from the SrcIdx index down to the DstIdx index.
void v_cptime_ExecuteScheduledCmd (void)
 Execute a scheduled command if expiration date is up.
void v_cptime_GetDateInFatStringFormat (char *pcDate)
 Get the current time in the "YYYYMMDDHHMMSSMS" string format.
void v_cptime_Init (void)
 Init the time module.
void v_cptime_UpdateLocalTime (void)
 Update the local time.

Variables

static Scheduled_Cmd axCmdsSched [CPTIME_MAX_NB_SCHED_CMDS]
const signed portCHAR *const CPTIME_ERRMSG_SETCFG_INVALIDDATE = (signed portCHAR *)ERROR_CRLF"Invalid date"CRLF
const signed portCHAR *const CPTIME_ERRMSG_SETCFG_SYNTAXERROR = (signed portCHAR *)ERROR_CRLF"Usage: set_sys_config sys=time field=value"CRLF
const signed portCHAR *const CPTIME_ERRMSG_SETCFG_UNKNOWNFIELD = (signed portCHAR *)ERROR_CRLF"Unknown configuration field"CRLF
const signed portCHAR *const CPTIME_ERRMSG_SETVAL_INVALIDTIME = (signed portCHAR *)ERROR_CRLF"Invalid schedule date"CRLF
const signed portCHAR *const CPTIME_ERRMSG_SETVAL_UNAVAILSCHEDSLOT = (signed portCHAR *)ERROR_CRLF"No scheduling slot left"CRLF
static int NbSchedCmd = 0
time_t xcptime_LocalTime
static portTickType xcptime_TickOrigin = 0


Define Documentation

#define CPTIME_MAX_NB_SCHED_CMDS   10

Max nb of scheduled commands.

Definition at line 62 of file cptime.c.

Referenced by e_cptime_cmd_set_config(), e_cptime_RecordScheduledCmd(), and prv_v_ShiftScheduledCmdsArray().


Typedef Documentation


Function Documentation

eExecStatus e_cptime_cmd_get_config ( eModId  xModId,
signed short  FsNavId,
int  ac,
signed portCHAR *  av[],
signed portCHAR **  ppcStringReply 
)

The get time config command: get the config fields value of the time module Takes no parameter.

Note:
This function must be of the type pfShellCmd defined by the shell module.
Parameters:
xModId Input. The module that is calling this function.
FsNavId Ignored.
ac Input. The argument counter. For this command, should be 1.
av Input. The argument vector.
ppcStringReply Input/Output. The response string. If Input is NULL, no response string will be output. Else a malloc for the response string is performed here; the caller must free this string.
Returns:
the status of the command execution.

Definition at line 431 of file cptime.c.

References SHELL_ERRMSG_MEMALLOC, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK, sprintf(), and xcptime_LocalTime.

Referenced by e_syscmds_cmd_get_config().

00434 {
00435    struct tm *pxDate;
00436 
00437 
00438    // NOTE: currently the only configurable item of the time module is the
00439    // current time. So, we're only returning the current time like this:
00440    // "curtime=mm/dd/yy hh:mm:ss\r\n" => 31 Bytes (including the \0 character).
00441 
00442 
00443    if(ppcStringReply != NULL)
00444    {
00445       // Allocate a buffer for answer
00446       *ppcStringReply = (signed portCHAR *)pvPortMalloc(31);
00447       if( NULL == *ppcStringReply )
00448       {
00449          *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_MEMALLOC;
00450          return( SHELL_EXECSTATUS_KO );
00451       }
00452 
00453       // Get the broken-down representation of the current date.
00454       pxDate = gmtime( &xcptime_LocalTime );
00455 
00456       // WARNING: pxDate->tm_year == nunmber of years since 1900.
00457       // For years >= 2000, we'll display the last 2 digits only.
00458       if( pxDate->tm_year >= 100 )  pxDate->tm_year -= 100;
00459       sprintf( (char *)*ppcStringReply, "curtime=%02d/%02d/%02d  %02d:%02d:%02d\r\n",
00460                 pxDate->tm_mon +1, pxDate->tm_mday, pxDate->tm_year,
00461                 pxDate->tm_hour, pxDate->tm_min, pxDate->tm_sec );
00462 
00463       /* no error, return */
00464       return( SHELL_EXECSTATUS_OK );
00465    }
00466    return( SHELL_EXECSTATUS_KO );
00467 }

eExecStatus e_cptime_cmd_set_config ( eModId  xModId,
signed short  FsNavId,
int  ac,
signed portCHAR *  av[],
signed portCHAR **  ppcStringReply 
)

The set time config command: set each config field value of the time module. Takes one parameter : field=value.

Note:
This function must be of the type pfShellCmd defined by the shell module.
Parameters:
xModId Input. The module that is calling this function.
FsNavId Ignored.
ac Input. The argument counter. For this command, should be 1.
av Input. The argument vector.
ppcStringReply Input/Output. The response string. If Input is NULL, no response string will be output. Else a malloc for the response string is performed here; the caller must free this string.
Returns:
the status of the command execution.

Definition at line 297 of file cptime.c.

References CPTIME_ERRMSG_SETCFG_INVALIDDATE, CPTIME_ERRMSG_SETCFG_SYNTAXERROR, CPTIME_ERRMSG_SETCFG_UNKNOWNFIELD, CPTIME_MAX_NB_SCHED_CMDS, NAKED_TRACE_COM2, NbSchedCmd, prv_v_ShiftScheduledCmdsArray(), SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK, xcptime_LocalTime, and st_scheduled_cmd::xScheduledTime.

Referenced by e_syscmds_cmd_set_config().

00300 {
00301    /* int           fd;
00302    eFsCmdsStatus eFsStatus; */
00303    struct tm     xLogDate;
00304    time_t        xRawDate;
00305    int           i;
00306    int           SrcIdx=0;
00307 
00308 
00309    // NOTE: currently the only configurable item of the time module is the
00310    // current time. "curtime=mm/dd/yy hh:mm:ss"
00311 
00312    // 1) Check the input.
00313    //  i) Exactly two arguments (the field and the value).
00314    if( 2 != ac )
00315    {   // Syntax error.
00316       if(ppcStringReply != NULL)
00317          *ppcStringReply = (signed portCHAR *)CPTIME_ERRMSG_SETCFG_SYNTAXERROR;
00318       return( SHELL_EXECSTATUS_KO );
00319    }
00320 
00321    // The field must be one of : {curtime}
00322    if( !strcmp("curtime", (char *)av[0]) )
00323    {
00324       // 2) Read the current time string
00325       sscanf( (char *)av[1], "%02d/%02d/%02d %02d:%02d:%02d",
00326               &(xLogDate.tm_mon), &(xLogDate.tm_mday), &(xLogDate.tm_year),
00327               &(xLogDate.tm_hour), &(xLogDate.tm_min), &(xLogDate.tm_sec) );
00328       (xLogDate.tm_mon)--; // Adjust to the logic where January index is 0.
00329       // WARNING: pxLogDate->tm_year == number of years since 1900.
00330       // We get the last 2 digits only : we suppose the year is >= 2000.
00331       xLogDate.tm_year += 100;
00332       xLogDate.tm_isdst = 0; // Daylight saving time info is not in effect.
00333 
00334       // Convert time broken-down representation to arithmetic representation.
00335       xRawDate = mktime( &xLogDate );
00336 
00337       if( -1 == xRawDate )
00338       {
00339          if(ppcStringReply != NULL)
00340             *ppcStringReply = (signed portCHAR *)CPTIME_ERRMSG_SETCFG_INVALIDDATE;
00341          return( SHELL_EXECSTATUS_KO );
00342       }
00343       else
00344       {
00345          NAKED_TRACE_COM2( "CPTIME:NbSchedCmd=%d", NbSchedCmd );
00346          // Update all cells of the scheduled commands array: cancel the schedules
00347          // that are in the past but keep the schedules that are still in the future.
00348          for( i=NbSchedCmd-1; i>=0; i-- )
00349          {
00350             if( axCmdsSched[i].xScheduledTime <= xRawDate )
00351             {    // This schedule is in the past => cancel it and all other cells
00352                  // that are further down the past.
00353                // NOTE: all below scheduled commands are obsolete if this one is.
00354                // => Cancel them too.
00355                SrcIdx = i+1; // Remember the last active scheduled commands index.
00356                for( i=0; i<SrcIdx; i++ )
00357                {
00358                   axCmdsSched[i].xScheduledTime = 0; // Mark this cell as unused.
00359                   NbSchedCmd--; // One scheduled cmd removed.
00360                }
00361                NAKED_TRACE_COM2( "CPTIME:NbSchedCmd=%d, SrcIdx=%d", NbSchedCmd, SrcIdx );
00362                break;
00363             }
00364          }
00365 
00366          // Shift the Scheduled Commands array down to the index 0.
00367          if( ( 0 != NbSchedCmd ) && ( CPTIME_MAX_NB_SCHED_CMDS > SrcIdx ) )
00368          {
00369             NAKED_TRACE_COM2( "CPTIME:Shifting array from SrcIdx=%d to 0 idx", SrcIdx );
00370             prv_v_ShiftScheduledCmdsArray( SrcIdx, 0 );
00371          }
00372 
00373          // Switch to new time.
00374          xcptime_LocalTime = xRawDate;
00375       }
00376    }
00377    else
00378    {   // Unsupported field.
00379       if(ppcStringReply != NULL)
00380          *ppcStringReply = (signed portCHAR *)CPTIME_ERRMSG_SETCFG_UNKNOWNFIELD;
00381       return( SHELL_EXECSTATUS_KO );
00382    }
00383    return( SHELL_EXECSTATUS_OK );
00384 
00385 
00386 /****** CODE TO USE WHEN THERE ARE OTHER FIELDS THAN CURTIME.
00387    if ((fd = open(TIME_CONFIG_FILE, (O_RDWR))) >= 0)
00388    {
00389       if (config_file_set_value(fd, ac, av) != 0)
00390       {
00391          if(ppcStringReply != NULL)
00392          {
00393             *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00394          }
00395          return( SHELL_EXECSTATUS_KO );
00396       }
00397       close (fd);
00398 
00399       return( SHELL_EXECSTATUS_OK );
00400    }
00401    else
00402    {
00403       eFsStatus = e_fscmds_CheckNavError(); // Get the fs error.
00404       if( ppcStringReply != NULL )
00405       {
00406          v_fscmds_GetStrMsgFromErr(eFsStatus, ppcStringReply);
00407       }
00408       return( SHELL_EXECSTATUS_KO );
00409    }
00410 ******/
00411 
00412 }

eExecStatus e_cptime_RecordScheduledCmd ( char *  pcDate,
int  CmdId,
void(*)(int, void *)  pfScheduledCmd,
void *  pvCmdParams,
signed portCHAR **  ppcStringReply 
)

Record a scheduled command.

Parameters:
pcDate Input. The date the cmd is scheduled at. Format: seconds OR mm/dd/yy hh:mm:ss
CmdId Scheduled command id.
pfScheduledCmd pointer on fct to be called at expiration date
pvCmdParams pointer on struct to give as param to pfScheduledCmd
ppcStringReply Input/Output. The response string. If Input is NULL, no response string will be output. Else a malloc for the response string is performed here; the caller must free this string.
Returns:
the status of the command execution.

Definition at line 204 of file cptime.c.

References st_scheduled_cmd::CmdId, CPTIME_ERRMSG_SETVAL_INVALIDTIME, CPTIME_ERRMSG_SETVAL_UNAVAILSCHEDSLOT, CPTIME_MAX_NB_SCHED_CMDS, NbSchedCmd, st_scheduled_cmd::pfScheduledCmd, st_scheduled_cmd::pxParam, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK_NO_FREE, xcptime_LocalTime, and st_scheduled_cmd::xScheduledTime.

Referenced by e_actuator_ScheduleCmdSet().

00208 {
00209    time_t   xTimeOffsetSec;
00210    int      i, j, k;
00211 
00212 
00213    // Check if there is at least one spot left in the scheduled cmd array.
00214    if( CPTIME_MAX_NB_SCHED_CMDS == NbSchedCmd )
00215    {
00216       *ppcStringReply = (signed portCHAR *)CPTIME_ERRMSG_SETVAL_UNAVAILSCHEDSLOT;
00217       return( SHELL_EXECSTATUS_KO );
00218    }
00219 
00220    // First implem: the timevalue is an offset in seconds.
00221    xTimeOffsetSec = xcptime_LocalTime + atoi( pcDate );
00222    if( xTimeOffsetSec < ( xcptime_LocalTime +3 ) )
00223    {
00224       *ppcStringReply = (signed portCHAR *)CPTIME_ERRMSG_SETVAL_INVALIDTIME;
00225       return( SHELL_EXECSTATUS_KO );
00226    }
00227 
00228    // Find a spot where to insert the new record.
00229    for( i=0; (i<CPTIME_MAX_NB_SCHED_CMDS)&&(0 != axCmdsSched[i].xScheduledTime)&&(axCmdsSched[i].xScheduledTime <= xTimeOffsetSec); i++ );
00230 
00231    NbSchedCmd++; // One more scheduled cmd.
00232 
00233    // Shift the cells if the spot was used.
00234    if(i<CPTIME_MAX_NB_SCHED_CMDS) // Not necessary because of the first test done in this function: added to remove a compiler warning.
00235    {
00236      if( 0!= axCmdsSched[i].xScheduledTime )
00237      {
00238         for(j=NbSchedCmd-1, k=NbSchedCmd-2; k>=i; j--, k--)
00239         {
00240            if( 0 != axCmdsSched[k].xScheduledTime )
00241            {
00242               axCmdsSched[j].xScheduledTime = axCmdsSched[k].xScheduledTime;
00243               axCmdsSched[j].CmdId = axCmdsSched[k].CmdId;
00244               axCmdsSched[j].pfScheduledCmd = axCmdsSched[k].pfScheduledCmd;
00245               axCmdsSched[j].pxParam = axCmdsSched[k].pxParam;
00246            }
00247         }
00248      }
00249 
00250      // Record the scheduled cmd.
00251      axCmdsSched[i].xScheduledTime = xTimeOffsetSec;
00252      axCmdsSched[i].CmdId = CmdId;
00253      axCmdsSched[i].pfScheduledCmd = pfScheduledCmd;
00254      axCmdsSched[i].pxParam = pvCmdParams;
00255    }
00256 
00257    return( SHELL_EXECSTATUS_OK_NO_FREE );
00258 }

static void prv_v_ShiftScheduledCmdsArray ( int  SrcIdx,
int  DstIdx 
) [static]

Shift the Scheduled Cmds array from the SrcIdx index down to the DstIdx index.

Parameters:
SrcIdx Input. The source index.
DstIdx Input. The Destination index.

Definition at line 478 of file cptime.c.

References st_scheduled_cmd::CmdId, CPTIME_MAX_NB_SCHED_CMDS, st_scheduled_cmd::pfScheduledCmd, st_scheduled_cmd::pxParam, and st_scheduled_cmd::xScheduledTime.

Referenced by e_cptime_cmd_set_config(), and v_cptime_ExecuteScheduledCmd().

00479 {
00480 
00481 
00482    if( SrcIdx == DstIdx )
00483       return;
00484    // Shift the array of scheduled commands.
00485    for( ;
00486         (SrcIdx<CPTIME_MAX_NB_SCHED_CMDS)&&(0 != axCmdsSched[SrcIdx].xScheduledTime);
00487         SrcIdx++,DstIdx++ )
00488    {
00489       axCmdsSched[DstIdx].xScheduledTime = axCmdsSched[SrcIdx].xScheduledTime;
00490       axCmdsSched[DstIdx].CmdId = axCmdsSched[SrcIdx].CmdId;
00491       axCmdsSched[DstIdx].pfScheduledCmd = axCmdsSched[SrcIdx].pfScheduledCmd;
00492       axCmdsSched[DstIdx].pxParam = axCmdsSched[SrcIdx].pxParam;
00493       // Mark this cell as unused now that its content has been moved.
00494       axCmdsSched[SrcIdx].xScheduledTime = 0;
00495    }
00496 }

void v_cptime_ExecuteScheduledCmd ( void   ) 

Execute a scheduled command if expiration date is up.

Definition at line 264 of file cptime.c.

References NbSchedCmd, st_scheduled_cmd::pfScheduledCmd, prv_v_ShiftScheduledCmdsArray(), xcptime_LocalTime, and st_scheduled_cmd::xScheduledTime.

Referenced by portTASK_FUNCTION().

00265 {
00266    if( ( 0 != axCmdsSched[0].xScheduledTime )
00267        && ( axCmdsSched[0].xScheduledTime <= xcptime_LocalTime ) )
00268    {
00269       // Execute the command.
00270       axCmdsSched[0].pfScheduledCmd( axCmdsSched[0].CmdId, axCmdsSched[0].pxParam );
00271 
00272       axCmdsSched[0].xScheduledTime = 0; // Mark this cell as unused.
00273       NbSchedCmd--; // One scheduled cmd removed.
00274 
00275       // Shift the array of scheduled commands.
00276       prv_v_ShiftScheduledCmdsArray(1,0);
00277    }
00278 }

void v_cptime_GetDateInFatStringFormat ( char *  pcDate  ) 

Get the current time in the "YYYYMMDDHHMMSSMS" string format.

Parameters:
pcDate Input/Output. Input allocated array. Output is the current time expressed in the "YYYYMMDDHHMMSSMS" string format.

Definition at line 170 of file cptime.c.

References sprintf(), and xcptime_LocalTime.

Referenced by b_mmi_mkdir_aLOG(), b_mmi_mkdir_bLOG(), config_file_set_value(), e_fscmds_shell_mkdir(), e_fscmds_touch(), prv_append(), prv_vsave_logs(), and prv_xopen_current_logfile().

00171 {
00172   struct tm *pxDate;
00173   
00174   // Get the broken-down representation of the current date.
00175   pxDate = gmtime( &xcptime_LocalTime );
00176 
00177   // WARNING: pxDate->tm_year == number of years since 1900.
00178   // For years >= 2000, we'll display the last 2 digits only.
00179   /*       if( pxDate->tm_year >= 100 )
00180             pxDate->tm_year -= 100;*/
00181   if( pxDate->tm_year >= 100 )
00182     pxDate->tm_year = 2000 + ( pxDate->tm_year - 100 );
00183   else
00184     pxDate->tm_year = 1900 + pxDate->tm_year;
00185   sprintf( pcDate, "%.4d%.2d%.2d%.2d%.2d0000", pxDate->tm_year, 
00186            pxDate->tm_mon +1, pxDate->tm_mday, pxDate->tm_hour, pxDate->tm_min );
00187 }

void v_cptime_Init ( void   ) 

Init the time module.

Definition at line 135 of file cptime.c.

References xcptime_LocalTime.

Referenced by main().

00136 {
00137    struct tm xLogDate;
00138 
00139 
00140    // Init. of the Control Panel current time to: April 3rd 2007 00:00:00
00141    xLogDate.tm_mon = 3;    // April
00142    xLogDate.tm_mday = 3;   // April 3rd
00143    xLogDate.tm_year = 107; // 2007
00144    xLogDate.tm_hour = 0;
00145    xLogDate.tm_min = 0;
00146    xLogDate.tm_sec = 0;
00147    xLogDate.tm_isdst = 0; // Daylight saving time info is not in effect.
00148    // Convert time broken-down representation to arithmetic representation.
00149    xcptime_LocalTime = mktime( &xLogDate );
00150 }

void v_cptime_UpdateLocalTime ( void   ) 

Update the local time.

Definition at line 126 of file cptime.c.

References configTICK_RATE_HZ, xcptime_LocalTime, and xcptime_TickOrigin.

00127 {
00128    xcptime_LocalTime = ( xTaskGetTickCount() - xcptime_TickOrigin )/configTICK_RATE_HZ;
00129 }


Variable Documentation

Scheduled_Cmd axCmdsSched[CPTIME_MAX_NB_SCHED_CMDS] [static]

Initial value:

 {
 {0, 0, NULL, NULL},
 {0, 0, NULL, NULL},
 {0, 0, NULL, NULL},
 {0, 0, NULL, NULL},
 {0, 0, NULL, NULL},
 {0, 0, NULL, NULL},
 {0, 0, NULL, NULL},
 {0, 0, NULL, NULL},
 {0, 0, NULL, NULL},
 {0, 0, NULL, NULL} }
Array of scheduled commands.

Definition at line 104 of file cptime.c.

const signed portCHAR* const CPTIME_ERRMSG_SETCFG_INVALIDDATE = (signed portCHAR *)ERROR_CRLF"Invalid date"CRLF

Error msg upon set_sys_config sys=time curtime="date" where date is invalid.

Definition at line 79 of file cptime.c.

Referenced by e_cptime_cmd_set_config().

const signed portCHAR* const CPTIME_ERRMSG_SETCFG_SYNTAXERROR = (signed portCHAR *)ERROR_CRLF"Usage: set_sys_config sys=time field=value"CRLF

Error msg upon set cfg time syntax error.

Definition at line 73 of file cptime.c.

Referenced by e_cptime_cmd_set_config().

const signed portCHAR* const CPTIME_ERRMSG_SETCFG_UNKNOWNFIELD = (signed portCHAR *)ERROR_CRLF"Unknown configuration field"CRLF

Error msg upon set cfg time of an invalid config field.

Definition at line 76 of file cptime.c.

Referenced by e_cptime_cmd_set_config().

const signed portCHAR* const CPTIME_ERRMSG_SETVAL_INVALIDTIME = (signed portCHAR *)ERROR_CRLF"Invalid schedule date"CRLF

Error msg if the time for scheduling is invalid.

Definition at line 70 of file cptime.c.

Referenced by e_cptime_RecordScheduledCmd().

const signed portCHAR* const CPTIME_ERRMSG_SETVAL_UNAVAILSCHEDSLOT = (signed portCHAR *)ERROR_CRLF"No scheduling slot left"CRLF

Error msg if there is no scheduling slot left.

Definition at line 67 of file cptime.c.

Referenced by e_cptime_RecordScheduledCmd().

int NbSchedCmd = 0 [static]

Nb of scheduled commands.

Definition at line 117 of file cptime.c.

Referenced by e_cptime_cmd_set_config(), e_cptime_RecordScheduledCmd(), and v_cptime_ExecuteScheduledCmd().

portTickType xcptime_TickOrigin = 0 [static]

The OS tick origin. This may not be == 0, if the local time is set through a shell command or acquired though the TIME protocol.

Definition at line 87 of file cptime.c.

Referenced by v_cptime_UpdateLocalTime().


Generated on Fri Feb 19 02:22:43 2010 for AVR32 - Control Panel demonstration. by  doxygen 1.5.5