iOSのBLEコントローラアプリ「RCBController」とmbed HRM1017を接続し、RCサーボモータを操作するテストプログラムです。
Dependencies: BLE_API_Native_IRC Servo mbed
Fork of BLE_RCBController by
- 古いBLEライブラリを使っているのでプラットフォームは”Nordic nRF51822”を選択してください。
- ライブラリ類はUpdateしないでください。コンパイルエラーになります。
うまく接続できない時は、iPhone/iPadのBluetoothをOFF->ONしてキャッシュをクリアしてみてください。
BLE_API_Native_IRC/hw/nRF51822n/btle/custom/custom_helper.cpp@0:8c643bfe55b7, 2014-07-10 (annotated)
- Committer:
- jksoft
- Date:
- Thu Jul 10 14:21:52 2014 +0000
- Revision:
- 0:8c643bfe55b7
??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:8c643bfe55b7 | 1 | /* mbed Microcontroller Library |
jksoft | 0:8c643bfe55b7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
jksoft | 0:8c643bfe55b7 | 3 | * |
jksoft | 0:8c643bfe55b7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jksoft | 0:8c643bfe55b7 | 5 | * you may not use this file except in compliance with the License. |
jksoft | 0:8c643bfe55b7 | 6 | * You may obtain a copy of the License at |
jksoft | 0:8c643bfe55b7 | 7 | * |
jksoft | 0:8c643bfe55b7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jksoft | 0:8c643bfe55b7 | 9 | * |
jksoft | 0:8c643bfe55b7 | 10 | * Unless required by applicable law or agreed to in writing, software |
jksoft | 0:8c643bfe55b7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jksoft | 0:8c643bfe55b7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jksoft | 0:8c643bfe55b7 | 13 | * See the License for the specific language governing permissions and |
jksoft | 0:8c643bfe55b7 | 14 | * limitations under the License. |
jksoft | 0:8c643bfe55b7 | 15 | */ |
jksoft | 0:8c643bfe55b7 | 16 | |
jksoft | 0:8c643bfe55b7 | 17 | #include "custom_helper.h" |
jksoft | 0:8c643bfe55b7 | 18 | |
jksoft | 0:8c643bfe55b7 | 19 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 20 | /*! |
jksoft | 0:8c643bfe55b7 | 21 | @brief Adds the base UUID to the custom service. All UUIDs used |
jksoft | 0:8c643bfe55b7 | 22 | by this service are based on this 128-bit UUID. |
jksoft | 0:8c643bfe55b7 | 23 | |
jksoft | 0:8c643bfe55b7 | 24 | @note This UUID needs to be added to the SoftDevice stack before |
jksoft | 0:8c643bfe55b7 | 25 | adding the service's primary service via |
jksoft | 0:8c643bfe55b7 | 26 | 'sd_ble_gatts_service_add' |
jksoft | 0:8c643bfe55b7 | 27 | |
jksoft | 0:8c643bfe55b7 | 28 | @param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16) |
jksoft | 0:8c643bfe55b7 | 29 | |
jksoft | 0:8c643bfe55b7 | 30 | @returns The UUID type. |
jksoft | 0:8c643bfe55b7 | 31 | A return value of 0 should be considered an error. |
jksoft | 0:8c643bfe55b7 | 32 | |
jksoft | 0:8c643bfe55b7 | 33 | @retval 0x00 BLE_UUID_TYPE_UNKNOWN |
jksoft | 0:8c643bfe55b7 | 34 | @retval 0x01 BLE_UUID_TYPE_BLE |
jksoft | 0:8c643bfe55b7 | 35 | @retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN |
jksoft | 0:8c643bfe55b7 | 36 | |
jksoft | 0:8c643bfe55b7 | 37 | @section EXAMPLE |
jksoft | 0:8c643bfe55b7 | 38 | @code |
jksoft | 0:8c643bfe55b7 | 39 | |
jksoft | 0:8c643bfe55b7 | 40 | // Take note that bytes 2/3 are blank since these are used to identify |
jksoft | 0:8c643bfe55b7 | 41 | // the primary service and individual characteristics |
jksoft | 0:8c643bfe55b7 | 42 | #define CFG_CUSTOM_UUID_BASE "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E" |
jksoft | 0:8c643bfe55b7 | 43 | |
jksoft | 0:8c643bfe55b7 | 44 | uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE); |
jksoft | 0:8c643bfe55b7 | 45 | ASSERT(uuid_type > 0, ERROR_NOT_FOUND); |
jksoft | 0:8c643bfe55b7 | 46 | |
jksoft | 0:8c643bfe55b7 | 47 | // We can now safely add the primary service and any characteristics |
jksoft | 0:8c643bfe55b7 | 48 | // for our custom service ... |
jksoft | 0:8c643bfe55b7 | 49 | |
jksoft | 0:8c643bfe55b7 | 50 | @endcode |
jksoft | 0:8c643bfe55b7 | 51 | */ |
jksoft | 0:8c643bfe55b7 | 52 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 53 | uint8_t custom_add_uuid_base(uint8_t const * const p_uuid_base) |
jksoft | 0:8c643bfe55b7 | 54 | { |
jksoft | 0:8c643bfe55b7 | 55 | ble_uuid128_t base_uuid; |
jksoft | 0:8c643bfe55b7 | 56 | uint8_t uuid_type = 0; |
jksoft | 0:8c643bfe55b7 | 57 | |
jksoft | 0:8c643bfe55b7 | 58 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
jksoft | 0:8c643bfe55b7 | 59 | for(uint8_t i=0; i<16; i++) |
jksoft | 0:8c643bfe55b7 | 60 | { |
jksoft | 0:8c643bfe55b7 | 61 | base_uuid.uuid128[i] = p_uuid_base[15-i]; |
jksoft | 0:8c643bfe55b7 | 62 | } |
jksoft | 0:8c643bfe55b7 | 63 | |
jksoft | 0:8c643bfe55b7 | 64 | ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); |
jksoft | 0:8c643bfe55b7 | 65 | |
jksoft | 0:8c643bfe55b7 | 66 | return uuid_type; |
jksoft | 0:8c643bfe55b7 | 67 | } |
jksoft | 0:8c643bfe55b7 | 68 | |
jksoft | 0:8c643bfe55b7 | 69 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 70 | /*! |
jksoft | 0:8c643bfe55b7 | 71 | |
jksoft | 0:8c643bfe55b7 | 72 | */ |
jksoft | 0:8c643bfe55b7 | 73 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 74 | error_t custom_decode_uuid_base(uint8_t const * const p_uuid_base, ble_uuid_t * p_uuid) |
jksoft | 0:8c643bfe55b7 | 75 | { |
jksoft | 0:8c643bfe55b7 | 76 | uint8_t uuid_base_le[16]; |
jksoft | 0:8c643bfe55b7 | 77 | |
jksoft | 0:8c643bfe55b7 | 78 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
jksoft | 0:8c643bfe55b7 | 79 | for(uint8_t i=0; i<16; i++) |
jksoft | 0:8c643bfe55b7 | 80 | { |
jksoft | 0:8c643bfe55b7 | 81 | uuid_base_le[i] = p_uuid_base[15-i]; |
jksoft | 0:8c643bfe55b7 | 82 | } |
jksoft | 0:8c643bfe55b7 | 83 | |
jksoft | 0:8c643bfe55b7 | 84 | ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid) ); |
jksoft | 0:8c643bfe55b7 | 85 | |
jksoft | 0:8c643bfe55b7 | 86 | return ERROR_NONE; |
jksoft | 0:8c643bfe55b7 | 87 | } |
jksoft | 0:8c643bfe55b7 | 88 | |
jksoft | 0:8c643bfe55b7 | 89 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 90 | /*! |
jksoft | 0:8c643bfe55b7 | 91 | @brief Adds a new characteristic to the custom service, assigning |
jksoft | 0:8c643bfe55b7 | 92 | properties, a UUID add-on value, etc. |
jksoft | 0:8c643bfe55b7 | 93 | |
jksoft | 0:8c643bfe55b7 | 94 | @param[in] service_handle |
jksoft | 0:8c643bfe55b7 | 95 | @param[in] p_uuid The 16-bit value to add to the base UUID |
jksoft | 0:8c643bfe55b7 | 96 | for this characteristic (normally >1 |
jksoft | 0:8c643bfe55b7 | 97 | since 1 is typically used by the primary |
jksoft | 0:8c643bfe55b7 | 98 | service). |
jksoft | 0:8c643bfe55b7 | 99 | @param[in] char_props The characteristic properties, as |
jksoft | 0:8c643bfe55b7 | 100 | defined by ble_gatt_char_props_t |
jksoft | 0:8c643bfe55b7 | 101 | @param[in] max_length The maximum length of this characeristic |
jksoft | 0:8c643bfe55b7 | 102 | @param[in] p_char_handle |
jksoft | 0:8c643bfe55b7 | 103 | |
jksoft | 0:8c643bfe55b7 | 104 | @returns |
jksoft | 0:8c643bfe55b7 | 105 | @retval ERROR_NONE Everything executed normally |
jksoft | 0:8c643bfe55b7 | 106 | */ |
jksoft | 0:8c643bfe55b7 | 107 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 108 | error_t custom_add_in_characteristic(uint16_t service_handle, ble_uuid_t* p_uuid, uint8_t properties, |
jksoft | 0:8c643bfe55b7 | 109 | uint8_t *p_data, uint16_t min_length, uint16_t max_length, |
jksoft | 0:8c643bfe55b7 | 110 | ble_gatts_char_handles_t* p_char_handle) |
jksoft | 0:8c643bfe55b7 | 111 | { |
jksoft | 0:8c643bfe55b7 | 112 | /* Characteristic metadata */ |
jksoft | 0:8c643bfe55b7 | 113 | ble_gatts_attr_md_t cccd_md; |
jksoft | 0:8c643bfe55b7 | 114 | ble_gatt_char_props_t char_props; |
jksoft | 0:8c643bfe55b7 | 115 | |
jksoft | 0:8c643bfe55b7 | 116 | memcpy(&char_props, &properties, 1); |
jksoft | 0:8c643bfe55b7 | 117 | |
jksoft | 0:8c643bfe55b7 | 118 | if ( char_props.notify || char_props.indicate ) |
jksoft | 0:8c643bfe55b7 | 119 | { |
jksoft | 0:8c643bfe55b7 | 120 | /* Notification requires cccd */ |
jksoft | 0:8c643bfe55b7 | 121 | memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t) ); |
jksoft | 0:8c643bfe55b7 | 122 | cccd_md.vloc = BLE_GATTS_VLOC_STACK; |
jksoft | 0:8c643bfe55b7 | 123 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); |
jksoft | 0:8c643bfe55b7 | 124 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); |
jksoft | 0:8c643bfe55b7 | 125 | } |
jksoft | 0:8c643bfe55b7 | 126 | |
jksoft | 0:8c643bfe55b7 | 127 | ble_gatts_char_md_t char_md = { 0 }; |
jksoft | 0:8c643bfe55b7 | 128 | |
jksoft | 0:8c643bfe55b7 | 129 | char_md.char_props = char_props; |
jksoft | 0:8c643bfe55b7 | 130 | char_md.p_cccd_md = (char_props.notify || char_props.indicate ) ? &cccd_md : NULL; |
jksoft | 0:8c643bfe55b7 | 131 | |
jksoft | 0:8c643bfe55b7 | 132 | /* Attribute declaration */ |
jksoft | 0:8c643bfe55b7 | 133 | ble_gatts_attr_md_t attr_md = { 0 }; |
jksoft | 0:8c643bfe55b7 | 134 | |
jksoft | 0:8c643bfe55b7 | 135 | attr_md.vloc = BLE_GATTS_VLOC_STACK; |
jksoft | 0:8c643bfe55b7 | 136 | attr_md.vlen = (min_length == max_length) ? 0 : 1; |
jksoft | 0:8c643bfe55b7 | 137 | |
jksoft | 0:8c643bfe55b7 | 138 | if ( char_props.read || char_props.notify || char_props.indicate ) |
jksoft | 0:8c643bfe55b7 | 139 | { |
jksoft | 0:8c643bfe55b7 | 140 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); |
jksoft | 0:8c643bfe55b7 | 141 | } |
jksoft | 0:8c643bfe55b7 | 142 | |
jksoft | 0:8c643bfe55b7 | 143 | if ( char_props.write ) |
jksoft | 0:8c643bfe55b7 | 144 | { |
jksoft | 0:8c643bfe55b7 | 145 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); |
jksoft | 0:8c643bfe55b7 | 146 | } |
jksoft | 0:8c643bfe55b7 | 147 | |
jksoft | 0:8c643bfe55b7 | 148 | ble_gatts_attr_t attr_char_value = { 0 }; |
jksoft | 0:8c643bfe55b7 | 149 | |
jksoft | 0:8c643bfe55b7 | 150 | attr_char_value.p_uuid = p_uuid; |
jksoft | 0:8c643bfe55b7 | 151 | attr_char_value.p_attr_md = &attr_md; |
jksoft | 0:8c643bfe55b7 | 152 | attr_char_value.init_len = min_length; |
jksoft | 0:8c643bfe55b7 | 153 | attr_char_value.max_len = max_length; |
jksoft | 0:8c643bfe55b7 | 154 | attr_char_value.p_value = p_data; |
jksoft | 0:8c643bfe55b7 | 155 | |
jksoft | 0:8c643bfe55b7 | 156 | |
jksoft | 0:8c643bfe55b7 | 157 | ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, |
jksoft | 0:8c643bfe55b7 | 158 | &char_md, |
jksoft | 0:8c643bfe55b7 | 159 | &attr_char_value, |
jksoft | 0:8c643bfe55b7 | 160 | p_char_handle) ); |
jksoft | 0:8c643bfe55b7 | 161 | |
jksoft | 0:8c643bfe55b7 | 162 | return ERROR_NONE; |
jksoft | 0:8c643bfe55b7 | 163 | } |