2 * Copyright (C) 2015, 2016, 2017 "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.
24 #include "afb-session.h"
25 #include "afb-context.h"
27 static void init_context(struct afb_context *context, struct afb_session *session, const char *token)
29 assert(session != NULL);
31 /* reset the context for the session */
32 context->session = session;
34 context->api_key = NULL;
35 context->loa_in = afb_session_get_LOA(session) & 7;
39 if (afb_session_check_token(session, token))
40 context->validated = 1;
42 context->invalidated = 1;
46 void afb_context_init(struct afb_context *context, struct afb_session *session, const char *token)
48 init_context(context, afb_session_addref(session), token);
51 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
54 struct afb_session *session;
56 session = afb_session_get (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 afb_session_new_token (context->session);
72 context->refreshed = 1;
74 if (context->loa_changing && !context->loa_changed) {
75 afb_session_set_LOA (context->session, context->loa_out);
76 context->loa_changed = 1;
78 if (context->closing && !context->closed) {
79 afb_session_close(context->session);
82 afb_session_unref(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 afb_session_new_token (context->session);
95 context->refreshed = 1;
97 return afb_session_token(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 afb_session_uuid(context->session);
109 void *afb_context_get(struct afb_context *context)
111 assert(context->session != NULL);
112 return afb_session_get_cookie(context->session, context->api_key);
115 void afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
118 assert(context->session != NULL);
119 rc = afb_session_set_cookie(context->session, context->api_key, value, free_value);
123 void afb_context_close(struct afb_context *context)
125 context->closing = 1;
128 void afb_context_refresh(struct afb_context *context)
130 assert(context->validated);
131 context->refreshing = 1;
134 int afb_context_check(struct afb_context *context)
136 return context->validated;
139 int afb_context_check_loa(struct afb_context *context, unsigned loa)
141 return context->loa_in >= loa;
144 int afb_context_change_loa(struct afb_context *context, unsigned loa)
146 if (!context->validated || loa > 7)
149 if (loa == context->loa_in && !context->loa_changed)
150 context->loa_changing = 0;
152 context->loa_out = loa & 7;
153 context->loa_changing = 1;
154 context->loa_changed = 0;