1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
22 //logger.h included first to avoid conflict with RBL library PROGMEM attribute
25 #include "cableserver.h"
30 #include <RBL_nRF8001.h>
32 #include "caleinterface.h"
33 #include "oic_malloc.h"
34 #include "caadapterutils.h"
35 #include "cafragmentation.h"
40 * @var g_bleServerDataReceivedCallback
41 * @brief Maintains the callback to be notified on receival of network packets from other
44 static CABLEDataReceivedCallback g_bleServerDataReceivedCallback = NULL;
47 * @def MAX_EVENT_COUNT
48 * @brief Maximum number of tries to get the event on BLE Shield address.
50 #define MAX_EVENT_COUNT 20
52 static bool g_serverRunning = false;
53 static uint8_t *g_coapBuffer = NULL;
56 * @var g_receivedDataLen
57 * @brief Actual length of data received.
59 static uint32_t g_receivedDataLen = 0;
62 * @var g_packetDataLen
63 * @brief Total Length of data that is being fragmented.
65 static uint32_t g_packetDataLen = 0;
67 void CACheckLEDataInternal()
71 if (CAIsLEDataAvailable())
73 // Allocate Memory for COAP Buffer and do ParseHeader
74 if (NULL == g_coapBuffer)
76 OIC_LOG(DEBUG, TAG, "IN");
77 uint8_t headerArray[CA_HEADER_LENGTH];
78 while (CAIsLEDataAvailable() && g_receivedDataLen < CA_HEADER_LENGTH)
80 headerArray[g_receivedDataLen++] = CALEReadData();
83 g_packetDataLen = CAParseHeader(headerArray, CA_HEADER_LENGTH);
85 if (g_packetDataLen > COAP_MAX_PDU_SIZE)
87 OIC_LOG(ERROR, TAG, "len > pdu_size");
91 g_coapBuffer = (uint8_t *)OICCalloc((size_t)g_packetDataLen, 1);
92 if (NULL == g_coapBuffer)
94 OIC_LOG(ERROR, TAG, "malloc");
98 OIC_LOG(DEBUG, TAG, "OUT");
99 g_receivedDataLen = 0;
102 OIC_LOG(DEBUG, TAG, "IN");
103 while (CAIsLEDataAvailable())
105 OIC_LOG(DEBUG, TAG, "In While loop");
106 g_coapBuffer[g_receivedDataLen++] = CALEReadData();
107 if (g_receivedDataLen == g_packetDataLen)
109 OIC_LOG(DEBUG, TAG, "Read Comp BLE Pckt");
110 if (g_receivedDataLen > 0)
112 OIC_LOG_V(DEBUG, TAG, "recv dataLen=%u", g_receivedDataLen);
113 uint32_t sentLength = 0;
114 // g_coapBuffer getting freed by CAMesssageHandler
115 g_bleServerDataReceivedCallback("", g_coapBuffer,
116 g_receivedDataLen, &sentLength);
119 g_receivedDataLen = 0;
124 OIC_LOG(DEBUG, TAG, "OUT");
128 OIC_LOG(DEBUG, TAG, "NoData");
133 CAResult_t CALEInitialize()
135 OIC_LOG(DEBUG, TAG, "IN");
137 // Set your BLE Shield name here, max. length 10
138 ble_set_name(__OIC_DEVICE_NAME__);
140 OIC_LOG(DEBUG, TAG, "LEName Set");
144 OIC_LOG(DEBUG, TAG, "OUT");
148 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
150 OIC_LOG(DEBUG, TAG, "IN");
151 OIC_LOG(DEBUG, TAG, "OUT");
154 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
156 OIC_LOG(DEBUG, TAG, "IN");
157 OIC_LOG(DEBUG, TAG, "OUT");
160 unsigned char CAIsLEDataAvailable()
162 return ble_available();
165 unsigned char CAIsLEConnected()
167 return ble_connected();
170 uint8_t CALEReadData()
172 return (uint8_t)ble_read();
175 CAResult_t CALEDoEvents()
181 CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *char_value,
182 uint32_t valueLength)
184 // ble_write_bytes() api can send only max of 255 bytes at a time
185 // This function shall never be called to send more than 255 bytes by the fragmentation logic.
186 // Currently ble_write_bytes api returns void.
187 ble_write_bytes((unsigned char *)char_value, (unsigned char)valueLength);
191 void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback)
193 OIC_LOG(DEBUG, TAG, "IN");
194 g_bleServerDataReceivedCallback = callback;
195 OIC_LOG(DEBUG, TAG, "OUT");
198 CAResult_t CAStartLEGattServer()
200 OIC_LOG(DEBUG, TAG, "IN");
201 CAResult_t result = CALEInitialize();
202 if (CA_STATUS_OK != result)
204 OIC_LOG_V(ERROR, TAG, "ble init fail: %d", result);
205 return CA_STATUS_FAILED;
208 * Below for loop is to process the BLE Events received from BLE Shield.
209 * BLE Events includes BLE Shield Address Added as a patch to RBL Library.
211 for (int iter = 0; iter < MAX_EVENT_COUNT; iter++)
213 CACheckLEDataInternal();
216 g_serverRunning = true;
217 OIC_LOG(DEBUG, TAG, "OUT");
221 CAResult_t CAStopLEGattServer()
223 OIC_LOG(DEBUG, TAG, "IN");
224 // There is no server running to stop.
225 OIC_LOG(DEBUG, TAG, "OUT");
229 void CATerminateLEGattServer()
231 OIC_LOG(DEBUG, TAG, "IN");
233 g_serverRunning = false;
234 OIC_LOG(DEBUG, TAG, "OUT");
240 if (false == g_serverRunning)
242 OIC_LOG(ERROR, TAG, "Server is not running");
245 CACheckLEDataInternal();