#include #include #include #include #include #include #include #include #include #include #include enum { OLP_SET_HEARTBEAT_SWITCH = 0, OLP_SET_WORK_MODE = 1, OLP_SET_WORK_LINE = 2, OLP_SET_SWITCHBACK_MODE = 3, OLP_HEARTBEAT_PACKET = 4, OLP_GET_CHANNEL_INFO = 5, OLP_GET_CHANNEL_CONF = 6, OLP_OBJECT_MAX }; struct olp6500_packet { uint8_t action; uint8_t slot; uint16_t object; uint8_t port; uint8_t len; char data[0]; } __attribute__((packed)); unsigned int g_logger_to_stdout = 1; unsigned int g_logger_level = LOG_DEBUG; unsigned int g_keep_running = 1; #define BUFF_MAX 256 #define NR_OLP6500_CHANNEL_PER_DEVICE_MAX (17) extern int olp6500_packet_construct(struct olp_channel * channel, int type, char * buff); extern int olp6500_check_recv_data(char * buff, uint16_t object); extern int olp6500_apply_control_command_to_peer(struct olp_dev_desc * dev_desc, uint32_t channel_id); extern int olp6500_destroy_timer(struct olp_device * olp_dev); void olp_dev_desc_init(struct olp_dev_desc * dev_desc, struct olp_channel * channel) { snprintf(dev_desc->devsym, sizeof(dev_desc->devsym), "olp0"); dev_desc->type = OLP_DEVICE_TYPE_OLP_6500; channel->olp_channel_id = 1; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = dev_desc; dev_desc->channels = channel; } static void testcase_heartbeat_set_packet_construct(void ** state) { int pkt_len = 0; char buff[BUFF_MAX] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; memset(&dev_desc, 0, sizeof(dev_desc)); memset(&dev_channel, 0, sizeof(dev_channel)); olp_dev_desc_init(&dev_desc, &dev_channel); pkt_len = olp6500_packet_construct(&dev_desc.channels[0], OLP_SET_HEARTBEAT_SWITCH, buff); assert_true(pkt_len == 10); assert_true(buff[0] == 0x01); assert_true(buff[1] == 0x01); assert_true(buff[2] == 0x06); assert_true(buff[3] == 0x00); assert_true(buff[4] == 0x00); assert_true(buff[5] == 0x04); assert_true(buff[6] == 0x01); assert_true(buff[7] == 0x01); assert_true(buff[8] == 0x03); assert_true(buff[9] == 0x03); } static void testcase_work_mode_set_packet_construct(void ** state) { int pkt_len = 0; char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; memset(&dev_desc, 0, sizeof(dev_desc)); memset(&dev_channel, 0, sizeof(dev_channel)); olp_dev_desc_init(&dev_desc, &dev_channel); pkt_len = olp6500_packet_construct(&dev_desc.channels[0], OLP_SET_WORK_MODE, buff); assert_true(pkt_len == 7); assert_true(buff[0] == 0x01); assert_true(buff[1] == 0x01); assert_true(buff[2] == 0x10); assert_true(buff[3] == 0x10); assert_true(buff[4] == 0x00); assert_true(buff[5] == 0x01); assert_true(buff[6] == 0x01); } static void testcase_work_line_set_packet_construct(void ** state) { int pkt_len = 0; char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; memset(&dev_desc, 0, sizeof(dev_desc)); memset(&dev_channel, 0, sizeof(dev_channel)); olp_dev_desc_init(&dev_desc, &dev_channel); pkt_len = olp6500_packet_construct(&dev_desc.channels[0], OLP_SET_WORK_LINE, buff); assert_true(pkt_len == 7); assert_true(buff[0] == 0x01); assert_true(buff[1] == 0x01); assert_true(buff[2] == 0x10); assert_true(buff[3] == 0x70); assert_true(buff[4] == 0x00); assert_true(buff[5] == 0x01); assert_true(buff[6] == 0x30); } static void testcase_switchback_mode_set_packet_construct(void ** state) { int pkt_len = 0; char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; memset(&dev_desc, 0, sizeof(dev_desc)); memset(&dev_channel, 0, sizeof(dev_channel)); olp_dev_desc_init(&dev_desc, &dev_channel); pkt_len = olp6500_packet_construct(&dev_desc.channels[0], OLP_SET_SWITCHBACK_MODE, buff); assert_true(pkt_len == 7); assert_true(buff[0] == 0x01); assert_true(buff[1] == 0x01); assert_true(buff[2] == 0x10); assert_true(buff[3] == 0x20); assert_true(buff[4] == 0x00); assert_true(buff[5] == 0x01); assert_true(buff[6] == 0x02); } static void testcase_heartbeat_packet_construct(void ** state) { int pkt_len = 0; char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; memset(&dev_desc, 0, sizeof(dev_desc)); memset(&dev_channel, 0, sizeof(dev_channel)); olp_dev_desc_init(&dev_desc, &dev_channel); pkt_len = olp6500_packet_construct(&dev_desc.channels[0], OLP_HEARTBEAT_PACKET, buff); assert_true(pkt_len == 6); assert_true(buff[0] == 0x00); assert_true(buff[1] == 0x01); assert_true(buff[2] == 0x06); assert_true(buff[3] == 0x01); assert_true(buff[4] == 0x00); assert_true(buff[5] == 0x00); } static void testcase_channel_info_packet_construct(void ** state) { int pkt_len = 0; char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; memset(&dev_desc, 0, sizeof(dev_desc)); memset(&dev_channel, 0, sizeof(dev_channel)); olp_dev_desc_init(&dev_desc, &dev_channel); pkt_len = olp6500_packet_construct(&dev_desc.channels[0], OLP_GET_CHANNEL_INFO, buff); assert_true(pkt_len == 6); assert_true(buff[0] == 0x00); assert_true(buff[1] == 0x01); assert_true(buff[2] == 0x02); assert_true(buff[3] == 0x00); assert_true(buff[4] == 0x00); assert_true(buff[5] == 0x00); } static void testcase_channel_conf_get_packet_construct(void ** state) { int pkt_len = 0; char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; memset(&dev_desc, 0, sizeof(dev_desc)); memset(&dev_channel, 0, sizeof(dev_channel)); olp_dev_desc_init(&dev_desc, &dev_channel); pkt_len = olp6500_packet_construct(&dev_desc.channels[0], OLP_GET_CHANNEL_CONF, buff); assert_true(pkt_len == 6); assert_true(buff[0] == 0x00); assert_true(buff[1] == 0x01); assert_true(buff[2] == 0x05); assert_true(buff[3] == 0x01); assert_true(buff[4] == 0x00); assert_true(buff[5] == 0x00); } static void testcase_heartbeat_switch_set_packet_recv(void ** state) { int ret = 0; unsigned char data_succ[] = {0x01, 0x00, 0x06, 0x00, 0x00, 0x04, 0x01, 0x01, 0x05, 0x03}; unsigned char data_fail[] = {0x01, 0x00, 0x86, 0x00, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_SET_HEARTBEAT_SWITCH); assert_true(ret == 0); ret = olp6500_check_recv_data((char *)data_fail, OLP_SET_HEARTBEAT_SWITCH); assert_true(ret == -1); } static void testcase_work_mode_set_packet_recv(void ** state) { int ret = 0; unsigned char data_succ[] = {0x01, 0x00, 0x10, 0x10, 0x00, 0x01, 0x02}; unsigned char data_fail[] = {0x01, 0x00, 0x90, 0x10, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_SET_WORK_MODE); assert_true(ret == 0); ret = olp6500_check_recv_data((char *)data_fail, OLP_SET_WORK_MODE); assert_true(ret == -1); } static void testcase_work_line_set_packet_recv(void ** state) { int ret = 0; unsigned char data_succ[] = {0x01, 0x00, 0x10, 0x70, 0x00, 0x01, 0x30}; unsigned char data_fail[] = {0x01, 0x00, 0x90, 0x70, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_SET_WORK_LINE); assert_true(ret == 0); ret = olp6500_check_recv_data((char *)data_fail, OLP_SET_WORK_LINE); assert_true(ret == -1); } static void testcase_switchback_mode_set_packet_recv(void ** state) { int ret = 0; unsigned char data_succ[] = {0x01, 0x00, 0x10, 0x20, 0x00, 0x01, 0x02}; unsigned char data_fail[] = {0x01, 0x00, 0x90, 0x20, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_SET_SWITCHBACK_MODE); assert_true(ret == 0); ret = olp6500_check_recv_data((char *)data_fail, OLP_SET_SWITCHBACK_MODE); assert_true(ret == -1); } static void testcase_heartbeat_packet_recv(void ** state) { int ret = 0; unsigned char data_succ[] = {0x00, 0x00, 0x06, 0x01, 0x00, 0x00}; unsigned char data_fail[] = {0x00, 0x00, 0x86, 0x01, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_HEARTBEAT_PACKET); assert_true(ret == 0); ret = olp6500_check_recv_data((char *)data_fail, OLP_HEARTBEAT_PACKET); assert_true(ret == -1); } static void testcase_channel_info_get_packet_recv(void ** state) { int ret = 0; unsigned char data_succ[] = {0x00, 0x00, 0x02, 0x00, 0x00, 0x00}; unsigned char data_fail[] = {0x00, 0x00, 0x82, 0x00, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_GET_CHANNEL_INFO); assert_true(ret == 0); ret = olp6500_check_recv_data((char *)data_fail, OLP_GET_CHANNEL_INFO); assert_true(ret == -1); } static void testcase_channel_conf_get_packet_recv(void ** state) { int ret = 0; unsigned char data_succ[] = {0x00, 0x00, 0x05, 0x01, 0x00, 0x00}; unsigned char data_fail[] = {0x00, 0x00, 0x85, 0x01, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_GET_CHANNEL_CONF); assert_true(ret == 0); ret = olp6500_check_recv_data((char *)data_fail, OLP_GET_CHANNEL_CONF); assert_true(ret == -1); } static void channel_1_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 1; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_2_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 2; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_3_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 3; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 0; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_4_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 4; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 0; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 0); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_5_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 5; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_BYPASS; channel->en_heartbeat = 0; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 0); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_6_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 6; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_7_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 7; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_8_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 8; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_9_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 9; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_10_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 10; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_11_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 11; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_12_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 12; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_13_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 13; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_14_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 14; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_15_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 15; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } static void channel_16_ctrl_cmd(void ** state) { int ret = 0; struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); memset(olp_channel, 0, sizeof(olp_channel)); channel = &(olp_channel[1]); channel->olp_channel_id = 16; channel->used = OLP_STATE_USED; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; channel->heartbeat_timeout_interval_in_ms = 60; channel->heartbeat_send_interval_in_ms = 60; channel->heartbeat_lost_threshold = 3; channel->nonrevertive_mode = 1; channel->dev_desc = &olp_dev_desc; olp_dev_desc.used = OLP_STATE_USED; olp_dev_desc.conn_type = OLP_CONNECT_TYPE_NETWORK; olp_dev_desc.network.addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &olp_dev_desc.network.addr.sin_addr); olp_dev_desc.network.addr.sin_port = htons(6800); snprintf(olp_dev_desc.devsym, sizeof(olp_dev_desc.devsym), "olp0"); olp_dev_desc.type = OLP_DEVICE_TYPE_OLP_6500; olp_dev_desc.channels = olp_channel; olp_dev.olp_dev_descs[olp_dev.nr_olp_dev_descs++] = &olp_dev_desc; ret = olp6500_apply_control_command_to_peer(&olp_dev_desc, 1); assert_true(ret == 0); assert_true(channel->timer.enable == 1); assert_true(channel->runtime.errcode == 0); ret = olp6500_destroy_timer(&olp_dev); assert_true(ret == 0); assert_true(channel->timer.enable == 0); } void * olp_device_server(void * args) { int ret = 0; int sock = 0; char send_buff[64] = {0}; char recv_buff[64] = {0}; struct sockaddr_in addr; struct sockaddr cli_addr; socklen_t cli_len = sizeof(cli_addr); struct olp6500_packet * pkt = NULL; pthread_detach(pthread_self()); sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) return 0; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(6800); addr.sin_addr.s_addr = htonl(INADDR_ANY); bind(sock, (struct sockaddr *)&addr, sizeof(addr)); while (1) { memset(recv_buff, 0, sizeof(recv_buff)); memset(send_buff, 0, sizeof(send_buff)); memset(&cli_addr, 0, sizeof(cli_addr)); ret = recvfrom(sock, recv_buff, sizeof(recv_buff), 0, &cli_addr, &cli_len); if (ret == -1) { printf("recvfrom fail! error:%s\n", strerror(errno)); continue; } pkt = (struct olp6500_packet *)recv_buff; ret = sendto(sock, recv_buff, pkt->len + 6, 0, &cli_addr, cli_len); if (ret == -1) { printf("sendto fail! error:%s\n", strerror(errno)); continue; } } return NULL; } int main(int argc, char * argv[]) { int ret = 0; pthread_t tid; ret = pthread_create(&tid, NULL, olp_device_server, NULL); if (ret != 0) { MR_ERROR("olp devices server start error: %s", strerror(ret)); return -1; } const struct CMUnitTest group_packet_construct[] = { cmocka_unit_test(testcase_heartbeat_set_packet_construct), cmocka_unit_test(testcase_work_mode_set_packet_construct), cmocka_unit_test(testcase_work_line_set_packet_construct), cmocka_unit_test(testcase_switchback_mode_set_packet_construct), cmocka_unit_test(testcase_heartbeat_packet_construct), cmocka_unit_test(testcase_channel_info_packet_construct), cmocka_unit_test(testcase_channel_conf_get_packet_construct), }; const struct CMUnitTest group_packet_recv[] = { cmocka_unit_test(testcase_heartbeat_switch_set_packet_recv), cmocka_unit_test(testcase_work_mode_set_packet_recv), cmocka_unit_test(testcase_work_line_set_packet_recv), cmocka_unit_test(testcase_switchback_mode_set_packet_recv), cmocka_unit_test(testcase_heartbeat_packet_recv), cmocka_unit_test(testcase_channel_info_get_packet_recv), cmocka_unit_test(testcase_channel_conf_get_packet_recv), }; const struct CMUnitTest group_control_command[] = { cmocka_unit_test(channel_1_ctrl_cmd), cmocka_unit_test(channel_2_ctrl_cmd), cmocka_unit_test(channel_3_ctrl_cmd), cmocka_unit_test(channel_4_ctrl_cmd), cmocka_unit_test(channel_5_ctrl_cmd), cmocka_unit_test(channel_6_ctrl_cmd), cmocka_unit_test(channel_7_ctrl_cmd), cmocka_unit_test(channel_8_ctrl_cmd), cmocka_unit_test(channel_9_ctrl_cmd), cmocka_unit_test(channel_10_ctrl_cmd), cmocka_unit_test(channel_11_ctrl_cmd), cmocka_unit_test(channel_12_ctrl_cmd), cmocka_unit_test(channel_13_ctrl_cmd), cmocka_unit_test(channel_14_ctrl_cmd), cmocka_unit_test(channel_15_ctrl_cmd), cmocka_unit_test(channel_16_ctrl_cmd), }; // 0 on success, or the number of failed tests. ret = cmocka_run_group_tests(group_packet_construct, NULL, NULL); ret += cmocka_run_group_tests(group_packet_recv, NULL, NULL); ret += cmocka_run_group_tests(group_control_command, NULL, NULL); return ret; }