テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8468a4403fea 1 /* mbed Microcontroller Library
jksoft 0:8468a4403fea 2 * Copyright (c) 2006-2013 ARM Limited
jksoft 0:8468a4403fea 3 *
jksoft 0:8468a4403fea 4 * Licensed under the Apache License, Version 2.0 (the "License");
jksoft 0:8468a4403fea 5 * you may not use this file except in compliance with the License.
jksoft 0:8468a4403fea 6 * You may obtain a copy of the License at
jksoft 0:8468a4403fea 7 *
jksoft 0:8468a4403fea 8 * http://www.apache.org/licenses/LICENSE-2.0
jksoft 0:8468a4403fea 9 *
jksoft 0:8468a4403fea 10 * Unless required by applicable law or agreed to in writing, software
jksoft 0:8468a4403fea 11 * distributed under the License is distributed on an "AS IS" BASIS,
jksoft 0:8468a4403fea 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jksoft 0:8468a4403fea 13 * See the License for the specific language governing permissions and
jksoft 0:8468a4403fea 14 * limitations under the License.
jksoft 0:8468a4403fea 15 */
jksoft 0:8468a4403fea 16
jksoft 0:8468a4403fea 17 #ifndef __GAP_H__
jksoft 0:8468a4403fea 18 #define __GAP_H__
jksoft 0:8468a4403fea 19
jksoft 0:8468a4403fea 20 #include "GapAdvertisingData.h"
jksoft 0:8468a4403fea 21 #include "GapAdvertisingParams.h"
jksoft 0:8468a4403fea 22 #include "GapEvents.h"
jksoft 0:8468a4403fea 23
jksoft 0:8468a4403fea 24 class Gap {
jksoft 0:8468a4403fea 25 public:
jksoft 0:8468a4403fea 26 typedef enum addr_type_e {
jksoft 0:8468a4403fea 27 ADDR_TYPE_PUBLIC = 0,
jksoft 0:8468a4403fea 28 ADDR_TYPE_RANDOM_STATIC,
jksoft 0:8468a4403fea 29 ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE,
jksoft 0:8468a4403fea 30 ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE
jksoft 0:8468a4403fea 31 } addr_type_t;
jksoft 0:8468a4403fea 32
jksoft 0:8468a4403fea 33 static const unsigned ADDR_LEN = 6;
jksoft 0:8468a4403fea 34 typedef uint8_t address_t[ADDR_LEN];
jksoft 0:8468a4403fea 35
jksoft 0:8468a4403fea 36 /**
jksoft 0:8468a4403fea 37 * Enumeration for disconnection reasons. The values for these reasons are
jksoft 0:8468a4403fea 38 * derived from Nordic's implementation; but the reasons are meant to be
jksoft 0:8468a4403fea 39 * independent of the transport. If you are returned a reason which is not
jksoft 0:8468a4403fea 40 * covered by this enumeration, then please refer to the underlying
jksoft 0:8468a4403fea 41 * transport library.
jksoft 0:8468a4403fea 42 */
jksoft 0:8468a4403fea 43 enum DisconnectionReason_t {
jksoft 0:8468a4403fea 44 REMOTE_USER_TERMINATED_CONNECTION = 0x13,
jksoft 0:8468a4403fea 45 LOCAL_HOST_TERMINATED_CONNECTION = 0x16,
jksoft 0:8468a4403fea 46 CONN_INTERVAL_UNACCEPTABLE = 0x3B,
jksoft 0:8468a4403fea 47 };
jksoft 0:8468a4403fea 48
jksoft 0:8468a4403fea 49 /* Describes the current state of the device (more than one bit can be set) */
jksoft 0:8468a4403fea 50 typedef struct GapState_s {
jksoft 0:8468a4403fea 51 unsigned advertising : 1; /**< peripheral is currently advertising */
jksoft 0:8468a4403fea 52 unsigned connected : 1; /**< peripheral is connected to a central */
jksoft 0:8468a4403fea 53 } GapState_t;
jksoft 0:8468a4403fea 54
jksoft 0:8468a4403fea 55 typedef uint16_t Handle_t;
jksoft 0:8468a4403fea 56
jksoft 0:8468a4403fea 57 typedef struct {
jksoft 0:8468a4403fea 58 uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
jksoft 0:8468a4403fea 59 uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
jksoft 0:8468a4403fea 60 uint16_t slaveLatency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
jksoft 0:8468a4403fea 61 uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
jksoft 0:8468a4403fea 62 } ConnectionParams_t;
jksoft 0:8468a4403fea 63
jksoft 0:8468a4403fea 64 static const uint16_t UNIT_1_25_MS = 1250; /**< Number of microseconds in 1.25 milliseconds. */
jksoft 0:8468a4403fea 65 static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) {
jksoft 0:8468a4403fea 66 return (durationInMillis * 1000) / UNIT_1_25_MS;
jksoft 0:8468a4403fea 67 }
jksoft 0:8468a4403fea 68
jksoft 0:8468a4403fea 69 typedef void (*EventCallback_t)(void);
jksoft 0:8468a4403fea 70 typedef void (*ConnectionEventCallback_t)(Handle_t, addr_type_t peerAddrType, const address_t peerAddr, const ConnectionParams_t *);
jksoft 0:8468a4403fea 71 typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t);
jksoft 0:8468a4403fea 72
jksoft 0:8468a4403fea 73 friend class BLEDevice;
jksoft 0:8468a4403fea 74 private:
jksoft 0:8468a4403fea 75 /* These functions must be defined in the sub-class */
jksoft 0:8468a4403fea 76 virtual ble_error_t setAddress(addr_type_t type, const address_t address) = 0;
jksoft 0:8468a4403fea 77 virtual ble_error_t getAddress(addr_type_t *typeP, address_t address) = 0;
jksoft 0:8468a4403fea 78 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0;
jksoft 0:8468a4403fea 79 virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0;
jksoft 0:8468a4403fea 80 virtual ble_error_t stopAdvertising(void) = 0;
jksoft 0:8468a4403fea 81 virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0;
jksoft 0:8468a4403fea 82 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0;
jksoft 0:8468a4403fea 83 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0;
jksoft 0:8468a4403fea 84 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0;
jksoft 0:8468a4403fea 85
jksoft 0:8468a4403fea 86 virtual ble_error_t setDeviceName(const uint8_t *deviceName) = 0;
jksoft 0:8468a4403fea 87 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0;
jksoft 0:8468a4403fea 88 virtual ble_error_t setAppearance(uint16_t appearance) = 0;
jksoft 0:8468a4403fea 89 virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0;
jksoft 0:8468a4403fea 90
jksoft 0:8468a4403fea 91 private:
jksoft 0:8468a4403fea 92 /* Event callback handlers */
jksoft 0:8468a4403fea 93 void setOnTimeout(EventCallback_t callback) {onTimeout = callback;}
jksoft 0:8468a4403fea 94 void setOnConnection(ConnectionEventCallback_t callback) {onConnection = callback;}
jksoft 0:8468a4403fea 95 void setOnDisconnection(DisconnectionEventCallback_t callback) {onDisconnection = callback;}
jksoft 0:8468a4403fea 96
jksoft 0:8468a4403fea 97 GapState_t getState(void) const {
jksoft 0:8468a4403fea 98 return state;
jksoft 0:8468a4403fea 99 }
jksoft 0:8468a4403fea 100
jksoft 0:8468a4403fea 101 protected:
jksoft 0:8468a4403fea 102 Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL) {
jksoft 0:8468a4403fea 103 /* empty */
jksoft 0:8468a4403fea 104 }
jksoft 0:8468a4403fea 105
jksoft 0:8468a4403fea 106 public:
jksoft 0:8468a4403fea 107 void processConnectionEvent(Handle_t handle, addr_type_t type, const address_t addr, const ConnectionParams_t *params) {
jksoft 0:8468a4403fea 108 state.connected = 1;
jksoft 0:8468a4403fea 109 if (onConnection) {
jksoft 0:8468a4403fea 110 onConnection(handle, type, addr, params);
jksoft 0:8468a4403fea 111 }
jksoft 0:8468a4403fea 112 }
jksoft 0:8468a4403fea 113
jksoft 0:8468a4403fea 114 void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) {
jksoft 0:8468a4403fea 115 state.connected = 0;
jksoft 0:8468a4403fea 116 if (onDisconnection) {
jksoft 0:8468a4403fea 117 onDisconnection(handle, reason);
jksoft 0:8468a4403fea 118 }
jksoft 0:8468a4403fea 119 }
jksoft 0:8468a4403fea 120
jksoft 0:8468a4403fea 121 void processEvent(GapEvents::gapEvent_e type) {
jksoft 0:8468a4403fea 122 switch (type) {
jksoft 0:8468a4403fea 123 case GapEvents::GAP_EVENT_TIMEOUT:
jksoft 0:8468a4403fea 124 state.advertising = 0;
jksoft 0:8468a4403fea 125 if (onTimeout) {
jksoft 0:8468a4403fea 126 onTimeout();
jksoft 0:8468a4403fea 127 }
jksoft 0:8468a4403fea 128 break;
jksoft 0:8468a4403fea 129 }
jksoft 0:8468a4403fea 130 }
jksoft 0:8468a4403fea 131
jksoft 0:8468a4403fea 132 protected:
jksoft 0:8468a4403fea 133 GapState_t state;
jksoft 0:8468a4403fea 134
jksoft 0:8468a4403fea 135 private:
jksoft 0:8468a4403fea 136 EventCallback_t onTimeout;
jksoft 0:8468a4403fea 137 ConnectionEventCallback_t onConnection;
jksoft 0:8468a4403fea 138 DisconnectionEventCallback_t onDisconnection;
jksoft 0:8468a4403fea 139
jksoft 0:8468a4403fea 140 private:
jksoft 0:8468a4403fea 141 /* disallow copy and assignment */
jksoft 0:8468a4403fea 142 Gap(const Gap &);
jksoft 0:8468a4403fea 143 Gap& operator=(const Gap &);
jksoft 0:8468a4403fea 144 };
jksoft 0:8468a4403fea 145
jksoft 0:8468a4403fea 146 #endif // ifndef __GAP_H__