1 /*
2 * Copyright (C) 2018-2020 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8 //#include <aos/aos.h>
9 #include <aos/kernel.h>
10
11 #include <misc/printk.h>
12 #include <hal/hal.h>
13
14 #include "genie_service.h"
15
16 static genie_service_ctx_t genie_service_ctx;
17
genie_service_check_ctx(genie_service_ctx_t * p_ctx)18 static int genie_service_check_ctx(genie_service_ctx_t *p_ctx)
19 {
20 if (!p_ctx || !p_ctx->event_cb || !p_ctx->p_mesh_elem || p_ctx->mesh_elem_counts < 1)
21 {
22 GENIE_LOG_ERR("ctx:%p %p %p %d", p_ctx, p_ctx->event_cb, p_ctx->p_mesh_elem, p_ctx->mesh_elem_counts);
23 return -1;
24 }
25
26 memset(&genie_service_ctx, 0, sizeof(genie_service_ctx_t));
27 memcpy(&genie_service_ctx, p_ctx, sizeof(genie_service_ctx_t));
28
29 return 0;
30 }
31
genie_service_get_context(void)32 genie_service_ctx_t *genie_service_get_context(void)
33 {
34 return &genie_service_ctx;
35 }
36
genie_service_init(genie_service_ctx_t * p_ctx)37 int genie_service_init(genie_service_ctx_t *p_ctx)
38 {
39 int ret = 0;
40
41 GENIE_LOG_ERR("genie service init");
42 if (genie_service_check_ctx(p_ctx) < 0)
43 {
44 GENIE_LOG_ERR("genie service context error");
45 return GENIE_SERVICE_ERRCODE_USER_INPUT;
46 }
47
48 genie_storage_init();
49
50 genie_cli_init();
51
52 #if defined(CONIFG_GENIE_MESH_BINARY_CMD) || defined(CONFIG_GENIE_MESH_AT_CMD)
53 genie_sal_uart_init();
54 #endif
55
56 genie_reset_init();
57
58 genie_transport_init();
59
60 #ifdef CONFIG_GENIE_MESH_GLP
61 p_ctx->lpm_conf.sleep_ms = GENIE_GLP_SLEEP_TIME;
62 p_ctx->lpm_conf.wakeup_ms = GENIE_GLP_WAKEUP_TIME;
63 #endif
64
65 #ifdef CONFIG_PM_SLEEP
66 genie_lpm_init(&p_ctx->lpm_conf);
67 #endif
68
69 if (genie_triple_init() < 0)
70 {
71 #ifdef CONFIG_PM_SLEEP
72 genie_sal_sleep_disable();
73 #endif
74 return GENIE_SERVICE_ERRCODE_NO_TRIPLE;
75 }
76
77 ret = genie_mesh_init(genie_service_ctx.p_mesh_elem, genie_service_ctx.mesh_elem_counts);
78 if (ret != 0)
79 {
80 GENIE_LOG_ERR("genie service init fail(%d)", ret);
81 return GENIE_SERVICE_ERRCODE_MESH_INIT;
82 }
83 else
84 {
85 return GENIE_SERVICE_SUCCESS;
86 }
87 }
88