1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <string.h>
9 
10 #include "aos/kernel.h"
11 #include "aos/kv.h"
12 #include <k_api.h>
13 
14 #include "linkkit/infra/infra_compat.h"
15 #include "linkkit/wrappers/wrappers_defs.h"
16 
17 #define DEFAULT_THREAD_PRI AOS_DEFAULT_APP_PRI
18 
19 #define _RHINO_SDK_DEMO__  1
20 
21 #define PLATFORM_LOG(format, ...)                                    \
22     do {                                                             \
23         printf("Linux:%s:%d %s()| " format "\n", __FILE__, __LINE__, \
24                __func__, ##__VA_ARGS__);                         \
25     } while (0);
26 
27 #ifndef TAG
28 #define TAG "HAL_OS"
29 #endif
30 
HAL_MutexCreate(void)31 void *HAL_MutexCreate(void)
32 {
33     aos_mutex_t mutex;
34     if (0 != aos_mutex_new(&mutex)) {
35         return NULL;
36     }
37     return mutex;
38 }
39 
HAL_MutexDestroy(_IN_ void * mutex)40 int HAL_MutexDestroy(_IN_ void *mutex)
41 {
42     if (NULL != mutex) {
43         aos_mutex_free((aos_mutex_t *)&mutex);
44     }
45     return 0;
46 }
47 
HAL_MutexLock(_IN_ void * mutex)48 int HAL_MutexLock(_IN_ void *mutex)
49 {
50     if (NULL != mutex) {
51         return aos_mutex_lock((aos_mutex_t *)&mutex, AOS_WAIT_FOREVER);
52     } else
53         return -EINVAL;
54 }
55 
HAL_MutexUnlock(_IN_ void * mutex)56 int HAL_MutexUnlock(_IN_ void *mutex)
57 {
58     if (NULL != mutex) {
59         return aos_mutex_unlock((aos_mutex_t *)&mutex);
60     } else
61         return -EINVAL;
62 }
63 
HAL_Malloc(_IN_ uint32_t size)64 void *HAL_Malloc(_IN_ uint32_t size)
65 {
66     return aos_malloc(size);
67 }
68 
HAL_Free(_IN_ void * ptr)69 void HAL_Free(_IN_ void *ptr)
70 {
71     aos_free(ptr);
72 }
73 
HAL_Reboot(void)74 void HAL_Reboot(void)
75 {
76     aos_reboot();
77 }
78 
HAL_UptimeMs(void)79 uint64_t HAL_UptimeMs(void)
80 {
81     return aos_now_ms();
82 }
83 
HAL_SleepMs(_IN_ uint32_t ms)84 void HAL_SleepMs(_IN_ uint32_t ms)
85 {
86     aos_msleep(ms);
87 }
88 
HAL_Snprintf(_IN_ char * str,const int len,const char * fmt,...)89 int HAL_Snprintf(_IN_ char *str, const int len, const char *fmt, ...)
90 {
91     va_list args;
92     int rc;
93 
94     va_start(args, fmt);
95     rc = vsnprintf(str, len, fmt, args);
96     va_end(args);
97 
98     return rc;
99 }
100 
HAL_Vsnprintf(_IN_ char * str,_IN_ const int len,_IN_ const char * format,va_list ap)101 int HAL_Vsnprintf(_IN_ char *str, _IN_ const int len, _IN_ const char *format,
102                   va_list ap)
103 {
104     return vsnprintf(str, len, format, ap);
105 }
106 
HAL_Srandom(uint32_t seed)107 void HAL_Srandom(uint32_t seed)
108 {
109     // srandom(seed);
110     srand(seed);
111 }
112 
HAL_Random(uint32_t region)113 uint32_t HAL_Random(uint32_t region)
114 {
115     uint32_t output;
116 #if defined(CONFIG_TEE_CA)
117     csi_tee_rand_generate((uint8_t *)&output, 4);
118 #else
119     static uint32_t rnd = 0x12345;
120 
121     output = rnd * 0xFFFF777;
122     rnd = output;
123 #endif
124     return (region > 0) ? (output % region) : 0;
125 }
126 
HAL_SemaphoreCreate(void)127 void *HAL_SemaphoreCreate(void)
128 {
129     aos_sem_t sem;
130 
131     if (0 != aos_sem_new(&sem, 0)) {
132         return NULL;
133     }
134 
135     return sem;
136 }
137 
HAL_SemaphoreDestroy(_IN_ void * sem)138 void HAL_SemaphoreDestroy(_IN_ void *sem)
139 {
140     aos_sem_free((aos_sem_t *)&sem);
141 }
142 
HAL_SemaphorePost(_IN_ void * sem)143 void HAL_SemaphorePost(_IN_ void *sem)
144 {
145     aos_sem_signal((aos_sem_t *)&sem);
146 }
147 
HAL_SemaphoreWait(_IN_ void * sem,_IN_ uint32_t timeout_ms)148 int HAL_SemaphoreWait(_IN_ void *sem, _IN_ uint32_t timeout_ms)
149 {
150     if (PLATFORM_WAIT_INFINITE == timeout_ms) {
151         return aos_sem_wait((aos_sem_t *)&sem, AOS_WAIT_FOREVER);
152     } else {
153         return aos_sem_wait((aos_sem_t *)&sem, timeout_ms);
154     }
155 }
156 
157 typedef struct {
158     aos_task_t task;
159     int detached;
160     void *arg;
161     void *(*routine)(void *arg);
162 } task_context_t;
163 
task_wrapper(void * arg)164 static void task_wrapper(void *arg)
165 {
166     task_context_t *task = arg;
167     if (task == NULL) {
168         return;
169     }
170     task->routine(task->arg);
171 
172     aos_free(task);
173     task = NULL;
174 }
175 
176 #define DEFAULT_THREAD_NAME "AosThread"
177 #define DEFAULT_THREAD_SIZE 4096
HAL_ThreadCreate(_OU_ void ** thread_handle,_IN_ void * (* work_routine)(void *),_IN_ void * arg,_IN_ hal_os_thread_param_t * hal_os_thread_param,_OU_ int * stack_used)178 int HAL_ThreadCreate(_OU_ void **thread_handle,
179                      _IN_ void *(*work_routine)(void *), _IN_ void *arg,
180                      _IN_ hal_os_thread_param_t *hal_os_thread_param,
181                      _OU_ int *stack_used)
182 {
183     int ret = -1;
184     if (stack_used != NULL) {
185         *stack_used = 0;
186     }
187     char *tname;
188     size_t ssiz;
189     int detach_state = 0;
190     int priority;
191 
192     if (hal_os_thread_param) {
193         detach_state = hal_os_thread_param->detach_state;
194     }
195     if (!hal_os_thread_param || !hal_os_thread_param->name) {
196         tname = DEFAULT_THREAD_NAME;
197     } else {
198         tname = hal_os_thread_param->name;
199     }
200 
201     if (!hal_os_thread_param || hal_os_thread_param->stack_size == 0) {
202         ssiz = DEFAULT_THREAD_SIZE;
203     } else {
204         ssiz = hal_os_thread_param->stack_size;
205     }
206 
207     if (!hal_os_thread_param || hal_os_thread_param->priority == 0) {
208         priority = DEFAULT_THREAD_PRI;
209     } else if (hal_os_thread_param->priority < os_thread_priority_idle ||
210                hal_os_thread_param->priority > os_thread_priority_realtime) {
211         priority = DEFAULT_THREAD_PRI;
212     } else {
213         priority = DEFAULT_THREAD_PRI - hal_os_thread_param->priority;
214     }
215 
216     task_context_t *task = aos_malloc(sizeof(task_context_t));
217     if (!task) {
218         return -1;
219     }
220     memset(task, 0, sizeof(task_context_t));
221 
222     task->arg = arg;
223     task->routine = work_routine;
224     task->detached = detach_state;
225 
226     ret = aos_task_new_ext(&task->task, tname, task_wrapper, task, ssiz,
227                            priority);
228 
229     *thread_handle = (void *)task;
230 
231     return ret;
232 }
233 
HAL_Firmware_Persistence_Start(void)234 void HAL_Firmware_Persistence_Start(void)
235 {
236 }
237 
HAL_Firmware_Persistence_Write(_IN_ char * buffer,_IN_ uint32_t length)238 int HAL_Firmware_Persistence_Write(_IN_ char *buffer, _IN_ uint32_t length)
239 {
240     return 0;
241 }
242 
HAL_Firmware_Persistence_Stop(void)243 int HAL_Firmware_Persistence_Stop(void)
244 {
245     return 0;
246 }
247 
HAL_Config_Write(const char * buffer,int length)248 int HAL_Config_Write(const char *buffer, int length)
249 {
250     if (!buffer || length <= 0) {
251         return -1;
252     }
253 
254     return aos_kv_set("alink", buffer, length, 1);
255 }
256 
HAL_Config_Read(char * buffer,int length)257 int HAL_Config_Read(char *buffer, int length)
258 {
259     if (!buffer || length <= 0) {
260         return -1;
261     }
262 
263     return aos_kv_get("alink", buffer, &length);
264 }
265 
HAL_Kv_Set(const char * key,const void * val,int len,int sync)266 int HAL_Kv_Set(const char *key, const void *val, int len, int sync)
267 {
268     int ret = 0;
269     ret = aos_kv_set(key, val, len, sync);
270     return ret;
271 }
272 
HAL_Kv_Get(const char * key,void * buffer,int * buffer_len)273 int HAL_Kv_Get(const char *key, void *buffer, int *buffer_len)
274 {
275     int ret = 0;
276     ret = aos_kv_get(key, buffer, buffer_len);
277     return ret;
278 }
279 
HAL_Kv_Del(const char * key)280 int HAL_Kv_Del(const char *key)
281 {
282     int ret = 0;
283     ret = aos_kv_del(key);
284     return ret;
285 }
286 
287 /*
288  * timer API
289  *
290  * */
291 typedef void (*hal_timer_cb)(void *);
292 
293 typedef struct time_data {
294     sys_time_t expect_time;
295     void *user_data;
296     hal_timer_cb cb;
297     struct time_data *next;
298     int isactive;
299 } timer_data_t;
300 
301 static timer_data_t *data_list = NULL;
302 static void *mutex = NULL;
303 
_list_insert(timer_data_t * data)304 static int _list_insert(timer_data_t *data)
305 {
306     if (data == NULL) {
307         return -1;
308     }
309     if (mutex == NULL) {
310         if (mutex == NULL) {
311             LOGE(TAG, "mutex create failed");
312             return -1;
313         }
314     }
315 
316     HAL_MutexLock(mutex);
317     data->next = data_list;
318     data_list = data;
319     HAL_MutexUnlock(mutex);
320     return 0;
321 }
322 
awss_timer_task(void * args)323 static void awss_timer_task(void *args)
324 {
325     int sleep_time;
326     int no_task_cnt = 0;
327 
328     while (1) {
329         if (!data_list) {
330             if (++no_task_cnt > 1000) {
331                 no_task_cnt = 1000;
332                 sleep_time = 1000;
333                 goto SLEEP;
334             }
335         } else {
336             no_task_cnt = 0;
337         }
338 
339         sleep_time = 10;
340         HAL_MutexLock(mutex);
341 
342         timer_data_t *cur = data_list;
343         while (cur != NULL) {
344             if (cur->isactive &&
345                 cur->expect_time + 1 <= krhino_sys_tick_get()) {
346                 cur->cb(cur->user_data);
347                 cur->isactive = 0;
348             }
349             cur = cur->next;
350         }
351 
352         HAL_MutexUnlock(mutex);
353 
354     SLEEP:
355         aos_msleep(sleep_time);
356     }
357 }
358 
HAL_Timer_Create(const char * name,void (* func)(void *),void * user_data)359 void *HAL_Timer_Create(const char *name, void (*func)(void *), void *user_data)
360 {
361     static aos_task_t *tsk = NULL;
362 
363     if (!tsk) {
364         mutex = HAL_MutexCreate();
365         tsk = (aos_task_t *)aos_malloc(sizeof(aos_task_t));
366         aos_task_new_ext(tsk, "AWSS_TIMER", awss_timer_task, NULL, 1024 * 4,
367                          20);
368     }
369 
370     timer_data_t *node = (timer_data_t *)aos_malloc(sizeof(timer_data_t));
371     if (node == NULL) {
372         return NULL;
373     }
374 
375     memset(node, 0, sizeof(timer_data_t));
376     node->user_data = user_data;
377     node->cb = func;
378 
379     _list_insert(node);
380     return node;
381 }
382 
HAL_Timer_Delete(void * timer)383 int HAL_Timer_Delete(void *timer)
384 {
385     CHECK_PARAM(timer, -1);
386 
387     timer_data_t *cur = data_list;
388     timer_data_t *pre = data_list;
389 
390     HAL_MutexLock(mutex);
391     while (cur != NULL) {
392         if (cur == timer) {
393             if (cur == pre) {
394                 data_list = cur->next;
395             } else {
396                 pre->next = cur->next;
397             }
398             aos_free(cur);
399             HAL_MutexUnlock(mutex);
400             return 0;
401         }
402         pre = cur;
403         cur = cur->next;
404     }
405     HAL_MutexUnlock(mutex);
406 
407     LOGE(TAG, "time not found");
408     return -1;
409 }
410 
HAL_Timer_Start(void * timer,int ms)411 int HAL_Timer_Start(void *timer, int ms)
412 {
413     CHECK_PARAM(timer, -1);
414 
415     if (timer == NULL) {
416         return -1;
417     }
418 
419     HAL_MutexLock(mutex);
420     timer_data_t *cur = data_list;
421     while (cur != NULL) {
422         if (cur == timer) {
423             cur->expect_time = krhino_sys_tick_get() + krhino_ms_to_ticks(ms);
424             cur->isactive = 1;
425             HAL_MutexUnlock(mutex);
426             return 0;
427         }
428         cur = cur->next;
429     }
430     HAL_MutexUnlock(mutex);
431 
432     return -1;
433 }
434 
HAL_Timer_Stop(void * timer)435 int HAL_Timer_Stop(void *timer)
436 {
437     CHECK_PARAM(timer, -1);
438 
439     if (timer == NULL) {
440         return -1;
441     }
442 
443     HAL_MutexLock(mutex);
444     timer_data_t *cur = data_list;
445     while (cur != NULL) {
446         if (cur == timer) {
447             cur->isactive = 0;
448             HAL_MutexUnlock(mutex);
449             return 0;
450         }
451         cur = cur->next;
452     }
453     HAL_MutexUnlock(mutex);
454 
455     return -1;
456 }
457 
458 static int64_t delta_ms = 0;
HAL_UTC_Set(long long ms)459 void HAL_UTC_Set(long long ms)
460 {
461     delta_ms = ms - aos_now_ms();
462 }
463 
HAL_UTC_Get(void)464 long long HAL_UTC_Get(void)
465 {
466     long long ret = aos_now_ms() + delta_ms;
467     return ret;
468 }
469 
get_aos_hex_version(const char * str,unsigned char hex[VERSION_NUM_SIZE])470 int get_aos_hex_version(const char *str, unsigned char hex[VERSION_NUM_SIZE])
471 {
472     char *p = NULL;
473     char *q = NULL;
474     int i = 0;
475     char str_ver[32] = { 0 };
476     if (str == NULL) {
477         return -1;
478     }
479     if (hex == NULL) {
480         return -1;
481     }
482     strncpy(str_ver, str, sizeof(str_ver) - 1);
483     p = strtok(str_ver, "-");
484     for (i = 0; i < 2; i++) {
485         if (p == NULL) {
486             return -1;
487         }
488         p = strtok(NULL, "-");
489     }
490 
491     q = strtok(p, ".");
492     for (i = 0; i < 4; i++) {
493         if (q == NULL) {
494             break;
495         } else {
496             hex[i] = atoi(q);
497         }
498         q = strtok(NULL, ".");
499     }
500     return 0;
501 }
502 
503 /**
504  * 激活使用,提供kernel版本号字节数组
505  */
aos_get_version_hex(unsigned char version[VERSION_NUM_SIZE])506 void aos_get_version_hex(unsigned char version[VERSION_NUM_SIZE])
507 {
508     memset(version, 0, VERSION_NUM_SIZE);
509     get_aos_hex_version(aos_version_get(), version);
510 }
511 
512 /**
513  * 激活使用,提供用字节数组表示mac地址,非字符串
514  */
aos_get_mac_hex(unsigned char mac[MAC_ADDRESS_SIZE])515 void aos_get_mac_hex(unsigned char mac[MAC_ADDRESS_SIZE])
516 {
517     memset(mac, 0, MAC_ADDRESS_SIZE);
518     hal_wifi_get_mac_addr(NULL, mac);
519     // return mac;
520 }
521 
522 /**
523  * 激活使用,提供用字节数组表示芯片ID,非字符串
524  */
aos_get_chip_code(unsigned char chip_code[CHIP_CODE_SIZE])525 void aos_get_chip_code(unsigned char chip_code[CHIP_CODE_SIZE])
526 {
527     memset(chip_code, 0, CHIP_CODE_SIZE);
528     // MCU_ID import by -D option MCU_FAMILY
529     strncpy(chip_code, "test", 4);
530 
531     return;
532 }
533