Make most of functions const correct (#714)

* Make most of the functions const correct

Used clang-tidy and sonarlint to help find places where const could
pretty easily applied. Also lot of hand work.

This commit does not yet touch handlers and typedefs of those.

* Fix Arduino uno handler_who_is() has extra parenthesis

For some reason there is extra parenthesis. Remove it this is more
likely buildable.

* Bugfix/bacapp: Fix uninitilized array_index

We have changed bacapp_snprintf_value() to be const correct. After that
we got

```
/home/runner/work/bacnet-stack/bacnet-stack/src/bacnet/bacapp.c:3183:27: warning: 4th function call argument is an uninitialized value [core.CallAndMessage]
                ret_val = bacapp_snprintf_weeklyschedule(
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
```

So analyzer could now spot that we do not actually initilize array_index
at all. Fix this by setting array_index to zero. Note that I actually do
not know if zeroing is right thing to do here. I choose zero as if this
has worked before it is most likely that it will work with zero value.

* cmake: Add and ignore Wwrite-strings compiler option

Wwrite-strings helps find places where const correctness is broken.

Example it will warn about these

```C

void func1(char* str);

func("test") /* "test" is const so we should not pass it to func1().

char* func2()
{
  return "test"; /* func2() should return const char*.
}
```

We still need to ignore it as not all are fixed but let's add it already
so we remember that it should be opened at some point.

---------

Co-authored-by: Kari Argillander <kari.argillander@fidelix.com>
This commit is contained in:
Kari Argillander
2024-08-29 22:08:02 +03:00
committed by GitHub
parent 0177c59f4a
commit a2f1d6959d
408 changed files with 2608 additions and 2336 deletions
+51 -45
View File
@@ -61,7 +61,7 @@ bool datetime_ymd_is_valid(uint16_t year, uint8_t month, uint8_t day)
* @param bdate - BACnet date structure
* @return true if the date is valid
*/
bool datetime_date_is_valid(BACNET_DATE *bdate)
bool datetime_date_is_valid(const BACNET_DATE *bdate)
{
bool status = false; /* true if value date */
@@ -118,7 +118,7 @@ void datetime_day_of_year_into_date(
* @param bdate - BACnet date structure
* @return number of days since Jan 1 (inclusive) of the given year
*/
uint32_t datetime_day_of_year(BACNET_DATE *bdate)
uint32_t datetime_day_of_year(const BACNET_DATE *bdate)
{
uint32_t days = 0;
@@ -164,7 +164,7 @@ uint32_t datetime_ymd_to_days_since_epoch(
* @param bdate - BACnet date structure
* @return number of days since epoch, or 0 if out of range
*/
uint32_t datetime_days_since_epoch(BACNET_DATE *bdate)
uint32_t datetime_days_since_epoch(const BACNET_DATE *bdate)
{
uint32_t days = 0;
@@ -261,7 +261,7 @@ uint8_t datetime_day_of_week(uint16_t year, uint8_t month, uint8_t day)
* @param btime - pointer to a BACNET_TIME structure
* @return true if the time is valid
*/
bool datetime_time_is_valid(BACNET_TIME *btime)
bool datetime_time_is_valid(const BACNET_TIME *btime)
{
bool status = false;
@@ -283,7 +283,7 @@ bool datetime_time_is_valid(BACNET_TIME *btime)
*
* @return true if the date and time are valid
*/
bool datetime_is_valid(BACNET_DATE *bdate, BACNET_TIME *btime)
bool datetime_is_valid(const BACNET_DATE *bdate, const BACNET_TIME *btime)
{
return datetime_date_is_valid(bdate) && datetime_time_is_valid(btime);
}
@@ -298,7 +298,7 @@ bool datetime_is_valid(BACNET_DATE *bdate, BACNET_TIME *btime)
*
* @return -/0=same/+
*/
int datetime_compare_date(BACNET_DATE *date1, BACNET_DATE *date2)
int datetime_compare_date(const BACNET_DATE *date1, const BACNET_DATE *date2)
{
int diff = 0;
@@ -325,7 +325,7 @@ int datetime_compare_date(BACNET_DATE *date1, BACNET_DATE *date2)
*
* @return -/0/+
*/
int datetime_compare_time(BACNET_TIME *time1, BACNET_TIME *time2)
int datetime_compare_time(const BACNET_TIME *time1, const BACNET_TIME *time2)
{
int diff = 0;
@@ -355,7 +355,8 @@ int datetime_compare_time(BACNET_TIME *time1, BACNET_TIME *time2)
*
* @return -/0/+
*/
int datetime_compare(BACNET_DATE_TIME *datetime1, BACNET_DATE_TIME *datetime2)
int datetime_compare(
const BACNET_DATE_TIME *datetime1, const BACNET_DATE_TIME *datetime2)
{
int diff = 0;
@@ -367,7 +368,8 @@ int datetime_compare(BACNET_DATE_TIME *datetime1, BACNET_DATE_TIME *datetime2)
return diff;
}
int datetime_wildcard_compare_date(BACNET_DATE *date1, BACNET_DATE *date2)
int datetime_wildcard_compare_date(
const BACNET_DATE *date1, const BACNET_DATE *date2)
{
int diff = 0;
@@ -391,7 +393,8 @@ int datetime_wildcard_compare_date(BACNET_DATE *date1, BACNET_DATE *date2)
return diff;
}
int datetime_wildcard_compare_time(BACNET_TIME *time1, BACNET_TIME *time2)
int datetime_wildcard_compare_time(
const BACNET_TIME *time1, const BACNET_TIME *time2)
{
int diff = 0;
@@ -421,7 +424,7 @@ int datetime_wildcard_compare_time(BACNET_TIME *time1, BACNET_TIME *time2)
}
int datetime_wildcard_compare(
BACNET_DATE_TIME *datetime1, BACNET_DATE_TIME *datetime2)
const BACNET_DATE_TIME *datetime1, const BACNET_DATE_TIME *datetime2)
{
int diff = 0;
@@ -434,7 +437,7 @@ int datetime_wildcard_compare(
return diff;
}
void datetime_copy_date(BACNET_DATE *dest_date, BACNET_DATE *src_date)
void datetime_copy_date(BACNET_DATE *dest_date, const BACNET_DATE *src_date)
{
if (dest_date && src_date) {
dest_date->year = src_date->year;
@@ -444,7 +447,7 @@ void datetime_copy_date(BACNET_DATE *dest_date, BACNET_DATE *src_date)
}
}
void datetime_copy_time(BACNET_TIME *dest_time, BACNET_TIME *src_time)
void datetime_copy_time(BACNET_TIME *dest_time, const BACNET_TIME *src_time)
{
if (dest_time && src_time) {
dest_time->hour = src_time->hour;
@@ -455,7 +458,7 @@ void datetime_copy_time(BACNET_TIME *dest_time, BACNET_TIME *src_time)
}
void datetime_copy(
BACNET_DATE_TIME *dest_datetime, BACNET_DATE_TIME *src_datetime)
BACNET_DATE_TIME *dest_datetime, const BACNET_DATE_TIME *src_datetime)
{
datetime_copy_time(&dest_datetime->time, &src_datetime->time);
datetime_copy_date(&dest_datetime->date, &src_datetime->date);
@@ -487,7 +490,9 @@ void datetime_set_time(BACNET_TIME *btime,
}
void datetime_set(
BACNET_DATE_TIME *bdatetime, BACNET_DATE *bdate, BACNET_TIME *btime)
BACNET_DATE_TIME *bdatetime,
const BACNET_DATE *bdate,
const BACNET_TIME *btime)
{
if (bdate && btime && bdatetime) {
bdatetime->time.hour = btime->hour;
@@ -576,7 +581,7 @@ void datetime_seconds_since_midnight_into_time(
*
* @return seconds since midnight
*/
uint32_t datetime_seconds_since_midnight(BACNET_TIME *btime)
uint32_t datetime_seconds_since_midnight(const BACNET_TIME *btime)
{
uint32_t seconds = 0;
@@ -594,7 +599,7 @@ uint32_t datetime_seconds_since_midnight(BACNET_TIME *btime)
*
* @return minutes since midnight
*/
uint16_t datetime_minutes_since_midnight(BACNET_TIME *btime)
uint16_t datetime_minutes_since_midnight(const BACNET_TIME *btime)
{
uint32_t minutes = 0;
@@ -661,7 +666,7 @@ void datetime_add_minutes(BACNET_DATE_TIME *bdatetime, int32_t minutes)
* @param bdatetime [in] the starting date and time
* @return seconds since epoch
*/
bacnet_time_t datetime_seconds_since_epoch(BACNET_DATE_TIME *bdatetime)
bacnet_time_t datetime_seconds_since_epoch(const BACNET_DATE_TIME *bdatetime)
{
bacnet_time_t seconds = 0;
uint32_t days = 0;
@@ -713,7 +718,7 @@ bacnet_time_t datetime_seconds_since_epoch_max(void)
}
/* Returns true if year is a wildcard */
bool datetime_wildcard_year(BACNET_DATE *bdate)
bool datetime_wildcard_year(const BACNET_DATE *bdate)
{
bool wildcard_present = false;
@@ -735,7 +740,7 @@ void datetime_wildcard_year_set(BACNET_DATE *bdate)
}
/* Returns true if month is a wildcard */
bool datetime_wildcard_month(BACNET_DATE *bdate)
bool datetime_wildcard_month(const BACNET_DATE *bdate)
{
bool wildcard_present = false;
@@ -757,7 +762,7 @@ void datetime_wildcard_month_set(BACNET_DATE *bdate)
}
/* Returns true if day is a wildcard */
bool datetime_wildcard_day(BACNET_DATE *bdate)
bool datetime_wildcard_day(const BACNET_DATE *bdate)
{
bool wildcard_present = false;
@@ -779,7 +784,7 @@ void datetime_wildcard_day_set(BACNET_DATE *bdate)
}
/* Returns true if weekday is a wildcard */
bool datetime_wildcard_weekday(BACNET_DATE *bdate)
bool datetime_wildcard_weekday(const BACNET_DATE *bdate)
{
bool wildcard_present = false;
@@ -801,7 +806,7 @@ void datetime_wildcard_weekday_set(BACNET_DATE *bdate)
}
/* Returns true if hour is a wildcard */
bool datetime_wildcard_hour(BACNET_TIME *btime)
bool datetime_wildcard_hour(const BACNET_TIME *btime)
{
bool wildcard_present = false;
@@ -823,7 +828,7 @@ void datetime_wildcard_hour_set(BACNET_TIME *btime)
}
/* Returns true if minute is a wildcard */
bool datetime_wildcard_minute(BACNET_TIME *btime)
bool datetime_wildcard_minute(const BACNET_TIME *btime)
{
bool wildcard_present = false;
@@ -845,7 +850,7 @@ void datetime_wildcard_minute_set(BACNET_TIME *btime)
}
/* Returns true if seconds is wildcard */
bool datetime_wildcard_second(BACNET_TIME *btime)
bool datetime_wildcard_second(const BACNET_TIME *btime)
{
bool wildcard_present = false;
@@ -867,7 +872,7 @@ void datetime_wildcard_second_set(BACNET_TIME *btime)
}
/* Returns true if hundredths is a wildcard */
bool datetime_wildcard_hundredths(BACNET_TIME *btime)
bool datetime_wildcard_hundredths(const BACNET_TIME *btime)
{
bool wildcard_present = false;
@@ -888,7 +893,7 @@ void datetime_wildcard_hundredths_set(BACNET_TIME *btime)
}
}
bool datetime_wildcard(BACNET_DATE_TIME *bdatetime)
bool datetime_wildcard(const BACNET_DATE_TIME *bdatetime)
{
bool wildcard_present = false;
@@ -912,7 +917,7 @@ bool datetime_wildcard(BACNET_DATE_TIME *bdatetime)
* on it's own. Also checks for special day and month values. Used in
* trendlog object.
*/
bool datetime_wildcard_present(BACNET_DATE_TIME *bdatetime)
bool datetime_wildcard_present(const BACNET_DATE_TIME *bdatetime)
{
bool wildcard_present = false;
@@ -970,7 +975,7 @@ void datetime_wildcard_set(BACNET_DATE_TIME *bdatetime)
* @return true if the time is converted
*/
bool datetime_utc_to_local(BACNET_DATE_TIME *local_time,
BACNET_DATE_TIME *utc_time,
const BACNET_DATE_TIME *utc_time,
int16_t utc_offset_minutes,
int8_t dst_adjust_minutes)
{
@@ -1002,7 +1007,7 @@ bool datetime_utc_to_local(BACNET_DATE_TIME *local_time,
* @return true if the time is converted
*/
bool datetime_local_to_utc(BACNET_DATE_TIME *utc_time,
BACNET_DATE_TIME *local_time,
const BACNET_DATE_TIME *local_time,
int16_t utc_offset_minutes,
int8_t dst_adjust_minutes)
{
@@ -1033,7 +1038,7 @@ bool datetime_local_to_utc(BACNET_DATE_TIME *utc_time,
* @param value The value to be encoded.
* @return the number of apdu bytes encoded
*/
int bacapp_encode_datetime(uint8_t *apdu, BACNET_DATE_TIME *value)
int bacapp_encode_datetime(uint8_t *apdu, const BACNET_DATE_TIME *value)
{
int len = 0;
int apdu_len = 0;
@@ -1059,7 +1064,7 @@ int bacapp_encode_datetime(uint8_t *apdu, BACNET_DATE_TIME *value)
* @return the number of apdu bytes encoded
*/
int bacapp_encode_context_datetime(
uint8_t *apdu, uint8_t tag_number, BACNET_DATE_TIME *value)
uint8_t *apdu, uint8_t tag_number, const BACNET_DATE_TIME *value)
{
int len = 0;
int apdu_len = 0;
@@ -1090,7 +1095,7 @@ int bacapp_encode_context_datetime(
* @return length of the APDU buffer decoded, or BACNET_STATUS_ERROR
*/
int bacnet_datetime_decode(
uint8_t *apdu, uint32_t apdu_size, BACNET_DATE_TIME *value)
const uint8_t *apdu, uint32_t apdu_size, BACNET_DATE_TIME *value)
{
int len = 0;
int apdu_len = 0;
@@ -1119,7 +1124,7 @@ int bacnet_datetime_decode(
return apdu_len;
}
int bacapp_decode_datetime(uint8_t *apdu, BACNET_DATE_TIME *value)
int bacapp_decode_datetime(const uint8_t *apdu, BACNET_DATE_TIME *value)
{
return bacnet_datetime_decode(apdu, MAX_APDU, value);
}
@@ -1132,7 +1137,7 @@ int bacapp_decode_datetime(uint8_t *apdu, BACNET_DATE_TIME *value)
* @param value - parameter to store the value after decoding
* @return length of the APDU buffer decoded, or BACNET_STATUS_ERROR
*/
int bacnet_datetime_context_decode(uint8_t *apdu,
int bacnet_datetime_context_decode(const uint8_t *apdu,
uint32_t apdu_size,
uint8_t tag_number,
BACNET_DATE_TIME *value)
@@ -1168,7 +1173,7 @@ int bacnet_datetime_context_decode(uint8_t *apdu,
* @deprecated - use bacnet_datetime_context_decode() instead
*/
int bacapp_decode_context_datetime(
uint8_t *apdu, uint8_t tag_number, BACNET_DATE_TIME *value)
const uint8_t *apdu, uint8_t tag_number, BACNET_DATE_TIME *value)
{
return bacnet_datetime_context_decode(apdu, MAX_APDU, tag_number, value);
}
@@ -1179,8 +1184,9 @@ int bacapp_decode_context_datetime(
* @param value2 - complex data value 2 structure
* @return true if the two complex data values are the same
*/
bool bacnet_daterange_same(BACNET_DATE_RANGE *value1,
BACNET_DATE_RANGE *value2)
bool bacnet_daterange_same(
const BACNET_DATE_RANGE *value1,
const BACNET_DATE_RANGE *value2)
{
bool status = false;
@@ -1206,7 +1212,7 @@ bool bacnet_daterange_same(BACNET_DATE_RANGE *value1,
* @param value - value to encode
* @return number of bytes emitted, BACNET_STATUS_ERROR on error
*/
int bacnet_daterange_encode(uint8_t *apdu, BACNET_DATE_RANGE *value)
int bacnet_daterange_encode(uint8_t *apdu, const BACNET_DATE_RANGE *value)
{
int len = 0;
int apdu_len = 0;
@@ -1237,7 +1243,7 @@ int bacnet_daterange_encode(uint8_t *apdu, BACNET_DATE_RANGE *value)
* @return number of bytes emitted, BACNET_STATUS_ERROR on error
*/
int bacnet_daterange_decode(
uint8_t *apdu, uint32_t apdu_size, BACNET_DATE_RANGE *value)
const uint8_t *apdu, uint32_t apdu_size, BACNET_DATE_RANGE *value)
{
int len = 0;
int apdu_len = 0;
@@ -1269,7 +1275,7 @@ int bacnet_daterange_decode(
* @return number of bytes decoded, or BACNET_STATUS_ERROR on error
*/
int bacnet_daterange_context_encode(
uint8_t *apdu, uint8_t tag_number, BACNET_DATE_RANGE *value)
uint8_t *apdu, uint8_t tag_number, const BACNET_DATE_RANGE *value)
{
int len = 0;
int apdu_len = 0;
@@ -1303,7 +1309,7 @@ int bacnet_daterange_context_encode(
* @param value - value to encode
* @return number of bytes decoded, BACNET_STATUS_ERROR on error
*/
int bacnet_daterange_context_decode(uint8_t *apdu,
int bacnet_daterange_context_decode(const uint8_t *apdu,
uint32_t apdu_size,
uint8_t tag_number,
BACNET_DATE_RANGE *value)
@@ -1370,7 +1376,7 @@ bool datetime_date_init_ascii(BACNET_DATE *bdate, const char *ascii)
* @param str_size - size of the string, or 0 for length only
* @return number of characters printed
*/
int datetime_date_to_ascii(BACNET_DATE *bdate, char *str, size_t str_size)
int datetime_date_to_ascii(const BACNET_DATE *bdate, char *str, size_t str_size)
{
int str_len = 0;
@@ -1431,7 +1437,7 @@ bool datetime_time_init_ascii(BACNET_TIME *btime, const char *ascii)
* @param str_size - size of the string, or 0 for length only
* @return number of characters printed
*/
int datetime_time_to_ascii(BACNET_TIME *btime, char *str, size_t str_size)
int datetime_time_to_ascii(const BACNET_TIME *btime, char *str, size_t str_size)
{
int str_len = 0;
@@ -1484,7 +1490,7 @@ bool datetime_init_ascii(BACNET_DATE_TIME *bdatetime, const char *ascii)
* @param str_size - size of the string, or 0 for length only
* @return number of characters printed
*/
int datetime_to_ascii(BACNET_DATE_TIME *bdatetime, char *str, size_t str_size)
int datetime_to_ascii(const BACNET_DATE_TIME *bdatetime, char *str, size_t str_size)
{
int str_len = 0;