1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 #include "dev_bind_internal.h"
5 
6 /****** Convert values between host and big-/little-endian byte order ******/
7 
8 /* reverse byte order */
reverse_16bit(uint16_t data)9 static uint16_t reverse_16bit(uint16_t data)
10 {
11     return (data >> 8) | (data << 8);
12 }
13 
14 /* host byte order to big endian */
os_htobe16(uint16_t data)15 uint16_t os_htobe16(uint16_t data)
16 {
17     if (os_is_big_endian()) {
18         return data;
19     }
20 
21     return reverse_16bit(data);
22 }
23 
24 /* host byte order to little endian */
os_htole16(uint16_t data)25 uint16_t os_htole16(uint16_t data)
26 {
27     if (os_is_big_endian()) {
28         return reverse_16bit(data);
29     }
30 
31     return data;
32 }
33 
34 /* big endian to host byte order */
os_be16toh(uint16_t data)35 uint16_t os_be16toh(uint16_t data)
36 {
37     return os_htobe16(data);
38 }
39 
40 /* little endian to host byte order */
os_le16toh(uint16_t data)41 uint16_t os_le16toh(uint16_t data)
42 {
43     return os_htole16(data);
44 }
45 
46 /* get unaligned data in big endian. */
os_get_unaligned_be16(uint8_t * ptr)47 uint16_t os_get_unaligned_be16(uint8_t *ptr)
48 {
49     uint16_t res;
50 
51     memcpy(&res, ptr, sizeof(uint16_t));
52 
53     return os_be16toh(res);
54 }
55 
56 /* get unaligned data in little endian. */
os_get_unaligned_le16(uint8_t * ptr)57 uint16_t os_get_unaligned_le16(uint8_t *ptr)
58 {
59     uint16_t res;
60 
61     memcpy(&res, ptr, sizeof(uint16_t));
62 
63     return os_le16toh(res);
64 }
65 
66 /* format mac string uppercase */
os_wifi_get_mac_str(char mac_str[OS_MAC_LEN])67 char *os_wifi_get_mac_str(char mac_str[OS_MAC_LEN])
68 {
69     char *str;
70     int colon_num = 0, i;
71 
72     str = HAL_Wifi_Get_Mac(mac_str);
73 
74     /* sanity check */
75     while (str) {
76         str = strchr(str, ':');
77         if (str) {
78             colon_num++;
79             str++; /* eating char ':' */
80         }
81     }
82 
83     /* convert to capital letter */
84     for (i = 0; i < OS_MAC_LEN && mac_str[i]; i++) {
85         if ('a' <= mac_str[i] && mac_str[i] <= 'z') {
86             mac_str[i] -= 'a' - 'A';
87         }
88     }
89 
90     return mac_str;
91 }
os_wifi_str2mac(char mac_str[OS_MAC_LEN],char mac[OS_ETH_ALEN])92 char *os_wifi_str2mac(char mac_str[OS_MAC_LEN], char mac[OS_ETH_ALEN])
93 {
94     int i = 0;
95     char *ptr = mac_str;
96     char mac_addr[OS_ETH_ALEN] = { 0 };
97 
98     if (ptr == NULL)
99         return NULL;
100 
101     while (isxdigit(*ptr) && i < OS_ETH_ALEN) {
102         mac_addr[i++] = (uint8_t)strtol(ptr, &ptr, 16);
103         ++ptr;
104     }
105 
106     if (i < OS_ETH_ALEN) /* don't touch mac when fail */
107         return NULL;
108 
109     if (mac)
110         memcpy(mac, mac_addr, OS_ETH_ALEN);
111 
112     return mac;
113 }
114 
os_wifi_get_mac(uint8_t mac[OS_ETH_ALEN])115 uint8_t *os_wifi_get_mac(uint8_t mac[OS_ETH_ALEN])
116 {
117     char mac_str[OS_MAC_LEN] = { 0 };
118 
119     os_wifi_get_mac_str(mac_str);
120 
121     return (uint8_t *)os_wifi_str2mac(mac_str, (char *)mac);
122 }
123 
awss_zalloc(uint32_t size)124 void *awss_zalloc(uint32_t size)
125 {
126     void *ptr = HAL_Malloc(size);
127     if (ptr != NULL) {
128         memset(ptr, 0, size);
129     }
130     return ptr;
131 }
132 
os_get_time_ms(void)133 uint32_t os_get_time_ms(void)
134 {
135     static uint32_t fixed_delta;
136 
137     if (!fixed_delta) {
138         fixed_delta = (uint32_t)HAL_UptimeMs() - 0xFFFF0000;
139     }
140 
141     /* add a big offset, for easier caught time overflow bug */
142     return (uint32_t)HAL_UptimeMs() - fixed_delta;
143 }
144 
time_elapsed_ms_since(uint32_t start_timestamp)145 uint32_t time_elapsed_ms_since(uint32_t start_timestamp)
146 {
147     uint32_t now = os_get_time_ms();
148     return now - start_timestamp;
149 }
150 
os_is_big_endian(void)151 int os_is_big_endian(void)
152 {
153     uint32_t data = 0xFF000000;
154 
155     if (0xFF == *(uint8_t *)&data) {
156         return 1; /* big endian */
157     }
158 
159     return 0; /* little endian */
160 }
161