xbee_err xbee_ll_init(struct xbee_ll_head *list) { if (!list) return XBEE_EMISSINGPARAM; list->is_head = 1; list->head = NULL; list->tail = NULL; list->self = list; if (xsys_mutex_init(&list->mutex)) return XBEE_EMUTEX; return 0; }
xbee_err xbee_logAlloc(struct xbee_log **nLog, int defLevel, FILE *defFile) { size_t memSize; struct xbee_log *log; if (!nLog) return XBEE_EMISSINGPARAM; memSize = sizeof(*log); if (!(log = malloc(memSize))) return XBEE_ENOMEM; memset(log, 0, memSize); xsys_mutex_init(&log->mutex); log->logLevel = defLevel; log->f = defFile; *nLog = log; return XBEE_ENONE; }
xbee_err xbee_frameBlockAlloc(struct xbee_frameBlock **nfBlock) { size_t memSize; struct xbee_frameBlock *fBlock; int i; if (!nfBlock) return XBEE_EMISSINGPARAM; memSize = sizeof(*fBlock); if (!(fBlock = malloc(memSize))) return XBEE_ENOMEM; memset(fBlock, 0, memSize); xsys_mutex_init(&fBlock->mutex); fBlock->numFrames = sizeof(fBlock->frame) / sizeof(fBlock->frame[0]); for (i = 0; i < fBlock->numFrames; i++) { fBlock->frame[i].id = i; xsys_sem_init(&fBlock->frame[i].sem); } *nfBlock = fBlock; return XBEE_ENONE; }
/* create a new connection based on the address information provided if a connection already exists with a matching address, *retCon points to it, and XBEE_EEXISTS is returned the userData provided is assigned to the connection immediately */ EXPORT int xbee_conNew(struct xbee *xbee, struct xbee_con **retCon, unsigned char id, struct xbee_conAddress *address, void *userData) { int ret; struct xbee_con *con; struct xbee_conType *conType; /* check parameters */ if (!xbee) { if (!xbee_default) return XBEE_ENOXBEE; xbee = xbee_default; } if (!xbee_validate(xbee)) return XBEE_ENOXBEE; if (!xbee->mode) return XBEE_ENOMODE; if (!retCon) return XBEE_EMISSINGPARAM; if (!address) return XBEE_EMISSINGPARAM; /* get the conType that is being requested */ if (id >= xbee->mode->conTypeCount) return XBEE_EINVAL; conType = &(xbee->mode->conTypes[id]); /* check the addressing information */ switch (conType->needsAddress) { /* don't care */ case 0: break; /* need either */ case 1: if (!address->addr16_enabled && !address->addr64_enabled) return XBEE_EINVAL; break; /* need 16-bit */ case 2: if (!address->addr16_enabled) return XBEE_EINVAL; break; /* need 64-bit */ case 3: if (!address->addr64_enabled) return XBEE_EINVAL; break; /* need both */ case 4: if (!address->addr16_enabled || !address->addr64_enabled) return XBEE_EINVAL; break; /* not supported */ default: xbee_log(1,"addressing mode %d is not supported", conType->needsAddress); return XBEE_EINVAL; } /* retrieve a connection if one aready exists, ignoring sleeping connections */ if ((con = xbee_conFromAddress(xbee, conType, address)) != NULL && !con->sleeping) { *retCon = con; ret = XBEE_EEXISTS; goto done; } ret = XBEE_ENONE; /* allocate the memory */ if ((con = calloc(1, sizeof(struct xbee_con))) == NULL) { ret = XBEE_ENOMEM; goto die1; } /* setup the connection info */ con->conType = conType; memcpy(&con->address, address, sizeof(struct xbee_conAddress)); con->userData = userData; ll_init(&con->rxList); xsys_sem_init(&con->callbackSem); xsys_mutex_init(&con->txMutex); /* this mapping is implemented as an extension, therefore it is entirely optional! */ if (xbee->f->conNew) { int ret; if ((ret = xbee->f->conNew(xbee, retCon, id, address, userData)) != 0) { /* ret should be either 0 / XBEE_ESTALE */; return ret; } } /* once everything has been done, add it to the list (enable it) */ ll_add_tail(&(con->conType->conList), con); *retCon = con; /* log the details */ xbee_log(2,"Created new '%s' connection @ %p", conType->name, con); xbee_conLogAddress(xbee, address); goto done; die1: done: return ret; }