Modify ua.h to ua_types.h for integrating header with tota-ua
[platform/core/system/libtota.git] / ss_engine / SS_Common.c
1 /*
2  * libtota
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <stdarg.h>
26 #include <fcntl.h>
27
28 #include "SS_ImageUpdate.h"
29 #include "SS_Engine_Errors.h"
30 #include "SS_Common.h"
31
32 #include "ua_types.h"
33 #include "fota_tar.h"
34 #include "fota_common.h"
35
36 void SS_Progress(void *pbUserData, SS_UINT32 uPercent)
37 {
38         LOG("Progress before\n");
39         LOGL(LOG_SSENGINE, "Progress.. (%u %%)\n", uPercent);
40         LOG("Progress after:\n");
41         ((ua_data_t *) pbUserData)->ui_progress(pbUserData, uPercent);
42 }
43
44 /* Prints a string like the C printf() function */
45 SS_UINT32 SS_Trace(const char *aFormat, ...)
46 {
47 #if 0
48         LOGL(LOG_SSENGINE, aFormat);
49 #else
50         char temp[4096];
51         va_list list;
52
53         va_start(list, aFormat);
54         vsnprintf(temp, sizeof(temp), aFormat, list);
55         va_end(list);
56
57         LOGL(LOG_SSENGINE, "%s", temp);
58 #endif
59         return S_SS_SUCCESS;
60 }
61
62 long SS_FSTrace(const unsigned short *aFormat, ...)
63 {
64         va_list list;
65
66         va_start(list, aFormat);
67         vprintf((const char *)aFormat, list);
68         va_end(list);
69
70         return S_SS_SUCCESS;
71 }
72
73 long SS_ResetTimerA(void)
74 {
75         //LOG("%s \n", __func__);
76
77         return S_SS_SUCCESS;
78 }
79
80 long SS_GetDelta(void *pbUserData, unsigned char *pbBuffer, SS_UINT32 dwStartAddressOffset, SS_UINT32 dwSize)
81 {
82         int ret = S_SS_SUCCESS;
83         int readCount = 0;
84         FILE *fp;
85         long itemOffset = 0;
86
87         ua_data_t *ua_data = (ua_data_t *) pbUserData;
88         ua_part_info_t *ua_partition = ua_data->parti_info;
89         ua_update_data_t *ua_update_data = ua_data->update_data;
90
91         LOGL(LOG_SSENGINE, "SS_GetDelta offset 0x%x(%u), size 0x%x(%u)\n",
92                  dwStartAddressOffset, dwStartAddressOffset, dwSize, dwSize);
93
94         itemOffset = tar_get_item_offset(ua_update_data->ua_delta_path, ua_partition->ua_subject_name);
95         if (itemOffset < 0)
96                 return E_SS_OPENFILE_ONLYR;
97
98         fp = fopen(ua_update_data->ua_delta_path, "r");
99         if (!fp) {
100                 LOGL(LOG_SSENGINE, "open file %s failed.\n", ua_update_data->ua_delta_path);
101                 return E_SS_OPENFILE_ONLYR;
102         }
103
104         if (fseek(fp, itemOffset + dwStartAddressOffset, 0) == -1)
105                 ret = E_SS_READ_ERROR;
106         else {
107                 readCount = fread(pbBuffer, 1, dwSize, fp);
108                 if (readCount != dwSize) {
109                         LOGL(LOG_SSENGINE, "error in read size\n");
110                         ret = E_SS_READ_ERROR;
111                 }
112         }
113         fclose(fp);
114
115         return ret;
116 }