1 /*
2 * Copyright (c) 2011-2014, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "TestPlatform.h"
32 #include "convert.hpp"
33 #include "Utility.h"
34
35 #include <iostream>
36 #include <string>
37 #include <vector>
38 #include <algorithm>
39
40 using std::cerr;
41 using std::endl;
42 using std::string;
43
44 static const uint16_t defaultPortNumber = 5001;
45
showUsage()46 static void showUsage()
47 {
48 cerr << "test-platform [-h|--help] <file path> [port number, default " << defaultPortNumber
49 << "]" << endl;
50 }
51
showInvalidUsage(const string & error)52 static void showInvalidUsage(const string &error)
53 {
54 cerr << "Invalid arguments: " << error;
55 showUsage();
56 }
57
showHelp()58 static void showHelp()
59 {
60 showUsage();
61 cerr << "<file path> must be a valid Paramter top level config file, "
62 << "often named ParameterFrameworkConfiguration.xml.\n"
63 << "Arguments:" << endl
64 << " -h|--help display this help and exit" << endl;
65 }
66
main(int argc,char * argv[])67 int main(int argc, char *argv[])
68 {
69 using Options = std::list<string>;
70 // argv[0] is the program name, not an option
71 Options options(argv + 1, argv + argc);
72
73 // Handle help option
74 auto helpOpts = {"-h", "--help"};
75 auto match = std::find_first_of(begin(options), end(options), begin(helpOpts), end(helpOpts));
76 if (match != end(options)) {
77 showHelp();
78 return 0;
79 }
80
81 if (options.empty()) {
82 showInvalidUsage("Expected a path to a Parameter Framework config file.");
83 return 1;
84 }
85
86 auto filePath = options.front();
87 options.pop_front();
88
89 // Handle optional port number argument
90 uint16_t portNumber = defaultPortNumber;
91
92 if (not options.empty()) {
93 if (not convertTo(options.front(), portNumber)) {
94 showInvalidUsage("Could not convert \"" + options.front() +
95 "\" to a socket port number.");
96 return 2;
97 };
98 options.pop_front();
99 }
100
101 // All arguments should have been consumed
102 if (not options.empty()) {
103 showInvalidUsage("Unexpected extra arguments: " + utility::asString(options));
104 return 3;
105 }
106
107 string strError;
108 if (!CTestPlatform(filePath, portNumber).run(strError)) {
109
110 cerr << "Test-platform error:" << strError.c_str() << endl;
111 return -1;
112 }
113 return 0;
114 }
115