mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-21 10:25:54 +08:00
更新硬件SDK
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Copyright (c) 2022 OpenLuat & AirM2M
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "common_api.h"
|
||||
#include "luat_mobile.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_debug.h"
|
||||
|
||||
#include "HTTPClient.h"
|
||||
|
||||
//ciphersuite: TLS-RSA-WITH-AES-128-CBC-SHA msglen = 2609
|
||||
//support session ticket/fragment
|
||||
#define TEST_SERVER_NAME "https://www.howsmyssl.com:443/a/check"
|
||||
|
||||
#define TEST_HOST "https://www.howsmyssl.com:443"
|
||||
#define HTTP_RECV_BUF_SIZE (1501)
|
||||
#define HTTP_HEAD_BUF_SIZE (800)
|
||||
|
||||
static HttpClientContext gHttpClient = {0};
|
||||
|
||||
static const char *testCaCrt = \
|
||||
{
|
||||
\
|
||||
"-----BEGIN CERTIFICATE-----\r\n"
|
||||
"MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\r\n" \
|
||||
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\r\n" \
|
||||
"DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\r\n" \
|
||||
"PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\r\n" \
|
||||
"Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\r\n" \
|
||||
"AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\r\n" \
|
||||
"rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\r\n" \
|
||||
"OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\r\n" \
|
||||
"xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\r\n" \
|
||||
"7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\r\n" \
|
||||
"aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\r\n" \
|
||||
"HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\r\n" \
|
||||
"SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\r\n" \
|
||||
"ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\r\n" \
|
||||
"AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\r\n" \
|
||||
"R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\r\n" \
|
||||
"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\r\n" \
|
||||
"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\r\n" \
|
||||
"-----END CERTIFICATE-----"
|
||||
};
|
||||
|
||||
/**
|
||||
\fn INT32 httpGetData(CHAR *getUrl, CHAR *buf, UINT32 len)
|
||||
\brief
|
||||
\return
|
||||
*/
|
||||
static INT32 httpGetData(CHAR *getUrl, CHAR *buf, UINT32 len)
|
||||
{
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
luat_rtos_semaphore_t net_semaphore_handle;
|
||||
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 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;
|
||||
|
||||
luat_mobile_event_register_handler(mobile_event_callback);
|
||||
|
||||
gHttpClient.caCert= (char*)testCaCrt;
|
||||
gHttpClient.caCertLen= strlen(testCaCrt)+1;
|
||||
gHttpClient.timeout_s = 2;
|
||||
gHttpClient.timeout_r = 20;
|
||||
gHttpClient.seclevel = 1;
|
||||
gHttpClient.ciphersuite[0] = 0xFFFF;
|
||||
gHttpClient.ignore = 1;
|
||||
luat_rtos_semaphore_take(net_semaphore_handle, LUAT_WAIT_FOREVER);
|
||||
result = httpConnect(&gHttpClient, TEST_HOST);
|
||||
if (result == HTTP_OK)
|
||||
{
|
||||
httpGetData(TEST_SERVER_NAME, recvBuf, HTTP_RECV_BUF_SIZE);
|
||||
httpClose(&gHttpClient);
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("http client connect error");
|
||||
}
|
||||
|
||||
while(1)
|
||||
{
|
||||
luat_rtos_task_sleep(300000);
|
||||
if (luat_mobile_get_register_status()!=1)
|
||||
{
|
||||
luat_rtos_semaphore_take(net_semaphore_handle, LUAT_WAIT_FOREVER);
|
||||
}
|
||||
result = httpConnect(&gHttpClient, TEST_HOST);
|
||||
if (result == HTTP_OK)
|
||||
{
|
||||
httpGetData(TEST_SERVER_NAME, recvBuf, HTTP_RECV_BUF_SIZE);
|
||||
httpClose(&gHttpClient);
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("http client connect error");
|
||||
}
|
||||
}
|
||||
|
||||
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级
|
||||
INIT_TASK_EXPORT(task_demo_https, "1");
|
||||
|
||||
Reference in New Issue
Block a user