/**
 * Produce an IN_DELETE/IN_CREATE notifications pair for an overwritten file.
 * Reopen a watch for the overwritten file.
 *
 * This function is used as a callback and is invoked from the dep-list
 * routines.
 *
 * @param[in] udata  A pointer to user data (#handle_context).
 * @param[in] path   File name of the overwritten file.
 * @param[in] inode  Inode number of the overwritten file.
 **/
static void
handle_overwritten (void *udata, const char *path, ino_t inode)
{
    assert (udata != NULL);

    handle_context *ctx = (handle_context *) udata;
    assert (ctx->wrk != NULL);
    assert (ctx->w != NULL);
    assert (ctx->be != NULL);

   int i;
    for (i = 0; i < ctx->wrk->sets.length; i++) {
        watch *wi = ctx->wrk->sets.watches[i];
        if (wi && (strcmp (wi->filename, path) == 0)
            && wi->parent == ctx->w) {
            if (watch_reopen (wi) == -1) {
                /* I dont know, what to do */
                /* Not a very beautiful way to remove a single dependency */
                dep_list *dl = dl_create (wi->filename, wi->inode);
                worker_remove_many (ctx->wrk, ctx->w, dl, 0);
                dl_shallow_free (dl);
            } else {
                uint32_t cookie = inode & 0x00000000FFFFFFFF;
                int event_len = 0;
                struct inotify_event *ev;

                ev = create_inotify_event (ctx->w->fd, IN_DELETE, cookie,
                                           path,
                                           &event_len);
                if (ev != NULL) {
                    bulk_write (ctx->be, ev, event_len);
                    free (ev);
                }  else {
                    perror_msg ("Failed to create an IN_DELETE event (*) for %s",
                                path);
                }

                ev = create_inotify_event (ctx->w->fd, IN_CREATE, cookie,
                                           path,
                                           &event_len);
                if (ev != NULL) {
                    bulk_write (ctx->be, ev, event_len);
                    free (ev);
                } else {
                    perror_msg ("Failed to create an IN_CREATE event (*) for %s",
                                path);
                }
            }
            break;
        }
    }
}
/**
 * Produce an IN_MOVED_FROM/IN_MOVED_TO notifications pair for a replaced file.
 * Also stops wathing on the replaced file.
 *
 * This function is used as a callback and is invoked from the dep-list
 * routines.
 *
 * @param[in] udata       A pointer to user data (#handle_context).
 * @param[in] from_path   File name of the source file.
 * @param[in] from_inode  Inode number of the source file.
 * @param[in] to_path     File name of the replaced file.
 * @param[in] to_inode    Inode number of the replaced file.
**/
static void
handle_replaced (void       *udata,
                 const char *from_path,
                 ino_t       from_inode,
                 const char *to_path,
                 ino_t       to_inode)
{
    assert (udata != NULL);

    handle_context *ctx = (handle_context *) udata;
    assert (ctx->wrk != NULL);
    assert (ctx->w != NULL);
    assert (ctx->be != NULL);

    uint32_t cookie = from_inode & 0x00000000FFFFFFFF;
    int event_len = 0;
    struct inotify_event *ev;

    ev = create_inotify_event (ctx->w->fd, IN_MOVED_FROM, cookie,
                               from_path,
                               &event_len);
    if (ev != NULL) {
        bulk_write (ctx->be, ev, event_len);
        free (ev);
    }  else {
        perror_msg ("Failed to create an IN_MOVED_FROM event (*) for %s",
                    from_path);
    }

    ev = create_inotify_event (ctx->w->fd, IN_MOVED_TO, cookie,
                               to_path,
                               &event_len);
    if (ev != NULL) {
        bulk_write (ctx->be, ev, event_len);
        free (ev);
    } else {
        perror_msg ("Failed to create an IN_MOVED_TO event (*) for %s",
                    to_path);
    }

    int i;
    for (i = 1; i < ctx->wrk->sets.length; i++) {
        watch *iw = ctx->wrk->sets.watches[i];
        if (iw && iw->parent == ctx->w && strcmp (to_path, iw->filename) == 0) {
            dep_list *dl = dl_create (iw->filename, iw->inode);
            worker_remove_many (ctx->wrk, ctx->w, dl, 0);
            dl_shallow_free (dl);
            break;
        }
    }
}
/**
 * Produce an IN_CREATE notification for a new file and start wathing on it.
 *
 * This function is used as a callback and is invoked from the dep-list
 * routines.
 *
 * @param[in] udata  A pointer to user data (#handle_context).
 * @param[in] path   File name of a new file.
 * @param[in] inode  Inode number of a new file.
 **/
static void
handle_added (void *udata, const char *path, ino_t inode)
{
    assert (udata != NULL);

    handle_context *ctx = (handle_context *) udata;
    assert (ctx->wrk != NULL);
    assert (ctx->w != NULL);
    assert (ctx->be != NULL);

    struct inotify_event *ie = NULL;
    int ie_len = 0;

    ie = create_inotify_event (ctx->w->fd, IN_CREATE, 0, path, &ie_len);
    if (ie != NULL) {
        bulk_write (ctx->be, ie, ie_len);
        free (ie);
    } else {
        perror_msg ("Failed to create an IN_CREATE event for %s", path);
    }

    char *npath = path_concat (ctx->w->filename, path);
    if (npath != NULL) {
        watch *neww = worker_start_watching (ctx->wrk, npath, path, ctx->w->flags, WATCH_DEPENDENCY);
        if (neww == NULL) {
            perror_msg ("Failed to start watching on a new dependency %s", npath);
        } else {
            neww->parent = ctx->w;
        }
        free (npath);
    } else {
        perror_msg ("Failed to allocate a path to start watching a dependency");
    }
}
Example #4
0
/**
 * Serves SIGRTMIN+11 by notifying the doServer function that a client has finished/resigned
 * from playing/observing a game.
 *@param[in] sig A signal number that will be served.
 */
void
sig_update_handler(int sig) {
	sigset_t mask, oldmask;
	sigemptyset(&mask);
	sigaddset(&mask, SIGRTMIN + 11);
	sigprocmask(SIG_BLOCK, &mask, &oldmask);
	bulk_write(3, "0", 1);
	sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
Example #5
0
/*---------------------------------------------------------------------------*/
static hg_return_t
hg_test_bulk_transfer_cb(const struct hg_cb_info *hg_cb_info)
{
    struct hg_test_bulk_args *bulk_args = (struct hg_test_bulk_args *)
            hg_cb_info->arg;
    hg_bulk_t local_bulk_handle = hg_cb_info->info.bulk.local_handle;
    hg_return_t ret = HG_SUCCESS;

    bulk_write_out_t out_struct;

    void *buf;
    size_t write_ret;

    if (hg_cb_info->ret == HG_CANCELED) {
        printf("HG_Bulk_transfer() was successfully canceled\n");

        /* Fill output structure */
        out_struct.ret = 0;
    } else if (hg_cb_info->ret != HG_SUCCESS) {
        HG_LOG_ERROR("Error in callback");
        ret = HG_PROTOCOL_ERROR;
        goto done;
    }

    if (hg_cb_info->ret == HG_SUCCESS) {
        /* Call bulk_write */
        HG_Bulk_access(local_bulk_handle, 0, bulk_args->nbytes,
            HG_BULK_READWRITE, 1, &buf, NULL, NULL);

        write_ret = bulk_write(bulk_args->fildes, buf, 0,
            bulk_args->nbytes, 1);

        /* Fill output structure */
        out_struct.ret = write_ret;
    }

    /* Free block handle */
    ret = HG_Bulk_free(local_bulk_handle);
    if (ret != HG_SUCCESS) {
        fprintf(stderr, "Could not free HG bulk handle\n");
        return ret;
    }

    /* Send response back */
    ret = HG_Respond(bulk_args->handle, NULL, NULL, &out_struct);
    if (ret != HG_SUCCESS) {
        fprintf(stderr, "Could not respond\n");
        return ret;
    }

done:
    HG_Destroy(bulk_args->handle);
    free(bulk_args);

    return ret;
}
/**
 * Produce an IN_MOVED_FROM/IN_MOVED_TO notifications pair for a renamed file.
 *
 * This function is used as a callback and is invoked from the dep-list
 * routines.
 *
 * @param[in] udata       A pointer to user data (#handle_context).
 * @param[in] from_path   The old name of the file.
 * @param[in] from_inode  Inode number of the old file.
 * @param[in] to_path     The new name of the file.
 * @param[in] to_inode    Inode number of the new file.
**/
static void
handle_moved (void       *udata,
              const char *from_path,
              ino_t       from_inode,
              const char *to_path,
              ino_t       to_inode)
{
    assert (udata != NULL);

    handle_context *ctx = (handle_context *) udata;
    assert (ctx->wrk != NULL);
    assert (ctx->w != NULL);
    assert (ctx->be != NULL);

    uint32_t cookie = from_inode & 0x00000000FFFFFFFF;
    int event_len = 0;
    struct inotify_event *ev;
    
    ev = create_inotify_event (ctx->w->fd, IN_MOVED_FROM, cookie,
                               from_path,
                               &event_len);
    if (ev != NULL) {   
        bulk_write (ctx->be, ev, event_len);
        free (ev);
    } else {
        perror_msg ("Failed to create an IN_MOVED_FROM event for %s",
                    from_path);
    }
    
    ev = create_inotify_event (ctx->w->fd, IN_MOVED_TO, cookie,
                               to_path,
                               &event_len);
    if (ev != NULL) {   
        bulk_write (ctx->be, ev, event_len);
        free (ev);
    } else {
        perror_msg ("Failed to create an IN_MOVED_TO event for %s",
                    to_path);
    }
}
int send_message(int fd, char* mess_type, char* mess) {
	int size;
	char* message;
	int c = 0;
	size = strlen(mess);

	message = malloc(strlen(mess_type) + 10 + size + 3);
	sprintf(message, "%s+%10d%s", mess_type, size, mess);

	c = bulk_write(fd, message, strlen(message));
	free(message);
	return c;
}
Example #8
0
/*---------------------------------------------------------------------------*/
static hg_return_t
hg_test_bulk_seg_transfer_cb(const struct hg_cb_info *hg_cb_info)
{
    struct hg_test_bulk_args *bulk_args = (struct hg_test_bulk_args *)
            hg_cb_info->arg;
    hg_bulk_t local_bulk_handle = hg_cb_info->info.bulk.local_handle;
    hg_return_t ret = HG_SUCCESS;

    bulk_write_out_t out_struct;

    void *buf;
    size_t write_ret;

    if (hg_atomic_incr32(&bulk_args->completed_transfers) != 2)
        goto done;

    /* Call bulk_write */
    HG_Bulk_access(local_bulk_handle, 0, bulk_args->nbytes, HG_BULK_READWRITE,
            1, &buf, NULL, NULL);

    write_ret = bulk_write(bulk_args->fildes, buf, 0,
        bulk_args->nbytes, 1);

    /* Fill output structure */
    out_struct.ret = write_ret;

    /* Free block handle */
    ret = HG_Bulk_free(local_bulk_handle);
    if (ret != HG_SUCCESS) {
        fprintf(stderr, "Could not free HG bulk handle\n");
        return ret;
    }

    /* Send response back */
    ret = HG_Respond(bulk_args->handle, NULL, NULL, &out_struct);
    if (ret != HG_SUCCESS) {
        fprintf(stderr, "Could not respond\n");
        return ret;
    }

    HG_Destroy(bulk_args->handle);
    free(bulk_args);

done:
    return ret;
}
void serve_files_page(int fd) {
	char* reply;
	char* html_files_response;

	reply = malloc(2048);
	html_files_response = files_response();
	sprintf(reply,
		"HTTP/1.1 200 OK\r\n"
		"Content-Type: text/html\r\n"
		"Connection: close\r\n"
		"Content-Length: %d\r\n"
		"\r\n"
		"%s"
		,strlen(html_files_response), html_files_response);

	bulk_write(fd, reply, strlen(reply));

	free(reply);
	free(html_files_response);
}
/**
 * Produce an IN_DELETE notification for a removed file.
 *
 * This function is used as a callback and is invoked from the dep-list
 * routines.
 *
 * @param[in] udata  A pointer to user data (#handle_context).
 * @param[in] path   File name of the removed file.
 * @param[in] inode  Inode number of the removed file.
 **/
static void
handle_removed (void *udata, const char *path, ino_t inode)
{
    assert (udata != NULL);

    handle_context *ctx = (handle_context *) udata;
    assert (ctx->wrk != NULL);
    assert (ctx->w != NULL);
    assert (ctx->be != NULL);

    struct inotify_event *ie = NULL;
    int ie_len = 0;

    ie = create_inotify_event (ctx->w->fd, IN_DELETE, 0, path, &ie_len);
    if (ie != NULL) {
        bulk_write (ctx->be, ie, ie_len);
        free (ie);
    } else {
        perror_msg ("Failed to create an IN_DELETE event for %s", path);
    }
}
Example #11
0
int SendMessage(char *msg) {
   // int fd = connect_socket(g_serverip, g_port);
    size_t CHUNKSIZE = 200;
    bulk_write(g_serverFd, msg, CHUNKSIZE);
    return g_serverFd;
}
void serve_file(int fd, int file_id, struct node* slaves){
	struct node* files_list;
	struct file_info* file;
	struct node* tids_list;
	int i = 0;
	pthread_t* tid;
	struct part_info* part_info;
	struct get_file_part_arg* file_part_arg;
	struct node* node;
	tids_list = initialize_list();
	void* data = 0;
	char* file_data;
	char* response;
	char* response_body;

	file_data = malloc(1024);
	response = malloc(1024);
	response_body = malloc(1024);

	files_list = prepare_list_of_files();
	files_list = files_list -> next;
	while(files_list -> id != -2){
		file = (struct file_info*)(files_list -> data);
		if( file -> file_id == file_id) {
			printf("ZNALEZIONO PLIK! ID: %d NUM OF PARTS: %d\n", file -> file_id, file -> num_of_parts);
			break;
		}
		files_list = files_list -> next;
	}

	if(files_list -> id == -2) return;

	for(i = 0; i < file -> num_of_parts; i++) {
		tid = malloc(sizeof(pthread_t));
		part_info = malloc(sizeof(struct part_info));
		part_info -> file_id = file_id;
		part_info -> part_id = i;
		file_part_arg = malloc(sizeof(struct get_file_part_arg));
		file_part_arg -> part = part_info;
		file_part_arg -> slaves = slaves;
		sleep(1);
		pthread_create(tid, NULL, get_file_part, (void*)file_part_arg);
		add_new_end(tids_list, (void*)(tid));
	}

	node = tids_list -> next;
	while(node -> id != -2) {
		pthread_join(*((pthread_t*)(node -> data)), &data);
		if(data == NULL) {

			response_body = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>Błąd!</title></head><body><h1>Bad Request</h1><p>Liczba wyłączonych slave\'ów przekroczyła poziom niezawodności tego pliku.<p></body></html>";
			sprintf(response,
			"HTTP/1.1 400 Bad Request\r\n"
			"Content-Length: %d\r\n"
			"Content-Type: text/html; charset=utf-8\r\n"
			"Connection: Closed\r\n"
			"\r\n"
			"%s",
			strlen(response_body), response_body);

			bulk_write(fd, response, strlen(response));
			add_file_downloaded_error_status();
			return;
		}

		part_info = (struct part_info*)data;
		strcat(file_data, part_info -> data);
		node = node -> next;
	}

	printf("OTRZYMANY PLIK: %s\n", file_data);

	sprintf(response,
		"HTTP/1.1 200 OK\r\n"
		"Content-Disposition: attachment; filename=\"%s\"\r\n"
		"Content-Type: text/plain\r\n"
		"Connection: close\r\n"
		"Content-Length: %d\r\n"
		"\r\n"
		"%s"
		,file -> filename, strlen(file_data), file_data);

	bulk_write(fd, response, strlen(response));

	add_file_downloaded_status();
}
Example #13
0
void send_config(const char *data) {
    if(*data) fprintf(stderr, "Sending configuration data:\n%s", data);
    unsigned char buf[255];
    int length = snprintf(buf, 255, "\x1bX\n%s\x80", data);
    bulk_write(3, buf, length);
}
Example #14
0
int main(int argc, char **argv) {

	FILE *pFile = NULL;

	int size;
	long lSize;

	unsigned char *buffer;
	unsigned char *buf2 = calloc(300, sizeof(unsigned char));

	if (argc < 2) {
		fprintf(stderr, "Missing upload filename \n\n");
		fprintf(stderr, "Usage: \n");
		fprintf(stderr, "ISP808 <uploadfile> \n");
		exit(1);;
	}
	pFile = fopen(argv[1], "rb");

	if (pFile == NULL) {
		fprintf(stderr, "ERROR: Unable to open file %s \n", argv[1]);
		exit(1);
	}

	// obtain file size:
	fseek(pFile, 0, SEEK_END);
	lSize = ftell(pFile);
	rewind(pFile);



	// allocate memory to contain the whole file:
	buffer = calloc(lSize, sizeof(unsigned char));

	if (buffer == NULL) {
		fputs("Memory error", stderr);
		exit(2);
	}

	size = fread(buffer, 1, lSize, pFile);
	if (size != lSize) {
		fputs("Reading error", stderr);
		exit(3);
	}

	libusb_context *context;
	libusb_init(&context);

	libusb_set_debug(context, 3);

	device_handle = libusb_open_device_with_vid_pid(context, VID, PID);
	if (!device_handle) {
		fprintf(stderr, "ERROR: Unable to find device "
			"(Vendor ID = %04x, Product ID = %04x)\n", VID, PID);
		fprintf(stderr, "Press the Mode Button and connect the cam to USB \n");

		return 1;
	}
	libusb_claim_interface(device_handle, 2);

	libusb_set_interface_alt_setting(device_handle, 0, 0);

	usleep(100);

	// Get the installed version

	fprintf(stdout, "\nRead 808 Version \n");
	GetVersion();
	GetVersion1();
	GetVersion2();

	fprintf(stdout, "\nStart Uploading the file: %s \n",argv[1]);

	char defout[] = { 0x00, 0x80, 0x04, 0x00, 0x2d, 0x10, 0x00, 0x00 };

	control_in_vendor_device(0x70, 0, 0, 5);

	usleep(100);

	control_out(0xfd, 0, 0x4f3, defout, 8);

	usleep(200); // Fix me

	// Download the code

	fprintf(stdout, "\nDownload Code.... \n");

	bulk_write(3, buffer, lSize);
	usleep(200);

	char defout2[] = { 0x00, 0x80, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 };

	control_out(0xfd, 0, 0x4f1, defout2, 0x10);

	usleep(100);

	// Dont know why
	bulk_write(3, buf2, 256);

	fprintf(stdout, "\nDownload Ready.... \n");
	usleep(100);

	if (pFile)
		fclose(pFile);

	free(buffer);

	libusb_release_interface(device_handle, 0);
	libusb_close(device_handle);
	libusb_exit(context);
	return 0;
}
Example #15
0
int main()
{
    int fd, ret;

    fd = config_spi();
    if (fd < 1)
    {
        printf("Failed to configure spi\n");
        return fd;
    }
    //write_word(fd, 0x00010014, 0xffffffff);
    int i=0, err_cnt=0;
    unsigned int *val, *wr_buf, *rd_buf;

    unsigned int readval;
    ret = write_word(fd, 0x00017108, 16);
    printf("write response was %u\n", ret);
    ret = read_word(fd, 0x00017108, &readval);
    printf("read response was %u\n", ret);
    printf("read val was %u\n", readval);

    int ntrials=1024;

    val = malloc(sizeof(unsigned int));
    for (i=0; i<ntrials; i++)
    {
        ret = write_word(fd, 0x00010004+4*i, i);
        if (ret != 143)
        {
            printf("write response was %u\n", ret);
        }
        ret = read_word(fd, 0x00010004+4*i, val);
        if (ret != 143)
        {
            printf("read response was %u\n", ret);
        }
        
        if (*val != i)
        {
            printf("Read does not match write! (r:%u vs w:%u)\n", *val, i);
            err_cnt++;
        }
    }

    printf("Errors after %d single writes: %d\n", i, err_cnt);

    free(val);

    printf("Trying bulk read of %d words\n", ntrials);
    wr_buf = calloc(ntrials, sizeof(unsigned int));
    rd_buf = calloc(ntrials, sizeof(unsigned int));
    for(i=0; i<ntrials; i++)
    {
        *(wr_buf+i) = i;
    }
    ret = bulk_write(fd, 0x00010004, 4*ntrials, wr_buf);
    printf("bulk write response was %u\n", ret);
    ret = bulk_read(fd, 0x00010004, 4*ntrials, rd_buf);
    for(i=0; i<ntrials; i++)
    {
        printf("Wrote %u, got back %u\n", *(wr_buf+i), *(rd_buf+i));
    }
    printf("bulk read response was %u\n", ret);
    free(wr_buf);
    free(rd_buf);

    close(fd);
    return 0;
}