Debian Codegraph @

crt/aws-c-mqtt/tests/v3/mqtt_mock_server_handler.h

raw symbols SCIP
1 #ifndef MQTT_MOCK_SERVER_HANDLER_H
2 #define MQTT_MOCK_SERVER_HANDLER_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/mqtt/private/packets.h>
8
9 struct aws_channel_handler;
10 struct aws_channel_slot;
11
12 static const int MOCK_LOG_SUBJECT = 60000;
13
14 struct mqtt_decoded_packet {
15 enum aws_mqtt_packet_type type;
16 struct aws_allocator *alloc;
17
18 /* CONNECT */
19 bool clean_session;
20 bool has_will;
21 bool will_retain;
22 bool has_password;
23 bool has_username;
24 uint16_t keep_alive_timeout;
25 enum aws_mqtt_qos will_qos;
26 struct aws_byte_cursor client_identifier; /* These cursors live with the received_message */
27 struct aws_byte_cursor will_topic;
28 struct aws_byte_cursor will_message;
29 struct aws_byte_cursor username;
30 struct aws_byte_cursor password;
31
32 /* PUBLISH SUBSCRIBE UNSUBSCRIBE */
33 uint16_t packet_identifier;
34 struct aws_byte_cursor topic_name; /* PUBLISH topic */
35 struct aws_byte_cursor publish_payload; /* PUBLISH payload */
36 struct aws_array_list sub_topic_filters; /* list of aws_mqtt_subscription for SUBSCRIBE */
37 struct aws_array_list unsub_topic_filters; /* list of aws_byte_cursor for UNSUBSCRIBE */
38 bool duplicate; /* PUBLISH only */
39
40 /* index of the received packet, indicating when it's received by the server */
41 size_t index;
42 };
43
44 struct aws_channel_handler *new_mqtt_mock_server(struct aws_allocator *allocator);
45 void destroy_mqtt_mock_server(struct aws_channel_handler *handler);
46 void mqtt_mock_server_handler_update_slot(struct aws_channel_handler *handler, struct aws_channel_slot *slot);
47
48 /**
49 * Mock server sends a publish packet back to client
50 */
51 int mqtt_mock_server_send_publish(
52 struct aws_channel_handler *handler,
53 struct aws_byte_cursor *topic,
54 struct aws_byte_cursor *payload,
55 bool dup,
56 enum aws_mqtt_qos qos,
57 bool retain);
58
59 /**
60 * Mock server sends a publish packet back to client with user-controlled packet id
61 */
62 int mqtt_mock_server_send_publish_by_id(
63 struct aws_channel_handler *handler,
64 uint16_t packet_id,
65 struct aws_byte_cursor *topic,
66 struct aws_byte_cursor *payload,
67 bool dup,
68 enum aws_mqtt_qos qos,
69 bool retain);
70
71 /**
72 * Sets whether or not connacks return session present
73 */
74 void mqtt_mock_server_set_session_present(struct aws_channel_handler *handler, bool session_present);
75
76 /**
77 * Sets whether or not server should reflect received PUBLISH packets.
78 */
79 void mqtt_mock_server_set_publish_reflection(struct aws_channel_handler *handler, bool reflect_publishes);
80
81 /**
82 * Set max number of PINGRESP that mock server will send back to client
83 */
84 void mqtt_mock_server_set_max_ping_resp(struct aws_channel_handler *handler, size_t max_ping);
85
86 /**
87 * Set max number of CONACK that mock server will send back to client
88 */
89 void mqtt_mock_server_set_max_connack(struct aws_channel_handler *handler, size_t connack_avail);
90
91 /**
92 * Disable the automatically response (suback/unsuback/puback) to the client
93 */
94 void mqtt_mock_server_disable_auto_ack(struct aws_channel_handler *handler);
95
96 /**
97 * Enable the automatically response (suback/unsuback/puback) to the client
98 */
99 void mqtt_mock_server_enable_auto_ack(struct aws_channel_handler *handler);
100
101 /**
102 * Sets what reason code to return in subacks
103 */
104 void mqtt_mock_server_suback_reason_code(struct aws_channel_handler *handler, uint8_t reason_code);
105
106 /**
107 * Send response back the client given the packet ID
108 */
109 int mqtt_mock_server_send_unsuback(struct aws_channel_handler *handler, uint16_t packet_id);
110 int mqtt_mock_server_send_puback(struct aws_channel_handler *handler, uint16_t packet_id);
111
112 int mqtt_mock_server_send_single_suback(
113 struct aws_channel_handler *handler,
114 uint16_t packet_id,
115 enum aws_mqtt_qos return_code);
116 /**
117 * Wait for puback_count PUBACK packages from client
118 */
119 void mqtt_mock_server_wait_for_pubacks(struct aws_channel_handler *handler, size_t puback_count);
120
121 /**
122 * Getters for decoded packets, call mqtt_mock_server_decode_packets first.
123 */
124 size_t mqtt_mock_server_decoded_packets_count(struct aws_channel_handler *handler);
125 /**
126 * Get the decoded packet by index
127 */
128 struct mqtt_decoded_packet *mqtt_mock_server_get_decoded_packet_by_index(struct aws_channel_handler *handler, size_t i);
129 /**
130 * Get the latest received packet by index
131 */
132 struct mqtt_decoded_packet *mqtt_mock_server_get_latest_decoded_packet(struct aws_channel_handler *handler);
133 /**
134 * Get the decoded packet by packet_id started from search_start_idx (included), Note: it may have multiple packets with
135 * the same ID, this will return the earliest received on with the packet_id. If out_idx is not NULL, the index of found
136 * packet will be stored at there, and if failed to find the packet, it will be set to SIZE_MAX, and the return value
137 * will be NULL.
138 */
139 struct mqtt_decoded_packet *mqtt_mock_server_find_decoded_packet_by_id(
140 struct aws_channel_handler *handler,
141 size_t search_start_idx,
142 uint16_t packet_id,
143 size_t *out_idx);
144 /**
145 * Get the decoded packet by type started from search_start_idx (included), Note: it may have multiple packets with
146 * the same type, this will return the earliest received on with the packet_id. If out_idx is not NULL, the index of
147 * found packet will be stored at there, and if failed to find the packet, it will be set to SIZE_MAX, and the return
148 * value will be NULL.
149 */
150 struct mqtt_decoded_packet *mqtt_mock_server_find_decoded_packet_by_type(
151 struct aws_channel_handler *handler,
152 size_t search_start_idx,
153 enum aws_mqtt_packet_type type,
154 size_t *out_idx);
155
156 /**
157 * Run all received messages through, and decode the messages.
158 */
159 int mqtt_mock_server_decode_packets(struct aws_channel_handler *handler);
160
161 /**
162 * Returns the number of PINGs the server has gotten
163 */
164 size_t mqtt_mock_server_get_ping_count(struct aws_channel_handler *handler);
165
166 #endif /* MQTT_MOCK_SERVER_HANDLER_H */