Yutaka Yoshida / Mbed 2 deprecated BLE_WallbotBLE_Challenge

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge by JKSoft

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 "mbed.h"
00021 #include "blecommon.h"
00022 #include "GapAdvertisingData.h"
00023 #include "GapAdvertisingParams.h"
00024 #include "GapEvents.h"
00025 
00026 /**************************************************************************/
00027 /*!
00028     \brief
00029     The base class used to abstract GAP functionality to a specific radio
00030     transceiver, SOC or BLE Stack.
00031 */
00032 /**************************************************************************/
00033 class Gap
00034 {
00035 public:
00036     typedef enum addr_type_e {
00037         ADDR_TYPE_PUBLIC = 0,
00038         ADDR_TYPE_RANDOM_STATIC,
00039         ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE,
00040         ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE
00041     } addr_type_t;
00042 
00043     /**
00044      * enumeration for disconnection reasons. The values for these reasons are
00045      * derived from Nordic's implementation; but the reasons are meant to be
00046      * independent of the transport. If you are returned a reason which is not
00047      * covered by this enumeration, then please refer to the underlying
00048      * transport library.
00049      */
00050     enum DisconnectionReason_t {
00051         REMOTE_USER_TERMINATED_CONNECTION = 0x13,
00052         LOCAL_HOST_TERMINATED_CONNECTION  = 0x16,
00053         CONN_INTERVAL_UNACCEPTABLE        = 0x3B,
00054     };
00055 
00056     /* Describes the current state of the device (more than one bit can be set) */
00057     typedef struct GapState_s {
00058         unsigned advertising : 1; /**< peripheral is currently advertising */
00059         unsigned connected   : 1; /**< peripheral is connected to a central */
00060     } GapState_t;
00061 
00062     typedef uint16_t Handle_t;
00063 
00064     typedef struct {
00065       uint16_t minConnectionInterval;        /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
00066       uint16_t maxConnectionInterval;        /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
00067       uint16_t slaveLatency;                 /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
00068       uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
00069     } ConnectionParams_t;
00070 
00071 public:
00072     /* These functions must be defined in the sub-class */
00073     virtual ble_error_t setAddress(addr_type_t type, const uint8_t address[6]) = 0;
00074     virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0;
00075     virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0;
00076     virtual ble_error_t stopAdvertising(void)                    = 0;
00077     virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0;
00078     virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0;
00079     virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0;
00080     virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0;
00081 
00082     virtual ble_error_t setDeviceName(const uint8_t *deviceName) = 0;
00083     virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0;
00084     virtual ble_error_t setAppearance(uint16_t appearance) = 0;
00085     virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0;
00086 
00087     typedef void (*EventCallback_t)(void);
00088     typedef void (*ConnectionEventCallback_t)(Handle_t, const ConnectionParams_t *);
00089     typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t);
00090 
00091     /* Event callback handlers */
00092     void setOnTimeout(EventCallback_t callback) {
00093         onTimeout = callback;
00094     }
00095     void setOnConnection(ConnectionEventCallback_t callback) {
00096         onConnection = callback;
00097     }
00098     void setOnDisconnection(DisconnectionEventCallback_t callback) {
00099         onDisconnection = callback;
00100     }
00101 
00102     void processConnectionEvent(Handle_t handle, const ConnectionParams_t *params) {
00103         state.connected = 1;
00104         if (onConnection) {
00105             onConnection(handle, params);
00106         }
00107     }
00108 
00109     void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) {
00110         state.connected = 0;
00111         if (onDisconnection) {
00112             onDisconnection(handle, reason);
00113         }
00114     }
00115 
00116     void processEvent(GapEvents::gapEvent_e type) {
00117         switch (type) {
00118             case GapEvents::GAP_EVENT_TIMEOUT:
00119                 state.advertising = 0;
00120                 if (onTimeout) {
00121                     onTimeout();
00122                 }
00123                 break;
00124         }
00125     }
00126 
00127     GapState_t getState(void) const {
00128         return state;
00129     }
00130 
00131 protected:
00132     Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL) {
00133         /* empty */
00134     }
00135 
00136 protected:
00137     GapState_t state;
00138 
00139 private:
00140     EventCallback_t              onTimeout;
00141     ConnectionEventCallback_t    onConnection;
00142     DisconnectionEventCallback_t onDisconnection;
00143 };
00144 
00145 #endif // ifndef __GAP_H__