From 9e2c045d6118ef264d6cf8fc655b72d7c4c403dc Mon Sep 17 00:00:00 2001 From: Angelo Mantellini Date: Tue, 9 Jun 2020 14:48:02 +0200 Subject: [HICN-626] Return output from libhicnlight Signed-off-by: Angelo Mantellini Change-Id: I2351ce4dcefa1311fa09049f87e8317c8fe449f4 --- hicn-light/src/hicn/config/controlListPolicies.c | 66 ++++++++++++++---------- 1 file changed, 39 insertions(+), 27 deletions(-) (limited to 'hicn-light/src/hicn/config/controlListPolicies.c') diff --git a/hicn-light/src/hicn/config/controlListPolicies.c b/hicn-light/src/hicn/config/controlListPolicies.c index 81be6ddfc..8eae6453b 100644 --- a/hicn-light/src/hicn/config/controlListPolicies.c +++ b/hicn-light/src/hicn/config/controlListPolicies.c @@ -33,10 +33,14 @@ static CommandReturn _controlListPolicies_Execute(CommandParser *parser, CommandOps *ops, - PARCList *args); + PARCList *args, + char *output, + size_t output_size); static CommandReturn _controlListPolicies_HelpExecute(CommandParser *parser, CommandOps *ops, - PARCList *args); + PARCList *args, + char *output, + size_t output_size); static const char *_commandListPolicies = "list policies"; static const char *_commandListPoliciesHelp = "help list policies"; @@ -57,9 +61,10 @@ CommandOps *controlListPolicies_HelpCreate(ControlState *state) { static CommandReturn _controlListPolicies_HelpExecute(CommandParser *parser, CommandOps *ops, - PARCList *args) { - printf("command: list policies\n"); - printf("\n"); + PARCList *args, + char *output, + size_t output_size) { + snprintf(output, output_size, "command: list policies\n\n"); return CommandReturn_Success; } @@ -77,9 +82,11 @@ typedef struct { static CommandReturn _controlListPolicies_Execute(CommandParser *parser, CommandOps *ops, - PARCList *args) { + PARCList *args, + char *output, + size_t output_size) { if (parcList_Size(args) != 2) { - _controlListPolicies_HelpExecute(parser, ops, args); + _controlListPolicies_HelpExecute(parser, ops, args, output, output_size); return CommandReturn_Failure; } @@ -96,7 +103,7 @@ static CommandReturn _controlListPolicies_Execute(CommandParser *parser, (header_control_message *)response[0].iov_base; uint8_t *receivedPayload = (uint8_t *)response[1].iov_base; if (!receivedPayload) { - printf("No payload!\n"); + snprintf(output, output_size, "No payload!\n"); return CommandReturn_Failure; } @@ -112,20 +119,15 @@ static CommandReturn _controlListPolicies_Execute(CommandParser *parser, char *addrString = NULL; in_port_t port = htons(1234); // this is a random port number that is ignored - + size_t output_offset = 0; if (receivedHeader->length > 0) { - printf("%*s %*s" - #define _(x, y) " %*s" - foreach_policy_tag - #undef _ - "%s", - MAXSZ_PREFIX, "prefix", MAXSZ_APP_NAME /*APP_NAME_LEN*/, "app_name", - #define _(x, y) MAXSZ_COLUMN, policy_tag_str[POLICY_TAG_ ## x], - foreach_policy_tag - #undef _ - "\n"); + output_offset += snprintf(output, output_size, "%*s %*s", MAXSZ_PREFIX, "prefix", MAXSZ_APP_NAME /*APP_NAME_LEN*/, "app_name"); + #define _(x, y) output_offset += snprintf(output + output_offset, output_size - output_offset, " %*s",MAXSZ_COLUMN, policy_tag_str[POLICY_TAG_ ## x]); + foreach_policy_tag + #undef _ + output_offset += snprintf(output + output_offset, output_size - output_offset, "\n"); } else { - printf(" --- No entry in the list \n"); + output_offset += snprintf(output, output_size, " --- No entry in the list \n"); } tag_state_str_t str; @@ -177,26 +179,36 @@ static CommandReturn _controlListPolicies_Execute(CommandParser *parser, parcMemory_Deallocate((void **)&result); parcBufferComposer_Release(&composer); #else - printf("%*s %*s" + + output_offset = snprintf(output + output_offset, output_size - output_offset, "%*s %*s", + MAXSZ_PREFIX, addrString, MAXSZ_APP_NAME /*APP_NAME_LEN*/, listPoliciesCommand->policy.app_name); + #define _(x, y) output_offset += snprintf(output + output_offset, output_size - output_offset, " %*s", MAXSZ_COLUMN, str.x); + foreach_policy_tag + #undef _ + + output_offset = snprintf(output + output_offset, output_size - output_offset, "\n"); + + + /*printf("%*s %*s" #define _(x, y) " %*s" foreach_policy_tag #undef _ "%s\n", - MAXSZ_PREFIX, addrString, MAXSZ_APP_NAME /*APP_NAME_LEN*/, listPoliciesCommand->policy.app_name, + MAXSZ_PREFIX, addrString, MAXSZ_APP_NAME *//*APP_NAME_LEN*//*, listPoliciesCommand->policy.app_name, #define _(x, y) MAXSZ_COLUMN, str.x, foreach_policy_tag #undef _ - ""); + "");*/ #endif } - printf("\nSTATISTICS\n\n"); + output_offset += snprintf(output + output_offset, output_size - output_offset, "\nSTATISTICS\n\n"); // STATISTICS - printf("%*s %*s %*s | %*s | %*s | %*s\n", + output_offset += snprintf(output + output_offset, output_size - output_offset, "%*s %*s %*s | %*s | %*s | %*s\n", MAXSZ_PREFIX, "", MAXSZ_APP_NAME /*APP_NAME_LEN*/, "", 3*MAXSZ_STR_STAT+2, "WIRED", 3*MAXSZ_STR_STAT+2, "WIFI", 3*MAXSZ_STR_STAT+2, "CELLULAR", 3*MAXSZ_STR_STAT+2, "ALL"); - printf("%*s %*s %*s %*s %*s | %*s %*s %*s | %*s %*s %*s | %*s %*s %*s\n", + output_offset += snprintf(output + output_offset, output_size - output_offset, "%*s %*s %*s %*s %*s | %*s %*s %*s | %*s %*s %*s | %*s %*s %*s\n", MAXSZ_PREFIX, "prefix", MAXSZ_APP_NAME /*APP_NAME_LEN*/, "app_name", MAXSZ_STR_STAT, "throughput", MAXSZ_STR_STAT, "latency", MAXSZ_STR_STAT, "loss_rate", MAXSZ_STR_STAT, "throughput", MAXSZ_STR_STAT, "latency", MAXSZ_STR_STAT, "loss_rate", @@ -208,7 +220,7 @@ static CommandReturn _controlListPolicies_Execute(CommandParser *parser, (i * sizeof(list_policies_command))); addrString = utils_CommandAddressToString( listPoliciesCommand->addressType, &listPoliciesCommand->address, &port); - printf("%*s %*s %*.2f %*.2f %*.2f | %*.2f %*.2f %*.2f | %*.2f %*.2f %*.2f | %*.2f %*.2f %*.2f\n", + output_offset += snprintf(output + output_offset, output_size - output_offset, "%*s %*s %*.2f %*.2f %*.2f | %*.2f %*.2f %*.2f | %*.2f %*.2f %*.2f | %*.2f %*.2f %*.2f\n", MAXSZ_PREFIX, addrString, MAXSZ_APP_NAME, listPoliciesCommand->policy.app_name, MAXSZ_STR_STAT, listPoliciesCommand->policy.stats.wired.throughput, MAXSZ_STR_STAT, listPoliciesCommand->policy.stats.wired.latency, -- cgit 1.2.3-korg