NanoService Device Library Hello World
Dependencies: Beep C12832_lcd EthernetInterface LM75B MMA7660 mbed-rtos mbed nsdl_lib
relay.cpp
00001 // GPS resource implementation 00002 00003 #include "mbed.h" 00004 #include "nsdl_support.h" 00005 #include "relay.h" 00006 #include "Beep.h" 00007 00008 #define RELAY_RES_ID "beep/0/on" 00009 00010 extern Serial pc; 00011 static Beep buzzer(p26); 00012 00013 /* Only GET and PUT method allowed */ 00014 static uint8_t relay_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) 00015 { 00016 sn_coap_hdr_s *coap_res_ptr = 0; 00017 static uint8_t relay_state = '0'; 00018 00019 pc.printf("relay callback\r\n"); 00020 00021 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) 00022 { 00023 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); 00024 00025 coap_res_ptr->payload_len = 1; 00026 coap_res_ptr->payload_ptr = &relay_state; 00027 sn_nsdl_send_coap_message(address, coap_res_ptr); 00028 } 00029 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) 00030 { 00031 if(received_coap_ptr->payload_len) 00032 { 00033 if(*(received_coap_ptr->payload_ptr) == '1') 00034 { 00035 buzzer.beep(1000,0); 00036 relay_state = '1'; 00037 00038 } 00039 else if(*(received_coap_ptr->payload_ptr) == '0') 00040 { 00041 buzzer.nobeep(); 00042 relay_state = '0'; 00043 } 00044 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); 00045 sn_nsdl_send_coap_message(address, coap_res_ptr); 00046 } 00047 } 00048 00049 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); 00050 00051 return 0; 00052 } 00053 00054 int create_relay_resource(sn_nsdl_resource_info_s *resource_ptr) 00055 { 00056 nsdl_create_dynamic_resource(resource_ptr, sizeof(RELAY_RES_ID)-1, (uint8_t*)RELAY_RES_ID, 0, 0, 0, &relay_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); 00057 return 0; 00058 }
Generated on Wed Jul 13 2022 06:16:52 by 1.7.2