1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include "amp_utils.h"
8 #include "amp_defines.h"
9 #include "aos_system.h"
10
11 #define MOD_STR "AMP_UTILS"
12
13 static AOS_SYSTEM_VERSION aos_version = {0};
14
hex2num(unsigned char ch)15 unsigned char hex2num(unsigned char ch)
16 {
17 if (ch >= 'a') {
18 return ch - 'a' + 10;
19 } else if (ch >= 'A') {
20 return ch - 'A' + 10;
21 }
22
23 return ch - '0';
24 }
25
itoch(int val)26 char itoch(int val)
27 {
28 if (val < 10) {
29 return (char)('0' + val);
30 }
31
32 return (char)('a' + val - 10);
33 }
34
num2hex(unsigned char ch,unsigned char * hex)35 void num2hex(unsigned char ch, unsigned char *hex)
36 {
37 hex[0] = itoch(ch / 16);
38 hex[1] = itoch(ch % 16);
39 }
40
41 /*****************************************************************************
42 *Function: end_with
43 *Description: check if str1 end with str2
44 *Input: str1 and str2
45 *Output: if str1 end with str2,return 1 else return 0
46 *****************************************************************************/
end_with(char * str1,char * str2)47 int end_with(char *str1, char *str2)
48 {
49 if (!str1 || !str2) {
50 return 0;
51 }
52
53 int len1 = strlen(str1);
54 int len2 = strlen(str2);
55
56 if (len1 >= len2) {
57 if (strcmp(str1 + len1 - len2, str2) == 0) {
58 return 1;
59 }
60 }
61
62 return 0;
63 }
64
65 /*****************************************************************************
66 *Function: hexdump
67 *Description: print buff with hex style
68 *Input: title: a string, buff: the dest buff for dump,len:the buffer len
69 *Output: none
70 *****************************************************************************/
amp_dump(const char * title,const void * buff,const int len)71 void amp_dump(const char *title, const void *buff, const int len)
72 {
73 int i, j, written;
74 unsigned char ascii[16 + 1] = {0};
75 // char header[64] = {0};
76 unsigned char *buf = (unsigned char *)buff;
77
78 written = 0;
79
80 amp_debug(MOD_STR, "%s : ", title);
81 for (i = 0; i < len; ++i) {
82 if (i % 16 == 0) {
83 amp_debug(MOD_STR, "| %08X: ", (unsigned int)(i + (long)buff));
84 written += 8;
85 }
86
87 amp_debug(MOD_STR, "%02X", buf[i]);
88 written += 2;
89
90 if (i % 2 == 1) {
91 amp_debug(MOD_STR, " ");
92 written += 1;
93 }
94 sprintf((char *)ascii + i % 16, "%c",
95 ((buf[i] >= ' ' && buf[i] <= '~') ? buf[i] : '.'));
96
97 if (((i + 1) % 16 == 0) || (i == len - 1)) {
98 for (j = 0; j < 48 - written; ++j) {
99 amp_debug(MOD_STR, " ");
100 }
101
102 amp_debug(MOD_STR, " %s", ascii);
103 amp_debug(MOD_STR, "");
104
105 written = 0;
106 memset(ascii, 0, sizeof(ascii));
107 }
108 }
109 amp_debug(MOD_STR, "%s",
110 "+"
111 "-----------------------"
112 "-----------------------"
113 "-----------------------");
114
115 return;
116 }
117
118
119 /**
120 * 设置用户JS脚本版本号
121 *
122 */
aos_userjs_version_set(char * version)123 void aos_userjs_version_set(char *version)
124 {
125 if (!version)
126 return;
127 snprintf(aos_version.userjs, sizeof(aos_version.userjs), "%s", version);
128 // amp_debug(MOD_STR, "version %s", aos_version.userjs);
129 }
130
131 /**
132 * 获取用户的JS脚本版本号
133 *
134 */
aos_userjs_version_get(void)135 const char *aos_userjs_version_get(void)
136 {
137 // amp_debug(MOD_STR, "version %s", aos_version.userjs);
138 return aos_version.userjs;
139 }
140
141 /**
142 * 获取用户的JS脚本版本号
143 *
144 */
aos_app_version_get(void)145 const char *aos_app_version_get(void)
146 {
147 const char *amp_version_fmt = "amp-v%s";
148 aos_snprintf(aos_version.app, 64, amp_version_fmt, AMP_VERSION_NUMBER);
149 // amp_debug(MOD_STR, "version %s", aos_version.app);
150 return aos_version.app;
151 }
152
153 /**
154 * 获取系统kernel软件版本号,在模组设备上即模组软件版本号
155 *
156 */
aos_kernel_version_get(void)157 const char *aos_kernel_version_get(void)
158 {
159 memset(aos_version.kernel, 0, AOS_VERSION_LENGTH);
160 memcpy(aos_version.kernel, AMP_MODULE_SOFTWARE, strlen(AMP_MODULE_SOFTWARE));
161 // amp_debug(MOD_STR, "version %s", aos_version.kernel);
162 return aos_version.kernel;
163 }
164
165
166 /**
167 * 获取系统软件版本号,包括JS轻应用软件版本和模组软件版本
168 * 用于模组软件不能单独升级、只能和应用软件一起打包升级的场景
169 *
170 */
aos_system_version_get(void)171 const char *aos_system_version_get(void)
172 {
173 const char *amp_version_fmt = "amp-v%s-%s";
174 memset(aos_version.system, 0, AOS_VERSION_LENGTH * 2);
175 aos_snprintf(aos_version.system, AMP_VERSION_LENGTH, amp_version_fmt, \
176 AMP_VERSION_NUMBER, AMP_MODULE_SOFTWARE);
177 // amp_debug(MOD_STR, "version %s", aos_version.system);
178 return aos_version.system;
179 }
180
181 /**
182 * 系统软件编译时间
183 *
184 */
aos_system_build_time(void)185 const char *aos_system_build_time(void)
186 {
187 const char *amp_version_fmt = "%s, %s";
188 memset(aos_version.build_time, 0, 128);
189
190 aos_snprintf(aos_version.build_time, AMP_VERSION_LENGTH, amp_version_fmt, \
191 __DATE__, __TIME__);
192 // amp_debug(MOD_STR, "version %s", aos_version.build_time);
193 return aos_version.build_time;
194 }
195
196 /**
197 * 模组硬件版本
198 *
199 */
aos_hardware_version_get(void)200 const char *aos_hardware_version_get(void)
201 {
202 memset(aos_version.module_hardware, 0, AOS_VERSION_LENGTH);
203 memcpy(aos_version.module_hardware, AMP_MODULE_HARDWARE, strlen(AMP_MODULE_HARDWARE));
204 // amp_debug(MOD_STR, "version %s", aos_version.module_hardware);
205 return aos_version.module_hardware;
206 }
207
208