add function to get the current LOA
[src/app-framework-binder.git] / src / afb-context.c
1 /*
2  * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
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
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <assert.h>
22 #include <stdlib.h>
23
24 #include "afb-session.h"
25 #include "afb-context.h"
26
27 static void init_context(struct afb_context *context, struct afb_session *session, const char *token)
28 {
29         assert(session != NULL);
30
31         /* reset the context for the session */
32         context->session = session;
33         context->flags = 0;
34         context->super = NULL;
35         context->api_key = NULL;
36         context->loa_in = afb_session_get_LOA(session) & 7;
37
38         /* check the token */
39         if (token != NULL) {
40                 if (afb_session_check_token(session, token))
41                         context->validated = 1;
42                 else
43                         context->invalidated = 1;
44         }
45 }
46
47 void afb_context_init(struct afb_context *context, struct afb_session *session, const char *token)
48 {
49         init_context(context, afb_session_addref(session), token);
50 }
51
52 void afb_context_subinit(struct afb_context *context, struct afb_context *super)
53 {
54         *context = *super;
55         context->super = super;
56 }
57
58 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
59 {
60         int created;
61         struct afb_session *session;
62
63         session = afb_session_get (uuid, &created);
64         if (session == NULL)
65                 return -1;
66         init_context(context, session, token);
67         if (created) {
68                 context->created = 1;
69                 /* context->refreshing = 1; */
70         }
71         return 0;
72 }
73
74 void afb_context_disconnect(struct afb_context *context)
75 {
76         if (context->session && !context->super) {
77                 if (context->refreshing && !context->refreshed) {
78                         afb_session_new_token (context->session);
79                         context->refreshed = 1;
80                 }
81                 if (context->loa_changing && !context->loa_changed) {
82                         afb_session_set_LOA (context->session, context->loa_out);
83                         context->loa_changed = 1;
84                 }
85                 if (context->closing && !context->closed) {
86                         afb_session_close(context->session);
87                         context->closed = 1;
88                 }
89                 afb_session_unref(context->session);
90                 context->session = NULL;
91         }
92 }
93
94 const char *afb_context_sent_token(struct afb_context *context)
95 {
96         if (context->session == NULL || context->closing || context->super)
97                 return NULL;
98         if (!context->refreshing)
99                 return NULL;
100         if (!context->refreshed) {
101                 afb_session_new_token (context->session);
102                 context->refreshed = 1;
103         }
104         return afb_session_token(context->session);
105 }
106
107 const char *afb_context_sent_uuid(struct afb_context *context)
108 {
109         if (context->session == NULL || context->closing || context->super)
110                 return NULL;
111         if (!context->created)
112                 return NULL;
113         return afb_session_uuid(context->session);
114 }
115
116 void *afb_context_get(struct afb_context *context)
117 {
118         assert(context->session != NULL);
119         return afb_session_get_cookie(context->session, context->api_key);
120 }
121
122 void afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
123 {
124         int rc;
125         assert(context->session != NULL);
126         rc = afb_session_set_cookie(context->session, context->api_key, value, free_value);
127         (void)rc; /* TODO */
128 }
129
130 void afb_context_close(struct afb_context *context)
131 {
132         if (context->super)
133                 afb_context_close(context->super);
134         else
135                 context->closing = 1;
136 }
137
138 void afb_context_refresh(struct afb_context *context)
139 {
140         if (context->super)
141                 afb_context_refresh(context->super);
142         else {
143                 assert(context->validated);
144                 context->refreshing = 1;
145         }
146 }
147
148 int afb_context_check(struct afb_context *context)
149 {
150         if (context->super)
151                 return afb_context_check(context);
152         return context->validated;
153 }
154
155 int afb_context_check_loa(struct afb_context *context, unsigned loa)
156 {
157         if (context->super)
158                 return afb_context_check_loa(context->super, loa);
159         return context->loa_in >= loa;
160 }
161
162 int afb_context_change_loa(struct afb_context *context, unsigned loa)
163 {
164         if (context->super)
165                 return afb_context_change_loa(context, loa);
166
167         if (!context->validated || loa > 7)
168                 return 0;
169
170         if (loa == context->loa_in && !context->loa_changed)
171                 context->loa_changing = 0;
172         else {
173                 context->loa_out = loa & 7;
174                 context->loa_changing = 1;
175                 context->loa_changed = 0;
176         }
177         return 1;
178 }
179
180 unsigned afb_context_get_loa(struct afb_context *context)
181 {
182         return context->loa_changing || context->loa_changed ? context->loa_out : context->loa_in;
183 }
184
185