1 /***************************************************************************
2 * Copyright (C) 2007-2008 by Øyvind Harboe *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 /* this file contains various functionality useful to standalone systems */
28 #include "configuration.h"
33 #include <time_support.h>
35 #include <sys/types.h>
50 #include <netinet/tcp.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
59 #include <netinet/in.h>
61 #include <arpa/inet.h>
71 int handle_rm_command(struct command_context_s
*cmd_ctx
, char *cmd
,
72 char **args
, int argc
)
76 command_print(cmd_ctx
, "rm <filename>");
77 return ERROR_INVALID_ARGUMENTS
;
80 if (unlink(args
[0]) != 0)
82 command_print(cmd_ctx
, "failed: %d", errno
);
89 /* loads a file and returns a pointer to it in memory. The file contains
90 * a 0 byte(sentinel) after len bytes - the length of the file. */
91 int loadFile(const char *fileName
, void **data
, int *len
)
94 pFile
= fopen(fileName
,"rb");
97 LOG_ERROR("Can't open %s\n", fileName
);
100 if (fseek(pFile
, 0, SEEK_END
)!=0)
102 LOG_ERROR("Can't open %s\n", fileName
);
109 LOG_ERROR("Can't open %s\n", fileName
);
114 if (fseek(pFile
, 0, SEEK_SET
)!=0)
116 LOG_ERROR("Can't open %s\n", fileName
);
120 *data
=malloc(*len
+1);
123 LOG_ERROR("Can't open %s\n", fileName
);
128 if (fread(*data
, 1, *len
, pFile
)!=*len
)
132 LOG_ERROR("Can't open %s\n", fileName
);
136 *(((char *)(*data
))+*len
)=0; /* sentinel */
146 int handle_cat_command(struct command_context_s
*cmd_ctx
, char *cmd
,
147 char **args
, int argc
)
151 command_print(cmd_ctx
, "cat <filename>");
152 return ERROR_INVALID_ARGUMENTS
;
155 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
159 int retval
= loadFile(args
[0], &data
, &len
);
160 if (retval
== ERROR_OK
)
162 command_print(cmd_ctx
, "%s", data
);
167 command_print(cmd_ctx
, "%s not found %d", args
[0], retval
);
172 int handle_trunc_command(struct command_context_s
*cmd_ctx
, char *cmd
,
173 char **args
, int argc
)
177 command_print(cmd_ctx
, "trunc <filename>");
178 return ERROR_INVALID_ARGUMENTS
;
181 FILE *config_file
= NULL
;
182 config_file
= fopen(args
[0], "w");
183 if (config_file
!= NULL
)
190 int handle_meminfo_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
193 struct mallinfo info
;
197 command_print(cmd_ctx
, "meminfo");
198 return ERROR_INVALID_ARGUMENTS
;
205 command_print(cmd_ctx
, "Diff: %d", prev
- info
.fordblks
);
207 prev
= info
.fordblks
;
209 command_print(cmd_ctx
, "Available ram: %d", info
.fordblks
);
215 int handle_append_command(struct command_context_s
*cmd_ctx
, char *cmd
,
216 char **args
, int argc
)
220 command_print(cmd_ctx
,
221 "append <filename> [<string1>, [<string2>, ...]]");
222 return ERROR_INVALID_ARGUMENTS
;
225 FILE *config_file
= NULL
;
226 config_file
= fopen(args
[0], "a");
227 if (config_file
!= NULL
)
230 fseek(config_file
, 0, SEEK_END
);
232 for (i
= 1; i
< argc
; i
++)
234 fwrite(args
[i
], strlen(args
[i
]), 1, config_file
);
237 fwrite(" ", 1, 1, config_file
);
240 fwrite("\n", 1, 1, config_file
);
249 int handle_cp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
253 return ERROR_INVALID_ARGUMENTS
;
256 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
260 int retval
= loadFile(args
[0], &data
, &len
);
261 if (retval
!= ERROR_OK
)
264 FILE *f
= fopen(args
[1], "wb");
266 retval
= ERROR_INVALID_ARGUMENTS
;
271 int chunk
= len
- pos
;
272 static const int maxChunk
= 512 * 1024; // ~1/sec
273 if (chunk
> maxChunk
)
278 if ((retval
==ERROR_OK
)&&(fwrite(((char *)data
)+pos
, 1, chunk
, f
)!=chunk
))
279 retval
= ERROR_INVALID_ARGUMENTS
;
281 if (retval
!= ERROR_OK
)
286 command_print(cmd_ctx
, "%d", len
- pos
);
294 if (retval
== ERROR_OK
)
296 command_print(cmd_ctx
, "Copied %s to %s", args
[0], args
[1]);
299 command_print(cmd_ctx
, "Failed: %d", retval
);
307 if (retval
!= ERROR_OK
)
316 #define SHOW_RESULT(a, b) LOG_ERROR(#a " failed %d\n", (int)b)
319 void copyfile(char *name2
, char *name1
)
327 fd1
= open(name1
, O_WRONLY
| O_CREAT
);
329 SHOW_RESULT( open
, fd1
);
331 fd2
= open(name2
, O_RDONLY
);
333 SHOW_RESULT( open
, fd2
);
337 done
= read(fd2
, buf
, IOSIZE
);
340 SHOW_RESULT( read
, done
);
344 if( done
== 0 ) break;
346 wrote
= write(fd1
, buf
, done
);
347 if( wrote
!= done
) SHOW_RESULT( write
, wrote
);
349 if( wrote
!= done
) break;
353 if( err
< 0 ) SHOW_RESULT( close
, err
);
356 if( err
< 0 ) SHOW_RESULT( close
, err
);
360 /* utility fn to copy a directory */
361 void copydir(char *name
, char *destdir
)
366 dirp
= opendir(destdir
);
369 mkdir(destdir
, 0777);
372 err
= closedir(dirp
);
375 dirp
= opendir(name
);
376 if( dirp
== NULL
) SHOW_RESULT( opendir
, -1 );
380 struct dirent
*entry
= readdir(dirp
);
385 if (strcmp(entry
->d_name
, ".") == 0)
387 if (strcmp(entry
->d_name
, "..") == 0)
392 char fullPath
[PATH_MAX
];
393 strncpy(fullPath
, name
, PATH_MAX
);
394 strcat(fullPath
, "/");
395 strncat(fullPath
, entry
->d_name
, PATH_MAX
- strlen(fullPath
));
397 if (stat(fullPath
, &buf
) == -1)
399 LOG_ERROR("unable to read status from %s", fullPath
);
402 isDir
= S_ISDIR(buf
.st_mode
) != 0;
407 // diag_printf("<INFO>: entry %14s",entry->d_name);
408 char fullname
[PATH_MAX
];
409 char fullname2
[PATH_MAX
];
411 strcpy(fullname
, name
);
412 strcat(fullname
, "/");
413 strcat(fullname
, entry
->d_name
);
415 strcpy(fullname2
, destdir
);
416 strcat(fullname2
, "/");
417 strcat(fullname2
, entry
->d_name
);
418 // diag_printf("from %s to %s\n", fullname, fullname2);
419 copyfile(fullname
, fullname2
);
421 // diag_printf("\n");
424 err
= closedir(dirp
);
425 if( err
< 0 ) SHOW_RESULT( stat
, err
);
432 zylinjtag_Jim_Command_rm(Jim_Interp
*interp
,
434 Jim_Obj
* const *argv
)
439 Jim_WrongNumArgs(interp
, 1, argv
, "rm ?dirorfile?");
444 if (unlink(Jim_GetString(argv
[1], NULL
)) == 0)
446 if (rmdir(Jim_GetString(argv
[1], NULL
)) == 0)
449 return del
? JIM_OK
: JIM_ERR
;
454 zylinjtag_Jim_Command_ls(Jim_Interp
*interp
,
456 Jim_Obj
* const *argv
)
460 Jim_WrongNumArgs(interp
, 1, argv
, "ls ?dir?");
464 char *name
= (char*) Jim_GetString(argv
[1], NULL
);
467 dirp
= opendir(name
);
472 Jim_Obj
*objPtr
= Jim_NewListObj(interp
, NULL
, 0);
476 struct dirent
*entry
= NULL
;
477 entry
= readdir(dirp
);
481 if ((strcmp(".", entry
->d_name
)==0)||(strcmp("..", entry
->d_name
)==0))
484 Jim_ListAppendElement(interp
, objPtr
, Jim_NewStringObj(interp
, entry
->d_name
, strlen(entry
->d_name
)));
488 Jim_SetResult(interp
, objPtr
);
493 int handle_peek_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
497 return ERROR_COMMAND_SYNTAX_ERROR
;
499 volatile int *address
=(volatile int *)strtoul(args
[0], NULL
, 0);
501 command_print(cmd_ctx
, "0x%x : 0x%x", address
, value
);
505 int handle_poke_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
509 return ERROR_INVALID_ARGUMENTS
;
511 volatile int *address
=(volatile int *)strtoul(args
[0], NULL
, 0);
512 int value
=strtoul(args
[1], NULL
, 0);
518 zylinjtag_Jim_Command_peek(Jim_Interp
*interp
,
520 Jim_Obj
* const *argv
)
524 Jim_WrongNumArgs(interp
, 1, argv
, "peek ?address?");
529 if (Jim_GetLong(interp
, argv
[1], &address
) != JIM_OK
)
532 int value
= *((volatile int *) address
);
534 Jim_SetResult(interp
, Jim_NewIntObj(interp
, value
));
540 zylinjtag_Jim_Command_poke(Jim_Interp
*interp
,
542 Jim_Obj
* const *argv
)
546 Jim_WrongNumArgs(interp
, 1, argv
, "poke ?address? ?value?");
551 if (Jim_GetLong(interp
, argv
[1], &address
) != JIM_OK
)
554 if (Jim_GetLong(interp
, argv
[2], &value
) != JIM_OK
)
557 *((volatile int *) address
) = value
;
563 /* not so pretty code to fish out ip number*/
564 static int zylinjtag_Jim_Command_ip(Jim_Interp
*interp
, int argc
,
565 Jim_Obj
* const *argv
)
567 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
569 struct ifaddrs
*ifa
= NULL
, *ifp
= NULL
;
571 if (getifaddrs(&ifp
) < 0)
576 for (ifa
= ifp
; ifa
; ifa
= ifa
->ifa_next
)
581 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
582 salen
= sizeof(struct sockaddr_in
);
583 else if (ifa
->ifa_addr
->sa_family
== AF_INET6
)
584 salen
= sizeof(struct sockaddr_in6
);
588 if (getnameinfo(ifa
->ifa_addr
, salen
, ip
, sizeof(ip
), NULL
, 0,
594 Jim_AppendString(interp
, tclOutput
, ip
, strlen(ip
));
601 Jim_SetResult(interp
, tclOutput
);
607 /* not so pretty code to fish out eth0 mac address */
608 static int zylinjtag_Jim_Command_mac(Jim_Interp
*interp
, int argc
,
609 Jim_Obj
* const *argv
)
613 struct ifreq
*ifr
, *ifend
;
619 SockFD
= socket(AF_INET
, SOCK_DGRAM
, 0);
625 ifc
.ifc_len
= sizeof(ifs
);
627 if (ioctl(SockFD
, SIOCGIFCONF
, &ifc
) < 0)
633 ifend
= ifs
+ (ifc
.ifc_len
/ sizeof(struct ifreq
));
634 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ifr
++)
636 //if (ifr->ifr_addr.sa_family == AF_INET)
638 if (strcmp("eth0", ifr
->ifr_name
)!=0)
640 strncpy(ifreq
.ifr_name
, ifr
->ifr_name
, sizeof(ifreq
.ifr_name
));
641 if (ioctl(SockFD
, SIOCGIFHWADDR
, &ifreq
) < 0)
650 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
653 sprintf(buffer
, "%02x-%02x-%02x-%02x-%02x-%02x",
654 ifreq
.ifr_hwaddr
.sa_data
[0]&0xff,
655 ifreq
.ifr_hwaddr
.sa_data
[1]&0xff,
656 ifreq
.ifr_hwaddr
.sa_data
[2]&0xff,
657 ifreq
.ifr_hwaddr
.sa_data
[3]&0xff,
658 ifreq
.ifr_hwaddr
.sa_data
[4]&0xff,
659 ifreq
.ifr_hwaddr
.sa_data
[5]&0xff);
661 Jim_AppendString(interp
, tclOutput
, buffer
, strlen(buffer
));
663 Jim_SetResult(interp
, tclOutput
);
676 int ioutil_init(struct command_context_s
*cmd_ctx
)
678 register_command(cmd_ctx
, NULL
, "rm", handle_rm_command
, COMMAND_ANY
,
681 register_command(cmd_ctx
, NULL
, "cat", handle_cat_command
, COMMAND_ANY
,
682 "display file content");
684 register_command(cmd_ctx
, NULL
, "trunc", handle_trunc_command
, COMMAND_ANY
,
685 "truncate a file to 0 size");
687 register_command(cmd_ctx
, NULL
, "cp", handle_cp_command
,
688 COMMAND_ANY
, "copy a file <from> <to>");
690 register_command(cmd_ctx
, NULL
, "append_file", handle_append_command
,
691 COMMAND_ANY
, "append a variable number of strings to a file");
693 register_command(cmd_ctx
, NULL
, "meminfo", handle_meminfo_command
,
694 COMMAND_ANY
, "display available ram memory");
696 Jim_CreateCommand(interp
, "rm", zylinjtag_Jim_Command_rm
, NULL
, NULL
);
698 Jim_CreateCommand(interp
, "peek", zylinjtag_Jim_Command_peek
, NULL
, NULL
);
699 Jim_CreateCommand(interp
, "poke", zylinjtag_Jim_Command_poke
, NULL
, NULL
);
700 Jim_CreateCommand(interp
, "ls", zylinjtag_Jim_Command_ls
, NULL
, NULL
);
702 Jim_CreateCommand(interp
, "mac", zylinjtag_Jim_Command_mac
,
705 Jim_CreateCommand(interp
, "ip", zylinjtag_Jim_Command_ip
,