Let TLS client example codes be simple reading (#6)

pull/302/head
Chun-Sheng, Li 5 years ago committed by GitHub
parent 62db1fad08
commit dc9fab9974
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,5 @@
/* /*
* tls_client_exmaple.c * tls_client_example.c
* *
* This example shows how to configure TLS * This example shows how to configure TLS
*/ */
@ -12,35 +12,58 @@
#include "hal_thread.h" #include "hal_thread.h"
void void
reportCallbackFunction(void* parameter, ClientReport report) printSpaces(int spaces)
{ {
MmsValue* dataSetValues = ClientReport_getDataSetValues(report); int i;
printf("received report for %s\n", ClientReport_getRcbReference(report)); for (i = 0; i < spaces; i++)
printf(" ");
}
int i; void
for (i = 0; i < 4; i++) { printDataDirectory(char* doRef, IedConnection con, int spaces)
ReasonForInclusion reason = ClientReport_getReasonForInclusion(report, i); {
IedClientError error;
LinkedList dataAttributes = IedConnection_getDataDirectory(con, &error, doRef);
//LinkedList dataAttributes = IedConnection_getDataDirectoryByFC(con, &error, doRef, MX);
if (dataAttributes != NULL) {
LinkedList dataAttribute = LinkedList_getNext(dataAttributes);
while (dataAttribute != NULL) {
char* daName = (char*) dataAttribute->data;
printSpaces(spaces);
printf("DA: %s\n", (char*) dataAttribute->data);
if (reason != IEC61850_REASON_NOT_INCLUDED) { dataAttribute = LinkedList_getNext(dataAttribute);
printf(" GGIO1.SPCSO%i.stVal: %i (included for reason %i)\n", i,
MmsValue_getBoolean(MmsValue_getElement(dataSetValues, i)), reason); char daRef[130];
sprintf(daRef, "%s.%s", doRef, daName);
printDataDirectory(daRef, con, spaces + 2);
} }
} }
LinkedList_destroy(dataAttributes);
} }
int main(int argc, char** argv) { int
main(int argc, char** argv)
{
char* hostname; char* hostname;
int port_number = -1; int tcpPort = 8102;
if (argc > 2) { if (argc > 1)
hostname = argv[1]; hostname = argv[1];
port_number = atoi(argv[2]);
}
else else
hostname = "localhost"; hostname = "localhost";
if (argc > 2)
tcpPort = atoi(argv[2]);
TLSConfiguration tlsConfig = TLSConfiguration_create(); TLSConfiguration tlsConfig = TLSConfiguration_create();
TLSConfiguration_setChainValidation(tlsConfig, true); TLSConfiguration_setChainValidation(tlsConfig, true);
@ -61,102 +84,147 @@ int main(int argc, char** argv) {
return 0; return 0;
} }
IedClientError error; IedClientError error;
IedConnection con = IedConnection_createWithTlsSupport(tlsConfig); IedConnection con = IedConnection_createWithTlsSupport(tlsConfig);
IedConnection_connect(con, &error, hostname, port_number); IedConnection_connect(con, &error, hostname, tcpPort);
if (error == IED_ERROR_OK) { if (error == IED_ERROR_OK) {
LinkedList serverDirectory = IedConnection_getServerDirectory(con, &error, false); printf("Get logical device list...\n");
LinkedList deviceList = IedConnection_getLogicalDeviceList(con, &error);
if (error != IED_ERROR_OK) if (error != IED_ERROR_OK) {
printf("failed to read server directory (error=%i)\n", error); printf("Failed to read device list (error code: %i)\n", error);
goto cleanup_and_exit;
}
if (serverDirectory) LinkedList device = LinkedList_getNext(deviceList);
LinkedList_destroy(serverDirectory);
/* read an analog measurement value from server */ while (device != NULL) {
MmsValue* value = IedConnection_readObject(con, &error, "simpleIOGenericIO/GGIO1.AnIn1.mag.f", IEC61850_FC_MX); printf("LD: %s\n", (char*) device->data);
if (value != NULL) { LinkedList logicalNodes = IedConnection_getLogicalDeviceDirectory(con, &error,
float fval = MmsValue_toFloat(value); (char*) device->data);
printf("read float value: %f\n", fval);
MmsValue_delete(value); LinkedList logicalNode = LinkedList_getNext(logicalNodes);
}
while (logicalNode != NULL) {
printf(" LN: %s\n", (char*) logicalNode->data);
/* write a variable to the server */ char lnRef[129];
value = MmsValue_newVisibleString("libiec61850.com");
IedConnection_writeObject(con, &error, "simpleIOGenericIO/GGIO1.NamPlt.vendor", IEC61850_FC_DC, value);
if (error != IED_ERROR_OK) sprintf(lnRef, "%s/%s", (char*) device->data, (char*) logicalNode->data);
printf("failed to write simpleIOGenericIO/GGIO1.NamPlt.vendor!\n");
MmsValue_delete(value); LinkedList dataObjects = IedConnection_getLogicalNodeDirectory(con, &error,
lnRef, ACSI_CLASS_DATA_OBJECT);
LinkedList dataObject = LinkedList_getNext(dataObjects);
/* read data set */ while (dataObject != NULL) {
ClientDataSet clientDataSet = IedConnection_readDataSetValues(con, &error, "simpleIOGenericIO/LLN0.Events", NULL); char* dataObjectName = (char*) dataObject->data;
if (clientDataSet == NULL) printf(" DO: %s\n", dataObjectName);
printf("failed to read dataset\n");
/* Read RCB values */ dataObject = LinkedList_getNext(dataObject);
ClientReportControlBlock rcb =
IedConnection_getRCBValues(con, &error, "simpleIOGenericIO/LLN0.RP.EventsRCB01", NULL);
char doRef[129];
bool rptEna = ClientReportControlBlock_getRptEna(rcb); sprintf(doRef, "%s/%s.%s", (char*) device->data, (char*) logicalNode->data, dataObjectName);
printf("RptEna = %i\n", rptEna); printDataDirectory(doRef, con, 6);
}
/* Install handler for reports */ LinkedList_destroy(dataObjects);
IedConnection_installReportHandler(con, "simpleIOGenericIO/LLN0.RP.EventsRCB01",
ClientReportControlBlock_getRptId(rcb), reportCallbackFunction, NULL);
/* Set trigger options and enable report */ LinkedList dataSets = IedConnection_getLogicalNodeDirectory(con, &error, lnRef,
ClientReportControlBlock_setTrgOps(rcb, TRG_OPT_DATA_UPDATE | TRG_OPT_INTEGRITY | TRG_OPT_GI); ACSI_CLASS_DATA_SET);
ClientReportControlBlock_setRptEna(rcb, true);
ClientReportControlBlock_setIntgPd(rcb, 5000);
IedConnection_setRCBValues(con, &error, rcb, RCB_ELEMENT_RPT_ENA | RCB_ELEMENT_TRG_OPS | RCB_ELEMENT_INTG_PD, true);
if (error != IED_ERROR_OK) LinkedList dataSet = LinkedList_getNext(dataSets);
printf("report activation failed (code: %i)\n", error);
Thread_sleep(1000); while (dataSet != NULL) {
char* dataSetName = (char*) dataSet->data;
bool isDeletable;
char dataSetRef[130];
sprintf(dataSetRef, "%s.%s", lnRef, dataSetName);
/* trigger GI report */ LinkedList dataSetMembers = IedConnection_getDataSetDirectory(con, &error, dataSetRef,
ClientReportControlBlock_setGI(rcb, true); &isDeletable);
IedConnection_setRCBValues(con, &error, rcb, RCB_ELEMENT_GI, true);
if (error != IED_ERROR_OK) if (isDeletable)
printf("Error triggering a GI report (code: %i)\n", error); printf(" Data set: %s (deletable)\n", dataSetName);
else
printf(" Data set: %s (not deletable)\n", dataSetName);
Thread_sleep(60000); LinkedList dataSetMemberRef = LinkedList_getNext(dataSetMembers);
/* disable reporting */ while (dataSetMemberRef != NULL) {
ClientReportControlBlock_setRptEna(rcb, false);
IedConnection_setRCBValues(con, &error, rcb, RCB_ELEMENT_RPT_ENA, true);
if (error != IED_ERROR_OK) char* memberRef = (char*) dataSetMemberRef->data;
printf("disable reporting failed (code: %i)\n", error);
ClientDataSet_destroy(clientDataSet); printf(" %s\n", memberRef);
ClientReportControlBlock_destroy(rcb); dataSetMemberRef = LinkedList_getNext(dataSetMemberRef);
}
close_connection: LinkedList_destroy(dataSetMembers);
dataSet = LinkedList_getNext(dataSet);
}
LinkedList_destroy(dataSets);
LinkedList reports = IedConnection_getLogicalNodeDirectory(con, &error, lnRef,
ACSI_CLASS_URCB);
LinkedList report = LinkedList_getNext(reports);
while (report != NULL) {
char* reportName = (char*) report->data;
printf(" RP: %s\n", reportName);
report = LinkedList_getNext(report);
}
LinkedList_destroy(reports);
reports = IedConnection_getLogicalNodeDirectory(con, &error, lnRef,
ACSI_CLASS_BRCB);
report = LinkedList_getNext(reports);
while (report != NULL) {
char* reportName = (char*) report->data;
printf(" BR: %s\n", reportName);
report = LinkedList_getNext(report);
}
LinkedList_destroy(reports);
logicalNode = LinkedList_getNext(logicalNode);
}
LinkedList_destroy(logicalNodes);
device = LinkedList_getNext(device);
}
LinkedList_destroy(deviceList);
IedConnection_close(con); IedConnection_close(con);
TLSConfiguration_destroy(tlsConfig);
} }
else { else {
printf("Failed to connect to %s\n", hostname); printf("Connection failed!\n");
} }
cleanup_and_exit:
IedConnection_destroy(con); IedConnection_destroy(con);
TLSConfiguration_destroy(tlsConfig); TLSConfiguration_destroy(tlsConfig);
} }

Loading…
Cancel
Save