This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move basic mam api into wrapper called map/mode. The wrapper is also moved under the map directory which is stand for Mask Authenticated Privacy. It will be a seperate binary to implement mam utility in the future.
- Loading branch information
Yu Wei Wu
committed
Jun 19, 2019
1 parent
4dbc257
commit ba75888
Showing
3 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
cc_library( | ||
name = "mode", | ||
srcs = ["mode.c"], | ||
hdrs = ["mode.h"], | ||
deps = [ | ||
"@entangled//common/trinary:tryte_ascii", | ||
"@entangled//mam/api", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "map/mode.h" | ||
#include "common/trinary/tryte_ascii.h" | ||
#include "mam/mam/mam_channel_t_set.h" | ||
|
||
retcode_t map_channel_create(mam_api_t *const api, tryte_t *const channel_id) { | ||
if (mam_channel_t_set_size(api->channels) == 0) { | ||
mam_api_channel_create(api, MSS_DEPTH, channel_id); | ||
} else { | ||
mam_channel_t *channel = &api->channels->value; | ||
trits_to_trytes(trits_begin(mam_channel_id(channel)), channel_id, NUM_TRITS_ADDRESS); | ||
} | ||
|
||
return RC_OK; | ||
} | ||
|
||
retcode_t map_announce_channel(mam_api_t *const api, tryte_t const *const channel_id, | ||
bundle_transactions_t *const bundle, trit_t *const msg_id, | ||
tryte_t *const new_channel_id) { | ||
retcode_t ret = RC_OK; | ||
|
||
ERR_BIND_RETURN(mam_api_channel_create(api, MSS_DEPTH, new_channel_id), ret); | ||
|
||
ret = mam_api_bundle_announce_channel(api, channel_id, new_channel_id, NULL, NULL, bundle, msg_id); | ||
|
||
return ret; | ||
} | ||
|
||
retcode_t map_announce_endpoint(mam_api_t *const api, tryte_t const *const channel_id, | ||
bundle_transactions_t *const bundle, trit_t *const msg_id, | ||
tryte_t *const new_endpoint_id) { | ||
retcode_t ret = RC_OK; | ||
|
||
ERR_BIND_RETURN(mam_api_endpoint_create(api, MSS_DEPTH, channel_id, new_endpoint_id), ret); | ||
|
||
ret = mam_api_bundle_announce_endpoint(api, channel_id, new_endpoint_id, NULL, NULL, bundle, msg_id); | ||
|
||
return ret; | ||
} | ||
|
||
retcode_t map_write_header_on_channel(mam_api_t *const api, tryte_t const *const channel_id, | ||
bundle_transactions_t *const bundle, trit_t *const msg_id) { | ||
retcode_t ret = RC_OK; | ||
|
||
ret = mam_api_bundle_write_header_on_channel(api, channel_id, NULL, NULL, bundle, msg_id); | ||
|
||
return ret; | ||
} | ||
|
||
retcode_t map_write_header_on_endpoint(mam_api_t *const api, tryte_t const *const channel_id, | ||
tryte_t const *const endpoint_id, bundle_transactions_t *const bundle, | ||
trit_t *const msg_id) { | ||
retcode_t ret = RC_OK; | ||
|
||
mam_api_bundle_write_header_on_endpoint(api, channel_id, endpoint_id, NULL, NULL, bundle, msg_id); | ||
|
||
return ret; | ||
} | ||
|
||
retcode_t map_write_packet(mam_api_t *const api, bundle_transactions_t *const bundle, char const *const payload, | ||
trit_t const *const msg_id, bool is_last_packet) { | ||
retcode_t ret = RC_OK; | ||
tryte_t *payload_trytes = (tryte_t *)malloc(2 * strlen(payload) * sizeof(tryte_t)); | ||
|
||
ascii_to_trytes(payload, payload_trytes); | ||
ret = mam_api_bundle_write_packet(api, msg_id, payload_trytes, strlen(payload) * 2, 0, is_last_packet, bundle); | ||
|
||
free(payload_trytes); | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#ifndef __MAP_MODE_H__ | ||
#define __MAP_MODE_H__ | ||
|
||
#include "mam/api/api.h" | ||
|
||
#define MSS_DEPTH 1 | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* Creates a channel | ||
* | ||
* @param api - The API [in,out] | ||
* @param channel_id - A known channel ID [out] | ||
* | ||
* @return return code | ||
*/ | ||
retcode_t map_channel_create(mam_api_t* const api, tryte_t* const channel_id); | ||
|
||
/** | ||
* Creates and announce a new channel on header | ||
* | ||
* @param api - The API [in,out] | ||
* @param channel_id - A known channel ID [in] | ||
* @param bundle - The bundle that the packet will be written into [out] | ||
* @param msg_id - The msg_id [out] | ||
* @param new_channel_id - The new channel ID [out] | ||
* | ||
* @return return code | ||
*/ | ||
retcode_t map_announce_channel(mam_api_t* const api, tryte_t const* const channel_id, | ||
bundle_transactions_t* const bundle, trit_t* const msg_id, | ||
tryte_t* const new_channel_id); | ||
|
||
/** | ||
* Creates and announce a new endpoint on header | ||
* | ||
* @param api - The API [in,out] | ||
* @param channel_id - A known channel ID [in] | ||
* @param bundle - The bundle that the packet will be written into [out] | ||
* @param msg_id - The msg_id [out] | ||
* @param new_endpoint - The new endpoint [out] | ||
* | ||
* @return return code | ||
*/ | ||
retcode_t map_announce_endpoint(mam_api_t* const api, tryte_t const* const channel_id, | ||
bundle_transactions_t* const bundle, trit_t* const msg_id, | ||
tryte_t* const new_endpoint_id); | ||
|
||
/** | ||
* Writes a header only bundle on a channel | ||
* | ||
* @param api - The API [in,out] | ||
* @param channel_id - A known channel ID [in] | ||
* @param bundle - The bundle that the packet will be written into [out] | ||
* @param msg_id - The msg_id [out] | ||
* | ||
* @return return code | ||
*/ | ||
retcode_t map_write_header_on_channel(mam_api_t* const api, tryte_t const* const channel_id, | ||
bundle_transactions_t* const bundle, trit_t* const msg_id); | ||
|
||
/** | ||
* Writes a header only bundle on an endpoint | ||
* | ||
* @param api - The API [in,out] | ||
* @param channel_id - A known channel ID [in] | ||
* @param endpoint_id - A known endpoint ID [in] | ||
* @param bundle - The bundle that the packet will be written into [out] | ||
* @param msg_id - The msg_id [out] | ||
* | ||
* @return return code | ||
*/ | ||
retcode_t map_write_header_on_endpoint(mam_api_t* const api, tryte_t const* const channel_id, | ||
tryte_t const* const endpoint_id, bundle_transactions_t* const bundle, | ||
trit_t* const msg_id); | ||
|
||
/** | ||
* Writes a packet on a bundle | ||
* | ||
* @param api - The API [in,out] | ||
* @param bundle - The bundle that the packet will be written into [out] | ||
* @param payload - The payload to write [in] | ||
* @param msg_id - The msg_id [in] | ||
* @param is_last_packet - True if this is the last packet to be written [in] | ||
* | ||
* @return return code | ||
*/ | ||
retcode_t map_write_packet(mam_api_t* const api, bundle_transactions_t* const bundle, char const* const payload, | ||
trit_t const* const msg_id, bool is_last_packet); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // __MAP_MODE_H__ |