-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Api to set the default target Signed-off-by: Artiom Divak <[email protected]>
- Loading branch information
1 parent
5eec39c
commit 7e28ac4
Showing
16 changed files
with
219 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright Contributors to the Eclipse BlueChi project | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
*/ | ||
#include "method-default-target.h" | ||
#include "client.h" | ||
#include "usage.h" | ||
|
||
#include "libbluechi/common/opt.h" | ||
|
||
static int method_get_default_target_on(Client *client, char *node_name) { | ||
_cleanup_sd_bus_error_ sd_bus_error error = SD_BUS_ERROR_NULL; | ||
_cleanup_sd_bus_message_ sd_bus_message *message = NULL; | ||
char *result = NULL; | ||
int r = 0; | ||
|
||
r = assemble_object_path_string(NODE_OBJECT_PATH_PREFIX, node_name, &client->object_path); | ||
if (r < 0) { | ||
return r; | ||
} | ||
|
||
r = sd_bus_call_method( | ||
client->api_bus, | ||
BC_INTERFACE_BASE_NAME, | ||
client->object_path, | ||
NODE_INTERFACE, | ||
"GetDefaultTarget", | ||
&error, | ||
&message, | ||
""); | ||
if (r < 0) { | ||
fprintf(stderr, "Failed to issue method call: %s\n", error.message); | ||
return r; | ||
} | ||
|
||
r = sd_bus_message_read(message, "s", &result); | ||
if (r < 0) { | ||
fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r)); | ||
return r; | ||
} | ||
|
||
printf("The default target of this node is: %s\n", result); | ||
|
||
return r; | ||
} | ||
|
||
|
||
static int method_set_default_target_on(Client *client, char *node_name, char *target, char *force) { | ||
_cleanup_sd_bus_error_ sd_bus_error error = SD_BUS_ERROR_NULL; | ||
_cleanup_sd_bus_message_ sd_bus_message *message = NULL; | ||
int r = 0; | ||
bool force_b = 0; | ||
if (!strcmp(force, "true")) { | ||
force_b = 1; | ||
} else if (!strcmp(force, "false")) { | ||
force_b = 0; | ||
} else { | ||
fprintf(stderr, "Please provide a true/false value for force parameter\n"); | ||
return -1; | ||
} | ||
|
||
|
||
r = assemble_object_path_string(NODE_OBJECT_PATH_PREFIX, node_name, &client->object_path); | ||
if (r < 0) { | ||
return r; | ||
} | ||
|
||
r = sd_bus_call_method( | ||
client->api_bus, | ||
BC_INTERFACE_BASE_NAME, | ||
client->object_path, | ||
NODE_INTERFACE, | ||
"SetDefaultTarget", | ||
&error, | ||
&message, | ||
"sb", | ||
target, | ||
force_b); | ||
if (r < 0) { | ||
fprintf(stderr, "Failed to issue method call: %s\n", error.message); | ||
return r; | ||
} | ||
|
||
r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "(sss)"); | ||
if (r < 0) { | ||
fprintf(stderr, "Failed to open the strings array container: %s\n", strerror(-r)); | ||
return r; | ||
} | ||
printf("["); | ||
for (;;) { | ||
const char *result1 = NULL; | ||
const char *result2 = NULL; | ||
const char *result3 = NULL; | ||
|
||
r = sd_bus_message_read(message, "(sss)", &result1, &result2, &result3); | ||
if (r < 0) { | ||
fprintf(stderr, "Failed to read the SetDefaultTarget response: %s\n", strerror(-r)); | ||
return r; | ||
} | ||
if (r == 0) { | ||
break; | ||
} | ||
printf("(%s %s %s),", result1, result2, result3); | ||
} | ||
printf("]\n"); | ||
|
||
printf("The default target of node %s is now: %s\n", node_name, target); | ||
|
||
return r; | ||
} | ||
|
||
void usage_method_get_default_target() { | ||
usage_print_header(); | ||
usage_print_usage("bluechictl get-default [nodename]"); | ||
} | ||
|
||
void usage_method_set_default_target() { | ||
usage_print_header(); | ||
usage_print_usage("bluechictl set-default [nodename]"); | ||
} | ||
|
||
|
||
int method_get_default_target(Command *command, void *userdata) { | ||
return method_get_default_target_on(userdata, command->opargv[0]); | ||
} | ||
|
||
int method_set_default_target(Command *command, void *userdata) { | ||
return method_set_default_target_on( | ||
userdata, command->opargv[0], command->opargv[1], command->opargv[2]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
summary: Test the set and get default target function | ||
id: 6cfa53b6-7429-43fd-a9b5-ff59e98b7408 |
Oops, something went wrong.