1414#include <string.h>
1515#include <stdint.h>
1616
17- // TODO: use GglByteVec for payload to allow in-place base64 encoding.
18- GglError ggipc_publish_to_topic_binary (
19- int conn , GglBuffer topic , GglBuffer payload , GglArena * alloc
17+ static GglError publish_to_topic_common (
18+ int conn , GglBuffer topic , GglMap publish_message
2019) {
21- GglBuffer encoded_payload ;
22- GglError ret = ggl_base64_encode (payload , alloc , & encoded_payload );
23- if (ret != GGL_ERR_OK ) {
24- return ret ;
25- }
26- GglMap binary_message
27- = GGL_MAP (ggl_kv (GGL_STR ("message" ), ggl_obj_buf (encoded_payload )));
28- GglMap publish_message
29- = GGL_MAP (ggl_kv (GGL_STR ("binaryMessage" ), ggl_obj_map (binary_message ))
30- );
3120 GglMap args = GGL_MAP (
3221 ggl_kv (GGL_STR ("topic" ), ggl_obj_buf (topic )),
3322 ggl_kv (GGL_STR ("publishMessage" ), ggl_obj_map (publish_message ))
3423 );
3524
25+ GglArena error_alloc = ggl_arena_init (GGL_BUF ((uint8_t [128 ]) { 0 }));
3626 GglIpcError remote_error = GGL_IPC_ERROR_DEFAULT ;
37- GglObject resp ;
38- ret = ggipc_call (
27+ GglError ret = ggipc_call (
3928 conn ,
4029 GGL_STR ("aws.greengrass#PublishToTopic" ),
4130 GGL_STR ("aws.greengrass#PublishToTopicRequest" ),
4231 args ,
43- alloc ,
44- & resp ,
32+ & error_alloc ,
33+ NULL ,
4534 & remote_error
4635 );
4736 if (ret == GGL_ERR_REMOTE ) {
@@ -60,40 +49,29 @@ GglError ggipc_publish_to_topic_binary(
6049 return ret ;
6150}
6251
52+ GglError ggipc_publish_to_topic_binary (
53+ int conn , GglBuffer topic , GglBuffer payload , GglArena alloc
54+ ) {
55+ GglBuffer encoded_payload ;
56+ GglError ret = ggl_base64_encode (payload , & alloc , & encoded_payload );
57+ if (ret != GGL_ERR_OK ) {
58+ return ret ;
59+ }
60+ GglMap binary_message
61+ = GGL_MAP (ggl_kv (GGL_STR ("message" ), ggl_obj_buf (encoded_payload )));
62+ GglMap publish_message
63+ = GGL_MAP (ggl_kv (GGL_STR ("binaryMessage" ), ggl_obj_map (binary_message ))
64+ );
65+
66+ return publish_to_topic_common (conn , topic , publish_message );
67+ }
68+
6369GglError ggipc_publish_to_topic_obj (
6470 int conn , GglBuffer topic , GglObject payload
6571) {
6672 GglMap json_message = GGL_MAP (ggl_kv (GGL_STR ("message" ), payload ));
6773 GglMap publish_message
6874 = GGL_MAP (ggl_kv (GGL_STR ("jsonMessage" ), ggl_obj_map (json_message )));
69- GglMap args = GGL_MAP (
70- ggl_kv (GGL_STR ("topic" ), ggl_obj_buf (topic )),
71- ggl_kv (GGL_STR ("publishMessage" ), ggl_obj_map (publish_message ))
72- );
73-
74- GglArena error_alloc = ggl_arena_init (GGL_BUF ((uint8_t [128 ]) { 0 }));
75- GglIpcError remote_error = GGL_IPC_ERROR_DEFAULT ;
76- GglError ret = ggipc_call (
77- conn ,
78- GGL_STR ("aws.greengrass#PublishToTopic" ),
79- GGL_STR ("aws.greengrass#PublishToTopicRequest" ),
80- args ,
81- & error_alloc ,
82- NULL ,
83- & remote_error
84- );
85- if (ret == GGL_ERR_REMOTE ) {
86- if (remote_error .error_code == GGL_IPC_ERR_UNAUTHORIZED_ERROR ) {
87- GGL_LOGE (
88- "Component unauthorized: %.*s" ,
89- (int ) remote_error .message .len ,
90- remote_error .message .data
91- );
92- return GGL_ERR_UNSUPPORTED ;
93- }
94- GGL_LOGE ("Server error." );
95- return GGL_ERR_FAILURE ;
96- }
9775
98- return ret ;
76+ return publish_to_topic_common ( conn , topic , publish_message ) ;
9977}
0 commit comments