![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Demo for the problem with nsdl_lib and rtos on FRDM- K64F
Dependencies: C12832_lcd WiflyInterface mbed-rtos mbed nsdl_lib
Revision 0:e41e45a41f0f, committed 2015-01-16
- Comitter:
- lvwei1990
- Date:
- Fri Jan 16 06:37:29 2015 +0000
- Commit message:
- Demo for the problem with nsdl_lib and rtos on FRDM- K64F
Changed in this revision
diff -r 000000000000 -r e41e45a41f0f C12832_lcd.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832_lcd.lib Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/lvwei1990/code/C12832_lcd/#4fc0b361cdab
diff -r 000000000000 -r e41e45a41f0f WiflyInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WiflyInterface.lib Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/WiflyInterface/#9d18ac47d27f
diff -r 000000000000 -r e41e45a41f0f dbg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbg.h Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,17 @@ +#ifndef DEBUG_H +#define DEBUG_H + +#include "nsdl_support.h" +#include "mbed.h" + +//Debug is disabled by default +#define DEBUG 0 + +#if (DEBUG) +extern Serial pc; +#define NSDL_DEBUG(x, ...) pc.printf("[NSDL_DEBUG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#else +#define NSDL_DEBUG(x, ...) +#endif + +#endif \ No newline at end of file
diff -r 000000000000 -r e41e45a41f0f main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,164 @@ +#include "mbed.h" +#include "WiflyInterface.h" +#include "C12832_lcd.h" +#include "nsdl_support.h" +#include "dbg.h" +#include "rtos.h" +// Include various resources +//#include "temperature.h" +#include "light.h" +//#include "gps.h" +//#include "relay.h" + +static C12832_LCD lcd; +Serial pc(USBTX, USBRX); // tx, rx + +// **************************************************************************** +// Configuration section + +// WiFi configuration (edit this) +#define WIFI_SSID "WIFITest" +#define WIFI_SECURITY WPA // NONE, WEP_128 or WPA +#define WIFI_PASSWORD "54510000" + +/* Define this to enable DHCP, otherwise manual address configuration is used */ +#define DHCP + +/* Manual IP configurations, if DHCP not defined */ +#define IP "10.45.0.206" +#define MASK "255.255.255.0" +#define GW "10.45.0.1" + +// NSP configuration +/* Change this IP address to that of your NanoService Platform installation */ +//static const char* NSP_ADDRESS = "208.111.39.209"; +static const char* NSP_ADDRESS = "23.97.178.38"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ +static const int NSP_PORT = 5683; +char endpoint_name[32] = "mbed-"; +uint8_t ep_type[] = {"mbed_device"}; +uint8_t lifetime_ptr[] = {"1200"}; + +// **************************************************************************** +// WiFi initialization + +static WiflyInterface wifly(D1, D0, D3, D2, "WIFITest", "54510000", NONE); + +static void wifi_init() +{ + char mbed_uid[33]; // for creating unique name for the board + char macbytes[6]; + + /* Initialize network */ +#ifdef DHCP + NSDL_DEBUG("DHCP in use\r\n"); + wifly.init(); +#else + wifly.init(IP, MASK, GW); +#endif + lcd.printf("wifly.init done\r\n"); + + if(wifly.connect() == 0) + lcd.printf("Connect OK\n\r"); + + mbed_mac_address(macbytes); + sprintf(mbed_uid,"%02x%02x%02x%02x%02x%02x", macbytes[0], macbytes[1], macbytes[2], macbytes[3], macbytes[4], macbytes[5]); + strncat(endpoint_name, mbed_uid, 12); + + + lcd.printf("IP Address:%s ", wifly.getIPAddress()); + lcd.cls(); + lcd.locate(0,0); +} + +// **************************************************************************** +// NSP initialization + +UDPSocket server; +Endpoint nsp; + +static void nsp_init() +{ + server.init(); + server.bind(NSP_PORT); + + nsp.set_address(NSP_ADDRESS, NSP_PORT); + + NSDL_DEBUG("name: %s", endpoint_name); + NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); + + lcd.locate(0,22); + lcd.printf("EP name:%s\n", endpoint_name); +} + +// **************************************************************************** +// Resource creation + +static int create_resources() +{ + sn_nsdl_resource_info_s *resource_ptr = NULL; + sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; + + NSDL_DEBUG("Creating resources"); + + /* Create resources */ + resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); + if(!resource_ptr) + return 0; + memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); + + resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); + if(!resource_ptr->resource_parameters_ptr) + { + nsdl_free(resource_ptr); + return 0; + } + memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); + + // Static resources + nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0, (uint8_t*) "Sensinode", sizeof("Sensinode")-1); + nsdl_create_static_resource(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0, (uint8_t*) "NSDL-C mbed device", sizeof("NSDL-C mbed device")-1); + + // Dynamic resources + //create_temperature_resource(resource_ptr); + create_light_resource(resource_ptr); + //create_gps_resource(resource_ptr); + //create_relay_resource(resource_ptr); + + /* Register with NSP */ + endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); + if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) + pc.printf("NSP registering failed\r\n"); + else + pc.printf("NSP registering OK\r\n"); + nsdl_clean_register_endpoint(&endpoint_ptr); + + nsdl_free(resource_ptr->resource_parameters_ptr); + nsdl_free(resource_ptr); + return 1; +} + +// **************************************************************************** +// Program entry point + +int main() +{ + lcd.cls(); + lcd.locate(0,0); + lcd.printf("mbed NanoService demo\r\n"); + NSDL_DEBUG("mbed NanoService Example App 0.1\n"); + + // Initialize Ethernet interface first + wifi_init(); + + // Initialize NSP node + nsp_init(); + + // Initialize NSDL stack + nsdl_init(); + + // Create NSDL resources + create_resources(); + + // Run the NSDL event loop (never returns) + nsdl_event_loop(); +} \ No newline at end of file
diff -r 000000000000 -r e41e45a41f0f mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/lvwei1990/code/mbed-rtos/#703a0ea0ca4d
diff -r 000000000000 -r e41e45a41f0f mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file
diff -r 000000000000 -r e41e45a41f0f nsdl_lib.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_lib.lib Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Sensinode/code/nsdl_lib/#050d0f54a0dc
diff -r 000000000000 -r e41e45a41f0f nsdl_support.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_support.cpp Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,190 @@ +// NSDL library support functions + +#include "mbed.h" +#include "nsdl_support.h" +#include "mbed.h" +#include "rtos.h" +#include "Endpoint.h" +#include "UDPSocket.h" + +extern Serial pc; +extern Endpoint nsp; +extern UDPSocket server; +extern char endpoint_name[24]; +extern uint8_t ep_type[]; +extern uint8_t lifetime_ptr[]; +bool nsdl_reg_update_needed = false; +/* The number of seconds between NSP registration messages */ +#define RD_UPDATE_PERIOD 60 + +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) +{ + pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len); + + if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) + pc.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) +{ + pc.printf("RX callback!\r\n"); + return 0; +} + +void registration_update_thread(void const *args) +{ + sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; + + while(true) + { + wait(RD_UPDATE_PERIOD); + endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); + if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) + pc.printf("NSP re-registering failed\r\n"); + else + pc.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) + pc.printf("libNsdl init failed\r\n"); + else + pc.printf("libNsdl init done\r\n"); + + /* Set nsp address for library */ + set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); +} + +/*void nsdl_reg_update() + { + 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) + pc.printf("NSP registration update failed\r\n"); + else + pc.printf("NSP registration update OK\r\n"); + nsdl_clean_register_endpoint(&endpoint_ptr); + } + +void nsdl_reg_update_timeout() + { + nsdl_reg_update_needed = true; + } + +Ticker regUpdateTicker;*/ + +void nsdl_event_loop() +{ + Thread registration_thread(registration_update_thread); + + sn_nsdl_addr_s received_packet_address; + uint8_t received_address[4]; + char buffer[1024]; + Endpoint from; + + memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); + received_packet_address.addr_ptr = received_address; + + //regUpdateTicker.attach(&nsdl_reg_update_timeout, RD_UPDATE_PERIOD); + while(1) + { + int n = server.receiveFrom(from, buffer, sizeof(buffer)); + if (n < 0) + { + pc.printf("Socket error\n\r"); + } + else + { + pc.printf("Received %d bytes\r\n", n); + sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); + } +/* if (nsdl_reg_update_needed) + { + nsdl_reg_update_needed = false; + nsdl_reg_update(); + }*/ + } +} + + \ No newline at end of file
diff -r 000000000000 -r e41e45a41f0f nsdl_support.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_support.h Fri Jan 16 06:37:29 2015 +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
diff -r 000000000000 -r e41e45a41f0f resources/light.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/light.cpp Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,72 @@ +// Light resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "light.h" + +//#define LIGHT_RES_ID "lt/0/dim" +#define LIGHT_RES_ID "11100/0/5900" + +extern Serial pc; +char leds[] = {"1111"}; //YGBR + +DigitalOut grn(LED1); +DigitalOut red(LED2); +DigitalOut blu(LED3); +DigitalOut yel(LED4); + +void set_leds(char *leds) +{ +int leds_int ; + + sscanf(leds, "%x", &leds_int); + + grn = ~leds_int & 1; + red = ~leds_int >> 4 & 1; + blu = ~leds_int >> 8 & 1; + yel = ~leds_int >> 12 & 1; +} + + +/* Only GET and PUT method allowed */ +static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + + //pc.printf("LED Strip callback\r\n"); + + if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) + { + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + coap_res_ptr->payload_len = strlen(leds); + coap_res_ptr->payload_ptr = (uint8_t*)leds; + sn_nsdl_send_coap_message(address, coap_res_ptr); + } + else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) + { + //pc.printf("PUT: %d bytes\r\n", received_coap_ptr->payload_len); + if(received_coap_ptr->payload_len == 4) + { + memcpy(leds, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + + leds[received_coap_ptr->payload_len] = '\0'; + pc.printf("PUT: %s\r\n",leds); + + //call LED strup update function here + set_leds(leds); + + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + } + } + + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_light_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(LIGHT_RES_ID)-1, (uint8_t*)LIGHT_RES_ID, 0, 0, 0, &light_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r e41e45a41f0f resources/light.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/light.h Fri Jan 16 06:37:29 2015 +0000 @@ -0,0 +1,10 @@ +// Light resource implementation + +#ifndef LIGHT_H +#define LIGHT_H + +#include "nsdl_support.h" + +int create_light_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif