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 @@
typedef struct {
UA_UInt16 id;
UA_UInt32 positionArrayId;
UA_UInt32 speedArrayId;
UA_Double latitude;
UA_Double longitude;
UA_Double altitudeAbs;
......@@ -30,9 +28,7 @@ typedef struct {
UA_Float speed;
UA_Float climbRate;
char message[MAX_STR_SIZE];
UA_UInt32 messageId;
char log[MAX_STR_SIZE];
UA_UInt32 logId;
} JSDroneData;
typedef struct {
......@@ -42,6 +38,16 @@ typedef struct {
UA_Int64 timestamp;
} JSPositionData;
typedef union {
struct {
UA_UInt32 message;
UA_UInt32 positionArray;
UA_UInt32 speedArray;
UA_UInt32 log;
};
UA_UInt32 getter[4];
} PubsubVariableIDs;
typedef struct {
char* name;
char* typeName;
......@@ -64,16 +70,12 @@ typedef struct {
VariableData *variableArray;
} 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,
UA_NetworkAddressUrlDataType *networkAddressUrl,
VariableStruct variables, UA_UInt32 id,
InstanceData *readerArray, UA_UInt32 nbReader,
UA_UInt32 maxVariableNb, UA_Duration interval,
VariableStruct *pubsubVariableArray,
PubsubVariableIDs *pubsubIDArray,
UA_UInt32 nbReader, UA_Duration interval,
UA_UInt16 (*get_reader_id)(UA_UInt32 nb),
VariableData (*get_value)(UA_String identifier),
void (*update)(UA_UInt32 id, const UA_DataValue*, bool print),
......@@ -85,10 +87,6 @@ UA_String get_log(void);
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);
DLL_PUBLIC JSModuleDef *js_init_module(JSContext *ctx, const char *module_name);
......
......@@ -260,9 +260,9 @@ dataChangeNotificationCallback(UA_Server *server, UA_UInt32 monitoredItemId,
* Add subscribedvariables to the DataSetReader */
static UA_StatusCode
addSubscribedVariables(UA_Server *server, UA_NodeId dataSetReaderId,
VariableData *variableArray, UA_UInt32 nb,
UA_UInt32 maxVariableNb, UA_Duration samplingInterval,
void (*init_node_id)(UA_UInt32 id, UA_UInt32 nb, UA_UInt32 magic)) {
VariableData *variableArray,
PubsubVariableIDs pubsubIDs, UA_UInt32 nb,
UA_Duration samplingInterval) {
if(server == NULL)
return UA_STATUSCODE_BADINTERNALERROR;
......@@ -310,7 +310,7 @@ addSubscribedVariables(UA_Server *server, UA_NodeId dataSetReaderId,
UA_NodeId newNode;
retval |= UA_Server_addVariableNode(server,
UA_NODEID_NUMERIC(1, (UA_UInt32)maxVariableNb*nb + i + 50000),
UA_NODEID_NUMERIC(1, pubsubIDs.getter[i]),
folderId,
UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
UA_QUALIFIEDNAME(1, (char *)readerConfig.dataSetMetaData.fields[i].name.data),
......@@ -319,7 +319,6 @@ addSubscribedVariables(UA_Server *server, UA_NodeId dataSetReaderId,
/*monitor variable*/
UA_MonitoredItemCreateRequest monRequest = UA_MonitoredItemCreateRequest_default(newNode);
init_node_id(newNode.identifier.numeric, nb, i);
monRequest.requestedParameters.samplingInterval = samplingInterval;
UA_Server_createDataChangeMonitoredItem(server, UA_TIMESTAMPSTORETURN_SOURCE,
monRequest, NULL, dataChangeNotificationCallback);
......@@ -430,8 +429,9 @@ setServer(UA_String *transportProfile,
}
static UA_StatusCode
subscribe(UA_Server *server, InstanceData *instanceArray, UA_UInt32 id,
UA_UInt32 nbReader, UA_UInt32 maxVariableNb, UA_Duration interval,
subscribe(UA_Server *server, VariableStruct *pubsubVariableArray,
PubsubVariableIDs *pubsubIDArray, UA_UInt32 id,
UA_UInt32 nbReader, UA_Duration interval,
UA_UInt16 (*get_reader_id)(UA_UInt32 nb),
void (*update)(UA_UInt32 id, const UA_DataValue*, bool print)) {
UA_UInt16 publisherIdent;
......@@ -455,16 +455,15 @@ subscribe(UA_Server *server, InstanceData *instanceArray, UA_UInt32 id,
readerConfig.name = UA_STRING(readerName);
readerConfig.publisherId.data = &publisherIdent;
retval = addDataSetReader(server, instanceArray[i].variables,
retval = addDataSetReader(server, pubsubVariableArray[i],
publisherIdent);
if (retval != UA_STATUSCODE_GOOD)
return EXIT_FAILURE;
/* Add SubscribedVariables to the created DataSetReader */
retval = addSubscribedVariables(server, readerIdent,
instanceArray[i].variables.variableArray,
i, maxVariableNb, interval,
instanceArray[i].init_node_id);
pubsubVariableArray[i].variableArray,
pubsubIDArray[i], i, interval);
if (retval != UA_STATUSCODE_GOOD)
return EXIT_FAILURE;
}
......@@ -475,8 +474,9 @@ subscribe(UA_Server *server, InstanceData *instanceArray, UA_UInt32 id,
int runPubsub(const UA_Logger *logger, UA_String *transportProfile,
UA_NetworkAddressUrlDataType *networkAddressUrl,
VariableStruct variables, UA_UInt32 id,
InstanceData *readerArray, UA_UInt32 nbReader,
UA_UInt32 maxVariableNb, UA_Duration interval,
VariableStruct *pubsubVariableArray,
PubsubVariableIDs *pubsubIDArray,
UA_UInt32 nbReader, UA_Duration interval,
UA_UInt16 (*get_reader_id)(UA_UInt32 nb),
VariableData (*get_value)(UA_String identifier),
void (*update)(UA_UInt32 id, const UA_DataValue*, bool print),
......@@ -507,8 +507,8 @@ int runPubsub(const UA_Logger *logger, UA_String *transportProfile,
/* Subscribing */
subscribe(server, readerArray, id, nbReader, maxVariableNb, interval,
get_reader_id, update);
subscribe(server, pubsubVariableArray, pubsubIDArray, id, nbReader,
interval, get_reader_id, update);
retval = UA_Server_run(server, running);
UA_Server_delete(server);
......
......@@ -30,6 +30,17 @@ UA_String initial_ua_string = {
};
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",
.typeName = "Position Array Type",
......@@ -58,17 +69,6 @@ VariableData droneVariableArray[] = {
.getter.getArray = (void * (*)(void))getSpeedArray,
#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",
.description = "Message to send to the other drones",
......@@ -131,6 +131,9 @@ pthread_cond_t threadCond;
bool isADrone;
PubsubVariableIDs *pubsubIdsArray;
const UA_UInt32 VARIABLE_ID_BASE = 50000;
// Position class functions
static void js_position_finalizer(JSRuntime *rt, JSValue val)
......@@ -457,48 +460,6 @@ VariableData pubsub_get_value(UA_String identifier) {
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)
{
UA_String uaStr = *(UA_String*) data;
......@@ -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++) {
s = (JSDroneData *) JS_GetOpaque(droneObjectIdList[i], jsDroneClassId);
if (s->positionArrayId == id) {
if (pubsubIdsArray[i].positionArray == id) {
updatedPositionArray = (UA_Int64*) var->value.data;
s->latitude = (double)updatedPositionArray[LATITUDE] / 1e7;
s->longitude = (double)updatedPositionArray[LONGITUDE] / 1e7;
......@@ -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);
}
return;
} else if (s->speedArrayId == id) {
} else if (pubsubIdsArray[i].speedArray == id) {
updatedSpeedArray = (UA_Float*) var->value.data;
s->yaw = updatedSpeedArray[YAW];
s->speed = updatedSpeedArray[SPEED];
......@@ -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);
}
return;
} else if (s->messageId == id) {
} else if (pubsubIdsArray[i].message == id) {
setDroneDataStr(var->value.data, s->message);
return;
} else if (s->logId == id) {
} else if (pubsubIdsArray[i].log == id) {
if (!isADrone) {
setDroneDataStr(var->value.data, s->log);
}
......@@ -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 3 (int): ID of the drone
* 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,
int argc, JSValueConst *argv) {
......@@ -578,7 +539,7 @@ static JSValue js_run_pubsub(JSContext *ctx, JSValueConst this_val,
UA_UInt32 id;
UA_Duration interval;
VariableStruct variables;
InstanceData *instanceArray;
VariableStruct *pubsubVariableArray;
UA_UInt32 nbPeer = nbDrone + nbSubscriber;
int res;
......@@ -604,25 +565,35 @@ static JSValue js_run_pubsub(JSContext *ctx, JSValueConst this_val,
isADrone = JS_ToBool(ctx, argv[5]);
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++) {
instanceArray[i].variables = droneVariables;
instanceArray[i].init_node_id = init_drone_node_id;
pubsubVariableArray[i] = droneVariables;
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++) {
instanceArray[i].variables = subscriberVariables;
instanceArray[i].init_node_id = init_subscriber_node_id;
pubsubVariableArray[i] = subscriberVariables;
PubsubVariableIDs ids = {
.message = (UA_UInt32) DRONE_VARIABLE_NB * i + VARIABLE_ID_BASE,
};
pubsubIdsArray[i] = ids;
}
pubsubExited = false;
res = runPubsub(UA_Log_Stdout, &transportProfile, &networkAddressUrl,
variables, id, instanceArray, nbPeer,
fmax(DRONE_VARIABLE_NB, SUBSCRIBER_VARIABLE_NB), interval,
get_drone_id, pubsub_get_value, pubsub_update_variables,
&pubsubShouldRun);
variables, id, pubsubVariableArray, pubsubIdsArray, nbPeer,
interval, get_drone_id, pubsub_get_value,
pubsub_update_variables, &pubsubShouldRun);
pubsubExited = true;
free(instanceArray);
free(pubsubVariableArray);
free(pubsubIdsArray);
JS_FreeCString(ctx, ipv6);
JS_FreeCString(ctx, port);
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