Commit 34e7c939 authored by 黄振令's avatar 黄振令

【修改内容】1. 线程启动放在gw获取mac后,确保能有mac;2. gw发现用自己的mac地址(不写死),如果mac失败推出线程

【提交人】huang.zhenling
parent ecde4c19
...@@ -266,7 +266,21 @@ int _init_param(struct jrpc_server *server) { ...@@ -266,7 +266,21 @@ int _init_param(struct jrpc_server *server) {
return 0; return 0;
} }
char g_mac[19] = {0};
char* kk_get_gw_mac(){
int cnt = 0;
EmberEUI64 eui64;
emberAfGetEui64(eui64);
while(eui64[0] ==0 && eui64[1] ==0 && eui64[2] == 0 && cnt++ < 3){
sleep(1);
}
if (eui64[0] ==0 && eui64[1] ==0 && eui64[2] == 0){
printf("get gw mac error !!! \n");
return NULL;
}
rpc_eui64ToString(eui64,g_mac);
return g_mac;
}
int search_ccu(char devcode[33], char ip[16], int* port){ int search_ccu(char devcode[33], char ip[16], int* port){
...@@ -283,7 +297,13 @@ int search_ccu(char devcode[33], char ip[16], int* port){ ...@@ -283,7 +297,13 @@ int search_ccu(char devcode[33], char ip[16], int* port){
struct sockaddr_in Addrto; struct sockaddr_in Addrto;
struct sockaddr_in AddrRev; struct sockaddr_in AddrRev;
sprintf(sendMessage,sendCmdFmt,GW_DEVICE_CODE,GW2CCU_PROTOCOL); char* macString = kk_get_gw_mac();
if (macString == NULL){
printf("[%s] get mac fail\n",__FUNCTION__);
return -1;
}
sprintf(sendMessage,sendCmdFmt,macString/*GW_DEVICE_CODE*/,GW2CCU_PROTOCOL);
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{ {
printf("[%s] socket fail\n",__FUNCTION__); printf("[%s] socket fail\n",__FUNCTION__);
...@@ -431,7 +451,12 @@ void* _msg_topo_add(){ ...@@ -431,7 +451,12 @@ void* _msg_topo_add(){
char msgFmt[] = "{\"info\":{\"msgtype\":\"/thing/topo/add\",\"productCode\":\"%s\",\"deviceCode\":\"%s\"},\ char msgFmt[] = "{\"info\":{\"msgtype\":\"/thing/topo/add\",\"productCode\":\"%s\",\"deviceCode\":\"%s\"},\
\"payload\":{\"msgId\":\"%d\",\"version\":\"1.0\",\"params\":{\"deviceCode\":\"%s\",\"productCode\":\"%s\",\"mac\":\"%s\"}}}"; \"payload\":{\"msgId\":\"%d\",\"version\":\"1.0\",\"params\":{\"deviceCode\":\"%s\",\"productCode\":\"%s\",\"mac\":\"%s\"}}}";
char msg[520] = {0}; char msg[520] = {0};
sprintf(msg, msgFmt, GW_PRODUCT_CODE, GW_DEVICE_CODE, 1, GW_DEVICE_CODE, GW_PRODUCT_CODE,GW_MAC); char* macString = kk_get_gw_mac();
if (macString == NULL){
printf("[%s] get mac fail\n",__FUNCTION__);
return NULL;
}
sprintf(msg, msgFmt, GW_PRODUCT_CODE, macString, 1, macString/*GW_DEVICE_CODE*/, GW_PRODUCT_CODE,macString);
cJSON* msgObj = cJSON_Parse(msg); cJSON* msgObj = cJSON_Parse(msg);
char* outbuf = cJSON_Print(msgObj); char* outbuf = cJSON_Print(msgObj);
cJSON_Delete(msgObj); cJSON_Delete(msgObj);
...@@ -447,8 +472,14 @@ void* _msg_event_property_post(char ip[16], int port){ ...@@ -447,8 +472,14 @@ void* _msg_event_property_post(char ip[16], int port){
\"time\":1524448722000,\"method\":\"thing.event.property.post\"}\ \"time\":1524448722000,\"method\":\"thing.event.property.post\"}\
}"; }";
char msg[620] = {0}; char msg[620] = {0};
sprintf(msg, msgFmt, GW_PRODUCT_CODE, GW_DEVICE_CODE, char* macString = kk_get_gw_mac();
1, 0, 0, 0, "12345", ip,GW_MAC,port); if (macString == NULL){
printf("[%s] get mac fail\n",__FUNCTION__);
return NULL;
}
sprintf(msg, msgFmt, GW_PRODUCT_CODE, macString/*GW_DEVICE_CODE*/,
1, 0, 0, 0, "12345", ip,macString, port);
cJSON* msgObj = cJSON_Parse(msg); cJSON* msgObj = cJSON_Parse(msg);
char* outbuf = cJSON_Print(msgObj); char* outbuf = cJSON_Print(msgObj);
cJSON_Delete(msgObj); cJSON_Delete(msgObj);
...@@ -461,14 +492,19 @@ void ipcHandle(void) ...@@ -461,14 +492,19 @@ void ipcHandle(void)
{ {
char deviceCode[33] = {0}; char deviceCode[33] = {0};
char ip[16] = {0}; char ip[16] = {0};
int port = 0; int port = 0;
emberAfAppPrint( "Thread rpc Interface Parse create\n" ); emberAfAppPrint( "Thread rpc Interface Parse create\n" );
search_ccu(deviceCode, ip, &port); search_ccu(deviceCode, ip, &port);
char* macString = kk_get_gw_mac();
if (macString == NULL){
printf("[%s] get mac fail, exit pthread !!!!!!!!!!!!!!!!!\n",__FUNCTION__);
return;
}
_init_param(&my_server); _init_param(&my_server);
if(strcmp(GW2CCU_PROTOCOL, "tcp") == 0){ if(strcmp(GW2CCU_PROTOCOL, "tcp") == 0){
kk_tcp_client_init(ip, port, _cb); kk_tcp_client_init(ip, port, _cb);
}else{ }else{
kk_ipc_init(IPC_PLAT2MID, _cb, GW_DEVICE_CODE, ip); kk_ipc_init(IPC_PLAT2MID, _cb, macString/*GW_DEVICE_CODE*/, ip);
} }
emberAfAppPrint("sizeof(rpc_table)=%d,sizeof(rpc_table_s)=%d,%d\n",sizeof(rpc_table),sizeof(rpc_table_s),sizeof(rpc_table)/sizeof(rpc_table_s)); emberAfAppPrint("sizeof(rpc_table)=%d,sizeof(rpc_table_s)=%d,%d\n",sizeof(rpc_table),sizeof(rpc_table_s),sizeof(rpc_table)/sizeof(rpc_table_s));
...@@ -479,6 +515,11 @@ void ipcHandle(void) ...@@ -479,6 +515,11 @@ void ipcHandle(void)
//send add gw to ccu //send add gw to ccu
char* outbuf = _msg_topo_add(); char* outbuf = _msg_topo_add();
if (outbuf == NULL){
printf("[%s] topo add msg failed, exit\n",__FUNCTION__);
return;
}
if (strcmp(GW2CCU_PROTOCOL, "tcp") != 0){ if (strcmp(GW2CCU_PROTOCOL, "tcp") != 0){
printf("check nanomsg is connect(%d) \n", kk_ipc_isconnect(IPC_PLAT2MID)); printf("check nanomsg is connect(%d) \n", kk_ipc_isconnect(IPC_PLAT2MID));
} }
...@@ -494,6 +535,11 @@ void ipcHandle(void) ...@@ -494,6 +535,11 @@ void ipcHandle(void)
if (cnt == 2){ if (cnt == 2){
sleep(1); sleep(1);
char* postmsg = _msg_event_property_post(ip,port); char* postmsg = _msg_event_property_post(ip,port);
if (outbuf == NULL){
printf("[%s] property_post msg failed\n",__FUNCTION__);
continue;
}
kk_sendData2CCU(postmsg, strlen(postmsg)); kk_sendData2CCU(postmsg, strlen(postmsg));
free(postmsg); free(postmsg);
} }
......
...@@ -516,10 +516,7 @@ int emberAfMain(MAIN_FUNCTION_PARAMETERS) ...@@ -516,10 +516,7 @@ int emberAfMain(MAIN_FUNCTION_PARAMETERS)
return -1; return -1;
} }
pthread_t tid;
//pthread_create(&tid, NULL, rpcInterfaceParse, NULL);
pthread_create(&tid, NULL, ipcHandle, NULL);
/*int pid=fork(); /*int pid=fork();
...@@ -546,6 +543,12 @@ int emberAfMain(MAIN_FUNCTION_PARAMETERS) ...@@ -546,6 +543,12 @@ int emberAfMain(MAIN_FUNCTION_PARAMETERS)
emberAfCorePrintln("~~~~~~~~~~~~~~~~~~~~~NCP MAC:"); emberAfCorePrintln("~~~~~~~~~~~~~~~~~~~~~NCP MAC:");
emberAfPrintBigEndianEui64(eui64); emberAfPrintBigEndianEui64(eui64);
pthread_t tid;
//pthread_create(&tid, NULL, rpcInterfaceParse, NULL);
pthread_create(&tid, NULL, ipcHandle, NULL);
// initialize the ZCL framework ,(plug in) ,mqtt init is here // initialize the ZCL framework ,(plug in) ,mqtt init is here
emAfInit(); emAfInit();
printf("[%s][%d]\n",__FUNCTION__,__LINE__); printf("[%s][%d]\n",__FUNCTION__,__LINE__);
......
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