2.4. 消息格式

消息格式使用 Google's Protocol Buffers 来编解码。 网关端可以选择任意语言版本,手环使用的是 Nanopb 实现。

2.4.1. 数据结构定义文件

数据格式定义如下(仅有此一个文件):

syntax = "proto3";

/*
 *******************************************************************************
 * Status
 *******************************************************************************
 */

message Status {
  message Request {}
  message Response {
    // 手环的本地时间戳,如果和网关的差别过大,网关可以通过设置更新手环的时间戳。
    // 手环第一次上电时的时间戳从0开始计时
    uint32 localTimestampInSecond = 1;
  }
}

/*
 *******************************************************************************
 * Settings
 *******************************************************************************
 */

message AdvertisementSettings {
  // 以分钟为单位的最小广播时间阈值
  uint32 miniReportThresholdInMinute = 1;
  // 以字节为单位的最小广播数据量阈值
  uint32 miniReportThresholdInByte = 2;
}

message ScanSettings {
  // BLE 扫描周期。合法范围[4,16384],单位625us。
  uint32 bleInterval = 1;
  // 每个bleInterval周期内的持续扫描时间。合法范围[4,16384],单位625us,必须小于等于bleInterval。
  uint32 bleWindow = 2;
  // 一次 BLE 扫描的持续时间。单位10ms。必须大于0。
  uint32 bleDuration = 3;
  // 多久触发一次扫描Beacon。
  uint32 intervalInSecond = 4;
  // 待扫描的Beacon名字
  string beaconName = 5;
}

message SleepCommand {
  // 睡眠或唤醒事件的时间点
  uint32 timeStampInSecond = 1;
  // 1: 睡眠事件
  // 0: 唤醒事件
  bool sleep = 2;
}

message SleepSettings {
  // 1: 添加睡眠唤醒事件
  // 0: 覆盖之前设置的睡眠唤醒事件
  bool append = 1;
  //  一系列的睡眠唤醒命令。建议一次发送不要超过10条。
  repeated  SleepCommand sleepCommands = 2;
}

enum SettingErrorCode {
  SUCCESS = 0;
  INVALID_PARAMETER = 1;
}

message Settings {
  message Request {
    optional uint32 newTimestamp = 1;
    optional AdvertisementSettings advSettings = 2;
    optional ScanSettings scanSettings = 3;
    optional SleepSettings sleepSettings = 4;
  }
  message Response {
    SettingErrorCode errorCode = 1;
  }
}

/*
 *******************************************************************************
 * Report
 *******************************************************************************
 */

enum ReportType{
  HEART = 0;
  ACC = 1;
  BEACON = 2;
}

// 此命令格式待定,可能会修改。
message Report {
  message Request {
    uint32 startTimestamp = 1;
    ReportType reportType = 2;
  }
  message Response {
    repeated bytes data = 1;
  }
}

/*
 *******************************************************************************
 * Request
 *******************************************************************************
 */

// 网关发送给手环的数据格式
message CougarRequest {
  oneof message {
    Status.Request status = 1;
    Settings.Request settings = 2;
    Report.Request report = 3;
  }
}

/*
 *******************************************************************************
 * Response
 *******************************************************************************
 */

// 手环返回的数据格式
message CougarResponse {
  oneof message {
    Status.Response status = 1;
    Settings.Response settings = 2;
    Report.Response report = 3;
  }
}

2.4.2. 数据封包

手环会修改MTU到最大值,而且保证一个数据包仅包含一个完整的Response。

2.4.3. 最外层格式

最外层的数据结构有两个

  • message CougarRequest {} 网关端发送

  • message CougarResponse {} 手环端返回

2.4.4. 内层格式

对于 CougarRequestCougarResponse ,均有三种内层格式。

  • 状态格式 (Status),定义在 message Status {} 中。

  • 设置格式 (Settings),定义在 message Settings {} 中。

  • 报告格式 (Report), 定义在 message Report {} 中。