Commit bd0a912a authored by 尹佳钦's avatar 尹佳钦

2020/8/10

parent e6d4291d
......@@ -293,6 +293,7 @@ APPLICATION_FILES= \
./jsonrpc/jsonrpc-c.c\
./jsonrpc/rpccJSON.c\
./yjq_ezsp.c\
./kk_test.c\
./rpc_api/src/rpc_common.c\
./rpc_api/src/rpc_onoff.c\
./rpc_api/src/rpc_global_cmd.c\
......
......@@ -424,3 +424,8 @@ int jrpc_deregister_procedure(struct jrpc_server *server, char *name) {
}
return 0;
}
......@@ -4,6 +4,7 @@
#include <stdio.h>
#include <pthread.h>
#include "../device-table/device-table.h"
#include "./jsonrpc/jsonrpc-c.h"
......@@ -16,6 +17,10 @@
#define MSG_INTERNAL_ERROR "Internal error"
extern pthread_mutex_t g_mutex_lock;
extern pthread_mutex_t report_mutex_lock;
typedef cJSON(*rpc_function)(jrpc_context * ctx, cJSON * params, cJSON *id);
void zclBufferSetup(uint8_t frameType, uint16_t clusterId, uint8_t commandId);
......@@ -29,6 +34,8 @@ void zclBufferSetup(uint8_t frameType, uint16_t clusterId, uint8_t commandId);
(clu), \
(cmd))
void set_json_error_type(jrpc_context * ctx_p,int errorCode,char * errorMsg);
void rpcBufferPrint(void);
EmberStatus rpcSendCommand(uint16_t destination,uint8_t srcEndpoint,uint8_t dstEndpoint,bool multicast);
......@@ -42,6 +49,8 @@ typedef struct{
char rpc_convert_char(char ch);
uint8_t rpc_get_u8(char *string);
uint16_t rpc_get_u16(char *string);
bool rpc_get_mac(char *string,uint8_t *mac);
EmberAfStatus ReadAttr(uint16_t node,
uint8_t endpoint,
......@@ -54,7 +63,7 @@ cJSON *rpc_ReadAttr(jrpc_context * ctx, cJSON * params, cJSON *id);
#define RPC_COMMON_FUNCTION_TABLE \
{rpc_ReadAttr,"ReadAttr"}\
{(rpc_function*)rpc_ReadAttr,"ReadAttr"}\
extern uint8_t appZclBuffer[];
......@@ -66,13 +75,60 @@ extern EmberApsFrame globalApsFrame;
EmberStatus rpc_read_modleId(EmberNodeId nodeId);
EmberStatus rpc_read_ApplicationVersion(EmberNodeId nodeId);
uint8_t rpc_add_device(EmberNodeId nodeId);
uint8_t rpc_read_response_process_callback(EmberNodeId nodeId,
uint8_t ep,
EmberAfClusterId cluster,
EmberAfAttributeId attribute,
uint8_t dataType,
uint8_t dataSize,
uint8_t *data);
void rpc_printfJSON(char* str,cJSON* item);
void rpc_eui64ToString(EmberEUI64 eui, char* euiString);
void rpc_nodeIdToString(EmberNodeId nodeId, char* nodeIdString);
void rpc_clusterIdToString(EmberAfClusterId clusterId, char* clusterIdString);
void rpc_attributeIdToString(EmberAfAttributeId attributeId, char* attributeIdString);
char* rpc_createTwoByteHexString(uint16_t value);
void rpc_cJSON_AddMACToObject(cJSON *item,EmberEUI64 nodeEui64);
void rpc_cJSON_AddNodeToObject(cJSON *item,EmberNodeId nodeId);
void rpc_cJSON_AddEndpointToObject(cJSON *item,uint8_t endpoint);
void rpc_cJSON_AddClusterToObject(cJSON *item,EmberAfClusterId clusterId);
void rpc_cJSON_AddAttributeToObject(cJSON *item,EmberAfAttributeId attributeId);
void rpc_cJSON_AddDataTypeToObject(cJSON *item, uint8_t type);
void rpc_cJSON_AddLengthToObject(cJSON *item, uint8_t length);
void rpc_cJSON_AddDataToObject(cJSON *item,uint8_t *data,uint8_t length);
void rpc_cJSON_AddStatusToObject(cJSON *item, uint8_t status);
void rpc_cJSON_AddAppVersionToObject(cJSON *item,uint8_t ver);
#define RPC_APPVERSION_STRING_LENGTH 3 //
#define RPC_ENDPOINT_STRING_LENGTH 4 //0-255 + NULL
#define RPC_EUI64_STRING_LENGTH 19 //"0x" + 16 characters + NULL
#define RPC_NODEID_STRING_LENGTH 7 //"0x" + 4 characters + NULL
#define RPC_CLUSTERID_STRING_LENGTH 7 // "0x" + 4 chars + NULL
#define RPC_ATTRIBUTE_STRING_LENGTH 7 // "0x" + 4 chars + NULL
#define RPC_DATATYPE_STRING_LENGTH 4 //0-255 + NULL
#define RPC_LENGTH_STRING_LENGTH 4 //0-255 + NULL
#define RPC_STATUS_STRING_LENGTH 4 //0-255 + NULL
......
......@@ -2,6 +2,8 @@
#define __PRC_INTERFACE_PARSE_H
#include "Z3GatewayHost.h"
#include "yjq_ezsp.h"
#include "./jsonrpc/rpccJSON.h"
#define PORT 5555
......@@ -10,6 +12,8 @@ void rpcInterfaceParse(void);
void ipcHandle(void);
void rpc_reportDevices(void);
void rpc_control_devices(cJSON *data,char *method);
......
......@@ -12,6 +12,9 @@ typedef struct{
}rpc_nwk_info_s;
EmberStatus nwkLeave(void);
EmberStatus nwkLeaveRequest(EmberNodeId target,bool removeChildren,bool rejoin);
void nwkGetInfo(rpc_nwk_info_s *info);
EmberStatus nwkFormCMD(bool centralized,uint16_t panId,int8_t txPower,uint8_t channel);
void nwkStartCMD(bool centralized);
......@@ -26,17 +29,25 @@ void nwkClearJoiningLinkKeyCMD(void);
cJSON *rpc_nwkGetInfo(jrpc_context * ctx, cJSON * params, cJSON *id);
cJSON *rpc_nwkFormCMD(jrpc_context * ctx, cJSON * params, cJSON *id);
cJSON *rpc_nwkStar(jrpc_context * ctx, cJSON * params, cJSON *id);
cJSON *rpc_nwkPermitJoin(jrpc_context * ctx, cJSON * params, cJSON *id);
cJSON *rpc_nwkLeave(jrpc_context * ctx, cJSON * params, cJSON *id);
cJSON *rpc_nwkLeaveRequest(jrpc_context * ctx, cJSON * params, cJSON *id);
#define RPC_NETWORK_FUNCTION_TABLE \
{rpc_nwkGetInfo,"nwkGetInfo"},\
{rpc_nwkFormCMD,"nwkForm"},\
{rpc_nwkStar,"nwkStar"},\
{rpc_nwkPermitJoin,"nwkPermitJoin"}
{(rpc_function*)rpc_nwkLeave,"nwkLeave"},\
{(rpc_function*)rpc_nwkLeaveRequest,"nwkLeaveRequest"},\
{(rpc_function*)rpc_nwkGetInfo,"nwkGetInfo"},\
{(rpc_function*)rpc_nwkFormCMD,"thing.topo.property.set"},\
{(rpc_function*)rpc_nwkStar,"nwkStar"}\
......
#include "RPC_API.h"
#include <pthread.h>
pthread_mutex_t report_mutex_lock;
void set_json_error_type(jrpc_context * ctx_p,int errorCode,char * errorMsg)
{
ctx_p->error_code = errorCode;
ctx_p->error_message = strdup(errorMsg);
}
static char table[]="0123456789ABCDEF";
......@@ -7,18 +17,33 @@ char rpc_convert_char(char ch)
{
if(ch>='0'&&ch<='9'){
ch-='0';
}else if(ch>='A'&&ch<="F"){
ch-='A'+10;
}else if(ch>='a'&&ch<="f"){
ch-='a'+10;
}else if(ch>='A' && ch<='F'){
ch=ch-'A'+10;
}else if(ch>='a'&&ch<='f'){
ch=ch-'a'+10;
}
return ch;
}
uint8_t rpc_get_u8(char *string)
{
if(string==NULL) return 0;
uint8_t value;
value = rpc_convert_char(string[0])<<4;
value|=rpc_convert_char(string[1]);
char valString[2]={0};
int len = strlen(string);
if(len<2){
memcpy(&valString[2-len],string,len);
}else{
memcpy(valString,string,2);
}
//emberAfCorePrintln("\r\nstring=%s,valString=%s\r\n",string,valString);
value = rpc_convert_char(valString[0])<<4;
value|=rpc_convert_char(valString[1]);
return value;
}
......@@ -26,12 +51,33 @@ uint16_t rpc_get_u16(char *string)
{
uint16_t value;
value = rpc_convert_char(string[0])<<12;
value|=rpc_convert_char(string[1])<<8;
value|=rpc_convert_char(string[2])<<4;
value|=(rpc_convert_char(string[1])<<8);
value|=(rpc_convert_char(string[2])<<4);
value|=rpc_convert_char(string[3]);
return value;
}
bool rpc_get_mac(char *string,uint8_t *mac)
{
if(string==NULL||mac==NULL) return false;
int len = strlen(string);
char macString[16]={0};
if(len<16){
memcpy(&macString[16-len],string,len);
}else{
memcpy(macString,string,16);
}
emberAfCorePrintln("\r\nlen=%d,macString=%s\r\n",len,macString);
for(int i=0;i<EUI64_SIZE;i++){
mac[EUI64_SIZE-1-i] = rpc_convert_char(macString[2*i])<<4;
mac[EUI64_SIZE-1-i]|=rpc_convert_char(macString[2*i+1]);
emberAfCorePrintln("i=%d,val=%02x",i,mac[EUI64_SIZE-1-i]);
}
return true;
}
EmberAfStatus ReadAttr(uint16_t node,
......@@ -301,11 +347,9 @@ void rpc_PreCommandReceivedCallback(EmberAfClusterCommand* cmd)
uint8_t clientServerMask = (cmd->direction == ZCL_DIRECTION_CLIENT_TO_SERVER
? CLUSTER_MASK_SERVER
: CLUSTER_MASK_CLIENT);
(cmd->clusterSpecific
? emAfProcessClusterSpecificCommand(cmd)
: emAfProcessGlobalCommand(cmd));
if(cmd->apsFrame->profileId!=0x0104) return;
emberAfAppPrint("@@@@@@@@@@@@@@@@@@@@@zclCmd=0X%X\n",zclCmd);
if(cmd->clusterSpecific==false){//global
switch(zclCmd){
......@@ -329,14 +373,119 @@ void rpc_PreCommandReceivedCallback(EmberAfClusterCommand* cmd)
char* rpc_createTwoByteHexString(uint16_t value)
{
char* outputString = (char *) malloc(7);
sprintf(outputString, "0x%04X", value);
return outputString;
}
void rpc_printfJSON(char* str,cJSON* item)
{
char* json = rpc_cJSON_Print(item);
emberAfAppPrintln("%s:\n%s\n",str,json);
free(json);
}
void rpc_eui64ToString(EmberEUI64 eui, char* euiString)
{
sprintf(euiString, "0x%02X%02X%02X%02X%02X%02X%02X%02X",
eui[7],
eui[6],
eui[5],
eui[4],
eui[3],
eui[2],
eui[1],
eui[0]);
}
void rpc_nodeIdToString(EmberNodeId nodeId, char* nodeIdString)
{
sprintf(nodeIdString, "0x%04X", nodeId);
}
void rpc_clusterIdToString(EmberAfClusterId clusterId, char* clusterIdString)
{
sprintf(clusterIdString, "0x%04X", clusterId);
}
void rpc_attributeIdToString(EmberAfAttributeId attributeId, char* attributeIdString)
{
sprintf(attributeIdString, "0x%04X", attributeId);
}
void rpc_cJSON_AddMACToObject(cJSON *item,EmberEUI64 nodeEui64)
{
char macString[RPC_EUI64_STRING_LENGTH];
rpc_eui64ToString(nodeEui64,macString);
rpc_cJSON_AddStringToObject(item, "mac", macString);
}
void rpc_cJSON_AddNodeToObject(cJSON *item,EmberNodeId nodeId)
{
char nodeString[RPC_NODEID_STRING_LENGTH];
rpc_nodeIdToString(nodeId,nodeString);
rpc_cJSON_AddStringToObject(item, "node", nodeString);
}
void rpc_cJSON_AddEndpointToObject(cJSON *item,uint8_t endpoint)
{
char endpointString[RPC_ENDPOINT_STRING_LENGTH];
sprintf(endpointString, "%d",endpoint);
rpc_cJSON_AddStringToObject(item, "endpoint",endpointString);
}
void rpc_cJSON_AddClusterToObject(cJSON *item,EmberAfClusterId clusterId)
{
char clusterString[RPC_CLUSTERID_STRING_LENGTH];
rpc_clusterIdToString(clusterId,clusterString);
rpc_cJSON_AddStringToObject(item, "clusterId", clusterString);
}
void rpc_cJSON_AddAttributeToObject(cJSON *item,EmberAfAttributeId attributeId)
{
char attributeString[RPC_ATTRIBUTE_STRING_LENGTH];
rpc_attributeIdToString(attributeId,attributeString);
rpc_cJSON_AddStringToObject(item, "attributeId", attributeString);
}
void rpc_cJSON_AddDataTypeToObject(cJSON *item, uint8_t type)
{
char dataTypeString[RPC_DATATYPE_STRING_LENGTH];
sprintf(dataTypeString, "0x%02X",type);
rpc_cJSON_AddStringToObject(item, "DataType", dataTypeString);
}
void rpc_cJSON_AddLengthToObject(cJSON *item, uint8_t length)
{
char lengthString[RPC_LENGTH_STRING_LENGTH];
sprintf(lengthString, "0x%02X",length);
rpc_cJSON_AddStringToObject(item, "length", lengthString);
}
void rpc_cJSON_AddDataToObject(cJSON *item,uint8_t *data,uint8_t length)
{
int i=0;
char *dataString = (char *)malloc(length*2+1);
for(;i<length;i++){
sprintf(&dataString[2*i],"%02X",data[i]);
}
dataString[2*i]='\0';
rpc_cJSON_AddStringToObject(item, "data", dataString);
free(dataString);
}
void rpc_cJSON_AddStatusToObject(cJSON *item, uint8_t status)
{
char statusString[RPC_STATUS_STRING_LENGTH];
sprintf(statusString, "0x%02X",status);
rpc_cJSON_AddStringToObject(item, "Status", statusString);
}
void rpc_cJSON_AddAppVersionToObject(cJSON *item,uint8_t ver)
{
char VerString[RPC_APPVERSION_STRING_LENGTH];
sprintf(VerString, "%02X",ver);
rpc_cJSON_AddStringToObject(item, "AppVersion", VerString);
}
......
......@@ -20,6 +20,7 @@
#include "rpc_onoff.h"
#include "com_api.h"
#include "kk_test.h"
static struct jrpc_server my_server;
......@@ -34,6 +35,7 @@ typedef struct{
rpc_table_s rpc_table[]={
{test_func,"test_func"},
RPC_KK_TEST_FUNCTION_TABLE,
RPC_NETWORK_FUNCTION_TABLE,
RPC_COMMON_FUNCTION_TABLE,
RPC_GLOBAL_COMMAND_FUNCTION_TABLE,
......@@ -245,3 +247,426 @@ void ipcHandle(void)
//jrpc_server_destroy(&my_server);
}
int jrpc_send_msg(cJSON * msgJson) {
int return_value = 0;
char * str_result = rpc_cJSON_Print(msgJson);
return_value = kk_ipc_send(IPC_PLAT2MID, str_result, strlen(str_result)+1);
free(str_result);
return return_value;
}
#define ATTRIBUTE_BUFFER_REPORT_DATA_TYPE 2
// Attribute reading buffer location definitions
#define ATTRIBUTE_BUFFER_ATTRIBUTEID_LOW_BITS 0
#define ATTRIBUTE_BUFFER_ATTRIBUTEID_HIGH_BITS 1
#define ATTRIBUTE_BUFFER_SUCCESS_CODE 2
#define ATTRIBUTE_BUFFER_DATA_TYPE 3
#define ATTRIBUTE_BUFFER_DATA_START 4
static void rpc_send_message(cJSON *data,char *method)
{
cJSON *item = rpc_cJSON_CreateObject();
rpc_cJSON_AddStringToObject(item, "jsonrpc", "2.0");
rpc_cJSON_AddStringToObject(item, "method",method);
rpc_cJSON_AddItemToObject(item, "params", data);
char* p = rpc_cJSON_Print(item);
emberAfAppPrintln("send send json:\n%s\n",p);
free(p);
jrpc_send_msg(item);
}
void rpc_read_attribute_response(cJSON *data)
{
rpc_send_message(data,"read_attribute_response");
}
void rpc_report_attribute(cJSON *data)
{
rpc_send_message(data,"report_attribute");
}
void rpc_report_devices(cJSON *data)
{
rpc_send_message(data,"report_devices");
}
void rpc_control_devices(cJSON *data,char *method)
{
rpc_send_message(data,method);
}
bool rpc_ReportAttributesCallback(EmberAfClusterId clusterId,
uint8_t * buffer,
uint16_t bufLen)
{
EmberEUI64 nodeEui64;
EmberNodeId nodeId = emberAfCurrentCommand()->source;
uint8_t ep = emberAfCurrentCommand()->apsFrame->sourceEndpoint;
emberAfDeviceTableGetEui64FromNodeId(nodeId, nodeEui64);
uint8_t * bufferTemp;
uint8_t * bufferPtr = buffer;
uint8_t i, bufferSize,typeSize;
kk_print_debug("\n********************report callback**********************\n");
emberAfAppPrint("[ ");
emberAfAppPrintBuffer(buffer,bufLen,true);
emberAfAppPrint("]\n");
if (bufLen == 0) {
emberAfAppPrintln("Report attributes callback: zero length buffer");
return false;
}
kk_print_debug("\nmac:");
emberAfPrintBigEndianEui64(nodeEui64);
emberAfAppPrintln(",EP=%d,cluster=0x%04X\n",ep,clusterId);
cJSON *item = rpc_cJSON_CreateObject();
cJSON *array_attr = rpc_cJSON_CreateObject();
rpc_cJSON_AddMACToObject(item,nodeEui64);
rpc_cJSON_AddNodeToObject(item,nodeId);
rpc_cJSON_AddEndpointToObject(item,ep);
rpc_cJSON_AddClusterToObject(item,clusterId);
array_attr = rpc_cJSON_CreateArray();
rpc_cJSON_AddItemToObject(item,"attributes",array_attr);
for (i = 0; i < bufLen; ) {
if(emberAfIsStringAttributeType(bufferPtr[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE])){
bufferSize = bufferPtr[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE + 1];
typeSize = 1;
}else if(emberAfIsLongStringAttributeType(bufferPtr[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE])){
bufferSize = HIGH_LOW_TO_INT(bufferPtr[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE + 2], bufferPtr[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE + 1]);
typeSize = 2;
}else {
typeSize = 0;
bufferSize = emberAfGetDataSize(
bufferPtr[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE]);
}
bufferSize = bufferSize + 3 + typeSize;
bufferTemp = (uint8_t*)malloc(bufferSize);
memcpy(bufferTemp, bufferPtr, bufferSize);
bufferPtr = bufferPtr + bufferSize;
i = i + bufferSize;
emberAfAppPrintln("i=%d,bufferSize=%d\n",i,bufferSize);
emberAfAppPrintln("Reported attribute: 0x%02X%02X, Type: %02X",
bufferTemp[ATTRIBUTE_BUFFER_ATTRIBUTEID_HIGH_BITS],
bufferTemp[ATTRIBUTE_BUFFER_ATTRIBUTEID_LOW_BITS],
bufferTemp[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE]);
cJSON *item_attr = rpc_cJSON_CreateObject();
rpc_cJSON_AddItemToArray(array_attr,item_attr);
EmberAfAttributeId attributeId = HIGH_LOW_TO_INT(bufferTemp[ATTRIBUTE_BUFFER_ATTRIBUTEID_HIGH_BITS],bufferTemp[ATTRIBUTE_BUFFER_ATTRIBUTEID_LOW_BITS]);
EmberEUI64 eui64;
if(emberAfDeviceTableGetEui64FromNodeId(nodeId,eui64)){
if(clusterId==ZCL_ON_OFF_CLUSTER_ID && attributeId==ZCL_ON_OFF_ATTRIBUTE_ID){
if(bufferTemp[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE]==ZCL_BOOLEAN_ATTRIBUTE_TYPE){
uint8_t LightStatus = bufferTemp[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE+1];
if(LightStatus==0||LightStatus==1){
kk_rpc_report_LightStatus(eui64,LightStatus);
}
}
}
}
rpc_cJSON_AddAttributeToObject(item_attr,attributeId);
rpc_cJSON_AddDataTypeToObject(item_attr,bufferTemp[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE]);
int dataLen = bufferSize-3-typeSize;
rpc_cJSON_AddLengthToObject(item_attr,dataLen);
rpc_cJSON_AddDataToObject(item_attr,&bufferTemp[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE+1+typeSize],dataLen);
free(bufferTemp);
}
//rpc_report_attribute(item);
return false;
}
bool rpc_ReadAttributesResponseCallback(EmberAfClusterId clusterId,
uint8_t *buffer,
uint16_t bufLen)
{
EmberEUI64 nodeEui64;
EmberNodeId nodeId = emberAfCurrentCommand()->source;
uint8_t ep = emberAfCurrentCommand()->apsFrame->sourceEndpoint;
emberAfDeviceTableGetEui64FromNodeId(nodeId, nodeEui64);
uint8_t * bufferTemp;
uint8_t * bufferPtr = buffer;
uint8_t i, bufferSize,typeSize;
uint8_t cnt=1;
uint8_t Status;
kk_print_debug("\n********************read attributes response callback**********************\n");
emberAfAppPrint("[ ");
emberAfAppPrintBuffer(buffer,bufLen,true);
emberAfAppPrint("]\n");
if (bufLen == 0) {
emberAfAppPrintln("read attributes response callback: zero length buffer");
return false;
}
kk_print_debug("\nmac:");
emberAfPrintBigEndianEui64(nodeEui64);
emberAfAppPrintln(",EP=%d,cluster=0x%04X\n",ep,clusterId);
cJSON *item = rpc_cJSON_CreateObject();
cJSON *array_attr = rpc_cJSON_CreateObject();
rpc_cJSON_AddMACToObject(item,nodeEui64);
rpc_cJSON_AddNodeToObject(item,nodeId);
rpc_cJSON_AddEndpointToObject(item,ep);
rpc_cJSON_AddClusterToObject(item,clusterId);
array_attr = rpc_cJSON_CreateArray();
rpc_cJSON_AddItemToObject(item,"attributes",array_attr);
cJSON *item_attr = rpc_cJSON_CreateObject();
rpc_cJSON_AddItemToArray(array_attr,item_attr);
//todo:check
for (i = 0; i < bufLen; ) {
Status = bufferPtr[2];
if(Status == EMBER_ZCL_STATUS_SUCCESS){
if(emberAfIsStringAttributeType(bufferPtr[3])){
bufferSize = bufferPtr[4];
typeSize = 1;
}else if(emberAfIsLongStringAttributeType(bufferPtr[3])){
bufferSize = HIGH_LOW_TO_INT(bufferPtr[5], bufferPtr[4]);
typeSize = 2;
}else {
typeSize = 0;
bufferSize = emberAfGetDataSize(
bufferPtr[3]);
}
bufferSize = bufferSize + 4 + typeSize;
bufferTemp = (uint8_t*)malloc(bufferSize);
memcpy(bufferTemp, bufferPtr, bufferSize);
bufferPtr = bufferPtr + bufferSize;
emberAfAppPrintln("i=%d,bufferSize=%d\n",i,bufferSize);
emberAfAppPrintln("Read attribute Response: 0x%02X%02X, Type: %02X",
bufferTemp[ATTRIBUTE_BUFFER_ATTRIBUTEID_HIGH_BITS],
bufferTemp[ATTRIBUTE_BUFFER_ATTRIBUTEID_LOW_BITS],
bufferTemp[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE]);
rpc_cJSON_AddStatusToObject(item_attr,Status);
EmberAfAttributeId attributeId = HIGH_LOW_TO_INT(bufferTemp[ATTRIBUTE_BUFFER_ATTRIBUTEID_HIGH_BITS],bufferTemp[ATTRIBUTE_BUFFER_ATTRIBUTEID_LOW_BITS]);
rpc_cJSON_AddAttributeToObject(item_attr,attributeId);
rpc_cJSON_AddDataTypeToObject(item_attr,bufferTemp[3]);
int dataLen = bufferSize-4-typeSize;
rpc_cJSON_AddLengthToObject(item_attr,dataLen);
uint8_t *dataPtr = &bufferTemp[ATTRIBUTE_BUFFER_REPORT_DATA_TYPE+1+1+typeSize];
rpc_cJSON_AddDataToObject(item_attr,dataPtr,dataLen);
//rpc_read_response_process_callback(nodeId,ep,clusterId,attributeId,bufferPtr[3],dataLen,dataPtr);
free(bufferTemp);
}else{
rpc_cJSON_AddStatusToObject(item_attr,Status);
bufferSize += 2;
emberAfAppPrintln("Status=%d\n",Status);
}
i = i + bufferSize;
}
rpc_read_attribute_response(item);
return false;
}
static cJSON* rpc_reportDeviceState(char *state,EmberEUI64 eui64)
{
char euiString[RPC_EUI64_STRING_LENGTH] = { 0 };
cJSON* stateJSON;
rpc_eui64ToString(eui64, euiString);
stateJSON = rpc_cJSON_CreateObject();
rpc_cJSON_AddStringToObject(stateJSON, "mac", euiString);
rpc_cJSON_AddStringToObject(stateJSON, "status", state);
return stateJSON;
}
void rpc_reportDeviceStateChange(EmberEUI64 eui64,uint8_t state)
{
char euiString[RPC_EUI64_STRING_LENGTH] = { 0 };
cJSON* stateChangeJson;
rpc_eui64ToString(eui64, euiString);
stateChangeJson = rpc_cJSON_CreateObject();
rpc_cJSON_AddStringToObject(stateChangeJson, "mac", euiString);
rpc_cJSON_AddNumberToObject(stateChangeJson, "deviceState", state);
rpc_printfJSON("devicestatechange",stateChangeJson);
}
void emberAfPluginDeviceTableStateChangeCallback(EmberNodeId nodeId,
uint8_t state)
{
EmberEUI64 nodeEui64;
emberAfDeviceTableGetEui64FromNodeId(nodeId, nodeEui64);
rpc_reportDeviceStateChange(nodeEui64, state);
}
void emberAfPluginDeviceTableRejoinDeviceCallback(EmberEUI64 nodeEui64)
{
uint16_t deviceTableIndex = emberAfDeviceTableGetFirstIndexFromEui64(nodeEui64);
if(deviceTableIndex == EMBER_AF_PLUGIN_DEVICE_TABLE_NULL_INDEX){
return ;
}
EmberAfPluginDeviceTableEntry *deviceTable = emberAfDeviceTablePointer();
cJSON* nodeJson = rpc_reportDeviceState("rejoin",deviceTable[deviceTableIndex].eui64);
rpc_printfJSON("rejoin",nodeJson);
rpc_send_message(nodeJson,"device rejoin");
}
void emberAfPluginDeviceTableDeviceLeftCallback(EmberEUI64 nodeEui64)
{
uint16_t deviceTableIndex = emberAfDeviceTableGetFirstIndexFromEui64(nodeEui64);
if(deviceTableIndex == EMBER_AF_PLUGIN_DEVICE_TABLE_NULL_INDEX){
return ;
}
EmberAfPluginDeviceTableEntry *deviceTable = emberAfDeviceTablePointer();
cJSON* nodeJson = rpc_reportDeviceState("left",deviceTable[deviceTableIndex].eui64);
rpc_printfJSON("left",nodeJson);
rpc_send_message(nodeJson,"device left");
}
static cJSON* rpc_buildDeviceEndpoint(EmberEUI64 eui64, uint8_t endpoint)
{
cJSON* deviceEndpointObj;
char euiString[RPC_EUI64_STRING_LENGTH] = { 0 };
deviceEndpointObj = rpc_cJSON_CreateObject();
rpc_eui64ToString(eui64, euiString);
rpc_cJSON_AddStringToObject(deviceEndpointObj, "mac", euiString);
rpc_cJSON_AddNumberToObject(deviceEndpointObj, "ep", endpoint);
return deviceEndpointObj;
}
static cJSON* rpc_buildDeviceEndpointWithClusterInfo(
EmberEUI64 eui64,
uint8_t endpoint,
uint16_t *clusterIds,
uint8_t clusterOutStartPosition)
{
cJSON* deviceEndpointObj;
cJSON* clusterInfoArray;
cJSON* clusterInfoItem;
uint16_t clusterIdIndex;
char clusterIdString[RPC_CLUSTERID_STRING_LENGTH] = { 0 };
clusterInfoArray = rpc_cJSON_CreateArray();
deviceEndpointObj = rpc_buildDeviceEndpoint(eui64, endpoint);
for (clusterIdIndex = 0;
clusterIdIndex < EMBER_AF_PLUGIN_DEVICE_TABLE_CLUSTER_SIZE;
clusterIdIndex++) {
clusterInfoItem = rpc_cJSON_CreateObject();
if (clusterIds[clusterIdIndex] != ZCL_NULL_CLUSTER_ID) {
sprintf(clusterIdString, "0x%04X", clusterIds[clusterIdIndex]);
rpc_cJSON_AddStringToObject(clusterInfoItem, "clusterId", clusterIdString);
if (clusterIdIndex < clusterOutStartPosition) {
rpc_cJSON_AddStringToObject(clusterInfoItem, "clusterType", "In");
} else {
rpc_cJSON_AddStringToObject(clusterInfoItem, "clusterType", "Out");
}
rpc_cJSON_AddItemToArray(clusterInfoArray, clusterInfoItem);
clusterInfoItem = NULL;
} else {
rpc_cJSON_Delete(clusterInfoItem);
clusterInfoItem = NULL;
break;
}
}
rpc_cJSON_AddItemToObject(deviceEndpointObj, "clusterInfo", clusterInfoArray);
return deviceEndpointObj;
}
static cJSON* buildNodeJson(uint16_t nodeIndex)
{
cJSON* nodeJson;
cJSON* deviceEndpoint;
char nodeIdString[RPC_NODEID_STRING_LENGTH] = { 0 };
char* deviceTypeString;
EmberAfPluginDeviceTableEntry *deviceTable = emberAfDeviceTablePointer();
nodeJson = rpc_cJSON_CreateObject();
rpc_nodeIdToString(deviceTable[nodeIndex].nodeId, nodeIdString);
rpc_cJSON_AddStringToObject(nodeJson, "nodeId", nodeIdString);
rpc_cJSON_AddNumberToObject(nodeJson,
"deviceState",
deviceTable[nodeIndex].state);
deviceTypeString = rpc_createTwoByteHexString(deviceTable[nodeIndex].deviceId);
rpc_cJSON_AddStringToObject(nodeJson, "deviceType", deviceTypeString);
free(deviceTypeString);
deviceEndpoint = rpc_buildDeviceEndpointWithClusterInfo(
deviceTable[nodeIndex].eui64,
deviceTable[nodeIndex].endpoint,
deviceTable[nodeIndex].clusterIds,
deviceTable[nodeIndex].clusterOutStartPosition);
rpc_cJSON_AddItemToObject(nodeJson, "deviceEndpoint", deviceEndpoint);
return nodeJson;
}
void rpc_reportDevices(void)
{
uint16_t nodeIndex;
cJSON* nodeJson;
cJSON* devicesJson;
cJSON* devicesJsonNodeArray;
devicesJson = rpc_cJSON_CreateObject();
devicesJsonNodeArray = rpc_cJSON_CreateArray();
rpc_cJSON_AddItemToObject(devicesJson, "devices", devicesJsonNodeArray);
for (nodeIndex = 0;
nodeIndex < EMBER_AF_PLUGIN_DEVICE_TABLE_DEVICE_TABLE_SIZE;
nodeIndex++) {
if (emberAfDeviceTableGetNodeIdFromIndex(nodeIndex)
!= EMBER_AF_PLUGIN_DEVICE_TABLE_NULL_NODE_ID) {
nodeJson = buildNodeJson(nodeIndex);
rpc_cJSON_AddItemToArray(devicesJsonNodeArray, nodeJson);
break;
}
}
rpc_report_devices(devicesJson);
}
......@@ -139,6 +139,7 @@ EmberStatus nwkPermitJoinCMD(bool open)
"NWK Creator Security",
(open ? "Open" : "Close"),
status);
return status;
}
......@@ -174,6 +175,40 @@ void nwkClearJoiningLinkKeyCMD(void)
"Clear joining link keys",
EMBER_SUCCESS);
}
// network leave
EmberStatus nwkLeave(void)
{
EmberStatus status;
status = emberLeaveNetwork();
emberAfAppPrintln("%p 0x%x", "leave", status);
return status;
}
EmberStatus nwkLeaveRequest(EmberNodeId target,bool removeChildren,bool rejoin)
{
EmberEUI64 nullEui64 = { 0, 0, 0, 0, 0, 0, 0, 0 };
EmberStatus status;
uint8_t options = 0;
if (rejoin) {
options |= EMBER_ZIGBEE_LEAVE_AND_REJOIN;
}
if (removeChildren) {
options |= EMBER_ZIGBEE_LEAVE_AND_REMOVE_CHILDREN;
}
status = emberLeaveRequest(target,
nullEui64,
options,
EMBER_APS_OPTION_RETRY);
emberAfAppPrintln("Leave %p0x%X", "Request: ", status);
return status;
}
/**************************************************************************************************/
/*
void nwkStartCMD(bool centralized);
......@@ -207,12 +242,67 @@ cJSON * test_func(jrpc_context * ctx, cJSON * params, cJSON *id) {
return item1;
}*/
void set_json_error_type(jrpc_context * ctx_p,int errorCode,char * errorMsg)
cJSON *rpc_nwkLeaveRequest(jrpc_context * ctx, cJSON * params, cJSON *id)
{
cJSON *item_target,*item_rm,*item_rejoin;
EmberNodeId target;
bool removeChildren;
bool rejoin;
EmberStatus status;
if(params == NULL){
emberAfCorePrintln("\r\nparams == NULL\r\n" );
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
goto error_return;
}else if(params->type == cJSON_Object){
item_target = rpc_cJSON_GetObjectItem(params, "target");
item_rm = rpc_cJSON_GetObjectItem(params, "removeChildren");
item_rejoin = rpc_cJSON_GetObjectItem(params, "rejoin");
}else if(params->type == cJSON_Array){
//
}
if(item_target==NULL||item_rm==NULL||item_rejoin==NULL){
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
kk_print_debug("1....\n");
goto error_return;
}
if(item_target->type!=cJSON_String ||
(item_rm->type!=cJSON_False&& item_rm->type!=cJSON_True)||
(item_rejoin->type!=cJSON_False&& item_rejoin->type!=cJSON_True)){
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
kk_print_debug(".2....\n");
goto error_return;
}
target = rpc_get_u16(item_target->valuestring);
removeChildren = (item_rm->type==cJSON_True)?true:false;
rejoin = (item_rejoin->type==cJSON_True)?true:false;
status = nwkLeaveRequest(target,removeChildren,rejoin);
return rpc_cJSON_CreateNumber(status);
error_return:
return rpc_cJSON_CreateNull();
}
cJSON *rpc_nwkLeave(jrpc_context * ctx, cJSON * params, cJSON *id)
{
ctx_p->error_code = errorCode;
ctx_p->error_message = strdup(errorMsg);
EmberStatus status;
if(params == NULL){
emberAfCorePrintln("\r\nparams == NULL\r\n" );
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
return rpc_cJSON_CreateNull();
}else{
status = nwkLeave();
return rpc_cJSON_CreateNumber(status);
}
}
cJSON *rpc_nwkGetInfo(jrpc_context * ctx, cJSON *params, cJSON *id)
{
rpc_nwk_info_s info;
......@@ -269,52 +359,56 @@ cJSON *rpc_nwkGetInfo(jrpc_context * ctx, cJSON *params, cJSON *id)
for(int i=0;i<EMBER_ENCRYPTION_KEY_SIZE;i++){
sprintf(&buffer[i*3],"%02X ",info.nwkKey.key.contents[EMBER_ENCRYPTION_KEY_SIZE-1-i]);
}
}
rpc_cJSON_AddStringToObject(item, "network key",buffer);
}
return item;
}
}
cJSON *rpc_nwkFormCMD(jrpc_context * ctx, cJSON * params, cJSON *id)
{
cJSON *centralized,*panId,*txPower,*channel;
cJSON *item_centralized;
cJSON *item_panId;
cJSON *item_txPower;
cJSON *item_channel;
EmberStatus status;
if(params == NULL){
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
goto error_return;
}else if(params->type == cJSON_Object){
centralized = cJSON_GetObjectItem(params, "centralized");
txPower = cJSON_GetObjectItem(params, "txPower");
channel = cJSON_GetObjectItem(params, "channel");
panId = cJSON_GetObjectItem(params, "panId");
item_centralized = rpc_cJSON_GetObjectItem(params, "centralized");
item_txPower = rpc_cJSON_GetObjectItem(params, "txPower");
item_channel = rpc_cJSON_GetObjectItem(params, "channel");
item_panId = rpc_cJSON_GetObjectItem(params, "panId");
}else if(params->type == cJSON_Array){
centralized = cJSON_GetArrayItem(params, 0);
panId = cJSON_GetArrayItem(params, 1);
txPower= cJSON_GetArrayItem(params, 2);
channel = cJSON_GetArrayItem(params, 3);
item_centralized = rpc_cJSON_GetArrayItem(params, 0);
item_panId = rpc_cJSON_GetArrayItem(params, 1);
item_txPower= rpc_cJSON_GetArrayItem(params, 2);
item_channel = rpc_cJSON_GetArrayItem(params, 3);
}
else{
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
goto error_return;
}
if(centralized == NULL ||
panId == NULL ||
txPower == NULL ||
channel == NULL){
if(item_centralized == NULL ||
item_panId == NULL ||
item_txPower == NULL ||
item_channel == NULL){
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
goto error_return;
}
if(centralized->type != cJSON_Number ||
panId->type != cJSON_Number ||
txPower->type != cJSON_Number ||
channel->type != cJSON_Number ){
if(item_centralized->type != cJSON_Number ||
item_panId->type != cJSON_Number ||
item_txPower->type != cJSON_Number ||
item_channel->type != cJSON_Number ){
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
goto error_return;
}
status = nwkFormCMD(centralized->valueint,panId->valueint,txPower->valueint,channel->valueint);
status = nwkFormCMD(item_centralized->valueint,item_panId->valueint,item_txPower->valueint,item_channel->valueint);
return rpc_cJSON_CreateNumber(status);
......@@ -328,65 +422,46 @@ cJSON *rpc_nwkStar(jrpc_context * ctx, cJSON * params, cJSON *id)
}
cJSON *rpc_nwkaddChMaskCMD(jrpc_context * ctx, cJSON * params, cJSON *id)
{
kk_print_debug("1\r\n");
return rpc_cJSON_CreateNull();
}
cJSON *rpc_nwksubChMaskCMD(jrpc_context * ctx, cJSON * params, cJSON *id)
{
kk_print_debug("\r\n");
return rpc_cJSON_CreateNull();
}
cJSON *rpc_nwksetChMaskCMD(jrpc_context * ctx, cJSON * params, cJSON *id)
{
kk_print_debug("\r\n");
return rpc_cJSON_CreateNull();
}
cJSON *rpc_nwkgetChMaskCMD(jrpc_context * ctx, cJSON * params, cJSON *id)
{
kk_print_debug("\r\n");
return rpc_cJSON_CreateNull();
}
cJSON *rpc_nwkPermitJoin(jrpc_context * ctx, cJSON * params, cJSON *id)
{
cJSON * OpenOrClose;
EmberStatus status;
if(params == NULL){
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
goto error_return;
}else if(params->type == cJSON_Object){
OpenOrClose = cJSON_GetObjectItem(params, "Enable");
}else if(params->type == cJSON_Array){
OpenOrClose = cJSON_GetArrayItem(params, 0);
}else{
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
goto error_return;
}
if(OpenOrClose->type == cJSON_False){
status = nwkPermitJoinCMD(FALSE);
kk_print_debug("Disable Permit join\r\n");
}else if(OpenOrClose->type == cJSON_True){
status = nwkPermitJoinCMD(TRUE);
kk_print_debug("Enable Permit join 180s\r\n");
}else{
kk_print_debug("item type error[%d]\r\n",OpenOrClose->type);
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
goto error_return;
}
return rpc_cJSON_CreateNumber(status);
error_return:
return rpc_cJSON_CreateNull();
}
cJSON *rpc_nwkPermitJoinWithKeyCMD(jrpc_context * ctx, cJSON * params, cJSON *id)
{
kk_print_debug("1\r\n");
return rpc_cJSON_CreateNull();
}
cJSON *rpc_nwkSetJoiningLinkKeyCMD(jrpc_context * ctx, cJSON * params, cJSON *id)
{
kk_print_debug("1\r\n");
return rpc_cJSON_CreateNull();
}
cJSON *rpc_nwkClearJoiningLinkKeyCMD(jrpc_context * ctx, cJSON * params, cJSON *id)
{
kk_print_debug("2\r\n");
return rpc_cJSON_CreateNull();
}
......@@ -405,10 +480,6 @@ cJSON *rpc_nwkClearJoiningLinkKeyCMD(jrpc_context * ctx, cJSON * params, cJSON *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment