• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <hi_task.h>
17 #include <string.h>
18 #include <hi_wifi_api.h>
19 #include <hi_mux.h>
20 #include <hi_io.h>
21 #include <hi_gpio.h>
22 #include "iot_config.h"
23 #include "iot_log.h"
24 #include "iot_main.h"
25 #include "iot_profile.h"
26 #include "ohos_init.h"
27 #include "cmsis_os2.h"
28 
29 /* attribute initiative to report */
30 #define TAKE_THE_INITIATIVE_TO_REPORT
31 #define ONE_SECOND                          (1000)
32 /* oc request id */
33 #define CN_COMMAND_INDEX                    "commands/request_id="
34 #define WECHAT_SUBSCRIBE_LIGHT              "light"
35 #define WECHAT_SUBSCRIBE_LIGHT_ON_STATE     "1"
36 #define WECHAT_SUBSCRIBE_LIGHT_OFF_STATE    "0"
37 
38 int g_lightStatus = -1;
39 typedef void (*FnMsgCallBack)(hi_gpio_value val);
40 
41 typedef struct FunctionCallback {
42     hi_bool  stop;
43     hi_u32 conLost;
44     hi_u32 queueID;
45     hi_u32 iotTaskID;
46     FnMsgCallBack    msgCallBack;
47 }FunctionCallback;
48 FunctionCallback g_functionCallback;
49 
50 /* CPU Sleep time Set */
TaskMsleep(unsigned int ms)51 unsigned int TaskMsleep(unsigned int ms)
52 {
53     if (ms <= 0) {
54         return HI_ERR_FAILURE;
55     }
56     return hi_sleep((hi_u32)ms);
57 }
58 
DeviceConfigInit(hi_gpio_value val)59 static void DeviceConfigInit(hi_gpio_value val)
60 {
61     hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_GPIO);
62     hi_gpio_set_dir(HI_GPIO_IDX_9, HI_GPIO_DIR_OUT);
63     hi_gpio_set_ouput_val(HI_GPIO_IDX_9, val);
64 }
65 
DeviceMsgCallback(FnMsgCallBack msgCallBack)66 static int  DeviceMsgCallback(FnMsgCallBack msgCallBack)
67 {
68     g_functionCallback.msgCallBack = msgCallBack;
69     return 0;
70 }
71 
wechatControlDeviceMsg(hi_gpio_value val)72 static void wechatControlDeviceMsg(hi_gpio_value val)
73 {
74     DeviceConfigInit(val);
75 }
76 
77 // < this is the callback function, set to the mqtt, and if any messages come, it will be called
78 // < The payload here is the json string
DemoMsgRcvCallBack(int qos,const char * topic,const char * payload)79 static void DemoMsgRcvCallBack(int qos, const char *topic, const char *payload)
80 {
81     IOT_LOG_DEBUG("RCVMSG:QOS:%d TOPIC:%s PAYLOAD:%s\r\n", qos, topic, payload);
82     /* 云端下发命令后,板端的操作处理 */
83     if (strstr(payload, WECHAT_SUBSCRIBE_LIGHT) != NULL) {
84         if (strstr(payload, WECHAT_SUBSCRIBE_LIGHT_OFF_STATE) != NULL) {
85             wechatControlDeviceMsg(HI_GPIO_VALUE1);
86             g_lightStatus = HI_FALSE;
87         } else {
88             wechatControlDeviceMsg(HI_GPIO_VALUE0);
89             g_lightStatus = HI_TRUE;
90         }
91     }
92     return HI_NULL;
93 }
94 
95 /* publish sample */
IotPublishSample(void)96 hi_void IotPublishSample(void)
97 {
98     /* reported attribute */
99     WeChatProfile weChatProfile = {
100         .subscribeType = "type",
101         .status.subState = "state",
102         .status.subReport = "reported",
103         .status.reportVersion = "version",
104         .status.Token = "clientToken",
105         /* report motor */
106         .reportAction.subDeviceActionMotor = "motor",
107         .reportAction.motorActionStatus = 0, /* 0 : motor off */
108         /* report temperature */
109         .reportAction.subDeviceActionTemperature = "temperature",
110         .reportAction.temperatureData = 30, /* 30 :temperature data */
111         /* report humidity */
112         .reportAction.subDeviceActionHumidity = "humidity",
113         .reportAction.humidityActionData = 70, /* humidity data */
114         /* report light_intensity */
115         .reportAction.subDeviceActionLightIntensity = "light_intensity",
116         .reportAction.lightIntensityActionData = 60, /* 60 : light_intensity */
117     };
118 
119     /* report light */
120     if (g_lightStatus == HI_TRUE) {
121         weChatProfile.reportAction.subDeviceActionLight = "light";
122         weChatProfile.reportAction.lightActionStatus = 1; /* 1: light on */
123     } else if (g_lightStatus == HI_FALSE) {
124         weChatProfile.reportAction.subDeviceActionLight = "light";
125         weChatProfile.reportAction.lightActionStatus = 0; /* 0: light off */
126     } else {
127         weChatProfile.reportAction.subDeviceActionLight = "light";
128         weChatProfile.reportAction.lightActionStatus = 0; /* 0: light off */
129     }
130     /* profile report */
131     IoTProfilePropertyReport(CONFIG_USER_ID, &weChatProfile);
132 }
133 
134 // < this is the demo main task entry,here we will set the wifi/cjson/mqtt ready and
135 // < wait if any work to do in the while
DemoEntry(const char * arg)136 static hi_void *DemoEntry(const char *arg)
137 {
138     WifiStaReadyWait();
139     cJsonInit();
140     IoTMain();
141     /* 云端下发回调 */
142     IoTSetMsgCallback(DemoMsgRcvCallBack);
143     /* 主动上报 */
144 #ifdef TAKE_THE_INITIATIVE_TO_REPORT
145     while (1) {
146         /* 用户可以在这调用发布函数进行发布,需要用户自己写调用函数 */
147         IotPublishSample(); // 发布例程
148 #endif
149         TaskMsleep(ONE_SECOND);
150     }
151     return NULL;
152 }
153 
154 // < This is the demo entry, we create a task here,
155 // and all the works has been done in the demo_entry
156 #define CN_IOT_TASK_STACKSIZE  0x1000
157 #define CN_IOT_TASK_PRIOR 25
158 #define CN_IOT_TASK_NAME "IOTDEMO"
159 
AppDemoIot(void)160 static void AppDemoIot(void)
161 {
162     osThreadAttr_t attr;
163     IoTWatchDogDisable();
164 
165     attr.name = "IOTDEMO";
166     attr.attr_bits = 0U;
167     attr.cb_mem = NULL;
168     attr.cb_size = 0U;
169     attr.stack_mem = NULL;
170     attr.stack_size = CN_IOT_TASK_STACKSIZE;
171     attr.priority = CN_IOT_TASK_PRIOR;
172 
173     if (osThreadNew((osThreadFunc_t)DemoEntry, NULL, &attr) == NULL) {
174         printf("[mqtt] Failed to create IOTDEMO!\n");
175     }
176 }
177 
178 SYS_RUN(AppDemoIot);