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 CABLEServerDataReceivedCallback 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 char *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 char headerArray[CA_HEADER_LENGTH] = "";
78 while (CAIsLEDataAvailable() && g_receivedDataLen < CA_HEADER_LENGTH)
80 headerArray[g_receivedDataLen++] = CALEReadData();
83 g_packetDataLen = CAParseHeader(headerArray);
85 if (g_packetDataLen > COAP_MAX_PDU_SIZE)
87 OIC_LOG(ERROR, TAG, "len > pdu_size");
91 g_coapBuffer = (char *)OICCalloc((size_t)g_packetDataLen, sizeof(char));
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 g_coapBuffer[g_receivedDataLen] = '\0';
111 if (g_receivedDataLen > 0)
113 OIC_LOG_V(DEBUG, TAG, "recv dataLen=%d", g_receivedDataLen);
114 uint32_t sentLength = 0;
115 // g_coapBuffer getting freed by CAMesssageHandler
116 g_bleServerDataReceivedCallback("", "", g_coapBuffer,
117 g_receivedDataLen, &sentLength);
120 g_receivedDataLen = 0;
125 OIC_LOG(DEBUG, TAG, "OUT");
129 OIC_LOG(DEBUG, TAG, "NoData");
134 CAResult_t CALEInitialize()
136 OIC_LOG(DEBUG, TAG, "IN");
138 // Set your BLE Shield name here, max. length 10
139 ble_set_name(__OIC_DEVICE_NAME__);
141 OIC_LOG(DEBUG, TAG, "LEName Set");
145 OIC_LOG(DEBUG, TAG, "OUT");
149 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
151 OIC_LOG(DEBUG, TAG, "IN");
152 OIC_LOG(DEBUG, TAG, "OUT");
155 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
157 OIC_LOG(DEBUG, TAG, "IN");
158 OIC_LOG(DEBUG, TAG, "OUT");
161 unsigned char CAIsLEDataAvailable()
163 return ble_available();
166 unsigned char CAIsLEConnected()
168 return ble_connected();
172 return (char)ble_read();
175 CAResult_t CALEDoEvents()
181 CAResult_t CAUpdateCharacteristicsToAllGattClients(const char *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(CABLEServerDataReceivedCallback 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();