mbed Sensor node for Instrumented Booth over ETH.

Dependencies:   EthernetInterface-1 MaxbotixDriver Presence HTU21D_TEMP_HUMID_SENSOR_SAMPLE Resources SHARPIR mbed-rtos mbed-src WDT_K64F nsdl_lib

Fork of Trenton_Switch_LPC1768_ETH by Demo Team

Files at this revision

API Documentation at this revision

Comitter:
erigow01
Date:
Fri Apr 04 15:17:07 2014 +0000
Parent:
15:59f4cee0da79
Child:
17:8ca4a5801430
Commit message:
Seems to be working relatively reliably.

Changed in this revision

LPD8806.lib Show diff for this revision Revisions of this file
NVIC_set_all_priorities.lib Show diff for this revision Revisions of this file
WiflyInterface.lib Show annotated file Show diff for this revision Revisions of this file
cc3000_hostdriver_mbedsocket.lib Show diff for this revision Revisions of this file
init.cpp Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
nsdl_support.cpp Show annotated file Show diff for this revision Revisions of this file
nsdl_support.h Show annotated file Show diff for this revision Revisions of this file
--- a/LPD8806.lib	Tue Mar 25 13:00:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-LPD8806#a4f1d497fc36
--- a/NVIC_set_all_priorities.lib	Tue Mar 25 13:00:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/frankvnk/code/NVIC_set_all_priorities/#01504ecd2025
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WiflyInterface.lib	Fri Apr 04 15:17:07 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/mbed-official/code/WiflyInterface/#c77799a03294
--- a/cc3000_hostdriver_mbedsocket.lib	Tue Mar 25 13:00:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/Kojto/code/cc3000_hostdriver_mbedsocket/#50ab13d8f2dc
--- a/init.cpp	Tue Mar 25 13:00:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,70 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "main.h"
-#include "mbed.h"
-
-#if (MY_BOARD == WIGO)
-
-#include "NVIC_set_all_priorities.h"
-
-/**
- *  \brief Wi-Go initialization
- *  \param none
- *  \return none
- */
-void init() {
-    DigitalOut PWR_EN1(PTB2);
-    DigitalOut PWR_EN2(PTB3);
-
-    // Wi-Go set current to 500mA since we're turning on the Wi-Fi
-    PWR_EN1 = 0;
-    PWR_EN2 = 1;
-
-    NVIC_set_all_irq_priorities(3);
-    NVIC_SetPriority(SPI0_IRQn, 0x0);     // Wi-Fi SPI interrupt must be higher priority than SysTick
-    NVIC_SetPriority(PORTA_IRQn, 0x1);
-    NVIC_SetPriority(SysTick_IRQn, 0x2);  // SysTick set to lower priority than Wi-Fi SPI bus interrupt
-    PORTA->PCR[16] |=PORT_PCR_ISF_MASK;
-    PORTA->ISFR |= (1 << 16);
-}
-
-#elif (MY_BOARD == WIFI_DIPCORTEX)
-
-/**
- *  \brief Wifi DipCortex initialization
- *  \param none
- *  \return none
- */
-void init() {
-    NVIC_SetPriority(SSP1_IRQn, 0x0);
-    NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
-
-    // SysTick set to lower priority than Wi-Fi SPI bus interrupt
-    NVIC_SetPriority(SysTick_IRQn, 0x2);
-}
-
-#else
-
-/**
- *  \brief Place here init routine for your board
- *  \param none
- *  \return none
- */
-void init() {
-
-}
-
-#endif
--- a/main.cpp	Tue Mar 25 13:00:09 2014 +0000
+++ b/main.cpp	Fri Apr 04 15:17:07 2014 +0000
@@ -14,138 +14,51 @@
  * limitations under the License.
  */
 #include "mbed.h"
-#include "cc3000.h"
+#include "WiflyInterface.h"
 #include "main.h"
 #include "rtos.h"
-#include "LPD8806.h"
-// NanoService includes
-#include "Endpoint.h"
-#include "UDPSocket.h"
-#include "sn_nsdl.h"
-#include "sn_coap_header.h"
-#include "sn_coap_protocol.h"
-#include "sn_nsdl_lib.h"
-#include "sn_grs.h"
-#include <stdint.h>
+
+#include "nsdl_support.h"
 
-#define STRINGIFY(x) #x
-#define TO_STRING(x) STRINGIFY(x)
+//#include "pressure_mat.h"
 
-using namespace mbed_cc3000;
-
-tUserFS user_info;
+//Serial pc(USBTX, USBRX);
 
-/* cc3000 module declaration specific for user's board. Check also init() */
-#if (MY_BOARD == WIGO)
-cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5));
-Serial pc(USBTX, USBRX);
-#elif (MY_BOARD == WIFI_DIPCORTEX)
-cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37));
-Serial pc(UART_TX, UART_RX);
-#elif (MY_BOARD == MBED_BOARD_EXAMPLE)
-cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7));
-Serial pc(USBTX, USBRX);
-#else
-
-#endif
-
-#ifndef CC3000_UNENCRYPTED_SMART_CONFIG
-  const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
-#else
-  const uint8_t smartconfigkey = 0;
-#endif
+/* wifly object where:
+*     - p9 and p10 are for the serial communication
+*     - p25 is for the reset pin
+*     - p26 is for the connection status
+*     - "mbed" is the ssid of the network
+*     - "password" is the password
+*     - WPA is the security
+*/
+//WiflyInterface wifly(p9, p10, p25, p26, SSID, AP_KEY, WPA); //LPC1768
+WiflyInterface wifly(PTE16, PTE17, PTD4, PTD2, SSID, AP_KEY, WPA); //KL46Z
 
 // NSP configuration
 /* Change this IP address to that of your NanoService Platform installation */
 Endpoint nsp;
 UDPSocket server;
-static const char* NSP_ADDRESS = "10.2.131.119"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ 
-static const int NSP_PORT = 5683;
-char endpoint_name[] = {"umbrella"};
-uint8_t ep_type[] = {"mbed_kl25z"};
+//extern TCPSocketConnection server;
+char endpoint_name[] = {"welcomemat"};
+uint8_t ep_type[] = {"mbed_KL46Z"};
 uint8_t lifetime_ptr[] = {"86400"};
 
-typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *);
-
-//LED Config
-//Number of RGB LEDs in strand:
-int nLEDs=80;
-int datapin=1;
-int clockpin=2;
-LPD8806 strip = LPD8806(nLEDs,datapin,clockpin);
-
-
-/**
- *  \brief Print cc3000 information
- *  \param none
- *  \return none
- */
-void print_cc3000_info() {
-    uint8_t myMAC[8];
-
-    printf("MAC address + cc3000 info \r\n");
-    wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
-    wifi.get_mac_address(myMAC);
-    printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]);
-
-    printf(" FTC        %i \r\n",user_info.FTC);
-    printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]);
-    printf(" SERV_PACK  %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]);
-    printf(" DRV_VER    %i.%i.%i \r\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]);
-    printf(" FW_VER     %i.%i.%i \r\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]);
-}
+//static const char* NSP_ADDRESS = "208.111.39.209"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ 
+//static const char* NSP_ADDRESS = "10.2.131.119"; /* aseserver NSP, */ 
+static const char* NSP_ADDRESS = "192.168.1.10"; /* Trenton BBB NSP */ 
+static const int NSP_PORT = 5683;
 
-/**
- *  \brief Connect to SSID with a timeout
- *  \param ssid     Name of SSID
- *  \param key      Password
- *  \param sec_mode Security mode
- *  \return none
- */
-void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) {
-    printf("Connecting to SSID: %s. Timeout is 10s. \r\n",ssid);
-    if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) {
-        printf(" Connected. \r\n");
-    } else {
-        printf(" Connection timed-out (error). Please restart. \r\n");
-        while(1);
-  }
-}
-
-/**
- *  \brief Connect to SSID without security
- *  \param ssid Name of SSID
- *  \return none
- */
-void connect_to_ssid(char *ssid) {
-    wifi.connect_open((uint8_t *)ssid);
-}
-
-/**
- *  \brief First time configuration
- *  \param none
- *  \return none
- */
-void do_FTC(void) {
-    printf("Running First Time Configuration \r\n");
-    wifi.start_smart_config(smartconfigkey);
-    while (wifi.is_dhcp_configured() == false) {
-         wait_ms(500);
-         printf("Waiting for dhcp to be set. \r\n");
+/* 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);
     }
-    user_info.FTC = 1;
-    wifi.set_user_file_info((uint8_t *)&user_info, sizeof(user_info));
-    wifi._wlan.stop();
-    printf("FTC finished. \r\n");
-}
-
-/**
- *  \brief Start smart config
- *  \param none
- *  \return none
- */
-void start_smart_config() {
-    wifi.start_smart_config(smartconfigkey);
 }
 
 // ****************************************************************************
@@ -153,125 +66,19 @@
 
 static void nsp_connect()
 {
-    
     printf("EP Name: %s", endpoint_name);
     printf("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT);
     
     // Bind the port
     //cellular->bind(EP_PORT);
     server.init();
+    //server.connect(NSP_ADDRESS, NSP_PORT);
     server.bind(NSP_PORT);
     nsp.set_address(NSP_ADDRESS, NSP_PORT);
 
     printf("UDP connection to NSP successful.\r\n");  
 }
 
-extern "C" void *nsdl_alloc(uint16_t size)
-{
-    return malloc(size);
-}
-
-extern "C" void nsdl_free(void* ptr_to_free)
-{
-    free(ptr_to_free);
-}
-
-static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
-{
-/*    
-    int buffer_len = data_len+2;
-    printf("TX callback! Sending %d bytes\r\n", buffer_len);
-    char buffer[buffer_len];
-    buffer[0] = data_len >> 8;
-    buffer[1] = data_len & 0xFF;
-    memcpy(buffer+2, data_ptr, data_len);
-  */  
-    //if(cellular->write((char*)buffer, (int)buffer_len, 1000) != buffer_len)
-    if(server.sendTo(nsp, (char*)data_ptr, (int)data_len) != data_len)
-        printf("Sending failed\r\n");
-        
-    
-    return 1;
-    
-}
-
-static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
-{
-    printf("RX callback!\r\n");
-    return 0;
-}
-
-void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len)
-{
-    resource_structure->access = SN_GRS_GET_ALLOWED;
-    resource_structure->mode = SN_GRS_STATIC;
-    resource_structure->pathlen = pt_len;
-    resource_structure->path = pt;
-    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
-    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
-    resource_structure->resource = rsc;
-    resource_structure->resourcelen = rsc_len;
-    sn_nsdl_create_resource(resource_structure);
-}
-
-void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right)
-{
-    resource_structure->access = (sn_grs_resource_acl_e)access_right;
-    resource_structure->resource = 0;
-    resource_structure->resourcelen = 0;
-    resource_structure->sn_grs_dyn_res_callback = callback_ptr;
-    resource_structure->mode = SN_GRS_DYNAMIC;
-    resource_structure->pathlen = pt_len;
-    resource_structure->path = pt;
-    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
-    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
-    resource_structure->resource_parameters_ptr->observable = is_observable;
-    sn_nsdl_create_resource(resource_structure);
-}
-
-sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr)
-{
-    if (NULL == endpoint_structure)
-    {   
-        endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
-    }
-    if (endpoint_structure)
-    {
-        memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
-        endpoint_structure->endpoint_name_ptr = name;
-        endpoint_structure->endpoint_name_len = strlen((char*)name);
-        endpoint_structure->type_ptr = typename_ptr;
-        endpoint_structure->type_len =  strlen((char*)typename_ptr);
-        endpoint_structure->lifetime_ptr = lifetime_ptr;
-        endpoint_structure->lifetime_len =  strlen((char*)lifetime_ptr);
-    }
-    return endpoint_structure;
-}
-
-void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
-{
-    if (*endpoint_structure)
-    {
-        nsdl_free(*endpoint_structure);
-        *endpoint_structure = NULL;
-    }
-}
-
-void nsdl_init()
-{
-    uint8_t nsp_addr[4];
-    sn_nsdl_mem_s memory_cbs;
-    memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
-    memory_cbs.sn_nsdl_free = &nsdl_free;
-    if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) {
-        printf("libNsdl init failed\r\n");
-    } else {
-        printf("libNsdl init done\r\n");
-    }
-    /* Set nsp address for library */
-    set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
-}
-
 static int create_resources()
 {
     sn_nsdl_resource_info_s *resource_ptr = NULL;
@@ -294,11 +101,10 @@
     memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
 
     // Static resources
-    nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0,  (uint8_t*) "KL25Z Umbrella Stand", sizeof("KL25Z Umbrella Stand")-1);
+    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_light_resource(resource_ptr);
-    // create_gps_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);
@@ -314,140 +120,24 @@
     return 1;
 }
 
-void nsp_register()
-{
-    sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
-
-    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-registration failed\r\n");
-    } else {
-        printf("NSP re-registration OK\r\n");
-    }
-    nsdl_clean_register_endpoint(&endpoint_ptr);
-}
-
-void socket_event_loop()
-{
-    sn_nsdl_addr_s received_packet_address;
-    uint8_t received_address[4];
-    char buffer[2048];
-    int n;
-    Endpoint from;
-
-    memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
-    received_packet_address.addr_ptr = received_address; 
-
-    printf("Starting socket read loop...\r\n");
-    while(1)
-    {
-        //n = cellular->read(buffer, sizeof(buffer), 1000);
-        n = server.receiveFrom(from, buffer, sizeof(buffer));
-        printf("Received %d bytes", n);
-        if (n < 0)
-        {
-            printf("Socket error\r\n");
-        }
-        else
-        {   
-            uint16_t len = 0;
-            if (n > 2) {
-                len = 256 * buffer[0] + buffer[1]; 
-                printf("CoAP length header = %d bytes\r\n", len);
-                sn_nsdl_process_coap((uint8_t*)buffer+2, len, &received_packet_address);
-            }
-        } 
-    }
-
-}
-
-/************************************************************************************
-Use Color Fill to fill strips from bottom up in a 6 strip alternating configuration.
-c           The colour
-delay       Time between each pixel activation
-numStrips   Number of sub-strips
-direction   <0 implies top down, >0 bottom up, 0 all at once. Assumes first pixel is top.
-alternating 0 implies first pixel of each strip is top, otherwise alternates top/bottom/top etc.
-*************************************************************************************/
-void colorFillAlternatingSubStrips(uint32_t c, uint8_t delay, uint8_t numSubStrips, uint8_t alternating, int8_t direction) {
-    
-    int i,j;
-    int pixelsPerStrip = strip.numPixels() / numSubStrips;
-
-    //set each pixel sequentially:
-    for (i=0; i < pixelsPerStrip; i++) {
-        for(j = 0; j < numSubStrips; j++) {
-            if(!alternating || j % 2 == 0) {
-                // Even strip
-                if(direction <= 0) {
-                    //Down
-                    strip.setPixelColor(j*pixelsPerStrip + i, c);
-                } else if (direction > 0) {
-                    //Up
-                    strip.setPixelColor((j+1)*pixelsPerStrip - 1 - i, c);
-                }
-            } else {
-                // Odd Strip
-                if(direction < 0) {
-                    //Down
-                    strip.setPixelColor((j+1)*pixelsPerStrip - 1 - i, c);
-                } else if (direction >= 0) {
-                    //Up
-                    strip.setPixelColor(j*pixelsPerStrip + i, c);
-                }
-            }
-        }
-        if(direction != 0) {
-            strip.show();
-            wait_ms(delay);
-        }
-    }
-    if(direction == 0) {
-        strip.show();
-        wait_ms(delay);
-    }
-}
-
-void doNotificationIteration() {
-    colorFillAlternatingSubStrips(strip.Color(0,127,0), 100, 6, 1, 1);
-    wait_ms (3000);
-    colorFillAlternatingSubStrips(strip.Color(0,0,0), 100, 6, 1, 1);
-    wait_ms (1000);
-}
 
 
 /**
- *  \brief Simple socket demo
  *  \param none
  *  \return int
  */
 int main() {
-    init(); /* board dependent init */
-    pc.baud(115200);
-
-    wifi.start(0);
-    printf("cc3000 simple socket demo. \r\n");
-    print_cc3000_info();
-
-    printf("Attempting SSID Connection. \r\n");
-
-    wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
-#ifndef CC3000_TINY_DRIVER
-    connect_to_ssid(SSID, AP_KEY, AP_SECURITY);
-#else
-    connect_to_ssid(SSID);
-#endif
-
-    printf("DHCP request \r\n");
-    while (wifi.is_dhcp_configured() == false) {
-         wait_ms(500);
-         printf("  Waiting for dhcp to be set. \r\n");
-    }
-
-    tNetappIpconfigRetArgs ipinfo2;
-    wifi.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure
-    printf("DHCP assigned IP Address = %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]);
-
+    //factory reset Wifly
+    wifly.reset();
+    printf("\nFactory Reset\n");
+    //reboot wifly
+    bool success = wifly.reboot();
+    printf("Reboot: %d\n", success); //success = 1 -> successful process
+    printf("Initialising Wifly...\n\r");
+    wifly.init(); // use DHCP
+    while (!wifly.connect()); // join the network
+    printf("IP Address is %s\n\r", wifly.getIPAddress());
+        
     // Bind the socket and configure NSP settings
     nsp_connect();
  
@@ -455,9 +145,12 @@
     nsdl_init();
 
     // Create resources & register with NSP
-    create_resources(); 
-     
-    // Start socket listening loop
-    socket_event_loop();
+    create_resources();
+    
+    //Create the NSDL exec thread
+    //static Thread exec_thread(exec_call_thread);
+    
+    // Run the NSDL event loop (never returns)
+    nsdl_event_loop();
 
 }
--- a/main.h	Tue Mar 25 13:00:09 2014 +0000
+++ b/main.h	Fri Apr 04 15:17:07 2014 +0000
@@ -16,42 +16,17 @@
 #ifndef MAIN_H
 #define MAIN_H
 
-#define WIGO               1
-#define WIFI_DIPCORTEX     2
-#define MBED_BOARD_EXAMPLE 3
-#define UNDEFINED          4
-
-#define MY_BOARD WIGO
-
-// use this defines in AP_SECURITY
-#define NONE 0
-#define WEP  1
-#define WPA  2
-#define WPA2 3
-
-// use smart config
-#define USE_SMART_CONFIG 0
+#include "WiflyInterface.h"
 
  // Default SSID Settings
-#define AP_KEY       "goatroom"
-#define AP_SECURITY  WPA2            // WPA2 must be enabled for use with iPhone or Android phone hotspot!
-#define SSID         "ASEDEMO2"
+#define AP_KEY       "asedemo1"
+#define SSID         "trentonhub"
+
 
-#define USE_DHCP      1
-#define USE_STATIC_IP 2
- 
-// Modify the following settings as necessary for your Wi-Fi Network setup:
-#define IP_ALLOC_METHOD USE_DHCP        // for DHCP assigned IP address
-//#define IP_ALLOC_METHOD USE_STATIC_IP // for static IP address
- 
-#define STATIC_IP_OCT1 192
-#define STATIC_IP_OCT2 168
-#define STATIC_IP_OCT3 1
-#define STATIC_IP_OCT4 3
- 
-#define STATIC_GW_OCT4 1             // Static Gateway address  = STATIC_IP_OCT1.STATIC_IP_OCT2.STATIC_IP_OCT3.STATIC_GW_OCT4
- 
-
-void init();
+extern Endpoint nsp;
+extern UDPSocket server;
+extern char endpoint_name[];
+extern uint8_t ep_type[];
+extern uint8_t lifetime_ptr[];
 
 #endif
--- a/mbed-rtos.lib	Tue Mar 25 13:00:09 2014 +0000
+++ b/mbed-rtos.lib	Fri Apr 04 15:17:07 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#c2779d329e1c
+http://mbed.org/users/mbed_official/code/mbed-rtos/#420a92812cd1
--- a/mbed.bld	Tue Mar 25 13:00:09 2014 +0000
+++ b/mbed.bld	Fri Apr 04 15:17:07 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/9c8f0e3462fb
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/7d30d6019079
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nsdl_support.cpp	Fri Apr 04 15:17:07 2014 +0000
@@ -0,0 +1,204 @@
+// NSDL library support functions
+
+#include "mbed.h"
+#include "nsdl_support.h"
+#include "mbed.h"
+#include "rtos.h"
+#include "WiflyInterface.h"
+
+extern Endpoint nsp;
+extern UDPSocket server;
+//extern TCPSocketConnection server;
+extern char endpoint_name[16];
+extern uint8_t ep_type[];
+extern uint8_t lifetime_ptr[];
+
+/* The number of seconds between NSP registration messages */
+#define RD_UPDATE_PERIOD  300
+
+void *nsdl_alloc(uint16_t size)
+{
+    return malloc(size);
+}
+
+void nsdl_free(void* ptr_to_free)
+{
+    free(ptr_to_free);
+}
+
+/*
+ * Create a static resoure
+ * Only get is allowed
+ */
+void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len)
+{
+    resource_structure->access = SN_GRS_GET_ALLOWED;
+    resource_structure->mode = SN_GRS_STATIC;
+    resource_structure->pathlen = pt_len;
+    resource_structure->path = pt;
+    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
+    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
+    resource_structure->resource = rsc;
+    resource_structure->resourcelen = rsc_len;
+    sn_nsdl_create_resource(resource_structure);
+}
+
+void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right)
+{
+    resource_structure->access = (sn_grs_resource_acl_e)access_right;
+    resource_structure->resource = 0;
+    resource_structure->resourcelen = 0;
+    resource_structure->sn_grs_dyn_res_callback = callback_ptr;
+    resource_structure->mode = SN_GRS_DYNAMIC;
+    resource_structure->pathlen = pt_len;
+    resource_structure->path = pt;
+    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
+    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
+    resource_structure->resource_parameters_ptr->observable = is_observable;
+    sn_nsdl_create_resource(resource_structure);
+}
+
+sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr)
+{
+    if (NULL == endpoint_structure)
+    {   
+        endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
+    }
+    if (endpoint_structure)
+    {
+        memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
+        endpoint_structure->endpoint_name_ptr = name;
+        endpoint_structure->endpoint_name_len = strlen((char*)name);
+        endpoint_structure->type_ptr = typename_ptr;
+        endpoint_structure->type_len =  strlen((char*)typename_ptr);
+        endpoint_structure->lifetime_ptr = lifetime_ptr;
+        endpoint_structure->lifetime_len =  strlen((char*)lifetime_ptr);
+    }
+    return endpoint_structure;
+}
+
+void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
+{
+    if (*endpoint_structure)
+    {
+        nsdl_free(*endpoint_structure);
+        *endpoint_structure = NULL;
+    }
+}
+
+static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
+{
+    /*
+    //TCP
+    int buffer_len = data_len+2;
+    printf("TX callback! Sending %d bytes", buffer_len);
+    char buffer[buffer_len];
+    buffer[0] = data_len >> 8;
+    buffer[1] = data_len & 0xFF;
+    memcpy(buffer+2, data_ptr, data_len);
+    
+    //if(cellular->write((char*)buffer, (int)buffer_len, 1000) != buffer_len)
+    if(server.send((char*)data_ptr, data_len) != data_len)
+        printf("Sending failed");
+
+    return 1;
+    */
+
+    //UDP
+    printf("TX callback!\n\rSending %d bytes\r\n", data_len);
+    if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
+        printf("sending failed\n\r");
+
+    return 1;
+
+}
+
+static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
+{
+    printf("RX callback!\r\n");
+    return 0;
+}
+
+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_init()
+{
+    uint8_t nsp_addr[4];
+    sn_nsdl_mem_s memory_cbs;
+
+    /* Initialize libNsdl */
+    memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
+    memory_cbs.sn_nsdl_free = &nsdl_free;
+    if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
+        printf("libNsdl init failed\r\n");
+    else
+        printf("libNsdl init done\r\n");
+
+    /* Set nsp address for library */
+    set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
+}
+
+void nsdl_event_loop()
+{
+    //Thread registration_thread(registration_update_thread);
+
+    sn_nsdl_addr_s received_packet_address;
+    uint8_t received_address[4];
+    int8_t nsdl_result = 0;
+    //UDP
+    char buffer[1024];
+    //TCP
+    //char buffer[2048];
+    Endpoint from;
+
+    memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
+    received_packet_address.addr_ptr = received_address;
+
+    while(1)
+    {
+        //UDP
+        int n = server.receiveFrom(from, buffer, sizeof(buffer));
+        //TCP
+        //int n = server.receive(buffer, sizeof(buffer));
+        if (n < 0)
+        {
+            printf("Socket error\n\r");
+        }
+        else
+        { 
+        /*
+            //TCP
+            uint16_t len = 0;
+            if (n > 2) {
+                len = 256 * buffer[0] + buffer[1]; 
+                printf("CoAP length header = %d bytes", len);
+                sn_nsdl_process_coap((uint8_t*)buffer+2, len, &received_packet_address);
+            }
+            */
+        
+            //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);
+        
+        }
+    }
+}
+
+            
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nsdl_support.h	Fri Apr 04 15:17:07 2014 +0000
@@ -0,0 +1,24 @@
+// Support functions for the NSDL library
+
+#ifndef NSDL_SUPPORT_H
+#define NSDL_SUPPORT_H
+
+#include "mbed.h"
+#include <stdint.h>
+#include "sn_nsdl.h"
+#include "sn_coap_header.h"
+#include "sn_coap_protocol.h"
+#include "sn_nsdl_lib.h"
+
+typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *);
+
+extern "C" void *nsdl_alloc(uint16_t size);
+extern "C" void nsdl_free(void* ptr_to_free);
+void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len);
+void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right);
+sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* ypename_ptr, uint8_t *lifetime_ptr);
+void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure);
+void nsdl_init();
+void nsdl_event_loop();
+
+#endif // NSDL_SUPPORT_H