#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static void handler_command_set_name(mpack_node_t map, controller_t *controller) { char name_buffer[MAX_NAME_LENGTH + 1]; mpack_node_copy_cstr(mpack_node_map_uint(map, COMMAND_MAPPING_NAME), name_buffer, MAX_NAME_LENGTH + 1); controller_set_name(controller, name_buffer); } 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)); relay_t *target_relay = controller->relays[relay_num]; mpack_node_t schedules_array = mpack_node_map_uint(map, COMMAND_MAPPING_SCHEDULES_ARRAY); for(int i = 0; i < 7; ++i) { mpack_node_t schedules_map = mpack_node_array_at(schedules_array, i); uuid_t schedule_id; memcpy(schedule_id, mpack_node_data(mpack_node_map_uint(schedules_map, COMMAND_MAPPING_SCHEDULE_ID)), sizeof(uuid_t)); uint16_t periods_count = mpack_node_u16(mpack_node_map_uint(schedules_map, COMMAND_MAPPING_PERIODS_COUNT)); uint16_t *periods = (uint16_t*)mpack_node_bin_data(mpack_node_map_uint(schedules_map, COMMAND_MAPPING_PERIODS_BLOB)); if(target_relay->schedules[i]) { schedule_free(target_relay->schedules[i]); } target_relay->schedules[i] = schedule_create(schedule_id, i, 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\n", 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\n", 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\n", 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\n", command_code); switch(command_code) { case COMMAND_CODE_GET_TIME: break; case COMMAND_CODE_GET_ID: break; case COMMAND_CODE_SET_NAME: handler_command_set_name(root, controller); 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\n"); } if(mpack_tree_destroy(&tree) != mpack_ok) { LOG_WARN("error when destroying mpack tree\n"); } free(payload); close(client_fd); }