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 /* Link Header Files */
23 #include "status_code.h"
24 #include "link_service.h"
25 #include "link_platform.h"
26 #include "link_agent.h"
27 #include "hisignalling.h"
28
29 #define NUM2 2
30 #define SBuf_LEN 33
31 #define PBUF_LEN 32
32
33 static const char *g_device_type = "Pegasus:Hi3861";
34 static volatile sig_atomic_t g_interrupted = 0;
35 unsigned int hisignallingPackageLen = 0;
36 unsigned char hisignallingSendBuf[HISIGNALLING_MSG_BUFF_LEN] = {0};
37 char send_buff[SEND_BUFF_LEN] = {0};
38 unsigned char light_on_off_buff[4] = {0, 2, 0, 3};
39 unsigned int led_flag = 0;
40
41 /**
42 * @berf hisignalling protocol send msg
43 * @param void *buf: send data buff
44 * @param unsigned int data_len: send data length
45 */
HisignallingMsgSend(char * buf,unsigned int dataLen)46 unsigned int HisignallingMsgSend(char *buf, unsigned int dataLen)
47 {
48 unsigned int ret = 0;
49 HisignallingProtocalType hisignallingMsg = {0};
50 unsigned int writeDataLen = 0;
51
52 hisignallingMsg.frameHeader[0]= 0xAA; /* Protocol head data 1 */
53 hisignallingMsg.frameHeader[1]= 0x55; /* Protocol head data 2 */
54 (void)memcpy_s(hisignallingMsg.hisignallingMsgBuf, dataLen, buf, dataLen);
55 hisignallingMsg.endOfFrame = 0xff; /* Protocol tail data */
56
57 hisignallingPackageLen = HisignallingDataPackage(&hisignallingMsg, dataLen, hisignallingSendBuf);
58 if (!hisignallingPackageLen) {
59 printf("hisignalling_data_package failed\r\n");
60 return -1;
61 }
62 if (*hisignallingSendBuf == 0) {
63 printf("hisignalling send buf is null!\r\n");
64 return -1;
65 }
66 return 0;
67 }
68
69 /**
70 * @berf Receive signal handle
71 * @param int sig: signal
72 */
SigIntHandler(int sig)73 static void SigIntHandler(int sig)
74 {
75 g_interrupted = 1;
76 }
77 /**
78 * @berf Client gets server status and value
79 * @param struct LinkService* ar: server struct
80 * @param const char* property: client property
81 * @param char* value: client value
82 * @param int len: client value len
83 */
DoGET(struct LinkService * ar,const char * property,char * value,int len)84 static int DoGET(struct LinkService* ar, const char* property, char* value, int len)
85 {
86 printf("Receive property: %s(value=%s)\n", property, value);
87
88 if (strcmp(property, "Status") == 0) {
89 }
90 /*
91 * if Ok return StatusOk,
92 * Otherwise, any error, return StatusFailure
93 */
94 return StatusOk;
95 }
96
97 /**
98 * @berf Client puts property status and value
99 * @param struct LinkService* ar: server struct
100 * @param const char* property: client property
101 * @param char* value: client value
102 * @param int len: client value len
103 */
DoPUT(struct LinkService * ar,const char * property,char * value,int len)104 static int DoPUT(struct LinkService* ar, const char* property, char* value, int len)
105 {
106 printf("Receive property: %s(value=%s)\n", property, value);
107 /*
108 * if Ok return StatusOk,
109 * Otherwise, any error, return StatusFailure
110 */
111 return StatusOk;
112 }
113
114 /*
115 * It is a Doorbell device
116 */
117 static const char* g_wifista_type = "Taurus:Hi3516DV300";
DoType(struct LinkService * ar)118 static const char* DoType(struct LinkService* ar)
119 {
120 return g_wifista_type;
121 }
122
123 /**
124 * @berf histreaming send message
125 * @param unsigned char *write_buffer: send data buff
126 */
HistreamingMsgSendSwitch(unsigned char * write_buffer)127 static unsigned int HistreamingMsgSendSwitch(unsigned char *write_buffer)
128 {
129 SwitchTriggerSendMsg(write_buffer);
130 return 0;
131 }
132
HiSteramingServer(void)133 int HiSteramingServer(void)
134 {
135 /* server start */
136 if (signal(SIGINT, SigIntHandler) == 0) {
137 return 0;
138 }
139 if (signal(SIGKILL, SigIntHandler) == 0) {
140 return 0;
141 }
142 if (signal(SIGTERM, SigIntHandler) == 0) {
143 return 0;
144 }
145 }
146
HiStreamingData(char * send_buff)147 char HiStreamingData(char *send_buff)
148 {
149 if (led_flag % NUM2 == 0) {
150 char send_buff1[SEND_BUFF_LEN] = "AA5500020003FF8ED2BEDF";
151 (void)memcpy_s(send_buff, SEND_BUFF_LEN, send_buff1, SEND_BUFF_LEN);
152 } else {
153 char send_buff2[SEND_BUFF_LEN] = "AA5500020004FFC1932818";
154 (void)memcpy_s(send_buff, SEND_BUFF_LEN, send_buff2, SEND_BUFF_LEN);
155 }
156 printf("out = %s\n", send_buff);
157 if (led_flag >= 0xffffffff) {
158 led_flag = 0;
159 }
160 led_flag++;
161 return 0;
162 }
163
164 /**
165 * @berf main function
166 */
main(int argc,char ** argv)167 int main(int argc, char** argv)
168 {
169 LinkPlatform *link = 0;
170 LinkAgent *linkAgent = 0;
171 LinkService* doorbell = 0;
172 int status = 0;
173
174 // /* server start */
175 HiSteramingServer();
176 /*
177 * Construct doorbell
178 */
179 doorbell = (LinkService*)malloc(sizeof(LinkService));
180 if (!doorbell) {
181 return NULL;
182 }
183
184 doorbell->get = DoGET;
185 doorbell->modify = DoPUT;
186 doorbell->type = DoType;
187
188 link = LinkPlatformGet(); // start histreaming server link platform service
189 if (!link) {
190 LinkPlatformFree(link); // free histreaming server link platform
191 }
192 link->addLinkService(link, doorbell, HISIGNALLING_MSG_HEADER_LEN); // add histreaming server link service
193 link->open(link);
194
195 /* client start */
196 HiSteramingServer();
197 link = LinkPlatformGet();
198 if (!link) {
199 LinkPlatformFree(link);
200 }
201
202 linkAgent = LinkAgentGet(link);
203 if (!linkAgent) {
204 LinkAgentFree(linkAgent);
205 }
206
207 link->setDebuglevel(link, NUM7); // set histreaming client debug level
208 link->open(link);
209
210 while (!g_interrupted) {
211 QueryResult *qres = linkAgent->query(linkAgent, g_device_type); // start histreaming client agent module
212 while (qres == NULL && !g_interrupted) {
213 qres = linkAgent->query(linkAgent, g_device_type); // client Waiting and server connection
214 }
215 if (qres != 0) { // Connect with the server
216 LinkServiceAgent *serviceAgent = qres->at(qres, 0);
217 if (serviceAgent != NULL) {
218 HiStreamingData(send_buff);
219 status = serviceAgent->modify(serviceAgent, "status", send_buff, SEND_BUFF_LEN);
220 }
221 if (StatusDeviceOffline == status) {
222 link->discover(link, g_device_type);
223 }
224 QueryResultFree(qres); // free histreaming agent module
225 }
226
227 sleep(HISIGNALLING_MSG_HEADER_LEN);
228 }
229 LinkPlatformFree(link);
230 LinkAgentFree(linkAgent);
231 return 0;
232 }