Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Network.ping() #156

Merged
merged 1 commit into from
Apr 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions inc/spark_wiring_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class NetworkClass
IPAddress gatewayIP();
char* SSID();
int8_t RSSI();
uint32_t ping(IPAddress remoteIP);
uint32_t ping(IPAddress remoteIP, uint8_t nTries);

friend class TCPClient;
friend class TCPServer;
Expand Down
2 changes: 2 additions & 0 deletions inc/spark_wlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ extern int Spark_Process_API_Response(void);
extern volatile uint32_t TimingFlashUpdateTimeout;

extern tNetappIpconfigRetArgs ip_config;
extern netapp_pingreport_args_t ping_report;
extern int ping_report_num;

extern volatile uint8_t WLAN_DHCP;
extern volatile uint8_t SPARK_WLAN_SETUP;
Expand Down
24 changes: 24 additions & 0 deletions src/spark_wiring_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,28 @@ int8_t NetworkClass::RSSI()
the same way.
*****************************************************************************/

uint32_t NetworkClass::ping(IPAddress remoteIP)
{
return ping(remoteIP, 5);
}

uint32_t NetworkClass::ping(IPAddress remoteIP, uint8_t nTries)
{
uint32_t result = 0;
uint32_t pingIPAddr = remoteIP[3] << 24 | remoteIP[2] << 16 | remoteIP[1] << 8 | remoteIP[0];
unsigned long pingSize = 32UL;
unsigned long pingTimeout = 500UL; // in milliseconds

memset(&ping_report,0,sizeof(netapp_pingreport_args_t));
ping_report_num = 0;

long psend = netapp_ping_send(&pingIPAddr, (unsigned long)nTries, pingSize, pingTimeout);
unsigned long lastTime = millis();
while( ping_report_num==0 && (millis() < lastTime+2*nTries*pingTimeout)) {}
if (psend==0L && ping_report_num) {
result = ping_report.packets_received;
}
return result;
}

NetworkClass Network;
11 changes: 9 additions & 2 deletions src/spark_wlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ static uint32_t lastEvent = 0;
#define ON_EVENT_DELTA()
#endif
tNetappIpconfigRetArgs ip_config;
netapp_pingreport_args_t ping_report;
int ping_report_num;

volatile uint8_t WLAN_MANUAL_CONNECT = 0; //For Manual connection, set this to 1
volatile uint8_t WLAN_DELETE_PROFILES;
Expand Down Expand Up @@ -266,7 +268,7 @@ void Start_Smart_Config(void)
LED_On(LED_RGB);

/* Mask out all non-required events */
wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_ASYNC_PING_REPORT);
wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_UNSOL_INIT);

Set_NetApp_Timeout();

Expand Down Expand Up @@ -341,6 +343,11 @@ void WLAN_Async_Callback(long lEventType, char *data, unsigned char length)
WLAN_CAN_SHUTDOWN = 1;
break;

case HCI_EVNT_WLAN_ASYNC_PING_REPORT:
memcpy(&ping_report,data,length);
ping_report_num++;
break;

case HCI_EVNT_BSD_TCP_CLOSE_WAIT:
long socket = -1;
STREAM_TO_UINT32(data,0,socket);
Expand Down Expand Up @@ -403,7 +410,7 @@ void SPARK_WLAN_Setup(void (*presence_announcement_callback)(void))
SPARK_LED_FADE = 0;

/* Mask out all non-required events from CC3000 */
wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_ASYNC_PING_REPORT);
wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_UNSOL_INIT);

if(NVMEM_SPARK_Reset_SysFlag == 0x0001 || nvmem_read(NVMEM_SPARK_FILE_ID, NVMEM_SPARK_FILE_SIZE, 0, NVMEM_Spark_File_Data) != NVMEM_SPARK_FILE_SIZE)
{
Expand Down