Improves naming of session's module
[src/app-framework-binder.git] / src / afb-context.c
index 2f391de..eec4ebb 100644 (file)
@@ -1,6 +1,7 @@
 /*
- * Copyright (C) 2015 "IoT.bzh"
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
  * Author "Fulup Ar Foll"
+ * Author José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <assert.h>
 #include <stdlib.h>
 
-#include "session.h"
+#include "afb-session.h"
 #include "afb-context.h"
 
-
-void afb_context_init(struct afb_context *context, struct AFB_clientCtx *session, const char *token)
+static void init_context(struct afb_context *context, struct afb_session *session, const char *token)
 {
        assert(session != NULL);
 
@@ -32,38 +32,55 @@ void afb_context_init(struct afb_context *context, struct AFB_clientCtx *session
        context->session = session;
        context->flags = 0;
        context->api_index = -1;
+       context->loa_in = afb_session_get_LOA(session) & 7;
 
        /* check the token */
        if (token != NULL) {
-               if (ctxTokenCheck(session, token))
+               if (afb_session_check_token(session, token))
                        context->validated = 1;
                else
                        context->invalidated = 1;
        }
 }
 
+void afb_context_init(struct afb_context *context, struct afb_session *session, const char *token)
+{
+       init_context(context, afb_session_addref(session), token);
+}
+
 int afb_context_connect(struct afb_context *context, const char *uuid, const char *token)
 {
        int created;
-       struct AFB_clientCtx *session;
+       struct afb_session *session;
 
-       session = ctxClientGetSession (uuid, &created);
+       session = afb_session_get (uuid, &created);
        if (session == NULL)
                return -1;
-       afb_context_init(context, session, token);
-       if (created)
+       init_context(context, session, token);
+       if (created) {
                context->created = 1;
+               /* context->refreshing = 1; */
+       }
        return 0;
 }
 
 void afb_context_disconnect(struct afb_context *context)
 {
        if (context->session != NULL) {
+               if (context->refreshing && !context->refreshed) {
+                       afb_session_new_token (context->session);
+                       context->refreshed = 1;
+               }
+               if (context->loa_changing && !context->loa_changed) {
+                       afb_session_set_LOA (context->session, context->loa_out);
+                       context->loa_changed = 1;
+               }
                if (context->closing && !context->closed) {
+                       afb_session_close(context->session);
                        context->closed = 1;
-                       ctxClientClose(context->session);
                }
-               ctxClientUnref(context->session);
+               afb_session_unref(context->session);
+               context->session = NULL;
        }
 }
 
@@ -71,13 +88,13 @@ const char *afb_context_sent_token(struct afb_context *context)
 {
        if (context->session == NULL || context->closing)
                return NULL;
-       if (!(context->created || context->refreshing))
+       if (!context->refreshing)
                return NULL;
        if (!context->refreshed) {
-               ctxTokenNew (context->session);
+               afb_session_new_token (context->session);
                context->refreshed = 1;
        }
-       return ctxClientGetToken(context->session);
+       return afb_session_token(context->session);
 }
 
 const char *afb_context_sent_uuid(struct afb_context *context)
@@ -86,19 +103,19 @@ const char *afb_context_sent_uuid(struct afb_context *context)
                return NULL;
        if (!context->created)
                return NULL;
-       return ctxClientGetUuid(context->session);
+       return afb_session_uuid(context->session);
 }
 
 void *afb_context_get(struct afb_context *context)
 {
        assert(context->session != NULL);
-       return ctxClientValueGet(context->session, context->api_index);
+       return afb_session_get_value(context->session, context->api_index);
 }
 
 void afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
 {
        assert(context->session != NULL);
-       return ctxClientValueSet(context->session, context->api_index, value, free_value);
+       return afb_session_set_value(context->session, context->api_index, value, free_value);
 }
 
 void afb_context_close(struct afb_context *context)
@@ -108,6 +125,7 @@ void afb_context_close(struct afb_context *context)
 
 void afb_context_refresh(struct afb_context *context)
 {
+       assert(context->validated);
        context->refreshing = 1;
 }
 
@@ -116,7 +134,24 @@ int afb_context_check(struct afb_context *context)
        return context->validated;
 }
 
-int afb_context_create(struct afb_context *context)
+int afb_context_check_loa(struct afb_context *context, unsigned loa)
 {
-       return context->created;
+       return context->loa_in >= loa;
 }
+
+int afb_context_change_loa(struct afb_context *context, unsigned loa)
+{
+       if (!context->validated || loa > 7)
+               return 0;
+
+       if (loa == context->loa_in && !context->loa_changed)
+               context->loa_changing = 0;
+       else {
+               context->loa_out = loa & 7;
+               context->loa_changing = 1;
+               context->loa_changed = 0;
+       }
+       return 1;
+}
+
+