Commit f7c22541 authored by 尹佳钦's avatar 尹佳钦
parents eea97e5a 0d6f9397
...@@ -31,6 +31,54 @@ static ipc_cb* g_cb = NULL; ...@@ -31,6 +31,54 @@ static ipc_cb* g_cb = NULL;
static struct ev_io w_accept; static struct ev_io w_accept;
// Save/Load the gateway list
void kk_gw_list_save(void)
{
FILE *fp;
uint8_t i;
uint8_t j;
int len = MAX_LISTEN_NUM * sizeof(kk_tcp_ctrl_t);
// Save device table
fp = fopen("gwlist.txt", "w");
fwrite(g_tcp_ctrl,len,1,fp);
fclose(fp);
}
void kk_gw_list_load(void)
{
uint16_t i;
FILE *fp;
int readLen = MAX_LISTEN_NUM * sizeof(kk_tcp_ctrl_t);
int retLen = 0;
fp = fopen("gwlist.txt", "r");
if (!fp) {
printf("open gatewaylist.txt failed! \n");
goto error1;
}
char buf[1024] = {0};
retLen = fread(g_tcp_ctrl, readLen, 1, fp);
printf("read gatewaylist.txt retLen= %d, readLen=%d ! \n", retLen,readLen);
if (retLen != readLen ){
printf("read gatewaylist.txt failed! \n");
}
fclose(fp);
// Set the rest of the device table to null.
error1:
for (i=0; i < MAX_LISTEN_NUM; i++) {
printf("deviceCode ip sock [%s] [%s] [%d] \n",g_tcp_ctrl[i].deviceCode,
g_tcp_ctrl[i].ip==NULL?"":g_tcp_ctrl[i].ip, g_tcp_ctrl[i].sock);
g_tcp_ctrl[i].sock = -1;
}
}
static int get_idx_by_ip(char ip[MAX_IP_LEN]){ static int get_idx_by_ip(char ip[MAX_IP_LEN]){
int i = 0; int i = 0;
...@@ -162,6 +210,7 @@ int kk_set_tcp_channel(char devCode[DEVICE_CODE_LEN], char ip[MAX_IP_LEN]){ ...@@ -162,6 +210,7 @@ int kk_set_tcp_channel(char devCode[DEVICE_CODE_LEN], char ip[MAX_IP_LEN]){
if(strcmp(devCode, g_tcp_ctrl[i].deviceCode) == 0){ if(strcmp(devCode, g_tcp_ctrl[i].deviceCode) == 0){
strncpy(g_tcp_ctrl[i].ip, ip, strlen(ip)); strncpy(g_tcp_ctrl[i].ip, ip, strlen(ip));
printf("find and replace it [%d][%s][%s] \n",i, g_tcp_ctrl[i].ip, devCode); printf("find and replace it [%d][%s][%s] \n",i, g_tcp_ctrl[i].ip, devCode);
kk_gw_list_save();
break; break;
} }
...@@ -179,6 +228,7 @@ int kk_set_tcp_channel(char devCode[DEVICE_CODE_LEN], char ip[MAX_IP_LEN]){ ...@@ -179,6 +228,7 @@ int kk_set_tcp_channel(char devCode[DEVICE_CODE_LEN], char ip[MAX_IP_LEN]){
strncpy(g_tcp_ctrl[isEmptyIdx].ip, ip, strlen(ip)); strncpy(g_tcp_ctrl[isEmptyIdx].ip, ip, strlen(ip));
strncpy(g_tcp_ctrl[isEmptyIdx].deviceCode, devCode, strlen(devCode)); strncpy(g_tcp_ctrl[isEmptyIdx].deviceCode, devCode, strlen(devCode));
printf("idx deviceCode ip[%d][%s][%s]",isEmptyIdx, g_tcp_ctrl[isEmptyIdx].deviceCode, g_tcp_ctrl[isEmptyIdx].ip); printf("idx deviceCode ip[%d][%s][%s]",isEmptyIdx, g_tcp_ctrl[isEmptyIdx].deviceCode, g_tcp_ctrl[isEmptyIdx].ip);
kk_gw_list_save();
return 0; return 0;
} }
...@@ -384,9 +434,8 @@ int kk_TCP_channel_init(ipc_cb cb) ...@@ -384,9 +434,8 @@ int kk_TCP_channel_init(ipc_cb cb)
} }
g_init = 1; g_init = 1;
memset(g_tcp_ctrl, 0, sizeof(kk_tcp_ctrl_t)*MAX_LISTEN_NUM); memset(g_tcp_ctrl, 0, sizeof(kk_tcp_ctrl_t)*MAX_LISTEN_NUM);
for(i = 0; i < MAX_LISTEN_NUM; i++){ kk_gw_list_load();
g_tcp_ctrl[i].sock = -1;
}
if (g_pTh ==NULL && 0 != pthread_create(&g_pTh, NULL, loop_tcp_thread, NULL)) { if (g_pTh ==NULL && 0 != pthread_create(&g_pTh, NULL, loop_tcp_thread, NULL)) {
printf("create pthread failed\r\n"); printf("create pthread failed\r\n");
...@@ -479,6 +528,9 @@ void _MutexUnlock(void *mutex) ...@@ -479,6 +528,9 @@ void _MutexUnlock(void *mutex)
typedef struct { typedef struct {
void *mutex; void *mutex;
int sd; int sd;
int isConnect;
char ip[MAX_IP_LEN];
int port;
ipc_cb* cb; ipc_cb* cb;
} kk_tcp_client_t; } kk_tcp_client_t;
...@@ -495,29 +547,7 @@ static int _init_client(){ ...@@ -495,29 +547,7 @@ static int _init_client(){
g_client_ctrl.sd = -1; g_client_ctrl.sd = -1;
} }
static void loop_tcp_client_thread(void *arg){
printf("loop_tcp_client_thread start!\r\n");
char buf[1024]= {0};
int ret = 0;
while(1){
_MutexLock(g_client_ctrl.mutex);
ret = read(g_client_ctrl.sd, buf, sizeof(buf));
_MutexUnlock(g_client_ctrl.mutex);
if(-1== ret){
//printf("=================read error \n");
//break ;
}else if(ret > 0){
printf("buf = %s\n",buf);
if (g_client_ctrl.cb != NULL){
g_client_ctrl.cb(buf,ret,"");
}
}
usleep(100000);
}
printf("loop_tcp_client_thread================== end \n");
}
static int client_socket_init(int *sd, char *ipaddr, uint16_t port) static int client_socket_init(int *sd, char *ipaddr, uint16_t port)
{ {
...@@ -570,14 +600,57 @@ err1: ...@@ -570,14 +600,57 @@ err1:
return -1; return -1;
} }
static void loop_tcp_client_thread(void *arg){
printf("loop_tcp_client_thread start!\r\n");
char buf[1024]= {0};
int ret = 0;
while(1){
if(-1 == client_socket_init(&g_client_ctrl.sd,g_client_ctrl.ip, g_client_ctrl.port)){
printf("connect failed \n");
sleep(1);
continue;
}
g_client_ctrl.isConnect = 1;
while(g_client_ctrl.isConnect){
_MutexLock(g_client_ctrl.mutex);
ret = read(g_client_ctrl.sd, buf, sizeof(buf));
_MutexUnlock(g_client_ctrl.mutex);
if(-1== ret){
//printf("=================read error \n");
//break ;
}else if(ret > 0){
printf("buf = %s\n",buf);
if (g_client_ctrl.cb != NULL){
g_client_ctrl.cb(buf,ret,"");
}
}
usleep(100000);
}
printf("network error, try connect again! \n");
close(g_client_ctrl.sd);
}
printf("loop_tcp_client_thread================== end \n");
}
int kk_tcp_client_send(char* data, int len){ int kk_tcp_client_send(char* data, int len){
int ret = 0; int ret = 0;
if (g_client_ctrl.sd > -1 && data != NULL){ int cnt = 0;
if ( data != NULL){
while(g_client_ctrl.sd == -1 && cnt < 5){
printf("[%s] tcp don't connect, sleep 1s !!!! \n",__FUNCTION__);
sleep(1);
cnt++;
}
_MutexLock(g_client_ctrl.mutex); _MutexLock(g_client_ctrl.mutex);
ret = write(g_client_ctrl.sd, data, len); ret = write(g_client_ctrl.sd, data, len);
_MutexUnlock(g_client_ctrl.mutex); _MutexUnlock(g_client_ctrl.mutex);
if (ret < 0){ if (ret < 0){
printf("[%s] write failed!!!! \n",__FUNCTION__); printf("[%s] write failed ret=%d, reconnect !!!! \n",__FUNCTION__, ret);
g_client_ctrl.isConnect = 0;
} }
} }
} }
...@@ -585,11 +658,8 @@ int kk_tcp_client_init(char ip[MAX_IP_LEN], int port, ipc_cb cb) ...@@ -585,11 +658,8 @@ int kk_tcp_client_init(char ip[MAX_IP_LEN], int port, ipc_cb cb)
{ {
kk_tcp_client_deinit(); kk_tcp_client_deinit();
_init_client(); _init_client();
if(-1 == client_socket_init(&g_client_ctrl.sd,ip, port)){ g_client_ctrl.port = port;
printf("connect failed \n"); memcpy(g_client_ctrl.ip, ip, strlen(ip));
return -1;
}
if (g_pTh ==NULL && 0 != pthread_create(&g_pTh, NULL, loop_tcp_client_thread, NULL)) { if (g_pTh ==NULL && 0 != pthread_create(&g_pTh, NULL, loop_tcp_client_thread, NULL)) {
printf("create pthread failed\r\n"); printf("create pthread failed\r\n");
return -1; return -1;
......
...@@ -381,6 +381,8 @@ void *udp_dispatch_yield(void *args){ ...@@ -381,6 +381,8 @@ void *udp_dispatch_yield(void *args){
char device_code[DEVICE_CODE_LEN] = {0}; char device_code[DEVICE_CODE_LEN] = {0};
int devId = 0; int devId = 0;
kk_TCP_channel_init(gw2mid_cb);
while(1) while(1)
{ {
...@@ -437,7 +439,7 @@ void *udp_dispatch_yield(void *args){ ...@@ -437,7 +439,7 @@ void *udp_dispatch_yield(void *args){
DEBUG_PRINT("gwDevCode =%s proto=%s \n",gwDevCode,proto); DEBUG_PRINT("gwDevCode =%s proto=%s \n",gwDevCode,proto);
if(strcmp(proto,"tcp") == 0){ if(strcmp(proto,"tcp") == 0){
// //
kk_TCP_channel_init(gw2mid_cb);
kk_set_tcp_channel(gwDevCode,inet_ntoa(from.sin_addr)); kk_set_tcp_channel(gwDevCode,inet_ntoa(from.sin_addr));
} }
......
...@@ -217,7 +217,7 @@ static int eval_request(struct jrpc_server *server, cJSON *root) { ...@@ -217,7 +217,7 @@ static int eval_request(struct jrpc_server *server, cJSON *root) {
void _cb(void* data){ void _cb(void* data){
if (data != NULL){ if (data != NULL){
printf("plat2mid_cb: %s RECEIVED \r\n", data); //printf("plat2mid_cb: %s RECEIVED \r\n", data);
cJSON *root; cJSON *root;
char *end_ptr = NULL; char *end_ptr = NULL;
...@@ -424,6 +424,39 @@ int search_ccu(char devcode[33], char ip[16], int* port){ ...@@ -424,6 +424,39 @@ int search_ccu(char devcode[33], char ip[16], int* port){
} }
#define GW_PRODUCT_CODE "2"
#define GW_MAC GW_DEVICE_CODE
void* _msg_topo_add(){
char msgFmt[] = "{\"info\":{\"msgtype\":\"/thing/topo/add\",\"productCode\":\"%s\",\"deviceCode\":\"%s\"},\
\"payload\":{\"msgId\":\"%d\",\"version\":\"1.0\",\"params\":{\"deviceCode\":\"%s\",\"productCode\":\"%s\",\"mac\":\"%s\"}}}";
char msg[520] = {0};
sprintf(msg, msgFmt, GW_PRODUCT_CODE, GW_DEVICE_CODE, 1, GW_DEVICE_CODE, GW_PRODUCT_CODE,GW_MAC);
cJSON* msgObj = cJSON_Parse(msg);
char* outbuf = cJSON_Print(msgObj);
cJSON_Delete(msgObj);
return outbuf;
}
void* _msg_event_property_post(char ip[16], int port){
char msgFmt[] = "{\"info\":{\"msgtype\":\"/thing/event/property/post\",\"productCode\":\"%s\",\"deviceCode\":\"%s\"},\
\"payload\":{\"msgId\":\"%d\",\"version\":\"1.0\",\"params\":{\"NetChannelState\":%d,\"WhiteListState\":%d,\
\"OnlineDetectionState\":%d,\"SN\":\"%s\",\"IPAddress\":\"%s\",\"MACAddress\":\"%s\",\"Port\":%d},\
\"time\":1524448722000,\"method\":\"thing.event.property.post\"}\
}";
char msg[620] = {0};
sprintf(msg, msgFmt, GW_PRODUCT_CODE, GW_DEVICE_CODE,
1, 0, 0, 0, "12345", ip,GW_MAC,port);
cJSON* msgObj = cJSON_Parse(msg);
char* outbuf = cJSON_Print(msgObj);
cJSON_Delete(msgObj);
return outbuf;
}
void ipcHandle(void) void ipcHandle(void)
{ {
char deviceCode[33] = {0}; char deviceCode[33] = {0};
...@@ -445,23 +478,23 @@ void ipcHandle(void) ...@@ -445,23 +478,23 @@ void ipcHandle(void)
} }
//send add gw to ccu //send add gw to ccu
char info[] = "{\"msgtype\":\"/thing/topo/add\",\"productType\":\"gw\",\"productCode\":\"2\",\"deviceCode\":\"1122334455667788\"}"; char* outbuf = _msg_topo_add();
char payload[] = "{\"msgId\":\"1\",\"version\":\"1.0\",\"params\":{\"deviceCode\":\"1122334455667788\",\"productCode\":\"2\",\"mac\":\"1122334455667788\"}}";
cJSON* root = cJSON_CreateObject();
cJSON* infoObj = cJSON_Parse(info);
cJSON* payloadObj = cJSON_Parse(payload);
cJSON_AddItemToObject(root, "info", infoObj);
cJSON_AddItemToObject(root, "payload",payloadObj);
char* outbuf = cJSON_Print(root);
cJSON_Delete(root);
kk_sendData2CCU(outbuf, strlen(outbuf)); kk_sendData2CCU(outbuf, strlen(outbuf));
//kk_sendData2CCU("allan test", strlen("allan test"));
free(outbuf); free(outbuf);
int cnt = 0;
//handle procidure //handle procidure
while(1){ while(1){
// //
usleep(20000); usleep(20000);
cnt++;
if (cnt == 2){
sleep(1);
char* postmsg = _msg_event_property_post(ip,port);
kk_sendData2CCU(postmsg, strlen(postmsg));
free(postmsg);
}
} }
//jrpc_server_run(&my_server); //jrpc_server_run(&my_server);
......
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