Check failed: status == CUDNN_STATUS_SUCCESS (4 vs. 0) CUDNN_STATUS_BAD_PARAM

解决Python加载Caffe网络错误
本文详细介绍了在Python环境中加载Caffe网络时遇到的错误及其解决方案。通过两种方法,一是调整网络配置文件,二是重新编译Caffe源码,避免使用CuDNN,最终成功解决了加载网络和前向计算的问题。

在python上加载caffe 的网络时会报上述错误,解决方案如下

方式1

1.在报错的相应层添加 engine: CAFFE

结果:加载网络时没有报错,但是net.forward前向计算时,会报Check failed: status == CUDNN_STATUS_SUCCESS (8 vs. 0) CUDNN_STATUS_BAD_PARAM
解决如下

2.重新编译caffe源码

编译时, USE_CUDNN := 0

结果:用cudnn编译和不用cudnn编译结果不一样,因此我采用的是方式2

方式2

所有卷积层有group参数的,都加上 engine: CAFFE

/* Copyright(c) 2009-2025 Shenzhen TP-LINK Technologies Co.Ltd. * * file poe.c * brief * details * * author Liao Jinlei * version 1.0.0 * date 04Mar25 * * warning * * history \arg 1.0.0, 04Mar25, Liao Jinlei, Create the file. */ #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #include <netinet/ip.h> #include <netinet/ip_icmp.h> #include <string.h> #include <time.h> #include <errno.h> #include "type_pub.h" #include "utility_debug.h" #include "opLanPort_info.h" #include "unix_sock_msgDef.h" #include "apiPoeMP3924.h" #include "poe_recovery.h" /**************************************************************************************************/ /* DEFINES */ /**************************************************************************************************/ // #define DBG(args...) printf(args...) // #define ERR(args...) printf(args...) #define MAX_THREADS get_poe_last_uport() // 最大线程数限制: 2 #define ICMP_DATA_SIZE 56 #define MAX_ICMP_SIZE 1024 #define TIMEOUT 1000 // 超时时间(毫秒) // @wxh dbg // #define poe_recovery_init_setByWxh_l_array_poe_recovery_cfgs /**************************************************************************************************/ /* TYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* EXTERN_PROTOTYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* LOCAL_PROTOTYPES */ /**************************************************************************************************/ // void poe_recovery_task(int *arg); // static int _poe_auto_recovery_config_set(int user_port, const POE_PORTRECOVERYCFG_STRUCT* recovery_cfg); //@wxh 已重构优化,调用此函数处直接调用 ppoe_recovery_set_port_config 函数 // static int _poe_auto_recovery_config_del(int user_port); //@wxh 已重构优化,调用处直接赋值默认参数,并调用 ppoe_recovery_set_port_config /**************************************************************************************************/ /* VARIABLES */ /**************************************************************************************************/ // 静态变量:指定IP地址 // static char* TARGET_IP_1 = "192.168.0.253"; //650-dt // static char* TARGET_IP_2 = "192.168.0.26"; // 线程数组 pthread_t* l_array_poe_recovery_threads = NULL; int* l_array_poe_recovery_ids = NULL; // 存储线程ID POE_PORTRECOVERYCFG_STRUCT* l_array_poe_recovery_cfgs = NULL; static int l_thread_count = 0; // 当前线程数量 static int l_poe_recovery_global_status = POE_RECOVERY_DISABLE_UC; /* 默认Disable */ /**************************************************************************************************/ /* LOCAL_FUNCTIONS */ /**************************************************************************************************/ // 创建原始套接字 static int _create_raw_socket() { int raw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); if (raw_socket < 0) { perror("Socket creation failed"); return -1; } return raw_socket; } // 计算校验和 static unsigned short _calculate_checksum(void* b, int len) { unsigned short* buf = b; unsigned int sum = 0; unsigned short result; for (sum = 0; len > 1; len -= 2) sum += *buf++; if (len == 1) sum += *(unsigned char*)buf; sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); result = ~sum; return result; } // 发送 ICMP 回显请求 static int _ping_ip(int raw_socket, const char* ip_address, int seq,int user_port) { struct sockaddr_in dest_addr; struct icmp icmp_hdr; char buffer[MAX_ICMP_SIZE]; int sent_bytes; // 设置目标地址 memset(&dest_addr, 0, sizeof(dest_addr)); dest_addr.sin_family = AF_INET; if (inet_pton(AF_INET, ip_address, &dest_addr.sin_addr) <= 0) { perror("Invalid IP address"); return -1; } // 构造 ICMP 数据包 memset(&icmp_hdr, 0, sizeof(icmp_hdr)); icmp_hdr.icmp_type = ICMP_ECHO; icmp_hdr.icmp_code = 0; icmp_hdr.icmp_id = getpid()+user_port*1000; icmp_hdr.icmp_seq = seq; icmp_hdr.icmp_cksum = 0; // 填充数据部分 memset(buffer, 0, MAX_ICMP_SIZE); memcpy(buffer, &icmp_hdr, sizeof(icmp_hdr)); memset(buffer + sizeof(icmp_hdr), 'E', ICMP_DATA_SIZE); icmp_hdr.icmp_cksum = _calculate_checksum(buffer, sizeof(icmp_hdr) + ICMP_DATA_SIZE); memcpy(buffer, &icmp_hdr, sizeof(icmp_hdr)); // 发送 ICMP 数据包 sent_bytes = sendto(raw_socket, buffer, sizeof(icmp_hdr) + ICMP_DATA_SIZE, 0, (struct sockaddr*)&dest_addr, sizeof(dest_addr)); if (sent_bytes < 0) { perror("Sendto failed"); return -1; } return ERR_NO_ERROR; } // 获取 ICMP 回显应答 static int _get_ping_result(int raw_socket, const char* ip_address, int seq, int user_port) { struct sockaddr_in from_addr; struct icmp icmp_hdr; char buffer[MAX_ICMP_SIZE]; int recv_bytes; socklen_t from_len; fd_set readfds; struct timeval timeout; // 设置超时时间 timeout.tv_sec = TIMEOUT / 1000; timeout.tv_usec = (TIMEOUT % 1000) * 1000; int retry = 5; while(retry--) { // 等待 ICMP 回显应答 FD_ZERO(&readfds); FD_SET(raw_socket, &readfds); int activity = select(raw_socket + 1, &readfds, NULL, NULL, &timeout); if ((activity < 0) && (errno != EINTR)) { printf("Select error"); return -1; } if (activity == 0) { printf("Ping request timed out\n"); return -1; } if (FD_ISSET(raw_socket, &readfds)) { from_len = sizeof(from_addr); recv_bytes = recvfrom(raw_socket, buffer, MAX_ICMP_SIZE, 0, (struct sockaddr*)&from_addr, &from_len); if (recv_bytes < 0) { perror("Recvfrom failed"); return -1; } // 解析 ICMP 数据包 memcpy(&icmp_hdr, buffer + sizeof(struct iphdr), sizeof(icmp_hdr)); if (icmp_hdr.icmp_type == ICMP_ECHOREPLY && icmp_hdr.icmp_id == (getpid()+user_port*1000) && icmp_hdr.icmp_seq == seq) // if (icmp_hdr.icmp_type == ICMP_ECHOREPLY && icmp_hdr.icmp_id == (getpid()+user_port*1000)) { printf("[success][PORT %d] Ping reply from %s: icmp_seq=%d\n", user_port,inet_ntoa(from_addr.sin_addr), icmp_hdr.icmp_seq); printf("[success][PORT %d] [actual] icmp_type: %d, icmp_id: %d, icmp_seq: %d\n", user_port,icmp_hdr.icmp_type,icmp_hdr.icmp_id ,icmp_hdr.icmp_seq ); printf("[success][PORT %d] [supose] icmp_type: %d, icmp_id: %d, icmp_seq: %d\n",user_port,ICMP_ECHOREPLY,(getpid()+user_port*1000),seq ); return ERR_NO_ERROR; } else { printf("[failed][PORT %d] Received unexpected ICMP packet\n",user_port); printf("[failed][PORT %d] [actual] icmp_type: %d, icmp_id: %d, icmp_seq: %d\n", user_port,icmp_hdr.icmp_type,icmp_hdr.icmp_id ,icmp_hdr.icmp_seq ); printf("[failed][PORT %d] [supose] icmp_type: %d, icmp_id: %d, icmp_seq: %d\n",user_port,ICMP_ECHOREPLY,(getpid()+user_port*1000),seq ); // return -1; } } } return -1; } static int _poe_recovery_counter_command(int user_port, POE_RECOVERY_COUNTER_CMD cmd) { int rv = ERR_NO_ERROR; if (user_port > get_poe_last_uport() || user_port < get_poe_first_uport() || l_array_poe_recovery_cfgs == NULL) { return ERR_BAD_PARAM; } // RECOVERY_TASK_LOCK(); switch (cmd) { case CLR_FAILURE: l_array_poe_recovery_cfgs[user_port-1].failure = 0; break; case INC_FAILURE: l_array_poe_recovery_cfgs[user_port-1].failure = (l_array_poe_recovery_cfgs[user_port-1].failure++ == POE_RECOVERY_FAILURE_MAX) ? 1 : l_array_poe_recovery_cfgs[user_port-1].failure; break; case INC_RESTART: l_array_poe_recovery_cfgs[user_port-1].restart = (l_array_poe_recovery_cfgs[user_port-1].restart++ == POE_RECOVERY_RESTART_MAX) ? 1 : l_array_poe_recovery_cfgs[user_port-1].restart; break; case INC_TOTAL: l_array_poe_recovery_cfgs[user_port-1].total = (l_array_poe_recovery_cfgs[user_port-1].total++ == POE_RECOVERY_TOTAL_MAX) ? 1 : l_array_poe_recovery_cfgs[user_port-1].total; break; case CLR_FAILURE_RESTART_TOTAL: l_array_poe_recovery_cfgs[user_port-1].failure = 0; l_array_poe_recovery_cfgs[user_port-1].restart = 0; l_array_poe_recovery_cfgs[user_port-1].total = 0; default: rv = ERR_BAD_PARAM; break; } // RECOVERY_TASK_UNLOCK(); return rv; } static int _poe_recovery_validate_port_config(const POE_PORTRECOVERYCFG_STRUCT *poe_port_recovery_cfg, int check_ip) { printf("_poe_recovery_validate_port_config will goto ping recovery %d\n", 5); // @wxh dbg #if 0 if (check_ip && !sw_poe_recovery_ip_validate(poe_port_recovery_cfg->ip)) { return FALSE; } #endif if (poe_port_recovery_cfg->status != POE_RECOVERY_DISABLE_UC && poe_port_recovery_cfg->status != POE_RECOVERY_ENABLE_UC) { printf("[%s] ERR_BAD_PARAM: status.\r\n", __FUNCTION__); return FALSE; } if (poe_port_recovery_cfg->startup < POE_RECOVERY_STARTUP_MIN || poe_port_recovery_cfg->startup > POE_RECOVERY_STARTUP_MAX) { printf("[%s] ERR_BAD_PARAM: startup.\r\n", __FUNCTION__); return FALSE; } if (poe_port_recovery_cfg->interval < POE_RECOVERY_INTERVAL_MIN || poe_port_recovery_cfg->interval > POE_RECOVERY_INTERVAL_MAX) { printf("[%s] ERR_BAD_PARAM: interval.\r\n", __FUNCTION__); return FALSE; } if (poe_port_recovery_cfg->retry < POE_RECOVERY_RETRY_MIN || poe_port_recovery_cfg->retry > POE_RECOVERY_RETRY_MAX) { printf("[%s] ERR_BAD_PARAM: retry.\r\n", __FUNCTION__); return FALSE; } if (poe_port_recovery_cfg->reboot < POE_RECOVERY_BREAK_MIN || poe_port_recovery_cfg->reboot > POE_RECOVERY_BREAK_MAX) { printf("[%s] ERR_BAD_PARAM: reboot.\r\n", __FUNCTION__); return FALSE; } return TRUE; } static int _poe_recovery_get_port_config(int user_port, POE_PORTRECOVERYCFG_STRUCT *poe_port_recovery_cfg) { if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport() || NULL == poe_port_recovery_cfg) { return ERR_BAD_PARAM; } // RECOVERY_TASK_LOCK(); memcpy(poe_port_recovery_cfg, &l_array_poe_recovery_cfgs[user_port-1], sizeof(POE_PORTRECOVERYCFG_STRUCT)); // RECOVERY_TASK_UNLOCK(); return ERR_NO_ERROR; } /* set to local variable */ static int _poe_recovery_set_port_config(int user_port, const POE_PORTRECOVERYCFG_STRUCT *poe_port_recovery_cfg) { printf("_poe_recovery_set_port_config will goto ping recovery %d\n", 6); // @wxh dbg if (NULL == poe_port_recovery_cfg || l_array_poe_recovery_cfgs == NULL) { return ERR_BAD_PARAM; } // RECOVERY_TASK_LOCK(); memset(l_array_poe_recovery_cfgs[user_port-1].ip, 0, 16); snprintf(l_array_poe_recovery_cfgs[user_port-1].ip, 16, "%s", poe_port_recovery_cfg->ip); l_array_poe_recovery_cfgs[user_port-1].status = poe_port_recovery_cfg->status; l_array_poe_recovery_cfgs[user_port-1].startup = poe_port_recovery_cfg->startup; l_array_poe_recovery_cfgs[user_port-1].interval = poe_port_recovery_cfg->interval; l_array_poe_recovery_cfgs[user_port-1].retry = poe_port_recovery_cfg->retry; l_array_poe_recovery_cfgs[user_port-1].reboot = poe_port_recovery_cfg->reboot; // RECOVERY_TASK_UNLOCK(); return ERR_NO_ERROR; } static int _poe_recovery_get_global_status(int *status) { // RECOVERY_TASK_LOCK(); *status = l_poe_recovery_global_status; // RECOVERY_TASK_UNLOCK(); return ERR_NO_ERROR; } /* set to local variable */ static int _poe_recovery_set_global_status(int status) { // RECOVERY_TASK_LOCK(); l_poe_recovery_global_status = status; // RECOVERY_TASK_UNLOCK(); return ERR_NO_ERROR; } /*! *\Function: * _poe_port_status_set *\Description: * Port PoE status set api for PoE Auto Recovery *\Input: user_port - user port, start from 1 enable - status <POE_ENABLE_UC | POE_DISABLE_UC> *\Output: * n/a *\Return: Port PoE status set result *\Note: */ static int _poe_port_status_set(unsigned int user_port, int enable) { // POE_PORTCFG_STRUCT poe_port_config = {0}; int rv = ERR_NO_ERROR; int power_dis_flag = 0; int time_seg_dis_flag = 0; /* Check params */ if (user_port > get_poe_last_uport() || user_port < get_poe_first_uport()) { printf("[%s] PoE function is unavailable on user_port %d.\r\n", __FUNCTION__, user_port); return ERR_BAD_PARAM; } if (POE_DISABLE != enable && POE_ENABLE != enable) { return ERR_BAD_PARAM; } ad_poe_port_flag_get(user_port, POE_MANAGE_POWER_DIS, &power_dis_flag); ad_poe_port_flag_get(user_port, POE_MANAGE_TIMESEG_DIS, &time_seg_dis_flag); if (POE_RELEASE == power_dis_flag && POE_RELEASE == time_seg_dis_flag) { /* Update adapter config */ rv = ad_poe_set_port_state(user_port, enable); if (ERR_NO_ERROR != rv) { return rv; } } /* Update poe port flag */ return ad_poe_port_flag_set(user_port, POE_MANAGE_RECOVERY_DIS, (POE_DISABLE == enable) ? POE_OCCUPY : POE_RELEASE); } // 线程task static void* poe_recovery_task(void* arg) { POE_PORTRECOVERYCFG_STRUCT poe_port_recovery_cfg; int fail_cnt = 0; int global_status; int user_port = *(int*)arg; int seq = 0; if (user_port > get_poe_last_uport() || user_port < get_poe_first_uport()) { return NULL; } /* PoE Auto Recovery Task主循环 */ while (1) { global_status = POE_RECOVERY_DISABLE_UC; (void)_poe_recovery_get_global_status(&global_status); _poe_recovery_get_port_config(user_port, &poe_port_recovery_cfg); if (POE_RECOVERY_ENABLE_UC != poe_port_recovery_cfg.status || POE_RECOVERY_ENABLE_UC != global_status) { printf("[Port %d] PoE Auto Recovery disabled!\r\n", user_port); sleep(1); continue; } int raw_socket = _create_raw_socket(); if (raw_socket < 0) { printf("Thread %d: Failed to create socket\n", user_port); return NULL; } printf("Thread %d: raw_socket is %d \n", user_port,raw_socket); printf("[Port %d] Startup: %d secnonds...\r\n", user_port, poe_port_recovery_cfg.startup); sleep(poe_port_recovery_cfg.startup); /* PoE Auto Recovery工作循环,检测到Enable才进入 */ while (1) { global_status = POE_RECOVERY_DISABLE_UC; (void)_poe_recovery_get_global_status(&global_status); _poe_recovery_get_port_config(user_port, &poe_port_recovery_cfg); if (POE_RECOVERY_ENABLE_UC != poe_port_recovery_cfg.status || POE_RECOVERY_ENABLE_UC != global_status) { printf("[Port %d] PoE Auto Recovery disabled!\r\n", user_port); break; } //ping target ip if (_ping_ip(raw_socket, poe_port_recovery_cfg.ip, seq, user_port) < 0) { printf("[Port %d] Failed to send ping to %s, try again.\r\n", user_port, poe_port_recovery_cfg.ip); sleep(3); continue; } _poe_recovery_counter_command(user_port, INC_TOTAL); if (_get_ping_result(raw_socket, poe_port_recovery_cfg.ip, seq, user_port) < 0) { printf("Thread %d: Ping failed for %s\r\n", user_port, poe_port_recovery_cfg.ip); fail_cnt++; _poe_recovery_counter_command(user_port, INC_FAILURE); printf("[Port %d] Ping FAIL: total=%d, failure=%d, fail_cnt=%d\r\n", user_port, poe_port_recovery_cfg.total+1, poe_port_recovery_cfg.failure+1, fail_cnt); } else { printf("Thread %d: Ping success for %s\n", user_port, poe_port_recovery_cfg.ip); fail_cnt=0; _poe_recovery_counter_command(user_port, CLR_FAILURE); printf("[Port %d] Ping SUCCESS: total=%d, failure=%d, fail_cnt=%d.\r\n", user_port, poe_port_recovery_cfg.total+1, poe_port_recovery_cfg.failure, fail_cnt); } seq++; if (fail_cnt >= poe_port_recovery_cfg.retry) { printf("[Port %d] Ping: fail_cnt reaches the limit(%d), now reboot pd.(Reboot Time: %d seconds)\r\n", user_port, poe_port_recovery_cfg.retry, poe_port_recovery_cfg.reboot); fail_cnt = 0; _poe_recovery_counter_command(user_port, CLR_FAILURE); _poe_recovery_counter_command(user_port, INC_RESTART); _poe_port_status_set(user_port, POE_DISABLE); // ad_poe_set_port_state(user_port, POE_DISABLE); sleep(poe_port_recovery_cfg.reboot); _poe_port_status_set(user_port, POE_ENABLE); // ad_poe_set_port_state(user_port, POE_ENABLE); break; } printf("[Port %d] Ping: wait %d seconds for next ping...\r\n\n", user_port, poe_port_recovery_cfg.interval); sleep(poe_port_recovery_cfg.interval - POE_RECOVERY_PING_TIMEOUT); } close(raw_socket); } } // 创建线程 static int poe_recovery_task_create_thread(int user_port) { // printf("poe_recovery_task_create_thread will goto ping recovery %d \n", 7); // @wxh dbg //@wxh 勘误修改 l_thread_count代表创建的线程数 应该小于等于2,这里 >= 应改为 > 符号 // if (l_thread_count >= MAX_THREADS) { if (l_thread_count > MAX_THREADS) { printf("Error: Maximum number of l_array_poe_recovery_threads reached (%d)\n", MAX_THREADS); return -1; } printf("poe_recovery_task_create_thread thread ID:%d \n",user_port); if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport()) { printf("Error: Invalid thread ID \n"); return -1; } if (l_array_poe_recovery_ids[user_port-1] != POE_RECOVERY_THREAD_NOT_USE) { printf("Error: Thread ID %d is already in use\n", user_port); return -1; } int* arg = malloc(sizeof(int)); if (!arg) { perror("Memory allocation failed"); return -1; } *arg = user_port; if (pthread_create(&l_array_poe_recovery_threads[user_port-1], NULL, poe_recovery_task, arg) != 0) { perror("Failed to create thread"); free(arg); return -1; } l_array_poe_recovery_ids[user_port-1] = POE_RECOVERY_THREAD_IN_USE; // 标记线程ID为已使用 _poe_recovery_counter_command(user_port, CLR_FAILURE_RESTART_TOTAL); l_thread_count++; //todo // stats[user_port] = (PingStats){0, 0, 0}; // 初始化统计信息 printf("Thread for user_port %d created successfully\n", user_port); return ERR_NO_ERROR; } // 关闭线程 static int poe_recovery_task_destroy_thread(int user_port) { // @wxh 勘误修改 这里的 >= 会导致取消线程2时报错,导致线程2无法被取消,应该改为 > // if (user_port < 0 || user_port >= MAX_THREADS) { if (user_port <0 || user_port > MAX_THREADS) { printf("Error: Invalid thread ID\n"); return -1; } if (l_array_poe_recovery_ids[user_port-1] == POE_RECOVERY_THREAD_NOT_USE) { printf("Error: Thread ID %d does not exist\n", user_port); return -1; } if (pthread_cancel(l_array_poe_recovery_threads[user_port-1]) != 0) { perror("Failed to cancel thread"); return -1; } pthread_join(l_array_poe_recovery_threads[user_port-1], NULL); l_array_poe_recovery_ids[user_port-1] = POE_RECOVERY_THREAD_NOT_USE; // 标记线程ID为未使用 _poe_recovery_counter_command(user_port, CLR_FAILURE_RESTART_TOTAL); l_thread_count--; printf("Thread %d canceled successfully\n", user_port); return ERR_NO_ERROR; } /* // 清理所有线程 static void poe_recovery_task_cleanup_threads() { int i; for (i = get_poe_first_uport(); i <= get_poe_last_uport(); i++) { if (l_array_poe_recovery_ids[i-1] != POE_RECOVERY_THREAD_NOT_USE) { pthread_cancel(l_array_poe_recovery_threads[i-1]); pthread_join(l_array_poe_recovery_threads[i-1], NULL); } } } */ /**************************************************************************************************/ /* PUBLIC_FUNCTIONS */ /**************************************************************************************************/ //set port config to local variable int poe_recovery_set_port_config( int user_port, const POE_PORTRECOVERYCFG_STRUCT *poe_port_recovery_cfg, int check_ip) { int rv = 0; int global_status; // POE_PORTCFG_STRUCT poe_port_cfg; POE_PORTRECOVERYCFG_STRUCT poe_port_recovery_cfg_old; printf("[%s] enter on port %d.\r\n", __FUNCTION__, user_port); /* check param legality */ if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport() || NULL == poe_port_recovery_cfg) { printf("[%s][port %d] ERR_BAD_PARAM: user_port.\r\n", __FUNCTION__, user_port); return ERR_BAD_PARAM; } /* check poe config */ // sw_poe_get_port_config(user_port, &poe_port_cfg); _poe_recovery_get_port_config(user_port, &poe_port_recovery_cfg_old); printf("[%s] done _poe_recovery_get_port_config on port %d.\r\n", __FUNCTION__, user_port); /* todo:when port not enable poe,shoule return. if (poe_port_recovery_cfg_old.status == POE_RECOVERY_DISABLE_UC && poe_port_recovery_cfg->status == POE_RECOVERY_ENABLE_UC && poe_port_cfg.status == POE_DISABLE) { // sprintf(buff, "port %d", user_port); // swPoeAddLogSinglePara(LOG_POE_RECOVERY_CONFIG_SET_FAIL, (ULONG)buff); return ERR_POE_RECOVERY_POE_DISABLED; } */ /* validate configuration */ if (!_poe_recovery_validate_port_config(poe_port_recovery_cfg, check_ip)) { printf("[%s][port %d] ERR_BAD_PARAM: user_port.\r\n", __FUNCTION__, user_port); return ERR_BAD_PARAM; } rv = _poe_recovery_set_port_config(user_port, poe_port_recovery_cfg); //更新参数到了 l_array_poe_recovery_cfgs if (ERR_NO_ERROR != rv) { printf("[%s][port %d] _poe_recovery_set_port_config ERROR.\r\n", __FUNCTION__, user_port); return rv; } /* create/destroy poe recovery task */ /* 全局enable的情况下,端口enable/disable触发task创建/销毁 */ /* 全局disable的情况下,端口enable/disable只改变参数,后续全局enable/disable时再创建/销毁线程 */ rv = _poe_recovery_get_global_status(&global_status); if (rv != ERR_NO_ERROR) { return rv; } /* 未更改之前的逻辑,对端口的 status 变化有响应,对端口的其他5个参数变化没有相应 */ // if (poe_port_recovery_cfg_old.status != poe_port_recovery_cfg->status && POE_RECOVERY_ENABLE_UC == global_status) // { // printf("poe_port_recovery_cfg_old.status = %u\n", poe_port_recovery_cfg_old.status); //@wxh dbg // printf("poe_port_recovery_cfg->status = %u\n", poe_port_recovery_cfg->status); //@wxh dbg // printf("POE_RECOVERY_ENABLE_UC = %d\n", POE_RECOVERY_ENABLE_UC); //@wxh dbg // printf("global_status = %d\n", global_status); //@wxh dbg // /* enable -> disable */ // if (poe_port_recovery_cfg_old.status == POE_RECOVERY_ENABLE_UC) // { // rv = poe_recovery_task_destroy_thread(user_port); // if (ERR_NO_ERROR != rv) // { // return rv; // } // } // /* disable -> enable */ // else // { // rv = poe_recovery_task_create_thread(user_port); // if (ERR_NO_ERROR != rv) // { // printf("@@@@@ poe_recovery_task_create_thread failure\n"); // return rv; // } // printf("@@@@@ poe_recovery_task_create_thread sucess\n"); // } // } if (POE_RECOVERY_ENABLE_UC == global_status) { printf("[%s][port %d] POE_RECOVERY_ENABLE_UC == global_status\r\n", __FUNCTION__, user_port); printf("[%s][port %d] poe_port_recovery_cfg_old.status = %d \r\n", __FUNCTION__, user_port,poe_port_recovery_cfg_old.status); printf("[%s][port %d] poe_port_recovery_cfg->status = %d \r\n", __FUNCTION__, user_port,poe_port_recovery_cfg->status); if (poe_port_recovery_cfg_old.status != poe_port_recovery_cfg->status) { // printf("poe_port_recovery_cfg_old.status = %u\n", poe_port_recovery_cfg_old.status); //@wxh dbg // printf("poe_port_recovery_cfg->status = %u\n", poe_port_recovery_cfg->status); //@wxh dbg // printf("POE_RECOVERY_ENABLE_UC = %d\n", POE_RECOVERY_ENABLE_UC); //@wxh dbg // printf("global_status = %d\n", global_status); //@wxh dbg /* enable -> disable */ if (poe_port_recovery_cfg_old.status == POE_RECOVERY_ENABLE_UC) { printf("----->---- [%s] /* disable -> enable */\n", __FUNCTION__); //@wxh dbg rv = poe_recovery_task_destroy_thread(user_port); if (ERR_NO_ERROR != rv) { return rv; } } /* disable -> enable */ else { printf("----->---- [%s] /* disable -> enable */\n", __FUNCTION__); //@wxh dbg rv = poe_recovery_task_create_thread(user_port); if (ERR_NO_ERROR != rv) { printf("----->---- poe_recovery_task_create_thread failure\n"); return rv; } printf("----->---- poe_recovery_task_create_thread sucess\n"); } } /* @wxh 新旧的端口 status 都为 POE_RECOVERY_ENABLE_UC 时,改变ip、startup、interval、retry、reboot 5个参数时 需要销毁线程,并且重新创建线程来使这些更新的参数生效。 */ else { /* @wxh 当5个参数中的任意一个参数发生变化时,销毁线程,重新创建线程来使这些更新的参数生效 */ printf("[%s][port %d] goto else\r\n", __FUNCTION__, user_port); if (POE_RECOVERY_ENABLE_UC == poe_port_recovery_cfg_old.status) { if (0 != strcmp(poe_port_recovery_cfg_old.ip, poe_port_recovery_cfg->ip) || poe_port_recovery_cfg_old.startup != poe_port_recovery_cfg->startup || poe_port_recovery_cfg_old.interval != poe_port_recovery_cfg->interval || poe_port_recovery_cfg_old.retry != poe_port_recovery_cfg->retry || poe_port_recovery_cfg_old.reboot != poe_port_recovery_cfg->reboot) { printf("---->---- 除status的5个参数(ip startup interval retry reboot )发生变化,销毁线程,重新创建线程\n"); printf("---->---->---- old param poe_port_recovery_cfg_old: status:%d ip:%s interval:%d startup:%d retry:%d reboot:%d\n", poe_port_recovery_cfg_old.status, poe_port_recovery_cfg_old.ip, poe_port_recovery_cfg_old.interval, poe_port_recovery_cfg_old.startup, poe_port_recovery_cfg_old.retry, poe_port_recovery_cfg_old.reboot); printf("---->---->---- new param poe_port_recovery_cfg: status:%d ip:%s interval:%d startup:%d retry:%d reboot:%d\n", poe_port_recovery_cfg->status, poe_port_recovery_cfg->ip, poe_port_recovery_cfg->interval, poe_port_recovery_cfg->startup, poe_port_recovery_cfg->retry, poe_port_recovery_cfg->reboot); rv |= poe_recovery_task_destroy_thread(user_port); if (ERR_NO_ERROR != rv) { printf("---->---->---- poe_recovery_task_destroy_thread failure\n"); } printf("---->---->---- poe_recovery_task_create_thread sucess\n"); rv |= poe_recovery_task_create_thread(user_port); if (ERR_NO_ERROR != rv) { printf("---->---->---- poe_recovery_task_create_thread failure\n"); } printf("---->---->---- poe_recovery_task_create_thread sucess\n"); } } } } else { printf("---->---- [%s] poe recovery set port but l_poe_recovery_global_status is POE_RECOVERY_DISABLE_UC\n", __FUNCTION__); printf("---->---- in poe_port_recovery_cfg: status:%d ip:%s interval:%d startup:%d retry:%d reboot:%d\n", poe_port_recovery_cfg->status, poe_port_recovery_cfg->ip, poe_port_recovery_cfg->interval, poe_port_recovery_cfg->startup, poe_port_recovery_cfg->retry, poe_port_recovery_cfg->reboot); } //@wxh 如果 port recovery status 是enable状态,此时上层api 将其中的ip 、interval、startup 等值改变了如何处理? printf("[%s][port %d] end.\r\n", __FUNCTION__, user_port); return ERR_NO_ERROR; } int poe_auto_recovery_global_set(int status) { int user_port, rv, global_status; POE_PORTRECOVERYCFG_STRUCT poe_port_recovery_cfg; if (POE_RECOVERY_DISABLE_UC != status && POE_RECOVERY_ENABLE_UC != status) { return ERR_BAD_PARAM; } if (0 == get_include_poe_auto_recovery()) { printf("[%s] not support auto recovery\n", __FUNCTION__); return ERR_NO_ERROR; } rv = _poe_recovery_get_global_status(&global_status); if (rv != ERR_NO_ERROR) { return rv; } if (status != global_status) { /* update 全局变量 l_poe_recovery_global_status */ _poe_recovery_set_global_status(status); /* enable -> disable */ if (POE_RECOVERY_DISABLE_UC == status) { printf("####**&&** in [%s] /* enable -> disable */\n", __FUNCTION__); for (user_port = get_poe_first_uport(); user_port <= get_poe_last_uport(); user_port++) { rv = poe_recovery_task_destroy_thread(user_port); if (ERR_NO_ERROR != rv) { return rv; } } } /* disable -> enable */ else { printf("####**&&** in [%s] /* disable -> enable */\n", __FUNCTION__); for (user_port = get_poe_first_uport(); user_port <= get_poe_last_uport(); user_port++) { /* Check poe recovery config */ _poe_recovery_get_port_config(user_port, &poe_port_recovery_cfg); printf("####**&&** in [%s] _poe_recovery_get_port_config, result:\n", __FUNCTION__); printf("[port %d] status:%d ip:%s interval:%d startup:%d retry:%d reboot:%d\n", poe_port_recovery_cfg.port_no, poe_port_recovery_cfg.status, poe_port_recovery_cfg.ip, poe_port_recovery_cfg.interval, poe_port_recovery_cfg.startup, poe_port_recovery_cfg.retry, poe_port_recovery_cfg.reboot); if (POE_RECOVERY_ENABLE_UC == poe_port_recovery_cfg.status) { rv = poe_recovery_task_create_thread(user_port); if (ERR_NO_ERROR != rv) { printf("####**&&** poe_recovery_task_create_thread failure\n"); return rv; } printf("####**&&** poe_recovery_task_create_thread sucess\n"); } else { printf("####**&&** poe_port_recovery_cfg.status != POE_RECOVERY_ENABLE_UC\n"); printf("####**&&** poe_port_recovery_cfg.status = %d\n", poe_port_recovery_cfg.status); } } } } printf("[%s] Recovery status changed to %d \n", __FUNCTION__, status); return ERR_NO_ERROR; } int poe_auto_recovery_global_get(int* status) { if( NULL == status ) { return ERR_BAD_PARAM; } if (0 == get_include_poe_auto_recovery()) { printf("[%s] not support auto recovery\n", __FUNCTION__); return ERR_NO_ERROR; } _poe_recovery_get_global_status(status); // printf("[%s] get Recovery status: %d \n", __FUNCTION__, *status); return ERR_NO_ERROR; } //once set one port int poe_auto_recovery_config_set(const POE_OUT_MSG *in_info) { POE_PORTRECOVERYCFG_STRUCT* port_cfg = (POE_PORTRECOVERYCFG_STRUCT*)in_info->data.poe_recovery_port_cfg; POE_PORTRECOVERYCFG_STRUCT recovery_cfg = {0}; int user_port = port_cfg->port_no; int rv = 0; if (0 == get_include_poe_auto_recovery()) { printf("[%s] not support auto recovery\n", __FUNCTION__); return ERR_NO_ERROR; } if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport()) { printf("Input port_id:%d is invalid\n", user_port); return -1; } printf("####ppoe_auto_recovery_config_set in [%s] port:%d\n status:%d ip:%s\n interval:%d\n startup:%d\n retry:%d\n reboot:%d\n failure:%d\n restart:%d\n total:%d\n",__FUNCTION__, port_cfg->port_no, port_cfg->status,port_cfg->ip,port_cfg->interval,port_cfg->startup,port_cfg->retry,port_cfg->reboot,port_cfg->failure,port_cfg->restart,port_cfg->total); recovery_cfg.status = port_cfg->status; // POE_RECOVERY_STATUS_UC status; /* PoE Recovery使能 */ snprintf(recovery_cfg.ip, 16, "%s", port_cfg->ip); //char ip[IP_STR_LEN]; /* Ping IP Address */ recovery_cfg.startup = port_cfg->startup; // int startup; /* Startip Delay (seconds) */ recovery_cfg.interval = port_cfg->interval; // int interval; /* Interval (seconds) */ recovery_cfg.retry = port_cfg->retry; // int retry; /* Failure Threshold */ recovery_cfg.reboot = port_cfg->reboot; // int reboot; /* Break Time (seconds)*/ recovery_cfg.failure = port_cfg->failure; // int failure; /* Failures */ recovery_cfg.restart = port_cfg->restart; // int restart; /* Restarts */ recovery_cfg.total = port_cfg->total; // int total; /* Total */ rv = poe_recovery_set_port_config(user_port, &recovery_cfg, TRUE); if (rv != ERR_NO_ERROR) printf("[%s]Set auto recovery port%d failed\n", __FUNCTION__, user_port); return ERR_NO_ERROR; } //@wxh 已重构优化 //once set one port int poe_auto_recovery_config_del(const POE_OUT_MSG *in_info) { POE_PORTRECOVERYCFG_STRUCT* port_cfg = (POE_PORTRECOVERYCFG_STRUCT*)in_info->data.poe_recovery_port_cfg; int user_port = port_cfg->port_no; int rv = 0; POE_PORTRECOVERYCFG_STRUCT recovery_cfg = {0}; if (0 == get_include_poe_auto_recovery()) { printf("[%s] not support auto recovery\n", __FUNCTION__); return ERR_NO_ERROR; } if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport()) { printf("Input port_id:%d is invalid\n", user_port); return -1; } /* 设置为默认参数 */ recovery_cfg.startup = POE_RECOVERY_STARTUP; recovery_cfg.interval = POE_RECOVERY_INTERVAL; recovery_cfg.retry = POE_RECOVERY_RETRY; recovery_cfg.reboot = POE_RECOVERY_REBOOT; recovery_cfg.status = POE_RECOVERY_DISABLE_UC; rv = poe_recovery_set_port_config(user_port, &recovery_cfg, FALSE); if (rv != ERR_NO_ERROR) printf("[%s]Del auto recovery port%d failed\n", __FUNCTION__, user_port); return rv; } //@wxh 已重构优化 int poe_auto_recovery_status_get(POE_OUT_MSG *out_info) { POE_PORTRECOVERYCFG_STRUCT recovery_cfg = {0}; // POE_PORTRECOVERYCFG_STRUCT* cur_out_port_cfg = out_info->data.poe_recovery_port_cfg; int user_port = 0; if (0 == get_include_poe_auto_recovery()) { printf("[%s] not support auto recovery\n", __FUNCTION__); return ERR_NO_ERROR; } for (user_port = get_poe_first_uport(); user_port <= get_poe_last_uport(); user_port++) { _poe_recovery_get_port_config(user_port, &recovery_cfg); out_info->data.poe_recovery_port_cfg[user_port - 1].port_no = user_port; /* 端口号 */ out_info->data.poe_recovery_port_cfg[user_port - 1].status = recovery_cfg.status; /* PoE Recovery使能 */ // strcpy(out_info->data.poe_recovery_port_cfg[user_port - 1].ip, recovery_cfg.ip); // char ip[IP_STR_LEN]; /* Ping IP Address */ // strcpy(out_info->data.poe_recovery_port_cfg[user_port - 1].ip, "192.168.0.253"); // char ip[IP_STR_LEN]; /* Ping IP Address */ 使用strcpy不安全,应该用snprintf snprintf(out_info->data.poe_recovery_port_cfg[user_port - 1].ip, 16, "%s", recovery_cfg.ip); //char ip[IP_STR_LEN]; /* Ping IP Address */ out_info->data.poe_recovery_port_cfg[user_port - 1].startup = recovery_cfg.startup; /* Startip Delay (seconds) */ out_info->data.poe_recovery_port_cfg[user_port - 1].interval = recovery_cfg.interval; /* Interval (seconds) */ out_info->data.poe_recovery_port_cfg[user_port - 1].retry = recovery_cfg.retry; /* Failure Threshold */ out_info->data.poe_recovery_port_cfg[user_port - 1].reboot = recovery_cfg.reboot; /* Break Time (seconds)*/ out_info->data.poe_recovery_port_cfg[user_port - 1].failure = recovery_cfg.failure; /* Failures */ out_info->data.poe_recovery_port_cfg[user_port - 1].restart = recovery_cfg.restart; /* Restarts */ out_info->data.poe_recovery_port_cfg[user_port - 1].total = recovery_cfg.total; /* Total */ } return ERR_NO_ERROR; } int poe_recovery_init(void) { printf("goto poe_recovery_init\n"); //@wxh dbg // int user_port = 0; int user_port = 1; //@wxh 根据apiPoeMP3924.c user_port - user_port, start from 1 // POE_PORTRECOVERYCFG_STRUCT recovery_cfg = {0}; // int status = -1; // int rv = 0; for (user_port = get_poe_first_uport(); user_port <= get_poe_last_uport(); user_port++) { /* init recovery flag */ ad_poe_port_flag_set(user_port, POE_MANAGE_RECOVERY_DIS, POE_RELEASE); } if (0 == get_include_poe_auto_recovery()) { return ERR_NO_ERROR; } //todo ? /* poe auto recoevry need to load ping module first */ // sw_ping_init(); l_array_poe_recovery_threads = (pthread_t *)malloc(sizeof(pthread_t)*get_poe_last_uport()); l_array_poe_recovery_ids = (int *)malloc(sizeof(int)*get_poe_last_uport()); l_array_poe_recovery_cfgs = (POE_PORTRECOVERYCFG_STRUCT *)malloc(sizeof(POE_PORTRECOVERYCFG_STRUCT)*get_poe_last_uport()); memset(l_array_poe_recovery_cfgs, 0, sizeof(POE_PORTRECOVERYCFG_STRUCT) * get_poe_last_uport()); // poe_recovery_id = (pal_thread_t *)POE_ALLOC(sizeof(pal_thread_t)*get_poe_last_uport()); for (user_port = get_poe_first_uport(); user_port <= get_poe_last_uport(); user_port++) { /* init thread id */ // printf("l_array_poe_recovery_ids[%d] = POE_RECOVERY_THREAD_NOT_USE; \n",user_port-1); l_array_poe_recovery_ids[user_port-1] = POE_RECOVERY_THREAD_NOT_USE; } printf("Start to load and set userconfig for auto recovery.\n"); printf("in swPoeOut.c sw_poeOut_init load flash data and set auto recovery\n"); return ERR_NO_ERROR; } /**************************************************************************************************/ /* GLOBAL_FUNCTIONS */ /**************************************************************************************************/ 画出一个流程图,专业的解释我这段代码里面的ping检测功能
最新发布
10-30
/* Copyright(c) 2009-2024 Shenzhen TP-LINK Technologies Co.Ltd. * * file poe.c * brief * details * * author Di Baosheng * version 1.0.0 * date 02Jan24 * * warning * * history \arg 1.0.0, 02Jan24, Di Baosheng, Create the file. */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/un.h> #include <sys/shm.h> #include <sys/sem.h> #include <errno.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include "wlan_pub.h" #include "type_pub.h" #include "ipc_pub.h" #include "op_info.h" #include "unix_sock.h" #include "unix_sock_msgDef.h" #include "unix_sock.h" #include "utility_uloop.h" #include "nm_api.h" #include "dev_config.h" #include "poe.h" #include "apiPoeMP3924.h" #include "poe_recovery.h" /**************************************************************************************************/ /* DEFINES */ /**************************************************************************************************/ #define POE_INVALID_FD (-1) #define CLOSE(fd) \ do{ \ if ((fd) >= 0) \ { \ if (0 != close(fd)) \ { \ PRINT_ERR("Failed to close socket %d.\n", fd); \ } \ fd = POE_INVALID_FD; \ } \ }while(0) #define POE_TIME_INTERVAL (350) /* 350ms */ #define POE_FLAG_TIME_INTERVAL (1000) /* 1000ms */ /**************************************************************************************************/ /* TYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* EXTERN_PROTOTYPES */ /**************************************************************************************************/ extern int poe_recovery_init(void); /**************************************************************************************************/ /* LOCAL_PROTOTYPES */ /**************************************************************************************************/ static void _poeListenEventHandler(struct evloop_fd *pEvent, unsigned int flag); static void _poeClientEventHandler(struct evloop_fd *pEvent, unsigned int flag); static void _poeTimerHandler(struct evloop_timeout *pTmo); static void _poeFlagTimerHandler(struct evloop_timeout *pTmo); // static void _printprint(void); // /**************************************************************************************************/ /* VARIABLES */ /**************************************************************************************************/ static UNIX_SOCK_FD l_sockFd; POE_USRCONFIG_STRUCT *puc_poe_config = NULL; // puc_poe_config->poe_port_config = NULL; static struct evloop_fd l_poe_listen_event_fd = { .cb = _poeListenEventHandler, .fd = POE_INVALID_FD, }; static struct evloop_fd l_poe_client_event_fd = { .cb = _poeClientEventHandler, .fd = POE_INVALID_FD, }; static struct evloop_timeout l_poe_timer = { .cb = _poeTimerHandler, }; static struct evloop_timeout l_poe_flag_timer = { .cb = _poeFlagTimerHandler, }; /**************************************************************************************************/ /* LOCAL_FUNCTIONS */ /**************************************************************************************************/ int uc_poe_set_port_config(int user_port, POE_PORTCFG_STRUCT * poe_port_config) { // TASK_LOCK(); puc_poe_config->poe_port_config[user_port-1].power_limit = poe_port_config->power_limit; puc_poe_config->poe_port_config[user_port-1].priority = poe_port_config->priority; puc_poe_config->poe_port_config[user_port-1].status = poe_port_config->status; puc_poe_config->poe_port_config[user_port-1].poe_profile_no = poe_port_config->poe_profile_no; // TASK_UNLOCK(); return ERR_NO_ERROR; } /*! *\Function: * sw_poe_get_port_config *\Description: * Get Po_e port config. *\Input: * user_port - the name of profile *\Output: * poe_port_config - port config *\Return: * ERR_NO_ERROR - on success * (Others) - on error *\Note: * n/a */ int sw_poe_get_port_config(int user_port, POE_PORTCFG_STRUCT *poe_port_config) { if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport()) { return ERR_BAD_PARAM; } // TASK_LOCK(); poe_port_config->power_limit = puc_poe_config->poe_port_config[user_port-1].power_limit; poe_port_config->priority = puc_poe_config->poe_port_config[user_port-1].priority; poe_port_config->status = puc_poe_config->poe_port_config[user_port-1].status; poe_port_config->poe_profile_no = puc_poe_config->poe_port_config[user_port-1].poe_profile_no; // TASK_UNLOCK(); return ERR_NO_ERROR; } /*! *\Function: * sw_poe_set_port_config *\Description: * Set Po_e port config. *\Input: * user_port - the name of profile * p_poe_port_config - port config *\Output: * n/a *\Return: * ERR_NO_ERROR - on success * (Others) - on error *\Note: */ int sw_poe_set_port_config_no_recovery(int user_port, POE_PORTCFG_STRUCT *p_poe_port_config) { int rv = 0; int timeseg_dis = 0; int power_dis = 0; int recovery_dis = 0; POE_PORTCFG_STRUCT poe_port_config_old={0}; /* check port legality */ if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport()) { printf("port %d PARAM1 ERROR\n", user_port); return ERR_BAD_PARAM; } /* check port config */ if (NULL == p_poe_port_config) { printf("port %d PARAM2 ERROR\n", user_port); return ERR_BAD_PARAM; } /* Step 1. sw and uc layer */ /* backup the old port config */ rv = sw_poe_get_port_config(user_port, &poe_port_config_old); if((rv) != ERR_NO_ERROR) { printf("port %d sw_poe_get_port_config ERROR\n", user_port); return rv; } /* updt to uc */ printf("userport:%d status:%d\n", user_port, p_poe_port_config->status); rv = uc_poe_set_port_config(user_port, p_poe_port_config); if((rv) != ERR_NO_ERROR) { printf("port %d uc_poe_set_port_config ERROR\n", user_port); return rv; } /* Step 2. ad layer */ /* updt to ad: user setting port state */ printf("updt to ad: userport:%d status:%d\n", user_port, p_poe_port_config->status); //updt g_poe_control. rv = ad_poe_set_port_user_status(user_port, p_poe_port_config->status);/*将用户的端口ENABLE与DISALBE更新的AD层*/ if((rv) != ERR_NO_ERROR) { printf("port %d ad_poe_set_port_user_status ERROR\n", user_port); return rv; } /* updt to ad: priority */ rv = ad_poe_set_port_priority(user_port, p_poe_port_config->priority); if((rv) != ERR_NO_ERROR) { printf("port %d ad_poe_set_port_priority ERROR\n", user_port); return rv; } /* updt to ad: power_limit */ rv = ad_poe_set_port_power_limt(user_port, p_poe_port_config->power_limit); if((rv) != ERR_NO_ERROR) { printf("port %d ad_poe_set_port_power_limt ERROR\n", user_port); return rv; } /* updt to ad: poe_profile_no */ POE_PORT_INFO(user_port).poe_profile_no = p_poe_port_config->poe_profile_no; // if (puc_poe_config->poe_global_config.sys_poe_enable) { /* if the port hasn't been closed by the power or time management. then enable or disalbe the PSE using the port_config->status. */ ad_poe_port_flag_get(user_port,POE_MANAGE_TIMESEG_DIS,&timeseg_dis); ad_poe_port_flag_get(user_port,POE_MANAGE_POWER_DIS,&power_dis); ad_poe_port_flag_get(user_port,POE_MANAGE_RECOVERY_DIS,&recovery_dis); if (timeseg_dis == POE_RELEASE && power_dis == POE_RELEASE && recovery_dis == POE_RELEASE) { rv = ad_poe_set_port_state(user_port, p_poe_port_config->status); if((rv) != ERR_NO_ERROR) { //printf("port %d ERROR\n", user_port); // return rv; } } } return ERR_NO_ERROR ; } int sw_poe_get_global_config(POE_GLOCFG_STRUCT * poe_global_config) { // TASK_LOCK(); int ret = ERR_NO_ERROR; int flag_value = 0; int port_no = 0; ret |= ad_poe_get_sys_power_cons((void*)&(puc_poe_config->poe_global_config.sys_actual_cons)); ret |= ad_poe_get_sys_power_limt((void*)&(puc_poe_config->poe_global_config.sys_power_limit)); ret |= ad_poe_get_sys_power_max((void*)&(puc_poe_config->poe_global_config.sys_power_max)); // poe_global_config->sys_poe_enable = puc_poe_config->poe_global_config.sys_poe_enable; poe_global_config->sys_power_max = puc_poe_config->poe_global_config.sys_power_max; poe_global_config->sys_power_limit = puc_poe_config->poe_global_config.sys_power_limit; poe_global_config->sys_actual_cons = puc_poe_config->poe_global_config.sys_actual_cons; poe_global_config->sys_overload = 0; for (port_no = get_poe_first_uport(); port_no <= get_poe_last_uport(); port_no++) { ad_poe_port_flag_get(port_no,POE_MANAGE_POWER_DIS,&flag_value); if(flag_value != 0) { poe_global_config->sys_overload = 1; break; } } // TASK_UNLOCK(); return ret; } int uc_poe_set_global_config(POE_GLOCFG_STRUCT * poe_global_config) { // TASK_LOCK(); // puc_poe_config->poe_global_config.sys_poe_enable = poe_global_config->sys_poe_enable; puc_poe_config->poe_global_config.sys_power_max = poe_global_config->sys_power_max; puc_poe_config->poe_global_config.sys_power_limit = poe_global_config->sys_power_limit; // sys_actual_cons; only get,cant set // TASK_UNLOCK(); return ERR_NO_ERROR; } /*! *\Function: * sw_poe_set_global_config *\Description: * Set Po_e global config. *\Input: * poe_global_config - global config *\Output: * n/a *\Return: * ERR_NO_ERROR - on success * (Others) - on error *\Note: * n/a */ int sw_poe_set_global_config(POE_GLOCFG_STRUCT * poe_global_config) { int rv; rv = uc_poe_set_global_config(poe_global_config); if((rv) != ERR_NO_ERROR) { return rv; } rv |= ad_poe_set_sys_power_limt(poe_global_config->sys_power_limit); if((rv) != ERR_NO_ERROR) { return rv; } return ERR_NO_ERROR; } ///////////////////////// msg function int sw_poe_port_recovery_restart(int user_port, int status) { int rv = 0; if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport()) { return ERR_BAD_PARAM; } if (status != 0 && status != 1 && status != 2) { return ERR_BAD_PARAM; } if (status == 1) { rv = ad_poe_set_port_state(user_port, POE_DISABLE); if (ERR_NO_ERROR != rv) { printf("[%s] Failed to get Po_e port recovery on %d.\r\n", __FUNCTION__, user_port); return ERR_HW_OP_FAIL; } usleep(300*1000); rv = ad_poe_set_port_state(user_port, POE_ENABLE); if (ERR_NO_ERROR != rv) { printf("[%s] Failed to get Po_e port recovery on %d.\r\n", __FUNCTION__, user_port); return ERR_HW_OP_FAIL; } } return ERR_NO_ERROR; } /*! *\Function: * sw_poe_get_port_in *\Description: * Get Po_e port power supply info. *\Input: * user_port - panel port number *\Output: * poe_port_in - port power supply info *\Return: * ERR_NO_ERROR - on success * (Others) - on error *\Note: * n/a */ int sw_poe_get_port_in(int user_port, power_info_t *poe_port_in) { if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport()) { return ERR_BAD_PARAM; } return ad_poe_get_port_power_info(user_port, poe_port_in); } /*! *\Function: * sw_poe_get_power_reamin_percent *\Description: * Get power remain and percent. *\Input: * n/a *\Output: * power_remain - power remain power_percent - power remain percent *\Return: * ERR_NO_ERROR - on success * (Others) - on error *\Note: * n/a */ int sw_poe_get_power_reamin_percent(float *power_remain, float *power_percent) { int rv; rv = ad_poe_get_sys_power_remain_percent(power_remain, power_percent); if((rv) != ERR_NO_ERROR) { return rv; } return ERR_NO_ERROR ; } int sw_poe_set_port_config(int user_port, POE_PORTCFG_STRUCT * p_poe_port_config) { PRINT_DBG("#### [%s] will goto ping recovery %d.\n", __FUNCTION__, 3); // @wxh dbg int rv = 0; POE_PORTRECOVERYCFG_STRUCT poe_port_recovery_cfg; // POE_PORTRECOVERYCFG_STRUCT poe_port_recovery_uci_cfg; POE_PORTCFG_STRUCT old_poe_port_config = {0}; /* 获取old poe port config */ rv = sw_poe_get_port_config(user_port, &old_poe_port_config); if (ERR_NO_ERROR != rv) { printf("Failed to get port config from puc_poe_config [port_id:%d].\n", user_port); return rv; } rv = sw_poe_set_port_config_no_recovery(user_port, p_poe_port_config); //not support recovery right now // if (ERR_NO_ERROR == rv && 0 != get_include_poe_auto_recovery()) if (0) //dbg wxh { if (POE_DISABLE == p_poe_port_config->status) /* 配置Po_e Disable的时候也要强制Disable Po_e Recovery */ { poe_recovery_get_port_config(user_port, &poe_port_recovery_cfg); if (poe_port_recovery_cfg.status == POE_RECOVERY_ENABLE_UC) { poe_port_recovery_cfg.status = POE_RECOVERY_DISABLE_UC; poe_recovery_set_port_config(user_port, &poe_port_recovery_cfg, FALSE); //@wxh dbg 需要将 user_port 对应与recovery相关的参数置为有效,测试recovery功能 } } /* poe port状态由DISABLE-->ENABLE, 需要打开设置对应的auto recovery */ else if (POE_ENABLE == p_poe_port_config->status && POE_DISABLE == old_poe_port_config.status) { // poe_recovery_get_uclite_config(user_port, &poe_port_recovery_uci_cfg); poe_recovery_get_port_config(user_port, &poe_port_recovery_cfg); /* 根据uci配置的状态进行判断 */ // if (poe_port_recovery_uci_cfg.status == POE_RECOVERY_ENABLE_UC) if (poe_port_recovery_cfg.status == POE_RECOVERY_ENABLE_UC) { poe_recovery_set_port_config(user_port, &poe_port_recovery_cfg, FALSE); } } } return rv; } //not use /* enable[0:disable, 1:enable, 2:No operation] restart[0:ignore, 1:restart, 2:No operation] */ int sw_poe_port_status_set(int user_port, int enable, int restart) { int rv = 0; POE_PORTCFG_STRUCT poe_port_config = {0}; printf("#### [%s] user_port:%d, enable:%d, restart:%d\n", __FUNCTION__, user_port, enable, restart); if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport()) { return ERR_BAD_PARAM; } /* enable[0:disable, 1:enable, 2:No operation] restart[0:ignore, 1:restart, 2:No operation] */ if ((enable != 0 && enable != 1 && enable != 2) || (restart != 0 && restart != 1 && restart != 2)) { return ERR_BAD_PARAM; } if (enable != 2) { rv = sw_poe_get_port_config(user_port, &poe_port_config); if (ERR_NO_ERROR != rv) { printf("[%s] Failed to get Po_e port config on %d.\r\n", __FUNCTION__, user_port); return ERR_HW_OP_FAIL; } poe_port_config.status = enable; rv = sw_poe_set_port_config_no_recovery(user_port, &poe_port_config); if (ERR_NO_ERROR != rv) { printf("[%s] Failed to set Po_e port config on %d.\r\n", __FUNCTION__, user_port); return ERR_HW_OP_FAIL; } } if (restart != 2) { rv = sw_poe_port_recovery_restart(user_port, restart); if (ERR_NO_ERROR != rv) { printf("[%s] Failed to set Po_e port recovery on %d.\r\n", __FUNCTION__, user_port); return ERR_HW_OP_FAIL; } } return ERR_NO_ERROR; } //once set one port int sw_poe_port_set(const POE_OUT_MSG *in_info) { PRINT_DBG("#### [%s] will goto ping recovery %d.\n", __FUNCTION__, 2); // @wxh dbg POE_PORTCFG_STRUCT uci_port_cfg = {0}; // POE_PORTCFG_STRUCT old_port_cfg = {0}; int rv = 0; int user_port = 0; POE_PORT_CFG_STRUCT_UC* port_cfg = (POE_PORT_CFG_STRUCT_UC*)in_info->data.poe_port_cfg; user_port = port_cfg->port_no; if (user_port < get_poe_first_uport() || user_port > get_poe_last_uport()) { printf("Input port_id:%d is invalid\n", user_port); return -1; } uci_port_cfg.poe_profile_no = port_cfg->poe_profile_no; uci_port_cfg.status = port_cfg->poe_enable; uci_port_cfg.priority = port_cfg->priority; uci_port_cfg.power_limit = port_cfg->power_limit; rv = sw_poe_set_port_config(user_port, &uci_port_cfg); if (ERR_NO_ERROR != rv) { printf("Failed to set Po_e port config on %d.\r\n", user_port); return -1; } return ERR_NO_ERROR; } int sw_poe_port_status_get(POE_OUT_MSG *out_info) { // printf("#### goto [%s]\n", __FUNCTION__); power_info_t poe_port_in; int userport = 0; int flag_value = 0; float power_consume = 0; float power_remain; float power_percent; int power_max; int power_limit; for (userport = get_poe_first_uport(); userport <= get_poe_last_uport(); userport++) { sw_poe_get_port_in(userport, &poe_port_in); out_info->data.poe_port_cfg[userport-1].port_no = userport; /* 端口号 */ // out_info->data.poe_port_cfg[userport-1].poe_profile_no = /* profile号 0:none. 1,2,3...:profile num */ out_info->data.poe_port_cfg[userport-1].poe_profile_no = POE_PORT_INFO(userport).poe_profile_no; /* profile号 0:none. 1,2,3...:profile num */ out_info->data.poe_port_cfg[userport-1].poe_enable = POE_PORT_INFO(userport).poe_enable; /* 端口Po_e是否开启 */ out_info->data.poe_port_cfg[userport-1].power_limit = POE_PORT_INFO(userport).power_limit; /* 端口功率限制 unit: 0.1W */ out_info->data.poe_port_cfg[userport-1].priority = POE_PORT_INFO(userport).priority; /* 端口优先级 */ out_info->data.poe_port_cfg[userport-1].actual_state = (poe_port_in.actual_cons ? poe_port_in.power_state : POE_OFF); /* 端口实际上是否在供电 */ out_info->data.poe_port_cfg[userport-1].actual_cur = (poe_port_in.actual_cur); /* 端口实际电流 unit: m_a */ out_info->data.poe_port_cfg[userport-1].actual_vol = poe_port_in.actual_vol; /* 端口实际电压 unit: 0.1V */ out_info->data.poe_port_cfg[userport-1].actual_cons = poe_port_in.actual_cons; /* 端口实际功率 unit: 0.1W */ out_info->data.poe_port_cfg[userport-1].pd_class = (poe_port_in.actual_cons ? (int)poe_port_in.pd_class : 0); /* 端口连接的PD设备class等级 */ out_info->data.poe_port_cfg[userport-1].power_state = (poe_port_in.actual_cons ? poe_port_in.power_state : POE_OFF); /* 端口状态: on off overload short */ // out_info->data.poe_port_cfg[userport].power_state_backup = /* 端口之前状态: on off overload short */ power_consume += ((float)poe_port_in.actual_cons) / 10.0; ad_poe_port_flag_get(userport,POE_MANAGE_PWR_OVERLOAD,&flag_value); if(flag_value == POE_RELEASE) { out_info->data.poe_port_cfg[userport-1].port_overload = 0; } else { out_info->data.poe_port_cfg[userport-1].port_overload = 1; } // printf("#### [%s] out_info, [port_no ] poe_enable:%d priority:%d power_limit:%d\n", __FUNCTION__, out_info->data.poe_port_cfg[userport-1].poe_enalbe, out_info->data.poe_port_cfg[userport-1].priority, out_info->data.poe_port_cfg[userport-1].power_limit); } // printf("\n\n"); ad_poe_get_sys_power_max(&power_max); ad_poe_get_sys_power_limt(&power_limit); sw_poe_get_power_reamin_percent(&power_remain, &power_percent); return ERR_NO_ERROR; } /**************************************************************************************************/ /* EVLOOP_LOCAL_FUNCTIONS */ /**************************************************************************************************/ static void _poeTimerHandler(struct evloop_timeout *pTmo) { api_poe_powerInfoUpdate(); api_poe_portPowerOnManage(); api_poe_info_manage(); api_poe_power_manage(); if (evloop_timeout_set(&l_poe_timer, POE_TIME_INTERVAL) < 0) { PRINT_ERR("failed to do evloop_timeout_set\n"); } } static void _poeFlagTimerHandler(struct evloop_timeout *pTmo) { // if((0 == puc_poe_config->poe_global_config.sys_poe_enable)) // { // PRINT_DBG("The function is not enable, return!"); // return; // } ad_poe_flag_manage(); if (evloop_timeout_set(&l_poe_flag_timer, POE_FLAG_TIME_INTERVAL) < 0) { PRINT_ERR("failed to do evloop_timeout_set\n"); } } static void _poeClientEventHandler(struct evloop_fd *pEvent, unsigned int flag) { int len = 0; int ret = 0; POE_OUT_MSG msg; //@wxh 重构优化(),POE_GLOCFG_STRUCT在poe.h中定义,定义与 unix_sock_msgDef.h 中的 POE_GLOBAL_CFG_STRUCT_UC 完全相同 POE_GLOCFG_STRUCT l_poe_gloCfg = {0}; //@wxh 增加的定义 POE_RECOVERY_STATUS_UC l_poe_recovery_gloCfg = -1; // POE_RECOVERY_DISABLE_UC = 0; POE_RECOVERY_ENABLE_UC = 1; 无效的glob status -1 if (NULL == pEvent) { return; } /* recv hearder */ if (0 != unix_sock_recvHeader(pEvent->fd, UNIX_MSG_POE_MSG, &len)) { PRINT_ERR("unix_sock_recvHeader failed!"); goto leave; } if (len != sizeof(msg)) { PRINT_ERR("len(%d) is invalid.", len); goto leave; } memset(&msg, 0, sizeof(msg)); /* recv data */ if (0 != unix_sock_recvData(pEvent->fd, len, (UINT8 *)&msg)) { PRINT_ERR("recv data from client failed"); goto leave; } switch (msg.type) { case POE_MSG_TYPE_GET_GLOBAL_STATUS: // ok { // PRINT_DBG("-------------POE_MSG_TYPE_GET_GLOBAL_STATUS-------------\n"); // @wxh dbg sw_poe_get_global_config((void *)&l_poe_gloCfg); if (0 != unix_sock_sendHeader(pEvent->fd, sizeof(POE_GLOCFG_STRUCT), UNIX_MSG_POE_MSG)) { PRINT_ERR("send header failed"); goto leave; } if (0 != unix_sock_sendData(pEvent->fd, (UINT8 *)&l_poe_gloCfg, sizeof(POE_GLOCFG_STRUCT))) { PRINT_ERR("send data faield"); goto leave; } } break; case POE_MSG_TYPE_SET_GLOBAL_STATUS: // ok //CMD_POE_GLOBAL_SET { PRINT_DBG("-------------POE_MSG_TYPE_SET_GLOBAL_STATUS-------------\n"); // @wxh dbg // sw_poe_global_set() // l_poe_gloCfg.sys_poe_enable = msg.data.poe_global_cfg.sys_poe_enable; // l_poe_gloCfg.sys_power_max = msg.data.poe_global_cfg.sys_power_max; //sys_power_max cant be set l_poe_gloCfg.sys_power_limit = msg.data.poe_global_cfg.sys_power_limit; // not set sys_actual_cons.it can only be read. if (0 != sw_poe_set_global_config((void *)&l_poe_gloCfg)) { PRINT_ERR("sw_poe_set_global_config failed"); goto leave; } // if (puc_poe_config->poe_global_config.sys_poe_enable) // { if (evloop_timeout_set(&l_poe_timer, POE_TIME_INTERVAL) < 0) { PRINT_ERR("failed to do evloop_timeout_set\n"); goto leave; } if (evloop_timeout_set(&l_poe_flag_timer, POE_FLAG_TIME_INTERVAL) < 0) { PRINT_ERR("failed to do evloop_timeout_set\n"); goto leave; } // } // else // { // api_poe_powerInfoUpdate(); // } if (0 != unix_sock_sendHeader(pEvent->fd, sizeof(ret), UNIX_MSG_POE_MSG)) { PRINT_ERR("send header failed"); goto leave; } if (0 != unix_sock_sendData(pEvent->fd, (UINT8 *)&ret, sizeof(ret))) { PRINT_ERR("send data faield"); goto leave; } } break; case POE_MSG_TYPE_GET_PORT_CONFIG: // CMD_POE_STATUS_GET { // PRINT_DBG("-------------POE_MSG_TYPE_GET_PORT_CONFIG-------------\n"); // @wxh dbg memset(&msg, 0, sizeof(msg)); sw_poe_port_status_get((void *)&msg); if (0 != unix_sock_sendHeader(pEvent->fd, sizeof(POE_OUT_MSG), UNIX_MSG_POE_MSG)) { PRINT_ERR("send header failed"); goto leave; } if (0 != unix_sock_sendData(pEvent->fd, (UINT8 *)&msg, sizeof(POE_OUT_MSG))) { PRINT_ERR("send data faield"); goto leave; } } break; case POE_MSG_TYPE_SET_PORT_CONFIG: // CMD_POE_PORT_SET, CMD_POE_STATUS_SET { PRINT_DBG("-------POE_MSG_TYPE_SET_PORT_CONFIG------- will goto ping recovery %d.\n", 1); // @wxh dbg sw_poe_port_set((void *)&msg); // ok // sw_poe_port_status_set() //do we need? if (0 != unix_sock_sendHeader(pEvent->fd, sizeof(ret), UNIX_MSG_POE_MSG)) { PRINT_ERR("send header failed"); goto leave; } if (0 != unix_sock_sendData(pEvent->fd, (UINT8 *)&ret, sizeof(ret))) { PRINT_ERR("send data faield"); goto leave; } } break; case POE_RECOVRY_MSG_TYPE_SET_GLOBAL_STATUS: { PRINT_DBG("-------------POE_RECOVRY_MSG_TYPE_SET_GLOBAL_STATUS-------------\n"); // @wxh dbg printf("#### in case POE_RECOVRY_MSG_TYPE_SET_GLOBAL_STATUS: \n"); printf("#### msg.data.poe_recovery_global_cfg = %d \n", msg.data.poe_recovery_global_cfg); if (ERR_NO_ERROR != poe_recovery_set_global_status(msg.data.poe_recovery_global_cfg)) { PRINT_ERR("poe_recovery_set_global_status failed\n"); } if (0 != unix_sock_sendHeader(pEvent->fd, sizeof(ret), UNIX_MSG_POE_MSG)) { PRINT_ERR("send header failed"); goto leave; } if (0 != unix_sock_sendData(pEvent->fd, (UINT8 *)&ret, sizeof(ret))) { PRINT_ERR("send data faield"); goto leave; } } break; case POE_RECOVRY_MSG_TYPE_GET_GLOBAL_STATUS: { PRINT_DBG("-------------POE_RECOVRY_MSG_TYPE_GET_GLOBAL_STATUS-------------\n"); // @wxh dbg if (ERR_NO_ERROR != poe_recovery_get_global_status((void *)&l_poe_recovery_gloCfg)) { PRINT_ERR("poe_recovery_get_global_status failed\n"); } if (0 != unix_sock_sendHeader(pEvent->fd, sizeof(POE_RECOVERY_STATUS_UC), UNIX_MSG_POE_MSG)) { PRINT_ERR("send header failed"); goto leave; } if (0 != unix_sock_sendData(pEvent->fd, (UINT8 *)&l_poe_recovery_gloCfg, sizeof(POE_RECOVERY_STATUS_UC))) { PRINT_ERR("send data faield"); goto leave; } } break; case POE_RECOVRY_MSG_TYPE_SET_PORT_CONFIG: // CMD_POE_AUTO_RECOVERY_CONFIG_SET { PRINT_DBG("-------------POE_RECOVRY_MSG_TYPE_SET_PORT_CONFIG-------------\n"); // @wxh dbg if (ERR_NO_ERROR != poe_auto_recovery_config_set((void *)&msg)) { PRINT_ERR("poe_auto_recovery_config_set failed\n"); } if (0 != unix_sock_sendHeader(pEvent->fd, sizeof(ret), UNIX_MSG_POE_MSG)) { PRINT_ERR("send header failed"); goto leave; } if (0 != unix_sock_sendData(pEvent->fd, (UINT8 *)&ret, sizeof(ret))) { PRINT_ERR("send data faield"); goto leave; } } break; case POE_RECOVRY_MSG_TYPE_GET_PORT_CONFIG: // CMD_POE_AUTO_RECOVERY_STATUS_GET: //@wxh not use { PRINT_DBG("-------------POE_RECOVRY_MSG_TYPE_GET_PORT_CONFIG-------------\n"); // @wxh dbg memset(&msg, 0, sizeof(msg)); if (ERR_NO_ERROR != poe_auto_recovery_status_get((void *)&msg)) { PRINT_ERR("poe_auto_recovery_status_get failed"); } if (0 != unix_sock_sendHeader(pEvent->fd, sizeof(POE_OUT_MSG), UNIX_MSG_POE_MSG)) { PRINT_ERR("send header failed"); goto leave; } if (0 != unix_sock_sendData(pEvent->fd, (UINT8 *)&msg, sizeof(POE_OUT_MSG))) { PRINT_ERR("send data faield"); goto leave; } } break; case POE_RECOVRY_MSG_TYPE_DEL_PORT_CONFIG: { // CMD_POE_AUTO_RECOVERY_CONFIG_SET PRINT_DBG("-------------POE_RECOVRY_MSG_TYPE_DEL_PORT_CONFIG-------------\n"); // @wxh dbg if (ERR_NO_ERROR != poe_auto_recovery_config_del((void *)&msg)) { PRINT_ERR("poe_auto_recovery_config_del failed"); } } break; default: PRINT_ERR("invalid msg.type: %d\n", msg.type); } leave: evloop_fd_del(pEvent, flag); CLOSE(pEvent->fd); return; } static void _poeListenEventHandler(struct evloop_fd *pEvent, unsigned int flag) { struct sockaddr_un cliaddr; int clifd = POE_INVALID_FD; socklen_t cliaddrlen = 0; if (NULL == pEvent) { return; } memset(&cliaddr, 0, sizeof(cliaddr)); cliaddrlen = sizeof(cliaddr); clifd = accept(pEvent->fd, (struct sockaddr *)&cliaddr, &cliaddrlen); if (clifd == -1) { PRINT_ERR("Accept failed: %s\n", strerror(errno)); goto leave; } if (POE_INVALID_FD != l_poe_client_event_fd.fd) { PRINT_ERR("previous msg still processing...pre=%d new=%d\n", l_poe_client_event_fd.fd, clifd); goto leave; } // PRINT_DBG("Accept a new client %d.\n", clifd); l_poe_client_event_fd.fd = clifd; if (0 != evloop_fd_add(&l_poe_client_event_fd, EVLOOP_READ)) { PRINT_ERR("Failed to add client event!\n"); goto leave; } return; leave: CLOSE(clifd); return; } static int _poeEventRegister(void) { int ret = 0; if (0 != (unix_sock_initSrv(UNIX_SOCK_ID_POE, &l_sockFd, 10))) { PRINT_ERR("unix_sock_initSrv error\n"); return -1; } l_poe_listen_event_fd.fd = l_sockFd.fd; ret = evloop_fd_add(&l_poe_listen_event_fd, EVLOOP_READ); if (ret < 0) { PRINT_ERR("Failed to add unix socket event %s.\n", strerror(errno)); return -1; } return ERR_NO_ERROR; } static int _poe_init(void) { puc_poe_config = (POE_USRCONFIG_STRUCT*)malloc(sizeof(POE_USRCONFIG_STRUCT)); puc_poe_config->poe_port_config = (POE_PORTCFG_STRUCT*)malloc(sizeof(POE_PORTCFG_STRUCT) * get_poe_last_uport()); if (0 != api_poe_init()) { PRINT_ERR("api_poe_init failed\n"); return -1; } if (0 != _poeEventRegister()) { PRINT_ERR("Failed to init netlink event.\n"); return -1; } return ERR_NO_ERROR; } static int _poe_fini(void) { if (evloop_timeout_cancel(&l_poe_timer) < 0) { PRINT_ERR("Failed to cancel l_poe_timer.\n"); } if (evloop_timeout_cancel(&l_poe_flag_timer) < 0) { PRINT_ERR("Failed to cancel l_poe_flag_timer.\n"); } if (unix_sock_close(&l_sockFd) < 0) { PRINT_ERR("Failed to close unix socket l_sockFd.\n"); } l_poe_listen_event_fd.fd = POE_INVALID_FD; free(puc_poe_config->poe_port_config); puc_poe_config->poe_port_config = NULL; free(puc_poe_config); puc_poe_config = NULL; return ERR_NO_ERROR; } void handle_poe_cli_commands(int argc, char *argv[]) { int i = 0; if (argc == 1) return; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-h") == 0) { printf( "Usage: poe without [OPTIONS]\n" " poe Start the PoE process\n\n" "Usage: poe [OPTIONS]\n" " [OPTIONS]\n" " -r, poe -r [pseAddr] Read value at [pseAddr].\n" " -w, poe -w [pseAddr] [value] Write [value] to [pseAddr].\n" " -s, Show value of key pseAddr.\n" " -h, Display usage\n\n" "pseAddr: <Register Name> <Addr> <Type> <Bits>\n" " Port Enable (0x05) Read/Write D[4:0]\n" " Power On/Off Trigger (0x11) Read/Write D[7:0]\n" " ICUT2 Threshold (0x14) Read/Write D[2:0]\n" " ILIM2 Threshold (0x18) Read/Write D[0]\n" " DET/CLS Complete Status (0x25) Read and Clear D[7:0]\n" " Power Status (0x2A) Read D[7:0]\n" "\n\n" ); exit(0); } else if (strcmp(argv[i], "-s") == 0) { ad_poe_updt_event_status_mp3924_print(); exit(0); } else if (strcmp(argv[i], "-r") == 0 && (i + 1) < argc) { UINT8 val_dec = 0; UINT8 addr_hex = strtol(argv[i + 1], NULL, 16); ad_poe_i2c_read(addr_hex, &val_dec); printf("poe read 0x%02x = 0x%02x \n", addr_hex, val_dec); exit(0); } else if (strcmp(argv[i], "-w") == 0 && (i + 2) < argc) { UINT8 addr_hex = strtol(argv[i + 1], NULL, 16); UINT8 val_hex = strtol(argv[i + 2], NULL, 16); ad_poe_i2c_write(addr_hex, val_hex); printf("poe read 0x%02x = 0x%02x \n", addr_hex, val_hex); exit(0); } } printf("Error: Invalid command or missing parameters\n"); exit(1); } /**************************************************************************************************/ /* PUBLIC_FUNCTIONS */ /**************************************************************************************************/ /**************************************************************************************************/ /* GLOBAL_FUNCTIONS */ /**************************************************************************************************/ int main(int argc, char *argv[]) { // if (1 != argc) if (0) { int i = 0; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-h") == 0) { printf( "Usage: poe without [OPTIONS] \n" " poe, start the PoE proess\n\n" "Usage: poe [OPTIONS] \n" "[OPTIONS] \n" " -r, poe -r [pseAddr] Read value at [pseAddr].\n" " -w, poe -w [pseAddr] [value] Write [value] to [pseAddr].\n" " -s, Show value of key pseAddr.\n" " -h, Display usage\n"); exit(0); } if (strcmp(argv[i], "-s") == 0) { ad_poe_updt_event_status_mp3924_print(); exit(0); } if (strcmp(argv[i], "-r") == 0) { char *input_str = argv[i + 1]; UINT8 val_dec = 0; UINT8 val_hex = strtol(input_str, NULL, 16); ad_poe_i2c_read(val_hex, &val_dec); printf("poe read %s = 0x%x \n", input_str, val_dec); printf("poe read 0x%02x = 0x%02x \n", val_hex, val_dec); exit(0); } if (strcmp(argv[i], "-w") == 0) { char *input_str1 = argv[i + 1]; char *input_str2 = argv[i + 2]; printf("poe wrtie %s = 0x%s\n", input_str1, input_str2); UINT8 addr_hex = strtol(input_str1, NULL, 16); UINT8 value_hex = strtol(input_str2, NULL, 16); ad_poe_i2c_write(addr_hex, value_hex); exit(0); } } exit(0); } handle_poe_cli_commands(argc, argv); if (!DEVCFG_DLPORT_POEOUT_IS_SUPP()) { PRINT_ERR("Unsupport poeout, exit poe\n"); return ERR_NO_ERROR; } if (daemon(0, 1) < 0) { perror("poe daemon"); exit(-1); } evloop_init(); if (0 != _poe_init()) { goto exit; } //@wxh dbg if( 0 != poe_recovery_init() ) { PRINT_ERR("sw_poe_recovery init failed!\n"); goto exit; } evloop_run(); exit: evloop_done(); _poe_fini(); return -1; } //这段代码中sw_和uc_开头的命名都不符合现在的需求,办我依照函数作用分类重命名这些函数,给出重命名前后的函数名和为什么重命名这些函数。还有一些函数是没有用到的,没有用到的函数名字整理给我 //增加提示,函数调用中含义ad_开头的函数是apiPoeMP3925.c中的函数,apiPoeMP3925.c是产商提供的操作MP3925的poe芯片寄存器和事件终端等的代码文件。ad开头的函数不需要更改他的名字。重新整理上一行我的需求
10-24
以及DnsSec的CLI诊断程序该怎么写? static int dnsSecurityDnssecDiagnoseHandle(UI_CTX *ctx) { JSON_Object *root = ctx->param; JSON_Object *params = root ? json_object_get_object(root, "params") : NULL; const char *domain = params ? json_object_get_string(params, "domain") : NULL; const char *type = params ? json_object_get_string(params, "type") : NULL; const char *dns_server = params ? json_object_get_string(params, "dns_server") : NULL; if (!domain || !type || !dns_server) { return write_bad_param_rsp(ctx); // 参数错误 } DNSSEC_DIAGNOSE_RESULT_T *result = NULL; APPL_ERRCODE ret = libDnsProxyShellDnssecDiagnose(domain, type, dns_server, &result); // 查询失败时,返回 87002 错误码 if (ret != ERR_NO_ERROR) { libDnsProxyShellDnssecResultFree(result); return write_bad_param_rsp(ctx); } // 构造成功响应 JSON_Value *rspVal = json_value_init_object(); JSON_Object *rspObj = json_value_get_object(rspVal); JSON_Value *resultVal = json_value_init_object(); JSON_Object *resultObj = json_value_get_object(resultVal); // 创建 ip 数组和 verify 数组 JSON_Value *ipArrVal = json_value_init_array(); JSON_Array *ipArr = json_value_get_array(ipArrVal); JSON_Value *verifyArrVal = json_value_init_array(); JSON_Array *verifyArr = json_value_get_array(verifyArrVal); if (result && result->result_count > 0) { for (int i = 0; i < result->result_count; i++) { json_array_append_string(ipArr, result->results[i].ip); json_array_append_string(verifyArr, result->results[i].status); } } json_object_set_value(resultObj, "verify", verifyArrVal); json_object_set_string(resultObj, "type", type); json_object_set_string(resultObj, "name", domain); json_object_set_value(resultObj, "ip", ipArrVal); libDnsProxyShellDnssecResultFree(result); // 释放内存 // 最终响应 json_object_set_number(rspObj, "id", 1); json_object_set_value(rspObj, "result", resultVal); json_object_set_string(rspObj, "error_code", "0"); ctx->rsp = rspVal; return ERR_NO_ERROR; }
10-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤劳的凌菲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值