User`s guide

KwikNet Low Level Services
K
A
DAK
177
...continued
Example #include "kn_lib.h"
CJ_ID udptaskid; /* UDP Task id */
volatile int udpresult; /* Result passed to task */
struct knx_udpmsg *udpmsgp; /* Saved UDP message pointer */
/* Application UDP callback function */
int myUDPfn(struct knx_udpmsg *msgp, void *userp)
{
if ((long)userp != 0xFEEDFACEL)
return (-1); /* Not my UDP datagram */
udpmsgp = msgp; /* Save the message pointer */
udpresult = 0; /* Got a response */
cjtkwake(udptaskid); /* Let UDP Task resume */
return (0); /* Accept the message */
}
/* Application task which sends and receives UDP datagrams*/
void myUDPtask(void)
{
struct in_addr fhost; /* Foreign host IPv4 address */
unsigned long handle; /* UDP channel handle */
char *dp; /* Data pointer */
udptaskid = cjtkid(); /* Provide my task id */
kn_inet_addr("192.168.5.12", &fhost); /* Destination */
kn_inet_addr("192.168.5.62", &lhost); /* Local address */
if ( (handle = kn_udpopen(&fhost,
45, /* Foreign port */
&lhost, /* Local host */
43, /* Local port */
myUDPfn, (void *)0xFEEDFACEL)) == 0)
return; /* Cannot open UDP channel */
udpresult = -1;
dp = "KwikNet is asking for a UDP response.\n";
if (kn_udpsend(handle, &fhost, 45, dp, strlen(dp)) == 0)
{
/* Wait 60 seconds for response */
if (udpresult == -1) /* Reply may be here already */
cjtkwaitm(cjtmconvert(60000L));
if (udpresult == 0)
{
kn_dprintf(0, "Got UDP reply: %s.\n",
udpmsgp->xudpm_datap;);
kn_udpfree(udpmsgp); /* Free the message */
}
}
kn_udpclose(handle); /* Close the UDP channel */
}