Добавить протокол Altera Logic и общие клиенты прошивки
This commit is contained in:
72
c/set-protocol/tests/test_altera_logic.c
Normal file
72
c/set-protocol/tests/test_altera_logic.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "altera_logic.h"
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
static void response(void *s, uint8_t *data, size_t n) {
|
||||
size_t i; uint8_t sum=0;
|
||||
for(i=0;i<n-1;++i) sum^=data[i]; data[n-1]=sum;
|
||||
/* Serial reads can split at every byte. */
|
||||
for(i=0;i<n;++i) la_feed(s,data+i,1);
|
||||
}
|
||||
static void connect_ok(void *s) {
|
||||
uint8_t p[6], info[]={0x5a,0x81,0,16,0,16,50,1,0xe8};
|
||||
const uint8_t request[]={0xa5,1,0,0,0,0xa4};
|
||||
la_init(s); assert(la_next(s,p,5)==0);
|
||||
assert(la_next(s,p,6)==6 && !memcmp(p,request,6));
|
||||
assert(la_next(s,p,6)==0);
|
||||
response(s,info,sizeof(info)); assert(la_get(s,LA_STATE)==LA_READY);
|
||||
}
|
||||
int main(void) {
|
||||
void *s=calloc(1,la_context_size()); uint8_t p[6], ack[4], status[7], block[133];
|
||||
uint16_t samples[4096]; unsigned int cmd, offset, i;
|
||||
assert(s); connect_ok(s);
|
||||
assert(la_start(s,65536,0,0,0,0)==LA_ARGUMENT);
|
||||
assert(la_start(s,49,1,0,1,1)==LA_ARGUMENT);
|
||||
assert(la_start(s,49,0,0,1,1)==0);
|
||||
for(cmd=2;cmd<=7;++cmd) {
|
||||
assert(la_next(s,p,6)==6 && p[1]==cmd);
|
||||
if(cmd==2) { const uint8_t v[]={0xa5,2,49,0,0,0x96}; assert(!memcmp(p,v,6)); }
|
||||
ack[0]=0x5a; ack[1]=(uint8_t)(cmd|0x80); ack[2]=0; response(s,ack,4);
|
||||
}
|
||||
assert(la_next(s,p,6)==0); la_tick(s,25);
|
||||
assert(la_next(s,p,6)==6 && p[1]==8);
|
||||
{ uint8_t waiting[]={0x5a,0x88,0,1,0,8,0}; response(s,waiting,7); }
|
||||
la_tick(s,60000); /* Waiting for a physical trigger is not a UART timeout. */
|
||||
assert(la_get(s,LA_STATE)==LA_CAPTURING);
|
||||
assert(la_next(s,p,6)==6 && p[1]==8);
|
||||
status[0]=0x5a; status[1]=0x88; status[2]=0; status[3]=6;
|
||||
status[4]=0;status[5]=8;response(s,status,7);
|
||||
for(offset=0;offset<4096;offset+=64) {
|
||||
assert(la_next(s,p,6)==6 && p[1]==9 && p[4]==64);
|
||||
assert((unsigned int)(p[2]|(p[3]<<8))==offset);
|
||||
block[0]=0x5a;block[1]=0x89;block[2]=0;block[3]=64;
|
||||
for(i=0;i<64;++i) { block[4+2*i]=(uint8_t)(offset+i);block[5+2*i]=(uint8_t)((offset+i)>>8); }
|
||||
response(s,block,133);
|
||||
}
|
||||
assert(la_get(s,LA_STATE)==LA_DONE);
|
||||
assert(la_samples(s,samples,4095)==0);
|
||||
assert(la_samples(s,samples,4096)==4096);
|
||||
for(i=0;i<4096;++i) assert(samples[i]==i);
|
||||
assert(la_get(s,LA_TRIGGER_INDEX)==2048);
|
||||
/* Corrupt XOR, wrong command, device error, timeout and extra data. */
|
||||
la_init(s);la_next(s,p,6);
|
||||
{ uint8_t bad[]={0x5a,0x81,0,16,0,16,50,1,0};la_feed(s,bad,9); }
|
||||
assert(la_get(s,LA_ERROR_CODE)==LA_CHECKSUM);
|
||||
la_init(s);la_next(s,p,6);
|
||||
{ uint8_t bad[]={0x5a,0x88};la_feed(s,bad,2); }
|
||||
assert(la_get(s,LA_ERROR_CODE)==LA_BAD_FRAME);
|
||||
la_init(s);la_next(s,p,6);
|
||||
{ uint8_t bad[]={0x5a,0x81,1,0};response(s,bad,4); }
|
||||
assert(la_get(s,LA_ERROR_CODE)==LA_DEVICE_ERROR);
|
||||
la_init(s);la_next(s,p,6);la_tick(s,999);
|
||||
assert(la_get(s,LA_STATE)==LA_CONNECTING);la_tick(s,1);
|
||||
assert(la_get(s,LA_ERROR_CODE)==LA_TIMEOUT && la_next(s,p,6)==0);
|
||||
connect_ok(s);la_feed(s,p,1);assert(la_get(s,LA_ERROR_CODE)==LA_BAD_FRAME);
|
||||
la_init(s);la_next(s,p,6);
|
||||
{ uint8_t bad[]={0x5a,0x81,0,8,0,16,50,1,0};response(s,bad,9); }
|
||||
assert(la_get(s,LA_ERROR_CODE)==LA_UNSUPPORTED);
|
||||
la_demo_init(s);assert(la_demo_capture(s,49)==0);
|
||||
assert(la_get(s,LA_STATE)==LA_DONE && la_samples(s,samples,4096)==4096);
|
||||
free(s);puts("Altera Logic C tests passed");return 0;
|
||||
}
|
||||
62
c/set-protocol/tests/test_altera_stream.c
Normal file
62
c/set-protocol/tests/test_altera_stream.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "altera_stream.h"
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
static void meta(void *s,uint32_t session,int uart) {
|
||||
uint8_t data[32];uint32_t id;size_t n,i;
|
||||
n=las_metadata(LAS_DEVICE_ID,session,1000000,(uint32_t)uart,&id,data,sizeof(data));assert(n);
|
||||
assert(id==0x1EE3FE00U);
|
||||
if(uart) for(i=0;i<n;++i) las_uart(s,data+i,1);
|
||||
else las_can(s,id,data,n,1,0);
|
||||
}
|
||||
static void sample(void *s,uint32_t session,uint32_t index,int uart) {
|
||||
uint8_t data[32];uint32_t id;size_t n;
|
||||
n=las_data(LAS_DEVICE_ID,session,index,index&65535U,(uint32_t)uart,&id,data,sizeof(data));assert(n);
|
||||
assert(id==0x1EE3FF00U);
|
||||
if(uart) las_uart(s,data,n);else las_can(s,id,data,n,1,0);
|
||||
}
|
||||
int main(void) {
|
||||
void *can=malloc(las_context_size()),*uart=malloc(las_context_size());
|
||||
uint64_t *indices=malloc(LAS_CAPACITY*sizeof(uint64_t));
|
||||
uint16_t *samples=malloc(LAS_CAPACITY*sizeof(uint16_t));
|
||||
uint8_t *breaks=malloc(LAS_CAPACITY);uint8_t packet[32];uint32_t id;size_t n,i;
|
||||
assert(can&&uart&&indices&&samples&&breaks);
|
||||
assert(las_init(can,14)&&las_init(uart,14));assert(!las_init(can,16));
|
||||
sample(can,1,0,0);assert(las_get(can,LAS_COUNT)==0);
|
||||
meta(can,1,0);meta(uart,1,1);
|
||||
/* Independent golden SETCAN bytes. */
|
||||
n=las_data(14,1,0x12345678U,0x8001,0,&id,packet,sizeof(packet));
|
||||
{ const uint8_t v[]={1,0,1,0x80,0x78,0x56,0x34,0x12};assert(n==8&&!memcmp(v,packet,8)); }
|
||||
for(i=0;i<9000;++i) { sample(can,1,(uint32_t)i,0);sample(uart,1,(uint32_t)i,1); }
|
||||
assert(las_get(can,LAS_COUNT)==8192&&las_get(uart,LAS_COUNT)==8192);
|
||||
assert(las_get(can,LAS_RECEIVED)==9000&&las_get(uart,LAS_RECEIVED)==9000);
|
||||
assert(las_snapshot(can,indices,samples,breaks,LAS_CAPACITY)==8192);
|
||||
assert(indices[0]==808&&indices[8191]==8999&&samples[8191]==8999);
|
||||
assert(las_snapshot(uart,indices,samples,breaks,LAS_CAPACITY)==8192);
|
||||
assert(indices[0]==808&&indices[8191]==8999&&samples[8191]==8999);
|
||||
sample(can,1,8999,1);assert(las_get(can,LAS_DUPLICATES)==1); /* second transport duplicate */
|
||||
sample(can,1,9003,0);assert(las_get(can,LAS_MISSING)==3);
|
||||
las_snapshot(can,indices,samples,breaks,LAS_CAPACITY);assert(breaks[8191]);
|
||||
/* Repeated metadata must not erase rolling history. */
|
||||
meta(can,1,1);assert(las_get(can,LAS_COUNT)==8192);
|
||||
meta(can,2,0);assert(las_get(can,LAS_COUNT)==0);
|
||||
sample(can,1,9004,1);assert(las_get(can,LAS_COUNT)==0);
|
||||
meta(can,1,1);assert(las_get(can,LAS_SESSION)==2);
|
||||
sample(can,2,0xffffffffU,0);sample(can,2,0,1);
|
||||
las_snapshot(can,indices,samples,breaks,LAS_CAPACITY);
|
||||
assert(indices[0]==0xffffffffULL&&indices[1]==0x100000000ULL&&!breaks[1]);
|
||||
/* Reject wrong address, standard ID, RTR and short payload. */
|
||||
n=las_data(14,2,1,0,0,&id,packet,sizeof(packet));assert(n==8);
|
||||
las_can(can,id,packet,8,0,0);las_can(can,id,packet,8,1,1);
|
||||
las_can(can,id^(1U<<20),packet,8,1,0);las_can(can,id,packet,7,1,0);
|
||||
assert(las_get(can,LAS_COUNT)==2&&las_get(can,LAS_INVALID)==1);
|
||||
n=las_data(14,2,1,1,1,&id,packet,sizeof(packet));packet[n-1]^=0x80;
|
||||
las_uart(can,packet,n);assert(las_get(can,LAS_CRC_ERRORS)==1);
|
||||
sample(can,2,1,1);assert(las_get(can,LAS_COUNT)==3); /* recovery after CRC */
|
||||
assert(!las_metadata(14,65536,1000,0,&id,packet,sizeof(packet)));
|
||||
assert(!las_metadata(14,1,0,0,&id,packet,sizeof(packet)));
|
||||
assert(!las_data(14,1,0,65536,0,&id,packet,sizeof(packet)));
|
||||
free(can);free(uart);free(indices);free(samples);free(breaks);
|
||||
puts("Altera SETCAN/CAN/UART stream tests passed");return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user