Wallbot_CaaS

Dependencies:   MPU6050 mbed PID

Fork of BLE_MPU6050_test6_challenge_sb by Junichi Katsu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Gap.h Source File

Gap.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __GAP_H__
00018 #define __GAP_H__
00019 
00020 #include "GapAdvertisingData.h"
00021 #include "GapAdvertisingParams.h"
00022 #include "GapEvents.h"
00023 
00024 class Gap {
00025 public:
00026     typedef enum addr_type_e {
00027         ADDR_TYPE_PUBLIC = 0,
00028         ADDR_TYPE_RANDOM_STATIC,
00029         ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE,
00030         ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE
00031     } addr_type_t;
00032 
00033     static const unsigned ADDR_LEN = 6;
00034     typedef uint8_t address_t[ADDR_LEN];
00035 
00036     /**
00037      * Enumeration for disconnection reasons. The values for these reasons are
00038      * derived from Nordic's implementation; but the reasons are meant to be
00039      * independent of the transport. If you are returned a reason which is not
00040      * covered by this enumeration, then please refer to the underlying
00041      * transport library.
00042      */
00043     enum DisconnectionReason_t {
00044         REMOTE_USER_TERMINATED_CONNECTION = 0x13,
00045         LOCAL_HOST_TERMINATED_CONNECTION  = 0x16,
00046         CONN_INTERVAL_UNACCEPTABLE        = 0x3B,
00047     };
00048 
00049     /* Describes the current state of the device (more than one bit can be set) */
00050     typedef struct GapState_s {
00051         unsigned advertising : 1; /**< peripheral is currently advertising */
00052         unsigned connected   : 1; /**< peripheral is connected to a central */
00053     } GapState_t;
00054 
00055     typedef uint16_t Handle_t;
00056 
00057     typedef struct {
00058         uint16_t minConnectionInterval;      /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
00059         uint16_t maxConnectionInterval;      /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
00060         uint16_t slaveLatency;               /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
00061         uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
00062     } ConnectionParams_t;
00063 
00064     static const uint16_t UNIT_1_25_MS = 1250; /**< Number of microseconds in 1.25 milliseconds. */
00065     static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) {
00066         return (durationInMillis * 1000) / UNIT_1_25_MS;
00067     }
00068 
00069     typedef void (*EventCallback_t)(void);
00070     typedef void (*ConnectionEventCallback_t)(Handle_t, addr_type_t peerAddrType, const address_t peerAddr, const ConnectionParams_t *);
00071     typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t);
00072 
00073     friend class BLEDevice;
00074 private:
00075     /* These functions must be defined in the sub-class */
00076     virtual ble_error_t setAddress(addr_type_t type,   const address_t address)                    = 0;
00077     virtual ble_error_t getAddress(addr_type_t *typeP, address_t address)                          = 0;
00078     virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0;
00079     virtual ble_error_t startAdvertising(const GapAdvertisingParams &)                             = 0;
00080     virtual ble_error_t stopAdvertising(void)                                                      = 0;
00081     virtual ble_error_t disconnect(DisconnectionReason_t reason)                                   = 0;
00082     virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params)                   = 0;
00083     virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params)             = 0;
00084     virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params)  = 0;
00085 
00086     virtual ble_error_t setDeviceName(const uint8_t *deviceName)              = 0;
00087     virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0;
00088     virtual ble_error_t setAppearance(uint16_t appearance)                    = 0;
00089     virtual ble_error_t getAppearance(uint16_t *appearanceP)                  = 0;
00090 
00091 private:
00092     /* Event callback handlers */
00093     void setOnTimeout(EventCallback_t callback) {onTimeout = callback;}
00094     void setOnConnection(ConnectionEventCallback_t callback) {onConnection = callback;}
00095     void setOnDisconnection(DisconnectionEventCallback_t callback) {onDisconnection = callback;}
00096 
00097     GapState_t getState(void) const {
00098         return state;
00099     }
00100 
00101 protected:
00102     Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL) {
00103         /* empty */
00104     }
00105 
00106 public:
00107     void processConnectionEvent(Handle_t handle, addr_type_t type, const address_t addr, const ConnectionParams_t *params) {
00108         state.connected = 1;
00109         if (onConnection) {
00110             onConnection(handle, type, addr, params);
00111         }
00112     }
00113 
00114     void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) {
00115         state.connected = 0;
00116         if (onDisconnection) {
00117             onDisconnection(handle, reason);
00118         }
00119     }
00120 
00121     void processEvent(GapEvents::gapEvent_e type) {
00122         switch (type) {
00123             case GapEvents::GAP_EVENT_TIMEOUT:
00124                 state.advertising = 0;
00125                 if (onTimeout) {
00126                     onTimeout();
00127                 }
00128                 break;
00129         }
00130     }
00131 
00132 protected:
00133     GapState_t                   state;
00134 
00135 private:
00136     EventCallback_t              onTimeout;
00137     ConnectionEventCallback_t    onConnection;
00138     DisconnectionEventCallback_t onDisconnection;
00139 
00140 private:
00141     /* disallow copy and assignment */
00142     Gap(const Gap &);
00143     Gap& operator=(const Gap &);
00144 };
00145 
00146 #endif // ifndef __GAP_H__