Improves naming of session's module
[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->api_index = -1;
35         context->loa_in = afb_session_get_LOA(session) & 7;
36
37         /* check the token */
38         if (token != NULL) {
39                 if (afb_session_check_token(session, token))
40                         context->validated = 1;
41                 else
42                         context->invalidated = 1;
43         }
44 }
45
46 void afb_context_init(struct afb_context *context, struct afb_session *session, const char *token)
47 {
48         init_context(context, afb_session_addref(session), token);
49 }
50
51 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
52 {
53         int created;
54         struct afb_session *session;
55
56         session = afb_session_get (uuid, &created);
57         if (session == NULL)
58                 return -1;
59         init_context(context, session, token);
60         if (created) {
61                 context->created = 1;
62                 /* context->refreshing = 1; */
63         }
64         return 0;
65 }
66
67 void afb_context_disconnect(struct afb_context *context)
68 {
69         if (context->session != NULL) {
70                 if (context->refreshing && !context->refreshed) {
71                         afb_session_new_token (context->session);
72                         context->refreshed = 1;
73                 }
74                 if (context->loa_changing && !context->loa_changed) {
75                         afb_session_set_LOA (context->session, context->loa_out);
76                         context->loa_changed = 1;
77                 }
78                 if (context->closing && !context->closed) {
79                         afb_session_close(context->session);
80                         context->closed = 1;
81                 }
82                 afb_session_unref(context->session);
83                 context->session = NULL;
84         }
85 }
86
87 const char *afb_context_sent_token(struct afb_context *context)
88 {
89         if (context->session == NULL || context->closing)
90                 return NULL;
91         if (!context->refreshing)
92                 return NULL;
93         if (!context->refreshed) {
94                 afb_session_new_token (context->session);
95                 context->refreshed = 1;
96         }
97         return afb_session_token(context->session);
98 }
99
100 const char *afb_context_sent_uuid(struct afb_context *context)
101 {
102         if (context->session == NULL || context->closing)
103                 return NULL;
104         if (!context->created)
105                 return NULL;
106         return afb_session_uuid(context->session);
107 }
108
109 void *afb_context_get(struct afb_context *context)
110 {
111         assert(context->session != NULL);
112         return afb_session_get_value(context->session, context->api_index);
113 }
114
115 void afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
116 {
117         assert(context->session != NULL);
118         return afb_session_set_value(context->session, context->api_index, value, free_value);
119 }
120
121 void afb_context_close(struct afb_context *context)
122 {
123         context->closing = 1;
124 }
125
126 void afb_context_refresh(struct afb_context *context)
127 {
128         assert(context->validated);
129         context->refreshing = 1;
130 }
131
132 int afb_context_check(struct afb_context *context)
133 {
134         return context->validated;
135 }
136
137 int afb_context_check_loa(struct afb_context *context, unsigned loa)
138 {
139         return context->loa_in >= loa;
140 }
141
142 int afb_context_change_loa(struct afb_context *context, unsigned loa)
143 {
144         if (!context->validated || loa > 7)
145                 return 0;
146
147         if (loa == context->loa_in && !context->loa_changed)
148                 context->loa_changing = 0;
149         else {
150                 context->loa_out = loa & 7;
151                 context->loa_changing = 1;
152                 context->loa_changed = 0;
153         }
154         return 1;
155 }
156
157