ThingPlug Test

Dependents:   WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P

Fork of WizFi310Interface by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi310Interface.cpp Source File

WizFi310Interface.cpp

00001 /* WizFi310 implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #include "WizFi310Interface.h"
00019 
00020 // Various timeouts for different WizFi310 operations
00021 #define WizFi310_CONNECT_TIMEOUT 15000
00022 #define WizFi310_SEND_TIMEOUT    500
00023 #define WizFi310_RECV_TIMEOUT    0
00024 #define WizFi310_MISC_TIMEOUT    500
00025 
00026 #define WizFi310_DELAY_MS       300
00027 
00028 
00029 // WizFi310Interface implementation
00030 WizFi310Interface::WizFi310Interface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm,  int baud)
00031     : _wizfi310(tx, rx, cts, rts, reset, alarm, baud)
00032 {
00033     memset(_ids, 0, sizeof(_ids));
00034 }
00035 
00036 int WizFi310Interface::connect(
00037     const char *ssid,
00038     const char *pass,
00039     nsapi_security_t security)
00040 {
00041     if (!_wizfi310.startup()) 
00042     {
00043         return NSAPI_ERROR_DEVICE_ERROR;
00044     }
00045 
00046     _wizfi310.setSsid(ssid);
00047     _wizfi310.setSec(security, pass);
00048     _wizfi310.setAddress("");
00049     
00050     if( _wizfi310.join(WizFi310::WM_STATION) == -1)
00051     {
00052         return NSAPI_ERROR_NO_CONNECTION;
00053     }
00054 
00055     return 0;
00056 }
00057 
00058 int WizFi310Interface::disconnect()
00059 {
00060     if ( _wizfi310.cmdWLEAVE() == -1 )  return NSAPI_ERROR_DEVICE_ERROR;
00061 
00062     return 0;
00063 }
00064 
00065 const char *WizFi310Interface::get_ip_address()
00066 {
00067     return _wizfi310.getIPAddress();
00068 }
00069 
00070 const char *WizFi310Interface::get_mac_address()
00071 {
00072     return _wizfi310.getMACAddress();
00073 }
00074 
00075 int WizFi310Interface::conTP(
00076     const char *clientId, 
00077     const char *credentialId, 
00078     const char *serviceId, 
00079     const char *devId, 
00080     const char *containerNm,
00081     const char *commandName
00082     )
00083 {
00084     //if ( _wizfi310.joinTP(clientId, credentialId, serviceId, devId, containerNm) == -1 ) return NSAPI_ERROR_NO_SOCKET;
00085     
00086     if(_wizfi310.cmdSKTPCON("1", clientId, credentialId, serviceId, devId))    return -1;
00087     WIZ_INFO("ThingPlug Connected");
00088     
00089     if(_wizfi310.cmdSKTPDEVICE("1", devId))    return -1;    
00090     WIZ_INFO("Device Registered");
00091     
00092     if(_wizfi310.cmdSKTPCONTAINER("1", containerNm))    return -1;
00093     WIZ_INFO("Created Container\r\n");
00094     
00095     if(_wizfi310.cmdSKTPCMD("1", commandName))    return -1;
00096     WIZ_INFO("Created CommandName\r\n");
00097     
00098     return 0;
00099 }
00100 
00101 int WizFi310Interface::sendTP(const char *containerNm, const char *sendData)
00102 {
00103     if(_wizfi310.cmdSKTPSEND(containerNm, sendData))    return -1;
00104     WIZ_INFO("Data sent\r\n");
00105     
00106     return 0;
00107 }
00108 
00109 int WizFi310Interface::recvTP(const char *commandName, int executeStatus, int executeResult)
00110 {
00111     int cnt, cid = 0, ret = 0;
00112     char buffer[1024] = "";
00113        
00114     if(_wizfi310.recv(cid, buffer, sizeof(buffer)))
00115     {
00116         //printf("%s\r\n", buffer);
00117         
00118         if(_wizfi310.cmdSKTPRESULT(commandName, executeStatus, executeResult))  
00119         {
00120             _wizfi310.initCon(cid, true);
00121             
00122             return -1;
00123         }
00124                 
00125         for(cnt = 0; cnt < sizeof(buffer); cnt++)
00126         {
00127             if( buffer[cnt] == '8' )
00128             {
00129                 if( buffer[cnt + 1] == '0' )
00130                     ret = 1;
00131                     
00132                 else if( buffer[cnt + 1] == '1' )
00133                     ret = 2;
00134             }
00135         }
00136         
00137         WIZ_INFO("Complete");
00138         _wizfi310.initCon(cid, true);
00139         
00140         return ret;
00141     }
00142     
00143     //WIZ_INFO("Incorrect Request\r\n");
00144     _wizfi310.initCon(cid, true);
00145     
00146     return -1;
00147 }
00148 
00149 int WizFi310Interface::disConTP()
00150 {   
00151     if(_wizfi310.cmdSKTPCON("0"))    return -1;
00152     WIZ_INFO("ThingPlug Disconnected\r\n");
00153     
00154     return 0;
00155 }
00156 
00157 struct wizfi310_socket {
00158     int id;
00159     nsapi_protocol_t proto;
00160     bool connected;
00161 };
00162 
00163 int WizFi310Interface::socket_open(void **handle, nsapi_protocol_t proto)
00164 {
00165     // Look for an unused socket
00166 
00167     /*
00168     int id = -1;
00169  
00170     for (int i = 0; i < WIZFI310_SOCKET_COUNT; i++) {
00171         if (_ids[i] == false) {
00172             id = i;
00173             _ids[i] = true;
00174             break;
00175         }
00176     }
00177  
00178     if (id == -1) {
00179         return NSAPI_ERROR_NO_SOCKET;
00180     }
00181     */
00182     
00183     struct wizfi310_socket *socket = new struct wizfi310_socket;
00184     if (!socket) {
00185         return NSAPI_ERROR_NO_SOCKET;
00186     }
00187     
00188     socket->id = -1;
00189     socket->proto = proto;
00190     socket->connected = false;
00191     *handle = socket;
00192     return 0;
00193 }
00194 
00195 int WizFi310Interface::socket_close(void *handle)
00196 {
00197     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00198     int err = 0;
00199  
00200     if(socket->id == -1){
00201         err = NSAPI_ERROR_NO_SOCKET;
00202     }
00203     else if (_wizfi310.close(socket->id) == -1) {
00204         err = NSAPI_ERROR_DEVICE_ERROR;
00205     }
00206 
00207     _ids[socket->id] = false;
00208     wait_ms(WizFi310_DELAY_MS);
00209     delete socket;
00210     return err;
00211 }
00212 
00213 int WizFi310Interface::socket_bind(void *handle, const SocketAddress &address)
00214 {
00215     return NSAPI_ERROR_UNSUPPORTED;
00216 }
00217 
00218 int WizFi310Interface::socket_listen(void *handle, int backlog)
00219 {
00220     return NSAPI_ERROR_UNSUPPORTED;
00221 }
00222 
00223 int WizFi310Interface::socket_connect(void *handle, const SocketAddress &addr)
00224 {
00225     int cid=-1;
00226     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00227 
00228     WizFi310::Protocol proto = (socket->proto == NSAPI_UDP) ? WizFi310::PROTO_UDP : WizFi310::PROTO_TCP;
00229     if((cid = _wizfi310.open(proto, addr.get_ip_address(), addr.get_port())) == -1 )
00230     {
00231         return NSAPI_ERROR_DEVICE_ERROR;
00232     }
00233 
00234     if(cid >= WIZFI310_SOCKET_COUNT)
00235     {
00236         return NSAPI_ERROR_NO_SOCKET;
00237     }
00238     
00239     _ids[cid] = true;
00240     socket->id = cid;
00241     socket->connected = true;
00242     wait_ms(WizFi310_DELAY_MS);
00243     return 0;
00244 }
00245     
00246 int WizFi310Interface::socket_accept(void **handle, void *server)
00247 {
00248     return NSAPI_ERROR_UNSUPPORTED;
00249 }
00250 
00251 int WizFi310Interface::socket_send(void *handle, const void *data, unsigned size)
00252 {
00253     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00254  
00255     if ( _wizfi310.send(socket->id, (const char*)data, size) == -1 ) {
00256         return NSAPI_ERROR_DEVICE_ERROR;
00257     }
00258  
00259     return size;
00260 }
00261 
00262 int WizFi310Interface::socket_recv(void *handle, void *data, unsigned size)
00263 {
00264     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00265  
00266     int32_t recv = _wizfi310.recv(socket->id, (char*)data, size);
00267     if (recv <= 0) {
00268         return NSAPI_ERROR_WOULD_BLOCK;
00269     }
00270  
00271     return recv;
00272 }
00273 
00274 int WizFi310Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
00275 {
00276     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00277     if (!socket->connected) {
00278         int err = socket_connect(socket, addr);
00279         if (err < 0) {
00280             return err;
00281         }
00282     }
00283     
00284     return socket_send(socket, data, size);
00285 }
00286 
00287 int WizFi310Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
00288 {
00289     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;    
00290     return socket_recv(socket, data, size);
00291 }
00292 
00293 void WizFi310Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
00294 {
00295 }