C
The next code is based on language C. This code allow us to send an receive data.
To access the libraries of the BLE API and ALPWISE stack library it is required to include these files in our code.
/* system header files */
#include <stdio.h>
/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"
/* own header files */
#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"
//#include "BCDS_Ble.h"
#include "BCDS_BlePeripheral.h"
#include "BCDS_BidirectionalService.h"
#include "BCDS_SensorServices.h"
#include "semphr.h"We are going to define some variables, in this case are Semaphores, and also notice that have global access. And also we define some constant that would help us to make the timeout.
/* constant definitions ***************************************************** */
#define BLE_START_SYNC_TIMEOUT UINT32_C(5000)
#define BLE_WAKEUP_SYNC_TIMEOUT UINT32_C(5000)
#define BLE_SEND_TIMEOUT UINT32_C(1000)
/* local variables ********************************************************** */
static SemaphoreHandle_t BleStartSyncSemphr = NULL;
static SemaphoreHandle_t BleWakeUpSyncSemphr = NULL;
static SemaphoreHandle_t SendCompleteSync = NULL;We need to define some function so we could set up the service. We need to define the incoming data and read request, and also the sending process of every message sent to the remote client.
BleDataReceivedCallback - would help us to copy the data that the XDK received via BLE into a buffer. If the message is to long, it will be cut off. Finally, the received data would be printed.
BleDataSentCallback function only releases a semaphore that handles synchroninzing multiple sending processes. This function is called everytime a message is done being sent.
The BLE library would give use some peripheral events, such as device connects, disconnects, etc. For this we need to define callback function. This function has two inputs, the event itself, and data that belongs to the event. The events hadthe corresponding data type.
The first two events occur during the starting up process. In these cases the semaphores, that were previously taken, will be given back. In the case of connection events, the remote client’s address will be printed. In disconnection events, a simple message will be printed.
In this part of the code are listed the functions that we need to called to get the BLE peripheral up and running, using all the funcitons that have previously defined.
Last updated
Was this helpful?