Skip to content

Commit

Permalink
Merge branch 'new-command-parsing'
Browse files Browse the repository at this point in the history
  • Loading branch information
yep1 committed Oct 18, 2021
2 parents 7bee09c + bb3e81e commit f8b12fa
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 1,052 deletions.
27 changes: 0 additions & 27 deletions .vscode/launch.json

This file was deleted.

81 changes: 19 additions & 62 deletions src/commandParser.cpp
Original file line number Diff line number Diff line change
@@ -1,64 +1,18 @@
/****************************************************************************
FileName: commandParser.cpp
Dependencies: See INCLUDES section
Compiler: Visual Studio Community 2015
Company: Yepkit, Lda.
Software License Agreement:
Copyright (c) 2015 Yepkit Lda
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*****************************************************************************
File Description:
Change History:
Rev Date Description
---- ----------- -----------------------------------------
1.0 2015-09-11 First Release
****************************************************************************
* Summary:
* Main program function
*
*
*
*****************************************************************************/


// INCLUDES ---------------------------------------------------------------

// SPDX-License-Identifier: Apache-2.0
/*
* Board routing command parser.
*/
#include <cstdlib>
#include <iostream>
#include <stdio.h>

#include "stdafx.h"
#include "commandParser.h"
#include <ykush.h>
#include <ykushxs.h>
#include <ykush_help.h>
#include <ykush2.h>
#include <ykush3.h>
#include <string>

#include <command_parser.h>

extern unsigned int PRODUCT_ID;

Expand All @@ -69,35 +23,38 @@ enum Board {
YKUSH3,
};


int commandParser(int argc, char** argv)
{
CommandLine cmdl;
Help help(argv[0]);
std::string str_ykush ("ykush");
std::string str_ykush3 ("ykush3");
std::string str_ykush2 ("ykush2");
std::string str_ykushxs ("ykushxs");

if ( argc <= 1) {
if (argc <= 1) {
help.print_all();
return 0;
}

//Parse input options and define action
if( argc >= 2) {
if ( str_ykushxs.compare( argv[1]) == 0 ) {
cmdl.parse(argc, argv);


if(argc >= 2) {
if (str_ykushxs.compare(argv[1]) == 0) {
//YKUSHXS
return ykushxs_cmd_parser(argc, argv);
} else if ( str_ykush2.compare( argv[1]) == 0 ) {
} else if (str_ykush2.compare(argv[1]) == 0) {
//YKUSH2
ykush2_cmd_parser(argc, argv); //Not active yet.
ykush2_cmd_parser(argc, argv);
return 0;
} else if ( str_ykush3.compare( argv[1]) == 0) {
} else if (cmdl.is_board("ykush3")) {
//YKUSH3
return ykush3_cmd_parser(argc, argv);
} else if ( str_ykush.compare( argv[1]) == 0 ) {
return ykush3_action_parser(&cmdl.command);
//return ykush3_cmd_parser(argc, argv);
} else if (str_ykush.compare(argv[1]) == 0) {
//YKUSH
if ( argc >= 3 )
if (argc >= 3)
return ykush_cmd_parser(argc - 1 , &argv[1]);
else
help.print_all();
Expand Down
14 changes: 14 additions & 0 deletions src/help/ykush_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,22 @@ void Help::print_ykush3(void)

std::cout << "--gpio enable|disable Enable / Disable GPIO control interface. Will become active on next reset/power-on.\n\n";

std::cout << "--i2c-slave enable|disable Enables or disables I2C slave mode.\n";
std::cout << "--i2c-set-address <address> Set the board I2C slave address to <address>.\n";
std::cout << " 7 bit addressing is used and should be entered\n";
std::cout << " in hexadecimal format with the 0x prefix.\n";
std::cout << " Only the 7 most significant bits are considered.\n";
std::cout << "--i2c-master enable|disable Enables or disables I2C master mode.\n";
std::cout << "--i2c-write <address> <data_byte 1> ... <data_byte n> Writes n bytes to slave I2C device\n";
std::cout << " with address <address>. <address> is in hexadecimal.\n";
std::cout << "--i2c-read <address> <num_bytes> Reads <num_bytes> from slave I2C device with address <address>.\n";
std::cout << " <address> is in hexadecimal. Only the 7 most significant bits are considered.\n\n";


std::cout << "--reset Resets (reboot) the YKUSH3 board.\n";

std::cout << "--boot Enter and stay in bootloader mode.\n";

std::cout << "-h Display help for YKUSH board specific commands.\n";

std::cout << "--bootloader-version Board bootloader version.\n";
Expand Down
161 changes: 83 additions & 78 deletions src/utils/command_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
/*******************************************************************************
Copyright 2019 Yepkit Lda (www.yepkit.com)
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.
*******************************************************************************/

// SPDX-License-Identifier: Apache-2.0
#include <command_parser.h>
#include <string>
#include <cstring>
#include <ykush_help.h>
#include <cstdlib>



/**
* CommandLine class constructor
*
*/
#include <iostream>

CommandLine::CommandLine()
{
Expand All @@ -40,108 +19,134 @@ CommandLine::~CommandLine()
struct command_option *next_opt = cur_opt->next;
free(cur_opt->name);
struct command_parameter *cur_param = cur_opt->parameters;
while ( cur_param ) {
while (cur_param) {
struct command_parameter *next_param = cur_param->next;
free( cur_param->name );
free( cur_param );
free(cur_param->value);
free(cur_param);
cur_param = next_param;
}
free( cur_opt );
free(cur_opt);
cur_opt = next_opt;
}
}


int CommandLine::parse(int argc, char **argv)
{
command.app_name = argv[0];

if ( argc < 2 )
return -1;

if (argc < 2)
return 1;

int i = 1;
struct command_option *cur_opt = NULL;
struct command_option *root_opt = NULL;

bool exit_while;
while ( i < ( argc - 1 ) ) {

while (i < argc) {
exit_while = false;
if (std::strlen(argv[i]) > 1) {
if ((i == 1) && (argv[1][0] != '-')) {
/* Board name */
struct command_option *tmp_opt;
tmp_opt = (struct command_option *)
std::calloc(1,
sizeof(struct command_option));

if (cur_opt) {
cur_opt->next = tmp_opt;
cur_opt = cur_opt->next;
} else {
root_opt = tmp_opt;
cur_opt = root_opt;
}


if ( std::strlen(argv[i]) > 1 ) {
if ( argv[i][0] == '-') { //is an option
//add new option to command.options
tmp_opt->name = (char *)
std::calloc(std::strlen(argv[i]),
sizeof(char));
std::memcpy(tmp_opt->name,
argv[i],
std::strlen(argv[i]));

}
if (argv[i][0] == '-') {
/* Add new option to command.options. */
struct command_option *tmp_opt;
tmp_opt = (struct command_option *) std::calloc(1, sizeof(struct command_option));
if (cur_opt)
tmp_opt = (struct command_option *)
std::calloc(1,
sizeof(struct command_option));

if (cur_opt) {
cur_opt->next = tmp_opt;
else
cur_opt = cur_opt->next;
} else {
root_opt = tmp_opt;
cur_opt = tmp_opt;
cur_opt->next = NULL;
cur_opt = root_opt;
}

std::memcpy( cur_opt->name, argv[i], std::strlen( argv[i] ) + 1 ); //copy string allocating memory for destination
tmp_opt->name = (char *)
std::calloc(std::strlen(argv[i]),
sizeof(char));

std::memcpy(tmp_opt->name,
argv[i],
std::strlen(argv[i]));

i++;
//get option parameters, if they exist
/* Get option parameters, if they exist. */
struct command_parameter *cur_param = NULL;
struct command_parameter *root_param = NULL;
while ( ( i < ( argc - 1 ) ) && !exit_while ) {
if ( argv[i][0] == '-' ) { //is another option
while ((i < (argc - 1)) && !exit_while) {
i++;
if (argv[i][0] == '-') {
i--;
exit_while = true;
} else { //is a parameter
} else {
/* Parameter. */
//add parameter to option
struct command_parameter *tmp_param;
tmp_param = (struct command_parameter *) std::calloc(1, sizeof(struct command_parameter));
if (cur_param)
tmp_param = (struct command_parameter *)
std::calloc(1,
sizeof(struct command_parameter));
if (cur_param) {
cur_param->next = tmp_param;
else
cur_param = cur_param->next;
} else {
root_param = tmp_param;
cur_param = tmp_param;
cur_param->next = NULL;
std::memcpy( cur_param->name, argv[i], std::strlen( argv[i] ) + 1 ); //copy string allocating memory for destination

i++;
cur_param = root_param;
}

cur_param->value = (char *)
std::calloc(std::strlen(argv[i]),
sizeof(char));
std::memcpy(cur_param->value,
argv[i],
std::strlen(argv[i]));
}
}
cur_opt->parameters = root_param;
}
}
i++;
}

command.options = root_opt;

return 0;
}


int CommandLine::is_board(char *board_name)
/*
* Returns 1 if board_name is in the options,
* and 0 if not.
*/
int CommandLine::is_board(std::string board_name)
{
int i = 0;
std::string str1 ("-b");
std::string str2 ("--board");
std::string str3 (board_name);
//look for -b or --board option
struct command_option *cur_opt;
struct command_parameter *cur_param;

cur_opt = command.options;
while ( cur_opt ) {
if ( (str1.compare( cur_opt->name ) == 0) || (str2.compare( cur_opt->name ) == 0) ) {
//board option was found.
//Now let's check if the board name parameter matches the board_name
cur_param = cur_opt->parameters;
while ( cur_param ) {
if ( str3.compare(cur_param->name) )
return 0; //board match
cur_param = cur_param->next;
}
}
while (cur_opt) {
if (board_name.compare(cur_opt->name) == 0)
return 1;

cur_opt = cur_opt->next;
}
return -1;
return 0;
}


Loading

0 comments on commit f8b12fa

Please sign in to comment.