Skip to content

Commit

Permalink
PR Feedback + restyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhaskar-Sarma committed May 19, 2020
1 parent 7347bd9 commit 979557d
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 20 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2085,7 +2085,7 @@ src/lib/support/Makefile
src/lib/support/tests/Makefile
src/platform/Makefile
tests/Makefile
src/chipcli/Makefile
src/qrcodetool/Makefile
])

#
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ DIST_SUBDIRS = \
setup_payload \
crypto \
$(PLATFORM_SUBDIRS) \
chipcli \
qrcodetool \
$(NULL)

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

if CHIP_BUILD_TESTS
Expand Down
15 changes: 8 additions & 7 deletions src/chipcli/chipqrcodetool.cpp → src/chipcli/chiptool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <string.h>
#include <unistd.h>

#include "chipqrcodetool_command_manager.h"
#include "chiptool_command_manager.h"

static int match_command(const char * command_name, const char * name)
{
Expand All @@ -30,13 +30,14 @@ static int match_command(const char * command_name, const char * name)

static int help(int argc, char ** argv)
{
chipqrcodetool_command_t * cmd = NULL;
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,
Expand All @@ -53,8 +54,8 @@ static int execute_command(int argc, char ** argv)
{
return -1;
}
const chipqrcodetool_command_t * command_to_execute = NULL;
bool found = false;
const chiptool_command_t * command_to_execute = NULL;
bool found = false;

for (command_to_execute = commands; command_to_execute->c_name; command_to_execute++)
{
Expand All @@ -66,11 +67,11 @@ static int execute_command(int argc, char ** argv)
}

if (found)
{
{
ChipLogDetail(chipTool, "Executing cmd %s\n", command_to_execute->c_name);
return command_to_execute->c_func(argc, argv);
}
else
else
{
help(0, NULL);
}
Expand All @@ -87,7 +88,7 @@ int main(int argc, char ** argv)
prog_name = prog_name ? prog_name + 1 : argv[0];
/* Do getopt stuff for global options. */
optind = 1;
optreset = 1;

while ((ch = getopt(argc, argv, "h")) != -1)
{
switch (ch)
Expand Down
12 changes: 8 additions & 4 deletions src/chipcli/Makefile.am → src/qrcodetool/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
include $(abs_top_nlbuild_autotools_dir)/automake/pre.am

bin_PROGRAMS = chipqrcodetool
chipqrcodetool_SOURCES = chipqrcodetool.cpp setup_payload_commands.cpp setup_payload_commands.h chipqrcodetool_command_manager.h
bin_PROGRAMS = qrcodetool
qrcodetool_SOURCES = qrcodetool.cpp setup_payload_commands.cpp setup_payload_commands.h qrcodetool_command_manager.h

chipqrcodetool_CPPFLAGS = \
qrcodetool_CPPFLAGS = \
$(NLASSERT_CPPFLAGS) \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/lib \
-I$(top_srcdir)/src/system \
-I$(top_srcdir)/src/include/ \
$(NULL)

chipqrcodetool_LDADD = \
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
* limitations under the License.
*/

#ifndef CHIPTOOL_CMD_MANAGER_H
#define CHIPTOOL_CMD_MANAGER_H
#ifndef QRCODETOOL_CMD_MANAGER_H
#define QRCODETOOL_CMD_MANAGER_H

#include "setup_payload_commands.h"

typedef int (*command_func)(int argc, char * const * argv);

typedef struct chipqrcodetool_command_t
typedef struct qrcodetool_command_t
{
const char * c_name; /* name of the command. */
command_func c_func; /* function to execute the command. */
const char * c_usage; /* usage string for command. */
const char * c_help; /* help string for (or description of) command. */
} chipqrcodetool_command_t;
} qrcodetool_command_t;

chipqrcodetool_command_t commands[] = { { "generate-qr-code", setup_payload_operation_generate_qr_code,
qrcodetool_command_t commands[] = { { "generate-qr-code", setup_payload_operation_generate_qr_code,
" -f File path of payload.\n", "Generate qr code from payload in text file." },

{ "generate-manual-code", setup_payload_operation_generate_manual_code,
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion src/setup_payload/SetupPayloadHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#ifndef SetupPayloadHelper_h
#define SetupPayloadHelper_h

#include <stdio.h>
#include <string>
#include <core/CHIPError.h>

Expand Down

0 comments on commit 979557d

Please sign in to comment.