![](/media/cache/group/arm_mbed.PNG.50x50_q85.png)
Project with example IPSO resources for LED bar, Gas Sensor, and Light Sensor
Dependencies: EthernetInterface LED_Bar mbed-rtos mbed nsdl_lib
resources/IPSO_presence.cpp@4:69ea903414fe, 2014-10-25 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sat Oct 25 01:31:08 2014 +0000
- Revision:
- 4:69ea903414fe
- Parent:
- 1:76ab3521eb51
- Child:
- 5:3fa1d1f8b3f8
sync - new IP address
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 1:76ab3521eb51 | 1 | // IPSO Presence sensor resource implementation |
michaeljkoster | 1:76ab3521eb51 | 2 | |
michaeljkoster | 1:76ab3521eb51 | 3 | #include "mbed.h" |
michaeljkoster | 1:76ab3521eb51 | 4 | #include "nsdl_support.h" |
michaeljkoster | 1:76ab3521eb51 | 5 | |
michaeljkoster | 1:76ab3521eb51 | 6 | #define PRESENCE_RES_ID "3302/0/5500" |
michaeljkoster | 1:76ab3521eb51 | 7 | #define PRESENCE_RES_RT "urn:X-ipso:presence" |
michaeljkoster | 1:76ab3521eb51 | 8 | |
michaeljkoster | 1:76ab3521eb51 | 9 | extern Serial pc; |
michaeljkoster | 1:76ab3521eb51 | 10 | uint8_t presence_max_age = 0; |
michaeljkoster | 1:76ab3521eb51 | 11 | uint8_t presence_content_type = 50; |
michaeljkoster | 1:76ab3521eb51 | 12 | |
michaeljkoster | 1:76ab3521eb51 | 13 | DigitalIn presenceSensor(D2); |
michaeljkoster | 1:76ab3521eb51 | 14 | bool presence; |
michaeljkoster | 1:76ab3521eb51 | 15 | char presenceString[2]; |
michaeljkoster | 1:76ab3521eb51 | 16 | |
michaeljkoster | 1:76ab3521eb51 | 17 | /* Only GET method allowed */ |
michaeljkoster | 1:76ab3521eb51 | 18 | static uint8_t presence_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
michaeljkoster | 1:76ab3521eb51 | 19 | { |
michaeljkoster | 1:76ab3521eb51 | 20 | sn_coap_hdr_s *coap_res_ptr = 0; |
michaeljkoster | 1:76ab3521eb51 | 21 | presence = presenceSensor.read(); |
michaeljkoster | 1:76ab3521eb51 | 22 | sprintf(presenceString,"%d", presence); |
michaeljkoster | 4:69ea903414fe | 23 | pc.printf("presence callback\r\n"); |
michaeljkoster | 4:69ea903414fe | 24 | pc.printf("presence state %s\r\n", presenceString); |
michaeljkoster | 1:76ab3521eb51 | 25 | |
michaeljkoster | 1:76ab3521eb51 | 26 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) |
michaeljkoster | 1:76ab3521eb51 | 27 | { |
michaeljkoster | 1:76ab3521eb51 | 28 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
michaeljkoster | 1:76ab3521eb51 | 29 | |
michaeljkoster | 1:76ab3521eb51 | 30 | coap_res_ptr->payload_len = strlen(presenceString); |
michaeljkoster | 1:76ab3521eb51 | 31 | coap_res_ptr->payload_ptr = (uint8_t*)presenceString; |
michaeljkoster | 1:76ab3521eb51 | 32 | |
michaeljkoster | 1:76ab3521eb51 | 33 | coap_res_ptr->content_type_ptr = &presence_content_type; |
michaeljkoster | 1:76ab3521eb51 | 34 | coap_res_ptr->content_type_len = sizeof(presence_content_type); |
michaeljkoster | 1:76ab3521eb51 | 35 | |
michaeljkoster | 1:76ab3521eb51 | 36 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); |
michaeljkoster | 1:76ab3521eb51 | 37 | if(!coap_res_ptr->options_list_ptr) |
michaeljkoster | 1:76ab3521eb51 | 38 | { |
michaeljkoster | 1:76ab3521eb51 | 39 | pc.printf("cant alloc option list for max-age\r\n"); |
michaeljkoster | 1:76ab3521eb51 | 40 | coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover |
michaeljkoster | 1:76ab3521eb51 | 41 | } |
michaeljkoster | 1:76ab3521eb51 | 42 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
michaeljkoster | 1:76ab3521eb51 | 43 | coap_res_ptr->options_list_ptr->max_age_ptr = &presence_max_age; |
michaeljkoster | 1:76ab3521eb51 | 44 | coap_res_ptr->options_list_ptr->max_age_len = sizeof(presence_max_age); |
michaeljkoster | 1:76ab3521eb51 | 45 | |
michaeljkoster | 1:76ab3521eb51 | 46 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
michaeljkoster | 1:76ab3521eb51 | 47 | nsdl_free(coap_res_ptr->options_list_ptr); |
michaeljkoster | 1:76ab3521eb51 | 48 | coap_res_ptr->options_list_ptr = NULL; |
michaeljkoster | 1:76ab3521eb51 | 49 | coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory |
michaeljkoster | 1:76ab3521eb51 | 50 | |
michaeljkoster | 1:76ab3521eb51 | 51 | } |
michaeljkoster | 1:76ab3521eb51 | 52 | |
michaeljkoster | 1:76ab3521eb51 | 53 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
michaeljkoster | 1:76ab3521eb51 | 54 | |
michaeljkoster | 1:76ab3521eb51 | 55 | return 0; |
michaeljkoster | 1:76ab3521eb51 | 56 | } |
michaeljkoster | 1:76ab3521eb51 | 57 | |
michaeljkoster | 1:76ab3521eb51 | 58 | int create_IPSO_presence_resource(sn_nsdl_resource_info_s *resource_ptr) |
michaeljkoster | 1:76ab3521eb51 | 59 | { |
michaeljkoster | 1:76ab3521eb51 | 60 | 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, 0, &presence_resource_cb, (SN_GRS_GET_ALLOWED)); |
michaeljkoster | 1:76ab3521eb51 | 61 | return 0; |
michaeljkoster | 1:76ab3521eb51 | 62 | } |