Skip to content

Commit 6787dae

Browse files
committed
add gizwits api and demo
1 parent eef98e5 commit 6787dae

File tree

6 files changed

+396
-8
lines changed

6 files changed

+396
-8
lines changed

demo/gizwits/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## ----------------------------------------------------------- ##
2+
## Don't touch the next line unless you know what you're doing.##
3+
## ----------------------------------------------------------- ##
4+
5+
# Name of the module
6+
LOCAL_NAME := demo/gizwits
7+
8+
# List of submodules which contain code we need to include in the final lib
9+
LOCAL_API_DEPENDS := \
10+
11+
LOCAL_ADD_INCLUDE := include\
12+
include/std_inc \
13+
include/api_inc \
14+
15+
16+
# Set this to any non-null string to signal a module which
17+
# generates a binary (must contain a "main" entry point).
18+
# If left null, only a library will be generated.
19+
IS_ENTRY_POINT := no
20+
21+
## ------------------------------------ ##
22+
## Add your custom flags here ##
23+
## ------------------------------------ ##
24+
MYCFLAGS +=
25+
26+
## ------------------------------------- ##
27+
## List all your sources here ##
28+
## ------------------------------------- ##
29+
C_SRC := ${notdir ${wildcard src/*.c}}
30+
31+
## ------------------------------------- ##
32+
## Do Not touch below this line ##
33+
## ------------------------------------- ##
34+
include ${SOFT_WORKDIR}/platform/compilation/cust_rules.mk

demo/gizwits/src/demo_gizwits.c

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
/*
2+
* @File demo_socket.c
3+
* @Brief socket(TCP) communication with block IO example
4+
*
5+
* @Author: Neucrack
6+
* @Date: 2018-06-12 18:04:08
7+
* @Last Modified by: Neucrack
8+
* @Last Modified time: 2018-06-22 19:15:45
9+
*/
10+
11+
12+
#include <string.h>
13+
#include <stdio.h>
14+
#include <api_os.h>
15+
#include <api_event.h>
16+
#include <api_socket.h>
17+
#include <api_network.h>
18+
#include <api_debug.h>
19+
#include <api_gizwits.h>
20+
#include <time.h>
21+
#include <api_info.h>
22+
#include <assert.h>
23+
24+
/*******************************************************************/
25+
///////////////////////// configuration ////////////////////////
26+
const char* GIZWITS_CONFIG_FILE_PATH = "gizwits_config.conf";
27+
#define GIZWITS_TRACKER_PK "c2df95705871421d8d4d8cfdb673ec39"
28+
#define GIZWITS_TRACKER_PK_SECRET "a039314fda***********6eb033cb218"
29+
/*******************************************************************/
30+
31+
32+
#define MAIN_TASK_STACK_SIZE (2048 * 2)
33+
#define MAIN_TASK_PRIORITY 0
34+
#define MAIN_TASK_NAME "Socket Test Task"
35+
36+
#define TEST_TASK_STACK_SIZE (2048 * 2)
37+
#define TEST_TASK_PRIORITY 0
38+
#define TEST_TASK_NAME "Test Task"
39+
40+
static HANDLE socketTaskHandle = NULL;
41+
static HANDLE testTaskHandle = NULL;
42+
static HANDLE semStart = NULL;
43+
44+
45+
void EventDispatch(API_Event_t* pEvent)
46+
{
47+
switch(pEvent->id)
48+
{
49+
case API_EVENT_ID_NO_SIMCARD:
50+
Trace(10,"!!NO SIM CARD%d!!!!",pEvent->param1);
51+
break;
52+
53+
case API_EVENT_ID_NETWORK_REGISTER_SEARCHING:
54+
Trace(2,"network register searching");
55+
break;
56+
57+
case API_EVENT_ID_NETWORK_REGISTER_DENIED:
58+
Trace(2,"network register denied");
59+
break;
60+
61+
case API_EVENT_ID_NETWORK_REGISTER_NO:
62+
Trace(2,"network register no");
63+
break;
64+
65+
case API_EVENT_ID_NETWORK_REGISTERED_HOME:
66+
case API_EVENT_ID_NETWORK_REGISTERED_ROAMING:
67+
{
68+
uint8_t status;
69+
Trace(2,"network register success");
70+
bool ret = Network_GetAttachStatus(&status);
71+
if(!ret)
72+
Trace(1,"get attach staus fail");
73+
Trace(1,"attach status:%d",status);
74+
if(status == 0)
75+
{
76+
ret = Network_StartAttach();
77+
if(!ret)
78+
{
79+
Trace(1,"network attach fail");
80+
}
81+
}
82+
else
83+
{
84+
Network_PDP_Context_t context = {
85+
.apn ="cmnet",
86+
.userName = "" ,
87+
.userPasswd = ""
88+
};
89+
Network_StartActive(context);
90+
}
91+
break;
92+
}
93+
case API_EVENT_ID_NETWORK_ATTACHED:
94+
Trace(2,"network attach success");
95+
Network_PDP_Context_t context = {
96+
.apn ="cmnet",
97+
.userName = "" ,
98+
.userPasswd = ""
99+
};
100+
Network_StartActive(context);
101+
break;
102+
103+
case API_EVENT_ID_NETWORK_ACTIVATED:
104+
Trace(2,"network activate success");
105+
OS_ReleaseSemaphore(semStart);
106+
break;
107+
108+
default:
109+
break;
110+
}
111+
}
112+
113+
void OnReceivedOnePacket(Gizwits_t* gizwits, Gizwits_Action_t action,uint8_t* data, int len)
114+
{
115+
Trace(1,"this is one packet from mqtt, action: %d", action);
116+
117+
switch(action){
118+
case GIZWITS_VARIABLE_LENGTH_ACTION_CONTROL:
119+
Trace(1,"please control your device here, and then report the new status, both app and mqtt");
120+
// GIZWITS_Send();
121+
break;
122+
123+
case GIZWITS_VARIABLE_LENGTH_ACTION_READ_STATUS:
124+
Trace(1,"please update your device status into data buf and send back to mqtt");
125+
// GIZWITS_Send();
126+
break;
127+
128+
case GIZWITS_VARIABLE_LENGTH_ACTION_TRANS_RECV:
129+
Trace(1,"this is your raw data from mqtt");
130+
break;
131+
132+
case GIZWITS_VARIABLE_LENGTH_ACTION_PUSH_OTA:
133+
Trace(1,"get your ota file url: %s", gizwits->otaUrl);
134+
Trace(1,"did:%s",data);
135+
break;
136+
default:
137+
Trace(1,"unkown action:%d",action);
138+
break;
139+
}
140+
}
141+
142+
143+
bool GIZWITS_TEST()
144+
{
145+
Gizwits_Config_t config;
146+
Gizwits_t gizwits;
147+
int ret;
148+
149+
GIZWITS_GetConfig(&config,GIZWITS_CONFIG_FILE_PATH);
150+
memcpy(config.pk,GIZWITS_TRACKER_PK,strlen(GIZWITS_TRACKER_PK));
151+
memcpy(config.pk_secret,GIZWITS_TRACKER_PK_SECRET,strlen(GIZWITS_TRACKER_PK_SECRET));
152+
config.alive = 300;
153+
INFO_GetIMEI(config.imei);
154+
155+
Trace(1,"imei:%s",config.imei);
156+
Trace(1,"pk:%s",config.pk);
157+
Trace(1,"pk_secret:%s",config.pk_secret);
158+
Trace(1,"did:%s",config.did);
159+
Trace(1,"alive time:%d",config.alive);
160+
161+
Assert(strstr(config.pk_secret,"*") == NULL,"!!!!!please config gizwits parameters first!!!!!!!");
162+
163+
ret = GIZWITS_Connect(&gizwits,&config,GIZWITS_CONFIG_FILE_PATH);
164+
if(ret<0)
165+
{
166+
Trace(1,"connect gizwits fail");
167+
return false;
168+
}
169+
Trace(1,"connect gizwits success");
170+
171+
time_t ping_timeOld, sendTime;
172+
ping_timeOld = time(NULL);
173+
sendTime = ping_timeOld;
174+
fd_set readfds;
175+
struct timeval select_timeval;
176+
int fd;
177+
178+
while(1){
179+
//heart beat in 310s
180+
if(time(NULL) - ping_timeOld > 280)
181+
{
182+
if(!GIZWITS_Ping(&gizwits))
183+
Trace(1,"gizwits ping fail");
184+
ping_timeOld = time(NULL);
185+
}
186+
187+
//send every 20+5 s
188+
if(time(NULL) - sendTime > 20)
189+
{
190+
if(GIZWITS_Send(&gizwits,GIZWITS_VARIABLE_LENGTH_ACTION_READ_STATUS,"123456",6) < 0)
191+
Trace(1,"gizwits send fail");
192+
sendTime = time(NULL);
193+
}
194+
195+
//receive
196+
select_timeval.tv_sec = 5;
197+
select_timeval.tv_usec = 0;
198+
FD_ZERO(&readfds);
199+
200+
fd = GIZWITS_GetSocket(&gizwits);
201+
if(fd > 0)
202+
{
203+
FD_SET(fd, &readfds);
204+
}
205+
if(select(fd+1, &readfds, NULL, NULL, &select_timeval) <= 0){
206+
Trace(1,"server: no select");
207+
continue;
208+
}
209+
Trace(1,"server: get one select issue");
210+
if(FD_ISSET(fd, &readfds)){
211+
Trace(1,"can read");
212+
ret = GIZWITS_DoReceive(&gizwits,OnReceivedOnePacket);
213+
Trace(1,"recive return:%d",ret);
214+
}
215+
}
216+
217+
}
218+
219+
220+
221+
void Socket_BIO_Test()
222+
{
223+
//wait for gprs network connection ok
224+
semStart = OS_CreateSemaphore(0);
225+
OS_WaitForSemaphore(semStart,OS_TIME_OUT_WAIT_FOREVER);
226+
OS_DeleteSemaphore(semStart);
227+
228+
Trace(1,"start connect gizwits now");
229+
GIZWITS_TEST();
230+
}
231+
232+
void test_MainTask(void* param)
233+
{
234+
API_Event_t* event=NULL;
235+
236+
Socket_BIO_Test();
237+
238+
while(1)
239+
{
240+
if(OS_WaitEvent(socketTaskHandle, (void**)&event, OS_TIME_OUT_WAIT_FOREVER))
241+
{
242+
// EventDispatch(event);
243+
OS_Free(event->pParam1);
244+
OS_Free(event->pParam2);
245+
OS_Free(event);
246+
}
247+
}
248+
}
249+
250+
251+
void socket_MainTask(void *pData)
252+
{
253+
API_Event_t* event=NULL;
254+
255+
testTaskHandle = OS_CreateTask(test_MainTask,
256+
NULL, NULL, TEST_TASK_STACK_SIZE, TEST_TASK_PRIORITY, 0, 0, TEST_TASK_NAME);
257+
258+
while(1)
259+
{
260+
if(OS_WaitEvent(socketTaskHandle, (void**)&event, OS_TIME_OUT_WAIT_FOREVER))
261+
{
262+
EventDispatch(event);
263+
OS_Free(event->pParam1);
264+
OS_Free(event->pParam2);
265+
OS_Free(event);
266+
}
267+
}
268+
}
269+
270+
void gizwits_Main()
271+
{
272+
socketTaskHandle = OS_CreateTask(socket_MainTask,
273+
NULL, NULL, MAIN_TASK_STACK_SIZE, MAIN_TASK_PRIORITY, 0, 0, MAIN_TASK_NAME);
274+
OS_SetUserMainHandle(&socketTaskHandle);
275+
}
276+

include/api_gizwits.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef __API_GIZWITS_H
2+
#define __API_GIZWITS_H
3+
4+
#include "sdk_init.h"
5+
6+
7+
// bool GIZWITS_GetConfig(Gizwits_Config_t* config, const char* path);
8+
// int GIZWITS_Connect(Gizwits_t* gizwits, Gizwits_Config_t* config, const char* configPath);
9+
// int GIZWITS_Send(Gizwits_t* gizwits, Gizwits_Action_t action, uint8_t* data, int len);
10+
// int GIZWITS_DoReceive(Gizwits_t* gizwits, OnReceivedOnePacket_Callback_t onOnePacket);
11+
// bool GIZWITS_Ping(Gizwits_t* gizwits);
12+
// int GIZWITS_GetSocket(Gizwits_t* gizwits);
13+
// bool GIZWITS_Close(Gizwits_t* gizwits);
14+
15+
#define GIZWITS_GetConfig CSDK_FUNC(GIZWITS_GetConfig)
16+
#define GIZWITS_Connect CSDK_FUNC(GIZWITS_Connect)
17+
#define GIZWITS_Send CSDK_FUNC(GIZWITS_Send)
18+
#define GIZWITS_DoReceive CSDK_FUNC(GIZWITS_DoReceive)
19+
#define GIZWITS_Ping CSDK_FUNC(GIZWITS_Ping)
20+
#define GIZWITS_GetSocket CSDK_FUNC(GIZWITS_GetSocket)
21+
#define GIZWITS_Close CSDK_FUNC(GIZWITS_Close)
22+
23+
24+
25+
#endif

include/api_inc/api_inc_gizwits.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef __API_INC_GIZWITS_H
2+
#define __API_INC_GIZWITS_H
3+
4+
#include "stdbool.h"
5+
#include "stdint.h"
6+
7+
8+
typedef struct
9+
{
10+
char imei[32];
11+
char did[32];
12+
char passcode[16];
13+
char pk[48];
14+
char pk_secret[48];
15+
char hard_version[16];
16+
char soft_version[16];
17+
uint16_t alive;
18+
}Gizwits_Config_t;
19+
20+
21+
typedef enum{
22+
GIZWITS_FIXED_LENGTH_ACTION_CONTROL = 0x01 ,
23+
GIZWITS_FIXED_LENGTH_ACTION_READ_STATUS = 0x02 ,
24+
GIZWITS_FIXED_LENGTH_ACTION_READ_STATUS_ACK = 0x03 ,
25+
GIZWITS_FIXED_LENGTH_ACTION_REPORT_STATUS = 0x04 ,
26+
GIZWITS_FIXED_LENGTH_ACTION_TRANS_RECV = 0x05 ,
27+
GIZWITS_FIXED_LENGTH_ACTION_TRANS_SEND = 0x06 ,
28+
GIZWITS_FIXED_LENGTH_ACTION_PUSH_OTA = 0xfe ,
29+
30+
GIZWITS_VARIABLE_LENGTH_ACTION_CONTROL = 0x11 ,
31+
GIZWITS_VARIABLE_LENGTH_ACTION_READ_STATUS = 0x12 ,
32+
GIZWITS_VARIABLE_LENGTH_ACTION_READ_STATUS_ACK = 0x13 ,
33+
GIZWITS_VARIABLE_LENGTH_ACTION_REPORT_STATUS = 0x14 ,
34+
GIZWITS_VARIABLE_LENGTH_ACTION_TRANS_RECV = 0x15 ,
35+
GIZWITS_VARIABLE_LENGTH_ACTION_TRANS_SEND = 0x16 ,
36+
GIZWITS_VARIABLE_LENGTH_ACTION_PUSH_OTA = 0xfe ,
37+
38+
GIZWITS_ACTION_MAX
39+
}Gizwits_Action_t;
40+
41+
42+
43+
typedef struct{
44+
Gizwits_Config_t* config;
45+
void* cloud;
46+
char* otaUrl;
47+
}Gizwits_t;
48+
49+
typedef void (*OnReceivedOnePacket_Callback_t)(Gizwits_t* gizwits, Gizwits_Action_t action,uint8_t* data, int len);
50+
51+
52+
#endif

0 commit comments

Comments
 (0)