1 #include "core_global.h"
2 
3 typedef struct {
4     void *mutex;
5     uint8_t is_inited;
6     uint32_t used_count;
7     int32_t alink_id;
8     char mqtt_backup_ip[16];
9 } g_core_global_t;
10 
11 g_core_global_t g_core_global = {NULL, 0, 0, 0, {0}};
12 
core_global_init(aiot_sysdep_portfile_t * sysdep)13 int32_t core_global_init(aiot_sysdep_portfile_t *sysdep)
14 {
15     if (g_core_global.is_inited == 1) {
16         g_core_global.used_count++;
17         return STATE_SUCCESS;
18     }
19     g_core_global.is_inited = 1;
20 
21 
22     g_core_global.mutex = sysdep->core_sysdep_mutex_init();
23     g_core_global.used_count++;
24 
25     return STATE_SUCCESS;
26 }
27 
core_global_alink_id_next(aiot_sysdep_portfile_t * sysdep,int32_t * alink_id)28 int32_t core_global_alink_id_next(aiot_sysdep_portfile_t *sysdep, int32_t *alink_id)
29 {
30     int32_t id = 0;
31     sysdep->core_sysdep_mutex_lock(g_core_global.mutex);
32     g_core_global.alink_id++;
33     if (g_core_global.alink_id < 0) {
34         g_core_global.alink_id = 0;
35     }
36     id = g_core_global.alink_id;
37     sysdep->core_sysdep_mutex_unlock(g_core_global.mutex);
38 
39     *alink_id = id;
40     return STATE_SUCCESS;
41 }
42 
core_global_set_mqtt_backup_ip(aiot_sysdep_portfile_t * sysdep,char ip[16])43 int32_t core_global_set_mqtt_backup_ip(aiot_sysdep_portfile_t *sysdep, char ip[16])
44 {
45     int cpy_len = strlen(ip) > 15 ? 15 : strlen(ip);
46     sysdep->core_sysdep_mutex_lock(g_core_global.mutex);
47     memset(g_core_global.mqtt_backup_ip, 0, 16);
48     memcpy(g_core_global.mqtt_backup_ip, ip, cpy_len);
49     sysdep->core_sysdep_mutex_unlock(g_core_global.mutex);
50 
51     return STATE_SUCCESS;
52 }
53 
core_global_get_mqtt_backup_ip(aiot_sysdep_portfile_t * sysdep,char ip[16])54 int32_t core_global_get_mqtt_backup_ip(aiot_sysdep_portfile_t *sysdep, char ip[16])
55 {
56     sysdep->core_sysdep_mutex_lock(g_core_global.mutex);
57     memcpy(ip, g_core_global.mqtt_backup_ip, strlen(g_core_global.mqtt_backup_ip));
58     sysdep->core_sysdep_mutex_unlock(g_core_global.mutex);
59 
60     return STATE_SUCCESS;
61 }
62 
core_global_deinit(aiot_sysdep_portfile_t * sysdep)63 int32_t core_global_deinit(aiot_sysdep_portfile_t *sysdep)
64 {
65     if (g_core_global.used_count > 0) {
66         g_core_global.used_count--;
67     }
68 
69     if (g_core_global.used_count != 0) {
70         return STATE_SUCCESS;
71     }
72     sysdep->core_sysdep_mutex_deinit(&g_core_global.mutex);
73 
74     g_core_global.mutex = NULL;
75     g_core_global.is_inited = 0;
76     g_core_global.used_count = 0;
77 
78     return STATE_SUCCESS;
79 }
80 
81