Small demo to purely test UDP, depends on the module already being configured to auto connect to an access point

Dependencies:   USBDevice cc3000_hostdriver_mbedsocket mbed

Fork of WifiDipCortex-UDPDemo by Carl - SolderSplash Labs

Wifi-DipCortex - Test application that shows you how to listen for UDP message on one port while sending messages on another

- Listens on UDP_LISTEN_PORT, upon data being recvied it can be found in UDP_RecvBuffer - Transmits UDP_TX_Buffer on UDP_TARGET_PORT to UDP_TARGET_IP

main.cpp

Committer:
SolderSplashLabs
Date:
2014-07-09
Revision:
1:5fa4efb214e7
Parent:
0:ff40ad3448c5
Child:
2:6813713a64e1

File content as of revision 1:5fa4efb214e7:

/* ------------------------------------------------------------------------------------------------------------
    SolderSplash Labs - Wifi-DipCortex
    Test application that shows you how to listen for UDP message on one port while sending messages on another
    
    - Listens on UDP_LISTEN_PORT, upon data being recvied it can be found in UDP_RecvBuffer
    - Transmits UDP_TX_Buffer on UDP_TARGET_PORT to UDP_TARGET_IP
    
    Note : V1.28 firmware has an issue sending UDP data to 224.0.0.251
            http://forum.soldersplash.co.uk/viewtopic.php?f=15&t=97
----------------------------------------------------------------------------------------------------------- */

#include "mbed.h"
#include "cc3000.h"
#include "wifi.h"
#include "UDPSocket.h"
#include "USBSerial.h"

using namespace mbed_cc3000;

#define SERIAL_BAUD_RATE        115200
#define SOCKOPT_RECV_NONBLOCK   0

cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37));
Serial uart(p19, p20);

// USB CDC serial port
USBSerial pc;

const char UDP_TX_Buffer[] = {0,0,0,0,0,1,0,0,0,0,0,0,6,0x61,0x61,0x61,0x61,0x61,0x61,5,0x6c,0x6f,0x63,0x61,0x6c,0,0,1,0,1};

// Multicast address - this causes issues with firmware version 1.28
//const char* UDP_TARGET_IP = "224.0.0.251";

// This is the broadcast address for the subnet 192.168.0.x
const char* UDP_TARGET_IP = "192.168.0.255";

const int UDP_TARGET_PORT = 5353;
UDPSocket UDP_TransmitSocket;

const int UDP_LISTEN_PORT = 11028;
UDPSocket UDP_RecvSocket;

#define UDP_RECV_BUF_LEN 512
uint8_t UDP_RecvBuffer[ UDP_RECV_BUF_LEN ];

// ------------------------------------------------------------------------------------------------------------
/*!
    @brief WaitForConnection
*/
// ------------------------------------------------------------------------------------------------------------
void WaitForConnection ( void )
{
tNetappIpconfigRetArgs ipinfo;
    
    while (! ( wifi.is_dhcp_configured() ) )
    {
        // Still not connected
        pc.printf("No Connection\r\n");
        wait(1);
    }
    
    if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() ))
    {
        wifi.get_ip_config(&ipinfo);
    }
    
    pc.printf("Connected to (%s) %s \r\n", ipinfo.uaSSID, wifi.getIPAddress());

}

// ------------------------------------------------------------------------------------------------------------
/*!
    @brief WaitForUser
*/
// ------------------------------------------------------------------------------------------------------------
void WaitForUser ( void )
{
char charIn;

    pc.printf("Press Enter to Continue\r\n");
    
    while (1)
    {
        charIn = pc.getc();
        
        pc.printf("%c", charIn);
        if ((charIn == '\n') || (charIn == '\r'))
        {
            break;
        }
    }
}

// ------------------------------------------------------------------------------------------------------------
/*!
    @brief UdpTx_Init
*/
// ------------------------------------------------------------------------------------------------------------
void UdpTx_Init ( void )
{
    // Creating the socket once and then re-using it is quicker than creating it everytime
    // but it does tie up a 1 of the 4 sockets
    UDP_TransmitSocket.init();   
}

// ------------------------------------------------------------------------------------------------------------
/*!
    @brief UdpTx
*/
// ------------------------------------------------------------------------------------------------------------
void UdpTx ( void )
{
Endpoint target;
int returnVal;
 
 
    target.set_address(UDP_TARGET_IP, UDP_TARGET_PORT);
    
    pc.printf("Sending UDP Message ..\r\n");
    
    returnVal = UDP_TransmitSocket.sendTo(target, (char *)UDP_TX_Buffer, sizeof(UDP_TX_Buffer));
        
    if ( returnVal < 0 )
    {
        pc.printf("UdpTx : failed to send UDP message ReturnVal : %i\r\n", returnVal);
    }
    else
    {
         pc.printf("UDP Message Sent (%i)..\r\n", returnVal);
    }
}
    
// ------------------------------------------------------------------------------------------------------------
/*!
    @brief UdpRxTx_Init - Set up a non blocking UDP port for incoming messages
*/
// ------------------------------------------------------------------------------------------------------------
void UdpRx_Init ( void )
{
long optvalue_block = 0;

    // Note : This seems wrong but .. we are using a socket option that stops the cc3000 blocking
    // this lets us call recvfrom and not wait for a message, if it has any data it returns with it, if theres no data it returns immediately
    UDP_RecvSocket.set_blocking(true, 0);

    if ( 0 == UDP_RecvSocket.bind(UDP_LISTEN_PORT) )
    {
        // Socket bound to the port
        // Configure the module to not block when checking for UDP data
        UDP_RecvSocket.set_option(SOL_SOCKET, SOCKOPT_RECV_NONBLOCK, &optvalue_block, 1);
        pc.printf("UDP Socket open and listening\r\n");
    }
    else
    {
        pc.printf("Failed to bind to UDP port\r\n");
    }
}

// ------------------------------------------------------------------------------------------------------------
/*!
    @brief UdpRx
*/
// ------------------------------------------------------------------------------------------------------------
void UdpRx ( void )
{
Endpoint client;
int returnVal = 0;
    
    returnVal = UDP_RecvSocket.receiveFrom(client, (char *)UDP_RecvBuffer, UDP_RECV_BUF_LEN);
    
    if ( returnVal > 0 )
    {
        pc.printf("UDP Message Recv'd %i bytes from : %s \r\n", returnVal, client.get_address());
        
        // TODO : Process your UDP message here        
    }
}

// ------------------------------------------------------------------------------------------------------------
/*!
    @brief Init
*/
// ------------------------------------------------------------------------------------------------------------
void init() 
{
    NVIC_SetPriority(SSP1_IRQn, 0x0); 
    NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
    
    // SysTick set to lower priority than Wi-Fi SPI bus interrupt
    NVIC_SetPriority(SysTick_IRQn, 0x2); 
    
    // Enable RAM1
    LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26);
    
    uart.baud(SERIAL_BAUD_RATE);
    
    wait(1);
}

// ------------------------------------------------------------------------------------------------------------
/*!
    @brief main loop
*/
// ------------------------------------------------------------------------------------------------------------
int main( void ) 
{  
int tickCnt = 0;

    // Initalise the WiFi Module
    init(); 
    
    WaitForUser();
    
    pc.printf("Starting CC3000 module\r\n");
    
    wifi.start(0);
    
    WaitForConnection();
    
    // set up a lisening socket
    UdpRx_Init();
    UdpTx_Init();
    
    while (1)
    {        
        // Check for UDP coms, non blocking
        UdpRx();
        
        // TODO : Do other things, like service sensors etc..
        // For now lets simulate a task that lasts for 100ms
        wait(0.1);
        pc.printf(".");
        
        if ( tickCnt > 100 )
        {
            tickCnt = 0;
            // And then send a UDP message
            UdpTx();
        }
        tickCnt ++;
    }
   
}