sbgVersion.c
Go to the documentation of this file.
1 #include "sbgVersion.h"
2 
3 //----------------------------------------------------------------------//
4 //- Version encoding / decoding methods -//
5 //----------------------------------------------------------------------//
6 
12 SBG_COMMON_LIB_API void sbgVersionDecode(uint32_t encodedVersion, SbgVersion *pVersionInfo)
13 {
14  //
15  // Check input parameters
16  //
17  assert(pVersionInfo);
18 
19  //
20  // Test if we have a software version scheme
21  //
22  if (encodedVersion&SBG_VERSION_SOFT_SCHEME)
23  {
24  //
25  // We have a software scheme, decode it
26  //
27  pVersionInfo->softwareScheme = true;
28 
29  //
30  // Decode the software scheme fields
31  //
36 
37  //
38  // Set the revision to zero as it's not used
39  //
40  pVersionInfo->rev = 0;
41  }
42  else
43  {
44  //
45  // We have a basic scheme, decode it
46  //
47  pVersionInfo->softwareScheme = false;
48 
49  //
50  // Decode the software scheme fields
51  //
52  pVersionInfo->major = (encodedVersion >> 24) & 0xFF;
53  pVersionInfo->minor = (encodedVersion >> 16) & 0xFF;
54  pVersionInfo->rev = (encodedVersion >> 8) & 0xFF;
55  pVersionInfo->build = (encodedVersion >> 0) & 0xFF;
56 
57  //
58  // Set the qualifier to zero
59  //
60  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_DEV;
61  }
62 }
63 
69 SBG_COMMON_LIB_API uint32_t sbgVersionEncode(const SbgVersion *pVersionInfo)
70 {
71  uint32_t encodedVersion = 0;
72 
73  //
74  // Check input parameter
75  //
76  assert(pVersionInfo);
77 
78  //
79  // Test if we have a software scheme or a basic one
80  //
81  if (pVersionInfo->softwareScheme)
82  {
83  //
84  // We have a software, scheme, so test that the version is valid
85  //
86  assert((pVersionInfo->major <= 63) && (pVersionInfo->minor <= 63) && (pVersionInfo->rev == 0));
87 
88  //
89  // Indicate that we have a software version scheme
90  //
91  encodedVersion = SBG_VERSION_SOFTWARE(pVersionInfo->major, pVersionInfo->minor, pVersionInfo->build, pVersionInfo->qualifier);
92  }
93  else
94  {
95  //
96  // We have a basic version scheme so check parameter validty
97  //
98  assert(pVersionInfo->major <= 127);
99 
100  //
101  // Encode the basic version information
102  //
103  encodedVersion = SBG_VERSION_BASIC(pVersionInfo->major, pVersionInfo->minor, pVersionInfo->rev, pVersionInfo->build);
104  }
105 
106  return encodedVersion;
107 }
108 
109 //----------------------------------------------------------------------//
110 //- Version comparaison methods -//
111 //----------------------------------------------------------------------//
112 
123 SBG_COMMON_LIB_API int32_t sbgVersionCompare(const SbgVersion *pVersionA, const SbgVersion *pVersionB, SbgVersionCmpThresold thresold)
124 {
125  int32_t result;
126 
127  //
128  // Check input parameter
129  //
130  assert((pVersionA) && (pVersionB));
131 
132  //
133  // Check that the two versions are using the same scheme
134  //
135  assert(pVersionA->softwareScheme == pVersionB->softwareScheme);
136 
137  //
138  // Do the comparaison according to the selected thresold
139  // Start by compairing the major field
140  //
141  result = pVersionA->major - pVersionB->major;
142 
143  //
144  // Test if we have to also compare the minor field
145  //
146  if ( (result == 0) && ((thresold == SBG_VERSION_CMP_THRESOLD_MINOR) || (thresold == SBG_VERSION_CMP_THRESOLD_REVISION) || (thresold == SBG_VERSION_CMP_THRESOLD_BUILD) || (thresold == SBG_VERSION_CMP_THRESOLD_QUALIFIER)) )
147  {
148  //
149  // Update the result using the minor indication
150  //
151  result = pVersionA->minor - pVersionB->minor;
152 
153  //
154  // Test if we have to also compare the revision field (for basic version only)
155  //
156  if ( (result == 0) && ((thresold == SBG_VERSION_CMP_THRESOLD_REVISION) || (thresold == SBG_VERSION_CMP_THRESOLD_BUILD) || (thresold == SBG_VERSION_CMP_THRESOLD_QUALIFIER)) )
157  {
158  //
159  // Test if we have a software scheme or a basic scheme version
160  //
161  if (pVersionA->softwareScheme)
162  {
163  //
164  // We have a software scheme so set the result to 0
165  //
166  result = 0;
167  }
168  else
169  {
170  //
171  // We have a basic scheme so we can compare the revision field
172  //
173  result = pVersionA->rev - pVersionB->rev;
174  }
175 
176  //
177  // Test if we have to also compare the build field
178  //
179  if ( (result == 0) && ((thresold == SBG_VERSION_CMP_THRESOLD_BUILD) || (thresold == SBG_VERSION_CMP_THRESOLD_QUALIFIER)) )
180  {
181  //
182  // Compare the build field
183  //
184  result = pVersionA->build - pVersionB->build;
185 
186  //
187  // Test if we have to also compare the qualifier field
188  //
189  if ( (result == 0) && (thresold == SBG_VERSION_CMP_THRESOLD_QUALIFIER) )
190  {
191  //
192  // Test if we have a software scheme
193  //
194  if (pVersionA->softwareScheme)
195  {
196  //
197  // We have a software scheme so set the result to 0
198  //
199  result = pVersionA->qualifier - pVersionB->qualifier;
200  }
201  else
202  {
203  //
204  // We have a basic scheme so set the result to 0
205  //
206  result = 0;
207  }
208  }
209  }
210  }
211  }
212 
213  return result;
214 }
215 
226 SBG_COMMON_LIB_API int32_t sbgVersionCompareEncoded(uint32_t versionA, uint32_t versionB, SbgVersionCmpThresold thresold)
227 {
228  SbgVersion versionAInfo;
229  SbgVersion versionBInfo;
230 
231  //
232  // Decode the versions
233  //
234  sbgVersionDecode(versionA, &versionAInfo);
235  sbgVersionDecode(versionB, &versionBInfo);
236 
237  //
238  // Do the comparaison
239  //
240  return sbgVersionCompare(&versionAInfo, &versionBInfo, thresold);
241 }
242 
254 SBG_COMMON_LIB_API int32_t sbgVersionIsWithinRange(const SbgVersion *pLowerVersion, const SbgVersion *pHigherVersion, const SbgVersion *pVersion)
255 {
256  assert(pLowerVersion);
257  assert(pHigherVersion);
258  assert(pVersion);
259 
260  //
261  // Use the encoded version to speed up the comparaison
262  //
263  return sbgVersionIsWithinRangeEncoded(sbgVersionEncode(pLowerVersion), sbgVersionEncode(pHigherVersion), sbgVersionEncode(pVersion));
264 }
265 
277 SBG_COMMON_LIB_API int32_t sbgVersionIsWithinRangeEncoded(uint32_t lowerVersion, uint32_t higherVersion, uint32_t version)
278 {
279  //
280  // Make sure that all versions are using the same scheme
281  //
282  assert(lowerVersion <= higherVersion);
283  assert(( (sbgVersionIsUsingSoftwareScheme(lowerVersion) == sbgVersionIsUsingSoftwareScheme(higherVersion)) &&
285  ( (sbgVersionIsUsingSoftwareScheme(lowerVersion) != sbgVersionIsUsingSoftwareScheme(higherVersion)) &&
287 
288  //
289  // We can compare safely the encoded version directly
290  //
291  if (version < lowerVersion)
292  {
293  return -1;
294  }
295  else if (version > higherVersion)
296  {
297  return 1;
298  }
299  else
300  {
301  return 0;
302  }
303 }
304 
305 //----------------------------------------------------------------------//
306 //- Version to string methods -//
307 //----------------------------------------------------------------------//
308 
317 SBG_COMMON_LIB_API SbgErrorCode sbgVersionToString(const SbgVersion *pVersionInfo, char *pBuffer, uint32_t sizeOfBuffer)
318 {
319  SbgErrorCode errorCode = SBG_NO_ERROR;
320 
321  //
322  // Check input parameters
323  //
324  assert((pVersionInfo) && (pBuffer) && (sizeOfBuffer>0));
325 
326  //
327  // Test if we have a basic or software version scheme
328  //
329  if (pVersionInfo->softwareScheme)
330  {
331  //
332  // We have a software version scheme
333  // Test that the buffer is big enough to store the generated string (31.31.65535-hotfix)
334  //
335  if (sizeOfBuffer >= 19)
336  {
337  //
338  // Generate the string version
339  //
340  sprintf(pBuffer, "%u.%u.%u-", pVersionInfo->major, pVersionInfo->minor, pVersionInfo->build);
341 
342  //
343  // Append the qualifier
344  //
345  switch (pVersionInfo->qualifier)
346  {
348  strcat(pBuffer, "dev");
349  break;
351  strcat(pBuffer, "alpha");
352  break;
354  strcat(pBuffer, "beta");
355  break;
357  strcat(pBuffer, "rc");
358  break;
360  strcat(pBuffer, "stable");
361  break;
363  strcat(pBuffer, "hotfix");
364  break;
365  default:
366  break;
367  }
368  }
369  else
370  {
371  //
372  // Output buffer is to small
373  //
374  errorCode = SBG_BUFFER_OVERFLOW;
375  }
376 
377  }
378  else
379  {
380  //
381  // We have a basic version scheme, generate the string
382  // Test that the buffer is big enough to store the generated string
383  //
384  if (sizeOfBuffer >= 16)
385  {
386  //
387  // Generate the string version
388  //
389  sprintf(pBuffer, "%u.%u.%u.%u", pVersionInfo->major, pVersionInfo->minor, pVersionInfo->rev, pVersionInfo->build);
390  }
391  else
392  {
393  //
394  // Output buffer is to small
395  //
396  errorCode = SBG_BUFFER_OVERFLOW;
397  }
398  }
399 
400  //
401  // Return status of operation
402  //
403  return errorCode;
404 }
405 
414 SBG_COMMON_LIB_API SbgErrorCode sbgVersionToStringEncoded(uint32_t version, char *pBuffer, uint32_t sizeOfBuffer)
415 {
416  SbgVersion versionInfo;
417 
418  //
419  // Decode the versions
420  //
421  sbgVersionDecode(version, &versionInfo);
422 
423  //
424  // Return the version as a string
425  //
426  return sbgVersionToString(&versionInfo, pBuffer, sizeOfBuffer);
427 }
428 
429 //----------------------------------------------------------------------//
430 //- String to version methods -//
431 //----------------------------------------------------------------------//
432 
439 SBG_COMMON_LIB_API SbgErrorCode sbgVersionFromString(const char *pVersionStr, SbgVersion *pVersionInfo)
440 {
441  SbgErrorCode errorCode = SBG_NO_ERROR;
442  char qualifierStr[32];
443  uint32_t major;
444  uint32_t minor;
445  uint32_t rev;
446  uint32_t build;
447 
448  //
449  // Check input parameters
450  //
451  assert((pVersionStr) && (pVersionInfo));
452 
453  //
454  // Zero init the returned version struct
455  //
456  memset(pVersionInfo, 0x00, sizeof(SbgVersion));
457 
458  //
459  // Try to parse a basic version
460  //
461  if (sscanf(pVersionStr, "%u.%u.%u.%u", &major, &minor, &rev, &build) == 4)
462  {
463  //
464  // We have read successfully a basic version
465  //
466  pVersionInfo->softwareScheme = false;
467 
468  //
469  // Fill the version numbers
470  //
471  pVersionInfo->major = (uint8_t)major;
472  pVersionInfo->minor = (uint8_t)minor;
473  pVersionInfo->rev = (uint8_t)rev;
474  pVersionInfo->build = (uint8_t)build;
475  }
476  else if (sscanf(pVersionStr, "%u.%u.%u-%s", &major, &minor, &build, qualifierStr) == 4)
477  {
478  //
479  // We have read successfully a software scheme version
480  //
481  pVersionInfo->softwareScheme = true;
482 
483  //
484  // Fill the version numbers
485  //
486  pVersionInfo->major = (uint8_t)major;
487  pVersionInfo->minor = (uint8_t)minor;
488  pVersionInfo->build = (uint16_t)build;
489 
490  //
491  // Also convert the qualifier
492  //
493  if (!strcmp(qualifierStr, "dev"))
494  {
495  //
496  // Dev qualifier
497  //
498  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_DEV;
499  }
500  else if (!strcmp(qualifierStr, "alpha"))
501  {
502  //
503  // Alpha qualifier
504  //
505  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_ALPHA;
506  }
507  else if (!strcmp(qualifierStr, "beta"))
508  {
509  //
510  // Beta qualifier
511  //
512  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_BETA;
513  }
514  else if (!strcmp(qualifierStr, "rc"))
515  {
516  //
517  // Release Candidate qualifier
518  //
519  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_RC;
520  }
521  else if (!strcmp(qualifierStr, "stable"))
522  {
523  //
524  // Stable qualifier
525  //
526  pVersionInfo->qualifier = SBG_VERSION_QUALIFIER_STABLE;
527  }
528  else if (!strcmp(qualifierStr, "hotfix"))
529  {
530  //
531  // Hot Fix qualifier
532  //
534  }
535  else
536  {
537  //
538  // Unknown qualifier
539  //
540  errorCode = SBG_INVALID_PARAMETER;
541  }
542  }
543  else
544  {
545  //
546  // Invalid format
547  //
548  errorCode = SBG_INVALID_PARAMETER;
549  }
550 
551  return errorCode;
552 }
553 
560 SBG_COMMON_LIB_API SbgErrorCode sbgVersionFromStringEncoded(const char *pVersionStr, uint32_t *pVersion)
561 {
562  SbgErrorCode errorCode = SBG_NO_ERROR;
563  SbgVersion versionInfo;
564 
565  //
566  // Check input parameters
567  //
568  assert((pVersionStr) && (pVersion));
569 
570  //
571  // Parse the version from a string
572  //
573  errorCode = sbgVersionFromString(pVersionStr, &versionInfo);
574 
575  //
576  // Test that no error has occurred
577  //
578  if (errorCode == SBG_NO_ERROR)
579  {
580  //
581  // Encode the version and return it
582  //
583  *pVersion = sbgVersionEncode(&versionInfo);
584  }
585  else
586  {
587  //
588  // An error has occurred so return a zero version
589  //
590  *pVersion = 0;
591  }
592 
593  //
594  // Return error
595  //
596  return errorCode;
597 }
SBG_COMMON_LIB_API SbgErrorCode sbgVersionFromStringEncoded(const char *pVersionStr, uint32_t *pVersion)
Definition: sbgVersion.c:560
Helper methods and definitions used to handle version.
SBG_COMMON_LIB_API SbgErrorCode sbgVersionToStringEncoded(uint32_t version, char *pBuffer, uint32_t sizeOfBuffer)
Definition: sbgVersion.c:414
#define SBG_COMMON_LIB_API
Definition: sbgDefines.h:58
#define SBG_VERSION_SOFT_SCHEME_QUALIFIER_MASK
Definition: sbgVersion.h:51
#define SBG_VERSION_SOFT_SCHEME_BUILD_SHIFT
Definition: sbgVersion.h:61
SBG_INLINE bool sbgVersionIsUsingSoftwareScheme(uint32_t encodedVersion)
Definition: sbgVersion.h:164
#define SBG_VERSION_SOFT_SCHEME_MINOR_SHIFT
Definition: sbgVersion.h:58
SBG_COMMON_LIB_API SbgErrorCode sbgVersionFromString(const char *pVersionStr, SbgVersion *pVersionInfo)
Definition: sbgVersion.c:439
enum _SbgVersionCmpThresold SbgVersionCmpThresold
#define SBG_VERSION_SOFT_SCHEME_MINOR_MASK
Definition: sbgVersion.h:57
SBG_COMMON_LIB_API int32_t sbgVersionCompareEncoded(uint32_t versionA, uint32_t versionB, SbgVersionCmpThresold thresold)
Definition: sbgVersion.c:226
SBG_COMMON_LIB_API int32_t sbgVersionIsWithinRange(const SbgVersion *pLowerVersion, const SbgVersion *pHigherVersion, const SbgVersion *pVersion)
Definition: sbgVersion.c:254
uint8_t major
Definition: sbgVersion.h:100
bool softwareScheme
Definition: sbgVersion.h:98
uint16_t build
Definition: sbgVersion.h:103
SBG_COMMON_LIB_API int32_t sbgVersionCompare(const SbgVersion *pVersionA, const SbgVersion *pVersionB, SbgVersionCmpThresold thresold)
Definition: sbgVersion.c:123
SBG_COMMON_LIB_API SbgErrorCode sbgVersionToString(const SbgVersion *pVersionInfo, char *pBuffer, uint32_t sizeOfBuffer)
Definition: sbgVersion.c:317
#define SBG_VERSION_SOFT_SCHEME_MAJOR_SHIFT
Definition: sbgVersion.h:55
enum _SbgVersionQualifier SbgVersionQualifier
#define SBG_VERSION_SOFT_SCHEME
Definition: sbgVersion.h:49
SBG_COMMON_LIB_API uint32_t sbgVersionEncode(const SbgVersion *pVersionInfo)
Definition: sbgVersion.c:69
SBG_COMMON_LIB_API int32_t sbgVersionIsWithinRangeEncoded(uint32_t lowerVersion, uint32_t higherVersion, uint32_t version)
Definition: sbgVersion.c:277
uint8_t rev
Definition: sbgVersion.h:102
#define SBG_VERSION_SOFTWARE(major, minor, build, qualifier)
Definition: sbgVersion.h:131
uint8_t minor
Definition: sbgVersion.h:101
#define SBG_VERSION_SOFT_SCHEME_BUILD_MASK
Definition: sbgVersion.h:60
enum _SbgErrorCode SbgErrorCode
#define SBG_VERSION_BASIC(major, minor, rev, build)
Definition: sbgVersion.h:118
#define SBG_VERSION_SOFT_SCHEME_MAJOR_MASK
Definition: sbgVersion.h:54
SbgVersionQualifier qualifier
Definition: sbgVersion.h:99
SBG_COMMON_LIB_API void sbgVersionDecode(uint32_t encodedVersion, SbgVersion *pVersionInfo)
Definition: sbgVersion.c:12
#define SBG_VERSION_SOFT_SCHEME_QUALIFIER_SHIFT
Definition: sbgVersion.h:52


sbg_driver
Author(s): SBG Systems
autogenerated on Sat Sep 3 2022 02:53:36