AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 mbed HRM1017で動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo

Dependencies:   BLE_API mbed

Fork of BLE_RCBController2 by Junichi Katsu

BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するmbed HRM1017用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
/media/uploads/lipoyang/blepropo_ui.png
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
/media/uploads/lipoyang/ble_wiring.png

Committer:
jksoft
Date:
Wed Aug 20 13:41:01 2014 +0000
Revision:
4:ebda47d22091
Parent:
nRF51822/btle/btle.cpp@1:48f6e08a3ac2
?????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 1:48f6e08a3ac2 1 /* mbed Microcontroller Library
jksoft 1:48f6e08a3ac2 2 * Copyright (c) 2006-2013 ARM Limited
jksoft 1:48f6e08a3ac2 3 *
jksoft 1:48f6e08a3ac2 4 * Licensed under the Apache License, Version 2.0 (the "License");
jksoft 1:48f6e08a3ac2 5 * you may not use this file except in compliance with the License.
jksoft 1:48f6e08a3ac2 6 * You may obtain a copy of the License at
jksoft 1:48f6e08a3ac2 7 *
jksoft 1:48f6e08a3ac2 8 * http://www.apache.org/licenses/LICENSE-2.0
jksoft 1:48f6e08a3ac2 9 *
jksoft 1:48f6e08a3ac2 10 * Unless required by applicable law or agreed to in writing, software
jksoft 1:48f6e08a3ac2 11 * distributed under the License is distributed on an "AS IS" BASIS,
jksoft 1:48f6e08a3ac2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jksoft 1:48f6e08a3ac2 13 * See the License for the specific language governing permissions and
jksoft 1:48f6e08a3ac2 14 * limitations under the License.
jksoft 1:48f6e08a3ac2 15 */
jksoft 1:48f6e08a3ac2 16
jksoft 1:48f6e08a3ac2 17 #include "common/common.h"
jksoft 1:48f6e08a3ac2 18 #include "nordic_common.h"
jksoft 1:48f6e08a3ac2 19
jksoft 1:48f6e08a3ac2 20 #include "app_timer.h"
jksoft 1:48f6e08a3ac2 21 #include "btle.h"
jksoft 1:48f6e08a3ac2 22
jksoft 1:48f6e08a3ac2 23 #include "ble_stack_handler_types.h"
jksoft 1:48f6e08a3ac2 24 #include "ble_radio_notification.h"
jksoft 1:48f6e08a3ac2 25 #include "ble_flash.h"
jksoft 1:48f6e08a3ac2 26 #if NEED_BOND_MANAGER
jksoft 1:48f6e08a3ac2 27 #include "ble_bondmngr.h"
jksoft 1:48f6e08a3ac2 28 #endif
jksoft 1:48f6e08a3ac2 29 #include "ble_conn_params.h"
jksoft 1:48f6e08a3ac2 30
jksoft 1:48f6e08a3ac2 31 #include "btle_gap.h"
jksoft 1:48f6e08a3ac2 32 #include "btle_advertising.h"
jksoft 1:48f6e08a3ac2 33 #include "custom/custom_helper.h"
jksoft 1:48f6e08a3ac2 34
jksoft 1:48f6e08a3ac2 35 #include "softdevice_handler.h"
jksoft 1:48f6e08a3ac2 36 #include "pstorage.h"
jksoft 1:48f6e08a3ac2 37
jksoft 1:48f6e08a3ac2 38 #include "GapEvents.h"
jksoft 1:48f6e08a3ac2 39 #include "nRF51Gap.h"
jksoft 1:48f6e08a3ac2 40 #include "nRF51GattServer.h"
jksoft 1:48f6e08a3ac2 41
jksoft 1:48f6e08a3ac2 42 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 1:48f6e08a3ac2 43 static void service_error_callback(uint32_t nrf_error);
jksoft 1:48f6e08a3ac2 44 #endif
jksoft 1:48f6e08a3ac2 45 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
jksoft 1:48f6e08a3ac2 46 void app_error_handler(uint32_t error_code,
jksoft 1:48f6e08a3ac2 47 uint32_t line_num,
jksoft 1:48f6e08a3ac2 48 const uint8_t *p_file_name);
jksoft 1:48f6e08a3ac2 49
jksoft 1:48f6e08a3ac2 50 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 1:48f6e08a3ac2 51 static error_t bond_manager_init(void);
jksoft 1:48f6e08a3ac2 52 #endif
jksoft 1:48f6e08a3ac2 53
jksoft 1:48f6e08a3ac2 54 static void btle_handler(ble_evt_t *p_ble_evt);
jksoft 1:48f6e08a3ac2 55
jksoft 1:48f6e08a3ac2 56 static void sys_evt_dispatch(uint32_t sys_evt)
jksoft 1:48f6e08a3ac2 57 {
jksoft 1:48f6e08a3ac2 58 #if NEED_PSTORAGE /* disabled by default */
jksoft 1:48f6e08a3ac2 59 pstorage_sys_event_handler(sys_evt);
jksoft 1:48f6e08a3ac2 60 #endif
jksoft 1:48f6e08a3ac2 61 }
jksoft 1:48f6e08a3ac2 62
jksoft 1:48f6e08a3ac2 63 error_t btle_init(void)
jksoft 1:48f6e08a3ac2 64 {
jksoft 1:48f6e08a3ac2 65 APP_TIMER_INIT(0, 8, 5, false);
jksoft 1:48f6e08a3ac2 66 // SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
jksoft 1:48f6e08a3ac2 67 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, false);
jksoft 1:48f6e08a3ac2 68
jksoft 1:48f6e08a3ac2 69 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
jksoft 1:48f6e08a3ac2 70 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
jksoft 1:48f6e08a3ac2 71
jksoft 1:48f6e08a3ac2 72 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 1:48f6e08a3ac2 73 bond_manager_init();
jksoft 1:48f6e08a3ac2 74 #endif
jksoft 1:48f6e08a3ac2 75 btle_gap_init();
jksoft 1:48f6e08a3ac2 76
jksoft 1:48f6e08a3ac2 77 return ERROR_NONE;
jksoft 1:48f6e08a3ac2 78 }
jksoft 1:48f6e08a3ac2 79
jksoft 1:48f6e08a3ac2 80 static void btle_handler(ble_evt_t *p_ble_evt)
jksoft 1:48f6e08a3ac2 81 {
jksoft 1:48f6e08a3ac2 82 /* Library service handlers */
jksoft 1:48f6e08a3ac2 83 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 1:48f6e08a3ac2 84 ble_bondmngr_on_ble_evt(p_ble_evt);
jksoft 1:48f6e08a3ac2 85 #endif
jksoft 1:48f6e08a3ac2 86 ble_conn_params_on_ble_evt(p_ble_evt);
jksoft 1:48f6e08a3ac2 87
jksoft 1:48f6e08a3ac2 88 /* Custom event handler */
jksoft 1:48f6e08a3ac2 89 switch (p_ble_evt->header.evt_id) {
jksoft 1:48f6e08a3ac2 90 case BLE_GAP_EVT_CONNECTED: {
jksoft 1:48f6e08a3ac2 91 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
jksoft 1:48f6e08a3ac2 92 nRF51Gap::getInstance().setConnectionHandle(handle);
jksoft 1:48f6e08a3ac2 93 nRF51Gap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_CONNECTED, handle);
jksoft 1:48f6e08a3ac2 94 break;
jksoft 1:48f6e08a3ac2 95 }
jksoft 1:48f6e08a3ac2 96
jksoft 1:48f6e08a3ac2 97 case BLE_GAP_EVT_DISCONNECTED: {
jksoft 1:48f6e08a3ac2 98 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
jksoft 1:48f6e08a3ac2 99 // Since we are not in a connection and have not started advertising,
jksoft 1:48f6e08a3ac2 100 // store bonds
jksoft 1:48f6e08a3ac2 101 nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
jksoft 1:48f6e08a3ac2 102 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 1:48f6e08a3ac2 103 ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store());
jksoft 1:48f6e08a3ac2 104 #endif
jksoft 1:48f6e08a3ac2 105 nRF51Gap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_DISCONNECTED, handle);
jksoft 1:48f6e08a3ac2 106 break;
jksoft 1:48f6e08a3ac2 107 }
jksoft 1:48f6e08a3ac2 108
jksoft 1:48f6e08a3ac2 109 case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
jksoft 1:48f6e08a3ac2 110 ble_gap_sec_params_t sec_params = {0};
jksoft 1:48f6e08a3ac2 111
jksoft 1:48f6e08a3ac2 112 sec_params.timeout = 30; /*< Timeout for Pairing Request or
jksoft 1:48f6e08a3ac2 113 * Security Request (in seconds). */
jksoft 1:48f6e08a3ac2 114 sec_params.bond = 1; /**< Perform bonding. */
jksoft 1:48f6e08a3ac2 115 sec_params.mitm = CFG_BLE_SEC_PARAM_MITM;
jksoft 1:48f6e08a3ac2 116 sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES;
jksoft 1:48f6e08a3ac2 117 sec_params.oob = CFG_BLE_SEC_PARAM_OOB;
jksoft 1:48f6e08a3ac2 118 sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE;
jksoft 1:48f6e08a3ac2 119 sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE;
jksoft 1:48f6e08a3ac2 120
jksoft 1:48f6e08a3ac2 121 ASSERT_STATUS_RET_VOID(
jksoft 1:48f6e08a3ac2 122 sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().
jksoft 1:48f6e08a3ac2 123 getConnectionHandle(),
jksoft 1:48f6e08a3ac2 124 BLE_GAP_SEC_STATUS_SUCCESS,
jksoft 1:48f6e08a3ac2 125 &sec_params));
jksoft 1:48f6e08a3ac2 126 }
jksoft 1:48f6e08a3ac2 127 break;
jksoft 1:48f6e08a3ac2 128
jksoft 1:48f6e08a3ac2 129 case BLE_GAP_EVT_TIMEOUT:
jksoft 1:48f6e08a3ac2 130 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
jksoft 1:48f6e08a3ac2 131 nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT);
jksoft 1:48f6e08a3ac2 132 }
jksoft 1:48f6e08a3ac2 133 break;
jksoft 1:48f6e08a3ac2 134
jksoft 1:48f6e08a3ac2 135 case BLE_GATTC_EVT_TIMEOUT:
jksoft 1:48f6e08a3ac2 136 case BLE_GATTS_EVT_TIMEOUT:
jksoft 1:48f6e08a3ac2 137 // Disconnect on GATT Server and Client timeout events.
jksoft 1:48f6e08a3ac2 138 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
jksoft 1:48f6e08a3ac2 139 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
jksoft 1:48f6e08a3ac2 140 break;
jksoft 1:48f6e08a3ac2 141
jksoft 1:48f6e08a3ac2 142 default:
jksoft 1:48f6e08a3ac2 143 break;
jksoft 1:48f6e08a3ac2 144 }
jksoft 1:48f6e08a3ac2 145
jksoft 1:48f6e08a3ac2 146 nRF51GattServer::getInstance().hwCallback(p_ble_evt);
jksoft 1:48f6e08a3ac2 147 }
jksoft 1:48f6e08a3ac2 148
jksoft 1:48f6e08a3ac2 149 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 1:48f6e08a3ac2 150 /**************************************************************************/
jksoft 1:48f6e08a3ac2 151 /*!
jksoft 1:48f6e08a3ac2 152 @brief Initialises the bond manager
jksoft 1:48f6e08a3ac2 153
jksoft 1:48f6e08a3ac2 154 @note Bond data will be cleared on reset if the bond delete
jksoft 1:48f6e08a3ac2 155 button is pressed during initialisation (the button is
jksoft 1:48f6e08a3ac2 156 defined as CFG_BLE_BOND_DELETE_BUTTON_NUM).
jksoft 1:48f6e08a3ac2 157
jksoft 1:48f6e08a3ac2 158 @returns
jksoft 1:48f6e08a3ac2 159 */
jksoft 1:48f6e08a3ac2 160 /**************************************************************************/
jksoft 1:48f6e08a3ac2 161 static error_t bond_manager_init(void)
jksoft 1:48f6e08a3ac2 162 {
jksoft 1:48f6e08a3ac2 163 ble_bondmngr_init_t bond_para = {0};
jksoft 1:48f6e08a3ac2 164
jksoft 1:48f6e08a3ac2 165 ASSERT_STATUS ( pstorage_init());
jksoft 1:48f6e08a3ac2 166
jksoft 1:48f6e08a3ac2 167 bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND;
jksoft 1:48f6e08a3ac2 168 bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR;
jksoft 1:48f6e08a3ac2 169 //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ;
jksoft 1:48f6e08a3ac2 170 bond_para.evt_handler = NULL;
jksoft 1:48f6e08a3ac2 171 bond_para.error_handler = service_error_callback;
jksoft 1:48f6e08a3ac2 172
jksoft 1:48f6e08a3ac2 173 ASSERT_STATUS( ble_bondmngr_init( &bond_para ));
jksoft 1:48f6e08a3ac2 174
jksoft 1:48f6e08a3ac2 175 /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */
jksoft 1:48f6e08a3ac2 176 // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH,
jksoft 1:48f6e08a3ac2 177 // NRF_RADIO_NOTIFICATION_DISTANCE_4560US,
jksoft 1:48f6e08a3ac2 178 // ble_flash_on_radio_active_evt) );
jksoft 1:48f6e08a3ac2 179
jksoft 1:48f6e08a3ac2 180 return ERROR_NONE;
jksoft 1:48f6e08a3ac2 181 }
jksoft 1:48f6e08a3ac2 182 #endif // #if NEED_BOND_MANAGER
jksoft 1:48f6e08a3ac2 183
jksoft 1:48f6e08a3ac2 184 #if NEED_BOND_MANAGER /* disabled by default */
jksoft 1:48f6e08a3ac2 185 /**************************************************************************/
jksoft 1:48f6e08a3ac2 186 /*!
jksoft 1:48f6e08a3ac2 187 @brief
jksoft 1:48f6e08a3ac2 188 @param[in] nrf_error
jksoft 1:48f6e08a3ac2 189 @returns
jksoft 1:48f6e08a3ac2 190 */
jksoft 1:48f6e08a3ac2 191 /**************************************************************************/
jksoft 1:48f6e08a3ac2 192 static void service_error_callback(uint32_t nrf_error)
jksoft 1:48f6e08a3ac2 193 {
jksoft 1:48f6e08a3ac2 194 ASSERT_STATUS_RET_VOID( nrf_error );
jksoft 1:48f6e08a3ac2 195 }
jksoft 1:48f6e08a3ac2 196 #endif // #if NEED_BOND_MANAGER
jksoft 1:48f6e08a3ac2 197
jksoft 1:48f6e08a3ac2 198 /**************************************************************************/
jksoft 1:48f6e08a3ac2 199 /*!
jksoft 1:48f6e08a3ac2 200 @brief Callback when an error occurs inside the SoftDevice
jksoft 1:48f6e08a3ac2 201
jksoft 1:48f6e08a3ac2 202 @param[in] line_num
jksoft 1:48f6e08a3ac2 203 @param[in] p-file_name
jksoft 1:48f6e08a3ac2 204
jksoft 1:48f6e08a3ac2 205 @returns
jksoft 1:48f6e08a3ac2 206 */
jksoft 1:48f6e08a3ac2 207 /**************************************************************************/
jksoft 1:48f6e08a3ac2 208 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
jksoft 1:48f6e08a3ac2 209 {
jksoft 1:48f6e08a3ac2 210 ASSERT(false, (void) 0);
jksoft 1:48f6e08a3ac2 211 }
jksoft 1:48f6e08a3ac2 212
jksoft 1:48f6e08a3ac2 213 /**************************************************************************/
jksoft 1:48f6e08a3ac2 214 /*!
jksoft 1:48f6e08a3ac2 215 @brief Handler for general errors above the SoftDevice layer.
jksoft 1:48f6e08a3ac2 216 Typically we can' recover from this so we do a reset.
jksoft 1:48f6e08a3ac2 217
jksoft 1:48f6e08a3ac2 218 @param[in] error_code
jksoft 1:48f6e08a3ac2 219 @param[in] line_num
jksoft 1:48f6e08a3ac2 220 @param[in] p-file_name
jksoft 1:48f6e08a3ac2 221
jksoft 1:48f6e08a3ac2 222 @returns
jksoft 1:48f6e08a3ac2 223 */
jksoft 1:48f6e08a3ac2 224 /**************************************************************************/
jksoft 1:48f6e08a3ac2 225 void app_error_handler(uint32_t error_code,
jksoft 1:48f6e08a3ac2 226 uint32_t line_num,
jksoft 1:48f6e08a3ac2 227 const uint8_t *p_file_name)
jksoft 1:48f6e08a3ac2 228 {
jksoft 1:48f6e08a3ac2 229 ASSERT_STATUS_RET_VOID( error_code );
jksoft 1:48f6e08a3ac2 230 NVIC_SystemReset();
jksoft 1:48f6e08a3ac2 231 }