1

我正在使用示例 CustomGATTSvc 代码来熟悉 Movesense pod 上的 GATT 接口,但在尝试向代码中添加更多服务时遇到了问题。我的项目最终需要具备以下能力:

  1. 获取和设置 pod 上的 RTC
  2. 使用 Datalogger 和 Logbook 存储和检索加速度计数据。

所有这些都必须使用 GATT 接口来实现,因为我们希望在 Cordova 中开发移动应用程序,据我所知,它不支持 Movesense 库。

作为第一步,我尝试向示例代码中已经存在的 Health Thermometer 服务添加第二个服务。我希望实现当前时间服务(与当前时间服务相关的所有内容都包含在 #define CURRENT_TIME_SVC 中) -

#define THERMOMETER_SERV_UUID           0x1809  // Health Thermometer
#define CURRENT_TIME_SERV_UUID          0x1805  // Current Time Service

const uint16_t measCharUUID16 = 0x2A1C;
const uint16_t intervalCharUUID16 = 0x2A21;
const uint16_t healthThermometerSvcUUID16 = THERMOMETER_SERV_UUID; // Health Temperature probe

#ifdef CURRENT_TIME_SVC
const uint16_t timeCharUUID16 = 0x2A2C; //Random UUID for Current Time
const uint16_t timeSvcUUID16 = CURRENT_TIME_SERV_UUID;  //Current Time
#endif

在 configGattSvc() 函数中,我将服务和特征配置如下:

  WB_RES::GattSvc customGattThermometerSvc;
  WB_RES::GattChar characteristics[2];
  WB_RES::GattChar &measChar = characteristics[0];
  WB_RES::GattChar &intervalChar = characteristics[1];
  //const uint16_t healthThermometerSvcUUID16 = 0x1809;

#ifdef CURRENT_TIME_SVC
  WB_RES::GattSvc customGattTimeSvc;
  WB_RES::GattChar characteristicsTime[1];
  WB_RES::GattChar &timeChar = characteristicsTime[0];
  //const uint16_t timeSvcUUID16 = 0x1805;
#endif

  // Define the CMD characteristics
  WB_RES::GattProperty measCharProp = WB_RES::GattProperty::INDICATE;
  WB_RES::GattProperty intervalCharProps[2] = {WB_RES::GattProperty::READ, WB_RES::GattProperty::WRITE};
  WB_RES::GattProperty timeCharProps[3] = {WB_RES::GattProperty::READ, WB_RES::GattProperty::WRITE, WB_RES::GattProperty::NOTIFY};

  measChar.props = whiteboard::MakeArray<WB_RES::GattProperty>( &measCharProp, 1);
  measChar.uuid = whiteboard::MakeArray<uint8_t>( reinterpret_cast<const uint8_t*>(&measCharUUID16), 2);

  intervalChar.props = whiteboard::MakeArray<WB_RES::GattProperty>( intervalCharProps, 2);
  intervalChar.uuid = whiteboard::MakeArray<uint8_t>( reinterpret_cast<const uint8_t*>(&intervalCharUUID16), 2);
  intervalChar.initial_value = whiteboard::MakeArray<uint8_t>( reinterpret_cast<const uint8_t*>(&mMeasIntervalSecs), 2);

#ifdef CURRENT_TIME_SVC
  timeChar.props = whiteboard::MakeArray<WB_RES::GattProperty>( timeCharProps, 3);
  timeChar.uuid = whiteboard::MakeArray<uint8_t>( reinterpret_cast<const uint8_t*>(&timeCharUUID16), 2);
  timeChar.initial_value = whiteboard::MakeArray<uint8_t>( reinterpret_cast<const uint8_t*>(&mTimeSecs), 2);
#endif

  // Combine chars to service
  customGattThermometerSvc.uuid = whiteboard::MakeArray<uint8_t>( reinterpret_cast<const uint8_t*>(&healthThermometerSvcUUID16), 2);
  customGattThermometerSvc.chars = whiteboard::MakeArray<WB_RES::GattChar>(characteristics, 2);

  // Create custom service
  asyncPost(WB_RES::LOCAL::COMM_BLE_GATTSVC(), AsyncRequestOptions::Empty, customGattThermometerSvc);

#ifdef CURRENT_TIME_SVC
  // Combine Time chars to service
  customGattTimeSvc.uuid = whiteboard::MakeArray<uint8_t>( reinterpret_cast<const uint8_t*>(&timeSvcUUID16), 2);
  customGattTimeSvc.chars = whiteboard::MakeArray<WB_RES::GattChar>(characteristicsTime, 1);
  // Create custom service
  asyncPost(WB_RES::LOCAL::COMM_BLE_GATTSVC(), AsyncRequestOptions::Empty, customGattTimeSvc);
#endif

在 onGetResult 我扩展了代码以合并当前时间服务和特征订阅,如下所示:

void CustomGATTSvcClient::onGetResult(whiteboard::RequestId requestId, whiteboard::ResourceId resourceId, whiteboard::Result resultCode, const whiteboard::Value& rResultData)
{
  DEBUGLOG("CustomGATTSvcClient::onGetResult");
  switch(resourceId.localResourceId)
  {
    case WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE::LID:
    {
      const WB_RES::GattSvc &svc = rResultData.convertTo<const WB_RES::GattSvc &>();
      for (size_t i=0; i<svc.chars.size(); i++) {
        const WB_RES::GattChar &c = svc.chars[i];
        uint16_t uuid16 = *reinterpret_cast<const uint16_t*>(&(c.uuid[0]));

        if(uuid16 == measCharUUID16)
        mMeasCharHandle = c.handle.hasValue() ? c.handle.getValue() : 0;
        else if(uuid16 == intervalCharUUID16)
        mIntervalCharHandle = c.handle.hasValue() ? c.handle.getValue() : 0;
        #ifdef CURRENT_TIME_SVC
        else if(uuid16 == timeCharUUID16)
        mTimeCharHandle = c.handle.hasValue() ? c.handle.getValue() : 0;
        #endif
      }

      if (!mIntervalCharHandle || !mMeasCharHandle)
      {
        DEBUGLOG("ERROR: Not all chars were configured!");
        return;
      }
#ifdef CURRENT_TIME_SVC
      if (!mTimeCharHandle)
      {
        DEBUGLOG("ERROR: Not all chars were configured!");
        return;
      }
#endif
      char pathBuffer[32]= {'\0'};
      snprintf(pathBuffer, sizeof(pathBuffer), "/Comm/Ble/GattSvc/%d/%d", mTemperatureSvcHandle, mIntervalCharHandle);
      getResource(pathBuffer, mIntervalCharResource);
      snprintf(pathBuffer, sizeof(pathBuffer), "/Comm/Ble/GattSvc/%d/%d", mTemperatureSvcHandle, mMeasCharHandle);
      getResource(pathBuffer, mMeasCharResource);
#ifdef CURRENT_TIME_SVC
      snprintf(pathBuffer, sizeof(pathBuffer), "/Comm/Ble/GattSvc/%d/%d", mTimeSvcHandle, mTimeCharHandle);
      getResource(pathBuffer, mTimeCharResource);
#endif

      // Subscribe to listen to intervalChar notifications (someone writes new value to intervalChar)
      asyncSubscribe(mIntervalCharResource, AsyncRequestOptions::Empty);
      // Subscribe to listen to measChar notifications (someone enables/disables the INDICATE characteristic)
      asyncSubscribe(mMeasCharResource, AsyncRequestOptions::Empty);
#ifdef CURRENT_TIME_SVC
      // Subscribe to listen to timeChar notifications (someone writes new value to timeChar)
      asyncSubscribe(mTimeCharResource, AsyncRequestOptions::Empty);
#endif
    }
    break;

    case WB_RES::LOCAL::MEAS_TEMP::LID:
    {
      // Temperature result or error
      if (resultCode == whiteboard::HTTP_CODE_OK)
      {
        WB_RES::TemperatureValue value = rResultData.convertTo<WB_RES::TemperatureValue>();
        float temperature = value.measurement;

        // Convert K to C
        temperature -= 273.15;

        // Return data
        //uint8_t buffer[5]; // 1 byte or flags, 4 for FLOAT "in Celsius" value
        uint8_t buffer[5];
        buffer[0]=0;
        // convert normal float to IEEE-11073 "medical" FLOAT type into buffer
        floatToFLOAT(temperature, &buffer[1]);

        // Write the result to measChar. This results INDICATE to be triggered in GATT service
        WB_RES::Characteristic newMeasCharValue;
        newMeasCharValue.bytes = whiteboard::MakeArray<uint8_t>(buffer, sizeof(buffer));
        asyncPut(WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE_CHARHANDLE(), AsyncRequestOptions::Empty, mTemperatureSvcHandle,
        mMeasCharHandle, newMeasCharValue);
      }
    }
    break;
#ifdef CURRENT_TIME_SVC
    case WB_RES::LOCAL::TIME::LID:
    {
      // Return with the RTC Time
      if (resultCode == whiteboard::HTTP_CODE_OK)
      {
        WB_RES::DetailedTime value = rResultData.convertTo<WB_RES::DetailedTime>();
        int64 tm = value.utcTime;
        uint8_t buffer[2];
        // Could have an endian issue here, will have to check once connection works
        buffer[0] - (tm&0xFF00)>>8;
        buffer[1] = tm &0xFF;
        // Here we need to get the current time and then Put it back to the device connected

        WB_RES::Characteristic newTimeCharValue;
        newTimeCharValue.bytes = whiteboard::MakeArray<uint8_t>(buffer, sizeof(buffer));
        asyncPut(WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE_CHARHANDLE(), AsyncRequestOptions::Empty, mTimeSvcHandle,
        mTimeCharHandle, newTimeCharValue);
      }
    }
    break;
#endif
  }
}

我在 onGetResult 中添加了时间本地资源,因为我仍然不确定如何从 pod 中“获取”RTC 时间。如何访问 /Time 资源?

onNotify 修改如下:

void CustomGATTSvcClient::onNotify(whiteboard::ResourceId resourceId, const whiteboard::Value& value, const whiteboard::ParameterList& rParameters)
{
  switch(resourceId.localResourceId)
  {
    case WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE_CHARHANDLE::LID:
    {
      WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE_CHARHANDLE::SUBSCRIBE::ParameterListRef parameterRef(rParameters);
      if (parameterRef.getCharHandle() == mIntervalCharHandle)
      {
        const WB_RES::Characteristic &charValue = value.convertTo<const WB_RES::Characteristic &>();
        uint16_t interval = *reinterpret_cast<const uint16_t*>(&charValue.bytes[0]);
        DEBUGLOG(": mMeasCharResource: len: %d, new interval: %d", charValue.bytes.size(), interval);
        // Update the interval
        if (interval >= 1 && interval <= 65535)
        mMeasIntervalSecs = interval;
        // restart timer if exists
        if (mMeasurementTimer != whiteboard::ID_INVALID_TIMER) {
          stopTimer(mMeasurementTimer);
          mMeasurementTimer = startTimer(mMeasIntervalSecs*1000, true);
        }
      }
      else if (parameterRef.getCharHandle() == mMeasCharHandle)
      {
        const WB_RES::Characteristic &charValue = value.convertTo<const WB_RES::Characteristic &>();
        bool bNotificationsEnabled = charValue.notifications.hasValue() ? charValue.notifications.getValue() : false;
        DEBUGLOG(": mMeasCharHandle. bNotificationsEnabled: %d", bNotificationsEnabled);
        // Start or stop the timer
        if (mMeasurementTimer != whiteboard::ID_INVALID_TIMER)
        {
          stopTimer(mMeasurementTimer);
          mMeasurementTimer = whiteboard::ID_INVALID_TIMER;
        }
        if (bNotificationsEnabled)
        mMeasurementTimer = startTimer(mMeasIntervalSecs*1000, true);
      }
#ifdef CURRENT_TIME_SVC
      else if (parameterRef.getCharHandle() == mTimeCharHandle)
      {
        // Received Time information!
        const WB_RES::Characteristic &charValue = value.convertTo<const WB_RES::Characteristic &>();
        uint16_t tm = *reinterpret_cast<const uint16_t*>(&charValue.bytes[0]);
        DEBUGLOG(": mMeasCharResource: len: %d, new interval: %d", charValue.bytes.size(), tm);
        // Update the interval
        mTimeSecs = tm;
      }
#endif
    }
    break;
  }
}

到目前为止,我认为代码应该是正确的并且可以工作,但是我对 onPostResult 的最后一段代码有疑问:

void CustomGATTSvcClient::onPostResult(whiteboard::RequestId requestId, whiteboard::ResourceId resourceId, whiteboard::Result resultCode, const whiteboard::Value& rResultData)
{
  DEBUGLOG("CustomGATTSvcClient::onPostResult: %d", resultCode);
  if (resultCode == whiteboard::HTTP_CODE_CREATED) {
#if 1
  // This is the code that I propose using when having more than one service but it doesn't seem to work.

    const WB_RES::GattSvc &svc = rResultData.convertTo<const WB_RES::GattSvc &>();
    uint16_t uuid16 = *reinterpret_cast<const uint16_t*>(&(svc.uuid[0]));

    if(uuid16 == healthThermometerSvcUUID16) {
      mTemperatureSvcHandle = (int32_t)rResultData.convertTo<uint16_t>();
      DEBUGLOG("Custom Gatt service was created. handle: %d", mTemperatureSvcHandle);
      asyncGet(WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE(), AsyncRequestOptions::Empty, mTemperatureSvcHandle);
    }
    #ifdef CURRENT_TIME_SVC
    else if(uuid16 == timeSvcUUID16) {
      mTimeSvcHandle = (int32_t)rResultData.convertTo<uint16_t>();
      DEBUGLOG("Custom Gatt service was created. handle: %d", mTimeSvcHandle);
      asyncGet(WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE(), AsyncRequestOptions::Empty, mTimeSvcHandle);
    }
    #endif
#else
    // This is the code that does work with a single Service but is doesn't work as soon as I add a second service.
    // Custom Gatt service was created
    mTemperatureSvcHandle = (int32_t)rResultData.convertTo<uint16_t>();
    DEBUGLOG("Custom Gatt service was created. handle: %d", mTemperatureSvcHandle);

    // Request more info about created svc so we get the char handles
    asyncGet(WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE(), AsyncRequestOptions::Empty, mTemperatureSvcHandle);
#endif
  }
}

你会看到我在这段代码中有一个#if 1 .. (code1) .. #else .. (code2) .. #endif。第一个#if 1 部分(code1)中的代码是我编写的用于与代码创建的两个 GATT 服务一起使用的代码。我觉得既然有两个服务,就必须进行测试(使用服务 UUID)来确定正在处理哪个服务。#else (code2)之后的代码来自于刚刚使用 health temp 服务的原始示例代码。

当我使用(code1)编译时,一切都编译得很好,但我似乎无法订阅健康温度服务。当我切换到使用(code2)时,健康温度服务工作正常,我可以毫无问题地订阅它。

为了测试 GATT 接口,我使用了蓝牙低功耗浏览器 Bluetility。https://github.com/jnross/Bluetility

我的问题如下:

  1. 我所有的代码都编译得很好,但是一旦我使用(code1),我就无法订阅并让 Health Temp Service 工作。我究竟做错了什么? (code2)不能使用多个服务,因为它仅适用于 Health Temp 服务。
  2. 我已经开始实现当前时间服务,但在我修复上面的 Q1 之前,不确定如何实现代码来获取和设置当前时间。
  3. 一旦成功完成上述操作,我将开始弄清楚如何添加另一个服务来访问加速度计资源(“/Meas/Acc/13”),并使用数据记录器存储加速度计数据和日志以提取它在同一个 GATT 服务中。

如果任何人能够为我指明实现最终目标的正确道路,我将不胜感激。预先感谢。

4

1 回答 1

0

我认为您在响应处理中存在类型不匹配。

void CustomGATTSvcClient::onPostResult(...)
    {
      if (resultCode == whiteboard::HTTP_CODE_CREATED) {
      const WB_RES::GattSvc &svc = rResultData.convertTo<const WB_RES::GattSvc &>();

-> https://bitbucket.org/suunto/movesense-device-lib/src/master/MovesenseCoreLib/resources/movesense-api/comm/ble_gattsvc.yaml

使用代码 201(created) 响应类型将 /Comm/Ble/GattSvc POST 请求定义为 GattSvcHandle,它是整数而不是 struct WB_RES::GattSvc

因此,以下比较逻辑也失败了。

解决方案建议:您可以在 onPostResult 中尝试您的比较逻辑:

if(mTemperatureSvcHandle == 0) {
      mTemperatureSvcHandle = (int32_t)rResultData.convertTo<uint16_t>();
      asyncGet(WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE(), AsyncRequestOptions::Empty, mTemperatureSvcHandle);
    }
    else {
      mTimeSvcHandle = (int32_t)rResultData.convertTo<uint16_t>();
      asyncGet(WB_RES::LOCAL::COMM_BLE_GATTSVC_SVCHANDLE(), AsyncRequestOptions::Empty, mTimeSvcHandle);}

(此解决方案只是一种解决方法,用于测试这是否可以解决您同时运行两个服务的问题,因为这没有考虑到帖子是异步的,最后您可能需要一些更优雅的方式来识别与相关的服务后结果)

于 2018-10-16T11:35:18.063 回答