Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Servo X_NUCLEO_IKS01A2 X_NUCLEO_IDW01M1v2 NetworkSocketAPI NDefLib MQTT
X_NUCLEO_IDW01M1/SPWFInterface.cpp
- Committer:
- mridup
- Date:
- 2016-04-08
- Revision:
- 0:cbf8bc43bc9e
File content as of revision 0:cbf8bc43bc9e:
/* SpwfSAInterface implementation of NetworkInterfaceAPI
* Copyright (c) 2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SPWFInterface.h"
// Various timeouts for different SPWF operations
#define SPWF_CONNECT_TIMEOUT 20000
#define SPWF_SEND_TIMEOUT 500
#define SPWF_RECV_TIMEOUT 10000
#define SPWF_MISC_TIMEOUT 15000
/**
* The singleton which represents the SpwfSAInterface.
*
*/
static SpwfSAInterface spwf_device(PA_9, PA_10, PC_12, PC_8, PA_12, true);
/**
* Export the instance to the user.
*/
SpwfSAInterface *createSPWFInstance(void)
{
return (&spwf_device);
}
// SpwfSAInterface implementation
SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, PinName rst, PinName wkup, PinName rts, bool debug)
: _spwf(tx, rx, rst, wkup, rts)
{
memset(_ids, 0, sizeof(_ids));
}
SpwfSAInterface::~SpwfSAInterface()
{
}
int32_t SpwfSAInterface::init(void)
{
_spwf.setTimeout(SPWF_MISC_TIMEOUT);
return (_spwf.init());
}
int32_t SpwfSAInterface::connect(
const char *ap,
const char *pass_phrase,
ns_security_t security)
{
_spwf.setTimeout(SPWF_CONNECT_TIMEOUT);
if(security == NS_SECURITY_WPA2) return NS_ERROR_DEVICE_ERROR;
WiFi_Priv_Mode mode = (WiFi_Priv_Mode)security;
return (_spwf.connect((char*)ap, (char*)pass_phrase, mode));//0 on success
}
int32_t SpwfSAInterface::disconnect()
{
//_spwf.setTimeout(SPWF_SEND_TIMEOUT);
return (_spwf.disconnect());
}
const char *SpwfSAInterface::getIPAddress()
{
return _spwf.getIPAddress();
}
const char *SpwfSAInterface::getMACAddress()
{
return _spwf.getMACAddress();
}
void SpwfSAInterface::setid(bool set, int id)
{
if(set)
_ids[id] = true;
else
_ids[id] = false;
}
SocketInterface *SpwfSAInterface::createSocket(ns_protocol_t proto)
{
return new SpwfSASocket(this, &_spwf, proto);
}
void SpwfSAInterface::destroySocket(SocketInterface *iface)
{
SpwfSASocket *socket = (SpwfSASocket *)iface;
_ids[socket->_id] = false;
delete socket;
}
// SpwfSASocket implementation
int32_t SpwfSAInterface::SpwfSASocket::open(const char *ip, uint16_t port)
{
uint8_t sock_id = -1;
//__spwf->setTimeout(SPWF_SEND_TIMEOUT);
const char *proto = (_proto == NS_UDP) ? "u" : "t";//"s" for secure socket?
__spwf->socket_client_open((uint8_t*)ip, (uint32_t)port, (uint8_t *)proto, &sock_id);
//TODO: Maintain a socket table to map socket ID to host & port
//TODO: lookup on client table to see if already socket is allocated to same host/port
//multimap <char *, vector <uint16_t> > ::iterator i = c_table.find((char*)ip);
if(sock_id <= SPWFSA_SOCKET_COUNT)
{
_id = sock_id;//the socket ID of this Socket instance
_itf->setid(true, _id);
//_itf->c_table.insert(pair <char *, vector <uint16_t> > ((char*)ip, port));
}
else
return NS_ERROR_NO_SOCKET;
return 0;//0 means SUCCESS
}
int32_t SpwfSAInterface::SpwfSASocket::close()
{
int32_t err;
//__spwf->setTimeout(SPWF_SEND_TIMEOUT);
_itf->setid(false, _id);
//_itf->c_table.empty();
err = __spwf->socket_client_close((uint8_t)_id);
return err;
}
int32_t SpwfSAInterface::SpwfSASocket::send(const void *data, uint32_t size)
{
int32_t err;
//__spwf->setTimeout(SPWF_SEND_TIMEOUT);
err = __spwf->socket_client_write((uint8_t)_id, (uint16_t)size, (char*)data);
return err;
}
//return no of bytes read
int32_t SpwfSAInterface::SpwfSASocket::recv(void *data, uint32_t size)
{
int32_t recv;
__spwf->setTimeout(SPWF_RECV_TIMEOUT);
recv = __spwf->socket_client_recv((uint8_t)_id, (uint16_t)size, (char*)data);
return recv;
}
void SpwfSAInterface::debug(const char * string)
{
_spwf.debug_print(string);
}