NanoService Example for u-blox Cellular modems

Dependencies:   Beep LM75B MMA7660 mbed nsdl_lib

Fork of NSDL_HelloWorld by Sensinode

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers relay.cpp Source File

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 static Beep buzzer(p26);
00011 
00012 /* Only GET and PUT method allowed */
00013 static uint8_t relay_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
00014 {
00015     sn_coap_hdr_s *coap_res_ptr = 0;
00016     static uint8_t relay_state = '0';
00017 
00018     printf("relay callback\r\n");
00019 
00020     if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
00021     {
00022         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
00023 
00024         coap_res_ptr->payload_len = 1;
00025         coap_res_ptr->payload_ptr = &relay_state;
00026         sn_nsdl_send_coap_message(address, coap_res_ptr);
00027     }
00028     else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
00029     {
00030         if(received_coap_ptr->payload_len)
00031         {
00032             if(*(received_coap_ptr->payload_ptr) == '1')
00033             {
00034                 buzzer.beep(1000,0);
00035                 relay_state = '1';
00036                 
00037             }
00038             else if(*(received_coap_ptr->payload_ptr) == '0')
00039             {
00040                 buzzer.nobeep();
00041                 relay_state = '0';
00042             }
00043             coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
00044             sn_nsdl_send_coap_message(address, coap_res_ptr);
00045         }
00046     }
00047 
00048     sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
00049 
00050     return 0;
00051 }
00052 
00053 int create_relay_resource(sn_nsdl_resource_info_s *resource_ptr)
00054 {
00055     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));
00056     return 0;
00057 }