Added max_points in .ini, increased spiff csv file character limit, and file name limit. Added more prints for how many devices you can configure based on current memory size and max_points.
This commit is contained in:
@@ -41,6 +41,17 @@ static bool str_equals(const char *a, const char *b)
|
||||
return strcmp(a, b) == 0;
|
||||
}
|
||||
|
||||
static bool line_starts_with_valid_point_type(const char *line)
|
||||
{
|
||||
if (line == NULL)
|
||||
return false;
|
||||
|
||||
return strncmp(line, "Coil,", 5U) == 0 ||
|
||||
strncmp(line, "Discrete_Input,", 15U) == 0 ||
|
||||
strncmp(line, "Input_Register,", 15U) == 0 ||
|
||||
strncmp(line, "Holding_Register,", 17U) == 0;
|
||||
}
|
||||
|
||||
static modbus_point_type_t parse_point_type(const char *s)
|
||||
{
|
||||
if (str_equals(s, "Coil"))
|
||||
@@ -413,7 +424,7 @@ static bool append_helper_points(modbus_db_t *db, const modbus_point_t *base_pt)
|
||||
helper.reg_value = 0U;
|
||||
|
||||
|
||||
if (db->count >= MODBUS_MAX_POINTS)
|
||||
if (db->count >= db->capacity)
|
||||
return false;
|
||||
|
||||
|
||||
@@ -602,7 +613,12 @@ static bool parse_csv_line(const char *line_in, modbus_point_t *pt)
|
||||
|
||||
void modbus_points_init(modbus_db_t *db)
|
||||
{
|
||||
modbus_points_reset(db);
|
||||
if (db == NULL)
|
||||
return;
|
||||
|
||||
db->points = NULL;
|
||||
db->count = 0;
|
||||
db->capacity = 0;
|
||||
}
|
||||
|
||||
void modbus_points_reset(modbus_db_t *db)
|
||||
@@ -610,10 +626,17 @@ void modbus_points_reset(modbus_db_t *db)
|
||||
if (db == NULL)
|
||||
return;
|
||||
|
||||
memset(db, 0, sizeof(*db));
|
||||
if (db->points != NULL)
|
||||
{
|
||||
free(db->points);
|
||||
db->points = NULL;
|
||||
}
|
||||
|
||||
db->count = 0;
|
||||
db->capacity = 0;
|
||||
}
|
||||
|
||||
bool modbus_points_load(modbus_db_t *db, const char *filename)
|
||||
bool modbus_points_load(modbus_db_t *db, const char *filename, size_t max_points)
|
||||
{
|
||||
FILE *fp;
|
||||
char line[MODBUS_MAX_CSV_LINE_LEN];
|
||||
@@ -629,8 +652,26 @@ bool modbus_points_load(modbus_db_t *db, const char *filename)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (max_points == 0U)
|
||||
{
|
||||
printf("CSV ERROR: max_points cannot be 0\n");
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
|
||||
modbus_points_reset(db);
|
||||
|
||||
db->capacity = max_points;
|
||||
db->points = calloc(db->capacity, sizeof(modbus_point_t));
|
||||
if (db->points == NULL)
|
||||
{
|
||||
printf("CSV ERROR: failed to allocate point table (%u points, %u bytes)\n",
|
||||
(unsigned)max_points,
|
||||
(unsigned)(max_points * sizeof(modbus_point_t)));
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
|
||||
while (fgets(line, sizeof(line), fp) != NULL)
|
||||
{
|
||||
modbus_point_t pt;
|
||||
@@ -654,53 +695,21 @@ bool modbus_points_load(modbus_db_t *db, const char *filename)
|
||||
if (line_num == 1U)
|
||||
continue;
|
||||
|
||||
if (!line_starts_with_valid_point_type(line))
|
||||
{
|
||||
char temp_line[MODBUS_MAX_CSV_LINE_LEN];
|
||||
char *fields[8];
|
||||
size_t field_count = 0;
|
||||
char *p;
|
||||
bool all_fields_empty = true;
|
||||
size_t i;
|
||||
|
||||
strncpy(temp_line, line, sizeof(temp_line) - 1U);
|
||||
temp_line[sizeof(temp_line) - 1U] = '\0';
|
||||
|
||||
p = temp_line;
|
||||
fields[field_count++] = p;
|
||||
|
||||
while (*p != '\0' && field_count < 8U)
|
||||
{
|
||||
if (*p == ',')
|
||||
{
|
||||
*p = '\0';
|
||||
fields[field_count++] = p + 1;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
|
||||
if (field_count == 8U)
|
||||
{
|
||||
for (i = 0; i < field_count; i++)
|
||||
{
|
||||
trim_whitespace(fields[i]);
|
||||
if (fields[i][0] != '\0')
|
||||
{
|
||||
all_fields_empty = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (all_fields_empty)
|
||||
continue;
|
||||
}
|
||||
printf("CSV WARNING line %u: skipping non-data line: %s\n",
|
||||
(unsigned)line_num,
|
||||
line);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (db->count >= MODBUS_MAX_POINTS)
|
||||
if (db->count >= db->capacity)
|
||||
{
|
||||
printf("CSV ERROR line %u: exceeded MODBUS_MAX_POINTS (%u)\n",
|
||||
printf("CSV ERROR line %u: exceeded configured max_points (%u)\n",
|
||||
(unsigned)line_num,
|
||||
(unsigned)MODBUS_MAX_POINTS);
|
||||
(unsigned)db->capacity);
|
||||
fclose(fp);
|
||||
modbus_points_reset(db);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -710,6 +719,7 @@ bool modbus_points_load(modbus_db_t *db, const char *filename)
|
||||
(unsigned)line_num,
|
||||
line);
|
||||
fclose(fp);
|
||||
modbus_points_reset(db);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -721,6 +731,7 @@ bool modbus_points_load(modbus_db_t *db, const char *filename)
|
||||
(unsigned)pt.offset);
|
||||
|
||||
fclose(fp);
|
||||
modbus_points_reset(db);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -736,6 +747,7 @@ bool modbus_points_load(modbus_db_t *db, const char *filename)
|
||||
(unsigned)pt.pics_offset,
|
||||
(unsigned)pt.pics_reg_span);
|
||||
fclose(fp);
|
||||
modbus_points_reset(db);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -746,6 +758,7 @@ bool modbus_points_load(modbus_db_t *db, const char *filename)
|
||||
printf("CSV ERROR line %u: failed to append helper points\n",
|
||||
(unsigned)line_num);
|
||||
fclose(fp);
|
||||
modbus_points_reset(db);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user