Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed nanoservice_client_1_12
Fork of Trenton_Switch_LPC1768_WIFLY by
Diff: main.cpp
- Revision:
- 17:8ca4a5801430
- Parent:
- 16:3fb612af0dc5
- Child:
- 18:eb1a194d60d9
diff -r 3fb612af0dc5 -r 8ca4a5801430 main.cpp
--- a/main.cpp Fri Apr 04 15:17:07 2014 +0000
+++ b/main.cpp Mon Apr 07 09:29:11 2014 +0000
@@ -16,11 +16,10 @@
#include "mbed.h"
#include "WiflyInterface.h"
#include "main.h"
-#include "rtos.h"
#include "nsdl_support.h"
-//#include "pressure_mat.h"
+#include "pressure_mat.h"
//Serial pc(USBTX, USBRX);
@@ -49,17 +48,7 @@
static const char* NSP_ADDRESS = "192.168.1.10"; /* Trenton BBB NSP */
static const int NSP_PORT = 5683;
-/* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
-static void exec_call_thread(void const *args)
-{
- int32_t time = 0;
- while (true)
- {
- wait(1);
- time++;
- sn_nsdl_exec(time);
- }
-}
+
// ****************************************************************************
// NSP initialization
@@ -104,7 +93,7 @@
nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "KL46Z Welcome Mat", sizeof("KL46Z Welcome Mat")-1);
// Dynamic resources
- //create_pressure_mat_resource(resource_ptr);
+ create_pressure_mat_resource(resource_ptr);
/* Register with NSP */
endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
@@ -120,6 +109,96 @@
return 1;
}
+/* The number of seconds between NSDL Ticks*/
+#define NSDL_TICK_PERIOD 1
+/* The number of seconds between NSP registration messages */
+#define RD_UPDATE_PERIOD 300
+static void registration_update_thread(void const *args)
+{
+ sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
+
+ while(true)
+ {
+ wait(RD_UPDATE_PERIOD);
+ printf("NSP attempt re-register...\r\n");
+ endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
+ if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
+ printf("NSP re-registering failed\r\n");
+ else
+ printf("NSP re-registering OK\r\n");
+ nsdl_clean_register_endpoint(&endpoint_ptr);
+ }
+}
+
+void nsdl_event_loop()
+{
+ //Thread registration_thread(registration_update_thread);
+
+ //For timing control
+ Timer nsdlTickTimer;
+ Timer registrationTimer;
+
+ //Re-registration
+ sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
+
+ //For recieving NSP messages
+ sn_nsdl_addr_s received_packet_address;
+ uint8_t received_address[4];
+ int8_t nsdl_result = 0;
+ char buffer[1024];
+ Endpoint from;
+ memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
+ received_packet_address.addr_ptr = received_address;
+ server.set_blocking(false, 1500);
+
+ //Check incoming socket...
+ int n = 0;
+ int32_t time = 0;
+ nsdlTickTimer.start();
+ registrationTimer.start();
+ while(true)
+ {
+ //Wifly UDP Packet Receive...
+ n = server.receiveFrom(from, buffer, sizeof(buffer));
+ if (n < 0)
+ {
+ //No Data
+ //printf("Socket error\n\r");
+ }
+ else
+ {
+ //UDP
+ //wait(0.25); //Waiting seems to increase reliability of comms...
+ printf("Received %d bytes\r\n", n);
+ nsdl_result = sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
+ printf("Processed COAP Packet: %d\r\n", nsdl_result);
+ n = 0;
+ }
+
+ //Check if need to send pressure mat update...
+ pressure_mat_report();
+
+ //NSDL Tick
+ if(nsdlTickTimer.read() >= NSDL_TICK_PERIOD) {
+ sn_nsdl_exec(time);
+ nsdlTickTimer.reset();
+ }
+
+ //Registration Tick
+ if(registrationTimer.read() >= RD_UPDATE_PERIOD) {
+ printf("NSP attempt re-register...\r\n");
+ endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
+ if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
+ printf("NSP re-registering failed\r\n");
+ else
+ printf("NSP re-registering OK\r\n");
+ nsdl_clean_register_endpoint(&endpoint_ptr);
+ registrationTimer.reset();
+ }
+ }
+
+
+}
/**
@@ -146,11 +225,6 @@
// Create resources & register with NSP
create_resources();
-
- //Create the NSDL exec thread
- //static Thread exec_thread(exec_call_thread);
-
// Run the NSDL event loop (never returns)
nsdl_event_loop();
-
}
