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.
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.
/* global functions ********************************************************* */
void BleInit(void){
BleStartSyncSemphr = xSemaphoreCreateBinary();
BleWakeUpSyncSemphr = xSemaphoreCreateBinary();
SendCompleteSync = xSemaphoreCreateBinary();
BlePeripheral_Initialize(BleEventCallBack, CreateServiceCallback);
BlePeripheral_SetDeviceName((uint8_t*) "XDK BLE Guide");
if(RETCODE_OK == BlePeripheral_Start()) {
xSemaphoreTake(BleStartSyncSemphr, BLE_START_SYNC_TIMEOUT);
}
if(RETCODE_OK == BlePeripheral_Wakeup()) {
xSemaphoreTake(BleWakeUpSyncSemphr, BLE_WAKEUP_SYNC_TIMEOUT);
}
}
/**
* @brief This is a template function where the user can write his custom application.
*
*/
void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
{
if (CmdProcessorHandle == NULL)
{
printf("Command processor handle is null \n\r");
assert(false);
}
BCDS_UNUSED(param2);
vTaskDelay(5000);
BleInit();
TransmitBleData();
}