diff --git a/endpoint/endpointComp/endpoint.c b/endpoint/endpointComp/endpoint.c index 9fc551f8..e5529fe3 100644 --- a/endpoint/endpointComp/endpoint.c +++ b/endpoint/endpointComp/endpoint.c @@ -25,8 +25,9 @@ "999999B" #define TEST_DEVICE_ID "470010171566423" -const uint8_t test_key[32] = {82, 142, 184, 64, 74, 105, 126, 65, 154, 116, 14, 193, 208, 41, 8, 115, - 158, 252, 228, 160, 79, 5, 167, 185, 13, 159, 135, 113, 49, 209, 58, 68}; +const uint8_t test_private_key[AES_CBC_KEY_SIZE] = {82, 142, 184, 64, 74, 105, 126, 65, 154, 116, 14, + 193, 208, 41, 8, 115, 158, 252, 228, 160, 79, 5, + 167, 185, 13, 159, 135, 113, 49, 209, 58, 68}; const uint8_t test_iv[AES_IV_SIZE] = {164, 3, 98, 193, 52, 162, 107, 252, 184, 42, 74, 225, 157, 26, 88, 72}; static void print_help(void) { @@ -37,31 +38,98 @@ static void print_help(void) { "SYNOPSIS\n" " endpoint [-h]\n" " endpoint [--help]\n" + " endpoint [--val=number]\n" + " [--msg=\"message\"]\n" + " [--msg-fmt=\"message-format\"]\n" + " [--addr=\"address\"]\n" + " [--next-addr=\"next-address\"]\n" + " [--iv=\"initialize-vector\"]\n" "\n" "OPTIONS\n" " -h\n" " --help\n" " Print the information for helping the users. Ignore other arguments.\n" + "\n" + " --val=number\n" + " Assign the integer value for send_transaction_information.\n" + "\n" + " --msg=\"message\"\n" + " Assign the message value with string for send_transaction_information.\n" + "\n" + " --msg-fmt=\"message-format\"\n" + " Assign the message format of --msg with string.\n" + "\n" + " --addr=\"address\"\n" + " Assign the address with string for send_transaction_information.\n" + " The \"address\" should be composed with [9A-Z] and the length should be 81.\n" + "\n" + " --next-addr=\"next-address\"\n" + " Assign the next address with string for send_transaction_information.\n" + " The \"next-address\" should be composed with [9A-Z] and the length should be 81.\n" + "\n" + " --iv=\"initialize-vector\"\n" + " Assign initialize vector with string for send_transaction_information.\n" + " The \"initialize-vector\" format is \"number,number,...,number\".\n" "\n"); exit(EXIT_SUCCESS); } +static void string_to_uint8_arr(const char* string, uint8_t arr[]) { + if (!string) return; + + size_t str_start = 0, str_bytes = 0, arr_idx = 0; + // The maximum value of uint8_t is 255, hence 4 is enough for the string length including the null-terminated + // character. + char parse_str[4]; + int parse_int; + + // Seperate the string with the delimiter "," and convert the substring to uint8_t. + while (str_start < le_utf8_NumChars(string)) { + le_utf8_CopyUpToSubStr(parse_str, &string[str_start], ",", sizeof(parse_str), &str_bytes); + le_utf8_ParseInt(&parse_int, parse_str); + arr[arr_idx] = (uint8_t)parse_int; + + str_start += (str_bytes + 1); + arr_idx++; + } + return; +} + COMPONENT_INIT { // FIXME: // The current code is a prototype for passing the CI. // The initialization of hardware and the input from hardware are not implemented yet. + int value = TEST_VALUE; + const char* message = TEST_MESSAGE; + const char* message_fmt = TEST_MESSAGE_FMT; + const char* tag = TEST_TAG; + const char* address = TEST_ADDRESS; + const char* next_address = TEST_NEXT_ADDRESS; + uint8_t private_key[AES_CBC_KEY_SIZE] = {0}; + const char* device_id = TEST_DEVICE_ID; + const char* iv_str = NULL; + uint8_t iv[AES_IV_SIZE] = {0}; + + le_arg_SetIntVar(&value, NULL, "val"); + le_arg_SetStringVar(&message, NULL, "msg"); + le_arg_SetStringVar(&message_fmt, NULL, "msg-fmt"); + le_arg_SetStringVar(&tag, NULL, "tag"); + le_arg_SetStringVar(&address, NULL, "addr"); + le_arg_SetStringVar(&next_address, NULL, "next-addr"); + le_arg_SetStringVar(&iv_str, NULL, "iv"); le_arg_SetFlagCallback(print_help, "h", "help"); le_arg_Scan(); - uint8_t iv[AES_IV_SIZE] = {0}; - - memcpy(iv, test_iv, AES_IV_SIZE); + memcpy(private_key, test_private_key, AES_CBC_KEY_SIZE); + if (iv_str) + string_to_uint8_arr(iv_str, iv); + else + memcpy(iv, test_iv, AES_IV_SIZE); srand(time(NULL)); while (true) { - send_transaction_information(TEST_VALUE, TEST_MESSAGE, TEST_MESSAGE_FMT, TEST_TAG, TEST_ADDRESS, TEST_NEXT_ADDRESS, - test_key, TEST_DEVICE_ID, iv); + send_transaction_information(value, message, message_fmt, tag, address, next_address, private_key, device_id, iv); sleep(10); } } diff --git a/endpoint/endpointComp/endpoint.h b/endpoint/endpointComp/endpoint.h index 8a5d8567..c8e3b58e 100644 --- a/endpoint/endpointComp/endpoint.h +++ b/endpoint/endpointComp/endpoint.h @@ -9,9 +9,19 @@ #ifndef ENDPOINT_H #define ENDPOINT_H +#include + /** * @brief Print the help message of endpoint application */ static void print_help(void); +/** + * @brief Convert the string to uint8_t array + * + * @param[in] string The string with the combination of number and comma + * @param[in,out] arr The array for storing the converted number + */ +static void string_to_uint8_arr(const char* string, uint8_t arr[]); + #endif // ENDPOINT_H