/* pull the given mode information into the given xbee instance */ xbee_err xbee_modeImport(struct xbee_modeConType **retConTypes, const struct xbee_mode *mode) { int i, n; struct xbee_modeConType *conTypes; if (!retConTypes || !mode) return XBEE_EMISSINGPARAM; if (*retConTypes) return XBEE_EINVAL; for (n = 0; mode->conTypes && mode->conTypes[n] && mode->conTypes[n]->name; n++); if ((conTypes = malloc(sizeof(*conTypes) * (n + 1))) == NULL) return XBEE_ENOMEM; memset(&conTypes[n], 0, sizeof(*conTypes)); for (i = 0; i < n; i++) { /* keep the pointers (they are const after all) */ memcpy(&conTypes[i], mode->conTypes[i], sizeof(*conTypes)); /* setup the addressCmp function */ if (conTypes[i].addressCmp == NULL) conTypes[i].addressCmp = xbee_conAddressCmpDefault; /* initialization added for microsoft compiler support */ if (conTypes[i].init) conTypes[i].init(&(conTypes[i])); conTypes[i].conList = xbee_ll_alloc(); } *retConTypes = conTypes; return XBEE_ENONE; }
static xbee_err init(struct xbee *xbee, va_list ap) { xbee_err ret; char *t; struct xbee_modeData *data; if (!xbee) return XBEE_EMISSINGPARAM; if ((data = malloc(sizeof(*data))) == NULL) return XBEE_ENOMEM; memset(data, 0, sizeof(*data)); xbee->modeData = data; data->conList = xbee_ll_alloc(); ret = XBEE_ENONE; /* get the hostname */ t = va_arg(ap, char*); if ((data->netInfo.host = malloc(strlen(t) + 1)) == NULL) { ret = XBEE_ENOMEM; goto die; } strcpy(data->netInfo.host, t); /* get the port number */ data->netInfo.port = va_arg(ap, int); /* setup the network interface */ if ((ret = xbee_netSetup(&data->netInfo)) != XBEE_ENONE) goto die; return XBEE_ENONE; die: mode_shutdown(xbee); return ret; }
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; }
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; }
xbee_err xbee_modeAddConType(struct xbee_modeConType **extConTypes, const struct xbee_modeConType *newConType) { int n; struct xbee_modeConType *conTypes; if (!extConTypes || !newConType) return XBEE_EMISSINGPARAM; if (!*extConTypes) return XBEE_EINVAL; if (!newConType->name) return XBEE_EINVAL; if (!newConType->rxHandler && !newConType->txHandler) return XBEE_EINVAL; for (n = 0; (*extConTypes)[n].name; n++); if ((conTypes = realloc(*extConTypes, sizeof(*conTypes) * (n + 2))) == NULL) return XBEE_ENOMEM; *extConTypes = conTypes; prepare_repopConTypes(conTypes); memset(&conTypes[n + 1], 0, sizeof(*conTypes)); memcpy(&conTypes[n], newConType, sizeof(*newConType)); conTypes[n].conList = xbee_ll_alloc(); return XBEE_ENONE; }
xbee_err xbee_rxAlloc(struct xbee_rxInfo **nInfo) { static char logColor = 1; size_t memSize; struct xbee_rxInfo *info; if (!nInfo) return XBEE_EMISSINGPARAM; memSize = sizeof(*info); if (!(info = malloc(memSize))) return XBEE_ENOMEM; memset(info, 0, memSize); info->bufList = xbee_ll_alloc(); xsys_sem_init(&info->sem); /* give it a log color */ info->logColor = logColor; if (logColor++ > 7) logColor = 7; *nInfo = info; return XBEE_ENONE; }
EXPORT INIT void xbee_init(void) { xsys_thread_key_init(&threadInfoKey, NULL); if (!xbeeList && (xbeeList = xbee_ll_alloc()) == NULL) { fprintf(stderr, "libxbee: failed to initialize xbeeList...\n"); } if (!conList && (conList = xbee_ll_alloc()) == NULL) { fprintf(stderr, "libxbee: failed to initialize conList...\n"); } if (!pktList && (pktList = xbee_ll_alloc()) == NULL) { fprintf(stderr, "libxbee: failed to initialize pktList...\n"); } if (!netDeadClientList && (netDeadClientList = xbee_ll_alloc()) == NULL) { fprintf(stderr, "libxbee: failed to initialize netDeadClientList...\n"); } if (!threadList && (threadList = xbee_ll_alloc()) == NULL) { fprintf(stderr, "libxbee: failed to initialize threadList...\n"); } if (!needsFree && (needsFree = xbee_ll_alloc()) == NULL) { fprintf(stderr, "libxbee: failed to initialize needsFree...\n"); } }