#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static void handler_command_set_schedule(mpack_node_t map, controller_t *controller) { uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM)); int uid_size = sizeof(uuid_t); uuid_t schedule_id; memcpy(schedule_id, mpack_node_data(mpack_node_map_uint(map, COMMAND_MAPPING_SCHEDULE_ID)), uid_size); uint16_t periods_count = mpack_node_u16(mpack_node_map_uint(map, COMMAND_MAPPING_PERIODS_COUNT)); uint16_t *periods = (uint16_t*)mpack_node_bin_data(mpack_node_map_uint(map, COMMAND_MAPPING_PERIODS_BLOB)); relay_t *target_relay = controller->relays[relay_num]; if(target_relay->schedule) { schedule_free(target_relay->schedule); } target_relay->schedule = schedule_create(schedule_id, periods_count, periods); relay_debug(target_relay); } static void handler_command_set_relay_name(mpack_node_t map, controller_t *controller) { uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM)); const char *relay_name = mpack_node_str(mpack_node_map_uint(map, COMMAND_MAPPING_NAME)); if(relay_num < controller->relay_count) { relay_set_name(controller->relays[relay_num], relay_name); } relay_debug(controller->relays[relay_num]); } void handler_command(int fd, controller_t *controller) { struct sockaddr_storage their_addr; socklen_t addr_size; int client_fd; addr_size = sizeof(their_addr); if((client_fd = accept(fd, (struct sockaddr *) &their_addr, &addr_size)) < 0) { LOG_ERROR("could not accept client: %s", strerror(errno)); return; } uint32_t payload_length; if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0) { LOG_ERROR("unable to receive header: %s", strerror(errno)); return; } void *payload = malloc((payload_length + 1)); ssize_t bytes_transferred; if((bytes_transferred = recv(client_fd, payload, payload_length, 0)) <= 0) { LOG_ERROR("unable to receive payload: %s", strerror(errno)); return; } mpack_tree_t tree; mpack_tree_init_data(&tree, payload, payload_length); mpack_tree_parse(&tree); mpack_node_t root = mpack_tree_root(&tree); uint8_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE)); LOG_INFO("received command %d", command_code); switch(command_code) { case COMMAND_CODE_GET_TIME: break; case COMMAND_CODE_GET_ID: break; case COMMAND_CODE_SET_NAME: controller_set_name(controller, mpack_node_str(mpack_node_map_uint(root, COMMAND_MAPPING_NAME))); break; case COMMAND_CODE_GET_NAME: break; case COMMAND_CODE_SET_SCHEDULE: handler_command_set_schedule(root, controller); break; case COMMAND_CODE_GET_SCHEDULE: break; case COMMAND_CODE_SET_RELAY_NAME: handler_command_set_relay_name(root, controller); break; case COMMAND_CODE_GET_RELAY_NAME: break; default: LOG_ERROR("received invalid command"); } if(mpack_tree_destroy(&tree) != mpack_ok) { LOG_WARN("error when destroying mpack tree"); } free(payload); close(client_fd); }