opc_ua/opcua_client_maker.py
branchwxPython4
changeset 3591 50600b946ea7
parent 3589 a0b645a934c9
child 3612 428fb2472f43
equal deleted inserted replaced
3590:09dad7f00c36 3591:50600b946ea7
   309             self.tree.SetMainColumn(0)
   309             self.tree.SetMainColumn(0)
   310 
   310 
   311             config = self.config_getter()
   311             config = self.config_getter()
   312             self.client = Client(config["URI"])
   312             self.client = Client(config["URI"])
   313             
   313             
   314             # TODO: crypto stuff
   314             AuthType = config["AuthType"]
   315             # client.set_security_string("Basic256Sha256,SignAndEncrypt,certificate-example.der,private-key-example.pem")
   315             if AuthType=="UserPasword":
       
   316                 self.client.set_user(config["User"])
       
   317                 self.client.set_password(config["Password"])
       
   318             elif AuthType=="x509":
       
   319                 self.client.set_security_string(
       
   320                     "{Policy},{Mode},{Certificate},{PrivateKey}".format(**config))
   316 
   321 
   317             self.client.connect()
   322             self.client.connect()
   318             self.client.load_type_definitions()  # load definition of server specific structures/extension objects
   323             self.client.load_type_definitions()  # load definition of server specific structures/extension objects
   319             rootnode = self.client.get_root_node()
   324             rootnode = self.client.get_root_node()
   320 
   325 
   497         template = """/* code generated by beremiz OPC-UA extension */
   502         template = """/* code generated by beremiz OPC-UA extension */
   498 
   503 
   499 #include <open62541/client_config_default.h>
   504 #include <open62541/client_config_default.h>
   500 #include <open62541/client_highlevel.h>
   505 #include <open62541/client_highlevel.h>
   501 #include <open62541/plugin/log_stdout.h>
   506 #include <open62541/plugin/log_stdout.h>
       
   507 #include <open62541/plugin/securitypolicy.h>
       
   508 
       
   509 #include <open62541/types.h>
       
   510 #include <open62541/types_generated_handling.h>
       
   511 
       
   512 static UA_INLINE UA_ByteString
       
   513 loadFile(const char *const path) {{
       
   514     UA_ByteString fileContents = UA_STRING_NULL;
       
   515 
       
   516     FILE *fp = fopen(path, "rb");
       
   517     if(!fp) {{
       
   518         errno = 0;
       
   519         return fileContents;
       
   520     }}
       
   521 
       
   522     fseek(fp, 0, SEEK_END);
       
   523     fileContents.length = (size_t)ftell(fp);
       
   524     fileContents.data = (UA_Byte *)UA_malloc(fileContents.length * sizeof(UA_Byte));
       
   525     if(fileContents.data) {{
       
   526         fseek(fp, 0, SEEK_SET);
       
   527         size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
       
   528         if(read != fileContents.length)
       
   529             UA_ByteString_clear(&fileContents);
       
   530     }} else {{
       
   531         fileContents.length = 0;
       
   532     }}
       
   533     fclose(fp);
       
   534 
       
   535     return fileContents;
       
   536 }}
   502 
   537 
   503 static UA_Client *client;
   538 static UA_Client *client;
       
   539 static UA_ClientConfig *cc;
   504 
   540 
   505 #define DECL_VAR(ua_type, C_type, c_loc_name)                                                       \\
   541 #define DECL_VAR(ua_type, C_type, c_loc_name)                                                       \\
   506 static UA_Variant c_loc_name##_variant;                                                             \\
   542 static UA_Variant c_loc_name##_variant;                                                             \\
   507 static C_type c_loc_name##_buf = 0;                                                                 \\
   543 static C_type c_loc_name##_buf = 0;                                                                 \\
   508 C_type *c_loc_name = &c_loc_name##_buf;
   544 C_type *c_loc_name = &c_loc_name##_buf;
   514     UA_Client_disconnect(client);
   550     UA_Client_disconnect(client);
   515     UA_Client_delete(client);
   551     UA_Client_delete(client);
   516 }}
   552 }}
   517 
   553 
   518 
   554 
       
   555 #define INIT_NoAuth()                                                                              \\
       
   556     UA_ClientConfig_setDefault(cc);                                                                \\
       
   557     retval = UA_Client_connect(client, uri);
       
   558 
       
   559 /* Note : Policy is ignored here since open62541 client supports all policies by default */
       
   560 #define INIT_x509(Policy, UpperCaseMode, PrivateKey, Certificate)                                  \\
       
   561     /* TODO try paths given in runtime CLI */                                                      \\
       
   562     UA_ByteString certificate = loadFile(Certificate);                                             \\
       
   563     UA_ByteString privateKey  = loadFile(PrivateKey);                                              \\
       
   564                                                                                                    \\
       
   565     printf("INIT_x509 %s,%s,%s,%s\\n", #Policy, #UpperCaseMode, PrivateKey, Certificate);          \\
       
   566     cc->securityMode = UA_MESSAGESECURITYMODE_##UpperCaseMode;                                     \\
       
   567     UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey, NULL, 0, NULL, 0);           \\
       
   568                                                                                                    \\
       
   569     retval = UA_Client_connect(client, uri);                                                       \\
       
   570                                                                                                    \\
       
   571     UA_ByteString_clear(&certificate);                                                             \\
       
   572     UA_ByteString_clear(&privateKey);
       
   573 
       
   574 #define INIT_UserPassword(User, Password)                                                          \\
       
   575     printf("TODO INIT_UserPassword %s,%s\\n", User, Password);                                     \\
       
   576     retval = UA_Client_connectUsername(client, uri, User, Password);
       
   577 
   519 #define INIT_READ_VARIANT(ua_type, c_loc_name)                                                     \\
   578 #define INIT_READ_VARIANT(ua_type, c_loc_name)                                                     \\
   520     UA_Variant_init(&c_loc_name##_variant);
   579     UA_Variant_init(&c_loc_name##_variant);
   521 
   580 
   522 #define INIT_WRITE_VARIANT(ua_type, ua_type_enum, c_loc_name)                                      \\
   581 #define INIT_WRITE_VARIANT(ua_type, ua_type_enum, c_loc_name)                                      \\
   523     UA_Variant_setScalar(&c_loc_name##_variant, (ua_type*)c_loc_name, &UA_TYPES[ua_type_enum]);
   582     UA_Variant_setScalar(&c_loc_name##_variant, (ua_type*)c_loc_name, &UA_TYPES[ua_type_enum]);
   524 
   583 
   525 int __init_{locstr}(int argc,char **argv)
   584 int __init_{locstr}(int argc,char **argv)
   526 {{
   585 {{
   527     UA_StatusCode retval;
   586     UA_StatusCode retval;
   528     client = UA_Client_new();
   587     client = UA_Client_new();
   529     UA_ClientConfig_setDefault(UA_Client_getConfig(client));
   588     cc = UA_Client_getConfig(client);
       
   589     char *uri = "{uri}";
   530 {init}
   590 {init}
   531 
   591 
   532     /* Connect to server */
       
   533     retval = UA_Client_connect(client, "{uri}");
       
   534     if(retval != UA_STATUSCODE_GOOD) {{
   592     if(retval != UA_STATUSCODE_GOOD) {{
   535         UA_Client_delete(client);
   593         UA_Client_delete(client);
   536         return EXIT_FAILURE;
   594         return EXIT_FAILURE;
   537     }}
   595     }}
   538 }}
   596 }}
   564 """
   622 """
   565         
   623         
   566         formatdict = dict(
   624         formatdict = dict(
   567             locstr   = locstr,
   625             locstr   = locstr,
   568             uri      = config["URI"],
   626             uri      = config["URI"],
   569             # TODO: pass authentication code.
       
   570             decl     = "",
   627             decl     = "",
   571             cleanup  = "",
   628             cleanup  = "",
   572             init     = "",
   629             init     = "",
   573             retrieve = "",
   630             retrieve = "",
   574             publish  = "" 
   631             publish  = "" 
   575         )
   632         )
       
   633 
       
   634         AuthType = config["AuthType"]
       
   635         if AuthType == "x509":
       
   636             config["UpperCaseMode"] = config["Mode"].upper()
       
   637             formatdict["init"] += """
       
   638     INIT_x509({Policy}, {UpperCaseMode}, "{PrivateKey}", "{Certificate}")""".format(**config)
       
   639         elif AuthType == "UserPassword":
       
   640             formatdict["init"] += """
       
   641     INIT_UserPassword("{User}", "{Password}")""".format(**config)
       
   642         else:
       
   643             formatdict["init"] += """
       
   644     INIT_NoAuth()"""
       
   645 
   576         for direction, data in self.iteritems():
   646         for direction, data in self.iteritems():
   577             iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction]
   647             iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction]
   578             for row in data:
   648             for row in data:
   579                 name, ua_nsidx, ua_nodeid_type, _ua_node_id, ua_type, iec_number = row
   649                 name, ua_nsidx, ua_nodeid_type, _ua_node_id, ua_type, iec_number = row
   580                 iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type]
   650                 iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type]
   584 
   654 
   585                 formatdict["decl"] += """
   655                 formatdict["decl"] += """
   586 DECL_VAR({ua_type}, {C_type}, {c_loc_name})""".format(**locals())
   656 DECL_VAR({ua_type}, {C_type}, {c_loc_name})""".format(**locals())
   587 
   657 
   588                 if direction == "input":
   658                 if direction == "input":
   589                     formatdict["init"] +="""
   659                     formatdict["init"] += """
   590     INIT_READ_VARIANT({ua_type}, {c_loc_name})""".format(**locals())
   660     INIT_READ_VARIANT({ua_type}, {c_loc_name})""".format(**locals())
   591                     formatdict["retrieve"] += """
   661                     formatdict["retrieve"] += """
   592     READ_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
   662     READ_VALUE({ua_type}, {ua_type_enum}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
   593 
   663 
   594                 if direction == "output":
   664                 if direction == "output":
   595                     formatdict["init"] +="""
   665                     formatdict["init"] += """
   596     INIT_WRITE_VARIANT({ua_type}, {ua_type_enum}, {c_loc_name})""".format(**locals())
   666     INIT_WRITE_VARIANT({ua_type}, {ua_type_enum}, {c_loc_name})""".format(**locals())
   597                     formatdict["publish"] += """
   667                     formatdict["publish"] += """
   598     WRITE_VALUE({ua_type}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
   668     WRITE_VALUE({ua_type}, {c_loc_name}, {ua_nodeid_type}, {ua_nsidx}, {ua_node_id})""".format(**locals())
   599 
   669 
   600         Ccode = template.format(**formatdict)
   670         Ccode = template.format(**formatdict)