最新revisionだとなんかerrorになるので、暫定的に rev 111にrevert。ごめんなさい。こういうときどういうふうにcommitすればいいのか分からなかったので。

Dependents:   MiniSteer_BLE

Fork of BLE_API by Bluetooth Low Energy

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     /* Describes the current state of the device (more than one bit can be set) */
00044     typedef struct GapState_s {
00045         unsigned advertising : 1; /**< peripheral is currently advertising */
00046         unsigned connected   : 1; /**< peripheral is connected to a central */
00047     } GapState_t;
00048 
00049     typedef uint16_t Handle_t;
00050 
00051     typedef struct {
00052       uint16_t minConnectionInterval;        /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
00053       uint16_t maxConnectionInterval;        /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
00054       uint16_t slaveLatency;                 /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
00055       uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
00056     } ConnectionParams_t;
00057 
00058 public:
00059     /* These functions must be defined in the sub-class */
00060     virtual ble_error_t setAddress(addr_type_t type, const uint8_t address[6]) = 0;
00061     virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0;
00062     virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0;
00063     virtual ble_error_t stopAdvertising(void)                    = 0;
00064     virtual ble_error_t disconnect(void)                         = 0;
00065     virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0;
00066     virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0;
00067     virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0;
00068 
00069     typedef void (*EventCallback_t)(void);
00070     typedef void (*HandleSpecificEventCallback_t)(Handle_t);
00071 
00072     /* Event callback handlers */
00073     void setOnTimeout(EventCallback_t callback) {
00074         onTimeout = callback;
00075     }
00076     void setOnConnection(HandleSpecificEventCallback_t callback) {
00077         onConnection = callback;
00078     }
00079     void setOnDisconnection(HandleSpecificEventCallback_t callback) {
00080         onDisconnection = callback;
00081     }
00082 
00083     void processHandleSpecificEvent(GapEvents::gapEvent_e type, Handle_t handle) {
00084         switch (type) {
00085             case GapEvents::GAP_EVENT_CONNECTED:
00086                 state.connected = 1;
00087                 if (onConnection) {
00088                     onConnection(handle);
00089                 }
00090                 break;
00091             case GapEvents::GAP_EVENT_DISCONNECTED:
00092                 state.connected = 0;
00093                 if (onDisconnection) {
00094                     onDisconnection(handle);
00095                 }
00096                 break;
00097         }
00098     }
00099 
00100     void processEvent(GapEvents::gapEvent_e type) {
00101         switch (type) {
00102             case GapEvents::GAP_EVENT_TIMEOUT:
00103                 state.advertising = 0;
00104                 if (onTimeout) {
00105                     onTimeout();
00106                 }
00107                 break;
00108         }
00109     }
00110 
00111     GapState_t getState(void) const {
00112         return state;
00113     }
00114 
00115 protected:
00116     Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL) {
00117         /* empty */
00118     }
00119 
00120 protected:
00121     GapState_t state;
00122 
00123 private:
00124     EventCallback_t               onTimeout;
00125     HandleSpecificEventCallback_t onConnection;
00126     HandleSpecificEventCallback_t onDisconnection;
00127 };
00128 
00129 #endif // ifndef __GAP_H__