Add comprehensive smoke and feature tests for Gateway API
- Introduced LtdFeatureTests to validate BACnet and OPC UA point updates, trend logging, and alarm rule functionality. - Added OpcUaClientTests to ensure proper OPC UA client configuration, point value synchronization, and session management. - Created OpcUaFeatureTests to test OPC UA server capabilities, including browsing, reading, writing, and subscription notifications. - Implemented native_bacnet_test.py for isolated BACnet wire testing, covering discovery, read/write operations, and command processing. - Added ui-theme-tabs.test.cjs to verify theme switching, tab interactions, and protocol form accessibility in the UI.
This commit is contained in:
1 parent
d9165a60ff
commit
98c48d9d78
80 files changed
+11120
-315
No files matched your search
@@ -0,0 +1,474 @@
|
||||
/* SE-Inno native object bridge. APDU encoding, COV and priority arbitration
|
||||
* remain entirely in the pinned upstream BACnet stack. No UDP emulation. */
|
||||
#include "gateway_bridge.h"
|
||||
#include <sqlite3.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include "bacnet/bacapp.h"
|
||||
#include "bacnet/iam.h"
|
||||
#include "bacnet/basic/service/h_apdu.h"
|
||||
#include "bacnet/basic/service/s_whois.h"
|
||||
#include "bacnet/datalink/bip.h"
|
||||
#include "bacnet/datalink/bvlc.h"
|
||||
#include "bacnet/basic/object/device.h"
|
||||
#include "bacnet/basic/object/ai.h"
|
||||
#include "bacnet/basic/object/ao.h"
|
||||
#include "bacnet/basic/object/av.h"
|
||||
#include "bacnet/basic/object/bi.h"
|
||||
#include "bacnet/basic/object/bo.h"
|
||||
#include "bacnet/basic/object/bv.h"
|
||||
#include "bacnet/basic/object/ms-input.h"
|
||||
#include "bacnet/basic/object/mso.h"
|
||||
#include "bacnet/basic/object/msv.h"
|
||||
#include "bacnet/basic/object/calendar.h"
|
||||
#include "bacnet/basic/object/schedule.h"
|
||||
#include "bacnet/basic/object/netport.h"
|
||||
#include "bacnet/basic/bbmd/h_bbmd.h"
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
BACNET_OBJECT_TYPE type;
|
||||
bool (*name_set)(uint32_t, const char *);
|
||||
bool (*reliability_set)(uint32_t, BACNET_RELIABILITY);
|
||||
} Kind;
|
||||
static const Kind kinds[] = {
|
||||
{"AnalogInput", OBJECT_ANALOG_INPUT, Analog_Input_Name_Set, Analog_Input_Reliability_Set},
|
||||
{"AnalogOutput", OBJECT_ANALOG_OUTPUT, Analog_Output_Name_Set, Analog_Output_Reliability_Set},
|
||||
{"AnalogValue", OBJECT_ANALOG_VALUE, Analog_Value_Name_Set, Analog_Value_Reliability_Set},
|
||||
{"BinaryInput", OBJECT_BINARY_INPUT, Binary_Input_Name_Set, Binary_Input_Reliability_Set},
|
||||
{"BinaryOutput", OBJECT_BINARY_OUTPUT, Binary_Output_Name_Set, Binary_Output_Reliability_Set},
|
||||
{"BinaryValue", OBJECT_BINARY_VALUE, Binary_Value_Name_Set, Binary_Value_Reliability_Set},
|
||||
{"MultiStateInput", OBJECT_MULTI_STATE_INPUT, Multistate_Input_Name_Set, Multistate_Input_Reliability_Set},
|
||||
{"MultiStateOutput", OBJECT_MULTI_STATE_OUTPUT, Multistate_Output_Name_Set, Multistate_Output_Reliability_Set},
|
||||
{"MultiStateValue", OBJECT_MULTI_STATE_VALUE, Multistate_Value_Name_Set, Multistate_Value_Reliability_Set},
|
||||
{"Calendar", OBJECT_CALENDAR, Calendar_Name_Set, NULL},
|
||||
{"Schedule", OBJECT_SCHEDULE, Schedule_Name_Set, NULL}
|
||||
};
|
||||
typedef struct Point {
|
||||
char id[37];
|
||||
char *name;
|
||||
char *address_json;
|
||||
char *value_type;
|
||||
const Kind *kind;
|
||||
uint32_t instance;
|
||||
bool writable, ready, present;
|
||||
char last[128];
|
||||
struct Point *next;
|
||||
} Point;
|
||||
static sqlite3 *db;
|
||||
static Point *points;
|
||||
static object_functions_t table[16];
|
||||
static write_property_function writers[MAX_BACNET_OBJECT_TYPE];
|
||||
static char *network_configuration;
|
||||
static uint32_t network_ipv4, network_netmask;
|
||||
static char discovery_id[33];
|
||||
static char discovery_target[16];
|
||||
static uint16_t discovery_port;
|
||||
static long long discovery_until;
|
||||
static long long last_scan;
|
||||
|
||||
static long long now_ms(void)
|
||||
{
|
||||
struct timespec t; clock_gettime(CLOCK_REALTIME, &t);
|
||||
return (long long)t.tv_sec * 1000 + t.tv_nsec / 1000000;
|
||||
}
|
||||
static void fatal(void) { fprintf(stderr, "Gateway database failure: %s\n", sqlite3_errmsg(db)); exit(75); }
|
||||
static sqlite3_stmt *prepare(const char *sql)
|
||||
{
|
||||
sqlite3_stmt *s = NULL;
|
||||
if (sqlite3_prepare_v2(db, sql, -1, &s, NULL) != SQLITE_OK) fatal();
|
||||
return s;
|
||||
}
|
||||
static void execute(sqlite3_stmt *s)
|
||||
{
|
||||
if (sqlite3_step(s) != SQLITE_DONE) fatal();
|
||||
sqlite3_finalize(s);
|
||||
}
|
||||
static void sql(const char *text) { if (sqlite3_exec(db, text, NULL, NULL, NULL) != SQLITE_OK) fatal(); }
|
||||
static const char *str(sqlite3_stmt *s, int i) { const char *v = (const char *)sqlite3_column_text(s, i); return v ? v : ""; }
|
||||
static Point *find(BACNET_OBJECT_TYPE type, uint32_t instance)
|
||||
{
|
||||
for (Point *p = points; p; p = p->next) if (p->kind->type == type && p->instance == instance) return p;
|
||||
return NULL;
|
||||
}
|
||||
static bool input(Point *p) { return p->kind->type == OBJECT_ANALOG_INPUT || p->kind->type == OBJECT_BINARY_INPUT || p->kind->type == OBJECT_MULTI_STATE_INPUT; }
|
||||
static bool point_current(Point *p)
|
||||
{
|
||||
sqlite3_stmt *s = prepare(
|
||||
"SELECT 1 FROM points WHERE id=? AND bacnet_object_kind=? AND bacnet_object_instance=? AND "
|
||||
"address_json=? AND value_type=? AND writable=? AND deleted_at_utc IS NULL "
|
||||
"AND protocol='Bacnet' AND scope='Gateway'");
|
||||
sqlite3_bind_text(s, 1, p->id, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(s, 2, p->kind->name, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int64(s, 3, p->instance);
|
||||
sqlite3_bind_text(s, 4, p->address_json, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(s, 5, p->value_type, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int(s, 6, p->writable);
|
||||
bool current = sqlite3_step(s) == SQLITE_ROW;
|
||||
sqlite3_finalize(s);
|
||||
return current;
|
||||
}
|
||||
static void ready(Point *p)
|
||||
{
|
||||
p->ready = true;
|
||||
if (p->kind->reliability_set) p->kind->reliability_set(p->instance, RELIABILITY_NO_FAULT_DETECTED);
|
||||
}
|
||||
static void persist(Point *p, BACNET_WRITE_PROPERTY_DATA *w)
|
||||
{
|
||||
sqlite3_stmt *s = prepare("INSERT OR REPLACE INTO bacnet_properties(point_id,property,array_index,priority,payload) VALUES(?,?,?,?,?)");
|
||||
sqlite3_bind_text(s, 1, p->id, -1, SQLITE_TRANSIENT);
|
||||
sqlite3_bind_int(s, 2, w->object_property);
|
||||
sqlite3_bind_int64(s, 3, w->array_index);
|
||||
sqlite3_bind_int(s, 4, w->priority);
|
||||
sqlite3_bind_blob(s, 5, w->application_data, w->application_data_len, SQLITE_TRANSIENT);
|
||||
execute(s);
|
||||
}
|
||||
static bool network_write(BACNET_WRITE_PROPERTY_DATA *w)
|
||||
{
|
||||
Point *p = find(w->object_type, w->object_instance);
|
||||
if (!p || w->object_property == PROP_OBJECT_IDENTIFIER || w->object_property == PROP_OBJECT_NAME ||
|
||||
(!p->writable && !input(p) && w->object_property == PROP_PRESENT_VALUE)) {
|
||||
w->error_class = ERROR_CLASS_PROPERTY; w->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; return false;
|
||||
}
|
||||
sql("BEGIN IMMEDIATE");
|
||||
if (!point_current(p)) {
|
||||
sql("ROLLBACK");
|
||||
w->error_class = ERROR_CLASS_OBJECT;
|
||||
w->error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
return false;
|
||||
}
|
||||
bool ok = writers[w->object_type] && writers[w->object_type](w);
|
||||
if (ok) {
|
||||
persist(p, w);
|
||||
if (w->object_property == PROP_PRESENT_VALUE) ready(p);
|
||||
sql("COMMIT");
|
||||
} else sql("ROLLBACK");
|
||||
return ok;
|
||||
}
|
||||
static bool apply_input(Point *p, BACNET_WRITE_PROPERTY_DATA *w)
|
||||
{
|
||||
BACNET_APPLICATION_DATA_VALUE value = {0};
|
||||
if (bacapp_decode_application_data(w->application_data, w->application_data_len, &value) <= 0) return false;
|
||||
switch (p->kind->type) {
|
||||
case OBJECT_ANALOG_INPUT:
|
||||
if (Analog_Input_Out_Of_Service(p->instance)) return false;
|
||||
Analog_Input_Present_Value_Set(p->instance, value.type.Real); return true;
|
||||
case OBJECT_BINARY_INPUT:
|
||||
return !Binary_Input_Out_Of_Service(p->instance) && Binary_Input_Present_Value_Set(p->instance, value.type.Enumerated);
|
||||
case OBJECT_MULTI_STATE_INPUT:
|
||||
return !Multistate_Input_Out_Of_Service(p->instance) && Multistate_Input_Present_Value_Set(p->instance, value.type.Unsigned_Int);
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
static void restore(Point *p)
|
||||
{
|
||||
sqlite3_stmt *s = prepare("SELECT property,array_index,priority,payload FROM bacnet_properties WHERE point_id=? ORDER BY id");
|
||||
sqlite3_bind_text(s, 1, p->id, -1, SQLITE_TRANSIENT);
|
||||
while (sqlite3_step(s) == SQLITE_ROW) {
|
||||
BACNET_WRITE_PROPERTY_DATA w = {0};
|
||||
w.object_type = p->kind->type; w.object_instance = p->instance;
|
||||
w.object_property = sqlite3_column_int(s, 0); w.array_index = sqlite3_column_int64(s, 1); w.priority = sqlite3_column_int(s, 2);
|
||||
w.application_data_len = sqlite3_column_bytes(s, 3);
|
||||
if (w.application_data_len > MAX_APDU) continue;
|
||||
memcpy(w.application_data, sqlite3_column_blob(s, 3), w.application_data_len);
|
||||
bool ok = input(p) && w.object_property == PROP_PRESENT_VALUE && w.priority == 0 ? apply_input(p, &w) : writers[w.object_type](&w);
|
||||
if (ok && w.object_property == PROP_PRESENT_VALUE) ready(p);
|
||||
if (!ok) fprintf(stderr, "Cannot restore BACnet object %s property %u\n", p->id, w.object_property);
|
||||
}
|
||||
sqlite3_finalize(s);
|
||||
}
|
||||
static void load_points(void)
|
||||
{
|
||||
// Remove stale objects before adding replacements, even if they reuse an instance.
|
||||
sql("BEGIN");
|
||||
sqlite3_stmt *s = prepare(
|
||||
"SELECT id,display_name,bacnet_object_kind,bacnet_object_instance,writable,"
|
||||
"COALESCE(json_extract(address_json,'$.units'),95),COALESCE(json_extract(address_json,'$.numberOfStates'),16),"
|
||||
"address_json,value_type FROM points WHERE protocol='Bacnet' AND scope='Gateway' AND deleted_at_utc IS NULL");
|
||||
for (Point *p = points; p; p = p->next) p->present = false;
|
||||
while (sqlite3_step(s) == SQLITE_ROW) {
|
||||
for (Point *p = points; p; p = p->next) {
|
||||
if (strcmp(p->id, str(s, 0)) || strcmp(p->kind->name, str(s, 2)) ||
|
||||
p->instance != (uint32_t)sqlite3_column_int(s, 3) ||
|
||||
strcmp(p->address_json, str(s, 7)) || strcmp(p->value_type, str(s, 8))) continue;
|
||||
p->present = true;
|
||||
p->writable = sqlite3_column_int(s, 4);
|
||||
if (strcmp(p->name, str(s, 1))) {
|
||||
char *name = strdup(str(s, 1));
|
||||
if (!name) exit(75);
|
||||
if (!p->kind->name_set(p->instance, name)) { free(name); exit(75); }
|
||||
free(p->name);
|
||||
p->name = name;
|
||||
Device_Inc_Database_Revision();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (sqlite3_reset(s) != SQLITE_OK) fatal();
|
||||
Point **link = &points;
|
||||
while (*link) {
|
||||
Point *p = *link;
|
||||
if (p->present) { link = &p->next; continue; }
|
||||
BACNET_DELETE_OBJECT_DATA object = { .object_type = p->kind->type, .object_instance = p->instance };
|
||||
if (!Device_Delete_Object(&object)) {
|
||||
fprintf(stderr, "Unable to remove stale BACnet object; rebuilding from the active catalog\n");
|
||||
exit(75);
|
||||
}
|
||||
*link = p->next;
|
||||
free(p->name); free(p->address_json); free(p->value_type); free(p);
|
||||
}
|
||||
while (sqlite3_step(s) == SQLITE_ROW) {
|
||||
const Kind *kind = NULL;
|
||||
for (unsigned i = 0; i < sizeof(kinds)/sizeof(kinds[0]); i++) if (!strcmp(str(s, 2), kinds[i].name)) kind = &kinds[i];
|
||||
if (!kind || find(kind->type, sqlite3_column_int(s, 3))) continue;
|
||||
object_functions_t *f = Device_Object_Functions_Find(kind->type);
|
||||
uint32_t instance = sqlite3_column_int(s, 3);
|
||||
if (!f || !f->Object_Create || f->Object_Create(instance) != instance) continue;
|
||||
Point *p = calloc(1, sizeof(*p)); if (!p) exit(75);
|
||||
snprintf(p->id, sizeof(p->id), "%s", str(s, 0)); p->name = strdup(str(s, 1));
|
||||
p->address_json = strdup(str(s, 7)); p->value_type = strdup(str(s, 8));
|
||||
if (!p->name || !p->address_json || !p->value_type) exit(75);
|
||||
p->kind = kind; p->instance = instance; p->writable = sqlite3_column_int(s, 4); p->next = points; points = p;
|
||||
kind->name_set(instance, p->name);
|
||||
if (kind->reliability_set) kind->reliability_set(instance, RELIABILITY_UNRELIABLE_OTHER); else p->ready = true;
|
||||
unsigned units = sqlite3_column_int(s, 5), states = sqlite3_column_int(s, 6);
|
||||
char *state_names = NULL;
|
||||
if (kind->type == OBJECT_MULTI_STATE_INPUT || kind->type == OBJECT_MULTI_STATE_OUTPUT || kind->type == OBJECT_MULTI_STATE_VALUE) {
|
||||
if (states < 1 || states > 1000) states = 16;
|
||||
state_names = calloc(states * 24 + 1, 1); if (!state_names) exit(75);
|
||||
char *next = state_names;
|
||||
for (unsigned i = 1; i <= states; i++) next += sprintf(next, "State %u", i) + 1;
|
||||
}
|
||||
switch (kind->type) {
|
||||
case OBJECT_ANALOG_INPUT: Analog_Input_Units_Set(instance, units); break;
|
||||
case OBJECT_ANALOG_OUTPUT: Analog_Output_Units_Set(instance, units); break;
|
||||
case OBJECT_ANALOG_VALUE: Analog_Value_Units_Set(instance, units); break;
|
||||
case OBJECT_MULTI_STATE_INPUT: Multistate_Input_State_Text_List_Set(instance, state_names); break;
|
||||
case OBJECT_MULTI_STATE_OUTPUT: Multistate_Output_State_Text_List_Set(instance, state_names); break;
|
||||
case OBJECT_MULTI_STATE_VALUE: Multistate_Value_State_Text_List_Set(instance, state_names); break;
|
||||
default: break;
|
||||
}
|
||||
free(state_names);
|
||||
restore(p);
|
||||
Device_Inc_Database_Revision();
|
||||
}
|
||||
sqlite3_finalize(s);
|
||||
sql("COMMIT");
|
||||
}
|
||||
void Gateway_Init(void)
|
||||
{
|
||||
const char *path = getenv("SEIUGW_DATABASE");
|
||||
if (sqlite3_open_v2(path ? path : "seiugw.db", &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) fatal();
|
||||
sqlite3_busy_timeout(db, 5000);
|
||||
Device_Object_Functions_Init(NULL);
|
||||
unsigned n = 0;
|
||||
table[n++] = *Device_Object_Functions_Find(OBJECT_DEVICE);
|
||||
table[n++] = *Device_Object_Functions_Find(OBJECT_NETWORK_PORT);
|
||||
for (unsigned i = 0; i < sizeof(kinds)/sizeof(kinds[0]); i++) {
|
||||
object_functions_t *f = Device_Object_Functions_Find(kinds[i].type);
|
||||
if (!f) { fprintf(stderr, "Missing object implementation %s\n", kinds[i].name); exit(75); }
|
||||
table[n] = *f; writers[f->Object_Type] = f->Object_Write_Property;
|
||||
table[n++].Object_Write_Property = network_write;
|
||||
}
|
||||
table[n].Object_Type = MAX_BACNET_OBJECT_TYPE;
|
||||
Device_Init(table);
|
||||
Device_Set_Vendor_Identifier(815);
|
||||
load_points();
|
||||
// Claimed commands were interrupted: never replay an unknown write outcome.
|
||||
sql("UPDATE bacnet_commands SET state='failed',result='Interrupted; not replayed' WHERE state='claimed'");
|
||||
}
|
||||
static void read_interface_ipv4(uint32_t *address, uint32_t *netmask)
|
||||
{
|
||||
*address = *netmask = 0;
|
||||
const char *name = getenv("BACNET_IFACE");
|
||||
if (!name || strlen(name) >= IFNAMSIZ) return;
|
||||
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0) return;
|
||||
struct ifreq request = {0};
|
||||
strcpy(request.ifr_name, name);
|
||||
if (ioctl(fd, SIOCGIFADDR, &request) == 0)
|
||||
*address = ((struct sockaddr_in *)&request.ifr_addr)->sin_addr.s_addr;
|
||||
if (ioctl(fd, SIOCGIFNETMASK, &request) == 0)
|
||||
*netmask = ((struct sockaddr_in *)&request.ifr_netmask)->sin_addr.s_addr;
|
||||
close(fd);
|
||||
}
|
||||
void Gateway_Configure(void)
|
||||
{
|
||||
sqlite3_stmt *s = prepare("SELECT json_extract(value,'$.deviceInstance'),json_extract(value,'$.deviceName'),json_extract(value,'$.bacnet.interface'),json_extract(value,'$.bacnet.ipPort'),json_extract(value,'$.bacnet.bbmdMode'),json_extract(value,'$.bacnet.bbmdAddress'),json_extract(value,'$.bacnet.foreignDeviceTtlSeconds'),json_extract(value,'$.bacnet') FROM settings WHERE key='gateway.configuration'");
|
||||
if (sqlite3_step(s) != SQLITE_ROW) fatal();
|
||||
Device_Set_Object_Instance_Number(sqlite3_column_int(s, 0)); Device_Object_Name_ANSI_Init(str(s, 1));
|
||||
setenv("BACNET_IFACE", str(s, 2), 1); setenv("BACNET_IP_PORT", str(s, 3), 1); setenv("BACNET_IP_BROADCAST_PORT", str(s, 3), 1);
|
||||
setenv("SEIUGW_BBMD_MODE", str(s, 4), 1);
|
||||
if (!strcmp(str(s, 4), "ForeignDevice")) {
|
||||
setenv("BACNET_BBMD_ADDRESS", str(s, 5), 1); setenv("BACNET_BBMD_TIMETOLIVE", str(s, 6), 1);
|
||||
}
|
||||
network_configuration = strdup(str(s, 7));
|
||||
sqlite3_finalize(s);
|
||||
read_interface_ipv4(&network_ipv4, &network_netmask);
|
||||
}
|
||||
static void receive_i_am(uint8_t *request, uint16_t length, BACNET_ADDRESS *source)
|
||||
{
|
||||
if (!discovery_id[0] || now_ms() >= discovery_until || source->mac_len != 6)
|
||||
return;
|
||||
uint32_t device;
|
||||
unsigned max_apdu;
|
||||
int segmentation;
|
||||
uint16_t vendor;
|
||||
if (bacnet_iam_request_decode(request, length, &device, &max_apdu, &segmentation, &vendor) <= 0 ||
|
||||
max_apdu > 65535)
|
||||
return;
|
||||
char address[16];
|
||||
const uint8_t *mac = source->mac;
|
||||
unsigned port = (mac[4] << 8) | mac[5];
|
||||
if (memcmp(mac, &network_ipv4, 4) == 0 && port == bip_get_port()) return;
|
||||
snprintf(address, sizeof(address), "%u.%u.%u.%u", mac[0], mac[1], mac[2], mac[3]);
|
||||
if (discovery_target[0] && (strcmp(address, discovery_target) || port != discovery_port))
|
||||
return;
|
||||
sqlite3_stmt *s = prepare(
|
||||
"INSERT OR IGNORE INTO bacnet_discovered_devices(scan_id,device_instance,address,port,vendor,max_apdu,network,mac) VALUES(?,?,?,?,?,?,?,?)");
|
||||
sqlite3_bind_text(s, 1, discovery_id, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int(s, 2, device);
|
||||
sqlite3_bind_text(s, 3, address, -1, SQLITE_TRANSIENT);
|
||||
sqlite3_bind_int(s, 4, port);
|
||||
sqlite3_bind_int(s, 5, vendor);
|
||||
sqlite3_bind_int(s, 6, max_apdu);
|
||||
sqlite3_bind_int(s, 7, source->net);
|
||||
if (source->net && source->len && source->len <= MAX_MAC_LEN) {
|
||||
char remote_mac[MAX_MAC_LEN * 2 + 1];
|
||||
for (unsigned i = 0; i < source->len; i++)
|
||||
snprintf(remote_mac + 2 * i, 3, "%02X", source->adr[i]);
|
||||
sqlite3_bind_text(s, 8, remote_mac, -1, SQLITE_TRANSIENT);
|
||||
} else {
|
||||
sqlite3_bind_null(s, 8);
|
||||
}
|
||||
execute(s);
|
||||
}
|
||||
static void discover(long long now)
|
||||
{
|
||||
sqlite3_stmt *s = prepare("SELECT scan_id,target,port,expires_ms FROM bacnet_discovery WHERE id=1 AND sent=0 AND expires_ms>?");
|
||||
sqlite3_bind_int64(s, 1, now);
|
||||
if (sqlite3_step(s) != SQLITE_ROW) {
|
||||
sqlite3_finalize(s);
|
||||
return;
|
||||
}
|
||||
snprintf(discovery_id, sizeof(discovery_id), "%s", str(s, 0));
|
||||
snprintf(discovery_target, sizeof(discovery_target), "%s", str(s, 1));
|
||||
discovery_port = (uint16_t)sqlite3_column_int(s, 2);
|
||||
discovery_until = sqlite3_column_int64(s, 3);
|
||||
sqlite3_finalize(s);
|
||||
if (discovery_target[0]) {
|
||||
BACNET_IP_ADDRESS ip = {0};
|
||||
BACNET_ADDRESS address = {0};
|
||||
char port[6];
|
||||
snprintf(port, sizeof(port), "%u", discovery_port);
|
||||
if (!bvlc_address_port_from_ascii(&ip, discovery_target, port) ||
|
||||
!bvlc_ip_address_to_bacnet_local(&address, &ip))
|
||||
return;
|
||||
Send_WhoIs_Remote(&address, -1, -1);
|
||||
} else {
|
||||
Send_WhoIs_Global(-1, -1);
|
||||
}
|
||||
s = prepare("UPDATE bacnet_discovery SET sent=1 WHERE scan_id=?");
|
||||
sqlite3_bind_text(s, 1, discovery_id, -1, SQLITE_STATIC);
|
||||
execute(s);
|
||||
}
|
||||
void Gateway_Network_Ready(void)
|
||||
{
|
||||
const char *mode = getenv("SEIUGW_BBMD_MODE");
|
||||
bool bbmd = mode && !strcmp(mode, "Bbmd");
|
||||
uint32_t instance = Network_Port_Index_To_Instance(0);
|
||||
bvlc_bbmd_accept_fd_registrations_set(bbmd);
|
||||
Network_Port_BBMD_Accept_FD_Registrations_Set(instance, bbmd);
|
||||
Network_Port_BIP_Mode_Set(instance, bbmd ? BACNET_IP_MODE_BBMD : mode && !strcmp(mode, "ForeignDevice") ? BACNET_IP_MODE_FOREIGN : BACNET_IP_MODE_NORMAL);
|
||||
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, receive_i_am);
|
||||
}
|
||||
static void commands(void)
|
||||
{
|
||||
for (unsigned i = 0; i < 100; i++) {
|
||||
sqlite3_stmt *s = prepare("SELECT id,point_id,value_json,priority,created_ms FROM bacnet_commands WHERE state='pending' ORDER BY id LIMIT 1");
|
||||
if (sqlite3_step(s) != SQLITE_ROW) { sqlite3_finalize(s); break; }
|
||||
long long id = sqlite3_column_int64(s, 0), created = sqlite3_column_int64(s, 4);
|
||||
Point *p = points; while (p && strcmp(p->id, str(s, 1))) p = p->next;
|
||||
char *value = strdup(str(s, 2)); unsigned priority = sqlite3_column_int(s, 3); sqlite3_finalize(s);
|
||||
s = prepare("UPDATE bacnet_commands SET state='claimed' WHERE id=?"); sqlite3_bind_int64(s, 1, id); execute(s);
|
||||
bool ok = false;
|
||||
if (p && now_ms() - created < 10000 && priority >= 1 && priority <= 16 && priority != 6) {
|
||||
BACNET_WRITE_PROPERTY_DATA w = {0};
|
||||
w.object_type = p->kind->type; w.object_instance = p->instance;
|
||||
w.object_property = PROP_PRESENT_VALUE; w.array_index = BACNET_ARRAY_ALL; w.priority = input(p) ? 0 : priority;
|
||||
if (!strcmp(value, "null")) w.application_data_len = encode_application_null(w.application_data);
|
||||
else if (p->kind->type <= OBJECT_ANALOG_VALUE) w.application_data_len = encode_application_real(w.application_data, strtof(value, NULL));
|
||||
else if (p->kind->type >= OBJECT_BINARY_INPUT && p->kind->type <= OBJECT_BINARY_VALUE) w.application_data_len = encode_application_enumerated(w.application_data, !strcmp(value, "true") ? BINARY_ACTIVE : BINARY_INACTIVE);
|
||||
else w.application_data_len = encode_application_unsigned(w.application_data, strtoul(value, NULL, 10));
|
||||
sql("BEGIN IMMEDIATE");
|
||||
if (point_current(p))
|
||||
ok = input(p) ? apply_input(p, &w) : writers[w.object_type](&w);
|
||||
if (ok) { persist(p, &w); ready(p); sql("COMMIT"); } else sql("ROLLBACK");
|
||||
}
|
||||
free(value);
|
||||
s = prepare("UPDATE bacnet_commands SET state=?,result=? WHERE id=?");
|
||||
sqlite3_bind_text(s, 1, ok ? "done" : "failed", -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(s, 2, ok ? "Good" : "Rejected, expired or object unavailable", -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int64(s, 3, id); execute(s);
|
||||
}
|
||||
}
|
||||
static void observe(Point *p)
|
||||
{
|
||||
uint8_t buffer[MAX_APDU]; BACNET_READ_PROPERTY_DATA r = {0}; BACNET_APPLICATION_DATA_VALUE value = {0};
|
||||
r.object_type = p->kind->type; r.object_instance = p->instance; r.object_property = PROP_PRESENT_VALUE;
|
||||
r.array_index = BACNET_ARRAY_ALL; r.application_data = buffer; r.application_data_len = sizeof(buffer);
|
||||
int length = Device_Read_Property(&r);
|
||||
if (length <= 0 || bacapp_decode_application_data(buffer, length, &value) <= 0) return;
|
||||
char json[80] = "null";
|
||||
if (p->ready) switch (value.tag) {
|
||||
case BACNET_APPLICATION_TAG_REAL: if (isfinite(value.type.Real)) snprintf(json, sizeof(json), "%.9g", (double)value.type.Real); break;
|
||||
case BACNET_APPLICATION_TAG_UNSIGNED_INT: snprintf(json, sizeof(json), "%u", (unsigned)value.type.Unsigned_Int); break;
|
||||
case BACNET_APPLICATION_TAG_ENUMERATED: snprintf(json, sizeof(json), "%s", value.type.Enumerated ? "true" : "false"); break;
|
||||
case BACNET_APPLICATION_TAG_BOOLEAN: snprintf(json, sizeof(json), "%s", value.type.Boolean ? "true" : "false"); break;
|
||||
default: break;
|
||||
}
|
||||
const char *status = p->ready && strcmp(json, "null") ? "Good" : "BadWaitingForInitialData";
|
||||
r.object_property = PROP_RELIABILITY;
|
||||
length = Device_Read_Property(&r);
|
||||
if (p->ready && length > 0 && bacapp_decode_application_data(buffer, length, &value) > 0 && value.type.Enumerated != RELIABILITY_NO_FAULT_DETECTED) status = "BadSensorFailure";
|
||||
r.object_property = PROP_STATUS_FLAGS;
|
||||
length = Device_Read_Property(&r);
|
||||
if (length > 0 && bacapp_decode_application_data(buffer, length, &value) > 0 && value.tag == BACNET_APPLICATION_TAG_BIT_STRING && bitstring_bit(&value.type.Bit_String, STATUS_FLAG_OUT_OF_SERVICE)) status = "UncertainOutOfService";
|
||||
char snapshot[128]; snprintf(snapshot, sizeof(snapshot), "%s:%s", status, json);
|
||||
if (!strcmp(snapshot, p->last)) return;
|
||||
sqlite3_stmt *s = prepare("INSERT INTO bacnet_observations(point_id,value_json,status,observed_ms) VALUES(?,?,?,?)");
|
||||
sqlite3_bind_text(s, 1, p->id, -1, SQLITE_TRANSIENT); sqlite3_bind_text(s, 2, json, -1, SQLITE_TRANSIENT);
|
||||
sqlite3_bind_text(s, 3, status, -1, SQLITE_STATIC); sqlite3_bind_int64(s, 4, now_ms()); execute(s);
|
||||
snprintf(p->last, sizeof(p->last), "%s", snapshot);
|
||||
}
|
||||
void Gateway_Tick(void)
|
||||
{
|
||||
long long now = now_ms();
|
||||
if (now - last_scan >= 1000) {
|
||||
uint32_t address, netmask;
|
||||
read_interface_ipv4(&address, &netmask);
|
||||
if (address != network_ipv4 || netmask != network_netmask) {
|
||||
fprintf(stderr, "BACnet interface IPv4 address or netmask changed; restarting\n");
|
||||
exit(75);
|
||||
}
|
||||
last_scan = now; load_points();
|
||||
sqlite3_stmt *s = prepare("SELECT json_extract(value,'$.bacnet'),json_extract(value,'$.deviceInstance'),json_extract(value,'$.deviceName') FROM settings WHERE key='gateway.configuration'");
|
||||
if (sqlite3_step(s) == SQLITE_ROW && (strcmp(network_configuration, str(s, 0)) || Device_Object_Instance_Number() != sqlite3_column_int(s, 1))) {
|
||||
sqlite3_finalize(s); fprintf(stderr, "BACnet network configuration changed; restarting\n"); exit(75);
|
||||
}
|
||||
if (sqlite3_column_count(s) >= 3) Device_Object_Name_ANSI_Init(str(s, 2));
|
||||
sqlite3_finalize(s);
|
||||
discover(now);
|
||||
s = prepare("INSERT OR REPLACE INTO bacnet_runtime VALUES(1,?,'Running')"); sqlite3_bind_int64(s, 1, now); execute(s);
|
||||
s = prepare("DELETE FROM bacnet_commands WHERE state IN ('done','failed') AND created_ms<?"); sqlite3_bind_int64(s, 1, now - 86400000); execute(s);
|
||||
}
|
||||
commands();
|
||||
for (Point *p = points; p; p = p->next) observe(p);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef SEIUGW_BRIDGE_H
|
||||
#define SEIUGW_BRIDGE_H
|
||||
void Gateway_Init(void);
|
||||
void Gateway_Configure(void);
|
||||
void Gateway_Network_Ready(void);
|
||||
void Gateway_Tick(void);
|
||||
#endif
|
||||
Reference in new issue
Block a user