|
|
@@ -1,253 +1,264 @@
|
|
|
-#include "fcntl.h"
|
|
|
-#include <stdio.h>
|
|
|
-#include <stdlib.h>
|
|
|
-#include <string.h>
|
|
|
-#include <unistd.h>
|
|
|
-#include "breaker_detection.h"
|
|
|
-#include "modbus_handle.h"
|
|
|
-#include "common.h"
|
|
|
-#include "thread.h"
|
|
|
-#include "string.h"
|
|
|
-
|
|
|
-static breaker_scanner_t breaker_scanner;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-int breaker_init(int gpio)
|
|
|
-{
|
|
|
- char buff[1024] = {0};
|
|
|
- sprintf(buff,"/sys/class/gpio/gpio%d",gpio);
|
|
|
- //open the file
|
|
|
- int fd = open(buff,O_RDONLY);
|
|
|
- if(fd < 0)
|
|
|
- {
|
|
|
- printf("Failed to open gpio %d and create!!!!\n",gpio);
|
|
|
- sprintf(buff,"%s","/sys/class/gpio/export");
|
|
|
- fd = open(buff,O_WRONLY);
|
|
|
- if(fd < 0)
|
|
|
- {
|
|
|
- printf("Failed to open export file and create!!!!\n");
|
|
|
- goto err_1;
|
|
|
- }
|
|
|
- sprintf(buff,"%d",gpio);
|
|
|
- write(fd,buff,strlen(buff));
|
|
|
- close(fd);
|
|
|
- fd = 0;
|
|
|
- printf("determin io input deriction!!!");
|
|
|
-
|
|
|
- sprintf(buff,"/sys/class/gpio/gpio%d/direction",gpio);
|
|
|
- fd = open(buff,O_WRONLY);
|
|
|
- if(fd < 0)
|
|
|
- {
|
|
|
- printf("Failed to open gpio %d and create!!!!\n",gpio);
|
|
|
- goto err_2;
|
|
|
- }
|
|
|
- write(fd,"in",2);
|
|
|
- }
|
|
|
- close(fd);
|
|
|
-
|
|
|
- return 0;
|
|
|
-
|
|
|
-err_2:
|
|
|
- {
|
|
|
- sprintf(buff,"%s","/sys/class/gpio/unexport");
|
|
|
- fd = open(buff,O_WRONLY);
|
|
|
- sprintf(buff,"%d",gpio);
|
|
|
- write(fd,buff,strlen(buff));
|
|
|
- close(fd);
|
|
|
- }
|
|
|
-err_1:
|
|
|
- return -1;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-int breaker_read_status(int gpio)
|
|
|
-{
|
|
|
- char value_path[1024] = {0};
|
|
|
- char buf[3]; // 用于存储读取到的GPIO值,通常为'0'或'1'
|
|
|
- ssize_t bytesRead;
|
|
|
- int fd;
|
|
|
- // 构造GPIO值的路径
|
|
|
- sprintf(value_path, "/sys/class/gpio/gpio%d/value", gpio);
|
|
|
-
|
|
|
- // 打开GPIO值文件以读取
|
|
|
- fd = open(value_path, O_RDONLY);
|
|
|
- if (fd == -1) {
|
|
|
- perror("Failed to open GPIO value for reading");
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- // 读取GPIO的值
|
|
|
- bytesRead = read(fd, buf, sizeof(buf) - 1);
|
|
|
- if (bytesRead == -1) {
|
|
|
- perror("Failed to read from GPIO value");
|
|
|
- close(fd);
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- // 确保字符串以null终止
|
|
|
- buf[bytesRead] = '\0';
|
|
|
- close(fd);
|
|
|
- // 打印读取到的GPIO值
|
|
|
- // printf("GPIO %d value: %s\n", GPIO_PE3, buf);
|
|
|
- if (buf[0]=='0')
|
|
|
- {
|
|
|
- return 0;
|
|
|
- }else{
|
|
|
- return 1;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
-int breaker_deinit(int gpio)
|
|
|
-{
|
|
|
- char buff[1024] = {0};
|
|
|
- int fd = 0;
|
|
|
- sprintf(buff,"%s","/sys/class/gpio/unexport");
|
|
|
- fd = open(buff,O_WRONLY);
|
|
|
- sprintf(buff,"%d",gpio);
|
|
|
- write(fd,buff,strlen(buff));
|
|
|
- close(fd);
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-int breaker_485_get_chn_status(void* manger,int saddr,int channel,unsigned short *statue)
|
|
|
-{
|
|
|
- int ret = 0 ;
|
|
|
- unsigned short data_temp = 0;
|
|
|
- ret = g_modbus_read_reg(manger,saddr,channel,&data_temp);
|
|
|
- if(ret<=0)
|
|
|
- {
|
|
|
- printf("erriiiirrr!!!\n");
|
|
|
- return -1 ;
|
|
|
- }
|
|
|
-
|
|
|
- if(data_temp)
|
|
|
- {
|
|
|
- *statue = 0;
|
|
|
- }else
|
|
|
- {
|
|
|
- *statue = 1;
|
|
|
- }
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int breaker_485_get_all_chn_status(void* manger,int saddr,unsigned short *status)
|
|
|
-{
|
|
|
- int ret = 0 ;
|
|
|
- unsigned short data_temp[6] = {0};
|
|
|
- ret = g_modbus_read_x_reg(manger,saddr,BREADER_485_ADDR_CHN_REG+1,BREAKER_485_CHN_MAX,data_temp);
|
|
|
- for(int i =0;i < BREAKER_485_CHN_MAX;i++)
|
|
|
- {
|
|
|
- if(data_temp[i])
|
|
|
- {
|
|
|
- status[i] = 0;
|
|
|
- }else
|
|
|
- {
|
|
|
- status[i] = 1;
|
|
|
- }
|
|
|
- }
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int breaker_485_get_chns(void* manger,int saddr,unsigned short* cnt)
|
|
|
-{
|
|
|
- int ret = 0 ;
|
|
|
- unsigned short data_temp = 0;
|
|
|
- ret = g_modbus_read_x_reg(manger,saddr,BREADER_485_ADDR_CHN_REG,1,&data_temp);
|
|
|
- if(ret<=0)
|
|
|
- return -1 ;
|
|
|
-
|
|
|
- *cnt = data_temp & 0x00ff;
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void* breaker_scanner_thread(void* argv)
|
|
|
-{
|
|
|
-
|
|
|
- //GlobalDeviceManager* gm= (GlobalDeviceManager*)(argv);
|
|
|
- thread_handle_t *h=(thread_handle_t*)(argv);
|
|
|
- GlobalDeviceManager* gm= (GlobalDeviceManager*)(h->arg);
|
|
|
- memset(&breaker_scanner,0,sizeof(breaker_scanner));
|
|
|
- int ret = 0;
|
|
|
- uint16_t value =0;
|
|
|
- uint16_t status[BREAKER_485_CHN_MAX] = {0};
|
|
|
- //scanner all boadr
|
|
|
- for(int i = 0; i < BREAKER_BOARD_MAX; i++)
|
|
|
- {
|
|
|
- ret = breaker_485_get_chn_status((void*)&gm->_globalRelaySampManger,
|
|
|
- BREADER_485_ADDR+i,BREADER_485_ADDR_CHN_REG,&value);
|
|
|
- if(ret == 0)
|
|
|
- {
|
|
|
- breaker_scanner.breaker_flag[i] = 1;
|
|
|
- }else
|
|
|
- {
|
|
|
- breaker_scanner.breaker_flag[i] = 0;
|
|
|
- }
|
|
|
- }
|
|
|
- int count = 0;
|
|
|
-
|
|
|
- while(h->quit==0)
|
|
|
- {
|
|
|
- if((count % 10) == 1)
|
|
|
- {
|
|
|
- for(int i = 0; i < BREAKER_BOARD_MAX; i++)
|
|
|
- {
|
|
|
- ret = breaker_485_get_chn_status((void*)&gm->_globalRelaySampManger,
|
|
|
- BREADER_485_ADDR+i,BREADER_485_ADDR_CHN_REG,&value);
|
|
|
- if(ret == 0)
|
|
|
- {
|
|
|
- breaker_scanner.breaker_flag[i] = 1;
|
|
|
- }else
|
|
|
- {
|
|
|
- breaker_scanner.breaker_flag[i] = 0;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for(int i = 0; i < BREAKER_BOARD_MAX; i++)
|
|
|
- {
|
|
|
- if(breaker_scanner.breaker_flag[i])
|
|
|
- {
|
|
|
-
|
|
|
- breaker_485_get_all_chn_status((void*)&gm->_globalRelaySampManger,BREADER_485_ADDR+i,status);
|
|
|
- pthread_mutex_lock(&gm->_breaker_mutex);
|
|
|
- memcpy(breaker_scanner.board[i].ch_status,status,sizeof(status));
|
|
|
- pthread_mutex_unlock(&gm->_breaker_mutex);
|
|
|
- // ret = breaker_485_get_chn_status((void*)&gm->_globalRelaySampManger,
|
|
|
- // BREADER_485_ADDR+i,BREADER_485_ADDR_CHN_REG,&value);
|
|
|
- // //pthread_mutex_lock(&gm->_breaker_mutex);
|
|
|
- // if(ret == 0)
|
|
|
- // {
|
|
|
- // breaker_scanner.breaker_flag[i] = 1; // board is online
|
|
|
- // //breaker_485_get_all_chn_status((void*)&gm->_globalRelaySampManger,BREADER_485_ADDR+i,status);
|
|
|
- // //memcmp(breaker_scanner.board[i].scanner_info.ch_status,status,sizeof(status));
|
|
|
- // }else
|
|
|
- // {
|
|
|
- // breaker_scanner.breaker_flag[i] = 0; //board is offline
|
|
|
- // }
|
|
|
-
|
|
|
-
|
|
|
- // // pthread_mutex_unlock(&gm->_breaker_mutex);
|
|
|
- memset(status,0,sizeof(status));
|
|
|
- }else
|
|
|
- {
|
|
|
- memset(status,0,sizeof(status));
|
|
|
- pthread_mutex_lock(&gm->_breaker_mutex);
|
|
|
- memcpy(breaker_scanner.board[i].ch_status,status,sizeof(status));
|
|
|
- pthread_mutex_unlock(&gm->_breaker_mutex);
|
|
|
- }
|
|
|
- }
|
|
|
- count ++;
|
|
|
- sleep(1);
|
|
|
- }
|
|
|
-
|
|
|
- return NULL;
|
|
|
-}
|
|
|
-
|
|
|
-breaker_scanner_t *get_breaker_scanner(void)
|
|
|
-{
|
|
|
- return &breaker_scanner;
|
|
|
+#include "fcntl.h"
|
|
|
+#include <stdio.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <string.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include "breaker_detection.h"
|
|
|
+#include "modbus_handle.h"
|
|
|
+#include "common.h"
|
|
|
+#include "thread.h"
|
|
|
+#include "string.h"
|
|
|
+
|
|
|
+static breaker_scanner_t breaker_scanner;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int breaker_init(int gpio)
|
|
|
+{
|
|
|
+ char buff[1024] = {0};
|
|
|
+ sprintf(buff,"/sys/class/gpio/gpio%d",gpio);
|
|
|
+ //open the file
|
|
|
+ int fd = open(buff,O_RDONLY);
|
|
|
+ if(fd < 0)
|
|
|
+ {
|
|
|
+ printf("Failed to open gpio %d and create!!!!\n",gpio);
|
|
|
+ sprintf(buff,"%s","/sys/class/gpio/export");
|
|
|
+ fd = open(buff,O_WRONLY);
|
|
|
+ if(fd < 0)
|
|
|
+ {
|
|
|
+ printf("Failed to open export file and create!!!!\n");
|
|
|
+ goto err_1;
|
|
|
+ }
|
|
|
+ sprintf(buff,"%d",gpio);
|
|
|
+ write(fd,buff,strlen(buff));
|
|
|
+ close(fd);
|
|
|
+ fd = 0;
|
|
|
+ printf("determin io input deriction!!!");
|
|
|
+
|
|
|
+ sprintf(buff,"/sys/class/gpio/gpio%d/direction",gpio);
|
|
|
+ fd = open(buff,O_WRONLY);
|
|
|
+ if(fd < 0)
|
|
|
+ {
|
|
|
+ printf("Failed to open gpio %d and create!!!!\n",gpio);
|
|
|
+ goto err_2;
|
|
|
+ }
|
|
|
+ write(fd,"in",2);
|
|
|
+ }
|
|
|
+ close(fd);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+
|
|
|
+err_2:
|
|
|
+ {
|
|
|
+ sprintf(buff,"%s","/sys/class/gpio/unexport");
|
|
|
+ fd = open(buff,O_WRONLY);
|
|
|
+ sprintf(buff,"%d",gpio);
|
|
|
+ write(fd,buff,strlen(buff));
|
|
|
+ close(fd);
|
|
|
+ }
|
|
|
+err_1:
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int breaker_read_status(int gpio)
|
|
|
+{
|
|
|
+ char value_path[1024] = {0};
|
|
|
+ char buf[3]; // 用于存储读取到的GPIO值,通常为'0'或'1'
|
|
|
+ ssize_t bytesRead;
|
|
|
+ int fd;
|
|
|
+ // 构造GPIO值的路径
|
|
|
+ sprintf(value_path, "/sys/class/gpio/gpio%d/value", gpio);
|
|
|
+
|
|
|
+ // 打开GPIO值文件以读取
|
|
|
+ fd = open(value_path, O_RDONLY);
|
|
|
+ if (fd == -1) {
|
|
|
+ perror("Failed to open GPIO value for reading");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取GPIO的值
|
|
|
+ bytesRead = read(fd, buf, sizeof(buf) - 1);
|
|
|
+ if (bytesRead == -1) {
|
|
|
+ perror("Failed to read from GPIO value");
|
|
|
+ close(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保字符串以null终止
|
|
|
+ buf[bytesRead] = '\0';
|
|
|
+ close(fd);
|
|
|
+ // 打印读取到的GPIO值
|
|
|
+ // printf("GPIO %d value: %s\n", GPIO_PE3, buf);
|
|
|
+ if (buf[0]=='0')
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }else{
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+int breaker_deinit(int gpio)
|
|
|
+{
|
|
|
+ char buff[1024] = {0};
|
|
|
+ int fd = 0;
|
|
|
+ sprintf(buff,"%s","/sys/class/gpio/unexport");
|
|
|
+ fd = open(buff,O_WRONLY);
|
|
|
+ sprintf(buff,"%d",gpio);
|
|
|
+ write(fd,buff,strlen(buff));
|
|
|
+ close(fd);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int breaker_485_get_chn_status(void* manger,int saddr,int channel,unsigned short *statue)
|
|
|
+{
|
|
|
+ int ret = 0 ;
|
|
|
+ unsigned short data_temp = 0;
|
|
|
+ ret = g_modbus_read_reg(manger,saddr,channel,&data_temp);
|
|
|
+ if(ret<=0)
|
|
|
+ {
|
|
|
+ printf("erriiiirrr!!!\n");
|
|
|
+ return -1 ;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(data_temp)
|
|
|
+ {
|
|
|
+ *statue = 0;
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ *statue = 1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int breaker_485_get_all_chn_status(void* manger,int saddr,unsigned short *status)
|
|
|
+{
|
|
|
+ int ret = 0 ;
|
|
|
+ unsigned short data_temp[6] = {0};
|
|
|
+ ret = g_modbus_read_x_reg(manger,saddr,BREADER_485_ADDR_CHN_REG+1,BREAKER_485_CHN_MAX,data_temp);
|
|
|
+ if(ret < 0)
|
|
|
+ return ret;
|
|
|
+ for(int i =0;i < BREAKER_485_CHN_MAX;i++)
|
|
|
+ {
|
|
|
+ if(data_temp[i])
|
|
|
+ {
|
|
|
+ status[i] = 0;
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ status[i] = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int breaker_485_get_chns(void* manger,int saddr,unsigned short* cnt)
|
|
|
+{
|
|
|
+ int ret = 0 ;
|
|
|
+ unsigned short data_temp = 0;
|
|
|
+ ret = g_modbus_read_x_reg(manger,saddr,BREADER_485_ADDR_CHN_REG,1,&data_temp);
|
|
|
+ if(ret<=0)
|
|
|
+ return -1 ;
|
|
|
+
|
|
|
+ *cnt = data_temp & 0x00ff;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void* breaker_scanner_thread(void* argv)
|
|
|
+{
|
|
|
+
|
|
|
+ //GlobalDeviceManager* gm= (GlobalDeviceManager*)(argv);
|
|
|
+ thread_handle_t *h=(thread_handle_t*)(argv);
|
|
|
+ GlobalDeviceManager* gm= (GlobalDeviceManager*)(h->arg);
|
|
|
+ memset(&breaker_scanner,0,sizeof(breaker_scanner));
|
|
|
+ int ret = 0;
|
|
|
+ uint16_t value =0;
|
|
|
+ uint16_t status[BREAKER_485_CHN_MAX] = {0};
|
|
|
+ //scanner all boadr
|
|
|
+ for(int i = 0; i < BREAKER_BOARD_MAX; i++)
|
|
|
+ {
|
|
|
+ ret = breaker_485_get_chn_status((void*)&gm->_globalRelaySampManger,
|
|
|
+ BREADER_485_ADDR+i,BREADER_485_ADDR_CHN_REG,&value);
|
|
|
+ if(ret == 0)
|
|
|
+ {
|
|
|
+ breaker_scanner.breaker_flag[i] = 1;
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ breaker_scanner.breaker_flag[i] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ int count = 0;
|
|
|
+
|
|
|
+ while(h->quit==0)
|
|
|
+ {
|
|
|
+ if((count % 10) == 1)
|
|
|
+ {
|
|
|
+ for(int i = 0; i < BREAKER_BOARD_MAX; i++)
|
|
|
+ {
|
|
|
+ ret = breaker_485_get_chn_status((void*)&gm->_globalRelaySampManger,
|
|
|
+ BREADER_485_ADDR+i,BREADER_485_ADDR_CHN_REG,&value);
|
|
|
+ if(ret == 0)
|
|
|
+ {
|
|
|
+ breaker_scanner.breaker_flag[i] = 1;
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ breaker_scanner.breaker_flag[i] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for(int i = 0; i < BREAKER_BOARD_MAX; i++)
|
|
|
+ {
|
|
|
+ if(breaker_scanner.breaker_flag[i])
|
|
|
+ {
|
|
|
+
|
|
|
+ if(breaker_485_get_all_chn_status((void*)&gm->_globalRelaySampManger,BREADER_485_ADDR+i,status))
|
|
|
+ {
|
|
|
+ memset(status,0,sizeof(status));
|
|
|
+ pthread_mutex_lock(&gm->_breaker_mutex);
|
|
|
+ memcpy(breaker_scanner.board[i].ch_status,status,sizeof(status));
|
|
|
+ pthread_mutex_unlock(&gm->_breaker_mutex);
|
|
|
+ breaker_scanner.breaker_flag[i] = 0;
|
|
|
+ continue;
|
|
|
+
|
|
|
+ }
|
|
|
+ pthread_mutex_lock(&gm->_breaker_mutex);
|
|
|
+ memcpy(breaker_scanner.board[i].ch_status,status,sizeof(status));
|
|
|
+ pthread_mutex_unlock(&gm->_breaker_mutex);
|
|
|
+ // ret = breaker_485_get_chn_status((void*)&gm->_globalRelaySampManger,
|
|
|
+ // BREADER_485_ADDR+i,BREADER_485_ADDR_CHN_REG,&value);
|
|
|
+ // //pthread_mutex_lock(&gm->_breaker_mutex);
|
|
|
+ // if(ret == 0)
|
|
|
+ // {
|
|
|
+ // breaker_scanner.breaker_flag[i] = 1; // board is online
|
|
|
+ // //breaker_485_get_all_chn_status((void*)&gm->_globalRelaySampManger,BREADER_485_ADDR+i,status);
|
|
|
+ // //memcmp(breaker_scanner.board[i].scanner_info.ch_status,status,sizeof(status));
|
|
|
+ // }else
|
|
|
+ // {
|
|
|
+ // breaker_scanner.breaker_flag[i] = 0; //board is offline
|
|
|
+ // }
|
|
|
+
|
|
|
+
|
|
|
+ // // pthread_mutex_unlock(&gm->_breaker_mutex);
|
|
|
+ memset(status,0,sizeof(status));
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ memset(status,0,sizeof(status));
|
|
|
+ pthread_mutex_lock(&gm->_breaker_mutex);
|
|
|
+ memcpy(breaker_scanner.board[i].ch_status,status,sizeof(status));
|
|
|
+ pthread_mutex_unlock(&gm->_breaker_mutex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ count ++;
|
|
|
+ sleep(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+breaker_scanner_t *get_breaker_scanner(void)
|
|
|
+{
|
|
|
+ return &breaker_scanner;
|
|
|
}
|