Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GattServer.h Source File

GattServer.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 MBED_GATT_SERVER_H__
00018 #define MBED_GATT_SERVER_H__
00019 
00020 #include "ble/common/StaticInterface.h"
00021 #include "ble/GattService.h"
00022 #include "ble/GattAttribute.h"
00023 #include "ble/GattServerEvents.h"
00024 #include "ble/GattCallbackParamTypes.h"
00025 #include "ble/CallChainOfFunctionPointersWithContext.h"
00026 #include "BleImplementationForward.h"
00027 
00028 #if !defined(DOXYGEN_ONLY)
00029 namespace ble {
00030 namespace interface {
00031 #endif
00032 
00033 /**
00034  * @addtogroup ble
00035  * @{
00036  * @addtogroup gatt
00037  * @{
00038  * @addtogroup server
00039  * @{
00040  */
00041 
00042 /**
00043  * Construct and operates a GATT server.
00044  *
00045  * A Gatt server is a collection of GattService; these services contain
00046  * characteristics that a peer connected to the device may read or write.
00047  * These characteristics may also emit updates to subscribed clients when their
00048  * values change.
00049  *
00050  * @p Server Layout
00051  *
00052  * Application code can add a GattService object to the server with the help of
00053  * the function addService(). That function registers all the GattCharacteristic
00054  * enclosed in the service, as well as all the characteristics descriptors (see
00055  * GattAttribute) these characteristics contain. Service registration assigns
00056  * a unique handle to the various attributes being part of the service; this
00057  * handle should be used for subsequent read or write of these components.
00058  *
00059  * There are no primitives defined to remove a single service; however, a call to
00060  * the function reset() removes all services previously registered in the
00061  * GattServer.
00062  *
00063  * @p Characteristic and attributes access
00064  *
00065  * Values of the characteristic and the characteristic descriptor present in the
00066  * GattServer must be accessed through the handle assigned to them when the service
00067  * has been registered; the GattServer class offers several flavors of read()
00068  * and write() functions that retrieve or mutate an attribute value.
00069  *
00070  * Application code can query if a client has subscribed to a given
00071  * characteristic's value update by invoking the function areUpdatesEnabled().
00072  *
00073  * @p Events
00074  *
00075  * The GattServer allows application code to register several event handlers that
00076  * can be used to monitor client and server activities:
00077  *   - onDataSent(): Register an event handler that is called when a
00078  *     characteristic value update has been sent to a client.
00079  *   - onDataWriten(): Register an event handler that is called when a
00080  *     client has written an attribute of the server.
00081  *   - onDataRead(): Register an event handler that is called when a
00082  *     client has read an attribute of the server.
00083  *   - onUpdatesEnabled: Register an event handler that is called when a
00084  *     client subscribes to updates of a characteristic.
00085  *   - onUpdatesDisabled: Register an event handler that is called when a
00086  *     client unsubscribes from updates of a characteristic.
00087  *   - onConfimationReceived: Register an event handler that is called
00088  *     when a client acknowledges a characteristic value notification.
00089  *
00090  * @note The term characteristic value update is used to represent
00091  * Characteristic Value Notification and Characteristic Value Indication when
00092  * the nature of the server initiated is not relevant.
00093  */
00094 #if !defined(DOXYGEN_ONLY)
00095 template <class Impl>
00096 class GattServer : public StaticInterface<Impl, GattServer> {
00097 #else
00098 class GattServer {
00099 #endif
00100 
00101     using StaticInterface<Impl, ::ble::interface::GattServer>::impl;
00102 
00103 public:
00104 
00105     /**
00106      * Definition of the general handler of GattServer related events.
00107      */
00108     struct EventHandler {
00109         /**
00110          * Function invoked when the connections changes the ATT_MTU which controls
00111          * the maximum size of an attribute that can be read in a single L2CAP packet
00112          * which might be fragmented across multiple packets.
00113          *
00114          * @param connectionHandle The handle of the connection that changed the size.
00115          * @param attMtuSize
00116          */
00117         virtual void onAttMtuChange(
00118             ble::connection_handle_t connectionHandle,
00119             uint16_t attMtuSize
00120         )
00121         {
00122         }
00123     };
00124 
00125     /**
00126      * Assign the event handler implementation that will be used by the
00127      * module to signal events back to the application.
00128      *
00129      * @param handler Application implementation of an EventHandler.
00130      */
00131     void setEventHandler(EventHandler *handler)
00132     {
00133         eventHandler = handler;
00134     }
00135 
00136     /**
00137      * Event handler invoked when the server has sent data to a client.
00138      *
00139      * @see onDataSent().
00140      */
00141     typedef FunctionPointerWithContext<unsigned> DataSentCallback_t;
00142 
00143     /**
00144      * Callchain of DataSentCallback_t objects.
00145      *
00146      * @see onDataSent().
00147      */
00148     typedef CallChainOfFunctionPointersWithContext<unsigned>
00149         DataSentCallbackChain_t;
00150 
00151     /**
00152      * Event handler invoked when the client has written an attribute of the
00153      * server.
00154      *
00155      * @see onDataWritten().
00156      */
00157     typedef FunctionPointerWithContext<const GattWriteCallbackParams*>
00158         DataWrittenCallback_t;
00159 
00160     /**
00161      * Callchain of DataWrittenCallback_t objects.
00162      *
00163      * @see onDataWritten().
00164      */
00165     typedef CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams*>
00166         DataWrittenCallbackChain_t;
00167 
00168     /**
00169      * Event handler invoked when the client has read an attribute of the server.
00170      *
00171      * @see onDataRead().
00172      */
00173     typedef FunctionPointerWithContext<const GattReadCallbackParams*>
00174         DataReadCallback_t;
00175 
00176     /**
00177      * Callchain of DataReadCallback_t.
00178      *
00179      * @see onDataRead().
00180      */
00181     typedef CallChainOfFunctionPointersWithContext<const GattReadCallbackParams*>
00182         DataReadCallbackChain_t;
00183 
00184     /**
00185      * Event handler invoked when the GattServer is reset.
00186      *
00187      * @see onShutdown() reset()
00188      */
00189     typedef FunctionPointerWithContext<const GattServer *>
00190         GattServerShutdownCallback_t;
00191 
00192     /**
00193      * Callchain of GattServerShutdownCallback_t.
00194      *
00195      * @see onShutdown() reset()
00196      */
00197     typedef CallChainOfFunctionPointersWithContext<const GattServer*>
00198         GattServerShutdownCallbackChain_t;
00199 
00200     /**
00201      * Event handler that handles subscription to characteristic updates,
00202      * unsubscription from characteristic updates and notification confirmation.
00203      *
00204      * @see onUpdatesEnabled() onUpdateDisabled() onConfirmationReceived()
00205      */
00206     typedef FunctionPointerWithContext<GattAttribute::Handle_t> EventCallback_t;
00207 
00208 protected:
00209     /**
00210      * Construct a GattServer instance.
00211      */
00212     GattServer();
00213 
00214     /*
00215      * The following functions are meant to be overridden in the platform
00216      * specific subclass.
00217      */
00218 public:
00219 
00220     /**
00221      * Add a service declaration to the local attribute server table.
00222      *
00223      * This functions inserts a service declaration in the attribute table
00224      * followed by the characteristic declarations (including characteristic
00225      * descriptors) present in @p service.
00226      *
00227      * The process assigns a unique attribute handle to all the elements added
00228      * into the attribute table. This handle is an ID that must be used for
00229      * subsequent interractions with the elements.
00230      *
00231      * @note There is no mirror function that removes a single service.
00232      * Application code can remove all the registered services by calling
00233      * reset().
00234      *
00235      * @attention Service, characteristics and descriptors objects registered
00236      * within the GattServer must remain reachable until reset() is called.
00237      *
00238      * @param[in] service The service to be added; attribute handle of services,
00239      * characteristic and characteristic descriptors are updated by the
00240      * process.
00241      *
00242      * @return BLE_ERROR_NONE if the service was successfully added.
00243      */
00244     ble_error_t addService(GattService &service);
00245 
00246     /**
00247      * Read the value of an attribute present in the local GATT server.
00248      *
00249      * @param[in] attributeHandle Handle of the attribute to read.
00250      * @param[out] buffer A buffer to hold the value being read.
00251      * @param[in,out] lengthP Length of the buffer being supplied. If the
00252      * attribute value is longer than the size of the supplied buffer, this
00253      * variable holds upon return the total attribute value length (excluding
00254      * offset). The application may use this information to allocate a suitable
00255      * buffer size.
00256      *
00257      * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
00258      *
00259      * @attention read(ble::connection_handle_t, GattAttribute::Handle_t, uint8_t *, uint16_t *)
00260      * must be used to read Client Characteristic Configuration Descriptor (CCCD)
00261      * because the value of this type of attribute depends on the connection.
00262      */
00263     ble_error_t read(
00264         GattAttribute::Handle_t attributeHandle,
00265         uint8_t buffer[],
00266         uint16_t *lengthP
00267     );
00268 
00269     /**
00270      * Read the value of an attribute present in the local GATT server.
00271      *
00272      * The connection handle allows application code to read the value of a
00273      * Client Characteristic Configuration Descriptor for a given connection.
00274      *
00275      * @param[in] connectionHandle Connection handle.
00276      * @param[in] attributeHandle Attribute handle for the value attribute of
00277      * the characteristic.
00278      * @param[out] buffer A buffer to hold the value being read.
00279      * @param[in,out] lengthP Length of the buffer being supplied. If the
00280      * attribute value is longer than the size of the supplied buffer, this
00281      * variable holds upon return the total attribute value length (excluding
00282      * offset). The application may use this information to allocate a suitable
00283      * buffer size.
00284      *
00285      * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
00286      */
00287     ble_error_t read(
00288         ble::connection_handle_t connectionHandle,
00289         GattAttribute::Handle_t attributeHandle,
00290         uint8_t *buffer,
00291         uint16_t *lengthP
00292     );
00293 
00294     /**
00295      * Update the value of an attribute present in the local GATT server.
00296      *
00297      * @param[in] attributeHandle Handle of the attribute to write.
00298      * @param[in] value A pointer to a buffer holding the new value.
00299      * @param[in] size Size in bytes of the new value (in bytes).
00300      * @param[in] localOnly If this flag is false and the attribute handle
00301      * written is a characteristic value, then the server sends an update
00302      * containing the new value to all clients that have subscribed to the
00303      * characteristic's notifications or indications. Otherwise, the update does
00304      * not generate a single server initiated event.
00305      *
00306      * @return BLE_ERROR_NONE if the attribute value has been successfully
00307      * updated.
00308      */
00309     ble_error_t write(
00310         GattAttribute::Handle_t attributeHandle,
00311         const uint8_t *value,
00312         uint16_t size,
00313         bool localOnly = false
00314     );
00315 
00316     /**
00317      * Update the value of an attribute present in the local GATT server.
00318      *
00319      * The connection handle parameter allows application code to direct
00320      * notification or indication resulting from the update to a specific client.
00321      *
00322      * @param[in] connectionHandle Connection handle.
00323      * @param[in] attributeHandle Handle for the value attribute of the
00324      * characteristic.
00325      * @param[in] value A pointer to a buffer holding the new value.
00326      * @param[in] size Size of the new value (in bytes).
00327      * @param[in] localOnly If this flag is false and the attribute handle
00328      * written is a characteristic value, then the server sends an update
00329      * containing the new value to the client identified by the parameter
00330      * @p connectionHandle if it is subscribed to the characteristic's
00331      * notifications or indications. Otherwise, the update does not generate a
00332      * single server initiated event.
00333      *
00334      * @return BLE_ERROR_NONE if the attribute value has been successfully
00335      * updated.
00336      */
00337     ble_error_t write(
00338         ble::connection_handle_t connectionHandle,
00339         GattAttribute::Handle_t attributeHandle,
00340         const uint8_t *value,
00341         uint16_t size,
00342         bool localOnly = false
00343     );
00344 
00345     /**
00346      * Determine if one of the connected clients has subscribed to notifications
00347      * or indications of the characteristic in input.
00348      *
00349      * @param[in] characteristic The characteristic.
00350      * @param[out] enabledP Upon return, *enabledP is true if updates are
00351      * enabled for a connected client; otherwise, *enabledP is false.
00352      *
00353      * @return BLE_ERROR_NONE if the connection and handle are found. False
00354      * otherwise.
00355      */
00356     ble_error_t areUpdatesEnabled(
00357         const GattCharacteristic &characteristic,
00358         bool *enabledP
00359     );
00360 
00361     /**
00362      * Determine if an identified client has subscribed to notifications or
00363      * indications of a given characteristic.
00364      *
00365      * @param[in] connectionHandle The connection handle.
00366      * @param[in] characteristic The characteristic.
00367      * @param[out] enabledP Upon return, *enabledP is true if the client
00368      * identified by @p connectionHandle has subscribed to notifications or
00369      * indications of @p characteristic; otherwise, *enabledP is false.
00370      *
00371      * @return BLE_ERROR_NONE if the connection and handle are found. False
00372      * otherwise.
00373      */
00374     ble_error_t areUpdatesEnabled(
00375         ble::connection_handle_t connectionHandle,
00376         const GattCharacteristic &characteristic,
00377         bool *enabledP
00378     );
00379 
00380     /**
00381      * Indicate if the underlying stack emit events when an attribute is read by
00382      * a client.
00383      *
00384      * @attention This function should be overridden to return true if
00385      * applicable.
00386      *
00387      * @return true if onDataRead is supported; false otherwise.
00388      */
00389     bool isOnDataReadAvailable() const;
00390 
00391     /*
00392      * APIs with nonvirtual implementations.
00393      */
00394 public:
00395     /**
00396      * Add an event handler that monitors emission of characteristic value
00397      * updates.
00398      *
00399      * @param[in] callback Event handler being registered.
00400      *
00401      * @note It is possible to chain together multiple onDataSent callbacks
00402      * (potentially from different modules of an application).
00403      */
00404     void onDataSent(const DataSentCallback_t &callback)
00405     {
00406         dataSentCallChain.add(callback);
00407     }
00408 
00409     /**
00410      * Add an event handler that monitors emission of characteristic value
00411      * updates.
00412      *
00413      * @param[in] objPtr Pointer to the instance that is used to invoke the
00414      * event handler.
00415      * @param[in] memberPtr Event handler being registered. It is a member
00416      * function.
00417      */
00418     template <typename T>
00419     void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count))
00420     {
00421         dataSentCallChain.add(objPtr, memberPtr);
00422     }
00423 
00424     /**
00425      * Access the callchain of data sent event handlers.
00426      *
00427      * @return A reference to the DATA_SENT event callback chain.
00428      */
00429     DataSentCallbackChain_t &onDataSent()
00430     {
00431         return dataSentCallChain;
00432     }
00433 
00434     /**
00435      * Set an event handler that is called after
00436      * a connected peer has written an attribute.
00437      *
00438      * @param[in] callback The event handler being registered.
00439      *
00440      * @attention It is possible to set multiple event handlers. Registered
00441      * handlers may be removed with onDataWritten().detach(callback).
00442      */
00443     void onDataWritten(const DataWrittenCallback_t &callback)
00444     {
00445         dataWrittenCallChain.add(callback);
00446     }
00447 
00448     /**
00449      * Set an event handler that is called after
00450      * a connected peer has written an attribute.
00451      *
00452      * @param[in] objPtr Pointer to the instance that is used to invoke the
00453      * event handler (@p memberPtr).
00454      * @param[in] memberPtr Event handler being registered. It is a member
00455      * function.
00456      */
00457     template <typename T>
00458     void onDataWritten(
00459         T *objPtr,
00460         void (T::*memberPtr)(const GattWriteCallbackParams *context)
00461     ) {
00462         dataWrittenCallChain.add(objPtr, memberPtr);
00463     }
00464 
00465     /**
00466      * Access the callchain of data written event handlers.
00467      *
00468      * @return A reference to the data written event callbacks chain.
00469      *
00470      * @note It is possible to register callbacks using
00471      * onDataWritten().add(callback).
00472      *
00473      * @note It is possible to unregister callbacks using
00474      * onDataWritten().detach(callback).
00475      */
00476     DataWrittenCallbackChain_t &onDataWritten()
00477     {
00478         return dataWrittenCallChain;
00479     }
00480 
00481     /**
00482      * Set an event handler that monitors attribute reads from connected clients.
00483      *
00484      * @param[in] callback Event handler being registered.
00485      *
00486      * @return BLE_ERROR_NOT_IMPLEMENTED if this functionality isn't available;
00487      * else BLE_ERROR_NONE.
00488      *
00489      * @note  This functionality may not be available on all underlying stacks.
00490      * Application code may work around that limitation by monitoring read
00491      * requests instead of read events.
00492      *
00493      * @see GattCharacteristic::setReadAuthorizationCallback()
00494      * @see isOnDataReadAvailable().
00495      *
00496      * @attention It is possible to set multiple event handlers. Registered
00497      * handlers may be removed with onDataRead().detach(callback).
00498      */
00499     ble_error_t onDataRead(const DataReadCallback_t &callback)
00500     {
00501         if (!isOnDataReadAvailable()) {
00502             return BLE_ERROR_NOT_IMPLEMENTED;
00503         }
00504 
00505         dataReadCallChain.add(callback);
00506         return BLE_ERROR_NONE;
00507     }
00508 
00509     /**
00510      * Set an event handler that monitors attribute reads from connected clients.
00511      *
00512      * @param[in] objPtr Pointer to the instance that is used to invoke the
00513      * event handler (@p memberPtr).
00514      * @param[in] memberPtr Event handler being registered. It is a member
00515      * function.
00516      */
00517     template <typename T>
00518     ble_error_t onDataRead(
00519         T *objPtr,
00520         void (T::*memberPtr)(const GattReadCallbackParams *context)
00521     ) {
00522         if (!isOnDataReadAvailable()) {
00523             return BLE_ERROR_NOT_IMPLEMENTED;
00524         }
00525 
00526         dataReadCallChain.add(objPtr, memberPtr);
00527         return BLE_ERROR_NONE;
00528     }
00529 
00530     /**
00531      * Access the callchain of data read event handlers.
00532      *
00533      * @return A reference to the data read event callbacks chain.
00534      *
00535      * @note It is possible to register callbacks using
00536      * onDataRead().add(callback).
00537      *
00538      * @note It is possible to unregister callbacks using
00539      * onDataRead().detach(callback).
00540      */
00541     DataReadCallbackChain_t &onDataRead()
00542     {
00543         return dataReadCallChain;
00544     }
00545 
00546     /**
00547      * Set an event handler that monitors shutdown or reset of the GattServer.
00548      *
00549      * The event handler is invoked when the GattServer instance is about
00550      * to be shut down. This can result in a call to reset() or BLE::reset().
00551      *
00552      * @param[in] callback Event handler being registered.
00553      *
00554      * @note It is possible to set up multiple shutdown event handlers.
00555      *
00556      * @note It is possible to unregister a callback using
00557      * onShutdown().detach(callback)
00558      */
00559     void onShutdown(const GattServerShutdownCallback_t &callback)
00560     {
00561         shutdownCallChain.add(callback);
00562     }
00563 
00564     /**
00565      * Set an event handler that monitors shutdown or reset of the GattServer.
00566      *
00567      * The event handler is invoked when the GattServer instance is about
00568      * to be shut down. This can result of a call to reset() or BLE::reset().
00569      *
00570      * @param[in] objPtr Pointer to the instance that is used to invoke the
00571      * event handler (@p memberPtr).
00572      * @param[in] memberPtr Event handler being registered. It is a member
00573      * function.
00574      */
00575     template <typename T>
00576     void onShutdown(T *objPtr, void (T::*memberPtr)(const GattServer *))
00577     {
00578         shutdownCallChain.add(objPtr, memberPtr);
00579     }
00580 
00581     /**
00582      * Access the callchain of shutdown event handlers.
00583      *
00584      * @return A reference to the shutdown event callbacks chain.
00585      *
00586      * @note It is possible to register callbacks using
00587      * onShutdown().add(callback).
00588      *
00589      * @note It is possible to unregister callbacks using
00590      * onShutdown().detach(callback).
00591      */
00592     GattServerShutdownCallbackChain_t& onShutdown()
00593     {
00594         return shutdownCallChain;
00595     }
00596 
00597     /**
00598      * Set up an event handler that monitors subscription to characteristic
00599      * updates.
00600      *
00601      * @param[in] callback Event handler being registered.
00602      */
00603     void onUpdatesEnabled(EventCallback_t callback)
00604     {
00605         updatesEnabledCallback = callback;
00606     }
00607 
00608     /**
00609      * Set up an event handler that monitors unsubscription from characteristic
00610      * updates.
00611      *
00612      * @param[in] callback Event handler being registered.
00613      */
00614     void onUpdatesDisabled(EventCallback_t callback)
00615     {
00616         updatesDisabledCallback = callback;
00617     }
00618 
00619     /**
00620      * Set up an event handler that monitors notification acknowledgment.
00621      *
00622      * The event handler is called when a client sends a confirmation that it has
00623      * correctly received a notification from the server.
00624      *
00625      * @param[in] callback Event handler being registered.
00626      */
00627     void onConfirmationReceived(EventCallback_t callback)
00628     {
00629         confirmationReceivedCallback = callback;
00630     }
00631 
00632     /* Entry points for the underlying stack to report events back to the user. */
00633 protected:
00634     /**
00635      * Helper function that notifies all registered handlers of an occurrence
00636      * of a data written event.
00637      *
00638      * @attention Vendor implementation must invoke this function after one of
00639      * the GattServer attributes has been written.
00640      *
00641      * @param[in] params The data written parameters passed to the registered
00642      * handlers.
00643      */
00644     void handleDataWrittenEvent(const GattWriteCallbackParams *params)
00645     {
00646         dataWrittenCallChain.call(params);
00647     }
00648 
00649     /**
00650      * Helper function that notifies all registered handlers of an occurrence
00651      * of a data read event.
00652      *
00653      * @attention Vendor implementation must invoke this function after one of
00654      * the GattServer attributes has been read.
00655      *
00656      * @param[in] params The data read parameters passed to the registered
00657      * handlers.
00658      */
00659     void handleDataReadEvent(const GattReadCallbackParams *params)
00660     {
00661         dataReadCallChain.call(params);
00662     }
00663 
00664     /**
00665      * Helper function that notifies the registered handler of an occurrence
00666      * of updates enabled, updates disabled or confirmation received events.
00667      *
00668      * @attention Vendor implementation must invoke this function when a client
00669      * subscribes to characteristic updates, unsubscribes from characteristic
00670      * updates or a notification confirmation has been received.
00671      *
00672      * @param[in] type The type of event that occurred.
00673      * @param[in] attributeHandle The handle of the attribute concerned by the
00674      * event.
00675      */
00676     void handleEvent(
00677         GattServerEvents::gattEvent_e type,
00678         GattAttribute::Handle_t attributeHandle
00679     ) {
00680         switch (type) {
00681             case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
00682                 if (updatesEnabledCallback) {
00683                     updatesEnabledCallback(attributeHandle);
00684                 }
00685                 break;
00686             case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
00687                 if (updatesDisabledCallback) {
00688                     updatesDisabledCallback(attributeHandle);
00689                 }
00690                 break;
00691             case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
00692                 if (confirmationReceivedCallback) {
00693                     confirmationReceivedCallback(attributeHandle);
00694                 }
00695                 break;
00696 
00697             case GattServerEvents::GATT_EVENT_DATA_SENT:
00698                 // Called every time a notification or indication has been sent
00699                 handleDataSentEvent(1);
00700                 break;
00701 
00702             default:
00703                 break;
00704         }
00705     }
00706 
00707     /**
00708      * Helper function that notifies all registered handlers of an occurrence
00709      * of a data sent event.
00710      *
00711      * @attention Vendor implementation must invoke this function after the
00712      * emission of a notification or an indication.
00713      *
00714      * @param[in] count Number of packets sent.
00715      */
00716     void handleDataSentEvent(unsigned count)
00717     {
00718         dataSentCallChain.call(count);
00719     }
00720 
00721 public:
00722     /**
00723      * Shut down the GattServer instance.
00724      *
00725      * This function notifies all event handlers listening for shutdown events
00726      * that the GattServer is about to be shut down; then it clears all
00727      * GattServer state.
00728      *
00729      * @note This function is meant to be overridden in the platform-specific
00730      * subclass. Overides must call the parent function before any cleanup.
00731      *
00732      * @return BLE_ERROR_NONE on success.
00733      */
00734     ble_error_t reset(void);
00735 
00736 protected:
00737     /* --- Abstract calls to override --- */
00738 
00739     /* Derived implementation must call the base reset_ */
00740     ble_error_t reset_(void);
00741 
00742     ble_error_t addService_(GattService &service);
00743 
00744     ble_error_t read_(
00745         GattAttribute::Handle_t attributeHandle,
00746         uint8_t buffer[],
00747         uint16_t *lengthP
00748     );
00749 
00750     ble_error_t read_(
00751         ble::connection_handle_t connectionHandle,
00752         GattAttribute::Handle_t attributeHandle,
00753         uint8_t *buffer,
00754         uint16_t *lengthP
00755     );
00756 
00757     ble_error_t write_(
00758         GattAttribute::Handle_t attributeHandle,
00759         const uint8_t *value,
00760         uint16_t size,
00761         bool localOnly
00762     );
00763 
00764     ble_error_t write_(
00765         ble::connection_handle_t connectionHandle,
00766         GattAttribute::Handle_t attributeHandle,
00767         const uint8_t *value,
00768         uint16_t size,
00769         bool localOnly
00770     );
00771 
00772     ble_error_t areUpdatesEnabled_(
00773         const GattCharacteristic &characteristic,
00774         bool *enabledP
00775     );
00776 
00777     ble_error_t areUpdatesEnabled_(
00778         ble::connection_handle_t connectionHandle,
00779         const GattCharacteristic &characteristic,
00780         bool *enabledP
00781     );
00782 
00783     bool isOnDataReadAvailable_() const;
00784 
00785 protected:
00786     /**
00787      * Event handler provided by the application.
00788      */
00789     EventHandler *eventHandler;
00790 
00791     /**
00792      * The total number of services added to the ATT table.
00793      */
00794     uint8_t serviceCount;
00795 
00796     /**
00797      * The total number of characteristics added to the ATT table.
00798      */
00799     uint8_t characteristicCount;
00800 
00801 private:
00802     /**
00803      * Callchain containing all registered callback handlers for data sent
00804      * events.
00805      */
00806     DataSentCallbackChain_t dataSentCallChain;
00807 
00808     /**
00809      * Callchain containing all registered callback handlers for data written
00810      * events.
00811      */
00812     DataWrittenCallbackChain_t dataWrittenCallChain;
00813 
00814     /**
00815      * Callchain containing all registered callback handlers for data read
00816      * events.
00817      */
00818     DataReadCallbackChain_t dataReadCallChain;
00819 
00820     /**
00821      * Callchain containing all registered callback handlers for shutdown
00822      * events.
00823      */
00824     GattServerShutdownCallbackChain_t shutdownCallChain;
00825 
00826     /**
00827      * The registered callback handler for updates enabled events.
00828      */
00829     EventCallback_t updatesEnabledCallback;
00830 
00831     /**
00832      * The registered callback handler for updates disabled events.
00833      */
00834     EventCallback_t updatesDisabledCallback;
00835 
00836     /**
00837      * The registered callback handler for confirmation received events.
00838      */
00839     EventCallback_t confirmationReceivedCallback;
00840 
00841 private:
00842     /* Disallow copy and assignment. */
00843     GattServer(const GattServer &);
00844     GattServer& operator=(const GattServer &);
00845 };
00846 
00847 /**
00848  * @}
00849  * @}
00850  * @}
00851  */
00852 
00853 #if !defined(DOXYGEN_ONLY)
00854 } // interface
00855 } // ble
00856 
00857 typedef ble::impl::GattServer GattServer;
00858 
00859 #endif
00860 
00861 
00862 #endif /* ifndef MBED_GATT_SERVER_H__ */