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 "iot_config.h"
19 #include "iot_log.h"
20 #include "iot_main.h"
21 #include "iot_profile.h"
22 #include "ohos_init.h"
23 #include "cmsis_os2.h"
24 #include "app_demo_multi_sample.h"
25 #include "app_demo_config.h"
26 #include "wifi_connecter.h"
27
28 /* report traffic light count */
29 unsigned char ocBeepStatus = BEEP_OFF;
30 static int g_iState = 0;
31
32 /* attribute initiative to report */
33 #define TAKE_THE_INITIATIVE_TO_REPORT
34 /* oc request id */
35 #define CN_COMMAND_INDEX "commands/request_id="
36 /* oc report HiSpark attribute */
37 #define IO_FUNC_GPIO_OUT 0
38 #define IOT_GPIO_INDEX_10 10
39 #define IOT_GPIO_INDEX_11 11
40 #define IOT_GPIO_INDEX_12 12
41 #define TRAFFIC_LIGHT_CMD_PAYLOAD "led_value"
42 #define TRAFFIC_LIGHT_CMD_CONTROL_MODE "ControlModule"
43 #define TRAFFIC_LIGHT_CMD_AUTO_MODE "AutoModule"
44 #define TRAFFIC_LIGHT_CMD_HUMAN_MODE "HumanModule"
45 #define TRAFFIC_LIGHT_YELLOW_ON_PAYLOAD "YELLOW_LED_ON"
46 #define TRAFFIC_LIGHT_RED_ON_PAYLOAD "RED_LED_ON"
47 #define TRAFFIC_LIGHT_GREEN_ON_PAYLOAD "GREEN_LED_ON"
48 #define TRAFFIC_LIGHT_SERVICE_ID_PAYLOAD "TrafficLight"
49 #define TRAFFIC_LIGHT_BEEP_CONTROL "BeepControl"
50 #define TRAFFIC_LIGHT_BEEP_ON "BEEP_ON"
51 #define TRAFFIC_LIGHT_BEEP_OFF "BEEP_OFF"
52 #define TRAFFIC_LIGHT_HUMAN_INTERVENTION_ON "HUMAN_MODULE_ON"
53 #define TRAFFIC_LIGHT_HUMAN_INTERVENTION_OFF "HUMAN_MODULE_OFF"
54
55 #define TASK_SLEEP_1000MS (1000)
56
TrafficLightAppOption(HiTrafficLightMode appOptionMode,HiControlModeType appOptionType)57 void TrafficLightAppOption(HiTrafficLightMode appOptionMode, HiControlModeType appOptionType)
58 {
59 unsigned char currentMode = 0;
60
61 currentMode = SetKeyStatus(appOptionMode);
62 switch (GetKeyStatus(CURRENT_MODE)) {
63 case TRAFFIC_CONTROL_MODE:
64 TrafficLightStatusReport(TRAFFIC_CONTROL_MODE, SetupTrflControlModule);
65 break;
66 case TRAFFIC_AUTO_MODE:
67 TrafficLightStatusReport(TRAFFIC_AUTO_MODE, SetupTrflAutoModule);
68 break;
69 case TRAFFIC_HUMAN_MODE:
70 TrafficLightStatusReport(TRAFFIC_HUMAN_MODE, SetupTrflHumanModule);
71 break;
72 default:
73 break;
74 }
75 }
76
TrafficLightMsgRcvCallBack(char * payload)77 static void TrafficLightMsgRcvCallBack(char *payload)
78 {
79 unsigned char currentMode = 0;
80 unsigned char currentType = 0;
81 IOT_LOG_DEBUG("PAYLOAD:%s\r\n", payload);
82 if (strstr(payload, TRAFFIC_LIGHT_CMD_CONTROL_MODE) != NULL) {
83 currentMode = SetKeyStatus(TRAFFIC_CONTROL_MODE);
84 if (strstr(payload, TRAFFIC_LIGHT_YELLOW_ON_PAYLOAD) != NULL) { // YELLOW LED
85 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7, "2.Yellow On ",
86 OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
87 GpioControl(IOT_GPIO_INDEX_10, IOT_GPIO_INDEX_10,
88 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE0, IO_FUNC_GPIO_OUT);
89 GpioControl(IOT_GPIO_INDEX_12, IOT_GPIO_INDEX_12,
90 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE1, IO_FUNC_GPIO_OUT);
91 } else if (strstr(payload, TRAFFIC_LIGHT_RED_ON_PAYLOAD) != NULL) { // RED LED
92 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7, "1.Red On ",
93 OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
94 GpioControl(IOT_GPIO_INDEX_10, IOT_GPIO_INDEX_10,
95 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE1, IO_FUNC_GPIO_OUT);
96 GpioControl(IOT_GPIO_INDEX_11, IOT_GPIO_INDEX_11,
97 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE0, IO_FUNC_GPIO_OUT);
98 } else if (strstr(payload, TRAFFIC_LIGHT_GREEN_ON_PAYLOAD) != NULL) { // GREEN LED
99 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7, "3.Green On ",
100 OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
101 GpioControl(IOT_GPIO_INDEX_12, IOT_GPIO_INDEX_12,
102 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE0, IO_FUNC_GPIO_OUT);
103 GpioControl(IOT_GPIO_INDEX_11, IOT_GPIO_INDEX_11,
104 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE1, IO_FUNC_GPIO_OUT);
105 }
106 TrafficLightAppOption(currentMode, currentType);
107 } else if (strstr(payload, TRAFFIC_LIGHT_CMD_AUTO_MODE) != NULL) { // Auto module
108 currentMode = SetKeyStatus(TRAFFIC_AUTO_MODE);
109 TrafficLightAppOption(currentMode, currentType);
110 } else if (strstr(payload, TRAFFIC_LIGHT_CMD_HUMAN_MODE) != NULL) { // Human module
111 currentMode = SetKeyStatus(TRAFFIC_HUMAN_MODE);
112 if (strstr(payload, TRAFFIC_LIGHT_HUMAN_INTERVENTION_ON) != NULL) {
113 currentType = SetKeyType(TRAFFIC_HUMAN_TYPE);
114 } else if (strstr(payload, TRAFFIC_LIGHT_HUMAN_INTERVENTION_OFF)) {
115 currentType = SetKeyType(TRAFFIC_NORMAL_TYPE);
116 }
117 TrafficLightAppOption(currentMode, currentType);
118 } else if (strstr(payload, TRAFFIC_LIGHT_BEEP_CONTROL) != NULL) { // BEEP option
119 if (strstr(payload, TRAFFIC_LIGHT_BEEP_ON) != NULL) { // BEEP ON
120 ocBeepStatus = BEEP_ON;
121 } else if (strstr(payload, TRAFFIC_LIGHT_BEEP_OFF) != NULL) { // BEEP OFF
122 ocBeepStatus = BEEP_OFF;
123 }
124 }
125 }
126
127 // /< this is the callback function, set to the mqtt, and if any messages come, it will be called
128 // /< The payload here is the json string
DemoMsgRcvCallBack(int qos,const char * topic,char * payload)129 static void DemoMsgRcvCallBack(int qos, const char *topic, char *payload)
130 {
131 const char *requesID;
132 char *tmp;
133 IoTCmdResp resp;
134 IOT_LOG_DEBUG("RCVMSG:QOS:%d TOPIC:%s PAYLOAD:%s\r\n", qos, topic, payload);
135 /* app 下发的操作 */
136 TrafficLightMsgRcvCallBack(payload);
137 tmp = strstr(topic, CN_COMMAND_INDEX);
138 if (tmp != NULL) {
139 // /< now you could deal your own works here --THE COMMAND FROM THE PLATFORM
140 // /< now er roport the command execute result to the platform
141 requesID = tmp + strlen(CN_COMMAND_INDEX);
142 resp.requestID = requesID;
143 resp.respName = NULL;
144 resp.retCode = 0; ////< which means 0 success and others failed
145 resp.paras = NULL;
146 (void)IoTProfileCmdResp(CONFIG_DEVICE_PWD, &resp);
147 }
148 return;
149 }
150
SetupCleanTrflStatus(HiTrafficLightMode earlyMode)151 void SetupCleanTrflStatus(HiTrafficLightMode earlyMode)
152 {
153 IoTProfileService service;
154 IoTProfileKV property;
155 if (earlyMode == TRAFFIC_CONTROL_MODE) {
156 memset_s(&property, sizeof(property), 0, sizeof(property));
157 property.type = EN_IOT_DATATYPE_STRING;
158 property.key = "HumanModule";
159 property.value = "OFF";
160 memset_s(&service, sizeof(service), 0, sizeof(service));
161 service.serviceID = "TrafficLight";
162 service.serviceProperty = &property;
163 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
164 } else if (earlyMode == TRAFFIC_AUTO_MODE) {
165 memset_s(&property, sizeof(property), 0, sizeof(property));
166 property.type = EN_IOT_DATATYPE_STRING;
167 property.key = "ControlModule";
168 property.value = "OFF";
169 memset_s(&service, sizeof(service), 0, sizeof(service));
170 service.serviceID = "TrafficLight";
171 service.serviceProperty = &property;
172 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
173 } else if (earlyMode == TRAFFIC_HUMAN_MODE) {
174 memset_s(&property, sizeof(property), 0, sizeof(property));
175 property.type = EN_IOT_DATATYPE_STRING;
176 property.key = "AutoModule";
177 property.value = "OFF";
178 memset_s(&service, sizeof(service), 0, sizeof(service));
179 service.serviceID = "TrafficLight";
180 service.serviceProperty = &property;
181 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
182 }
183 }
184 /* traffic light:1.control module */
SetupTrflControlModule(HiTrafficLightMode currentMode,HiControlModeType currentType)185 void SetupTrflControlModule(HiTrafficLightMode currentMode, HiControlModeType currentType)
186 {
187 IoTProfileService service;
188 IoTProfileKV property;
189 unsigned char status = 0;
190
191 printf("traffic light:control module\r\n");
192 if (currentMode != TRAFFIC_CONTROL_MODE && currentType != RED_ON) {
193 printf("select current module is not the TRAFFIC_CONTROL_MODE\r\n");
194 return HI_NULL;
195 }
196 status = SetKeyStatus(TRAFFIC_CONTROL_MODE);
197
198 memset_s(&property, sizeof(property), 0, sizeof(property));
199 property.type = EN_IOT_DATATYPE_STRING;
200 property.key = "ControlModule";
201 if (currentType == RED_ON) {
202 property.value = "RED_LED_ON";
203 } else if (currentType == YELLOW_ON) {
204 property.value = "YELLOW_LED_ON";
205 } else if (currentType == GREEN_ON) {
206 property.value = "GREEN_LED_ON";
207 }
208 memset_s(&service, sizeof(service), 0, sizeof(service));
209 service.serviceID = "TrafficLight";
210 service.serviceProperty = &property;
211 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
212 /* report beep status */
213 memset_s(&property, sizeof(property), 0, sizeof(property));
214 property.type = EN_IOT_DATATYPE_STRING;
215 property.key = "AutoModule";
216 if (ocBeepStatus == BEEP_ON) {
217 property.value = "BEEP_ON";
218 } else {
219 property.value = "BEEP_OFF";
220 }
221 memset_s(&service, sizeof(service), 0, sizeof(service));
222 service.serviceID = "TrafficLight";
223 service.serviceProperty = &property;
224 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
225 }
226 /* report light time count */
ReportLedLightTimeCount(void)227 void ReportLedLightTimeCount(void)
228 {
229 IoTProfileService service;
230 IoTProfileKV property;
231 /* report red led light time count */
232 memset_s(&property, sizeof(property), 0, sizeof(property));
233 property.type = EN_IOT_DATATYPE_INT;
234 property.key = "AutoModuleRLedTC";
235 property.iValue = GetLedStatus(RED_LED_AUTOMODE_TIMECOUNT);
236 memset_s(&service, sizeof(service), 0, sizeof(service));
237 service.serviceID = "TrafficLight";
238 service.serviceProperty = &property;
239 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
240 /* report yellow led light time count */
241 memset_s(&property, sizeof(property), 0, sizeof(property));
242 property.type = EN_IOT_DATATYPE_INT;
243 property.key = "AutoModuleYLedTC";
244 property.iValue = GetLedStatus(YELLOW_LED_AUTOMODE_TIMECOUNT) ;
245 memset_s(&service, sizeof(service), 0, sizeof(service));
246 service.serviceID = "TrafficLight";
247 service.serviceProperty = &property;
248 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
249 /* report green led light time count */
250 memset_s(&property, sizeof(property), 0, sizeof(property));
251 property.type = EN_IOT_DATATYPE_INT;
252 property.key = "AutoModuleGLedTC";
253 property.iValue = GetLedStatus(GREEN_LED_AUTOMODE_TIMECOUNT);
254 memset_s(&service, sizeof(service), 0, sizeof(service));
255 service.serviceID = "TrafficLight";
256 service.serviceProperty = &property;
257 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
258 }
259 /* traffic light:2.auto module */
SetupTrflAutoModule(HiTrafficLightMode currentMode,HiControlModeType currentType)260 void SetupTrflAutoModule(HiTrafficLightMode currentMode, HiControlModeType currentType)
261 {
262 IoTProfileService service;
263 IoTProfileKV property;
264 unsigned char status = 0;
265
266 printf("traffic light:auto module\r\n");
267 if (currentMode != TRAFFIC_AUTO_MODE) {
268 printf("select current module is not the CONTROL_MODE\r\n");
269 return HI_NULL;
270 }
271 /* report beep status */
272 status = SetKeyStatus(TRAFFIC_AUTO_MODE);
273 memset_s(&property, sizeof(property), 0, sizeof(property));
274 property.type = EN_IOT_DATATYPE_STRING;
275 property.key = "AutoModule";
276 if (ocBeepStatus == BEEP_ON) {
277 property.value = "BEEP_ON";
278 } else {
279 property.value = "BEEP_OFF";
280 }
281 memset_s(&service, sizeof(service), 0, sizeof(service));
282 service.serviceID = "TrafficLight";
283 service.serviceProperty = &property;
284 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
285 /* report light time count */
286 ReportLedLightTimeCount();
287 }
288
289 /* traffic light:3.human module */
SetupTrflHumanModule(HiTrafficLightMode currentMode,HiControlModeType currentType)290 void SetupTrflHumanModule(HiTrafficLightMode currentMode, HiControlModeType currentType)
291 {
292 IoTProfileService service;
293 IoTProfileKV property;
294 unsigned char status = 0;
295
296 printf("traffic light:human module\r\n");
297 if (currentMode != TRAFFIC_HUMAN_MODE) {
298 printf("select current module is not the CONTROL_MODE\r\n");
299 return HI_NULL;
300 }
301 status = GetKeyStatus(TRAFFIC_HUMAN_MODE);
302 memset_s(&property, sizeof(property), 0, sizeof(property));
303 property.type = EN_IOT_DATATYPE_STRING;
304 property.key = "HumanModule";
305 if (ocBeepStatus == BEEP_ON) {
306 property.value = "BEEP_ON";
307 } else {
308 property.value = "BEEP_OFF";
309 }
310 memset_s(&service, sizeof(service), 0, sizeof(service));
311 service.serviceID = "TrafficLight";
312 service.serviceProperty = &property;
313 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
314
315 /* red led light time count */
316 memset_s(&property, sizeof(property), 0, sizeof(property));
317 property.type = EN_IOT_DATATYPE_INT;
318 property.key = "HumanModuleRledTC";
319 property.iValue = GetLedStatus(RED_LED_HUMANMODE_TIMECOUNT);
320 memset_s(&service, sizeof(service), 0, sizeof(service));
321 service.serviceID = "TrafficLight";
322 service.serviceProperty = &property;
323 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
324 /* yellow led light time count */
325 memset_s(&property, sizeof(property), 0, sizeof(property));
326 property.type = EN_IOT_DATATYPE_INT;
327 property.key = "HumanModuleYledTC";
328 property.iValue = GetLedStatus(YELLOW_LED_HUMANMODE_TIMECOUNT);
329 memset_s(&service, sizeof(service), 0, sizeof(service));
330 service.serviceID = "TrafficLight";
331 service.serviceProperty = &property;
332 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
333 /* green led light time count */
334 memset_s(&property, sizeof(property), 0, sizeof(property));
335 property.type = EN_IOT_DATATYPE_INT;
336 property.key = "HumanModuleGledTC";
337 property.iValue = GetLedStatus(GREEN_LED_HUMANMODE_TIMECOUNT);
338 memset_s(&service, sizeof(service), 0, sizeof(service));
339 service.serviceID = "TrafficLight";
340 service.serviceProperty = &property;
341 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
342 }
343
TrafficLightStatusReport(HiTrafficLightMode currentMode,const TrflCallBackFunc msgReport)344 void TrafficLightStatusReport(HiTrafficLightMode currentMode, const TrflCallBackFunc msgReport)
345 {
346 printf("traffic light: reporting status...\r\n");
347 switch (currentMode) {
348 case TRAFFIC_CONTROL_MODE:
349 msgReport(TRAFFIC_CONTROL_MODE, NULL);
350 break;
351 case TRAFFIC_AUTO_MODE:
352 msgReport(TRAFFIC_AUTO_MODE, NULL);
353 break;
354 case TRAFFIC_HUMAN_MODE:
355 msgReport(TRAFFIC_HUMAN_MODE, NULL);
356 break;
357 default:
358 break;
359 }
360 return HI_NULL;
361 }
362
ReportTrafficLightMsg(void)363 static void ReportTrafficLightMsg(void)
364 {
365 switch (GetKeyStatus(CURRENT_MODE)) {
366 case TRAFFIC_CONTROL_MODE:
367 TrafficLightStatusReport(TRAFFIC_CONTROL_MODE, SetupTrflControlModule);
368 break;
369 case TRAFFIC_AUTO_MODE:
370 TrafficLightStatusReport(TRAFFIC_AUTO_MODE, SetupTrflAutoModule);
371 break;
372 case TRAFFIC_HUMAN_MODE:
373 TrafficLightStatusReport(TRAFFIC_HUMAN_MODE, SetupTrflHumanModule);
374 break;
375 default:
376 break;
377 }
378 }
379 ///< this is the demo main task entry,here we will set the wifi/cjson/mqtt ready ,and
380 ///< wait if any work to do in the while
DemoEntry(const char * arg)381 static void *DemoEntry(const char *arg)
382 {
383 hi_watchdog_disable();
384 WifiStaReadyWait();
385 CJsonInit();
386 printf("cJsonInit init \r\n");
387 IoTMain();
388 IoTSetMsgCallback(DemoMsgRcvCallBack);
389 /* 主动上报 */
390 #ifdef TAKE_THE_INITIATIVE_TO_REPORT
391 while (1) {
392 // /< here you could add your own works here--we report the data to the IoTplatform
393 hi_sleep(TASK_SLEEP_1000MS);
394 // /< now we report the data to the iot platform
395 ReportTrafficLightMsg();
396 if (g_iState == 0xffff) {
397 g_iState = 0;
398 break;
399 }
400 }
401 #endif
402 }
403
404 ///< This is the demo entry, we create a task here, and all the works has been done in the demo_entry
405 #define CN_IOT_TASK_STACKSIZE 0x1000
406 #define CN_IOT_TASK_PRIOR 28
407 #define CN_IOT_TASK_NAME "IOTDEMO"
AppDemoIot(void)408 static void AppDemoIot(void)
409 {
410 osThreadAttr_t attr;
411 IoTWatchDogDisable();
412
413 attr.name = "IOTDEMO";
414 attr.attr_bits = 0U;
415 attr.cb_mem = NULL;
416 attr.cb_size = 0U;
417 attr.stack_mem = NULL;
418 attr.stack_size = CN_IOT_TASK_STACKSIZE;
419 attr.priority = CN_IOT_TASK_PRIOR;
420
421 if (osThreadNew((osThreadFunc_t)DemoEntry, NULL, &attr) == NULL) {
422 printf("[TrafficLight] Failed to create IOTDEMO!\n");
423 }
424 }
425
426 SYS_RUN(AppDemoIot);