automotive-dlt
dlt-passive-node-ctrl.c
Go to the documentation of this file.
1 
23 /*******************************************************************************
24 ** **
25 ** SRC-MODULE: dlt-passive-node-ctrl.c **
26 ** **
27 ** TARGET : linux **
28 ** **
29 ** PROJECT : DLT **
30 ** **
31 ** AUTHOR : Christoph Lipka <clipka@jp.adit-jv.com> **
32 ** PURPOSE : **
33 ** **
34 ** REMARKS : **
35 ** **
36 ** PLATFORM DEPENDANT [yes/no]: yes **
37 ** **
38 ** TO BE CHANGED BY USER [yes/no]: no **
39 ** **
40 *******************************************************************************/
41 
42 /*******************************************************************************
43 ** Author Identity **
44 ********************************************************************************
45 ** **
46 ** Initials Name Company **
47 ** -------- ------------------------- ---------------------------------- **
48 ** CL Christoph Lipka ADIT **
49 *******************************************************************************/
50 
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <ctype.h>
55 #include <pthread.h>
56 #include "dlt_protocol.h"
57 #include "dlt_client.h"
58 #include "dlt-control-common.h"
60 
61 #define MAX_RESPONSE_LENGTH 32
62 
63 #define DLT_NODE_CONNECT 1
64 #define DLT_NODE_DISCONNECT 0
65 #define DLT_NODE_CONNECT_UNDEF 999
66 
67 #define DLT_GATEWAY_CONNECTED 2
68 #define DLT_NODE_CONNECTED_STR "Connected"
69 #define DLT_NODE_DISCONNECTED_STR "Disconnected"
70 
71 #define UNDEFINED 999
72 
73 static struct PassiveNodeOptions {
74  unsigned int command;
75  unsigned int connection_state;
77  long timeout;
78 } g_options = {
79  .command = UNDEFINED,
80  .connection_state = UNDEFINED,
81  .node_id = {'\0'},
82 };
83 
84 unsigned int get_command(void)
85 {
86  return g_options.command;
87 }
88 
89 void set_command(unsigned int c)
90 {
91  g_options.command = c;
92 }
93 
94 unsigned int get_connection_state(void)
95 {
97 }
98 
99 void set_connection_state(unsigned int s)
100 {
101  if ((s == DLT_NODE_CONNECT) || (s == DLT_NODE_DISCONNECT))
102  {
105  }
106  else
107  {
108  pr_error("Connection status %u invalid\n", s);
109  exit(-1);
110  }
111 }
112 
113 void set_node_id(char *id)
114 {
115  if (id == 0)
116  {
117  pr_error("node identifier is NULL\n");
118  exit(-1);
119  }
120  else
121  {
122  strncpy(g_options.node_id, id, DLT_ID_SIZE);
123  }
124 }
125 
126 char *get_node_id()
127 {
128  return g_options.node_id;
129 }
130 
137  DltServicePassiveNodeConnectionInfo *info)
138 {
139  unsigned int i = 0;
140  char *status;
141 
142  if (info == NULL)
143  {
144  return;
145  }
146 
147  printf("\nPassive Node connection status:\n"
148  "---------------------------------\n");
149  for(i = 0; i < info->num_connections; i++)
150  {
151  if (info->connection_status[i] == DLT_GATEWAY_CONNECTED)
152  {
153  status = DLT_NODE_CONNECTED_STR;
154  }
155  else
156  {
157  status = DLT_NODE_DISCONNECTED_STR;
158  }
159 
160  printf("%.4s: %s\n", &info->node_id[i * DLT_ID_SIZE], status);
161  }
162 
163  printf("\n");
164 }
165 
177 static int dlt_passive_node_analyze_response(char *answer,
178  void *payload,
179  int len)
180 {
181  int ret = -1;
182  char resp_ok[MAX_RESPONSE_LENGTH] = { 0 };
183 
184  if (answer == NULL || payload == NULL)
185  {
186  return -1;
187  }
188 
189  snprintf(resp_ok,
191  "service(%u), ok",
192  get_command());
193 
194  pr_verbose("Response received: '%s'\n", answer);
195  pr_verbose("Response expected: '%s'\n", resp_ok);
196 
197  if (strncmp(answer, resp_ok, strlen(resp_ok)) == 0)
198  {
199  ret = 0;
200 
202  {
203  if ((int)sizeof(DltServicePassiveNodeConnectionInfo) > len)
204  {
205  pr_error("Received payload is smaller than expected\n");
206  pr_verbose("Expected: %lu,\nreceived: %d",
207  sizeof(DltServicePassiveNodeConnectionInfo),
208  len);
209  ret = -1;
210  }
211  else
212  {
213  DltServicePassiveNodeConnectionInfo *info =
214  (DltServicePassiveNodeConnectionInfo *) (payload);
215  if (info == NULL)
216  {
217  fprintf(stderr, "Received response is NULL\n");
218  return -1;
219  }
220 
222  }
223  }
224  }
225 
226  return ret;
227 }
228 
235 {
236  DltControlMsgBody *mb = calloc(1, sizeof(DltControlMsgBody));
237  char *ecuid = get_node_id();
238  if (mb == NULL)
239  {
240  return NULL;
241  }
242 
244  {
245  mb->data = calloc(1, sizeof(DltServicePassiveNodeConnect));
246  if (mb->data == NULL)
247  {
248  free(mb);
249  return NULL;
250  }
251  mb->size = sizeof(DltServicePassiveNodeConnect);
252  DltServicePassiveNodeConnect *serv = (DltServicePassiveNodeConnect *)
253  mb->data;
254  serv->service_id = DLT_SERVICE_ID_PASSIVE_NODE_CONNECT;
255  serv->connection_status = get_connection_state();
256 
257  memcpy(serv->node_id, ecuid, DLT_ID_SIZE);
258  }
259  else /* DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS */
260  {
261  mb->data = calloc(1, sizeof(DltServicePassiveNodeConnectionInfo));
262  if (mb->data == NULL)
263  {
264  free(mb);
265  return NULL;
266  }
267 
268  mb->size = sizeof(DltServicePassiveNodeConnectionInfo);
269  DltServicePassiveNodeConnectionInfo *serv =
270  (DltServicePassiveNodeConnectionInfo *) mb->data;
272  }
273 
274  return mb;
275 }
276 
281 {
282  if (msg_body == NULL)
283  {
284  return;
285  }
286 
287  if (msg_body->data != NULL)
288  {
289  free(msg_body->data);
290  }
291  free(msg_body);
292 }
293 
300 {
301  int ret = -1;
302 
303  /* Initializing the communication with the daemon */
305  get_ecuid(),
306  get_verbosity()) != 0)
307  {
308  pr_error("Failed to initialize connection with the daemon.\n");
309  return ret;
310  }
311 
312  /* prepare message body */
313  DltControlMsgBody *msg_body = NULL;
315 
316  if (msg_body == NULL)
317  {
318  pr_error("Data for Dlt Message body is NULL\n");
319  return ret;
320  }
321 
322  ret = dlt_control_send_message(msg_body, get_timeout());
323 
325 
327 
328  return ret;
329 }
330 
331 static void usage()
332 {
333  printf("Usage: dlt-passive-node-ctrl [options]\n");
334  printf("Send a trigger to DLT daemon to (dis)connect a passive node "
335  "or get current passive node status \n");
336  printf("\n");
337  printf("Options:\n");
338  printf(" -c Connection status (1 - connect, 0 - disconnect)\n");
339  printf(" -h Usage\n");
340  printf(" -n passive Node identifier (e.g. ECU2)\n");
341  printf(" -s Show passive node(s) connection status\n");
342  printf(" -t Specify connection timeout (Default: %ds)\n",
344  printf(" -v Set verbose flag (Default:%d)\n", get_verbosity());
345 }
346 
356 static int parse_args(int argc, char *argv[])
357 {
358  int c = 0;
359  int state = -1;
360 
361  /* Get command line arguments */
362  opterr = 0;
363 
364  while ((c = getopt(argc, argv, "c:hn:stv")) != -1)
365  {
366  switch(c)
367  {
368  case 'c':
369  state = (int)strtol(optarg,NULL, 10);
370  if (state == DLT_NODE_CONNECT || state == DLT_NODE_DISCONNECT)
371  {
372  set_connection_state(state);
374  }
375  else
376  {
377  pr_error("unknown connection state: %d\n", state);
378  return -1;
379  }
380  break;
381  case 'h':
382  usage();
383  return -1;
384  case 'n':
385  set_node_id(optarg);
386  break;
387  case 's':
389  break;
390  case 't':
391  set_timeout(strtol(optarg, NULL, 10));
392  break;
393  case 'v':
394  set_verbosity(1);
395  pr_verbose("Now in verbose mode.\n");
396  break;
397  case '?':
398  if (isprint(optopt))
399  {
400  pr_error("Unknown option -%c.\n", optopt);
401  }
402  else
403  {
404  pr_error("Unknown option character \\x%x.\n", optopt);
405  }
406  usage();
407  return -1;
408  default:
409  pr_error("Try %s -h for more information.\n", argv[0]);
410  return -1;
411  }
412  }
413 
414  return 0;
415 }
416 
426 int main(int argc, char *argv[])
427 {
428  int ret = 0;
429 
430  set_ecuid(NULL);
432 
433  /* Get command line arguments */
434  if (parse_args(argc, argv) != 0)
435  {
436  return -1;
437  }
438 
439  if (get_command() == UNDEFINED ||
441  g_options.node_id[0] == '\0' &&
443  {
444  pr_error("No valid parameter configuration given!\n");
445  usage();
446  return -1;
447  }
448 
449  pr_verbose("Sending command to DLT daemon.\n");
450 
451  /* one shot request */
453 
454  pr_verbose("Exiting.\n");
455 
456  return ret;
457 }
static void usage()
#define DLT_ID_SIZE
Definition: dlt_common.h:204
static int dlt_passive_node_analyze_response(char *answer, void *payload, int len)
Analyze received DLT Daemon response.
void set_verbosity(int v)
void set_connection_state(unsigned int s)
#define DLT_CTRL_TIMEOUT
void dlt_passive_node_destroy_message_body(DltControlMsgBody *msg_body)
Destroy message body.
static void dlt_print_passive_node_status(DltServicePassiveNodeConnectionInfo *info)
Print passive node status information.
void set_node_id(char *id)
int dlt_control_deinit(void)
Control communication clean-up.
static struct PassiveNodeOptions g_options
void set_timeout(long t)
#define DLT_NODE_CONNECT
#define UNDEFINED
static int parse_args(int argc, char *argv[])
Parse application arguments.
void set_command(unsigned int c)
char node_id[DLT_ID_SIZE]
#define DLT_NODE_DISCONNECT
char * get_ecuid(void)
char * get_node_id()
#define DLT_NODE_CONNECT_UNDEF
#define pr_error(fmt,...)
#define DLT_NODE_DISCONNECTED_STR
unsigned int get_connection_state(void)
int main(int argc, char *argv[])
Entry point.
static int dlt_passive_node_ctrl_single_request()
Send a single command to DLT daemon and wait for response.
#define MAX_RESPONSE_LENGTH
#define DLT_NODE_CONNECTED_STR
int dlt_control_send_message(DltControlMsgBody *body, int timeout)
Send a message to the daemon and wait for the asynchronous answer.
DltControlMsgBody * dlt_passive_node_prepare_message_body()
Prepare message body to be send to DLT Daemon.
#define DLT_GATEWAY_CONNECTED
#define DLT_SERVICE_ID_PASSIVE_NODE_CONNECT
Definition: dlt_protocol.h:202
#define pr_verbose(fmt,...)
int dlt_control_init(int(*response_analyzer)(char *, void *, int), char *ecuid, int verbosity)
Control communication initialization.
#define DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS
Definition: dlt_protocol.h:203
int get_verbosity(void)
unsigned int get_command(void)
long get_timeout(void)
#define NULL
Definition: dlt_common.h:232
void set_ecuid(char *ecuid)