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>
33 #include "caleinterface.h"
34 #include "oic_malloc.h"
35 #include "caadapterutils.h"
39 * Maximum TCP header length.
41 #define TCP_MAX_HEADER_LENGTH 6
44 * Maintains the callback to be notified on receival of network packets from other
47 static CABLEDataReceivedCallback g_bleServerDataReceivedCallback = NULL;
50 * Maximum number of tries to get the event on BLE Shield address.
52 #define MAX_EVENT_COUNT 20
54 static bool g_serverRunning = false;
55 static uint8_t *g_coapBuffer = NULL;
58 * Actual length of data received.
60 static size_t g_receivedDataLen = 0;
63 * Total Length of data that is being fragmented.
65 static size_t g_packetDataLen = 0;
67 void CAGetTCPHeaderDetails(unsigned char* recvBuffer, size_t *headerlen)
69 coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
70 ((unsigned char *)recvBuffer)[0] >> 4);
71 *headerlen = coap_get_tcp_header_length_for_transport(transport);
74 void CACheckLEDataInternal()
78 if (CAIsLEDataAvailable())
80 // Allocate Memory for COAP Buffer and do ParseHeader
81 if (NULL == g_coapBuffer)
83 OIC_LOG(DEBUG, TAG, "IN");
84 size_t bufSize = TCP_MAX_HEADER_LENGTH;
85 g_coapBuffer = (uint8_t *)OICCalloc(bufSize, 1);
86 if (NULL == g_coapBuffer)
88 OIC_LOG(ERROR, TAG, "malloc");
92 g_coapBuffer[g_receivedDataLen++] = CALEReadData();
94 CAGetTCPHeaderDetails(g_coapBuffer, &headerLen);
95 OIC_LOG_V(INFO, TAG, "hdr len %d", headerLen);
96 while (CAIsLEDataAvailable() && g_receivedDataLen < headerLen)
98 g_coapBuffer[g_receivedDataLen++] = CALEReadData();
101 g_packetDataLen = coap_get_total_message_length(g_coapBuffer, g_receivedDataLen);
102 OIC_LOG_V(INFO, TAG, "pkt len %d", g_packetDataLen);
103 if (g_packetDataLen > COAP_MAX_PDU_SIZE)
105 OIC_LOG(ERROR, TAG, "len > pdu_size");
109 bufSize = g_packetDataLen;
110 uint8_t *newBuf = (uint8_t *)OICRealloc(g_coapBuffer, bufSize);
113 OIC_LOG(ERROR, TAG, "malloc");
114 OICFree(g_coapBuffer);
118 g_coapBuffer = newBuf;
121 while (CAIsLEDataAvailable())
123 OIC_LOG(DEBUG, TAG, "In While loop");
124 g_coapBuffer[g_receivedDataLen++] = CALEReadData();
125 if (g_receivedDataLen == g_packetDataLen)
127 OIC_LOG(DEBUG, TAG, "Read Comp BLE Pckt");
128 if (g_receivedDataLen > 0)
130 OIC_LOG_V(DEBUG, TAG, "recv dataLen=%u", g_receivedDataLen);
131 uint32_t sentLength = 0;
132 // g_coapBuffer getting freed by CAMesssageHandler
133 g_bleServerDataReceivedCallback("", g_coapBuffer,
134 g_receivedDataLen, &sentLength);
137 g_receivedDataLen = 0;
138 OICFree(g_coapBuffer);
143 OIC_LOG(DEBUG, TAG, "OUT");
147 OIC_LOG(DEBUG, TAG, "NoData");
152 CAResult_t CALEInitialize()
154 OIC_LOG(DEBUG, TAG, "IN");
156 // Set your BLE Shield name here, max. length 10
157 ble_set_name(__OIC_DEVICE_NAME__);
159 OIC_LOG(DEBUG, TAG, "LEName Set");
163 OIC_LOG(DEBUG, TAG, "OUT");
167 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
169 OIC_LOG(DEBUG, TAG, "IN");
170 OIC_LOG(DEBUG, TAG, "OUT");
173 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
175 OIC_LOG(DEBUG, TAG, "IN");
176 OIC_LOG(DEBUG, TAG, "OUT");
179 unsigned char CAIsLEDataAvailable()
181 return ble_available();
184 unsigned char CAIsLEConnected()
186 return ble_connected();
189 uint8_t CALEReadData()
191 return (uint8_t)ble_read();
194 CAResult_t CALEDoEvents()
200 CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *char_value,
201 uint32_t valueLength)
203 // ble_write_bytes() api can send only max of 255 bytes at a time
204 // This function shall never be called to send more than 255 bytes by the fragmentation logic.
205 // Currently ble_write_bytes api returns void.
206 ble_write_bytes((unsigned char *)char_value, (unsigned char)valueLength);
210 void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback)
212 OIC_LOG(DEBUG, TAG, "IN");
213 g_bleServerDataReceivedCallback = callback;
214 OIC_LOG(DEBUG, TAG, "OUT");
217 CAResult_t CAStartLEGattServer()
219 OIC_LOG(DEBUG, TAG, "IN");
220 CAResult_t result = CALEInitialize();
221 if (CA_STATUS_OK != result)
223 OIC_LOG_V(ERROR, TAG, "ble init fail: %d", result);
224 return CA_STATUS_FAILED;
227 * Below for loop is to process the BLE Events received from BLE Shield.
228 * BLE Events includes BLE Shield Address Added as a patch to RBL Library.
230 for (int iter = 0; iter < MAX_EVENT_COUNT; iter++)
232 CACheckLEDataInternal();
235 g_serverRunning = true;
236 OIC_LOG(DEBUG, TAG, "OUT");
240 CAResult_t CAStopLEGattServer()
242 OIC_LOG(DEBUG, TAG, "IN");
243 CATerminateLEGattServer();
244 OIC_LOG(DEBUG, TAG, "OUT");
248 CAResult_t CAInitializeLEGattServer()
250 OIC_LOG(DEBUG, TAG, "Initialize GATT Server");
254 void CATerminateLEGattServer()
256 OIC_LOG(DEBUG, TAG, "IN");
257 if (true == g_serverRunning)
260 g_serverRunning = false;
262 OIC_LOG(DEBUG, TAG, "OUT");
268 if (false == g_serverRunning)
270 OIC_LOG(ERROR, TAG, "Server is not running");
273 CACheckLEDataInternal();