2 * Copyright (C) 2015-2019 "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"
28 #include "afb-token.h"
30 static void init_context(struct afb_context *context, struct afb_session *session, struct afb_token *token)
32 assert(session != NULL);
34 /* reset the context for the session */
35 context->session = session;
37 context->super = NULL;
38 context->api_key = NULL;
39 context->token = afb_token_addref(token);
43 if (afb_token_check(token))
44 context->validated = 1;
46 context->invalidated = 1;
50 void afb_context_init(struct afb_context *context, struct afb_session *session, struct afb_token *token)
52 init_context(context, afb_session_addref(session), token);
55 void afb_context_init_validated(struct afb_context *context, struct afb_session *session)
57 afb_context_init(context, session, NULL);
58 context->validated = 1;
61 void afb_context_subinit(struct afb_context *context, struct afb_context *super)
63 context->session = super->session;
65 context->super = super;
66 context->api_key = NULL;
67 context->token = super->token;
68 context->validated = super->validated;
71 int afb_context_connect(struct afb_context *context, const char *uuid, struct afb_token *token)
74 struct afb_session *session;
76 session = afb_session_get (uuid, AFB_SESSION_TIMEOUT_DEFAULT, &created);
79 init_context(context, session, token);
86 int afb_context_connect_validated(struct afb_context *context, const char *uuid)
88 int rc = afb_context_connect(context, uuid, NULL);
90 context->validated = 1;
94 void afb_context_disconnect(struct afb_context *context)
96 if (context->session && !context->super) {
97 if (context->closing && !context->closed) {
98 afb_context_change_loa(context, 0);
99 afb_context_set(context, NULL, NULL);
102 afb_token_unref(context->token);
103 afb_session_unref(context->session);
104 context->session = NULL;
108 const char *afb_context_uuid(struct afb_context *context)
110 return context->session ? afb_session_uuid(context->session) : "";
113 void *afb_context_make(struct afb_context *context, int replace, void *(*make_value)(void *closure), void (*free_value)(void *item), void *closure)
115 assert(context->session != NULL);
116 return afb_session_cookie(context->session, context->api_key, make_value, free_value, closure, replace);
119 void *afb_context_get(struct afb_context *context)
121 assert(context->session != NULL);
122 return afb_session_get_cookie(context->session, context->api_key);
125 int afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
127 assert(context->session != NULL);
128 return afb_session_set_cookie(context->session, context->api_key, value, free_value);
131 void afb_context_close(struct afb_context *context)
133 context->closing = 1;
136 int afb_context_check(struct afb_context *context)
139 return afb_context_check(context);
140 return context->validated;
143 int afb_context_check_loa(struct afb_context *context, unsigned loa)
145 return afb_context_get_loa(context) >= loa;
148 static inline const void *loa_key(struct afb_context *context)
150 return (const void*)(1+(intptr_t)(context->api_key));
153 static inline void *loa2ptr(unsigned loa)
155 return (void*)(intptr_t)loa;
158 static inline unsigned ptr2loa(void *ptr)
160 return (unsigned)(intptr_t)ptr;
163 int afb_context_change_loa(struct afb_context *context, unsigned loa)
165 if (!context->validated || loa > 7) {
170 return afb_session_set_cookie(context->session, loa_key(context), loa2ptr(loa), NULL);
173 unsigned afb_context_get_loa(struct afb_context *context)
175 assert(context->session != NULL);
176 return ptr2loa(afb_session_get_cookie(context->session, loa_key(context)));