int Send_ACK_Cmd() // use this function to send ACK command { int ret; unsigned char SendCmd[] = { C328_CMD_HEADER, C328_CMD_ACK, 0x00, 0x00, 0x00, 0x00 }; ret = sendbytes(serial, SendCmd, 6); usleep(1); return ret; }
void returnResult(int s, return_type *ret) { #ifdef _DEBUG_1_ printf("in returnResult()\n"); fflush(stdout); #endif if(ret == NULL || ret->return_size <= 0) { int i = 0; sendbytes(s, &i, sizeof(int)); return; } #ifdef _DEBUG_1_ printf("returnResult(), return_size = %d\n", ret->return_size); fflush(stdout); #endif /* else */ sendbytes(s, (void *)(&(ret->return_size)), sizeof(int)); sendbytes(s, ret->return_val, ret->return_size); }
int Send_SYNC_Cmd() // use this function to connect with c328. if success, return true, else return false. { int ret; unsigned char SendCmd[] = { C328_CMD_HEADER, C328_CMD_SYNC, 0x00, 0x00, 0x00, 0x00 }; unsigned char RecvCmd[6]; int i = 0; printf("Send_SYNC_Cmd()\n"); while(1) { i++; if(i > 60) { // you can edit the max times of sending sync command ret = -1; break; } printf("sendbytes()\n"); ret = sendbytes(serial, SendCmd, 6); printf("ret = %d\n", ret); if(ret == -1) { break; } usleep(50); ret = Wait_For_ACK(RecvCmd, 2); // read ACK command if( !ret || RecvCmd[CMD_HEADER] != C328_CMD_HEADER || RecvCmd[CMD_ID] != C328_CMD_ACK || RecvCmd[CMD_PARM1] != C328_CMD_SYNC ) { continue; } ret = Wait_For_ACK(RecvCmd, 2); // read SYNC command if( ret == -1 || RecvCmd[CMD_ID] != C328_CMD_SYNC ) { continue; } ret = Send_ACK_Cmd(); // send ACK command break; } usleep(50); return ret; }
int l3_write(struct l3_pins *adap, u8 addr, u8 *data, int len) { adap->setclk(1); adap->setdat(1); adap->setmode(1); udelay(adap->mode); adap->setmode(0); udelay(adap->mode_setup); sendbyte(adap, addr); udelay(adap->mode_hold); sendbytes(adap, data, len); adap->setclk(1); adap->setdat(1); adap->setmode(0); return len; }
int main(int argc, char **argv) { // Check arguments if (argc!=3) { printf("usage: putraw <device> <file>\n"); return 1; } int fd=open_port(argv[1]); if (fd==-1) { printf("unable to open device %s\n",argv[1]); return 2; } /* Get the file */ unsigned short datalen; struct stat st; stat(argv[2], &st); datalen = st.st_size; unsigned char *pblock=malloc(datalen); int ofd=open(argv[2], O_RDONLY|O_SYNC); int bytes=read(ofd,(void *)pblock,datalen); printf("Read file %s, length %d\n",argv[2],bytes); close(ofd); /* and file... */ sendbytes(fd,pblock,datalen); printf("Sent bytes %d\n",bytes); /* Close serial port */ close(fd); /* Free memory */ free(pblock); printf("The end\n"); }
return_type make_remote_call(const char *servernameorip, const int serverportnumber, const char *procedure_name, const int nparams, ...) { /* Setup socket, connect(), etc. */ struct addrinfo hints, *result; hints.ai_family = AF_INET; hints.ai_socktype = hints.ai_protocol = hints.ai_flags = 0; if(getaddrinfo(servernameorip, NULL, (const struct addrinfo *)(&hints), &result) != 0) { perror("getaddrinfo"); exit(1); } struct sockaddr_in svra; memcpy(&svra, result->ai_addr, sizeof(struct sockaddr_in)); svra.sin_port = htons(serverportnumber); freeaddrinfo(result); int s = socket(AF_INET, SOCK_STREAM, 0); if(s < 0) { perror("socket"); exit(1); } struct sockaddr_in mya; memset(&mya, 0, sizeof(struct sockaddr_in)); mya.sin_family = AF_INET; if(bind(s, (const struct sockaddr *)(&mya), sizeof(struct sockaddr_in)) < 0) { perror("bind"); exit(1); } if(connect(s, (const struct sockaddr *)(&svra), sizeof(struct sockaddr_in)) < 0) { perror("connect"); exit(1); } /* Make the remote call */ int procnamelen = strlen(procedure_name)+1; sendbytes(s, (void *)(&procnamelen), sizeof(int)); #ifdef _DEBUG_1_ printf("Send proc name len %d\n", procnamelen); fflush(stdout); #endif sendbytes(s, (void *)procedure_name, procnamelen); #ifdef _DEBUG_1_ printf("Send proc name %s\n", procedure_name); fflush(stdout); #endif sendbytes(s, (void *)(&nparams), sizeof(int)); #ifdef _DEBUG_1_ printf("Send nparams %d\n", nparams); fflush(stdout); #endif va_list ap; int i; va_start(ap, nparams); for(i = 0; i < nparams; i++) { int psize; void *pval; psize = va_arg(ap, int); pval = va_arg(ap, void *); #ifdef _DEBUG_1_ printf("The value of psize %d\n", psize); //printf("The value of pval %s\n", (char *)pval); #endif #ifdef _DEBUG_1_ // not always a valid assumption e.g. tmp\0 could be a pval if(psize == sizeof(int)) { //printf("make_remote_call(), int-sized arg: %d\n", // *(int *)pval); //fflush(stdout); } #endif sendbytes(s, (void *)(&psize), sizeof(int)); sendbytes(s, pval, psize); } va_end(ap); /* Get the result */ recvbytes(s, (void *)(&(r.return_size)), sizeof(int)); if(r.return_size < 0) { /* Error! */ fprintf(stderr, "Received %d return_size.\n", r.return_size); exit(1); } else if(r.return_size == 0) { r.return_val = NULL; } else { r.return_val = (void *)malloc(r.return_size); recvbytes(s, r.return_val, r.return_size); } shutdown(s, SHUT_RDWR); close(s); /* Warning! Potential memory leak -- r.return_val */ return(r); }
static TR_STATUS sendline(net_fd s, const char *buf) { return sendbytes(s, buf, strlen(buf)); }