1
+ /**
2
+ * @file hedera_api.c
3
+ * @author Cypherock X1 Team
4
+ * @brief Defines helper APIs for the Hedera app.
5
+ * @copyright Copyright (c) 2025 HODL TECH PTE LTD
6
+ */
7
+ #include "hedera_api.h"
8
+ #include <pb_decode.h>
9
+ #include <pb_encode.h>
10
+ #include "common_error.h"
11
+ #include "core_api.h"
12
+ #include "events.h"
13
+
14
+ bool decode_hedera_query (const uint8_t * data , uint16_t data_size , hedera_query_t * query_out ) {
15
+ if (NULL == data || NULL == query_out || 0 == data_size ) {
16
+ hedera_send_error (ERROR_COMMON_ERROR_CORRUPT_DATA_TAG , ERROR_DATA_FLOW_DECODING_FAILED );
17
+ return false;
18
+ }
19
+ memzero (query_out , sizeof (hedera_query_t ));
20
+ pb_istream_t stream = pb_istream_from_buffer (data , data_size );
21
+ bool status = pb_decode (& stream , HEDERA_QUERY_FIELDS , query_out );
22
+ if (!status ) {
23
+ hedera_send_error (ERROR_COMMON_ERROR_CORRUPT_DATA_TAG , ERROR_DATA_FLOW_DECODING_FAILED );
24
+ }
25
+ return status ;
26
+ }
27
+
28
+ bool encode_hedera_result (const hedera_result_t * result , uint8_t * buffer , uint16_t max_buffer_len , size_t * bytes_written_out ) {
29
+ if (NULL == result || NULL == buffer || NULL == bytes_written_out ) return false;
30
+ pb_ostream_t stream = pb_ostream_from_buffer (buffer , max_buffer_len );
31
+ bool status = pb_encode (& stream , HEDERA_RESULT_FIELDS , result );
32
+ if (status ) {
33
+ * bytes_written_out = stream .bytes_written ;
34
+ }
35
+ return status ;
36
+ }
37
+
38
+ bool check_hedera_query (const hedera_query_t * query , pb_size_t exp_query_tag ) {
39
+ if ((NULL == query ) || (exp_query_tag != query -> which_request )) {
40
+ hedera_send_error (ERROR_COMMON_ERROR_CORRUPT_DATA_TAG , ERROR_DATA_FLOW_INVALID_QUERY );
41
+ return false;
42
+ }
43
+ return true;
44
+ }
45
+
46
+ hedera_result_t init_hedera_result (pb_size_t result_tag ) {
47
+ hedera_result_t result = HEDERA_RESULT_INIT_ZERO ;
48
+ result .which_response = result_tag ;
49
+ return result ;
50
+ }
51
+
52
+ void hedera_send_error (pb_size_t which_error , uint32_t error_code ) {
53
+ hedera_result_t result = init_hedera_result (HEDERA_RESULT_COMMON_ERROR_TAG );
54
+ result .common_error = init_common_error (which_error , error_code );
55
+ hedera_send_result (& result );
56
+ }
57
+
58
+ void hedera_send_result (const hedera_result_t * result ) {
59
+ uint8_t buffer [1700 ] = {0 };
60
+ size_t bytes_encoded = 0 ;
61
+ ASSERT (encode_hedera_result (result , buffer , sizeof (buffer ), & bytes_encoded ));
62
+ send_response_to_host (buffer , bytes_encoded );
63
+ }
64
+
65
+ bool hedera_get_query (hedera_query_t * query , pb_size_t exp_query_tag ) {
66
+ evt_status_t event = get_events (EVENT_CONFIG_USB , MAX_INACTIVITY_TIMEOUT );
67
+ if (event .p0_event .flag ) {
68
+ return false;
69
+ }
70
+ if (!decode_hedera_query (event .usb_event .p_msg , event .usb_event .msg_size , query )) {
71
+ return false;
72
+ }
73
+ if (!check_hedera_query (query , exp_query_tag )) {
74
+ return false;
75
+ }
76
+ return true;
77
+ }
0 commit comments