1ef320154SBen Tyner #include <libpdbg.h>
2ef320154SBen Tyner 
3ef320154SBen Tyner #include <attn_handler.hpp>
473ac368aSBen Tyner #include <attn_monitor.hpp>
5ef320154SBen Tyner 
6*970fd4fbSBen Tyner #include <algorithm>
7*970fd4fbSBen Tyner 
8*970fd4fbSBen Tyner /*
9*970fd4fbSBen Tyner  * @brief Search the command line arguments for an option
10*970fd4fbSBen Tyner  *
11*970fd4fbSBen Tyner  * @param i_begin   command line args vector begin
12*970fd4fbSBen Tyner  * @param i_end     command line args vector end
13*970fd4fbSBen Tyner  * @param i_option  configuration option to look for
14*970fd4fbSBen Tyner  *
15*970fd4fbSBen Tyner  * @return true = option found on command line
16*970fd4fbSBen Tyner  */
17*970fd4fbSBen Tyner bool getCliOption(char** i_begin, char** i_end, const std::string& i_option)
18*970fd4fbSBen Tyner {
19*970fd4fbSBen Tyner     return (i_end != std::find(i_begin, i_end, i_option));
20*970fd4fbSBen Tyner }
21*970fd4fbSBen Tyner 
22*970fd4fbSBen Tyner /*
23*970fd4fbSBen Tyner  * @brief Search the command line arguments for a setting value
24*970fd4fbSBen Tyner  *
25*970fd4fbSBen Tyner  * @param i_begin   command line args vector begin
26*970fd4fbSBen Tyner  * @param i_end     command line args vectory end
27*970fd4fbSBen Tyner  * @param i_setting configuration setting to look for
28*970fd4fbSBen Tyner  *
29*970fd4fbSBen Tyner  * @return value of the setting or 0 if setting not found or value not given
30*970fd4fbSBen Tyner  */
31*970fd4fbSBen Tyner char* getCliSetting(char** i_begin, char** i_end, const std::string& i_setting)
32*970fd4fbSBen Tyner {
33*970fd4fbSBen Tyner     char** value = std::find(i_begin, i_end, i_setting);
34*970fd4fbSBen Tyner     if (value != i_end && ++value != i_end)
35*970fd4fbSBen Tyner     {
36*970fd4fbSBen Tyner         return *value;
37*970fd4fbSBen Tyner     }
38*970fd4fbSBen Tyner     return 0; // nullptr
39*970fd4fbSBen Tyner }
40*970fd4fbSBen Tyner 
41ef320154SBen Tyner /**
42ef320154SBen Tyner  * @brief Attention handler application main()
43ef320154SBen Tyner  *
44ef320154SBen Tyner  * This is the main interface to the Attention handler application, it will
45ef320154SBen Tyner  * initialize the libgpd targets and start a gpio mointor.
46ef320154SBen Tyner  *
47*970fd4fbSBen Tyner  * Command line arguments:
48*970fd4fbSBen Tyner  *
49*970fd4fbSBen Tyner  *  breakpoints - enables breakpoint special attn handling
50*970fd4fbSBen Tyner  *
51ef320154SBen Tyner  * @return 0 = success
52ef320154SBen Tyner  */
53*970fd4fbSBen Tyner int main(int argc, char* argv[])
54ef320154SBen Tyner {
55ef320154SBen Tyner     int rc = 0; // return code
56ef320154SBen Tyner 
5773ac368aSBen Tyner     gpiod_line* line; // gpio line to monitor
5873ac368aSBen Tyner 
5973ac368aSBen Tyner     boost::asio::io_service io; // async io monitoring service
6073ac368aSBen Tyner 
61ef320154SBen Tyner     // initialize pdbg targets
62ef320154SBen Tyner     pdbg_targets_init(nullptr);
63ef320154SBen Tyner 
6473ac368aSBen Tyner     // GPIO line configuration (falling edge, active low)
6573ac368aSBen Tyner     struct gpiod_line_request_config config
6673ac368aSBen Tyner     {
6773ac368aSBen Tyner         "attention", GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE, 0
6873ac368aSBen Tyner     };
6973ac368aSBen Tyner 
7073ac368aSBen Tyner     // get handle to attention GPIO line
7173ac368aSBen Tyner     line = gpiod_line_get("gpiochip0", 74);
7273ac368aSBen Tyner 
7373ac368aSBen Tyner     if (nullptr == line)
7473ac368aSBen Tyner     {
7573ac368aSBen Tyner         rc = 1; // error
7673ac368aSBen Tyner     }
7773ac368aSBen Tyner     else
7873ac368aSBen Tyner     {
79*970fd4fbSBen Tyner         // Check command line args for breakpoint handling enable option
80*970fd4fbSBen Tyner         bool bp_enable = getCliOption(argv, argv + argc, "-breakpoints");
81*970fd4fbSBen Tyner 
8273ac368aSBen Tyner         // Creating a vector of one gpio to monitor
8373ac368aSBen Tyner         std::vector<std::unique_ptr<attn::AttnMonitor>> gpios;
84*970fd4fbSBen Tyner         gpios.push_back(
85*970fd4fbSBen Tyner             std::make_unique<attn::AttnMonitor>(line, config, io, bp_enable));
8673ac368aSBen Tyner 
8773ac368aSBen Tyner         io.run(); // start GPIO monitor
8873ac368aSBen Tyner     }
8973ac368aSBen Tyner 
90ef320154SBen Tyner     return rc;
91ef320154SBen Tyner }
92