Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | /* * Copyright (c) 2017 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <logging/log.h> LOG_MODULE_REGISTER(net_test, LOG_LEVEL_WRN); #include <ztest.h> #include <net/socket.h> #include <net/mqtt.h> #include <string.h> #include <errno.h> #include "../../mqtt_pubsub/src/config.h" #define BUFFER_SIZE 32 static u8_t rx_buffer[BUFFER_SIZE]; static u8_t tx_buffer[BUFFER_SIZE]; static struct mqtt_client client_ctx; static struct sockaddr broker; static struct pollfd fds[1]; static int nfds; static bool connected; static int payload_left; static const u8_t *payload; static const u8_t payload_short[] = "Short payload"; /* Generated by http://www.lipsum.com/ * 2 paragraphs, 171 words, 1210 bytes of Lorem Ipsum */ static const u8_t payload_long[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse " "eleifend odio sit amet scelerisque tincidunt. In orci ligula, egestas " "ut neque sit amet, fringilla malesuada dolor. Fusce rhoncus nunc in " "lacus tincidunt lobortis. Donec aliquam lectus gravida fermentum " "egestas. Curabitur eu ex pretium, dapibus massa in, pretium tellus. " "Suspendisse ac efficitur magna, ut convallis nisl. Duis sed sapien " "odio. Aliquam efficitur sed tellus sit amet eleifend. Sed facilisis " "ligula aliquam erat ornare hendrerit. Aenean tincidunt nunc a nulla " "vestibulum mollis. Vivamus fringilla euismod nisi sit amet malesuada. " "Vivamus consequat ultricies metus sed feugiat. Aenean malesuada " "cursus sem." "\n" "Vestibulum tempor diam et aliquam tristique. Nullam condimentum felis " "et convallis finibus. Aliquam erat volutpat. Nam et blandit tortor. " "Nullam pharetra arcu aliquam, suscipit mi eu, faucibus lacus. " "Pellentesque eleifend nulla sit amet purus maximus, et elementum " "lectus fringilla. Praesent consectetur diam eget tellus molestie " "tempor. Class aptent taciti sociosqu ad litora torquent per conubia " "nostra, per inceptos himenaeos. Nam tincidunt urna augue, nec cursus " "arcu eleifend nec. Donec semper tellus in leo nullam." "\n"; static void broker_init(void) { #if defined(CONFIG_NET_IPV6) struct sockaddr_in6 *broker6 = net_sin6(&broker); broker6->sin6_family = AF_INET6; broker6->sin6_port = htons(SERVER_PORT); inet_pton(AF_INET6, SERVER_ADDR, &broker6->sin6_addr); #else struct sockaddr_in *broker4 = net_sin(&broker); broker4->sin_family = AF_INET; broker4->sin_port = htons(SERVER_PORT); inet_pton(AF_INET, SERVER_ADDR, &broker4->sin_addr); #endif } static void prepare_fds(struct mqtt_client *client) { if (client->transport.type == MQTT_TRANSPORT_NON_SECURE) { fds[0].fd = client->transport.tcp.sock; } fds[0].events = ZSOCK_POLLIN; nfds = 1; } static void clear_fds(void) { nfds = 0; } static void wait(int timeout) { if (nfds > 0) { if (poll(fds, nfds, timeout) < 0) { TC_PRINT("poll error: %d\n", errno); } } } void publish_handler(struct mqtt_client *const client, const struct mqtt_evt *evt) { int rc; static u8_t buf[sizeof(payload_long)]; if (evt->result != 0) { TC_PRINT("MQTT PUBLISH error: %d\n", evt->result); goto error; } if (payload_left != evt->param.publish.message.payload.len) { TC_PRINT("Invalid payload length: %d\n", evt->param.publish.message.payload.len); goto error; } rc = mqtt_readall_publish_payload(client, buf, payload_left); if (rc != 0) { TC_PRINT("Error while reading publish payload\n"); goto error; } if (memcmp(payload, buf, evt->param.publish.message.payload.len) != 0) { TC_PRINT("Invalid payload content\n"); goto error; } payload_left = 0; return; error: payload_left = -1; } void mqtt_evt_handler(struct mqtt_client *const client, const struct mqtt_evt *evt) { int err; switch (evt->type) { case MQTT_EVT_CONNACK: if (evt->result != 0) { TC_PRINT("MQTT connect failed %d\n", evt->result); break; } connected = true; TC_PRINT("[%s:%d] MQTT_EVT_CONNACK: Connected!\n", __func__, __LINE__); break; case MQTT_EVT_DISCONNECT: TC_PRINT("[%s:%d] MQTT_EVT_DISCONNECT: disconnected %d\n", __func__, __LINE__, evt->result); connected = false; payload_left = -1; clear_fds(); break; case MQTT_EVT_PUBLISH: publish_handler(client, evt); break; case MQTT_EVT_PUBACK: if (evt->result != 0) { TC_PRINT("MQTT PUBACK error %d\n", evt->result); payload_left = -1; break; } TC_PRINT("[%s:%d] MQTT_EVT_PUBACK packet id: %u\n", __func__, __LINE__, evt->param.puback.message_id); break; case MQTT_EVT_PUBREC: if (evt->result != 0) { TC_PRINT("MQTT PUBREC error %d\n", evt->result); break; } TC_PRINT("[%s:%d] MQTT_EVT_PUBREC packet id: %u\n", __func__, __LINE__, evt->param.pubrec.message_id); const struct mqtt_pubrel_param rel_param = { .message_id = evt->param.pubrec.message_id }; err = mqtt_publish_qos2_release(client, &rel_param); if (err != 0) { TC_PRINT("Failed to send MQTT PUBREL: %d\n", err); } break; case MQTT_EVT_PUBCOMP: if (evt->result != 0) { TC_PRINT("MQTT PUBCOMP error %d\n", evt->result); break; } TC_PRINT("[%s:%d] MQTT_EVT_PUBCOMP packet id: %u\n", __func__, __LINE__, evt->param.pubcomp.message_id); break; case MQTT_EVT_SUBACK: if (evt->result != 0) { TC_PRINT("MQTT SUBACK error %d\n", evt->result); break; } TC_PRINT("[%s:%d] items: %d packet id: %u\n", __func__, __LINE__, evt->param.suback.return_codes.len, evt->param.suback.message_id); break; case MQTT_EVT_UNSUBACK: if (evt->result != 0) { TC_PRINT("MQTT UNSUBACK error %d\n", evt->result); break; } TC_PRINT("[%s:%d] packet id: %u\n", __func__, __LINE__, evt->param.unsuback.message_id); break; default: TC_PRINT("[%s:%d] Invalid MQTT packet\n", __func__, __LINE__); break; } } static char *get_mqtt_topic(void) { return "sensors"; } static void client_init(struct mqtt_client *client) { mqtt_client_init(client); broker_init(); /* MQTT client configuration */ client->broker = &broker; client->evt_cb = mqtt_evt_handler; client->client_id.utf8 = (u8_t *)MQTT_CLIENTID; client->client_id.size = strlen(MQTT_CLIENTID); client->password = NULL; client->user_name = NULL; client->protocol_version = MQTT_VERSION_3_1_1; client->transport.type = MQTT_TRANSPORT_NON_SECURE; client->rx_buf = rx_buffer; client->rx_buf_size = sizeof(rx_buffer); client->tx_buf = tx_buffer; client->tx_buf_size = sizeof(tx_buffer); } /* In this routine we block until the connected variable is 1 */ static int try_to_connect(struct mqtt_client *client) { int rc, i = 0; while (i++ < APP_CONNECT_TRIES && !connected) { client_init(&client_ctx); rc = mqtt_connect(client); if (rc != 0) { k_sleep(APP_SLEEP_MSECS); continue; } prepare_fds(client); wait(APP_SLEEP_MSECS); mqtt_input(client); if (!connected) { mqtt_abort(client); } } if (connected) { return 0; } return -EINVAL; } static int test_connect(void) { int rc; rc = try_to_connect(&client_ctx); if (rc != 0) { return TC_FAIL; } return TC_PASS; } static int test_subscribe(void) { int rc; struct mqtt_topic topic; struct mqtt_subscription_list sub; topic.topic.utf8 = get_mqtt_topic(); topic.topic.size = strlen(topic.topic.utf8); topic.qos = MQTT_QOS_1_AT_LEAST_ONCE; sub.list = &topic; sub.list_count = 1U; sub.message_id = sys_rand32_get(); rc = mqtt_subscribe(&client_ctx, &sub); if (rc != 0) { return TC_FAIL; } wait(APP_SLEEP_MSECS); rc = mqtt_input(&client_ctx); if (rc != 0) { return TC_FAIL; } return TC_PASS; } static int test_publish(enum mqtt_qos qos) { int rc; struct mqtt_publish_param param; payload_left = strlen(payload); param.message.topic.qos = qos; param.message.topic.topic.utf8 = (u8_t *)get_mqtt_topic(); param.message.topic.topic.size = strlen(param.message.topic.topic.utf8); param.message.payload.data = (u8_t *)payload; param.message.payload.len = payload_left; param.message_id = sys_rand32_get(); param.dup_flag = 0U; param.retain_flag = 0U; rc = mqtt_publish(&client_ctx, ¶m); if (rc != 0) { return TC_FAIL; } while (payload_left > 0) { wait(APP_SLEEP_MSECS); rc = mqtt_input(&client_ctx); if (rc != 0) { return TC_FAIL; } } if (payload_left != 0) { return TC_FAIL; } return TC_PASS; } static int test_unsubscribe(void) { int rc; struct mqtt_topic topic; struct mqtt_subscription_list unsub; topic.topic.utf8 = get_mqtt_topic(); topic.topic.size = strlen(topic.topic.utf8); unsub.list = &topic; unsub.list_count = 1U; unsub.message_id = sys_rand32_get(); rc = mqtt_unsubscribe(&client_ctx, &unsub); if (rc != 0) { return TC_FAIL; } wait(APP_SLEEP_MSECS); rc = mqtt_input(&client_ctx); if (rc != 0) { return TC_FAIL; } return TC_PASS; } static int test_disconnect(void) { int rc; rc = mqtt_disconnect(&client_ctx); if (rc != 0) { return TC_FAIL; } wait(APP_SLEEP_MSECS); rc = mqtt_input(&client_ctx); if (rc != 0) { return TC_FAIL; } if (connected) { return TC_FAIL; } return TC_PASS; } void test_mqtt_connect(void) { zassert_true(test_connect() == TC_PASS, NULL); } void test_mqtt_subscribe(void) { zassert_true(test_subscribe() == TC_PASS, NULL); } void test_mqtt_publish_short(void) { payload = payload_short; zassert_true(test_publish(MQTT_QOS_0_AT_MOST_ONCE) == TC_PASS, NULL); } void test_mqtt_publish_long(void) { payload = payload_long; zassert_true(test_publish(MQTT_QOS_1_AT_LEAST_ONCE) == TC_PASS, NULL); } void test_mqtt_unsubscribe(void) { zassert_true(test_unsubscribe() == TC_PASS, NULL); } void test_mqtt_disconnect(void) { zassert_true(test_disconnect() == TC_PASS, NULL); } |