- mms_utility: fixed memory leak

pull/179/head
Michael Zillgith 6 years ago
parent 31e183d357
commit 83f9f1eaa6

@ -30,17 +30,17 @@ print_help()
} }
static void static void
mmsFileDirectoryHandler (void* parameter, char* filename, uint32_t size, uint64_t lastModified) mmsFileDirectoryHandler(void* parameter, char* filename, uint32_t size, uint64_t lastModified)
{ {
char* lastName = (char*) parameter; char* lastName = (char*) parameter;
strcpy (lastName, filename); strcpy(lastName, filename);
printf("%s\n", filename); printf("%s\n", filename);
} }
static void static void
mmsGetFileAttributeHandler (void* parameter, char* filename, uint32_t size, uint64_t lastModified) mmsGetFileAttributeHandler(void* parameter, char* filename, uint32_t size, uint64_t lastModified)
{ {
char gtString[30]; char gtString[30];
Conversions_msTimeToGeneralizedTime(lastModified, (uint8_t*) gtString); Conversions_msTimeToGeneralizedTime(lastModified, (uint8_t*) gtString);
@ -99,142 +99,144 @@ printRawMmsMessage(void* parameter, uint8_t* message, int messageLength, bool re
printf("\n"); printf("\n");
} }
int main(int argc, char** argv) { int main(int argc, char** argv)
{
char* hostname = StringUtils_copyString("localhost");
int tcpPort = 102; char* hostname = StringUtils_copyString("localhost");
int maxPduSize = 65000; int tcpPort = 102;
int arrayIndex = -1; int maxPduSize = 65000;
int arrayIndex = -1;
char* domainName = NULL;
char* variableName = NULL; char* domainName = NULL;
char* componentName = NULL; char* variableName = NULL;
char* filename = NULL; char* componentName = NULL;
char* journalName = NULL; char* filename = NULL;
char* journalName = NULL;
int readDeviceList = 0;
int getDeviceDirectory = 0; int readDeviceList = 0;
int identifyDevice = 0; int getDeviceDirectory = 0;
int readWriteHasDomain = 0; int identifyDevice = 0;
int readVariable = 0; int readWriteHasDomain = 0;
int showFileList = 0; int readVariable = 0;
int getFileAttributes = 0; int showFileList = 0;
int readJournal = 0; int getFileAttributes = 0;
int printRawMmsMessages = 0; int readJournal = 0;
int deleteFile = 0; int printRawMmsMessages = 0;
int readVariableList = 0; int deleteFile = 0;
int readVariableList = 0;
int c;
int c;
while ((c = getopt(argc, argv, "mifdh:p:l:t:a:r:g:j:x:v:c:y:")) != -1)
switch (c) { while ((c = getopt(argc, argv, "mifdh:p:l:t:a:r:g:j:x:v:c:y:")) != -1)
case 'm': switch (c) {
printRawMmsMessages = 1; case 'm':
break; printRawMmsMessages = 1;
break;
case 'h':
hostname = StringUtils_copyString(optarg); case 'h':
break; free(hostname);
case 'p': hostname = StringUtils_copyString(optarg);
tcpPort = atoi(optarg); break;
break; case 'p':
case 'l': tcpPort = atoi(optarg);
maxPduSize = atoi(optarg); break;
break; case 'l':
case 'd': maxPduSize = atoi(optarg);
readDeviceList = 1; break;
break; case 'd':
case 'i': readDeviceList = 1;
identifyDevice = 1; break;
break; case 'i':
case 't': identifyDevice = 1;
getDeviceDirectory = 1; break;
domainName = StringUtils_copyString(optarg); case 't':
break; getDeviceDirectory = 1;
case 'a': domainName = StringUtils_copyString(optarg);
readWriteHasDomain = 1; break;
domainName = StringUtils_copyString(optarg); case 'a':
break; readWriteHasDomain = 1;
case 'r': domainName = StringUtils_copyString(optarg);
readVariable = 1; break;
variableName = StringUtils_copyString(optarg); case 'r':
break; readVariable = 1;
case 'c': variableName = StringUtils_copyString(optarg);
componentName = StringUtils_copyString(optarg); break;
break; case 'c':
case 'v': componentName = StringUtils_copyString(optarg);
readVariableList = 1; break;
variableName = StringUtils_copyString(optarg); case 'v':
readVariableList = 1;
variableName = StringUtils_copyString(optarg);
break;
case 'f':
showFileList = 1;
break;
case 'g':
getFileAttributes = 1;
filename = StringUtils_copyString(optarg);
break;
case 'x':
deleteFile = 1;
filename = StringUtils_copyString(optarg);
break;
case 'j':
readJournal = 1;
journalName = StringUtils_copyString(optarg);
break; break;
case 'f':
showFileList = 1; case 'y':
break; arrayIndex = atoi(optarg);
case 'g': break;
getFileAttributes = 1;
filename = StringUtils_copyString(optarg); default:
break; print_help();
case 'x': return 0;
deleteFile = 1; }
filename = StringUtils_copyString(optarg);
break; MmsConnection con = MmsConnection_create();
case 'j': MmsError error;
readJournal = 1;
journalName = StringUtils_copyString(optarg); /* Set maximum MMS PDU size (local detail) to 2000 byte */
break; MmsConnection_setLocalDetail(con, maxPduSize);
case 'y': if (printRawMmsMessages)
arrayIndex = atoi(optarg); MmsConnection_setRawMessageHandler(con, (MmsRawMessageHandler) printRawMmsMessage, NULL);
break;
if (!MmsConnection_connect(con, &error, hostname, tcpPort)) {
default: printf("MMS connect failed!\n");
print_help(); goto exit;
return 0; }
} else
printf("MMS connected.\n");
MmsConnection con = MmsConnection_create();
if (identifyDevice) {
MmsError error; MmsServerIdentity* identity =
MmsConnection_identify(con, &error);
/* Set maximum MMS PDU size (local detail) to 2000 byte */
MmsConnection_setLocalDetail(con, maxPduSize); if (identity != NULL) {
printf("\nServer identity:\n----------------\n");
if (printRawMmsMessages) printf(" vendor:\t%s\n", identity->vendorName);
MmsConnection_setRawMessageHandler(con, (MmsRawMessageHandler) printRawMmsMessage, NULL); printf(" model:\t%s\n", identity->modelName);
printf(" revision:\t%s\n", identity->revision);
if (!MmsConnection_connect(con, &error, hostname, tcpPort)) { }
printf("MMS connect failed!\n"); else
goto exit; printf("Reading server identity failed!\n");
} }
else
printf("MMS connected.\n"); if (readDeviceList) {
printf("\nDomains present on server:\n--------------------------\n");
if (identifyDevice) { LinkedList nameList = MmsConnection_getDomainNames(con, &error);
MmsServerIdentity* identity = LinkedList_printStringList(nameList);
MmsConnection_identify(con, &error); LinkedList_destroy(nameList);
}
if (identity != NULL) {
printf("\nServer identity:\n----------------\n"); if (getDeviceDirectory) {
printf(" vendor:\t%s\n", identity->vendorName); LinkedList variableList = MmsConnection_getDomainVariableNames(con, &error,
printf(" model:\t%s\n", identity->modelName); domainName);
printf(" revision:\t%s\n", identity->revision);
} if (variableList) {
else
printf("Reading server identity failed!\n");
}
if (readDeviceList) {
printf("\nDomains present on server:\n--------------------------\n");
LinkedList nameList = MmsConnection_getDomainNames(con, &error);
LinkedList_printStringList(nameList);
LinkedList_destroy(nameList);
}
if (getDeviceDirectory) {
LinkedList variableList = MmsConnection_getDomainVariableNames(con, &error,
domainName);
if (variableList) {
LinkedList element = LinkedList_getNext(variableList); LinkedList element = LinkedList_getNext(variableList);
printf("\nMMS domain variables for domain %s\n", domainName); printf("\nMMS domain variables for domain %s\n", domainName);
@ -248,14 +250,14 @@ int main(int argc, char** argv) {
} }
LinkedList_destroy(variableList); LinkedList_destroy(variableList);
} }
else { else {
printf("\nFailed to read domain directory (error=%d)\n", error); printf("\nFailed to read domain directory (error=%d)\n", error);
} }
variableList = MmsConnection_getDomainJournals(con, &error, domainName); variableList = MmsConnection_getDomainJournals(con, &error, domainName);
if (variableList) { if (variableList) {
LinkedList element = variableList; LinkedList element = variableList;
@ -268,21 +270,21 @@ int main(int argc, char** argv) {
} }
LinkedList_destroy(variableList); LinkedList_destroy(variableList);
} }
else { else {
printf("\nFailed to read domain journals (error=%d)\n", error); printf("\nFailed to read domain journals (error=%d)\n", error);
} }
} }
if (readJournal) { if (readJournal) {
printf(" read journal %s...\n", journalName); printf(" read journal %s...\n", journalName);
char* logDomain = journalName; char* logDomain = journalName;
char* logName = strchr(journalName, '/'); char* logName = strchr(journalName, '/');
if (logName != NULL) { if (logName != NULL) {
logName[0] = 0; logName[0] = 0;
logName++; logName++;
@ -334,44 +336,43 @@ int main(int argc, char** argv) {
readNext = true; readNext = true;
} }
} while ((moreFollows == true) || (readNext == true)); }
while ((moreFollows == true) || (readNext == true));
}
}
else
printf(" Invalid log name!\n");
}
if (readVariable) {
if (readWriteHasDomain) {
MmsValue* result;
if (componentName == NULL) {
if (arrayIndex == -1) {
result = MmsConnection_readVariable(con, &error, domainName, variableName);
}
else {
result = MmsConnection_readSingleArrayElementWithComponent(con, &error, domainName, variableName, arrayIndex, NULL);
}
}
else {
if (arrayIndex == -1) {
result = MmsConnection_readVariableComponent(con, &error, domainName, variableName, componentName);
}
else {
result = MmsConnection_readSingleArrayElementWithComponent(con, &error, domainName, variableName, arrayIndex, componentName);
}
}
if (error != MMS_ERROR_NONE) {
printf("Reading variable failed: (ERROR %i)\n", error);
} }
} else {
else printf("Read SUCCESS\n");
printf(" Invalid log name!\n");
} if (result != NULL) {
if (readVariable) {
if (readWriteHasDomain) {
MmsValue* result;
if (componentName == NULL) {
if (arrayIndex == -1) {
result = MmsConnection_readVariable(con, &error, domainName, variableName);
}
else {
result = MmsConnection_readSingleArrayElementWithComponent(con, &error, domainName, variableName, arrayIndex, NULL);
}
}
else {
if (arrayIndex == -1) {
result = MmsConnection_readVariableComponent(con, &error, domainName, variableName, componentName);
}
else {
result = MmsConnection_readSingleArrayElementWithComponent(con, &error, domainName, variableName, arrayIndex, componentName);
}
}
if (error != MMS_ERROR_NONE) {
printf("Reading variable failed: (ERROR %i)\n", error);
}
else {
printf("Read SUCCESS\n");
if (result != NULL) {
char outbuf[1024]; char outbuf[1024];
MmsValue_printToBuffer(result, outbuf, 1024); MmsValue_printToBuffer(result, outbuf, 1024);
@ -379,17 +380,17 @@ int main(int argc, char** argv) {
printf("%s\n", outbuf); printf("%s\n", outbuf);
MmsValue_delete(result); MmsValue_delete(result);
} }
else else
printf("result: NULL\n"); printf("result: NULL\n");
} }
} }
else else
printf("Reading VMD scope variable not yet supported!\n"); printf("Reading VMD scope variable not yet supported!\n");
} }
if (readVariableList) { if (readVariableList) {
if (readWriteHasDomain) { if (readWriteHasDomain) {
MmsValue* variables = MmsConnection_readNamedVariableListValues(con, &error, domainName, variableName, true); MmsValue* variables = MmsConnection_readNamedVariableListValues(con, &error, domainName, variableName, true);
@ -402,25 +403,25 @@ int main(int argc, char** argv) {
} }
else else
printf("Reading VMD scope variable list not yet supported!\n"); printf("Reading VMD scope variable list not yet supported!\n");
} }
if (showFileList) { if (showFileList) {
char lastName[300]; char lastName[300];
lastName[0] = 0; lastName[0] = 0;
char* continueAfter = NULL; char* continueAfter = NULL;
while (MmsConnection_getFileDirectory(con, &error, "", continueAfter, mmsFileDirectoryHandler, lastName)) { while (MmsConnection_getFileDirectory(con, &error, "", continueAfter, mmsFileDirectoryHandler, lastName)) {
continueAfter = lastName; continueAfter = lastName;
} }
} }
if (getFileAttributes) { if (getFileAttributes) {
MmsConnection_getFileDirectory(con, &error, filename, NULL, mmsGetFileAttributeHandler, NULL); MmsConnection_getFileDirectory(con, &error, filename, NULL, mmsGetFileAttributeHandler, NULL);
} }
if (deleteFile) { if (deleteFile) {
MmsConnection_fileDelete(con, &error, filename); MmsConnection_fileDelete(con, &error, filename);
if (error != MMS_ERROR_NONE) { if (error != MMS_ERROR_NONE) {
printf("Delete file failed: (ERROR %i)\n", error); printf("Delete file failed: (ERROR %i)\n", error);
@ -428,15 +429,15 @@ int main(int argc, char** argv) {
else { else {
printf("File deleted\n"); printf("File deleted\n");
} }
} }
exit: exit:
free(hostname); free(hostname);
free(domainName); free(domainName);
free(variableName); free(variableName);
free(journalName); free(journalName);
free(componentName); free(componentName);
MmsConnection_destroy(con); MmsConnection_destroy(con);
} }

Loading…
Cancel
Save