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