From c88cef8acabcb80e495224a8f45695a50877c7c3 Mon Sep 17 00:00:00 2001 From: Paulo Date: Fri, 3 Jul 2020 17:39:49 +0100 Subject: [PATCH 1/8] Ykush3 new command parsing. All currently deployed actions have been migrated to new parsing method. New I2C actions are work in progress. --- src/commandParser.cpp | 81 +--- src/help/ykush_help.cpp | 14 + src/utils/command_parser.cpp | 161 ++++---- src/utils/command_parser.h | 56 +-- src/ykush3/ykush3.cpp | 700 +++++++++-------------------------- src/ykush3/ykush3.h | 28 +- src/ykushcmd.vcxproj | 179 --------- src/ykushcmd.vcxproj.filters | 69 ---- src/ykushcmd.vcxproj.user | 6 - 9 files changed, 326 insertions(+), 968 deletions(-) delete mode 100644 src/ykushcmd.vcxproj delete mode 100644 src/ykushcmd.vcxproj.filters delete mode 100644 src/ykushcmd.vcxproj.user diff --git a/src/commandParser.cpp b/src/commandParser.cpp index 6827eab..63de5d7 100644 --- a/src/commandParser.cpp +++ b/src/commandParser.cpp @@ -1,56 +1,10 @@ -/**************************************************************************** - 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 #include #include - -#include "stdafx.h" #include "commandParser.h" #include #include @@ -58,7 +12,7 @@ #include #include #include - +#include extern unsigned int PRODUCT_ID; @@ -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(); diff --git a/src/help/ykush_help.cpp b/src/help/ykush_help.cpp index e1f004b..ef98258 100644 --- a/src/help/ykush_help.cpp +++ b/src/help/ykush_help.cpp @@ -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
Set the board I2C slave address to
.\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 slave mode.\n"; + std::cout << "--i2c-write
... Writes to slave I2C device\n"; + std::cout << " with address
.
is in hexadecimal.\n"; + std::cout << "--i2c-read
Reads from slave I2C device with address
.\n"; + std::cout << "
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"; diff --git a/src/utils/command_parser.cpp b/src/utils/command_parser.cpp index 561008b..b5794f5 100644 --- a/src/utils/command_parser.cpp +++ b/src/utils/command_parser.cpp @@ -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 #include #include #include #include - - - -/** - * CommandLine class constructor - * - */ +#include CommandLine::CommandLine() { @@ -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; } diff --git a/src/utils/command_parser.h b/src/utils/command_parser.h index 5123e7e..619307e 100644 --- a/src/utils/command_parser.h +++ b/src/utils/command_parser.h @@ -1,26 +1,12 @@ -/******************************************************************************* -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 #ifndef _COMMAND_PARSER_H_ #define _COMMAND_PARSER_H_ +#include struct command_parameter { - char *name; + char *value; struct command_parameter *next; }; @@ -35,13 +21,9 @@ struct command_line { struct command_option *options; }; -/** - * \defgroup command_line Command line - */ -/** - * \ingroup command_line - * \brief Parses the command line and sets the command object. +/* + * Parses the command line and sets the command object. * */ class CommandLine { @@ -50,33 +32,33 @@ class CommandLine { CommandLine(); ~CommandLine(); - /** - * \brief Processes the command line and sets the command property. + /* + * Processes the command line and sets the command property. */ int parse(int argc, char **argv); - /** - * \brief Iterates through the command_line object and looks for a board option. - * - * \param board_name board name. + /* + * Iterates through the command_line object and looks for a board option. * - * \retval 0 board is in options. - * \retval -1 board is not in option. + * Input parameters: + * + * board_name char array with the board name. * + * Return values: + * + * 0 A valid board name was provided. + * 1 Unknown error. + * 2 Invalid board name provided. */ - int is_board(char *board_name); + int is_board(std::string board_name); + struct command_line command; protected: - - private: - struct command_line command; - }; - #endif diff --git a/src/ykush3/ykush3.cpp b/src/ykush3/ykush3.cpp index 06b11a4..e809aa2 100644 --- a/src/ykush3/ykush3.cpp +++ b/src/ykush3/ykush3.cpp @@ -1,19 +1,7 @@ -/******************************************************************************* -Copyright 2017 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 #include #include @@ -23,566 +11,237 @@ limitations under the License. #include #include +/* + * Defines the actions to be taken based on the command line + * parameters and options. + */ +int ykush3_action_parser(struct command_line *cmdl) +{ + Ykush3 ykush3; + struct command_option *cur_opt; + struct command_parameter *cur_param; + char *serial; + bool routed = false; + + std::string opt_name; + std::string param_name; + cur_opt = cmdl->options; + while (cur_opt) { + opt_name = cur_opt->name; + + if (opt_name.compare("-s") == 0) { + ykush3.set_usb_serial(cur_opt->parameters->value); + } else if (opt_name.compare("-u") == 0) { + return ykush3.port_up(cur_opt->parameters->value[0]); + } else if (opt_name.compare("-d") == 0) { + return ykush3.port_down(cur_opt->parameters->value[0]); + } else if (opt_name.compare("-g") == 0) { + return ykush3.get_port_status(cur_opt->parameters->value[0]); + } else if (opt_name.compare("-on") == 0) { + return ykush3.port_up('4'); + } else if (opt_name.compare("-off") == 0) { + return ykush3.port_down('4'); + } else if (opt_name.compare("-l") == 0) { + return ykush3_list_attached(); + } else if (opt_name.compare("-c") == 0) { + return ykush3.config_port (cur_opt->parameters->value[0], + cur_opt->parameters->next->value[0]); + } else if (opt_name.compare("--reset") == 0) { + return ykush3.reset(); + } else if (opt_name.compare("-r") == 0) { + return ykush3.read_io(cur_opt->parameters->value[0]); + } else if (opt_name.compare("-w") == 0) { + return ykush3.write_io(cur_opt->parameters->value[0], + cur_opt->parameters->next->value[0]); + } else if (opt_name.compare("--gpio") == 0) { + param_name = cur_opt->parameters->value; + if (param_name.compare("enable") == 0) + return ykush3.gpio_ctrl_enable(); + else if (param_name.compare("disable") == 0) + return ykush3.gpio_ctrl_disable(); + } else if (opt_name.compare("--boot") == 0) { + return ykush3.enter_bootloader(); + } else if (opt_name.compare("--bootloader-version") == 0) { + return ykush3.display_version_bootloader(); + } else if (opt_name.compare("--firmware-version") == 0) { + return ykush3.display_version_firmware(); + } else if (opt_name.compare("--i2c-slave") == 0) { + param_name = cur_opt->parameters->value; + if (param_name.compare("enable") == 0) + return ykush3.i2c_enable_disable_control(true); + else if (param_name.compare("disable") == 0) + return ykush3.i2c_enable_disable_control(false); + } else if (opt_name.compare("--i2c-master") == 0) { + param_name = cur_opt->parameters->value; + if (param_name.compare("enable") == 0) + return ykush3.i2c_enable_disable_gateway(true); + else if (param_name.compare("disable") == 0) + return ykush3.i2c_enable_disable_gateway(false); + } else if (opt_name.compare("--i2c-set-address") == 0) { + if (cur_opt->parameters) + return ykush3.i2c_set_address(cur_opt->parameters->value); + } else if (opt_name.compare("--i2c-write") == 0) { + // criar uma funcção que trate disto. + } -extern char *app_exc_name; - - -int -ykush3_cmd_parser (int argc, char** argv) - { - char bySerialFlag = 0; - enum ykush3Action action = YKUSH3_HELP; - Ykush3 ykush3; - char port; - char value; - char status_response = 0; - bool action_taken = false; - - if (argc < 3) - { - ykush3.print_help (); - return -1; - } - - std::string str = argv[2]; - - if (str.compare ("-s") == 0) - { - //BY SERIAL - if (argc < 6) - { - ykush3.print_help (); - return -1; - } - // set serialç in object - ykush3.set_usb_serial(argv[3]); - - str = argv[4]; - bySerialFlag = 1; - if (str.compare ("-u") == 0 ) - { - action = YKUSH3_PORT_UP; - port = argv[5][0]; - } - else if (str.compare ("-d") == 0 ) - { - action = YKUSH3_PORT_DOWN; - port = argv[5][0]; - } - else if (str.compare ("-g") == 0 ) - { - action = YKUSH3_GET_STATUS; - port = argv[5][0]; - } - else if (str.compare ("-on") == 0 ) - { - action = YKUSH3_EXT_CTRL_ON; - } - else if (str.compare ("-off") == 0 ) - { - action = YKUSH3_EXT_CTRL_OFF; - } - else if (str.compare ("-w") == 0 ) - { - if (argc < 7) - { - ykush3.print_help (); - return -1; - } - action = YKUSH3_WRITE_IO; - port = argv[5][0]; - value = argv[6][0]; - } - else if (str.compare ("-r") == 0) - { - action = YKUSH3_READ_IO; - port = argv[5][0]; - } - else if (str.compare ("-c") == 0) - { - action = YKUSH3_CONFIG; - port = argv[5][0]; - value = argv[6][0]; - } - else if (str.compare("--reset") == 0) - { - action = YKUSH3_RESET; - } - else if (str.compare ("--gpio") == 0) - { - std::string str2 = argv[5]; - if (str2.compare ("enable") == 0) - { - action = YKUSH3_GPIO_EN; - } - else if (str2.compare ("disable") == 0 ) - { - action = YKUSH3_GPIO_DIS; - } - else - { - ykush3.print_help (); - return -1; - } - } - else if (str.compare ("--boot") == 0) - { - action = YKUSH3_ENTER_BOOTLOADER; - } - else if (str.compare ("--firmware-version") == 0) - { - action = YKUSH3_FIRMWARE_VERSION; - } - else if (str.compare ("--bootloader-version") == 0) - { - action = YKUSH3_BOOTLOADER_VERSION; - } - else - { - ykush3.print_help (); - return -1; - } - } - // Without serial prefix - else if (str.compare ("-u") == 0) - { - if (argc < 3) - { - ykush3.print_help (); - return -1; - } - action = YKUSH3_PORT_UP; - port = argv[3][0]; - } - else if (str.compare ("-d") == 0) - { - if (argc < 4) - { - ykush3.print_help (); - return -1; - } - action = YKUSH3_PORT_DOWN; - port = argv[3][0]; - } - else if (str.compare ("-l") == 0) - { - action = YKUSH3_LIST_BOARDS; - } - else if (str.compare ("-g") == 0) - { - if (argc < 4) - { - ykush3.print_help (); - return -1; - } - action = YKUSH3_GET_STATUS; - port = argv[3][0]; - } - else if (str.compare ("-on") == 0) - { - action = YKUSH3_EXT_CTRL_ON; - } - else if (str.compare ("-off") == 0) - { - action = YKUSH3_EXT_CTRL_OFF; - } - else if (str.compare ("-w") == 0) - { - if (argc < 5) - { - ykush3.print_help (); - return -1; - } - action = YKUSH3_WRITE_IO; - port = argv[3][0]; - value = argv[4][0]; - } - else if (str.compare ("-r") == 0) - { - action = YKUSH3_READ_IO; - port = argv[3][0]; - } - else if (str.compare ("-c") == 0) - { - if (argc < 5) - { - ykush3.print_help (); - return -1; - } - action = YKUSH3_CONFIG; - port = argv[3][0]; - value = argv[4][0]; - } - else if (str.compare ("--reset") == 0) - { - action = YKUSH3_RESET; - } - else if (str.compare ("--boot") == 0) - { - action = YKUSH3_ENTER_BOOTLOADER; - } - else if (str.compare ("--gpio") == 0) - { - std::string str2 = argv[3]; - if (str2.compare ("enable") == 0) - { - action = YKUSH3_GPIO_EN; - } - else if (str2.compare ("disable") == 0) - { - action = YKUSH3_GPIO_DIS; - } - else - { - ykush3.print_help (); - return -1; - } - } - else if (str.compare ("--firmware-version") == 0) - { - action = YKUSH3_FIRMWARE_VERSION; - } - else if (str.compare ("--bootloader-version") == 0) - { - action = YKUSH3_BOOTLOADER_VERSION; - } - else - { - ykush3.print_help (); - return -1; - } - - switch (action) - { - case YKUSH3_PORT_UP: - if (bySerialFlag) - return ykush3.port_up (argv[3], port); - else - return ykush3.port_up (NULL, port); - break; - - case YKUSH3_PORT_DOWN: - if (bySerialFlag) - return ykush3.port_down (argv[3], port); - else - return ykush3.port_down (NULL, port); - break; - - case YKUSH3_LIST_BOARDS: - ykush3_list_attached (); - break; - - case YKUSH3_GET_STATUS: - if (bySerialFlag) - { - status_response = ykush3.get_port_status (argv[3], port); - if (status_response < 0) - return status_response; - if (status_response >> 4) - printf ("\n\nDownstream port %d is ON\n\n", status_response & 0x0F); - else - printf ("\n\nDownstream port %d is OFF\n\n", status_response & 0x0F); - return 0; - } - else - { - status_response = ykush3.get_port_status (NULL, port); - if (status_response < 0) - return status_response; - if (status_response >> 4) - printf ("\n\nDownstream port %d is ON\n\n", status_response & 0x0F); - else - printf ("\n\nDownstream port %d is OFF\n\n", status_response & 0x0F); - return 0; - } - break; - - case YKUSH3_EXT_CTRL_ON: - if (bySerialFlag) - return ykush3.port_up (argv[3], '4'); - else - return ykush3.port_up (NULL, '4'); - break; - - case YKUSH3_EXT_CTRL_OFF: - if (bySerialFlag) - return ykush3.port_down (argv[3], '4'); - else - return ykush3.port_down (NULL, '4'); - break; - - case YKUSH3_WRITE_IO: - if (bySerialFlag) - return ykush3.write_io (argv[3], port, value); - else - return ykush3.write_io (NULL, port, value); - return 0; - break; - - case YKUSH3_READ_IO: - if (bySerialFlag) - printf ("\n%d\n", ykush3.read_io(argv[3], port)); - else - printf ("\n%d\n", ykush3.read_io(NULL, port)); - return 0; - break; - - case YKUSH3_CONFIG: - if (bySerialFlag) - return ykush3.config_port (argv[3], port, value); - else - return ykush3.config_port (NULL, port, value); - break; - - case YKUSH3_RESET: - if (bySerialFlag) - return ykush3.reset (argv[3]); - else - return ykush3.reset (NULL); - break; - - case YKUSH3_GPIO_EN: - if (bySerialFlag) - return ykush3.gpio_ctrl_enable (argv[3]); - else - return ykush3.gpio_ctrl_enable (NULL); - break; - - case YKUSH3_GPIO_DIS: - if (bySerialFlag) - return ykush3.gpio_ctrl_disable (argv[3]); - else - return ykush3.gpio_ctrl_disable (NULL); - break; - - case YKUSH3_ENTER_BOOTLOADER: - if (bySerialFlag) - return ykush3.enter_bootloader (argv[3]); - else - return ykush3.enter_bootloader (NULL); - break; - - case YKUSH3_FIRMWARE_VERSION: - return ykush3.display_version_firmware (); - break; - - case YKUSH3_BOOTLOADER_VERSION: - return ykush3.display_version_bootloader (); - break; - - default: - ykush3.print_help (); - return -1; - break; - - } - - return 0; - } - - + cur_opt = cur_opt->next; + } + if (cmdl) + ykush3.print_help(cmdl->app_name); + return 1; +} -int Ykush3::port_up(char *serial, char port) +int Ykush3::port_up(char port) { - switch(port) { + switch (port) { case '1': hid_report_out[0] = 0x11; break; - case '2': hid_report_out[0] = 0x12; break; - case '3': hid_report_out[0] = 0x13; break; - case 'a': hid_report_out[0] = 0x1a; break; - case '4': hid_report_out[0] = 0x14; break; - default: return -1; break; - } - - //send HID report to board - return sendHidReport(serial, hid_report_out, hid_report_in, 64); - + return sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); } - - -int Ykush3::port_down(char *serial, char port) +int Ykush3::port_down(char port) { - switch(port) - { + switch (port) { case '1': - hid_report_out[0] = 0x01; - break; - + hid_report_out[0] = 0x01; + break; case '2': - hid_report_out[0] = 0x02; - break; - + hid_report_out[0] = 0x02; + break; case '3': - hid_report_out[0] = 0x03; - break; - + hid_report_out[0] = 0x03; + break; case 'a': - hid_report_out[0] = 0x0a; - break; - + hid_report_out[0] = 0x0a; + break; case '4': - hid_report_out[0] = 0x04; - break; - + hid_report_out[0] = 0x04; + break; default: - return 0; - break; - - } + return 0; + break; + } - //send HID report to board - return sendHidReport(serial, hid_report_out, hid_report_in, 64); - - + return sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); } -int Ykush3::get_port_status(char *serial, char port) +int Ykush3::get_port_status(char port) { - int status; + int status; - switch(port) - { + switch (port) { case '1': - hid_report_out[0] = 0x21; - break; - + hid_report_out[0] = 0x21; + break; case '2': - hid_report_out[0] = 0x22; - break; - + hid_report_out[0] = 0x22; + break; case '3': - hid_report_out[0] = 0x23; - break; - + hid_report_out[0] = 0x23; + break; case '4': - hid_report_out[0] = 0x24; - break; - + hid_report_out[0] = 0x24; + break; default: - return 0; - break; - - } + return -1; + break; + } - //send HID report to board - sendHidReport(serial, hid_report_out, hid_report_in, 64); - - //handle board response HID report - status = hid_report_in[1]; - - return status; + sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); + + status = hid_report_in[1]; + return status; } -int Ykush3::write_io(char *serial, char port, char value) +int Ykush3::write_io(char port, char value) { - hid_report_out[0] = 0x31; - if(value=='0'){ - hid_report_out[2] = 0; - } else { - hid_report_out[2] = 1; - } - + hid_report_out[0] = 0x31; + if (value == '0') + hid_report_out[2] = 0; + else + hid_report_out[2] = 1; - switch(port) - { + switch (port) { case '1': - hid_report_out[1] = 0x01; - break; - + hid_report_out[1] = 0x01; + break; case '2': - hid_report_out[1] = 0x02; - break; - + hid_report_out[1] = 0x02; + break; case '3': - hid_report_out[1] = 0x03; - break; - + hid_report_out[1] = 0x03; + break; default: - return 0; - break; - - } + return 1; + break; + } - //send HID report to board - return sendHidReport(serial, hid_report_out, hid_report_in, 64); - + return sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); } -int Ykush3::read_io(char *serial, char port) +int Ykush3::read_io(char port) { - - hid_report_out[0] = 0x30; + hid_report_out[0] = 0x30; - switch(port) - { + switch (port) { case '1': - hid_report_out[1] = 0x01; - break; - + hid_report_out[1] = 0x01; + break; case '2': - hid_report_out[1] = 0x02; - break; - + hid_report_out[1] = 0x02; + break; case '3': - hid_report_out[1] = 0x03; - break; - + hid_report_out[1] = 0x03; + break; default: - return 0; - break; - - } - - - //send HID report to board - return sendHidReport(serial, hid_report_out, hid_report_in, 64); + return 1; + break; + } - //process response - return hid_report_in[3]; - + sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); + return hid_report_in[3]; } -int Ykush3::config_port(char *serial, char port, char value) +int Ykush3::config_port(char port, char value) { hid_report_out[0] = 0x41; - if ( value == '0' ) + if (value == '0') hid_report_out[2] = 0; - else if ( value == '1' ) + else if (value == '1') hid_report_out[2] = 1; else hid_report_out[2] = 2; //Persist mode - switch ( port ) { + switch (port) { case '1': hid_report_out[1] = 0x01; break; @@ -596,60 +255,53 @@ int Ykush3::config_port(char *serial, char port, char value) hid_report_out[1] = 0x04; break; default: - return 0; + return 1; break; } - //send HID report to board - return sendHidReport(serial, hid_report_out, hid_report_in, 64); - + return sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); } -int Ykush3::reset(char *serial) +int Ykush3::reset(void) { hid_report_out[0] = 0x55; - //send HID report to board - sendHidReport(serial, hid_report_out, hid_report_in, 64); + sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); return 0; } -int -Ykush3::gpio_ctrl_enable (char *serial) - { - hid_report_out[0] = 0x32; - hid_report_out[1] = 0x01; +int Ykush3::gpio_ctrl_enable(void) +{ + hid_report_out[0] = 0x32; + hid_report_out[1] = 0x01; - //send HID report to board - sendHidReport (serial, hid_report_out, hid_report_in, 64); - return 0; - } + sendHidReport (usb_serial, hid_report_out, hid_report_in, 64); + return 0; +} -int -Ykush3::gpio_ctrl_disable (char *serial) - { - hid_report_out[0] = 0x32; - hid_report_out[1] = 0x00; +int Ykush3::gpio_ctrl_disable(void) +{ + hid_report_out[0] = 0x32; + hid_report_out[1] = 0x00; - //send HID report to board - sendHidReport (serial, hid_report_out, hid_report_in, 64); - return 0; - } + sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); + return 0; +} int -Ykush3::enter_bootloader (char *serial) +Ykush3::enter_bootloader (void) { hid_report_out[0] = 0x42; //send HID report to board - sendHidReport (serial, hid_report_out, hid_report_in, 64); + sendHidReport (usb_serial, hid_report_out, hid_report_in, 64); return 0; } @@ -841,9 +493,9 @@ int ykush3_list_attached() } -void Ykush3::print_help(void) +void Ykush3::print_help(char *app_name) { - Help help(app_exc_name); + Help help(app_name); help.print_version(); help.print_usage(); diff --git a/src/ykush3/ykush3.h b/src/ykush3/ykush3.h index a22d568..bf30bc0 100644 --- a/src/ykush3/ykush3.h +++ b/src/ykush3/ykush3.h @@ -62,33 +62,33 @@ class Ykush3 : public UsbDevice } //Downstream ports and 5V EXT port control - int get_port_status(char *serial, char port); //get downstream port status + int get_port_status(char port); //get downstream port status - int port_up(char *serial, char port); + int port_up(char port); - int port_down(char *serial, char port); + int port_down(char port); //GPIO control - int write_io(char *serial, char port, char value); + int write_io(char port, char value); - int read_io(char *serial, char port); + int read_io(char port); - int gpio_ctrl_enable(char *serial); + int gpio_ctrl_enable(void); - int gpio_ctrl_disable(char *serial); + int gpio_ctrl_disable(void); - int enter_bootloader(char *serial); + int enter_bootloader(void); //Configurations control - int config_port(char *serial, char port, char value); + int config_port(char port, char value); //Reset - int reset(char *serial); + int reset(void); //Help - void print_help(void); + void print_help(char *app_name); //I2C int i2c_enable_disable_control(bool enable_flag); //ToDo @@ -102,9 +102,8 @@ class Ykush3 : public UsbDevice int display_version_firmware (void); int set_usb_serial(char *serial); - - private: + private: char *usb_serial; }; @@ -116,6 +115,9 @@ class Ykush3 : public UsbDevice //FUNCTIONS //--------------------------------- + +int ykush3_action_parser(struct command_line *cmdl); + int ykush3_cmd_parser(int argc, char** argv); diff --git a/src/ykushcmd.vcxproj b/src/ykushcmd.vcxproj deleted file mode 100644 index 6e2805b..0000000 --- a/src/ykushcmd.vcxproj +++ /dev/null @@ -1,179 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - {C8815452-C975-41CE-8E08-E12E7E488901} - Win32Proj - ykushcmd - 10.0.15063.0 - - - - Application - true - v141 - Unicode - - - Application - false - v141_xp - true - Unicode - - - Application - true - v141 - Unicode - - - Application - false - v141 - true - Unicode - - - - - - - - - - - - - - - - - - - - - true - - - true - - - false - - - false - - - - Use - Level3 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Use - Level3 - Disabled - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - Use - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - $(ProjectDir);$(ProjectDir)\windows;$(ProjectDir)\help;$(ProjectDir)\ykush;$(ProjectDir)\ykushxs;%(AdditionalIncludeDirectories) - - - Console - true - true - true - $(ProjectDir)\windows;%(AdditionalLibraryDirectories) - hidapi.lib;%(AdditionalDependencies) - - - - - Level3 - Use - MaxSpeed - true - true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - - - - - - - - - - - - - - - %(PreprocessorDefinitions) - - - Create - Create - Create - Create - - - - - - - - - - \ No newline at end of file diff --git a/src/ykushcmd.vcxproj.filters b/src/ykushcmd.vcxproj.filters deleted file mode 100644 index 0ac0498..0000000 --- a/src/ykushcmd.vcxproj.filters +++ /dev/null @@ -1,69 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - \ No newline at end of file diff --git a/src/ykushcmd.vcxproj.user b/src/ykushcmd.vcxproj.user deleted file mode 100644 index 7e04c94..0000000 --- a/src/ykushcmd.vcxproj.user +++ /dev/null @@ -1,6 +0,0 @@ - - - - false - - \ No newline at end of file From 4c65a6a46aa218c1106778932fedeca1205266c7 Mon Sep 17 00:00:00 2001 From: Paulo Date: Mon, 6 Jul 2020 14:41:36 +0100 Subject: [PATCH 2/8] wip - write i2c --- src/ykush3/ykush3.cpp | 57 +++++++++++++++++++++++++++++++------------ src/ykush3/ykush3.h | 7 ++---- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/ykush3/ykush3.cpp b/src/ykush3/ykush3.cpp index e809aa2..5ae326c 100644 --- a/src/ykush3/ykush3.cpp +++ b/src/ykush3/ykush3.cpp @@ -79,9 +79,10 @@ int ykush3_action_parser(struct command_line *cmdl) return ykush3.i2c_enable_disable_gateway(false); } else if (opt_name.compare("--i2c-set-address") == 0) { if (cur_opt->parameters) - return ykush3.i2c_set_address(cur_opt->parameters->value); + return ykush3.i2c_set_address( + cur_opt->parameters->value); } else if (opt_name.compare("--i2c-write") == 0) { - // criar uma funcção que trate disto. + return i2c_write_buffer(cur_opt); } cur_opt = cur_opt->next; @@ -305,8 +306,6 @@ Ykush3::enter_bootloader (void) return 0; } - - int Ykush3::i2c_enable_disable_control(bool enable_flag) { hid_report_out[0] = 0x51; @@ -319,7 +318,7 @@ int Ykush3::i2c_enable_disable_control(bool enable_flag) sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); - if ( (hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x51) ) { + if ((hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x51)) { //command executed with success return 0; } @@ -327,7 +326,6 @@ int Ykush3::i2c_enable_disable_control(bool enable_flag) return 1; } - int Ykush3::i2c_enable_disable_gateway(bool enable_flag) { hid_report_out[0] = 0x51; @@ -348,7 +346,6 @@ int Ykush3::i2c_enable_disable_gateway(bool enable_flag) return 1; } - int Ykush3::i2c_set_address(char *i2c_address) { std::cout << "i2c_set_address com address: " << i2c_address << std::endl; @@ -359,7 +356,7 @@ int Ykush3::i2c_set_address(char *i2c_address) hex2bin(i2c_address + 2, &hid_report_out[2], 2); sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); - if ( (hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x51) ) { + if ((hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x51)) { //command executed with success return 0; } @@ -368,7 +365,9 @@ int Ykush3::i2c_set_address(char *i2c_address) } -int Ykush3::i2c_write(char *i2c_address_ASCII, char *num_bytes_ASCII, char **data_to_write_ASCII) +int Ykush3::i2c_write(char *i2c_address_ASCII, + char *num_bytes_ASCII, + char **data_to_write_ASCII) { hid_report_out[0] = 0x52; hid_report_out[1] = 0x01; @@ -378,20 +377,21 @@ int Ykush3::i2c_write(char *i2c_address_ASCII, char *num_bytes_ASCII, char **dat //convert num_bytes_ASCII to binary int size = strlen(num_bytes_ASCII); - if ( size <= 0 ) + if (size <= 0) return 1; dec2bin(num_bytes_ASCII, &hid_report_out[3], size); - if ( hid_report_out[3] > 60 ) + if (hid_report_out[3] > 60) return 2; //convert data_to_write_ASCII to binary - for ( int i = 0; i < hid_report_out[3]; i++ ) { + for (int i = 0; i < hid_report_out[3]; i++) { + //BUG: isto não está bem!!! hex2bin(num_bytes_ASCII + 2, &hid_report_out[i + 4], 2); } sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); - if ( (hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x52) ) { + if ((hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x52)) { //command executed with success return 0; } @@ -399,7 +399,10 @@ int Ykush3::i2c_write(char *i2c_address_ASCII, char *num_bytes_ASCII, char **dat } -int Ykush3::i2c_read(char *i2c_address_ASCII, char *num_bytes_ASCII, unsigned char *data_buffer, int *bytes_read) +int Ykush3::i2c_read(char *i2c_address_ASCII, + char *num_bytes_ASCII, + unsigned char *data_buffer, + int *bytes_read) { hid_report_out[0] = 0x52; hid_report_out[1] = 0x02; @@ -439,7 +442,7 @@ Ykush3::display_version_bootloader (void) int major, minor, patch; hid_report_out[0] = 0x61; hid_report_out[1] = 0x01; - if (sendHidReport (usb_serial, hid_report_out, hid_report_in, 64) != 0 ) + if (sendHidReport(usb_serial, hid_report_out, hid_report_in, 64) != 0) { std::cout << "Unable to get bootloader version \n"; return 1; @@ -451,7 +454,13 @@ Ykush3::display_version_bootloader (void) return 0; } - std::cout << "Bootloader version-" << (int) hid_report_in[2] << "." << (int) hid_report_in[3] << "." << (int) hid_report_in[4] << std::endl; + std::cout << "Bootloader version-" + << (int) hid_report_in[2] + << "." + << (int) hid_report_in[3] + << "." + << (int) hid_report_in[4] + << std::endl; return 0; } @@ -504,6 +513,22 @@ void Ykush3::print_help(char *app_name) +int ykush3::i2c_write_buffer(struct command_option *cur_opt) +{ + if (cur_opt) { + char *param cur_opt->parameters->next; + char buffer[60][]; + char num_bytes = 0; + while (param) { + + + num_bytes++; + param = param->next; + } + return i2c_write(cur_opt->parameters->value, num_bytes, buffer); + } + return 1; +} diff --git a/src/ykush3/ykush3.h b/src/ykush3/ykush3.h index bf30bc0..f6e531c 100644 --- a/src/ykush3/ykush3.h +++ b/src/ykush3/ykush3.h @@ -94,17 +94,14 @@ class Ykush3 : public UsbDevice int i2c_enable_disable_control(bool enable_flag); //ToDo int i2c_enable_disable_gateway(bool enable_flag); //ToDo int i2c_set_address(char *i2c_address); //ToDo - int i2c_write(char *i2c_address_ASCII, char *num_bytes_ASCII, char **data_to_write_ASCII); //ToDo + int i2c_write(char *i2c_address_ASCII, int num_bytes, char **data_to_write_ASCII); //ToDo int i2c_read(char *i2c_address_ASCII, char *num_bytes_ASCII, unsigned char *data_buffer, int *bytes_read); //ToDo //Versioning int display_version_bootloader (void); int display_version_firmware (void); - int set_usb_serial(char *serial); - - private: - char *usb_serial; + int set_usb_serial(char *sint i2c_write_buffer(struct command_option *cur_opt); }; From ef51c42cd99e06804a843ccc63715d2a9ef5b01f Mon Sep 17 00:00:00 2001 From: paulo Date: Sun, 23 May 2021 23:17:23 +0100 Subject: [PATCH 3/8] Status patched in the new command parser scheme --- .vscode/launch.json | 27 ---------------------- src/help/ykush_help.cpp | 4 ++-- src/ykush3/ykush3.cpp | 51 ++++++++++++++++++++++++++++++----------- src/ykush3/ykush3.h | 3 ++- ykush.sln | 28 ---------------------- 5 files changed, 42 insertions(+), 71 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 ykush.sln diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index f527803..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "(gdb) Launch", - "type": "cppdbg", - "request": "launch", - "program": "enter program name, for example ${workspaceFolder}/a.out", - "args": [], - "stopAtEntry": false, - "cwd": "${workspaceFolder}", - "environment": [], - "externalConsole": true, - "MIMode": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ] - } - ] -} \ No newline at end of file diff --git a/src/help/ykush_help.cpp b/src/help/ykush_help.cpp index ef98258..96a9fec 100644 --- a/src/help/ykush_help.cpp +++ b/src/help/ykush_help.cpp @@ -119,8 +119,8 @@ void Help::print_ykush3(void) 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 slave mode.\n"; - std::cout << "--i2c-write
... Writes to slave I2C device\n"; - std::cout << " with address
.
is in hexadecimal.\n"; + std::cout << "--i2c-write
... Writes n bytes to slave I2C device\n"; + std::cout << " with address
.
is in hexadecimal.\n"; std::cout << "--i2c-read
Reads from slave I2C device with address
.\n"; std::cout << "
is in hexadecimal. Only the 7 most significant bits are considered.\n\n"; diff --git a/src/ykush3/ykush3.cpp b/src/ykush3/ykush3.cpp index 5ae326c..4eb9883 100644 --- a/src/ykush3/ykush3.cpp +++ b/src/ykush3/ykush3.cpp @@ -36,7 +36,7 @@ int ykush3_action_parser(struct command_line *cmdl) } else if (opt_name.compare("-d") == 0) { return ykush3.port_down(cur_opt->parameters->value[0]); } else if (opt_name.compare("-g") == 0) { - return ykush3.get_port_status(cur_opt->parameters->value[0]); + return ykush3.get_port_status(cur_opt->parameters->value[0]); } else if (opt_name.compare("-on") == 0) { return ykush3.port_up('4'); } else if (opt_name.compare("-off") == 0) { @@ -82,7 +82,7 @@ int ykush3_action_parser(struct command_line *cmdl) return ykush3.i2c_set_address( cur_opt->parameters->value); } else if (opt_name.compare("--i2c-write") == 0) { - return i2c_write_buffer(cur_opt); + return ykush3.i2c_write_buffer(cur_opt); } cur_opt = cur_opt->next; @@ -172,7 +172,30 @@ int Ykush3::get_port_status(char port) sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); status = hid_report_in[1]; - return status; + switch (status) { + case 0x01: + std::cout << "Port 1 OFF\n"; + break; + case 0x02: + std::cout << "Port 2 OFF\n"; + break; + case 0x03: + std::cout << "Port 3 OFF\n"; + break; + case 0x11: + std::cout << "Port 1 ON\n"; + break; + case 0x12: + std::cout << "Port 2 ON\n"; + break; + case 0x13: + std::cout << "Port 3 ON\n"; + break; + default: + return -1; + } + + return 0; } @@ -366,7 +389,7 @@ int Ykush3::i2c_set_address(char *i2c_address) int Ykush3::i2c_write(char *i2c_address_ASCII, - char *num_bytes_ASCII, + int num_bytes, char **data_to_write_ASCII) { hid_report_out[0] = 0x52; @@ -376,17 +399,21 @@ int Ykush3::i2c_write(char *i2c_address_ASCII, hex2bin(i2c_address_ASCII + 2, &hid_report_out[2], 2); //convert num_bytes_ASCII to binary + /* int size = strlen(num_bytes_ASCII); if (size <= 0) return 1; - dec2bin(num_bytes_ASCII, &hid_report_out[3], size); - if (hid_report_out[3] > 60) - return 2; + dec2bin(num_bytes_ASCII, &hid_report_out[3], size); + */ + if (num_bytes > 60) + hid_report_out[3] = 60; + else + hid_report_out[3] = num_bytes; //convert data_to_write_ASCII to binary for (int i = 0; i < hid_report_out[3]; i++) { //BUG: isto não está bem!!! - hex2bin(num_bytes_ASCII + 2, &hid_report_out[i + 4], 2); + hex2bin(data_to_write_ASCII[i], &hid_report_out[i + 4], 2); } sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); @@ -513,15 +540,13 @@ void Ykush3::print_help(char *app_name) -int ykush3::i2c_write_buffer(struct command_option *cur_opt) +int Ykush3::i2c_write_buffer(struct command_option *cur_opt) { if (cur_opt) { - char *param cur_opt->parameters->next; - char buffer[60][]; + command_parameter *param = cur_opt->parameters->next; + char *buffer[60]; char num_bytes = 0; while (param) { - - num_bytes++; param = param->next; } diff --git a/src/ykush3/ykush3.h b/src/ykush3/ykush3.h index f6e531c..f7efc80 100644 --- a/src/ykush3/ykush3.h +++ b/src/ykush3/ykush3.h @@ -101,7 +101,8 @@ class Ykush3 : public UsbDevice int display_version_bootloader (void); int display_version_firmware (void); - int set_usb_serial(char *sint i2c_write_buffer(struct command_option *cur_opt); + int set_usb_serial(char *s); + int i2c_write_buffer(struct command_option *cur_opt); }; diff --git a/ykush.sln b/ykush.sln deleted file mode 100644 index 809f3cc..0000000 --- a/ykush.sln +++ /dev/null @@ -1,28 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.23107.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ykushcmd", "ykushcmd\ykushcmd.vcxproj", "{C8815452-C975-41CE-8E08-E12E7E488901}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C8815452-C975-41CE-8E08-E12E7E488901}.Debug|x64.ActiveCfg = Debug|x64 - {C8815452-C975-41CE-8E08-E12E7E488901}.Debug|x64.Build.0 = Debug|x64 - {C8815452-C975-41CE-8E08-E12E7E488901}.Debug|x86.ActiveCfg = Debug|Win32 - {C8815452-C975-41CE-8E08-E12E7E488901}.Debug|x86.Build.0 = Debug|Win32 - {C8815452-C975-41CE-8E08-E12E7E488901}.Release|x64.ActiveCfg = Release|x64 - {C8815452-C975-41CE-8E08-E12E7E488901}.Release|x64.Build.0 = Release|x64 - {C8815452-C975-41CE-8E08-E12E7E488901}.Release|x86.ActiveCfg = Release|Win32 - {C8815452-C975-41CE-8E08-E12E7E488901}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal From 35a7f9e1ccadc4aee92cf392617775e9094590ba Mon Sep 17 00:00:00 2001 From: Paulo Gouveia Date: Mon, 11 Oct 2021 21:45:02 +0100 Subject: [PATCH 4/8] YKUSH3 I2C master write command ready for testing. I2C write command ready for testing on YKUSH3 boards. Next step: Create I2C master read command for YKUSH3 boards. --- src/ykush3/ykush3.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ykush3/ykush3.cpp b/src/ykush3/ykush3.cpp index 4eb9883..cf51e8e 100644 --- a/src/ykush3/ykush3.cpp +++ b/src/ykush3/ykush3.cpp @@ -412,10 +412,12 @@ int Ykush3::i2c_write(char *i2c_address_ASCII, //convert data_to_write_ASCII to binary for (int i = 0; i < hid_report_out[3]; i++) { - //BUG: isto não está bem!!! - hex2bin(data_to_write_ASCII[i], &hid_report_out[i + 4], 2); + hex2bin(data_to_write_ASCII[i]+2, &hid_report_out[i + 4], 2); } + for (int i=0; i<64; i++) + std::cout << std::hex << (int)hid_report_out[i] << " "; + sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); if ((hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x52)) { @@ -544,9 +546,11 @@ int Ykush3::i2c_write_buffer(struct command_option *cur_opt) { if (cur_opt) { command_parameter *param = cur_opt->parameters->next; + param = param->next; char *buffer[60]; char num_bytes = 0; while (param) { + buffer[num_bytes] = param->value; num_bytes++; param = param->next; } From 926b21a109d8fed9d7ba92cc388b14b04cfb6fc1 Mon Sep 17 00:00:00 2001 From: Paulo Gouveia Date: Wed, 13 Oct 2021 21:15:13 +0100 Subject: [PATCH 5/8] YKUSH3 I2C master mode read command. Added a command for reading from a slave I2C device through a YKUSH3 configured in I2C master mode. --- src/ykush3/ykush3.cpp | 18 +++++++++++++++++- src/ykush3/ykush3.h | 5 +++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/ykush3/ykush3.cpp b/src/ykush3/ykush3.cpp index cf51e8e..0429260 100644 --- a/src/ykush3/ykush3.cpp +++ b/src/ykush3/ykush3.cpp @@ -83,6 +83,8 @@ int ykush3_action_parser(struct command_line *cmdl) cur_opt->parameters->value); } else if (opt_name.compare("--i2c-write") == 0) { return ykush3.i2c_write_buffer(cur_opt); + } else if (opt_name.compare("--i2c-read") == 0) { + return ykush3.i2c_read_buffer(cur_opt); } cur_opt = cur_opt->next; @@ -559,5 +561,19 @@ int Ykush3::i2c_write_buffer(struct command_option *cur_opt) return 1; } - +int Ykush3::i2c_read_buffer(struct command_option *cur_opt) +{ + if (cur_opt) { + char *address = cur_opt->parameters->value; + command_parameter *param = cur_opt->parameters->next; + param = param->next; + char *num_bytes = param->value; + unsigned char buffer[60]; + int bytes_read = 0; + i2c_read(address, num_bytes, buffer, &bytes_read); + for (int i=0; i Date: Thu, 14 Oct 2021 14:35:24 +0100 Subject: [PATCH 6/8] Corrected jump byte on the buffer of YKUSH I2C master write. --- src/ykush3/ykush3.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ykush3/ykush3.cpp b/src/ykush3/ykush3.cpp index 0429260..52ab65a 100644 --- a/src/ykush3/ykush3.cpp +++ b/src/ykush3/ykush3.cpp @@ -416,9 +416,10 @@ int Ykush3::i2c_write(char *i2c_address_ASCII, for (int i = 0; i < hid_report_out[3]; i++) { hex2bin(data_to_write_ASCII[i]+2, &hid_report_out[i + 4], 2); } - - for (int i=0; i<64; i++) - std::cout << std::hex << (int)hid_report_out[i] << " "; + + // DEBUG print + //for (int i=0; i<64; i++) + // std::cout << std::hex << (int)hid_report_out[i] << " "; sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); @@ -548,7 +549,6 @@ int Ykush3::i2c_write_buffer(struct command_option *cur_opt) { if (cur_opt) { command_parameter *param = cur_opt->parameters->next; - param = param->next; char *buffer[60]; char num_bytes = 0; while (param) { @@ -556,6 +556,7 @@ int Ykush3::i2c_write_buffer(struct command_option *cur_opt) num_bytes++; param = param->next; } + std::cout << std::endl << (int)num_bytes << std::endl; return i2c_write(cur_opt->parameters->value, num_bytes, buffer); } return 1; From bc40090d7ffcd82d18bc903946333437d28955a4 Mon Sep 17 00:00:00 2001 From: Paulo Gouveia Date: Fri, 15 Oct 2021 23:03:21 +0100 Subject: [PATCH 7/8] YKUSH3 I2C master and slave mode tested Tested with YKUSH3 firmware version 1.4.0 --- src/ykush3/ykush3.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ykush3/ykush3.cpp b/src/ykush3/ykush3.cpp index 52ab65a..2b9c579 100644 --- a/src/ykush3/ykush3.cpp +++ b/src/ykush3/ykush3.cpp @@ -449,7 +449,10 @@ int Ykush3::i2c_read(char *i2c_address_ASCII, dec2bin(num_bytes_ASCII, &hid_report_out[3], size); sendHidReport(usb_serial, hid_report_out, hid_report_in, 64); - + // DEBUG PRINT + int j; + //for (j = 0; j < 64; j++) + // std::cout << std::hex << (int)hid_report_in[j] << " "; //handle response message if ((hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x52)) { //get num_bytes @@ -457,7 +460,7 @@ int Ykush3::i2c_read(char *i2c_address_ASCII, return 1; int i; for (i = 0; (i < hid_report_in[2]) && (i < 60); i++) { - data_buffer[i] = hid_report_in[i + 4]; + data_buffer[i] = hid_report_in[i + 3]; } *bytes_read = i; } else { @@ -556,7 +559,6 @@ int Ykush3::i2c_write_buffer(struct command_option *cur_opt) num_bytes++; param = param->next; } - std::cout << std::endl << (int)num_bytes << std::endl; return i2c_write(cur_opt->parameters->value, num_bytes, buffer); } return 1; @@ -567,13 +569,12 @@ int Ykush3::i2c_read_buffer(struct command_option *cur_opt) if (cur_opt) { char *address = cur_opt->parameters->value; command_parameter *param = cur_opt->parameters->next; - param = param->next; char *num_bytes = param->value; unsigned char buffer[60]; int bytes_read = 0; i2c_read(address, num_bytes, buffer, &bytes_read); for (int i=0; i Date: Mon, 18 Oct 2021 11:55:24 +0100 Subject: [PATCH 8/8] Correct title naming YKUSH3 when listing attached boards. Note that when a board is attached to the host but the ykushcmd doesn't have the admin permissions to use the libsub the list command will give a segmentation fault error. To avoid this always use the ykushcmd with the correct permission on Linux. --- src/help/ykush_help.cpp | 2 +- src/yk_usb_device.cpp | 3 +-- src/ykush3/ykush3.cpp | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/help/ykush_help.cpp b/src/help/ykush_help.cpp index 96a9fec..75f13df 100644 --- a/src/help/ykush_help.cpp +++ b/src/help/ykush_help.cpp @@ -118,7 +118,7 @@ void Help::print_ykush3(void) 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 slave mode.\n"; + std::cout << "--i2c-master enable|disable Enables or disables I2C master mode.\n"; std::cout << "--i2c-write
... Writes n bytes to slave I2C device\n"; std::cout << " with address
.
is in hexadecimal.\n"; std::cout << "--i2c-read
Reads from slave I2C device with address
.\n"; diff --git a/src/yk_usb_device.cpp b/src/yk_usb_device.cpp index ef24347..a3f5595 100644 --- a/src/yk_usb_device.cpp +++ b/src/yk_usb_device.cpp @@ -42,12 +42,11 @@ int UsbDevice::listConnected() cur_dev = devs; while (cur_dev) { std::cout << i << ". Board found with serial number: " << cur_dev->serial_number_ascii << "\n"; - cur_dev = cur_dev->next; + cur_dev = cur_dev->next; i++; } usbhid->free_enumeration(devs); - return i; } #else diff --git a/src/ykush3/ykush3.cpp b/src/ykush3/ykush3.cpp index 2b9c579..5fe727c 100644 --- a/src/ykush3/ykush3.cpp +++ b/src/ykush3/ykush3.cpp @@ -529,9 +529,9 @@ int Ykush3::set_usb_serial(char *serial) int ykush3_list_attached() { Ykush3 ykush3; - printf("\n\nAttached YKUSH Boards:\n"); + printf("\n\nAttached YKUSH3 Boards:\n"); if( ykush3.listConnected() == 0 ) - printf("\n\nNo YKUSH boards found."); + printf("\n\nNo YKUSH3 boards found."); printf("\n\n"); return 0; }