Commit 681bb385 authored by whmaizmy's avatar whmaizmy
parents 95aad7e2 00917ffc
......@@ -30,9 +30,9 @@
#include "kk_product.h"
//#include "iot_import.h"
char g_product_type[PRODUCT_TYPE_LEN];
char g_product_code[PRODUCT_CODE_LEN];
char g_device_code[DEVICE_CODE_LEN];
char g_product_type[PRODUCT_TYPE_LEN]={0};
char g_product_code[PRODUCT_CODE_LEN]={0};
char g_device_code[DEVICE_CODE_LEN] = {0};
#define PLATFORM_WAIT_INFINITE (~0)
......
......@@ -270,8 +270,8 @@ int dm_mgr_search_mac_by_topic(_IN_ char* topic, _OU_ char mac[DEVI
int res = 0;
dm_mgr_dev_node_t *node = NULL;
char product_key[PRODUCT_KEY_MAXLEN];
char device_name[DEVICE_NAME_MAXLEN];
char product_key[PRODUCT_KEY_MAXLEN] = {0};
char device_name[DEVICE_NAME_MAXLEN] = {0};
if (topic == NULL) {
return INVALID_PARAMETER;
......
......@@ -144,7 +144,7 @@ int dm_queue_msg_insert(void *data)
node = malloc(sizeof(dm_queue_msg_node_t));
if (node == NULL) {
_dm_queue_unlock();
return DM_MEMORY_NOT_ENOUGH;
return MEMORY_NOT_ENOUGH;
}
memset(node, 0, sizeof(dm_queue_msg_node_t));
......
......@@ -20,10 +20,23 @@
void mid_cb(void* data, int len){
if (data != NULL){
printf("app2mid_cb: %s RECEIVED \r\n", data);
char *out;cJSON *json, *topic, *payload;
char *out;
cJSON *json;
cJSON *topic;
cJSON*payload;
int res;
void* buf = malloc(len);
memcpy(buf, data, len);
res = dm_queue_msg_insert((void *)buf);
if (res != SUCCESS_RETURN) {
free(buf);
//return FAIL_RETURN;
}
json=cJSON_Parse(data);
if (!json) {
printf("Error before: [%s]\n","cJSON_Parse");
......@@ -31,31 +44,28 @@ void mid_cb(void* data, int len){
else
{
topic = cJSON_GetObjectItem(json, "topic");
payload = cJSON_GetObjectItem(json, "payload");
printf("mid_cb topic: [%s] ,payload= %s \n",topic->valuestring,payload->valuestring );
if (strcmp(payload->valuestring, "addsub")==0){
//kk_mid_subdev_add("a1OYuSallan","allanWno8yDdsjCX15iq","");
}else{
void* buf = malloc(len);
memcpy(buf, data, len);
int res = dm_queue_msg_insert((void *)buf);
if (res != SUCCESS_RETURN) {
free(buf);
//return FAIL_RETURN;
if (topic != NULL && (strstr(topic->valuestring, "register_reply") != NULL ||
strstr(topic->valuestring, "add_reply") != NULL ||
strstr(topic->valuestring, "login_reply") != NULL ||
strstr(topic->valuestring, "offline_reply") != NULL))
{
printf("This topic don't send to platform: %s \r\n", topic->valuestring);
return;
}
//kk_tsl_service_property_set(topic->valuestring, payload->valuestring, strlen(payload->valuestring), NULL);
}
char mac[DEVICE_MAC_MAXLEN];
payload = cJSON_GetObjectItem(json, "payload");
char mac[DEVICE_MAC_MAXLEN] = {0};
res =dm_mgr_search_mac_by_topic(topic->valuestring, mac);
if (res != SUCCESS_RETURN) {
return;
}
printf("dm_mgr_search_mac_by_topic mac: %s \r\n", mac);
cJSON * jsonplay=cJSON_Parse(payload->valuestring);
cJSON_AddStringToObject(jsonplay, "mac", mac);
void* out = cJSON_Print(jsonplay);
printf("dm_mgr_search_mac_by_topic out: %s \r\n", out);
kk_ipc_send(IPC_MID2PLAT, out, strlen(out));
free(out);
cJSON_Delete(jsonplay);
......@@ -69,7 +79,7 @@ void mid_cb(void* data, int len){
void mid2p_cb(void* data, int len){
if (data != NULL){
printf("mid2plat_cb: %s RECEIVED \r\n", data);
//printf("mid2plat_cb: %s RECEIVED \r\n", data);
void* buf = malloc(len);
memcpy(buf, data, len);
int res = dm_queue_msg_insert2((void *)buf);
......@@ -86,7 +96,13 @@ void kk_platMsg_handle(void* data){
char *out;
int res = 0;
cJSON *json, *method, *params;
cJSON *json;
cJSON *method;
cJSON *params;
cJSON *jsonPay;
cJSON *proType;
cJSON *proCode;
cJSON *mac;
json=cJSON_Parse(data);
if (!json) {
......@@ -94,32 +110,23 @@ void kk_platMsg_handle(void* data){
}
else{
method = cJSON_GetObjectItem(json, "method");
mac = cJSON_GetObjectItem(json, "mac");
if (method != NULL && strcmp(method->valuestring, "thing.topo.add")==0){
cJSON *jsonPay, *proType, *proCode, *mac;
params = cJSON_GetObjectItem(json, "params");
jsonPay =cJSON_Parse(params->valuestring);
if (!jsonPay) {
printf("Error before: [%s]\n","cJSON_Parse");
}else{
jsonPay = cJSON_GetObjectItem(json, "params");
proType = cJSON_GetObjectItem(jsonPay, "productType");
proCode = cJSON_GetObjectItem(jsonPay, "productCode");
mac = cJSON_GetObjectItem(jsonPay, "mac");
printf("productType productCode : [%s][%s]\n",proType, proCode);
kk_set_tsl_by_productKey(proCode->valuestring,"model.json");
kk_mid_subdev_add(proType->valuestring,proCode->valuestring,"", mac->valuestring);
cJSON_Delete(jsonPay);
}
}else if (method != NULL && strcmp(method->valuestring, "thing.event.property.post")==0){
cJSON *mac;
mac = cJSON_GetObjectItem(json, "mac");
printf("productType productCode mac: [%s][%s] [%s] \n",proType->valuestring, proCode->valuestring, mac->valuestring);
kk_set_tsl_by_productKey(proType->valuestring,"model.json");
kk_mid_subdev_add(proType->valuestring,proCode->valuestring,"", mac->valuestring);
//kk_tsl_service_property_set(topic->valuestring, data, strlen(data), NULL);
}else if (method != NULL && mac != NULL){
printf("save property and send to cloud \n");
kk_tsl_property_set_byMac(mac->valuestring, data, strlen(data)+1);
}else{
printf("kk_platMsg_handle data: [%s]\n",data);
printf("kk_platMsg_handle data: don't handle it [%s]\n",data);
//kk_tsl_service_property_set(topic->valuestring, payload->valuestring, strlen(payload->valuestring), NULL);
}
......@@ -275,25 +282,19 @@ int main(const int argc, const char **argv)
IOT_Linkkit_Close(mid_ctx->master_devid);
return -1;
}
char buf[100] = {0};
int ct = 0;
for (;;) {
usleep(4000000);
usleep(200000);
kk_platMsg_dispatch();
if (ct == 0){
ct =1;
kk_set_tsl_by_productKey("a1OYuSallan","model.json");
kk_mid_subdev_add("a1OYuSallan","allanWno8yDdsjCX15iq","","aabbccddeeff1122");
void* buf = "{ \"msgId\": \"7\", \"version\": \"1.0\", \"mac\": \"588E81FFFED3834A\", \"method\": \"thing.topo.add\", \"params\": { \"AppVersion\": \"10\", \"deviceType\": \"1\", \"deviceCode\": \"2\", \"productType\": \"3\", \"productCode\": \"4\" }}";
kk_platMsg_handle(buf);
//kk_set_tsl_by_productKey("a1OYuSallan","model.json");
//kk_mid_subdev_add("a1OYuSallan","allanWno8yDdsjCX15iq","","aabbccddeeff1122");
}
/*memset(buf, 0, 100);
sprintf(buf , "send data app time = %d\n", getSysTime());
printf("M2A: %s", buf);
kk_ipc_send(IPC_MID2APP, buf, strlen(buf)+1);
memset(buf, 0, 100);
sprintf(buf, "send data to plat time = %d\n", getSysTime());
printf("M2P: %s", buf);
kk_ipc_send(IPC_MID2PLAT, buf, strlen(buf) + 1);*/
}
}
......@@ -89,25 +89,33 @@ void emAfZigbeeKeyEstablishment(EmberEUI64 partner, EmberKeyStatus status)
// ReadAttributesResponse function declarations.
bool emberAfReadAttributesResponseCallback(EmberAfClusterId clusterId,uint8_t* buffer,uint16_t bufLen);
bool emAfReadAttributesResponse(EmberAfClusterId clusterId,uint8_t* buffer,uint16_t bufLen)
{
return emberAfReadAttributesResponseCallback(clusterId, buffer, bufLen);
}
// ReportAttributes function declarations.
bool emberAfReportAttributesCallback(EmberAfClusterId clusterId,uint8_t * buffer,uint16_t bufLen);
bool rpc_ReportAttributesCallback(EmberAfClusterId clusterId,uint8_t * buffer,uint16_t bufLen);
bool rpc_ReadAttributesResponseCallback(EmberAfClusterId clusterId,uint8_t *buffer,uint16_t bufLen);
bool emAfReportAttributes(EmberAfClusterId clusterId,uint8_t * buffer,uint16_t bufLen)
{
return emberAfReportAttributesCallback(clusterId, buffer, bufLen);
return rpc_ReportAttributesCallback(clusterId, buffer, bufLen);
//return emberAfReportAttributesCallback(clusterId, buffer, bufLen);
}
bool emAfReadAttributesResponse(EmberAfClusterId clusterId,uint8_t* buffer,uint16_t bufLen)
{
rpc_ReadAttributesResponseCallback(clusterId, buffer, bufLen);
return emberAfReadAttributesResponseCallback(clusterId, buffer, bufLen);
}
// PluginDeviceTableDeviceLeft function declarations.
void emAfPluginCommandRelayRemoveDeviceByEui64(EmberEUI64 newNodeEui64);
void emberAfPluginDeviceTableDeviceLeftCallback(EmberEUI64 newNodeEui64);
void emAfPluginDeviceTableDeviceLeftCallback(EmberEUI64 newNodeEui64)
{
emAfPluginCommandRelayRemoveDeviceByEui64(newNodeEui64);
//emAfPluginCommandRelayRemoveDeviceByEui64(newNodeEui64);
emberAfPluginDeviceTableDeviceLeftCallback(newNodeEui64);
}
......@@ -73,7 +73,7 @@ static int send_result(struct jrpc_connection * conn, cJSON * result,
}
static int invoke_procedure(struct jrpc_server *server,
struct jrpc_connection * conn, char *name, cJSON *params, cJSON *id) {
struct jrpc_connection * conn, char *name, cJSON *params, cJSON *id,cJSON * mac) {
cJSON *returned = NULL;
int procedure_found = 0;
jrpc_context ctx;
......@@ -84,7 +84,7 @@ static int invoke_procedure(struct jrpc_server *server,
if (!strcmp(server->procedures[i].name, name)) {
procedure_found = 1;
ctx.data = server->procedures[i].data;
returned = server->procedures[i].function(&ctx, params, id);
returned = server->procedures[i].function(&ctx, params, id,mac);
break;
}
}
......@@ -119,8 +119,9 @@ static int eval_request(struct jrpc_server *server,
rpc_cJSON_CreateNumber(id->valueint);
if (server->debug_level)
printf("Method Invoked[2]: %s\n", method->valuestring);
cJSON *mac =NULL;
return invoke_procedure(server, conn, method->valuestring,
params, id_copy);
params, id_copy,mac);
}
}
}
......
......@@ -37,7 +37,7 @@ typedef struct {
char * error_message;
} jrpc_context;
typedef cJSON* (*jrpc_function)(jrpc_context *context, cJSON *params, cJSON* id);
typedef cJSON* (*jrpc_function)(jrpc_context *context, cJSON *params, cJSON* id,cJSON* mac);
struct jrpc_procedure {
char * name;
......
......@@ -73,7 +73,7 @@ bool kk_rpc_report_LightStatus(EmberEUI64 mac,bool LightStatus)
cJSON *rpc_Control(jrpc_context * ctx, cJSON *params, cJSON *id)
cJSON *rpc_Control(jrpc_context * ctx, cJSON *params, cJSON *id,cJSON *mac)
{
rpc_nwk_info_s info;
EmberStatus status;
......@@ -82,18 +82,17 @@ cJSON *rpc_Control(jrpc_context * ctx, cJSON *params, cJSON *id)
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
goto error_return;
}else{
cJSON *mac_item= rpc_cJSON_GetObjectItem(params, "mac");
cJSON *LightStatus_item = rpc_cJSON_GetObjectItem(params, "LightStatus");
uint8_t LightStatus = rpc_get_u8(LightStatus_item->valuestring);
uint8_t mac[EUI64_SIZE];
bool flag= rpc_get_mac(mac_item->valuestring,mac);
emberAfCorePrintBuffer(mac,EUI64_SIZE,true);
uint8_t eui64[EUI64_SIZE];
bool flag= rpc_get_mac(mac->valuestring,eui64);
emberAfCorePrintBuffer(eui64,EUI64_SIZE,true);
for(int i=0;i<EUI64_SIZE;i++){
emberAfCorePrintln("i=%d,val=%02x",i,mac[i]);
emberAfCorePrintln("i=%d,val=%02x",i,eui64[i]);
}
EmberNodeId node = emberAfDeviceTableGetNodeIdFromEui64(mac);
EmberNodeId node = emberAfDeviceTableGetNodeIdFromEui64(eui64);
if(node==0xffff){
emberAfCorePrintln("\r\n not find device!\r\n" );
set_json_error_type(ctx,JRPC_INVALID_PARAMS,MSG_INVALID_PARAMS);
......
......@@ -11,9 +11,7 @@
#define KK_REPORT_ATTRIBUTE_METHOD "thing.service.property.report"
cJSON *rpc_Control(jrpc_context * ctx, cJSON * params, cJSON *id);
cJSON *rpc_Control(jrpc_context * ctx, cJSON * params, cJSON *id,cJSON *mac);
......
......@@ -395,7 +395,7 @@ void rpc_printfJSON(char* str,cJSON* item)
void rpc_eui64ToString(EmberEUI64 eui, char* euiString)
{
sprintf(euiString, "0x%02X%02X%02X%02X%02X%02X%02X%02X",
sprintf(euiString, "%02X%02X%02X%02X%02X%02X%02X%02X",
eui[7],
eui[6],
eui[5],
......
......@@ -89,11 +89,15 @@ static int send_result_resp(cJSON * result,
int return_value = 0;
cJSON *result_root = rpc_cJSON_CreateObject();
if (result)
rpc_cJSON_AddItemToObject(result_root, "result", result);
rpc_cJSON_AddItemToObject(result_root, "id", id);
rpc_cJSON_AddItemToObject(result_root, "code", result);
if(id){
printf("id json:\n%s\n",id->valuestring);
rpc_cJSON_AddItemToObject(result_root, "msgId", id);
}
char * str_result = rpc_cJSON_Print(result_root);
return_value = kk_ipc_send(IPC_PLAT2MID, str_result, strlen(result_root)+1);
printf("send json:\n%s\n",str_result);
return_value = kk_ipc_send(IPC_PLAT2MID, str_result, strlen(str_result)+1);
free(str_result);
rpc_cJSON_Delete(result_root);
return return_value;
......@@ -120,7 +124,7 @@ static int send_error_resp(int code, char* message,
}
static int invoke_procedure(struct jrpc_server *server,
char *name, cJSON *params, cJSON *id) {
char *name, cJSON *params, cJSON *id,cJSON *mac) {
cJSON *returned = NULL;
int procedure_found = 0;
jrpc_context ctx;
......@@ -131,7 +135,7 @@ static int invoke_procedure(struct jrpc_server *server,
if (!strcmp(server->procedures[i].name, name)) {
procedure_found = 1;
ctx.data = server->procedures[i].data;
returned = server->procedures[i].function(&ctx, params, id);
returned = server->procedures[i].function(&ctx, params, id,mac);
break;
}
}
......@@ -147,15 +151,17 @@ static int invoke_procedure(struct jrpc_server *server,
}
static int eval_request(struct jrpc_server *server, cJSON *root) {
cJSON *method, *params, *id;
cJSON *method, *params, *id,*mac;
method = rpc_cJSON_GetObjectItem(root, "method");
if (method != NULL && method->type == cJSON_String) {
params = rpc_cJSON_GetObjectItem(root, "params");
if (params == NULL|| params->type == cJSON_Array
|| params->type == cJSON_Object) {
id = rpc_cJSON_GetObjectItem(root, "id");
id = rpc_cJSON_GetObjectItem(root, "msgId");
if (id == NULL|| id->type == cJSON_String
|| id->type == cJSON_Number) {
mac = rpc_cJSON_GetObjectItem(root, "mac");
if(mac->type = cJSON_String){
//We have to copy ID because using it on the reply and deleting the response Object will also delete ID
cJSON * id_copy = NULL;
if (id != NULL)
......@@ -166,7 +172,8 @@ static int eval_request(struct jrpc_server *server, cJSON *root) {
if (server->debug_level)
printf("Method Invoked[2]: %s\n", method->valuestring);
return invoke_procedure(server, method->valuestring,
params, id_copy);
params, id_copy,mac);
}
}
}
}
......@@ -254,6 +261,11 @@ int jrpc_send_msg(cJSON * msgJson) {
char * str_result = rpc_cJSON_Print(msgJson);
emberAfAppPrintln("send json:\n%s\n",str_result);
return_value = kk_ipc_send(IPC_PLAT2MID, str_result, strlen(str_result)+1);
free(str_result);
return return_value;
......
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