123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857 |
- /*******************************************************************************
- 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.
- *******************************************************************************/
- #include <ykush3.h>
- #include <stdio.h>
- #include <ykush_help.h>
- #include <command_parser.h>
- #include <iostream>
- #include <string>
- #include <string2val.h>
- #include <cstring>
- 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 < 5)
- {
- 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;
- }
- int Ykush3::port_up(char *serial, char 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);
- }
- int Ykush3::port_down(char *serial, char port)
- {
- switch(port)
- {
- case '1':
- hid_report_out[0] = 0x01;
- break;
- case '2':
- hid_report_out[0] = 0x02;
- break;
-
- case '3':
- hid_report_out[0] = 0x03;
- break;
- case 'a':
- hid_report_out[0] = 0x0a;
- break;
-
- case '4':
- hid_report_out[0] = 0x04;
- break;
- default:
- return 0;
- break;
- }
-
- //send HID report to board
- return sendHidReport(serial, hid_report_out, hid_report_in, 64);
-
- }
- int Ykush3::get_port_status(char *serial, char port)
- {
- int status;
- switch(port)
- {
- case '1':
- hid_report_out[0] = 0x21;
- break;
- case '2':
- hid_report_out[0] = 0x22;
- break;
-
- case '3':
- hid_report_out[0] = 0x23;
- break;
- case '4':
- hid_report_out[0] = 0x24;
- break;
- default:
- return 0;
- 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;
- }
- int Ykush3::write_io(char *serial, char port, char value)
- {
- hid_report_out[0] = 0x31;
- if(value=='0'){
- hid_report_out[2] = 0;
- } else {
- hid_report_out[2] = 1;
- }
- switch(port)
- {
- case '1':
- hid_report_out[1] = 0x01;
- break;
- case '2':
- hid_report_out[1] = 0x02;
- break;
-
- case '3':
- 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);
- }
- int Ykush3::read_io(char *serial, char port)
- {
-
- hid_report_out[0] = 0x30;
- switch(port)
- {
- case '1':
- hid_report_out[1] = 0x01;
- break;
- case '2':
- hid_report_out[1] = 0x02;
- break;
-
- case '3':
- hid_report_out[1] = 0x03;
- break;
-
- default:
- return 0;
- break;
- }
-
-
- //send HID report to board
- sendHidReport(serial, hid_report_out, hid_report_in, 64);
-
- //process response
- return hid_report_in[3];
- }
-
- int Ykush3::config_port(char *serial, char port, char value)
- {
- hid_report_out[0] = 0x41;
- if ( value == '0' )
- hid_report_out[2] = 0;
- else if ( value == '1' )
- hid_report_out[2] = 1;
- else
- hid_report_out[2] = 2; //Persist mode
-
- switch ( port ) {
- case '1':
- hid_report_out[1] = 0x01;
- break;
- case '2':
- hid_report_out[1] = 0x02;
- break;
- case '3':
- hid_report_out[1] = 0x03;
- break;
- case 'e':
- hid_report_out[1] = 0x04;
- break;
- default:
- return 0;
- break;
- }
- //send HID report to board
- return sendHidReport(serial, hid_report_out, hid_report_in, 64);
- }
- int Ykush3::reset(char *serial)
- {
- hid_report_out[0] = 0x55;
-
- //send HID report to board
- sendHidReport(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;
-
- //send HID report to board
- sendHidReport (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;
-
- //send HID report to board
- sendHidReport (serial, hid_report_out, hid_report_in, 64);
- return 0;
- }
- int
- Ykush3::enter_bootloader (char *serial)
- {
- hid_report_out[0] = 0x42;
-
- //send HID report to board
- sendHidReport (serial, hid_report_out, hid_report_in, 64);
- return 0;
- }
- int Ykush3::i2c_enable_disable_control(bool enable_flag)
- {
- hid_report_out[0] = 0x51;
- hid_report_out[1] = 0x01;
-
- if ( enable_flag )
- hid_report_out[2] = 0x01;
- else
- hid_report_out[2] = 0x00;
- sendHidReport(usb_serial, hid_report_out, hid_report_in, 64);
- if ( (hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x51) ) {
- //command executed with success
- return 0;
- }
- return 1;
- }
- int Ykush3::i2c_enable_disable_gateway(bool enable_flag)
- {
- hid_report_out[0] = 0x51;
- hid_report_out[1] = 0x02;
- if ( enable_flag )
- hid_report_out[2] = 0x01;
- else
- hid_report_out[2] = 0x00;
- sendHidReport(usb_serial, hid_report_out, hid_report_in, 64);
- if ( (hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x51) ) {
- //command executed with success
- return 0;
- }
- return 1;
- }
- int Ykush3::i2c_set_address(char *i2c_address)
- {
- std::cout << "i2c_set_address com address: " << i2c_address << std::endl;
- hid_report_out[0] = 0x51;
- hid_report_out[1] = 0x03;
- 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) ) {
- //command executed with success
- return 0;
- }
- return 1;
- }
- 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;
- //convert i2c_address_ASCII to binary
- 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;
- //convert data_to_write_ASCII to binary
- for ( int i = 0; i < hid_report_out[3]; i++ ) {
- 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) ) {
- //command executed with success
- return 0;
- }
- return 0;
- }
- 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;
- //convert i2c_address_ASCII to binary
- 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);
- sendHidReport(usb_serial, hid_report_out, hid_report_in, 64);
- //handle response message
- if ((hid_report_in[0] == 0x01) && (hid_report_in[1] == 0x52)) {
- //get num_bytes
- if (hid_report_in[2] < 0)
- return 1;
- int i;
- for (i = 0; (i < hid_report_in[2]) && (i < 60); i++) {
- data_buffer[i] = hid_report_in[i + 4];
- }
- *bytes_read = i;
- } else {
- return 1;
- }
- return 0;
- }
- int
- 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 )
- {
- std::cout << "Unable to get bootloader version \n";
- return 1;
- }
- //print response
- if ((hid_report_in[0] != 0x01) && (hid_report_in[0] != 0x61))
- {
- std::cout << "Bootloader version-0.10.0\n";
- return 0;
- }
- std::cout << "Bootloader version-" << (int) hid_report_in[2] << "." << (int) hid_report_in[3] << "." << (int) hid_report_in[4] << std::endl;
- return 0;
- }
-
- int
- Ykush3::display_version_firmware (void)
- {
- hid_report_out[0] = 0x61;
- hid_report_out[1] = 0x02;
- sendHidReport(usb_serial, hid_report_out, hid_report_in, 64);
- //print response
- if (( hid_report_in[0] != 0x01) && (hid_report_in[0] != 0x61))
- {
- std::cout << "Firmware version-1.0.0\n";
- return 0;
- }
- std::cout << "Firmware version-" << (int) hid_report_in[2] << "." << (int) hid_report_in[3] << "." << (int) hid_report_in[4] << std::endl;
- return 0;
- }
- int Ykush3::set_usb_serial(char *serial)
- {
- usb_serial = serial;
- return 0;
- }
-
- int ykush3_list_attached()
- {
- Ykush3 ykush3;
- printf("\n\nAttached YKUSH 3 Boards:\n");
- if( ykush3.listConnected() == 0 )
- printf("\n\nNo YKUSH 3 boards found.");
- printf("\n\n");
- return 0;
- }
- void Ykush3::print_help(void)
- {
- Help help(app_exc_name);
- help.print_version();
- help.print_usage();
- help.print_ykush3();
- }
|