Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit b143777

Browse files
committed
feat(mam): Implement send_mam_res object
1 parent 3b8aa41 commit b143777

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

accelerator/errors.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extern "C" {
4040
#define SC_MODULE_SERIALIZER (0x03 << SC_MODULE_SHIFT)
4141
#define SC_MODULE_CACHE (0x04 << SC_MODULE_SHIFT)
4242
#define SC_MODULE_MAM (0x05 << SC_MODULE_SHIFT)
43+
#define SC_MODULE_RES (0x06 << SC_MODULE_SHIFT)
4344
/** @} */
4445

4546
/** @name serverity code */
@@ -105,6 +106,12 @@ typedef enum {
105106
/**< No payload or no chid */
106107
SC_MAM_FAILED_WRITE = 0x08 | SC_MODULE_MAM | SC_SEVERITY_FATAL,
107108
/**< Failed to write */
109+
110+
// response module
111+
SC_RES_OOM = 0x01 | SC_MODULE_RES | SC_SEVERITY_FATAL,
112+
/**< Fail to create response object */
113+
SC_RES_NULL = 0x02 | SC_MODULE_RES | SC_SEVERITY_FATAL,
114+
/**< NULL object in response */
108115
} status_t;
109116

110117
typedef enum {

response/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ cc_library(
1010
visibility = ["//visibility:public"],
1111
deps = [
1212
"@entangled//cclient/types",
13+
"@entangled//common/model:transaction",
14+
"//accelerator:ta_errors",
1315
],
1416
)

response/response.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "response/ta_generate_address.h"
77
#include "response/ta_get_tips.h"
88
#include "response/ta_get_transaction_object.h"
9+
#include "response/ta_send_mam_res.h"
910
#include "response/ta_send_transfer.h"
1011

1112
/**

response/ta_send_mam_res.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "ta_send_mam_res.h"
2+
3+
send_mam_res_t* send_mam_res_new() {
4+
send_mam_res_t* res = (send_mam_res_t*)malloc(sizeof(send_mam_res_t));
5+
if (res) {
6+
res->bundle_hash = NULL;
7+
res->channel_id = NULL;
8+
}
9+
10+
return res;
11+
}
12+
13+
status_t send_mam_res_set_bundle_hash(send_mam_res_t* res,
14+
const tryte_t* bundle_hash) {
15+
if (res->bundle_hash || !bundle_hash) {
16+
return SC_RES_NULL;
17+
}
18+
19+
size_t bundle_hash_size = NUM_TRYTES_ADDRESS * sizeof(char);
20+
res->bundle_hash = (char*)malloc(bundle_hash_size);
21+
if (!res->bundle_hash) {
22+
return SC_RES_OOM;
23+
}
24+
25+
memcpy(res->bundle_hash, bundle_hash, bundle_hash_size);
26+
return SC_OK;
27+
}
28+
29+
status_t send_mam_res_set_channel_id(send_mam_res_t* res,
30+
const tryte_t* channel_id) {
31+
if (res->channel_id || !channel_id) {
32+
return SC_RES_NULL;
33+
}
34+
35+
size_t channel_id_size = NUM_TRYTES_ADDRESS * sizeof(char);
36+
res->channel_id = (char*)malloc(channel_id_size);
37+
if (!res->channel_id) {
38+
return SC_RES_OOM;
39+
}
40+
41+
memcpy(res->channel_id, channel_id, channel_id_size);
42+
return SC_OK;
43+
}
44+
45+
void send_mam_res_free(send_mam_res_t** res) {
46+
if (!res || !(*res)) {
47+
return;
48+
}
49+
if ((*res)->bundle_hash) {
50+
free((*res)->bundle_hash);
51+
(*res)->bundle_hash = NULL;
52+
}
53+
if ((*res)->channel_id) {
54+
free((*res)->channel_id);
55+
(*res)->channel_id = NULL;
56+
}
57+
58+
free(*res);
59+
*res = NULL;
60+
}

response/ta_send_mam_res.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#ifndef RESPONSE_TA_SEND_MAM_RES_H_
2+
#define RESPONSE_TA_SEND_MAM_RES_H_
3+
4+
#include "accelerator/errors.h"
5+
#include "common/model/transaction.h"
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
/**
12+
* @file response/ta_send_mam_res.h
13+
*/
14+
15+
/** struct of ta_send_mam_res_t */
16+
typedef struct send_mam_res_s {
17+
/** ascii string bundle hash */
18+
char* bundle_hash;
19+
/** ascii string channel id */
20+
char* channel_id;
21+
} send_mam_res_t;
22+
23+
/**
24+
* Allocate memory of send_mam_res_t
25+
*
26+
* @return
27+
* - struct of send_mam_res_t on success
28+
* - NULL on error
29+
*/
30+
send_mam_res_t* send_mam_res_new();
31+
32+
/**
33+
* @brief Set the bundle_hash field of send_mam_res object.
34+
*
35+
* Receive a bundle hash tryte_t array which is 81 trytes long,
36+
* and convert (instead of decoding) the received bundle hash to ascii string.
37+
* After conversion, set the bundle_hash field of send_mam_res object.
38+
*
39+
* @param[in] res send_mam_res_t struct object
40+
* @param[in] bundle_hash bundle hash decoded in trytes string
41+
*
42+
* @return
43+
* - SC_OK on success
44+
* - non-zero on error
45+
*/
46+
status_t send_mam_res_set_bundle_hash(send_mam_res_t* res,
47+
const tryte_t* bundle_hash);
48+
49+
/**
50+
* @brief Set the channel_id field of send_mam_res object.
51+
*
52+
* Receive a channel id tryte_t array which is 81 trytes long,
53+
* and convert (instead of decoding) the received channel id to ascii string.
54+
* After conversion, set the channel_id field of send_mam_res object.
55+
*
56+
* @param[in] res send_mam_res_t struct object
57+
* @param[in] channel_id channel id decoded in trytes string
58+
*
59+
* @return
60+
* - SC_OK on success
61+
* - non-zero on error
62+
*/
63+
status_t send_mam_res_set_channel_id(send_mam_res_t* res,
64+
const tryte_t* channel_id);
65+
66+
/**
67+
* Free memory of send_mam_res_t
68+
*
69+
* @param req Data type of send_mam_res_t
70+
*
71+
* @return NULL
72+
*/
73+
void send_mam_res_free(send_mam_res_t** res);
74+
75+
#ifdef __cplusplus
76+
}
77+
#endif
78+
79+
#endif // RESPONSE_TA_SEND_MAM_RES_H_

0 commit comments

Comments
 (0)