Пример #1
0
/*
 * Process build commands and return the results.
 *
 * Returns:
 *  Success: SUCCESS
 *  Failure: ERR_INVALID_COMMAND, ERR_INVALID_MODEL_ID, and various others (< 0) 
 */
int do_build() {

	int ret = 0;
	uint32_t target_id32 = 0;

    uint32_t command = recv_uint32();
    uint32_t model_id = 0;

    assemble_result_t result;
    cgc_memset(&result, 0, sizeof(assemble_result_t));

    switch(command) {
	case B_CMD_INIT_PANEL:
		model_id = recv_uint32();
		ret = init_electric_model(model_id);
		break;
	case B_CMD_ADD_BREAKER:
		model_id = recv_uint32();
		ret = add_breaker_to_load_center(model_id, &result);
		break;
	case B_CMD_ADD_OUTLET:
		model_id = recv_uint32();
		target_id32 = recv_uint32();
		ret = add_outlet_to_breaker(model_id, target_id32, &result);
		break;
	case B_CMD_ADD_SPLITTER:
		model_id = recv_uint32();
		target_id32 = recv_uint32();
		ret = add_n_way_splitter_to_receptacle(model_id, target_id32, &result);
		break;
	case B_CMD_ADD_LIGHT_STRING:
		model_id = recv_uint32();
		target_id32 = recv_uint32();
		ret = add_light_string_to_receptacle(model_id, target_id32, &result);
		break;
	default:
		ret = ERR_INVALID_COMMAND;
    }

    // send results if no error
    if ((ret >= 0) && (B_CMD_INIT_PANEL != command)) {
        do_send_results(&result);
    }

	return ret;
}
Пример #2
0
/*
 * Process examine commands and return the results.
 *
 * Returns:
 *  Success: SUCCESS
 *  Failure: ERR_INVALID_COMMAND
 */
int do_examine() {
	int ret = SUCCESS;
	uint32_t target_id32 = 0;

    uint32_t command = recv_uint32();

    switch(command) {
	case E_CMD_PANEL_OVERLOADED:
		ret = is_electrical_panel_overloaded();
		break;
	case E_CMD_BREAKER_OVERLOADED:
		target_id32 = recv_uint32();
		ret = is_breaker_overloaded(target_id32);
		break;
	case E_CMD_OUTLET_OVERLOADED:
		target_id32 = recv_uint32();
		ret = is_outlet_overloaded(target_id32);
		break;
	case E_CMD_SPLITTER_OVERLOADED:
		target_id32 = recv_uint32();
		ret = is_splitter_overloaded(target_id32);
		break;
	case E_CMD_LIGHT_STRING_OVERLOADED:
		target_id32 = recv_uint32();
		ret = is_light_string_overloaded(target_id32);
		break;
	case E_CMD_RECEPTACLE_OVERLOADED:
		target_id32 = recv_uint32();
		ret = is_receptacle_overloaded(target_id32);
		break;
	default:
		ret = ERR_INVALID_COMMAND;
    }

    // send results if no error
    if (ret >= 0) {
    	assemble_result_t result = {0};
    	result.object_id = (uint32_t)ret;
        do_send_results(&result);
    }

	return ret;
}
Пример #3
0
int recv_file (int sock) {
    int          recv_len;      // The number of bytes received from a recv
    char        *filename;
    FILE        *fp;            // The local file to write
    uint32_t     file_length;   // The length of the incoming file in bytes
    unsigned int i;
    byte         current_byte;  // The current byte received

    filename = (char *) malloc (MAX_LINE);

    // receive filename
    if (recv_line(sock, filename, MAX_LINE) <= 0) {
        return -1;
    }

    // Receive the incoming file length
    if (recv_uint32(sock, &file_length) <= 0)
        return -1;

    // Ensure that the file exists
    if (file_length == 0) {
        fprintf(stderr, "[!!] Remote file does not exist or is empty\n");
        return -1;
    }

#if DEBUG
    printf("Receiving %s | %d bytes | ... ", filename, file_length);
    fflush(stdout);
#endif

    // If it does exist, open a local copy as binary
    fp = fopen(filename, "wb+");
    if (!fp) {
        return -1;
    }

    // Loop over each byte and write the received byte to the local file
    for (i = 0; i < file_length; ++i) {
        recv_len = recv_byte(sock, &current_byte);
        if (recv_len <= 0) {
            fclose(fp);
            return -1;
        }

#if DEBUG
        printf("\rReceiving %s | %d bytes | %d%%... ", filename, file_length,
                (int) (100 * floor((i + 1) / file_length)));
        fflush(stdout);
#endif
        fwrite(&current_byte, 1, 1, fp);
    }

    // Close the local file when finished
    fclose(fp);
    free(filename);

#if DEBUG
    printf("Done.\n");
#endif
    return 0;
}