opc_ua/opcua_client_maker.py
branchwxPython4
changeset 3633 837e7782d83e
parent 3620 1cf21430ed4a
child 3643 c6068b674b30
equal deleted inserted replaced
3632:92bfefddae27 3633:837e7782d83e
   507 #include <open62541/plugin/securitypolicy.h>
   507 #include <open62541/plugin/securitypolicy.h>
   508 
   508 
   509 #include <open62541/types.h>
   509 #include <open62541/types.h>
   510 #include <open62541/types_generated_handling.h>
   510 #include <open62541/types_generated_handling.h>
   511 
   511 
       
   512 #define _Log(level, ...)                                                                           \\
       
   513     {{                                                                                             \\
       
   514         char mstr[256];                                                                            \\
       
   515         snprintf(mstr, 255, __VA_ARGS__);                                                          \\
       
   516         LogMessage(level, mstr, strlen(mstr));                                                     \\
       
   517     }}
       
   518 
       
   519 #define LogInfo(...) _Log(LOG_INFO, __VA_ARGS__);
       
   520 #define LogError(...) _Log(LOG_CRITICAL, __VA_ARGS__);
       
   521 #define LogWarning(...) _Log(LOG_WARNING, __VA_ARGS__);
       
   522 
   512 static UA_INLINE UA_ByteString
   523 static UA_INLINE UA_ByteString
   513 loadFile(const char *const path) {{
   524 loadFile(const char *const path) {{
   514     UA_ByteString fileContents = UA_STRING_NULL;
   525     UA_ByteString fileContents = UA_STRING_NULL;
   515 
   526 
   516     FILE *fp = fopen(path, "rb");
   527     FILE *fp = fopen(path, "rb");
   517     if(!fp) {{
   528     if(!fp) {{
   518         errno = 0;
   529         errno = 0;
       
   530         LogError("OPC-UA could not open %s", path);
   519         return fileContents;
   531         return fileContents;
   520     }}
   532     }}
   521 
   533 
   522     fseek(fp, 0, SEEK_END);
   534     fseek(fp, 0, SEEK_END);
   523     fileContents.length = (size_t)ftell(fp);
   535     fileContents.length = (size_t)ftell(fp);
   524     fileContents.data = (UA_Byte *)UA_malloc(fileContents.length * sizeof(UA_Byte));
   536     fileContents.data = (UA_Byte *)UA_malloc(fileContents.length * sizeof(UA_Byte));
   525     if(fileContents.data) {{
   537     if(fileContents.data) {{
   526         fseek(fp, 0, SEEK_SET);
   538         fseek(fp, 0, SEEK_SET);
   527         size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
   539         size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
   528         if(read != fileContents.length)
   540         if(read != fileContents.length){{
   529             UA_ByteString_clear(&fileContents);
   541             UA_ByteString_clear(&fileContents);
       
   542             LogError("OPC-UA could not read %s", path);
       
   543         }}
   530     }} else {{
   544     }} else {{
   531         fileContents.length = 0;
   545         fileContents.length = 0;
       
   546         LogError("OPC-UA Not enough memoty to load %s", path);
   532     }}
   547     }}
   533     fclose(fp);
   548     fclose(fp);
   534 
   549 
   535     return fileContents;
   550     return fileContents;
   536 }}
   551 }}
   549 {{
   564 {{
   550     UA_Client_disconnect(client);
   565     UA_Client_disconnect(client);
   551     UA_Client_delete(client);
   566     UA_Client_delete(client);
   552 }}
   567 }}
   553 
   568 
   554 
       
   555 #define _Log(level, ...)                                                                           \\
       
   556     {{                                                                                             \\
       
   557         char mstr[256];                                                                            \\
       
   558         snprintf(mstr, 255, __VA_ARGS__);                                                          \\
       
   559         LogMessage(level, mstr, strlen(mstr));                                                     \\
       
   560     }}
       
   561 
       
   562 #define LogInfo(...) _Log(LOG_INFO, __VA_ARGS__);
       
   563 #define LogError(...) _Log(LOG_CRITICAL, __VA_ARGS__);
       
   564 #define LogWarning(...) _Log(LOG_WARNING, __VA_ARGS__);
       
   565 
       
   566 #define INIT_NoAuth()                                                                              \\
   569 #define INIT_NoAuth()                                                                              \\
   567     LogInfo("OPC-UA Init no auth");                                                                \\
   570     LogInfo("OPC-UA Init no auth");                                                                \\
   568     UA_ClientConfig_setDefault(cc);                                                                \\
   571     UA_ClientConfig_setDefault(cc);                                                                \\
   569     retval = UA_Client_connect(client, uri);
   572     retval = UA_Client_connect(client, uri);
   570 
   573 
   571 /* Note : Policy is ignored here since open62541 client supports all policies by default */
   574 /* Note : Policy is ignored here since open62541 client supports all policies by default */
   572 #define INIT_x509(Policy, UpperCaseMode, PrivateKey, Certificate)                                  \\
   575 #define INIT_x509(Policy, UpperCaseMode, PrivateKey, Certificate)                                  \\
   573     /* TODO try paths given in runtime CLI */                                                      \\
   576     LogInfo("OPC-UA Init x509 %s,%s,%s,%s", #Policy, #UpperCaseMode, PrivateKey, Certificate);     \\
       
   577                                                                                                    \\
   574     UA_ByteString certificate = loadFile(Certificate);                                             \\
   578     UA_ByteString certificate = loadFile(Certificate);                                             \\
   575     UA_ByteString privateKey  = loadFile(PrivateKey);                                              \\
   579     UA_ByteString privateKey  = loadFile(PrivateKey);                                              \\
   576                                                                                                    \\
   580                                                                                                    \\
   577     LogInfo("OPC-UA Init x509 %s,%s,%s,%s", #Policy, #UpperCaseMode, PrivateKey, Certificate);     \\
       
   578     cc->securityMode = UA_MESSAGESECURITYMODE_##UpperCaseMode;                                     \\
   581     cc->securityMode = UA_MESSAGESECURITYMODE_##UpperCaseMode;                                     \\
   579     UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey, NULL, 0, NULL, 0);           \\
   582     UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey, NULL, 0, NULL, 0);           \\
   580                                                                                                    \\
   583                                                                                                    \\
   581     retval = UA_Client_connect(client, uri);                                                       \\
   584     retval = UA_Client_connect(client, uri);                                                       \\
   582                                                                                                    \\
   585                                                                                                    \\