forked from open-degu/degu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moddegu.c
125 lines (102 loc) · 3.4 KB
/
moddegu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <errno.h>
#include <zephyr.h>
#include "py/nlr.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "py/binary.h"
#include <stdio.h>
#include <string.h>
#include <net/coap.h>
#include <power.h>
#include <net/net_if.h>
#include <net/openthread.h>
#include <openthread/thread.h>
#include "zcoap.h"
#include "degu_utils.h"
#include "degu_ota.h"
#include "degu_pm.h"
STATIC mp_obj_t degu_check_update(void) {
return mp_obj_new_int(check_update());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(degu_check_update_obj, degu_check_update);
STATIC mp_obj_t degu_update_shadow(mp_obj_t shadow) {
int ret = degu_coap_request("thing", COAP_METHOD_POST, (u8_t *)mp_obj_str_get_str(shadow), NULL);
return mp_obj_new_int(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(degu_update_shadow_obj, degu_update_shadow);
STATIC mp_obj_t degu_get_shadow(void) {
vstr_t vstr;
int ret;
u8_t *payload = (u8_t *)m_malloc(MAX_COAP_MSG_LEN);
if (!payload) {
printf("can't malloc\n");
return mp_const_none;
}
memset(payload, 0, MAX_COAP_MSG_LEN);
ret = degu_coap_request("thing", COAP_METHOD_GET, payload, NULL);
if (payload != NULL && ret >= COAP_RESPONSE_CODE_OK) {
vstr_init_len(&vstr, strlen(payload));
strcpy(vstr.buf, payload);
m_free(payload);
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
}
else {
m_free(payload);
return mp_const_none;
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(degu_get_shadow_obj, degu_get_shadow);
STATIC mp_obj_t mod_suspend(mp_obj_t time_sec)
{
s32_t time_to_wake = mp_obj_get_int(time_sec);
struct net_if *iface;
struct openthread_context *ot_context;
otLinkModeConfig config;
uint8_t channel;
iface = net_if_get_default();
ot_context = net_if_l2_data(iface);
channel = otLinkGetChannel(ot_context->instance);
config = otThreadGetLinkMode(ot_context->instance);
#ifdef CONFIG_SYS_POWER_MANAGEMENT
sys_pm_ctrl_enable_state(SYS_POWER_STATE_SLEEP_3);
sys_set_power_state(SYS_POWER_STATE_SLEEP_3);
#endif
openthread_suspend(ot_context->instance);
k_sleep(K_SECONDS(time_to_wake));
openthread_resume(ot_context->instance, channel, config);
#ifdef CONFIG_SYS_POWER_MANAGEMENT
sys_pm_ctrl_disable_state(SYS_POWER_STATE_SLEEP_3);
#endif
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_suspend_obj, mod_suspend);
STATIC mp_obj_t mod_powerdown(void)
{
degu_ext_device_power(false);
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
sys_pm_suspend_devices();
#endif
#ifdef CONFIG_SYS_POWER_MANAGEMENT
sys_pm_ctrl_enable_state(SYS_POWER_STATE_DEEP_SLEEP_1);
sys_set_power_state(SYS_POWER_STATE_DEEP_SLEEP_1);
sys_pm_ctrl_disable_state(SYS_POWER_STATE_DEEP_SLEEP_1);
#endif
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
sys_pm_resume_devices();
#endif
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_powerdown_obj, mod_powerdown);
STATIC const mp_rom_map_elem_t degu_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_degu) },
{ MP_ROM_QSTR(MP_QSTR_check_update), MP_ROM_PTR(°u_check_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_update_shadow), MP_ROM_PTR(°u_update_shadow_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_shadow), MP_ROM_PTR(°u_get_shadow_obj) },
{ MP_ROM_QSTR(MP_QSTR_suspend), MP_ROM_PTR(&mod_suspend_obj) },
{ MP_ROM_QSTR(MP_QSTR_powerdown), MP_ROM_PTR(&mod_powerdown_obj) },
};
STATIC MP_DEFINE_CONST_DICT (mp_module_degu_globals, degu_globals_table);
const mp_obj_module_t mp_module_degu = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_degu_globals,
};