packaging: add AMB configuration that enables the plugin
[profile/ivi/ico-vic-amb-plugin.git] / tests / vic3.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
16 template <typename T>
17 int SetDataOpt(DataOpt *opt, int idx, T value) {
18     memcpy(opt->status + idx, &value, sizeof(value));
19     return sizeof(value);
20 }
21
22 int main(int argc, char *argv[]) {
23     int ret;
24     std::string configpath = "/etc/ambd/AMBformat.conf";
25     while ((ret = getopt(argc, argv, "c:")) != -1) {
26         switch (ret) {
27         case 'c':
28             configpath = std::string(optarg);
29             break;
30         default :
31             break;
32         }
33     }
34
35     ifstream fin("standardvehicleinfo.txt");
36     std::string str;
37     std::vector<std::string> standardvehicleinfolist;
38     while (!fin.eof()) {
39         fin >> str;
40         standardvehicleinfolist.push_back(str);
41     }
42
43     AMBConfig config;
44     if (!config.readConfig(configpath)) {
45         std::cerr << "Can't read VIC-Plugin config file." << std::endl;
46         return -1;
47     }
48     PortInfo portinfo = config.getPort();
49
50     ControlWebsocketClient stddataws, customdataws;
51     if (!stddataws.initialize("ws://127.0.0.1", portinfo.standard.dataPort, "standarddatamessage-only")) {
52         std::cerr << "Can't connect standarddatamessage-only." << std::endl;
53         return -1;
54     }
55     if (!customdataws.initialize("ws://127.0.0.1", portinfo.custom.dataPort, "customdatamessage-only")) {
56         std::cerr << "Can't connect customdatamessage-only." << std::endl;
57         return -1;
58     }
59
60     vehicleinfolist =config.getVehicleInfoConfig();
61     auto itr_end = vehicleinfolist.end();
62     int idx = 0;
63     struct timeval tv;
64     LogResult logger;
65     enum ClientType cltype = CLIENT_VIC;
66     enum CommandType cotype = CMDTYPE_SET;
67     DataMessage datamsg;
68
69     for (int i = 0; i < 10; i++) {
70         stddataws.service();
71         customdataws.service();
72         usleep(50 * 1000);
73     }
74
75     for (auto itr = vehicleinfolist.begin(); itr != itr_end; itr++) {
76         stddataws.service();
77         customdataws.service();
78         usleep(50 * 1000);
79
80         DataOpt dopt;
81         dopt.common_status = SUPPORT;
82         idx = 0;
83         auto itr2_end = (*itr).status.end();
84         gettimeofday(&tv, NULL);
85         for (auto itr2 = (*itr).status.begin(); itr2 != itr2_end; itr2++) {
86             switch ((*itr2).type) {
87             case INT : {
88                 int i = 5;
89                 idx += SetDataOpt(&dopt, idx, i);
90                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i);
91                 break;
92             }
93             case DOUBLE : {
94                 double d = 5.0;
95                 idx += SetDataOpt(&dopt, idx, d);
96                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, d);
97                 break;
98             }
99             case CHAR : {
100                 char c = 5;
101                 idx += SetDataOpt(&dopt, idx, c);
102                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, c);
103                 break;
104             }
105             case INT16 : {
106                 int16_t i16 = 5;
107                 idx += SetDataOpt(&dopt, idx, i16);
108                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i16);
109                 break;
110             }
111             case UINT16 : {
112                 uint16_t ui16 = 5;
113                 idx += SetDataOpt(&dopt, idx, ui16);
114                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui16);
115                 break;
116             }
117             case UINT32 : {
118                 uint32_t ui32 = 5;
119                 idx += SetDataOpt(&dopt, idx, ui32);
120                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui32);
121                 break;
122             }
123             case INT64 : {
124                 int64_t i64 = 5;
125                 idx += SetDataOpt(&dopt, idx, i64);
126                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i64);
127                 break;
128             }
129             case UINT64 : {
130                 uint64_t ui64 = 5;
131                 idx += SetDataOpt(&dopt, idx, ui64);
132                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui64);
133                 break;
134             }
135             case BOOL : {
136                 bool b = true;
137                 idx += SetDataOpt(&dopt, idx, b);
138                 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, b);
139                 break;
140             }
141             default : {
142                 break;
143             }
144             }
145         }
146         if (std::find(standardvehicleinfolist.begin(), standardvehicleinfolist.end(), std::string((*itr).KeyEventType)) != standardvehicleinfolist.end()) {
147             stddataws.send(datamsg.encode((*itr).KeyEventType, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(int) + idx);
148             usleep(100 * 1000);
149         }
150         else {
151             customdataws.send(datamsg.encode((*itr).KeyEventType, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(int) + idx);
152             usleep(100 * 1000);
153         }
154         stddataws.service();
155         customdataws.service();
156         usleep(50 * 1000);
157     }
158     
159     for (int i = 0; i < 10; i++) {
160         stddataws.service();
161         customdataws.service();
162         usleep(50 * 1000);
163     }
164
165     return 0;
166 }