Tizen 2.1 base
[platform/framework/web/download-provider.git] / agent / download-agent-utils-dl-id-history.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "download-agent-type.h"
18 #include "download-agent-utils.h"
19 #include "download-agent-utils-dl-id-history.h"
20
21 da_result_t init_dl_id_history(dl_id_history_t *dl_id_history)
22 {
23         da_result_t ret = DA_RESULT_OK;
24
25         /* Initial dl_id_history will be starting number for dl_id.
26          * dl_id will be sequentially increased from the dl_id_history,
27          * then dl_id_history will be updated. */
28         _da_thread_mutex_init(&(dl_id_history->mutex), DA_NULL);
29         _da_thread_mutex_lock(&(dl_id_history->mutex));
30         get_random_number(&(dl_id_history->starting_num));
31         dl_id_history->cur_dl_id = DA_INVALID_ID;
32         _da_thread_mutex_unlock(&(dl_id_history->mutex));
33
34         DA_LOG_CRITICAL(Default,"starting num = %d", dl_id_history->starting_num);
35         return ret;
36 }
37
38 da_result_t deinit_dl_id_history(dl_id_history_t *dl_id_history)
39 {
40         da_result_t ret = DA_RESULT_OK;
41
42         _da_thread_mutex_lock(&(dl_id_history->mutex));
43         dl_id_history->starting_num = DA_INVALID_ID;
44         dl_id_history->cur_dl_id = DA_INVALID_ID;
45         _da_thread_mutex_unlock(&(dl_id_history->mutex));
46
47         _da_thread_mutex_destroy(&(dl_id_history->mutex));
48
49         return ret;
50 }
51
52 int get_available_dl_id(dl_id_history_t *dl_id_history)
53 {
54         int dl_id = 0;
55
56         _da_thread_mutex_lock(&(dl_id_history->mutex));
57
58         if (dl_id_history->cur_dl_id == DA_INVALID_ID)
59                 dl_id_history->cur_dl_id = dl_id_history->starting_num;
60         else if (dl_id_history->cur_dl_id > 254)
61                 dl_id_history->cur_dl_id = 1;
62         else
63                 dl_id_history->cur_dl_id++;
64
65         dl_id = dl_id_history->cur_dl_id;
66
67         _da_thread_mutex_unlock(&(dl_id_history->mutex));
68
69         DA_LOG_CRITICAL(Default,"dl_id = %d", dl_id);
70         return dl_id;
71 }