Commit f4f1f65e authored by Ivan Tyagov's avatar Ivan Tyagov

Handle callbacks dynamically.

See merge request !42
parents 00a5d1a4 711fe800
...@@ -12,7 +12,7 @@ const int DEFAULT_I2C_SLAVE_ADDR = 0x58; ...@@ -12,7 +12,7 @@ const int DEFAULT_I2C_SLAVE_ADDR = 0x58;
int I2C_SLAVE_ADDR_LIST[127] = {0}; int I2C_SLAVE_ADDR_LIST[127] = {0};
// the number of physical relays // the number of physical relays
int DEFAULT_RELAY_COUNT = 4; const int DEFAULT_RELAY_COUNT = 4;
// the block device at host machine // the block device at host machine
static char *DEFAULT_I2C_BLOCK_DEVICE_NAME = "/dev/i2c-1"; static char *DEFAULT_I2C_BLOCK_DEVICE_NAME = "/dev/i2c-1";
......
...@@ -374,177 +374,72 @@ static void afterWriteTimeI2C1_3(UA_Server *server, ...@@ -374,177 +374,72 @@ static void afterWriteTimeI2C1_3(UA_Server *server,
static void addValueCallbackToCurrentTimeVariable(UA_Server *server) static void addValueCallbackToCurrentTimeVariable(UA_Server *server)
{ {
int length = getI2CSlaveListLength(); int device_slave_list_length = getI2CSlaveListLength();
// I2C0 int device_number, offset_number;
// relay 0 char *node_id = malloc(sizeof(char) * 10);
UA_NodeId currentNodeId0 = UA_NODEID_STRING(1, "i2c0.relay0");
UA_ValueCallback callback0; // list of relays, digital and analog callbacks for all devices (XXX: for now only two, add more!)
callback0.onRead = beforeReadTime; static void (*callables[24])
callback0.onWrite = afterWriteTimeI2C0_0; (UA_Server *server, const UA_NodeId *sessionId, void *sessionContext,
UA_Server_setVariableNode_valueCallback(server, currentNodeId0, callback0); const UA_NodeId *nodeId, void *nodeContext, const UA_NumericRange *range,
const UA_DataValue *data) = {// relays - IC0
// relay 1 afterWriteTimeI2C0_0,
UA_NodeId currentNodeId1 = UA_NODEID_STRING(1, "i2c0.relay1"); afterWriteTimeI2C0_1,
UA_ValueCallback callback1; afterWriteTimeI2C0_2,
callback1.onRead = beforeReadTime; afterWriteTimeI2C0_3,
callback1.onWrite = afterWriteTimeI2C0_1; // digital - IC0
UA_Server_setVariableNode_valueCallback(server, currentNodeId1, callback1); beforeReadTimeI2C0In0,
beforeReadTimeI2C0In1,
// relay 2 beforeReadTimeI2C0In2,
UA_NodeId currentNodeId2 = UA_NODEID_STRING(1, "i2c0.relay2"); beforeReadTimeI2C0In3,
UA_ValueCallback callback2; // analong - IC0
callback2.onRead = beforeReadTime; beforeReadTimeI2C0Ain0,
callback2.onWrite = afterWriteTimeI2C0_2; beforeReadTimeI2C0Ain1,
UA_Server_setVariableNode_valueCallback(server, currentNodeId2, callback2); beforeReadTimeI2C0Ain2,
beforeReadTimeI2C0Ain3,
// relay 3
UA_NodeId currentNodeId3 = UA_NODEID_STRING(1, "i2c0.relay3"); // relays - IC1
UA_ValueCallback callback3; afterWriteTimeI2C1_0,
callback3.onRead = beforeReadTime; afterWriteTimeI2C1_1,
callback3.onWrite = afterWriteTimeI2C0_3; afterWriteTimeI2C1_2,
UA_Server_setVariableNode_valueCallback(server, currentNodeId3, callback3); afterWriteTimeI2C1_3,
// digital - IC1
// Digital input 0 beforeReadTimeI2C1In0,
UA_NodeId currentNodeId4 = UA_NODEID_STRING(1, "i2c0.in0"); beforeReadTimeI2C1In1,
UA_ValueCallback callback4; beforeReadTimeI2C1In2,
callback4.onRead = beforeReadTimeI2C0In0; beforeReadTimeI2C1In3,
callback4.onWrite = afterWriteTime; // analog - IC1
UA_Server_setVariableNode_valueCallback(server, currentNodeId4, callback4); beforeReadTimeI2C1Ain0,
beforeReadTimeI2C1Ain1,
// Digital input 1 beforeReadTimeI2C1Ain2,
UA_NodeId currentNodeId5 = UA_NODEID_STRING(1, "i2c0.in1"); beforeReadTimeI2C1Ain3};
UA_ValueCallback callback5;
callback5.onRead = beforeReadTimeI2C0In1; // set proper callbacks from list of all
callback5.onWrite = afterWriteTime; for (device_number = 0; device_number < device_slave_list_length; device_number++){
UA_Server_setVariableNode_valueCallback(server, currentNodeId5, callback5); for (offset_number = 0; offset_number < 4; offset_number++){
// relays
// Digital input 2 sprintf(node_id, "i2c%d.relay%d", device_number, offset_number);
UA_NodeId currentNodeId6 = UA_NODEID_STRING(1, "i2c0.in2"); UA_NodeId currentNodeId = UA_NODEID_STRING(1, node_id);
UA_ValueCallback callback6; UA_ValueCallback callback;
callback6.onRead = beforeReadTimeI2C0In2; callback.onRead = beforeReadTime;
callback6.onWrite = afterWriteTime; callback.onWrite = callables[device_number * 12 + offset_number];
UA_Server_setVariableNode_valueCallback(server, currentNodeId6, callback6); UA_Server_setVariableNode_valueCallback(server, currentNodeId, callback);
// Digital input 3 // digital inputs
UA_NodeId currentNodeId7 = UA_NODEID_STRING(1, "i2c0.in3"); sprintf(node_id, "i2c%d.in%d", device_number, offset_number);
UA_ValueCallback callback7; UA_NodeId currentNodeId1 = UA_NODEID_STRING(1, node_id);
callback7.onRead = beforeReadTimeI2C0In3; UA_ValueCallback callback1;
callback7.onWrite = afterWriteTime; callback1.onRead = callables[device_number * 12 + offset_number + 4];
UA_Server_setVariableNode_valueCallback(server, currentNodeId7, callback7); callback1.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId1, callback1);
// Analog input 0
UA_NodeId currentNodeId8 = UA_NODEID_STRING(1, "i2c0.ain0"); // analog inputs
UA_ValueCallback callback8; sprintf(node_id, "i2c%d.ain%d", device_number, offset_number);
callback8.onRead = beforeReadTimeI2C0Ain0; UA_NodeId currentNodeId2 = UA_NODEID_STRING(1, node_id);
callback8.onWrite = afterWriteTime; UA_ValueCallback callback2;
UA_Server_setVariableNode_valueCallback(server, currentNodeId8, callback8); callback2.onRead = callables[device_number * 12 + offset_number + 8];
callback2.onWrite = afterWriteTime;
// Analog input 1 UA_Server_setVariableNode_valueCallback(server, currentNodeId2, callback2);
UA_NodeId currentNodeId9 = UA_NODEID_STRING(1, "i2c0.ain1");
UA_ValueCallback callback9;
callback9.onRead = beforeReadTimeI2C0Ain1;
callback9.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId9, callback9);
// Analog input 2
UA_NodeId currentNodeId10 = UA_NODEID_STRING(1, "i2c0.ain2");
UA_ValueCallback callback10;
callback10.onRead = beforeReadTimeI2C0Ain2;
callback10.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId10, callback10);
// Analog input 3
UA_NodeId currentNodeId11 = UA_NODEID_STRING(1, "i2c0.ain3");
UA_ValueCallback callback11;
callback11.onRead = beforeReadTimeI2C0Ain3;
callback11.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId11, callback11);
if (length > 1)
{
// I2C1
// relay 0
UA_NodeId currentNodeId12 = UA_NODEID_STRING(1, "i2c1.relay0");
UA_ValueCallback callback12;
callback12.onRead = beforeReadTime;
callback12.onWrite = afterWriteTimeI2C1_0;
UA_Server_setVariableNode_valueCallback(server, currentNodeId12, callback12);
// relay 1
UA_NodeId currentNodeId13 = UA_NODEID_STRING(1, "i2c1.relay1");
UA_ValueCallback callback13;
callback13.onRead = beforeReadTime;
callback13.onWrite = afterWriteTimeI2C1_1;
UA_Server_setVariableNode_valueCallback(server, currentNodeId13, callback13);
// relay 2
UA_NodeId currentNodeId14 = UA_NODEID_STRING(1, "i2c1.relay2");
UA_ValueCallback callback14;
callback14.onRead = beforeReadTime;
callback14.onWrite = afterWriteTimeI2C1_2;
UA_Server_setVariableNode_valueCallback(server, currentNodeId14, callback14);
// relay 2
UA_NodeId currentNodeId15 = UA_NODEID_STRING(1, "i2c1.relay3");
UA_ValueCallback callback15;
callback15.onRead = beforeReadTime;
callback15.onWrite = afterWriteTimeI2C1_3;
UA_Server_setVariableNode_valueCallback(server, currentNodeId15, callback15);
// Digital input 0
UA_NodeId currentNodeId16 = UA_NODEID_STRING(1, "i2c1.in0");
UA_ValueCallback callback16;
callback16.onRead = beforeReadTimeI2C1In0;
callback16.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId16, callback16);
// Digital input 1
UA_NodeId currentNodeId17 = UA_NODEID_STRING(1, "i2c1.in1");
UA_ValueCallback callback17;
callback17.onRead = beforeReadTimeI2C1In1;
callback17.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId17, callback17);
// Digital input 2
UA_NodeId currentNodeId18 = UA_NODEID_STRING(1, "i2c1.in2");
UA_ValueCallback callback18;
callback18.onRead = beforeReadTimeI2C1In2;
callback18.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId18, callback18);
// Digital input 3
UA_NodeId currentNodeId19 = UA_NODEID_STRING(1, "i2c1.in3");
UA_ValueCallback callback19;
callback19.onRead = beforeReadTimeI2C1In3;
callback19.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId19, callback19);
// Analog input 0
UA_NodeId currentNodeId20 = UA_NODEID_STRING(1, "i2c1.ain0");
UA_ValueCallback callback20;
callback20.onRead = beforeReadTimeI2C1Ain0;
callback20.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId20, callback20);
// Analog input 1
UA_NodeId currentNodeId21 = UA_NODEID_STRING(1, "i2c1.ain1");
UA_ValueCallback callback21;
callback21.onRead = beforeReadTimeI2C1Ain1;
callback21.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId21, callback21);
// Analog input 2
UA_NodeId currentNodeId22 = UA_NODEID_STRING(1, "i2c1.ain2");
UA_ValueCallback callback22;
callback22.onRead = beforeReadTimeI2C1Ain2;
callback22.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId22, callback22);
// Analog input 3
UA_NodeId currentNodeId23 = UA_NODEID_STRING(1, "i2c1.ain3");
UA_ValueCallback callback23;
callback23.onRead = beforeReadTimeI2C1Ain3;
callback23.onWrite = afterWriteTime;
UA_Server_setVariableNode_valueCallback(server, currentNodeId23, callback23);
} }
}
} }
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