Ejemplo n.º 1
0
int main(int argc, char *argv[]) {
	int ret;
	void *p;
	
	/* this is the connection we will make... again, you don't have access to the struct */
	struct xbee_con *conA;
	struct xbee_con *conB;
	
	/* the packet that is recieved... you have access to this! (see xbee.h) */
	struct xbee_pkt *pkt;
	
	/* set the log level REALLY high, so we can see all the messages. default is 0 */
	//xbee_logSetLevel(100);

	/* setup the xbee instances! */
	if (setupXbee(&xbeeA, "/dev/ttyUSB0", &conA, 0x0013A200, 0x402D607E)) exit(1);
	if (setupXbee(&xbeeB, "/dev/ttyUSB1", &conB, 0x0013A200, 0x402D607B)) exit(2);

	usleep(25000);

	/* start the chain reaction! */
	xbee_conTx(xbeeA, conA, "Hello!");

	/* the main loop */
	for (;;) {
		sleep(5);
	}
	
	/* shutdown the libxbee instance */
	xbee_shutdown(xbeeA);
	xbee_shutdown(xbeeB);
	
	return 0;
}
Ejemplo n.º 2
0
bool
Server::Send(const std::string& data)
{
    xbee_err ret;

    xbee_conTx(_connection, NULL, data.c_str());
    return true;
}
Ejemplo n.º 3
0
void myCB(struct xbee *xbee, struct xbee_con *con, struct xbee_pkt **pkt, void **data) {
	if ((*pkt)->dataLen > 0) {
		printf("rx: [%s]\n", (*pkt)->data);
		if (!strncmp((*pkt)->data, "Hello?",7)) {
			xbee_conTx(con, NULL, "Hi!");
		}
	}
}
Ejemplo n.º 4
0
int main(void) {
	void *d;
	struct xbee *xbee;
	struct xbee_con *con;
	struct xbee_pkt *pkt;
	struct xbee_conAddress address;
	char txRet;
	int i;
	xbee_err ret;

	if ((ret = xbee_setup(&xbee, "xbee2", "/dev/ttyUSB1", 57600)) != XBEE_ENONE) {
		printf("ret: %d (%s)\n", ret, xbee_errorToStr(ret));
		return ret;
	}

	memset(&address, 0, sizeof(address));
	address.addr64_enabled = 1;
	address.addr64[0] = 0x00;
	address.addr64[1] = 0x13;
	address.addr64[2] = 0xA2;
	address.addr64[3] = 0x00;
	address.addr64[4] = 0x40;
	address.addr64[5] = 0x2D;
	address.addr64[6] = 0x60;
	address.addr64[7] = 0x7B;
	if ((ret = xbee_conNew(xbee, &con, "Remote AT", &address)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret));
		return ret;
	}
	
	for (i = 0; i < 60 * 4; i++) {
		unsigned char value;
		if ((ret = xbee_conTx(con, NULL, "IS")) != XBEE_ENONE) break;
		if ((ret = xbee_conRx(con, &pkt, NULL)) != XBEE_ENONE) break;
		
		if ((ret = xbee_pktDigitalGet(pkt, 3, 0, &value)) != XBEE_ENONE) {
			printf("xbee_pktDigitalGet(channel=3): ret %d\n", ret);
		} else {
			printf("D3: %d\n", value);
		}
		
		xbee_pktFree(pkt);
		usleep(250000);
	}
	if (ret != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conTx() or xbee_conRx() returned: %d", ret);
		return ret;
	}
	
	if ((ret = xbee_conEnd(con)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conEnd() returned: %d", ret);
		return ret;
	}

	xbee_shutdown(xbee);

	return 0;
}
Ejemplo n.º 5
0
void myCB(struct xbee *xbee, struct xbee_con *con, struct xbee_pkt **pkt, void **data) {
	if ((*pkt)->dataLen > 0) {
		if ((*pkt)->data[0] == '@') {
			xbee_conCallbackSet(con, NULL, NULL);
			printf("*** DISABLED CALLBACK... ***\n");
		}
		printf("rx: [%s]\n", (*pkt)->data);
	}
	printf("tx: %d\n", xbee_conTx(con, NULL, "Hello\r\n"));
}
Ejemplo n.º 6
0
int main(void) {
	void *d;
	struct xbee *xbee;
	struct xbee_con *con;
	unsigned char txRet;
	xbee_err ret;

	/* setup libxbee, using the USB to Serial adapter '/dev/ttyUSB0' at 57600 baud */
	if ((ret = xbee_setup(&xbee, "xbee1", "/dev/ttyUSB0", 57600)) != XBEE_ENONE) {
		printf("ret: %d (%s)\n", ret, xbee_errorToStr(ret));
		return ret;
	}

	/* create a new AT connection to the local XBee */
	if ((ret = xbee_conNew(xbee, &con, "Local AT", NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret));
		return ret;
	}

	/* send the AT command 'NI' (request the Node Identifier)
	   when the response is recieved, the packet will be directed to the callback function */
	ret = xbee_conTx(con, &txRet, "NI");
	/* print out the return value
	   if this is non-zero, then check 'enum xbee_errors' in xbee.h for its meaning
	   alternatively look at the xbee_errorToStr() function */
	printf("tx: %d\n", ret);
	if (ret) {
		/* if ret was non-zero, then some error occured
		   if ret == XBEE_ETX then it is possible that txRet is now -17 / XBEE_ETIMEOUT
		   alternatively, txRet will contain the status code returned by the XBee */
		printf("txRet: %d\n", txRet);
	} else {
		struct xbee_pkt *pkt;
		if ((ret = xbee_conRx(con, &pkt, NULL)) != XBEE_ENONE) {
			printf("Error after calling xbee_conRx(): %s\n", xbee_errorToStr(ret));
		} else {
			int i;
			printf("Response is %d bytes long:\n", pkt->dataLen);
			for (i = 0; i < pkt->dataLen; i++) {
				printf("%3d: 0x%02X - %c\n", i, pkt->data[i], (((pkt->data[i] >= ' ') && (pkt->data[i] <= '~'))?pkt->data[i]:'.'));
			}
		}
	}
	
	/* shutdown the connection */
	if ((ret = xbee_conEnd(con)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conEnd() returned: %d", ret);
		return ret;
	}

	/* shutdown libxbee */
	xbee_shutdown(xbee);

	return 0;
}
Ejemplo n.º 7
0
int main(void) {
	void *d;
	struct xbee *xbee;
	struct xbee_con *con;
	xbee_err ret;
	unsigned char txRet;
	struct timespec to;

	if (sem_init(&ndComplete, 0, 0) != 0) {
		printf("sem_init() returned an error: %d - %s\n", errno, strerror(errno));
		return -1;
	}
	
	if ((ret = xbee_setup(&xbee, "xbee1", "/dev/ttyUSB0", 57600)) != XBEE_ENONE) {
		printf("ret: %d (%s)\n", ret, xbee_errorToStr(ret));
		return ret;
	}

	if ((ret = xbee_conNew(xbee, &con, "Local AT", NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret));
		return ret;
	}

	if ((ret = xbee_conCallbackSet(con, nodeCB, NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conCallbackSet() returned: %d", ret);
		return ret;
	}

	if ((ret = xbee_conTx(con, &txRet, "ND")) != XBEE_ENONE && (ret != XBEE_ETX && ret != XBEE_ETIMEOUT)) {
		xbee_log(xbee, -1, "xbee_conTx() returned: %d-%d", ret, txRet);
		return ret;
	}

	printf("ND Sent!... waiting for completion\n");

	clock_gettime(CLOCK_REALTIME, &to);
	to.tv_sec  += 10;
	if (sem_timedwait(&ndComplete, &to) != 0) {
		if (errno == ETIMEDOUT) {
			printf("Timeout while waiting for ND command to complete...\n");
		} else {
			printf("Error calling sem_timedwait()... sleeping for 10 seconds instead\n");
			sleep(10);
		}
	}

	if ((ret = xbee_conEnd(con)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conEnd() returned: %d", ret);
		return ret;
	}

	xbee_shutdown(xbee);

	return 0;
}
Ejemplo n.º 8
0
int main(void) {
	void *d;
	struct xbee *xbee;
	struct xbee_con *con;
	struct xbee_conAddress address;
	struct xbee_conSettings settings;
	xbee_err ret;

	if ((ret = xbee_setup(&xbee, "xbee2", "/dev/ttyUSB1", 57600)) != XBEE_ENONE) {
		printf("ret: %d (%s)\n", ret, xbee_errorToStr(ret));
		return ret;
	}

	memset(&address, 0, sizeof(address));
	address.addr64_enabled = 1;
	address.addr64[0] = 0x00;
	address.addr64[1] = 0x00;
	address.addr64[2] = 0x00;
	address.addr64[3] = 0x00;
	address.addr64[4] = 0x00;
	address.addr64[5] = 0x00;
	address.addr64[6] = 0xFF;
	address.addr64[7] = 0xFF;
	if ((ret = xbee_conNew(xbee, &con, "Data", &address)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret));
		return ret;
	}

	if ((ret = xbee_conCallbackSet(con, myCB, NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conCallbackSet() returned: %d", ret);
		return ret;
	}

	/* getting an ACK for a broadcast message is kinda pointless... */
	xbee_conSettings(con, NULL, &settings);
	settings.disableAck = 1;
	xbee_conSettings(con, &settings, NULL);

	for (;;) {
		xbee_conTx(con, NULL, "Hello...\r\n");

		/* you probrably don't want to transmit much quicker than once per 2 seconds... read the datashee for more info */
		sleep(2);
	}

	if ((ret = xbee_conEnd(con)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conEnd() returned: %d", ret);
		return ret;
	}

	xbee_shutdown(xbee);

	return 0;
}
Ejemplo n.º 9
0
int main(void) {
	void *d;
	struct xbee *xbee;
	struct xbee_con *con;
	struct xbee_conAddress address;
	unsigned char txRet;
	xbee_err ret;

	if ((ret = xbee_setup(&xbee, "xbee2", "/dev/ttyUSB1", 57600)) != XBEE_ENONE) {
		printf("ret: %d (%s)\n", ret, xbee_errorToStr(ret));
		return ret;
	}

	/* this is the 64-bit address of the remote XBee module
	   it should be entered with the MSB first, so the address below is
	   SH = 0x0013A200    SL = 0x40081826 */
	memset(&address, 0, sizeof(address));
	address.addr64_enabled = 1;
	address.addr64[0] = 0x00;
	address.addr64[1] = 0x13;
	address.addr64[2] = 0xA2;
	address.addr64[3] = 0x00;
	address.addr64[4] = 0x40;
	address.addr64[5] = 0x2D;
	address.addr64[6] = 0x60;
	address.addr64[7] = 0x7B;
	if ((ret = xbee_conNew(xbee, &con, "Remote AT", &address)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret));
		return ret;
	}

	if ((ret = xbee_conCallbackSet(con, myCB, NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conCallbackSet() returned: %d", ret);
		return ret;
	}
	
	ret = xbee_conTx(con, &txRet, "NI");
	printf("tx: %d\n", ret);
	if (ret) {
		printf("txRet: %d\n", txRet);
	} else {
		usleep(1000000);
	}
	
	if ((ret = xbee_conEnd(con)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conEnd() returned: %d", ret);
		return ret;
	}

	xbee_shutdown(xbee);

	return 0;
}
Ejemplo n.º 10
0
int main(void) {
	void *d;
	struct xbee *xbee;
	struct xbee_con *con;
	struct xbee_conAddress address;
	char txRet;
	xbee_err ret;

	if ((ret = xbee_setup(&xbee, "xbee2", "/dev/ttyUSB1", 57600)) != XBEE_ENONE) {
		printf("ret: %d (%s)\n", ret, xbee_errorToStr(ret));
		return ret;
	}

	memset(&address, 0, sizeof(address));
	address.addr64_enabled = 1;
	address.addr64[0] = 0x00;
	address.addr64[1] = 0x13;
	address.addr64[2] = 0xA2;
	address.addr64[3] = 0x00;
	address.addr64[4] = 0x40;
	address.addr64[5] = 0x2D;
	address.addr64[6] = 0x60;
	address.addr64[7] = 0x7B;
	if ((ret = xbee_conNew(xbee, &con, "Remote AT", &address)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret));
		return ret;
	}

	if ((ret = xbee_conCallbackSet(con, myCB, NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conCallbackSet() returned: %d", ret);
		return ret;
	}
	
	ret = xbee_conTx(con, &txRet, "NI");
	printf("tx: %d\n", ret);
	if (ret) {
		printf("txRet: %d\n", txRet);
	} else {
		sleep(1);
	}
	
	if ((ret = xbee_conEnd(con)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conEnd() returned: %d", ret);
		return ret;
	}

	xbee_shutdown(xbee);

	return 0;
}
Ejemplo n.º 11
0
/* just echo it back */
void callback(struct xbee *xbee, struct xbee_con *con, struct xbee_pkt **pkt, void **data) {
	xbee_err ret;
	unsigned char retVal;
	int seq;
	int pktLen;

	if ((*pkt)->dataLen < 6) {
		xbee_log(xbee, -10, "SHRT - short packet...");
		ret = xbee_conTx(con, &retVal, "SHRT");
	} else {

		seq  = ((*pkt)->data[0] << 24) & 0xFF000000;
		seq |= ((*pkt)->data[1] << 16) & 0xFF0000;
		seq |= ((*pkt)->data[2] <<  8) & 0xFF00;
		seq |= ((*pkt)->data[3]      ) & 0xFF;

		pktLen  = (((*pkt)->data[4]) << 8) & 0xFF00;
		pktLen |= (((*pkt)->data[5])     ) & 0xFF;

		if (pktLen < 6 || pktLen != (*pkt)->dataLen - 6) {
			xbee_log(xbee, -10, "XLEN - packet length mismatch... (rx'd %d / expected %d bytes)", (*pkt)->dataLen - 6, pktLen);
			ret = xbee_conTx(con, &retVal, "XLEN");
		} else if (seq >= PACKET_COUNT) {
			xbee_log(xbee, -10, "DONE - test complete!");
			ret = xbee_conTx(con, &retVal, "DONE");
			*data = NULL;
		} else {
			xbee_log(xbee, -10, "Tx #%d", seq);
			ret = xbee_connTx(con, &retVal, (*pkt)->data, (*pkt)->dataLen);
		}
	}

	if (ret != XBEE_ENONE) {
		xbee_log(xbee, -1, "ret = %d      retVal = %d", ret, retVal);
	}
}
Ejemplo n.º 12
0
/* the callback function */
void myCB(struct xbee *xbee, struct xbee_con *con, struct xbee_pkt **pkt, void **userData) {
	/* who recieved the message? */
	if (xbee == xbeeA) {
		printf("xbeeA rcvd:   ");
	} else if (xbee == xbeeB) {
		printf("xbeeB rcvd:   ");
	} else {
		printf("unknown rcvd: ");
	}

	/* what did the message say? */
	printf("They said this: %*s\n", (*pkt)->datalen, (*pkt)->data);

	usleep(250000);

	/* respond! */
	xbee_conTx(xbee, con, "%*s", (*pkt)->datalen, (*pkt)->data);
}
Ejemplo n.º 13
0
int main(void) {
	void *d;
	struct xbee *xbee;
	struct xbee_con *con;
	char txRet;
	xbee_err ret;

	if ((ret = xbee_setup(&xbee, "xbee1", "/dev/ttyUSB0", 57600)) != XBEE_ENONE) {
		printf("ret: %d (%s)\n", ret, xbee_errorToStr(ret));
		return ret;
	}

	if ((ret = xbee_conNew(xbee, &con, "Local AT", NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret));
		return ret;
	}

	if ((ret = xbee_conCallbackSet(con, myCB, NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conCallbackSet() returned: %d", ret);
		return ret;
	}
	
	ret = xbee_conTx(con, &txRet, "NI");
	printf("tx: %d\n", ret);
	if (ret) {
		printf("txRet: %d\n", txRet);
	} else {
		sleep(1);
	}
	
	if ((ret = xbee_conEnd(con)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conEnd() returned: %d", ret);
		return ret;
	}

	xbee_shutdown(xbee);

	return 0;
}
Ejemplo n.º 14
0
int main(void) {
	void *d;
	struct xbee *xbee;
	struct xbee_con *con;
	struct xbee_conAddress address;
	xbee_err ret;

	if ((ret = xbee_setup(&xbee, "debug", "xbee1")) != XBEE_ENONE) {
		printf("ret: %d (%s)\n", ret, xbee_errorToStr(ret));
		return ret;
	}

	memset(&address, 0, sizeof(address));
	address.addr64_enabled = 1;
	address.addr64[0] = 0x00;
	address.addr64[1] = 0x13;
	address.addr64[2] = 0xA2;
	address.addr64[3] = 0x00;
	address.addr64[4] = 0x40;
	address.addr64[5] = 0x08;
	address.addr64[6] = 0x18;
	address.addr64[7] = 0x26;
	if ((ret = xbee_conNew(xbee, &con, "64-bit Data", &address)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret));
		return ret;
	}

	xbee_conTx(con, NULL, "testing...");

	if ((ret = xbee_conEnd(con)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conEnd() returned: %d", ret);
		return ret;
	}

	xbee_shutdown(xbee);

	return 0;
}
Ejemplo n.º 15
0
static xbee_err prepare_backchannel(struct xbee *xbee) {
	xbee_err ret;
	struct xbee_modeData *data;
	unsigned char retVal;
	struct xbee_conAddress address;
	struct xbee_pkt *pkt;
	int callbackCount;
	int i, pos, slen;
	struct xbee_con *bc_start;
	
	data = xbee->modeData;
	pkt = NULL;
	
	/* create the 'start' backchannel connection - this is ALWAYS ON ENDPOINT 0x00 */
	memset(&address, 0, sizeof(address));
	address.endpoints_enabled = 1;
	address.endpoint_local = 0;
	address.endpoint_remote = 0;
	
	if ((ret = _xbee_conNew(xbee, &xbee->iface, 1, &bc_start, "backchannel", &address)) != XBEE_ENONE) return ret;
	
	/* transmit our libxbee_commit string - the git commit id */
	if ((ret = xbee_conTx(bc_start, &retVal, "%s", libxbee_commit)) != XBEE_ENONE) {
		switch (retVal) {
			case 1:
				xbee_log(0, "The server encountered an internal error");
				break;
			case 2:
				xbee_log(0, "The server is running a different version of libxbee");
				break;
			default:
				xbee_log(0, "Failed to initialize connection to server for an unknown reason...");
		}
		goto done;
	}
	
	/* grab the returned data (an in-order list of the back channel endpoints, starting at 0x01) */
	if ((ret = xbee_conRx(bc_start, &pkt, NULL)) != XBEE_ENONE) goto done;
	
	/* pick out the remote system's mode name, and try to locate it */
	for (i = 0; i < pkt->dataLen && pkt->data[i] != '\0'; i++);
	if (i > 0) {
		if ((data->serverModeName = malloc(sizeof(char) * (i + 1))) == NULL) {
			ret = XBEE_ENOMEM;
			goto done;
		}
		strncpy(data->serverModeName, (char*)pkt->data, i);
		data->serverModeName[i] = '\0';
		
		if (xbee_modeRetrieve(data->serverModeName, &data->serverMode) != XBEE_ENONE) {
			xbee_log(-10, "WARNING: remote mode '%s' is not avaliable on this system... Some packets may not be fully processed", data->serverModeName);
		}
	}
	
	callbackCount = pkt->data[i + 1];
	
	memset(&address, 0, sizeof(address));
	address.endpoints_enabled = 1;
	
	for (pos = i + 2, i = 1; pos < pkt->dataLen && i < callbackCount + 1; pos += slen + 1, i++) {
		char *name;
		struct xbee_con **retCon;
		
		name = (char *)&(pkt->data[pos]);
		slen = strlen(name);
		
		/* check for a buffer overflow */
		if (slen > pkt->dataLen - pos) {
			slen = pkt->dataLen - pos;
			name[slen] = '\0';
		}
		
		retCon = NULL;
		
		/* try to match the string with an element in struct xbee_modeData */
#define TRY(conName)  if (!data->bc_##conName && !strncasecmp(name, #conName, slen))
		TRY (conValidate) {
			retCon = &data->bc_conValidate;
		} else TRY (conSleep) {
			retCon = &data->bc_conSleep;
		} else TRY (conSettings) {
Ejemplo n.º 16
0
void config_load(FILE *f, struct xbee_con *con, int write) {
	char *buf;
	int bufLen;
	char *p, *q;
	int a, b;
	unsigned char val;
	
	xbee_err ret;
	struct xbee_pkt *pkt;
	unsigned char retVal;
	struct xbee_conSettings settings;
	
	if (xbee_conSettings(con, NULL, &settings) != XBEE_ENONE) return;
	settings.queueChanges = 1;
	if (xbee_conSettings(con, &settings, NULL) != XBEE_ENONE) return;
	
	buf = NULL;
	bufLen = 0;
	
	while (!feof(f)) {
		if (getline(&buf, &bufLen, f) == -1) {
			if (feof(f)) break;
			fprintf(stderr, "\ngetline(): unknown error...\n");
			exit(1);
		}
		
		if (buf[0] == '#') continue;
		
		for (a = 0; achars[a]; a++) {
			if (achars[a] == buf[0]) break;
		}
		if (!achars[a]) goto skip;
		
		for (b = 0; bchars[b]; b++) {
			if (bchars[b] == buf[1]) break;
		}
		if (!bchars[b]) goto skip;
		
		p = &(buf[2]);
		q = strchr(p, '=');
		*q = '\0';
		q++;
		
		while (*q != '\0' && *q != '\n') {
			while (*q == ' ') { q++; }
			if (sscanf(q, "0x%02hhX", &val) != 1) {
				fprintf(stderr, "\nInvalid parameters for %c%c\n", buf[0], buf[1]);
				exit(1);
			}
			q += 4;
			*p = val;
			if (p != &(buf[2]) || val != 0) p++;
		}
		if (p == &(buf[2])) p++;
		
		fprintf(stderr, "\r%c%c...", buf[0], buf[1]);
		fflush(stderr);
		if ((ret = xbee_conPurge(con)) != XBEE_ENONE) {
			fprintf(stderr, "\nxbee_conPurge(): %d - %s\n", ret, xbee_errorToStr(ret));
			exit(1);
		}
		if ((ret = xbee_connTx(con, &retVal, buf, p - buf + 1)) != XBEE_ENONE && ret != XBEE_ETX) {
			fprintf(stderr, "\nxbee_conTx(): %d - %s\n", ret, xbee_errorToStr(ret));
			exit(1);
		}
		if (retVal != 0) {
			fprintf(stderr, "\nError sending command: %c%c - %hhd\n", buf[0], buf[1], retVal);
			//exit(1);
		}
		continue;
		
skip:
		fprintf(stderr, "\nSkipping invalid command: %c%c\n", buf[0], buf[1]);
	}
	
	if (buf) free(buf);
	
	if (xbee_conSettings(con, NULL, &settings) != XBEE_ENONE) return;
	settings.queueChanges = 0;
	if (xbee_conSettings(con, &settings, NULL) != XBEE_ENONE) return;
	
	xbee_conTx(con, NULL, "AC");
	if (write) xbee_conTx(con, NULL, "WR");
}
Ejemplo n.º 17
0
int main(int argc, char *argv[]) {
	int ret;
	sem_t sem;
	void *p;
	
	/* this is our xbee instance... from 'user' space you don't have access to the struct */
	struct xbee *xbee;
	
	/* this is the connection we will make... again, you don't have access to the struct */
	struct xbee_con *con;
	struct xbee_conAddress addr;
	
	/* the packet that is recieved... you have access to this! (see xbee.h) */
	struct xbee_pkt *pkt;
	
	/* make a lixbee instance, and connect it to /dev/ttyUSB1 @ 57600 baud
	   you don't have to keep hold of the returned xbee, in which case you can pass NULL and the most recently started instance will be used! */
	if ((ret = xbee_setup("/dev/ttyUSB0", 57600, &xbee)) != 0) {
		xbee_log(NULL,-1,"xbee_setup(): failed... (%d)", ret);
		exit(1);
	}
	/* setup libxbee to use the series 1 packets - you have to do this before you do anything else! */
	xbee_modeSet(xbee, "series1");
	
	/* get the connection type ID, you pass in a string, it returns an ID */
	if ((ret = xbee_conTypeIdFromName(xbee, "Remote AT", &conType)) != 0) {
		xbee_log(xbee,-1,"xbee_conTypeIdFromName(): failed... (%d)", ret);
		exit(1);
	}
	
	if ((ret = sem_init(&sem, 0, 0)) != 0) {
		xbee_log(xbee,-1,"sem_init(): failed... (%d)", ret);
		exit(1);
	}
	
	/* clear the address field */
	memset(&addr, 0, sizeof(addr));
	/* build a connection to the following address */
	addr.addr64_enabled = 1;
	addr.addr64[0] = 0x00;
	addr.addr64[1] = 0x13;
	addr.addr64[2] = 0xA2;
	addr.addr64[3] = 0x00;
	addr.addr64[4] = 0x40;
	addr.addr64[5] = 0x33;
	addr.addr64[6] = 0xCA;
	addr.addr64[7] = 0xCB;
	if ((ret = xbee_conNew(xbee, &con, conType, &addr, &sem)) != 0) {
		xbee_log(xbee,-1,"xbee_newcon(): failed... (%d)", ret);
		exit(1);
	}
	{
    struct xbee_conOptions opts;
    /* enable waitForAck... this allows us to see if the packet was sent successfully! */
    xbee_conOptions(xbee, con, &opts, NULL);
    opts.waitForAck = 1;
    xbee_conOptions(xbee, con, NULL, &opts);
	}
	/* attach the callback */
	xbee_conAttachCallback(xbee, con, myCB, NULL);

	/* send the request */
	if ((ret = xbee_conTx(xbee, con, "NI")) != 0) {
		xbee_log(xbee,-1,"Something went wrong... (%d)", ret);
	} else {
		struct timespec to;
		clock_gettime(CLOCK_REALTIME, &to);
		to.tv_sec += 5;
		if (sem_timedwait(&sem, &to)) {
			printf("Timeout...\n");
		}
	}

	sem_destroy(&sem);

	/* shutdown the connection */
	xbee_conEnd(xbee, con, NULL);
	
	/* shutdown the libxbee instance */
	xbee_shutdown(xbee);
	
	return 0;
}
Ejemplo n.º 18
0
int main(void) {
	void *d;
	struct xbee *xbee;
	struct xbee_con *con;
	char txRet;
	xbee_err ret;
	int i, o, t;

	sem_init(&sem, 0, 0);

	if ((ret = xbee_setup(&xbee, "xbee1", "/dev/ttyUSB0", 57600)) != XBEE_ENONE) {
		printf("ret: %d (%s)\n", ret, xbee_errorToStr(ret));
		return ret;
	}

	if ((ret = xbee_conNew(xbee, &con, "Local AT", NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret));
		return ret;
	}

	if ((ret = xbee_conCallbackSet(con, myCB, NULL)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conCallbackSet() returned: %d", ret);
		return ret;
	}
	
	o = 0;
	t = 0;

	for (i = 0; i < 1000; i++) {
		ret = xbee_conTx(con, &txRet, "NI");
		printf("tx: %d\n", ret);
		if (ret) {
			printf("txRet: %d\n", txRet);
			sleep(1);
		} else {
			struct timespec to;
			clock_gettime(CLOCK_REALTIME, &to);
			to.tv_sec++;
			if (sem_timedwait(&sem, &to) == 0) {
				o++;
				usleep(10000);
			} else {
				printf("             TIMEOUT!\n");
				usleep(250000);
				t++;
			}
		}
	}

	printf("%d / %d / %d - success/timeout/total - success rate (%2.1f%%) / timeout rate (%2.1f%%)\n",
		o, t, i, (double)((double)o/(double)i)*100.0, (double)((double)t/(double)i)*100.0);
	
	if ((ret = xbee_conEnd(con)) != XBEE_ENONE) {
		xbee_log(xbee, -1, "xbee_conEnd() returned: %d", ret);
		return ret;
	}

	xbee_shutdown(xbee);

	return 0;
}
Ejemplo n.º 19
0
void config_save(FILE *f, struct xbee_con *con) {
	int a,b,s,l,q;
	int i;
	
	xbee_err ret;
	unsigned char retVal;
	struct xbee_pkt *pkt;
	unsigned char cmd[3];
	
	l = sizeof(skip) / sizeof(*skip);
	fprintf(stderr, "Skipping %d special commands:", l);
	for (i = 0; i < l; i++) {
		if (!(i % 10)) fprintf(stderr,"\n  ");
		fprintf(stderr, "%s%s", (i % 10 ? ", " : ""), skip[i]);
	}
	fprintf(stderr, "\n");
	
	i = 0;
	for (a = 0; achars[a]; a++) {
		for (b = 0; bchars[b]; b++) {
			for (s = 0; s < l; s++) {
				if (!skip[s]) continue;
				if (skip[s][0] == '\0') continue;
				if (skip[s][1] == '\0') continue;
				if (achars[a] == skip[s][0] && bchars[b] == skip[s][1]) break;
			}
			if (s != l) continue;
			if ((ret = xbee_conPurge(con)) != XBEE_ENONE) {
				fprintf(stderr, "xbee_conPurge(): %d - %s\n", ret, xbee_errorToStr(ret));
				exit(1);
			}
			cmd[0] = achars[a];
			cmd[1] = bchars[b];
			cmd[2] = '\0';
			if ((ret = xbee_conTx(con, &retVal, "%c%c", achars[a], bchars[b])) != XBEE_ENONE && ret != XBEE_ETX) {
				fprintf(stderr, "xbee_conTx(): %d - %s\n", ret, xbee_errorToStr(ret));
				exit(1);
			}
			if (retVal != 0) continue;
			if ((ret = xbee_conRx(con, &pkt, NULL)) != XBEE_ENONE) {
				fprintf(stderr, "xbee_conRx(): %d - %s\n", ret, xbee_errorToStr(ret));
				exit(1);
			}
			if (pkt->status != 0) {
				fprintf(stderr, "xbee_conRx(): AT command returned error - %d\n", pkt->status);
				exit(1);
			}
			if (strncasecmp(pkt->atCommand, cmd, 2)) {
				fprintf(stderr, "xbee_conRx(): AT command response mis-match\n");
				exit(1);
			}
			if (pkt->dataLen > 0) {
				fprintf(stderr, "\r%c%c...", achars[a], bchars[b]);
				fflush(stderr);
				fprintf(f, "%c%c =", achars[a], bchars[b]);
				for (q = 0; q < pkt->dataLen; q++) {
					fprintf(f, " 0x%02X", pkt->data[q]);
				}
				fprintf(f, "\n");
				i++;
			}
			xbee_pktFree(pkt);
		}
	}
	
	printf("\rTotal: %d\n", i);
}