ZLL网关程序分析
主机接口
zllSocCmd.h(ZLL的Socket主机接口)
此模块包含ZLL的Socket主机接口API。其包含的函数方法在zllSocCmd.c中实现
ZLL Soc Types
定义了描述设备的数据结构以及CC253x传送来消息时主机应用程序注册的回调函数
// Endpoint information record entry端点信息记录条目
typedef struct 描述设备的数据结构
{
uint8_t IEEEAddr[8]; IEEE地址
uint16_t nwkAddr; // Network address 网络地址
uint8_t endpoint; // Endpoint identifier 端点标识符
uint16_t profileID; // Profile identifier 配置文件标识符
uint16_t deviceID; // Device identifier 设备ID
uint8_t version; // Version
char* deviceName; 设备名称
uint8_t status; 状态(以供将来使用)
} epInfo_t
主机应用程序注册的回调函数如下:
typedef uint8_t (*zllSocTlIndicationCb_t)(epInfo_t *epInfo);
typedef uint8_t (*zllNewDevIndicationCb_t)(epInfo_t *epInfo);
typedef uint8_t (*zllSocZclGetStateCb_t)(uint8_t state, uint16_t nwkAddr, uint8_t endpoint);
typedef uint8_t (*zllSocZclGetLevelCb_t)(uint8_t level, uint16_t nwkAddr, uint8_t endpoint);
typedef uint8_t (*zllSocZclGetHueCb_t)(uint8_t hue, uint16_t nwkAddr, uint8_t endpoint);
typedef uint8_t (*zllSocZclGetSatCb_t)(uint8_t sat, uint16_t nwkAddr, uint8_t endpoint);
typedef struct
{
zllSocTlIndicationCb_t pfnTlIndicationCb;
// TouchLink Indication callback触摸指示回调(用于ZLL控制的桥梁,而不是关于HA照明网关)
zllNewDevIndicationCb_t pfnNewDevIndicationCb;
// New device Indication callback 新设备指示回调
zllSocZclGetStateCb_t pfnZclGetStateCb;
// ZCL response callback for get State 状态响应回调
zllSocZclGetLevelCb_t pfnZclGetLevelCb;
// ZCL response callback for get Level 亮度响应回调
zllSocZclGetHueCb_t pfnZclGetHueCb;
// ZCL response callback for get Hue 色调响应回调
zllSocZclGetSatCb_t pfnZclGetSatCb;
// ZCL response callback for get Sat 饱和度响应回调
} zllSocCallbacks_t;
#define Z_EXTADDR_LEN 8
ZCL消息寻址模式
typedef enum
{
afAddrNotPresent = 0,
afAddrGroup = 1, 组播地址
afAddr16Bit = 2,
afAddr64Bit = 3,
afAddrBroadcast = 15
} afAddrMode_t;
/********************************************************************/
ZLL Soc API
configuration API's
int32_t zllSocOpen( char *devicePath );
功能:打开到CC253x的串口
参数:devicePath - path to the UART device 到设备的路径,(端口号)
返回:serialPortFd 打开成功则返回串口文件描述符
void zllSocRegisterCallbacks( zllSocCallbacks_t zllSocCallbacks);
功能:回调注册函数
参数:回调函数指针
void zllSocClose( void );
功能:关闭到CC253x的串口
void zllSocProcessRpc (void); //read and process the RPC from the ZLL controller
当有来自于CC253x的数据时,必须调用消息处理函数进行来数据读取和解析。
ZLL API's
void zllSocTouchLink(void); 发送 touchLink 命令到CC253x.
void zllSocResetToFn(void);
发送恢复出厂(factory new)设置命令给CC253x.
Linux 网关文档中给出的注释为:Resets the Gateway to its Factory New state.
void zllSocSendResetToFn(void);
Send the reset to factory new command to a ZLL device
Linux 网关文档中给出的注释为Reserved for use with ZLL Controller
void zllSocOpenNwk(void); Send the open network command to a ZLL device
Opens the network for 60s for new devices to join//.打开网络60秒让新设备加进来
ZCL Set API's
void zllSocSetState(uint8_t state, uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
功能:发送on/off 命令给一个 ZLL light
参数 state - 0: Off, 1: On.
dstAddr - Nwk Addr or Group ID of the Light(s) to be controled.被控制灯的网络地址或组ID
endpoint - endpoint of the Light. 端点ID
addrMode - Unicast or Group cast. 单播或组播
void zllSocSetLevel(uint8_t level, uint16_t time, uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
功能:Send the level command to a ZLL light.发送亮度命令给一个ZLL灯
参数:level - 0-128 = 0-100%
dstAddr - Nwk Addr or Group ID of the Light(s) to be controled.被控制灯的网络地址或组ID
endpoint - endpoint of the Light. 端点ID
addrMode - Unicast or Group cast. 单播或组播
void zllSocSetHue(uint8_t hue, uint16_t time, uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
功能:Send the hue command to a ZLL light. 发送“色调”命令到一个ZLL灯
参数:hue - 0-128 represent the 360Deg hue color wheel : 0=red, 42=blue, 85=green
dstAddr - Nwk Addr or Group ID of the Light(s) to be controled.被控制灯的网络地址或组ID
endpoint - endpoint of the Light. 端点ID
addrMode - Unicast or Group cast. 单播或组播
void zllSocSetSat(uint8_t sat, uint16_t time, uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
功能:Send the satuartion command to a ZLL light.发送饱和度命令到一个ZLL灯
参数:sat - 0-128 : 0=white, 128: fully saturated color
dstAddr - Nwk Addr or Group ID of the Light(s) to be controled.被控制灯的网络地址或组ID
endpoint - endpoint of the Light. 端点ID
addrMode - Unicast or Group cast. 单播或组播
void zllSocSetHueSat(uint8_t hue, uint8_t sat, uint16_t time, uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
功能:Send the hue and satuartion command to a ZLL light.发送“色调”和“饱和度”命令到一个ZLL灯
参数:hue - 0-128 represent the 360Deg hue color wheel : 0=red, 42=blue, 85=green
sat - 0-128 : 0=white, 128: fully saturated color
dstAddr - Nwk Addr or Group ID of the Light(s) to be controled.被控制灯的网络地址或组ID
endpoint - endpoint of the Light. 端点ID
addrMode - Unicast or Group cast. 单播或组播
void zllSocAddGroup(uint16_t groupId, uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
* @brief Add Group.
*
* @param groupId - Group ID of the Scene.
* @param dstAddr - Nwk Addr or Group ID of the Light(s) to be controled.
* @param endpoint - endpoint of the Light.
* @param addrMode - Unicast or Group cast.
void zllSocStoreScene(uint16_t groupId, uint8_t sceneId, uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
@brief Store Scene.
* @param groupId - Group ID of the Scene.
* @param sceneId - Scene ID of the Scene.
* @param dstAddr - Nwk Addr or Group ID of the Light(s) to be controled.
* @param endpoint - endpoint of the Light.
* @param addrMode - Unicast or Group cast.
void zllSocRecallScene(uint16_t groupId, uint8_t sceneId, uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
@brief Recall Scene.
*
* @param groupId - Group ID of the Scene.
* @param sceneId - Scene ID of the Scene.
* @param dstAddr - Nwk Addr or Group ID of the Light(s) to be controled.
* @param endpoint - endpoint of the Light.
* @param addrMode - Unicast or Group cast.
void zllSocBind(uint16_t srcNwkAddr, uint8_t srcEndpoint, uint8_t srcIEEE[8], uint8_t dstEndpoint, uint8_t dstIEEE[8], uint16_t clusterID);
ZCL Get API's
void zllSocGetState(uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
功能:Send the get state command to a ZLL light.发送“获取状态”命令到a ZLL light
参数:dstAddr - Nwk Addr or Group ID of the Light(s) to be sent the command.要接收命令的灯的网络地址或组ID
endpoint - endpoint of the Light. 端点ID
addrMode - Unicast or Group cast. 单播或组播
void zllSocGetLevel(uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
功能:Send the get level command to a ZLL light.发送“获取亮度”命令到a ZLL light
参数:dstAddr - Nwk Addr or Group ID of the Light(s) to be sent the command.要接收命令的灯的网络地址或组ID
endpoint - endpoint of the Light. 端点ID
addrMode - Unicast or Group cast. 单播或组播
void zllSocGetHue(uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
功能:Send the get hue command to a ZLL light.发送“获取色调”命令到a ZLL light
参数:dstAddr - Nwk Addr or Group ID of the Light(s) to be sent the command.要接收命令的灯的网络地址或组ID
endpoint - endpoint of the Light. 端点ID
addrMode - Unicast or Group cast. 单播或组播
void zllSocGetSat(uint16_t dstAddr, uint8_t endpoint, uint8_t addrMode);
功能:Send the get saturation command to a ZLL light发送“获取饱和度”命令到a ZLL light
参数:dstAddr - Nwk Addr or Group ID of the Light(s) to be sent the command.要接收命令的灯的网络地址或组ID
endpoint - endpoint of the Light. 端点ID
addrMode - Unicast or Group cast. 单播或组播
一些有用的宏定义及数据类型重定义
hal_types.h
Types (数据类型重新定义)
typedef signed char int8;
typedef unsigned char uint8;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef unsigned char bool;
typedef uint16 halDataAlign_t;
Standard Defines
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NULL
#define NULL 0
#endif
hal_defs.h
This file contains useful macros and data types 此文件包含有用的宏和数据类型
Macros(宏)
#ifndef BV //这是将某位置位的宏
#define BV(n) (1 << (n)) 00000001往左移移N位,如左移3位得00001000
#endif
#ifndef BF
#define BF(x,b,s) (((x) & (b)) >> (s)) x和b位与运算再右移s位
#endif
#ifndef MIN 取较小值
#define MIN(n,m) (((n) < (m)) ? (n) : (m))
#endif
#ifndef MAX 取较大值
#define MAX(n,m) (((n) < (m)) ? (m) : (n))
#endif
#ifndef ABS 取绝对值
#define ABS(n) (((n) < 0) ? -(n) : (n))
#endif
#define BREAK_UINT32( var, ByteNum ) \
(uint8_t)((uint32_t)(((var) >>((ByteNum) * 8)) & 0x00FF))
/* takes a byte out of a uint32_t : var - uint32_t, ByteNum - byte to take out (0 - 3) */
将一个32位无符号整数var的第ByteNum个字节取出来
#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \
((uint32_t)((uint32_t)((Byte0) & 0x00FF) \
+ ((uint32_t)((Byte1) & 0x00FF) << 8) \
+ ((uint32_t)((Byte2) & 0x00FF) << 16) \
+ ((uint32_t)((Byte3) & 0x00FF) << 24)))
//将四个字节组合成一个32位无符号整数,Byte3为高位字节
#define BUILD_UINT16(loByte, hiByte) \
((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))
//将两个字节组合成一个16位无符号整数,hiByte为高位字节
#define HI_UINT16(a) (((a) >> 8) & 0xFF) //提取16为无符号整数的高8位
#define LO_UINT16(a) ((a) & 0xFF) //提取16为无符号整数的低8位
#define BUILD_UINT8(hiByte, loByte) \
((uint8_t)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))
//将hiByte, loByte组合成一个8位无符号整数,hiByte在高位
#define HI_UINT8(a) (((a) >> 4) & 0x0F) //提取8为无符号整数a的高4位
#define LO_UINT8(a) ((a) & 0x0F) //提86为无符号整数的a的低4位
#ifndef GET_BIT
#define GET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] & BV((IDX) % 8)) ? TRUE : FALSE)
#endif
#ifndef SET_BIT
#define SET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] |= BV((IDX) % 8)))
#endif
#ifndef CLR_BIT
#define CLR_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] &= (BV((IDX) % 8) ^ 0xFF)))
#endif
#define st(x) do { x } while (__LINE__ == -1)
// __LINE__ 是个宏,它代表当前代码在源文件的行号,它是大于0的,所以__LINE__ == -1 等同于0
这个宏供其他宏使用以形成一个完全有效的C语句。*如果没有,if / else条件语句可能意外的行为, 无关键作用,无需特别注意
*该宏用于定义不同的编译器/汇编ASM NOP指令
#ifdef ccs
#define ASM_NOP asm(" NOP")
#elif defined rvmdk
#define ASM_NOP __nop()
#else
#define ASM_NOP asm("NOP")
#endif
远程过程调用
socket_server.h
Description: Socket Remote Procedure Call Interface - sample device application.
套接字远程过程调用接口 - 示例设备应用程序。
TYPEDEFS 类型定义
typedef void (*socketServerCb_t)( int clientFd );
参数:客户端文件描述符
INCLUDES 库文件
#include "hal_types.h"
CONSTANTS 常数
#define MAX_CLIENTS 50
定义的函数方法
int32 socketSeverInit( uint32 port );
功能:initialises the server. 初始化服务
参数:port 端口号
返回:return Status 为0时成功
* serverSocketConfig - initialises the server.
*/
int32 serverSocketConfig(socketServerCb_t rxCb, socketServerCb_t connectCb);
/*
* getClientFds - get clients fd's.
*/
void socketSeverGetClientFds(int *fds, int maxFds);
/*
* getClientFds - get clients fd's.
*/
uint32 socketSeverGetNumClients(void);
/*
* socketSeverPoll - services the Socket events.
*/
void socketSeverPoll(int clinetFd, int revent);
/*
* socketSeverSendAllclients - Send a buffer to all clients.
*/
int32 socketSeverSendAllclients(uint8* buf, uint32 len);
/*
* socketSeverSend - Send a buffer to a clients.
*/
int32 socketSeverSend(uint8* buf, uint32 len, int32 fdClient);
/*
* socketSeverClose - Closes the client connections.
*/
void socketSeverClose(void);
interface_srpcserver.h
define the outgoing RPCS command ID's
从网关发送的命令
#define RPCS_NEW_ZLL_DEVICE 0x0001
#define RPCS_DEV_ANNCE 0x0002
#define RPCS_SIMPLE_DESC 0x0003
#define RPCS_TEMP_READING 0x0004
#define RPCS_POWER_READING 0x0005
#define RPCS_PING 0x0006
#define RPCS_GET_DEV_STATE_RSP 0x0007
#define RPCS_GET_DEV_LEVEL_RSP 0x0008
#define RPCS_GET_DEV_HUE_RSP 0x0009
#define RPCS_GET_DEV_SAT_RSP 0x000a
#define RPCS_ADD_GROUP_RSP 0x000b
#define RPCS_GET_GROUP_RSP 0x000c
#define RPCS_ADD_SCENE_RSP 0x000d
#define RPCS_GET_SCENE_RSP 0x000e
define incoming RPCS command ID's
发送给网关的命令
#define RPCS_CLOSE 0x80;
#define RPCS_GET_DEVICES 0x81;
#define RPCS_SET_DEV_STATE 0x82;
#define RPCS_SET_DEV_LEVEL 0x83;
#define RPCS_SET_DEV_COLOR 0x84;
#define RPCS_GET_DEV_STATE 0x85;
#define RPCS_GET_DEV_LEVEL 0x86;
#define RPCS_GET_DEV_HUE 0x87;
#define RPCS_GET_DEV_SAT 0x88;
#define RPCS_BIND_DEVICES 0x89;
#define RPCS_GET_THERM_READING 0x8a;
#define RPCS_GET_POWER_READING 0x8b;
#define RPCS_DISCOVER_DEVICES 0x8c;
#define RPCS_SEND_ZCL 0x8d;
#define RPCS_GET_GROUPS 0x8e;
#define RPCS_ADD_GROUP 0x8f;
#define RPCS_GET_SCENES 0x90;
#define RPCS_STORE_SCENE 0x91;
#define RPCS_RECALL_SCENE 0x92;
#define RPCS_IDENTIFY_DEVICE 0x93;
#define RPCS_CHANGE_DEVICE_NAME 0x94;
#define RPCS_REMOVE_DEVICE 0x95;
#define SRPC_FUNC_ID 0
#define SRPC_MSG_LEN 1
#define SRPC_TCP_PORT 0x2be3
#define CLOSE_AUTH_NUM 0x2536
定义的结构体
typedef struct
{
union
{
uint16_t shortAddr;
uint8_t extAddr[Z_EXTADDR_LEN];
} addr;
afAddrMode_t addrMode;
uint8_t endPoint;
uint16_t panId; // used for the INTER_PAN feature
} afAddrType_t;
RPSC ZLL Interface function
void SRPC_Init(void);
功能:initialises the RPC interface and waits for a client to connect.
初始化远程过程调用接口并等待一个客户端来连接
uint8_t RSPC_SendEpInfo(epInfo_t *epInfo);
功能:This function exposes an interface to allow an upper layer to start send an ep indo to all devices.此函数提供一个了接口,允许上层开始将一个设备的信息发送给所有设备。
参数:epInfo_t *epInfo pointer to the epInfo to be sent
void RPCS_ZLL_CallBack_getStateRsp(uint8_t state, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd)
功能:Sends the get State Rsp to the client that sent a get state
void RPCS_ZLL_CallBack_getLevelRsp(uint8_t level, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd);
功能:Sends the get Level Rsp to the client that sent a get level
void RPCS_ZLL_CallBack_getHueRsp(uint8_t hue, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd);
功能:Sends the get Hue Rsp to the client that sent a get hue
void RPCS_ZLL_CallBack_getSatRsp(uint8_t sat, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd);
功能:Sends the get Sat Rsp to the client that sent a get sat
下面两个函数在程序中并未实现
void RPCS_ZLL_CallBack_getTempRsp(uint16_t temp, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd);
void RPCS_ZLL_CallBack_getPowerRsp(uint32_t power, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd);
终端节点处理
interface_devicelist.h
device states
#define DEVLIST_STATE_NOT_ACTIVE 0 //设备未激活
#define DEVLIST_STATE_ACTIVE 1 //设备已激活
定义的函数方法
void devListAddDevice( epInfo_t *epInfo);
功能:创建一个设备并添加一个记录到列表
参数:epInfo_t *epInfo 描述设备信息的结构体,在zllSocCmd.h中已定义
void devListRemoveDevice( uint16_t nwkAddr, uint8_t endpoint );
功能:从列表中删除一个设备记录
参数:uint16_t nwkAddr, 要移除设备的网络地址
uint8_t endpoint 要移除设备的端点ID
uint32_t devListNumDevices( void );
功能:获取列表中的设备数
epInfo_t* devListGetNextDev( uint16_t nwkAddr, uint8_t endpoint );
功能:返回列表中的下一个设备信息
参数:uint16_t nwkAddr, if 0xFFFF it will return head of the list
uint8_t endpoint
返回:epInfo, return next epInfo from nwkAddr and ep supplied or NULL if at end of the list 返回参数表示的设备的下一个设备,如果参数表示的设备位于列表的结尾则返回空值
void devListChangeDeviceName( uint16_t devNwkAddr, uint8_t devEndpoint, char *deviceNameStr);
功能:改变设备名称
参数:uint16_t devNwkAddr uint8_t devEndpoint char *deviceNameStr(设备名称)
void devListRestorDevices( void );
功能:restore device list from file. 从文件中恢复设备列表(devicelistfile.dat)
interface_grouplist.h
定义的结构体
typedef struct
{
uint16_t groupId; 组ID
char *groupNameStr; 组名称字符串
}groupListItem_t;
定义的函数方法
uint16_t groupListAddGroup( char *groupNameStr );
功能:创建一个“组”并添加一个记录到列表
参数:char *groupNameStr 组名称字符串
返回:groupId 组ID
void groupListAddDeviceToGroup( char *groupNameStr, uint16_t nwkAddr );
功能:添加一个设备到一个组
参数:char *groupNameStr 组名称
uint16_t nwkAddr 网络地址
groupListItem_t* groupListGetNextGroup( char *groupNameStr );
功能:返回列表中的下一个组
参数:char *groupNameStr if NULL it will return head of the list
返回:groupListItem_t, return next group from groupNameStr supplied or NULL if at end of the list
void groupListRestorGroups( void );
功能:从grouplistfile.dat文件恢复组列表
interface_scenelist.h
定义的结构体
typedef struct
{
uint16_t groupId; 组ID
uint8_t sceneId; 场景ID
char *sceneNameStr; 场景名称字符串
}sceneListItem_t;
定义的函数方法
uint8_t sceneListAddScene( char *sceneNameStr, uint16_t groupId );
功能:创建一个“场景”并添加一个记录到列表
参数:char *sceneNameStr, 场景名称字符串
uint16_t groupId 组ID
返回:sceneId 场景ID
uint8_t sceneListGetSceneId( char *sceneNameStr, uint16_t groupId );
功能:获取“场景”ID
参数:char *sceneNameStr, 场景
uint16_t groupId 组ID
返回:sceneId 返回场景ID
sceneListItem_t* sceneListGetNextScene( char *sceneNameStr, uint16_t groupId );
功能:返回列表中下一个场景信息
参数:char *sceneNameStr, if NULL it will return head of the list
uint16_t groupId group that the scene is apart of, ignored if sceneStr is NULL.
void sceneListRestorScenes( void );
功能:从scenelistfile.dat文件恢复场景列表
附件列表