2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
25 #include "afb-context.h"
27 static void init_context(struct afb_context *context, struct AFB_clientCtx *session, const char *token)
29 assert(session != NULL);
31 /* reset the context for the session */
32 context->session = session;
34 context->api_index = -1;
35 context->loa_in = ctxClientGetLOA(session) & 7;
39 if (ctxTokenCheck(session, token))
40 context->validated = 1;
42 context->invalidated = 1;
46 void afb_context_init(struct afb_context *context, struct AFB_clientCtx *session, const char *token)
48 init_context(context, ctxClientAddRef(session), token);
51 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
54 struct AFB_clientCtx *session;
56 session = ctxClientGetSession (uuid, &created);
59 init_context(context, session, token);
62 /* context->refreshing = 1; */
67 void afb_context_disconnect(struct afb_context *context)
69 if (context->session != NULL) {
70 if (context->refreshing && !context->refreshed) {
71 ctxTokenNew (context->session);
72 context->refreshed = 1;
74 if (context->loa_changing && !context->loa_changed) {
75 ctxClientSetLOA (context->session, context->loa_out);
76 context->loa_changed = 1;
78 if (context->closing && !context->closed) {
79 ctxClientClose(context->session);
82 ctxClientUnref(context->session);
83 context->session = NULL;
87 const char *afb_context_sent_token(struct afb_context *context)
89 if (context->session == NULL || context->closing)
91 if (!context->refreshing)
93 if (!context->refreshed) {
94 ctxTokenNew (context->session);
95 context->refreshed = 1;
97 return ctxClientGetToken(context->session);
100 const char *afb_context_sent_uuid(struct afb_context *context)
102 if (context->session == NULL || context->closing)
104 if (!context->created)
106 return ctxClientGetUuid(context->session);
109 void *afb_context_get(struct afb_context *context)
111 assert(context->session != NULL);
112 return ctxClientValueGet(context->session, context->api_index);
115 void afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
117 assert(context->session != NULL);
118 return ctxClientValueSet(context->session, context->api_index, value, free_value);
121 void afb_context_close(struct afb_context *context)
123 context->closing = 1;
126 void afb_context_refresh(struct afb_context *context)
128 assert(context->validated);
129 context->refreshing = 1;
132 int afb_context_check(struct afb_context *context)
134 return context->validated;
137 int afb_context_check_loa(struct afb_context *context, unsigned loa)
139 return context->loa_in >= loa;
142 int afb_context_change_loa(struct afb_context *context, unsigned loa)
144 if (!context->validated || loa > 7)
147 if (loa == context->loa_in && !context->loa_changed)
148 context->loa_changing = 0;
150 context->loa_out = loa & 7;
151 context->loa_changing = 1;
152 context->loa_changed = 0;