Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,15 @@ on:
branches: [main]

jobs:
build-example:
host-test:
runs-on: ubuntu-latest
strategy:
matrix:
idf_version:
- "v5.4"
target:
- esp32s3
container:
image: espressif/idf:${{ matrix.idf_version }}
steps:
- uses: actions/checkout@v4
- name: Build example
working-directory: examples/basic
- name: Run host tests
run: |
. $IDF_PATH/export.sh
idf.py set-target ${{ matrix.target }}
idf.py build
cmake -S test/host -B test/host/build
cmake --build test/host/build
test/host/build/host_test

build-test:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
idf_component_register(
SRCS "src/cast_protocol.c"
SRCS "src/cast_chunker.c"
INCLUDE_DIRS "include"
REQUIRES ""
)
4 changes: 0 additions & 4 deletions examples/basic/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions examples/basic/main/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions examples/basic/main/idf_component.yml

This file was deleted.

11 changes: 0 additions & 11 deletions examples/basic/main/main.c

This file was deleted.

1 change: 1 addition & 0 deletions include/cast_itransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "esp_err.h"
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

/**
* @brief CASTトランスポートインタフェース構造体
Expand Down
43 changes: 31 additions & 12 deletions include/cast_protocol.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
#pragma once
/**
* @file cast_protocol.h
*/
#ifndef CAST_PROTOCOL_H
#define CAST_PROTOCOL_H

#include "esp_err.h"
#include <stdint.h>
#include <stddef.h>

#include "cast_itransport.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Initialize the cast protocol
*
* @return
* - ESP_OK on success
* - ESP_FAIL on failure
* @brief 送信する画像のフォーマット情報
*/
esp_err_t cast_protocol_init(void);
typedef enum {
CAST_FMT_JPEG = 0,
CAST_FMT_RGB565 = 1,
CAST_FMT_GRAYSCALE = 2,
CAST_FMT_NONE = 255 // 制御パケット用
} cast_image_format_t;

/**
* @brief Deinitialize the cast protocol
*
* @return
* - ESP_OK on success
* @brief 画像を複数チャンクに分割し送信する
* MTUは各無線通信規格によりある程度決められている
* @param data 送信するデータのポインタ
* @param len データ長
* @param fmt 画像のフォーマット
* @param transport 無線規格の振る舞い
* @return esp_err_t 送信成否(ESP_OK, ESP_FAILなど)
*/
esp_err_t cast_protocol_deinit(void);
esp_err_t cast_send_frame(
const uint8_t *data,
size_t len,
cast_image_format_t fmt,
cast_transport_interface_t *transport
);

#ifdef __cplusplus
}
#endif

#endif /* CAST_PROTOCOL_H */
65 changes: 65 additions & 0 deletions src/cast_chunker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @file cast_chunker.c
*/
#include "cast_protocol.h"
#include "cast_internal.h"

#include <stdlib.h>
#include <string.h>

esp_err_t cast_send_frame(
const uint8_t *data,
size_t len,
cast_image_format_t fmt,
cast_transport_interface_t *transport
) {
static uint16_t frame_counter = 0;
const size_t mtu = transport->get_mtu();
const size_t max_payload = mtu - CAST_PROTOCOL_OVERHEAD;

// 総チャンク数の計算(切り上げ)
uint16_t total_chunks = (len + max_payload - 1) / max_payload;

// 送信用の一時バッファ(MTUサイズ分確保)
uint8_t *packet_buf = (uint8_t *)malloc(mtu);
if (!packet_buf) return ESP_ERR_NO_MEM;

size_t sent_bytes = 0;

for (uint16_t i = 0; i < total_chunks; i++) {
// 残りデータ量と最大ペイロードの小さい方を今回のサイズにする
size_t current_payload_len = (len - sent_bytes > max_payload) ? max_payload : (len - sent_bytes);

// 1. ヘッダの組み立て
cast_header_t *header = (cast_header_t *)packet_buf;
header->magic = CAST_MAGIC_BYTE;
header->type = CAST_PKT_TYPE_DATA;
header->format = fmt;
header->frame_id = frame_counter;
header->chunk_index = i;
header->total_chunks = total_chunks;
header->payload_len = (uint16_t)current_payload_len;

// 2. データのコピー(ヘッダの直後へ)
memcpy(packet_buf + sizeof(cast_header_t), data + sent_bytes, current_payload_len);

// 3. トレーラ(CRC)の付与(今は0固定)
uint16_t crc = 0;
memcpy(packet_buf + sizeof(cast_header_t) + current_payload_len, &crc, 2);

// 4. 送信
size_t total_packet_len = sizeof(cast_header_t) + current_payload_len + 2;
esp_err_t err = transport->send(packet_buf, total_packet_len);

if (err != ESP_OK) {
free(packet_buf);
return err;
}

sent_bytes += current_payload_len;
}

free(packet_buf);
frame_counter++;
return ESP_OK;
}
11 changes: 1 addition & 10 deletions src/cast_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define CAST_INTERNAL_H

#include <stdint.h>
#include "cast_protocol.h"

#define CAST_MAGIC_BYTE 0xCA // CASTプロトコルの識別用

Expand All @@ -18,16 +19,6 @@ typedef enum {
CAST_PKT_TYPE_CTRL = 3 // 戦略変更命令
} cast_packet_type_t;

/**
* @brief 送信する画像のフォーマット情報
*/
typedef enum {
CAST_FMT_JPEG = 0,
CAST_FMT_RGB565 = 1,
CAST_FMT_GRAYSCALE = 2,
CAST_FMT_NONE = 255 // 制御パケット用
} cast_image_format_t;

/**
* @brief CASTプロトコル共通ヘッダ (11バイト)
* * [フィールド再利用(Overloading)の設計指針]
Expand Down
16 changes: 0 additions & 16 deletions src/cast_protocol.c

This file was deleted.

5 changes: 3 additions & 2 deletions test/host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ FetchContent_MakeAvailable(googletest)

# Component source (compiled as C)
add_library(component STATIC
../../src/cast_protocol.c
../../src/cast_chunker.c
)
target_include_directories(component PUBLIC
../../include
../../src
mocks
)

# Test executable
add_executable(host_test
test_cast_protocol.cpp
test_cast_chunker.cpp
)
target_link_libraries(host_test PRIVATE component GTest::gtest_main)

Expand Down
5 changes: 3 additions & 2 deletions test/host/mocks/esp_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

typedef int esp_err_t;

#define ESP_OK 0
#define ESP_FAIL (-1)
#define ESP_OK 0
#define ESP_FAIL (-1)
#define ESP_ERR_NO_MEM 0x101
Loading