pubsub_opts.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2012, 2018 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v2.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  * https://www.eclipse.org/legal/epl-2.0/
10  * and the Eclipse Distribution License is available at
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  *
13  * Contributors:
14  * Ian Craggs - initial contribution
15  *******************************************************************************/
16 
17 #include "pubsub_opts.h"
18 
19 #include <string.h>
20 #include <stdlib.h>
21 
22 
24 {
25  int rc = 0;
26 
27  printf("\nLibrary information:\n");
28  while (info->name)
29  {
30  printf("%s: %s\n", info->name, info->value);
31  info++;
32  rc = 1; /* at least one value printed */
33  }
34  if (rc == 1)
35  printf("\n");
36  return rc;
37 }
38 
39 
40 void usage(struct pubsub_opts* opts, pubsub_opts_nameValue* name_values, const char* program_name)
41 {
42  printf("Eclipse Paho MQTT C %s\n", opts->publisher ? "publisher" : "subscriber");
43  printVersionInfo(name_values);
44 
45  printf("Usage: %s [topicname] [-t topic] [-c connection] [-h host] [-p port]\n"
46  " [-q qos] [-i clientid] [-u username] [-P password] [-k keepalive_timeout]\n"
47  , program_name);
48  printf(" [-V MQTT-version] [--quiet] [--trace trace-level]\n");
49  if (opts->publisher)
50  {
51  printf(" [-r] [-n] [-m message] [-f filename]\n");
52  printf(" [--maxdatalen len] [--message-expiry seconds] [--user-property name value]\n");
53  }
54  else
55  printf(" [-R] [--no-delimiter]\n");
56  printf(" [--will-topic topic] [--will-payload message] [--will-qos qos] [--will-retain]\n");
57  printf(" [--cafile filename] [--capath dirname] [--cert filename] [--key filename]\n"
58  " [--keypass string] [--ciphers string] [--insecure]");
59 
60  printf(
61  "\n\n -t (--topic) : MQTT topic to %s to\n"
62  " -c (--connection) : connection string, overrides host/port e.g wss://hostname:port/ws. Use this option\n"
63  " rather than host/port to connect with TLS and/or web sockets. No default.\n"
64  " -h (--host) : host to connect to. Default is %s.\n"
65  " -p (--port) : network port to connect to. Default is %s.\n"
66  " -q (--qos) : MQTT QoS to %s with (0, 1 or 2). Default is %d.\n"
67  " -V (--MQTTversion) : MQTT version (31, 311, or 5). Default is 311.\n"
68  " --quiet : do not print error messages.\n"
69  " --trace : print internal trace (\"error\", \"min\", \"max\" or \"protocol\").\n",
70  opts->publisher ? "publish" : "subscribe", opts->host, opts->port,
71  opts->publisher ? "publish" : "subscribe", opts->qos);
72 
73  if (opts->publisher)
74  {
75  printf(" -r (--retained) : use MQTT retain option. Default is off.\n");
76  printf(" -n (--null-message) : send 0-length message.\n");
77  printf(" -m (--message) : the payload to send.\n");
78  printf(" -f (--filename) : use the contents of the named file as the payload.\n");
79  }
80 
81  printf(
82  " -i (--clientid) : MQTT client id. Default is %s.\n"
83  " -u (--username) : MQTT username. No default.\n"
84  " -P (--password) : MQTT password. No default.\n"
85  " -k (--keepalive) : MQTT keepalive timeout value. Default is %d seconds.\n"
86  " --delimiter : delimiter string. Default is \\n.\n",
87  opts->clientid, opts->keepalive);
88 
89  if (opts->publisher)
90  {
91  printf(" --maxdatalen : maximum length of data to read when publishing strings (default is %d)\n",
92  opts->maxdatalen);
93  printf(" --message-expiry : MQTT 5 only. Sets the message expiry property in seconds.\n");
94  printf(" --user-property : MQTT 5 only. Sets a user property.\n");
95  }
96  else
97  {
98  printf(" --no-delimiter : do not use a delimiter string between messages.\n");
99  printf(" -R (--no-retained) : do not print retained messages.\n");
100  }
101 
102  printf(
103  " --will-topic : will topic on connect. No default.\n"
104  " --will-payload : will message. If the will topic is set, but not payload, a null message will be set.\n"
105  " --will-retain : set the retained flag on the will message. The default is off.\n"
106  " --will-qos : the will message QoS. The default is 0.\n"
107  );
108 
109  printf(
110  " --cafile : a filename for the TLS truststore.\n"
111  " --capath : a directory name containing TLS trusted server certificates.\n"
112  " --cert : a filename for the TLS keystore containing client certificates.\n"
113  " --key : client private key file.\n"
114  " --keypass : password for the client private key file.\n"
115  " --ciphers : the list of cipher suites that the client will present to the server during\n"
116  " the TLS handshake.\n"
117  " --insecure : don't check that the server certificate common name matches the hostname.\n"
118  " --psk : pre-shared-key in hexadecimal (no leading 0x) \n"
119  " --psk-identity : client identity string for TLS-PSK mode.\n"
120  );
121 
122  printf("\nSee http://eclipse.org/paho for more information about the Eclipse Paho project.\n");
123  exit(EXIT_FAILURE);
124 }
125 
126 
127 
128 int getopts(int argc, char** argv, struct pubsub_opts* opts)
129 {
130  int count = 1;
131 
132  if (argv[1][0] != '-')
133  {
134  opts->topic = argv[1];
135  count = 2;
136  }
137 
138  while (count < argc)
139  {
140  if (strcmp(argv[count], "--verbose") == 0 || strcmp(argv[count], "-v") == 0)
141  opts->verbose = 1;
142  else if (strcmp(argv[count], "--quiet") == 0)
143  opts->quiet = 1;
144  else if (strcmp(argv[count], "--qos") == 0 || strcmp(argv[count], "-q") == 0)
145  {
146  if (++count < argc)
147  {
148  if (strcmp(argv[count], "0") == 0)
149  opts->qos = 0;
150  else if (strcmp(argv[count], "1") == 0)
151  opts->qos = 1;
152  else if (strcmp(argv[count], "2") == 0)
153  opts->qos = 2;
154  else
155  return 1;
156  }
157  else
158  return 1;
159  }
160  else if (strcmp(argv[count], "--connection") == 0 || strcmp(argv[count], "-c") == 0)
161  {
162  if (++count < argc)
163  opts->connection = argv[count];
164  else
165  return 1;
166  }
167  else if (strcmp(argv[count], "--host") == 0 || strcmp(argv[count], "-h") == 0)
168  {
169  if (++count < argc)
170  opts->host = argv[count];
171  else
172  return 1;
173  }
174  else if (strcmp(argv[count], "--port") == 0 || strcmp(argv[count], "-p") == 0)
175  {
176  if (++count < argc)
177  opts->port = argv[count];
178  else
179  return 1;
180  }
181  else if (strcmp(argv[count], "--clientid") == 0 || strcmp(argv[count], "-i") == 0)
182  {
183  if (++count < argc)
184  opts->clientid = argv[count];
185  else
186  return 1;
187  }
188  else if (strcmp(argv[count], "--username") == 0 || strcmp(argv[count], "-u") == 0)
189  {
190  if (++count < argc)
191  opts->username = argv[count];
192  else
193  return 1;
194  }
195  else if (strcmp(argv[count], "--password") == 0 || strcmp(argv[count], "-P") == 0)
196  {
197  if (++count < argc)
198  opts->password = argv[count];
199  else
200  return 1;
201  }
202  else if (strcmp(argv[count], "--maxdatalen") == 0)
203  {
204  if (++count < argc)
205  opts->maxdatalen = atoi(argv[count]);
206  else
207  return 1;
208  }
209  else if (strcmp(argv[count], "--delimiter") == 0)
210  {
211  if (++count < argc)
212  opts->delimiter = argv[count];
213  else
214  return 1;
215  }
216  else if (strcmp(argv[count], "--no-delimiter") == 0)
217  opts->delimiter = NULL;
218  else if (strcmp(argv[count], "--keepalive") == 0 || strcmp(argv[count], "-k") == 0)
219  {
220  if (++count < argc)
221  opts->keepalive = atoi(argv[count]);
222  else
223  return 1;
224  }
225  else if (strcmp(argv[count], "--topic") == 0 || strcmp(argv[count], "-t") == 0)
226  {
227  if (++count < argc)
228  opts->topic = argv[count];
229  else
230  return 1;
231  }
232  else if (strcmp(argv[count], "--will-topic") == 0)
233  {
234  if (++count < argc)
235  opts->will_topic = argv[count];
236  else
237  return 1;
238  }
239  else if (strcmp(argv[count], "--will-payload") == 0)
240  {
241  if (++count < argc)
242  opts->will_payload = argv[count];
243  else
244  return 1;
245  }
246  else if (strcmp(argv[count], "--will-qos") == 0)
247  {
248  if (++count < argc)
249  opts->will_qos = atoi(argv[count]);
250  else
251  return 1;
252  }
253  else if (strcmp(argv[count], "--will-retain") == 0)
254  {
255  if (++count < argc)
256  opts->will_retain = 1;
257  else
258  return 1;
259  }
260  else if (strcmp(argv[count], "--insecure") == 0)
261  opts->insecure = 1;
262  else if (strcmp(argv[count], "--capath") == 0)
263  {
264  if (++count < argc)
265  opts->capath = argv[count];
266  else
267  return 1;
268  }
269  else if (strcmp(argv[count], "--cafile") == 0)
270  {
271  if (++count < argc)
272  opts->cafile = argv[count];
273  else
274  return 1;
275  }
276  else if (strcmp(argv[count], "--cert") == 0)
277  {
278  if (++count < argc)
279  opts->cert = argv[count];
280  else
281  return 1;
282  }
283  else if (strcmp(argv[count], "--key") == 0)
284  {
285  if (++count < argc)
286  opts->key = argv[count];
287  else
288  return 1;
289  }
290  else if (strcmp(argv[count], "--keypass") == 0)
291  {
292  if (++count < argc)
293  opts->keypass = argv[count];
294  else
295  return 1;
296  }
297  else if (strcmp(argv[count], "--ciphers") == 0)
298  {
299  if (++count < argc)
300  opts->ciphers = argv[count];
301  else
302  return 1;
303  }
304  else if (strcmp(argv[count], "--psk") == 0)
305  {
306  if (++count < argc)
307  opts->psk = argv[count];
308  else
309  return 1;
310  }
311  else if (strcmp(argv[count], "--psk-identity") == 0)
312  {
313  if (++count < argc)
314  opts->psk_identity = argv[count];
315  else
316  return 1;
317  }
318  else if (strcmp(argv[count], "-V") == 0)
319  {
320  if (++count < argc)
321  {
322  if (strcmp(argv[count], "mqttv31") == 0 || strcmp(argv[count], "31") == 0)
324  else if (strcmp(argv[count], "mqttv311") == 0 || strcmp(argv[count], "311") == 0)
326  else if (strcmp(argv[count], "mqttv5") == 0 || strcmp(argv[count], "5") == 0)
327  opts->MQTTVersion = MQTTVERSION_5;
328  else
329  return 1;
330  }
331  else
332  return 1;
333  }
334  else if (strcmp(argv[count], "--trace") == 0)
335  {
336  if (++count < argc)
337  {
338  if (strcmp(argv[count], "error") == 0)
340  else if (strcmp(argv[count], "protocol") == 0)
342  else if (strcmp(argv[count], "min") == 0 || strcmp(argv[count], "on") == 0)
344  else if (strcmp(argv[count], "max") == 0)
346  else
347  return 1;
348  }
349  else
350  return 1;
351  }
352  else if (opts->publisher == 0)
353  {
354  if (strcmp(argv[count], "--no-retained") == 0 || strcmp(argv[count], "-R") == 0)
355  opts->retained = 1;
356  else
357  {
358  fprintf(stderr, "Unknown option %s\n", argv[count]);
359  return 1;
360  }
361  }
362  else if (opts->publisher == 1)
363  {
364  if (strcmp(argv[count], "--retained") == 0 || strcmp(argv[count], "-r") == 0)
365  opts->retained = 1;
366  else if (strcmp(argv[count], "--user-property") == 0)
367  {
368  if (count + 2 < argc)
369  {
370  opts->user_property.name = argv[++count];
371  opts->user_property.value = argv[++count];
372  }
373  else
374  return 1;
375  }
376  else if (strcmp(argv[count], "--message-expiry") == 0)
377  {
378  if (++count < argc)
379  opts->message_expiry = atoi(argv[count]);
380  else
381  return 1;
382  }
383  else if (strcmp(argv[count], "-m") == 0 || strcmp(argv[count], "--message") == 0)
384  {
385  if (++count < argc)
386  {
387  opts->stdin_lines = 0;
388  opts->message = argv[count];
389  }
390  else
391  return 1;
392  }
393  else if (strcmp(argv[count], "-f") == 0 || strcmp(argv[count], "--filename") == 0)
394  {
395  if (++count < argc)
396  {
397  opts->stdin_lines = 0;
398  opts->filename = argv[count];
399  }
400  else
401  return 1;
402  }
403  else if (strcmp(argv[count], "-n") == 0 || strcmp(argv[count], "--null-message") == 0)
404  {
405  opts->stdin_lines = 0;
406  opts->null_message = 1;
407  }
408  else
409  {
410  fprintf(stderr, "Unknown option %s\n", argv[count]);
411  return 1;
412  }
413  }
414  else
415  {
416  fprintf(stderr, "Unknown option %s\n", argv[count]);
417  return 1;
418  }
419 
420  count++;
421  }
422 
423  if (opts->topic == NULL)
424  return 1;
425 
426  return 0;
427 }
428 
429 
430 char* readfile(int* data_len, struct pubsub_opts* opts)
431 {
432  char* buffer = NULL;
433  long filesize = 0L;
434  FILE* infile = fopen(opts->filename, "rb");
435 
436  if (infile == NULL)
437  {
438  fprintf(stderr, "Can't open file %s\n", opts->filename);
439  return NULL;
440  }
441  fseek(infile, 0, SEEK_END);
442  filesize = ftell(infile);
443  rewind(infile);
444 
445  buffer = malloc(sizeof(char)*filesize);
446  if (buffer == NULL)
447  {
448  fprintf(stderr, "Can't allocate buffer to read file %s\n", opts->filename);
449  fclose(infile);
450  return NULL;
451  }
452  *data_len = (int)fread(buffer, 1, filesize, infile);
453  if (*data_len != filesize)
454  {
455  fprintf(stderr, "%d bytes read of %ld expected for file %s\n", *data_len, filesize, opts->filename);
456  fclose(infile);
457  free(buffer);
458  return NULL;
459  }
460 
461  fclose(infile);
462  return buffer;
463 }
464 
465 
467 {
468  int i = 0;
469 
470  for (i = 0; i < props->count; ++i)
471  {
472  int id = props->array[i].identifier;
473  const char* name = MQTTPropertyName(id);
474  char* intformat = "Property name %s value %d\n";
475 
476  switch (MQTTProperty_getType(id))
477  {
479  printf(intformat, name, props->array[i].value.byte);
480  break;
482  printf(intformat, name, props->array[i].value.integer2);
483  break;
485  printf(intformat, name, props->array[i].value.integer4);
486  break;
488  printf(intformat, name, props->array[i].value.integer4);
489  break;
492  printf("Property name %s value len %.*s\n", name,
493  props->array[i].value.data.len, props->array[i].value.data.data);
494  break;
496  printf("Property name %s key %.*s value %.*s\n", name,
497  props->array[i].value.data.len, props->array[i].value.data.data,
498  props->array[i].value.value.len, props->array[i].value.value.data);
499  break;
500  }
501  }
502 }
char * topic
Definition: pubsub_opts.h:41
char * capath
Definition: pubsub_opts.h:58
char * message
Definition: pubsub_opts.h:34
char * connection
Definition: pubsub_opts.h:49
MQTTProperties props
Definition: paho_c_pub.c:54
char * cert
Definition: pubsub_opts.h:59
char * will_topic
Definition: pubsub_opts.h:52
MQTTLenString value
char * readfile(int *data_len, struct pubsub_opts *opts)
Definition: pubsub_opts.c:430
char * delimiter
Definition: pubsub_opts.h:31
char * psk_identity
Definition: pubsub_opts.h:64
char * filename
Definition: pubsub_opts.h:35
char * name
Definition: pubsub_opts.h:69
const char * name
Definition: pubsub_opts.h:76
struct pubsub_opts opts
Definition: paho_c_pub.c:42
void usage(struct pubsub_opts *opts, pubsub_opts_nameValue *name_values, const char *program_name)
Definition: pubsub_opts.c:40
char * key
Definition: pubsub_opts.h:61
int MQTTProperty_getType(enum MQTTPropertyCodes value)
#define malloc(x)
Definition: Heap.h:41
#define free(x)
Definition: Heap.h:55
char * clientid
Definition: pubsub_opts.h:42
char * host
Definition: pubsub_opts.h:47
int printVersionInfo(pubsub_opts_nameValue *info)
Definition: pubsub_opts.c:23
constexpr size_t count()
Definition: core.h:960
char * cafile
Definition: pubsub_opts.h:60
char * ciphers
Definition: pubsub_opts.h:63
const char * MQTTPropertyName(enum MQTTPropertyCodes value)
int message_expiry
Definition: pubsub_opts.h:67
#define MQTTVERSION_3_1_1
Definition: MQTTAsync.h:203
enum MQTTPropertyCodes identifier
int MQTTVersion
Definition: pubsub_opts.h:40
const char * name
int will_retain
Definition: pubsub_opts.h:55
MQTTProperty * array
char * keypass
Definition: pubsub_opts.h:62
#define MQTTVERSION_5
Definition: MQTTAsync.h:207
void logProperties(MQTTProperties *props)
Definition: pubsub_opts.c:466
char * value
Definition: pubsub_opts.h:70
int stdin_lines
Definition: pubsub_opts.h:36
char * password
Definition: pubsub_opts.h:46
char * will_payload
Definition: pubsub_opts.h:53
char * psk
Definition: pubsub_opts.h:65
int getopts(int argc, char **argv, struct pubsub_opts *opts)
Definition: pubsub_opts.c:128
int tracelevel
Definition: pubsub_opts.h:30
#define MQTTVERSION_3_1
Definition: MQTTAsync.h:199
const char * value
Definition: pubsub_opts.h:77
enum MQTTReasonCodes rc
Definition: test10.c:1112
char * port
Definition: pubsub_opts.h:48
int null_message
Definition: pubsub_opts.h:38
int maxdatalen
Definition: pubsub_opts.h:32
struct pubsub_opts::@69 user_property
char * username
Definition: pubsub_opts.h:45


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10