55#include "ggl/aws_iot_call.h"
66#include <assert.h>
77#include <errno.h>
8- #include <ggl/arena.h>
9- #include <ggl/buffer.h>
10- #include <ggl/cleanup.h>
8+ #include <gg/arena.h>
9+ #include <gg/buffer.h>
10+ #include <gg/cleanup.h>
11+ #include <gg/error.h>
12+ #include <gg/json_decode.h>
13+ #include <gg/json_encode.h>
14+ #include <gg/log.h>
15+ #include <gg/map.h>
16+ #include <gg/object.h>
17+ #include <gg/vector.h>
1118#include <ggl/core_bus/aws_iot_mqtt.h>
1219#include <ggl/core_bus/client.h> // IWYU pragma: keep (cleanup)
13- #include <ggl/error.h>
14- #include <ggl/json_decode.h>
15- #include <ggl/json_encode.h>
16- #include <ggl/log.h>
17- #include <ggl/map.h>
18- #include <ggl/object.h>
19- #include <ggl/vector.h>
2020#include <pthread.h>
2121#include <sys/types.h>
2222#include <time.h>
@@ -35,44 +35,44 @@ typedef struct {
3535 pthread_mutex_t * mtx ;
3636 pthread_cond_t * cond ;
3737 bool ready ;
38- GglBuffer * client_token ;
39- GglArena * alloc ;
40- GglObject * result ;
41- GglError ret ;
38+ GgBuffer * client_token ;
39+ GgArena * alloc ;
40+ GgObject * result ;
41+ GgError ret ;
4242} CallbackCtx ;
4343
4444static void cleanup_pthread_cond (pthread_cond_t * * cond ) {
4545 pthread_cond_destroy (* cond );
4646}
4747
48- static GglError get_client_token (GglObject payload , GglBuffer * * client_token ) {
48+ static GgError get_client_token (GgObject payload , GgBuffer * * client_token ) {
4949 assert (client_token != NULL );
5050 assert (* client_token != NULL );
5151
52- if (ggl_obj_type (payload ) != GGL_TYPE_MAP ) {
52+ if (gg_obj_type (payload ) != GG_TYPE_MAP ) {
5353 * client_token = NULL ;
54- return GGL_ERR_OK ;
54+ return GG_ERR_OK ;
5555 }
56- GglMap payload_map = ggl_obj_into_map (payload );
56+ GgMap payload_map = gg_obj_into_map (payload );
5757
58- GglObject * found ;
59- if (!ggl_map_get (payload_map , GGL_STR ("clientToken" ), & found )) {
58+ GgObject * found ;
59+ if (!gg_map_get (payload_map , GG_STR ("clientToken" ), & found )) {
6060 * client_token = NULL ;
61- return GGL_ERR_OK ;
61+ return GG_ERR_OK ;
6262 }
63- if (ggl_obj_type (* found ) != GGL_TYPE_BUF ) {
64- GGL_LOGE ("Invalid clientToken type." );
65- return GGL_ERR_INVALID ;
63+ if (gg_obj_type (* found ) != GG_TYPE_BUF ) {
64+ GG_LOGE ("Invalid clientToken type." );
65+ return GG_ERR_INVALID ;
6666 }
67- * * client_token = ggl_obj_into_buf (* found );
68- return GGL_ERR_OK ;
67+ * * client_token = gg_obj_into_buf (* found );
68+ return GG_ERR_OK ;
6969}
7070
71- static bool match_client_token (GglObject payload , GglBuffer * client_token ) {
72- GglBuffer * payload_client_token = & (GglBuffer ) { 0 };
71+ static bool match_client_token (GgObject payload , GgBuffer * client_token ) {
72+ GgBuffer * payload_client_token = & (GgBuffer ) { 0 };
7373
74- GglError ret = get_client_token (payload , & payload_client_token );
75- if (ret != GGL_ERR_OK ) {
74+ GgError ret = get_client_token (payload , & payload_client_token );
75+ if (ret != GG_ERR_OK ) {
7676 return false;
7777 }
7878
@@ -84,93 +84,92 @@ static bool match_client_token(GglObject payload, GglBuffer *client_token) {
8484 return false;
8585 }
8686
87- return ggl_buffer_eq (* client_token , * payload_client_token );
87+ return gg_buffer_eq (* client_token , * payload_client_token );
8888}
8989
90- static GglError subscription_callback (
91- void * ctx , uint32_t handle , GglObject data
90+ static GgError subscription_callback (
91+ void * ctx , uint32_t handle , GgObject data
9292) {
9393 (void ) handle ;
9494 CallbackCtx * call_ctx = ctx ;
9595
96- GglBuffer topic ;
97- GglBuffer payload = { 0 };
98- GglError ret
99- = ggl_aws_iot_mqtt_subscribe_parse_resp (data , & topic , & payload );
100- if (ret != GGL_ERR_OK ) {
96+ GgBuffer topic ;
97+ GgBuffer payload = { 0 };
98+ GgError ret = ggl_aws_iot_mqtt_subscribe_parse_resp (data , & topic , & payload );
99+ if (ret != GG_ERR_OK ) {
101100 return ret ;
102101 }
103102
104103 bool decoded = true;
105- ret = ggl_json_decode_destructive (
104+ ret = gg_json_decode_destructive (
106105 payload , call_ctx -> alloc , call_ctx -> result
107106 );
108- if (ret != GGL_ERR_OK ) {
109- GGL_LOGE ("Failed to decode response payload." );
110- * (call_ctx -> result ) = GGL_OBJ_NULL ;
107+ if (ret != GG_ERR_OK ) {
108+ GG_LOGE ("Failed to decode response payload." );
109+ * (call_ctx -> result ) = GG_OBJ_NULL ;
111110 decoded = false;
112111 }
113112
114113 if (!match_client_token (* call_ctx -> result , call_ctx -> client_token )) {
115114 // Skip this message
116- return GGL_ERR_OK ;
115+ return GG_ERR_OK ;
117116 }
118117
119- if (ggl_buffer_has_suffix (topic , GGL_STR ("/accepted" ))) {
118+ if (gg_buffer_has_suffix (topic , GG_STR ("/accepted" ))) {
120119 if (!decoded ) {
121- return GGL_ERR_INVALID ;
120+ return GG_ERR_INVALID ;
122121 }
123- call_ctx -> ret = GGL_ERR_OK ;
124- } else if (ggl_buffer_has_suffix (topic , GGL_STR ("/rejected" ))) {
125- GGL_LOGE (
122+ call_ctx -> ret = GG_ERR_OK ;
123+ } else if (gg_buffer_has_suffix (topic , GG_STR ("/rejected" ))) {
124+ GG_LOGE (
126125 "Received rejected response: %.*s" , (int ) payload .len , payload .data
127126 );
128- call_ctx -> ret = GGL_ERR_REMOTE ;
127+ call_ctx -> ret = GG_ERR_REMOTE ;
129128 } else {
130- return GGL_ERR_INVALID ;
129+ return GG_ERR_INVALID ;
131130 }
132131
133132 // Err to close subscription
134- return GGL_ERR_EXPECTED ;
133+ return GG_ERR_EXPECTED ;
135134}
136135
137136static void subscription_close_callback (void * ctx , uint32_t handle ) {
138137 (void ) handle ;
139138 CallbackCtx * call_ctx = ctx ;
140139
141- GGL_MTX_SCOPE_GUARD (call_ctx -> mtx );
140+ GG_MTX_SCOPE_GUARD (call_ctx -> mtx );
142141 call_ctx -> ready = true;
143142 pthread_cond_signal (call_ctx -> cond );
144143}
145144
146- GglError ggl_aws_iot_call (
147- GglBuffer socket_name ,
148- GglBuffer topic ,
149- GglObject payload ,
145+ GgError ggl_aws_iot_call (
146+ GgBuffer socket_name ,
147+ GgBuffer topic ,
148+ GgObject payload ,
150149 bool virtual ,
151- GglArena * alloc ,
152- GglObject * result
150+ GgArena * alloc ,
151+ GgObject * result
153152) {
154153 static pthread_mutex_t mem_mtx = PTHREAD_MUTEX_INITIALIZER ;
155- GGL_MTX_SCOPE_GUARD (& mem_mtx );
154+ GG_MTX_SCOPE_GUARD (& mem_mtx );
156155
157156 // TODO: Share memory for topic filter and encode
158157 static uint8_t topic_filter_mem [AWS_IOT_MAX_TOPIC_SIZE ];
159158 static uint8_t json_encode_mem [GGL_MAX_IOT_CORE_API_PAYLOAD_LEN ];
160159
161- GglByteVec topic_filter = GGL_BYTE_VEC (topic_filter_mem );
160+ GgByteVec topic_filter = GG_BYTE_VEC (topic_filter_mem );
162161
163- GglError ret = ggl_byte_vec_append (& topic_filter , topic );
164- ggl_byte_vec_chain_append (& ret , & topic_filter , GGL_STR ("/+" ));
165- if (ret != GGL_ERR_OK ) {
166- GGL_LOGE ("Failed to construct response topic filter." );
162+ GgError ret = gg_byte_vec_append (& topic_filter , topic );
163+ gg_byte_vec_chain_append (& ret , & topic_filter , GG_STR ("/+" ));
164+ if (ret != GG_ERR_OK ) {
165+ GG_LOGE ("Failed to construct response topic filter." );
167166 return ret ;
168167 }
169168
170- GglByteVec payload_vec = GGL_BYTE_VEC (json_encode_mem );
171- ret = ggl_json_encode (payload , ggl_byte_vec_writer (& payload_vec ));
172- if (ret != GGL_ERR_OK ) {
173- GGL_LOGE ("Failed to encode JSON payload." );
169+ GgByteVec payload_vec = GG_BYTE_VEC (json_encode_mem );
170+ ret = gg_json_encode (payload , gg_byte_vec_writer (& payload_vec ));
171+ if (ret != GG_ERR_OK ) {
172+ GG_LOGE ("Failed to encode JSON payload." );
174173 return ret ;
175174 }
176175
@@ -180,45 +179,45 @@ GglError ggl_aws_iot_call(
180179 pthread_cond_t notify_cond ;
181180 pthread_cond_init (& notify_cond , & notify_condattr );
182181 pthread_condattr_destroy (& notify_condattr );
183- GGL_CLEANUP (cleanup_pthread_cond , & notify_cond );
182+ GG_CLEANUP (cleanup_pthread_cond , & notify_cond );
184183 pthread_mutex_t notify_mtx = PTHREAD_MUTEX_INITIALIZER ;
185184
186185 CallbackCtx ctx = {
187186 .mtx = & notify_mtx ,
188187 .cond = & notify_cond ,
189188 .ready = false,
190- .client_token = & (GglBuffer ) { 0 },
189+ .client_token = & (GgBuffer ) { 0 },
191190 .alloc = alloc ,
192191 .result = result ,
193- .ret = GGL_ERR_FAILURE ,
192+ .ret = GG_ERR_FAILURE ,
194193 };
195194
196195 ret = get_client_token (payload , & ctx .client_token );
197- if (ret != GGL_ERR_OK ) {
196+ if (ret != GG_ERR_OK ) {
198197 return ret ;
199198 }
200199
201200 uint32_t sub_handle = 0 ;
202201 ret = ggl_aws_iot_mqtt_subscribe (
203202 socket_name ,
204- GGL_BUF_LIST (topic_filter .buf ),
203+ GG_BUF_LIST (topic_filter .buf ),
205204 1 ,
206205 virtual ,
207206 subscription_callback ,
208207 subscription_close_callback ,
209208 & ctx ,
210209 & sub_handle
211210 );
212- if (ret != GGL_ERR_OK ) {
213- GGL_LOGE ("Response topic subscription failed." );
211+ if (ret != GG_ERR_OK ) {
212+ GG_LOGE ("Response topic subscription failed." );
214213 return ret ;
215214 }
216215
217216 ret = ggl_aws_iot_mqtt_publish (
218217 socket_name , topic , payload_vec .buf , 1 , true
219218 );
220- if (ret != GGL_ERR_OK ) {
221- GGL_LOGE ("Response topic subscription failed." );
219+ if (ret != GG_ERR_OK ) {
220+ GG_LOGE ("Response topic subscription failed." );
222221 ggl_client_sub_close (sub_handle );
223222 return ret ;
224223 }
@@ -232,14 +231,14 @@ GglError ggl_aws_iot_call(
232231 {
233232 // Must be unlocked before closing subscription
234233 // (else subscription response may be blocked, and close would deadlock)
235- GGL_MTX_SCOPE_GUARD (& notify_mtx );
234+ GG_MTX_SCOPE_GUARD (& notify_mtx );
236235
237236 while (!ctx .ready ) {
238237 int cond_ret
239238 = pthread_cond_timedwait (& notify_cond , & notify_mtx , & timeout );
240239 if ((cond_ret != 0 ) && (cond_ret != EINTR )) {
241240 assert (cond_ret == ETIMEDOUT );
242- GGL_LOGW ("Timed out waiting for a response." );
241+ GG_LOGW ("Timed out waiting for a response." );
243242 timed_out = true;
244243 break ;
245244 }
0 commit comments