Exemplo n.º 1
0
Arquivo: pkt.c Projeto: C24IO/libxbee3
xbee_err xbee_pktDataKeyGet(struct xbee_pkt *pkt, const char *key, int id, struct pkt_dataKey **retKey) {
	xbee_err ret;
	struct pkt_dataKey *k;
	
	if (!pkt || !key) return XBEE_EMISSINGPARAM;
#ifndef XBEE_DISABLE_STRICT_OBJECTS
	if (xbee_pktValidate(pkt) != XBEE_ENONE) return XBEE_EINVAL;
#endif /* XBEE_DISABLE_STRICT_OBJECTS */
	
	xbee_ll_lock(pkt->dataItems);
	ret = XBEE_EFAILED;
	for (k = NULL; (_xbee_ll_get_next(pkt->dataItems, k, (void**)&k, 0) == XBEE_ENONE) && k; ) {
		if (!strncasecmp(key, k->name, PKT_DATAKEY_MAXLEN)) {
			if (id == -1 || id == k->id) {
				if (retKey) *retKey = k;
				ret = XBEE_ENONE;
				break;
			}
		}
	}
	xbee_ll_unlock(pkt->dataItems);
	
	return ret;
}
Exemplo n.º 2
0
xbee_err xbee_netTx(struct xbee *xbee, void *arg, struct xbee_sbuf *buf) {
    int pos, ret;
    int fd;
    size_t txSize;
    size_t memSize;
    struct xbee_buf *iBuf;

    size_t *txBufSize;
    struct xbee_buf **txBuf;

    if (!xbee || !buf) return XBEE_EMISSINGPARAM;

    if (arg) {
        /* this is on the server end */
        struct xbee_netClientInfo *info;
        info = arg;
        if (xbee != info->xbee) return XBEE_EINVAL;
        fd = info->fd;

        txBufSize = &info->txBufSize;
        txBuf = &info->txBuf;
    } else {
        /* this is on the client end */
        struct xbee_modeData *info;
        info = xbee->modeData;
        fd = info->netInfo.fd;

        txBufSize = &info->netInfo.txBufSize;
        txBuf = &info->netInfo.txBuf;
    }

    txSize = 3 + buf->len;
    memSize = txSize + sizeof(*iBuf);

    iBuf = *txBuf;
    if (!iBuf || *txBufSize < memSize) {
        void *p;

        /* make sure we save this buffer... */
        xbee_ll_lock(needsFree);
        if ((p = realloc(iBuf, memSize)) == NULL) {
            xbee_ll_unlock(needsFree);
            return XBEE_ENOMEM;
        }
        if (iBuf) _xbee_ll_ext_item(needsFree, iBuf, 0);
        _xbee_ll_add_tail(needsFree, p, 0);
        xbee_ll_unlock(needsFree);
        iBuf = p;

        *txBuf = iBuf;
        *txBufSize = memSize;
    }

    iBuf->len = txSize;
    iBuf->data[0] = 0x7E;
    iBuf->data[1] = ((buf->len - 1) >> 8) & 0xFF;
    iBuf->data[2] = ((buf->len - 1)     ) & 0xFF;
    memcpy(&(iBuf->data[3]), buf->data, buf->len);

    for (pos = 0; pos < iBuf->len; pos += ret) {
        ret = send(fd, iBuf->data, iBuf->len - pos, MSG_NOSIGNAL);
        if (ret >= 0) continue;
        return XBEE_EIO;
    }

    return XBEE_ENONE;
}