IOT - Bosch
  • Introduction
  • Devices
    • Bosch XDK 110
      • Introduction
      • Operating System
      • Hardware
        • Sensors
          • Accelerometer
            • C
          • Gyroscope
            • C
            • Mita
          • Magnetometer
            • C
          • Environmental
            • C
            • Mita
          • Ambient Light
            • C
            • Mita
          • Acoustic
            • C
      • Software
        • XDK WorkSpace
          • Structure
          • Debug
          • Supported languages
            • C
              • Static Library (.a)
            • XDK Live - Mita
            • MicroFlo
      • Connectivity
        • Bluetooth Low Energy (BLE)
          • Overview
          • General Info
          • Implementation
            • C
            • XDK Live
        • WI-FI
          • OverView
          • Implementation
            • C
            • XDK Live
        • WI-FI Enterprise
      • Protocols
        • CoAP
          • Overview
          • Implementation -TBD
        • HTTP
          • Overview
          • Structure and Methods
          • Implementation
            • C - Rest
            • C - Post
            • XDK Live
        • HTTPS
          • Overview
          • Implementation TBD
        • LWM2M
          • Overview
          • Implementation TBD
        • MQTT
          • Overview
          • Implementation
            • C
            • XDK Live
        • USB
          • Overview
          • Implementation TBD
      • Data Storage
      • XDK Extension Bus
      • Community
      • Applications
        • Language C
          • HomeAssitant - MQTT
            • Prerequisites
            • Server
            • Device
          • IOTA-MQTT-XDK
            • Prerequisites
            • XDK110
            • Mqtt JSON to MAM
            • SensorHub
            • Demo
        • Language XDK Live
          • MQTT
            • Hello World
            • HomeAssistant
              • Prerequisites
              • Server
              • Device
            • Docker-HomeAssistant
          • HTTP
            • Roku Remote Control
              • Roku API
              • MITA
                • Example
    • Bosch AMRA
    • Bosch GLM 100C
    • Bosch FLEXIDOME IP panoramic
    • Bosch GLM 100C
    • Bosch Rexroth Nexo
    • Bosch Rexroth PRC 7000
    • Bosch RRC / CT100
    • Bosch Rexroth IoT Gateway
  • Bosch IOT Solutions
    • Bosch IOT Projects
      • Smart Home
      • Industry 4.0
      • Smart Cities
      • Connected-mobility
    • Bosch IOT Suite
      • Bosch Analytics
      • Bosch IOT Hub
      • Bosch Iot Permission
      • IoT Remote Manager
      • IoT Rollouts
      • IoT Things
      • Demo TBD **
    • BPM and BRM
  • IOTA
    • Introduction
      • Tangle
      • Glossary
      • Differences with other tech
      • How does iota work
      • Developers
    • Qubic
      • What is Qubic?
      • Target
      • Qubic Protocol
    • Ecosystem
    • Applications
      • Python
      • XDK110
        • Prerequisites
        • XDK110
        • Mqtt JSON to MAM
        • SensorHub
    • Bosch/IOTA
  • ByteBall
    • SmartContract
    • Use Case
      • BoshCoins
Powered by GitBook
On this page

Was this helpful?

  1. Devices
  2. Bosch XDK 110
  3. Connectivity
  4. Bluetooth Low Energy (BLE)
  5. Implementation

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.

/* local functions ********************************************************** */

static void TransmitBleData(void)
{
    Retcode_T sendingReturnValue = BidirectionalService_SendData(
            (uint8_t*) "Hello Client!\0",
            (uint8_t) sizeof("Hello Client!\0") - 1);
    if (sendingReturnValue == RETCODE_OK) {
        xSemaphoreTake(SendCompleteSync, BLE_SEND_TIMEOUT);
    }
}

static void BleDataReceivedCallBack(uint8_t *rxBuffer, uint8_t rxDataLength)
{
    uint8_t bleReceiveBuff[UINT8_C(21)];
    {
        memset(bleReceiveBuff, 0, sizeof(bleReceiveBuff));
        memcpy(bleReceiveBuff, rxBuffer, rxDataLength < UINT8_C(21) ? rxDataLength : UINT8_C(21) - 1); 
        printf("Received data: %s \n", bleReceiveBuff); 
    } 
} 

static void BleDataSentCallback(Retcode_T sendStatus) { 
    BCDS_UNUSED(sendStatus); 
    xSemaphoreGive(SendCompleteSync); 
}

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.

static void BleEventCallBack(BlePeripheral_Event_T event, void * data) { 
    switch (event) { 

        case BLE_PERIPHERAL_STARTED: 
            xSemaphoreGive( BleStartSyncSemphr ); 
            break; 

        case BLE_PERIPHERAL_WAKEUP_SUCCEEDED: 
            xSemaphoreGive( BleWakeUpSyncSemphr ); 
            break; 

        case BLE_PERIPHERAL_CONNECTED: { 
            Ble_RemoteDeviceAddress_T *remoteAddress; 
            remoteAddress = (Ble_RemoteDeviceAddress_T*) data; 
            printf("Device connected: %02x:%02x:%02x:%02x:%02x:%02x \r\n", remoteAddress->Addr[0], remoteAddress->Addr[1], remoteAddress->Addr[2],
                        remoteAddress->Addr[3], remoteAddress->Addr[4], remoteAddress->Addr[5]);
        }
        break;

    case BLE_PERIPHERAL_DISCONNECTED:
        printf("Device Disconnected: \r\n");
        break;

    default:
        break;
    }
}

static Retcode_T CreateServiceCallback(void)
{
    BidirectionalService_Init(BleDataReceivedCallBack, BleDataSentCallback);
    SensorServices_Init(NULL, NULL);
    BidirectionalService_Register();
    SensorServices_Register();
    return RETCODE_OK;
}

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();
}
PreviousImplementationNextXDK Live

Last updated 5 years ago

Was this helpful?