bd6883b8fe1961790e4d9f610ed8eeb0df8afc4f
[src/app-framework-main.git] / src / afm-launch-mode.c
1 /*
2  Copyright 2015, 2016 IoT.bzh
3
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 #include <string.h>
20 #include <assert.h>
21
22 #include "afm-launch-mode.h"
23
24 /*
25  * There is actually 2 possible launch mode:
26  *  - local
27  *  - remote
28  */
29 static const char s_mode_local[] = "local";
30 static const char s_mode_remote[] = "remote";
31
32 /*
33  * Records the current default mode
34  */
35 static enum afm_launch_mode default_mode = mode_local;
36
37 /*
38  * Set the default launch mode to 'mode'
39  */
40 int is_valid_launch_mode(enum afm_launch_mode mode)
41 {
42         switch(mode) {
43         case mode_local:
44         case mode_remote:
45                 return 1;
46         default:
47                 return 0;
48         }
49 }
50
51 /*
52  * Get the default launch mode
53  *
54  * Ensure a valid result
55  */
56 enum afm_launch_mode get_default_launch_mode()
57 {
58         return default_mode;
59 }
60
61 /*
62  * Set the default launch mode to 'mode'
63  *
64  * Requires 'mode' to be valid
65  */
66 void set_default_launch_mode(enum afm_launch_mode mode)
67 {
68         assert(is_valid_launch_mode(mode));
69         default_mode = mode;
70 }
71
72 /*
73  * Get the launch mode corresponding to the 'name'
74  *
75  * Returns invalid_launch_mode if the 'name' is not valid.
76  */
77 enum afm_launch_mode launch_mode_of_name(const char *name)
78 {
79         if (name) {
80                 if (!strcmp(name, s_mode_local))
81                         return mode_local;
82                 if (!strcmp(name, s_mode_remote))
83                         return mode_remote;
84         }
85         return invalid_launch_mode;
86 }
87
88 /*
89  * Get the name of the launch 'mode'
90  *
91  * Requires 'mode' to be valid
92  */
93 const char *name_of_launch_mode(enum afm_launch_mode mode)
94 {
95         assert(is_valid_launch_mode(mode));
96         switch (mode) {
97         case mode_local:  return s_mode_local;
98         case mode_remote: return s_mode_remote;
99         default:          return NULL;
100         }
101 }