File: main.cpp

package info (click to toggle)
libfreesrp 0.3.0-5
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 536 kB
  • sloc: cpp: 3,755; makefile: 4
file content (281 lines) | stat: -rw-r--r-- 7,689 bytes parent folder | download
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
276
277
278
279
280
281
/*
 * Copyright 2017 by Lukas Lao Beyer <lukas@electronics.kitchen>
 *
 * This file is part of libfreesrp.
 *
 * libfreesrp is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * libfreesrp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with libfreesrp.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <iostream>
#include <boost/tokenizer.hpp>

#include <freesrp.hpp>

#include "cmds.hpp"

#include "optionparser.hpp"

using namespace std;
using namespace FreeSRP;

bool process_command(const FreeSRP::FreeSRP &srp)
{
    bool exit = false;

    cout << "FreeSRP CTL> ";

    string input;
    getline(cin, input);

    boost::char_separator<char> sep(" ");
    boost::tokenizer<boost::char_separator<char>> tokens(input, sep);

    string cmd_id;
    vector<string> params;

    int i = 0;
    for(const string &token : tokens)
    {
        if(i == 0)
        {
            cmd_id = token;
        }
        else
        {
            params.push_back(token);
        }
        i++;
    }

    bool cmd_found = false;
    for(const cmds::cmd_def &cmd : cmds::cmds)
    {
        if(cmd.cmd == cmd_id)
        {
            cmd_found = true;
            if(cmd.exit)
            {
                exit = true;
                break;
            }

            if(cmd.func != nullptr)
            {
                try
                {
                    cmd.func(srp, params);
                }
                catch(ConnectionError e)
                {
                    cerr << "Error sending command to FreeSRP, " << e.what() << endl;
                    exit = true;
                }
            }
        }
    }

    if(!cmd_found)
    {
        cout << "Command '" << cmd_id << "' not found. Type 'help' for a list of available commands." << endl;
    }

    return !exit;
}

enum optionIndex {NONE, HELP, INTERACTIVE, FPGA, FX3, LIST};
const option::Descriptor usage[] = {
    {NONE,        0, "",  "",       option::Arg::None,      "usage: freesrp-ctl [options] [id]\noptions:"},
    {HELP,        0, "",  "help",   option::Arg::None,      "  --help                           Print usage and exit"},
    {LIST,        0, "l", "list",   option::Arg::None,      "  --list, -l                       List serial numbers of connected FreeSRPs"},
    {INTERACTIVE, 0, "i", "interactive", option::Arg::None, "  --interactive, -i                Run in interactive mode"},
    {FPGA,        0, "",  "fpga",   option::Arg::Optional,  "  --fpga=/path/to/bitstream.bin    Load the FPGA with the specified bitstream"},
    {FX3,         0, "",  "fx3",    option::Arg::Optional,  "  --fx3=/path/to/firmware.img      Upload firmware to a Cypress EZ-USB FX3"},
    {NONE,        0, "",  "",       option::Arg::None,      "id:\n  (optional) the serial number of the device to connect to."},
    {0,0,0,0,0,0}
};

void list_devices()
{
    vector<string> connected_devs = FreeSRP::FreeSRP::list_connected();

    if(connected_devs.size() == 0)
    {
	cout << "No FreeSRP found" << endl;
	return;
    }

    cout << "FreeSRP devices detected:" << endl;
    
    for(string &serial : connected_devs)
    {
	cout << "   * " << serial << endl;
    }
}

void check_fx3()
{
    // Check for FX3
    if(Util::find_fx3())
    {
        cout << "NOTE: Found a Cypress EZ-USB FX3 device. This could be a FreeSRP in bootloader mode.\n"
                "You can upload the FreeSRP firmware to it by running 'freesrp-ctl --fx3=/path/to/firmware.img'" << endl;
    }
}

int main(int argc, char *argv[])
{
    // Parse options
    argc-=(argc>0); argv+=(argc>0); // Skip program name (argv[0]), if present
    option::Stats stats(usage, argc, argv);
    option::Option options[stats.options_max], buffer[stats.buffer_max];
    option::Parser parse(usage, argc, argv, options, buffer);

    if(parse.error())
    {
        return 1;
    }

    std::string serial = "";
    
    if(parse.nonOptionsCount() > 0)
    {
        serial = parse.nonOption(0);
    }

    if(options[HELP])
    {
        option::printUsage(cout, usage);
        return 0;
    }

    if(options[LIST])
    {
	list_devices();
	check_fx3();
	return 0;
    }

    std::string fpgaconfig_filename = "";

    if(options[FPGA])
    {
        if(options[FPGA].arg)
        {
            fpgaconfig_filename = options[FPGA].arg;
        }
        else
        {
            cerr << "Error: --fpga option expects a filename! See 'freesrp-ctl --help'." << endl;
            return 1;
        }
    }

    bool interactive = false;
    if(options[INTERACTIVE])
    {
        interactive = true;
    }

    std::string fx3_firmware = "";

    if(options[FX3])
    {
        if(options[FX3].arg)
        {
            fx3_firmware = options[FX3].arg;
        }
        else
        {
            cerr << "Error: --fx3 option expects a filename! See 'freesrp-ctl --help'." << endl;
        }
    }

    if(fx3_firmware.length() > 0)
    {
        // Upload firmware to FX3.
        try
        {
            if(Util::find_fx3(true, fx3_firmware))
            {
                // Firmware upload succeeded, continue
                cout << "Successfully uploaded FreeSRP firmware to FX3" << endl;
                this_thread::sleep_for(chrono::milliseconds(600));
            }
            else
            {
                cerr << "Firmware upload to FX3 failed!" << endl;
                return 1;
            }
        }
        catch(const runtime_error &e)
        {
            cerr << "Error while uploading firmware to FX3! " << e.what() << endl;
            return 1;
        }
    }

    // Connect to FreeSRP and start interactive mode if requested
    try
    {
        FreeSRP::FreeSRP srp(serial);
        cout << "Found FreeSRP" << endl;

        // Configure FPGA if bitstream specified
        if(fpgaconfig_filename.length() > 0)
        {
            cout << "Loading FPGA with '" << fpgaconfig_filename << "'" << endl;
            switch(srp.load_fpga(fpgaconfig_filename))
            {
                case FPGA_CONFIG_DONE:
                    cout << "FPGA configured successfully" << endl;
                    break;
                case FPGA_CONFIG_ERROR:
                    cout << "Error configuring FPGA!" << endl;
                    break;
                case FPGA_CONFIG_SKIPPED:
                    cout << "FPGA already configured. To re-configure, please restart the FreeSRP." << endl;
                    break;
            }
        }

        // Check FPGA status
        if(!srp.fpga_loaded())
        {
            cout << "FPGA not configured. Please configure the FPGA first: " << endl;
            cout << "Example: freesrp-ctl --fpga=/path/to/bitstream.bin" << endl;
            return 1;
        }

        cout << "Connected to FreeSRP" << endl;
        cout << "Version: " << srp.version() << endl;

        if(interactive)
        {
            cout << "Type 'help' for a list of valid commands." << endl;
            while(process_command(srp)) {}
        }
    }
    catch(const ConnectionError &e)
    {
        cerr << "Could not connect to FreeSRP: " << e.what() << endl;
    }
    catch(const runtime_error &e)
    {
        cerr << "Unexpected exception occurred! " << e.what() << endl;
    }

    check_fx3();
    
    return 0;
}