FskErr KprCoAPServerSessionNew(KprCoAPServer server, KprCoAPMessage request, KprCoAPEndpoint endpoint, KprCoAPServerSession *it) { FskErr err = kFskErrNone; KprCoAPServerSession self = NULL; KprCoAPMessageOptionRecord *option; bailIfError(KprMemPtrNewClear(sizeof(KprCoAPServerSessionRecord), &self)); bailIfError(KprRetainableNew(&self->retainable)); self->server = server; self->sessionId = KprCoAPServerNextSessionId(server); self->endpoint = KprRetain(endpoint); self->request = KprRetain(request); self->autoAck = true; self->confiramableRequest = KprCoAPMessageIsConfirmable(request); KprCoAPEndpointGetExpireTime(endpoint, &self->expireAt); option = KprCoAPMessageFindOption(request, kKprCoAPMessageOptionObserve); self->observeRequested = (option != NULL && option->value.uint == kKprCoAPMessageObserveRegister); bailIfError(KprCoAPMessageBuildUri(self->request)); *it = self; bail: if (err) { KprCoAPServerSessionDispose(self); } return err; }
FskErr KprCoAPClientStartRequest(KprCoAPClient self, UInt32 ipaddr, UInt16 port, KprCoAPMessage message) { FskErr err = kFskErrNone; KprCoAPClientRequest request = NULL; KprCoAPEndpoint endpoint = NULL; KprMemoryBlock generatedToken = NULL; if (self->autoToken && message->token == NULL) { bailIfError(KprCoAPClientNextAutoToken(self, &generatedToken)); message->token = KprRetain(generatedToken); KprMemoryBlockDispose(generatedToken); } bailIfError(KprCoAPClientGetEndpoint(self, ipaddr, port, &endpoint)); bailIfError(KprCoAPClientRequestNew(&request, self, message, endpoint)); FskListAppend(&self->requests, request); bailIfError(KprCoAPEndpointSendMessage(endpoint, message)); bail: if (err) { FskListRemove(&self->requests, request); KprCoAPClientRequestDispose(request); KprCoAPClientDisposeUnusedEndpoint(self); } return err; }
FskErr KprCoAPClientRequestNew(KprCoAPClientRequest *it, KprCoAPClient client, KprCoAPMessage message, KprCoAPEndpoint endpoint) { FskErr err = kFskErrNone; KprCoAPClientRequest self = NULL; KprCoAPMessageOptionRecord *option; bailIfError(KprMemPtrNewClear(sizeof(KprCoAPClientRequestRecord), &self)); self->client = client; self->message= KprRetain(message); self->endpoint = KprRetain(endpoint); option = KprCoAPMessageFindOption(message, kKprCoAPMessageOptionObserve); self->observeRequested = (option != NULL && option->value.uint == kKprCoAPMessageObserveRegister); *it = self; FskDebugStr("CoAP ClientRequest created: %x", (int) self); bail: if (err) { KprCoAPClientRequestDispose(self); } return err; }
static FskErr KprCoAPClientNextAutoToken(KprCoAPClient self, KprMemoryBlock *token) { FskErr err = kFskErrNone; UInt32 max, value; if (self->recycleTokens) { *token = FskListRemoveFirst(&self->recycleTokens); (void)KprRetain(*token); return kFskErrNone; } switch (self->nextTokenBytes) { case 1: max = 0xffU; break; case 2: max = 0xffffU; break; case 3: max = 0xffffffU; break; case 4: max = 0xffffffffU; break; default: return kFskErrMemFull; } value = self->nextTokenId; value = FskEndianU32_NtoL(value); bailIfError(KprMemoryBlockNew(self->nextTokenBytes, &value, token)); if (self->nextTokenId >= max) { self->nextTokenBytes += 1; self->nextTokenId = 0; } else { self->nextTokenId += 1; } bail: return err; }
FskErr KprCoAPClientEndRequest(KprCoAPClient self, KprCoAPClientRequest request, const char *reason) { FskErr err = kFskErrNone; if (self->callbacks.requestEndCallback) { bailIfError(self->callbacks.requestEndCallback(request->message, reason, self->refcon)); } FskListRemove(&self->requests, request); if (request->message->token) { FskListAppend(&self->recycleTokens, KprRetain(request->message->token)); } bailIfError(KprCoAPClientRequestDispose(request)); bail: return err; }