针对Simplicity Studio 4,EmberZNet协议栈Zigbee3.0设备入网流程
Platform:Simplicity Studio 4、EmberZNet SDK 6.4.1.0
【SPP】Content:
Zigbee3.0设备自定义ZCL数据包发送数据
1、借助zcl-cli.c文件定义的一些变量,构建ZCL数据buffer,在自己的c文件下引用以下几个变量
extern int8u appZclBuffer[EMBER_AF_MAXIMUM_SEND_PAYLOAD_LENGTH];
extern int16u appZclBufferLen;
extern int16u mfgSpecificId;
extern int8u disableDefaultResponse;
extern EmberApsFrame globalApsFrame;
2、使用zclBufferSetup函数填充appZclBuffer特殊数据区
void zclBufferSetup(uint8_t frameType, uint16_t clusterId, uint8_t commandId)
{
uint8_t index = 0;
emAfApsFrameClusterIdSetup(clusterId);
appZclBuffer[index++] = (frameType
| ZCL_FRAME_CONTROL_CLIENT_TO_SERVER
| (mfgSpecificId != EMBER_AF_NULL_MANUFACTURER_CODE
? ZCL_MANUFACTURER_SPECIFIC_MASK
: 0)
| (disableDefaultResponse
? ZCL_DISABLE_DEFAULT_RESPONSE_MASK
: 0));
if (mfgSpecificId != EMBER_AF_NULL_MANUFACTURER_CODE) {
appZclBuffer[index++] = (uint8_t)mfgSpecificId;
appZclBuffer[index++] = (uint8_t)(mfgSpecificId >> 8);
}
appZclBuffer[index++] = emberAfNextSequence();
appZclBuffer[index++] = commandId;
appZclBufferLen = index;
}
例如:(其中 ZCL_XXX_CLUSTER_ID与ZCL_XXX_COMMAND_ID为发送端的cluster与command)
zclBufferSetup(ZCL_CLUSTER_SPECIFIC_COMMAND
|ZCL_FRAME_CONTROL_CLIENT_TO_SERVER
|ZCL_DISABLE_DEFAULT_RESPONSE_MASK,
ZCL_XXX_CLUSTER_ID,
ZCL_XXX_COMMAND_ID);
3、使用zclBufferAddByte、zclBufferAddWord、zclBufferAddInt32...函数填充appZclBuffer自定义数据区
void zclBufferAddByte(uint8_t byte)
{
appZclBuffer[appZclBufferLen] = byte;
appZclBufferLen += 1;
}
void zclBufferAddWord(uint16_t word)
{
zclBufferAddByte(LOW_BYTE(word));
zclBufferAddByte(HIGH_BYTE(word));
}
void zclBufferAddInt32(uint32_t value)
{
uint8_t i;
for (i = 0; i < 4; i++) {
zclBufferAddByte(LOW_BYTE(value));
value = value >> 8;
}
}
void zclBufferAddBuffer(const uint8_t *buffer, uint8_t length)
{
MEMMOVE(appZclBuffer + appZclBufferLen, buffer, length);
appZclBufferLen += length;
}
// Made non-to remove warnings
void zclBufferAddString(const uint8_t *buffer)
{
zclBufferAddBuffer(buffer, emberAfStringLength(buffer) + 1);
}
4、使用emAfApsFrameEndpointSetup函数设置源与目的端点
void emAfApsFrameEndpointSetup(uint8_t srcEndpoint,
uint8_t dstEndpoint)
{
globalApsFrame.sourceEndpoint = (srcEndpoint == 0
? emberAfPrimaryEndpointForCurrentNetworkIndex()
: srcEndpoint);
globalApsFrame.destinationEndpoint = dstEndpoint;
}
5、使用组播、广播、单播三种方式其中一种将自定义的appZclBuffer数据发送出去
EmberStatus emberAfSendMulticast(EmberMulticastId multicastId,
EmberApsFrame *apsFrame,
uint16_t messageLength,
uint8_t *message)
{
return emberAfSendMulticastWithCallback(multicastId,
apsFrame,
messageLength,
message,
NULL);
}
EmberStatus emberAfSendBroadcast(EmberNodeId destination,
EmberApsFrame *apsFrame,
uint16_t messageLength,
uint8_t *message)
{
return emberAfSendBroadcastWithCallback(destination,
apsFrame,
messageLength,
message,
NULL);
}
EmberStatus emberAfSendUnicast(EmberOutgoingMessageType type,
uint16_t indexOrDestination,
EmberApsFrame *apsFrame,
uint16_t messageLength,
uint8_t *message)
{
return emberAfSendUnicastWithCallback(type,
indexOrDestination,
apsFrame,
messageLength,
message,
NULL);
}
6、应用实例(实现将0x55、0xABCD两个数据单播到协调器)
static EmberStatus zclBufferSendToCoor(void) //发送到协调器
{
emAfApsFrameEndpointSetup(1, 1);
return emberAfSendUnicast(EMBER_OUTGOING_DIRECT,
COORDINATER_NETWORK_ID,
&globalApsFrame,
appZclBufferLen,
appZclBuffer);
}
void UnicastFunction(int16u data)
{
mfgSpecificId = EMBER_AF_MANUFACTURER_CODE;
zclXDefaultRespCommand = TRUE;
zclBufferSetup(ZCL_CLUSTER_SPECIFIC_COMMAND,
ZCL_XXX_CLUSTER_ID,
ZCL_XXX_COMMAND_ID);
mfgSpecificId = EMBER_AF_NULL_MANUFACTURER_CODE;
zclBufferAddWord(data);
if (zclBufferSendToCoor() == EMBER_SUCCESS) //成功
{
}
}