![](/media/cache/group/arm_mbed.PNG.50x50_q85.png)
Initial commit
Dependencies: Chainable_RGB_LED EthernetInterface LED_Bar mbed-rtos mbed nsdl_lib
Fork of SensorNotifyLED by
Revision 0:950566b0e099, committed 2014-10-30
- Comitter:
- michaeljkoster
- Date:
- Thu Oct 30 21:44:12 2014 +0000
- Commit message:
- Initial commit
Changed in this revision
diff -r 000000000000 -r 950566b0e099 Chainable_RGB_LED.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Chainable_RGB_LED.lib Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/seeed/code/Chainable_RGB_LED/#24631ceaa38a
diff -r 000000000000 -r 950566b0e099 EthernetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/michaeljkoster/code/EthernetInterface/#6ca542f6c58d
diff -r 000000000000 -r 950566b0e099 IAP.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IAP.lib Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/sam_grove/code/IAP/#f794a51897b8
diff -r 000000000000 -r 950566b0e099 LED_Bar.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_Bar.lib Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Seeed/code/LED_Bar/#edcb13b58b4f
diff -r 000000000000 -r 950566b0e099 dbg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbg.h Thu Oct 30 21:44:12 2014 +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 000000000000 -r 950566b0e099 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,183 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "nsdl_support.h" +#include "dbg.h" +#include "IAP.h" + +// Include resources +#include "LEDbar.h" +#include "IPSO_illuminance.h" +#include "gas_sensor.h" +#include "IPSO_presence.h" +#include "LED_array.h" + +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 "10.0.0.199" +#define MASK "255.255.255.0" +#define GW "10.0.0.1" + +extern "C" void mbed_mac_address(char *mac) +{ + static char buf[64] = {0}; + IAP iap; + int32_t *block = iap.read_serial(); + uint32_t serial_number[5] = {0}; + + memset(buf, 0, sizeof(buf)); + serial_number[0] = *(block); + serial_number[1] = *(block+1); + // we only want bottom 16 bits of word1 (MAC bits 32-47) + // and bit 9 forced to 1, bit 8 forced to 0 + // Locally administered MAC, reduced conflicts + // http://en.wikipedia.org/wiki/MAC_address + //serial_number[0] |= 0x00000200; + //serial_number[0] &= 0x0000FEFF; + memcpy(mac, (uint8_t*) &serial_number[0], 6); + mac[0] |= 0x02; + mac[0] &= 0xFE; + mac[5] |= 0x02; + mac[5] &= 0xFE; + + // snprintf(buf, 16, "%4X%08X", serial_number[0], serial_number[1]); +} + + +// NSP configuration +/* Change this IP address to that of your NanoService Platform installation */ +//static const char* NSP_ADDRESS = "23.102.162.118"; // coen296.cloudapp.net +static const char* NSP_ADDRESS = "191.239.5.150"; // barista2.cloudapp.net +static const int NSP_PORT = 5683; +char endpoint_name[24] = "mbedDEMO-"; +uint8_t ep_type[] = {"DEMO"}; +uint8_t lifetime_ptr[] = {"60"}; + +// **************************************************************************** +// Ethernet initialization + +EthernetInterface eth; +static void ethernet_init() +{ + + /* Initialize network */ +#ifdef DHCP + NSDL_DEBUG("DHCP in use\r\n"); + eth.init(); + NSDL_DEBUG("eth.init\r\n"); +#else + eth.init(IP, MASK, GW); + NSDL_DEBUG("eth.init\r\n"); +#endif + if(eth.connect(30000) == 0) + pc.printf("Connect OK\n\r"); + + NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); +} + +// **************************************************************************** +// NSP initialization + +UDPSocket server; +Endpoint nsp; + +char macbytes[6] = {0}; +char MAC[20]; + +static void nsp_init() +{ + server.init(); + server.bind(NSP_PORT); + + nsp.set_address(NSP_ADDRESS, NSP_PORT); + + mbed_mac_address(macbytes); + + sprintf(MAC, "%02X%02X%02X%02X%02X%02X", macbytes[0], macbytes[1], macbytes[2], macbytes[3], macbytes[4], macbytes[5]); + pc.printf("MAC: %s\r\n", MAC); + + strncat(endpoint_name, MAC, 12); + + NSDL_DEBUG("name: %s", endpoint_name); + NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); + +} + +// **************************************************************************** +// 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("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1); + nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "DEMO", sizeof("DEMO")-1); + + // Dynamic resources + create_LEDbar_resource(resource_ptr); + create_IPSO_illuminance_resource(resource_ptr); + create_gas_sensor_resource(resource_ptr); + create_IPSO_presence_resource(resource_ptr); + create_LED_array_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() +{ + + NSDL_DEBUG("Hello mDS Demo Endpoint Application\n"); + + // 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(); +}
diff -r 000000000000 -r 950566b0e099 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#1be1070d822f
diff -r 000000000000 -r 950566b0e099 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1 \ No newline at end of file
diff -r 000000000000 -r 950566b0e099 nsdl_lib.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_lib.lib Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Sensinode/code/nsdl_lib/#388450b1e776
diff -r 000000000000 -r 950566b0e099 nsdl_support.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_support.cpp Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,202 @@ +// NSDL library support functions + +#include "mbed.h" +#include "nsdl_support.h" +#include "mbed.h" +#include "rtos.h" +#include "EthernetInterface.h" +#include "UDPSocket.h" +#include "Endpoint.h" + +/* __heapvalid((__heapprt)fprintf,stdout, 1);\*/ +#define MEM_VALID(x) \ + int s##x=0;\ + int *h##x = new int [1];\ + std::printf("[stack]0x%08x\t[heap]0x%08x\t[memory avail]%d bytes \tLine: %d %s\r\n", &s##x, h##x, &s##x-h##x, __LINE__, __FILE__);\ + if (h##x > &s##x)\ + printf("collision\n");\ + else\ + delete [] h##x;\ + __nop() + +extern Serial pc; +extern Endpoint nsp; +extern UDPSocket server; +extern char endpoint_name[24]; +extern uint8_t ep_type[]; +extern uint8_t lifetime_ptr[]; +char null_ep_name[] = ""; +uint8_t null_ep_type[] = ""; +uint8_t null_lifetime_ptr[] = "60"; +bool nsdl_reg_update_needed = false; + +/* The number of seconds between NSP registration messages */ +#define RD_UPDATE_PERIOD 30 + +void *nsdl_alloc(uint16_t size) +{ + void *buf = malloc(size); +// printf("alloc\r\n"); +// MEM_VALID(0); + return buf; +} + +void nsdl_free(void* ptr_to_free) +{ + free(ptr_to_free); +// printf("de-alloc\r\n"); +// MEM_VALID(0); +} + +/* + * 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"); + pc.printf("msg_code: %d \r\n", coap_packet_ptr->msg_code); + pc.printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len); + int i; + pc.printf("Payload:'"); + for (i=0; i < coap_packet_ptr->payload_len; i++) + pc.printf("%c", *(coap_packet_ptr->payload_ptr + i)); + pc.printf("' \r\n"); + if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr) + { + pc.printf("Location: /"); + int i; + for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) pc.printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i])); + pc.printf(" \r\n"); + } + //sn_coap_packet_debug(coap_packet_ptr); + 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*)null_ep_name, null_ep_type, null_lifetime_ptr); + if(sn_nsdl_update_registration(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 if (n > 0) + { + pc.printf("Received %d bytes\r\n", n); + sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); + } + } +} + + \ No newline at end of file
diff -r 000000000000 -r 950566b0e099 nsdl_support.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_support.h Thu Oct 30 21:44:12 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
diff -r 000000000000 -r 950566b0e099 resources/IPSO_illuminance.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/IPSO_illuminance.cpp Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,62 @@ +// IPSO Illuminance sensor resource implementation + +#include "mbed.h" +#include "nsdl_support.h" + +#define ILLUM_RES_ID "3301/0/5700" +#define ILLUM_RES_RT "urn:X-ipso:illuminance" + +extern Serial pc; +uint8_t illum_max_age = 0; +uint8_t illum_content_type = 50; + +AnalogIn illumSensor(A1); +int illum_percent; +char illumPctString[5]; + +/* Only GET method allowed */ +static uint8_t illuminance_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; + illum_percent = illumSensor.read() * 100; + sprintf(illumPctString,"%d", illum_percent); + pc.printf("illum callback\r\n"); + pc.printf("illum percent %s\r\n", illumPctString); + + 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(illumPctString); + coap_res_ptr->payload_ptr = (uint8_t*)illumPctString; + + coap_res_ptr->content_type_ptr = &illum_content_type; + coap_res_ptr->content_type_len = sizeof(illum_content_type); + + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); + if(!coap_res_ptr->options_list_ptr) + { + pc.printf("cant alloc option list for max-age\r\n"); + coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover + } + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->max_age_ptr = &illum_max_age; + coap_res_ptr->options_list_ptr->max_age_len = sizeof(illum_max_age); + + sn_nsdl_send_coap_message(address, coap_res_ptr); + nsdl_free(coap_res_ptr->options_list_ptr); + coap_res_ptr->options_list_ptr = NULL; + coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory + + } + + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + + return 0; +} + +int create_IPSO_illuminance_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(ILLUM_RES_ID)-1, (uint8_t*)ILLUM_RES_ID, sizeof(ILLUM_RES_RT)-1, (uint8_t*)ILLUM_RES_RT, 0, &illuminance_resource_cb, (SN_GRS_GET_ALLOWED)); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r 950566b0e099 resources/IPSO_illuminance.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/IPSO_illuminance.h Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,10 @@ +// IPSO Illuminance Sensor implementation + +#ifndef IPSO_ILLUM_H +#define IPSO_ILLUM_H + +#include "nsdl_support.h" + +int create_IPSO_illuminance_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 000000000000 -r 950566b0e099 resources/IPSO_presence.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/IPSO_presence.cpp Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,113 @@ +// IPSO Presence sensor resource implementation + +#include "mbed.h" +#include "rtos.h" +#include "nsdl_support.h" + +#define PRESENCE_RES_ID "3302/0/5500" +#define PRESENCE_RES_RT "urn:X-ipso:presence" + +extern Serial pc; +uint8_t presence_max_age = 0; +uint8_t presence_content_type = 50; + +static uint8_t pres_obs_number = 0; +static uint8_t *pres_obs_token_ptr = NULL; +static uint8_t pres_obs_token_len = 0; + +DigitalIn presenceSensor(D2); +uint8_t presence = 0; +uint8_t last_presence = 0; +char presenceString[1]; + +static void pres_observe_thread(void const *args) + { + while (true) + { + wait(.1); + presence = presenceSensor.read(); + if((presence != last_presence) && pres_obs_number != 0 && pres_obs_token_ptr != NULL) + { + last_presence = presence; + pc.printf("presence: %d\r\n", presence); + pres_obs_number++; + sprintf(presenceString,"%d", presence); + if(sn_nsdl_send_observation_notification(pres_obs_token_ptr, pres_obs_token_len, (uint8_t*)presenceString, strlen(presenceString), &pres_obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) + pc.printf("Presence observation sending failed\r\n"); + else + pc.printf("Presence observation\r\n"); + } + } + } + + +/* Only GET method allowed */ +static uint8_t presence_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; + presence = presenceSensor.read(); + sprintf(presenceString,"%d", presence); + pc.printf("presence callback\r\n"); + pc.printf("presence state %s\r\n", presenceString); + + 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(presenceString); + coap_res_ptr->payload_ptr = (uint8_t*)presenceString; + + coap_res_ptr->content_type_ptr = &presence_content_type; + coap_res_ptr->content_type_len = sizeof(presence_content_type); + + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); + if(!coap_res_ptr->options_list_ptr) + { + pc.printf("cant alloc option list\r\n"); + coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover + } + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->max_age_ptr = &presence_max_age; + coap_res_ptr->options_list_ptr->max_age_len = sizeof(presence_max_age); + + if(received_coap_ptr->token_ptr) + { + pc.printf("Token included\r\n"); + if(pres_obs_token_ptr) + { + free(pres_obs_token_ptr); + pres_obs_token_ptr = 0; + } + pres_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); + if(pres_obs_token_ptr) + { + memcpy(pres_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); + pres_obs_token_len = received_coap_ptr->token_len; + } + } + + if(received_coap_ptr->options_list_ptr->observe) + { + coap_res_ptr->options_list_ptr->observe_ptr = &pres_obs_number; + coap_res_ptr->options_list_ptr->observe_len = 1; + pres_obs_number++; + } + + sn_nsdl_send_coap_message(address, coap_res_ptr); + nsdl_free(coap_res_ptr->options_list_ptr); + coap_res_ptr->options_list_ptr = NULL; + coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory + } + + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + + return 0; +} + +int create_IPSO_presence_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + static Thread exec_thread(pres_observe_thread); + + nsdl_create_dynamic_resource(resource_ptr, sizeof(PRESENCE_RES_ID)-1, (uint8_t*)PRESENCE_RES_ID, sizeof(PRESENCE_RES_RT)-1, (uint8_t*)PRESENCE_RES_RT, 1, &presence_resource_cb, (SN_GRS_GET_ALLOWED)); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r 950566b0e099 resources/IPSO_presence.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/IPSO_presence.h Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,10 @@ +// IPSO Presence Sensor implementation + +#ifndef IPSO_PRESENCE_H +#define IPSO_PRESENCE_H + +#include "nsdl_support.h" + +int create_IPSO_presence_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 000000000000 -r 950566b0e099 resources/LED_array.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/LED_array.cpp Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,115 @@ +// Light resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "ChainableLED.h" +#include "LED_array.h" + +#define LED_ARRAY_RES_ID "11100/0/5900" +#define LED_ARRAY_RES_RT "urn:X-mbed:LEDarray" + +extern Serial pc; +char led_value[] = {"0000000"}; //RRGGBBII + +#define LED_COUNT 1 + +ChainableLED led_chain(P0_0, P0_1, LED_COUNT); + +uint8_t LED_array_max_age = 0; +uint8_t LED_array_content_type = 0; + +void set_led_array(char *color_string) +{ + static uint8_t red, green, blue, index; + int color_int; + + sscanf(color_string, "%X", &color_int); + + index = color_int & 255; + blue = color_int >> 8 & 255; + green = color_int >> 16 & 255; + red = color_int >> 24 & 255; + + if(index > 0 and index <= LED_COUNT) + { + led_chain.setColorRGB(index-1, red, green, blue); + } + else if(index == 0) + { + for(int i = 0; i < LED_COUNT; i++) + { + led_chain.setColorRGB(i, red, green, blue); + } + } +} + + +/* Only GET and PUT method allowed */ +static uint8_t LED_array_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 array 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(led_value); + coap_res_ptr->payload_ptr = (uint8_t*)led_value; + + coap_res_ptr->content_type_ptr = &LED_array_content_type; + coap_res_ptr->content_type_len = sizeof(LED_array_content_type); + + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); + if(!coap_res_ptr->options_list_ptr) + { + pc.printf("cant alloc option list for max-age\r\n"); + coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover + } + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->max_age_ptr = &LED_array_max_age; + coap_res_ptr->options_list_ptr->max_age_len = sizeof(LED_array_max_age); + + sn_nsdl_send_coap_message(address, coap_res_ptr); + nsdl_free(coap_res_ptr->options_list_ptr); + coap_res_ptr->options_list_ptr = NULL; + coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory + + } + 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 == 8) + { + memcpy(led_value, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + + led_value[received_coap_ptr->payload_len] = '\0'; + pc.printf("PUT: %s\r\n",led_value); + + //call LED array update function here + set_led_array(led_value); + + 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_LED_array_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(LED_ARRAY_RES_ID)-1, (uint8_t*)LED_ARRAY_RES_ID, sizeof(LED_ARRAY_RES_RT)-1, (uint8_t*)LED_ARRAY_RES_RT, 0, &LED_array_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); + set_led_array("FF000000"); + wait(0.5); + set_led_array("00FF0000"); + wait(0.5); + set_led_array("0000FF00"); + wait(0.5); + set_led_array("00000000"); + + + return 0; +} \ No newline at end of file
diff -r 000000000000 -r 950566b0e099 resources/LED_array.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/LED_array.h Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,10 @@ +// RGB LED Array resource implementation + +#ifndef LED_ARRAY_H +#define LED_ARRAY_H + +#include "nsdl_support.h" + +int create_LED_array_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 000000000000 -r 950566b0e099 resources/LEDbar.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/LEDbar.cpp Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,91 @@ +// Light resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "LEDbar.h" +#include "LED_Bar.h" + +#define LEDBAR_RES_ID "11101/0/5901" +#define LEDBAR_RES_RT "urn:X-mbed:LEDbar" + +extern Serial pc; +char leds[] = {"0000000000"}; //GGGGGGGGYR + +LED_Bar bar(D7, D6); + +uint8_t ledbar_max_age = 0; +uint8_t ledbar_content_type = 0; + +void set_leds(char *leds) +{ +int i; + for (i=0; i<=9; i++) { + if(leds[i] == '1') + bar.setSingleLed((9-i),1); + else if(leds[i] == '0') + bar.setSingleLed((9-i),0); + } +} + + +/* Only GET and PUT method allowed */ +static uint8_t LEDbar_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; + + coap_res_ptr->content_type_ptr = &ledbar_content_type; + coap_res_ptr->content_type_len = sizeof(ledbar_content_type); + + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); + if(!coap_res_ptr->options_list_ptr) + { + pc.printf("cant alloc option list for max-age\r\n"); + coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover + } + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->max_age_ptr = &ledbar_max_age; + coap_res_ptr->options_list_ptr->max_age_len = sizeof(ledbar_max_age); + + sn_nsdl_send_coap_message(address, coap_res_ptr); + nsdl_free(coap_res_ptr->options_list_ptr); + coap_res_ptr->options_list_ptr = NULL; + coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory + + } + 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 == 10) + { + 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_LEDbar_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(LEDBAR_RES_ID)-1, (uint8_t*)LEDBAR_RES_ID, sizeof(LEDBAR_RES_RT)-1, (uint8_t*)LEDBAR_RES_RT, 0, &LEDbar_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); + set_leds("0000000000"); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r 950566b0e099 resources/LEDbar.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/LEDbar.h Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,10 @@ +// Light resource implementation + +#ifndef LEDBAR_H +#define LEDBAR_H + +#include "nsdl_support.h" + +int create_LEDbar_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 000000000000 -r 950566b0e099 resources/gas_sensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/gas_sensor.cpp Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,62 @@ +// gas sensor resource implementation + +#include "mbed.h" +#include "nsdl_support.h" + +#define GAS_RES_ID "11102/0/5700" +#define GAS_RES_RT "urn:X-mbed:gas-sensor" + +extern Serial pc; +uint8_t gas_max_age = 0; +uint8_t gas_content_type = 50; + +AnalogIn gasSensor(A0); +int gas_percent; +char gasPctString[5]; + +/* Only GET method allowed */ +static uint8_t gas_sensor_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; + gas_percent = gasSensor.read() * 100; + sprintf(gasPctString,"%d", gas_percent); + pc.printf("gas callback\r\n"); + pc.printf("gas percent %s\r\n", gasPctString); + + 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(gasPctString); + coap_res_ptr->payload_ptr = (uint8_t*)gasPctString; + + coap_res_ptr->content_type_ptr = &gas_content_type; + coap_res_ptr->content_type_len = sizeof(gas_content_type); + + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); + if(!coap_res_ptr->options_list_ptr) + { + pc.printf("cant alloc option list for max-age\r\n"); + coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover + } + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->max_age_ptr = &gas_max_age; + coap_res_ptr->options_list_ptr->max_age_len = sizeof(gas_max_age); + + sn_nsdl_send_coap_message(address, coap_res_ptr); + nsdl_free(coap_res_ptr->options_list_ptr); + coap_res_ptr->options_list_ptr = NULL; + coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory + + } + + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + + return 0; +} + +int create_gas_sensor_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(GAS_RES_ID)-1, (uint8_t*)GAS_RES_ID, sizeof(GAS_RES_RT)-1, (uint8_t*)GAS_RES_RT, 0, &gas_sensor_resource_cb, (SN_GRS_GET_ALLOWED)); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r 950566b0e099 resources/gas_sensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/gas_sensor.h Thu Oct 30 21:44:12 2014 +0000 @@ -0,0 +1,10 @@ +// IPSO Illuminance Sensor implementation + +#ifndef GAS_SENSOR_H +#define GAS_SENSOR_H + +#include "nsdl_support.h" + +int create_gas_sensor_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif