2 * Copyright (C) 2015-2018 "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.
26 #include "afb-session.h"
27 #include "afb-context.h"
29 static void init_context(struct afb_context *context, struct afb_session *session, const char *token)
31 assert(session != NULL);
33 /* reset the context for the session */
34 context->session = session;
36 context->super = NULL;
37 context->api_key = NULL;
41 if (afb_session_check_token(session, token))
42 context->validated = 1;
44 context->invalidated = 1;
48 void afb_context_init(struct afb_context *context, struct afb_session *session, const char *token)
50 init_context(context, afb_session_addref(session), token);
53 void afb_context_subinit(struct afb_context *context, struct afb_context *super)
55 context->session = super->session;
57 context->super = super;
58 context->api_key = NULL;
59 context->validated = super->validated;
62 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
65 struct afb_session *session;
67 session = afb_session_get (uuid, AFB_SESSION_TIMEOUT_DEFAULT, &created);
70 init_context(context, session, token);
73 /* context->refreshing = 1; */
78 void afb_context_disconnect(struct afb_context *context)
80 if (context->session && !context->super) {
81 if (context->refreshing && !context->refreshed) {
82 afb_session_new_token (context->session);
83 context->refreshed = 1;
85 if (context->closing && !context->closed) {
86 afb_context_change_loa(context, 0);
87 afb_context_set(context, NULL, NULL);
90 afb_session_unref(context->session);
91 context->session = NULL;
95 const char *afb_context_sent_token(struct afb_context *context)
97 if (context->session == NULL || context->closing || context->super)
99 if (!context->refreshing)
101 if (!context->refreshed) {
102 afb_session_new_token (context->session);
103 context->refreshed = 1;
105 return afb_session_token(context->session);
108 const char *afb_context_uuid(struct afb_context *context)
110 return context->session ? afb_session_uuid(context->session) : "";
113 const char *afb_context_sent_uuid(struct afb_context *context)
115 if (context->session == NULL || context->closing || context->super)
117 if (!context->created)
119 return afb_session_uuid(context->session);
122 void *afb_context_make(struct afb_context *context, int replace, void *(*make_value)(void *closure), void (*free_value)(void *item), void *closure)
124 assert(context->session != NULL);
125 return afb_session_cookie(context->session, context->api_key, make_value, free_value, closure, replace);
128 void *afb_context_get(struct afb_context *context)
130 assert(context->session != NULL);
131 return afb_session_get_cookie(context->session, context->api_key);
134 int afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
136 assert(context->session != NULL);
137 return afb_session_set_cookie(context->session, context->api_key, value, free_value);
140 void afb_context_close(struct afb_context *context)
142 context->closing = 1;
145 void afb_context_refresh(struct afb_context *context)
148 afb_context_refresh(context->super);
150 assert(context->validated);
151 context->refreshing = 1;
152 if (!context->refreshed) {
153 afb_session_new_token (context->session);
154 context->refreshed = 1;
159 int afb_context_check(struct afb_context *context)
162 return afb_context_check(context);
163 return context->validated;
166 int afb_context_check_loa(struct afb_context *context, unsigned loa)
168 return afb_context_get_loa(context) >= loa;
171 static inline const void *loa_key(struct afb_context *context)
173 return (const void*)(1+(intptr_t)(context->api_key));
176 static inline void *loa2ptr(unsigned loa)
178 return (void*)(intptr_t)loa;
181 static inline unsigned ptr2loa(void *ptr)
183 return (unsigned)(intptr_t)ptr;
186 int afb_context_change_loa(struct afb_context *context, unsigned loa)
188 if (!context->validated || loa > 7) {
193 return afb_session_set_cookie(context->session, loa_key(context), loa2ptr(loa), NULL);
196 unsigned afb_context_get_loa(struct afb_context *context)
198 assert(context->session != NULL);
199 return ptr2loa(afb_session_get_cookie(context->session, loa_key(context)));