Skip to content

Commit 7ebc5ca

Browse files
Merge pull request #647 from Cypherock/develop
Develop
2 parents 877d29e + 22e59e7 commit 7ebc5ca

29 files changed

+3410
-4
lines changed

apps/icp_app/icp_api.c

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

apps/icp_app/icp_api.h

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @file icp_api.h
3+
* @author Cypherock X1 Team
4+
* @brief Header file to export some helper functions for the ICP app
5+
* @copyright Copyright (c) 2024 HODL TECH PTE LTD
6+
* <br/> You may obtain a copy of license at <a href="https://mitcc.org/"
7+
* target=_blank>https://mitcc.org/</a>
8+
*/
9+
#ifndef ICP_API_H
10+
#define ICP_API_H
11+
12+
/*****************************************************************************
13+
* INCLUDES
14+
*****************************************************************************/
15+
16+
#include <icp/core.pb.h>
17+
#include <stdint.h>
18+
19+
/*****************************************************************************
20+
* MACROS AND DEFINES
21+
*****************************************************************************/
22+
23+
/*****************************************************************************
24+
* TYPEDEFS
25+
*****************************************************************************/
26+
27+
/*****************************************************************************
28+
* EXPORTED VARIABLES
29+
*****************************************************************************/
30+
31+
/*****************************************************************************
32+
* GLOBAL FUNCTION PROTOTYPES
33+
*****************************************************************************/
34+
35+
/**
36+
* @brief API to decode query from host with `ICP_QUERY_FIELDS`
37+
*
38+
* @param[in] data: PB encoded bytestream received from host
39+
* @param[in] data_size: size of pb encoded bytestream
40+
* @param[out] query_out: @ref icp_query_t obj to copy the decoded result to
41+
* @return bool True if decoding was successful, else false
42+
*/
43+
bool decode_icp_query(const uint8_t *data,
44+
uint16_t data_size,
45+
icp_query_t *query_out);
46+
47+
/**
48+
* @brief Encodes the ICP result with `ICP_RESULT_FIELDS` to byte-stream
49+
*
50+
* @param[in] result: object of populated @ref icp_result_t to be encoded
51+
* @param[out] buffer: buffer to fill byte-stream into
52+
* @param[in] max_buffer_len: Max length allowed for writing bytestream to
53+
* buffer
54+
* @param[out] bytes_written_out: bytes written to bytestream
55+
* @return bool True if decoding was successful, else false
56+
*/
57+
bool encode_icp_result(const icp_result_t *result,
58+
uint8_t *buffer,
59+
uint16_t max_buffer_len,
60+
size_t *bytes_written_out);
61+
62+
/**
63+
* @brief This API checks if the `which_request` field of the query of type
64+
* `icp_query_t` matches against the expected tag.
65+
*
66+
* @param query The query of type `icp_query_t` to be checked
67+
* @param exp_query_tag The expected tag of the query
68+
* @return true If the query tag matches the expected tag
69+
* @return false If the query tag does not match the expected tag
70+
*/
71+
bool check_icp_query(const icp_query_t *query, pb_size_t exp_query_tag);
72+
73+
/**
74+
* @brief Returns zero initialized object of type
75+
* icp_result_t result_tag set in result.which_response field
76+
*
77+
* @param result_tag Result tag to be set in the icp_result_t result
78+
* @return icp_result_t Result object of type icp_result_t
79+
*/
80+
icp_result_t init_icp_result(pb_size_t result_tag);
81+
82+
/**
83+
* @brief Send the error to the host.
84+
*
85+
* @param which_error The error type to be sent
86+
* @param error_code The error code to sent to the host
87+
*/
88+
void icp_send_error(pb_size_t which_error, uint32_t error_code);
89+
90+
/**
91+
* @brief This API encodes icp_result_t in protobuf structure.
92+
* @details If the encoding is successful, then it sends the corresponding
93+
* result to the host.
94+
*
95+
* The function ASSERTs the result of encode_icp_result internally.
96+
*
97+
* @param result The result which needs to be sent to the host.
98+
*/
99+
void icp_send_result(const icp_result_t *result);
100+
101+
/**
102+
* @brief This API receives request of type icp_query_t of type
103+
* exp_query_tag from the host.
104+
*
105+
* @param query The reference to which the query needs to be populated
106+
* @param exp_query_tag The expected tag of the query
107+
* @return true If the query was recieved from the host matching the tag
108+
* @return false If the request timed out or the recieved request did not match
109+
* the tag
110+
*/
111+
bool icp_get_query(icp_query_t *query, pb_size_t exp_query_tag);
112+
113+
#endif

0 commit comments

Comments
 (0)