packaging: add AMB configuration that enables the plugin
[profile/ivi/ico-vic-amb-plugin.git] / tests / vicseq.cc
1 #include <unistd.h>
2 #include <sys/time.h>
3
4 #include <algorithm>
5 #include <cstdio>
6 #include <fstream>
7 #include <vector>
8
9 #include "ambconfig.h"
10 #include "controlwebsocketclient.h"
11 #include "datamessage.h"
12 #include "eventmessage.h"
13 #include "logresult.h"
14
15 std::vector<VehicleInfoDefine> vehicleinfolist;
16 int maxstandarddatacount = 1;
17 int maxcustomdatacount = 0;
18 int interspace = 100;
19 int testdatanum = maxstandarddatacount + maxcustomdatacount;
20
21 struct TestData {
22     std::string vicproperty;
23     std::string dbusproperty;
24     DataType type;
25     bool isstd;
26 };
27
28 template <typename T>
29 int SetDataOpt(DataOpt *opt, int idx, T value) {
30     memcpy(opt->status + idx, &value, sizeof(value));
31     return sizeof(value);
32 }
33
34 int main(int argc, char *argv[]) {
35     int ret;
36     std::string configpath = "/etc/ambd/AMBformat.conf";
37     std::string retstr;
38     while ((ret = getopt(argc, argv, "c:i:n:s:")) != -1) {
39         switch (ret) {
40         case 'c':
41             configpath = std::string(optarg);
42             break;
43         case 'i':
44             retstr = std::string(optarg);
45             interspace = std::stoi(retstr);
46             break;
47         case 'n':
48             retstr = std::string(optarg);
49             maxcustomdatacount = std::stoi(retstr);
50             testdatanum = maxstandarddatacount + maxcustomdatacount;
51             break;
52         case 's':
53             retstr = std::string(optarg);
54             maxstandarddatacount = std::stoi(retstr);
55             testdatanum = maxstandarddatacount + maxcustomdatacount;
56             break;
57         default :
58             break;
59         }
60     }
61
62     ifstream fin("standardvehicleinfo.txt");
63     std::string str;
64     std::vector<std::string> standardvehicleinfolist;
65     while (!fin.eof()) {
66         fin >> str;
67         standardvehicleinfolist.push_back(str);
68     }
69
70     AMBConfig config;
71     if (!config.readConfig(configpath)) {
72         std::cerr << "Can't read VIC-Plugin config file." << std::endl;
73         return -1;
74     }
75     PortInfo portinfo = config.getPort();
76
77     ControlWebsocketClient stddataws, customdataws;
78     if (!stddataws.initialize("ws://127.0.0.1", portinfo.standard.dataPort, "standarddatamessage-only")) {
79         std::cerr << "Can't connect standarddatamessage-only." << std::endl;
80         return -1;
81     }
82     if (!customdataws.initialize("ws://127.0.0.1", portinfo.custom.dataPort, "customdatamessage-only")) {
83         std::cerr << "Can't connect customdatamessage-only." << std::endl;
84         return -1;
85     }
86
87     vehicleinfolist =config.getVehicleInfoConfig();
88     auto itr_end = vehicleinfolist.end();
89     int idx = 0;
90     struct timeval tv;
91     LogResult logger;
92     enum ClientType cltype = CLIENT_VIC;
93     enum CommandType cotype = CMDTYPE_SET;
94     DataMessage datamsg;
95     struct TestData testdata[testdatanum];
96     int datacount = 0;
97     int standarddatacount = 0;
98     int customdatacount = 0;
99
100     for (int i = 0; i < 10; i++) {
101         stddataws.service();
102         customdataws.service();
103         usleep(50 * 1000);
104     }
105
106     DataOpt dopt;
107     for (auto itr = vehicleinfolist.begin(); itr != itr_end && datacount < testdatanum; itr++) {
108         if ((*itr).status.size() > 1) {
109             continue;
110         }
111         auto itr2_end = (*itr).status.end();
112         if (std::find(standardvehicleinfolist.begin(), standardvehicleinfolist.end(), std::string((*itr).KeyEventType)) != standardvehicleinfolist.end()) {
113             if (standarddatacount >= maxstandarddatacount) {
114                 continue;
115             }
116             testdata[datacount].vicproperty = std::string((*itr).KeyEventType);
117             testdata[datacount].isstd = true;
118             for (auto itr2 = (*itr).status.begin(); itr2 != itr2_end; itr2++) {
119                 testdata[datacount].dbusproperty = std::string((*itr2).dbusPropertyName);
120                 testdata[datacount].type = (*itr2).type;
121             }
122             datacount++;
123             standarddatacount++;
124         }
125         else {
126             if (customdatacount >= maxcustomdatacount) {
127                 continue;
128             }
129             testdata[datacount].vicproperty = std::string((*itr).KeyEventType);
130             testdata[datacount].isstd = false;
131             for (auto itr2 = (*itr).status.begin(); itr2 != itr2_end; itr2++) {
132                 testdata[datacount].dbusproperty = std::string((*itr2).dbusPropertyName);
133                 testdata[datacount].type = (*itr2).type;
134             }
135             datacount++;
136             customdatacount++;
137         }
138     }
139     
140     struct timeval starttv, endtv;
141     for (int i = 0; i < 100; i++) {
142         gettimeofday(&starttv, NULL);
143         for (int j = 0; j < testdatanum; j++) {
144             switch (testdata[j].type) {
145             case INT : {
146                 static int i = 0;
147                 idx = SetDataOpt(&dopt, 0, i);
148                 logger.PutOut(cltype, cotype, testdata[j].dbusproperty, i++);
149                 break;
150             }
151             case DOUBLE : {
152                 static double d = 0.0;
153                 idx = SetDataOpt(&dopt, 0, d);
154                 logger.PutOut(cltype, cotype, testdata[j].dbusproperty, d);
155                 d += 0.1;
156                 break;
157             }
158             case CHAR : {
159                 static char c = 0;
160                 idx = SetDataOpt(&dopt, 0, c);
161                 logger.PutOut(cltype, cotype, testdata[j].dbusproperty, c++);
162                 break;
163             }
164             case INT16 : {
165                 static int16_t i16 = 0;
166                 idx = SetDataOpt(&dopt, 0, i16);
167                 logger.PutOut(cltype, cotype, testdata[j].dbusproperty, i16++);
168                 break;
169             }
170             case UINT16 : {
171                 static uint16_t ui16 = 0;
172                 idx = SetDataOpt(&dopt, 0, ui16);
173                 logger.PutOut(cltype, cotype, testdata[j].dbusproperty, ui16++);
174                 break;
175             }
176             case UINT32 : {
177                 static uint32_t ui32 = 0;
178                 idx = SetDataOpt(&dopt, 0, ui32);
179                 logger.PutOut(cltype, cotype, testdata[j].dbusproperty, ui32++);
180                 break;
181             }
182             case INT64 : {
183                 static int64_t i64 = 0;
184                 idx = SetDataOpt(&dopt, 0, i64);
185                 logger.PutOut(cltype, cotype, testdata[j].dbusproperty, i64++);
186                 break;
187             }
188             case UINT64 : {
189                 static uint64_t ui64 = 0;
190                 idx = SetDataOpt(&dopt, 0, ui64);
191                 logger.PutOut(cltype, cotype, testdata[j].dbusproperty, ui64++);
192                 break;
193             }
194             case BOOL : {
195                 static bool b = true;
196                 idx = SetDataOpt(&dopt, 0, b);
197                 logger.PutOut(cltype, cotype, testdata[j].dbusproperty, b);
198                 b = !b;
199                 break;
200             }
201             default : {
202                 break;
203             }
204             }
205             gettimeofday(&tv, NULL);
206             if (testdata[j].isstd) {
207                 stddataws.send(datamsg.encode(const_cast<char*>(testdata[j].vicproperty.c_str()), tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(int) + idx);
208             }
209             else {
210                 customdataws.send(datamsg.encode(const_cast<char*>(testdata[j].vicproperty.c_str()), tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(int) + idx);
211             }
212         }
213
214         stddataws.service();
215         customdataws.service();
216         gettimeofday(&endtv, NULL);
217         //std::cerr << "End - Start = " << ((endtv.tv_sec - starttv.tv_sec) * 1000 * 1000 + (endtv.tv_usec - starttv.tv_usec)) << std::endl;
218         usleep(interspace * 1000 - ((endtv.tv_sec - starttv.tv_sec) * 1000 * 1000 + (endtv.tv_usec - starttv.tv_usec)));
219     }
220
221     return 0;
222 }