summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/ui/ui_toyclock.c
blob: 7fdcf492c3891cd813b89921b61509ad6bce59ee (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  SWARM toy clock commands			File: ui_toyclock.c
    *  
    *  time-of-year clock
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#include "lib_types.h"
#include "lib_string.h"
#include "lib_queue.h"
#include "lib_malloc.h"
#include "lib_printf.h"

#include "cfe_iocb.h"
#include "cfe_devfuncs.h"
#include "cfe_ioctl.h"
#include "cfe_timer.h"
#include "cfe_error.h"

#include "ui_command.h"
#include "cfe.h"

#include "bsp_config.h"

/*  *********************************************************************
    *  prototypes
    ********************************************************************* */

int ui_init_toyclockcmds(void);

static int ui_cmd_showtime(ui_cmdline_t *cmd,int argc,char *argv[]);
static int ui_cmd_settime(ui_cmdline_t *cmd,int argc,char *argv[]);
static int ui_cmd_setdate(ui_cmdline_t *cmd,int argc,char *argv[]);

/*  *********************************************************************
    *  ui_init_toyclockcmds()
    *  
    *  Add toy clock commands to the command table
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   0
    ********************************************************************* */
int ui_init_toyclockcmds(void)
{

    cmd_addcmd("show time",
	       ui_cmd_showtime,
	       NULL,
	       "Display current time according to RTC",
	       "show time",
	       "");

    cmd_addcmd("set time",
	       ui_cmd_settime,
	       NULL,
	       "Set current time",
	       "set time hh:mm:ss",
	       "");

    cmd_addcmd("set date",
	       ui_cmd_setdate,
	       NULL,
	       "Set current date",
	       "set date mm/dd/yyyy",
	       "");

    return 0;
}

/*  *********************************************************************
    *  User interface commands
    ********************************************************************* */

static int ui_cmd_settime(ui_cmdline_t *cmd,int argc,char *argv[])
{
    char *line;
    char *p;
    int hr,min,sec;
    int fd;
    uint8_t buf[12];
    int res=0;

    if ((line = cmd_getarg(cmd,0)) == NULL) {
	return ui_showusage(cmd);
	}

    /* convert colons to spaces for the gettoken routine */
    while ((p = strchr(line,':'))) *p = ' ';

    /* parse and check command-line args */
    hr = -1; min = -1; sec = -1;

    p = gettoken(&line);
    if (p) hr = atoi(p);
    p = gettoken(&line);
    if (p) min = atoi(p);
    p = gettoken(&line);
    if (p) sec = atoi(p);

    if ((hr < 0) || (hr > 23) ||
	(min < 0) || (min >= 60) ||
	(sec < 0) || (sec >= 60)) {
	return ui_showusage(cmd);
	}

    /*
     * hour-minute-second-month-day-year1-year2-(time/date flag)
     * time/date flag (offset 7) is used to let device know what
     * is being set.
     */
    buf[0] = hr;
    buf[1] = min;
    buf[2] = sec;
    buf[7] = 0x00;	/*SET_TIME = 0x00, SET_DATE = 0x01*/

    fd = cfe_open("clock0");
    if (fd < 0) {
	ui_showerror(fd,"could not open clock device");
	return fd;
	}
    
    res = cfe_write(fd,buf,8);
    if (res < 0) {
	ui_showerror(res,"could not set time");
	}
    cfe_close(fd);
    return 0;
}

static int ui_cmd_setdate(ui_cmdline_t *cmd,int argc,char *argv[])
{
    char *line;
    char *p;
    int dt,mo,yr,y2k;
    int fd;
    uint8_t buf[12];
    int res=0;

    if ((line = cmd_getarg(cmd,0)) == NULL) {
	return ui_showusage(cmd);
	}

    /* convert colons to spaces for the gettoken routine */

    while ((p = strchr(line,'/'))) *p = ' ';

    /* parse and check command-line args */

    dt = -1; mo = -1; yr = -1;

    p = gettoken(&line);
    if (p) mo = atoi(p);
    p = gettoken(&line);
    if (p) dt = atoi(p);
    p = gettoken(&line);
    if (p) yr = atoi(p);

    if ((mo <= 0) || (mo > 12) ||
	(dt <= 0) || (dt > 31) ||
	(yr < 1900) || (yr > 2099)) {
	return ui_showusage(cmd);
	}

    y2k = (yr >= 2000) ? 0x20 : 0x19;
    yr %= 100;

    /*
     * hour-minute-second-month-day-year1-year2-(time/date flag)
     * time/date flag (offset 7) is used to let device know what
     * is being set.
     */
    buf[3] = mo;
    buf[4] = dt;
    buf[5] = yr;
    buf[6] = y2k;
    buf[7] = 0x01; 	/*SET_TIME = 0x00, SET_DATE = 0x01*/

    fd = cfe_open("clock0");
    if (fd < 0) {
	ui_showerror(fd,"could not open clock device");
	return fd;
	}

    res = cfe_write(fd,buf,8);
    if (res < 0) {
	ui_showerror(res,"could not set date");
	}

    cfe_close(fd);
    return 0;
}


static int ui_cmd_showtime(ui_cmdline_t *cmd,int argc,char *argv[])
{
    uint8_t hr,min,sec;
    uint8_t mo,day,yr,y2k;
    int res=0;
    int fd;
    uint8_t buf[12];
    
    fd = cfe_open("clock0");
    if (fd < 0) {
	ui_showerror(fd,"could not open clock device");
	return fd;
	}
    res = cfe_read(fd,buf,8);
    if (res < 0) {
	ui_showerror(res,"could not get time/date");
	}
    cfe_close(fd);

    hr = buf[0];
    min = buf[1];
    sec = buf[2];
    mo = buf[3];
    day = buf[4];
    yr = buf[5];
    y2k = buf[6];
    
    printf("Current date & time is: ");
    printf("%02X/%02X/%02X%02X  %02X:%02X:%02X\n",mo,day,y2k,yr,hr,min,sec);

    return 0;
}