1/* ********************************************************************* 2 * Broadcom Common Firmware Environment (CFE) 3 * 4 * SWARM toy clock commands File: ui_toyclock.c 5 * 6 * time-of-year clock 7 * 8 * Author: Mitch Lichtenberg (mpl@broadcom.com) 9 * 10 ********************************************************************* 11 * 12 * Copyright 2000,2001,2002,2003 13 * Broadcom Corporation. All rights reserved. 14 * 15 * This software is furnished under license and may be used and 16 * copied only in accordance with the following terms and 17 * conditions. Subject to these conditions, you may download, 18 * copy, install, use, modify and distribute modified or unmodified 19 * copies of this software in source and/or binary form. No title 20 * or ownership is transferred hereby. 21 * 22 * 1) Any source code used, modified or distributed must reproduce 23 * and retain this copyright notice and list of conditions 24 * as they appear in the source file. 25 * 26 * 2) No right is granted to use any trade name, trademark, or 27 * logo of Broadcom Corporation. The "Broadcom Corporation" 28 * name may not be used to endorse or promote products derived 29 * from this software without the prior written permission of 30 * Broadcom Corporation. 31 * 32 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR 33 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED 34 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 35 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 36 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 37 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT, 38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 40 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 42 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 43 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 44 * THE POSSIBILITY OF SUCH DAMAGE. 45 ********************************************************************* */ 46 47 48#include "lib_types.h" 49#include "lib_string.h" 50#include "lib_queue.h" 51#include "lib_malloc.h" 52#include "lib_printf.h" 53 54#include "cfe_iocb.h" 55#include "cfe_devfuncs.h" 56#include "cfe_ioctl.h" 57#include "cfe_timer.h" 58#include "cfe_error.h" 59 60#include "ui_command.h" 61#include "cfe.h" 62 63#include "bsp_config.h" 64 65/* ********************************************************************* 66 * prototypes 67 ********************************************************************* */ 68 69int ui_init_toyclockcmds(void); 70 71static int ui_cmd_showtime(ui_cmdline_t *cmd,int argc,char *argv[]); 72static int ui_cmd_settime(ui_cmdline_t *cmd,int argc,char *argv[]); 73static int ui_cmd_setdate(ui_cmdline_t *cmd,int argc,char *argv[]); 74 75/* ********************************************************************* 76 * ui_init_toyclockcmds() 77 * 78 * Add toy clock commands to the command table 79 * 80 * Input parameters: 81 * nothing 82 * 83 * Return value: 84 * 0 85 ********************************************************************* */ 86int ui_init_toyclockcmds(void) 87{ 88 89 cmd_addcmd("show time", 90 ui_cmd_showtime, 91 NULL, 92 "Display current time according to RTC", 93 "show time", 94 ""); 95 96 cmd_addcmd("set time", 97 ui_cmd_settime, 98 NULL, 99 "Set current time", 100 "set time hh:mm:ss", 101 ""); 102 103 cmd_addcmd("set date", 104 ui_cmd_setdate, 105 NULL, 106 "Set current date", 107 "set date mm/dd/yyyy", 108 ""); 109 110 return 0; 111} 112 113/* ********************************************************************* 114 * User interface commands 115 ********************************************************************* */ 116 117static int ui_cmd_settime(ui_cmdline_t *cmd,int argc,char *argv[]) 118{ 119 char *line; 120 char *p; 121 int hr,min,sec; 122 int fd; 123 uint8_t buf[12]; 124 int res=0; 125 126 if ((line = cmd_getarg(cmd,0)) == NULL) { 127 return ui_showusage(cmd); 128 } 129 130 /* convert colons to spaces for the gettoken routine */ 131 while ((p = strchr(line,':'))) *p = ' '; 132 133 /* parse and check command-line args */ 134 hr = -1; min = -1; sec = -1; 135 136 p = gettoken(&line); 137 if (p) hr = atoi(p); 138 p = gettoken(&line); 139 if (p) min = atoi(p); 140 p = gettoken(&line); 141 if (p) sec = atoi(p); 142 143 if ((hr < 0) || (hr > 23) || 144 (min < 0) || (min >= 60) || 145 (sec < 0) || (sec >= 60)) { 146 return ui_showusage(cmd); 147 } 148 149 /* 150 * hour-minute-second-month-day-year1-year2-(time/date flag) 151 * time/date flag (offset 7) is used to let device know what 152 * is being set. 153 */ 154 buf[0] = hr; 155 buf[1] = min; 156 buf[2] = sec; 157 buf[7] = 0x00; /*SET_TIME = 0x00, SET_DATE = 0x01*/ 158 159 fd = cfe_open("clock0"); 160 if (fd < 0) { 161 ui_showerror(fd,"could not open clock device"); 162 return fd; 163 } 164 165 res = cfe_write(fd,buf,8); 166 if (res < 0) { 167 ui_showerror(res,"could not set time"); 168 } 169 cfe_close(fd); 170 return 0; 171} 172 173static int ui_cmd_setdate(ui_cmdline_t *cmd,int argc,char *argv[]) 174{ 175 char *line; 176 char *p; 177 int dt,mo,yr,y2k; 178 int fd; 179 uint8_t buf[12]; 180 int res=0; 181 182 if ((line = cmd_getarg(cmd,0)) == NULL) { 183 return ui_showusage(cmd); 184 } 185 186 /* convert colons to spaces for the gettoken routine */ 187 188 while ((p = strchr(line,'/'))) *p = ' '; 189 190 /* parse and check command-line args */ 191 192 dt = -1; mo = -1; yr = -1; 193 194 p = gettoken(&line); 195 if (p) mo = atoi(p); 196 p = gettoken(&line); 197 if (p) dt = atoi(p); 198 p = gettoken(&line); 199 if (p) yr = atoi(p); 200 201 if ((mo <= 0) || (mo > 12) || 202 (dt <= 0) || (dt > 31) || 203 (yr < 1900) || (yr > 2099)) { 204 return ui_showusage(cmd); 205 } 206 207 y2k = (yr >= 2000) ? 0x20 : 0x19; 208 yr %= 100; 209 210 /* 211 * hour-minute-second-month-day-year1-year2-(time/date flag) 212 * time/date flag (offset 7) is used to let device know what 213 * is being set. 214 */ 215 buf[3] = mo; 216 buf[4] = dt; 217 buf[5] = yr; 218 buf[6] = y2k; 219 buf[7] = 0x01; /*SET_TIME = 0x00, SET_DATE = 0x01*/ 220 221 fd = cfe_open("clock0"); 222 if (fd < 0) { 223 ui_showerror(fd,"could not open clock device"); 224 return fd; 225 } 226 227 res = cfe_write(fd,buf,8); 228 if (res < 0) { 229 ui_showerror(res,"could not set date"); 230 } 231 232 cfe_close(fd); 233 return 0; 234} 235 236 237static int ui_cmd_showtime(ui_cmdline_t *cmd,int argc,char *argv[]) 238{ 239 uint8_t hr,min,sec; 240 uint8_t mo,day,yr,y2k; 241 int res=0; 242 int fd; 243 uint8_t buf[12]; 244 245 fd = cfe_open("clock0"); 246 if (fd < 0) { 247 ui_showerror(fd,"could not open clock device"); 248 return fd; 249 } 250 res = cfe_read(fd,buf,8); 251 if (res < 0) { 252 ui_showerror(res,"could not get time/date"); 253 } 254 cfe_close(fd); 255 256 hr = buf[0]; 257 min = buf[1]; 258 sec = buf[2]; 259 mo = buf[3]; 260 day = buf[4]; 261 yr = buf[5]; 262 y2k = buf[6]; 263 264 printf("Current date & time is: "); 265 printf("%02X/%02X/%02X%02X %02X:%02X:%02X\n",mo,day,y2k,yr,hr,min,sec); 266 267 return 0; 268} 269 270 271 272 273 274 275 276