• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 #include "cmsis_os2.h"
23 #include "ohos_init.h"
24 #include "lwip/sockets.h"
25 #include "wifi_connect.h"
26 
27 #include "E53_IA1.h"
28 #include "oc_mqtt.h"
29 
30 #define MSGQUEUE_COUNT 16 // number of Message Queue Objects
31 #define MSGQUEUE_SIZE 10
32 #define CLOUD_TASK_STACK_SIZE (1024 * 10)
33 #define CLOUD_TASK_PRIO 24
34 #define SENSOR_TASK_STACK_SIZE (1024 * 2)
35 #define SENSOR_TASK_PRIO 25
36 #define TASK_DELAY_3S 3
37 
38 typedef struct { // object data type
39     char *Buf;
40     uint8_t Idx;
41 } MSGQUEUE_OBJ_t;
42 
43 MSGQUEUE_OBJ_t msg;
44 osMessageQueueId_t mid_MsgQueue; // message queue id
45 
46 #define CLIENT_ID "6114182f2cce4f0286ff45f9_dsdsd_0_0_2021122901"
47 #define USERNAME "6114182f2cce4f0286ff45f9_dsdsd"
48 #define PASSWORD "6d795e8252d1e53e164ea759563bdef2174e9b4ae91036002ebb5097a9479598"
49 
50 typedef enum {
51     en_msg_cmd = 0,
52     en_msg_report,
53 } en_msg_type_t;
54 
55 typedef struct {
56     char *request_id;
57     char *payload;
58 } cmd_t;
59 
60 typedef struct {
61     int lum;
62     int temp;
63     int hum;
64 } report_t;
65 
66 typedef struct {
67     en_msg_type_t msg_type;
68     union {
69         cmd_t cmd;
70         report_t report;
71     } msg;
72 } app_msg_t;
73 
74 typedef struct {
75     int connected;
76     int led;
77     int motor;
78 } app_cb_t;
79 static app_cb_t g_app_cb;
80 
ReportMsg(report_t * report)81 static void ReportMsg(report_t *report)
82 {
83     oc_mqtt_profile_service_t service;
84     oc_mqtt_profile_kv_t temperature;
85     oc_mqtt_profile_kv_t humidity;
86     oc_mqtt_profile_kv_t luminance;
87     oc_mqtt_profile_kv_t led;
88     oc_mqtt_profile_kv_t motor;
89 
90     service.event_time = NULL;
91     service.service_id = "Agriculture";
92     service.service_property = &temperature;
93     service.nxt = NULL;
94 
95     temperature.key = "Temperature";
96     temperature.value = &report->temp;
97     temperature.type = EN_OC_MQTT_PROFILE_VALUE_INT;
98     temperature.nxt = &humidity;
99 
100     humidity.key = "Humidity";
101     humidity.value = &report->hum;
102     humidity.type = EN_OC_MQTT_PROFILE_VALUE_INT;
103     humidity.nxt = &luminance;
104 
105     luminance.key = "Luminance";
106     luminance.value = &report->lum;
107     luminance.type = EN_OC_MQTT_PROFILE_VALUE_INT;
108     luminance.nxt = &led;
109 
110     led.key = "LightStatus";
111     led.value = g_app_cb.led ? "ON" : "OFF";
112     led.type = EN_OC_MQTT_PROFILE_VALUE_STRING;
113     led.nxt = &motor;
114 
115     motor.key = "MotorStatus";
116     motor.value = g_app_cb.motor ? "ON" : "OFF";
117     motor.type = EN_OC_MQTT_PROFILE_VALUE_STRING;
118     motor.nxt = NULL;
119 
120     oc_mqtt_profile_propertyreport(USERNAME, &service);
121     return;
122 }
123 
MsgRcvCallback(uint8_t * recv_data,size_t recv_size,uint8_t ** resp_data,size_t * resp_size)124 void MsgRcvCallback(uint8_t *recv_data, size_t recv_size, uint8_t **resp_data, size_t *resp_size)
125 {
126     app_msg_t *app_msg;
127 
128     int ret = 0;
129     app_msg = malloc(sizeof(app_msg_t));
130     app_msg->msg_type = en_msg_cmd;
131     app_msg->msg.cmd.payload = (char *)recv_data;
132 
133     printf("recv data is %s\n", recv_data);
134     ret = osMessageQueuePut(mid_MsgQueue, &app_msg, 0U, 0U);
135     if (ret != 0) {
136         free(recv_data);
137     }
138     *resp_data = NULL;
139     *resp_size = 0;
140 }
141 
oc_cmdresp(cmd_t * cmd,int cmdret)142 static void oc_cmdresp(cmd_t *cmd, int cmdret)
143 {
144     oc_mqtt_profile_cmdresp_t cmdresp;
145     ///< do the response
146     cmdresp.paras = NULL;
147     cmdresp.request_id = cmd->request_id;
148     cmdresp.ret_code = cmdret;
149     cmdresp.ret_name = NULL;
150     (void)oc_mqtt_profile_cmdresp(NULL, &cmdresp);
151 }
152 ///< COMMAND DEAL
153 #include <cJSON.h>
DealCmdMsg(cmd_t * cmd)154 static void DealCmdMsg(cmd_t *cmd)
155 {
156     cJSON *obj_root, *obj_cmdname, *obj_paras, *obj_para;
157 
158     int cmdret = 1;
159 
160     obj_root = cJSON_Parse(cmd->payload);
161     if (obj_root == NULL) {
162         oc_cmdresp(cmd, cmdret);
163     }
164 
165     obj_cmdname = cJSON_GetObjectItem(obj_root, "command_name");
166     if (obj_cmdname == NULL) {
167         cJSON_Delete(obj_root);
168     }
169     if (strcmp(cJSON_GetStringValue(obj_cmdname), "Agriculture_Control_light") == 0) {
170         obj_paras = cJSON_GetObjectItem(obj_root, "paras");
171         if (obj_paras == NULL) {
172             cJSON_Delete(obj_root);
173         }
174         obj_para = cJSON_GetObjectItem(obj_paras, "Light");
175         if (obj_para == NULL) {
176             cJSON_Delete(obj_root);
177         }
178         ///< operate the LED here
179         if (strcmp(cJSON_GetStringValue(obj_para), "ON") == 0) {
180             g_app_cb.led = 1;
181             LightStatusSet(ON);
182             printf("Light On!");
183         } else {
184             g_app_cb.led = 0;
185             LightStatusSet(OFF);
186             printf("Light Off!");
187         }
188         cmdret = 0;
189     } else if (strcmp(cJSON_GetStringValue(obj_cmdname), "Agriculture_Control_Motor") == 0) {
190         obj_paras = cJSON_GetObjectItem(obj_root, "Paras");
191         if (obj_paras == NULL) {
192             cJSON_Delete(obj_root);
193         }
194         obj_para = cJSON_GetObjectItem(obj_paras, "Motor");
195         if (obj_para == NULL) {
196             cJSON_Delete(obj_root);
197         }
198         ///< operate the Motor here
199         if (strcmp(cJSON_GetStringValue(obj_para), "ON") == 0) {
200             g_app_cb.motor = 1;
201             MotorStatusSet(ON);
202             printf("Motor On!");
203         } else {
204             g_app_cb.motor = 0;
205             MotorStatusSet(OFF);
206             printf("Motor Off!");
207         }
208         cmdret = 0;
209     }
210 
211     cJSON_Delete(obj_root);
212 }
213 
CloudMainTaskEntry(void)214 static int CloudMainTaskEntry(void)
215 {
216     app_msg_t *app_msg;
217 
218     uint32_t ret = WifiConnect("BearPi", "123456789");
219 
220     device_info_init(CLIENT_ID, USERNAME, PASSWORD);
221     oc_mqtt_init();
222     oc_set_cmd_rsp_cb(MsgRcvCallback);
223 
224     while (1) {
225         app_msg = NULL;
226         (void)osMessageQueueGet(mid_MsgQueue, (void **)&app_msg, NULL, 0U);
227         if (app_msg != NULL) {
228             switch (app_msg->msg_type) {
229                 case en_msg_cmd:
230                     DealCmdMsg(&app_msg->msg.cmd);
231                     break;
232                 case en_msg_report:
233                     ReportMsg(&app_msg->msg.report);
234                     break;
235                 default:
236                     break;
237             }
238             free(app_msg);
239         }
240     }
241     return 0;
242 }
243 
SensorTaskEntry(void)244 static int SensorTaskEntry(void)
245 {
246     int ret;
247     app_msg_t *app_msg;
248     E53IA1Data data;
249     ret = E53IA1Init();
250     if (ret != 0) {
251         printf("E53_IA1 Init failed!\r\n");
252         return;
253     }
254     while (1) {
255         ret = E53IA1ReadData(&data);
256         if (ret != 0) {
257             printf("E53_IA1 Read Data failed!\r\n");
258             return;
259         }
260         app_msg = malloc(sizeof(app_msg_t));
261         printf("SENSOR:lum:%.2f temp:%.2f hum:%.2f\r\n", data.Lux, data.Temperature, data.Humidity);
262         if (app_msg != NULL) {
263             app_msg->msg_type = en_msg_report;
264             app_msg->msg.report.hum = (int)data.Humidity;
265             app_msg->msg.report.lum = (int)data.Lux;
266             app_msg->msg.report.temp = (int)data.Temperature;
267             if (osMessageQueuePut(mid_MsgQueue, &app_msg, 0U, 0U) != 0) {
268                 free(app_msg);
269             }
270         }
271         sleep(TASK_DELAY_3S);
272     }
273     return 0;
274 }
275 
IotMainTaskEntry(void)276 static void IotMainTaskEntry(void)
277 {
278     mid_MsgQueue = osMessageQueueNew(MSGQUEUE_COUNT, MSGQUEUE_SIZE, NULL);
279     if (mid_MsgQueue == NULL) {
280         printf("Failed to create Message Queue!\n");
281     }
282 
283     osThreadAttr_t attr;
284 
285     attr.name = "CloudMainTaskEntry";
286     attr.attr_bits = 0U;
287     attr.cb_mem = NULL;
288     attr.cb_size = 0U;
289     attr.stack_mem = NULL;
290     attr.stack_size = CLOUD_TASK_STACK_SIZE;
291     attr.priority = CLOUD_TASK_PRIO;
292 
293     if (osThreadNew((osThreadFunc_t)CloudMainTaskEntry, NULL, &attr) == NULL) {
294         printf("Failed to create CloudMainTaskEntry!\n");
295     }
296     attr.stack_size = SENSOR_TASK_STACK_SIZE;
297     attr.priority = SENSOR_TASK_PRIO;
298     attr.name = "SensorTaskEntry";
299     if (osThreadNew((osThreadFunc_t)SensorTaskEntry, NULL, &attr) == NULL) {
300         printf("Failed to create SensorTaskEntry!\n");
301     }
302 }
303 
304 APP_FEATURE_INIT(IotMainTaskEntry);