-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHIP QRCode command line tool implementation (#770)
* CHIP QRCode command line tool implementation * PR Feedback + restyle * Fix styling - Restyled by clang-format * Fix device build Co-authored-by: Bhaskar Sarma <bhaskars@apple.com> Co-authored-by: Restyled.io <commits@restyled.io>
- Loading branch information
1 parent
744cb22
commit ab8ffd5
Showing
13 changed files
with
722 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <support/logging/CHIPLogging.h> | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
#include "chiptool_command_manager.h" | ||
|
||
static int match_command(const char * command_name, const char * name) | ||
{ | ||
return !strncmp(command_name, name, strlen(name)); | ||
} | ||
|
||
static int help(int argc, char ** argv) | ||
{ | ||
chiptool_command_t * cmd = NULL; | ||
for (cmd = commands; cmd->c_name != NULL; cmd++) | ||
{ | ||
ChipLogDetail(chipTool, "%s\t%s\n", cmd->c_name, cmd->c_help); | ||
} | ||
} | ||
|
||
static int usage(const char * prog_name) | ||
{ | ||
ChipLogDetail(chipTool, | ||
"Usage: %s [-h] [command] [opt ...]\n" | ||
"%s commands are:\n", | ||
prog_name, prog_name); | ||
help(0, NULL); | ||
return 2; | ||
} | ||
|
||
static int execute_command(int argc, char ** argv) | ||
{ | ||
if (argc == 0) | ||
{ | ||
return -1; | ||
} | ||
const chiptool_command_t * command_to_execute = NULL; | ||
bool found = false; | ||
|
||
for (command_to_execute = commands; command_to_execute->c_name; command_to_execute++) | ||
{ | ||
if (match_command(command_to_execute->c_name, argv[0])) | ||
{ | ||
found = true; | ||
break; | ||
} | ||
} | ||
|
||
if (found) | ||
{ | ||
ChipLogDetail(chipTool, "Executing cmd %s\n", command_to_execute->c_name); | ||
return command_to_execute->c_func(argc, argv); | ||
} | ||
else | ||
{ | ||
help(0, NULL); | ||
} | ||
} | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
int result = 0; | ||
int do_help = 0; | ||
int ch; | ||
|
||
/* Remember my name. */ | ||
char * prog_name = strrchr(argv[0], '/'); | ||
prog_name = prog_name ? prog_name + 1 : argv[0]; | ||
/* Do getopt stuff for global options. */ | ||
optind = 1; | ||
|
||
while ((ch = getopt(argc, argv, "h")) != -1) | ||
{ | ||
switch (ch) | ||
{ | ||
case 'h': | ||
do_help = 1; | ||
break; | ||
|
||
case '?': | ||
default: | ||
return usage(prog_name); | ||
} | ||
} | ||
|
||
argc -= optind; | ||
argv += optind; | ||
|
||
if (do_help) | ||
{ | ||
/* Munge argc/argv so that argv[0] is something. */ | ||
help(0, NULL); | ||
} | ||
else if (argc > 0) | ||
{ | ||
execute_command(argc, argv); | ||
} | ||
else | ||
{ | ||
result = usage(prog_name); | ||
} | ||
return result; | ||
} |
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,23 @@ | ||
include $(abs_top_nlbuild_autotools_dir)/automake/pre.am | ||
|
||
bin_PROGRAMS = qrcodetool | ||
qrcodetool_SOURCES = qrcodetool.cpp setup_payload_commands.cpp setup_payload_commands.h qrcodetool_command_manager.h | ||
|
||
qrcodetool_CPPFLAGS = \ | ||
$(NLASSERT_CPPFLAGS) \ | ||
-I$(top_srcdir)/src \ | ||
-I$(top_srcdir)/src/lib \ | ||
-I$(top_srcdir)/src/system \ | ||
-I$(top_srcdir)/src/include/ \ | ||
$(NULL) | ||
|
||
qrcodetool_LDADD = \ | ||
$(top_builddir)/src/lib/libCHIP.a \ | ||
$(top_builddir)/src/setup_payload/libSetupPayload.a \ | ||
$(NULL) | ||
|
||
NLFOREIGN_FILE_DEPENDENCIES = \ | ||
$(qrcodetool_LDADD) \ | ||
$(NULL) | ||
|
||
include $(abs_top_nlbuild_autotools_dir)/automake/post.am |
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,123 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <support/logging/CHIPLogging.h> | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
#include "qrcodetool_command_manager.h" | ||
|
||
static int match_command(const char * command_name, const char * name) | ||
{ | ||
return !strncmp(command_name, name, strlen(name)); | ||
} | ||
|
||
static int help(int argc, char ** argv) | ||
{ | ||
qrcodetool_command_t * cmd = NULL; | ||
for (cmd = commands; cmd->c_name != NULL; cmd++) | ||
{ | ||
ChipLogDetail(chipTool, "%s\t%s\n", cmd->c_name, cmd->c_help); | ||
} | ||
return 0; | ||
} | ||
|
||
static int usage(const char * prog_name) | ||
{ | ||
ChipLogDetail(chipTool, | ||
"Usage: %s [-h] [command] [opt ...]\n" | ||
"%s commands are:\n", | ||
prog_name, prog_name); | ||
help(0, NULL); | ||
return 2; | ||
} | ||
|
||
static int execute_command(int argc, char ** argv) | ||
{ | ||
if (argc == 0) | ||
{ | ||
return -1; | ||
} | ||
const qrcodetool_command_t * command_to_execute = NULL; | ||
bool found = false; | ||
|
||
for (command_to_execute = commands; command_to_execute->c_name; command_to_execute++) | ||
{ | ||
if (match_command(command_to_execute->c_name, argv[0])) | ||
{ | ||
found = true; | ||
break; | ||
} | ||
} | ||
|
||
if (found) | ||
{ | ||
ChipLogDetail(chipTool, "Executing cmd %s\n", command_to_execute->c_name); | ||
return command_to_execute->c_func(argc, argv); | ||
} | ||
else | ||
{ | ||
return help(0, NULL); | ||
} | ||
} | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
int result = 0; | ||
int do_help = 0; | ||
int ch; | ||
|
||
/* Remember my name. */ | ||
char * prog_name = strrchr(argv[0], '/'); | ||
prog_name = prog_name ? prog_name + 1 : argv[0]; | ||
/* Do getopt stuff for global options. */ | ||
optind = 1; | ||
|
||
while ((ch = getopt(argc, argv, "h")) != -1) | ||
{ | ||
switch (ch) | ||
{ | ||
case 'h': | ||
do_help = 1; | ||
break; | ||
|
||
case '?': | ||
default: | ||
return usage(prog_name); | ||
} | ||
} | ||
|
||
argc -= optind; | ||
argv += optind; | ||
|
||
if (do_help) | ||
{ | ||
/* Munge argc/argv so that argv[0] is something. */ | ||
result = help(0, NULL); | ||
} | ||
else if (argc > 0) | ||
{ | ||
result = execute_command(argc, argv); | ||
} | ||
else | ||
{ | ||
result = usage(prog_name); | ||
} | ||
return result; | ||
} |
Oops, something went wrong.