2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 * @brief System Test Tool for set VehicleInfo
21 #include <sys/ioctl.h>
25 #include <sys/types.h>
39 #define LWS_DEFAULTIP "127.0.0.1" /* connection default ip(localhost) */
40 #define LWS_DEFAULTPORT 25010 /* connection default port */
41 #define LWS_PROTOCOLNAME "standarddatamessage-only"
42 /* connection protocol name */
46 unsigned char datatype[4];
47 } vehicleinfo_key[] = {
48 { "VehicleSpeed", "VELOCITY", {TYPE_INT32, TYPE_NULL, 0,0} },
49 { "Speed", "VELOCITY", {TYPE_INT32, TYPE_NULL, 0,0} },
50 { "Velocity", "VELOCITY", {TYPE_INT32, TYPE_NULL, 0,0} },
51 { "Location", "LOCATION", {TYPE_DOUBLE, TYPE_DOUBLE, TYPE_DOUBLE, TYPE_NULL} },
52 { "Direction", "DIRECTION", {TYPE_INT32, TYPE_NULL, 0,0} },
53 { "EngineSpeed", "ENGINE_SPEED", {TYPE_INT32, TYPE_NULL, 0, 0} },
54 { "Engine", "ENGINE_SPEED", {TYPE_INT32, TYPE_NULL, 0, 0} },
55 { "Shift", "SHIFT", {TYPE_SHIFT, TYPE_BYTE, TYPE_NULL, 0} },
56 { "ShiftPosition", "SHIFT", {TYPE_SHIFT, TYPE_BYTE, TYPE_NULL, 0} },
57 { "Break_Signal", "BRAKE_SIGNAL", {TYPE_BOOL, TYPE_NULL, 0,0} },
58 { "BreakSignal", "BRAKE_SIGNAL", {TYPE_BOOL, TYPE_NULL, 0,0} },
59 { "Break", "BRAKE_SIGNAL", {TYPE_BOOL, TYPE_NULL, 0,0} },
60 { "Blinker", "TURN_SIGNAL", {TYPE_INT32, TYPE_NULL, 0, 0} },
61 { "Winker", "TURN_SIGNAL", {TYPE_INT32, TYPE_NULL, 0, 0} },
62 { "TurnSignal", "TURN_SIGNAL", {TYPE_INT32, TYPE_NULL, 0, 0} },
63 { "Turn", "TURN_SIGNAL", {TYPE_INT32, TYPE_NULL, 0, 0} },
64 { "lightStatus", "LIGHTSTATUS", {TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_BOOL} },
65 { "light", "LIGHTSTATUS", {TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_BOOL} },
66 { "WATER_TEMP", "WATER_TEMP", {TYPE_INT32, TYPE_NULL, 0, 0} },
67 { "EXTERIORBRIGHTNESS", "EXTERIORBRIGHTNESS", {TYPE_UINT16, TYPE_NULL, 0, 0} },
68 { "EXTERIOR", "EXTERIORBRIGHTNESS", {TYPE_UINT16, TYPE_NULL, 0, 0} },
69 { "\0", "\0", {TYPE_NULL, 0,0,0} } };
73 char KeyEventType[64];
74 struct timeval recordtime;
82 static struct ico_uws_context *context = NULL;
83 /* connection context */
84 static void *connect_id = NULL;
85 /* connection connection id */
86 static int connected = 0; /* connection flag */
88 static void uws_callback(const struct ico_uws_context *context,
89 const ico_uws_evt_e reason, const void *id,
90 const ico_uws_detail *detail, void *user_data);
93 uws_callback(const struct ico_uws_context *context, const ico_uws_evt_e reason,
94 const void *id, const ico_uws_detail *detail, void *user_data)
96 if (reason == ICO_UWS_EVT_OPEN) {
97 connect_id = (void *)id;
100 else if (reason == ICO_UWS_EVT_ERROR) {
101 fprintf(stderr, "Communication Error[%d]\n", detail->_ico_uws_error.code);
107 init_comm(const int port, const char *spadr)
112 snprintf(uri_name, sizeof(uri_name), "ws://%s:%d", spadr, port);
114 context = ico_uws_create_context(uri_name, LWS_PROTOCOLNAME);
115 if (context == NULL) {
116 fprintf(stderr, "Can not create conextion context(uri=%s port=%d)\n",
120 if (ico_uws_set_event_cb(context, uws_callback, NULL) !=
122 fprintf(stderr, "Can not set callback.\n");
126 /* wait for connection */
127 for (rep = 0; rep < (2*1000); rep += 50) {
128 if (connected) break;
129 ico_uws_service(context);
135 set_vehicleinfo(const char *cmd)
147 struct KeyDataMsg_t msg;
152 for (i = 0; cmd[i]; i++) {
153 if ((cmd[i] == '=') || (cmd[i] == ' ')) break;
154 if (j < (int)(sizeof(prop)-1)) {
162 for (i++; cmd[i]; i++) {
163 if (cmd[i] == ' ') continue;
164 if (j < (int)(sizeof(value)-1)) {
171 if (strcasecmp(prop, "sleep") == 0) {
174 for (i = 0; value[i]; i++) {
175 if (value[i] == '.') break;
176 sec = sec * 10 + (value[i] & 0x0f);
178 if (value[i] == '.') {
181 msec = (value[i] & 0x0f) * 100;
185 msec = msec + (value[i] & 0x0f) * 10;
189 msec = msec + (value[i] & 0x0f);
192 if (sec > 0) sleep(sec);
193 if (msec > 0) usleep(msec * 1000);
198 for (key = 0; vehicleinfo_key[key].prop[0]; key++) {
199 if (strcasecmp(prop, vehicleinfo_key[key].prop) == 0) break;
201 if (! vehicleinfo_key[key].prop[0]) {
202 fprintf(stderr, "VehicleInfo UnKnown property[%s]\n", prop);
206 memset(&msg, 0, sizeof(msg));
207 strcpy(msg.msg.KeyEventType, vehicleinfo_key[key].eventtype);
208 gettimeofday(&(msg.msg.recordtime), NULL);
209 msgsize = sizeof(msg) - 128;
213 for (idx = 0; idx < 4; idx++) {
214 if (vehicleinfo_key[key].datatype[idx] == TYPE_NULL) break;
217 for (j = i; value[j]; j++) {
218 if ((value[j] == ',') || (value[j] == ';') ||
219 (value[j] == ':') || (value[j] == ' ')) break;
224 switch (vehicleinfo_key[key].datatype[idx] % 10) {
227 if (vehicleinfo_key[key].datatype[idx] == TYPE_SHIFT) {
228 if ((strcasecmp(&value[i], "sp") == 0) ||
229 (strcasecmp(&value[i], "s0") == 0)) {
230 strcpy(&value[i], "0");
232 else if (strcasecmp(&value[i], "sr") == 0) {
233 strcpy(&value[i], "1");
235 else if (strcasecmp(&value[i], "sn") == 0) {
236 strcpy(&value[i], "2");
238 else if ((strcasecmp(&value[i], "sd") == 0) ||
239 (strcasecmp(&value[i], "s4") == 0)) {
240 strcpy(&value[i], "4");
242 else if ((strcasecmp(&value[i], "s1") == 0) ||
243 (strcasecmp(&value[i], "sl") == 0)) {
244 strcpy(&value[i], "5");
246 else if (strcasecmp(&value[i], "s2") == 0) {
247 strcpy(&value[i], "6");
249 else if (strcasecmp(&value[i], "s3") == 0) {
250 strcpy(&value[i], "7");
253 msg.msg.data.status[pt++] = strtoul(&value[i], (char **)0, 0);
258 sp = (short *)&msg.msg.data.status[pt];
259 *sp = strtol(&value[i], (char **)0, 0);
261 msgsize += sizeof(short);
265 ip = (int *)&msg.msg.data.status[pt];
266 *ip = strtol(&value[i], (char **)0, 0);
268 msgsize += sizeof(int);
271 dp = (double *)&msg.msg.data.status[pt];
272 *dp = strtod(&value[i], (char **)0);
273 pt += sizeof(double);
274 msgsize += sizeof(double);
282 switch (vehicleinfo_key[key].datatype[idx]) {
289 msgsize += sizeof(short);
293 msgsize += sizeof(int);
296 msgsize += sizeof(double);
304 ico_uws_send(context, connect_id, (unsigned char *)&msg, msgsize);
308 usage(const char *prog)
310 fprintf(stderr, "Usage: %s [-port=port] [-ip=ip_addr] [propaty=value] [propaty=value] ...\n", prog);
315 main(int argc, char *argv[])
318 int port = LWS_DEFAULTPORT;
322 strcpy(spadr, LWS_DEFAULTIP);
325 for (i = 1; i < argc; i++) {
326 if (argv[i][0] == '-') {
327 if (strncasecmp(argv[i], "-port=", 6) == 0) {
328 port = strtoul(&argv[i][6], (char **)0, 0);
330 else if (strncasecmp(argv[i], "-ip=", 4) == 0) {
331 memset(spadr, 0, sizeof(spadr));
332 strncpy(spadr, &argv[i][4], sizeof(spadr)-1);
343 init_comm(port, spadr);
346 fprintf(stderr, "Can't connect server.\n");
351 while (fgets(buf, sizeof(buf), stdin)) {
353 for (i = 0; buf[i]; i++) {
354 if ((buf[i] == '#') || (buf[i] == '\n') || (buf[i] == '\r')) break;
355 if (buf[i] == '\t') buf[i] = ' ';
356 if ((j < 0) && (buf[i] != ' ')) j = i;
360 set_vehicleinfo(&buf[j]);
364 for (i = 1; i < argc; i++) {
365 if (argv[i][0] == '-') continue;
366 set_vehicleinfo(argv[i]);
370 ico_uws_unset_event_cb(context);
371 ico_uws_close(context);