Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / linux / server.c
1 /******************************************************************
2  *
3  * Copyright 2015 Intel Corporation All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ******************************************************************/
18
19 #include "server.h"
20
21 #include "cacommon.h"
22 #include "logger.h"
23
24 #include <assert.h>
25
26
27 // Logging tag.
28 static char const TAG[] = "BLE_SERVER";
29
30 // ---------------------------------------------------------------------
31 //                      GATT Request Handling
32 // ---------------------------------------------------------------------
33 void CAGattServerHandleRequestData()
34 {
35 }
36
37 // ---------------------------------------------------------------------
38 //                      GATT Response Handling
39 // ---------------------------------------------------------------------
40 /**
41  * Send response data to the GATT client.
42  *
43  * Respone data will be sent to the client through the given response
44  * @a characteristic proxy as a GATT characteristic notification.
45  *
46  * @param[in] characteristic The D-Bus proxy for the response
47  *                           characteristic through which the
48  *                           notification will be sent.
49  * @param[in] data           The byte array to be sent.
50  * @param[in] length         The number of elements in the byte
51  *                           array.
52  */
53 static bool CAGattServerSendResponseNotification(
54     GattCharacteristic1 * characteristic,
55     char const * data,
56     size_t length)
57 {
58     if (!gatt_characteristic1_get_notifying(characteristic))
59     {
60         OIC_LOG(WARNING,
61                 TAG,
62                 "Attempt to send response with notifications "
63                 "disabled.\n"
64                 "Client must enable notifications. "
65                 "No response was sent.");
66
67         return false;
68     }
69
70     GVariant * const value =
71         g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE,
72                                   data,
73                                   length,
74                                   sizeof(data[0]));
75
76     /**
77      * Send the response fragment by setting the "Value" property on
78      * the response characteristic, and emitting the
79      * @c org.freedesktop.Dbus.Properties.PropertiesChanged signal,
80      * accordingly.
81      *
82      * @todo Do we need to explicitly emit the @c GObject @c notify or
83      *       @c org.freedesktop.Dbus.Properties.PropertiesChanged
84      *       signal here?
85      */
86     gatt_characteristic1_set_value(characteristic, value);
87
88     return true;
89 }
90
91 bool CAGattServerSendResponse(void const * method_info,
92                               void const * data,
93                               size_t length)
94 {
95     assert(method_info != NULL);
96
97     CAGattResponseInfo const * const info = method_info;
98
99     GattCharacteristic1 * const characteristic =
100         info->characteristic;
101
102     if (!CAGattServerSendResponseNotification(characteristic,
103                                               (char const *) data,
104                                               length))
105     {
106         return false;
107     }
108
109     return true;
110 }