• 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 <stdio.h>
17 #include <stdlib.h>
18 #include <memory.h>
19 #include <hi_pwm.h>
20 #include <hi_time.h>
21 /* Link Header Files */
22 #include <link_service.h>
23 #include <link_platform.h>
24 #include <hi_io.h>
25 #include <hi_early_debug.h>
26 #include <hi_gpio.h>
27 #include <hi_types_base.h>
28 #include <hi_stdlib.h>
29 #include <hi_task.h>
30 #include "ohos_init.h"
31 #include "cmsis_os2.h"
32 #include "iot_gpio_ex.h"
33 #include "wifi_connecter.h"
34 #include "iot_gpio.h"
35 #include "app_demo_histreaming.h"
36 
37 #define LED_TEST_GPIO 9
38 #define LED_INTERVAL_TIME_US 300000
39 
40 #define HISTREAMING_DEMO_TASK_STAK_SIZE (1024*8)
41 #define HISTREAMING_DEMO_TASK_PRIORITY  25
42 #define REV_BUFF_LEN    512
43 
44 char rev_buff[512] = {0};
45 unsigned char hex_buff[512] = {0};
46 unsigned int hex_len = 0;
47 
48 UartDefConfig uartDefConfig = {0};
49 
StringToHex(char * str,unsigned char * out,unsigned int * outlen)50 int StringToHex(char *str, unsigned char *out, unsigned int *outlen)
51 {
52     char *p = str;
53     char high = 0, low = 0;
54     int tmplen = strlen(p), cnt = 0;
55     tmplen = strlen(p);
56     while (cnt < (tmplen / HIGH_NUM)) {
57         high = ((*p > HIGH_ASCII) && ((*p <= 'F') || (*p <= 'f'))) ? *p - HIGH_NUM2 - HIGH_NUM3 : *p - HIGH_NUM2;
58         low = (*(++ p) > HIGH_ASCII && ((*p <= 'F') || (*p <= 'f'))) ? *(p) - HIGH_NUM2 - HIGH_NUM3 : *(p) - HIGH_NUM2;
59         out[cnt] = (((high & 0x0f) << HIGH_NUM4) | (low & 0x0f));
60         p ++;
61         cnt ++;
62     }
63     if (tmplen % HIGH_NUM != 0) {
64         out[cnt] = ((*p > HIGH_ASCII) && ((*p <= 'F') || (*p <= 'f'))) ? *p - HIGH_NUM2 - HIGH_NUM3 : *p - HIGH_NUM2;
65     }
66     if (outlen != NULL) {
67         *outlen = tmplen / HIGH_NUM + tmplen % HIGH_NUM;
68     }
69     return tmplen / HIGH_NUM + tmplen % HIGH_NUM;
70 }
71 
72 /**
73  * @bref The device side sends the characteristic value to the app side
74  * @param struct LinkService* ar: histreaming LinkServer structural morphology
75  * @param const char* property: characteristic value
76  * @param char* value: send value to apps
77  * @param int len: send value length
78  */
GetStatusValue(struct LinkService * ar,const char * property,char * value,int len)79 static int GetStatusValue(struct LinkService* ar, const char* property, char* value, int len)
80 {
81     (void)(ar);
82     if (strcmp(property, "Status") == 0) {
83     }
84 /*
85  * if Ok return 0,
86  * Otherwise, any error, return StatusFailure
87  */
88     return 0;
89 }
90 /**
91  * @bref recv from app cmd
92  * @bref Receive the message sent by the app, and operate the hi3861 device side accordingly
93  * @param struct LinkService* ar: histreaming LinkServer structural morphology
94  * @param const char* property: Eigenvalues sent by app
95  * @param char* value: Value sent by app
96  * @param int len: Length of APP sent
97  */
ModifyStatus(struct LinkService * ar,const char * property,char * value,int len)98 static int ModifyStatus(struct LinkService* ar, const char* property, char* value, int len)
99 {
100     (void)(ar);
101     printf("Receive property: %s(value=%s, [%d])\n", property, value, len);
102     if (property == NULL || value == NULL) {
103         return -1;
104     }
105     if (memcpy_s(rev_buff, REV_BUFF_LEN, value, len) != 0) {
106         return 0;
107     }
108     /* modify status property */
109     /* colorful light module */
110     printf("%s, %d\r\n",  rev_buff, len);
111 
112     StringToHex(rev_buff, hex_buff, &hex_len);
113     for (int i = 0; i < hex_len; i++) {
114         printf("0x%x ", hex_buff[i]);
115     }
116     printf("\r\n");
117     IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
118     usleep(LED_INTERVAL_TIME_US);
119     IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
120     usleep(LED_INTERVAL_TIME_US);
121 /*
122  * if Ok return 0,
123  * Otherwise, any error, return StatusFailure
124  */
125     return 0;
126 }
127 
128 /*
129  * It is a Wifi IoT device
130  */
131 static const char* g_wifiStaType = "Pegasus:Hi3861";
GetDeviceType(const struct LinkService * ar)132 static const char* GetDeviceType(const struct LinkService* ar)
133 {
134     (void)(ar);
135 
136     return g_wifiStaType;
137 }
138 
139 static void *g_linkPlatform = NULL;
140 
HistreamingOpen(void)141 void* HistreamingOpen(void)
142 {
143     LinkService* wifiIot = 0;
144     LinkPlatform* link = 0;
145 
146     wifiIot = (LinkService*)malloc(sizeof(LinkService));
147     if (!wifiIot) {
148         printf("malloc wifiIot failure\n");
149         return NULL;
150     }
151     wifiIot->get    = GetStatusValue;
152     wifiIot->modify = ModifyStatus;
153     wifiIot->type = GetDeviceType;
154 
155     link = LinkPlatformGet();
156     if (!link) {
157         printf("get link failure\n");
158     }
159 
160     if (link->addLinkService(link, wifiIot, 1) != 0) {
161         HistreamingClose(link);
162         return NULL;
163     }
164     if (link->open(link) != 0) {
165         HistreamingClose(link);
166         return NULL;
167     }
168     /* cache link ptr */
169     g_linkPlatform = (void*)(link);
170     hi_free(0, wifiIot);
171     return (void*)link;
172 }
173 
HistreamingClose(const char * link)174 void HistreamingClose(const char *link)
175 {
176     LinkPlatform *linkPlatform = (LinkPlatform*)(link);
177     if (!linkPlatform) {
178         return;
179     }
180 
181     linkPlatform->close(linkPlatform);
182 
183     if (linkPlatform != NULL) {
184         LinkPlatformFree(linkPlatform);
185     }
186 }
187 
HistreamingDemo(hi_void)188 hi_void HistreamingDemo(hi_void)
189 {
190     ConnectToHotspot();
191     IoTGpioInit(LED_TEST_GPIO);
192     IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);
193     osThreadAttr_t histreaming = {0};
194     histreaming.stack_size = HISTREAMING_DEMO_TASK_STAK_SIZE;
195     histreaming.priority = HISTREAMING_DEMO_TASK_PRIORITY;
196     histreaming.name = (hi_char*)"histreaming_demo";
197     if (osThreadNew((osThreadFunc_t)HistreamingOpen, NULL, &histreaming) == NULL) {
198         printf("Failed to create histreaming task\r\n");
199     }
200 }
201 
202 SYS_RUN(HistreamingDemo);