mod_io_opc_ua.h 30.4 KB
Newer Older
1 2 3 4
/*
 * OPC-UA code representation of MOD-IOs connected to a Lime2
 */

Ivan Tyagov's avatar
Ivan Tyagov committed
5 6
#include <open62541/server.h>

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
void addIntegerVariableNode(UA_Server *server, char *node_id, char *node_description)
{
    UA_Int32 myInteger = 0;
    UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
    UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);

    UA_VariableAttributes attr0 = UA_VariableAttributes_default;
    UA_Variant_setScalar(&attr0.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
    attr0.description = UA_LOCALIZEDTEXT("en-US", node_description);
    attr0.displayName = UA_LOCALIZEDTEXT("en-US", node_description);
    attr0.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
    attr0.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
    UA_NodeId myIntegerNodeId0 = UA_NODEID_STRING(1, node_id);
    UA_QualifiedName myIntegerName0 = UA_QUALIFIEDNAME(1, node_description);
    UA_Server_addVariableNode(server, myIntegerNodeId0, parentNodeId,
                              parentReferenceNodeId, myIntegerName0,
                              UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr0, NULL, NULL);
}

void addUIntegerVariableReadNode(UA_Server *server, char *node_id, char *node_description)
{
    UA_UInt32 myInteger = 0;
    UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
    UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);

    UA_VariableAttributes attr0 = UA_VariableAttributes_default;
    UA_Variant_setScalar(&attr0.value, &myInteger, &UA_TYPES[UA_TYPES_UINT32]);
    attr0.description = UA_LOCALIZEDTEXT("en-US", node_description);
    attr0.displayName = UA_LOCALIZEDTEXT("en-US", node_description);
    attr0.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
    attr0.accessLevel = UA_ACCESSLEVELMASK_READ;
    UA_NodeId myIntegerNodeId0 = UA_NODEID_STRING(1, node_id);
    UA_QualifiedName myIntegerName0 = UA_QUALIFIEDNAME(1, node_description);
    UA_Server_addVariableNode(server, myIntegerNodeId0, parentNodeId,
                              parentReferenceNodeId, myIntegerName0,
                              UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr0, NULL, NULL);
}

void addBooleanVariableReadNode(UA_Server *server, char *node_id, char *node_description)
{
    UA_Boolean myBoolean = false;
    UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
    UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);

    UA_VariableAttributes attr0 = UA_VariableAttributes_default;
    UA_Variant_setScalar(&attr0.value, &myBoolean, &UA_TYPES[UA_TYPES_BOOLEAN]);
    attr0.description = UA_LOCALIZEDTEXT("en-US", node_description);
    attr0.displayName = UA_LOCALIZEDTEXT("en-US", node_description);
    attr0.dataType = UA_TYPES[UA_TYPES_BOOLEAN].typeId;
    attr0.accessLevel = UA_ACCESSLEVELMASK_READ;
    UA_NodeId myIntegerNodeId0 = UA_NODEID_STRING(1, node_id);
    UA_QualifiedName myIntegerName0 = UA_QUALIFIEDNAME(1, node_description);
    UA_Server_addVariableNode(server, myIntegerNodeId0, parentNodeId,
                              parentReferenceNodeId, myIntegerName0,
                              UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr0, NULL, NULL);
}

static void addVariable(UA_Server *server)
{
    /* 
     * Create all variables representing MOD-IO's relays
     */
    int length = getI2CSlaveListLength();
    if (length >= 1)
    {
        // IC2-0
        addIntegerVariableNode(server, "i2c0.relay0", "I2C0 / Relay 0");
        addIntegerVariableNode(server, "i2c0.relay1", "I2C0 / Relay 1");
        addIntegerVariableNode(server, "i2c0.relay2", "I2C0 / Relay 2");
        addIntegerVariableNode(server, "i2c0.relay3", "I2C0 / Relay 3");
        addBooleanVariableReadNode(server, "i2c0.in0", "I2C0 / Digital Input 0");
        addBooleanVariableReadNode(server, "i2c0.in1", "I2C0 / Digital Input 1");
        addBooleanVariableReadNode(server, "i2c0.in2", "I2C0 / Digital Input 2");
        addBooleanVariableReadNode(server, "i2c0.in3", "I2C0 / Digital Input 3");
        addUIntegerVariableReadNode(server, "i2c0.ain0", "I2C0 / Analog Input 0");
        addUIntegerVariableReadNode(server, "i2c0.ain1", "I2C0 / Analog Input 1");
        addUIntegerVariableReadNode(server, "i2c0.ain2", "I2C0 / Analog Input 2");
        addUIntegerVariableReadNode(server, "i2c0.ain3", "I2C0 / Analog Input 3");
    }
    if (length >= 2)
    {
        // IC2-1
        addIntegerVariableNode(server, "i2c1.relay0", "I2C1 / Relay 0");
        addIntegerVariableNode(server, "i2c1.relay1", "I2C1 / Relay 1");
        addIntegerVariableNode(server, "i2c1.relay2", "I2C1 / Relay 2");
        addIntegerVariableNode(server, "i2c1.relay3", "I2C1 / Relay 3");
        addBooleanVariableReadNode(server, "i2c1.in0", "I2C1 / Digital Input 0");
        addBooleanVariableReadNode(server, "i2c1.in1", "I2C1 / Digital Input 1");
        addBooleanVariableReadNode(server, "i2c1.in2", "I2C1 / Digital Input 2");
        addBooleanVariableReadNode(server, "i2c1.in3", "I2C1 / Digital Input 3");
        addUIntegerVariableReadNode(server, "i2c1.ain0", "I2C1 / Analog Input 0");
        addUIntegerVariableReadNode(server, "i2c1.ain1", "I2C1 / Analog Input 1");
        addUIntegerVariableReadNode(server, "i2c1.ain2", "I2C1 / Analog Input 2");
        addUIntegerVariableReadNode(server, "i2c1.ain3", "I2C1 / Analog Input 3");
    }
}

/* Connect to variables to physical relays
 *
 */
static void beforeReadTime(UA_Server *server,
                           const UA_NodeId *sessionId, void *sessionContext,
                           const UA_NodeId *nodeid, void *nodeContext,
                           const UA_NumericRange *range, const UA_DataValue *data)
{
}
static void beforeReadTimeI2C0Ain0(UA_Server *server,
                                   const UA_NodeId *sessionId, void *sessionContext,
                                   const UA_NodeId *nodeid, void *nodeContext,
                                   const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[0];
    int *data_input = 0;
    uint8_t read_addr =0x30;
    if (!I2C_VIRTUAL_MODE) {
      getAnalogInputStateAIN(addr, &data_input, read_addr);
      if (data->value.type == &UA_TYPES[UA_TYPES_UINT32])
      {
        *(UA_UInt32 *)data->value.data = *data_input;
      }
    }
}

static void beforeReadTimeI2C0Ain1(UA_Server *server,
                                   const UA_NodeId *sessionId, void *sessionContext,
                                   const UA_NodeId *nodeid, void *nodeContext,
                                   const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[0];
    int *data_input = 0;
    uint8_t read_addr =0x31;
    if (!I2C_VIRTUAL_MODE) {
      getAnalogInputStateAIN(addr, &data_input, read_addr);
      if (data->value.type == &UA_TYPES[UA_TYPES_UINT32])
      {
        *(UA_UInt32 *)data->value.data = *data_input;
      }
    }
}

static void beforeReadTimeI2C0Ain2(UA_Server *server,
                                   const UA_NodeId *sessionId, void *sessionContext,
                                   const UA_NodeId *nodeid, void *nodeContext,
                                   const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[0];
    int *data_input = 0;
    uint8_t read_addr =0x32;
    if (!I2C_VIRTUAL_MODE) {
      getAnalogInputStateAIN(addr, &data_input, read_addr);
      if (data->value.type == &UA_TYPES[UA_TYPES_UINT32])
      {
        *(UA_UInt32 *)data->value.data = *data_input;
      }
    }
}

static void beforeReadTimeI2C0Ain3(UA_Server *server,
                                   const UA_NodeId *sessionId, void *sessionContext,
                                   const UA_NodeId *nodeid, void *nodeContext,
                                   const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[0];
    int *data_input = 0;
    uint8_t read_addr =0x33;
    if (!I2C_VIRTUAL_MODE) {
      getAnalogInputStateAIN(addr, &data_input, read_addr);
      if (data->value.type == &UA_TYPES[UA_TYPES_UINT32])
      {
        *(UA_UInt32 *)data->value.data = *data_input;
      }
    }
}
static void beforeReadTimeI2C1Ain0(UA_Server *server,
                                   const UA_NodeId *sessionId, void *sessionContext,
                                   const UA_NodeId *nodeid, void *nodeContext,
                                   const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[1];
    int *data_input = 0;
    uint8_t read_addr =0x30;
    if (!I2C_VIRTUAL_MODE) {
      getAnalogInputStateAIN(addr, &data_input, read_addr);
      if (data->value.type == &UA_TYPES[UA_TYPES_UINT32])
      {
        *(UA_UInt32 *)data->value.data = *data_input;
      }
    }
}

static void beforeReadTimeI2C1Ain1(UA_Server *server,
                                   const UA_NodeId *sessionId, void *sessionContext,
                                   const UA_NodeId *nodeid, void *nodeContext,
                                   const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[1];
    int *data_input = 0;    
    uint8_t read_addr =0x31;
    if (!I2C_VIRTUAL_MODE) {
      getAnalogInputStateAIN(addr, &data_input, read_addr);
      if (data->value.type == &UA_TYPES[UA_TYPES_UINT32])
      {
        *(UA_UInt32 *)data->value.data = *data_input;
      }
    }
}

static void beforeReadTimeI2C1Ain2(UA_Server *server,
                                   const UA_NodeId *sessionId, void *sessionContext,
                                   const UA_NodeId *nodeid, void *nodeContext,
                                   const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[1];
    int *data_input = 0;
    uint8_t read_addr =0x32;
    if (!I2C_VIRTUAL_MODE) {
      getAnalogInputStateAIN(addr, &data_input, read_addr);
      if (data->value.type == &UA_TYPES[UA_TYPES_UINT32])
      {
        *(UA_UInt32 *)data->value.data = *data_input;
      }
    }
}

static void beforeReadTimeI2C1Ain3(UA_Server *server,
                                   const UA_NodeId *sessionId, void *sessionContext,
                                   const UA_NodeId *nodeid, void *nodeContext,
                                   const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[1];
    int *data_input = 0;
    uint8_t read_addr =0x33;
    if (!I2C_VIRTUAL_MODE) {
      getAnalogInputStateAIN(addr, &data_input, read_addr);
      if (data->value.type == &UA_TYPES[UA_TYPES_UINT32])
      {
        *(UA_UInt32 *)data->value.data = *data_input;
      }
    }
}

static void beforeReadTimeI2C0In0(UA_Server *server,
                                  const UA_NodeId *sessionId, void *sessionContext,
                                  const UA_NodeId *nodeid, void *nodeContext,
                                  const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[0];
    char *data_input = 0;
    if (!I2C_VIRTUAL_MODE) {
      getDigitalInputState(addr, &data_input);
      if ((*data_input) & (1UL << 0))
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = true;
        }
      }
      else
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = false;
        }
      }
    }
}

static void beforeReadTimeI2C0In1(UA_Server *server,
                                  const UA_NodeId *sessionId, void *sessionContext,
                                  const UA_NodeId *nodeid, void *nodeContext,
                                  const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[0];
    char *data_input = 0;
    if (!I2C_VIRTUAL_MODE) {
      getDigitalInputState(addr, &data_input);
      if ((*data_input) & (1UL << 1))
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = true;
        }
      }
      else
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = false;
        }
      }
    }
}

static void beforeReadTimeI2C0In2(UA_Server *server,
                                  const UA_NodeId *sessionId, void *sessionContext,
                                  const UA_NodeId *nodeid, void *nodeContext,
                                  const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[0];
    char *data_input = 0;
    if (!I2C_VIRTUAL_MODE) {
      getDigitalInputState(addr, &data_input);
      if ((*data_input) & (1UL << 2))
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = true;
        }
      }
      else
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = false;
        }
      }
    }
}

static void beforeReadTimeI2C0In3(UA_Server *server,
                                  const UA_NodeId *sessionId, void *sessionContext,
                                  const UA_NodeId *nodeid, void *nodeContext,
                                  const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[0];
    char *data_input = 0;
    if (!I2C_VIRTUAL_MODE) {     
      getDigitalInputState(addr, &data_input);
      if ((*data_input) & (1UL << 3))
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = true;
        }
      }
      else
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = false;
        }
      }
    }
}
static void beforeReadTimeI2C1In0(UA_Server *server,
                                  const UA_NodeId *sessionId, void *sessionContext,
                                  const UA_NodeId *nodeid, void *nodeContext,
                                  const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[1];
    char *data_input = 0;
    if (!I2C_VIRTUAL_MODE) {   
      getDigitalInputState(addr, &data_input);
      if ((*data_input) & (1UL << 0))
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = true;
        }
      }
      else
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = false;
        }
      }
    }
}

static void beforeReadTimeI2C1In1(UA_Server *server,
                                  const UA_NodeId *sessionId, void *sessionContext,
                                  const UA_NodeId *nodeid, void *nodeContext,
                                  const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[1];
    char *data_input = 0;
    if (!I2C_VIRTUAL_MODE) {
      getDigitalInputState(addr, &data_input);
      if ((*data_input) & (1UL << 1))
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = true;
        }
      }
      else
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = false;
        }
      }
    }
}

static void beforeReadTimeI2C1In2(UA_Server *server,
                                  const UA_NodeId *sessionId, void *sessionContext,
                                  const UA_NodeId *nodeid, void *nodeContext,
                                  const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[1];
    char *data_input = 0;
    if (!I2C_VIRTUAL_MODE) {
      getDigitalInputState(addr, &data_input);
      if ((*data_input) & (1UL << 2))
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = true;
        }
      }
      else
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = false;
        }
      }
    }
}

static void beforeReadTimeI2C1In3(UA_Server *server,
                                  const UA_NodeId *sessionId, void *sessionContext,
                                  const UA_NodeId *nodeid, void *nodeContext,
                                  const UA_NumericRange *range, const UA_DataValue *data)
{
    int addr = I2C_SLAVE_ADDR_LIST[1];
    char *data_input = 0;
    if (!I2C_VIRTUAL_MODE) {
      getDigitalInputState(addr, &data_input);
      if ((*data_input) & (1UL << 3))
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = true;
        }
      }
      else
      {
        if (data->value.type == &UA_TYPES[UA_TYPES_BOOLEAN])
        {
            *(UA_Boolean *)data->value.data = false;
        }
      }
    }
}

static void afterWriteTime(UA_Server *server,
                           const UA_NodeId *sessionId, void *sessionContext,
                           const UA_NodeId *nodeId, void *nodeContext,
                           const UA_NumericRange *range, const UA_DataValue *data)
{
    // nothing to do as not yet needed.
}

// XXX: having afterWriteTime{0..3} is not needed and maybe with introspection of context we can
// write only one callback function
static void afterWriteTimeI2C0_0(UA_Server *server,
                                 const UA_NodeId *sessionId, void *sessionContext,
                                 const UA_NodeId *nodeId, void *nodeContext,
                                 const UA_NumericRange *range, const UA_DataValue *data)
{
    if (data->value.type == &UA_TYPES[UA_TYPES_INT32])
    {
        UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
        int addr = I2C_SLAVE_ADDR_LIST[0];
        if (hrValue > 0)
        {
            I2C_0_RELAYS_STATE |= 1UL << 0;
            setRelayState(I2C_0_RELAYS_STATE, addr);
        }
        else
        {
            I2C_0_RELAYS_STATE &= ~(1UL << 0);
            setRelayState(I2C_0_RELAYS_STATE, addr);
        }
    }
}

static void afterWriteTimeI2C0_1(UA_Server *server,
                                 const UA_NodeId *sessionId, void *sessionContext,
                                 const UA_NodeId *nodeId, void *nodeContext,
                                 const UA_NumericRange *range, const UA_DataValue *data)
{
    if (data->value.type == &UA_TYPES[UA_TYPES_INT32])
    {
        UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
        int addr = I2C_SLAVE_ADDR_LIST[0];
        if (hrValue > 0)
        {
            I2C_0_RELAYS_STATE |= 1UL << 1;
            setRelayState(I2C_0_RELAYS_STATE, addr);
        }
        else
        {
            I2C_0_RELAYS_STATE &= ~(1UL << 1);
            setRelayState(I2C_0_RELAYS_STATE, addr);
        }
    }
}

static void afterWriteTimeI2C0_2(UA_Server *server,
                                 const UA_NodeId *sessionId, void *sessionContext,
                                 const UA_NodeId *nodeId, void *nodeContext,
                                 const UA_NumericRange *range, const UA_DataValue *data)
{
    if (data->value.type == &UA_TYPES[UA_TYPES_INT32])
    {
        UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
        int addr = I2C_SLAVE_ADDR_LIST[0];
        if (hrValue > 0)
        {
            I2C_0_RELAYS_STATE |= 1UL << 2;
            setRelayState(I2C_0_RELAYS_STATE, addr);
        }
        else
        {
            I2C_0_RELAYS_STATE &= ~(1UL << 2);
            setRelayState(I2C_0_RELAYS_STATE, addr);
        }
    }
}

static void afterWriteTimeI2C0_3(UA_Server *server,
                                 const UA_NodeId *sessionId, void *sessionContext,
                                 const UA_NodeId *nodeId, void *nodeContext,
                                 const UA_NumericRange *range, const UA_DataValue *data)
{
    if (data->value.type == &UA_TYPES[UA_TYPES_INT32])
    {
        UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
        int addr = I2C_SLAVE_ADDR_LIST[0];
        if (hrValue > 0)
        {
            I2C_0_RELAYS_STATE |= 1UL << 3;
            setRelayState(I2C_0_RELAYS_STATE, addr);
        }
        else
        {
            I2C_0_RELAYS_STATE &= ~(1UL << 3);
            setRelayState(I2C_0_RELAYS_STATE, addr);
        }
    }
}

// I2C1
static void afterWriteTimeI2C1_0(UA_Server *server,
                                 const UA_NodeId *sessionId, void *sessionContext,
                                 const UA_NodeId *nodeId, void *nodeContext,
                                 const UA_NumericRange *range, const UA_DataValue *data)
{
    if (data->value.type == &UA_TYPES[UA_TYPES_INT32])
    {
        UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
        int addr = I2C_SLAVE_ADDR_LIST[1];
        if (hrValue > 0)
        {
            I2C_1_RELAYS_STATE |= 1UL << 0;
            setRelayState(I2C_1_RELAYS_STATE, addr);
        }
        else
        {
            I2C_1_RELAYS_STATE &= ~(1UL << 0);
            setRelayState(I2C_1_RELAYS_STATE, addr);
        }
    }
}

static void afterWriteTimeI2C1_1(UA_Server *server,
                                 const UA_NodeId *sessionId, void *sessionContext,
                                 const UA_NodeId *nodeId, void *nodeContext,
                                 const UA_NumericRange *range, const UA_DataValue *data)
{
    if (data->value.type == &UA_TYPES[UA_TYPES_INT32])
    {
        UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
        int addr = I2C_SLAVE_ADDR_LIST[1];
        if (hrValue > 0)
        {
            I2C_1_RELAYS_STATE |= 1UL << 1;
            setRelayState(I2C_1_RELAYS_STATE, addr);
        }
        else
        {
            I2C_1_RELAYS_STATE &= ~(1UL << 1);
            setRelayState(I2C_1_RELAYS_STATE, addr);
        }
    }
}

static void afterWriteTimeI2C1_2(UA_Server *server,
                                 const UA_NodeId *sessionId, void *sessionContext,
                                 const UA_NodeId *nodeId, void *nodeContext,
                                 const UA_NumericRange *range, const UA_DataValue *data)
{
    if (data->value.type == &UA_TYPES[UA_TYPES_INT32])
    {
        UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
        int addr = I2C_SLAVE_ADDR_LIST[1];
        if (hrValue > 0)
        {
            I2C_1_RELAYS_STATE |= 1UL << 2;
            setRelayState(I2C_1_RELAYS_STATE, addr);
        }
        else
        {
            I2C_1_RELAYS_STATE &= ~(1UL << 2);
            setRelayState(I2C_1_RELAYS_STATE, addr);
        }
    }
}

static void afterWriteTimeI2C1_3(UA_Server *server,
                                 const UA_NodeId *sessionId, void *sessionContext,
                                 const UA_NodeId *nodeId, void *nodeContext,
                                 const UA_NumericRange *range, const UA_DataValue *data)
{
    if (data->value.type == &UA_TYPES[UA_TYPES_INT32])
    {
        UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
        int addr = I2C_SLAVE_ADDR_LIST[1];
        if (hrValue > 0)
        {
            I2C_1_RELAYS_STATE |= 1UL << 3;
            setRelayState(I2C_1_RELAYS_STATE, addr);
        }
        else
        {
            I2C_1_RELAYS_STATE &= ~(1UL << 3);
            setRelayState(I2C_1_RELAYS_STATE, addr);
        }
    }
}

static void addValueCallbackToCurrentTimeVariable(UA_Server *server)
{
    int length = getI2CSlaveListLength();
    // I2C0
    // relay 0
    UA_NodeId currentNodeId0 = UA_NODEID_STRING(1, "i2c0.relay0");
    UA_ValueCallback callback0;
    callback0.onRead = beforeReadTime;
    callback0.onWrite = afterWriteTimeI2C0_0;
    UA_Server_setVariableNode_valueCallback(server, currentNodeId0, callback0);

    // relay 1
    UA_NodeId currentNodeId1 = UA_NODEID_STRING(1, "i2c0.relay1");
    UA_ValueCallback callback1;
    callback1.onRead = beforeReadTime;
    callback1.onWrite = afterWriteTimeI2C0_1;
    UA_Server_setVariableNode_valueCallback(server, currentNodeId1, callback1);

    // relay 2
    UA_NodeId currentNodeId2 = UA_NODEID_STRING(1, "i2c0.relay2");
    UA_ValueCallback callback2;
    callback2.onRead = beforeReadTime;
    callback2.onWrite = afterWriteTimeI2C0_2;
    UA_Server_setVariableNode_valueCallback(server, currentNodeId2, callback2);

    // relay 3
    UA_NodeId currentNodeId3 = UA_NODEID_STRING(1, "i2c0.relay3");
    UA_ValueCallback callback3;
    callback3.onRead = beforeReadTime;
    callback3.onWrite = afterWriteTimeI2C0_3;
    UA_Server_setVariableNode_valueCallback(server, currentNodeId3, callback3);

    // Digital input 0
    UA_NodeId currentNodeId4 = UA_NODEID_STRING(1, "i2c0.in0");
    UA_ValueCallback callback4;
    callback4.onRead = beforeReadTimeI2C0In0;
    callback4.onWrite = afterWriteTime;
    UA_Server_setVariableNode_valueCallback(server, currentNodeId4, callback4);

    // Digital input 1
    UA_NodeId currentNodeId5 = UA_NODEID_STRING(1, "i2c0.in1");
    UA_ValueCallback callback5;
    callback5.onRead = beforeReadTimeI2C0In1;
    callback5.onWrite = afterWriteTime;
    UA_Server_setVariableNode_valueCallback(server, currentNodeId5, callback5);

    // Digital input 2
    UA_NodeId currentNodeId6 = UA_NODEID_STRING(1, "i2c0.in2");
    UA_ValueCallback callback6;
    callback6.onRead = beforeReadTimeI2C0In2;
    callback6.onWrite = afterWriteTime;
    UA_Server_setVariableNode_valueCallback(server, currentNodeId6, callback6);

    // Digital input 3
    UA_NodeId currentNodeId7 = UA_NODEID_STRING(1, "i2c0.in3");
    UA_ValueCallback callback7;
    callback7.onRead = beforeReadTimeI2C0In3;
    callback7.onWrite = afterWriteTime;
    UA_Server_setVariableNode_valueCallback(server, currentNodeId7, callback7);

    // Analog input 0
    UA_NodeId currentNodeId8 = UA_NODEID_STRING(1, "i2c0.ain0");
    UA_ValueCallback callback8;
    callback8.onRead = beforeReadTimeI2C0Ain0;
    callback8.onWrite = afterWriteTime;
    UA_Server_setVariableNode_valueCallback(server, currentNodeId8, callback8);

    // Analog input 1
    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);
    }
}