添加智能灯固件代码

This commit is contained in:
kerwincui
2021-07-13 17:14:51 +08:00
parent 332f74dd17
commit ecc0b91b8b
2568 changed files with 229441 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
idf_component_register(SRCS "pcap.c"
INCLUDE_DIRS .)

View File

@@ -0,0 +1,11 @@
#
# Component Makefile
#
# This Makefile should, at the very least, just include $(SDK_PATH)/Makefile. By default,
# this will take the sources in the src/ directory, compile them and link them into
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
# please read the SDK documents if you need to do this.
#
#include $(IDF_PATH)/make/component_common.mk
COMPONENT_ADD_INCLUDEDIRS := .

View File

@@ -0,0 +1,138 @@
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
#include "esp_types.h"
#include "esp_err.h"
#include "esp_log.h"
#include "pcap.h"
static const char *PCAP_TAG = "pcap";
#define PCAP_CHECK(a, str, goto_tag, ...) \
do \
{ \
if (!(a)) \
{ \
ESP_LOGE(PCAP_TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
goto goto_tag; \
} \
} while (0)
/**
* @brief Pcap File Header
*
*/
typedef struct {
uint32_t magic; /*!< Magic Number */
uint16_t major; /*!< Major Version */
uint16_t minor; /*!< Minor Version */
uint32_t zone; /*!< Time Zone Offset */
uint32_t sigfigs; /*!< Timestamp Accuracy */
uint32_t snaplen; /*!< Max Length to Capture */
uint32_t link_type; /*!< Link Layer Type */
} pcap_file_header_t;
/**
* @brief Pcap Packet Header
*
*/
typedef struct {
uint32_t seconds; /*!< Number of seconds since January 1st, 1970, 00:00:00 GMT */
uint32_t microseconds; /*!< Number of microseconds when the packet was captured (offset from seconds) */
uint32_t capture_length; /*!< Number of bytes of captured data, no longer than packet_length */
uint32_t packet_length; /*!< Actual length of current packet */
} pcap_packet_header_t;
/**
* @brief Pcap Runtime Handle
*
*/
typedef struct {
FILE *file; /*!< File handle */
} pcap_runtime_t;
esp_err_t pcap_capture_packet(pcap_handle_t handle, void *payload, uint32_t length, uint32_t seconds, uint32_t microseconds)
{
PCAP_CHECK(handle, "pcap handle is NULL", err);
pcap_runtime_t *pcap_rt = (pcap_runtime_t *)handle;
PCAP_CHECK(pcap_rt->file, "pcap file is NULL", err);
size_t real_write = 0;
pcap_packet_header_t header = {
.seconds = seconds,
.microseconds = microseconds,
.capture_length = length,
.packet_length = length
};
real_write = fwrite(&header, sizeof(header), 1, pcap_rt->file);
PCAP_CHECK(real_write == 1, "write packet header error", err);
real_write = fwrite(payload, sizeof(uint8_t), length, pcap_rt->file);
PCAP_CHECK(real_write == length, "write packet payload error", err);
/* Flush content in the buffer into device */
fflush(pcap_rt->file);
return ESP_OK;
err:
return ESP_FAIL;
}
esp_err_t pcap_deinit(pcap_handle_t handle)
{
PCAP_CHECK(handle, "pcap handle is NULL", err);
pcap_runtime_t *pcap_rt = (pcap_runtime_t *)handle;
PCAP_CHECK(pcap_rt->file, "pcap file is NULL", err);
PCAP_CHECK(fclose(pcap_rt->file) == 0, "close pcap file failed", err);
pcap_rt->file = NULL;
free(pcap_rt);
ESP_LOGD(PCAP_TAG, "pcap deinit OK");
return ESP_OK;
err:
ESP_LOGW(PCAP_TAG, "pcap deinit failed");
return ESP_FAIL;
}
esp_err_t pcap_init(pcap_config_t *config, pcap_handle_t *handle)
{
PCAP_CHECK(config, "config is NULL", err);
PCAP_CHECK(handle, "pcap handle is NULL", err);
pcap_runtime_t *pcap_rt = calloc(sizeof(pcap_runtime_t), 1);
PCAP_CHECK(pcap_rt, "calloc pcap runtime failed", err);
pcap_rt->file = config->fp;
/* Write Pcap File header */
pcap_file_header_t header = {
.magic = PCAP_MAGIC_BIG_ENDIAN,
.major = PCAP_VERSION_MAJOR,
.minor = PCAP_VERSION_MINOR,
.zone = PCAP_TIME_ZONE_GMT,
.sigfigs = 0,
.snaplen = 0x40000,
.link_type = config->link_type
};
size_t real_write = fwrite(&header, sizeof(header), 1, pcap_rt->file);
PCAP_CHECK(real_write == 1, "write pcap file header failed", err_write);
/* Flush content in the buffer into device */
fflush(pcap_rt->file);
*handle = (pcap_handle_t)pcap_rt;
ESP_LOGD(PCAP_TAG, "pcap init OK");
return ESP_OK;
/* Error Handling */
err_write:
fclose(pcap_rt->file);
pcap_rt->file = NULL;
free(pcap_rt);
err:
ESP_LOGW(PCAP_TAG, "pcap init failed");
return ESP_FAIL;
}

View File

@@ -0,0 +1,105 @@
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include "esp_err.h"
#define PCAP_MAGIC_BIG_ENDIAN 0xA1B2C3D4 /*!< Big-Endian */
#define PCAP_MAGIC_LITTLE_ENDIAN 0xD4C3B2A1 /*!< Little-Endian */
#define PCAP_VERSION_MAJOR 0x02 /*!< Major Version */
#define PCAP_VERSION_MINOR 0x04 /*!< Minor Version */
#define PCAP_TIME_ZONE_GMT 0x00 /*!< Time Zone */
/**
* @brief Link layer Type Definition, used for Pcap reader to decode payload
*
*/
typedef enum {
PCAP_LINK_TYPE_LOOPBACK = 0, /*!< Loopback devices, except for later OpenBSD */
PCAP_LINK_TYPE_ETHERNET = 1, /*!< Ethernet, and Linux loopback devices */
PCAP_LINK_TYPE_TOKEN_RING = 6, /*!< 802.5 Token Ring */
PCAP_LINK_TYPE_ARCNET = 7, /*!< ARCnet */
PCAP_LINK_TYPE_SLIP = 8, /*!< SLIP */
PCAP_LINK_TYPE_PPP = 9, /*!< PPP */
PCAP_LINK_TYPE_FDDI = 10, /*!< FDDI */
PCAP_LINK_TYPE_ATM = 100, /*!< LLC/SNAP encapsulated ATM */
PCAP_LINK_TYPE_RAW_IP = 101, /*!< Raw IP, without link */
PCAP_LINK_TYPE_BSD_SLIP = 102, /*!< BSD/OS SLIP */
PCAP_LINK_TYPE_BSD_PPP = 103, /*!< BSD/OS PPP */
PCAP_LINK_TYPE_CISCO_HDLC = 104, /*!< Cisco HDLC */
PCAP_LINK_TYPE_802_11 = 105, /*!< 802.11 */
PCAP_LINK_TYPE_BSD_LOOPBACK = 108, /*!< OpenBSD loopback devices(with AF_value in network byte order) */
PCAP_LINK_TYPE_LOCAL_TALK = 114 /*!< LocalTalk */
} pcap_link_type_t;
/**
* @brief Pcap configuration Type Definition
*
*/
typedef struct {
FILE *fp; /* Pointer to a standard file handle */
pcap_link_type_t link_type; /* Pcap Link Type */
} pcap_config_t;
/**
* @brief Pcap Handle Type Definition
*
*/
typedef void *pcap_handle_t;
/**
* @brief Initialize a pcap session
*
* @param config configuration of creating pcap object
* @param handle pcap handle
* @return esp_err_t
* - ESP_OK on success
* - ESP_FAIL on error
*/
esp_err_t pcap_init(pcap_config_t *config, pcap_handle_t *handle);
/**
* @brief De-initialize a pcap session
*
* @param handle pcap handle
* @return esp_err_t
* - ESP_OK on success
* - ESP_FAIL on error
*/
esp_err_t pcap_deinit(pcap_handle_t handle);
/**
* @brief Capture one packet into pcap file
*
* @param handle pcap handle
* @param payload pointer of the captured data
* @param length length of captured data
* @param seconds second of capture time
* @param microseconds microsecond of capture time
* @return esp_err_t
* - ESP_OK on success
* - ESP_FAIL on error
*/
esp_err_t pcap_capture_packet(pcap_handle_t handle, void *payload, uint32_t length, uint32_t seconds, uint32_t microseconds);
#ifdef __cplusplus
}
#endif