packaging: add AMB configuration that enables the plugin
[profile/ivi/ico-vic-amb-plugin.git] / tests / vic1.cc
1 #include <unistd.h>
2
3 #include <algorithm>
4 #include <cstdio>
5 #include <fstream>
6 #include <vector>
7
8 #include "ambconfig.h"
9 #include "controlwebsocketclient.h"
10 #include "datamessage.h"
11 #include "eventmessage.h"
12 #include "logresult.h"
13
14 std::vector<VehicleInfoDefine> vehicleinfolist;
15 char NOTREGISTERED[] = "NotRegistered";
16
17 class ControlWebsocketClientForControl : public ControlWebsocketClient {
18     public:
19         bool recv(char *recvbuf, const size_t len);
20     private:
21         LogResult logger;
22 };
23
24 bool ControlWebsocketClientForControl::recv(char *recvbuf, const size_t len) {
25     DataMessage datamsg;
26     datamsg.decode(recvbuf, len);
27     DataOpt dopt = datamsg.getDataOpt();
28
29     auto itr_end = vehicleinfolist.end();
30     ClientType cltype = CLIENT_VIC;
31     CommandType cotype = CMDTYPE_GET;
32     for (auto itr = vehicleinfolist.begin(); itr != itr_end; itr++) {
33         if (strcmp((*itr).KeyEventType, datamsg.getKeyEventType()) == 0) {
34             auto itr2_end = (*itr).status.end();
35             int idx = 0;
36             for (auto itr2 = (*itr).status.begin(); itr2 != itr2_end; itr2++) {
37                 switch ((*itr2).type) {
38                 case INT:
39                     int i;
40                     memcpy(&i, dopt.status + idx, sizeof(i));
41                     logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i, datamsg.getRecordtime());
42                     idx += sizeof(i);
43                     break;
44                 case DOUBLE:
45                     double d;
46                     memcpy(&d, dopt.status + idx, sizeof(d));
47                     logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, d, datamsg.getRecordtime());
48                     idx += sizeof(d);
49                     break;
50                 case CHAR:
51                     char c;
52                     memcpy(&c, dopt.status + idx, sizeof(c));
53                     logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, c, datamsg.getRecordtime());
54                     idx += sizeof(c);
55                     break;
56                 case INT16:
57                     int16_t i16;
58                     memcpy(&i16, dopt.status + idx, sizeof(i16));
59                     logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i16, datamsg.getRecordtime());
60                     idx += sizeof(i16);
61                     break;
62                 case UINT16:
63                     uint16_t ui16;
64                     memcpy(&ui16, dopt.status + idx, sizeof(ui16));
65                     logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui16, datamsg.getRecordtime());
66                     idx += sizeof(ui16);
67                     break;
68                 case UINT32:
69                     uint32_t ui32;
70                     memcpy(&ui32, dopt.status + idx, sizeof(ui32));
71                     logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui32, datamsg.getRecordtime());
72                     idx += sizeof(ui32);
73                     break;
74                 case INT64:
75                     int64_t i64;
76                     memcpy(&i64, dopt.status + idx, sizeof(i64));
77                     logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i64, datamsg.getRecordtime());
78                     idx += sizeof(i64);
79                     break;
80                 case UINT64:
81                     uint64_t ui64;
82                     memcpy(&ui64, dopt.status + idx, sizeof(ui64));
83                     logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui64, datamsg.getRecordtime());
84                     idx += sizeof(ui64);
85                     break;
86                 case BOOL:
87                     bool b;
88                     memcpy(&b, dopt.status + idx, sizeof(b));
89                     logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, b, datamsg.getRecordtime());
90                     idx += sizeof(b);
91                     break;
92                 default:
93                     break;
94                 }
95             }
96         }
97     }
98     return true;
99 }
100
101 template <typename T>
102 int SetDataOpt(DataOpt *opt, int idx, T value) {
103     memcpy(opt->status + idx, &value, sizeof(value));
104     return sizeof(value);
105 }
106
107 int main(int argc, char *argv[]) {
108     int ret;
109     std::string configpath = "/etc/ambd/AMBformat.conf";
110     while ((ret = getopt(argc, argv, "c:")) != -1) {
111         switch (ret) {
112         case 'c':
113             configpath = std::string(optarg);
114             break;
115         default :
116             break;
117         }
118     }
119
120     ifstream fin("standardvehicleinfo.txt");
121     std::string str;
122     std::vector<std::string> standardvehicleinfolist;
123     while (!fin.eof()) {
124         fin >> str;
125         standardvehicleinfolist.push_back(str);
126     }
127
128     AMBConfig config;
129     if (!config.readConfig(configpath)) {
130         std::cerr << "Can't read VIC-Plugin config file." << std::endl;
131         return -1;
132     }
133     PortInfo portinfo = config.getPort();
134
135     ControlWebsocketClient stddataws, customdataws;
136     ControlWebsocketClientForControl stdctrlws, customctrlws;
137     if (!stddataws.initialize("ws://127.0.0.1", portinfo.standard.dataPort, "standarddatamessage-only")) {
138         std::cerr << "Can't connect standarddatamessage-only." << std::endl;
139         return -1;
140     }
141     if (!stdctrlws.initialize("ws://127.0.0.1", portinfo.standard.controlPort, "standardcontrolmessage-only")) {
142         std::cerr << "Can't connect standarddatamessage-only." << std::endl;
143         return -1;
144     }
145     if (!customdataws.initialize("ws://127.0.0.1", portinfo.custom.dataPort, "customdatamessage-only")) {
146         std::cerr << "Can't connect customdatamessage-only." << std::endl;
147         return -1;
148     }
149     if (!customctrlws.initialize("ws://127.0.0.1", portinfo.custom.controlPort, "customcontrolmessage-only")) {
150         std::cerr << "Can't connect customdatamessage-only." << std::endl;
151         return -1;
152     }
153
154     vehicleinfolist =config.getVehicleInfoConfig();
155     auto itr_end = vehicleinfolist.end();
156     int idx = 0;
157     struct timeval tv;
158     LogResult logger;
159     enum ClientType cltype = CLIENT_VIC;
160     enum CommandType cotype = CMDTYPE_SET;
161     DataMessage datamsg;
162
163     for (int i = 0; i < 10; i++) {
164         stddataws.service();
165         customdataws.service();
166         stdctrlws.service();
167         customctrlws.service();
168         usleep(50 * 1000);
169     }
170
171     for (auto itr = vehicleinfolist.begin(); itr != itr_end; itr++) {
172         stddataws.service();
173         customdataws.service();
174         stdctrlws.service();
175         customctrlws.service();
176         usleep(50 * 1000);
177
178         DataOpt dopt;
179         dopt.common_status = SUPPORT;
180         idx = 0;
181         auto itr2_end = (*itr).status.end();
182         gettimeofday(&tv, NULL);
183         for (auto itr2 = (*itr).status.begin(); itr2 != itr2_end; itr2++) {
184             switch ((*itr2).type) {
185             case INT : {
186                 int i = 5;
187                 idx += SetDataOpt(&dopt, idx, i);
188                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i);
189                 break;
190             }
191             case DOUBLE : {
192                 double d = 5.0;
193                 idx += SetDataOpt(&dopt, idx, d);
194                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, d);
195                 break;
196             }
197             case CHAR : {
198                 char c = 5;
199                 idx += SetDataOpt(&dopt, idx, c);
200                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, c);
201                 break;
202             }
203             case INT16 : {
204                 int16_t i16 = 5;
205                 idx += SetDataOpt(&dopt, idx, i16);
206                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i16);
207                 break;
208             }
209             case UINT16 : {
210                 uint16_t ui16 = 5;
211                 idx += SetDataOpt(&dopt, idx, ui16);
212                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui16);
213                 break;
214             }
215             case UINT32 : {
216                 uint32_t ui32 = 5;
217                 idx += SetDataOpt(&dopt, idx, ui32);
218                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui32);
219                 break;
220             }
221             case INT64 : {
222                 int64_t i64 = 5;
223                 idx += SetDataOpt(&dopt, idx, i64);
224                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i64);
225                 break;
226             }
227             case UINT64 : {
228                 uint64_t ui64 = 5;
229                 idx += SetDataOpt(&dopt, idx, ui64);
230                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui64);
231                 break;
232             }
233             case BOOL : {
234                 bool b = true;
235                 idx += SetDataOpt(&dopt, idx, b);
236                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, b);
237                 break;
238             }
239             default : {
240                 break;
241             }
242             }
243         }
244         if (std::find(standardvehicleinfolist.begin(), standardvehicleinfolist.end(), std::string((*itr).KeyEventType)) != standardvehicleinfolist.end()) {
245             stddataws.send(datamsg.encode((*itr).KeyEventType, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(int) + idx);
246             usleep(100 * 1000);
247         }
248         else {
249             customdataws.send(datamsg.encode((*itr).KeyEventType, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(int) + idx);
250             usleep(100 * 1000);
251         }
252         stddataws.service();
253         customdataws.service();
254         stdctrlws.service();
255         customctrlws.service();
256         usleep(50 * 1000);
257     }
258     DataOpt dopt;
259     dopt.common_status = SUPPORT;
260     SetDataOpt(&dopt, 0, (int)5);
261     stddataws.send(datamsg.encode(NOTREGISTERED, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + 2 * sizeof(int));
262     stddataws.service();
263     customdataws.service();
264     stdctrlws.service();
265     customctrlws.service();
266     usleep(50 * 1000);
267     customdataws.send(datamsg.encode(NOTREGISTERED, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + 2 * sizeof(int));
268     stddataws.service();
269     customdataws.service();
270     stdctrlws.service();
271     customctrlws.service();
272     usleep(50 * 1000);
273     sleep(1);
274     EventMessage eventmsg;
275     EventOpt eopt;
276     eopt.common = -1;
277     eopt.sense = -1;
278     eopt.event_mask = -1;
279
280     for (auto itr = vehicleinfolist.begin(); itr != itr_end; itr++) {
281         stddataws.service();
282         customdataws.service();
283         stdctrlws.service();
284         customctrlws.service();
285         usleep(50 * 1000);
286
287         gettimeofday(&tv, NULL);
288         if (std::find(standardvehicleinfolist.begin(), standardvehicleinfolist.end(), std::string((*itr).KeyEventType)) != standardvehicleinfolist.end()) {
289             stdctrlws.send(eventmsg.encode((*itr).KeyEventType, tv, eopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(EventOpt));
290             usleep(100 * 1000);
291         }
292         else {
293             customctrlws.send(eventmsg.encode((*itr).KeyEventType, tv, eopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(EventOpt));
294             usleep(100 * 1000);
295         }
296         stddataws.service();
297         customdataws.service();
298         stdctrlws.service();
299         customctrlws.service();
300         usleep(50 * 1000);
301     }
302     stdctrlws.send(eventmsg.encode(NOTREGISTERED, tv, eopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(EventOpt));
303     stddataws.service();
304     customdataws.service();
305     stdctrlws.service();
306     customctrlws.service();
307     usleep(50 * 1000);
308     customctrlws.send(eventmsg.encode(NOTREGISTERED, tv, eopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(EventOpt));
309     stddataws.service();
310     customdataws.service();
311     stdctrlws.service();
312     customctrlws.service();
313     usleep(50 * 1000);
314
315     for (int i = 0; i < 10; i++) {
316         stddataws.service();
317         customdataws.service();
318         stdctrlws.service();
319         customctrlws.service();
320         usleep(50 * 1000);
321     }
322     
323     return 0;
324 }