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