opc_ua/opcua_client_maker.py
branchwxPython4
changeset 3391 0ae5a15efa18
parent 3379 c3d462118d37
child 3405 fdc12f7d27c8
equal deleted inserted replaced
3389:da4f9cbec3b9 3391:0ae5a15efa18
   486 #include <open62541/plugin/log_stdout.h>
   486 #include <open62541/plugin/log_stdout.h>
   487 
   487 
   488 UA_Client *client;
   488 UA_Client *client;
   489 
   489 
   490 #define DECL_VAR(ua_type, C_type, c_loc_name)                                                       \\
   490 #define DECL_VAR(ua_type, C_type, c_loc_name)                                                       \\
   491 UA_Variant *c_loc_name##_variant;                                                                   \\
   491 UA_Variant c_loc_name##_variant;                                                                    \\
   492 C_type c_loc_name##_buf = 0;                                                                        \\
   492 C_type c_loc_name##_buf = 0;                                                                        \\
   493 C_type *c_loc_name = &c_loc_name##_buf;
   493 C_type *c_loc_name = &c_loc_name##_buf;
   494 
   494 
   495 %(decl)s
   495 %(decl)s
   496 
       
   497 #define FREE_VARIANT(ua_type, c_loc_name)                                                           \\
       
   498     UA_Variant_delete(c_loc_name##_variant);
       
   499 
   496 
   500 void __cleanup_%(locstr)s(void)
   497 void __cleanup_%(locstr)s(void)
   501 {
   498 {
   502     UA_Client_disconnect(client);
   499     UA_Client_disconnect(client);
   503     UA_Client_delete(client);
   500     UA_Client_delete(client);
   504 %(cleanup)s
       
   505 }
   501 }
   506 
   502 
   507 
   503 
   508 #define ALLOC_VARIANT(ua_type, c_loc_name)                                                          \\
   504 #define INIT_READ_VARIANT(ua_type, c_loc_name)                                                          \\
   509     c_loc_name##_variant = UA_Variant_new();
   505     UA_Variant_init(&c_loc_name##_variant);
       
   506 
       
   507 #define INIT_WRITE_VARIANT(ua_type, ua_type_enum, c_loc_name)       \\
       
   508     UA_Variant_setScalar(&c_loc_name##_variant, (ua_type*)c_loc_name, &UA_TYPES[ua_type_enum]);
   510 
   509 
   511 int __init_%(locstr)s(int argc,char **argv)
   510 int __init_%(locstr)s(int argc,char **argv)
   512 {
   511 {
   513     UA_StatusCode retval;
   512     UA_StatusCode retval;
   514     client = UA_Client_new();
   513     client = UA_Client_new();
   521         UA_Client_delete(client);
   520         UA_Client_delete(client);
   522         return EXIT_FAILURE;
   521         return EXIT_FAILURE;
   523     }
   522     }
   524 }
   523 }
   525 
   524 
   526 #define READ_VALUE(ua_type, ua_type_enum, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id)         \\
   525 #define READ_VALUE(ua_type, ua_type_enum, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id)        \\
   527     retval = UA_Client_readValueAttribute(                                                          \\
   526     retval = UA_Client_readValueAttribute(                                                         \\
   528         client, ua_nodeid_type(ua_nsidx, ua_node_id), c_loc_name##_variant);                        \\
   527         client, ua_nodeid_type(ua_nsidx, ua_node_id), &c_loc_name##_variant);                      \\
   529     if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(c_loc_name##_variant) &&                 \\
   528     if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(&c_loc_name##_variant) &&               \\
   530        c_loc_name##_variant->type == &UA_TYPES[ua_type_enum]) {                                     \\
   529        c_loc_name##_variant.type == &UA_TYPES[ua_type_enum]) {                                     \\
   531             c_loc_name##_buf = *(ua_type*)c_loc_name##_variant->data;                               \\
   530             c_loc_name##_buf = *(ua_type*)c_loc_name##_variant.data;                               \\
       
   531             UA_Variant_clear(&c_loc_name##_variant);  /* Unalloc requiered on each read ! */       \\
   532     }
   532     }
   533 
   533 
   534 void __retrieve_%(locstr)s(void)
   534 void __retrieve_%(locstr)s(void)
   535 {
   535 {
   536     UA_StatusCode retval;
   536     UA_StatusCode retval;
   537 %(retrieve)s
   537 %(retrieve)s
   538 }
   538 }
   539 
   539 
   540 #define WRITE_VALUE(ua_type, ua_type_enum, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id)        \\
   540 #define WRITE_VALUE(ua_type, c_loc_name, ua_nodeid_type, ua_nsidx, ua_node_id)       \\
   541     UA_Variant_setScalarCopy(c_loc_name##_variant, (ua_type*)c_loc_name, &UA_TYPES[ua_type_enum]);  \\
   541     UA_Client_writeValueAttribute(                                                                 \\
   542     UA_Client_writeValueAttribute(client, ua_nodeid_type(ua_nsidx, ua_node_id), c_loc_name##_variant);
   542         client, ua_nodeid_type(ua_nsidx, ua_node_id), &c_loc_name##_variant);
   543 
   543 
   544 void __publish_%(locstr)s(void)
   544 void __publish_%(locstr)s(void)
   545 {
   545 {
   546 %(publish)s
   546 %(publish)s
   547 }
   547 }
   566                 ua_nodeid_type, id_formating = UA_NODE_ID_types[ua_nodeid_type]
   566                 ua_nodeid_type, id_formating = UA_NODE_ID_types[ua_nodeid_type]
   567                 ua_node_id = id_formating.format(_ua_node_id)
   567                 ua_node_id = id_formating.format(_ua_node_id)
   568 
   568 
   569                 formatdict["decl"] += """
   569                 formatdict["decl"] += """
   570 DECL_VAR({ua_type}, {C_type}, {c_loc_name})""".format(**locals())
   570 DECL_VAR({ua_type}, {C_type}, {c_loc_name})""".format(**locals())
   571                 formatdict["cleanup"] += """
   571 
   572     FREE_VARIANT({ua_type}, {c_loc_name})""".format(**locals())
   572                 if direction == "input":
   573                 formatdict["init"] +="""
   573                     formatdict["init"] +="""
   574     ALLOC_VARIANT({ua_type}, {c_loc_name})""".format(**locals())
   574     INIT_READ_VARIANT({ua_type}, {c_loc_name})""".format(**locals())
   575                 formatdict["retrieve"] += """
   575                     formatdict["retrieve"] += """
   576     READ_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
   576     READ_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
   577                 formatdict["publish"] += """
   577 
   578     WRITE_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
   578                 if direction == "output":
       
   579                     formatdict["init"] +="""
       
   580     INIT_WRITE_VARIANT({ua_type}, {ua_type_enum}, {c_loc_name})""".format(**locals())
       
   581                     formatdict["publish"] += """
       
   582     WRITE_VALUE({ua_type}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
   579 
   583 
   580         Ccode = template%formatdict
   584         Ccode = template%formatdict
   581         
   585         
   582         return Ccode
   586         return Ccode
   583 
   587