resolve prevent issues
[platform/framework/native/env-config.git] / appinfo / appinfo.c
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <limits.h>
22
23 #include <dlog.h>
24
25 #include "appinfo.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "APPINFO"
29
30 #ifdef _SECURE_LOG
31 #define _SECURE_LOGI LOGI
32 #define _SECURE_LOGE LOGE
33 #else
34 #define _SECURE_LOGI(...)
35 #define _SECURE_LOGE(...)
36 #endif
37
38 #ifndef likely
39 #define likely(x)    __builtin_expect(!!(x), 1)
40 #endif
41 #ifndef unlikely
42 #define unlikely(x)  __builtin_expect(!!(x), 0)
43 #endif
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 static const char INVALID_EXEC_NAME[] = "";
50 static app_info_version_e _current_api_ver = APP_INFO_VERSION_2_2;
51 static int _compat_mode = 0;
52 static int _app_info_init = 0;
53
54 static int _argc = 0;
55 static char** _argv = NULL;
56
57 #define MAX_OSP_PKGID 10
58 #define MAX_APPID 256
59
60 static char __pkgid[MAX_OSP_PKGID+1]={'\0',};
61 static char __appid[MAX_APPID]={'\0',};
62 static const char* _pkgid = NULL;
63 static const char* _appid = NULL;
64 static const char* _execname = &INVALID_EXEC_NAME;
65
66
67 /**
68  * @brief       Initializes the appinfo structure
69  *
70  * @param[in] appid     The application id to initialize
71  * @param[in] is_invalid_appid  @c 1 for invalid appid compatibility mode, @c 0 otherwise
72  * @return      0 on success, otherwise a negative error value
73  * @retval      APP_INFO_ERROR_NONE Succesful
74  * @retval      APP_INFO_ERROR_INVALID_ARG The appid is invalid
75  * @remarks     It initialize the platform API version to APP_INFO_VERSION_3_0 and compat mode 0.
76  */
77 int
78 appinfo_init(const char* appid, int is_invalid_appid)
79 {
80         if (!appid || (appid && strlen(appid) >= sizeof(__appid)))
81         {
82                 return APP_INFO_ERROR_INVALID_ARG;
83         }
84
85         strncpy(__appid, appid, strlen(appid));
86         if (is_invalid_appid)
87         {
88                 // SLP-style old appid
89                 _pkgid = __appid;
90                 _appid = __appid;
91                 _execname = __appid;
92         }
93         else
94         {
95                 if (strlen(appid) < sizeof(MAX_OSP_PKGID))
96                 {
97                         return APP_INFO_ERROR_INVALID_ARG;
98                 }
99                 // proper OSP or Web appid
100                 strncpy(__pkgid, appid, MAX_OSP_PKGID);
101                 _appid = __appid;
102                 _pkgid = __pkgid;
103                 _execname = __appid+(MAX_OSP_PKGID+1);
104         }
105
106         _current_api_ver = APP_INFO_VERSION_2_2;
107         _compat_mode = 0;
108         _app_info_init = 1;
109
110         return APP_INFO_ERROR_NONE;
111 }
112
113 /**
114  * @brief       Returns the API version
115  *
116  * @return      app_info_version_e value
117  */
118 int
119 appinfo_get_api_version(void)
120 {
121         return _current_api_ver;
122 }
123
124 /**
125  * @brief       Sets the API version for the current process
126  *
127  * @param[in]   ver Given API version
128  * @return      0 on success, otherwise a negative error value
129  * @retval      APP_INFO_ERROR_NONE Succesful
130  */
131
132 int
133 appinfo_set_api_version(app_info_version_e ver)
134 {
135         if (ver < 0)
136         {
137                 return APP_INFO_ERROR_INVALID_ARG;
138         }
139
140         _current_api_ver = ver;
141
142         return 0;
143 }
144
145 /*
146 int
147 appinfo_set_api_version(int ver)
148 {
149         app_info_version_e temp_ver;
150         temp_ver = __get_api_version_from_str(appinfo_get_api_str_by_version(ver));
151
152         if ( temp_ver == APP_INFO_VERSION_INVALID)
153         {
154                 return APP_INFO_ERROR_INVALID_ARG;
155         }
156         else
157         {
158                 _current_api_ver = temp_ver;
159                 return APP_INFO_ERROR_NONE;
160         }
161 }
162 */
163
164 /**
165  * @brief       Returns the API version string by given API version
166  *
167  * @param[in]   ver Given API version
168  * @return      the API version string by the given version
169  * @remakr      If wrong API version, return NULL
170  */
171 const char*
172 appinfo_get_api_str_by_version(int ver)
173 {
174         switch (ver) {
175         case APP_INFO_VERSION_1_0 :
176                 return "1.0";
177         case APP_INFO_VERSION_1_0_2 :
178                 return "1.0.2";
179         case APP_INFO_VERSION_1_1 :
180                 return "1.1";
181         case APP_INFO_VERSION_1_2 :
182                 return "1.2";
183         case APP_INFO_VERSION_2_0 :
184                 return "2.0";
185         case APP_INFO_VERSION_2_1 :
186                 return "2.1";
187         case APP_INFO_VERSION_2_2 :
188                 return "2.2";
189         case APP_INFO_VERSION_3_0 :
190                 return "3.0";
191         default :
192                 return NULL;
193         }
194 }
195
196 app_info_version_e appinfo_get_api_version_from_str(const char * ver_str)
197 {
198         if (ver_str == NULL)
199         {
200                 return APP_INFO_VERSION_INVALID;
201         }
202
203         if (!strcmp(ver_str,"3.0"))
204         {
205                 return APP_INFO_VERSION_3_0;
206         }
207         else if (!strcmp(ver_str,"2.2"))
208         {
209                 return APP_INFO_VERSION_2_2;
210         }
211         else if (!strcmp(ver_str,"2.1"))
212         {
213                 return APP_INFO_VERSION_2_1;
214         }
215         else if (!strcmp(ver_str,"2.0"))
216         {
217                 return APP_INFO_VERSION_2_0;
218         }
219         else if (!strcmp(ver_str,"1.2"))
220         {
221                 return APP_INFO_VERSION_1_2;
222         }
223         else if (!strcmp(ver_str,"1.1"))
224         {
225                 return APP_INFO_VERSION_1_1;
226         }
227         else if (!strcmp(ver_str,"1.0.2"))
228         {
229                 return APP_INFO_VERSION_1_0_2;
230         }
231         else if (!strcmp(ver_str,"1.0"))
232         {
233                 return APP_INFO_VERSION_1_0;
234         }
235         else
236         {
237                 return APP_INFO_VERSION_INVALID;
238         }
239 }
240
241 /**
242  * @brief       Returns whether the application is compat mode or not
243  *
244  * @return      @c 1 for compat mode, @c 0 otherwise
245  */
246 int
247 appinfo_is_compat(void)
248 {
249         return _compat_mode;
250 }
251
252 /**
253  * @brief       Sets the application compat mode
254  *
255  * @param[in]   the compatibility mode
256  * @return      0 on success, otherwise a negative error value
257  * @retval      APP_INFO_ERROR_NONE Succesful
258  * @retval      APP_INFO_ERROR_INVALID_ARG compat should be either @c 0 or @c 1.
259  */
260 int
261 appinfo_set_compat(int compat)
262 {
263         if (compat == 0 || compat == 1)
264         {
265                 _compat_mode = compat;
266                 return APP_INFO_ERROR_NONE;
267         }
268         return APP_INFO_ERROR_INVALID_ARG;
269
270 }
271
272 /**
273  * @brief       Returns the appid for the application
274  *
275  * @retval      application ID if valid, @c NULL otherwise
276  */
277 const char*
278 appinfo_get_appid(void)
279 {
280         if (likely(_app_info_init))
281         {
282                 return _appid;
283         }
284
285         return NULL;
286 }
287
288 /**
289  * @brief       Returns the exec name for the application
290  *
291  * @retval      application exec name if valid, empty string otherwise
292  */
293 const char*
294 appinfo_get_execname(void)
295 {
296         return _execname;
297 }
298
299
300 /**
301  * @brief       Returns the packageid for the application
302  *
303  * @retval      package ID if valid, @c NULL otherwise
304  */
305 const char*
306 appinfo_get_packageid(void)
307 {
308         if (likely(_app_info_init))
309         {
310                 return _pkgid;
311         }
312
313         return NULL;
314 }
315
316 /**
317  * @brief       Returns whether the appinfo is initialized or not
318  *
319  * @return      @c 1 if initialized, @c 0 otherwise
320  */
321 int
322 appinfo_is_initialized(void)
323 {
324         return _app_info_init;
325 }
326
327 int
328 appinfo_set_argv(int argc, char** argv)
329 {
330         if (argc > 0 && argv)
331         {
332                 _argc = argc;
333                 _argv = argv;
334                 return 1;
335         }
336         return 0;
337 }
338
339 int appinfo_get_argv(int* argc, char*** argv)
340 {
341         if (_app_info_init && argc && argv)
342         {
343                 *argc = _argc;
344                 *argv = _argv;
345
346                 return 1;
347         }
348         return 0;
349 }
350
351 #define MIN(a,b)  ((a) < (b) ? (a) : (b))
352
353 int appinfo_update_submode_execname_and_appid(const char* execname)
354 {
355         if (execname == NULL)
356         {
357                 return 0;
358         }
359
360         const size_t max_len = MAX_APPID - MAX_OSP_PKGID - 1;
361         const size_t size = MIN(strlen(execname) - 1, max_len - 1);
362         strncpy(__appid + MAX_OSP_PKGID + 1, execname, size);
363         __appid[size + MAX_OSP_PKGID + 1] = '\0';
364
365         return 1;
366 }
367
368 #ifdef __cplusplus
369 }
370 #endif
371