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: libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler
network_stubs/network_stubs.cpp@6:98af441fd960, 2015-02-17 (annotated)
- Committer:
- ansond
- Date:
- Tue Feb 17 02:56:36 2015 +0000
- Revision:
- 6:98af441fd960
- Parent:
- 5:9233e88b9c83
- Child:
- 10:95122a4a80cb
updates still not working
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| ansond | 0:7809547930d9 | 1 | /** | 
| ansond | 0:7809547930d9 | 2 | * @file network_stubs.cpp | 
| ansond | 2:30f4a0dab604 | 3 | * @brief mbed Endpoint network stubs implementation (BLE) | 
| ansond | 0:7809547930d9 | 4 | * @author Doug Anson | 
| ansond | 0:7809547930d9 | 5 | * @version 1.0 | 
| ansond | 0:7809547930d9 | 6 | * @see | 
| ansond | 0:7809547930d9 | 7 | * | 
| ansond | 0:7809547930d9 | 8 | * Copyright (c) 2014 | 
| ansond | 0:7809547930d9 | 9 | * | 
| ansond | 0:7809547930d9 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
| ansond | 0:7809547930d9 | 11 | * you may not use this file except in compliance with the License. | 
| ansond | 0:7809547930d9 | 12 | * You may obtain a copy of the License at | 
| ansond | 0:7809547930d9 | 13 | * | 
| ansond | 0:7809547930d9 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| ansond | 0:7809547930d9 | 15 | * | 
| ansond | 0:7809547930d9 | 16 | * Unless required by applicable law or agreed to in writing, software | 
| ansond | 0:7809547930d9 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, | 
| ansond | 0:7809547930d9 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| ansond | 0:7809547930d9 | 19 | * See the License for the specific language governing permissions and | 
| ansond | 0:7809547930d9 | 20 | * limitations under the License. | 
| ansond | 0:7809547930d9 | 21 | */ | 
| ansond | 0:7809547930d9 | 22 | |
| ansond | 0:7809547930d9 | 23 | #include "network_stubs.h" | 
| ansond | 0:7809547930d9 | 24 | |
| ansond | 6:98af441fd960 | 25 | #include "UartRPCFunctions.h" | 
| ansond | 2:30f4a0dab604 | 26 | |
| ansond | 0:7809547930d9 | 27 | extern "C" { | 
| ansond | 0:7809547930d9 | 28 | |
| ansond | 2:30f4a0dab604 | 29 | // Globals | 
| ansond | 6:98af441fd960 | 30 | BLEDevice ble; | 
| ansond | 2:30f4a0dab604 | 31 | volatile bool __registered = false; | 
| ansond | 2:30f4a0dab604 | 32 | |
| ansond | 5:9233e88b9c83 | 33 | static const uint16_t deviceInfoServiceUUID[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE}; | 
| ansond | 2:30f4a0dab604 | 34 | |
| ansond | 2:30f4a0dab604 | 35 | char _ipAddress[IP_ADDRESS_LENGTH]; | 
| ansond | 2:30f4a0dab604 | 36 | int _ipPort; | 
| ansond | 2:30f4a0dab604 | 37 | |
| ansond | 2:30f4a0dab604 | 38 | // record the remote UDPSocket endpoint IP address | 
| ansond | 2:30f4a0dab604 | 39 | void ble_set_remote_ip_address(const char *ipAddress,const int ipAddressLength) | 
| ansond | 2:30f4a0dab604 | 40 | { | 
| ansond | 2:30f4a0dab604 | 41 | memset(_ipAddress,0,IP_ADDRESS_LENGTH); | 
| ansond | 2:30f4a0dab604 | 42 | int length = ipAddressLength; | 
| ansond | 2:30f4a0dab604 | 43 | if (length > IP_ADDRESS_LENGTH) length = IP_ADDRESS_LENGTH; | 
| ansond | 2:30f4a0dab604 | 44 | if (ipAddress != NULL) memcpy(_ipAddress,ipAddress,length); | 
| ansond | 2:30f4a0dab604 | 45 | } | 
| ansond | 2:30f4a0dab604 | 46 | |
| ansond | 2:30f4a0dab604 | 47 | // record the remote UDPSocket endpoint port | 
| ansond | 2:30f4a0dab604 | 48 | void ble_set_remote_ip_port(const int ipPort) | 
| ansond | 2:30f4a0dab604 | 49 | { | 
| ansond | 2:30f4a0dab604 | 50 | _ipPort = ipPort; | 
| ansond | 2:30f4a0dab604 | 51 | } | 
| ansond | 2:30f4a0dab604 | 52 | |
| ansond | 2:30f4a0dab604 | 53 | // BLE Disconnection callback | 
| ansond | 2:30f4a0dab604 | 54 | void ble_disconnect_callback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) | 
| ansond | 2:30f4a0dab604 | 55 | { | 
| ansond | 2:30f4a0dab604 | 56 | DBG("Disconnected handle %u!\r\n", handle); | 
| ansond | 2:30f4a0dab604 | 57 | __registered = false; | 
| ansond | 2:30f4a0dab604 | 58 | DBG("Restarting the advertising process\r\n"); | 
| ansond | 2:30f4a0dab604 | 59 | ble.startAdvertising(); | 
| ansond | 2:30f4a0dab604 | 60 | } | 
| ansond | 2:30f4a0dab604 | 61 | |
| ansond | 2:30f4a0dab604 | 62 | // BLE Connection callback | 
| ansond | 2:30f4a0dab604 | 63 | void ble_connect_callback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *params) | 
| ansond | 2:30f4a0dab604 | 64 | { | 
| ansond | 6:98af441fd960 | 65 | DBG("ble_connect_callback: BLE connected! waiting for 3 seconds...\r\n"); | 
| ansond | 5:9233e88b9c83 | 66 | wait_ms(3000); // wait for 3 seconds... let the gateway register the device completely and get ready to accept data... | 
| ansond | 5:9233e88b9c83 | 67 | |
| ansond | 2:30f4a0dab604 | 68 | if (__registered == false) { | 
| ansond | 6:98af441fd960 | 69 | DBG("ble_connect_callback: Opening remote UDP socket to: %s@%d...\r\n",_ipAddress,_ipPort); | 
| ansond | 6:98af441fd960 | 70 | if (ble_rpc_open_udp_socket(_ipAddress,_ipPort)) { | 
| ansond | 6:98af441fd960 | 71 | // NSP registration | 
| ansond | 6:98af441fd960 | 72 | DBG("ble_connect_callback: calling NSP registration...\r\n"); | 
| ansond | 6:98af441fd960 | 73 | register_endpoint(true); | 
| ansond | 6:98af441fd960 | 74 | DBG("ble_connect_callback: NSP registration completed\r\n"); | 
| ansond | 6:98af441fd960 | 75 | |
| ansond | 6:98af441fd960 | 76 | // registration completed | 
| ansond | 6:98af441fd960 | 77 | __registered = true; | 
| ansond | 6:98af441fd960 | 78 | } | 
| ansond | 6:98af441fd960 | 79 | else { | 
| ansond | 6:98af441fd960 | 80 | DBG("ble_connect_callback: remote UDP socket open FAILED...\r\n"); | 
| ansond | 6:98af441fd960 | 81 | __registered = false; | 
| ansond | 6:98af441fd960 | 82 | } | 
| ansond | 2:30f4a0dab604 | 83 | } | 
| ansond | 6:98af441fd960 | 84 | else { | 
| ansond | 6:98af441fd960 | 85 | DBG("ble_connect_callback: Already registered and connected...\r\n"); | 
| ansond | 6:98af441fd960 | 86 | } | 
| ansond | 2:30f4a0dab604 | 87 | } | 
| ansond | 2:30f4a0dab604 | 88 | |
| ansond | 2:30f4a0dab604 | 89 | // BLE Init | 
| ansond | 2:30f4a0dab604 | 90 | void ble_init(void) | 
| ansond | 2:30f4a0dab604 | 91 | { | 
| ansond | 2:30f4a0dab604 | 92 | // BLE initialization and callback setup | 
| ansond | 5:9233e88b9c83 | 93 | __registered = false; | 
| ansond | 2:30f4a0dab604 | 94 | ble.init(); | 
| ansond | 2:30f4a0dab604 | 95 | ble.onDisconnection(ble_disconnect_callback); | 
| ansond | 2:30f4a0dab604 | 96 | ble.onConnection(ble_connect_callback); | 
| ansond | 2:30f4a0dab604 | 97 | } | 
| ansond | 2:30f4a0dab604 | 98 | |
| ansond | 2:30f4a0dab604 | 99 | // BLE Begin Advertising | 
| ansond | 2:30f4a0dab604 | 100 | void ble_begin_advertising(void) | 
| ansond | 2:30f4a0dab604 | 101 | { | 
| ansond | 2:30f4a0dab604 | 102 | extern uint8_t endpoint_name[NODE_NAME_LENGTH]; // our NSP NODE name | 
| ansond | 2:30f4a0dab604 | 103 | |
| ansond | 2:30f4a0dab604 | 104 | DBG("Starting BLE Advertising (%s)...\r\n",endpoint_name); | 
| ansond | 2:30f4a0dab604 | 105 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); | 
| ansond | 5:9233e88b9c83 | 106 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); | 
| ansond | 2:30f4a0dab604 | 107 | ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_REMOTE_CONTROL); | 
| ansond | 2:30f4a0dab604 | 108 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,(uint8_t *)endpoint_name,strlen((char *)endpoint_name)); | 
| ansond | 5:9233e88b9c83 | 109 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)endpoint_name, strlen((char *)endpoint_name)); | 
| ansond | 5:9233e88b9c83 | 110 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,(uint8_t *)deviceInfoServiceUUID, sizeof(deviceInfoServiceUUID)); | 
| ansond | 5:9233e88b9c83 | 111 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID, sizeof(UARTServiceUUID)); | 
| ansond | 5:9233e88b9c83 | 112 | ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); | 
| ansond | 2:30f4a0dab604 | 113 | ble.startAdvertising(); | 
| ansond | 2:30f4a0dab604 | 114 | } | 
| ansond | 2:30f4a0dab604 | 115 | |
| ansond | 2:30f4a0dab604 | 116 | // BLE send data | 
| ansond | 6:98af441fd960 | 117 | int ble_send_data(uint8_t *data,int data_len) | 
| ansond | 2:30f4a0dab604 | 118 | { | 
| ansond | 2:30f4a0dab604 | 119 | int sent = 0; | 
| ansond | 2:30f4a0dab604 | 120 | DBG("in ble_send_data() data_len=%d data: [",data_len); | 
| ansond | 2:30f4a0dab604 | 121 | for(int i=0;i<data_len;++i) { | 
| ansond | 2:30f4a0dab604 | 122 | DBG("%.2x",data[i]); | 
| ansond | 2:30f4a0dab604 | 123 | if (i < (data_len-1)) DBG(" "); | 
| ansond | 2:30f4a0dab604 | 124 | } | 
| ansond | 2:30f4a0dab604 | 125 | DBG("] sent: "); | 
| ansond | 6:98af441fd960 | 126 | |
| ansond | 6:98af441fd960 | 127 | sent = ble_rpc_send_data(data,data_len); | 
| ansond | 2:30f4a0dab604 | 128 | DBG("%d bytes\r\n",sent); | 
| ansond | 2:30f4a0dab604 | 129 | return sent; | 
| ansond | 2:30f4a0dab604 | 130 | } | 
| ansond | 2:30f4a0dab604 | 131 | |
| ansond | 2:30f4a0dab604 | 132 | // BLE recv data | 
| ansond | 2:30f4a0dab604 | 133 | int ble_recv_data(uint8_t *buffer,int buffer_len) | 
| ansond | 2:30f4a0dab604 | 134 | { | 
| ansond | 6:98af441fd960 | 135 | int recv = ble_rpc_recv_data(buffer,buffer_len); | 
| ansond | 5:9233e88b9c83 | 136 | |
| ansond | 6:98af441fd960 | 137 | DBG("in ble_recv_data() recv=%d data: [",recv); | 
| ansond | 6:98af441fd960 | 138 | for(int i=0;i<recv;++i) { | 
| ansond | 6:98af441fd960 | 139 | DBG("%.2x",buffer[i]); | 
| ansond | 6:98af441fd960 | 140 | if (i < (recv-1)) DBG(" "); | 
| ansond | 2:30f4a0dab604 | 141 | } | 
| ansond | 6:98af441fd960 | 142 | DBG("] buffer_len=%d\r\n",buffer_len); | 
| ansond | 6:98af441fd960 | 143 | |
| ansond | 6:98af441fd960 | 144 | return recv; | 
| ansond | 2:30f4a0dab604 | 145 | } | 
| ansond | 2:30f4a0dab604 | 146 | |
| ansond | 0:7809547930d9 | 147 | // plumb out the network | 
| ansond | 0:7809547930d9 | 148 | void net_stubs_pre_plumb_network(bool canActAsRouterNode) | 
| ansond | 0:7809547930d9 | 149 | { | 
| ansond | 2:30f4a0dab604 | 150 | // BLE initialize... | 
| ansond | 6:98af441fd960 | 151 | DBG("Initializing BLE...\r\n"); | 
| ansond | 2:30f4a0dab604 | 152 | ble_init(); | 
| ansond | 0:7809547930d9 | 153 | } | 
| ansond | 0:7809547930d9 | 154 | |
| ansond | 0:7809547930d9 | 155 | // called after the endpoint is configured... | 
| ansond | 0:7809547930d9 | 156 | void net_stubs_post_plumb_network(void) | 
| ansond | 0:7809547930d9 | 157 | { | 
| ansond | 6:98af441fd960 | 158 | // Initialize the BLE RPC layer | 
| ansond | 6:98af441fd960 | 159 | DBG("Initializing BLE UART RPC layer...\r\n"); | 
| ansond | 6:98af441fd960 | 160 | ble_rpc_init(ble); | 
| ansond | 6:98af441fd960 | 161 | |
| ansond | 2:30f4a0dab604 | 162 | // BLE advertise... | 
| ansond | 2:30f4a0dab604 | 163 | DBG("Beginning BLE advertising...\r\n"); | 
| ansond | 2:30f4a0dab604 | 164 | ble_begin_advertising(); | 
| ansond | 0:7809547930d9 | 165 | } | 
| ansond | 0:7809547930d9 | 166 | |
| ansond | 0:7809547930d9 | 167 | // create a suitable main event loop for this specific network | 
| ansond | 0:7809547930d9 | 168 | void net_stubs_create_main_loop(void) | 
| ansond | 0:7809547930d9 | 169 | { | 
| ansond | 2:30f4a0dab604 | 170 | // nothing to do for BLE endpoints - we are using the Ticker-based loop in nsdl_support.cpp | 
| ansond | 0:7809547930d9 | 171 | ; | 
| ansond | 0:7809547930d9 | 172 | } | 
| ansond | 0:7809547930d9 | 173 | |
| ansond | 0:7809547930d9 | 174 | // register the endpoint | 
| ansond | 0:7809547930d9 | 175 | void net_stubs_register_endpoint(void) | 
| ansond | 0:7809547930d9 | 176 | { | 
| ansond | 2:30f4a0dab604 | 177 | // not used.. must wait until we get a BLE connect callback.. then register... | 
| ansond | 2:30f4a0dab604 | 178 | ; | 
| ansond | 0:7809547930d9 | 179 | } | 
| ansond | 0:7809547930d9 | 180 | |
| ansond | 0:7809547930d9 | 181 | // begin the main loop for processing network events | 
| ansond | 0:7809547930d9 | 182 | void net_stubs_begin_main_loop(void) | 
| ansond | 0:7809547930d9 | 183 | { | 
| ansond | 0:7809547930d9 | 184 | // NDSL main loop | 
| ansond | 2:30f4a0dab604 | 185 | DBG("Beginning NSDL Main Event Loop...\r\n"); | 
| ansond | 0:7809547930d9 | 186 | nsdl_event_loop(); | 
| ansond | 0:7809547930d9 | 187 | } | 
| ansond | 0:7809547930d9 | 188 | |
| ansond | 0:7809547930d9 | 189 | } |