Skip to content

Commit a15a765

Browse files
committed
Add ctx ptrs to IPC APIs with callbacks
1 parent 49d5eac commit a15a765

File tree

9 files changed

+64
-13
lines changed

9 files changed

+64
-13
lines changed

cpp/priv_include/ggl/ipc/client_c_api.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ GglError ggipc_publish_to_topic_binary_b64(
2828
) noexcept;
2929

3030
typedef void GgIpcSubscribeToTopicCallback(
31-
GglBuffer topic, GglObject payload, GgIpcSubscriptionHandle handle
31+
void *ctx,
32+
GglBuffer topic,
33+
GglObject payload,
34+
GgIpcSubscriptionHandle handle
3235
);
3336

3437
GglError ggipc_subscribe_to_topic(
3538
GglBuffer topic,
3639
GgIpcSubscribeToTopicCallback *callback,
40+
void *ctx,
3741
GgIpcSubscriptionHandle *handle
3842
) noexcept;
3943

@@ -46,13 +50,17 @@ GglError ggipc_publish_to_iot_core_b64(
4650
) noexcept;
4751

4852
typedef void GgIpcSubscribeToIotCoreCallback(
49-
GglBuffer topic, GglBuffer payload, GgIpcSubscriptionHandle handle
53+
void *ctx,
54+
GglBuffer topic,
55+
GglBuffer payload,
56+
GgIpcSubscriptionHandle handle
5057
) noexcept;
5158

5259
GglError ggipc_subscribe_to_iot_core(
5360
GglBuffer topic_filter,
5461
uint8_t qos,
5562
GgIpcSubscribeToIotCoreCallback *callback,
63+
void *ctx,
5664
GgIpcSubscriptionHandle *handle
5765
) noexcept;
5866

include/ggl/ipc/client.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ GglError ggipc_publish_to_topic_binary_b64(
6060
);
6161

6262
typedef void GgIpcSubscribeToTopicCallback(
63-
GglBuffer topic, GglObject payload, GgIpcSubscriptionHandle handle
63+
void *ctx,
64+
GglBuffer topic,
65+
GglObject payload,
66+
GgIpcSubscriptionHandle handle
6467
);
6568

6669
/// Subscribe to messages on a local topic
@@ -69,6 +72,7 @@ NONNULL(2)
6972
GglError ggipc_subscribe_to_topic(
7073
GglBuffer topic,
7174
GgIpcSubscribeToTopicCallback *callback,
75+
void *ctx,
7276
GgIpcSubscriptionHandle *handle
7377
);
7478

@@ -85,7 +89,10 @@ GglError ggipc_publish_to_iot_core_b64(
8589
);
8690

8791
typedef void GgIpcSubscribeToIotCoreCallback(
88-
GglBuffer topic, GglBuffer payload, GgIpcSubscriptionHandle handle
92+
void *ctx,
93+
GglBuffer topic,
94+
GglBuffer payload,
95+
GgIpcSubscriptionHandle handle
8996
);
9097

9198
/// Subscribe to MQTT messages from AWS IoT Core on a topic or topic filter
@@ -94,6 +101,7 @@ GglError ggipc_subscribe_to_iot_core(
94101
GglBuffer topic_filter,
95102
uint8_t qos,
96103
GgIpcSubscribeToIotCoreCallback *callback,
104+
void *ctx,
97105
GgIpcSubscriptionHandle *handle
98106
);
99107

@@ -136,7 +144,10 @@ GglError ggipc_update_state(GglComponentState state);
136144
GglError ggipc_restart_component(GglBuffer component_name);
137145

138146
typedef void GgIpcSubscribeToConfigurationUpdateCallback(
139-
GglBuffer component_name, GglList key_path, GgIpcSubscriptionHandle handle
147+
void *ctx,
148+
GglBuffer component_name,
149+
GglList key_path,
150+
GgIpcSubscriptionHandle handle
140151
);
141152

142153
/// Subscribe to configuration updates for a component
@@ -146,6 +157,7 @@ GglError ggipc_subscribe_to_configuration_update(
146157
const GglBuffer *component_name,
147158
GglBufList key_path,
148159
GgIpcSubscribeToConfigurationUpdateCallback *callback,
160+
void *ctx,
149161
GgIpcSubscriptionHandle *handle
150162
);
151163

include/ggl/ipc/client_raw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ GglError ggipc_call(
2626

2727
typedef GglError GgIpcSubscribeCallback(
2828
void *ctx,
29+
void *aux_ctx,
2930
GgIpcSubscriptionHandle handle,
3031
GglBuffer service_model_type,
3132
GglMap data
@@ -40,6 +41,7 @@ GglError ggipc_subscribe(
4041
void *response_ctx,
4142
GgIpcSubscribeCallback *sub_callback,
4243
void *sub_callback_ctx,
44+
void *sub_callback_aux_ctx,
4345
GgIpcSubscriptionHandle *sub_handle
4446
);
4547

samples/iot_core_mqtt/main.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
#include <stdlib.h>
1111

1212
static void response_handler(
13-
GglBuffer topic, GglBuffer payload, GgIpcSubscriptionHandle handle
13+
void *ctx,
14+
GglBuffer topic,
15+
GglBuffer payload,
16+
GgIpcSubscriptionHandle handle
1417
) {
18+
(void) ctx;
1519
(void) handle;
1620
printf(
1721
"Received [%.*s] on [%.*s].\n",
@@ -35,7 +39,7 @@ int main(void) {
3539
printf("Connected to GG nucleus.\n");
3640

3741
ret = ggipc_subscribe_to_iot_core(
38-
GGL_STR("hello"), 0, &response_handler, NULL
42+
GGL_STR("hello"), 0, &response_handler, NULL, NULL
3943
);
4044
if (ret != GGL_ERR_OK) {
4145
fprintf(stderr, "Failed to call subscribe_to_iot_core.\n");

samples/subscribe_to_configuration_update/main.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ static pthread_cond_t config_cond = PTHREAD_COND_INITIALIZER;
2020
static bool config_updated = false;
2121

2222
static void config_update_handler(
23-
GglBuffer component_name, GglList key_path, GgIpcSubscriptionHandle handle
23+
void *ctx,
24+
GglBuffer component_name,
25+
GglList key_path,
26+
GgIpcSubscriptionHandle handle
2427
) {
28+
(void) ctx;
2529
(void) handle;
2630
printf("Configuration update received:\n");
2731
printf(
@@ -64,7 +68,7 @@ int main(void) {
6468
// component
6569
GglBufList key_path = GGL_BUF_LIST(GGL_STR("test_str"));
6670
ret = ggipc_subscribe_to_configuration_update(
67-
NULL, key_path, config_update_handler, NULL
71+
NULL, key_path, config_update_handler, NULL, NULL
6872
);
6973

7074
if (ret != GGL_ERR_OK) {

src/ipc/client.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static pid_t recv_thread_id = -1;
5252
typedef struct {
5353
GgIpcSubscribeCallback *fn;
5454
void *ctx;
55+
void *aux_ctx;
5556
} StreamHandler;
5657

5758
static_assert(
@@ -444,6 +445,7 @@ typedef struct {
444445
void *response_ctx;
445446
GgIpcSubscribeCallback *sub_callback;
446447
void *sub_callback_ctx;
448+
void *sub_callback_aux_ctx;
447449
} ResponseHandlerCtx;
448450

449451
// Must hold stream_state_mtx
@@ -482,6 +484,7 @@ static void response_handler(
482484
(StreamHandler) {
483485
.fn = call_ctx->sub_callback,
484486
.ctx = call_ctx->sub_callback_ctx,
487+
.aux_ctx = call_ctx->sub_callback_aux_ctx,
485488
}
486489
);
487490
}
@@ -508,6 +511,7 @@ GglError ggipc_call(
508511
response_ctx,
509512
NULL,
510513
NULL,
514+
NULL,
511515
NULL
512516
);
513517
}
@@ -521,6 +525,7 @@ GglError ggipc_subscribe(
521525
void *response_ctx,
522526
GgIpcSubscribeCallback *sub_callback,
523527
void *sub_callback_ctx,
528+
void *sub_callback_aux_ctx,
524529
GgIpcSubscriptionHandle *sub_handle
525530
) {
526531
if (!connected()) {
@@ -551,6 +556,7 @@ GglError ggipc_subscribe(
551556
.response_ctx = response_ctx,
552557
.sub_callback = sub_callback,
553558
.sub_callback_ctx = sub_callback_ctx,
559+
.sub_callback_aux_ctx = sub_callback_aux_ctx,
554560
};
555561

556562
uint16_t stream_index;
@@ -626,6 +632,7 @@ static GglError call_sub_callback(
626632
GgIpcSubscriptionHandle handle,
627633
GgIpcSubscribeCallback *sub_callback,
628634
void *sub_callback_ctx,
635+
void *sub_callback_aux_ctx,
629636
EventStreamCommonHeaders common_headers,
630637
EventStreamMessage msg
631638
) {
@@ -696,7 +703,11 @@ static GglError call_sub_callback(
696703
}
697704

698705
return sub_callback(
699-
sub_callback_ctx, handle, service_model_type, ggl_obj_into_map(response)
706+
sub_callback_ctx,
707+
sub_callback_aux_ctx,
708+
handle,
709+
service_model_type,
710+
ggl_obj_into_map(response)
700711
);
701712
}
702713

@@ -749,6 +760,7 @@ static GglError dispatch_incoming_packet(int conn) {
749760
get_current_handle(index),
750761
stream_state_handler[index].fn,
751762
stream_state_handler[index].ctx,
763+
stream_state_handler[index].aux_ctx,
752764
common_headers,
753765
msg
754766
);

src/ipc/client/subscribe_to_configuration_update.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
static GglError subscribe_to_configuration_update_resp_handler(
1717
void *ctx,
18+
void *aux_ctx,
1819
GgIpcSubscriptionHandle handle,
1920
GglBuffer service_model_type,
2021
GglMap data
@@ -65,7 +66,7 @@ static GglError subscribe_to_configuration_update_resp_handler(
6566
GglList key_path = ggl_obj_into_list(*key_path_obj);
6667

6768
if (handler != NULL) {
68-
handler(component_name, key_path, handle);
69+
handler(aux_ctx, component_name, key_path, handle);
6970
}
7071

7172
return GGL_ERR_OK;
@@ -97,6 +98,7 @@ GglError ggipc_subscribe_to_configuration_update(
9798
const GglBuffer *component_name,
9899
GglBufList key_path,
99100
GgIpcSubscribeToConfigurationUpdateCallback *callback,
101+
void *ctx,
100102
GgIpcSubscriptionHandle *handle
101103
) {
102104
GglKVVec args = GGL_KV_VEC((GglKV[2]) { 0 });
@@ -131,6 +133,7 @@ GglError ggipc_subscribe_to_configuration_update(
131133
NULL,
132134
&subscribe_to_configuration_update_resp_handler,
133135
callback,
136+
ctx,
134137
handle
135138
);
136139
}

src/ipc/client/subscribe_to_iot_core.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
static GglError subscribe_to_iot_core_resp_handler(
1818
void *ctx,
19+
void *aux_ctx,
1920
GgIpcSubscriptionHandle handle,
2021
GglBuffer service_model_type,
2122
GglMap data
@@ -63,7 +64,7 @@ static GglError subscribe_to_iot_core_resp_handler(
6364
return GGL_ERR_INVALID;
6465
}
6566

66-
callback(topic, payload, handle);
67+
callback(aux_ctx, topic, payload, handle);
6768
return GGL_ERR_OK;
6869
}
6970

@@ -90,6 +91,7 @@ GglError ggipc_subscribe_to_iot_core(
9091
GglBuffer topic_filter,
9192
uint8_t qos,
9293
GgIpcSubscribeToIotCoreCallback *callback,
94+
void *ctx,
9395
GgIpcSubscriptionHandle *handle
9496
) {
9597
if (qos > 2) {
@@ -111,6 +113,7 @@ GglError ggipc_subscribe_to_iot_core(
111113
NULL,
112114
&subscribe_to_iot_core_resp_handler,
113115
callback,
116+
ctx,
114117
handle
115118
);
116119
}

src/ipc/client/subscribe_to_topic.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
static GglError subscribe_to_topic_resp_handler(
1818
void *ctx,
19+
void *aux_ctx,
1920
GgIpcSubscriptionHandle handle,
2021
GglBuffer service_model_type,
2122
GglMap data
@@ -99,7 +100,7 @@ static GglError subscribe_to_topic_resp_handler(
99100
payload = ggl_obj_buf(payload_buf);
100101
}
101102

102-
callback(topic, payload, handle);
103+
callback(aux_ctx, topic, payload, handle);
103104
return GGL_ERR_OK;
104105
}
105106

@@ -125,6 +126,7 @@ static GglError error_handler(
125126
GglError ggipc_subscribe_to_topic(
126127
GglBuffer topic,
127128
GgIpcSubscribeToTopicCallback callback,
129+
void *ctx,
128130
GgIpcSubscriptionHandle *handle
129131
) {
130132
GglMap args = GGL_MAP(ggl_kv(GGL_STR("topic"), ggl_obj_buf(topic)), );
@@ -138,6 +140,7 @@ GglError ggipc_subscribe_to_topic(
138140
NULL,
139141
&subscribe_to_topic_resp_handler,
140142
callback,
143+
ctx,
141144
handle
142145
);
143146
}

0 commit comments

Comments
 (0)