Implementation of the WifiPlusClick hardware module.

Dependents:   WifiPlusKlickExample

WifiPlusClick Libary

Overview

http://www.mikroe.com/img/development-tools/accessory-boards/click/wifi-plus/wifi_plus_click_main.png

This library implements the functionality exposed by a WifiPlusClick module from MikroElektronika (http://www.mikroe.com/click/wifi-plus/).

The WifiPlusClick module is an easy to handle module which provides access to up to 8 simultaneous socket objects - which is an an important aspect when you want to implement your own web server.

When I first started with the more commonly used Wifly module, I found out that the Wifly module does not reliably serve webpages which include other resources like images, JavaScript files or CSS files. The root cause seems to be the limitation that Wifly is only able to handle a single socket at this time. So I searched for an alternative and found this (actually cheaper) alternative :

WifiPlusClick HW Module

This module comes with its own limitations. The WifiPlusClick Module interface does not allow to use broadcasting or multicasting on UDP sockets. There are some additional limitations, but I think these are not so important. The following functionality is provided by the module and my library implementation :

  1. Wifi functionality
    1. Connections using AD-HOC or INFRASTRUCTURE mode
    2. List all available Wifi beacons
    3. WEP and WPA/WPA2 security modes including binary and ASCII keys
    4. reading binary WPA key after successfull Connection to speed up connection time
  2. Socket functionality
    1. UDP sockets
    2. TCP sockets

Limitations

I found the following limitations:

  1. UDP sockets cannot use multicasting or broadcasting
  2. set_option functionality is not provided by the HW
  3. 8 sockets can be configured with 1024 bytes of buffer each or 1 socket with 8192 bytes of buffer.

Sample application

Here is my sample application which you can use as a starting point.

Import programWifiPlusKlickExample

Example application of the WifiPlusClick library for use of WifiPlusClick HW Module from Mikroe.com

NOTE

The implementation of the Sockets in this library is still not completely tested. I only tested the TCP part of the sockets. Please let me know what your experiences are when using the library. I will be working on a multithreaded version of this library...

Committer:
leihen
Date:
Mon Jul 29 15:15:21 2013 +0000
Revision:
0:2a179bd4cc02
Initial Version of the WifiPlusClick Library.
; Tested in INFRASTRUCTURE mode only.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leihen 0:2a179bd4cc02 1 /* Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
leihen 0:2a179bd4cc02 2 *
leihen 0:2a179bd4cc02 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
leihen 0:2a179bd4cc02 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
leihen 0:2a179bd4cc02 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
leihen 0:2a179bd4cc02 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
leihen 0:2a179bd4cc02 7 * furnished to do so, subject to the following conditions:
leihen 0:2a179bd4cc02 8 *
leihen 0:2a179bd4cc02 9 * The above copyright notice and this permission notice shall be included in all copies or
leihen 0:2a179bd4cc02 10 * substantial portions of the Software.
leihen 0:2a179bd4cc02 11 *
leihen 0:2a179bd4cc02 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
leihen 0:2a179bd4cc02 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
leihen 0:2a179bd4cc02 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
leihen 0:2a179bd4cc02 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
leihen 0:2a179bd4cc02 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
leihen 0:2a179bd4cc02 17 */
leihen 0:2a179bd4cc02 18 #include "mbed.h"
leihen 0:2a179bd4cc02 19 #include "TCPSocketConnection.h"
leihen 0:2a179bd4cc02 20
leihen 0:2a179bd4cc02 21 TCPSocketConnection::TCPSocketConnection()
leihen 0:2a179bd4cc02 22 : _is_connected(false)
leihen 0:2a179bd4cc02 23 {
leihen 0:2a179bd4cc02 24 }
leihen 0:2a179bd4cc02 25
leihen 0:2a179bd4cc02 26 int TCPSocketConnection::connect(const char *host, const int port)
leihen 0:2a179bd4cc02 27 {
leihen 0:2a179bd4cc02 28 _socket = Wifi::getInstance()->SocketCreate(TCP);
leihen 0:2a179bd4cc02 29 if (_socket == InvalidSocketHandle)
leihen 0:2a179bd4cc02 30 return -1;
leihen 0:2a179bd4cc02 31
leihen 0:2a179bd4cc02 32 if (set_address(host, port) != 0)
leihen 0:2a179bd4cc02 33 return -1;
leihen 0:2a179bd4cc02 34
leihen 0:2a179bd4cc02 35 if (!Wifi::getInstance()->SocketConnect(_socket, reinterpret_cast<IPADDRESS_t*>(get_address()), port)) {
leihen 0:2a179bd4cc02 36 close();
leihen 0:2a179bd4cc02 37 return -1;
leihen 0:2a179bd4cc02 38 }
leihen 0:2a179bd4cc02 39 _is_connected = true;
leihen 0:2a179bd4cc02 40
leihen 0:2a179bd4cc02 41 return 0;
leihen 0:2a179bd4cc02 42 }
leihen 0:2a179bd4cc02 43
leihen 0:2a179bd4cc02 44 bool TCPSocketConnection::is_connected()
leihen 0:2a179bd4cc02 45 {
leihen 0:2a179bd4cc02 46 return _is_connected;
leihen 0:2a179bd4cc02 47 }
leihen 0:2a179bd4cc02 48
leihen 0:2a179bd4cc02 49 int TCPSocketConnection::send(char *data, int length)
leihen 0:2a179bd4cc02 50 {
leihen 0:2a179bd4cc02 51 if ((_socket == InvalidSocketHandle) || !_is_connected)
leihen 0:2a179bd4cc02 52 return -1;
leihen 0:2a179bd4cc02 53
leihen 0:2a179bd4cc02 54 int n = Wifi::getInstance()->SocketSend(_socket, data, length);
leihen 0:2a179bd4cc02 55
leihen 0:2a179bd4cc02 56 return n;
leihen 0:2a179bd4cc02 57 }
leihen 0:2a179bd4cc02 58
leihen 0:2a179bd4cc02 59 int TCPSocketConnection::send_all(char *data, int length)
leihen 0:2a179bd4cc02 60 {
leihen 0:2a179bd4cc02 61 if ((_socket == InvalidSocketHandle) || !_is_connected)
leihen 0:2a179bd4cc02 62 return -1;
leihen 0:2a179bd4cc02 63
leihen 0:2a179bd4cc02 64 int n = 0;
leihen 0:2a179bd4cc02 65
leihen 0:2a179bd4cc02 66 Timer t;
leihen 0:2a179bd4cc02 67 t.start();
leihen 0:2a179bd4cc02 68
leihen 0:2a179bd4cc02 69 while( n < length) {
leihen 0:2a179bd4cc02 70 int r = Wifi::getInstance()->SocketSend(_socket, &data[n], length-n);
leihen 0:2a179bd4cc02 71 if (r == -1) {
leihen 0:2a179bd4cc02 72 return n;
leihen 0:2a179bd4cc02 73 }
leihen 0:2a179bd4cc02 74 n += r;
leihen 0:2a179bd4cc02 75
leihen 0:2a179bd4cc02 76 if (_blocking && (t.read_ms() > _timeout)) {
leihen 0:2a179bd4cc02 77 break;
leihen 0:2a179bd4cc02 78 }
leihen 0:2a179bd4cc02 79 }
leihen 0:2a179bd4cc02 80
leihen 0:2a179bd4cc02 81 return n;
leihen 0:2a179bd4cc02 82 }
leihen 0:2a179bd4cc02 83
leihen 0:2a179bd4cc02 84 int TCPSocketConnection::recv(char *data, int length)
leihen 0:2a179bd4cc02 85 {
leihen 0:2a179bd4cc02 86 if ((_socket == InvalidSocketHandle) || !_is_connected)
leihen 0:2a179bd4cc02 87 return -1;
leihen 0:2a179bd4cc02 88
leihen 0:2a179bd4cc02 89 int n = Wifi::getInstance()->SocketRecv(_socket, data, length);
leihen 0:2a179bd4cc02 90
leihen 0:2a179bd4cc02 91 return n;
leihen 0:2a179bd4cc02 92 }
leihen 0:2a179bd4cc02 93
leihen 0:2a179bd4cc02 94 int TCPSocketConnection::recv_all(char *data, int length)
leihen 0:2a179bd4cc02 95 {
leihen 0:2a179bd4cc02 96 if ((_socket == InvalidSocketHandle) || !_is_connected)
leihen 0:2a179bd4cc02 97 return -1;
leihen 0:2a179bd4cc02 98
leihen 0:2a179bd4cc02 99 int n = 0;
leihen 0:2a179bd4cc02 100
leihen 0:2a179bd4cc02 101 Timer t;
leihen 0:2a179bd4cc02 102 t.start();
leihen 0:2a179bd4cc02 103
leihen 0:2a179bd4cc02 104 while( n < length) {
leihen 0:2a179bd4cc02 105 int r = Wifi::getInstance()->SocketRecv(_socket, &data[n], length-n);
leihen 0:2a179bd4cc02 106 if (r == -1) {
leihen 0:2a179bd4cc02 107 return n;
leihen 0:2a179bd4cc02 108 }
leihen 0:2a179bd4cc02 109 n += r;
leihen 0:2a179bd4cc02 110
leihen 0:2a179bd4cc02 111 if (_blocking && (t.read_ms() > _timeout)) {
leihen 0:2a179bd4cc02 112 break;
leihen 0:2a179bd4cc02 113 }
leihen 0:2a179bd4cc02 114 }
leihen 0:2a179bd4cc02 115
leihen 0:2a179bd4cc02 116 return n;
leihen 0:2a179bd4cc02 117 }