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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "gate/gatemain.h"

#include "gate/console.hpp"
#include "gate/applications.hpp"
#include "gate/files.hpp"
#include "gate/io/gpiodevices.hpp"

#if defined(GATE_COMPILER_MSVC) && defined(GATE_LEAK_DETECTION) && defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE) && (GATE_ARCH != GATE_ARCH_ARM32) && (GATE_ARCH != GATE_ARCH_ARM64)
#   include <vld.h>
#endif

namespace gate
{
namespace apps
{

    class GATE_API_LOCAL GpioCliApp : public App
    {
    private:
        AtomicInt cancelReceived;

        virtual void onSignal(int appsignal) override
        {
            this->cancelReceived.set(1);
        }

        virtual void onInit() override
        {
            this->cancelReceived.set(0);
        }


        void listDevices()
        {
            /* TODO */
        }

        void listDeviceLines(String const& devicePath)
        {
            io::GPIODevice dev(devicePath);
            io::GPIODevice::LinesInfoMap lines = dev.listLines();
            typedef io::GPIODevice::LinesInfoMap::mapping_t LineInfoMapping;
            GATEXX_ENUMERATE(LineInfoMapping, item, lines.enumerate())
            {
                gate::int64_t const line = item->key();
                gate::io::GPIODevice::LineInfo const& info = item->value();
                Console << "Line: " << line << "\t" << info.Description << (info.IsUsed ? "\t[IN USE]" : "") << strings::NewLine;
            }
        }

        void configureDeviceLine(String const& devicePath, size_t lineAddress, enumint_t configFlags)
        {
            io::GPIODevice dev(devicePath);
            dev.setLineConfig(lineAddress, configFlags);
        }

        void setDeviceLineValue(String const& devicePath, size_t lineAddress, int value)
        {
            io::GPIODevice dev(devicePath);
            dev.writeLine(lineAddress, value);
        }

        void getDeviceLineValue(String const& devicePath, size_t lineAddress)
        {
            io::GPIODevice dev(devicePath);
            gate::int64_t value = dev.readLine(lineAddress);
            Console << value << strings::NewLine;
        }

        static Array<String> tokenize(String const& line)
        {
            ArrayList<String> ret;

            String str(line);

            while (!str.empty())
            {
                String token = str.readToken();
                if (token.empty())
                {
                    break;
                }
                ret.add(token);
            }
            return ret.toArray();
        }

        void runInteractiveMode(String const& devicePath, Stream& stream)
        {
            static StaticString const cmdHelp = "help";
            static StaticString const cmdExit = "exit";
            static StaticString const cmdExit2 = "x";
            static StaticString const cmdList = "list";
            static StaticString const cmdList2 = "l";
            static StaticString const cmdRead = "read";
            static StaticString const cmdRead2 = "r";
            static StaticString const cmdWrite = "write";
            static StaticString const cmdWrite2 = "w";

            io::GPIODevice dev(devicePath);

            bool_t readNextCommand = true;

            while (readNextCommand)
            {
                stream << ">: ";
                String const rawLine = stream.readLine();
                String const line = rawLine.trim();
                if (line.empty())<--- Shadowed declaration<--- Shadowed declaration
                {
                    continue;
                }
                Array<String> const tokens = tokenize(line);

                String const& cmd = tokens[0];
                if ((cmd == cmdHelp))
                {
                    stream
                        << "list" << strings::NewLine << "\tlists all GPIO lines" << strings::NewLine
                        << "read [line-number]" << strings::NewLine << "\t reads the current value from an input GPIO line" << strings::NewLine
                        << "write [line-number] [value]" << strings::NewLine << "\t sets a new value to an output GPIO line" << strings::NewLine
                        << "exit" << strings::NewLine << "\tabort interactive command mode" << strings::NewLine
                        ;
                    readNextCommand = false;
                }
                else if ((cmd == cmdExit) || (cmd == cmdExit2))
                {
                    readNextCommand = false;
                }
                else if ((cmd == cmdList) || (cmd == cmdList2))
                {
                    gate::io::GPIODevice::LinesInfoMap lines = dev.listLines();
                    typedef gate::io::GPIODevice::LinesInfoMap::mapping_t LinesMapping;
                    GATEXX_ENUMERATE(LinesMapping, entry, lines.enumerate())
                    {
                        stream << "Line " << static_cast<gate::int64_t>(entry->key());
                        gate::io::GPIODevice::LineInfo lineInfo = entry->value();
                        if (!lineInfo.Description.empty())
                        {
                            stream << " (" << lineInfo.Description << ")";
                        }
                        if (lineInfo.IsUsed)
                        {
                            stream << " [IN USE]";
                        }
                        stream << strings::NewLine;
                    }
                }
                else if ((cmd == cmdRead) || (cmd == cmdRead2))
                {
                    if (tokens.size() < 2)
                    {
                        stream 
                            << "Missing line parameter" << strings::NewLine
                            << "Usage:   read [line-nr]" << strings::NewLine
                            << "Example: read 12" << strings::NewLine
                            ;
                    }
                    else
                    {
                        String const& paramLine = tokens[1];
                        gate::int64_t line = paramLine.parseInt();
                        stream << "Reading line " << line << " = ";<--- Shadow variable
                        try
                        {
                            gate::intptr_t value = dev.readLine(gate::io::GPIODevice::line_t(line));
                            stream << gate::int64_t(value) << strings::NewLine;
                        }
                        catch(...)
                        {
                            stream << "Failed" << strings::NewLine;
                        }
                    }
                }
                else if ((cmd == cmdWrite) || (cmd == cmdWrite2))
                {
                    if (tokens.size() < 3)
                    {
                        stream 
                            << "Missing line and/or value parameter" << strings::NewLine
                            << "Usage:   write [line-nr] [value]" << strings::NewLine
                            << "Example: write 12 1 " << strings::NewLine
                            ;
                    }
                    else
                    {
                        String const& paramLine = tokens[1];
                        String const& paramValue = tokens[2];
                        gate::int64_t line = paramLine.parseInt();
                        gate::int64_t value = paramValue.parseInt();<--- Shadow variable
                        stream << "Write to line " << line << " value " << value << " ... ";
                        try
                        {
                            dev.writeLine(gate::io::GPIODevice::line_t(line), gate::intptr_t(value));
                            stream << "OK" << strings::NewLine;
                        }
                        catch(...)
                        {
                            stream << "Failed" << strings::NewLine;
                        }
                    }
                }
                else
                {
                    stream << "Unkown command" << strings::NewLine;
                }
            }
        }

    public:

        virtual void run() override
        {
            static AppOptionDef optHelp = AppOptionDef::createSwitch("help", "?", "show_help", "Shows help");
            static AppOptionDef optList = AppOptionDef::createSwitch("list", "L", "list_items", "List device paths or lines of a given device");
            static AppOptionDef optDevice = AppOptionDef::createString("device", "D", "", "device_path", "Path of GPIO device to be opened");
            static AppOptionDef optLine = AppOptionDef::createInt32("line", "A", 0, "line_address", "Address of line to be accessed");
            static AppOptionDef optConfig = AppOptionDef::createInt32("config", "C", -1, "config_flags", "Configuration flags to be applied to GPIO line");
            static AppOptionDef optWrite = AppOptionDef::createInt32("write", "W", -1, "write_value", "Value to be written to GPIO line");
            static AppOptionDef optRead = AppOptionDef::createSwitch("read", "R", "read_value", "Reads the current value from the GPIO line");
            static AppOptionDef optInteractive = AppOptionDef::createSwitch("interactive", "I", "interactive", "Starts interactive command interface");

            static AppOptionDef* options[] =
            {
                &optHelp,
                &optList,
                &optDevice,
                &optLine,
                &optConfig,
                &optWrite,
                &optRead,
                &optInteractive
            };
            static size_t const optionsCount = sizeof(options) / sizeof(options[0]);

            App::parseAppOptions(this->getArgs(), options, optionsCount);

            if (optHelp.getBool())
            {
                App::printAppOptions(options, optionsCount, Console);
                return;
            }

            String const devicePath = optDevice.getString();
            if (optList.getBool())
            {
                if (!devicePath)
                {
                    listDevices();
                }
                else
                {
                    listDeviceLines(devicePath);
                }
                return;
            }

            if (!devicePath)
            {

                ConError
                    << "Missing COM device argument." << strings::NewLine
                    << "Use '--help' to see all available options." << strings::NewLine;
                GATEXX_RAISE_ERROR(results::InvalidArg);
            }

            //if (optInteractive.getBool())
            {
                this->runInteractiveMode(devicePath, Console);
                return;
            }

            int32_t const deviceLineNumber = optLine.getInt32();
            size_t const deviceLine = static_cast<size_t>(deviceLineNumber);

            int32_t const configFlags = optConfig.getInt32();
            if (configFlags != -1)
            {
                configureDeviceLine(devicePath, deviceLine, static_cast<enumint_t>(configFlags));
            }

            int32_t const writeValue = optWrite.getInt32();
            if (writeValue >= 0)
            {
                this->setDeviceLineValue(devicePath, deviceLine, writeValue);
            }

            if (optRead.getBool())
            {
                this->getDeviceLineValue(devicePath, deviceLine);
            }
        }
    };

}   // end of namespace apps
} // end of namespace gate

#include <stdio.h>

int gate_main(char const* program, char const* const* arguments, gate_size_t argcount, gate_uintptr_t apphandle)
{
    gate::apps::GpioCliApp app;
    return gate::App::runApp(app, program, arguments, argcount, apphandle);
}