don't enforce to refresh the token
[src/app-framework-binder.git] / src / afb-context.c
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <assert.h>
21 #include <stdlib.h>
22
23 #include "session.h"
24 #include "afb-context.h"
25
26 static void init_context(struct afb_context *context, struct AFB_clientCtx *session, const char *token)
27 {
28         assert(session != NULL);
29
30         /* reset the context for the session */
31         context->session = session;
32         context->flags = 0;
33         context->api_index = -1;
34         context->loa_in = ctxClientGetLOA(session) & 7;
35
36         /* check the token */
37         if (token != NULL) {
38                 if (ctxTokenCheck(session, token))
39                         context->validated = 1;
40                 else
41                         context->invalidated = 1;
42         }
43 }
44
45 void afb_context_init(struct afb_context *context, struct AFB_clientCtx *session, const char *token)
46 {
47         init_context(context, ctxClientAddRef(session), token);
48 }
49
50 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
51 {
52         int created;
53         struct AFB_clientCtx *session;
54
55         session = ctxClientGetSession (uuid, &created);
56         if (session == NULL)
57                 return -1;
58         init_context(context, session, token);
59         if (created) {
60                 context->created = 1;
61                 /* context->refreshing = 1; */
62         }
63         return 0;
64 }
65
66 void afb_context_disconnect(struct afb_context *context)
67 {
68         if (context->session != NULL) {
69                 if (context->refreshing && !context->refreshed) {
70                         ctxTokenNew (context->session);
71                         context->refreshed = 1;
72                 }
73                 if (context->loa_changing && !context->loa_changed) {
74                         ctxClientSetLOA (context->session, context->loa_out);
75                         context->loa_changed = 1;
76                 }
77                 if (context->closing && !context->closed) {
78                         ctxClientClose(context->session);
79                         context->closed = 1;
80                 }
81                 ctxClientUnref(context->session);
82                 context->session = NULL;
83         }
84 }
85
86 const char *afb_context_sent_token(struct afb_context *context)
87 {
88         if (context->session == NULL || context->closing)
89                 return NULL;
90         if (!context->refreshing)
91                 return NULL;
92         if (!context->refreshed) {
93                 ctxTokenNew (context->session);
94                 context->refreshed = 1;
95         }
96         return ctxClientGetToken(context->session);
97 }
98
99 const char *afb_context_sent_uuid(struct afb_context *context)
100 {
101         if (context->session == NULL || context->closing)
102                 return NULL;
103         if (!context->created)
104                 return NULL;
105         return ctxClientGetUuid(context->session);
106 }
107
108 void *afb_context_get(struct afb_context *context)
109 {
110         assert(context->session != NULL);
111         return ctxClientValueGet(context->session, context->api_index);
112 }
113
114 void afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
115 {
116         assert(context->session != NULL);
117         return ctxClientValueSet(context->session, context->api_index, value, free_value);
118 }
119
120 void afb_context_close(struct afb_context *context)
121 {
122         context->closing = 1;
123 }
124
125 void afb_context_refresh(struct afb_context *context)
126 {
127         assert(context->validated);
128         context->refreshing = 1;
129 }
130
131 int afb_context_check(struct afb_context *context)
132 {
133         return context->validated;
134 }
135
136 int afb_context_check_loa(struct afb_context *context, unsigned loa)
137 {
138         return context->loa_in >= loa;
139 }
140
141 int afb_context_change_loa(struct afb_context *context, unsigned loa)
142 {
143         if (!context->validated || loa > 7)
144                 return 0;
145
146         if (loa == context->loa_in && !context->loa_changed)
147                 context->loa_changing = 0;
148         else {
149                 context->loa_out = loa & 7;
150                 context->loa_changing = 1;
151                 context->loa_changed = 0;
152         }
153         return 1;
154 }
155
156