Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHIP QRCode command line tool implementation #770

Merged
merged 4 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,38 @@
"typeinfo": "cpp",
"*.ipp": "cpp",
"aes.h": "c",
"stdio.h": "c"
"stdio.h": "c",
"complex": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"bitset": "cpp",
"iomanip": "cpp",
"hashtable": "cpp",
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__functional_base": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"bit": "cpp",
"chrono": "cpp",
"ios": "cpp",
"locale": "cpp",
"map": "cpp",
"mutex": "cpp",
"queue": "cpp",
"ratio": "cpp",
"set": "cpp",
"stack": "cpp"
},
"files.eol": "\n",
"editor.formatOnSave": true,
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,7 @@ src/lib/support/Makefile
src/lib/support/tests/Makefile
src/platform/Makefile
tests/Makefile
src/qrcodetool/Makefile
])

#
Expand Down
11 changes: 11 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ MAYBE_PLATFORM_SUBDIRS += \
$(NULL)
endif # CONFIG_DEVICE_LAYER

MAYBE_QRCODE_TOOL = \
$(NULL)

if !CONFIG_DEVICE_LAYER
MAYBE_QRCODE_TOOL += \
qrcodetool \
$(NULL)
endif

# Always package (e.g. for 'make dist') these subdirectories.

DIST_SUBDIRS = \
Expand All @@ -63,6 +72,7 @@ DIST_SUBDIRS = \
setup_payload \
crypto \
$(PLATFORM_SUBDIRS) \
qrcodetool \
$(NULL)

# Always build (e.g. for 'make all') these subdirectories.
Expand All @@ -78,6 +88,7 @@ SUBDIRS = \
setup_payload \
crypto \
$(MAYBE_PLATFORM_SUBDIRS) \
$(MAYBE_QRCODE_TOOL) \
$(NULL)

if CHIP_BUILD_TESTS
Expand Down
122 changes: 122 additions & 0 deletions src/chipcli/chiptool.cpp
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;
}
1 change: 1 addition & 0 deletions src/lib/support/logging/CHIPLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ enum LogModule
kLogModule_chipSystemLayer,
kLogModule_EventLogging,
kLogModule_Support,
kLogModule_chipTool,

kLogModule_Max
};
Expand Down
23 changes: 23 additions & 0 deletions src/qrcodetool/Makefile.am
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
123 changes: 123 additions & 0 deletions src/qrcodetool/qrcodetool.cpp
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;
}
Loading