summaryrefslogtreecommitdiffstats
path: root/main/common/date.c
blob: e695908474d2a87155513c9dd7c69b1f89e22ff1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**************************************************************************
 *
 * Copyright (c) 2013 Alcatel-Lucent
 *
 * Alcatel Lucent licenses this file to You under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  A copy of the License is contained the
 * file LICENSE at the top level of this repository.
 * You may also obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 *
 *  date.c:
 *
 *  A command front-end and utilities for time-of-day-clock hardware.
 *  Some of this is taken from the older file dallasdate.c which *should*
 *  be moved to the dev directory (if not already there).
 *  This code expects a few HW-specific functions to be available...
 *
 *  getTimeAndDate(struct todinfo *);
 *  setTimeAndDate(struct todinfo *);
 *
 *  If setTimeAndDate() is passed '0', then it is this code's attempt
 *  to disable the TOD oscillator (to reduce battery drain).
 *
 *
 * Original author:     Ed Sutter (ed.sutter@alcatel-lucent.com)
 *
 */
#include "config.h"
#include "stddefs.h"
#include "genlib.h"
#include "cli.h"
#include "date.h"

/* rangecheck():
    Return 0 if value is outside the range of high and low; else 1.
*/
static int
rangecheck(int value, char *name, int low, int high)
{
    if((value < low) || (value > high)) {
        printf("%s outside valid range of %d - %d\n",
               name,low,high);
        return(0);
    }
    return(1);
}

int
showDate(int center)
{
    int (*pfunc)(char *, ...);
    struct todinfo now;

    getTimeAndDate(&now);

    if(center) {
        pfunc = cprintf;
    } else {
        pfunc = printf;
    }

    pfunc("%02d/%02d/%04d @ %02d:%02d:%02d\n",
          now.month,now.date,now.year,
          now.hour,now.minute,now.second);

    return(0);
}

char *DateHelp[] = {
    "Display (mm/dd/yyyy@hh:mm:ss) or modify time and date.",
    "[{day date month year hour min sec}]",
#if INCLUDE_VERBOSEHELP
    "Where...",
    " day:   1-7 (sun=1)",
    " date:  1-31",
    " month: 1-12",
    " year:  full number (i.e. 2006)",
    " hour:  0-23",
    " min:   0-59",
    " sec:   0-59",
    "Note: 'date off' disables the oscillator (if HW supports this)",
#endif
    0
};

int
DateCmd(int argc,char *argv[])
{
    int     rngchk;
    struct  todinfo now;

    if(argc == 1) {
        showDate(0);
        return(CMD_SUCCESS);
    } else if((argc == 2) && !strcmp(argv[1],"off")) {
        if(setTimeAndDate(0) < 0) {
            printf("Clock can't be disabled\n");
            return(CMD_FAILURE);
        }
        return(CMD_SUCCESS);
    } else if(argc != 8) {
        return(CMD_PARAM_ERROR);
    }

    now.day = (char)atoi(argv[1]);
    now.date = (char)atoi(argv[2]);
    now.month = (char)atoi(argv[3]);
    now.year = atoi(argv[4]);
    now.hour = (char)atoi(argv[5]);
    now.minute = (char)atoi(argv[6]);
    now.second = (char)atoi(argv[7]);

    rngchk = 0;
    rngchk += rangecheck(now.day,"day",1,7);
    rngchk += rangecheck(now.date,"date",1,31);
    rngchk += rangecheck(now.month,"month",1,12);
    rngchk += rangecheck(now.year,"year",0,9999);
    rngchk += rangecheck(now.hour,"hour",0,23);
    rngchk += rangecheck(now.minute,"minute",0,59);
    rngchk += rangecheck(now.second,"second",0,59);

    if(rngchk != 7) {
        return(CMD_PARAM_ERROR);
    }

    setTimeAndDate(&now);
    return(CMD_SUCCESS);
}

int
timeofday(int cmd, void *arg)
{
    int rc;
    struct todinfo *now;

    rc = 0;
    now = (struct todinfo *)arg;

    switch(cmd) {
    case TOD_ON:
    case TOD_OFF:
        break;
    case TOD_SET:
        rc = setTimeAndDate(now);
        break;
    case TOD_GET:
        rc = getTimeAndDate(now);
        break;
    }
    return(rc);
}