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 <string.h>
17 #include <hi_wifi_api.h>
18 #include <hi_mux.h>
19 #include <hi_task.h>
20 #include "ohos_init.h"
21 #include "cmsis_os2.h"
22 #include "iot_config.h"
23 #include "iot_log.h"
24 #include "iot_main.h"
25 #include "iot_profile.h"
26
27 /* attribute initiative to report */
28 #define TAKE_THE_INITIATIVE_TO_REPORT
29 /* oc request id */
30 #define CN_COMMAND_INDEX "commands/request_id="
31
32 // this is the callback function, set to the mqtt, and if any messages come, it will be called
33 // The payload here is the json string
DemoMsgRcvCallBack(int qos,const char * topic,const char * payload)34 static void DemoMsgRcvCallBack(int qos, const char *topic, const char *payload)
35 {
36 const char *requesID;
37 char *tmp;
38 IoTCmdRespT resp;
39 IOT_LOG_DEBUG("RCVMSG:QOS:%d TOPIC:%s PAYLOAD:%s\r\n", qos, topic, payload);
40
41 tmp = strstr(topic, CN_COMMAND_INDEX);
42 if (tmp != NULL) {
43 // <now you could deal your own works here --THE COMMAND FROM THE PLATFORM
44 // <now er roport the command execute result to the platform
45 requesID = tmp + strlen(CN_COMMAND_INDEX);
46 resp.requestID = requesID;
47 resp.respName = NULL;
48 resp.retCode = 0; // <which means 0 success and others failed
49 resp.paras = NULL;
50 (void)IoTProfileCmdResp(CONFIG_DEVICE_PWD, &resp);
51 }
52 return;
53 }
54
55 /* Smart Can */
IotPublishPersonTime(hi_u32 time)56 hi_void IotPublishPersonTime(hi_u32 time)
57 {
58 IoTProfileServiceT service;
59 IoTProfileKVT property;
60
61 memset_s(&property, sizeof(property), 0, sizeof(property));
62 property.type = EN_IOT_DATATYPE_INT;
63 property.key = "Person_times";
64
65 property.iValue = time;
66
67 memset_s(&service, sizeof(service), 0, sizeof(service));
68 service.serviceID = "helloMQTT";
69 service.serviceProperty = &property;
70
71 IoTProfilePropertyReport(CONFIG_DEVICE_ID, &service);
72 }
73
74 // this is the demo main task entry,here we will set the wifi/cjson/mqtt ready ,and
75 // wait if any work to do in the while
DemoEntry(hi_void * arg)76 static hi_void *DemoEntry(hi_void *arg)
77 {
78 WifiStaReadyWait();
79 }
80
81 // This is the demo entry, we create a task here, and all the works has been done in the demo_entry
82 #define CN_IOT_TASK_STACKSIZE 0x1000
83 #define CN_IOT_TASK_PRIOR 25
84 #define CN_IOT_TASK_NAME "IOTDEMO"
85
AppDemoIot(void)86 static void AppDemoIot(void)
87 {
88 osThreadAttr_t attr;
89 IoTWatchDogDisable();
90
91 attr.name = "IOTDEMO";
92 attr.attr_bits = 0U;
93 attr.cb_mem = NULL;
94 attr.cb_size = 0U;
95 attr.stack_mem = NULL;
96 attr.stack_size = CN_IOT_TASK_STACKSIZE;
97 attr.priority = CN_IOT_TASK_PRIOR;
98
99 if (osThreadNew((osThreadFunc_t)DemoEntry, NULL, &attr) == NULL) {
100 printf("[TrafficLight] Failed to create IOTDEMO!\n");
101 }
102 }
103
104 SYS_RUN(AppDemoIot);