2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "download-agent-type.h"
18 #include "download-agent-utils.h"
19 #include "download-agent-utils-dl-id-history.h"
21 da_result_t init_dl_id_history(dl_id_history_t *dl_id_history)
23 da_result_t ret = DA_RESULT_OK;
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));
34 DA_LOG_CRITICAL(Default,"starting num = %d", dl_id_history->starting_num);
38 da_result_t deinit_dl_id_history(dl_id_history_t *dl_id_history)
40 da_result_t ret = DA_RESULT_OK;
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));
47 _da_thread_mutex_destroy(&(dl_id_history->mutex));
52 int get_available_dl_id(dl_id_history_t *dl_id_history)
56 _da_thread_mutex_lock(&(dl_id_history->mutex));
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;
63 dl_id_history->cur_dl_id++;
65 dl_id = dl_id_history->cur_dl_id;
67 _da_thread_mutex_unlock(&(dl_id_history->mutex));
69 DA_LOG_CRITICAL(Default,"dl_id = %d", dl_id);