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 // #ifdef CONFIG_HISTREAMING_SUPPORT
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <memory.h>
20 #include <hi_pwm.h>
21 #include <hi_time.h>
22 /* Link Header Files */
23 #include <link_service.h>
24 #include <link_platform.h>
25 #include <histreaming.h>
26 #include <hi_io.h>
27 #include <hi_early_debug.h>
28 #include <hi_gpio.h>
29 #include <hi_task.h>
30 #include <hi_types_base.h>
31 #include "app_demo_multi_sample.h"
32 #include "ssd1306_oled.h"
33 #include "iot_gpio.h"
34
35 #define HISTREAMING_TASK
36 #ifdef HISTREAMING_TASK
37 #define HISTREAMING_DEMO_TASK_STAK_SIZE (1024*8)
38 #define HISTREAMING_DEMO_TASK_PRIORITY 25
39 #define IO_FUNC_GPIO_OUT 0
40 #define IOT_GPIO_INDEX_10 10
41 #define IOT_GPIO_INDEX_11 11
42 #define IOT_GPIO_INDEX_12 12
43 hi_u32 g_histreamingDemoTaskId = 0;
44 #endif
45
46 static hi_void HistreamingTrafficLightControl(const char* property, char* value);
47
HistreamingManualControlModule(const char * property,char * value)48 static void HistreamingManualControlModule(const char* property, char* value)
49 {
50 if (strcmp(property, "tl_s") == 0) {
51 if (strcmp(value, "red_on") == 0) {
52 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7, "1.Red On ",
53 OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
54 GpioControl(IOT_GPIO_INDEX_10, IOT_GPIO_INDEX_10,
55 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE1, IO_FUNC_GPIO_OUT);
56 GpioControl(IOT_GPIO_INDEX_11, IOT_GPIO_INDEX_11,
57 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE0, IO_FUNC_GPIO_OUT);
58 }
59 if (strcmp(value, "yellow_on") == 0) {
60 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7, "2.Yellow On ",
61 OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
62 GpioControl(IOT_GPIO_INDEX_10, IOT_GPIO_INDEX_10,
63 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE0, IO_FUNC_GPIO_OUT);
64 GpioControl(IOT_GPIO_INDEX_12, IOT_GPIO_INDEX_12,
65 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE1, IO_FUNC_GPIO_OUT);
66 }
67 if (strcmp(value, "green_on") == 0) {
68 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7, "3.Green On ",
69 OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
70 GpioControl(IOT_GPIO_INDEX_12, IOT_GPIO_INDEX_12,
71 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE0, IO_FUNC_GPIO_OUT);
72 GpioControl(IOT_GPIO_INDEX_11, IOT_GPIO_INDEX_11,
73 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE1, IO_FUNC_GPIO_OUT);
74 }
75 }
76 }
HistreamingAutoModule(const char * property,char * value)77 static void HistreamingAutoModule(const char* property, char* value)
78 {
79 if (strcmp(property, "tla_s") == 0) {
80 if (strcmp(value, "tla") == 0) {
81 int ret = SetKeyStatus(TRAFFIC_AUTO_MODE);
82 if (ret != TRAFFIC_AUTO_MODE) {
83 return;
84 }
85 }
86 }
87 }
88
HistreamingHumanModuleNormal(const char * property,char * value)89 static void HistreamingHumanModuleNormal(const char* property, char* value)
90 {
91 if (strcmp(property, "tlh_s") == 0) {
92 if (strcmp(value, "tlh") == 0) {
93 int rc = SetKeyType(TRAFFIC_NORMAL_TYPE);
94 if (rc != TRAFFIC_NORMAL_TYPE) {
95 return;
96 }
97 int ret = SetKeyStatus(TRAFFIC_HUMAN_MODE);
98 if (ret != TRAFFIC_HUMAN_MODE) {
99 return;
100 }
101 }
102 }
103 }
104
HistreamingTrafficLightReturnMainMenu(const char * property,char * value)105 static hi_u32 HistreamingTrafficLightReturnMainMenu(const char* property, char* value)
106 {
107 if (strcmp(property, "tlr_s") == 0) {
108 if (strcmp(value, "tlr") == 0) {
109 int ret = SetKeyStatus(TRAFFIC_RETURN_MODE);
110 if (ret != TRAFFIC_RETURN_MODE) {
111 return;
112 }
113 return HI_ERR_SUCCESS;
114 }
115 }
116 }
117
118
119 /* histreaming traffic light function control */
HistreamingTrafficLightControl(const char * property,char * value)120 static hi_void HistreamingTrafficLightControl(const char* property, char* value)
121 {
122 /* manual control module */
123 HistreamingManualControlModule(property, value);
124 /* auto module */
125 HistreamingAutoModule(property, value);
126 // /* human module normal */
127 HistreamingHumanModuleNormal(property, value);
128 HistreamingTrafficLightReturnMainMenu(property, value);
129 }
130
GetStatusValue(const struct LinkService * ar,const char * property,const char * value,int len)131 static int GetStatusValue(const struct LinkService* ar, const char* property, const char* value, int len)
132 {
133 (void)(ar);
134 char *status = "Opend";
135
136 printf("Receive property: %s(value=%s[%d])\n", property, value, len);
137
138 if (strcmp(property, "Status") == 0) {
139 (void)strcpy_s(value, strlen(status) + 1, "Opend"); /* 1: '\0' length */
140 }
141 /*
142 * if Ok return 0,
143 * Otherwise, any error, return StatusFailure
144 */
145 return 0;
146 }
147 /* recv from app cmd */
ModifyStatus(const struct LinkService * ar,const char * property,const char * value,int len)148 static int ModifyStatus(const struct LinkService* ar, const char* property, const char* value, int len)
149 {
150 int ret;
151 (void)(ar);
152 printf("-----------$$$$$$$$$$$$\n");
153
154 if (property == NULL || value == NULL) {
155 return -1;
156 }
157 /* modify status property */
158 /* traffic light module */
159 HistreamingTrafficLightControl(property, value);
160 /* traffic light return main menu */
161 ret = HistreamingTrafficLightReturnMainMenu(property, value);
162 if (ret != 0) {
163 printf("HistreamingTrafficLightReturnMainMenu Failed\r\n");
164 }
165 printf("Receive property: %s(value=%s[%d])\n", property, value, len);
166 /*
167 * if Ok return 0,
168 * Otherwise, any error, return StatusFailure
169 */
170 return 0;
171 }
172
173 /*
174 * It is a Wifi IoT device
175 */
176 static const char* g_wifiStaType = "Light";
GetDeviceType(const struct LinkService * ar)177 static const char* GetDeviceType(const struct LinkService* ar)
178 {
179 (void)(ar);
180
181 return g_wifiStaType;
182 }
183
184 static void *g_linkPlatform = NULL;
185
HistreamingOpen(void)186 void* HistreamingOpen(void)
187 {
188 LinkService* wifiIot = 0;
189 LinkPlatform* link = 0;
190
191 wifiIot = (LinkService*)malloc(sizeof(LinkService));
192 if (!wifiIot) {
193 printf("malloc wifiIot failure\n");
194 return NULL;
195 }
196
197 wifiIot->get = GetStatusValue;
198 wifiIot->modify = ModifyStatus;
199 wifiIot->type = GetDeviceType;
200 link = LinkPlatformGet();
201 if (!link) {
202 printf("get link failure\n");
203 }
204
205 if (link->addLinkService(link, wifiIot, 1) != 0) {
206 HistreamingClose(link);
207 return NULL;
208 }
209 if (link->open(link) != 0) {
210 HistreamingClose(link);
211 return NULL;
212 }
213 /* cache link ptr */
214 g_linkPlatform = (void*)(link);
215 #ifdef HISTREAMING_TASK
216 hi_task_delete(g_histreamingDemoTaskId);
217 #endif
218 hi_free(0, wifiIot);
219 return (void*)link;
220 }
221
HistreamingClose(const char * link)222 void HistreamingClose(const char *link)
223 {
224 LinkPlatform *linkPlatform = (LinkPlatform*)(link);
225 if (!linkPlatform) {
226 return;
227 }
228
229 linkPlatform->close(linkPlatform);
230
231 if (linkPlatform != NULL) {
232 LinkPlatformFree(linkPlatform);
233 }
234 }
235 #ifdef HISTREAMING_TASK
HistreamingDemo(hi_void)236 hi_void HistreamingDemo(hi_void)
237 {
238 hi_u32 ret;
239 hi_task_attr histreaming = {0};
240 histreaming.stack_size = HISTREAMING_DEMO_TASK_STAK_SIZE;
241 histreaming.task_prio = HISTREAMING_DEMO_TASK_PRIORITY;
242 histreaming.task_name = "histreaming_demo";
243 ret = hi_task_create(&g_histreamingDemoTaskId, &histreaming, HistreamingOpen, HI_NULL);
244 if (ret != HI_ERR_SUCCESS) {
245 printf("Failed to create histreaming demo task!\n");
246 }
247 }
248 #endif
249 // #endif