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.
mesh_led_control_example.cpp@54:34cbb2c2155c, 2017-09-22 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Sep 22 10:30:13 2017 +0100
- Revision:
- 54:34cbb2c2155c
- Parent:
- 52:27692043e962
- Child:
- 59:3e1533ffb4f5
Merge pull request #131 from soramame21/master
Fix an incorrect multicast address.
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-mesh-minimal
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| mbed_official | 52:27692043e962 | 1 | /* | 
| mbed_official | 52:27692043e962 | 2 | * Copyright (c) 2016 ARM Limited. All rights reserved. | 
| mbed_official | 52:27692043e962 | 3 | * SPDX-License-Identifier: Apache-2.0 | 
| mbed_official | 52:27692043e962 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may | 
| mbed_official | 52:27692043e962 | 5 | * not use this file except in compliance with the License. | 
| mbed_official | 52:27692043e962 | 6 | * You may obtain a copy of the License at | 
| mbed_official | 52:27692043e962 | 7 | * | 
| mbed_official | 52:27692043e962 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| mbed_official | 52:27692043e962 | 9 | * | 
| mbed_official | 52:27692043e962 | 10 | * Unless required by applicable law or agreed to in writing, software | 
| mbed_official | 52:27692043e962 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT | 
| mbed_official | 52:27692043e962 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| mbed_official | 52:27692043e962 | 13 | * See the License for the specific language governing permissions and | 
| mbed_official | 52:27692043e962 | 14 | * limitations under the License. | 
| mbed_official | 52:27692043e962 | 15 | */ | 
| mbed_official | 52:27692043e962 | 16 | #include "mbed.h" | 
| mbed_official | 52:27692043e962 | 17 | #include "nanostack/socket_api.h" | 
| mbed_official | 52:27692043e962 | 18 | #include "mesh_led_control_example.h" | 
| mbed_official | 52:27692043e962 | 19 | #include "common_functions.h" | 
| mbed_official | 52:27692043e962 | 20 | #include "ip6string.h" | 
| mbed_official | 52:27692043e962 | 21 | #include "mbed-trace/mbed_trace.h" | 
| mbed_official | 52:27692043e962 | 22 | |
| mbed_official | 52:27692043e962 | 23 | static void init_socket(); | 
| mbed_official | 52:27692043e962 | 24 | static void handle_socket(); | 
| mbed_official | 52:27692043e962 | 25 | static void receive(); | 
| mbed_official | 52:27692043e962 | 26 | static void my_button_isr(); | 
| mbed_official | 52:27692043e962 | 27 | static void send_message(); | 
| mbed_official | 52:27692043e962 | 28 | static void blink(); | 
| mbed_official | 52:27692043e962 | 29 | static void update_state(uint8_t state); | 
| mbed_official | 52:27692043e962 | 30 | static void handle_message(char* msg); | 
| mbed_official | 52:27692043e962 | 31 | |
| mbed_official | 52:27692043e962 | 32 | // mesh local multicast to all nodes | 
| mbed_official | 54:34cbb2c2155c | 33 | #define multicast_addr_str "ff02::1" | 
| mbed_official | 52:27692043e962 | 34 | #define TRACE_GROUP "example" | 
| mbed_official | 52:27692043e962 | 35 | #define UDP_PORT 1234 | 
| mbed_official | 52:27692043e962 | 36 | #define MESSAGE_WAIT_TIMEOUT (30.0) | 
| mbed_official | 52:27692043e962 | 37 | #define MASTER_GROUP 0 | 
| mbed_official | 52:27692043e962 | 38 | #define MY_GROUP 1 | 
| mbed_official | 52:27692043e962 | 39 | |
| mbed_official | 52:27692043e962 | 40 | DigitalOut led_1(MBED_CONF_APP_LED, 1); | 
| mbed_official | 52:27692043e962 | 41 | InterruptIn my_button(MBED_CONF_APP_BUTTON); | 
| mbed_official | 52:27692043e962 | 42 | DigitalOut output(D3, 1); | 
| mbed_official | 52:27692043e962 | 43 | Timeout messageTimeout; | 
| mbed_official | 52:27692043e962 | 44 | |
| mbed_official | 52:27692043e962 | 45 | NetworkInterface * network_if; | 
| mbed_official | 52:27692043e962 | 46 | UDPSocket* my_socket; | 
| mbed_official | 52:27692043e962 | 47 | // queue for sending messages from button press. | 
| mbed_official | 52:27692043e962 | 48 | EventQueue queue; | 
| mbed_official | 52:27692043e962 | 49 | // for LED blinking | 
| mbed_official | 52:27692043e962 | 50 | Ticker ticker; | 
| mbed_official | 52:27692043e962 | 51 | |
| mbed_official | 52:27692043e962 | 52 | uint8_t multi_cast_addr[16] = {0}; | 
| mbed_official | 52:27692043e962 | 53 | uint8_t receive_buffer[20]; | 
| mbed_official | 52:27692043e962 | 54 | // how many hops the multicast message can go | 
| mbed_official | 52:27692043e962 | 55 | static const int16_t multicast_hops = 10; | 
| mbed_official | 52:27692043e962 | 56 | bool button_status = 0; | 
| mbed_official | 52:27692043e962 | 57 | |
| mbed_official | 52:27692043e962 | 58 | void start_mesh_led_control_example(NetworkInterface * interface){ | 
| mbed_official | 52:27692043e962 | 59 | tr_debug("start_mesh_led_control_example()"); | 
| mbed_official | 52:27692043e962 | 60 | MBED_ASSERT(MBED_CONF_APP_LED != NC); | 
| mbed_official | 52:27692043e962 | 61 | MBED_ASSERT(MBED_CONF_APP_BUTTON != NC); | 
| mbed_official | 52:27692043e962 | 62 | |
| mbed_official | 52:27692043e962 | 63 | network_if = interface; | 
| mbed_official | 52:27692043e962 | 64 | stoip6(multicast_addr_str, strlen(multicast_addr_str), multi_cast_addr); | 
| mbed_official | 52:27692043e962 | 65 | init_socket(); | 
| mbed_official | 52:27692043e962 | 66 | } | 
| mbed_official | 52:27692043e962 | 67 | |
| mbed_official | 52:27692043e962 | 68 | static void messageTimeoutCallback() | 
| mbed_official | 52:27692043e962 | 69 | { | 
| mbed_official | 52:27692043e962 | 70 | send_message(); | 
| mbed_official | 52:27692043e962 | 71 | } | 
| mbed_official | 52:27692043e962 | 72 | |
| mbed_official | 52:27692043e962 | 73 | static void blink() { | 
| mbed_official | 52:27692043e962 | 74 | led_1 = !led_1; | 
| mbed_official | 52:27692043e962 | 75 | } | 
| mbed_official | 52:27692043e962 | 76 | |
| mbed_official | 52:27692043e962 | 77 | void start_blinking() { | 
| mbed_official | 52:27692043e962 | 78 | ticker.attach(blink, 1.0); | 
| mbed_official | 52:27692043e962 | 79 | } | 
| mbed_official | 52:27692043e962 | 80 | |
| mbed_official | 52:27692043e962 | 81 | void cancel_blinking() { | 
| mbed_official | 52:27692043e962 | 82 | ticker.detach(); | 
| mbed_official | 52:27692043e962 | 83 | led_1=1; | 
| mbed_official | 52:27692043e962 | 84 | } | 
| mbed_official | 52:27692043e962 | 85 | |
| mbed_official | 52:27692043e962 | 86 | static void send_message() { | 
| mbed_official | 52:27692043e962 | 87 | tr_debug("send msg %d", button_status); | 
| mbed_official | 52:27692043e962 | 88 | |
| mbed_official | 52:27692043e962 | 89 | char buf[20]; | 
| mbed_official | 52:27692043e962 | 90 | int length; | 
| mbed_official | 52:27692043e962 | 91 | |
| mbed_official | 52:27692043e962 | 92 | /** | 
| mbed_official | 52:27692043e962 | 93 | * Multicast control message is a NUL terminated string of semicolon separated | 
| mbed_official | 52:27692043e962 | 94 | * <field identifier>:<value> pairs. | 
| mbed_official | 52:27692043e962 | 95 | * | 
| mbed_official | 52:27692043e962 | 96 | * Light control message format: | 
| mbed_official | 52:27692043e962 | 97 | * t:lights;g:<group_id>;s:<1|0>;\0 | 
| mbed_official | 52:27692043e962 | 98 | */ | 
| mbed_official | 52:27692043e962 | 99 | length = snprintf(buf, sizeof(buf), "t:lights;g:%03d;s:%s;", MY_GROUP, (button_status ? "1" : "0")) + 1; | 
| mbed_official | 52:27692043e962 | 100 | MBED_ASSERT(length > 0); | 
| mbed_official | 52:27692043e962 | 101 | tr_debug("Sending lightcontrol message, %d bytes: %s", length, buf); | 
| mbed_official | 52:27692043e962 | 102 | SocketAddress send_sockAddr(multi_cast_addr, NSAPI_IPv6, UDP_PORT); | 
| mbed_official | 52:27692043e962 | 103 | my_socket->sendto(send_sockAddr, buf, 20); | 
| mbed_official | 52:27692043e962 | 104 | //After message is sent, it is received from the network | 
| mbed_official | 52:27692043e962 | 105 | } | 
| mbed_official | 52:27692043e962 | 106 | |
| mbed_official | 52:27692043e962 | 107 | // As this comes from isr, we cannot use printing or network functions directly from here. | 
| mbed_official | 52:27692043e962 | 108 | static void my_button_isr() { | 
| mbed_official | 52:27692043e962 | 109 | button_status = !button_status; | 
| mbed_official | 52:27692043e962 | 110 | queue.call(send_message); | 
| mbed_official | 52:27692043e962 | 111 | } | 
| mbed_official | 52:27692043e962 | 112 | |
| mbed_official | 52:27692043e962 | 113 | static void update_state(uint8_t state) { | 
| mbed_official | 52:27692043e962 | 114 | if (state == 1) { | 
| mbed_official | 52:27692043e962 | 115 | tr_debug("Turning led on\n"); | 
| mbed_official | 52:27692043e962 | 116 | led_1 = 0; | 
| mbed_official | 52:27692043e962 | 117 | button_status=1; | 
| mbed_official | 52:27692043e962 | 118 | output = 0; | 
| mbed_official | 52:27692043e962 | 119 | } | 
| mbed_official | 52:27692043e962 | 120 | else { | 
| mbed_official | 52:27692043e962 | 121 | tr_debug("Turning led off\n"); | 
| mbed_official | 52:27692043e962 | 122 | led_1 = 1; | 
| mbed_official | 52:27692043e962 | 123 | button_status=0; | 
| mbed_official | 52:27692043e962 | 124 | output = 1; | 
| mbed_official | 52:27692043e962 | 125 | } | 
| mbed_official | 52:27692043e962 | 126 | } | 
| mbed_official | 52:27692043e962 | 127 | |
| mbed_official | 52:27692043e962 | 128 | static void handle_message(char* msg) { | 
| mbed_official | 52:27692043e962 | 129 | // Check if this is lights message | 
| mbed_official | 52:27692043e962 | 130 | uint8_t state=button_status; | 
| mbed_official | 52:27692043e962 | 131 | uint16_t group=0xffff; | 
| mbed_official | 52:27692043e962 | 132 | |
| mbed_official | 52:27692043e962 | 133 | if (strstr(msg, "t:lights;") == NULL) { | 
| mbed_official | 52:27692043e962 | 134 | return; | 
| mbed_official | 52:27692043e962 | 135 | } | 
| mbed_official | 52:27692043e962 | 136 | |
| mbed_official | 52:27692043e962 | 137 | if (strstr(msg, "s:1;") != NULL) { | 
| mbed_official | 52:27692043e962 | 138 | state = 1; | 
| mbed_official | 52:27692043e962 | 139 | } | 
| mbed_official | 52:27692043e962 | 140 | else if (strstr(msg, "s:0;") != NULL) { | 
| mbed_official | 52:27692043e962 | 141 | state = 0; | 
| mbed_official | 52:27692043e962 | 142 | } | 
| mbed_official | 52:27692043e962 | 143 | |
| mbed_official | 52:27692043e962 | 144 | // 0==master, 1==default group | 
| mbed_official | 52:27692043e962 | 145 | char *msg_ptr = strstr(msg, "g:"); | 
| mbed_official | 52:27692043e962 | 146 | if (msg_ptr) { | 
| mbed_official | 52:27692043e962 | 147 | char *ptr; | 
| mbed_official | 52:27692043e962 | 148 | group = strtol(msg_ptr, &ptr, 10); | 
| mbed_official | 52:27692043e962 | 149 | } | 
| mbed_official | 52:27692043e962 | 150 | |
| mbed_official | 52:27692043e962 | 151 | // in this example we only use one group | 
| mbed_official | 52:27692043e962 | 152 | if (group==MASTER_GROUP || group==MY_GROUP) { | 
| mbed_official | 52:27692043e962 | 153 | update_state(state); | 
| mbed_official | 52:27692043e962 | 154 | } | 
| mbed_official | 52:27692043e962 | 155 | } | 
| mbed_official | 52:27692043e962 | 156 | |
| mbed_official | 52:27692043e962 | 157 | static void receive() { | 
| mbed_official | 52:27692043e962 | 158 | // Read data from the socket | 
| mbed_official | 52:27692043e962 | 159 | SocketAddress source_addr; | 
| mbed_official | 52:27692043e962 | 160 | memset(receive_buffer, 0, sizeof(receive_buffer)); | 
| mbed_official | 52:27692043e962 | 161 | bool something_in_socket=true; | 
| mbed_official | 52:27692043e962 | 162 | // read all messages | 
| mbed_official | 52:27692043e962 | 163 | while (something_in_socket) { | 
| mbed_official | 52:27692043e962 | 164 | int length = my_socket->recvfrom(&source_addr, receive_buffer, sizeof(receive_buffer) - 1); | 
| mbed_official | 52:27692043e962 | 165 | if (length > 0) { | 
| mbed_official | 52:27692043e962 | 166 | int timeout_value = MESSAGE_WAIT_TIMEOUT; | 
| mbed_official | 52:27692043e962 | 167 | tr_debug("Packet from %s\n", source_addr.get_ip_address()); | 
| mbed_official | 52:27692043e962 | 168 | timeout_value += rand() % 30; | 
| mbed_official | 52:27692043e962 | 169 | tr_debug("Advertisiment after %d seconds", timeout_value); | 
| mbed_official | 52:27692043e962 | 170 | messageTimeout.detach(); | 
| mbed_official | 52:27692043e962 | 171 | messageTimeout.attach(&messageTimeoutCallback, timeout_value); | 
| mbed_official | 52:27692043e962 | 172 | // Handle command - "on", "off" | 
| mbed_official | 52:27692043e962 | 173 | handle_message((char*)receive_buffer); | 
| mbed_official | 52:27692043e962 | 174 | } | 
| mbed_official | 52:27692043e962 | 175 | else if (length!=NSAPI_ERROR_WOULD_BLOCK) { | 
| mbed_official | 52:27692043e962 | 176 | tr_error("Error happened when receiving %d\n", length); | 
| mbed_official | 52:27692043e962 | 177 | something_in_socket=false; | 
| mbed_official | 52:27692043e962 | 178 | } | 
| mbed_official | 52:27692043e962 | 179 | else { | 
| mbed_official | 52:27692043e962 | 180 | // there was nothing to read. | 
| mbed_official | 52:27692043e962 | 181 | something_in_socket=false; | 
| mbed_official | 52:27692043e962 | 182 | } | 
| mbed_official | 52:27692043e962 | 183 | } | 
| mbed_official | 52:27692043e962 | 184 | } | 
| mbed_official | 52:27692043e962 | 185 | |
| mbed_official | 52:27692043e962 | 186 | static void handle_socket() { | 
| mbed_official | 52:27692043e962 | 187 | // call-back might come from ISR | 
| mbed_official | 52:27692043e962 | 188 | queue.call(receive); | 
| mbed_official | 52:27692043e962 | 189 | } | 
| mbed_official | 52:27692043e962 | 190 | |
| mbed_official | 52:27692043e962 | 191 | static void init_socket() | 
| mbed_official | 52:27692043e962 | 192 | { | 
| mbed_official | 52:27692043e962 | 193 | my_socket = new UDPSocket(network_if); | 
| mbed_official | 52:27692043e962 | 194 | my_socket->set_blocking(false); | 
| mbed_official | 52:27692043e962 | 195 | my_socket->bind(UDP_PORT); | 
| mbed_official | 52:27692043e962 | 196 | my_socket->setsockopt(SOCKET_IPPROTO_IPV6, SOCKET_IPV6_MULTICAST_HOPS, &multicast_hops, sizeof(multicast_hops)); | 
| mbed_official | 52:27692043e962 | 197 | if (MBED_CONF_APP_BUTTON != NC) { | 
| mbed_official | 52:27692043e962 | 198 | my_button.fall(&my_button_isr); | 
| mbed_official | 52:27692043e962 | 199 | my_button.mode(PullUp); | 
| mbed_official | 52:27692043e962 | 200 | } | 
| mbed_official | 52:27692043e962 | 201 | //let's register the call-back function. | 
| mbed_official | 52:27692043e962 | 202 | //If something happens in socket (packets in or out), the call-back is called. | 
| mbed_official | 52:27692043e962 | 203 | my_socket->sigio(callback(handle_socket)); | 
| mbed_official | 52:27692043e962 | 204 | // dispatch forever | 
| mbed_official | 52:27692043e962 | 205 | queue.dispatch(); | 
| mbed_official | 52:27692043e962 | 206 | } |