Commit f7fde3a4 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

Define pubsub variables IDs in a separate structure

parent 24581dcf
...@@ -19,8 +19,6 @@ ...@@ -19,8 +19,6 @@
typedef struct { typedef struct {
UA_UInt16 id; UA_UInt16 id;
UA_UInt32 positionArrayId;
UA_UInt32 speedArrayId;
UA_Double latitude; UA_Double latitude;
UA_Double longitude; UA_Double longitude;
UA_Double altitudeAbs; UA_Double altitudeAbs;
...@@ -30,9 +28,7 @@ typedef struct { ...@@ -30,9 +28,7 @@ typedef struct {
UA_Float speed; UA_Float speed;
UA_Float climbRate; UA_Float climbRate;
char message[MAX_STR_SIZE]; char message[MAX_STR_SIZE];
UA_UInt32 messageId;
char log[MAX_STR_SIZE]; char log[MAX_STR_SIZE];
UA_UInt32 logId;
} JSDroneData; } JSDroneData;
typedef struct { typedef struct {
...@@ -42,6 +38,16 @@ typedef struct { ...@@ -42,6 +38,16 @@ typedef struct {
UA_Int64 timestamp; UA_Int64 timestamp;
} JSPositionData; } JSPositionData;
typedef union {
struct {
UA_UInt32 message;
UA_UInt32 positionArray;
UA_UInt32 speedArray;
UA_UInt32 log;
};
UA_UInt32 getter[4];
} PubsubVariableIDs;
typedef struct { typedef struct {
char* name; char* name;
char* typeName; char* typeName;
...@@ -64,16 +70,12 @@ typedef struct { ...@@ -64,16 +70,12 @@ typedef struct {
VariableData *variableArray; VariableData *variableArray;
} VariableStruct; } VariableStruct;
typedef struct {
VariableStruct variables;
void (*init_node_id)(UA_UInt32 id, UA_UInt32 nb, UA_UInt32 magic);
} InstanceData;
int runPubsub(const UA_Logger *logger, UA_String *transportProfile, int runPubsub(const UA_Logger *logger, UA_String *transportProfile,
UA_NetworkAddressUrlDataType *networkAddressUrl, UA_NetworkAddressUrlDataType *networkAddressUrl,
VariableStruct variables, UA_UInt32 id, VariableStruct variables, UA_UInt32 id,
InstanceData *readerArray, UA_UInt32 nbReader, VariableStruct *pubsubVariableArray,
UA_UInt32 maxVariableNb, UA_Duration interval, PubsubVariableIDs *pubsubIDArray,
UA_UInt32 nbReader, UA_Duration interval,
UA_UInt16 (*get_reader_id)(UA_UInt32 nb), UA_UInt16 (*get_reader_id)(UA_UInt32 nb),
VariableData (*get_value)(UA_String identifier), VariableData (*get_value)(UA_String identifier),
void (*update)(UA_UInt32 id, const UA_DataValue*, bool print), void (*update)(UA_UInt32 id, const UA_DataValue*, bool print),
...@@ -85,10 +87,6 @@ UA_String get_log(void); ...@@ -85,10 +87,6 @@ UA_String get_log(void);
UA_UInt16 get_drone_id(UA_UInt32 nb); UA_UInt16 get_drone_id(UA_UInt32 nb);
void init_drone_node_id(UA_UInt32 id, UA_UInt32 nb, UA_UInt32 magic);
void init_subscriber_node_id(UA_UInt32 id, UA_UInt32 nb, UA_UInt32 magic);
VariableData pubsub_get_value(UA_String identifier); VariableData pubsub_get_value(UA_String identifier);
DLL_PUBLIC JSModuleDef *js_init_module(JSContext *ctx, const char *module_name); DLL_PUBLIC JSModuleDef *js_init_module(JSContext *ctx, const char *module_name);
......
...@@ -260,9 +260,9 @@ dataChangeNotificationCallback(UA_Server *server, UA_UInt32 monitoredItemId, ...@@ -260,9 +260,9 @@ dataChangeNotificationCallback(UA_Server *server, UA_UInt32 monitoredItemId,
* Add subscribedvariables to the DataSetReader */ * Add subscribedvariables to the DataSetReader */
static UA_StatusCode static UA_StatusCode
addSubscribedVariables(UA_Server *server, UA_NodeId dataSetReaderId, addSubscribedVariables(UA_Server *server, UA_NodeId dataSetReaderId,
VariableData *variableArray, UA_UInt32 nb, VariableData *variableArray,
UA_UInt32 maxVariableNb, UA_Duration samplingInterval, PubsubVariableIDs pubsubIDs, UA_UInt32 nb,
void (*init_node_id)(UA_UInt32 id, UA_UInt32 nb, UA_UInt32 magic)) { UA_Duration samplingInterval) {
if(server == NULL) if(server == NULL)
return UA_STATUSCODE_BADINTERNALERROR; return UA_STATUSCODE_BADINTERNALERROR;
...@@ -310,7 +310,7 @@ addSubscribedVariables(UA_Server *server, UA_NodeId dataSetReaderId, ...@@ -310,7 +310,7 @@ addSubscribedVariables(UA_Server *server, UA_NodeId dataSetReaderId,
UA_NodeId newNode; UA_NodeId newNode;
retval |= UA_Server_addVariableNode(server, retval |= UA_Server_addVariableNode(server,
UA_NODEID_NUMERIC(1, (UA_UInt32)maxVariableNb*nb + i + 50000), UA_NODEID_NUMERIC(1, pubsubIDs.getter[i]),
folderId, folderId,
UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
UA_QUALIFIEDNAME(1, (char *)readerConfig.dataSetMetaData.fields[i].name.data), UA_QUALIFIEDNAME(1, (char *)readerConfig.dataSetMetaData.fields[i].name.data),
...@@ -319,7 +319,6 @@ addSubscribedVariables(UA_Server *server, UA_NodeId dataSetReaderId, ...@@ -319,7 +319,6 @@ addSubscribedVariables(UA_Server *server, UA_NodeId dataSetReaderId,
/*monitor variable*/ /*monitor variable*/
UA_MonitoredItemCreateRequest monRequest = UA_MonitoredItemCreateRequest_default(newNode); UA_MonitoredItemCreateRequest monRequest = UA_MonitoredItemCreateRequest_default(newNode);
init_node_id(newNode.identifier.numeric, nb, i);
monRequest.requestedParameters.samplingInterval = samplingInterval; monRequest.requestedParameters.samplingInterval = samplingInterval;
UA_Server_createDataChangeMonitoredItem(server, UA_TIMESTAMPSTORETURN_SOURCE, UA_Server_createDataChangeMonitoredItem(server, UA_TIMESTAMPSTORETURN_SOURCE,
monRequest, NULL, dataChangeNotificationCallback); monRequest, NULL, dataChangeNotificationCallback);
...@@ -430,8 +429,9 @@ setServer(UA_String *transportProfile, ...@@ -430,8 +429,9 @@ setServer(UA_String *transportProfile,
} }
static UA_StatusCode static UA_StatusCode
subscribe(UA_Server *server, InstanceData *instanceArray, UA_UInt32 id, subscribe(UA_Server *server, VariableStruct *pubsubVariableArray,
UA_UInt32 nbReader, UA_UInt32 maxVariableNb, UA_Duration interval, PubsubVariableIDs *pubsubIDArray, UA_UInt32 id,
UA_UInt32 nbReader, UA_Duration interval,
UA_UInt16 (*get_reader_id)(UA_UInt32 nb), UA_UInt16 (*get_reader_id)(UA_UInt32 nb),
void (*update)(UA_UInt32 id, const UA_DataValue*, bool print)) { void (*update)(UA_UInt32 id, const UA_DataValue*, bool print)) {
UA_UInt16 publisherIdent; UA_UInt16 publisherIdent;
...@@ -455,16 +455,15 @@ subscribe(UA_Server *server, InstanceData *instanceArray, UA_UInt32 id, ...@@ -455,16 +455,15 @@ subscribe(UA_Server *server, InstanceData *instanceArray, UA_UInt32 id,
readerConfig.name = UA_STRING(readerName); readerConfig.name = UA_STRING(readerName);
readerConfig.publisherId.data = &publisherIdent; readerConfig.publisherId.data = &publisherIdent;
retval = addDataSetReader(server, instanceArray[i].variables, retval = addDataSetReader(server, pubsubVariableArray[i],
publisherIdent); publisherIdent);
if (retval != UA_STATUSCODE_GOOD) if (retval != UA_STATUSCODE_GOOD)
return EXIT_FAILURE; return EXIT_FAILURE;
/* Add SubscribedVariables to the created DataSetReader */ /* Add SubscribedVariables to the created DataSetReader */
retval = addSubscribedVariables(server, readerIdent, retval = addSubscribedVariables(server, readerIdent,
instanceArray[i].variables.variableArray, pubsubVariableArray[i].variableArray,
i, maxVariableNb, interval, pubsubIDArray[i], i, interval);
instanceArray[i].init_node_id);
if (retval != UA_STATUSCODE_GOOD) if (retval != UA_STATUSCODE_GOOD)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
...@@ -475,8 +474,9 @@ subscribe(UA_Server *server, InstanceData *instanceArray, UA_UInt32 id, ...@@ -475,8 +474,9 @@ subscribe(UA_Server *server, InstanceData *instanceArray, UA_UInt32 id,
int runPubsub(const UA_Logger *logger, UA_String *transportProfile, int runPubsub(const UA_Logger *logger, UA_String *transportProfile,
UA_NetworkAddressUrlDataType *networkAddressUrl, UA_NetworkAddressUrlDataType *networkAddressUrl,
VariableStruct variables, UA_UInt32 id, VariableStruct variables, UA_UInt32 id,
InstanceData *readerArray, UA_UInt32 nbReader, VariableStruct *pubsubVariableArray,
UA_UInt32 maxVariableNb, UA_Duration interval, PubsubVariableIDs *pubsubIDArray,
UA_UInt32 nbReader, UA_Duration interval,
UA_UInt16 (*get_reader_id)(UA_UInt32 nb), UA_UInt16 (*get_reader_id)(UA_UInt32 nb),
VariableData (*get_value)(UA_String identifier), VariableData (*get_value)(UA_String identifier),
void (*update)(UA_UInt32 id, const UA_DataValue*, bool print), void (*update)(UA_UInt32 id, const UA_DataValue*, bool print),
...@@ -507,8 +507,8 @@ int runPubsub(const UA_Logger *logger, UA_String *transportProfile, ...@@ -507,8 +507,8 @@ int runPubsub(const UA_Logger *logger, UA_String *transportProfile,
/* Subscribing */ /* Subscribing */
subscribe(server, readerArray, id, nbReader, maxVariableNb, interval, subscribe(server, pubsubVariableArray, pubsubIDArray, id, nbReader,
get_reader_id, update); interval, get_reader_id, update);
retval = UA_Server_run(server, running); retval = UA_Server_run(server, running);
UA_Server_delete(server); UA_Server_delete(server);
......
...@@ -30,6 +30,17 @@ UA_String initial_ua_string = { ...@@ -30,6 +30,17 @@ UA_String initial_ua_string = {
}; };
VariableData droneVariableArray[] = { VariableData droneVariableArray[] = {
{
.name = "message",
.description = "Message to send to the other drones",
.value = &initial_ua_string,
.type = UA_TYPES_STRING,
.builtInType = UA_NS0ID_STRING,
.valueRank = UA_VALUERANK_SCALAR,
.arrayDimensionsSize = 0,
.arrayDimensions = NULL,
.getter.getString = get_message,
},
{ {
.name = "positionArray", .name = "positionArray",
.typeName = "Position Array Type", .typeName = "Position Array Type",
...@@ -58,17 +69,6 @@ VariableData droneVariableArray[] = { ...@@ -58,17 +69,6 @@ VariableData droneVariableArray[] = {
.getter.getArray = (void * (*)(void))getSpeedArray, .getter.getArray = (void * (*)(void))getSpeedArray,
#endif #endif
}, },
{
.name = "message",
.description = "Message to send to the other drones",
.value = &initial_ua_string,
.type = UA_TYPES_STRING,
.builtInType = UA_NS0ID_STRING,
.valueRank = UA_VALUERANK_SCALAR,
.arrayDimensionsSize = 0,
.arrayDimensions = NULL,
.getter.getString = get_message,
},
{ {
.name = "log", .name = "log",
.description = "Message to send to the other drones", .description = "Message to send to the other drones",
...@@ -131,6 +131,9 @@ pthread_cond_t threadCond; ...@@ -131,6 +131,9 @@ pthread_cond_t threadCond;
bool isADrone; bool isADrone;
PubsubVariableIDs *pubsubIdsArray;
const UA_UInt32 VARIABLE_ID_BASE = 50000;
// Position class functions // Position class functions
static void js_position_finalizer(JSRuntime *rt, JSValue val) static void js_position_finalizer(JSRuntime *rt, JSValue val)
...@@ -457,48 +460,6 @@ VariableData pubsub_get_value(UA_String identifier) { ...@@ -457,48 +460,6 @@ VariableData pubsub_get_value(UA_String identifier) {
return varDetails; return varDetails;
} }
static JSDroneData* getJSDroneData(UA_UInt32 id, UA_UInt32 nb, UA_UInt32 magic)
{
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT,
"Getting field %d of peer %d for node id %d", magic, nb, id);
return (JSDroneData *) JS_GetOpaque(droneObjectIdList[nb], jsDroneClassId);
}
void init_drone_node_id(UA_UInt32 id, UA_UInt32 nb, UA_UInt32 magic) {
JSDroneData *s = getJSDroneData(id, nb, magic);
switch(magic) {
case 0:
s->positionArrayId = id;
break;
case 1:
s->speedArrayId = id;
break;
case 2:
s->messageId = id;
break;
case 3:
s->logId = id;
break;
default:
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Unknown variable id");
break;
}
}
void init_subscriber_node_id(UA_UInt32 id, UA_UInt32 nb, UA_UInt32 magic) {
JSDroneData *s = getJSDroneData(id, nb, magic);
switch(magic) {
case 0:
s->messageId = id;
break;
default:
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Unknown variable id");
break;
}
}
static void setDroneDataStr(void *data, char *str) static void setDroneDataStr(void *data, char *str)
{ {
UA_String uaStr = *(UA_String*) data; UA_String uaStr = *(UA_String*) data;
...@@ -520,7 +481,7 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool ...@@ -520,7 +481,7 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool
for(UA_UInt32 i = 0; i < nbDrone + nbSubscriber; i++) { for(UA_UInt32 i = 0; i < nbDrone + nbSubscriber; i++) {
s = (JSDroneData *) JS_GetOpaque(droneObjectIdList[i], jsDroneClassId); s = (JSDroneData *) JS_GetOpaque(droneObjectIdList[i], jsDroneClassId);
if (s->positionArrayId == id) { if (pubsubIdsArray[i].positionArray == id) {
updatedPositionArray = (UA_Int64*) var->value.data; updatedPositionArray = (UA_Int64*) var->value.data;
s->latitude = (double)updatedPositionArray[LATITUDE] / 1e7; s->latitude = (double)updatedPositionArray[LATITUDE] / 1e7;
s->longitude = (double)updatedPositionArray[LONGITUDE] / 1e7; s->longitude = (double)updatedPositionArray[LONGITUDE] / 1e7;
...@@ -534,7 +495,7 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool ...@@ -534,7 +495,7 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool
s->id, s->latitude, s->longitude, s->altitudeAbs, s->altitudeRel); s->id, s->latitude, s->longitude, s->altitudeAbs, s->altitudeRel);
} }
return; return;
} else if (s->speedArrayId == id) { } else if (pubsubIdsArray[i].speedArray == id) {
updatedSpeedArray = (UA_Float*) var->value.data; updatedSpeedArray = (UA_Float*) var->value.data;
s->yaw = updatedSpeedArray[YAW]; s->yaw = updatedSpeedArray[YAW];
s->speed = updatedSpeedArray[SPEED]; s->speed = updatedSpeedArray[SPEED];
...@@ -546,10 +507,10 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool ...@@ -546,10 +507,10 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool
s->id, s->yaw, s->speed, s->climbRate); s->id, s->yaw, s->speed, s->climbRate);
} }
return; return;
} else if (s->messageId == id) { } else if (pubsubIdsArray[i].message == id) {
setDroneDataStr(var->value.data, s->message); setDroneDataStr(var->value.data, s->message);
return; return;
} else if (s->logId == id) { } else if (pubsubIdsArray[i].log == id) {
if (!isADrone) { if (!isADrone) {
setDroneDataStr(var->value.data, s->log); setDroneDataStr(var->value.data, s->log);
} }
...@@ -566,7 +527,7 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool ...@@ -566,7 +527,7 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool
* arg 2 (string): network interface used for multicast communication * arg 2 (string): network interface used for multicast communication
* arg 3 (int): ID of the drone * arg 3 (int): ID of the drone
* arg 4 (double): publication/subscription interval in ms * arg 4 (double): publication/subscription interval in ms
* arg 5 (bool): true if there will be data to publish * arg 5 (bool): true if the peer is a drone
*/ */
static JSValue js_run_pubsub(JSContext *ctx, JSValueConst this_val, static JSValue js_run_pubsub(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv) { int argc, JSValueConst *argv) {
...@@ -578,7 +539,7 @@ static JSValue js_run_pubsub(JSContext *ctx, JSValueConst this_val, ...@@ -578,7 +539,7 @@ static JSValue js_run_pubsub(JSContext *ctx, JSValueConst this_val,
UA_UInt32 id; UA_UInt32 id;
UA_Duration interval; UA_Duration interval;
VariableStruct variables; VariableStruct variables;
InstanceData *instanceArray; VariableStruct *pubsubVariableArray;
UA_UInt32 nbPeer = nbDrone + nbSubscriber; UA_UInt32 nbPeer = nbDrone + nbSubscriber;
int res; int res;
...@@ -604,25 +565,35 @@ static JSValue js_run_pubsub(JSContext *ctx, JSValueConst this_val, ...@@ -604,25 +565,35 @@ static JSValue js_run_pubsub(JSContext *ctx, JSValueConst this_val,
isADrone = JS_ToBool(ctx, argv[5]); isADrone = JS_ToBool(ctx, argv[5]);
variables = isADrone ? droneVariables : subscriberVariables; variables = isADrone ? droneVariables : subscriberVariables;
instanceArray = (InstanceData *) malloc((nbPeer) * sizeof(InstanceData)); pubsubVariableArray = (VariableStruct *) malloc((nbPeer) * sizeof(VariableStruct));
pubsubIdsArray = (PubsubVariableIDs *) malloc((nbPeer) * sizeof(PubsubVariableIDs));
for(UA_UInt32 i = 0; i < nbDrone; i++) { for(UA_UInt32 i = 0; i < nbDrone; i++) {
instanceArray[i].variables = droneVariables; pubsubVariableArray[i] = droneVariables;
instanceArray[i].init_node_id = init_drone_node_id; PubsubVariableIDs ids = {
.message = (UA_UInt32) DRONE_VARIABLE_NB * i + VARIABLE_ID_BASE,
.positionArray = (UA_UInt32) DRONE_VARIABLE_NB * i + VARIABLE_ID_BASE + 1,
.speedArray = (UA_UInt32) DRONE_VARIABLE_NB * i + VARIABLE_ID_BASE + 2,
.log = (UA_UInt32) DRONE_VARIABLE_NB * i + VARIABLE_ID_BASE + 3,
};
pubsubIdsArray[i] = ids;
} }
for(UA_UInt32 i = nbDrone; i < nbPeer; i++) { for(UA_UInt32 i = nbDrone; i < nbPeer; i++) {
instanceArray[i].variables = subscriberVariables; pubsubVariableArray[i] = subscriberVariables;
instanceArray[i].init_node_id = init_subscriber_node_id; PubsubVariableIDs ids = {
.message = (UA_UInt32) DRONE_VARIABLE_NB * i + VARIABLE_ID_BASE,
};
pubsubIdsArray[i] = ids;
} }
pubsubExited = false; pubsubExited = false;
res = runPubsub(UA_Log_Stdout, &transportProfile, &networkAddressUrl, res = runPubsub(UA_Log_Stdout, &transportProfile, &networkAddressUrl,
variables, id, instanceArray, nbPeer, variables, id, pubsubVariableArray, pubsubIdsArray, nbPeer,
fmax(DRONE_VARIABLE_NB, SUBSCRIBER_VARIABLE_NB), interval, interval, get_drone_id, pubsub_get_value,
get_drone_id, pubsub_get_value, pubsub_update_variables, pubsub_update_variables, &pubsubShouldRun);
&pubsubShouldRun);
pubsubExited = true; pubsubExited = true;
free(instanceArray); free(pubsubVariableArray);
free(pubsubIdsArray);
JS_FreeCString(ctx, ipv6); JS_FreeCString(ctx, ipv6);
JS_FreeCString(ctx, port); JS_FreeCString(ctx, port);
free(notConstNetIface); free(notConstNetIface);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment