-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
268 lines (237 loc) · 7.94 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of Quectel Co., Ltd. 2013
*
*****************************************************************************/
/*****************************************************************************
*
* Filename:
* ---------
* main.c
*
* Project:
* --------
* OpenCPU
*
* Description:
* ------------
* This app demonstrates how to send AT command with RIL API, and transparently
* transfer the response through MAIN UART. And how to use UART port.
* Developer can program the application based on this example.
*
****************************************************************************/
#ifdef __CUSTOMER_CODE__
#include "custom_feature_def.h"
#include "ril.h"
#include "ril_util.h"
#include "ril_telephony.h"
#include "ql_stdlib.h"
#include "ql_error.h"
#include "ql_trace.h"
#include "ql_uart.h"
#include "ql_system.h"
#include "gps_driver.h"
#include "ql_gpio.h"
#define DEBUG_ENABLE 1
#if DEBUG_ENABLE > 0
#define DEBUG_PORT UART_PORT1
#define DBG_BUF_LEN 512
static char DBG_BUFFER[DBG_BUF_LEN];
#define APP_DEBUG(FORMAT,...) {\
Ql_memset(DBG_BUFFER, 0, DBG_BUF_LEN);\
Ql_sprintf(DBG_BUFFER,FORMAT,##__VA_ARGS__); \
if (UART_PORT2 == (DEBUG_PORT)) \
{\
Ql_Debug_Trace(DBG_BUFFER);\
} else {\
Ql_UART_Write((Enum_SerialPort)(DEBUG_PORT), (u8*)(DBG_BUFFER), Ql_strlen((const char *)(DBG_BUFFER)));\
}\
}
#else
#define APP_DEBUG(FORMAT,...)
#endif
#define SERIAL_RX_BUFFER_LEN 2048
// Define the UART port and the receive data buffer
static Enum_SerialPort m_myUartPort = UART_PORT1;
static u8 m_RxBuf_Uart1[SERIAL_RX_BUFFER_LEN];
static void CallBack_UART_Hdlr(Enum_SerialPort port, Enum_UARTEventType msg, bool level, void* customizedPara);
static s32 ATResponse_Handler(char* line, u32 len, void* userData);
void proc_main_task(s32 taskId)
{
s32 ret;
ST_MSG msg;
// Register & open UART port
ret = Ql_UART_Register(m_myUartPort, CallBack_UART_Hdlr, NULL);
if (ret < QL_RET_OK)
{
Ql_Debug_Trace("Fail to register serial port[%d], ret=%d\r\n", m_myUartPort, ret);
}
ret = Ql_UART_Open(m_myUartPort, 115200, FC_NONE);
if (ret < QL_RET_OK)
{
Ql_Debug_Trace("Fail to open serial port[%d], ret=%d\r\n", m_myUartPort, ret);
}
APP_DEBUG("OpenCPU: Customer Application\r\n");
// START MESSAGE LOOP OF THIS TASK
while(TRUE)
{
Ql_OS_GetMessage(&msg);
switch(msg.message)
{
case MSG_ID_RIL_READY:
APP_DEBUG("<-- RIL is ready -->\r\n");
Ql_RIL_Initialize();
break;
/* case MSG_ID_URC_INDICATION:
//APP_DEBUG("<-- Received URC: type: %d, -->\r\n", msg.param1);
switch (msg.param1)
{
case URC_SYS_INIT_STATE_IND:
APP_DEBUG("<-- Sys Init Status %d -->\r\n", msg.param2);
break;
case URC_SIM_CARD_STATE_IND:
APP_DEBUG("<-- SIM Card Status:%d -->\r\n", msg.param2);
break;
case URC_GSM_NW_STATE_IND:
APP_DEBUG("<-- GSM Network Status:%d -->\r\n", msg.param2);
break;
case URC_GPRS_NW_STATE_IND:
APP_DEBUG("<-- GPRS Network Status:%d -->\r\n", msg.param2);
break;
case URC_CFUN_STATE_IND:
APP_DEBUG("<-- CFUN Status:%d -->\r\n", msg.param2);
break;
case URC_COMING_CALL_IND:
{
ST_ComingCall* pComingCall = (ST_ComingCall*)msg.param2;
APP_DEBUG("<-- Coming call, number:%s, type:%d -->\r\n", pComingCall->phoneNumber, pComingCall->type);
break;
}
case URC_CALL_STATE_IND:
APP_DEBUG("<-- Call state:%d\r\n", msg.param2);
break;
case URC_NEW_SMS_IND:
APP_DEBUG("<-- New SMS Arrives: index=%d\r\n", msg.param2);
break;
case URC_MODULE_VOLTAGE_IND:
APP_DEBUG("<-- VBatt Voltage Ind: type=%d\r\n", msg.param2);
break;
default:
APP_DEBUG("<-- Other URC: type=%d\r\n", msg.param1);
break;
} */
break;
default:
break;
}
}
}
static s32 ReadSerialPort(Enum_SerialPort port, /*[out]*/u8* pBuffer, /*[in]*/u32 bufLen)
{
s32 rdLen = 0;
s32 rdTotalLen = 0;
if (NULL == pBuffer || 0 == bufLen)
{
return -1;
}
Ql_memset(pBuffer, 0x0, bufLen);
while (1)
{
rdLen = Ql_UART_Read(port, pBuffer + rdTotalLen, bufLen - rdTotalLen);
if (rdLen <= 0) // All data is read out, or Serial Port Error!
{
break;
}
rdTotalLen += rdLen;
// Continue to read...
}
if (rdLen < 0) // Serial Port Error!
{
APP_DEBUG("Fail to read from port[%d]\r\n", port);
return -99;
}
return rdTotalLen;
}
static void CallBack_UART_Hdlr(Enum_SerialPort port, Enum_UARTEventType msg, bool level, void* customizedPara)
{
//APP_DEBUG("CallBack_UART_Hdlr: port=%d, event=%d, level=%d, p=%x\r\n", port, msg, level, customizedPara);
switch (msg)
{
case EVENT_UART_READY_TO_READ:
{
if (m_myUartPort == port)
{
s32 totalBytes = ReadSerialPort(port, m_RxBuf_Uart1, sizeof(m_RxBuf_Uart1));
if (totalBytes <= 0)
{
APP_DEBUG("<-- No data in UART buffer! -->\r\n");
return;
}
/* {// Read data from UART
s32 ret;
char* pCh = NULL;
// Echo
Ql_UART_Write(m_myUartPort, m_RxBuf_Uart1, totalBytes);
pCh = Ql_strstr((char*)m_RxBuf_Uart1, "\r\n");
if (pCh)
{
*(pCh + 0) = '\0';
*(pCh + 1) = '\0';
}
// No permission for single <cr><lf>
if (Ql_strlen((char*)m_RxBuf_Uart1) == 0)
{
return;
}
ret = Ql_RIL_SendATCmd((char*)m_RxBuf_Uart1, totalBytes, ATResponse_Handler, NULL, 0);
}*/
char* p = NULL;
p = Ql_strstr(m_RxBuf_Uart1, "GPSstart");
if(p)
{
//GPS work here
GPS_Data gps_data;
PowerOnGPS();
APP_DEBUG("Loading data....\r\n"); //waiting to receive data
Ql_Sleep(50000);//sleep for 50 s
GPS_receive_position(&gps_data);
GPS_receive_PDOP(&gps_data);
GPS_error_deviation(&gps_data);//calculate the deviation ( error of localisation) of the gps
GPS_show_data(&gps_data);
PowerOffGPS();
}
}
break;
}
case EVENT_UART_READY_TO_WRITE:
break;
default:
break;
}
}
static s32 ATResponse_Handler(char* line, u32 len, void* userData)
{
Ql_UART_Write(m_myUartPort, (u8*)line, len);
if (Ql_RIL_FindLine(line, len, "OK"))
{
return RIL_ATRSP_SUCCESS;
}
else if (Ql_RIL_FindLine(line, len, "ERROR"))
{
return RIL_ATRSP_FAILED;
}
else if (Ql_RIL_FindString(line, len, "+CME ERROR"))
{
return RIL_ATRSP_FAILED;
}
else if (Ql_RIL_FindString(line, len, "+CMS ERROR:"))
{
return RIL_ATRSP_FAILED;
}
return RIL_ATRSP_CONTINUE; //continue wait
}
#endif // __CUSTOMER_CODE__