예제 #1
0
파일: xbee.c 프로젝트: RengaPD/libxbee3
xbee_err xbee_alloc(struct xbee **nXbee) {
	size_t memSize;
	struct xbee *xbee;
	xbee_err ret;
	
	if (!nXbee) return XBEE_EMISSINGPARAM;

	memSize = sizeof(*xbee);

	if (!(xbee = malloc(memSize))) return XBEE_ENOMEM;
	
	memset(xbee, 0, memSize);
	if ((ret = xbee_frameBlockAlloc(&xbee->fBlock)) != XBEE_ENONE)         goto die1;
#ifndef XBEE_DISABLE_LOGGING
	if ((ret = xbee_logAlloc(&xbee->log)) != XBEE_ENONE)                   goto die1;
#endif /* !XBEE_DISABLE_LOGGING */
	if ((ret = xbee_txAlloc(&xbee->iface.tx)) != XBEE_ENONE)               goto die1;
	if ((ret = xbee_rxAlloc(&xbee->iface.rx)) != XBEE_ENONE)               goto die1;
	
	if ((ret = xbee_ll_add_tail(xbeeList, xbee)) != XBEE_ENONE)            goto die1;
	
	*nXbee = xbee;
	
	return XBEE_ENONE;
	
die1:
	xbee_free(xbee);
	return ret;
}
예제 #2
0
파일: pkt.c 프로젝트: C24IO/libxbee3
xbee_err xbee_pktAlloc(struct xbee_pkt **nPkt, struct xbee_pkt *oPkt, int dataLen) {
	size_t memSize;
	struct xbee_pkt *pkt;
	xbee_err ret;
	
	if (!nPkt) return XBEE_EMISSINGPARAM;
	
	if (oPkt) {
		if ((ret = xbee_ll_ext_item(pktList, oPkt)) != XBEE_ENONE) {
			return ret;
		}
	}
	
	memSize = sizeof(*pkt);
	memSize += sizeof(char) * dataLen;
	
	if (!(pkt = realloc(oPkt, memSize))) return XBEE_ENOMEM;
	
	if (!oPkt) {
		memset(pkt, 0, memSize);
		pkt->dataItems = xbee_ll_alloc();
	}
	
	if ((ret = xbee_ll_add_tail(pktList, pkt)) != XBEE_ENONE) {
		_xbee_pktFree(pkt);
		ret = XBEE_ELINKEDLIST;
	} else {
		*nPkt = pkt;
	}
	
	return ret;
}
예제 #3
0
xbee_err xbee_rx(struct xbee *xbee, int *restart, void *arg) {
	xbee_err ret;
	struct xbee_rxInfo *info;
	struct xbee_tbuf *buf;
	
	info = arg;
	if (!info->bufList || !info->ioFunc) {
		*restart = 0;
		return XBEE_EINVAL;
	}
	
	while (!xbee->die) {
		buf = NULL;
		if ((ret = info->ioFunc(xbee, info->ioArg, &buf)) != XBEE_ENONE) {
			if (ret == XBEE_EEOF) {
				*restart = 0;
				if (info->eofCallback) info->eofCallback(xbee, info);
				return XBEE_EEOF;
			} else if (ret == XBEE_ESHUTDOWN && xbee->die) {
				break;
			}
			xbee_log(1, "rx() returned %d (%s)... retrying in 10 ms", ret, xbee_errorToStr(ret));
			usleep(10000); /* 10 ms */
			continue;
		}

#ifndef XBEE_DISABLE_LOGGING
#ifndef XBEE_LOG_NO_RX
		if (xbee->log->enable_rx) {
			/* format: tx[0x0000000000000000] */
			char label[42]; /* enough space for a 64-bit pointer and ANSI color codes */
#ifndef XBEE_LOG_NO_COLOR
			if (xbee->log->use_color) {
				snprintf(label, sizeof(label), "Rx[%c[%dm%p%c[0m]", 27, 30 + info->logColor, info,  27);
			} else {
#endif /* !XBEE_LOG_NO_COLOR */
				snprintf(label, sizeof(label), "Rx[%p]", info);
#ifndef XBEE_LOG_NO_COLOR
			}
#endif /* !XBEE_LOG_NO_COLOR */
			xbee_logData(25, label, buf->data, buf->len);
		}
#endif /* !XBEE_LOG_NO_RX */
#endif /* !XBEE_DISABLE_LOGGING */
		
		if (xbee_ll_add_tail(info->bufList, buf) != XBEE_ENONE) return XBEE_ELINKEDLIST;
		buf = NULL;
		if (xsys_sem_post(&info->sem) != 0) return XBEE_ESEMAPHORE;
	}
	
	return XBEE_ESHUTDOWN;
}
예제 #4
0
파일: pkt.c 프로젝트: C24IO/libxbee3
xbee_err xbee_pktLink(struct xbee_con *con, struct xbee_pkt *pkt) {
	xbee_err ret;
	if (!con || !pkt) return XBEE_EMISSINGPARAM;
#ifndef XBEE_DISABLE_STRICT_OBJECTS
	if (xbee_conValidate(con) != XBEE_ENONE) return XBEE_EINVAL;
	if (xbee_pktValidate(pkt) != XBEE_ENONE) return XBEE_EINVAL;
#endif /* XBEE_DISABLE_STRICT_OBJECTS */
	if (xbee_ll_get_item(con->pktList, pkt) == XBEE_ENONE) return XBEE_EEXISTS;
	if ((ret = xbee_ll_add_tail(con->pktList, pkt)) == XBEE_ENONE) {
		pkt->xbee = con->xbee;
		pkt->con = con;
	}
	return ret;
}
예제 #5
0
파일: pkt.c 프로젝트: C24IO/libxbee3
xbee_err xbee_pktDataAdd(struct xbee_pkt *pkt, const char *key, int id, void *data, void (*freeCallback)(void*)) {
	struct pkt_dataKey *k;
	xbee_err ret;
	
	if (!pkt || !key || !data) return XBEE_EMISSINGPARAM;
#ifndef XBEE_DISABLE_STRICT_OBJECTS
	if (xbee_pktValidate(pkt) != XBEE_ENONE) return XBEE_EINVAL;
#endif /* XBEE_DISABLE_STRICT_OBJECTS */
	
	if ((ret = xbee_pktDataKeyAdd(pkt, key, id, &k, freeCallback)) != XBEE_ENONE && ret != XBEE_EEXISTS) {
		return XBEE_EFAILED;
	}
	
	if (xbee_ll_add_tail(k->items, data)) {
		return XBEE_ELINKEDLIST;
	}
	
	return XBEE_ENONE;
}
예제 #6
0
파일: pkt.c 프로젝트: C24IO/libxbee3
xbee_err xbee_pktDataKeyAdd(struct xbee_pkt *pkt, const char *key, int id, struct pkt_dataKey **retKey, void (*freeCallback)(void*)) {
	struct pkt_dataKey *k;
	xbee_err ret;
	
	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 */
	
	if (xbee_pktDataKeyGet(pkt, key, id, &k) == XBEE_ENONE) {
		if (retKey) *retKey = k;
		return XBEE_EEXISTS;
	}
	
	if ((k = calloc(1, sizeof(*k))) == NULL) {
		return XBEE_ENOMEM;
	}
	
	ret = XBEE_ENONE;
	snprintf(k->name, PKT_DATAKEY_MAXLEN, "%s", key);
	k->id = id;
	k->freeCallback = freeCallback;
	if ((k->items = xbee_ll_alloc()) == NULL) {
		ret = XBEE_ENOMEM;
		goto die1;
	}
	
	if (xbee_ll_add_tail(pkt->dataItems, k) != XBEE_ENONE) {
		ret = XBEE_ELINKEDLIST;
		goto die2;
	}
	
	if (retKey) *retKey = k;
	
	goto done;
die2:
	xbee_ll_free(k->items, NULL);
die1:
	free(k);
done:
	return ret;
}
예제 #7
0
파일: xbee.c 프로젝트: RengaPD/libxbee3
EXPORT xbee_err xbee_vsetup(struct xbee **retXbee, const char *mode, va_list ap) {
	xbee_err ret;
	const struct xbee_mode *xbeeMode;
	struct xbee *xbee;
	
	if (!retXbee || !mode) return XBEE_EMISSINGPARAM;
	
	if ((ret = xbee_modeRetrieve(mode, &xbeeMode)) != XBEE_ENONE) return ret;
	
	if ((ret = xbee_alloc(&xbee)) != XBEE_ENONE) return ret;
	
	if ((ret = xbee_modeImport(&xbee->iface.conTypes, xbeeMode)) != XBEE_ENONE) goto die;
	xbee->mode = xbeeMode;
	
	xbee->iface.rx->ioFunc = xbee->mode->rx_io;
	xbee->iface.rx->fBlock = xbee->fBlock;
	xbee->iface.rx->conTypes = &xbee->iface.conTypes;
	
	xbee->iface.tx->ioFunc = xbee->mode->tx_io;
	
	if ((ret = xbee->mode->init(xbee, ap)) != XBEE_ENONE) goto die;
	
	if ((ret = xbee_threadStart(xbee, NULL, 150000, 0, xbee_rx, xbee->iface.rx)) != XBEE_ENONE)                                goto die;
	if ((ret = xbee_threadStart(xbee, NULL, 150000, 0, xbee_rxHandler, xbee->iface.rx)) != XBEE_ENONE)                         goto die;
	if ((ret = xbee_threadStart(xbee, NULL, 150000, 0, xbee_tx, xbee->iface.tx)) != XBEE_ENONE)                                goto die;
	
	if (xbee->mode->prepare) if ((ret = xbee->mode->prepare(xbee)) != XBEE_ENONE)                                              goto die;
	
	if (xbee->mode->thread) if ((ret = xbee_threadStart(xbee, NULL, 150000, 0, xbee->mode->thread, NULL)) != XBEE_ENONE)       goto die;
	
	xbee_ll_add_tail(xbeeList, xbee);
	
	*retXbee = xbee;
	
	return XBEE_ENONE;

die:
	xbee_free(xbee);
	return ret;
}
예제 #8
0
파일: ll.c 프로젝트: attie/libxbee3
/* NULL ref will add to head */
xbee_err _xbee_ll_add_before(void *list, void *ref, void *item, int needMutex) {
	struct xbee_ll_head *h;
	struct xbee_ll_info *i, *t;
	xbee_err ret;
	ret = XBEE_ENONE;
	if (!list) return XBEE_EMISSINGPARAM;
	i = list;
	h = i->head;
	if (!(h && h->is_head && h->self == h)) return XBEE_EINVAL;
	if (!ref) return xbee_ll_add_tail(h, item);
	if (needMutex) xbee_mutex_lock(&h->mutex);
	i = h->head;
	while (i) {
		if (i->item == ref) break;
		i = i->next;
	}
	if (!i) {
		ret = XBEE_ENOTEXISTS;
		goto out;
	}
	if (!(t = calloc(1, sizeof(struct xbee_ll_info)))) {
		ret = XBEE_ENOMEM;
		goto out;
	}
	t->head = i->head;
	if (!i->prev) {
		h->head = t;
		t->prev = NULL;
	} else {
		i->prev->next = t;
		t->prev = i->prev;
	}
	i->prev = t;
	t->next = i;
	t->item = item;
out:
	if (needMutex) xbee_mutex_unlock(&h->mutex);
	return ret;
}
예제 #9
0
파일: net_io.c 프로젝트: attie/libxbee3
xbee_err xbee_netRx(struct xbee *xbee, void *arg, struct xbee_tbuf **buf) {
    char c;
    char length[2];
    int pos, len, ret;
    struct xbee_tbuf *iBuf;
    int fd;

    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;
    } else {
        /* this is on the client end */
        struct xbee_modeData *info;
        info = xbee->modeData;
        fd = info->netInfo.fd;
    }

    while (1) {
        do {
            if ((ret = recv(fd, &c, 1, MSG_NOSIGNAL)) < 0) return XBEE_EIO;
            if (ret == 0) goto eof;
        } while (c != 0x7E);

        for (len = 2, pos = 0; pos < len; pos += ret) {
            ret = recv(fd, &(length[pos]), len - pos, MSG_NOSIGNAL);
            if (ret > 0) continue;
            if (ret == 0) goto eof;
            return XBEE_EIO;
        }

        len = (((length[0] << 8) & 0xFF00) | (length[1] & 0xFF)) + 1;
        if ((iBuf = malloc(sizeof(*iBuf) + len)) == NULL) return XBEE_ENOMEM;
        xbee_ll_add_tail(needsFree, iBuf);

        iBuf->len = len;

        memset(&iBuf->ts, 0, sizeof(iBuf->ts));

        for (pos = 0; pos < iBuf->len; pos += ret) {
            ret = recv(fd, &(iBuf->data[pos]), iBuf->len - pos, MSG_NOSIGNAL);
            if (ret > 0) continue;
            xbee_ll_ext_item(needsFree, iBuf);
            free(iBuf);
            if (ret == 0) goto eof;
            return XBEE_EIO;
        }
        break;
    }

    /* needs free is handled for us by xbee_rxHandler(), we just need to register it */
    *buf = iBuf;

    return XBEE_ENONE;
eof:
    if (arg) {
        struct xbee_netClientInfo *info;
#ifndef XBEE_NO_NET_SERVER
        struct xbee_netClientInfo *deadClient;
#endif
        struct xbee_con *con;
        info = arg;

#ifndef XBEE_NO_NET_SERVER
        /* tidy up any dead clients - not including us */
        while (xbee_ll_ext_head(netDeadClientList, (void**)&deadClient) == XBEE_ENONE && deadClient != NULL) {
            xbee_netClientShutdown(deadClient);
        }

        /* xbee_netRx() is responsible for free()ing memory and killing off client threads on the server
           to do this, we need to add ourselves to the netDeadClientList, and remove ourselves from the clientList
           the server thread will then cleanup any clients on the next accept() */
        xbee_ll_add_tail(netDeadClientList, arg);
        xbee_ll_ext_item(xbee->netInfo->clientList, arg);
#endif /* XBEE_NO_NET_SERVER */

        /* kill the other threads */
        /* excluding the rx thread... thats us! */
        if (info->rxHandlerThread) {
            xbee_threadKillJoin(info->xbee, info->rxHandlerThread, NULL);
            info->rxHandlerThread = NULL;
        }
        if (info->txThread) {
            xbee_threadKillJoin(info->xbee, info->txThread, NULL);
            info->txThread = NULL;
        }

        /* close up the socket */
        shutdown(info->fd, SHUT_RDWR);
        xsys_close(info->fd);
        info->fd = -1; /* <-- mark it closed */

        /* end all of our connections */
        for (con = NULL; xbee_ll_ext_head(info->conList, (void **)&con) == XBEE_ENONE && con; ) {
            xbee_conEnd(con);
        }

        /* this leaves us with a call to xbee_threadKillJoin() and xbee_netClientFree() left! */
    }
    return XBEE_EEOF;
}