|
| 1 | +/* threadx_netx.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2025 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfMQTT. |
| 6 | + * |
| 7 | + * wolfMQTT is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfMQTT is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | +/* Include the autoconf generated config.h */ |
| 23 | +#ifdef HAVE_CONFIG_H |
| 24 | +#include <config.h> |
| 25 | +#endif |
| 26 | + |
| 27 | + |
| 28 | +/* This is a start to an example use with ThreadX. It currently is being used |
| 29 | + * to confirm that compiling with all necessary NetX ThreadX API's succeds */ |
| 30 | + |
| 31 | +#ifdef HAVE_NETX |
| 32 | + |
| 33 | +#include <stdio.h> |
| 34 | +#include "tx_api.h" |
| 35 | +#include "nx_api.h" |
| 36 | +#include "nxd_dns.h" |
| 37 | + |
| 38 | +#include "wolfmqtt/mqtt_client.h" |
| 39 | +#include "examples/mqttnet.h" |
| 40 | +#include "examples/mqttexample.h" |
| 41 | + |
| 42 | +#define DEMO_STACK_SIZE 40000 |
| 43 | +#define DEMO_IP_ADDRESS IP_ADDRESS(192,168,1,129) |
| 44 | +#define DEMO_NET_MASK 0xFFFFFF00 |
| 45 | +#define DEMO_GATEWAY_ADDRESS IP_ADDRESS(192,168,1,1) |
| 46 | +#define DEMO_DNS_SERVER IP_ADDRESS(8,8,8,8) |
| 47 | +#define DEMO_HOSTNAME "test.mosquitto.org" |
| 48 | +#define DEMO_PORT 1883 |
| 49 | +#define DEMO_TIMEOUT_MS 5000 |
| 50 | + |
| 51 | +/* ThreadX/NetX objects */ |
| 52 | +TX_THREAD demo_thread; |
| 53 | +ULONG demo_thread_stack[DEMO_STACK_SIZE / sizeof(ULONG)]; |
| 54 | +NX_PACKET_POOL pool_0; |
| 55 | +NX_IP ip_0; |
| 56 | +NX_DNS dns_0; |
| 57 | +void *first_unused_memory; |
| 58 | + |
| 59 | +/* Forward declaration */ |
| 60 | +void demo_thread_entry(ULONG thread_input); |
| 61 | +void nx_driver_placeholder(NX_IP_DRIVER *driver_req); |
| 62 | + |
| 63 | +#ifdef __linux__ |
| 64 | +int main(int argc, char** argv) |
| 65 | +{ |
| 66 | + tx_kernel_enter(); |
| 67 | + return 0; |
| 68 | +} |
| 69 | +#endif |
| 70 | + |
| 71 | +void tx_application_define(void *memory_ptr) |
| 72 | +{ |
| 73 | + first_unused_memory = memory_ptr; |
| 74 | + |
| 75 | + /* Create the main demo thread. */ |
| 76 | + tx_thread_create(&demo_thread, "Demo Thread", demo_thread_entry, 0, |
| 77 | + demo_thread_stack, DEMO_STACK_SIZE, 1, 1, |
| 78 | + TX_NO_TIME_SLICE, TX_AUTO_START); |
| 79 | +} |
| 80 | + |
| 81 | +void demo_thread_entry(ULONG thread_input) |
| 82 | +{ |
| 83 | + UINT status; |
| 84 | + NXD_ADDRESS dns_server; |
| 85 | + MQTTCtx mqttCtx; |
| 86 | + MqttNet net; |
| 87 | + SocketContext *sockCtx; |
| 88 | + int rc; |
| 89 | + UCHAR* pointer = (UCHAR*)first_unused_memory; |
| 90 | + |
| 91 | + /* Initialize NetX system */ |
| 92 | + nx_system_initialize(); |
| 93 | + |
| 94 | + /* Create a packet pool. */ |
| 95 | + status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", |
| 96 | + 1536, pointer, 16*1536); |
| 97 | + if (status != NX_SUCCESS) { |
| 98 | + printf("Packet pool create failed: %u\n", status); |
| 99 | + return; |
| 100 | + } |
| 101 | + pointer += 16*1536; |
| 102 | + |
| 103 | + /* Create an IP instance. */ |
| 104 | + status = nx_ip_create(&ip_0, "NetX IP Instance", |
| 105 | + DEMO_IP_ADDRESS, DEMO_NET_MASK, |
| 106 | + &pool_0, nx_driver_placeholder, |
| 107 | + pointer, 2048, 1); |
| 108 | + if (status != NX_SUCCESS) { |
| 109 | + printf("IP create failed: %u\n", status); |
| 110 | + return; |
| 111 | + } |
| 112 | + pointer += 2048; |
| 113 | + |
| 114 | + /* Enable ARP, ICMP, TCP, UDP */ |
| 115 | + nx_arp_enable(&ip_0, pointer, 1024); |
| 116 | + pointer += 1024; |
| 117 | + nx_icmp_enable(&ip_0); |
| 118 | + nx_tcp_enable(&ip_0); |
| 119 | + nx_udp_enable(&ip_0); |
| 120 | + |
| 121 | + /* Create DNS instance */ |
| 122 | + status = nx_dns_create(&dns_0, &ip_0, (UCHAR*)"DNS Client"); |
| 123 | + if (status != NX_SUCCESS) { |
| 124 | + printf("DNS create failed: %u\n", status); |
| 125 | + return; |
| 126 | + } |
| 127 | + dns_server.nxd_ip_version = NX_IP_VERSION_V4; |
| 128 | + dns_server.nxd_ip_address.v4 = DEMO_DNS_SERVER; |
| 129 | + nxd_dns_server_add(&dns_0, &dns_server); |
| 130 | + |
| 131 | + /* Initialize MQTT context and network */ |
| 132 | + mqtt_init_ctx(&mqttCtx); |
| 133 | + MqttClientNet_Init(&net, &mqttCtx); |
| 134 | + sockCtx = (SocketContext*)net.context; |
| 135 | + sockCtx->ipPtr = &ip_0; |
| 136 | + sockCtx->dnsPtr = &dns_0; |
| 137 | + |
| 138 | + /* Use NetConnect to connect to the MQTT broker */ |
| 139 | + rc = net.connect(sockCtx, DEMO_HOSTNAME, DEMO_PORT, DEMO_TIMEOUT_MS); |
| 140 | + if (rc == 0) { |
| 141 | + printf("NetConnect succeeded!\n"); |
| 142 | + net.disconnect(sockCtx); |
| 143 | + } else { |
| 144 | + printf("NetConnect failed: %d\n", rc); |
| 145 | + } |
| 146 | + |
| 147 | + /* Cleanup (not strictly necessary in a demo) */ |
| 148 | + nx_dns_delete(&dns_0); |
| 149 | + nx_ip_delete(&ip_0); |
| 150 | + nx_packet_pool_delete(&pool_0); |
| 151 | + /* Thread exits */ |
| 152 | + tx_thread_terminate(&demo_thread); |
| 153 | +} |
| 154 | + |
| 155 | +/* Placeholder driver for demo purposes */ |
| 156 | +void nx_driver_placeholder(NX_IP_DRIVER *driver_req) |
| 157 | +{ |
| 158 | + NX_PARAMETER_NOT_USED(driver_req); |
| 159 | +} |
| 160 | +#endif /* HAVE_NETX */ |
0 commit comments