![](/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@5:3fa1d1f8b3f8, 2014-10-25 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sat Oct 25 03:16:58 2014 +0000
- Revision:
- 5:3fa1d1f8b3f8
- Parent:
- 4:69ea903414fe
- Child:
- 6:4fa917ca6aa4
Added observe to presence sensor resource
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 | 5:3fa1d1f8b3f8 | 4 | #include "rtos.h" |
michaeljkoster | 1:76ab3521eb51 | 5 | #include "nsdl_support.h" |
michaeljkoster | 1:76ab3521eb51 | 6 | |
michaeljkoster | 1:76ab3521eb51 | 7 | #define PRESENCE_RES_ID "3302/0/5500" |
michaeljkoster | 1:76ab3521eb51 | 8 | #define PRESENCE_RES_RT "urn:X-ipso:presence" |
michaeljkoster | 1:76ab3521eb51 | 9 | |
michaeljkoster | 1:76ab3521eb51 | 10 | extern Serial pc; |
michaeljkoster | 1:76ab3521eb51 | 11 | uint8_t presence_max_age = 0; |
michaeljkoster | 1:76ab3521eb51 | 12 | uint8_t presence_content_type = 50; |
michaeljkoster | 1:76ab3521eb51 | 13 | |
michaeljkoster | 5:3fa1d1f8b3f8 | 14 | static uint8_t pres_obs_number = 0; |
michaeljkoster | 5:3fa1d1f8b3f8 | 15 | static uint8_t *pres_obs_token_ptr = NULL; |
michaeljkoster | 5:3fa1d1f8b3f8 | 16 | static uint8_t pres_obs_token_len = 0; |
michaeljkoster | 5:3fa1d1f8b3f8 | 17 | |
michaeljkoster | 1:76ab3521eb51 | 18 | DigitalIn presenceSensor(D2); |
michaeljkoster | 1:76ab3521eb51 | 19 | bool presence; |
michaeljkoster | 1:76ab3521eb51 | 20 | char presenceString[2]; |
michaeljkoster | 1:76ab3521eb51 | 21 | |
michaeljkoster | 5:3fa1d1f8b3f8 | 22 | static void pres_observe_thread(void const *args) |
michaeljkoster | 5:3fa1d1f8b3f8 | 23 | { |
michaeljkoster | 5:3fa1d1f8b3f8 | 24 | int32_t time = 0; |
michaeljkoster | 5:3fa1d1f8b3f8 | 25 | while (true) |
michaeljkoster | 5:3fa1d1f8b3f8 | 26 | { |
michaeljkoster | 5:3fa1d1f8b3f8 | 27 | wait(1); |
michaeljkoster | 5:3fa1d1f8b3f8 | 28 | time++; |
michaeljkoster | 5:3fa1d1f8b3f8 | 29 | //sn_nsdl_exec(time); |
michaeljkoster | 5:3fa1d1f8b3f8 | 30 | if((!(time % 10)) && pres_obs_number != 0 && pres_obs_token_ptr != NULL) |
michaeljkoster | 5:3fa1d1f8b3f8 | 31 | { |
michaeljkoster | 5:3fa1d1f8b3f8 | 32 | pres_obs_number++; |
michaeljkoster | 5:3fa1d1f8b3f8 | 33 | sprintf(presenceString,"%d", presenceSensor.read()); |
michaeljkoster | 5:3fa1d1f8b3f8 | 34 | if(sn_nsdl_send_observation_notification(pres_obs_token_ptr, pres_obs_token_len, (uint8_t*)presenceString, sizeof((uint8_t*)presenceString)-1, &pres_obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) |
michaeljkoster | 5:3fa1d1f8b3f8 | 35 | pc.printf("Presence observation sending failed\r\n"); |
michaeljkoster | 5:3fa1d1f8b3f8 | 36 | else |
michaeljkoster | 5:3fa1d1f8b3f8 | 37 | pc.printf("Presence observation\r\n"); |
michaeljkoster | 5:3fa1d1f8b3f8 | 38 | } |
michaeljkoster | 5:3fa1d1f8b3f8 | 39 | } |
michaeljkoster | 5:3fa1d1f8b3f8 | 40 | } |
michaeljkoster | 5:3fa1d1f8b3f8 | 41 | |
michaeljkoster | 5:3fa1d1f8b3f8 | 42 | |
michaeljkoster | 1:76ab3521eb51 | 43 | /* Only GET method allowed */ |
michaeljkoster | 1:76ab3521eb51 | 44 | 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 | 45 | { |
michaeljkoster | 1:76ab3521eb51 | 46 | sn_coap_hdr_s *coap_res_ptr = 0; |
michaeljkoster | 1:76ab3521eb51 | 47 | presence = presenceSensor.read(); |
michaeljkoster | 1:76ab3521eb51 | 48 | sprintf(presenceString,"%d", presence); |
michaeljkoster | 4:69ea903414fe | 49 | pc.printf("presence callback\r\n"); |
michaeljkoster | 4:69ea903414fe | 50 | pc.printf("presence state %s\r\n", presenceString); |
michaeljkoster | 1:76ab3521eb51 | 51 | |
michaeljkoster | 1:76ab3521eb51 | 52 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) |
michaeljkoster | 1:76ab3521eb51 | 53 | { |
michaeljkoster | 1:76ab3521eb51 | 54 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
michaeljkoster | 5:3fa1d1f8b3f8 | 55 | |
michaeljkoster | 1:76ab3521eb51 | 56 | coap_res_ptr->payload_len = strlen(presenceString); |
michaeljkoster | 1:76ab3521eb51 | 57 | coap_res_ptr->payload_ptr = (uint8_t*)presenceString; |
michaeljkoster | 1:76ab3521eb51 | 58 | |
michaeljkoster | 1:76ab3521eb51 | 59 | coap_res_ptr->content_type_ptr = &presence_content_type; |
michaeljkoster | 1:76ab3521eb51 | 60 | coap_res_ptr->content_type_len = sizeof(presence_content_type); |
michaeljkoster | 1:76ab3521eb51 | 61 | |
michaeljkoster | 1:76ab3521eb51 | 62 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); |
michaeljkoster | 1:76ab3521eb51 | 63 | if(!coap_res_ptr->options_list_ptr) |
michaeljkoster | 1:76ab3521eb51 | 64 | { |
michaeljkoster | 5:3fa1d1f8b3f8 | 65 | pc.printf("cant alloc option list\r\n"); |
michaeljkoster | 1:76ab3521eb51 | 66 | coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover |
michaeljkoster | 1:76ab3521eb51 | 67 | } |
michaeljkoster | 1:76ab3521eb51 | 68 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
michaeljkoster | 1:76ab3521eb51 | 69 | coap_res_ptr->options_list_ptr->max_age_ptr = &presence_max_age; |
michaeljkoster | 1:76ab3521eb51 | 70 | coap_res_ptr->options_list_ptr->max_age_len = sizeof(presence_max_age); |
michaeljkoster | 5:3fa1d1f8b3f8 | 71 | |
michaeljkoster | 5:3fa1d1f8b3f8 | 72 | if(received_coap_ptr->token_ptr) |
michaeljkoster | 5:3fa1d1f8b3f8 | 73 | { |
michaeljkoster | 5:3fa1d1f8b3f8 | 74 | pc.printf("Token included\r\n"); |
michaeljkoster | 5:3fa1d1f8b3f8 | 75 | if(pres_obs_token_ptr) |
michaeljkoster | 5:3fa1d1f8b3f8 | 76 | { |
michaeljkoster | 5:3fa1d1f8b3f8 | 77 | free(pres_obs_token_ptr); |
michaeljkoster | 5:3fa1d1f8b3f8 | 78 | pres_obs_token_ptr = 0; |
michaeljkoster | 5:3fa1d1f8b3f8 | 79 | } |
michaeljkoster | 5:3fa1d1f8b3f8 | 80 | pres_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); |
michaeljkoster | 5:3fa1d1f8b3f8 | 81 | if(pres_obs_token_ptr) |
michaeljkoster | 5:3fa1d1f8b3f8 | 82 | { |
michaeljkoster | 5:3fa1d1f8b3f8 | 83 | memcpy(pres_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); |
michaeljkoster | 5:3fa1d1f8b3f8 | 84 | pres_obs_token_len = received_coap_ptr->token_len; |
michaeljkoster | 5:3fa1d1f8b3f8 | 85 | } |
michaeljkoster | 5:3fa1d1f8b3f8 | 86 | } |
michaeljkoster | 1:76ab3521eb51 | 87 | |
michaeljkoster | 5:3fa1d1f8b3f8 | 88 | if(received_coap_ptr->options_list_ptr->observe) |
michaeljkoster | 5:3fa1d1f8b3f8 | 89 | { |
michaeljkoster | 5:3fa1d1f8b3f8 | 90 | //coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); |
michaeljkoster | 5:3fa1d1f8b3f8 | 91 | //memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
michaeljkoster | 5:3fa1d1f8b3f8 | 92 | coap_res_ptr->options_list_ptr->observe_ptr = &pres_obs_number; |
michaeljkoster | 5:3fa1d1f8b3f8 | 93 | coap_res_ptr->options_list_ptr->observe_len = 1; |
michaeljkoster | 5:3fa1d1f8b3f8 | 94 | pres_obs_number++; |
michaeljkoster | 5:3fa1d1f8b3f8 | 95 | } |
michaeljkoster | 5:3fa1d1f8b3f8 | 96 | |
michaeljkoster | 1:76ab3521eb51 | 97 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
michaeljkoster | 1:76ab3521eb51 | 98 | nsdl_free(coap_res_ptr->options_list_ptr); |
michaeljkoster | 1:76ab3521eb51 | 99 | coap_res_ptr->options_list_ptr = NULL; |
michaeljkoster | 1:76ab3521eb51 | 100 | coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory |
michaeljkoster | 1:76ab3521eb51 | 101 | |
michaeljkoster | 1:76ab3521eb51 | 102 | } |
michaeljkoster | 1:76ab3521eb51 | 103 | |
michaeljkoster | 1:76ab3521eb51 | 104 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
michaeljkoster | 1:76ab3521eb51 | 105 | |
michaeljkoster | 1:76ab3521eb51 | 106 | return 0; |
michaeljkoster | 1:76ab3521eb51 | 107 | } |
michaeljkoster | 1:76ab3521eb51 | 108 | |
michaeljkoster | 1:76ab3521eb51 | 109 | int create_IPSO_presence_resource(sn_nsdl_resource_info_s *resource_ptr) |
michaeljkoster | 1:76ab3521eb51 | 110 | { |
michaeljkoster | 5:3fa1d1f8b3f8 | 111 | static Thread exec_thread(pres_observe_thread); |
michaeljkoster | 5:3fa1d1f8b3f8 | 112 | |
michaeljkoster | 5:3fa1d1f8b3f8 | 113 | 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)); |
michaeljkoster | 1:76ab3521eb51 | 114 | return 0; |
michaeljkoster | 1:76ab3521eb51 | 115 | } |