summaryrefslogtreecommitdiff
path: root/samples/demo_bsd_udp.c
blob: 34188176b9d379e0b520961a762866322f347e35 (plain)
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
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation                                */
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/

/* This is a small demo of the high-performance NetX TCP/IP stack.  This demo concentrates
   on UDP packet sending and receiving - with ARP - using a simulated Ethernet driver.  */
/* This demo works for IPv4 only */

#include   "tx_api.h"
#include   "nx_api.h"
#ifndef NX_DISABLE_IPV4
#include   "nxd_bsd.h"
#include   <string.h>
#include   <stdlib.h>

#define     DEMO_STACK_SIZE         2048
#define     SERVER_PORT             721

/* Message sent to the server */
#define CLIENT_MESSAGE "Client BSD UDP Socket testing\n"


/* Define the ThreadX and NetX object control blocks...  */

TX_THREAD               thread_server;
TX_THREAD               thread_client;
NX_PACKET_POOL          bsd_pool;
NX_IP                   bsd_ip;


/* Define the counters used in the demo application...  */

ULONG                  thread_0_counter;
ULONG                  thread_2_counter;
ULONG                  error_counter;

/* Define thread prototypes.  */

VOID    thread_server_entry(ULONG thread_input);
VOID    thread_client_entry(ULONG thread_input);
VOID    _nx_ram_network_driver(struct NX_IP_DRIVER_STRUCT *driver_req);


/* Define main entry point.  */

int main()
{

    /* Enter the ThreadX kernel.  */
    tx_kernel_enter();
}

/* Define what the initial system looks like.  */

void    tx_application_define(void *first_unused_memory)
{

CHAR    *pointer;
UINT    status;

    
    /* Setup the working pointer.  */
    pointer =  (CHAR *) first_unused_memory;

    /* Create a thread for the Server  */
    tx_thread_create(&thread_server, "Server", thread_server_entry, 0,  
            pointer, DEMO_STACK_SIZE, 
            8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);

    pointer =  pointer + DEMO_STACK_SIZE;

    /* Create a thread for client.  */
    tx_thread_create(&thread_client, "Client", thread_client_entry, 0,  
            pointer, DEMO_STACK_SIZE, 
            16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);
    
    pointer =  pointer + DEMO_STACK_SIZE;

    /* Initialize the NetX system.  */
    nx_system_initialize();

    /* Create a BSD packet pool.  */
    status =  nx_packet_pool_create(&bsd_pool, "NetX BSD Packet Pool", 128, pointer, 16384);
    
    pointer = pointer + 16384;   
    if (status)
    {
        error_counter++;
        printf("Error in creating BSD packet pool\n!");
    }
      
    /* Create an IP instance for BSD.  */
    status += nx_ip_create(&bsd_ip, "NetX IP Instance 2", IP_ADDRESS(1, 2, 3, 4), 0xFFFFFF00UL,  
                           &bsd_pool, _nx_ram_network_driver,
                           pointer, DEMO_STACK_SIZE, 1);
    
    pointer =  pointer + DEMO_STACK_SIZE;

    /* Check for any errors */
    if (status)
        error_counter++;
    
    if (status)
        printf("Error creating BSD IP instance\n!");
    
    /* Enable ARP and supply ARP cache memory for BSD IP Instance */
    status +=  nx_arp_enable(&bsd_ip, (void *) pointer, 1024);
    
    pointer = pointer + 1024; 

    /* Check ARP enable status.  */     
    if (status)
        error_counter++;

    if (status)
        printf("Error in Enable ARP and supply ARP cache memory to BSD IP instance\n");

    /* Enable UDP traffic.  */
    status =  nx_udp_enable(&bsd_ip);

    /* Check for UDP enable errors.  */
    if (status)
        error_counter++;

    /* Now initialize BSD Socket Wrapper */
    status = (UINT)bsd_initialize (&bsd_ip, &bsd_pool, pointer, 2048, 2);

    if (status)
        error_counter++;

}


void    thread_server_entry(ULONG thread_input)
{

    /* Start the Server */
INT          status,addrlen1;
INT          sock_udp_2;
struct       sockaddr_in echoServAddr;               /* Echo server address */
struct       sockaddr_in fromAddr;                   /* Cleint address */
CHAR         buffer[64];

    NX_PARAMETER_NOT_USED(thread_input);

    /* Create a BSD UDP Server Socket */
    sock_udp_2 = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    /* Check for any errors */
    if (sock_udp_2 == -1)
    {
        printf("Error: BSD UDP Servert socket create\n");
        return;
    }   

     /* Fill ths socket with Server side information */
     memset(&echoServAddr, 0, sizeof(echoServAddr));
     echoServAddr.sin_family = PF_INET;
     echoServAddr.sin_addr.s_addr = htonl(IP_ADDRESS(1,2,3,4));
     echoServAddr.sin_port = htons(SERVER_PORT);

     /* Bind the UDP socket to the IP port.  */
     status = bind (sock_udp_2, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr));
     if (status < 0)
     {
         printf("Error: BSD UDP Server Socket Bind\n");
         return;
     }   
     
     while(1)
     {  

         addrlen1 = sizeof(struct sockaddr_in);

         /* Receive a UDP packet from a client. */
         status = recvfrom(sock_udp_2,(VOID *)buffer, 64, 0,(struct sockaddr *) &fromAddr, &addrlen1);

             /* Check for any errors */
         if (status == ERROR)
            printf("Error: BSD Server Socket receive\n");
         else
         {

             /* Print client information */
             printf("Server received data from Client at IP address 0x%x at port %lu\n", 
                    (UINT)fromAddr.sin_addr.s_addr, (ULONG)fromAddr.sin_port);

            /* Print the packet received from the client */
            printf("Server received from Client: %s\n", buffer);

            /* Now echo the recieved data string to the same client */
            status = sendto(sock_udp_2, buffer, (INT)(status + 1), 0, (struct sockaddr *) &fromAddr, sizeof(fromAddr));

            /* Check for any errors */
            if (status == ERROR)
                printf("Error:BSD Server Socket echo\n");
         }   

         tx_thread_sleep(NX_IP_PERIODIC_RATE);

        /* All done , loop back to recieve next packet */
    }  
}

/* Define the Client thread.  */
void    thread_client_entry(ULONG thread_input)
{

INT          status,sock_udp_1;                                 
INT          addrlen;
struct       sockaddr_in destAddr;                       /* Destination address */
struct       sockaddr_in ClientAddr;                     /* Client address */
struct       sockaddr_in fromAddr;                       /* Cleint address */
CHAR         echomsg[64];                                /* Message echoed by the server */

    NX_PARAMETER_NOT_USED(thread_input);
    
    /* Allow Server side to set up */
    tx_thread_sleep(10);
    thread_2_counter =0;
    
    while(1)
    {
         
         /* Create a BSD UDP Client Socket */
         sock_udp_1 = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
         /* Check for any errors */
         if (sock_udp_1 == ERROR)
         {
             printf("Error: BSD UDP Client socket create\n");
             return;
         }
         
         /* Set up client side */
         memset(&ClientAddr, 0, sizeof(ClientAddr));
         ClientAddr.sin_family = PF_INET;
         ClientAddr.sin_addr.s_addr = htonl(IP_ADDRESS(1,2,3,4));
    
         /* Assign a known Server port */
         memset(&destAddr, 0, sizeof(destAddr));
         destAddr.sin_addr.s_addr = htonl(IP_ADDRESS(1,2,3,4));
         destAddr.sin_port = htons(SERVER_PORT);
         destAddr.sin_family = PF_INET;
    
         /* Client socket is ready now start sending packets */
         printf("BSD Client Socket (%lu) sending test message: %s\n", (ULONG)sock_udp_1, CLIENT_MESSAGE);
    
         status = sendto(sock_udp_1, CLIENT_MESSAGE, (INT)(sizeof(CLIENT_MESSAGE)), 0,
                         (struct sockaddr *) &destAddr, sizeof(destAddr));

         /* Check for any errors */
         if (status == ERROR)
         {    

             printf("Error: BSD Client Socket send\n");
         }     
         else
         {
    
             addrlen = sizeof(struct sockaddr_in);
    
             /* Try to receive an echo from the server. */
             status = recvfrom(sock_udp_1,(VOID *)echomsg,64, 0, (struct sockaddr *) &fromAddr, &addrlen);
    
             /* * Check for any errors */
             if (status == ERROR)
             {    

                 printf("Error: BSD Client Socket echo receive\n");
             }     
             else
             {
    
                 /* Print Server detials */
                 printf("Client contacted Server at IP address 0x%x and port %lu\n", 
                        (UINT)fromAddr.sin_addr.s_addr, (ULONG)fromAddr.sin_port);

                 /* Print the received echo string from the server */
                 printf("Client (%lu) received echo string: %s\n", (ULONG)sock_udp_1, echomsg);
             }
         }   
    
         /* Done with this client socket */
         status = soc_close(sock_udp_1);
    
         /* Check for any errors */
         if (status == ERROR)
         {    
             printf("Error: BSD Client Socket close\n");
         } 
    
        /* Increment client transaction counter.  */
        thread_2_counter++;

        tx_thread_sleep(NX_IP_PERIODIC_RATE);       
    }
}
#endif /* NX_DISABLE_IPV4 */