Damien Frost / WiflyInterfaceMessaging

Fork of WiflyInterface by Damien Frost

Committer:
defrost
Date:
Thu Mar 31 16:22:37 2016 +0000
Revision:
25:36b2d76ca8d9
Parent:
14:5a9561156acc
Child:
26:eaaedb036df1
- Removed all printfs; - changed printing of messages to pc.printf, from std::printf

Who changed what in which revision?

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