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 <unistd.h>
18 #include <signal.h>
19 #include <stdlib.h>
20 #include <memory.h>
21
22 #include "status_code.h"
23 #include "link_service.h"
24 #include "link_platform.h"
25 #include "link_agent.h"
26 #include "hisignalling.h"
27
28 #define NUM2 2
29 #define SBuf_LEN 33
30 #define PBUF_LEN 32
31
32 static const char *g_device_type = "Pegasus:Hi3861";
33 static volatile sig_atomic_t g_interrupted = 0;
34 unsigned int hisignallingPackageLen = 0;
35 unsigned char hisignallingSendBuf[HISIGNALLING_MSG_BUFF_LEN] = {0};
36 char send_buff[SEND_BUFF_LEN] = {0};
37 unsigned char light_on_off_buff[4] = {0, 2, 0, 3};
38 unsigned int led_flag = 0;
39
40 /*
41 * @berf hisignalling 协议发送消息
42 * @param void *buf: 发送数据缓存
43 * @param unsigned int data_len: 发送数据长度
44 *
45 * @berf hisignalling protocol send msg
46 * @param void *buf: send data buffer
47 * @param unsigned int data_len: send data length
48 */
HisignallingMsgSend(char * buf,unsigned int dataLen)49 unsigned int HisignallingMsgSend(char *buf, unsigned int dataLen)
50 {
51 unsigned int ret = 0;
52 HisignallingProtocalType hisignallingMsg = {0};
53 unsigned int writeDataLen = 0;
54
55 hisignallingMsg.frameHeader[0]= 0xAA; /* Protocol head data 1 */
56 hisignallingMsg.frameHeader[1]= 0x55; /* Protocol head data 2 */
57 (void)memcpy_s(hisignallingMsg.hisignallingMsgBuf, dataLen, buf, dataLen);
58 hisignallingMsg.endOfFrame = 0xff; /* Protocol tail data */
59
60 hisignallingPackageLen = HisignallingDataPackage(&hisignallingMsg, dataLen, hisignallingSendBuf);
61 if (!hisignallingPackageLen) {
62 printf("hisignalling_data_package failed\r\n");
63 return -1;
64 }
65 if (*hisignallingSendBuf == 0) {
66 printf("hisignalling send buf is null!\r\n");
67 return -1;
68 }
69 return 0;
70 }
71
72 /*
73 * @berf 接收信号句柄
74 * @param int sig: 信号
75 *
76 * @berf Receive signal handle
77 * @param int sig: signal
78 */
SigIntHandler(int sig)79 static void SigIntHandler(int sig)
80 {
81 g_interrupted = 1;
82 }
83
84 /*
85 * @berf 客户端获取服务器状态和值
86 * @param struct LinkService* ar: 服务器结构
87 * @param const char* 属性:客户端属性
88 * @param char* value: 客户端值
89 * @param int len: 客户端值长度
90 *
91 * @berf Client gets server status and value
92 * @param struct LinkService* ar: server struct
93 * @param const char* property: client property
94 * @param char* value: client value
95 * @param int len: client value len
96 */
DoGET(struct LinkService * ar,const char * property,char * value,int len)97 static int DoGET(struct LinkService* ar, const char* property, char* value, int len)
98 {
99 printf("Receive property: %s(value=%s)\n", property, value);
100
101 if (strcmp(property, "Status") == 0) {
102 }
103 /*
104 * 如果确定返回 StatusOk,
105 * 否则,任何错误,返回 StatusFailure
106 *
107 * if Ok return StatusOk,
108 * Otherwise, any error, return StatusFailure
109 */
110 return StatusOk;
111 }
112
113 /*
114 * @berf Client 显示属性状态和值
115 * @param struct LinkService* ar: 服务器结构
116 * @param const char* 属性:客户端属性
117 * @param char* value: 客户端值
118 * @param int len: 客户端值长度
119 *
120 * @berf Client puts property status and value
121 * @param struct LinkService* ar: server struct
122 * @param const char* property: client property
123 * @param char* value: client value
124 * @param int len: client value len
125 */
DoPUT(struct LinkService * ar,const char * property,char * value,int len)126 static int DoPUT(struct LinkService* ar, const char* property, char* value, int len)
127 {
128 printf("Receive property: %s(value=%s)\n", property, value);
129 /*
130 * 如果确定返回 StatusOk,
131 * 否则,任何错误,返回 StatusFailure
132 *
133 * if Ok return StatusOk,
134 * Otherwise, any error, return StatusFailure
135 */
136 return StatusOk;
137 }
138
139 /*
140 * wifi sta模式设备类型是Taurus:Hi3516DV300
141 * The device type in wifi sta mode is "Taurus: Hi3516DV300"
142 */
143 static const char* g_wifista_type = "Taurus:Hi3516DV300";
DoType(struct LinkService * ar)144 static const char* DoType(struct LinkService* ar)
145 {
146 return g_wifista_type;
147 }
148
149 /*
150 * @berf histreaming 发送消息
151 * @param unsigned char *write_buffer: 发送数据缓冲区
152 *
153 * @berf histreaming send message
154 * @param unsigned char *write_buffer: send data buff
155 */
HistreamingMsgSendSwitch(unsigned char * write_buffer)156 static unsigned int HistreamingMsgSendSwitch(unsigned char *write_buffer)
157 {
158 SwitchTriggerSendMsg(write_buffer);
159 return 0;
160 }
161
HiSteramingServer(void)162 int HiSteramingServer(void)
163 {
164 /*
165 * 启动服务端
166 * Server start
167 */
168 if (signal(SIGINT, SigIntHandler) == 0) {
169 return 0;
170 }
171 if (signal(SIGKILL, SigIntHandler) == 0) {
172 return 0;
173 }
174 if (signal(SIGTERM, SigIntHandler) == 0) {
175 return 0;
176 }
177 }
178
HiStreamingData(char * send_buff)179 char HiStreamingData(char *send_buff)
180 {
181 if (led_flag % NUM2 == 0) {
182 char send_buff1[SEND_BUFF_LEN] = "AA5500020003FF8ED2BEDF";
183 (void)memcpy_s(send_buff, SEND_BUFF_LEN, send_buff1, SEND_BUFF_LEN);
184 } else {
185 char send_buff2[SEND_BUFF_LEN] = "AA5500020004FFC1932818";
186 (void)memcpy_s(send_buff, SEND_BUFF_LEN, send_buff2, SEND_BUFF_LEN);
187 }
188 printf("out = %s\n", send_buff);
189 if (led_flag >= 0xffffffff) {
190 led_flag = 0;
191 }
192 led_flag++;
193 return 0;
194 }
195
196 /*
197 * @berf main函数
198 * @berf main function
199 */
main(int argc,char ** argv)200 int main(int argc, char** argv)
201 {
202 LinkPlatform *link = 0;
203 LinkAgent *linkAgent = 0;
204 LinkService* doorbell = 0;
205 int status = 0;
206
207 /*
208 * 启动服务端
209 * Server start
210 */
211 HiSteramingServer();
212
213 /*
214 * 构造doorbell
215 * Construct doorbell
216 */
217 doorbell = (LinkService*)malloc(sizeof(LinkService));
218 if (!doorbell) {
219 return NULL;
220 }
221
222 doorbell->get = DoGET;
223 doorbell->modify = DoPUT;
224 doorbell->type = DoType;
225
226 link = LinkPlatformGet(); // start histreaming server link platform service
227 if (!link) {
228 LinkPlatformFree(link); // free histreaming server link platform
229 }
230 link->addLinkService(link, doorbell, HISIGNALLING_MSG_HEADER_LEN); // add histreaming server link service
231 link->open(link);
232
233 /*
234 * 启动客户端
235 * Client start
236 */
237 HiSteramingServer();
238 link = LinkPlatformGet();
239 if (!link) {
240 LinkPlatformFree(link);
241 }
242
243 linkAgent = LinkAgentGet(link);
244 if (!linkAgent) {
245 LinkAgentFree(linkAgent);
246 }
247
248 link->setDebuglevel(link, NUM7); // set histreaming client debug level
249 link->open(link);
250
251 while (!g_interrupted) {
252 QueryResult *qres = linkAgent->query(linkAgent, g_device_type); // start histreaming client agent module
253 while (qres == NULL && !g_interrupted) {
254 qres = linkAgent->query(linkAgent, g_device_type); // client Waiting and server connection
255 }
256 if (qres != 0) { // Connect with the server
257 LinkServiceAgent *serviceAgent = qres->at(qres, 0);
258 if (serviceAgent != NULL) {
259 HiStreamingData(send_buff);
260 status = serviceAgent->modify(serviceAgent, "status", send_buff, SEND_BUFF_LEN);
261 }
262 if (StatusDeviceOffline == status) {
263 link->discover(link, g_device_type);
264 }
265 QueryResultFree(qres); // free histreaming agent module
266 }
267
268 sleep(HISIGNALLING_MSG_HEADER_LEN);
269 }
270 LinkPlatformFree(link);
271 LinkAgentFree(linkAgent);
272 return 0;
273 }