예제 #1
0
static void
print_stuff() {
	printf("tmr now = %ss\n", time_now_f());

	printf("SDK version: %s\n", system_get_sdk_version());
	printf("APP version: %s built: %s %s\n", BUILD_DATE, __DATE__, __TIME__);

	printf("CPU freq was %d\n", system_get_cpu_freq());
	printf("CPU freq = %d\n", set_cpu_freq(160));
	printf("tmr now = %ss\n", time_now_f());

	printf("tout    = %sV\n", ffp(3, read_tout(0)*123/10));
	printf("vdd     = %sV\n", ffp(3, read_vdd()));

	print_mac(STATION_IF);
	print_mac(SOFTAP_IF);

	print_ip_info(SOFTAP_IF);
	print_ip_info(STATION_IF);

	printf("tmr now = %ss\n", time_now_f());
	printf("sleeping 2s\n");
	system_deep_sleep(2*1000000);
	vTaskDelete(NULL);
}
예제 #2
0
int handle_client(int csockfd, int timeout) {
    uint64_t fsize;
    ssize_t namelen;
    char buf[BUF_SZ];
    char path[DIRLEN + FNAME_LENGTH + 1];
    char *filename = path + DIRLEN;
    ssize_t nbytes;
    bool delete_file;

    /* reading filesize */
    nbytes = read_tout(csockfd, &fsize, sizeof(fsize), timeout, 0, false);
    if (nbytes != sizeof(fsize)) {
        log("Filesize reading error\n");
        return -1;
    }
    fsize = le64toh(fsize);

    if (fsize > ((uint64_t)1 << 62)) {
        log("Filesize is too big\n");
        send_code(csockfd, CODE_BAD_FILELEN, timeout);
        return -1;
    }

    if (send_code(csockfd, CODE_ACK, timeout))
        return -1;
    log("Filesize %llu accepted\n", fsize);

    /* reading filename */
    //memset(path, 0, sizeof(path));
    memcpy(path, DIRNAME, DIRLEN);

    nbytes = read_tout(csockfd, buf, sizeof(buf), timeout, MSG_PEEK, false);
    if (nbytes > 0) {
        namelen = fname_len(buf, nbytes);
        if (namelen <= 0 || namelen > FNAME_LENGTH) {
            log("Bad filename length\n");
            send_code(csockfd, CODE_BAD_FILELEN, timeout);
            return -1;
        }
        /* There is no lseek() for socket, so .. */
        read_tout(csockfd, filename, namelen + 1, 0, 0, true);
    } else {
        log("Filename reading error");
        send_code(csockfd, CODE_ERR_UNKNOWN, timeout);
        return -1;
    }

    if (!check_fname(filename)) {
        log("Bad filename (check_fname)!\n");
        send_code(csockfd, CODE_BAD_FILENAME, timeout);
        return -1;
    }

    if (access(path, F_OK) != -1) {
        send_code(csockfd, CODE_FILE_EXISTS, timeout);
        return -1;
    }
    int fd = open(path, O_CREAT | O_WRONLY, 0600);
    if (fd == -1) {
        perror("open()");
        if (errno == ENAMETOOLONG)
            send_code(csockfd, CODE_BAD_FILELEN, timeout);
        else
            send_code(csockfd, CODE_ERR_UNKNOWN, timeout);
        return -1;
    }

    if (send_code(csockfd, CODE_ACK, timeout))
        return -1;
    log("Filename `%s` accepted and file exchange started\n", filename);

    /* reading file */
    nbytes = 0;
    delete_file = false;

    do {
        fsize -= (uint64_t) nbytes;
        nbytes = read_tout(csockfd, buf, sizeof(buf), timeout, 0, false);
        if (nbytes > 0) {
            if (write(fd, buf, min(nbytes, fsize)) == -1) {
                perror("write()");
                send_code(csockfd, CODE_ERR_TRANSFER, timeout);
                delete_file = true;
                break;
            }
        }
    } while (nbytes > 0 && nbytes < fsize);

    if (nbytes < 0) {
        log("Error while transfer\n");
        send_code(csockfd, CODE_ERR_TRANSFER, timeout);
        delete_file = true;
    }

    close(fd);

    if (delete_file) {
        log("Deleting file\n");
        unlink(path);
        return -1;
    }

    if (send_code(csockfd, CODE_ACK, timeout))
        log("Warning: send_code was unsuccessful, but file recieved ok");
    log("File `%s` recieved ok!\n", path);

    return 0;
}