Merge branch 'master' of https://github.com/iotbzh/afb-daemon
[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
35         /* check the token */
36         if (token != NULL) {
37                 if (ctxTokenCheck(session, token))
38                         context->validated = 1;
39                 else
40                         context->invalidated = 1;
41         }
42 }
43
44 void afb_context_init(struct afb_context *context, struct AFB_clientCtx *session, const char *token)
45 {
46         init_context(context, ctxClientAddRef(session), token);
47 }
48
49 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
50 {
51         int created;
52         struct AFB_clientCtx *session;
53
54         session = ctxClientGetSession (uuid, &created);
55         if (session == NULL)
56                 return -1;
57         init_context(context, session, token);
58         if (created)
59                 context->created = 1;
60         return 0;
61 }
62
63 void afb_context_disconnect(struct afb_context *context)
64 {
65         if (context->session != NULL) {
66                 if (context->closing && !context->closed) {
67                         context->closed = 1;
68                         ctxClientClose(context->session);
69                 }
70                 ctxClientUnref(context->session);
71         }
72 }
73
74 const char *afb_context_sent_token(struct afb_context *context)
75 {
76         if (context->session == NULL || context->closing)
77                 return NULL;
78         if (!(context->created || context->refreshing))
79                 return NULL;
80         if (!context->refreshed) {
81                 ctxTokenNew (context->session);
82                 context->refreshed = 1;
83         }
84         return ctxClientGetToken(context->session);
85 }
86
87 const char *afb_context_sent_uuid(struct afb_context *context)
88 {
89         if (context->session == NULL || context->closing)
90                 return NULL;
91         if (!context->created)
92                 return NULL;
93         return ctxClientGetUuid(context->session);
94 }
95
96 void *afb_context_get(struct afb_context *context)
97 {
98         assert(context->session != NULL);
99         return ctxClientValueGet(context->session, context->api_index);
100 }
101
102 void afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
103 {
104         assert(context->session != NULL);
105         return ctxClientValueSet(context->session, context->api_index, value, free_value);
106 }
107
108 void afb_context_close(struct afb_context *context)
109 {
110         context->closing = 1;
111 }
112
113 void afb_context_refresh(struct afb_context *context)
114 {
115         context->refreshing = 1;
116 }
117
118 int afb_context_check(struct afb_context *context)
119 {
120         return context->validated;
121 }
122
123 int afb_context_create(struct afb_context *context)
124 {
125         return context->created;
126 }