2022-07-19 16:10:07 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/utils/net_snoop.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-03-10 01:20:48 +01:00
|
|
|
#include <assert.h>
|
2022-07-19 16:10:07 +02:00
|
|
|
#include <endian.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
2022-07-19 16:10:07 +02:00
|
|
|
#include <nuttx/net/snoop.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define SNOOP_VERSION_1 1
|
|
|
|
#define SNOOP_VERSION_2 2
|
|
|
|
|
|
|
|
/* microseconds since midnight, January 1st, 0 AD nominal Gregorian. */
|
|
|
|
|
|
|
|
#define SNOOP_EPOCH_USEC(tv) (((tv).tv_sec - 0x386d4380ll) * 1000000ll \
|
|
|
|
+ (tv).tv_usec + 0x00e03ab44a676000ll)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The availability of tools to capture, display and interpret packets
|
|
|
|
* traversing a network has proven extremely useful in debugging
|
|
|
|
* networking problems. The ability to capture packets and store them
|
|
|
|
* for later analysis allows one to de-couple the tasks of collecting
|
|
|
|
* information about a network problem and analysing that information.
|
|
|
|
*
|
|
|
|
* More info about snoop datalink type, please refer to
|
|
|
|
* https://www.rfc-editor.org/rfc/rfc1761.txt and
|
|
|
|
* https://fte.com/webhelpii/hsu/Content/Technical_Information/
|
|
|
|
* BT_Snoop_File_Format.htm
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The snoop packet capture file is an array of octets structured as
|
|
|
|
* follows:
|
|
|
|
*
|
|
|
|
* +------------------------+
|
|
|
|
* | |
|
|
|
|
* | File Header |
|
|
|
|
* | |
|
|
|
|
* +------------------------+
|
|
|
|
* | |
|
|
|
|
* | Packet Record |
|
|
|
|
* ~ Number 1 ~
|
|
|
|
* | |
|
|
|
|
* +------------------------+
|
|
|
|
* . .
|
|
|
|
* . .
|
|
|
|
* . .
|
|
|
|
* +------------------------+
|
|
|
|
* | |
|
|
|
|
* | Packet Record |
|
|
|
|
* ~ Number N ~
|
|
|
|
* | |
|
|
|
|
* +------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* snoop_file_header_s
|
|
|
|
*
|
|
|
|
* The structure of the File Header is as follows:
|
|
|
|
*
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | |
|
|
|
|
* + Identification Pattern +
|
|
|
|
* | |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Version Number |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Datalink Type |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
|
|
|
|
begin_packed_struct struct snoop_file_header_s
|
|
|
|
{
|
|
|
|
uint8_t magic[8]; /* Identification Pattern */
|
|
|
|
uint32_t version; /* Version Number */
|
|
|
|
uint32_t datalink; /* Datalink Type */
|
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
/* snoop_packet_header_s
|
|
|
|
*
|
|
|
|
* The structure of the packet record is as follows:
|
|
|
|
*
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Original Length |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Included Length |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Packet Record Length |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Cumulative Drops |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Timestamp Seconds |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Timestamp Microseconds |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | |
|
|
|
|
* . .
|
|
|
|
* . Packet Data .
|
|
|
|
* . .
|
|
|
|
* + +- - - - - - - -+
|
|
|
|
* | | Pad |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
|
|
|
|
begin_packed_struct struct snoop_packet_header_s
|
|
|
|
{
|
|
|
|
uint32_t orig_len; /* actual length of packet */
|
|
|
|
uint32_t incl_len; /* number of octets captured in file */
|
|
|
|
union
|
|
|
|
{
|
|
|
|
uint32_t flags; /* Packet Flags: 1 hci cmd , eg: btsnoop */
|
|
|
|
uint32_t rec_len; /* length of record */
|
|
|
|
};
|
|
|
|
uint32_t cum_drops; /* cumulative number of dropped packets */
|
|
|
|
union
|
|
|
|
{
|
|
|
|
uint64_t ts_usec; /* timestamp microseconds, eg: btsnoop */
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint32_t ts_sec; /* timestamp seconds */
|
|
|
|
uint32_t ts_usec; /* timestamp microseconds */
|
|
|
|
} ts;
|
|
|
|
};
|
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-09-04 12:26:59 +02:00
|
|
|
* Name: snoop_fill_packet_header
|
2022-07-19 16:10:07 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2022-09-04 12:26:59 +02:00
|
|
|
* This function fill snoop packet header info.
|
2022-07-19 16:10:07 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
static void snoop_fill_packet_header(FAR struct snoop_s *snoop,
|
|
|
|
uint32_t bytes, uint32_t drops,
|
|
|
|
uint32_t flags, FAR struct
|
|
|
|
snoop_packet_header_s *header)
|
2022-07-19 16:10:07 +02:00
|
|
|
{
|
2022-09-04 12:26:59 +02:00
|
|
|
struct timeval tv;
|
2022-07-19 16:10:07 +02:00
|
|
|
|
|
|
|
switch (snoop->datalink)
|
|
|
|
{
|
2022-09-04 12:26:59 +02:00
|
|
|
case SNOOP_DATALINK_HCI_UNENCAP:
|
|
|
|
case SNOOP_DATALINK_HCI_UART:
|
|
|
|
case SNOOP_DATALINK_HCI_BSCP:
|
|
|
|
case SNOOP_DATALINK_HCI_SERIAL:
|
2022-07-19 16:10:07 +02:00
|
|
|
gettimeofday(&tv, NULL);
|
2022-09-04 12:26:59 +02:00
|
|
|
header->ts_usec = htobe64(SNOOP_EPOCH_USEC(tv));
|
|
|
|
header->flags = htobe32(flags);
|
2022-07-19 16:10:07 +02:00
|
|
|
break;
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
case SNOOP_DATALINK_TYPE_TOKENBUS:
|
|
|
|
case SNOOP_DATALINK_TYPE_TOKERING:
|
|
|
|
case SNOOP_DATALINK_TYPE_METRONET:
|
|
|
|
case SNOOP_DATALINK_TYPE_ETHERNET:
|
|
|
|
case SNOOP_DATALINK_TYPE_HDLC:
|
|
|
|
case SNOOP_DATALINK_TYPE_CHARSYNC:
|
|
|
|
case SNOOP_DATALINK_TYPE_IBMC2C:
|
|
|
|
case SNOOP_DATALINK_TYPE_FDDI:
|
|
|
|
case SNOOP_DATALINK_TYPE_OTHER:
|
2022-07-19 16:10:07 +02:00
|
|
|
gettimeofday(&tv, NULL);
|
2022-09-04 12:26:59 +02:00
|
|
|
header->ts.ts_sec = htobe32(tv.tv_sec);
|
|
|
|
header->ts.ts_usec = htobe32(tv.tv_usec);
|
|
|
|
header->rec_len = htobe32(flags);
|
2022-07-19 16:10:07 +02:00
|
|
|
break;
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
default:
|
|
|
|
DEBUGASSERT(false);
|
2022-07-19 16:10:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
header->orig_len = htobe32(bytes);
|
|
|
|
header->incl_len = htobe32(bytes);
|
|
|
|
header->cum_drops = htobe32(drops);
|
|
|
|
}
|
2022-07-19 16:10:07 +02:00
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: snoop_flush
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function could flush snoop buf into file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int snoop_flush(FAR struct snoop_s *snoop)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (snoop->next == 0)
|
2022-07-19 16:10:07 +02:00
|
|
|
{
|
2022-09-04 12:26:59 +02:00
|
|
|
return 0;
|
2022-07-19 16:10:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = file_write(&snoop->filep, snoop->buf, snoop->next);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
snoop->next -= ret;
|
|
|
|
memmove(snoop->buf, snoop->buf + ret, snoop->next);
|
|
|
|
}
|
|
|
|
while (snoop->next > 0);
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
if (snoop->autosync)
|
|
|
|
{
|
|
|
|
ret = file_fsync(&snoop->filep);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: snoop_flush_lock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Snoop flush atomic
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int snoop_flush_lock(FAR struct snoop_s *snoop)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
nxmutex_lock(&snoop->mutex);
|
|
|
|
ret = snoop_flush(snoop);
|
|
|
|
nxmutex_unlock(&snoop->mutex);
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: snoop_flush_work
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Do snoop flush work.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void snoop_flush_work(FAR void *arg)
|
|
|
|
{
|
|
|
|
snoop_flush_lock((FAR struct snoop_s *)arg);
|
2022-07-19 16:10:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: snoop_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function open snoop file by datalink.
|
|
|
|
*
|
2022-09-04 12:26:59 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* snoop The snoop driver struct
|
|
|
|
* filename Snoop file name
|
|
|
|
* datalink Snoop datalink type, such as SNOOP_DATALINK_TYPE_XX
|
|
|
|
* autosync whether do file_sync when snoop_dump
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2022-07-19 16:10:07 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int snoop_open(FAR struct snoop_s *snoop, FAR const char *filename,
|
|
|
|
uint32_t datalink, bool autosync)
|
|
|
|
{
|
|
|
|
struct snoop_file_header_s header;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!snoop)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (datalink)
|
|
|
|
{
|
|
|
|
case SNOOP_DATALINK_TYPE_TOKENBUS:
|
|
|
|
case SNOOP_DATALINK_TYPE_TOKERING:
|
|
|
|
case SNOOP_DATALINK_TYPE_METRONET:
|
|
|
|
case SNOOP_DATALINK_TYPE_ETHERNET:
|
|
|
|
case SNOOP_DATALINK_TYPE_HDLC:
|
|
|
|
case SNOOP_DATALINK_TYPE_CHARSYNC:
|
|
|
|
case SNOOP_DATALINK_TYPE_IBMC2C:
|
|
|
|
case SNOOP_DATALINK_TYPE_FDDI:
|
|
|
|
case SNOOP_DATALINK_TYPE_OTHER:
|
|
|
|
{
|
2022-09-04 12:26:59 +02:00
|
|
|
static const uint8_t snoop_magic[] =
|
2022-07-19 16:10:07 +02:00
|
|
|
{
|
|
|
|
's', 'n', 'o', 'o', 'p', '\0', '\0', '\0'
|
|
|
|
};
|
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
memcpy(header.magic, snoop_magic, nitems(snoop_magic));
|
2022-07-19 16:10:07 +02:00
|
|
|
header.version = htobe32(SNOOP_VERSION_2);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
case SNOOP_DATALINK_HCI_UNENCAP:
|
|
|
|
case SNOOP_DATALINK_HCI_UART:
|
|
|
|
case SNOOP_DATALINK_HCI_BSCP:
|
|
|
|
case SNOOP_DATALINK_HCI_SERIAL:
|
|
|
|
{
|
2022-09-04 12:26:59 +02:00
|
|
|
static const uint8_t btsnoop_magic[] =
|
2022-07-19 16:10:07 +02:00
|
|
|
{
|
|
|
|
'b', 't', 's', 'n', 'o', 'o', 'p', '\0'
|
|
|
|
};
|
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
memcpy(header.magic, btsnoop_magic, nitems(btsnoop_magic));
|
2022-07-19 16:10:07 +02:00
|
|
|
header.version = htobe32(SNOOP_VERSION_1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:16:40 +02:00
|
|
|
ret = file_open(&snoop->filep, filename, O_RDWR | O_CREAT | O_CLOEXEC,
|
|
|
|
0666);
|
2022-07-19 16:10:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
snoop->datalink = datalink;
|
|
|
|
snoop->autosync = autosync;
|
2022-09-04 12:26:59 +02:00
|
|
|
snoop->next = 0;
|
2022-07-19 16:10:07 +02:00
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
header.datalink = htobe32(datalink);
|
2022-07-19 16:10:07 +02:00
|
|
|
ret = file_write(&snoop->filep, &header, sizeof(header));
|
|
|
|
if (ret != sizeof(header))
|
|
|
|
{
|
|
|
|
ret = ret < 0 ? ret : -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
nxmutex_init(&snoop->mutex);
|
2022-07-19 16:10:07 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
error:
|
2022-09-04 12:26:59 +02:00
|
|
|
snoop_close(snoop);
|
2022-07-19 16:10:07 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: snoop_dump
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function dump nbytes buf data into snoop file.
|
|
|
|
*
|
2022-09-04 12:26:59 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* snoop The snoop driver struct
|
|
|
|
* buf Snoop buffer
|
|
|
|
* nbytes Snoop buffer size
|
|
|
|
* drops cumulative number of dropped packets
|
|
|
|
* flags Packet Flags: 1 hci cmd , eg: btsnoop
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2022-07-19 16:10:07 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int snoop_dump(FAR struct snoop_s *snoop, FAR const void *buf,
|
|
|
|
uint32_t nbytes, uint32_t drops, uint32_t flags)
|
|
|
|
{
|
2022-09-04 12:26:59 +02:00
|
|
|
struct snoop_packet_header_s header;
|
|
|
|
irqstate_t irqflags;
|
|
|
|
int ret = 0;
|
2022-07-19 16:10:07 +02:00
|
|
|
|
|
|
|
if (!snoop)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
snoop_fill_packet_header(snoop, nbytes, drops, flags, &header);
|
|
|
|
|
|
|
|
irqflags = enter_critical_section();
|
|
|
|
if (up_interrupt_context())
|
2022-07-19 16:10:07 +02:00
|
|
|
{
|
2022-09-04 12:26:59 +02:00
|
|
|
if (sizeof(snoop->buf) - snoop->next <
|
|
|
|
nbytes + sizeof(struct snoop_packet_header_s))
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out_leave;
|
|
|
|
}
|
2022-07-19 16:10:07 +02:00
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
memcpy(snoop->buf + snoop->next, &header, sizeof(header));
|
|
|
|
snoop->next += sizeof(header);
|
|
|
|
memcpy(snoop->buf + snoop->next, buf, nbytes);
|
|
|
|
snoop->next += nbytes;
|
|
|
|
|
|
|
|
if (work_available(&snoop->work))
|
|
|
|
{
|
|
|
|
work_queue(HPWORK, &snoop->work, snoop_flush_work, snoop, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto out_leave;
|
|
|
|
}
|
|
|
|
else
|
2022-07-19 16:10:07 +02:00
|
|
|
{
|
2022-09-04 12:26:59 +02:00
|
|
|
nxmutex_lock(&snoop->mutex);
|
|
|
|
ret = snoop_flush(snoop);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = file_write(&snoop->filep, &header, sizeof(header));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
else if (ret != sizeof(header))
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = file_write(&snoop->filep, buf, nbytes);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
else if (ret != nbytes)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
2022-07-19 16:10:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
out_unlock:
|
|
|
|
nxmutex_unlock(&snoop->mutex);
|
|
|
|
out_leave:
|
|
|
|
leave_critical_section(irqflags);
|
|
|
|
return ret;
|
2022-07-19 16:10:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: snoop_sync
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function sync snoop buffer.
|
|
|
|
*
|
2022-09-04 12:26:59 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* snoop The snoop driver struct
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2022-07-19 16:10:07 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int snoop_sync(FAR struct snoop_s *snoop)
|
|
|
|
{
|
|
|
|
if (!snoop)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
return snoop_flush_lock(snoop);
|
2022-07-19 16:10:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: snoop_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function close snoop file.
|
|
|
|
*
|
2022-09-04 12:26:59 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* snoop The snoop driver struct
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2022-07-19 16:10:07 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int snoop_close(FAR struct snoop_s *snoop)
|
|
|
|
{
|
|
|
|
if (!snoop)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-09-04 12:26:59 +02:00
|
|
|
nxmutex_destroy(&snoop->mutex);
|
2022-07-19 16:10:07 +02:00
|
|
|
return file_close(&snoop->filep);
|
|
|
|
}
|