Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface PololuLedStripx mbed-rtos mbed nanoservice_client_1_12_X
Fork of LPC1768_LWM2M_Client by
resources/relay.cpp@0:9101343a70cd, 2014-07-22 (annotated)
- Committer:
- michaeljkoster
- Date:
- Tue Jul 22 23:58:23 2014 +0000
- Revision:
- 0:9101343a70cd
Demo with lwm2m
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| michaeljkoster | 0:9101343a70cd | 1 | // GPS resource implementation |
| michaeljkoster | 0:9101343a70cd | 2 | |
| michaeljkoster | 0:9101343a70cd | 3 | #include "mbed.h" |
| michaeljkoster | 0:9101343a70cd | 4 | #include "nsdl_support.h" |
| michaeljkoster | 0:9101343a70cd | 5 | #include "relay.h" |
| michaeljkoster | 0:9101343a70cd | 6 | #include "Beep.h" |
| michaeljkoster | 0:9101343a70cd | 7 | |
| michaeljkoster | 0:9101343a70cd | 8 | #define RELAY_RES_ID "beep/0/on" |
| michaeljkoster | 0:9101343a70cd | 9 | |
| michaeljkoster | 0:9101343a70cd | 10 | extern Serial pc; |
| michaeljkoster | 0:9101343a70cd | 11 | static Beep buzzer(p26); |
| michaeljkoster | 0:9101343a70cd | 12 | |
| michaeljkoster | 0:9101343a70cd | 13 | /* Only GET and PUT method allowed */ |
| michaeljkoster | 0:9101343a70cd | 14 | static uint8_t relay_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
| michaeljkoster | 0:9101343a70cd | 15 | { |
| michaeljkoster | 0:9101343a70cd | 16 | sn_coap_hdr_s *coap_res_ptr = 0; |
| michaeljkoster | 0:9101343a70cd | 17 | static uint8_t relay_state = '0'; |
| michaeljkoster | 0:9101343a70cd | 18 | |
| michaeljkoster | 0:9101343a70cd | 19 | pc.printf("relay callback\r\n"); |
| michaeljkoster | 0:9101343a70cd | 20 | |
| michaeljkoster | 0:9101343a70cd | 21 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) |
| michaeljkoster | 0:9101343a70cd | 22 | { |
| michaeljkoster | 0:9101343a70cd | 23 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
| michaeljkoster | 0:9101343a70cd | 24 | |
| michaeljkoster | 0:9101343a70cd | 25 | coap_res_ptr->payload_len = 1; |
| michaeljkoster | 0:9101343a70cd | 26 | coap_res_ptr->payload_ptr = &relay_state; |
| michaeljkoster | 0:9101343a70cd | 27 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
| michaeljkoster | 0:9101343a70cd | 28 | } |
| michaeljkoster | 0:9101343a70cd | 29 | else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) |
| michaeljkoster | 0:9101343a70cd | 30 | { |
| michaeljkoster | 0:9101343a70cd | 31 | if(received_coap_ptr->payload_len) |
| michaeljkoster | 0:9101343a70cd | 32 | { |
| michaeljkoster | 0:9101343a70cd | 33 | if(*(received_coap_ptr->payload_ptr) == '1') |
| michaeljkoster | 0:9101343a70cd | 34 | { |
| michaeljkoster | 0:9101343a70cd | 35 | buzzer.beep(1000,0); |
| michaeljkoster | 0:9101343a70cd | 36 | relay_state = '1'; |
| michaeljkoster | 0:9101343a70cd | 37 | |
| michaeljkoster | 0:9101343a70cd | 38 | } |
| michaeljkoster | 0:9101343a70cd | 39 | else if(*(received_coap_ptr->payload_ptr) == '0') |
| michaeljkoster | 0:9101343a70cd | 40 | { |
| michaeljkoster | 0:9101343a70cd | 41 | buzzer.nobeep(); |
| michaeljkoster | 0:9101343a70cd | 42 | relay_state = '0'; |
| michaeljkoster | 0:9101343a70cd | 43 | } |
| michaeljkoster | 0:9101343a70cd | 44 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); |
| michaeljkoster | 0:9101343a70cd | 45 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
| michaeljkoster | 0:9101343a70cd | 46 | } |
| michaeljkoster | 0:9101343a70cd | 47 | } |
| michaeljkoster | 0:9101343a70cd | 48 | |
| michaeljkoster | 0:9101343a70cd | 49 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
| michaeljkoster | 0:9101343a70cd | 50 | |
| michaeljkoster | 0:9101343a70cd | 51 | return 0; |
| michaeljkoster | 0:9101343a70cd | 52 | } |
| michaeljkoster | 0:9101343a70cd | 53 | |
| michaeljkoster | 0:9101343a70cd | 54 | int create_relay_resource(sn_nsdl_resource_info_s *resource_ptr) |
| michaeljkoster | 0:9101343a70cd | 55 | { |
| michaeljkoster | 0:9101343a70cd | 56 | 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)); |
| michaeljkoster | 0:9101343a70cd | 57 | return 0; |
| michaeljkoster | 0:9101343a70cd | 58 | } |
