MQTTVersion.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 API and implementation and/or initial documentation
15  *******************************************************************************/
16 
17 #include <stdio.h>
18 
19 #if !defined(_WRS_KERNEL)
20 
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <ctype.h>
27 #include "MQTTAsync.h"
28 
29 #if defined(_WIN32) || defined(_WIN64)
30 #include <windows.h>
31 #include <tchar.h>
32 #include <io.h>
33 #include <sys/stat.h>
34 #else
35 #include <dlfcn.h>
36 #include <sys/mman.h>
37 #include <unistd.h>
38 #endif
39 
40 
56  static const char* libraries[] = {"paho-mqtt3c", "paho-mqtt3cs", "paho-mqtt3a", "paho-mqtt3as"};
57  static const char* eyecatchers[] = {"MQTTAsyncV3_Version", "MQTTAsyncV3_Timestamp",
58  "MQTTClientV3_Version", "MQTTClientV3_Timestamp"};
59 
60 
61 char* FindString(char* filename, const char* eyecatcher_input);
63 int loadandcall(const char* libname);
64 void printEyecatchers(char* filename);
65 
66 
73 char* FindString(char* filename, const char* eyecatcher_input)
74 {
75  FILE* infile = NULL;
76  static char value[100];
77  const char* eyecatcher = eyecatcher_input;
78 
79  memset(value, 0, 100);
80  if ((infile = fopen(filename, "rb")) != NULL)
81  {
82  size_t buflen = strlen(eyecatcher);
83  char* buffer = (char*) malloc(buflen + 1); /* added space for unused null terminator to stop LGTM complaint */
84 
85  if (buffer != NULL)
86  {
87  int c = fgetc(infile);
88 
89  while (feof(infile) == 0)
90  {
91  int count = 0;
92  buffer[count++] = c;
93  if (memcmp(eyecatcher, buffer, buflen) == 0)
94  {
95  char* ptr = value;
96  c = fgetc(infile); /* skip space */
97  c = fgetc(infile);
98  while (isprint(c))
99  {
100  *ptr++ = c;
101  c = fgetc(infile);
102  }
103  break;
104  }
105  if (count == buflen)
106  {
107  memmove(buffer, &buffer[1], buflen - 1);
108  count--;
109  }
110  c = fgetc(infile);
111  }
112  free(buffer);
113  }
114 
115  fclose(infile);
116  }
117  return value;
118 }
119 
120 
122 {
123  int rc = 0;
124 
125  while (info->name)
126  {
127  printf("%s: %s\n", info->name, info->value);
128  info++;
129  rc = 1; /* at least one value printed */
130  }
131  return rc;
132 }
133 
134 typedef MQTTAsync_nameValue* (*func_type)(void);
135 
136 int loadandcall(const char* libname)
137 {
138  int rc = 0;
139  MQTTAsync_nameValue* (*func_address)(void) = NULL;
140 #if defined(_WIN32) || defined(_WIN64)
141  HMODULE APILibrary = LoadLibraryA(libname);
142  if (APILibrary == NULL)
143  printf("Error loading library %s, error code %d\n", libname, GetLastError());
144  else
145  {
146  func_address = (func_type)GetProcAddress(APILibrary, "MQTTAsync_getVersionInfo");
147  if (func_address == NULL)
148  func_address = (func_type)GetProcAddress(APILibrary, "MQTTClient_getVersionInfo");
149  if (func_address)
150  rc = printVersionInfo((*func_address)());
151  FreeLibrary(APILibrary);
152  }
153 #else
154  void* APILibrary = dlopen(libname, RTLD_LAZY); /* Open the Library in question */
155  if (APILibrary == NULL)
156  printf("Error loading library %s, error %s\n", libname, dlerror());
157  else
158  {
159  *(void **) (&func_address) = dlsym(APILibrary, "MQTTAsync_getVersionInfo");
160  if (func_address == NULL)
161  func_address = dlsym(APILibrary, "MQTTClient_getVersionInfo");
162  if (func_address)
163  rc = printVersionInfo((*func_address)());
164  dlclose(APILibrary);
165  }
166 #endif
167  return rc;
168 }
169 
170 
171 #if !defined(ARRAY_SIZE)
172 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
173 #endif
174 
175 void printEyecatchers(char* filename)
176 {
177  int i = 0;
178 
179  for (i = 0; i < ARRAY_SIZE(eyecatchers); ++i)
180  {
181  char* value = FindString(filename, eyecatchers[i]);
182  if (value[0])
183  printf("%s: %s\n", eyecatchers[i], value);
184  }
185 }
186 
187 
188 int main(int argc, char** argv)
189 {
190  printf("MQTTVersion: print the version strings of an MQTT client library\n");
191  printf("Copyright (c) 2012, 2018 IBM Corp.\n");
192 
193  if (argc == 1)
194  {
195  int i = 0;
196  char namebuf[60];
197 
198  printf("Specify a particular library name if it is not in the current directory, or not executable on this platform\n");
199 
200  for (i = 0; i < ARRAY_SIZE(libraries); ++i)
201  {
202 #if defined(__CYGWIN__)
203  sprintf(namebuf, "cyg%s-1.dll", libraries[i]);
204 #elif defined(_WIN32) || defined(_WIN64)
205  sprintf(namebuf, "%s.dll", libraries[i]);
206 #elif defined(OSX)
207  sprintf(namebuf, "lib%s.1.dylib", libraries[i]);
208 #else
209  sprintf(namebuf, "lib%s.so.1", libraries[i]);
210 #endif
211  printf("--- Trying library %s ---\n", libraries[i]);
212  if (!loadandcall(namebuf))
213  printEyecatchers(namebuf);
214  }
215  }
216  else
217  {
218  if (!loadandcall(argv[1]))
219  printEyecatchers(argv[1]);
220  }
221 
222  return 0;
223 }
224 #else
225 int main(void)
226 {
227  fprintf(stderr, "This tool is not supported on this platform yet.\n");
228  return 1;
229 }
230 #endif /* !defined(_WRS_KERNEL) */
int loadandcall(const char *libname)
Definition: MQTTVersion.c:136
enum MQTTPropertyCodes value
const char * name
Definition: MQTTAsync.h:1149
#define malloc(x)
Definition: Heap.h:41
void printEyecatchers(char *filename)
Definition: MQTTVersion.c:175
char * FindString(char *filename, const char *eyecatcher_input)
Definition: MQTTVersion.c:73
#define free(x)
Definition: Heap.h:55
constexpr size_t count()
Definition: core.h:960
int printVersionInfo(MQTTAsync_nameValue *info)
Definition: MQTTVersion.c:121
j template void())
Definition: json.hpp:3707
MQTTAsync_nameValue *(* func_type)(void)
Definition: MQTTVersion.c:134
const char * value
Definition: MQTTAsync.h:1150
int main(int argc, char **argv)
Definition: MQTTVersion.c:188
MQTTClient c
Definition: test10.c:1656
const void * ptr(const T *p)
Definition: format.h:3610
#define ARRAY_SIZE(a)
Definition: MQTTVersion.c:172
static const char * eyecatchers[]
Definition: MQTTVersion.c:57
enum MQTTReasonCodes rc
Definition: test10.c:1112
static eyecatcherType eyecatcher
Definition: Heap.c:61
static const char * libraries[]
Definition: MQTTVersion.c:56


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