Ejemplo n.º 1
0
int l_send_packet(lua_State* L) {
	int argc = lua_gettop(L);
	if (argc!=1)
		die("Incorrect number of args to send_packet\n");
	const char* payload = lua_tostring(L,1);
	char scbuf[3];
	scbuf[2]=0x0;
	int len = strlen(payload)/2;
	char* tmp = malloc(len);
	int i;
	for (i=0;i<len; i++)
	{
		strncpy(scbuf,&payload[i*2],2);
		sscanf(scbuf, "%hhx",&tmp[i]);
	}
	char* packet = pack_payload(tmp, len, HOST2MCU);
	write(us->fd, packet, PACKED_SIZE(len));
	
	tcdrain(us->fd);

	if (delay_hack) {
		int delay = PACKED_SIZE(len)*1000000/us->speed*12; /* account for 11 bits */	
		usleep(delay);
	}
		
	free(tmp);
	free(packet);
	return 0;
}
Ejemplo n.º 2
0
void test_pack_payload()
{
    uint8_t payload[] = { 0x78, 0x46, 0x02 };
    uint8_t *result = NULL;
    unsigned int length = 3;

    uint16_t total_length;

    total_length = pack_payload(payload, &result, length);
    TEST_CHECK_(result != NULL, "Result doesn't point to anything");
    TEST_CHECK_(total_length == 9, "Incorrect length: %d", total_length);
    TEST_CHECK(result[0] == 0x02);
    TEST_CHECK(result[8] == 0x03);
    TEST_CHECK_(result[1] == 3, "Incorrect LSB length: %d", result[1]);
    TEST_CHECK_(result[2] == 0, "Incorrect MSB length: %d", result[2]);
    TEST_CHECK_(result[6] == 0x3C, "Incorrect LRC: %#x", result[6]);
    TEST_CHECK_(result[7] == 0xC0, "Incorrect checksum:  %#x", result[7]);

    free(result);
}
Ejemplo n.º 3
0
void test_bijection()
{
    uint8_t unpacked[] = {
        0x06, 0x35, 0x33, 0x39, 0x54, 0x30, 0x39, 0x34, 0x35, 0x38, 0x32
    };

    unsigned int unpacked_length = 11;
    uint8_t *packed;

    uint16_t packed_length = pack_payload(unpacked, &packed, unpacked_length); 

    uint8_t *unpacked_result;
    uint16_t unpacked_result_length = unpack_payload(packed, &unpacked_result);

    TEST_CHECK(unpacked_result_length == unpacked_length);
    int cmp = memcmp(unpacked, unpacked_result, unpacked_result_length);
    TEST_CHECK(cmp == 0);

    free(packed);
    free(unpacked_result);
}
Ejemplo n.º 4
0
void test_pack_payload2()
{
    uint8_t payload[] = {
        0x06, 0x35, 0x33, 0x39, 0x54, 0x30, 0x39, 0x34, 0x35, 0x38, 0x32
    };
    uint8_t *result = NULL;
    unsigned int length = 11;
    uint16_t total_length;

    total_length = pack_payload(payload, &result, length);

    TEST_CHECK_(result != NULL, "Result doesn't point to anything");
    TEST_CHECK_(total_length == 17, "Incorrect length: %d", total_length);
    TEST_CHECK(result[0] == 0x02);
    TEST_CHECK(result[total_length - 1] == 0x03);
    TEST_CHECK_(result[1] == 0x0B, "Incorrect LSB length: %d", result[1]);
    TEST_CHECK_(result[2] == 0, "Incorrect MSB length: %d", result[2]);
    TEST_CHECK_(result[total_length - 3] == 0x6F, "Incorrect LRC: %#x", result[total_length - 3]);
    TEST_CHECK_(result[total_length - 2] == 0x37, "Incorrect checksum:  %#x", result[total_length - 2]);

    free(result);
}
Ejemplo n.º 5
0
int l_send_file(lua_State *L) {
	int argc = lua_gettop(L);
	if (argc!=2)
		die("Incorrect number of args to set_baud\n");
	char* filename = lua_tostring(L,1);
	int chunksize = lua_tonumber(L,2);
	
	FILE* fd = fopen(filename, "r");
	struct stat inf;
	if (0 != stat(filename,&inf)) {
		perror("stat failed: ");
		die("");
	}
	if (fd<0){
		perror("couldn't open file:");
		die("");
	}
	unsigned int sz = (unsigned int)inf.st_size;
	if (minf->iromsz < sz) {
		printf("WARNING: File too big to fit in flash, truncating!\n");
		sz = minf->iromsz+1;
	}
	printf("Downloading %s (%d bytes)\n", filename, sz);
	unsigned int maxsz = sz;
	//unsigned short sz = PACKED_SIZE(chunksize+7);
	/* We add a few bytes.
	   type of packet? 0x00
	   0x0 0x0 
	   2 bytes, offset to write at
	   2 bytes, size to write
	 */
	char* tmp = calloc(1, chunksize+7);
	int len;
	unsigned short offset=0;
	/* Since chunksize is fixed */
	tmp[5]=HIGH_BYTE((unsigned short) chunksize);
	tmp[6]=LOW_BYTE((unsigned short) chunksize);
	struct packet* response;
	struct write_response *rsp;
	unsigned short crc; 
	while (sz) {
		len = fread(&tmp[7], 1, chunksize, fd);
		crc = byte_sum(&tmp[7], chunksize);
		crc = crc & 0x00ff; /* We get a shorted, one byte crc */ 
		tmp[3]=HIGH_BYTE(offset);
		tmp[4]=LOW_BYTE(offset);		
		char* packet = pack_payload(tmp,chunksize+7, HOST2MCU);
		write(us->fd, packet, PACKED_SIZE(chunksize+7));
		do_dump_packet(packet,PACKED_SIZE(chunksize+7));
		free(packet);
		response = fetch_packet(us->fd);
		rsp = response->payload;
		if (rsp->errcode !=0 )
		{
			fprintf(stderr, "Warning, mcu reports error @0x%hx: %hhx\n", offset, rsp->errcode);
			//printf();
		}else
		{
			//tmp[0]
		}
		if (rsp->crc != (unsigned char) crc )
			fprintf(stderr, "Warning, crc error @0x%hx: %hhx vs %hhx\n", 
				offset, 
				rsp->crc, 
				(unsigned char) crc);
		do_dump_packet(response->payload, response->size);
		sz-= len;
		offset+=len;
		display_progressbar(maxsz,sz);	
	}
	printf("\n");
	
}