ThingPlug Test

Dependents:   WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P

Fork of WizFi310Interface by WIZnet

Committer:
cliff1
Date:
Fri Aug 25 00:55:43 2017 +0000
Revision:
7:b7019399eb1e
Parent:
1:16e57103a7dd
20170825 ThingPlug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jehoon 0:df571f8f8c03 1 /*
jehoon 0:df571f8f8c03 2 * Copyright (C) 2013 gsfan, MIT License
jehoon 0:df571f8f8c03 3 *
jehoon 0:df571f8f8c03 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jehoon 0:df571f8f8c03 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jehoon 0:df571f8f8c03 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jehoon 0:df571f8f8c03 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jehoon 0:df571f8f8c03 8 * furnished to do so, subject to the following conditions:
jehoon 0:df571f8f8c03 9 *
jehoon 0:df571f8f8c03 10 * The above copyright notice and this permission notice shall be included in all copies or
jehoon 0:df571f8f8c03 11 * substantial portions of the Software.
jehoon 0:df571f8f8c03 12 *
jehoon 0:df571f8f8c03 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jehoon 0:df571f8f8c03 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jehoon 0:df571f8f8c03 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jehoon 0:df571f8f8c03 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jehoon 0:df571f8f8c03 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jehoon 0:df571f8f8c03 18 */
jehoon 0:df571f8f8c03 19 /* Copyright (C) 2014 Wiznet, MIT License
jehoon 0:df571f8f8c03 20 * port to the Wiznet Module WizFi310
jehoon 0:df571f8f8c03 21 */
jehoon 0:df571f8f8c03 22
jehoon 0:df571f8f8c03 23 #include "mbed.h"
jehoon 0:df571f8f8c03 24 #include "WizFi310.h"
jehoon 0:df571f8f8c03 25
jehoon 0:df571f8f8c03 26 WizFi310 * WizFi310::_inst;
jehoon 0:df571f8f8c03 27
jehoon 0:df571f8f8c03 28
jehoon 0:df571f8f8c03 29 WizFi310::WizFi310(PinName tx,PinName rx,PinName cts, PinName rts,PinName reset, PinName alarm, int baud):
jehoon 0:df571f8f8c03 30 _wizfi(tx,rx), _reset(reset)
jehoon 0:df571f8f8c03 31 {
jehoon 0:df571f8f8c03 32 _inst = this;
jehoon 0:df571f8f8c03 33 memset(&_state, 0, sizeof(_state));
jehoon 0:df571f8f8c03 34 memset(&_con, 0, sizeof(_con));
jehoon 0:df571f8f8c03 35 _state.initialized = false;
jehoon 0:df571f8f8c03 36 _state.status = STAT_READY;
jehoon 0:df571f8f8c03 37 _state.cid = -1;
jehoon 0:df571f8f8c03 38 _state.buf = new CircBuffer<char>(CFG_DATA_SIZE);
jehoon 0:df571f8f8c03 39
jehoon 0:df571f8f8c03 40 initUart(cts, rts, alarm, baud);
jehoon 0:df571f8f8c03 41 _reset.output();
jehoon 0:df571f8f8c03 42
jehoon 0:df571f8f8c03 43 setRts(true); // release
jehoon 0:df571f8f8c03 44 /*
jehoon 0:df571f8f8c03 45 wait_ms(500);
jehoon 0:df571f8f8c03 46 cmdAT();
jehoon 0:df571f8f8c03 47 cmdMECHO(false);
jehoon 0:df571f8f8c03 48 if(cts != NC && rts != NC)
jehoon 0:df571f8f8c03 49 cmdUSET(baud,"HW");
jehoon 0:df571f8f8c03 50 else
jehoon 0:df571f8f8c03 51 cmdUSET(baud,"N");
jehoon 0:df571f8f8c03 52
jehoon 0:df571f8f8c03 53 // WizFi310 will restart by cmdUSET command.
jehoon 0:df571f8f8c03 54 wait_ms(1000);
jehoon 0:df571f8f8c03 55 cmdAT();
jehoon 0:df571f8f8c03 56 */
jehoon 0:df571f8f8c03 57 }
jehoon 0:df571f8f8c03 58
jehoon 0:df571f8f8c03 59 int WizFi310::join(WiFiMode mode)
jehoon 0:df571f8f8c03 60 {
jehoon 0:df571f8f8c03 61 char sec[10];
jehoon 0:df571f8f8c03 62
jehoon 0:df571f8f8c03 63 if( cmdMMAC() ) return -1;
jehoon 0:df571f8f8c03 64
jehoon 0:df571f8f8c03 65 if(mode == WM_AP)
jehoon 0:df571f8f8c03 66 _state.wm = WM_AP;
jehoon 0:df571f8f8c03 67 else
jehoon 0:df571f8f8c03 68 _state.wm = WM_STATION;
jehoon 0:df571f8f8c03 69
jehoon 0:df571f8f8c03 70 if ( cmdWNET(_state.dhcp) ) return -1;
jehoon 0:df571f8f8c03 71 if ( cmdWSET(_state.wm, _state.ssid) ) return -1;
jehoon 0:df571f8f8c03 72
jehoon 0:df571f8f8c03 73
jehoon 0:df571f8f8c03 74 switch (_state.sec)
jehoon 0:df571f8f8c03 75 {
jehoon 0:df571f8f8c03 76 case NSAPI_SECURITY_NONE:
jehoon 0:df571f8f8c03 77 strcpy(sec,"OPEN");
jehoon 0:df571f8f8c03 78 break;
jehoon 0:df571f8f8c03 79 case NSAPI_SECURITY_WEP:
jehoon 0:df571f8f8c03 80 strcpy(sec,"WEP");
jehoon 0:df571f8f8c03 81 break;
jehoon 0:df571f8f8c03 82 case NSAPI_SECURITY_WPA:
jehoon 0:df571f8f8c03 83 strcpy(sec,"WPA");
jehoon 0:df571f8f8c03 84 break;
jehoon 0:df571f8f8c03 85 case NSAPI_SECURITY_WPA2:
jehoon 0:df571f8f8c03 86 strcpy(sec,"WPA2");
jehoon 0:df571f8f8c03 87 break;
jehoon 0:df571f8f8c03 88 default:
jehoon 0:df571f8f8c03 89 strcpy(sec,"");
jehoon 0:df571f8f8c03 90 break;
jehoon 0:df571f8f8c03 91 }
jehoon 0:df571f8f8c03 92
jehoon 0:df571f8f8c03 93 if ( cmdWSEC(_state.wm, _state.pass, sec) ) return -1;
jehoon 0:df571f8f8c03 94 if ( cmdWJOIN() ) return -1;;
jehoon 0:df571f8f8c03 95 _state.associated = true;
jehoon 0:df571f8f8c03 96
jehoon 0:df571f8f8c03 97 return 0;
jehoon 0:df571f8f8c03 98 }
jehoon 0:df571f8f8c03 99
jehoon 0:df571f8f8c03 100 bool WizFi310::isAssociated()
jehoon 0:df571f8f8c03 101 {
jehoon 0:df571f8f8c03 102 return _state.associated;
jehoon 0:df571f8f8c03 103 }
jehoon 0:df571f8f8c03 104
jehoon 0:df571f8f8c03 105 int WizFi310::setMacAddress (const char *mac)
jehoon 0:df571f8f8c03 106 {
jehoon 0:df571f8f8c03 107 if (cmdMMAC(mac)) return -1;
jehoon 0:df571f8f8c03 108 strncpy(_state.mac, mac, sizeof(_state.mac));
jehoon 0:df571f8f8c03 109 return 0;
jehoon 0:df571f8f8c03 110 }
jehoon 0:df571f8f8c03 111
jehoon 0:df571f8f8c03 112 int WizFi310::getMacAddress (char *mac)
jehoon 0:df571f8f8c03 113 {
jehoon 0:df571f8f8c03 114 if (cmdMMAC()) return -1;
jehoon 0:df571f8f8c03 115 strcpy(mac, _state.mac);
jehoon 0:df571f8f8c03 116 return 0;
jehoon 0:df571f8f8c03 117 }
jehoon 0:df571f8f8c03 118
jehoon 0:df571f8f8c03 119 int WizFi310::setAddress (const char *name)
jehoon 0:df571f8f8c03 120 {
jehoon 0:df571f8f8c03 121 _state.dhcp = true;
jehoon 0:df571f8f8c03 122 strncpy(_state.name, name, sizeof(_state.name));
jehoon 0:df571f8f8c03 123 return 0;
jehoon 0:df571f8f8c03 124 }
jehoon 0:df571f8f8c03 125
jehoon 0:df571f8f8c03 126 int WizFi310::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name)
jehoon 0:df571f8f8c03 127 {
jehoon 0:df571f8f8c03 128 _state.dhcp = false;
jehoon 0:df571f8f8c03 129 strncpy(_state.ip, ip, sizeof(_state.ip));
jehoon 0:df571f8f8c03 130 strncpy(_state.netmask, netmask, sizeof(_state.netmask));
jehoon 0:df571f8f8c03 131 strncpy(_state.gateway, gateway, sizeof(_state.gateway));
jehoon 0:df571f8f8c03 132 strncpy(_state.nameserver, dns, sizeof(_state.nameserver));
jehoon 0:df571f8f8c03 133 strncpy(_state.name, name, sizeof(_state.name));
jehoon 0:df571f8f8c03 134 return 0;
jehoon 0:df571f8f8c03 135 }
jehoon 0:df571f8f8c03 136
jehoon 0:df571f8f8c03 137 int WizFi310::getAddress (char *ip, char *netmask, char *gateway)
jehoon 0:df571f8f8c03 138 {
jehoon 0:df571f8f8c03 139 strcpy(ip, _state.ip);
jehoon 0:df571f8f8c03 140 strcpy(netmask, _state.netmask);
jehoon 0:df571f8f8c03 141 strcpy(gateway, _state.gateway);
jehoon 0:df571f8f8c03 142 return 0;
jehoon 0:df571f8f8c03 143 }
jehoon 0:df571f8f8c03 144
jehoon 0:df571f8f8c03 145 int WizFi310::setSsid (const char *ssid)
jehoon 0:df571f8f8c03 146 {
jehoon 0:df571f8f8c03 147 strncpy(_state.ssid, ssid, sizeof(_state.ssid));
jehoon 0:df571f8f8c03 148 return 0;
jehoon 0:df571f8f8c03 149 }
jehoon 0:df571f8f8c03 150
jehoon 0:df571f8f8c03 151 //daniel
jehoon 0:df571f8f8c03 152 //int WizFi310::setSec ( Security sec, const char *phrase )
jehoon 0:df571f8f8c03 153 int WizFi310::setSec ( nsapi_security_t sec, const char *phrase )
jehoon 0:df571f8f8c03 154 {
jehoon 0:df571f8f8c03 155 _state.sec = sec;
jehoon 0:df571f8f8c03 156 strncpy(_state.pass, phrase, strlen(phrase));
jehoon 0:df571f8f8c03 157 return 0;
jehoon 0:df571f8f8c03 158 }
jehoon 0:df571f8f8c03 159
jehoon 0:df571f8f8c03 160 const char *WizFi310::getIPAddress(void)
jehoon 0:df571f8f8c03 161 {
jehoon 0:df571f8f8c03 162 return _state.ip;
jehoon 0:df571f8f8c03 163 }
jehoon 0:df571f8f8c03 164
jehoon 0:df571f8f8c03 165 const char *WizFi310::getMACAddress(void)
jehoon 0:df571f8f8c03 166 {
jehoon 0:df571f8f8c03 167 return _state.mac;
jehoon 0:df571f8f8c03 168 }
jehoon 0:df571f8f8c03 169
cliff1 7:b7019399eb1e 170
cliff1 7:b7019399eb1e 171 int WizFi310::joinTP(const char *clientId, const char *credentialId, const char *serviceId, const char *devId, const char *containerNm)
cliff1 7:b7019399eb1e 172 {
cliff1 7:b7019399eb1e 173 WIZ_INFO("ThingPlug Start\r\n");
cliff1 7:b7019399eb1e 174
cliff1 7:b7019399eb1e 175 if(cmdSKTPCON("1", clientId, credentialId, serviceId, devId)) return -1;
cliff1 7:b7019399eb1e 176 WIZ_INFO("ThingPlug Connected");
cliff1 7:b7019399eb1e 177
cliff1 7:b7019399eb1e 178 if(cmdSKTPDEVICE("1", devId)) return -1;
cliff1 7:b7019399eb1e 179 WIZ_INFO("Device Registered");
cliff1 7:b7019399eb1e 180
cliff1 7:b7019399eb1e 181 if(cmdSKTPCONTAINER("1", containerNm)) return -1;
cliff1 7:b7019399eb1e 182 WIZ_INFO("Created Container");
cliff1 7:b7019399eb1e 183
cliff1 7:b7019399eb1e 184 if(cmdSKTPSEND(containerNm, "010600000026020600000022")) return -1;
cliff1 7:b7019399eb1e 185 WIZ_INFO("Sending Data");
cliff1 7:b7019399eb1e 186
cliff1 7:b7019399eb1e 187 if(cmdSKTPCON("0", clientId, credentialId, serviceId, devId)) return -1;
cliff1 7:b7019399eb1e 188 WIZ_INFO("ThingPlug Disconnected");
cliff1 7:b7019399eb1e 189
cliff1 7:b7019399eb1e 190 return 0;
cliff1 7:b7019399eb1e 191 }