OpenCores
URL https://opencores.org/ocsvn/1g_ethernet_dpi/1g_ethernet_dpi/trunk

Subversion Repositories 1g_ethernet_dpi

[/] [1g_ethernet_dpi/] [trunk/] [sw/] [app/] [src/] [ethlc.cpp] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 kuzmi4
#include "ethlc.hpp"
2
 
3
/**
4
 * @brief   private: Execute Generic Shell Command
5
 *
6
 * @param[in]   command Command to execute.
7
 * @param[out]  output  Shell output.
8
 * @param[in]   mode read/write access
9
 *
10
 * @return 0 for success, 1 otherwise.
11
 *
12
*/
13
bool ethlc::exec_shell_cmd( const std::string&  command,
14
                            std::string&        output,
15
                            const std::string&  mode = "r")
16
{
17
    // Create the stringstream
18
    std::stringstream sout;
19
    // Run Popen
20
    FILE *in;
21
    char buff[512];
22
    // Test output
23
    if(!(in = popen(command.c_str(), mode.c_str()))){
24
        return 1;
25
    }
26
    // Parse output
27
    while(fgets(buff, sizeof(buff), in)!=NULL){
28
        sout << buff;
29
    }
30
    // Close
31
    int exit_code = pclose(in);
32
    // set output
33
    output = sout.str();
34
    // Return exit code
35
    return exit_code;
36
}
37
 
38
 
39
 
40
/**
41
 * @brief   public: Open-dev / class-constr /
42
 *
43
 * @param[in] address   dev IPv4-addr
44
 *
45
 * @return 0 for success, 1 otherwise.
46
 *
47
*/
48
ethlc::ethlc(const std::string& addr)
49
{
50
    // dec vars
51
    int code_sim, code_hw;
52
    std::string command;
53
    std::string details_sim, details_hw;
54
 
55
    // record addr
56
    address = addr;
57
 
58
    // ARPING it / sim
59
    command = "arping -c 1 " + address + " -I tap0"+ " 2>&1"; // !!!redirecting stderr to stdout
60
    code_sim = exec_shell_cmd(command, details_sim);
61
 
62
    // ARPING it / hw
63
    command = "arping -c 1 " + address + " -I eth0"+ " 2>&1"; // ..
64
    code_hw = exec_shell_cmd(command, details_hw);
65
 
66
    // final deal
67
    if ((code_hw != 0) && (code_sim != 0)) { // no device found
68
        std::cout << "no device found - 'ethlc::ethlc'" << std::endl;
69
        throw 7;
70
    } else {
71
        // if we are here - OK
72
        if (code_hw == -1) {
73
            details = details_sim;
74
        } else {
75
            details = details_hw;
76
        }
77
    }
78
}
79
 
80
/**
81
 * @brief   public: Close-dev / class-destr /
82
 *
83
 * @return 0 for success, 1 otherwise.
84
 *
85
*/
86
ethlc::~ethlc()
87
{
88
    // placeholder
89
}
90
 
91
 
92
/**
93
 * @brief   public: Work with dev
94
 *
95
 * @return 0 for success, 1 otherwise.
96
 *
97
*/
98
bool ethlc::ethlc_proc(void)
99
{
100
    // just PING it / ;)
101
    std::string command = "ping -c 1 " + address + " 2>&1"; // !!!redirecting stderr to stdout
102
    int code = exec_shell_cmd(command, details);
103
    return (code == 0);
104
}
105
 
106
 
107
/**
108
 * @brief   public: Out2usr detailes of shell output
109
 *
110
 * @param[out]  output  Shell output.
111
 *
112
*/
113
void ethlc::ethlc_gdet(std::string& pdetails)
114
{
115
    pdetails = details;
116
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.