Skip to content

Commit

Permalink
Merge pull request #17 from ut-issl/feature/space_packet
Browse files Browse the repository at this point in the history
新しいC2A間通信とSpacePacketの対応
  • Loading branch information
meltingrabbit authored Feb 2, 2022
2 parents b8e2eef + 60cf66b commit 27ad091
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 82 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ $ python GenerateC2ACode.py
"tlm_id_range" : ["0x00", "0x100"],
# 出力ファイルのエンコーディング
"output_file_encoding" : "utf-8",
# テレメパケットのヘッダ長.これをoffsetしたところから,C2A上のテレメのユーザー定義部
"header_len" : 13,
# MOBCか?(他のOBCのtlm/cmdを取りまとめるか?) 0/1
# 2nd OBCのコードを生成するときなどは 0 にする
# 0 の場合,以後のパラメタは無効
Expand All @@ -51,7 +49,8 @@ $ python GenerateC2ACode.py
"tlm_id_range" : ["0x90", "0xc0"],
# DBがあるディレクトリへのパス(絶対でも相対でもOK)
"db_path" : "../../c2a_sample_aobc/src/src_user/Settings/TlmCmd/DataBase/",
"tlm_max_contents_len" : 512,
# MOBC で保持するテレメの TLM ID の最大値(=テレメ種類数)
"max_tlm_num" : 256,
"driver_path" : "Aocs/",
"driver_type" : "AOBC_Driver",
"driver_name" : "aobc",
Expand All @@ -66,7 +65,8 @@ $ python GenerateC2ACode.py
"tlm_id_range" : ["0xc0", "0xf0"],
# DBがあるディレクトリへのパス(絶対でも相対でもOK)
"db_path" : ""../../c2a_sample_tobc/src/src_user/Settings/TlmCmd/DataBase/",
"tlm_max_contents_len" : 512,
# MOBC で保持するテレメの TLM ID の最大値(=テレメ種類数)
"max_tlm_num" : 256,
"driver_path" : "Thermal/",
"driver_type" : "TOBC_Driver",
"driver_name" : "tobc",
Expand Down
148 changes: 102 additions & 46 deletions my_mod/tlm_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,47 @@ def GenerateTlmBuffer(settings, other_obc_dbs):
obc_name = settings["other_obc_data"][i]["name"]
driver_type = settings["other_obc_data"][i]["driver_type"]
driver_name = settings["other_obc_data"][i]["driver_name"]
contents_len = settings["other_obc_data"][i]["tlm_max_contents_len"]
max_tlm_num = settings["other_obc_data"][i]["max_tlm_num"]

tlm_db = other_obc_dbs[obc_name]

body_c = ""
body_h = ""
tlmdef_body_h = ""

body_c += "static {_obc_name_upper}_Buffer {_obc_name_lower}_buffer_;\n"
body_c += "const {_obc_name_upper}_Buffer* const {_obc_name_lower}_buffer = &{_obc_name_lower}_buffer_;\n"
body_c += "\n"
body_h += "#define {_obc_name_upper}_TELEMETRY_BUFFE_SIZE (" + str(contents_len) + ")\n"
body_h += "\n"
body_h += "typedef struct\n"
body_h += "{{\n"
for tlm in tlm_db:
tlm_name = tlm["tlm_name"]
tlm_name_lower = tlm_name.lower()
body_h += " struct\n"
body_h += " {{\n"
body_h += " int size;\n"
body_h += " uint8_t buffer[{_obc_name_upper}_TELEMETRY_BUFFE_SIZE];\n"
body_h += " }} " + tlm_name_lower + ";\n"
body_c += (
"static DS_ERR_CODE {_obc_name_upper}_analyze_tlm_"
+ tlm_name_lower
+ "_(DS_StreamConfig* p_stream_config, "
+ "_(const CommonTlmPacket* packet, {_obc_name_upper}_TLM_CODE tlm_id, "
+ driver_type
+ "* "
+ driver_name
+ ");\n"
)

body_h += "}} {_obc_name_upper}_Buffer;\n"
body_c += "\n"
body_c += "static CommonTlmPacket {_obc_name_upper}_ctp_;\n"
body_c += "\n"

body_h += "typedef struct " + driver_type + " " + driver_type + ";\n"
body_h += "\n"
body_h += "#define {_obc_name_upper}_MAX_TLM_NUM (" + str(max_tlm_num) + ")\n"
body_h += "\n"
body_h += "typedef struct\n"
body_h += "{{\n"
body_h += " CommonTlmPacket packet; //!< 最新のテレメパケットを保持\n"
body_h += " uint8_t is_null_packet; //!< 一度でもテレメを受信しているか?(空配列が読み出されるのを防ぐため)\n"
body_h += "}} {_obc_name_upper}_TlmBufferElem;\n"
body_h += "\n"
body_h += "typedef struct\n"
body_h += "{{\n"
body_h += " {_obc_name_upper}_TlmBufferElem tlm[{_obc_name_upper}_MAX_TLM_NUM]; //!< TLM ID ごとに保持\n"
body_h += "}} {_obc_name_upper}_TlmBuffer;\n"
body_h += "\n"

tlmdef_body_h += "typedef struct\n"
tlmdef_body_h += "{{\n"
for tlm in tlm_db:
Expand Down Expand Up @@ -96,37 +102,55 @@ def GenerateTlmBuffer(settings, other_obc_dbs):

tlmdef_body_h += "}} {_obc_name_upper}_TlmData;\n"

body_h += "extern const {_obc_name_upper}_Buffer* const {_obc_name_lower}_buffer;\n"
body_h += "\n"
body_h += "void {_obc_name_upper}_buffer_init(void);\n"
body_h += (
"void {_obc_name_upper}_init_tlm_buffer(" + driver_type + "* " + driver_name + ");\n"
)
body_h += "\n"
body_h += (
"DS_ERR_CODE {_obc_name_upper}_buffer_tlm_contents(DS_StreamConfig* p_stream_config, "
"DS_ERR_CODE {_obc_name_upper}_buffer_tlm_packet(DS_StreamConfig* p_stream_config, "
+ driver_type
+ "* "
+ driver_name
+ ");\n"
)
body_h += "\n"
body_h += "// FIXME: TF_ACK になおす!\n"
body_h += (
"int {_obc_name_upper}_pick_up_tlm_buffer(const "
+ driver_type
+ "* "
+ driver_name
+ ", {_obc_name_upper}_TLM_CODE tlm_id, uint8_t* packet, int max_len);\n"
)

body_c += "\n"
body_c += "void {_obc_name_upper}_buffer_init(void)\n"
body_c += (
"void {_obc_name_upper}_init_tlm_buffer(" + driver_type + "* " + driver_name + ")\n"
)
body_c += "{{\n"
for tlm in tlm_db:
tlm_name = tlm["tlm_name"]
tlm_name_lower = tlm_name.lower()
body_c += " {_obc_name_lower}_buffer_." + tlm_name_lower + ".size = 0;\n"
body_c += " // packet などは,上位の driver の初期化で driver もろとも memset 0x00 されていると期待して,ここではしない\n"
body_c += " int i = 0;\n"
body_c += " for (i = 0; i < {_obc_name_upper}_MAX_TLM_NUM; ++i)\n"
body_c += " {{\n"
body_c += " " + driver_name + "->tlm_buffer.tlm[i].is_null_packet = 1;\n"
body_c += " }}\n"
body_c += "}}\n"
body_c += "\n"
body_c += (
"DS_ERR_CODE {_obc_name_upper}_buffer_tlm_contents(DS_StreamConfig* p_stream_config, "
"DS_ERR_CODE {_obc_name_upper}_buffer_tlm_packet(DS_StreamConfig* p_stream_config, "
+ driver_type
+ "* "
+ driver_name
+ ")\n"
)
body_c += "{{\n"

body_c += " uint8_t tlm_id = DS_C2AFMT_get_tlm_id(p_stream_config);\n"
body_c += " {_obc_name_upper}_TLM_CODE tlm_id;\n"
body_c += " DS_ERR_CODE ret;\n"
body_c += "\n"
body_c += " ret = DS_C2AFMT_get_ctp(p_stream_config, &{_obc_name_upper}_ctp_);\n"
body_c += " if (ret != DS_ERR_CODE_OK) return ret;\n"
body_c += "\n"
body_c += " tlm_id = ({_obc_name_upper}_TLM_CODE)CTP_get_id(&{_obc_name_upper}_ctp_);\n"
body_c += "\n"

body_c += " switch (tlm_id)\n"
Expand All @@ -139,7 +163,7 @@ def GenerateTlmBuffer(settings, other_obc_dbs):
body_c += (
" return {_obc_name_upper}_analyze_tlm_"
+ tlm_name_lower
+ "_(p_stream_config, "
+ "_(&{_obc_name_upper}_ctp_, tlm_id, "
+ driver_name
+ ");\n"
)
Expand Down Expand Up @@ -173,20 +197,18 @@ def GenerateTlmBuffer(settings, other_obc_dbs):
tlm_name = tlm["tlm_name"]
tlm_name_upper = tlm_name.upper()
tlm_name_lower = tlm_name.lower()

body_c += (
"static DS_ERR_CODE {_obc_name_upper}_analyze_tlm_"
+ tlm_name_lower
+ "_(DS_StreamConfig* p_stream_config, "
+ "_(const CommonTlmPacket* packet, {_obc_name_upper}_TLM_CODE tlm_id, "
+ driver_type
+ "* "
+ driver_name
+ ")\n"
)
body_c += "{{\n"
body_c += " uint32_t tlm_len = DS_ISSLFMT_get_tlm_length(p_stream_config);\n"
body_c += " const uint8_t* f = DSSC_get_rx_frame(p_stream_config) + DS_ISSLFMT_COMMON_HEADER_SIZE;\n"
body_c += " uint32_t contents_len = tlm_len - DS_C2AFMT_TCP_TLM_SECONDARY_HEADER_SIZE - 1; // FIXME: CCSDSは1起算?\n"
body_c += " const uint8_t* contents_pos = f + DS_C2AFMT_TCP_TLM_PRIMARY_HEADER_SIZE + DS_C2AFMT_TCP_TLM_SECONDARY_HEADER_SIZE;\n"
body_c += " const uint8_t* f = packet->packet;\n"
for k, v in conv_tpye_to_temp.items():
if k == "float":
body_c += " " + k + " " + v + " = 0.0f;\n"
Expand All @@ -195,18 +217,17 @@ def GenerateTlmBuffer(settings, other_obc_dbs):
else:
body_c += " " + k + " " + v + " = 0;\n"
body_c += "\n"
body_c += " // GSへのテレメ中継のためのバッファーへのコピー\n"
body_c += " if (contents_len > {_obc_name_upper}_TELEMETRY_BUFFE_SIZE) return DS_ERR_CODE_ERR;\n"
body_c += (
" memcpy({_obc_name_lower}_buffer_."
+ tlm_name_lower
+ ".buffer, contents_pos, (size_t)contents_len);\n"
)
body_c += " // GS へのテレメ中継のためのバッファーへのコピー\n"
body_c += (
" {_obc_name_lower}_buffer_." + tlm_name_lower + ".size = (int)contents_len;\n"
" CTP_copy_packet(&("
+ driver_name
+ "->tlm_buffer.tlm[tlm_id].packet), packet);\n"
)
body_c += " " + driver_name + "->tlm_buffer.tlm[tlm_id].is_null_packet = 0;\n"
body_c += " // TODO: CRC チェック\n"
body_c += "\n"
body_c += " // MOBC内部でテレメデータへアクセスしやすいようにするための構造体へのパース\n"

body_c += " // MOBC 内部でテレメデータへアクセスしやすいようにするための構造体へのパース\n"
last_var_type = ""
for j in range(DATA_START_ROW, len(tlm["data"])):
comment = tlm["data"][j][0]
Expand Down Expand Up @@ -252,7 +273,11 @@ def GenerateTlmBuffer(settings, other_obc_dbs):
+ ");\n"
)
body_c += (
" " + conv_tpye_to_temp[var_type] + " >>= " + str(7 - bit_pos) + ";\n"
" "
+ conv_tpye_to_temp[var_type]
+ " >>= "
+ str(conv_tpye_to_size[var_type] * 8 - bit_pos - bit_len)
+ ";\n"
)
body_c += (
" "
Expand Down Expand Up @@ -283,6 +308,34 @@ def GenerateTlmBuffer(settings, other_obc_dbs):
body_c += "}}\n"
body_c += "\n"

body_c += (
"int {_obc_name_upper}_pick_up_tlm_buffer(const "
+ driver_type
+ "* "
+ driver_name
+ ", {_obc_name_upper}_TLM_CODE tlm_id, uint8_t* packet, int max_len)\n"
)
body_c += "{{\n"
body_c += " const CommonTlmPacket* buffered_packet;\n"
body_c += " uint16_t packet_len;\n"
body_c += "\n"
body_c += " if (tlm_id >= {_obc_name_upper}_MAX_TLM_NUM) return TF_NOT_DEFINED;\n"
body_c += (
" if ("
+ driver_name
+ "->tlm_buffer.tlm[tlm_id].is_null_packet) return TF_NULL_PACKET;\n"
)
body_c += "\n"
body_c += " buffered_packet = &(" + driver_name + "->tlm_buffer.tlm[tlm_id].packet);\n"
body_c += " packet_len = CTP_get_packet_len(buffered_packet);\n"
body_c += "\n"
body_c += " if (packet_len > max_len) return TF_TOO_SHORT_LEN;\n"
body_c += "\n"
body_c += " memcpy(packet, &buffered_packet->packet, (size_t)packet_len);\n"
body_c += " return packet_len;\n"
body_c += "}}\n"
body_c += "\n"

output_file_path = (
settings["c2a_root_dir"]
+ r"src_user/Drivers/"
Expand Down Expand Up @@ -315,10 +368,10 @@ def OutputTlmBufferC_(file_path, name, body, settings):
* @brief テレメトリバッファー(テレメ中継)
* @note このコードは自動生成されています!
*/
#include "./{_obc_name_lower}_telemetry_definitions.h"
#include "./{_obc_name_lower}_telemetry_buffer.h"
#include <string.h> // for memcpy
#include "./{_obc_name_lower}.h"
#include <string.h>
"""[
1:
Expand Down Expand Up @@ -355,7 +408,10 @@ def OutputTlmBufferH_(file_path, name, body, settings):
#ifndef {_obc_name_upper}_TELEMETRY_BUFFER_H_
#define {_obc_name_upper}_TELEMETRY_BUFFER_H_
#include "./{_obc_name_lower}.h"
#include "./{_obc_name_lower}_telemetry_definitions.h"
#include <src_core/Drivers/Super/driver_super.h>
#include <src_core/TlmCmd/common_tlm_packet.h>
#include <src_core/TlmCmd/telemetry_frame.h>
"""[
1:
Expand Down
43 changes: 14 additions & 29 deletions my_mod/tlm_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ def GenerateTlmDef(settings, tlm_db, other_obc_dbs):
body_c = ""
body_h = ""

# "static int OBC_(unsigned char* contents, int max_len);"
# "static int OBC_(uint8_t* packet, int max_len);"
# " OBC_ID = 0x00,"
for tlm in tlm_db:
body_c += (
"static int Tlm_"
+ tlm["tlm_name"].upper()
+ "_(unsigned char* contents, int max_len);\n"
)
body_c += "static int Tlm_" + tlm["tlm_name"].upper() + "_(uint8_t* packet, int max_len);\n"
body_h += " Tlm_CODE_" + tlm["tlm_name"].upper() + " = " + tlm["tlm_id"] + ",\n"

if settings["is_main_obc"]:
Expand Down Expand Up @@ -66,7 +62,7 @@ def GenerateTlmDef(settings, tlm_db, other_obc_dbs):
if pos == "":
continue

pos = int(pos) - settings["header_len"]
pos = int(pos)
code = code.replace("@@", ",")
func_code += " "
if var_type == "int8_t":
Expand Down Expand Up @@ -96,14 +92,10 @@ def GenerateTlmDef(settings, tlm_db, other_obc_dbs):
else:
print("Error: Tlm DB Err at " + tlm["tlm_name"].upper(), file=sys.stderr)
sys.exit(1)
func_code += "(&contents[" + str(pos) + "], " + code + ");\n"
func_code += "(&packet[" + str(pos) + "], " + code + ");\n"

body_c += "\n"
body_c += (
"static int Tlm_"
+ tlm["tlm_name"].upper()
+ "_(unsigned char* contents, int max_len)\n"
)
body_c += "static int Tlm_" + tlm["tlm_name"].upper() + "_(uint8_t* packet, int max_len)\n"
body_c += "{\n"
for local_var in tlm["local_vars"]:
body_c += " " + local_var + "\n"
Expand Down Expand Up @@ -141,9 +133,7 @@ def GetTlmDefCOfOtherObcFunDef_(settings, tlm_db, other_obc_dbs):
temp_c += "// {_obc_name_upper} TLM\n"
for tlm in oter_obc_tlm_db:
temp_c += (
"static int Tlm_"
+ tlm["tlm_name"].upper()
+ "_(unsigned char* contents, int max_len);\n"
"static int Tlm_" + tlm["tlm_name"].upper() + "_(uint8_t* packet, int max_len);\n"
)

body_c += temp_c.format(
Expand Down Expand Up @@ -194,28 +184,23 @@ def GetTlmDefCOfOtherObcFunBody_(settings, tlm_db, other_obc_dbs):

obc_name = settings["other_obc_data"][i]["name"]
oter_obc_tlm_db = other_obc_dbs[obc_name]
driver_name = settings["other_obc_data"][i]["driver_name"]

temp_c = ""
for tlm in oter_obc_tlm_db:
tlm_name = tlm["tlm_name"]
tlm_name_upper = tlm_name.upper()
tlm_name_lower = tlm_name.lower()
# tlm_name_lower = tlm_name.lower()
temp_c += "\n"
temp_c += (
"static int Tlm_" + tlm_name_upper + "_(unsigned char* contents, int max_len)\n"
)
temp_c += "static int Tlm_" + tlm_name_upper + "_(uint8_t* packet, int max_len)\n"
temp_c += "{{\n"
temp_c += " int buffer_size = {_obc_name_lower}_buffer->" + tlm_name_lower + ".size;\n"
temp_c += "\n"
temp_c += " if (buffer_size > max_len) {{ return TF_TOO_SHORT_LEN; }}\n"
temp_c += "\n"
temp_c += (
" memcpy(contents, {_obc_name_lower}_buffer->"
+ tlm_name_lower
+ ".buffer, (size_t)buffer_size);\n"
" return {_obc_name_upper}_pick_up_tlm_buffer("
+ driver_name
+ ", {_obc_name_upper}_Tlm_CODE_"
+ tlm_name_upper
+ ", packet, max_len);\n"
)
temp_c += "\n"
temp_c += " return buffer_size;\n"
temp_c += "}}\n"

body_c += temp_c.format(
Expand Down
Loading

0 comments on commit 27ad091

Please sign in to comment.