from LPC1768_LWM2M_Client
Dependencies: F7_Ethernet mbed-rtos mbed nsdl_lib
Fork of rtos_basic by
Revision 8:5bcb48a04554, committed 2016-12-21
- Comitter:
- zef
- Date:
- Wed Dec 21 03:08:39 2016 +0000
- Parent:
- 7:8d9919175929
- Commit message:
- from LPC1768_LWM2M_Client Project
Changed in this revision
diff -r 8d9919175929 -r 5bcb48a04554 F7_Ethernet.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/F7_Ethernet.lib Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/DieterGraef/code/F7_Ethernet/#28ba13dd96f7
diff -r 8d9919175929 -r 5bcb48a04554 dbg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbg.h Wed Dec 21 03:08:39 2016 +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 1 + +#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 8d9919175929 -r 5bcb48a04554 main.cpp --- a/main.cpp Tue Nov 22 18:04:36 2016 +0000 +++ b/main.cpp Wed Dec 21 03:08:39 2016 +0000 @@ -1,22 +1,230 @@ +//#include "mbed.h" +//#include "rtos.h" +// +//DigitalOut led1(LED1); +//Serial pc(USBTX,USBRX);//tx rx +//Thread thread; +//Mutex stdio_mutex; +//uint8_t threadindex[3]; +//long count[3]; +//void led2_thread() { +// while (true) { +// //led2 = !led2; +// pc.printf("Hello World!\n"); +// Thread::wait(1000); +// +// } +//} +//void printString(void const *args) +//{ +// while(true) +// { +// stdio_mutex.lock(); +// pc.printf("My Thread Name is %d,Thread ID is %d\n",(uint8_t *)args,Thread::gettid()); +// count[*(uint8_t *)args-1]++; +// stdio_mutex.unlock(); +// Thread::yield(); +// } +//} +// +////int main() { +//// pc.baud(115200); +//// thread.start(led2_thread); +//// +//// while (true) { +//// led1 = !led1; +//// Thread::wait(500); +//// } +////} +//int main(){ +// pc.baud(115200); +// threadindex[0]=1; +// threadindex[1]=2; +// threadindex[2]=3; +// Thread thread1(printString,(void*)threadindex,osPriorityNormal); +// Thread thread2(printString,(void*)threadindex,osPriorityNormal); +// Thread thread3(printString,(void*)threadindex,osPriorityNormal); +// while(1){ +// stdio_mutex.lock(); +// pc.printf("Thread1 count is %ld,Thread2 count is %ld,Thread3 count is %ld.\n",count[0],count[1],count[2]); +// stdio_mutex.unlock(); +// Thread::wait(1000); +// } +//} +///*************************************************************************/ +////测试ok +//#include "mbed.h" +//#include "rtos.h" +//#include "EthernetInterface.h" +//DigitalOut led(LED1); +//int main() { +//EthernetInterface eth; +////eth.init(); Use DHCP +//eth.init("192.168.0.110","255.255.255.0","192.168.0.1"); +//eth.connect(); +//printf("IP Address is %s\n", eth.getIPAddress()); +//printf("MAC Address is %s\n", eth.getMACAddress()); +//printf("GateWay Address is %s\n", eth.getGateway()); +//while(1) +//{ +// led=!led; +// wait(1); +//} +//} +/*************************************************************************/ +//测试ok #include "mbed.h" #include "rtos.h" - -DigitalOut led1(LED1); -DigitalOut led2(LED2); -Thread thread; - -void led2_thread() { - while (true) { - led2 = !led2; - Thread::wait(1000); +#include "dbg.h" +#include "nsdl_support.h" +#include "EthernetInterface.h" +#include "temperature.h" +#include "light.h" +#include "gps.h" +#include "relay.h" +DigitalOut led(LED1); +Serial pc(USBTX,USBRX);//tx rx +// **************************************************************************** +// Configuration section + +// Ethernet configuration +/* Define this to enable DHCP, otherwise manual address configuration is used */ +//#define DHCP + +/* Manual IP configurations, if DHCP not defined */ +#define IP "192.168.1.210" +#define MASK "255.255.255.0" +#define GW "192.168.1.1" +// NSP configuration +/* Change this IP address to that of your NanoService Platform installation */ +//static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ +static const char* NSP_ADDRESS = "192.168.1.100"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ +//static const char* NSP_ADDRESS = "192.168.1.200"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ + +//static const char* NSP_ADDRESS = "5.39.83.206"; //http://leshan.eclipse.org/#/clients +static const int NSP_PORT = 5683; +char endpoint_name[16] = "mbed-thp"; +uint8_t ep_type[] = {"mbed_device"}; +uint8_t lifetime_ptr[] = {"1200"}; +// **************************************************************************** +// Ethernet initialization + +EthernetInterface eth; + +static void ethernet_init() +{ + char mbed_uid[33]; // for creating unique name for the board + + /* Initialize network */ +#ifdef DHCP + NSDL_DEBUG("DHCP in use\r\n"); + eth.init(); +#else + eth.init(IP, MASK, GW); +#endif + if(eth.connect(30000) == 0) + pc.printf("Connect OK\n\r"); + + //mbed_interface_uid(mbed_uid); + mbed_uid[8] = '\0'; + //strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name)); + + //lcd.locate(0,11); + //lcd.printf("IP:%s", eth.getIPAddress()); + + NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); +} +// **************************************************************************** +// 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; } - int main() { - thread.start(led2_thread); + pc.baud(115200); - while (true) { - led1 = !led1; - Thread::wait(500); - } -} + // Initialize Ethernet interface first + ethernet_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(); +// eth.init(); //Use DHCP +// eth.init("192.168.0.110","255.255.255.0","192.168.0.1"); +// eth.connect(); +// pc.printf("IP Address is %s\n", eth.getIPAddress()); +// pc.printf("MAC Address is %s\n", eth.getMACAddress()); +// pc.printf("GateWay Address is %s\n", eth.getGateway()); +// while(1) +// { +// led=!led; +// wait(1); +// } +} \ No newline at end of file
diff -r 8d9919175929 -r 5bcb48a04554 mbed.bld --- a/mbed.bld Tue Nov 22 18:04:36 2016 +0000 +++ b/mbed.bld Wed Dec 21 03:08:39 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/0ab6a29f35bf \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/d75b3fe1f5cb \ No newline at end of file
diff -r 8d9919175929 -r 5bcb48a04554 nsdl_lib.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_lib.lib Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Sensinode/code/nsdl_lib/#388450b1e776
diff -r 8d9919175929 -r 5bcb48a04554 nsdl_support.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_support.cpp Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,171 @@ +// NSDL library support functions + +#include "mbed.h" +#include "nsdl_support.h" +#include "mbed.h" +#include "rtos.h" +#include "EthernetInterface.h" + +extern Serial pc; +extern EthernetInterface eth; +extern Endpoint nsp; +extern UDPSocket 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 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) +{ + int ret; + 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; + ret=sn_nsdl_create_resource(resource_structure); + if(ret<0) + { + pc.printf("sn_nsdl_create_resource() error%d\n",ret); + } + pc.printf("path=%s\n",resource_structure->path); +} + +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; +} + +static 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_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; + + 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); + } + Thread::wait(1000); + } +} \ No newline at end of file
diff -r 8d9919175929 -r 5bcb48a04554 nsdl_support.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_support.h Wed Dec 21 03:08:39 2016 +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 8d9919175929 -r 5bcb48a04554 resources/gps.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/gps.cpp Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,41 @@ +// GPS resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "gps.h" + +#define GPS_RES_ID "gps/loc" + +extern Serial pc; +static uint8_t res_gps_val[] = {"52.182382,0.178849"}; + +/* Only GET method allowed */ +static uint8_t gps_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; + static float led_dimm = 0; + int led_state = 0; + char led_dimm_temp[4]; + + pc.printf("gps callback\r\n"); + + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + led_state = led_dimm * 100; + sprintf(led_dimm_temp, "%d", led_state); + + coap_res_ptr->payload_len = sizeof(res_gps_val)-1; + coap_res_ptr->payload_ptr = (uint8_t*)res_gps_val; + + sn_nsdl_send_coap_message(address, coap_res_ptr); + + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + + return 0; +} + +int create_gps_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(GPS_RES_ID)-1, (uint8_t*)GPS_RES_ID, 0, 0, 0, &gps_resource_cb, SN_GRS_GET_ALLOWED); + return 0; +} \ No newline at end of file
diff -r 8d9919175929 -r 5bcb48a04554 resources/gps.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/gps.h Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,10 @@ +// GPS resource implementation + +#ifndef GPS_H +#define GPS_H + +#include "nsdl_support.h" + +int create_gps_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 8d9919175929 -r 5bcb48a04554 resources/light.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/light.cpp Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,62 @@ +// Light resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "light.h" + +#define LIGHT_RES_ID "lt/0/dim" + +extern Serial pc; +//PwmOut led1(LED1); +//static PwmOut led2(LED2); +//static PwmOut led3(LED3); +//PwmOut led4(LED4); + +/* 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; + static float led_dimm = 0; + int led_state = 0; + char led_dimm_temp[4]; + + pc.printf("light 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); + + //led_state = led_dimm * 100; + sprintf(led_dimm_temp, "%d", led_state); + + coap_res_ptr->payload_len = strlen(led_dimm_temp); + coap_res_ptr->payload_ptr = (uint8_t*)led_dimm_temp; + sn_nsdl_send_coap_message(address, coap_res_ptr); + } + else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) + { + memcpy(led_dimm_temp, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + + //led_dimm_temp[received_coap_ptr->payload_len] = '\0'; + + //led_dimm = atof(led_dimm_temp); + //led_dimm = led_dimm/100; + + //led1.write(led_dimm); + //led2.write(led_dimm); + //led3.write(led_dimm); + //led4.write(led_dimm); + + 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 8d9919175929 -r 5bcb48a04554 resources/light.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/light.h Wed Dec 21 03:08:39 2016 +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
diff -r 8d9919175929 -r 5bcb48a04554 resources/relay.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/relay.cpp Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,58 @@ +// GPS resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "relay.h" +//#include "Beep.h" + +#define RELAY_RES_ID "beep/0/on" + +extern Serial pc; +//static Beep buzzer(p26); + +/* Only GET and PUT method allowed */ +static uint8_t relay_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; + static uint8_t relay_state = '0'; + + pc.printf("relay 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 = 1; + coap_res_ptr->payload_ptr = &relay_state; + sn_nsdl_send_coap_message(address, coap_res_ptr); + } + else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) + { + if(received_coap_ptr->payload_len) + { + if(*(received_coap_ptr->payload_ptr) == '1') + { + //buzzer.beep(1000,0); + relay_state = '1'; + + } + else if(*(received_coap_ptr->payload_ptr) == '0') + { + //buzzer.nobeep(); + relay_state = '0'; + } + 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_relay_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(RELAY_RES_ID)-1, (uint8_t*)RELAY_RES_ID, 0, 0, 0, &relay_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); + return 0; +} \ No newline at end of file
diff -r 8d9919175929 -r 5bcb48a04554 resources/relay.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/relay.h Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,10 @@ +// Relay resource implementation + +#ifndef RELAY_H +#define RELAY_H + +#include "nsdl_support.h" + +int create_relay_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 8d9919175929 -r 5bcb48a04554 resources/temperature.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/temperature.cpp Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,94 @@ +// Temperature resource implementation + +#include "mbed.h" +#include "rtos.h" +//#include "LM75B.h" +#include "nsdl_support.h" +#include "temperature.h" + +#define TEMP_RES_ID "sen/temp" + +//static LM75B tmp(p28,p27); +/* stored data for observable resource */ +static uint8_t obs_number = 0; +static uint8_t *obs_token_ptr = NULL; +static uint8_t obs_token_len = 0; +static char temp_val[5]; +extern Serial pc; +static uint8_t tmp=29; +/* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ +/* Node updates temperature every 10 seconds. Notification sending is done here. */ +static void exec_call_thread(void const *args) +{ + int32_t time = 0; + while (true) + { + wait(1); + time++; + sn_nsdl_exec(time); + if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL) + { + obs_number++; + //sprintf(temp_val,"%2.2f" ,tmp.read()); + sprintf(temp_val,"%2.2f" ,tmp); + if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)temp_val, 5, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) + pc.printf("Observation sending failed\r\n"); + else + pc.printf("Observation\r\n"); + } + } +} + +/* Only GET method allowed */ +/* Observable resource */ +static uint8_t temp_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + //sprintf(temp_val,"%2.2f" ,tmp.read()); + sprintf(temp_val,"%2.2f" ,tmp); + sn_coap_hdr_s *coap_res_ptr = 0; + + pc.printf("temp callback\r\n"); + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + coap_res_ptr->payload_len = 5; + coap_res_ptr->payload_ptr = (uint8_t*)temp_val; + + if(received_coap_ptr->token_ptr) + { + pc.printf("Token included\r\n"); + if(obs_token_ptr) + { + free(obs_token_ptr); + obs_token_ptr = 0; + } + obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); + if(obs_token_ptr) + { + memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); + obs_token_len = received_coap_ptr->token_len; + } + } + + if(received_coap_ptr->options_list_ptr->observe) + { + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; + coap_res_ptr->options_list_ptr->observe_len = 1; + obs_number++; + } + + sn_nsdl_send_coap_message(address, coap_res_ptr); + + coap_res_ptr->options_list_ptr->observe_ptr = 0; + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_temperature_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + static Thread exec_thread(exec_call_thread); + + nsdl_create_dynamic_resource(resource_ptr, sizeof(TEMP_RES_ID)-1, (uint8_t*)TEMP_RES_ID, 0, 0, 1, &temp_resource_cb, SN_GRS_GET_ALLOWED); + return 0; +} \ No newline at end of file
diff -r 8d9919175929 -r 5bcb48a04554 resources/temperature.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/temperature.h Wed Dec 21 03:08:39 2016 +0000 @@ -0,0 +1,10 @@ +// Temperature resource implementation + +#ifndef TEMPERATURE_H +#define TEMPERATURE_H + +#include "nsdl_support.h" + +int create_temperature_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif