- changed use of bzero (deprecated) to memset (thanks to Spen for pointing this out)
[fw/openocd] / src / openocd.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #define OPENOCD_VERSION "Open On-Chip Debugger (2006-06-25 22:45 CEST)"
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "log.h"
28 #include "types.h"
29 #include "jtag.h"
30 #include "configuration.h"
31 #include "interpreter.h"
32 #include "xsvf.h"
33 #include "target.h"
34 #include "flash.h"
35
36 #include "command.h"
37 #include "server.h"
38 #include "telnet_server.h"
39 #include "gdb_server.h"
40
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/poll.h>
45 #include <strings.h>
46 #include <netinet/in.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <signal.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <errno.h>
53
54 /* Give TELNET a way to find out what version this is */
55 int handle_version_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
56 {
57         command_print(cmd_ctx, OPENOCD_VERSION);
58
59         return ERROR_OK;
60 }
61
62 int main(int argc, char *argv[])
63 {
64         /* initialize commandline interface */
65         command_context_t *cmd_ctx, *cfg_cmd_ctx;
66         cmd_ctx = command_init();
67
68         register_command(cmd_ctx, NULL, "version", handle_version_command,
69                                          COMMAND_EXEC, "show OpenOCD version");
70         
71         /* register subsystem commands */
72         server_register_commands(cmd_ctx);
73         telnet_register_commands(cmd_ctx);
74         gdb_register_commands(cmd_ctx);
75         log_register_commands(cmd_ctx);
76         jtag_register_commands(cmd_ctx);
77         interpreter_register_commands(cmd_ctx);
78         xsvf_register_commands(cmd_ctx);
79         target_register_commands(cmd_ctx);
80         flash_register_commands(cmd_ctx);
81         
82         if (log_init(cmd_ctx) != ERROR_OK)
83                 return EXIT_FAILURE;
84         DEBUG("log init complete");
85         
86         INFO( OPENOCD_VERSION );
87
88         cfg_cmd_ctx = copy_command_context(cmd_ctx);
89         cfg_cmd_ctx->mode = COMMAND_CONFIG;
90         command_set_output_handler(cfg_cmd_ctx, configuration_output_handler, NULL);
91         
92         if (parse_cmdline_args(cfg_cmd_ctx, argc, argv) != ERROR_OK)
93                 return EXIT_FAILURE;
94
95         if (parse_config_file(cfg_cmd_ctx) != ERROR_OK)
96                 return EXIT_FAILURE;
97         
98         command_done(cfg_cmd_ctx);
99
100         if (jtag_init(cmd_ctx) != ERROR_OK)
101                 return EXIT_FAILURE;
102         DEBUG("jtag init complete");
103
104         if (target_init(cmd_ctx) != ERROR_OK)
105                 return EXIT_FAILURE;
106         DEBUG("target init complete");
107
108         if (flash_init(cmd_ctx) != ERROR_OK)
109                 return EXIT_FAILURE;
110         DEBUG("flash init complete");
111
112         /* initialize tcp server */
113         server_init();
114         
115         /* initialize telnet subsystem */
116         telnet_init("Open On-Chip Debugger");
117         gdb_init();
118
119         /* handle network connections */
120         server_loop(cmd_ctx);
121         
122         /* free commandline interface */
123         command_done(cmd_ctx);
124         
125         return EXIT_SUCCESS;
126 }