Red Hat Summit NanoService Demo for LPC1768 App Board using OMA Lightweight Objects

Dependencies:   Beep C12832_lcd EthernetInterface LM75B MMA7660 mbed-rtos mbed nsdl_lib

Fork of LWM2M_NanoService_Ethernet by MBED_DEMOS

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    "306/0/5850"
00009 
00010 extern Serial pc;
00011 static Beep buzzer(p26);
00012 
00013 static DigitalOut r(p21);
00014 static DigitalOut b(p22);
00015 static Ticker flash;
00016 
00017 void rbHandler(void)
00018 {
00019     r = !r;
00020     b = !b;
00021     buzzer.beep(1000, 0.1f);
00022 }
00023 
00024 /* Only GET and PUT method allowed */
00025 static uint8_t relay_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
00026 {
00027     sn_coap_hdr_s *coap_res_ptr = 0;
00028     static uint8_t relay_state = '0';
00029 
00030     pc.printf("relay callback\r\n");
00031 
00032     if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
00033     {
00034         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
00035 
00036         coap_res_ptr->payload_len = 1;
00037         coap_res_ptr->payload_ptr = &relay_state;
00038         sn_nsdl_send_coap_message(address, coap_res_ptr);
00039     }
00040     else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
00041     {
00042         if(received_coap_ptr->payload_len)
00043         {
00044             if(*(received_coap_ptr->payload_ptr) == '1')
00045             {
00046                 flash.attach(&rbHandler, 0.2f);
00047                 r = 1;
00048                 b = 0;
00049                 relay_state = '1';
00050                 
00051             }
00052             else if(*(received_coap_ptr->payload_ptr) == '0')
00053             {
00054                 buzzer.nobeep();
00055                 flash.detach();
00056                 r = 1;
00057                 b = 1;
00058                 relay_state = '0';
00059             }
00060             coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
00061             sn_nsdl_send_coap_message(address, coap_res_ptr);
00062         }
00063     }
00064 
00065     sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
00066 
00067     return 0;
00068 }
00069 
00070 int create_relay_resource(sn_nsdl_resource_info_s *resource_ptr)
00071 {
00072     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));
00073     r = 1;
00074     b = 1;
00075     return 0;
00076 }