|
| 1 | + |
| 2 | +/** |
| 3 | + * @file tron_api.c |
| 4 | + * @author Cypherock X1 Team |
| 5 | + * @brief Defines helpers apis for tron app. |
| 6 | + * @copyright Copyright (c) 2023 HODL TECH PTE LTD |
| 7 | + * <br/> You may obtain a copy of license at <a href="https://mitcc.org/" |
| 8 | + *target=_blank>https://mitcc.org/</a> |
| 9 | + * |
| 10 | + ****************************************************************************** |
| 11 | + * @attention |
| 12 | + * |
| 13 | + * (c) Copyright 2023 by HODL TECH PTE LTD |
| 14 | + * |
| 15 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 16 | + * a copy of this software and associated documentation files (the |
| 17 | + * "Software"), to deal in the Software without restriction, including |
| 18 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 19 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 20 | + * permit persons to whom the Software is furnished to do so, subject |
| 21 | + * to the following conditions: |
| 22 | + * |
| 23 | + * The above copyright notice and this permission notice shall be |
| 24 | + * included in all copies or substantial portions of the Software. |
| 25 | + * |
| 26 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 27 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 28 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 29 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 30 | + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 31 | + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 32 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 33 | + * |
| 34 | + * |
| 35 | + * "Commons Clause" License Condition v1.0 |
| 36 | + * |
| 37 | + * The Software is provided to you by the Licensor under the License, |
| 38 | + * as defined below, subject to the following condition. |
| 39 | + * |
| 40 | + * Without limiting other conditions in the License, the grant of |
| 41 | + * rights under the License will not include, and the License does not |
| 42 | + * grant to you, the right to Sell the Software. |
| 43 | + * |
| 44 | + * For purposes of the foregoing, "Sell" means practicing any or all |
| 45 | + * of the rights granted to you under the License to provide to third |
| 46 | + * parties, for a fee or other consideration (including without |
| 47 | + * limitation fees for hosting or consulting/ support services related |
| 48 | + * to the Software), a product or service whose value derives, entirely |
| 49 | + * or substantially, from the functionality of the Software. Any license |
| 50 | + * notice or attribution required by the License must also include |
| 51 | + * this Commons Clause License Condition notice. |
| 52 | + * |
| 53 | + * Software: All X1Wallet associated files. |
| 54 | + * License: MIT |
| 55 | + * Licensor: HODL TECH PTE LTD |
| 56 | + * |
| 57 | + ****************************************************************************** |
| 58 | + */ |
| 59 | + |
| 60 | +/***************************************************************************** |
| 61 | + * INCLUDES |
| 62 | + *****************************************************************************/ |
| 63 | + |
| 64 | +#include "tron_api.h" |
| 65 | + |
| 66 | +#include <pb_decode.h> |
| 67 | +#include <pb_encode.h> |
| 68 | + |
| 69 | +#include "common_error.h" |
| 70 | +#include "core_api.h" |
| 71 | +#include "events.h" |
| 72 | + |
| 73 | +/***************************************************************************** |
| 74 | + * EXTERN VARIABLES |
| 75 | + *****************************************************************************/ |
| 76 | + |
| 77 | +/***************************************************************************** |
| 78 | + * PRIVATE MACROS AND DEFINES |
| 79 | + *****************************************************************************/ |
| 80 | + |
| 81 | +/***************************************************************************** |
| 82 | + * PRIVATE TYPEDEFS |
| 83 | + *****************************************************************************/ |
| 84 | + |
| 85 | +/***************************************************************************** |
| 86 | + * STATIC VARIABLES |
| 87 | + *****************************************************************************/ |
| 88 | + |
| 89 | +/***************************************************************************** |
| 90 | + * GLOBAL VARIABLES |
| 91 | + *****************************************************************************/ |
| 92 | + |
| 93 | +/***************************************************************************** |
| 94 | + * STATIC FUNCTION PROTOTYPES |
| 95 | + *****************************************************************************/ |
| 96 | + |
| 97 | +/***************************************************************************** |
| 98 | + * STATIC FUNCTIONS |
| 99 | + *****************************************************************************/ |
| 100 | + |
| 101 | +/***************************************************************************** |
| 102 | + * GLOBAL FUNCTIONS |
| 103 | + *****************************************************************************/ |
| 104 | +bool decode_tron_query(const uint8_t *data, |
| 105 | + uint16_t data_size, |
| 106 | + tron_query_t *query_out) { |
| 107 | + if (NULL == data || NULL == query_out || 0 == data_size) { |
| 108 | + tron_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG, |
| 109 | + ERROR_DATA_FLOW_DECODING_FAILED); |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + // zeroise for safety from garbage in the query reference |
| 114 | + memzero(query_out, sizeof(tron_query_t)); |
| 115 | + |
| 116 | + /* Create a stream that reads from the buffer. */ |
| 117 | + pb_istream_t stream = pb_istream_from_buffer(data, data_size); |
| 118 | + |
| 119 | + /* Now we are ready to decode the message. */ |
| 120 | + bool status = pb_decode(&stream, TRON_QUERY_FIELDS, query_out); |
| 121 | + |
| 122 | + /* Send error to host if status is false*/ |
| 123 | + if (false == status) { |
| 124 | + tron_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG, |
| 125 | + ERROR_DATA_FLOW_DECODING_FAILED); |
| 126 | + } |
| 127 | + |
| 128 | + return status; |
| 129 | +} |
| 130 | + |
| 131 | +bool encode_tron_result(const tron_result_t *result, |
| 132 | + uint8_t *buffer, |
| 133 | + uint16_t max_buffer_len, |
| 134 | + size_t *bytes_written_out) { |
| 135 | + if (NULL == result || NULL == buffer || NULL == bytes_written_out) |
| 136 | + return false; |
| 137 | + |
| 138 | + /* Create a stream that will write to our buffer. */ |
| 139 | + pb_ostream_t stream = pb_ostream_from_buffer(buffer, max_buffer_len); |
| 140 | + |
| 141 | + /* Now we are ready to encode the message! */ |
| 142 | + bool status = pb_encode(&stream, TRON_RESULT_FIELDS, result); |
| 143 | + |
| 144 | + if (true == status) { |
| 145 | + *bytes_written_out = stream.bytes_written; |
| 146 | + } |
| 147 | + |
| 148 | + return status; |
| 149 | +} |
| 150 | + |
| 151 | +bool check_tron_query(const tron_query_t *query, pb_size_t exp_query_tag) { |
| 152 | + if ((NULL == query) || (exp_query_tag != query->which_request)) { |
| 153 | + tron_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG, |
| 154 | + ERROR_DATA_FLOW_INVALID_QUERY); |
| 155 | + return false; |
| 156 | + } |
| 157 | + return true; |
| 158 | +} |
| 159 | + |
| 160 | +tron_result_t init_tron_result(pb_size_t result_tag) { |
| 161 | + tron_result_t result = TRON_RESULT_INIT_ZERO; |
| 162 | + result.which_response = result_tag; |
| 163 | + return result; |
| 164 | +} |
| 165 | + |
| 166 | +void tron_send_error(pb_size_t which_error, uint32_t error_code) { |
| 167 | + tron_result_t result = init_tron_result(TRON_RESULT_COMMON_ERROR_TAG); |
| 168 | + result.common_error = init_common_error(which_error, error_code); |
| 169 | + tron_send_result(&result); |
| 170 | +} |
| 171 | + |
| 172 | +void tron_send_result(const tron_result_t *result) { |
| 173 | + // TODO: Set the options file for all |
| 174 | + uint8_t buffer[1700] = {0}; |
| 175 | + size_t bytes_encoded = 0; |
| 176 | + ASSERT(encode_tron_result(result, buffer, sizeof(buffer), &bytes_encoded)); |
| 177 | + send_response_to_host(&buffer[0], bytes_encoded); |
| 178 | +} |
| 179 | + |
| 180 | +bool tron_get_query(tron_query_t *query, pb_size_t exp_query_tag) { |
| 181 | + evt_status_t event = get_events(EVENT_CONFIG_USB, MAX_INACTIVITY_TIMEOUT); |
| 182 | + |
| 183 | + if (true == event.p0_event.flag) { |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + if (!decode_tron_query( |
| 188 | + event.usb_event.p_msg, event.usb_event.msg_size, query)) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + if (!check_tron_query(query, exp_query_tag)) { |
| 193 | + return false; |
| 194 | + } |
| 195 | + |
| 196 | + return true; |
| 197 | +} |
0 commit comments