示例#1
0
int Wait_For_ACK(unsigned char *RecvData, int TimeOut)	// use this function to get ACK command
{
	int ret;
	int cnt;

	unsigned char RecvCmd[6] = {0, 0, 0, 0, 0, 0};
	int TotalBytesNumberToRead = 6;
printf("Wait_For_ACK()\n");
	ret = recvbytes(serial, RecvCmd, 6, 2000000);
printf("ret = %d\n", ret);
	if(!ret) {
		return -1;
	}

	if(ret != TotalBytesNumberToRead) {
		return -1;
	}

	if(RecvCmd[CMD_HEADER] == 0xaa && RecvCmd[CMD_ID] >= 0x01 && RecvCmd[CMD_ID] <= 0x0F) {
		ret = 0;
		for(cnt = 0; cnt < TotalBytesNumberToRead; cnt++) {
			printf("0x%02x ", RecvCmd[cnt]);
		}
		printf("\n");
//		memcpy(pRet_Resp_Cmd, Temp_Cmd_Buff, 6);
	} else {
		ret = -1;
	}

	return ret;
}
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);
}
void recvCall(int s, char **pfname, int *pnparams, arg_type **pa) {
    int fnamelen;

#ifdef _DEBUG_1_
    printf("In recvCall()\n"); fflush(stdout);
#endif

    recvbytes(s, (void *)(&fnamelen), sizeof(int));

#ifdef _DEBUG_1_
    printf("recvCall(), fnamelen = %d\n", fnamelen); fflush(stdout);
#endif

    if(fnamelen <= 0) {
	fprintf(stderr, "fnamelen = %d\n!", fnamelen);
	exit(1);
    }

    *pfname = (char *)calloc(fnamelen + 1, sizeof(char));
    memset(*pfname, 0, fnamelen+1);
    recvbytes(s, (void *)(*pfname), fnamelen);

#ifdef _DEBUG_1_
    printf("recvCall(), fname = %s\n", *pfname); fflush(stdout);
#endif

    recvbytes(s, (void *)(pnparams), sizeof(int));

#ifdef _DEBUG_1_
    printf("recvCall(), *pnparams = %d\n", *pnparams); fflush(stdout);
#endif

    if((*pnparams) < 0) {
	fprintf(stderr, "*pnparams = %d\n!", *pnparams);
	exit(1);
    }

    int i;
    for(i = 0; i < *pnparams; i++) {
	arg_type *newarg = (arg_type *)malloc(sizeof(arg_type));
	recvbytes(s, (void *)(&(newarg->arg_size)), sizeof(int));
	if((newarg->arg_size) <= 0) {
	    fprintf(stderr, "newarg[%d]->arg_size = %d\n!",
		    i, newarg->arg_size);
	    exit(1);
	}

	newarg->arg_val = (void *)malloc(newarg->arg_size);
	recvbytes(s, (void *)(newarg->arg_val), newarg->arg_size);

	newarg->next = NULL;
	if(i == 0) {
	    *pa = newarg;
	}
	else {
	    /* insert at the end */
	    arg_type *tmp;
	    for(tmp = *pa; tmp->next != NULL; tmp = tmp->next) ;
	    tmp->next = newarg;
	}
    }

    if(*pnparams <= 0) {
	*pa = NULL;
    }
}