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.
25 #include "afb-session.h"
26 #include "afb-context.h"
28 static void init_context(struct afb_context *context, struct afb_session *session, const char *token)
30 assert(session != NULL);
32 /* reset the context for the session */
33 context->session = session;
35 context->super = NULL;
36 context->api_key = NULL;
40 if (afb_session_check_token(session, token))
41 context->validated = 1;
43 context->invalidated = 1;
47 void afb_context_init(struct afb_context *context, struct afb_session *session, const char *token)
49 init_context(context, afb_session_addref(session), token);
52 void afb_context_subinit(struct afb_context *context, struct afb_context *super)
54 context->session = super->session;
56 context->super = super;
57 context->api_key = NULL;
58 context->validated = super->validated;
61 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
64 struct afb_session *session;
66 session = afb_session_get (uuid, AFB_SESSION_TIMEOUT_DEFAULT, &created);
69 init_context(context, session, token);
72 /* context->refreshing = 1; */
77 void afb_context_disconnect(struct afb_context *context)
79 if (context->session && !context->super) {
80 if (context->refreshing && !context->refreshed) {
81 afb_session_new_token (context->session);
82 context->refreshed = 1;
84 if (context->closing && !context->closed) {
85 afb_context_change_loa(context, 0);
86 afb_context_set(context, NULL, NULL);
89 afb_session_unref(context->session);
90 context->session = NULL;
94 const char *afb_context_sent_token(struct afb_context *context)
96 if (context->session == NULL || context->closing || context->super)
98 if (!context->refreshing)
100 if (!context->refreshed) {
101 afb_session_new_token (context->session);
102 context->refreshed = 1;
104 return afb_session_token(context->session);
107 const char *afb_context_uuid(struct afb_context *context)
109 return context->session ? afb_session_uuid(context->session) : "";
112 const char *afb_context_sent_uuid(struct afb_context *context)
114 if (context->session == NULL || context->closing || context->super)
116 if (!context->created)
118 return afb_session_uuid(context->session);
121 void *afb_context_make(struct afb_context *context, int replace, void *(*make_value)(void *closure), void (*free_value)(void *item), void *closure)
123 assert(context->session != NULL);
124 return afb_session_cookie(context->session, context->api_key, make_value, free_value, closure, replace);
127 void *afb_context_get(struct afb_context *context)
129 assert(context->session != NULL);
130 return afb_session_get_cookie(context->session, context->api_key);
133 int afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
135 assert(context->session != NULL);
136 return afb_session_set_cookie(context->session, context->api_key, value, free_value);
139 void afb_context_close(struct afb_context *context)
141 context->closing = 1;
144 void afb_context_refresh(struct afb_context *context)
147 afb_context_refresh(context->super);
149 assert(context->validated);
150 context->refreshing = 1;
151 if (!context->refreshed) {
152 afb_session_new_token (context->session);
153 context->refreshed = 1;
158 int afb_context_check(struct afb_context *context)
161 return afb_context_check(context);
162 return context->validated;
165 int afb_context_check_loa(struct afb_context *context, unsigned loa)
167 return afb_context_get_loa(context) >= loa;
170 static inline const void *loa_key(struct afb_context *context)
172 return (const void*)(1+(intptr_t)(context->api_key));
175 static inline void *loa2ptr(unsigned loa)
177 return (void*)(intptr_t)loa;
180 static inline unsigned ptr2loa(void *ptr)
182 return (unsigned)(intptr_t)ptr;
185 int afb_context_change_loa(struct afb_context *context, unsigned loa)
187 if (!context->validated || loa > 7)
190 return 0 <= afb_session_set_cookie(context->session, loa_key(context), loa2ptr(loa), NULL);
193 unsigned afb_context_get_loa(struct afb_context *context)
195 assert(context->session != NULL);
196 return ptr2loa(afb_session_get_cookie(context->session, loa_key(context)));