- Added setBaud() function - Added CheckNetworkStatus() function - Improved messaging system

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WiflyInterface by Components

Committer:
defrost
Date:
Fri Apr 01 00:55:36 2016 +0000
Revision:
26:eaaedb036df1
Parent:
25:36b2d76ca8d9
Child:
27:208b4cf69b44
- Added setBaud() function to TCPSocket Connection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 #include "WiflyInterface.h"
samux 1:fb4494783863 2
defrost 26:eaaedb036df1 3 //#define DEBUG
defrost 26:eaaedb036df1 4 #define INFOMESSAGES
defrost 25:36b2d76ca8d9 5 //Debug is disabled by default
defrost 25:36b2d76ca8d9 6 #ifdef DEBUG
defrost 25:36b2d76ca8d9 7 #define DBG(x, ...) pc.printf("[WiflyInterface : DBG] "x"\r\n", ##__VA_ARGS__);
defrost 25:36b2d76ca8d9 8 #define WARN(x, ...) pc.printf("[WiflyInterface : WARN] "x"\r\n", ##__VA_ARGS__);
defrost 25:36b2d76ca8d9 9 #define ERR(x, ...) pc.printf("[WiflyInterface : ERR] "x"\r\n", ##__VA_ARGS__);
defrost 25:36b2d76ca8d9 10 #else
defrost 25:36b2d76ca8d9 11 #define DBG(x, ...)
defrost 25:36b2d76ca8d9 12 #define WARN(x, ...)
defrost 25:36b2d76ca8d9 13 #define ERR(x, ...)
defrost 25:36b2d76ca8d9 14 #endif
defrost 25:36b2d76ca8d9 15
defrost 26:eaaedb036df1 16 #ifdef INFOMESSAGES
defrost 25:36b2d76ca8d9 17 #define INFO(x, ...) pc.printf("[WiflyInterface : INFO] "x"\r\n", ##__VA_ARGS__);
defrost 25:36b2d76ca8d9 18 #else
defrost 25:36b2d76ca8d9 19 #define INFO(x, ...)
defrost 25:36b2d76ca8d9 20 #endif
defrost 25:36b2d76ca8d9 21
samux 1:fb4494783863 22 WiflyInterface::WiflyInterface( PinName tx, PinName rx, PinName reset, PinName tcp_status,
samux 1:fb4494783863 23 const char * ssid, const char * phrase, Security sec) :
samux 1:fb4494783863 24 Wifly(tx, rx, reset, tcp_status, ssid, phrase, sec)
samux 1:fb4494783863 25 {
samux 1:fb4494783863 26 ip_set = false;
samux 1:fb4494783863 27 }
samux 1:fb4494783863 28
samux 1:fb4494783863 29 int WiflyInterface::init()
samux 1:fb4494783863 30 {
samux 1:fb4494783863 31 state.dhcp = true;
samux 1:fb4494783863 32 reset();
defrost 25:36b2d76ca8d9 33 INFO("WiflyInterface Initialized.");
samux 1:fb4494783863 34 return 0;
samux 1:fb4494783863 35 }
samux 1:fb4494783863 36
samux 1:fb4494783863 37 int WiflyInterface::init(const char* ip, const char* mask, const char* gateway)
samux 1:fb4494783863 38 {
samux 1:fb4494783863 39 state.dhcp = false;
samux 1:fb4494783863 40 this->ip = ip;
samux 1:fb4494783863 41 strcpy(ip_string, ip);
samux 1:fb4494783863 42 ip_set = true;
samux 1:fb4494783863 43 this->netmask = mask;
samux 1:fb4494783863 44 this->gateway = gateway;
samux 1:fb4494783863 45 reset();
samux 1:fb4494783863 46
samux 1:fb4494783863 47 return 0;
samux 1:fb4494783863 48 }
samux 1:fb4494783863 49
samux 1:fb4494783863 50 int WiflyInterface::connect()
samux 1:fb4494783863 51 {
defrost 13:8846f12fa277 52 // join() returns a boolean, it does not like it all the time, thus casting it as int
defrost 14:5a9561156acc 53 int ii = (int) join();
defrost 25:36b2d76ca8d9 54 INFO("join() complete, return value: %d",ii);
defrost 14:5a9561156acc 55 return ii;
samux 1:fb4494783863 56 }
samux 1:fb4494783863 57
samux 1:fb4494783863 58 int WiflyInterface::disconnect()
samux 1:fb4494783863 59 {
samux 1:fb4494783863 60 return Wifly::disconnect();
samux 1:fb4494783863 61 }
samux 1:fb4494783863 62
samux 1:fb4494783863 63 char * WiflyInterface::getIPAddress()
samux 1:fb4494783863 64 {
samux 1:fb4494783863 65 char * match = 0;
samux 1:fb4494783863 66 if (!ip_set) {
samux 1:fb4494783863 67 if (!sendCommand("get ip a\r", NULL, ip_string))
samux 1:fb4494783863 68 return NULL;
samux 1:fb4494783863 69 exit();
samux 1:fb4494783863 70 flush();
samux 1:fb4494783863 71 match = strstr(ip_string, "<");
samux 1:fb4494783863 72 if (match != NULL) {
samux 1:fb4494783863 73 *match = '\0';
samux 1:fb4494783863 74 }
samux 1:fb4494783863 75 if (strlen(ip_string) < 6) {
samux 1:fb4494783863 76 match = strstr(ip_string, ">");
samux 1:fb4494783863 77 if (match != NULL) {
samux 1:fb4494783863 78 int len = strlen(match + 1);
samux 1:fb4494783863 79 memcpy(ip_string, match + 1, len);
samux 1:fb4494783863 80 }
samux 1:fb4494783863 81 }
samux 1:fb4494783863 82 ip_set = true;
samux 1:fb4494783863 83 }
samux 1:fb4494783863 84 return ip_string;
samux 1:fb4494783863 85 }