mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-21 18:35:54 +08:00
更新硬件SDK
This commit is contained in:
@@ -0,0 +1,332 @@
|
||||
/*
|
||||
* Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
|
||||
* This program is free software. It comes without any warranty, to the extent
|
||||
* permitted by applicable law. You can redistribute it and/or modify it under
|
||||
* the terms of the Do What The Fuck You Want To Public License, Version 2, as
|
||||
* published by Sam Hocevar. See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#include "minmea.h"
|
||||
#include "common_api.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_debug.h"
|
||||
#include "luat_uart.h"
|
||||
#include "luat_gpio.h"
|
||||
#include "HTTPClient.h"
|
||||
#include "luat_mobile.h"
|
||||
|
||||
// 注意:780EG内部gps与串口2相连
|
||||
#define UART_ID 2
|
||||
#define HTTP_RECV_BUF_SIZE (6000)
|
||||
#define HTTP_HEAD_BUF_SIZE (800)
|
||||
#define TEST_HOST "http://download.openluat.com/9501-xingli/HXXT_GPS_BDS_AGNSS_DATA.dat"
|
||||
|
||||
|
||||
static HttpClientContext gHttpClient = {0};
|
||||
|
||||
static luat_rtos_task_handle gps_task_handle;
|
||||
static luat_rtos_semaphore_t net_semaphore_handle;
|
||||
static luat_rtos_task_handle https_task_handle;
|
||||
|
||||
void mobile_event_callback(LUAT_MOBILE_EVENT_E event, uint8_t index, uint8_t status){
|
||||
if (event == LUAT_MOBILE_EVENT_NETIF && status == LUAT_MOBILE_NETIF_LINK_ON){
|
||||
LUAT_DEBUG_PRINT("network ready");
|
||||
luat_rtos_semaphore_release(net_semaphore_handle);
|
||||
}
|
||||
}
|
||||
|
||||
static int libminmea_parse_data(const char* data, size_t len) {
|
||||
size_t prev = 0;
|
||||
static char nmea_tmp_buff[86] = {0}; // nmea 最大长度82,含换行符
|
||||
for (size_t offset = 0; offset < len; offset++)
|
||||
{
|
||||
// \r == 0x0D \n == 0x0A
|
||||
if (data[offset] == 0x0A) {
|
||||
// 最短也需要是 OK\r\n
|
||||
// 应该\r\n的
|
||||
// 太长了
|
||||
if (offset - prev < 3 || data[offset - 1] != 0x0D || offset - prev > 82) {
|
||||
prev = offset + 1;
|
||||
continue;
|
||||
}
|
||||
memcpy(nmea_tmp_buff, data + prev, offset - prev - 1);
|
||||
nmea_tmp_buff[offset - prev - 1] = 0x00;
|
||||
if(strstr(nmea_tmp_buff, "GNRMC"))
|
||||
{
|
||||
parse_nmea((const char*)nmea_tmp_buff);
|
||||
}
|
||||
prev = offset + 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void luat_uart_recv_cb(int uart_id, uint32_t data_len){
|
||||
char* data_buff = malloc(data_len+1);
|
||||
memset(data_buff,0,data_len+1);
|
||||
luat_uart_read(uart_id, data_buff, data_len);
|
||||
LUAT_DEBUG_PRINT("uart_id:%d gnssdata:\n %s",uart_id,data_buff);
|
||||
libminmea_parse_data(data_buff, data_len);
|
||||
free(data_buff);
|
||||
}
|
||||
|
||||
static INT32 httpGetData(CHAR *getUrl, CHAR *buf, UINT32 len, UINT32 *dataLen)
|
||||
{
|
||||
HTTPResult result = HTTP_INTERNAL;
|
||||
HttpClientData clientData = {0};
|
||||
UINT32 count = 0;
|
||||
uint16_t headerLen = 0;
|
||||
|
||||
LUAT_DEBUG_ASSERT(buf != NULL,0,0,0);
|
||||
|
||||
clientData.headerBuf = malloc(HTTP_HEAD_BUF_SIZE);
|
||||
clientData.headerBufLen = HTTP_HEAD_BUF_SIZE;
|
||||
clientData.respBuf = buf;
|
||||
clientData.respBufLen = len;
|
||||
|
||||
result = httpSendRequest(&gHttpClient, getUrl, HTTP_GET, &clientData);
|
||||
LUAT_DEBUG_PRINT("send request result=%d", result);
|
||||
if (result != HTTP_OK)
|
||||
goto exit;
|
||||
do {
|
||||
LUAT_DEBUG_PRINT("recvResponse loop.");
|
||||
memset(clientData.headerBuf, 0, clientData.headerBufLen);
|
||||
memset(clientData.respBuf, 0, clientData.respBufLen);
|
||||
result = httpRecvResponse(&gHttpClient, &clientData);
|
||||
if(result == HTTP_OK || result == HTTP_MOREDATA){
|
||||
headerLen = strlen(clientData.headerBuf);
|
||||
if(headerLen > 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("total content length=%d", clientData.recvContentLength);
|
||||
}
|
||||
|
||||
if(clientData.blockContentLen > 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("response content:{%s}", (uint8_t*)clientData.respBuf);
|
||||
}
|
||||
count += clientData.blockContentLen;
|
||||
*dataLen = *dataLen + count;
|
||||
LUAT_DEBUG_PRINT("has recv=%d", count);
|
||||
}
|
||||
} while (result == HTTP_MOREDATA || result == HTTP_CONN);
|
||||
|
||||
LUAT_DEBUG_PRINT("result=%d", result);
|
||||
if (gHttpClient.httpResponseCode < 200 || gHttpClient.httpResponseCode > 404)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("invalid http response code=%d",gHttpClient.httpResponseCode);
|
||||
} else if (count == 0 || count != clientData.recvContentLength) {
|
||||
LUAT_DEBUG_PRINT("data not receive complete");
|
||||
} else {
|
||||
LUAT_DEBUG_PRINT("receive success");
|
||||
}
|
||||
exit:
|
||||
free(clientData.headerBuf);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static void task_test_https(void *param)
|
||||
{
|
||||
luat_rtos_semaphore_create(&net_semaphore_handle, 1);
|
||||
|
||||
char *recvBuf = malloc(HTTP_RECV_BUF_SIZE);
|
||||
HTTPResult result = HTTP_INTERNAL;
|
||||
uint32_t dataLen = 0;
|
||||
luat_mobile_event_register_handler(mobile_event_callback);
|
||||
gHttpClient.timeout_s = 2;
|
||||
gHttpClient.timeout_r = 20;
|
||||
luat_rtos_semaphore_take(net_semaphore_handle, LUAT_WAIT_FOREVER);
|
||||
result = httpConnect(&gHttpClient, TEST_HOST);
|
||||
if (result == HTTP_OK)
|
||||
{
|
||||
result = httpGetData(TEST_HOST, recvBuf, HTTP_RECV_BUF_SIZE, &dataLen);
|
||||
httpClose(&gHttpClient);
|
||||
|
||||
if (result == HTTP_OK)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("http client get data success %d", dataLen);
|
||||
|
||||
for (size_t i = 0; i < dataLen; i = i + 512)
|
||||
{
|
||||
if (i + 512 < dataLen)
|
||||
luat_uart_write(UART_ID, &recvBuf[i], 512);
|
||||
else
|
||||
luat_uart_write(UART_ID, &recvBuf[i], dataLen - i);
|
||||
luat_rtos_task_sleep(100);
|
||||
}
|
||||
}
|
||||
demo_udp_init();
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("http client connect error");
|
||||
}
|
||||
memset(recvBuf, 0x00, HTTP_RECV_BUF_SIZE);
|
||||
free(recvBuf);
|
||||
luat_rtos_task_delete(https_task_handle);
|
||||
}
|
||||
|
||||
static void task_demo_https(void)
|
||||
{
|
||||
// https所需的栈空间会大很多
|
||||
luat_rtos_task_create(&https_task_handle, 32*1024, 20, "https", task_test_https, NULL, NULL);
|
||||
}
|
||||
|
||||
//启动task_demoF_init,启动位置任务2级
|
||||
|
||||
static void task_test_gps(void *param)
|
||||
{
|
||||
|
||||
luat_uart_t uart = {
|
||||
.id = UART_ID,
|
||||
.baud_rate = 115200,
|
||||
.data_bits = 8,
|
||||
.stop_bits = 1,
|
||||
.parity = 0
|
||||
};
|
||||
luat_uart_pre_setup(UART_ID, 1);
|
||||
luat_uart_setup(&uart);
|
||||
|
||||
luat_uart_ctrl(UART_ID, LUAT_UART_SET_RECV_CALLBACK, luat_uart_recv_cb);
|
||||
|
||||
// gps电源
|
||||
luat_gpio_cfg_t cfg = {0};
|
||||
cfg.pin = HAL_GPIO_13;
|
||||
cfg.mode = LUAT_GPIO_OUTPUT;
|
||||
cfg.output_level = 1;
|
||||
cfg.alt_fun = 4;
|
||||
luat_gpio_open(&cfg);
|
||||
luat_rtos_task_sleep(200);
|
||||
while (1)
|
||||
{
|
||||
char cmd2[] = "$AIDINFO";
|
||||
luat_uart_write(UART_ID, cmd2, strlen(cmd2));
|
||||
luat_rtos_task_sleep(5000);
|
||||
}
|
||||
luat_rtos_task_delete(gps_task_handle);
|
||||
}
|
||||
|
||||
|
||||
#define INDENT_SPACES " "
|
||||
|
||||
int parse_nmea(const char *gpsdata)
|
||||
{
|
||||
switch (minmea_sentence_id(gpsdata, false)) {
|
||||
case MINMEA_SENTENCE_RMC: {
|
||||
struct minmea_sentence_rmc frame;
|
||||
if (minmea_parse_rmc(&frame, gpsdata)) {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxRMC: raw coordinates and speed: (%d/%d,%d/%d) %d/%d\n",
|
||||
frame.latitude.value, frame.latitude.scale,
|
||||
frame.longitude.value, frame.longitude.scale,
|
||||
frame.speed.value, frame.speed.scale);
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxRMC fixed-point coordinates and speed scaled to three decimal places: (%d,%d) %d\n",
|
||||
minmea_rescale(&frame.latitude, 1000),
|
||||
minmea_rescale(&frame.longitude, 1000),
|
||||
minmea_rescale(&frame.speed, 1000));
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxRMC floating point degree coordinates and speed: (%f,%f) %f\n",
|
||||
minmea_tocoord(&frame.latitude),
|
||||
minmea_tocoord(&frame.longitude),
|
||||
minmea_tofloat(&frame.speed));
|
||||
}
|
||||
else {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxRMC sentence is not parsed\n");
|
||||
}
|
||||
} break;
|
||||
case MINMEA_SENTENCE_GGA: {
|
||||
struct minmea_sentence_gga frame;
|
||||
if (minmea_parse_gga(&frame, gpsdata)) {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGGA: fix quality: %d\n", frame.fix_quality);
|
||||
}
|
||||
else {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGGA sentence is not parsed\n");
|
||||
}
|
||||
} break;
|
||||
case MINMEA_SENTENCE_GST: {
|
||||
struct minmea_sentence_gst frame;
|
||||
if (minmea_parse_gst(&frame, gpsdata)) {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGST: raw latitude,longitude and altitude error deviation: (%d/%d,%d/%d,%d/%d)\n",
|
||||
frame.latitude_error_deviation.value, frame.latitude_error_deviation.scale,
|
||||
frame.longitude_error_deviation.value, frame.longitude_error_deviation.scale,
|
||||
frame.altitude_error_deviation.value, frame.altitude_error_deviation.scale);
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGST fixed point latitude,longitude and altitude error deviation"
|
||||
" scaled to one decimal place: (%d,%d,%d)\n",
|
||||
minmea_rescale(&frame.latitude_error_deviation, 10),
|
||||
minmea_rescale(&frame.longitude_error_deviation, 10),
|
||||
minmea_rescale(&frame.altitude_error_deviation, 10));
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGST floating point degree latitude, longitude and altitude error deviation: (%f,%f,%f)",
|
||||
minmea_tofloat(&frame.latitude_error_deviation),
|
||||
minmea_tofloat(&frame.longitude_error_deviation),
|
||||
minmea_tofloat(&frame.altitude_error_deviation));
|
||||
}
|
||||
else {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGST sentence is not parsed\n");
|
||||
}
|
||||
} break;
|
||||
case MINMEA_SENTENCE_GSV: {
|
||||
struct minmea_sentence_gsv frame;
|
||||
if (minmea_parse_gsv(&frame, gpsdata)) {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGSV: message %d of %d\n", frame.msg_nr, frame.total_msgs);
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGSV: satellites in view: %d\n", frame.total_sats);
|
||||
for (int i = 0; i < 4; i++)
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGSV: sat nr %d, elevation: %d, azimuth: %d, snr: %d dbm\n",
|
||||
frame.sats[i].nr,
|
||||
frame.sats[i].elevation,
|
||||
frame.sats[i].azimuth,
|
||||
frame.sats[i].snr);
|
||||
}
|
||||
else {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxGSV sentence is not parsed\n");
|
||||
}
|
||||
} break;
|
||||
case MINMEA_SENTENCE_VTG: {
|
||||
struct minmea_sentence_vtg frame;
|
||||
if (minmea_parse_vtg(&frame, gpsdata)) {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxVTG: true track degrees = %f\n",
|
||||
minmea_tofloat(&frame.true_track_degrees));
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES " magnetic track degrees = %f\n",
|
||||
minmea_tofloat(&frame.magnetic_track_degrees));
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES " speed knots = %f\n",
|
||||
minmea_tofloat(&frame.speed_knots));
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES " speed kph = %f\n",
|
||||
minmea_tofloat(&frame.speed_kph));
|
||||
}
|
||||
else {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxVTG sentence is not parsed\n");
|
||||
}
|
||||
} break;
|
||||
case MINMEA_SENTENCE_ZDA: {
|
||||
struct minmea_sentence_zda frame;
|
||||
if (minmea_parse_zda(&frame, gpsdata)) {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxZDA: %d:%d:%d %02d.%02d.%d UTC%+03d:%02d\n",
|
||||
frame.time.hours,
|
||||
frame.time.minutes,
|
||||
frame.time.seconds,
|
||||
frame.date.day,
|
||||
frame.date.month,
|
||||
frame.date.year,
|
||||
frame.hour_offset,
|
||||
frame.minute_offset);
|
||||
}
|
||||
else {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxZDA sentence is not parsed\n");
|
||||
}
|
||||
} break;
|
||||
case MINMEA_INVALID: {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxxxx sentence is not valid\n");
|
||||
} break;
|
||||
default: {
|
||||
LUAT_DEBUG_PRINT(INDENT_SPACES "$xxxxx sentence is not parsed\n");
|
||||
} break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void task_demo_gps(void)
|
||||
{
|
||||
luat_rtos_task_create(&gps_task_handle, 1024 * 20, 20, "gps", task_test_gps, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
INIT_TASK_EXPORT(task_demo_gps,"1");
|
||||
INIT_TASK_EXPORT(task_demo_https, "2");
|
||||
/* vim: set ts=4 sw=4 et: */
|
||||
@@ -0,0 +1,325 @@
|
||||
#include "common_api.h"
|
||||
#include "sockets.h"
|
||||
#include "dns.h"
|
||||
#include "lwip/ip4_addr.h"
|
||||
#include "netdb.h"
|
||||
#include "luat_debug.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_mobile.h"
|
||||
#include "string.h"
|
||||
#include "lbsLoc.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define DEMO_SERVER_UDP_IP "bs.openluat.com" // 基站定位网址
|
||||
#define DEMO_SERVER_UDP_PORT 12411 // 端口
|
||||
|
||||
/// @brief 合宙IOT 项目productkey ,必须加上,否则定位失败
|
||||
static char *productKey = "vQfJesoQdXDK8X1sXkYg0KTU42UBhdjh";
|
||||
static luat_rtos_task_handle lbsloc_task_handle = NULL;
|
||||
|
||||
static uint8_t lbsloc_task_tatus = 0;
|
||||
|
||||
static bool ddddtoddmm(char *location, char *test)
|
||||
{
|
||||
char *integer = NULL;
|
||||
char *fraction = NULL;
|
||||
char tmpstr[15] = {0};
|
||||
float tmp = 0;
|
||||
integer = strtok(location, ".");
|
||||
if(integer)
|
||||
{
|
||||
fraction = strtok(NULL, ".");
|
||||
sprintf(tmpstr, "0.%d", atoi(fraction));
|
||||
tmp = atof(tmpstr) * 60;
|
||||
tmp = atoi(integer) * 100 + tmp;
|
||||
memset(tmpstr, 0x00, 15);
|
||||
sprintf(tmpstr, "%f", tmp);
|
||||
memcpy(test, tmpstr, strlen(tmpstr) + 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/// @brief 把string 转换为BCD 编码
|
||||
/// @param arr 字符串输入
|
||||
/// @param len 长度
|
||||
/// @param outPut 输出
|
||||
/// @return
|
||||
static uint8_t imeiToBcd(uint8_t *arr, uint8_t len, uint8_t *outPut)
|
||||
{
|
||||
if (len % 2 != 0)
|
||||
{
|
||||
arr[len] = 0x0f;
|
||||
}
|
||||
|
||||
uint8_t tmp = 0;
|
||||
|
||||
for (uint8_t j = 0; j < len; j = j + 2)
|
||||
{
|
||||
outPut[tmp] = (arr[j] & 0x0f) << 4 | (arr[j + 1] & 0x0f);
|
||||
tmp++;
|
||||
}
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
outPut[i] = (outPut[i] % 0x10) * 0x10 + (outPut[i] - (outPut[i] % 0x10)) / 0x10;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief BCD ->> str
|
||||
/// @param pOutBuffer
|
||||
/// @param pInBuffer
|
||||
/// @param nInLen 长度
|
||||
/// @return
|
||||
static uint32_t location_service_bcd_to_str(uint8_t *pOutBuffer, uint8_t *pInBuffer, uint32_t nInLen)
|
||||
{
|
||||
uint32_t len = 0;
|
||||
uint8_t ch;
|
||||
uint8_t *p = pOutBuffer;
|
||||
uint32_t i = 0;
|
||||
|
||||
if (pOutBuffer == NULL || pInBuffer == NULL || nInLen == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < nInLen; i++)
|
||||
{
|
||||
ch = pInBuffer[i] & 0x0F;
|
||||
if (ch == 0x0F)
|
||||
{
|
||||
break;
|
||||
}
|
||||
*pOutBuffer++ = ch + '0';
|
||||
|
||||
ch = (pInBuffer[i] >> 4) & 0x0F;
|
||||
if (ch == 0x0F)
|
||||
{
|
||||
break;
|
||||
}
|
||||
*pOutBuffer++ = ch + '0';
|
||||
}
|
||||
|
||||
len = pOutBuffer - p;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool location_service_parse_response(struct am_location_service_rsp_data_t *response, uint8_t *latitude, uint8_t *longitude,
|
||||
uint16_t *year, uint8_t *month, uint8_t *day, uint8_t *hour, uint8_t *minute, uint8_t *second)
|
||||
{
|
||||
uint8_t loc[20] = {0};
|
||||
uint32_t len = 0;
|
||||
|
||||
if (response == NULL || latitude == NULL || longitude == NULL || year == NULL || month == NULL || day == NULL || hour == NULL || minute == NULL || second == NULL)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("location_service_parse_response: invalid parameter");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!(response->result == 0 || response->result == 0xFF))
|
||||
{
|
||||
LUAT_DEBUG_PRINT("location_service_parse_response: result fail %d", response->result);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// latitude
|
||||
len = location_service_bcd_to_str(loc, response->latitude, AM_LOCATION_SERVICE_LOCATION_BCD_LEN);
|
||||
if (len <= 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("location_service_parse_response: latitude fail");
|
||||
return FALSE;
|
||||
}
|
||||
strncat((char *)latitude, (char *)loc, 3);
|
||||
strncat((char *)latitude, ".", 2);
|
||||
strncat((char *)latitude, (char *)(loc + 3), len - 3);
|
||||
len = location_service_bcd_to_str(loc, response->longitude, AM_LOCATION_SERVICE_LOCATION_BCD_LEN);
|
||||
if (len <= 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("location_service_parse_response: longitude fail");
|
||||
return FALSE;
|
||||
}
|
||||
strncat((char *)longitude, (char *)loc, 3);
|
||||
strncat((char *)longitude, (char *)".", 2);
|
||||
strncat((char *)longitude, (char *)(loc + 3), len - 3);
|
||||
*year = response->year + 2000;
|
||||
*month = response->month;
|
||||
*day = response->day;
|
||||
*hour = response->hour;
|
||||
*minute = response->minute;
|
||||
*second = response->second;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void lbsloc_task(void *arg)
|
||||
{
|
||||
lbsloc_task_tatus = 1;
|
||||
ip_addr_t remote_ip;
|
||||
struct sockaddr_in name;
|
||||
socklen_t sockaddr_t_size = sizeof(name);
|
||||
int ret;
|
||||
struct timeval to;
|
||||
int socket_id = -1;
|
||||
struct hostent dns_result;
|
||||
struct hostent *p_result;
|
||||
int h_errnop, read_len;
|
||||
struct am_location_service_rsp_data_t locationServiceResponse;
|
||||
|
||||
uint8_t latitude[20] = {0}; // 经度
|
||||
uint8_t longitude[20] = {0}; // 维度
|
||||
uint16_t year = 0; // 年
|
||||
uint8_t month = 0; // 月
|
||||
uint8_t day = 0; // 日
|
||||
uint8_t hour = 0; // 小时
|
||||
uint8_t minute = 0; // 分钟
|
||||
uint8_t second = 0; // 秒
|
||||
uint8_t lbsLocReqBuf[176] = {0};
|
||||
memset(lbsLocReqBuf, 0, 176);
|
||||
uint8_t sendLen = 0;
|
||||
lbsLocReqBuf[sendLen++] = strlen(productKey);
|
||||
memcpy(&lbsLocReqBuf[sendLen], (UINT8 *)productKey, strlen(productKey));
|
||||
sendLen = sendLen + strlen(productKey);
|
||||
lbsLocReqBuf[sendLen++] = 0x28;
|
||||
CHAR imeiBuf[16];
|
||||
memset(imeiBuf, 0, sizeof(imeiBuf));
|
||||
luat_mobile_get_imei(0, imeiBuf, 16);
|
||||
uint8_t imeiBcdBuf[8] = {0};
|
||||
imeiToBcd((uint8_t *)imeiBuf, 15, imeiBcdBuf);
|
||||
memcpy(&lbsLocReqBuf[sendLen], imeiBcdBuf, 8);
|
||||
sendLen = sendLen + 8;
|
||||
CHAR muidBuf[64];
|
||||
memset(muidBuf, 0, sizeof(muidBuf));
|
||||
luat_mobile_get_muid(muidBuf, sizeof(muidBuf));
|
||||
lbsLocReqBuf[sendLen++] = strlen(muidBuf);
|
||||
memcpy(&lbsLocReqBuf[sendLen], (UINT8 *)muidBuf, strlen(muidBuf));
|
||||
sendLen = sendLen + strlen(muidBuf);
|
||||
luat_mobile_cell_info_t cell_info;
|
||||
memset(&cell_info, 0, sizeof(cell_info));
|
||||
luat_mobile_get_cell_info(&cell_info);
|
||||
lbsLocReqBuf[sendLen++] = 0x01;
|
||||
lbsLocReqBuf[sendLen++] = (cell_info.lte_service_info.tac >> 8) & 0xFF;
|
||||
lbsLocReqBuf[sendLen++] = cell_info.lte_service_info.tac & 0xFF;
|
||||
lbsLocReqBuf[sendLen++] = (cell_info.lte_service_info.mcc >> 8) & 0xFF;
|
||||
lbsLocReqBuf[sendLen++] = cell_info.lte_service_info.mcc & 0XFF;
|
||||
uint8_t mnc = cell_info.lte_service_info.mnc;
|
||||
if (mnc > 10)
|
||||
{
|
||||
CHAR buf[3] = {0};
|
||||
snprintf(buf, 3, "%02x", mnc);
|
||||
int ret1 = atoi(buf);
|
||||
lbsLocReqBuf[sendLen++] = ret1;
|
||||
}
|
||||
else
|
||||
{
|
||||
lbsLocReqBuf[sendLen++] = mnc;
|
||||
}
|
||||
int16_t sRssi;
|
||||
uint8_t retRssi;
|
||||
sRssi = cell_info.lte_service_info.rssi;
|
||||
if (sRssi <= -113)
|
||||
{
|
||||
retRssi = 0;
|
||||
}
|
||||
else if (sRssi < -52)
|
||||
{
|
||||
retRssi = (sRssi + 113) >> 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
retRssi = 31;
|
||||
}
|
||||
lbsLocReqBuf[sendLen++] = retRssi;
|
||||
lbsLocReqBuf[sendLen++] = (cell_info.lte_service_info.cid >> 24) & 0xFF;
|
||||
lbsLocReqBuf[sendLen++] = (cell_info.lte_service_info.cid >> 16) & 0xFF;
|
||||
lbsLocReqBuf[sendLen++] = (cell_info.lte_service_info.cid >> 8) & 0xFF;
|
||||
lbsLocReqBuf[sendLen++] = cell_info.lte_service_info.cid & 0xFF;
|
||||
|
||||
char hostname[128] = {0};
|
||||
ret = lwip_gethostbyname_r(DEMO_SERVER_UDP_IP, &dns_result, hostname, 128, &p_result, &h_errnop);
|
||||
if (!ret)
|
||||
{
|
||||
remote_ip = *((ip_addr_t *)dns_result.h_addr_list[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
luat_rtos_task_sleep(1000);
|
||||
LUAT_DEBUG_PRINT("dns fail");
|
||||
lbsloc_task_tatus = 0;
|
||||
luat_rtos_task_delete(lbsloc_task_handle);
|
||||
return;
|
||||
}
|
||||
|
||||
socket_id = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = AM_LOCATION_SERVICE_RCV_TIMEOUT;
|
||||
timeout.tv_usec = 0;
|
||||
setsockopt(socket_id, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
memset(&name, 0, sizeof(name));
|
||||
name.sin_family = AF_INET;
|
||||
name.sin_addr.s_addr = remote_ip.u_addr.ip4.addr;
|
||||
name.sin_port = htons(DEMO_SERVER_UDP_PORT);
|
||||
ret = sendto(socket_id, lbsLocReqBuf, sendLen, 0, (const struct sockaddr *)&name, sockaddr_t_size);
|
||||
if (ret == sendLen)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("lbsLocSendRequest send lbsLoc request success");
|
||||
ret = recv(socket_id, &locationServiceResponse, sizeof(struct am_location_service_rsp_data_t), 0);
|
||||
if (sizeof(struct am_location_service_rsp_data_t) == ret)
|
||||
{
|
||||
if (location_service_parse_response(&locationServiceResponse, latitude, longitude, &year, &month, &day, &hour, &minute, &second) == TRUE)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("latitude:%s,longitude:%s,year:%d,month:%d,day:%d,hour:%d,minute:%d,second:%d\r\n", latitude, longitude, year, month, day, hour, minute, second);
|
||||
char data_buf[60] = {0};
|
||||
snprintf(data_buf, 60, "$AIDTIME,%d,%d,%d,%d,%d,%d,000\r\n", year, month, day, hour, minute, second);
|
||||
luat_uart_write(2, data_buf, strlen(data_buf));
|
||||
LUAT_DEBUG_PRINT("this is test1 %s", data_buf);
|
||||
luat_rtos_task_sleep(200);
|
||||
|
||||
memset(data_buf, 0x00, 60);
|
||||
|
||||
char lat[20] = {0};
|
||||
char lng[20] = {0};
|
||||
ddddtoddmm(latitude, lat);
|
||||
LUAT_DEBUG_PRINT("this is test2 %s", lat);
|
||||
ddddtoddmm(longitude, lng);
|
||||
LUAT_DEBUG_PRINT("this is test3 %s", lng);
|
||||
snprintf(data_buf, 60, "$AIDPOS,%s,N,%s,E,1.0\r\n", lat, lng);
|
||||
LUAT_DEBUG_PRINT("this is test4 %s", data_buf);
|
||||
luat_uart_write(2, data_buf, strlen(data_buf));
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("location_service_task: rcv response, but process fail");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("lbsLocSendRequest send lbsLoc request fail");
|
||||
}
|
||||
memset(latitude, 0, 20);
|
||||
memset(longitude, 0, 20);
|
||||
year = 0;
|
||||
month = 0;
|
||||
day = 0;
|
||||
hour = 0;
|
||||
minute = 0;
|
||||
second = 0;
|
||||
LUAT_DEBUG_PRINT("socket quit");
|
||||
close(socket_id);
|
||||
socket_id = -1;
|
||||
lbsloc_task_tatus = 0;
|
||||
luat_rtos_task_delete(lbsloc_task_handle);
|
||||
}
|
||||
|
||||
void demo_udp_init(void)
|
||||
{
|
||||
if (lbsloc_task_handle == NULL || lbsloc_task_tatus == 0)
|
||||
luat_rtos_task_create(&lbsloc_task_handle, 4 * 2048, 80, "udp", lbsloc_task, NULL, NULL);
|
||||
else
|
||||
LUAT_DEBUG_PRINT("lbsloc task create fail");
|
||||
}
|
||||
Reference in New Issue
Block a user